1 |
/* wptKeyserver.cpp - W32 Keyserver Access |
2 |
* Copyright (C) 2000-2006 Timo Schulz |
3 |
* Copyright (C) 2001 Marco Cunha |
4 |
* |
5 |
* This file is part of WinPT. |
6 |
* |
7 |
* WinPT is free software; you can redistribute it and/or |
8 |
* modify it under the terms of the GNU General Public License |
9 |
* as published by the Free Software Foundation; either version 2 |
10 |
* of the License, or (at your option) any later version. |
11 |
* |
12 |
* WinPT is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 |
* General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with WinPT; if not, write to the Free Software Foundation, |
19 |
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
*/ |
21 |
|
22 |
#ifdef HAVE_CONFIG_H |
23 |
#include <config.h> |
24 |
#endif |
25 |
|
26 |
#include <windows.h> |
27 |
#include <shlobj.h> |
28 |
#include <stdio.h> |
29 |
#include <sys/stat.h> |
30 |
#include <ctype.h> |
31 |
|
32 |
#include "wptKeyserver.h" |
33 |
#include "wptErrors.h" |
34 |
#include "wptTypes.h" |
35 |
#include "wptNLS.h" |
36 |
#include "wptW32API.h" |
37 |
#include "wptGPG.h" |
38 |
#include "wptRegistry.h" |
39 |
#include "wptUTF8.h" |
40 |
|
41 |
/* Map net_errno to a winsock error. */ |
42 |
#define net_errno ((int)WSAGetLastError ()) |
43 |
|
44 |
|
45 |
keyserver server[MAX_KEYSERVERS] = {0}; |
46 |
keyserver_proxy_s proxy = {0}; |
47 |
static const char *server_list[] = { |
48 |
"hkp://sks.keyserver.penguin.de", |
49 |
"hkp://subkeys.pgp.net", |
50 |
"ldap://keyserver.pgp.com", |
51 |
NULL |
52 |
}; |
53 |
|
54 |
|
55 |
static char hkp_errmsg[1024]; /* Holds the error message from the server */ |
56 |
static int hkp_err = 0; /* != 0 indicates an error occurred. */ |
57 |
|
58 |
/* Default keyserver and port. */ |
59 |
char *default_keyserver = NULL; |
60 |
WORD default_keyserver_port = 0; |
61 |
|
62 |
/* Default socket timeout. */ |
63 |
static int default_socket_timeout = 6; |
64 |
|
65 |
|
66 |
/* Wrapper for safe memory allocation. */ |
67 |
static char* |
68 |
safe_alloc (DWORD n) |
69 |
{ |
70 |
char *p; |
71 |
|
72 |
p = new char[n+1]; |
73 |
if (!p) |
74 |
BUG (0); |
75 |
memset (p, 0, n); |
76 |
return p; |
77 |
} |
78 |
|
79 |
/* Basic64 encode the input @inbuf to @outbuf. */ |
80 |
static void |
81 |
base64_encode (const char *inbuf, char *outbuf) |
82 |
{ |
83 |
char base64code[] = |
84 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
85 |
int index = 0, temp = 0, res = 0; |
86 |
int i = 0, inputlen = 0, len = 0; |
87 |
|
88 |
inputlen = strlen (inbuf); |
89 |
for (i = 0; i < inputlen; i++) { |
90 |
res = temp; |
91 |
res = (res << 8) | (inbuf[i] & 0xFF); |
92 |
switch (index++) { |
93 |
case 0: |
94 |
outbuf[len++] = base64code[res >> 2 & 0x3F]; |
95 |
res &= 0x3; |
96 |
break; |
97 |
case 1: |
98 |
outbuf[len++] = base64code[res >> 4 & 0x3F]; |
99 |
res &= 0xF; |
100 |
break; |
101 |
case 2: |
102 |
outbuf[len++] = base64code[res >> 6 & 0x3F]; |
103 |
outbuf[len++] = base64code[res & 0x3F]; |
104 |
res = index = 0; |
105 |
break; |
106 |
} |
107 |
temp = res; |
108 |
} |
109 |
|
110 |
if (index == 2) { |
111 |
outbuf[len++] = base64code[temp << 2 & 0x3F]; |
112 |
outbuf[len++] = '='; |
113 |
} |
114 |
else if (index == 1) { |
115 |
outbuf[len++] = base64code[temp << 4 & 0x3F]; |
116 |
outbuf[len++] = '='; |
117 |
outbuf[len++] = '='; |
118 |
} |
119 |
|
120 |
outbuf[len] = '\0'; |
121 |
} |
122 |
|
123 |
|
124 |
/* Check that the given buffer contains a valid keyserver URL |
125 |
and return the prefix length, 0 in case of an error. */ |
126 |
static int |
127 |
check_URL (const char *buf) |
128 |
{ |
129 |
const char *proto[] = { |
130 |
"ldap://", |
131 |
"http://", |
132 |
"finger://", |
133 |
"hkp://", |
134 |
NULL}; |
135 |
int i; |
136 |
|
137 |
if (strlen (buf) < 7) |
138 |
return 0; |
139 |
|
140 |
for (i=0; proto[i] != NULL; i++) { |
141 |
if (strstr (buf, proto[i])) |
142 |
return strlen (proto[i]); |
143 |
} |
144 |
return 0; |
145 |
} |
146 |
|
147 |
|
148 |
/* Skip the URL schema and return only the host part of it. */ |
149 |
static const char* |
150 |
skip_type_prefix (const char *hostname) |
151 |
{ |
152 |
int pos; |
153 |
|
154 |
if (!hostname) |
155 |
return hostname; |
156 |
|
157 |
pos = check_URL (hostname); |
158 |
if (pos > 0) |
159 |
hostname += pos; |
160 |
return hostname; |
161 |
} |
162 |
|
163 |
/* Parse the keyserver response and extract the human |
164 |
readable text passages. */ |
165 |
static int |
166 |
parse_keyserver_error (const char *resp, char *txt, size_t txtlen) |
167 |
{ |
168 |
char *p, *p2; |
169 |
|
170 |
/* If we find no 'Error' substring, we assume a success. */ |
171 |
if (!stristr (resp, "Error")) |
172 |
return -1; |
173 |
|
174 |
memset (txt, 0, txtlen); |
175 |
p = strstr (resp, "\r\n\r\n"); |
176 |
if (!p) |
177 |
return -1; |
178 |
resp += (p-resp); |
179 |
p = strstr (resp, "<h1>"); |
180 |
if (!p) |
181 |
return -1; |
182 |
resp += (p-resp)+strlen ("<h1>"); |
183 |
p = strstr (resp, "</h1>"); |
184 |
if (!p) |
185 |
return -1; |
186 |
resp += (p-resp)+strlen ("</h1>"); |
187 |
p2 = strstr (resp, "</body>"); |
188 |
if (p2 != NULL) |
189 |
memcpy (txt, resp, (p2-resp)); |
190 |
else { |
191 |
if (!strnicmp (resp, "<p>", 3)) |
192 |
resp += 3; |
193 |
while (resp && *resp == '\r' || *resp == '\n') |
194 |
resp++; |
195 |
memcpy (txt, resp, strlen (resp)); |
196 |
} |
197 |
return 0; |
198 |
} |
199 |
|
200 |
|
201 |
/* Check that the keyserver response indicates an OK. |
202 |
Return 0 on success. */ |
203 |
static int |
204 |
check_hkp_response (const char *resp, int recv) |
205 |
{ |
206 |
int ec; |
207 |
|
208 |
log_debug ("check_hkp_response: '%s'\r\n", resp); |
209 |
ec = recv ? WPTERR_WINSOCK_RECVKEY : WPTERR_WINSOCK_SENDKEY; |
210 |
if (!strstr (resp, "HTTP/1.0 200 OK") && |
211 |
!strstr (resp, "HTTP/1.1 200 OK") && |
212 |
!strstr (resp, "HTTP/1.0 500 OK") && |
213 |
!strstr (resp, "HTTP/1.1 500 OK")) |
214 |
return ec; /* http error */ |
215 |
|
216 |
if (!parse_keyserver_error (resp, hkp_errmsg, sizeof (hkp_errmsg)-2)) { |
217 |
if (!strlen (hkp_errmsg)) |
218 |
_snprintf (hkp_errmsg, sizeof (hkp_errmsg)-1, |
219 |
"Unknown keyserver error"); |
220 |
hkp_err = 1; |
221 |
return ec; |
222 |
} |
223 |
return 0; |
224 |
} |
225 |
|
226 |
|
227 |
/* Read a single line (\r\n) from a socket. |
228 |
Return 0 on success. */ |
229 |
static int |
230 |
sock_getline (int fd, char *buf, int buflen, int *nbytes) |
231 |
{ |
232 |
char ch; |
233 |
int nread = 0; |
234 |
|
235 |
if (nbytes) |
236 |
*nbytes = 0; |
237 |
*buf = 0; |
238 |
while (recv (fd, &ch, 1, 0) > 0) { |
239 |
*buf++ = ch; |
240 |
nread++; |
241 |
if (ch == '\r') |
242 |
continue; /* remove this char. */ |
243 |
if (ch == '\n' || nread == (buflen - 1)) { |
244 |
*buf = 0; |
245 |
if (nbytes) |
246 |
*nbytes = nread; |
247 |
return 0; |
248 |
} |
249 |
} |
250 |
|
251 |
return -1; |
252 |
} |
253 |
|
254 |
|
255 |
/* Perform a select() on the given fd to find out |
256 |
is there is data for reading. Wait at least @nsecs seconds. */ |
257 |
static int |
258 |
sock_select (int fd, int nsecs) |
259 |
{ |
260 |
FD_SET rfd; |
261 |
timeval tv = {nsecs, 0}; |
262 |
|
263 |
FD_ZERO (&rfd); |
264 |
FD_SET (fd, &rfd); |
265 |
if (select (fd + 1, &rfd, NULL, NULL, &tv) == SOCKET_ERROR) { |
266 |
log_debug ("sock_select: select() failed ec=%d.\r\n", net_errno); |
267 |
return SOCKET_ERROR; |
268 |
} |
269 |
if (FD_ISSET (fd, &rfd)) |
270 |
return 1; |
271 |
return 0; |
272 |
} |
273 |
|
274 |
|
275 |
/* Read from a socket @fd to buffer @buf with a fixed timeout |
276 |
of 10 seconds. The amount of bytes which were read are |
277 |
returned in @nbytes. |
278 |
Return value: 0 on success. */ |
279 |
static int |
280 |
sock_read (int fd, char *buf, int buflen, int *nbytes) |
281 |
{ |
282 |
DWORD nread; |
283 |
int nleft = buflen; |
284 |
int rc, n = 0; |
285 |
|
286 |
if (nbytes) |
287 |
*nbytes = 0; |
288 |
while (nleft > 0) { |
289 |
if (n >= default_socket_timeout) |
290 |
return WPTERR_WINSOCK_TIMEOUT; |
291 |
rc = sock_select (fd, 1); |
292 |
if (rc == SOCKET_ERROR) |
293 |
return rc; |
294 |
else if (!rc) |
295 |
n++; |
296 |
else { |
297 |
nread = recv (fd, buf, nleft, 0); |
298 |
if (nread == SOCKET_ERROR) { |
299 |
log_debug ("sock_read: recv() failed ec=%d.\r\n", net_errno); |
300 |
return SOCKET_ERROR; |
301 |
} |
302 |
else if (!nread) |
303 |
break; |
304 |
nleft -= nread; |
305 |
buf += nread; |
306 |
} |
307 |
} |
308 |
if (nbytes) |
309 |
*nbytes = buflen - nleft; |
310 |
|
311 |
return 0; |
312 |
} |
313 |
|
314 |
|
315 |
/* Helper to create a string from the gpgme data and |
316 |
then release the gpgme data object. */ |
317 |
char* |
318 |
data_release_and_get_mem (gpgme_data_t in, int *r_bufferlen) |
319 |
{ |
320 |
size_t n; |
321 |
char *buffer, *p; |
322 |
|
323 |
gpg_data_putc (in, '\0'); |
324 |
p = gpgme_data_release_and_get_mem (in, &n); |
325 |
buffer = m_strdup (p); |
326 |
if (r_bufferlen) |
327 |
*r_bufferlen = (int)n; |
328 |
gpgme_free (p); |
329 |
return buffer; |
330 |
} |
331 |
|
332 |
|
333 |
/* Read much data as possible from the socket @fd and |
334 |
return the data in @buffer. Caller must free data. |
335 |
Return value: 0 on success. */ |
336 |
int |
337 |
sock_read_ext (int fd, char **buffer, int *r_bufferlen) |
338 |
{ |
339 |
gpgme_data_t dh; |
340 |
char buf[1024]; |
341 |
size_t n=0; |
342 |
int nread, rc; |
343 |
|
344 |
if (gpgme_data_new (&dh)) |
345 |
BUG (0); |
346 |
while (n < 10) { |
347 |
rc = sock_select (fd, 1); |
348 |
if (rc == SOCKET_ERROR) { |
349 |
gpgme_data_release (dh); |
350 |
return rc; |
351 |
} |
352 |
else if (!rc) |
353 |
n++; |
354 |
else { |
355 |
nread = recv (fd, buf, sizeof (buf), 0); |
356 |
if (nread == SOCKET_ERROR) |
357 |
return SOCKET_ERROR; |
358 |
else if (!nread) |
359 |
break; |
360 |
gpgme_data_write (dh, buf, nread); |
361 |
} |
362 |
} |
363 |
|
364 |
*buffer = data_release_and_get_mem (dh, r_bufferlen); |
365 |
return 0; |
366 |
} |
367 |
|
368 |
|
369 |
/* Write the buffer @buf with the length @buflen to a socket @fd. */ |
370 |
static int |
371 |
sock_write (int fd, const char *buf, int buflen) |
372 |
{ |
373 |
DWORD nwritten; |
374 |
int nleft = buflen; |
375 |
|
376 |
while (nleft > 0) { |
377 |
nwritten = send (fd, buf, nleft, 0); |
378 |
if (nwritten == SOCKET_ERROR) { |
379 |
log_debug ("sock_write: send() failed ec=%d\r\n", net_errno); |
380 |
return SOCKET_ERROR; |
381 |
} |
382 |
nleft -= nwritten; |
383 |
buf += nwritten; |
384 |
} |
385 |
|
386 |
return 0; |
387 |
} |
388 |
|
389 |
|
390 |
/* Initialize the Winsock2 interface.*/ |
391 |
int |
392 |
wsock_init (void) |
393 |
{ |
394 |
WSADATA wsa; |
395 |
|
396 |
if (WSAStartup (0x202, &wsa)) { |
397 |
log_debug ("wsock_init: WSAStartup failed ec=%d\r\n", net_errno); |
398 |
return WPTERR_WINSOCK_INIT; |
399 |
} |
400 |
return 0; |
401 |
} |
402 |
|
403 |
|
404 |
/* Cleanup the Winsock2 interface. */ |
405 |
void |
406 |
wsock_end (void) |
407 |
{ |
408 |
char *p; |
409 |
int i; |
410 |
|
411 |
p = make_special_filename (CSIDL_APPDATA, "winpt\\keyserver.conf", NULL); |
412 |
kserver_save_conf (p); |
413 |
free_if_alloc (p); |
414 |
free_if_alloc (default_keyserver); |
415 |
for (i=0; i < MAX_KEYSERVERS; i++) { |
416 |
if (server[i].used && server[i].name != NULL) |
417 |
free_if_alloc (server[i].name); |
418 |
} |
419 |
kserver_proxy_release (&proxy); |
420 |
WSACleanup (); |
421 |
log_debug ("wsock_end: cleanup finished.\r\n"); |
422 |
} |
423 |
|
424 |
|
425 |
/* Return a string representation of a winsock error. */ |
426 |
const char* |
427 |
wsock_strerror (void) |
428 |
{ |
429 |
int ec = WSAGetLastError (); |
430 |
|
431 |
if (!ec) |
432 |
return ""; |
433 |
switch (ec) { |
434 |
case WSAENETDOWN: |
435 |
return _("Network unreachable"); |
436 |
|
437 |
case WSAEHOSTUNREACH: |
438 |
return _("Host unreachable"); |
439 |
|
440 |
case WSAHOST_NOT_FOUND: |
441 |
return _("Could not resolve host name"); |
442 |
|
443 |
case WSAECONNREFUSED: |
444 |
return _("Connection refused"); |
445 |
|
446 |
case WSAETIMEDOUT: |
447 |
case WSAECONNABORTED: |
448 |
return _("Connection timeout"); |
449 |
|
450 |
case WSAENETRESET: |
451 |
case WSAECONNRESET: |
452 |
return _("Connection resetted by peer"); |
453 |
|
454 |
case WSAESHUTDOWN: |
455 |
return _("Socket has been shutdown"); |
456 |
|
457 |
default: |
458 |
break; |
459 |
} |
460 |
|
461 |
return ""; |
462 |
} |
463 |
|
464 |
|
465 |
/* Set default socket timeout for all reading operations. */ |
466 |
void |
467 |
kserver_set_socket_timeout (int nsec) |
468 |
{ |
469 |
if (nsec < 0 || nsec > 3600) |
470 |
nsec = 0; |
471 |
default_socket_timeout = nsec; |
472 |
} |
473 |
|
474 |
|
475 |
/* Return the last keyserver error as a string. */ |
476 |
const char* |
477 |
kserver_strerror (void) |
478 |
{ |
479 |
if (hkp_err) |
480 |
return hkp_errmsg; |
481 |
return NULL; |
482 |
} |
483 |
|
484 |
|
485 |
/* Read a keyserver from the list at index @idx. |
486 |
Return value: keyserver name at the given position. */ |
487 |
const char* |
488 |
kserver_get_hostname (int idx, int type, WORD *port) |
489 |
{ |
490 |
if (type == -1) { |
491 |
*port = default_keyserver_port; |
492 |
return default_keyserver; |
493 |
} |
494 |
else if (!type && idx < DIM (server_list)) { |
495 |
/* XXX: handle non HKP servers. */ |
496 |
*port = HKP_PORT; |
497 |
return server_list[idx]; |
498 |
} |
499 |
return NULL; |
500 |
} |
501 |
|
502 |
|
503 |
/* Check if the computer is connected to the internet. |
504 |
Return 0 on success -1 otherwise. */ |
505 |
int |
506 |
kserver_check_inet_connection (void) |
507 |
{ |
508 |
int fd; |
509 |
|
510 |
if (!kserver_connect (default_keyserver, default_keyserver_port, &fd)) { |
511 |
closesocket (fd); |
512 |
return 0; |
513 |
} |
514 |
log_debug ("kserver_check_inet_connection: no inet connection.\r\n"); |
515 |
return -1; |
516 |
} |
517 |
|
518 |
|
519 |
/* If the request contains the keyid, it have to be |
520 |
always postfix with '0x'+keyid. This code checks |
521 |
if the keyid is a decimal value and if so if it contains |
522 |
the '0x'. If not it is inserted. */ |
523 |
const char* |
524 |
kserver_check_keyid (const char *keyid) |
525 |
{ |
526 |
static char id[22]; |
527 |
|
528 |
if (strstr (keyid, "@")) |
529 |
return keyid; /* email address */ |
530 |
if (strncmp (keyid, "0x", 2)) { |
531 |
memset (&id, 0, sizeof (id)); |
532 |
_snprintf (id, sizeof (id)-1, "0x%s", keyid); |
533 |
return id; |
534 |
} |
535 |
return keyid; |
536 |
} |
537 |
|
538 |
|
539 |
/* Update the keyserver proxy user. */ |
540 |
static void |
541 |
update_proxy_user (const char *proxy_user, const char *proxy_pass) |
542 |
{ |
543 |
char t[257]; /* user:pass = 127+1+127+1 = 257 */ |
544 |
int n = 0; |
545 |
|
546 |
n = 4*strlen (proxy_user) / 3 + 32 + strlen (proxy_pass) + 2; |
547 |
free_if_alloc (proxy.base64_user); |
548 |
proxy.base64_user = safe_alloc (n+1); |
549 |
_snprintf (t, sizeof (t)-1, "%s:%s", proxy_user, proxy_pass); |
550 |
base64_encode (t, proxy.base64_user); |
551 |
free_if_alloc (proxy.user); |
552 |
free_if_alloc (proxy.pass); |
553 |
proxy.user = m_strdup (proxy_user); |
554 |
proxy.pass = m_strdup (proxy_pass); |
555 |
} |
556 |
|
557 |
|
558 |
/* Get the port number from the given protocol. */ |
559 |
static int |
560 |
port_from_proto (int proto) |
561 |
{ |
562 |
switch (proto) { |
563 |
case KSPROTO_LDAP: return LDAP_PORT; |
564 |
case KSPROTO_FINGER: return FINGER_PORT; |
565 |
case KSPROTO_HTTP: return HKP_PORT; |
566 |
} |
567 |
return 0; |
568 |
} |
569 |
|
570 |
|
571 |
/* Get the port number from the given URL. */ |
572 |
static int |
573 |
proto_from_URL (const char *buf) |
574 |
{ |
575 |
if (strstr (buf, "ldap")) |
576 |
return KSPROTO_LDAP; |
577 |
else if (strstr (buf, "finger")) |
578 |
return KSPROTO_FINGER; |
579 |
return KSPROTO_HKP; |
580 |
} |
581 |
|
582 |
|
583 |
/* Set the default keyserver. |
584 |
If @hostname is NULL, use the predefined keyserver. */ |
585 |
void |
586 |
keyserver_set_default (const char *hostname, WORD port) |
587 |
{ |
588 |
if (!port) |
589 |
port = HKP_PORT; |
590 |
if (hostname != NULL) { |
591 |
free_if_alloc (default_keyserver); |
592 |
default_keyserver = m_strdup (hostname); |
593 |
default_keyserver_port = port; |
594 |
} |
595 |
if (!default_keyserver) { |
596 |
default_keyserver = m_strdup (DEF_HKP_KEYSERVER); |
597 |
default_keyserver_port = HKP_PORT; |
598 |
} |
599 |
server[0].name = m_strdup (default_keyserver); |
600 |
server[0].used = 1; |
601 |
server[0].port = port; |
602 |
server[0].proto = proto_from_URL (default_keyserver); |
603 |
} |
604 |
|
605 |
|
606 |
/* Skip all kind of whitespace chars in @str. */ |
607 |
static const char* |
608 |
skip_whitespace (const char *str) |
609 |
{ |
610 |
while (str && *str) { |
611 |
if (*str == ' ' || |
612 |
*str == '\t' || |
613 |
*str == '\n' || |
614 |
*str == '\r') { |
615 |
str++; |
616 |
continue; |
617 |
} |
618 |
break; |
619 |
} |
620 |
return str; |
621 |
} |
622 |
|
623 |
|
624 |
/* Save the keyserver config file in @conf. */ |
625 |
int |
626 |
kserver_save_conf (const char *conf) |
627 |
{ |
628 |
FILE *fp; |
629 |
int pos; |
630 |
|
631 |
fp = fopen (conf, "wb"); |
632 |
if (!fp) { |
633 |
msg_box (NULL, _("Could not save keyserver.conf file"), |
634 |
_("Keyserver"), MB_ERR); |
635 |
return -1; |
636 |
} |
637 |
|
638 |
fprintf (fp, "# do NOT manually modify this file, it will be " |
639 |
"generated automatically!!\r\n"); |
640 |
for (pos = 0; pos < MAX_KEYSERVERS; pos++) { |
641 |
if (!server[pos].used) |
642 |
continue; |
643 |
fprintf (fp, "%s:%d\r\n", server[pos].name, server[pos].port); |
644 |
} |
645 |
fclose (fp); |
646 |
return 0; |
647 |
} |
648 |
|
649 |
|
650 |
/* Load the keyserver config file @conf. */ |
651 |
int |
652 |
kserver_load_conf (const char *conf) |
653 |
{ |
654 |
FILE *fp; |
655 |
char buf[1024], *s, *p; |
656 |
char *user = NULL, *pass = NULL; |
657 |
int no_config=0, chk_pos=0; |
658 |
int pos; |
659 |
|
660 |
for (pos = 0; pos < MAX_KEYSERVERS; pos++) { |
661 |
server[pos].used = 0; |
662 |
server[pos].port = HKP_PORT; |
663 |
} |
664 |
|
665 |
fp = fopen (conf, "rb"); |
666 |
if (!fp) { |
667 |
for (pos = 0; server_list[pos]; pos++) { |
668 |
server[pos].used = 1; |
669 |
server[pos].name = m_strdup (server_list[pos]); |
670 |
server[pos].proto = proto_from_URL (server_list[pos]); |
671 |
} |
672 |
no_config=1; |
673 |
} |
674 |
get_reg_proxy_prefs (&proxy); |
675 |
if (user && pass) |
676 |
update_proxy_user (user, pass); |
677 |
else if (user && !pass || !user && pass) { |
678 |
msg_box (NULL, _("Invalid proxy configuration." |
679 |
"You need to set a user and a password " |
680 |
"to use proxy authentication!"), |
681 |
_("Proxy Error"), MB_ERR); |
682 |
} |
683 |
/* check if the host has a http:// prefix and skip it */ |
684 |
if (proxy.host && !strncmp (proxy.host, "http://", 7)) { |
685 |
char *host = m_strdup (proxy.host+7); |
686 |
free_if_alloc (proxy.host); |
687 |
proxy.host = host; |
688 |
} |
689 |
free_if_alloc (user); |
690 |
free_if_alloc (pass); |
691 |
|
692 |
pos = 0; |
693 |
while (fp && !feof (fp)) { |
694 |
s = fgets (buf, sizeof (buf)-1, fp); |
695 |
if (!s) |
696 |
break; |
697 |
if (strstr (buf, "\r\n")) |
698 |
buf[strlen (buf)-2] = '\0'; |
699 |
if (strstr (buf, "\n")) |
700 |
buf[strlen (buf)-1] = '\0'; |
701 |
s = (char *)skip_whitespace (buf); |
702 |
if (*s == '#' || strlen (s) < 7) |
703 |
continue; |
704 |
chk_pos = check_URL (s); |
705 |
if (!chk_pos) { |
706 |
msg_box (NULL, _("All entries of this file must have a valid prefix.\n" |
707 |
"Currently HKP/HTTP, LDAP and FINGER are supported.\n"), |
708 |
_("Keyserver Error"), MB_ERR); |
709 |
continue; |
710 |
} |
711 |
p = strchr (s+chk_pos, ':'); |
712 |
server[pos].used = 1; |
713 |
server[pos].proto = proto_from_URL (s); |
714 |
server[pos].port = port_from_proto (server[pos].proto); |
715 |
if (!p) |
716 |
server[pos].name = m_strdup (s); |
717 |
else { |
718 |
server[pos].port = atol (p+1); |
719 |
if (server[pos].port < 0 || server[pos].port > 65536) |
720 |
server[pos].port = HKP_PORT; |
721 |
server[pos].name = safe_alloc ((p-s)+2); |
722 |
memset (server[pos].name, 0, (p-s)+2); |
723 |
memcpy (server[pos].name, s, (p-s)); |
724 |
} |
725 |
pos++; |
726 |
if (pos > MAX_KEYSERVERS) { |
727 |
msg_box (NULL, _("The keyserver limit is exceeded"), |
728 |
_("Keyserver Warning"), MB_INFO); |
729 |
break; |
730 |
} |
731 |
} |
732 |
if (fp) |
733 |
fclose (fp); |
734 |
if (!pos) { |
735 |
/* only issue an error if the config file exist but |
736 |
it has no valid entries. */ |
737 |
keyserver_set_default (NULL, HKP_PORT); |
738 |
if (!no_config) |
739 |
return WPTERR_CONFIG_FILE; |
740 |
} |
741 |
|
742 |
return 0; |
743 |
} |
744 |
|
745 |
|
746 |
/* Connect to the keyserver @hostname (port @port). |
747 |
Return value: 0 on success */ |
748 |
int |
749 |
kserver_connect (const char *hostname, WORD port, int *conn_fd) |
750 |
{ |
751 |
struct hostent *hp; |
752 |
struct sockaddr_in sock; |
753 |
char host[128] = {0}; |
754 |
DWORD iaddr; |
755 |
int rc, fd; |
756 |
|
757 |
log_debug ("kserver_connect: %s:%d\r\n", hostname, port); |
758 |
|
759 |
if (!port) |
760 |
port = HKP_PORT; |
761 |
if (conn_fd) |
762 |
*conn_fd = 0; |
763 |
hostname = skip_type_prefix (hostname); |
764 |
|
765 |
if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) |
766 |
port = proxy.port; |
767 |
memset (&sock, 0, sizeof (sock)); |
768 |
sock.sin_family = AF_INET; |
769 |
sock.sin_port = htons (port); |
770 |
if (proxy.host) |
771 |
strncpy (host, proxy.host, 127); |
772 |
else |
773 |
strncpy (host, hostname, 127); |
774 |
|
775 |
if ((iaddr = inet_addr (host)) != INADDR_NONE) |
776 |
memcpy (&sock.sin_addr, &iaddr, sizeof (iaddr)); |
777 |
else if ((hp = gethostbyname (host))) { |
778 |
if (hp->h_addrtype != AF_INET || hp->h_length != 4) { |
779 |
log_debug ("gethostbyname: unknown address type.\r\n"); |
780 |
return WPTERR_WINSOCK_RESOLVE; |
781 |
} |
782 |
memcpy (&sock.sin_addr, hp->h_addr, hp->h_length); |
783 |
} |
784 |
else { |
785 |
log_debug ("gethostbyname: failed ec=%d.\r\n", net_errno); |
786 |
return WPTERR_WINSOCK_RESOLVE; |
787 |
} |
788 |
fd = socket (AF_INET, SOCK_STREAM, 0); |
789 |
if (fd == INVALID_SOCKET) |
790 |
return WPTERR_WINSOCK_SOCKET; |
791 |
rc = connect (fd, (struct sockaddr *) &sock, sizeof (sock)); |
792 |
if (rc == SOCKET_ERROR) { |
793 |
log_debug ("connect: failed ec=%d.\r\n", net_errno); |
794 |
closesocket (fd); |
795 |
return WPTERR_WINSOCK_CONNECT; |
796 |
} |
797 |
|
798 |
if (proxy.proto == PROXY_PROTO_SOCKS5) { |
799 |
rc = socks_handshake (&proxy, fd, hostname, port); |
800 |
if (rc) { |
801 |
closesocket (fd); |
802 |
return WPTERR_GENERAL; /* XXX: use better code. */ |
803 |
} |
804 |
} |
805 |
|
806 |
if (conn_fd) |
807 |
*conn_fd = fd; |
808 |
WSASetLastError (0); |
809 |
return 0; |
810 |
} |
811 |
|
812 |
|
813 |
/* Check if the URL needs to be encoded or not. */ |
814 |
static bool |
815 |
URL_must_encoded (const char *url) |
816 |
{ |
817 |
if (strchr (url, '.') || strchr (url, '@') || strchr (url, ' ')) |
818 |
return true; |
819 |
return false; |
820 |
} |
821 |
|
822 |
|
823 |
/* Perform URL encoding of the given data. */ |
824 |
static char* |
825 |
URL_encode (const char *url, DWORD ulen, DWORD *ret_nlen) |
826 |
{ |
827 |
gpgme_data_t hd; |
828 |
char numbuf[5], *p; |
829 |
size_t i; |
830 |
int nlen; |
831 |
|
832 |
if (gpgme_data_new (&hd)) |
833 |
BUG (0); |
834 |
for (i=0; i < ulen; i++) { |
835 |
if (isalnum (url[i]) || url[i] == '-') |
836 |
gpg_data_putc (hd, url[i]); |
837 |
else if (url[i] == ' ') |
838 |
gpg_data_putc (hd, '+'); |
839 |
else { |
840 |
sprintf (numbuf, "%%%02X", url[i]); |
841 |
gpgme_data_write (hd, numbuf, strlen (numbuf)); |
842 |
} |
843 |
} |
844 |
|
845 |
p = data_release_and_get_mem (hd, &nlen); |
846 |
if (ret_nlen) |
847 |
*ret_nlen = nlen; |
848 |
return p; |
849 |
} |
850 |
|
851 |
|
852 |
/* Format a request for the given keyid (send). */ |
853 |
static char* |
854 |
kserver_send_request (const char *hostname, WORD port, |
855 |
const char *pubkey, DWORD octets) |
856 |
{ |
857 |
const char *fmt; |
858 |
char *request; |
859 |
char *enc_pubkey; |
860 |
DWORD enc_octets; |
861 |
int reqlen; |
862 |
|
863 |
log_debug ("kserver_send_request: %s:%d\r\n", hostname, port); |
864 |
|
865 |
if (!port) |
866 |
port = HKP_PORT; |
867 |
enc_pubkey = URL_encode (pubkey, octets, &enc_octets); |
868 |
reqlen = 2*strlen (hostname) + 2*32/*port*/ + enc_octets + 32 /*len*/ + 1; |
869 |
|
870 |
if (proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
871 |
fmt = "POST http://%s:%d/pks/add HTTP/1.0\r\n" |
872 |
"Referer: \r\n" |
873 |
"User-Agent: WinPT/W32\r\n" |
874 |
"Host: %s:%d\r\n" |
875 |
"Proxy-Authorization: Basic %s\r\n" |
876 |
"Content-type: application/x-www-form-urlencoded\r\n" |
877 |
"Content-length: %d\r\n" |
878 |
"\r\n" |
879 |
"keytext=%s" |
880 |
"\r\n"; |
881 |
reqlen += strlen (fmt) + strlen (proxy.base64_user) + 1; |
882 |
request = safe_alloc (reqlen+1); |
883 |
_snprintf (request, reqlen, fmt, |
884 |
skip_type_prefix (hostname), port, hostname, port, |
885 |
proxy.base64_user, enc_octets+9, enc_pubkey); |
886 |
} |
887 |
else { |
888 |
fmt = "POST /pks/add HTTP/1.0\r\n" |
889 |
"Referer: \r\n" |
890 |
"User-Agent: WinPT/W32\r\n" |
891 |
"Host: %s:%d\r\n" |
892 |
"Content-type: application/x-www-form-urlencoded\r\n" |
893 |
"Content-length: %d\r\n" |
894 |
"\r\n" |
895 |
"keytext=%s" |
896 |
"\r\n"; |
897 |
reqlen += strlen (fmt)+1; |
898 |
request = safe_alloc (reqlen+1); |
899 |
_snprintf (request, reqlen, fmt, |
900 |
skip_type_prefix (hostname), port, |
901 |
enc_octets+9, enc_pubkey); |
902 |
} |
903 |
|
904 |
free_if_alloc (enc_pubkey); |
905 |
log_debug ("request:\r\n%s\r\n", request); |
906 |
return request; |
907 |
} |
908 |
|
909 |
|
910 |
/* Interface for receiving a public key. */ |
911 |
int |
912 |
kserver_recvkey (const char *hostname, WORD port, const char *keyid, |
913 |
char **r_key, int *r_keylen) |
914 |
{ |
915 |
const char *fmt; |
916 |
char *request = NULL; |
917 |
int conn_fd; |
918 |
int rc, reqlen; |
919 |
|
920 |
if (!port) |
921 |
port = HKP_PORT; |
922 |
hkp_err = 0; /* reset */ |
923 |
rc = kserver_connect (hostname, port, &conn_fd); |
924 |
if (rc) |
925 |
goto leave; |
926 |
|
927 |
reqlen = strlen (hostname)+32+strlen (keyid)+1; |
928 |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
929 |
fmt = "GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n" |
930 |
"Proxy-Authorization: Basic %s\r\n\r\n"; |
931 |
reqlen =+ strlen (fmt) + strlen (proxy.base64_user)+1; |
932 |
request = safe_alloc (reqlen+1); |
933 |
_snprintf (request, reqlen, fmt, skip_type_prefix (hostname), port, |
934 |
keyid, proxy.base64_user); |
935 |
} |
936 |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
937 |
fmt = "GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n"; |
938 |
reqlen += strlen (fmt)+1; |
939 |
request = safe_alloc (reqlen+1); |
940 |
_snprintf (request, reqlen, fmt, skip_type_prefix (hostname), |
941 |
port, keyid); |
942 |
} |
943 |
else { |
944 |
fmt = "GET /pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n"; |
945 |
reqlen += strlen (fmt)+1; |
946 |
request = safe_alloc (reqlen+1); |
947 |
_snprintf (request, reqlen, fmt, keyid); |
948 |
} |
949 |
|
950 |
log_debug ("request:\r\n%s\r\n", request); |
951 |
|
952 |
rc = sock_write (conn_fd, request, strlen (request)); |
953 |
if (rc == SOCKET_ERROR) { |
954 |
rc = WPTERR_WINSOCK_RECVKEY; |
955 |
goto leave; |
956 |
} |
957 |
|
958 |
rc = sock_read_ext (conn_fd, r_key, r_keylen); |
959 |
if (rc == SOCKET_ERROR) { |
960 |
rc = WPTERR_WINSOCK_RECVKEY; |
961 |
goto leave; |
962 |
} |
963 |
|
964 |
log_debug ("response:\r\n%s\r\n", *r_key); |
965 |
rc = check_hkp_response (*r_key, 1); |
966 |
if (rc) |
967 |
goto leave; |
968 |
|
969 |
WSASetLastError (0); |
970 |
|
971 |
leave: |
972 |
closesocket (conn_fd); |
973 |
free_if_alloc (request); |
974 |
return rc; |
975 |
} |
976 |
|
977 |
|
978 |
/* Interface to send a public key. */ |
979 |
int |
980 |
kserver_sendkey (const char *hostname, WORD port, const char *pubkey, int len ) |
981 |
{ |
982 |
char *request = NULL; |
983 |
char log[2048] = {0}; |
984 |
int conn_fd, n; |
985 |
int rc; |
986 |
|
987 |
hkp_err = 0; /* reset */ |
988 |
rc = kserver_connect (hostname, port, &conn_fd); |
989 |
if (rc) |
990 |
goto leave; |
991 |
|
992 |
request = kserver_send_request (hostname, port, pubkey, len); |
993 |
if (request == NULL) { |
994 |
rc = WPTERR_GENERAL; |
995 |
goto leave; |
996 |
} |
997 |
|
998 |
rc = sock_write (conn_fd, request, strlen (request)); |
999 |
if (rc == SOCKET_ERROR) { |
1000 |
rc = WPTERR_WINSOCK_SENDKEY; |
1001 |
goto leave; |
1002 |
} |
1003 |
|
1004 |
rc = sock_read (conn_fd, log, sizeof (log)-1, &n); |
1005 |
if (rc == SOCKET_ERROR) { |
1006 |
rc = WPTERR_WINSOCK_SENDKEY; |
1007 |
goto leave; |
1008 |
} |
1009 |
|
1010 |
log_debug ("kserver_sendkey: read %d bytes\r\n%s\r\n", n, log); |
1011 |
rc = check_hkp_response (log, 0); |
1012 |
if (rc) |
1013 |
goto leave; |
1014 |
|
1015 |
WSASetLastError (0); |
1016 |
|
1017 |
leave: |
1018 |
closesocket (conn_fd); |
1019 |
free_if_alloc (request); |
1020 |
return rc; |
1021 |
} |
1022 |
|
1023 |
|
1024 |
/* Check keyserver response. */ |
1025 |
static int |
1026 |
kserver_search_chkresp (int fd) |
1027 |
{ |
1028 |
char buf[128]; |
1029 |
int n=0; |
1030 |
|
1031 |
/* parse response 'HTTP/1.0 500 OK' */ |
1032 |
if (sock_getline (fd, buf, sizeof (buf)-1, &n)) |
1033 |
return WPTERR_KEYSERVER_NOTFOUND; |
1034 |
|
1035 |
log_debug ("kserver_search_chkpresp: %s\r\n", buf); |
1036 |
if (strncmp (buf, "HTTP/1.", 7)) |
1037 |
return WPTERR_KEYSERVER_NOTFOUND; |
1038 |
if (strncmp (buf+(8+1), "200", 3)) |
1039 |
return WPTERR_KEYSERVER_NOTFOUND; |
1040 |
return 0; |
1041 |
} |
1042 |
|
1043 |
|
1044 |
/* End the keyserver search procedure. */ |
1045 |
void |
1046 |
kserver_search_end (int conn_fd) |
1047 |
{ |
1048 |
log_debug ("kserver_search_end: fd=%d\r\n", conn_fd); |
1049 |
closesocket (conn_fd); |
1050 |
} |
1051 |
|
1052 |
|
1053 |
/* Extract the amount of keys from the info record. */ |
1054 |
static int |
1055 |
count_keys_in_response (char *buf) |
1056 |
{ |
1057 |
char *p; |
1058 |
int recno = 0; |
1059 |
int n = 0; |
1060 |
|
1061 |
/* info:1:4 */ |
1062 |
if (strncmp (buf, "info", 4)) |
1063 |
return 0; |
1064 |
p = strtok (buf, ":"); |
1065 |
while (p != NULL) { |
1066 |
recno++; |
1067 |
if (recno == 3) |
1068 |
n = atoi (p); |
1069 |
p = strtok (NULL, ":"); |
1070 |
} |
1071 |
return n; |
1072 |
} |
1073 |
|
1074 |
|
1075 |
/* Start the keyserver search. |
1076 |
Connect to host @hostname and port @port. |
1077 |
The pattern are given in @pattern. |
1078 |
The socket is returned in @conn_fd and @nkeys contains |
1079 |
the amount of keys which were found. */ |
1080 |
int |
1081 |
kserver_search_begin (const char *hostname, WORD port, |
1082 |
const char *pattern, int *conn_fd, int *nkeys) |
1083 |
{ |
1084 |
const char *fmt; |
1085 |
char *request = NULL; |
1086 |
char *enc_patt = NULL; |
1087 |
char status[128]; |
1088 |
int rc, sock_fd; |
1089 |
int reqlen; |
1090 |
|
1091 |
*conn_fd = 0; |
1092 |
|
1093 |
rc = kserver_connect (hostname, port, &sock_fd); |
1094 |
if (rc) |
1095 |
goto leave; |
1096 |
|
1097 |
enc_patt = URL_encode (pattern, strlen (pattern), NULL); |
1098 |
reqlen = strlen (enc_patt) + strlen (hostname) + 32 + 1; |
1099 |
|
1100 |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
1101 |
fmt = "GET http://%s:%d/pks/lookup?options=mr&op=index&search=%s HTTP/1.0\r\n" |
1102 |
"Proxy-Authorization: Basic %s\r\n\r\n"; |
1103 |
reqlen += strlen (proxy.base64_user) + strlen (fmt) + 1; |
1104 |
request = safe_alloc (reqlen+1); |
1105 |
_snprintf (request, reqlen, fmt, skip_type_prefix (hostname), port, |
1106 |
enc_patt, proxy.base64_user); |
1107 |
} |
1108 |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
1109 |
fmt = "GET http://%s:%d/pks/lookup?options=mr&op=index&search=%s HTTP/1.0\r\n\r\n"; |
1110 |
reqlen += strlen (fmt)+1; |
1111 |
request = safe_alloc (reqlen+1); |
1112 |
_snprintf (request, reqlen, skip_type_prefix (hostname), port, |
1113 |
enc_patt); |
1114 |
} |
1115 |
else { |
1116 |
fmt = "GET /pks/lookup?options=mr&op=index&search=%s HTTP/1.0\r\n\r\n"; |
1117 |
reqlen += strlen (fmt)+1; |
1118 |
request = safe_alloc (reqlen+1); |
1119 |
_snprintf (request, reqlen, fmt, enc_patt); |
1120 |
} |
1121 |
|
1122 |
log_debug ("kserver_search_begin:\r\n%s\r\n", request); |
1123 |
if (sock_write (sock_fd, request, strlen (request)) == SOCKET_ERROR) { |
1124 |
rc = WPTERR_GENERAL; |
1125 |
goto leave; |
1126 |
} |
1127 |
|
1128 |
rc = kserver_search_chkresp (sock_fd); |
1129 |
if (rc) { |
1130 |
closesocket (sock_fd); |
1131 |
sock_fd = 0; |
1132 |
goto leave; |
1133 |
} |
1134 |
|
1135 |
/* Skip all lines until we reach the "info:" record. */ |
1136 |
for (;;) { |
1137 |
if (sock_getline (sock_fd, status, sizeof (status)-1, &reqlen)) { |
1138 |
log_debug ("kserver_search_begin: retrieving status line failed.\r\n"); |
1139 |
closesocket (sock_fd); |
1140 |
sock_fd = 0; |
1141 |
rc = WPTERR_GENERAL; |
1142 |
break; |
1143 |
} |
1144 |
if (!strncmp (status, "info:", 5)) |
1145 |
break; |
1146 |
} |
1147 |
|
1148 |
if (!rc) |
1149 |
*nkeys = count_keys_in_response (status); |
1150 |
*conn_fd = sock_fd; |
1151 |
|
1152 |
leave: |
1153 |
free_if_alloc (request); |
1154 |
free_if_alloc (enc_patt); |
1155 |
return rc; |
1156 |
} |
1157 |
|
1158 |
|
1159 |
/* Parse a single pub record returned by the keyserver. */ |
1160 |
static void |
1161 |
parse_pub_record (keyserver_key_s *key, char *buf) |
1162 |
{ |
1163 |
enum pub_rec_t {ID=1, KEYID, ALGO, SIZE, CREATE, EXPIRE}; |
1164 |
char *p; |
1165 |
int recno = 0; |
1166 |
|
1167 |
/* pub:BF3DF9B4:17:1024:925411133:: */ |
1168 |
p = strtok (buf, ":"); |
1169 |
while (p != NULL) { |
1170 |
recno++; |
1171 |
switch (recno) { |
1172 |
case ID: |
1173 |
break; |
1174 |
|
1175 |
case KEYID: |
1176 |
free_if_alloc (key->keyid); |
1177 |
key->keyid = m_strdup (p); |
1178 |
break; |
1179 |
|
1180 |
case ALGO: |
1181 |
key->algo = atoi (p); |
1182 |
break; |
1183 |
|
1184 |
case SIZE: |
1185 |
key->bits = atoi (p); |
1186 |
break; |
1187 |
|
1188 |
case CREATE: |
1189 |
key->creation = strtoul (p, NULL, 10); |
1190 |
break; |
1191 |
|
1192 |
case EXPIRE: |
1193 |
key->expires = strtoul (p, NULL, 10); |
1194 |
break; |
1195 |
} |
1196 |
p = strtok (NULL, ":"); |
1197 |
} |
1198 |
|
1199 |
} |
1200 |
|
1201 |
|
1202 |
/* Parse a single user id returned by the keyserver. */ |
1203 |
static void |
1204 |
parse_uid_record (keyserver_key_s *key, char *buf) |
1205 |
{ |
1206 |
enum uid_rec_t {ID=1, UID, CREATE, EXPIRE}; |
1207 |
keyserver_uid_s *u, *n; |
1208 |
char *p, *raw; |
1209 |
int recno = 0; |
1210 |
|
1211 |
/* uid:Timo Schulz <[email protected]>:1138440360:: */ |
1212 |
u = new keyserver_uid_s; |
1213 |
memset (u, 0, sizeof *u); |
1214 |
|
1215 |
p = strtok (buf, ":"); |
1216 |
while (p != NULL) { |
1217 |
recno++; |
1218 |
|
1219 |
switch (recno) { |
1220 |
case ID: |
1221 |
break; |
1222 |
|
1223 |
case UID: |
1224 |
unhexify_buffer (p, &raw); |
1225 |
u->uid = utf8_to_native (raw); |
1226 |
free_if_alloc (raw); |
1227 |
break; |
1228 |
|
1229 |
case CREATE: |
1230 |
u->creation = strtoul (p, NULL, 10); |
1231 |
break; |
1232 |
|
1233 |
case EXPIRE: |
1234 |
u->expires = strtoul (p, NULL, 10); |
1235 |
break; |
1236 |
} |
1237 |
|
1238 |
p = strtok (NULL, ":"); |
1239 |
} |
1240 |
if (!key->uids) |
1241 |
key->uids = u; |
1242 |
else { |
1243 |
for (n = key->uids; n->next; n=n->next) |
1244 |
; |
1245 |
n->next = u; |
1246 |
} |
1247 |
} |
1248 |
|
1249 |
|
1250 |
/* Peek the next 3 bytes to check if the next line |
1251 |
would be a new "pub" record. In this case, return |
1252 |
-1 as an EOF indication, 0 otherwise. */ |
1253 |
static int |
1254 |
is_key_eof (int fd) |
1255 |
{ |
1256 |
char buf[64]; |
1257 |
int n; |
1258 |
|
1259 |
n = recv (fd, buf, 3, MSG_PEEK); |
1260 |
if (n < 3 || strncmp (buf, "pub", 3)) |
1261 |
return 0; |
1262 |
return -1; |
1263 |
} |
1264 |
|
1265 |
|
1266 |
/* Return the next key from the search response. */ |
1267 |
int |
1268 |
kserver_search_next (int fd, keyserver_key_s **r_key) |
1269 |
{ |
1270 |
keyserver_uid_s *uid, *u = NULL; |
1271 |
keyserver_key_s *key; |
1272 |
char buf[512]; |
1273 |
int n = 0; |
1274 |
long max = 0; |
1275 |
|
1276 |
log_debug ("keyserver_search_next:\r\n"); |
1277 |
*r_key = NULL; |
1278 |
|
1279 |
key = new keyserver_key_s; |
1280 |
memset (key, 0, sizeof *key); |
1281 |
for (;;) { |
1282 |
if (sock_getline (fd, buf, sizeof (buf)-1, &n)) |
1283 |
break; |
1284 |
/*log_debug ("record: '%s'\r\n", buf); */ |
1285 |
if (!strncmp (buf, "pub", 3)) |
1286 |
parse_pub_record (key, buf); |
1287 |
else |
1288 |
parse_uid_record (key, buf); |
1289 |
if (is_key_eof (fd)) |
1290 |
break; |
1291 |
} |
1292 |
|
1293 |
/* the uid with the newest self sig is used as the |
1294 |
primary user id. */ |
1295 |
for (uid = key->uids; uid; uid = uid->next) { |
1296 |
if (uid->creation > max) { |
1297 |
max = uid->creation; |
1298 |
u = uid; |
1299 |
} |
1300 |
} |
1301 |
|
1302 |
key->main_uid = u? u : key->uids; |
1303 |
*r_key = key; |
1304 |
return 0; |
1305 |
} |
1306 |
|
1307 |
|
1308 |
/* Release the keyserver key @key and all its elements. */ |
1309 |
void |
1310 |
kserver_release_key (keyserver_key_s *key) |
1311 |
{ |
1312 |
keyserver_uid_s *u; |
1313 |
|
1314 |
while (key->uids) { |
1315 |
u = key->uids->next; |
1316 |
free_if_alloc (key->uids->uid); |
1317 |
free_if_alloc (key->uids); |
1318 |
key->uids = u; |
1319 |
} |
1320 |
free_if_alloc (key->keyid); |
1321 |
free_if_alloc (key); |
1322 |
} |
1323 |
|
1324 |
|
1325 |
/* Release mbmers in the proxy context @ctx. */ |
1326 |
void |
1327 |
kserver_proxy_release (keyserver_proxy_t ctx) |
1328 |
{ |
1329 |
free_if_alloc (ctx->host); |
1330 |
free_if_alloc (ctx->pass); |
1331 |
free_if_alloc (ctx->user); |
1332 |
ctx->port = ctx->proto = 0; |
1333 |
} |
1334 |
|
1335 |
|
1336 |
/* Spawn a process given by @cmdl. */ |
1337 |
static int |
1338 |
spawn_application (char *cmdl) |
1339 |
{ |
1340 |
STARTUPINFO si; |
1341 |
PROCESS_INFORMATION pi; |
1342 |
int rc = 0; |
1343 |
|
1344 |
memset (&si, 0, sizeof (si)); |
1345 |
si.cb = sizeof (si); |
1346 |
si.dwFlags = STARTF_USESHOWWINDOW; |
1347 |
si.wShowWindow = SW_HIDE; |
1348 |
memset (&pi, 0, sizeof (pi)); |
1349 |
|
1350 |
if (!CreateProcess (NULL, cmdl, NULL, NULL, FALSE, 0, |
1351 |
NULL, NULL, &si, &pi)) { |
1352 |
log_box ("Keyserver Plugin", MB_ERR, "Could not spawn helper process"); |
1353 |
rc = -1; |
1354 |
} |
1355 |
|
1356 |
CloseHandle (pi.hThread); |
1357 |
WaitForSingleObject (pi.hProcess, INFINITE); |
1358 |
CloseHandle (pi.hProcess); |
1359 |
return rc; |
1360 |
} |
1361 |
|
1362 |
|
1363 |
static FILE* |
1364 |
do_spawn_ldap_helper (const char *host, const char *keyid) |
1365 |
{ |
1366 |
FILE *fp = NULL; |
1367 |
char *p, *sep; |
1368 |
char *ksprg; |
1369 |
char outf[256], inf[256]; |
1370 |
size_t n; |
1371 |
|
1372 |
p = get_gnupg_prog (); |
1373 |
n = strlen (p) + 1 + 128; |
1374 |
ksprg = safe_alloc (n+1); |
1375 |
if (!ksprg) |
1376 |
BUG (0); |
1377 |
sep = strrchr (p, '\\'); |
1378 |
if (sep != NULL) |
1379 |
p[(sep-p)] = 0; |
1380 |
|
1381 |
_snprintf (ksprg, n, "%s\\gpgkeys_ldap.exe", p); |
1382 |
free_if_alloc (p); |
1383 |
if (file_exist_check (ksprg)) { |
1384 |
log_box ("LDAP Keyserver Plugin", MB_ERR, |
1385 |
"%s: could not find LDAP keyserver module!", ksprg); |
1386 |
fp = NULL; |
1387 |
goto leave; |
1388 |
} |
1389 |
get_temp_name (outf, sizeof (outf)-1, keyid); |
1390 |
get_temp_name (inf, sizeof (inf)-1, NULL); |
1391 |
fp = fopen (inf, "w+b"); |
1392 |
if (!fp) { |
1393 |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", inf, |
1394 |
winpt_strerror (WPTERR_FILE_OPEN)); |
1395 |
goto leave; |
1396 |
} |
1397 |
fprintf (fp, |
1398 |
"VERSION 1\n" |
1399 |
"PROGRAM 1.4.3-cvs\n" |
1400 |
"SCHEME ldap\n" |
1401 |
"HOST %s\n" |
1402 |
"COMMAND GET\n" |
1403 |
"\n" |
1404 |
"%s\n", |
1405 |
host? skip_type_prefix (host): "64.94.85.200", keyid); |
1406 |
fclose (fp); |
1407 |
|
1408 |
p = safe_alloc (strlen (ksprg) + strlen (inf) + strlen (outf) + 32); |
1409 |
sprintf (p, "%s -o %s %s", ksprg, outf, inf); |
1410 |
if (spawn_application (p)) { |
1411 |
fp = NULL; |
1412 |
goto leave; |
1413 |
} |
1414 |
fp = fopen (outf, "rb"); |
1415 |
if (!fp) |
1416 |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf, |
1417 |
winpt_strerror (WPTERR_FILE_OPEN)); |
1418 |
|
1419 |
leave: |
1420 |
DeleteFile (inf); |
1421 |
DeleteFile (outf); |
1422 |
free_if_alloc (p); |
1423 |
free_if_alloc (ksprg); |
1424 |
return fp; |
1425 |
} |
1426 |
|
1427 |
/* Receive an key via LDAP from host @host with the keyid @keyid. |
1428 |
@key contains the key on success. */ |
1429 |
int |
1430 |
ldap_recvkey (const char *host, const char *keyid, |
1431 |
char **r_key, int *r_keylen) |
1432 |
{ |
1433 |
gpgme_data_t raw; |
1434 |
FILE *fp; |
1435 |
const char *s; |
1436 |
char buf[512]; |
1437 |
int start_key = 0, failed = 0; |
1438 |
int rc = 0; |
1439 |
|
1440 |
fp = do_spawn_ldap_helper (host, keyid); |
1441 |
if (!fp) |
1442 |
return WPTERR_GENERAL; |
1443 |
|
1444 |
if (gpgme_data_new (&raw)) |
1445 |
BUG (0); |
1446 |
while (!feof (fp)) { |
1447 |
s = fgets (buf, sizeof (buf)-1, fp); |
1448 |
if (!s) |
1449 |
break; |
1450 |
if (strstr (s, "KEY") && strstr (s, "FAILED")) { |
1451 |
failed = 1; |
1452 |
break; |
1453 |
} |
1454 |
if (!start_key && strstr (s, "KEY") && strstr (s, "BEGIN")) { |
1455 |
start_key = 1; |
1456 |
continue; |
1457 |
} |
1458 |
if (!start_key) |
1459 |
continue; |
1460 |
gpgme_data_write (raw, buf, strlen (buf)); |
1461 |
if (strstr (s, "KEY") && strstr (s, "END")) |
1462 |
break; |
1463 |
} |
1464 |
fclose (fp); |
1465 |
|
1466 |
if (failed) |
1467 |
rc = WPTERR_WINSOCK_RECVKEY; |
1468 |
*r_key = data_release_and_get_mem (raw, r_keylen); |
1469 |
return rc; |
1470 |
} |
1471 |
|
1472 |
|
1473 |
/* Receive an key via FINGER from host @host with the user @user. |
1474 |
On success @key contains the key. */ |
1475 |
int |
1476 |
finger_recvkey (const char *host, const char *user, |
1477 |
char **r_key, int *r_keylen) |
1478 |
{ |
1479 |
gpgme_data_t raw; |
1480 |
char buf[256]; |
1481 |
int fd, nread; |
1482 |
int start_key = 0; |
1483 |
int rc=0; |
1484 |
|
1485 |
rc = kserver_connect (host, FINGER_PORT, &fd); |
1486 |
if (rc) |
1487 |
return rc; |
1488 |
|
1489 |
sock_write (fd, user, strlen (user)); |
1490 |
sock_write (fd, "\r\n", 2); |
1491 |
|
1492 |
if (gpgme_data_new (&raw)) |
1493 |
BUG (0); |
1494 |
|
1495 |
for (;;) { |
1496 |
if (sock_getline (fd, buf, sizeof (buf), &nread)) |
1497 |
break; |
1498 |
strcat (buf, "\n"); |
1499 |
if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) { |
1500 |
gpgme_data_write (raw, buf, nread); |
1501 |
start_key = 1; |
1502 |
} |
1503 |
else if (strstr (buf, "END PGP PUBLIC KEY BLOCK" ) && start_key) { |
1504 |
gpgme_data_write (raw, buf, nread); |
1505 |
start_key--; |
1506 |
break; |
1507 |
} |
1508 |
else if (start_key) |
1509 |
gpgme_data_write (raw, buf, nread); |
1510 |
} |
1511 |
|
1512 |
closesocket (fd); |
1513 |
if (start_key != 0) |
1514 |
rc = WPTERR_WINSOCK_RECVKEY; |
1515 |
*r_key = data_release_and_get_mem (raw, r_keylen); |
1516 |
return rc; |
1517 |
} |
1518 |
|
1519 |
|
1520 |
/* Check if the given name @name is a valid hostname. */ |
1521 |
int |
1522 |
check_IP_or_hostname (const char *name) |
1523 |
{ |
1524 |
const char *not_allowed = "=!ยง$%&@#*~\\/}][{<>|,;:'"; |
1525 |
size_t i, j; |
1526 |
|
1527 |
for (i=0; i < strlen (name); i++) { |
1528 |
for (j =0; j < strlen (not_allowed); j++) { |
1529 |
if (name[i] == not_allowed[j]) |
1530 |
return -1; |
1531 |
} |
1532 |
} |
1533 |
return 0; |
1534 |
} |
1535 |
|
1536 |
|
1537 |
/* Split the URL @r_keyserver into the host and the port |
1538 |
part if possible. */ |
1539 |
gpgme_error_t |
1540 |
parse_keyserver_url (char **r_keyserver, unsigned short *r_port) |
1541 |
{ |
1542 |
char *p; |
1543 |
char *url = *r_keyserver; |
1544 |
int off = 0; |
1545 |
|
1546 |
/* no port is given so use the default port. */ |
1547 |
p = strrchr (url, ':'); |
1548 |
if (p == strchr (url, ':')) { |
1549 |
int port = port_from_proto (proto_from_URL (url)); |
1550 |
if (!port) |
1551 |
port = HKP_PORT; |
1552 |
*r_port = port; |
1553 |
return 0; |
1554 |
} |
1555 |
|
1556 |
if (url[(p-url)-1] == '/') /* remove / in .de/:11371 */ |
1557 |
off = 1; |
1558 |
|
1559 |
*r_keyserver = substr (url, 0, (p-url)-off); |
1560 |
*r_port = atoi (url+(p-url)+1); |
1561 |
free_if_alloc (url); |
1562 |
return 0; |
1563 |
} |