24 |
#endif |
#endif |
25 |
|
|
26 |
#include <windows.h> |
#include <windows.h> |
27 |
#include <windows.h> |
#include <shlobj.h> |
28 |
#include <stdio.h> |
#include <stdio.h> |
29 |
#include <sys/stat.h> |
#include <sys/stat.h> |
30 |
#include <ctype.h> |
#include <ctype.h> |
34 |
#include "wptTypes.h" |
#include "wptTypes.h" |
35 |
#include "wptNLS.h" |
#include "wptNLS.h" |
36 |
#include "wptW32API.h" |
#include "wptW32API.h" |
|
#include "wptVersion.h" |
|
37 |
#include "wptGPG.h" |
#include "wptGPG.h" |
38 |
#include "wptRegistry.h" |
#include "wptRegistry.h" |
39 |
|
|
40 |
|
#define net_errno ((int)WSAGetLastError ()) |
41 |
|
|
42 |
static char base64code[] = |
static char base64code[] = |
43 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
44 |
|
|
45 |
keyserver server[MAX_KEYSERVERS] = {0}; |
keyserver server[MAX_KEYSERVERS] = {0}; |
46 |
static keyserver_proxy_ctx proxy = {0}; |
static keyserver_proxy_ctx proxy = {0}; |
47 |
static const char * server_list[] = { |
static const char *server_list[] = { |
48 |
"hkp://wwwkeys.nl.pgp.net", |
"hkp://wwwkeys.nl.pgp.net", |
49 |
"hkp://wwwkeys.pl.pgp.net", |
"hkp://wwwkeys.pl.pgp.net", |
50 |
"hkp://wwwkeys.at.pgp.net", |
"hkp://wwwkeys.at.pgp.net", |
56 |
"hkp://wwwkeys.eu.pgp.net", |
"hkp://wwwkeys.eu.pgp.net", |
57 |
"hkp://wwwkeys.uk.pgp.net", |
"hkp://wwwkeys.uk.pgp.net", |
58 |
"hkp://wwwkeys.us.pgp.net", |
"hkp://wwwkeys.us.pgp.net", |
|
"hkp://gnv.us.ks.cryptnet.net", |
|
59 |
"hkp://subkeys.pgp.net", |
"hkp://subkeys.pgp.net", |
60 |
"ldap://keyserver.pgp.com", |
"ldap://keyserver.pgp.com", |
61 |
NULL |
NULL |
62 |
}; |
}; |
|
static char hkp_errmsg[1024]; |
|
|
static int hkp_err = 0; |
|
|
static u32 conf_timestamp = 0; |
|
|
char * default_keyserver = NULL; |
|
|
unsigned short default_keyserver_port = 0; |
|
63 |
|
|
64 |
|
|
65 |
|
static char hkp_errmsg[1024]; /* Holds the error message from the server */ |
66 |
|
static int hkp_err = 0; /* != 0 indicates an error occurred. */ |
67 |
|
static DWORD conf_timestamp = 0;/* timestamp of the configuration fiele. */ |
68 |
|
|
69 |
|
/* Default keyserver and port. */ |
70 |
|
char *default_keyserver = NULL; |
71 |
|
WORD default_keyserver_port = 0; |
72 |
|
|
73 |
|
|
74 |
|
/* Basic64 encode the input @inputstr to @outputstr. */ |
75 |
static void |
static void |
76 |
base64_encode( const char *inputstr, char *outputstr, int maxlen ) |
base64_encode (const char *inputstr, char *outputstr, int maxlen) |
77 |
{ |
{ |
78 |
int index = 0, temp = 0, res = 0, i = 0, inputlen = 0, len = 0; |
int index = 0, temp = 0, res = 0, i = 0, inputlen = 0, len = 0; |
79 |
|
|
80 |
inputlen = strlen (inputstr); /* fixme: check if len > maxlen */ |
/* XXX: check len < maxlen. */ |
81 |
|
inputlen = strlen (inputstr); |
82 |
for (i = 0; i <inputlen; i++) { |
for (i = 0; i <inputlen; i++) { |
83 |
res = temp; |
res = temp; |
84 |
res = (res << 8) | (inputstr[i] & 0xFF); |
res = (res << 8) | (inputstr[i] & 0xFF); |
85 |
switch (index++) { |
switch (index++) { |
86 |
case 0: outputstr[len++] = base64code[res >> 2 & 0x3F]; res &= 0x3; break; |
case 0: outputstr[len++] = base64code[res >> 2 & 0x3F]; res &= 0x3; break; |
87 |
case 1: outputstr[len++] = base64code[res >> 4 & 0x3F]; res &= 0xF; break; |
case 1: outputstr[len++] = base64code[res >> 4 & 0x3F]; res &= 0xF; break; |
88 |
case 2: outputstr[len++] = base64code[res >> 6 & 0x3F]; |
case 2: outputstr[len++] = base64code[res >> 6 & 0x3F]; |
89 |
outputstr[len++] = base64code[res & 0x3F]; res = index = 0; break; |
outputstr[len++] = base64code[res & 0x3F]; res = index = 0; break; |
90 |
} |
} |
91 |
temp = res; |
temp = res; |
92 |
} |
} |
93 |
|
|
94 |
switch( index ) { |
switch (index) { |
95 |
case 0: break; |
case 0: break; |
96 |
case 2: outputstr[len++] = base64code[temp << 2 & 0x3F]; |
case 2: outputstr[len++] = base64code[temp << 2 & 0x3F]; |
97 |
outputstr[len++] = '='; break; |
outputstr[len++] = '='; break; |
101 |
} |
} |
102 |
|
|
103 |
outputstr[len] = '\0'; |
outputstr[len] = '\0'; |
104 |
} /* base64_encode */ |
} |
105 |
|
|
106 |
|
|
107 |
/* Skip the URL schema and return only the host part of it. */ |
/* Skip the URL schema and return only the host part of it. */ |
108 |
static const char * |
static const char* |
109 |
skip_type_prefix (const char * hostname) |
skip_type_prefix (const char *hostname) |
110 |
{ |
{ |
111 |
if (hostname && !strncmp (hostname, "http://", 7)) |
if (hostname && !strncmp (hostname, "http://", 7)) |
112 |
hostname += 7; |
hostname += 7; |
124 |
Return 0 on success. */ |
Return 0 on success. */ |
125 |
static int |
static int |
126 |
check_hkp_response (const char *resp, int recv) |
check_hkp_response (const char *resp, int recv) |
127 |
{ |
{ |
128 |
|
char *p, *end; |
129 |
int ec, len; |
int ec, len; |
|
char *p, * end; |
|
130 |
|
|
131 |
ec = recv ? WPTERR_WINSOCK_RECVKEY : WPTERR_WINSOCK_SENDKEY; |
ec = recv ? WPTERR_WINSOCK_RECVKEY : WPTERR_WINSOCK_SENDKEY; |
132 |
if (!strstr (resp, "HTTP/1.0 200 OK") && |
if (!strstr (resp, "HTTP/1.0 200 OK") && |
139 |
if (p && strlen (p) < sizeof (hkp_errmsg)) { |
if (p && strlen (p) < sizeof (hkp_errmsg)) { |
140 |
end = strstr (p, "</p>"); |
end = strstr (p, "</p>"); |
141 |
len = end? (end - p + 1) : strlen (p); |
len = end? (end - p + 1) : strlen (p); |
142 |
|
memset (hkp_errmsg, 0, sizeof (hkp_errmsg)); |
143 |
strncpy (hkp_errmsg, p, len); |
strncpy (hkp_errmsg, p, len); |
144 |
hkp_err = 1; |
hkp_err = 1; |
145 |
} |
} |
163 |
while (recv (fd, &ch, 1, 0) > 0) { |
while (recv (fd, &ch, 1, 0) > 0) { |
164 |
*buf++ = ch; |
*buf++ = ch; |
165 |
count++; |
count++; |
166 |
|
/* XXX: remove the '\r' */ |
167 |
if (ch == '\n' || count == (buflen - 1)) { |
if (ch == '\n' || count == (buflen - 1)) { |
168 |
*buf = 0; |
*buf = 0; |
169 |
if (nbytes) |
if (nbytes) |
186 |
|
|
187 |
FD_ZERO (&rfd); |
FD_ZERO (&rfd); |
188 |
FD_SET (fd, &rfd); |
FD_SET (fd, &rfd); |
189 |
if (select (fd + 1, &rfd, NULL, NULL, &tv) == SOCKET_ERROR) |
if (select (fd + 1, &rfd, NULL, NULL, &tv) == SOCKET_ERROR) { |
190 |
|
log_debug ("sock_select: select() failed ec=%d.\r\n", net_errno); |
191 |
return SOCKET_ERROR; |
return SOCKET_ERROR; |
192 |
|
} |
193 |
if (FD_ISSET (fd, &rfd)) |
if (FD_ISSET (fd, &rfd)) |
194 |
return 1; |
return 1; |
195 |
return 0; |
return 0; |
196 |
} |
} |
197 |
|
|
198 |
|
|
199 |
/* Read from a socket with a fixed timeout of 10 seconds. |
/* Read from a socket @fd to buffer @buf with a fixed timeout |
200 |
|
of 10 seconds. The amount of bytes which were read are |
201 |
|
returned in @nbytes. |
202 |
Return value: 0 on success. */ |
Return value: 0 on success. */ |
203 |
static int |
static int |
204 |
sock_read (int fd, char *buf, int buflen, int *nbytes) |
sock_read (int fd, char *buf, int buflen, int *nbytes) |
212 |
return WPTERR_WINSOCK_TIMEOUT; |
return WPTERR_WINSOCK_TIMEOUT; |
213 |
rc = sock_select (fd, 1); |
rc = sock_select (fd, 1); |
214 |
if (rc == SOCKET_ERROR) |
if (rc == SOCKET_ERROR) |
215 |
return SOCKET_ERROR; |
return rc; |
216 |
else if( !rc ) |
else if (!rc) |
217 |
n++; |
n++; |
218 |
else { |
else { |
219 |
nread = recv (fd, buf, nleft, 0); |
nread = recv (fd, buf, nleft, 0); |
220 |
if (nread == SOCKET_ERROR) |
if (nread == SOCKET_ERROR) { |
221 |
|
log_debug ("sock_read: recv() failed ec=%d.\r\n", net_errno); |
222 |
return SOCKET_ERROR; |
return SOCKET_ERROR; |
223 |
|
} |
224 |
else if (!nread) |
else if (!nread) |
225 |
break; |
break; |
226 |
nleft -= nread; |
nleft -= nread; |
233 |
return 0; |
return 0; |
234 |
} |
} |
235 |
|
|
236 |
|
/* Read much data as possible from the socket @fd and |
237 |
|
return the data in @buffer. Caller must free data. |
238 |
|
Return value: 0 on success. */ |
239 |
|
int |
240 |
|
sock_read_ext (int fd, char **buffer, int *r_bufferlen) |
241 |
|
{ |
242 |
|
gpgme_data_t dh; |
243 |
|
char buf[1024], *p; |
244 |
|
size_t n=0; |
245 |
|
int nread, rc; |
246 |
|
|
247 |
/* Write @buf to a socket @fd. */ |
gpgme_data_new (&dh); |
248 |
|
while (n < 10) { |
249 |
|
rc = sock_select (fd, 1); |
250 |
|
if (rc == SOCKET_ERROR) { |
251 |
|
gpgme_data_release (dh); |
252 |
|
return rc; |
253 |
|
} |
254 |
|
else if (!rc) |
255 |
|
n++; |
256 |
|
else { |
257 |
|
nread = recv (fd, buf, sizeof (buf), 0); |
258 |
|
if (nread == SOCKET_ERROR) |
259 |
|
return SOCKET_ERROR; |
260 |
|
else if (!nread) |
261 |
|
break; |
262 |
|
gpgme_data_write (dh, buf, nread); |
263 |
|
} |
264 |
|
} |
265 |
|
gpg_data_putc (dh, '\0'); |
266 |
|
p = gpgme_data_release_and_get_mem (dh, &n); |
267 |
|
*buffer = m_strdup (p); |
268 |
|
if (r_bufferlen) |
269 |
|
*r_bufferlen = n; |
270 |
|
gpgme_free (p); |
271 |
|
return 0; |
272 |
|
} |
273 |
|
|
274 |
|
|
275 |
|
/* Write the buffer @buf with the length @buflen to a socket @fd. */ |
276 |
static int |
static int |
277 |
sock_write (int fd, const char *buf, int buflen) |
sock_write (int fd, const char *buf, int buflen) |
278 |
{ |
{ |
281 |
|
|
282 |
while (nleft > 0) { |
while (nleft > 0) { |
283 |
nwritten = send (fd, buf, nleft, 0); |
nwritten = send (fd, buf, nleft, 0); |
284 |
if (nwritten == SOCKET_ERROR) |
if (nwritten == SOCKET_ERROR) { |
285 |
|
log_debug ("sock_write: send() failed ec=%d\r\n", net_errno); |
286 |
return SOCKET_ERROR; |
return SOCKET_ERROR; |
287 |
|
} |
288 |
nleft -= nwritten; |
nleft -= nwritten; |
289 |
buf += nwritten; |
buf += nwritten; |
290 |
} |
} |
293 |
} |
} |
294 |
|
|
295 |
|
|
296 |
|
/* Set the default keyserver. */ |
297 |
void |
void |
298 |
set_default_kserver (void) |
set_default_kserver (void) |
299 |
{ |
{ |
300 |
char * p = get_reg_entry_keyserver ("Default"); |
char *p = get_reg_entry_keyserver ("Default"); |
301 |
free_if_alloc (default_keyserver); |
free_if_alloc (default_keyserver); |
302 |
default_keyserver = p && *p != ' ' ? p : m_strdup (DEF_HKP_KEYSERVER); |
default_keyserver = p && *p != ' ' ? p : m_strdup (DEF_HKP_KEYSERVER); |
303 |
p = get_reg_entry_keyserver ("Default_Port"); |
p = get_reg_entry_keyserver ("Default_Port"); |
304 |
if (p && *p) { |
if (p && *p) { |
305 |
default_keyserver_port = (u16)strtoul (p, NULL, 10); |
default_keyserver_port = (WORD)strtoul (p, NULL, 10); |
306 |
free_if_alloc (p); |
free_if_alloc (p); |
307 |
} |
} |
308 |
else |
else |
316 |
{ |
{ |
317 |
WSADATA wsa; |
WSADATA wsa; |
318 |
|
|
319 |
if (WSAStartup (0x202, &wsa)) |
if (WSAStartup (0x202, &wsa)) { |
320 |
|
log_debug ("wsock_init: WSAStartup failed ec=%d\r\n", net_errno); |
321 |
return WPTERR_WINSOCK_INIT; |
return WPTERR_WINSOCK_INIT; |
322 |
|
} |
323 |
set_default_kserver (); |
set_default_kserver (); |
324 |
return 0; |
return 0; |
325 |
} |
} |
329 |
void |
void |
330 |
wsock_end (void) |
wsock_end (void) |
331 |
{ |
{ |
332 |
|
char *p; |
333 |
int i; |
int i; |
334 |
|
|
335 |
|
p = make_special_filename (CSIDL_APPDATA, "winpt\\keyserver.conf", NULL); |
336 |
|
kserver_save_conf (p); |
337 |
|
free_if_alloc (p); |
338 |
free_if_alloc (default_keyserver); |
free_if_alloc (default_keyserver); |
|
default_keyserver = NULL; |
|
339 |
for (i=0; i < MAX_KEYSERVERS; i++) { |
for (i=0; i < MAX_KEYSERVERS; i++) { |
340 |
if (server[i].used) |
if (server[i].used) |
341 |
free_if_alloc (server[i].name); |
free_if_alloc (server[i].name); |
352 |
int ec = WSAGetLastError (); |
int ec = WSAGetLastError (); |
353 |
|
|
354 |
switch (ec) { |
switch (ec) { |
355 |
case WSAENETDOWN: |
case WSAENETDOWN: |
356 |
return _("The network subsystem has failed"); |
return _("The network subsystem has failed"); |
357 |
case WSAHOST_NOT_FOUND: |
case WSAHOST_NOT_FOUND: |
358 |
return _("Authoritative Answer Host not found"); |
return _("Authoritative Answer Host not found"); |
359 |
case WSAETIMEDOUT: |
case WSAETIMEDOUT: |
360 |
return _("The connection has been dropped because of a network failure"); |
return _("The connection has been dropped because of a network failure"); |
361 |
default: |
default: |
362 |
_snprintf (buf, sizeof (buf) -1, _("Unknown Winsock error ec=%d"),ec); |
_snprintf (buf, sizeof (buf) -1, _("Unknown Winsock error ec=%d"),ec); |
363 |
return buf; |
return buf; |
364 |
} |
} |
379 |
/* Read a keyserver from the list at index @idx. |
/* Read a keyserver from the list at index @idx. |
380 |
Return value: keyserver name at the given position. */ |
Return value: keyserver name at the given position. */ |
381 |
const char* |
const char* |
382 |
kserver_get_hostname (int idx, int type, u16 *port) |
kserver_get_hostname (int idx, int type, WORD *port) |
383 |
{ |
{ |
384 |
if (type == -1) { |
if (type == -1) { |
385 |
*port = default_keyserver_port; |
*port = default_keyserver_port; |
408 |
} |
} |
409 |
|
|
410 |
|
|
411 |
/* |
/* If the request contains the keyid, it have to be |
412 |
* If the request contains the keyid, it have to be |
always postfix with '0x'+keyid. This code checks |
413 |
* always postfix with '0x'+keyid. This code checks |
if the keyid is a decimal value and if so if it contains |
414 |
* if the keyid is a decimal value and if so if it contains |
the '0x'. If not it is inserted. */ |
|
* the '0x'. If not it is inserted. |
|
|
*/ |
|
415 |
const char* |
const char* |
416 |
kserver_check_keyid (const char *keyid) |
kserver_check_keyid (const char *keyid) |
417 |
{ |
{ |
418 |
static char id[20]; |
static char id[21]; |
419 |
|
|
420 |
if (strstr (keyid, "@")) |
if (strstr (keyid, "@")) |
421 |
return keyid; /* email address */ |
return keyid; /* email address */ |
425 |
return id; |
return id; |
426 |
} |
} |
427 |
return keyid; |
return keyid; |
428 |
} /* kserver_check_keyid */ |
} |
429 |
|
|
430 |
|
|
431 |
|
/* Update the keyserver proxy user. */ |
432 |
static void |
static void |
433 |
kserver_update_proxyuser (const char *proxy_user, const char *proxy_pass) |
kserver_update_proxyuser (const char *proxy_user, const char *proxy_pass) |
434 |
{ |
{ |
446 |
free_if_alloc (proxy.pass); |
free_if_alloc (proxy.pass); |
447 |
proxy.user = m_strdup (proxy_user); |
proxy.user = m_strdup (proxy_user); |
448 |
proxy.pass = m_strdup (proxy_pass); |
proxy.pass = m_strdup (proxy_pass); |
449 |
} /* kserver_update_proxyuser */ |
} |
450 |
|
|
451 |
|
|
452 |
/* Check that the given buffer contains a valid keyserver URL. */ |
/* Check that the given buffer contains a valid keyserver URL. */ |
490 |
|
|
491 |
|
|
492 |
void |
void |
493 |
keyserver_set_default (const char * hostname, u16 port) |
keyserver_set_default (const char *hostname, WORD port) |
494 |
{ |
{ |
495 |
if (hostname) { |
if (hostname) { |
496 |
free_if_alloc (default_keyserver); |
free_if_alloc (default_keyserver); |
503 |
server[0].used = 1; |
server[0].used = 1; |
504 |
server[0].port = port; |
server[0].port = port; |
505 |
server[0].proto = proto_from_URL (default_keyserver); |
server[0].proto = proto_from_URL (default_keyserver); |
506 |
} /* keyserver_set_default */ |
} |
507 |
|
|
508 |
|
|
509 |
static const char * |
/* Skip all kind of whitespace chars in @str. */ |
510 |
skip_whitespace (const char * str) |
static const char* |
511 |
|
skip_whitespace (const char *str) |
512 |
{ |
{ |
513 |
while (str && *str) |
while (str && *str) { |
|
{ |
|
514 |
if (*str == ' ' || |
if (*str == ' ' || |
515 |
*str == '\t' || |
*str == '\t' || |
516 |
*str == '\n' || |
*str == '\n' || |
517 |
*str == '\r') |
*str == '\r') { |
|
{ |
|
518 |
str++; |
str++; |
519 |
continue; |
continue; |
520 |
} |
} |
524 |
} |
} |
525 |
|
|
526 |
|
|
527 |
|
/* Save the keyserver config file in @conf. */ |
528 |
|
int |
529 |
|
kserver_save_conf (const char *conf) |
530 |
|
{ |
531 |
|
FILE *fp; |
532 |
|
int pos; |
533 |
|
|
534 |
|
fp = fopen (conf, "wb"); |
535 |
|
if (!fp) { |
536 |
|
msg_box (NULL, _("Could not save keyserver.conf file"), |
537 |
|
_("Keyserver"), MB_ERR); |
538 |
|
return -1; |
539 |
|
} |
540 |
|
|
541 |
|
fprintf (fp, "# do NOT manually modify this file, it will be generated automatically!!\r\n"); |
542 |
|
for (pos = 0; pos < MAX_KEYSERVERS; pos++) { |
543 |
|
if (!server[pos].used) |
544 |
|
continue; |
545 |
|
fprintf (fp, "%s:%d\r\n", server[pos].name, server[pos].port); |
546 |
|
} |
547 |
|
fclose (fp); |
548 |
|
return 0; |
549 |
|
} |
550 |
|
|
551 |
|
|
552 |
|
/* Load the keyserver config file @conf. */ |
553 |
int |
int |
554 |
kserver_load_conf (const char * conf) |
kserver_load_conf (const char *conf) |
555 |
{ |
{ |
556 |
struct stat statbuf; |
struct stat statbuf; |
557 |
FILE *fp; |
FILE *fp; |
558 |
char buf[1024], * s, * p; |
char buf[1024], * s, * p; |
559 |
char *user = NULL, *pass = NULL; |
char *user = NULL, *pass = NULL; |
|
int pos, proxy_auth = 0; |
|
560 |
int no_config=0, chk_pos=0; |
int no_config=0, chk_pos=0; |
561 |
|
int pos; |
562 |
|
|
563 |
for (pos = 0; pos < MAX_KEYSERVERS; pos++) { |
for (pos = 0; pos < MAX_KEYSERVERS; pos++) { |
564 |
server[pos].used = 0; |
server[pos].used = 0; |
578 |
if (user && pass) |
if (user && pass) |
579 |
kserver_update_proxyuser (user, pass); |
kserver_update_proxyuser (user, pass); |
580 |
else if (user && !pass || !user && pass) { |
else if (user && !pass || !user && pass) { |
581 |
msg_box( NULL, _("Invalid proxy configuration." |
msg_box (NULL, _("Invalid proxy configuration." |
582 |
"You need to set a user and a password" |
"You need to set a user and a password" |
583 |
"to use proxy authentication!"), _("Proxy Error"), MB_ERR ); |
"to use proxy authentication!"), |
584 |
|
_("Proxy Error"), MB_ERR); |
585 |
} |
} |
586 |
/* check if the host has a http:// prefix and skip it */ |
/* check if the host has a http:// prefix and skip it */ |
587 |
if( proxy.host && !strncmp( proxy.host, "http://", 7 ) ) { |
if (proxy.host && !strncmp (proxy.host, "http://", 7)) { |
588 |
const char *s = proxy.host+7; |
char *host = m_strdup (proxy.host+7); |
589 |
char * p = m_strdup (s); |
if (!host) |
|
if (!p) |
|
590 |
BUG (0); |
BUG (0); |
591 |
free_if_alloc (proxy.host); |
free_if_alloc (proxy.host); |
592 |
proxy.host = p; |
proxy.host = host; |
593 |
} |
} |
594 |
free_if_alloc (user); |
free_if_alloc (user); |
595 |
free_if_alloc (pass); |
free_if_alloc (pass); |
596 |
|
|
597 |
pos = 0; |
pos = 0; |
598 |
while( fp && !feof( fp ) ) { |
while (fp && !feof (fp)) { |
599 |
s = fgets (buf, sizeof (buf)-1, fp); |
s = fgets (buf, sizeof (buf)-1, fp); |
600 |
if (!s) |
if (!s) |
601 |
break; |
break; |
632 |
memcpy (server[pos].name, s, (p-s)); |
memcpy (server[pos].name, s, (p-s)); |
633 |
} |
} |
634 |
pos++; |
pos++; |
635 |
if (pos > MAX_KEYSERVERS) |
if (pos > MAX_KEYSERVERS) { |
636 |
{ |
msg_box (NULL, _("The keyserver limit is exceeded"), |
637 |
msg_box (NULL, _("The keyserver limit is exceeded"), _("Keyserver Warning"), MB_INFO); |
_("Keyserver Warning"), MB_INFO); |
638 |
break; |
break; |
639 |
} |
} |
640 |
} |
} |
641 |
if (fp) |
if (fp) |
642 |
fclose (fp); |
fclose (fp); |
643 |
if (!pos) |
if (!pos) { |
|
{ |
|
644 |
/* only issue an error if the config file exist but it has no valid entries. */ |
/* only issue an error if the config file exist but it has no valid entries. */ |
645 |
keyserver_set_default (NULL, HKP_PORT); |
keyserver_set_default (NULL, HKP_PORT); |
646 |
if (!no_config) |
if (!no_config) |
647 |
return WPTERR_CONFIG_FILE; |
return WPTERR_CONFIG_FILE; |
648 |
} |
} |
649 |
|
|
650 |
if (!stat (conf, &statbuf)) |
if (!stat (conf, &statbuf)) |
651 |
conf_timestamp = statbuf.st_mtime; |
conf_timestamp = statbuf.st_mtime; |
652 |
return 0; |
return 0; |
653 |
} /* kserver_load_conf */ |
} |
654 |
|
|
655 |
|
|
656 |
const char * |
/* Return the proxy host and port if available. Null otherwise. */ |
657 |
kserver_get_proxy (int * r_port) |
const char* |
658 |
|
kserver_get_proxy (int *r_port) |
659 |
{ |
{ |
660 |
if (proxy.host) { |
if (proxy.host) { |
661 |
if (r_port) |
if (r_port) |
663 |
return proxy.host; |
return proxy.host; |
664 |
} |
} |
665 |
return NULL; |
return NULL; |
666 |
} /* kserver_get_proxy */ |
} |
667 |
|
|
668 |
|
|
669 |
const char * |
/* Return a requested item from the proxy environment. */ |
670 |
|
const char* |
671 |
kserver_get_proxy_info (int id) |
kserver_get_proxy_info (int id) |
672 |
{ |
{ |
673 |
switch (id) { |
switch (id) { |
675 |
case PROXY_PASS: return proxy.pass; |
case PROXY_PASS: return proxy.pass; |
676 |
} |
} |
677 |
return NULL; |
return NULL; |
678 |
} /* kserver_get_proxy_info */ |
} |
679 |
|
|
680 |
|
|
681 |
/* Connect to the keyserver @hostname. */ |
/* Connect to the keyserver @hostname (port @port). |
682 |
|
Return value: 0 on success */ |
683 |
int |
int |
684 |
kserver_connect (const char *hostname, u16 port, int *conn_fd) |
kserver_connect (const char *hostname, WORD port, int *conn_fd) |
685 |
{ |
{ |
686 |
int rc, fd; |
int rc, fd; |
687 |
u32 iaddr; |
DWORD iaddr; |
688 |
char host[128] = {0}; |
char host[128] = {0}; |
689 |
struct hostent *hp; |
struct hostent *hp; |
690 |
struct sockaddr_in sock; |
struct sockaddr_in sock; |
715 |
memcpy (&sock.sin_addr, hp->h_addr, hp->h_length); |
memcpy (&sock.sin_addr, hp->h_addr, hp->h_length); |
716 |
} |
} |
717 |
else { |
else { |
718 |
log_debug ("gethostbyname: failed.\r\n"); |
log_debug ("gethostbyname: failed ec=%d.\r\n", net_errno); |
719 |
return WPTERR_WINSOCK_RESOLVE; |
return WPTERR_WINSOCK_RESOLVE; |
720 |
} |
} |
721 |
fd = socket (AF_INET, SOCK_STREAM, 0); |
fd = socket (AF_INET, SOCK_STREAM, 0); |
723 |
return WPTERR_WINSOCK_SOCKET; |
return WPTERR_WINSOCK_SOCKET; |
724 |
rc = connect (fd, (struct sockaddr *) &sock, sizeof (sock)); |
rc = connect (fd, (struct sockaddr *) &sock, sizeof (sock)); |
725 |
if (rc == SOCKET_ERROR) { |
if (rc == SOCKET_ERROR) { |
726 |
log_debug ("connect: failed.\r\n"); |
log_debug ("connect: failed ec=%d.\r\n", net_errno); |
727 |
closesocket (fd); |
closesocket (fd); |
728 |
return WPTERR_WINSOCK_CONNECT; |
return WPTERR_WINSOCK_CONNECT; |
729 |
} |
} |
732 |
*conn_fd = fd; |
*conn_fd = fd; |
733 |
WSASetLastError (0); |
WSASetLastError (0); |
734 |
return 0; |
return 0; |
735 |
} /* kserver_connect */ |
} |
736 |
|
|
737 |
|
|
738 |
/* Perform URL-encoding on the given pubkey blob. */ |
static bool |
739 |
static char* |
URL_must_encoded (const char *url) |
|
kserver_urlencode (const char *pubkey, size_t octets, size_t *newlen) |
|
740 |
{ |
{ |
741 |
char *p, numbuf[5]; |
if (strchr (url, '.') || strchr (url, '@') || strchr (url, ' ')) |
742 |
size_t j = 0; |
return true; |
743 |
size_t i, size; |
return false; |
744 |
|
} |
|
p = new char [2*octets]; |
|
|
if (!p) |
|
|
BUG (0); |
|
745 |
|
|
746 |
for (size=0, i=0; i < octets; i++) { |
|
747 |
if (isalnum (pubkey[i]) || pubkey[i] == '-') { |
/* Perform URL encoding of the given data. */ |
748 |
p[size] = pubkey[i]; |
static char* |
749 |
size++; |
URL_encode (const char *url, size_t ulen, size_t *ret_nlen) |
750 |
} |
{ |
751 |
else if (pubkey[i] == ' ') { |
gpgme_data_t hd; |
752 |
p[size] = '+'; |
char numbuf[5], *pp, *p; |
753 |
size++; |
size_t i, n; |
754 |
} |
|
755 |
|
gpgme_data_new (&hd); |
756 |
|
for (i=0; i < ulen; i++) { |
757 |
|
if (isalnum (url[i]) || url[i] == '-') |
758 |
|
gpg_data_putc (hd, url[i]); |
759 |
|
else if (url[i] == ' ') |
760 |
|
gpg_data_putc (hd, '+'); |
761 |
else { |
else { |
762 |
sprintf(numbuf, "%%%02X", pubkey[i]); |
sprintf (numbuf, "%%%02X", url[i]); |
763 |
for (j = 0; j<strlen(numbuf); j++) { |
gpgme_data_write (hd, numbuf, strlen (numbuf)); |
|
p[size] = numbuf[j]; |
|
|
size++; |
|
|
} |
|
764 |
} |
} |
765 |
} |
} |
766 |
p[size] = '\0'; |
gpg_data_putc (hd, '\0'); |
767 |
*newlen = size; |
|
768 |
|
/* Copy memory to avoid that we need to use gpgme_free later. */ |
769 |
|
pp = gpgme_data_release_and_get_mem (hd, &n); |
770 |
|
p = m_strdup (pp); |
771 |
|
gpgme_free (pp); |
772 |
|
if (ret_nlen) |
773 |
|
*ret_nlen = n; |
774 |
return p; |
return p; |
775 |
} |
} |
776 |
|
|
777 |
|
|
778 |
/* Format a request for the given keyid (send). */ |
/* Format a request for the given keyid (send). */ |
779 |
static char* |
static char* |
780 |
kserver_send_request (const char *hostname, u16 port, const char *pubkey, int octets) |
kserver_send_request (const char *hostname, WORD port, |
781 |
|
const char *pubkey, int octets) |
782 |
{ |
{ |
783 |
char *request = NULL, *enc_pubkey = NULL; |
char *request = NULL; |
784 |
int reqlen; |
char *enc_pubkey = NULL; |
785 |
size_t enc_octets; |
size_t enc_octets; |
786 |
|
int reqlen; |
787 |
|
|
788 |
log_debug ("kserver_send_request: %s:%d\n", hostname, port); |
log_debug ("kserver_send_request: %s:%d\r\n", hostname, port); |
789 |
|
|
790 |
if (!port) |
if (!port) |
791 |
port = HKP_PORT; |
port = HKP_PORT; |
793 |
request = new char[reqlen]; |
request = new char[reqlen]; |
794 |
if (!request) |
if (!request) |
795 |
BUG (0); |
BUG (0); |
796 |
enc_pubkey = kserver_urlencode (pubkey, octets, &enc_octets); |
enc_pubkey = URL_encode (pubkey, octets, &enc_octets); |
797 |
if (!enc_pubkey || !enc_octets) { |
if (!enc_pubkey || !enc_octets) { |
798 |
free_if_alloc (request); |
free_if_alloc (request); |
799 |
return NULL; |
return NULL; |
825 |
"\r\n" |
"\r\n" |
826 |
"keytext=%s" |
"keytext=%s" |
827 |
"\n", |
"\n", |
828 |
skip_type_prefix (hostname), port, enc_octets+9, enc_pubkey); |
skip_type_prefix (hostname), port, |
829 |
|
enc_octets+9, enc_pubkey); |
830 |
} |
} |
831 |
free_if_alloc (enc_pubkey); |
free_if_alloc (enc_pubkey); |
832 |
|
|
833 |
log_debug ("%s\n", request); |
log_debug ("%s\r\n", request); |
834 |
return request; |
return request; |
835 |
} /* kserver_send_request */ |
} |
836 |
|
|
837 |
|
|
|
/* Interface receiving a public key. */ |
|
838 |
int |
int |
839 |
kserver_recvkey (const char *hostname, u16 port, const char *keyid, char *key, int maxkeylen) |
kserver_recvkey_ext (const char *hostname, WORD port, const char *keyid, |
840 |
{ |
char **r_key, int *r_keylen) |
841 |
int rc, n; |
{ |
|
int conn_fd; |
|
842 |
char *request = NULL; |
char *request = NULL; |
843 |
|
int conn_fd; |
844 |
|
int rc, n; |
845 |
|
|
846 |
if (!port) |
if (!port) |
847 |
port = HKP_PORT; |
port = HKP_PORT; |
848 |
hkp_err = 0; /* reset */ |
hkp_err = 0; /* reset */ |
878 |
goto leave; |
goto leave; |
879 |
} |
} |
880 |
|
|
881 |
rc = sock_read( conn_fd, key, maxkeylen, &n ); |
rc = sock_read_ext (conn_fd, r_key, &n); |
882 |
if( rc == SOCKET_ERROR ) { |
if (rc == SOCKET_ERROR) { |
883 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
884 |
goto leave; |
goto leave; |
885 |
} |
} |
886 |
|
|
887 |
log_debug("%s\r\n", key); |
if (r_keylen) |
888 |
rc = check_hkp_response (key, 1); |
*r_keylen = n; |
889 |
|
log_debug("%s\r\n", *r_key); |
890 |
|
rc = check_hkp_response (*r_key, 1); |
891 |
if (rc) |
if (rc) |
892 |
goto leave; |
goto leave; |
893 |
|
|
897 |
closesocket (conn_fd); |
closesocket (conn_fd); |
898 |
free_if_alloc (request); |
free_if_alloc (request); |
899 |
return rc; |
return rc; |
900 |
} /* kserver_recvkey */ |
} |
901 |
|
|
902 |
|
/* Interface for receiving a public key. */ |
903 |
|
int |
904 |
|
kserver_recvkey (const char *hostname, WORD port, const char *keyid, |
905 |
|
char *key, int maxkeylen) |
906 |
|
{ |
907 |
|
char *tmpkey; |
908 |
|
int rc, nread=0; |
909 |
|
|
910 |
|
/* XXX: just a wrapper around the new method, replace it |
911 |
|
soon as possible. */ |
912 |
|
rc = kserver_recvkey_ext (hostname, port, keyid, &tmpkey, &nread); |
913 |
|
if (rc) |
914 |
|
return rc; |
915 |
|
|
916 |
|
if (nread > maxkeylen) { |
917 |
|
free_if_alloc (tmpkey); |
918 |
|
return WPTERR_GENERAL; |
919 |
|
} |
920 |
|
strcpy (key, tmpkey); |
921 |
|
free_if_alloc (tmpkey); |
922 |
|
return 0; |
923 |
|
} |
924 |
|
|
925 |
|
|
926 |
/* Interface to send a public key. */ |
/* Interface to send a public key. */ |
927 |
int |
int |
928 |
kserver_sendkey (const char *hostname, u16 port, const char *pubkey, int len ) |
kserver_sendkey (const char *hostname, WORD port, const char *pubkey, int len ) |
929 |
{ |
{ |
|
int n, rc; |
|
|
int conn_fd; |
|
930 |
char *request = NULL; |
char *request = NULL; |
931 |
char log[2048]; |
char log[2048]; |
932 |
|
int conn_fd, n; |
933 |
|
int rc; |
934 |
|
|
935 |
hkp_err = 0; /* reset */ |
hkp_err = 0; /* reset */ |
936 |
rc = kserver_connect (hostname, port, &conn_fd); |
rc = kserver_connect (hostname, port, &conn_fd); |
943 |
goto leave; |
goto leave; |
944 |
} |
} |
945 |
|
|
946 |
rc = sock_write( conn_fd, request, lstrlen(request) ); |
rc = sock_write (conn_fd, request, strlen (request)); |
947 |
if( rc == SOCKET_ERROR ) { |
if (rc == SOCKET_ERROR) { |
948 |
rc = WPTERR_WINSOCK_SENDKEY; |
rc = WPTERR_WINSOCK_SENDKEY; |
949 |
goto leave; |
goto leave; |
950 |
} |
} |
951 |
|
|
952 |
rc = sock_read( conn_fd, log, sizeof (log)-1, &n ); |
rc = sock_read (conn_fd, log, sizeof (log)-1, &n); |
953 |
if( rc == SOCKET_ERROR ) { |
if (rc == SOCKET_ERROR) { |
954 |
rc = WPTERR_WINSOCK_SENDKEY; |
rc = WPTERR_WINSOCK_SENDKEY; |
955 |
goto leave; |
goto leave; |
956 |
} |
} |
957 |
|
|
958 |
log_debug ("kserver_sendkey:\r\n%s\r\n", log); |
log_debug ("kserver_sendkey:\r\n%s\r\n", log); |
|
|
|
959 |
rc = check_hkp_response (log, 0); |
rc = check_hkp_response (log, 0); |
960 |
if( rc ) |
if (rc) |
961 |
goto leave; |
goto leave; |
962 |
|
|
963 |
WSASetLastError (0); |
WSASetLastError (0); |
966 |
closesocket (conn_fd); |
closesocket (conn_fd); |
967 |
free_if_alloc (request); |
free_if_alloc (request); |
968 |
return rc; |
return rc; |
969 |
} /* kserver_sendkey */ |
} |
970 |
|
|
971 |
|
|
972 |
int |
int |
973 |
kserver_search_init (const char * hostname, u16 port, const char * keyid, int * conn_fd) |
kserver_search_init (const char *hostname, WORD port, |
974 |
|
const char *keyid, int *conn_fd) |
975 |
{ |
{ |
976 |
|
char *request = NULL; |
977 |
|
char *enc_keyid = NULL; |
978 |
|
int n = 0; |
979 |
int rc, sock_fd; |
int rc, sock_fd; |
|
int n=0; |
|
|
char * request = NULL; |
|
980 |
|
|
981 |
rc = kserver_connect (hostname, port, &sock_fd); |
rc = kserver_connect (hostname, port, &sock_fd); |
982 |
if (rc) { |
if (rc) { |
983 |
*conn_fd = 0; |
*conn_fd = 0; |
984 |
goto leave; |
goto leave; |
985 |
} |
} |
986 |
|
|
987 |
|
enc_keyid = URL_encode (keyid, strlen (keyid), NULL); |
988 |
n=300; |
n=300; |
989 |
request = new char[n+1]; |
request = new char[n+1]; |
990 |
if (!request) |
if (!request) |
994 |
_snprintf (request, n, |
_snprintf (request, n, |
995 |
"GET http://%s:%d/pks/lookup?op=index&search=%s HTTP/1.0\r\n" |
"GET http://%s:%d/pks/lookup?op=index&search=%s HTTP/1.0\r\n" |
996 |
"Proxy-Authorization: Basic %s\r\n\r\n", |
"Proxy-Authorization: Basic %s\r\n\r\n", |
997 |
skip_type_prefix (hostname), port, keyid, proxy.base64_user); |
skip_type_prefix (hostname), port, enc_keyid, proxy.base64_user); |
998 |
} |
} |
999 |
else if (proxy.host) { |
else if (proxy.host) { |
1000 |
_snprintf (request, n, |
_snprintf (request, n, |
1001 |
"GET http://%s:%d/pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n", |
"GET http://%s:%d/pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n", |
1002 |
skip_type_prefix (hostname), port, keyid); |
skip_type_prefix (hostname), port, enc_keyid); |
1003 |
} |
} |
1004 |
else { |
else { |
1005 |
_snprintf (request, n, |
_snprintf (request, n, |
1006 |
"GET /pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n", keyid); |
"GET /pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n", |
1007 |
|
enc_keyid); |
1008 |
} |
} |
1009 |
|
|
1010 |
log_debug ("kserver_search_init:\r\n%s\r\n", request); |
log_debug ("kserver_search_init:\r\n%s\r\n", request); |
1018 |
|
|
1019 |
leave: |
leave: |
1020 |
free_if_alloc (request); |
free_if_alloc (request); |
1021 |
|
free_if_alloc (enc_keyid); |
1022 |
return rc; |
return rc; |
1023 |
} /* kserver_search_init */ |
} |
1024 |
|
|
1025 |
|
|
1026 |
|
/* Check keyserver response. */ |
1027 |
int |
int |
1028 |
kserver_search_chkresp (int fd) |
kserver_search_chkresp (int fd) |
1029 |
{ |
{ |
1040 |
if (strncmp (buf+(8+1), "200", 3)) |
if (strncmp (buf+(8+1), "200", 3)) |
1041 |
return WPTERR_KEYSERVER_NOTFOUND; |
return WPTERR_KEYSERVER_NOTFOUND; |
1042 |
return 0; |
return 0; |
1043 |
} /* kserver_search_chkresp */ |
} |
1044 |
|
|
1045 |
|
|
1046 |
|
/* Convert an iso date @iso_date (YYYY-MM-DD) into the locale |
1047 |
|
representation and store it into @loc_date. |
1048 |
|
Return value: 0 on success. */ |
1049 |
|
static int |
1050 |
|
parse_iso_date (const char *iso_date, char *loc_date, size_t loclen) |
1051 |
|
{ |
1052 |
|
SYSTEMTIME st; |
1053 |
|
char buf[16] = {0}, *p; |
1054 |
|
int pos=0; |
1055 |
|
|
1056 |
|
strncpy (buf, iso_date, sizeof (buf)-1); |
1057 |
|
p = strtok (buf, "-"); |
1058 |
|
while (p != NULL) { |
1059 |
|
switch (pos) { |
1060 |
|
case 0: st.wYear = (WORD)atoi (p); pos++; break; |
1061 |
|
case 1: st.wMonth = (WORD)atoi (p); pos++; break; |
1062 |
|
case 2: st.wDay = (WORD)atoi (p); pos++; break; |
1063 |
|
default: break; |
1064 |
|
} |
1065 |
|
p = strtok (NULL, "-"); |
1066 |
|
} |
1067 |
|
if (pos != 3) |
1068 |
|
return -1; |
1069 |
|
|
1070 |
|
if (!GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, |
1071 |
|
NULL, loc_date, loclen)) |
1072 |
|
return -1; |
1073 |
|
return 0; |
1074 |
|
} |
1075 |
|
|
1076 |
|
|
1077 |
int |
int |
1078 |
kserver_search (int fd, keyserver_key * key) |
kserver_search (int fd, keyserver_key *key) |
1079 |
{ |
{ |
1080 |
char buf[1024], *p; |
char buf[1024], *p; |
1081 |
int uidlen, nbytes, pos = 0; |
int uidlen, nbytes, pos = 0; |
1082 |
|
|
1083 |
log_debug ("keyserver_search:\n"); |
log_debug ("keyserver_search:\r\n"); |
1084 |
|
|
1085 |
if (sock_getline (fd, buf, sizeof (buf) - 1, &nbytes)) |
if (sock_getline (fd, buf, sizeof (buf) - 1, &nbytes)) |
1086 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
1087 |
|
|
1088 |
log_debug ("%s\n", buf); |
log_debug ("%s\r\n", buf); |
1089 |
|
|
1090 |
if (!strncmp (buf, "pub", 3)) { |
if (!strncmp (buf, "pub", 3)) { |
1091 |
int revoked = strstr (buf, "KEY REVOKED") != NULL? 1 : 0; |
int revoked = strstr (buf, "KEY REVOKED") != NULL? 1 : 0; |
1102 |
pos = p - buf + 5; |
pos = p - buf + 5; |
1103 |
memcpy (key->date, buf + pos, 10); |
memcpy (key->date, buf + pos, 10); |
1104 |
key->date[10] = '\0'; |
key->date[10] = '\0'; |
1105 |
|
parse_iso_date (key->date, key->date, sizeof (key->date)-1); |
1106 |
if (revoked) { |
if (revoked) { |
1107 |
strcpy (key->uid, "KEY REVOKED: not checked"); |
strcpy (key->uid, "KEY REVOKED: not checked"); |
1108 |
return 0; |
return 0; |
1113 |
p++; |
p++; |
1114 |
p++; |
p++; |
1115 |
uidlen = strlen (p) - 10; |
uidlen = strlen (p) - 10; |
1116 |
|
if (!strstr (p, "<") && !strstr (p, ">")) { |
1117 |
|
pos=0; |
1118 |
|
while (p && *p && pos < sizeof (key->uid)-1) { |
1119 |
|
if (*p == '<') |
1120 |
|
break; |
1121 |
|
key->uid[pos++] = *p++; |
1122 |
|
} |
1123 |
|
key->uid[pos] ='\0'; |
1124 |
|
return 0; |
1125 |
|
} |
1126 |
|
|
1127 |
|
if (uidlen > sizeof (key->uid)-1) |
1128 |
|
uidlen = sizeof (key->uid)-1; |
1129 |
memcpy (key->uid, p, uidlen); |
memcpy (key->uid, p, uidlen); |
1130 |
key->uid[uidlen] = '\0'; |
key->uid[uidlen] = '\0'; |
1131 |
strcat (key->uid, ">"); |
strcat (key->uid, ">"); |
1132 |
p = strchr (key->uid, '&'); |
p = strchr (key->uid, '&'); |
1133 |
if (p) { |
if (p) { |
1134 |
key->uid[(p-key->uid)] = '<'; |
key->uid[(p-key->uid)] = '<'; |
1135 |
memmove (key->uid+(p-key->uid)+1, key->uid+(p-key->uid)+4, strlen (key->uid)-3); |
memmove (key->uid+(p-key->uid)+1, key->uid+(p-key->uid)+4, |
1136 |
|
strlen (key->uid)-3); |
1137 |
} |
} |
1138 |
return 0; |
return 0; |
1139 |
} |
} |
1142 |
key->bits = 0; |
key->bits = 0; |
1143 |
memset (key, 0, sizeof *key); |
memset (key, 0, sizeof *key); |
1144 |
return 0; |
return 0; |
1145 |
} /* kserver_search */ |
} |
|
|
|
|
|
|
|
static int |
|
|
add_node( keyserver_cfgfile *cfg, const char *entry ) |
|
|
{ |
|
|
keyserver_node *node; |
|
|
char *p = NULL; |
|
|
|
|
|
p = strchr( entry, '=' ); |
|
|
if( p == NULL ) |
|
|
return WPTERR_GENERAL; |
|
|
node = new keyserver_node; |
|
|
if( !node ) |
|
|
BUG( NULL ); |
|
|
|
|
|
memset( node, 0, sizeof *node ); |
|
|
node->used = 1; |
|
|
|
|
|
node->host.used = 1; |
|
|
node->host.proto = proto_from_URL( entry ); |
|
|
memcpy( node->host.name, entry+7, p-entry-7 ); |
|
|
node->next = cfg->list; |
|
|
cfg->list = node; |
|
|
|
|
|
return 0; |
|
|
} /* add_node */ |
|
1146 |
|
|
1147 |
|
|
1148 |
|
/* Replace all current proxy items with the items |
1149 |
|
from the context @ctx. */ |
1150 |
void |
void |
1151 |
kserver_change_proxy( keyserver_proxy_ctx *ctx ) |
kserver_change_proxy (keyserver_proxy_ctx *ctx) |
1152 |
{ |
{ |
1153 |
proxy.port = ctx->port; |
proxy.port = ctx->port; |
1154 |
free_if_alloc (proxy.host); |
free_if_alloc (proxy.host); |
1155 |
proxy.host = ctx->host? m_strdup( ctx->host ) : NULL; |
proxy.host = ctx->host? m_strdup (ctx->host) : NULL; |
1156 |
free_if_alloc( proxy.pass ); |
free_if_alloc (proxy.pass); |
1157 |
proxy.pass = ctx->pass? m_strdup( ctx->pass ) : NULL; |
proxy.pass = ctx->pass? m_strdup (ctx->pass) : NULL; |
1158 |
free_if_alloc( proxy.user ); |
free_if_alloc (proxy.user); |
1159 |
proxy.user = ctx->user? m_strdup( ctx->user ) : NULL; |
proxy.user = ctx->user? m_strdup (ctx->user) : NULL; |
1160 |
set_reg_proxy_prefs( proxy.host, proxy.port, proxy.user, proxy.pass ); |
set_reg_proxy_prefs (proxy.host, proxy.port, proxy.user, proxy.pass); |
1161 |
} /* kserver_change_proxy */ |
} |
1162 |
|
|
1163 |
|
|
1164 |
int |
/* Release mbmers in the proxy context @ctx. */ |
1165 |
kserver_read_config( const char *fname, keyserver_cfgfile **ret_cfg ) |
void |
1166 |
|
proxy_release (keyserver_proxy_ctx *ctx) |
1167 |
{ |
{ |
1168 |
FILE *fp; |
free_if_alloc (ctx->host); |
1169 |
keyserver_cfgfile *cfg; |
free_if_alloc (ctx->pass); |
1170 |
char buf[256]; |
free_if_alloc (ctx->user); |
1171 |
int rc = 0; |
} |
|
|
|
|
*ret_cfg = NULL; |
|
|
fp = fopen( fname, "rb" ); |
|
|
if( fp == NULL ) |
|
|
return WPTERR_FILE_OPEN; |
|
|
|
|
|
cfg = new keyserver_cfgfile; |
|
|
if ( !cfg ) |
|
|
BUG( NULL ); |
|
|
memset( cfg, 0, sizeof *cfg ); |
|
|
|
|
|
while ( !feof( fp ) ) { |
|
|
if ( fgets( buf, sizeof(buf)-1, fp ) == NULL ) |
|
|
break; |
|
|
if ( *buf == '\r' || *buf == '\n' || *buf == '#' ) |
|
|
continue; |
|
|
buf[strlen(buf) -2] = '\0'; |
|
|
if ( !strncmp( buf, "use_proxy=", 10 ) ) { |
|
|
char *p = strchr(buf, ':'); |
|
|
if (!p) |
|
|
continue; |
|
|
cfg->proxy.port = atol( buf+(p-buf+1) ); |
|
|
buf[p-buf] = '\0'; |
|
|
cfg->proxy.host = m_strdup( buf+10 ); |
|
|
} |
|
|
else if ( !strncmp( buf, "proxy_user=", 11 ) ) |
|
|
cfg->proxy.user = m_strdup( buf+11 ); |
|
|
else if ( !strncmp( buf, "proxy_pass=", 11 ) ) |
|
|
cfg->proxy.pass = m_strdup( buf+11 ); |
|
|
if ( !strncmp( buf, "http://", 7 ) |
|
|
|| !strncmp( buf, "hkp://", 6 ) |
|
|
|| !strncmp( buf, "ldap://", 7 ) ) { |
|
|
add_node( cfg, buf ); |
|
|
cfg->nlist++; |
|
|
} |
|
|
} |
|
|
|
|
|
fclose( fp ); |
|
|
*ret_cfg = cfg; |
|
|
|
|
|
return rc; |
|
|
} /* kserver_read_config */ |
|
1172 |
|
|
1173 |
|
|
1174 |
int |
/* Spawn a process given by @cmdl. */ |
1175 |
kserver_write_config( const char * fname, keyserver_cfgfile * cfg ) |
static int |
1176 |
|
spawn_application (char *cmdl) |
1177 |
{ |
{ |
1178 |
|
STARTUPINFO si; |
1179 |
return 0; |
PROCESS_INFORMATION pi; |
1180 |
} |
int rc = 0; |
|
|
|
1181 |
|
|
1182 |
void |
memset (&si, 0, sizeof (si)); |
1183 |
kserver_cfgfile_release( keyserver_cfgfile *cfg ) |
si.cb = sizeof (si); |
1184 |
{ |
si.dwFlags = STARTF_USESHOWWINDOW; |
1185 |
keyserver_node *k, *k2; |
si.wShowWindow = SW_HIDE; |
1186 |
|
memset (&pi, 0, sizeof (pi)); |
1187 |
|
|
1188 |
proxy_release( &cfg->proxy ); |
if (!CreateProcess (NULL, cmdl, NULL, NULL, FALSE, 0, |
1189 |
for( k = cfg->list; cfg->nlist--; k = k2 ) { |
NULL, NULL, &si, &pi)) { |
1190 |
k2 = k->next; |
log_box ("Keyserver Plugin", MB_ERR, "Could not spawn helper process"); |
1191 |
free_if_alloc( k ); |
rc = -1; |
1192 |
} |
} |
|
free_if_alloc( cfg ); |
|
|
} /* kserver_cfgfile_release */ |
|
1193 |
|
|
1194 |
|
CloseHandle (pi.hThread); |
1195 |
void |
WaitForSingleObject (pi.hProcess, INFINITE); |
1196 |
proxy_release( keyserver_proxy_ctx *ctx ) |
CloseHandle (pi.hProcess); |
1197 |
{ |
return rc; |
1198 |
free_if_alloc( ctx->host ); |
} |
|
free_if_alloc( ctx->pass ); |
|
|
free_if_alloc( ctx->user ); |
|
|
} /* proxy_release */ |
|
1199 |
|
|
1200 |
|
|
1201 |
int |
/* Receive an key via LDAP from host @host with the keyid @keyid. |
1202 |
ldap_recvkey (const char * host, const char * keyid, char * key, int maxkeylen) |
@key contains the key on success. */ |
1203 |
{ |
int |
1204 |
|
ldap_recvkey (const char *host, const char *keyid, char *key, int maxkeylen) |
1205 |
|
{ |
1206 |
|
FILE *fp; |
1207 |
const char *s; |
const char *s; |
1208 |
char *ksprg = NULL, *p = NULL; |
char *ksprg = NULL, *p = NULL, *sep; |
1209 |
char temp[512], outf[512]; |
char inf[256], outf[256], buf[512]; |
1210 |
FILE * inp; |
DWORD n; |
1211 |
int rc, start_key = 0; |
int start_key = 0, failed = 0; |
1212 |
STARTUPINFO si; |
int rc = 0; |
|
PROCESS_INFORMATION pi; |
|
1213 |
|
|
1214 |
p = get_gnupg_path (); |
p = get_gnupg_prog (); |
1215 |
ksprg = new char[strlen (p) + 1 + 128]; |
n = strlen (p) + 1 + 128; |
1216 |
|
ksprg = new char[n+1]; |
1217 |
if (!ksprg) |
if (!ksprg) |
1218 |
BUG (0); |
BUG (0); |
1219 |
strcpy (ksprg, p); |
sep = strrchr (p, '\\'); |
1220 |
strcat (ksprg, "\\"); |
if (sep != NULL) |
1221 |
strcat (ksprg, "gpgkeys_ldap.exe"); |
p[(sep-p)] = 0; |
1222 |
|
|
1223 |
|
_snprintf (ksprg, n, "%s\\gpgkeys_ldap.exe", p); |
1224 |
free_if_alloc (p); |
free_if_alloc (p); |
1225 |
if (file_exist_check (ksprg)) { |
if (file_exist_check (ksprg)) { |
1226 |
log_box ( "LDAP Keyserver Plugin", MB_ERR, "Could not find LDAP keyserver module!"); |
log_box ("LDAP Keyserver Plugin", MB_ERR, |
1227 |
rc = -1; |
"%s: could not find LDAP keyserver module!", ksprg); |
1228 |
|
rc = -1; |
1229 |
goto leave; |
goto leave; |
1230 |
} |
} |
1231 |
GetTempPath (sizeof (temp)-1, temp); |
get_temp_name (outf, sizeof (outf)-1, keyid); |
1232 |
strcpy (outf, temp); |
get_temp_name (inf, sizeof (inf)-1, NULL); |
1233 |
strcat (outf, keyid); |
fp = fopen (inf, "w+b"); |
1234 |
strcat (outf, ".out"); |
if (!fp) { |
1235 |
strcat (temp, keyid); |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", inf, |
|
inp = fopen (temp, "w+b"); |
|
|
if( !inp ) { |
|
|
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", temp, |
|
1236 |
winpt_strerror (WPTERR_FILE_OPEN)); |
winpt_strerror (WPTERR_FILE_OPEN)); |
1237 |
rc = -1; |
rc = -1; |
1238 |
goto leave; |
goto leave; |
1239 |
} |
} |
1240 |
fprintf (inp, |
fprintf (fp, |
1241 |
"VERSION 0\n" |
"VERSION 1\n" |
1242 |
|
"PROGRAM 1.4.3-cvs\n" |
1243 |
|
"SCHEME ldap\n" |
1244 |
"HOST %s\n" |
"HOST %s\n" |
|
"OPTION verbose\n" |
|
1245 |
"COMMAND GET\n" |
"COMMAND GET\n" |
1246 |
"\n" |
"\n" |
1247 |
"%s\n", host? skip_type_prefix (host): "64.94.85.200", keyid); |
"%s\n", |
1248 |
fclose (inp); |
host? skip_type_prefix (host): "64.94.85.200", keyid); |
1249 |
|
fclose (fp); |
1250 |
|
|
1251 |
memset (&si, 0, sizeof (si)); |
p = new char[strlen (ksprg) + strlen (inf) + strlen (outf) + 32]; |
1252 |
si.cb = sizeof (si); |
if (!p) |
1253 |
si.dwFlags = STARTF_USESHOWWINDOW; |
BUG (NULL); |
1254 |
si.wShowWindow = SW_HIDE; |
sprintf (p, "%s -o %s %s", ksprg, outf, inf); |
1255 |
memset (&pi, 0, sizeof (pi)); |
if (spawn_application (p)) { |
|
p = new char[strlen (ksprg) + strlen (temp) + strlen (outf) + 32]; |
|
|
sprintf (p, "%s -o %s %s", ksprg, outf, temp); |
|
|
rc = CreateProcess (NULL, p, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi); |
|
|
if (!rc) { |
|
|
log_box ("LDAP Keyserver Plugin", MB_ERR, "Could not spawn process"); |
|
1256 |
rc = -1; |
rc = -1; |
1257 |
goto leave; |
goto leave; |
1258 |
} |
} |
|
CloseHandle (pi.hThread); |
|
|
WaitForSingleObject (pi.hProcess, INFINITE); |
|
|
CloseHandle (pi.hProcess); |
|
1259 |
|
|
1260 |
inp = fopen (outf, "rb"); |
fp = fopen (outf, "rb"); |
1261 |
if (!inp) { |
if (!fp) { |
1262 |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf, |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf, |
1263 |
winpt_strerror (WPTERR_FILE_OPEN)); |
winpt_strerror (WPTERR_FILE_OPEN)); |
1264 |
rc = -1; |
rc = -1; |
1265 |
goto leave; |
goto leave; |
1266 |
} |
} |
1267 |
memset (key, 0, maxkeylen); |
memset (key, 0, maxkeylen); |
1268 |
while( !feof( inp ) ) { |
while (!feof (fp)) { |
1269 |
s = fgets( temp, sizeof (temp)-1, inp ); |
s = fgets (buf, sizeof (buf)-1, fp); |
1270 |
if( !s ) |
if (!s) |
1271 |
|
break; |
1272 |
|
if (strstr (s, "KEY") && strstr (s, "FAILED")) { |
1273 |
|
failed = 1; |
1274 |
break; |
break; |
1275 |
if( !start_key && strstr( s, "KEY" ) && strstr( s, "BEGIN" ) ) { |
} |
1276 |
|
if (!start_key && strstr (s, "KEY") && strstr (s, "BEGIN")) { |
1277 |
start_key = 1; |
start_key = 1; |
1278 |
continue; |
continue; |
1279 |
} |
} |
1280 |
if( !start_key ) |
if (!start_key) |
1281 |
continue; |
continue; |
1282 |
strcat( key, temp ); |
strcat (key, buf); |
1283 |
maxkeylen -= strlen( temp ); |
maxkeylen -= strlen (buf); |
1284 |
if( maxkeylen < 0 || (strstr( s, "KEY" ) && strstr( s, "END" )) ) |
if (maxkeylen < 0 || (strstr (s, "KEY") && strstr (s, "END"))) |
1285 |
break; |
break; |
1286 |
} |
} |
1287 |
fclose( inp ); |
fclose (fp); |
1288 |
|
|
1289 |
leave: |
leave: |
1290 |
if( !strlen( key ) ) |
if (failed || !strlen (key)) |
1291 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
1292 |
free_if_alloc( p ); |
DeleteFile (inf); |
1293 |
free_if_alloc( ksprg ); |
DeleteFile (outf); |
1294 |
|
free_if_alloc (p); |
1295 |
|
free_if_alloc (ksprg); |
1296 |
return rc; |
return rc; |
1297 |
} /* ldap_recvkey */ |
} |
1298 |
|
|
1299 |
|
|
1300 |
static int |
static int |
1301 |
finger_readline (int fd, char * buf, int nbytes) |
finger_readline (int fd, char *buf, int nbytes) |
1302 |
{ |
{ |
1303 |
char c = 0; |
char c = 0; |
1304 |
int n, pos = 0; |
int n, pos = 0; |
1320 |
} |
} |
1321 |
|
|
1322 |
|
|
1323 |
|
/* Receive an key via FINGER from host @host with the user @user. |
1324 |
|
On success @key contains the key. */ |
1325 |
int |
int |
1326 |
finger_recvkey (const char * host, const char * user, char * key, int maxkeylen) |
finger_recvkey (const char *host, const char *user, char *key, int maxkeylen) |
1327 |
{ |
{ |
1328 |
struct hostent * hp; |
struct hostent * hp; |
1329 |
struct sockaddr_in saddr; |
struct sockaddr_in saddr; |
1381 |
} |
} |
1382 |
|
|
1383 |
|
|
1384 |
|
/* Check if the given name @name is a valid hostname. */ |
1385 |
int |
int |
1386 |
check_IP_or_hostname (const char *name) |
check_IP_or_hostname (const char *name) |
1387 |
{ |
{ |