24 |
#endif |
#endif |
25 |
|
|
26 |
#include <windows.h> |
#include <windows.h> |
27 |
|
#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 |
|
/* just map net_errno to a winsock error. */ |
41 |
#define net_errno ((int)WSAGetLastError ()) |
#define net_errno ((int)WSAGetLastError ()) |
42 |
|
|
|
static char base64code[] = |
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
|
43 |
|
|
44 |
keyserver server[MAX_KEYSERVERS] = {0}; |
keyserver server[MAX_KEYSERVERS] = {0}; |
45 |
static keyserver_proxy_ctx proxy = {0}; |
keyserver_proxy_s proxy = {0}; |
46 |
static const char *server_list[] = { |
static const char *server_list[] = { |
47 |
"hkp://wwwkeys.nl.pgp.net", |
"hkp://wwwkeys.nl.pgp.net", |
48 |
"hkp://wwwkeys.pl.pgp.net", |
"hkp://wwwkeys.pl.pgp.net", |
69 |
char *default_keyserver = NULL; |
char *default_keyserver = NULL; |
70 |
WORD default_keyserver_port = 0; |
WORD default_keyserver_port = 0; |
71 |
|
|
72 |
|
/* Default socket timeout. */ |
73 |
|
static int default_socket_timeout = 10; |
74 |
|
|
75 |
/* Basic64 encode the input @inputstr to @outputstr. */ |
/* Basic64 encode the input @inbuf to @outbuf. */ |
76 |
static void |
static void |
77 |
base64_encode (const char *inputstr, char *outputstr, int maxlen) |
base64_encode (const char *inbuf, char *outbuf) |
78 |
{ |
{ |
79 |
int index = 0, temp = 0, res = 0, i = 0, inputlen = 0, len = 0; |
char base64code[] = |
80 |
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
81 |
|
int index = 0, temp = 0, res = 0; |
82 |
|
int i = 0, inputlen = 0, len = 0; |
83 |
|
|
84 |
/* XXX: check len < maxlen. */ |
inputlen = strlen (inbuf); |
85 |
inputlen = strlen (inputstr); |
for (i = 0; i < inputlen; i++) { |
|
for (i = 0; i <inputlen; i++) { |
|
86 |
res = temp; |
res = temp; |
87 |
res = (res << 8) | (inputstr[i] & 0xFF); |
res = (res << 8) | (inbuf[i] & 0xFF); |
88 |
switch (index++) { |
switch (index++) { |
89 |
case 0: outputstr[len++] = base64code[res >> 2 & 0x3F]; res &= 0x3; break; |
case 0: |
90 |
case 1: outputstr[len++] = base64code[res >> 4 & 0x3F]; res &= 0xF; break; |
outbuf[len++] = base64code[res >> 2 & 0x3F]; |
91 |
case 2: outputstr[len++] = base64code[res >> 6 & 0x3F]; |
res &= 0x3; |
92 |
outputstr[len++] = base64code[res & 0x3F]; res = index = 0; break; |
break; |
93 |
|
case 1: |
94 |
|
outbuf[len++] = base64code[res >> 4 & 0x3F]; |
95 |
|
res &= 0xF; |
96 |
|
break; |
97 |
|
case 2: |
98 |
|
outbuf[len++] = base64code[res >> 6 & 0x3F]; |
99 |
|
outbuf[len++] = base64code[res & 0x3F]; |
100 |
|
res = index = 0; |
101 |
|
break; |
102 |
} |
} |
103 |
temp = res; |
temp = res; |
104 |
} |
} |
105 |
|
|
106 |
switch (index) { |
if (index == 2) { |
107 |
case 0: break; |
outbuf[len++] = base64code[temp << 2 & 0x3F]; |
108 |
case 2: outputstr[len++] = base64code[temp << 2 & 0x3F]; |
outbuf[len++] = '='; |
|
outputstr[len++] = '='; break; |
|
|
case 1: outputstr[len++] = base64code[temp << 4 & 0x3F]; |
|
|
outputstr[len++] = '='; |
|
|
outputstr[len++] = '='; |
|
109 |
} |
} |
110 |
|
else if (index == 1) { |
111 |
outputstr[len] = '\0'; |
outbuf[len++] = base64code[temp << 4 & 0x3F]; |
112 |
|
outbuf[len++] = '='; |
113 |
|
outbuf[len++] = '='; |
114 |
|
} |
115 |
|
|
116 |
|
outbuf[len] = '\0'; |
117 |
} |
} |
118 |
|
|
119 |
|
|
168 |
sock_getline (int fd, char *buf, int buflen, int *nbytes) |
sock_getline (int fd, char *buf, int buflen, int *nbytes) |
169 |
{ |
{ |
170 |
char ch; |
char ch; |
171 |
int count = 0; |
int nread = 0; |
172 |
|
|
173 |
if (nbytes) |
if (nbytes) |
174 |
*nbytes = 0; |
*nbytes = 0; |
175 |
*buf = 0; |
*buf = 0; |
176 |
while (recv (fd, &ch, 1, 0) > 0) { |
while (recv (fd, &ch, 1, 0) > 0) { |
177 |
*buf++ = ch; |
*buf++ = ch; |
178 |
count++; |
nread++; |
179 |
/* XXX: remove the '\r' */ |
if (ch == '\r') |
180 |
if (ch == '\n' || count == (buflen - 1)) { |
continue; /* remove this char. */ |
181 |
|
if (ch == '\n' || nread == (buflen - 1)) { |
182 |
*buf = 0; |
*buf = 0; |
183 |
if (nbytes) |
if (nbytes) |
184 |
*nbytes = count; |
*nbytes = nread; |
185 |
return 0; |
return 0; |
186 |
} |
} |
187 |
} |
} |
216 |
Return value: 0 on success. */ |
Return value: 0 on success. */ |
217 |
static int |
static int |
218 |
sock_read (int fd, char *buf, int buflen, int *nbytes) |
sock_read (int fd, char *buf, int buflen, int *nbytes) |
219 |
{ |
{ |
220 |
DWORD nread; |
DWORD nread; |
221 |
int nleft = buflen; |
int nleft = buflen; |
222 |
int rc, n = 0; |
int rc, n = 0; |
223 |
|
|
224 |
while (nleft > 0) { |
while (nleft > 0) { |
225 |
if (n >= 10) |
if (n >= default_socket_timeout) |
226 |
return WPTERR_WINSOCK_TIMEOUT; |
return WPTERR_WINSOCK_TIMEOUT; |
227 |
rc = sock_select (fd, 1); |
rc = sock_select (fd, 1); |
228 |
if (rc == SOCKET_ERROR) |
if (rc == SOCKET_ERROR) |
300 |
return SOCKET_ERROR; |
return SOCKET_ERROR; |
301 |
} |
} |
302 |
nleft -= nwritten; |
nleft -= nwritten; |
303 |
buf += nwritten; |
buf += nwritten; |
304 |
} |
} |
305 |
|
|
306 |
return 0; |
return 0; |
307 |
} |
} |
308 |
|
|
309 |
|
|
|
/* Set the default keyserver. */ |
|
|
void |
|
|
set_default_kserver (void) |
|
|
{ |
|
|
char *p = get_reg_entry_keyserver ("Default"); |
|
|
free_if_alloc (default_keyserver); |
|
|
default_keyserver = p && *p != ' ' ? p : m_strdup (DEF_HKP_KEYSERVER); |
|
|
p = get_reg_entry_keyserver ("Default_Port"); |
|
|
if (p && *p) { |
|
|
default_keyserver_port = (WORD)strtoul (p, NULL, 10); |
|
|
free_if_alloc (p); |
|
|
} |
|
|
else |
|
|
default_keyserver_port = HKP_PORT; |
|
|
} |
|
|
|
|
|
|
|
310 |
/* Initialize the Winsock2 interface.*/ |
/* Initialize the Winsock2 interface.*/ |
311 |
int |
int |
312 |
wsock_init (void) |
wsock_init (void) |
317 |
log_debug ("wsock_init: WSAStartup failed ec=%d\r\n", net_errno); |
log_debug ("wsock_init: WSAStartup failed ec=%d\r\n", net_errno); |
318 |
return WPTERR_WINSOCK_INIT; |
return WPTERR_WINSOCK_INIT; |
319 |
} |
} |
|
set_default_kserver (); |
|
320 |
return 0; |
return 0; |
321 |
} |
} |
322 |
|
|
325 |
void |
void |
326 |
wsock_end (void) |
wsock_end (void) |
327 |
{ |
{ |
328 |
|
char *p; |
329 |
int i; |
int i; |
330 |
|
|
331 |
|
p = make_special_filename (CSIDL_APPDATA, "winpt\\keyserver.conf", NULL); |
332 |
|
kserver_save_conf (p); |
333 |
|
free_if_alloc (p); |
334 |
free_if_alloc (default_keyserver); |
free_if_alloc (default_keyserver); |
335 |
for (i=0; i < MAX_KEYSERVERS; i++) { |
for (i=0; i < MAX_KEYSERVERS; i++) { |
336 |
if (server[i].used) |
if (server[i].used && server[i].name != NULL) |
337 |
free_if_alloc (server[i].name); |
free_if_alloc (server[i].name); |
338 |
} |
} |
339 |
|
kserver_proxy_release (&proxy); |
340 |
WSACleanup (); |
WSACleanup (); |
341 |
} |
} |
342 |
|
|
363 |
} |
} |
364 |
|
|
365 |
|
|
366 |
|
/* Set default socket timeout for all reading operations. */ |
367 |
|
void |
368 |
|
kserver_set_socket_timeout (int nsec) |
369 |
|
{ |
370 |
|
if (nsec < 0) |
371 |
|
nsec = 0; |
372 |
|
default_socket_timeout = nsec; |
373 |
|
} |
374 |
|
|
375 |
|
|
376 |
/* Return the last keyserver error as a string. */ |
/* Return the last keyserver error as a string. */ |
377 |
const char* |
const char* |
378 |
kserver_strerror (void) |
kserver_strerror (void) |
418 |
/* If the request contains the keyid, it have to be |
/* If the request contains the keyid, it have to be |
419 |
always postfix with '0x'+keyid. This code checks |
always postfix with '0x'+keyid. This code checks |
420 |
if the keyid is a decimal value and if so if it contains |
if the keyid is a decimal value and if so if it contains |
421 |
the '0x'. If not it is inserted. */ |
the '0x'. If not it is inserted. */ |
422 |
const char* |
const char* |
423 |
kserver_check_keyid (const char *keyid) |
kserver_check_keyid (const char *keyid) |
424 |
{ |
{ |
437 |
|
|
438 |
/* Update the keyserver proxy user. */ |
/* Update the keyserver proxy user. */ |
439 |
static void |
static void |
440 |
kserver_update_proxyuser (const char *proxy_user, const char *proxy_pass) |
update_proxy_user (const char *proxy_user, const char *proxy_pass) |
441 |
{ |
{ |
442 |
char t[257]; /* user:pass = 127+1+127+1 = 257 */ |
char t[257]; /* user:pass = 127+1+127+1 = 257 */ |
443 |
int n = 0; |
int n = 0; |
448 |
if (!proxy.base64_user) |
if (!proxy.base64_user) |
449 |
BUG (0); |
BUG (0); |
450 |
_snprintf (t, sizeof (t)-1, "%s:%s", proxy_user, proxy_pass); |
_snprintf (t, sizeof (t)-1, "%s:%s", proxy_user, proxy_pass); |
451 |
base64_encode (t, proxy.base64_user, 257); |
base64_encode (t, proxy.base64_user); |
452 |
free_if_alloc (proxy.user); |
free_if_alloc (proxy.user); |
453 |
free_if_alloc (proxy.pass); |
free_if_alloc (proxy.pass); |
454 |
proxy.user = m_strdup (proxy_user); |
proxy.user = m_strdup (proxy_user); |
486 |
|
|
487 |
/* Get the port number from the given URL. */ |
/* Get the port number from the given URL. */ |
488 |
static int |
static int |
489 |
proto_from_URL (const char * buf) |
proto_from_URL (const char *buf) |
490 |
{ |
{ |
491 |
if (strstr( buf, "ldap")) |
if (strstr( buf, "ldap")) |
492 |
return KSPROTO_LDAP; |
return KSPROTO_LDAP; |
499 |
void |
void |
500 |
keyserver_set_default (const char *hostname, WORD port) |
keyserver_set_default (const char *hostname, WORD port) |
501 |
{ |
{ |
502 |
if (hostname) { |
if (hostname != NULL) { |
503 |
free_if_alloc (default_keyserver); |
free_if_alloc (default_keyserver); |
504 |
default_keyserver = m_strdup (hostname); |
default_keyserver = m_strdup (hostname); |
505 |
if (!default_keyserver) |
if (!default_keyserver) |
506 |
BUG (0); |
BUG (0); |
507 |
default_keyserver_port = port; |
default_keyserver_port = port; |
508 |
} |
} |
509 |
|
if (!port) |
510 |
|
port = HKP_PORT; |
511 |
|
if (!default_keyserver) |
512 |
|
default_keyserver = m_strdup (DEF_HKP_KEYSERVER); |
513 |
server[0].name = m_strdup (default_keyserver); |
server[0].name = m_strdup (default_keyserver); |
514 |
server[0].used = 1; |
server[0].used = 1; |
515 |
server[0].port = port; |
server[0].port = port; |
535 |
} |
} |
536 |
|
|
537 |
|
|
538 |
|
/* Save the keyserver config file in @conf. */ |
539 |
|
int |
540 |
|
kserver_save_conf (const char *conf) |
541 |
|
{ |
542 |
|
FILE *fp; |
543 |
|
int pos; |
544 |
|
|
545 |
|
fp = fopen (conf, "wb"); |
546 |
|
if (!fp) { |
547 |
|
msg_box (NULL, _("Could not save keyserver.conf file"), |
548 |
|
_("Keyserver"), MB_ERR); |
549 |
|
return -1; |
550 |
|
} |
551 |
|
|
552 |
|
fprintf (fp, "# do NOT manually modify this file, it will be " |
553 |
|
"generated automatically!!\r\n"); |
554 |
|
for (pos = 0; pos < MAX_KEYSERVERS; pos++) { |
555 |
|
if (!server[pos].used) |
556 |
|
continue; |
557 |
|
fprintf (fp, "%s:%d\r\n", server[pos].name, server[pos].port); |
558 |
|
} |
559 |
|
fclose (fp); |
560 |
|
return 0; |
561 |
|
} |
562 |
|
|
563 |
|
|
564 |
/* Load the keyserver config file @conf. */ |
/* Load the keyserver config file @conf. */ |
565 |
int |
int |
566 |
kserver_load_conf (const char *conf) |
kserver_load_conf (const char *conf) |
567 |
{ |
{ |
568 |
struct stat statbuf; |
struct stat statbuf; |
569 |
FILE *fp; |
FILE *fp; |
570 |
char buf[1024], * s, * p; |
char buf[1024], *s, *p; |
571 |
char *user = NULL, *pass = NULL; |
char *user = NULL, *pass = NULL; |
572 |
int no_config=0, chk_pos=0; |
int no_config=0, chk_pos=0; |
573 |
int pos; |
int pos; |
586 |
} |
} |
587 |
no_config=1; |
no_config=1; |
588 |
} |
} |
589 |
get_reg_proxy_prefs (&proxy.host, &proxy.port, &user, &pass); |
get_reg_proxy_prefs (&proxy); |
590 |
if (user && pass) |
if (user && pass) |
591 |
kserver_update_proxyuser (user, pass); |
update_proxy_user (user, pass); |
592 |
else if (user && !pass || !user && pass) { |
else if (user && !pass || !user && pass) { |
593 |
msg_box (NULL, _("Invalid proxy configuration." |
msg_box (NULL, _("Invalid proxy configuration." |
594 |
"You need to set a user and a password" |
"You need to set a user and a password" |
624 |
_("Keyserver Error"), MB_ERR); |
_("Keyserver Error"), MB_ERR); |
625 |
continue; |
continue; |
626 |
} |
} |
627 |
chk_pos=6; |
chk_pos = 6; |
628 |
if (strstr (s, "finger")) |
if (strstr (s, "finger")) |
629 |
chk_pos = 10; |
chk_pos = 10; |
630 |
p = strchr (s+chk_pos, ':'); |
p = strchr (s+chk_pos, ':'); |
653 |
if (fp) |
if (fp) |
654 |
fclose (fp); |
fclose (fp); |
655 |
if (!pos) { |
if (!pos) { |
656 |
/* 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 |
657 |
|
it has no valid entries. */ |
658 |
keyserver_set_default (NULL, HKP_PORT); |
keyserver_set_default (NULL, HKP_PORT); |
659 |
if (!no_config) |
if (!no_config) |
660 |
return WPTERR_CONFIG_FILE; |
return WPTERR_CONFIG_FILE; |
666 |
} |
} |
667 |
|
|
668 |
|
|
|
/* Return the proxy host and port if available. Null otherwise. */ |
|
|
const char* |
|
|
kserver_get_proxy (int *r_port) |
|
|
{ |
|
|
if (proxy.host) { |
|
|
if (r_port) |
|
|
*r_port = proxy.port; |
|
|
return proxy.host; |
|
|
} |
|
|
return NULL; |
|
|
} |
|
|
|
|
|
|
|
|
/* Return a requested item from the proxy environment. */ |
|
|
const char* |
|
|
kserver_get_proxy_info (int id) |
|
|
{ |
|
|
switch (id) { |
|
|
case PROXY_USER: return proxy.user; |
|
|
case PROXY_PASS: return proxy.pass; |
|
|
} |
|
|
return NULL; |
|
|
} |
|
|
|
|
|
|
|
669 |
/* Connect to the keyserver @hostname (port @port). |
/* Connect to the keyserver @hostname (port @port). |
670 |
Return value: 0 on success */ |
Return value: 0 on success */ |
671 |
int |
int |
716 |
return WPTERR_WINSOCK_CONNECT; |
return WPTERR_WINSOCK_CONNECT; |
717 |
} |
} |
718 |
|
|
719 |
|
if (proxy.proto == PROXY_PROTO_SOCKS5) { |
720 |
|
rc = socks_handshake (&proxy, fd, hostname, port); |
721 |
|
if (rc) { |
722 |
|
closesocket (fd); |
723 |
|
return WPTERR_GENERAL; /* XXX: use better code. */ |
724 |
|
} |
725 |
|
} |
726 |
|
|
727 |
if (conn_fd) |
if (conn_fd) |
728 |
*conn_fd = fd; |
*conn_fd = fd; |
729 |
WSASetLastError (0); |
WSASetLastError (0); |
795 |
return NULL; |
return NULL; |
796 |
} |
} |
797 |
|
|
798 |
if (proxy.user) { |
if (proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
799 |
_snprintf (request, reqlen-1, |
_snprintf (request, reqlen-1, |
800 |
"POST http://%s:%d/pks/add HTTP/1.0\r\n" |
"POST http://%s:%d/pks/add HTTP/1.0\r\n" |
801 |
"Referer: \r\n" |
"Referer: \r\n" |
850 |
if (!request) |
if (!request) |
851 |
BUG (0); |
BUG (0); |
852 |
|
|
853 |
if (proxy.host && proxy.user) { |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
854 |
_snprintf (request, 300, |
_snprintf (request, 300, |
855 |
"GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n" |
"GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n" |
856 |
"Proxy-Authorization: Basic %s\r\n\r\n", |
"Proxy-Authorization: Basic %s\r\n\r\n", |
857 |
skip_type_prefix (hostname), port, keyid, proxy.base64_user); |
skip_type_prefix (hostname), port, keyid, proxy.base64_user); |
858 |
} |
} |
859 |
else if (proxy.host) { |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
860 |
_snprintf (request, 300, |
_snprintf (request, 300, |
861 |
"GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n", |
"GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n", |
862 |
skip_type_prefix (hostname), port, keyid); |
skip_type_prefix (hostname), port, keyid); |
882 |
|
|
883 |
if (r_keylen) |
if (r_keylen) |
884 |
*r_keylen = n; |
*r_keylen = n; |
885 |
log_debug("%s\r\n", *r_key); |
log_debug ("%s\r\n", *r_key); |
886 |
rc = check_hkp_response (*r_key, 1); |
rc = check_hkp_response (*r_key, 1); |
887 |
if (rc) |
if (rc) |
888 |
goto leave; |
goto leave; |
965 |
} |
} |
966 |
|
|
967 |
|
|
968 |
|
/* Check keyserver response. */ |
969 |
|
static int |
970 |
|
kserver_search_chkresp (int fd) |
971 |
|
{ |
972 |
|
char buf[128]; |
973 |
|
int n=0; |
974 |
|
|
975 |
|
/* parse response 'HTTP/1.0 500 OK' */ |
976 |
|
if (sock_getline (fd, buf, 127, &n)) |
977 |
|
return WPTERR_KEYSERVER_NOTFOUND; |
978 |
|
|
979 |
|
log_debug ("kserver_search_chkpresp: %s\r\n", buf); |
980 |
|
if (strncmp (buf, "HTTP/1.", 7)) |
981 |
|
return WPTERR_KEYSERVER_NOTFOUND; |
982 |
|
if (strncmp (buf+(8+1), "200", 3)) |
983 |
|
return WPTERR_KEYSERVER_NOTFOUND; |
984 |
|
return 0; |
985 |
|
} |
986 |
|
|
987 |
|
|
988 |
|
/* End the keyserver search procedure. */ |
989 |
|
void |
990 |
|
kserver_search_end (int conn_fd) |
991 |
|
{ |
992 |
|
log_debug ("kserver_search_end: fd=%d\r\n", conn_fd); |
993 |
|
closesocket (conn_fd); |
994 |
|
} |
995 |
|
|
996 |
|
|
997 |
|
/* Begin keyserver search procedure. */ |
998 |
int |
int |
999 |
kserver_search_init (const char *hostname, WORD port, |
kserver_search_begin (const char *hostname, WORD port, |
1000 |
const char *keyid, int *conn_fd) |
const char *pattern, int *conn_fd) |
1001 |
{ |
{ |
1002 |
char *request = NULL; |
char *request = NULL; |
1003 |
char *enc_keyid = NULL; |
char *enc_patt = NULL; |
1004 |
int n = 0; |
int n; |
1005 |
int rc, sock_fd; |
int rc, sock_fd; |
1006 |
|
|
1007 |
rc = kserver_connect (hostname, port, &sock_fd); |
rc = kserver_connect (hostname, port, &sock_fd); |
1010 |
goto leave; |
goto leave; |
1011 |
} |
} |
1012 |
|
|
1013 |
enc_keyid = URL_encode (keyid, strlen (keyid), NULL); |
enc_patt = URL_encode (pattern, strlen (pattern), NULL); |
1014 |
n=300; |
n = 140 + strlen (enc_patt) + strlen (hostname) + 32 + 2; |
1015 |
|
if (proxy.base64_user) |
1016 |
|
n += strlen (proxy.base64_user) + 1; |
1017 |
request = new char[n+1]; |
request = new char[n+1]; |
1018 |
if (!request) |
if (!request) |
1019 |
BUG (0); |
BUG (0); |
1020 |
|
|
1021 |
if (proxy.host && proxy.user) { |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
1022 |
_snprintf (request, n, |
_snprintf (request, n, |
1023 |
"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" |
1024 |
"Proxy-Authorization: Basic %s\r\n\r\n", |
"Proxy-Authorization: Basic %s\r\n\r\n", |
1025 |
skip_type_prefix (hostname), port, enc_keyid, proxy.base64_user); |
skip_type_prefix (hostname), port, enc_patt, proxy.base64_user); |
1026 |
} |
} |
1027 |
else if (proxy.host) { |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
1028 |
_snprintf (request, n, |
_snprintf (request, n, |
1029 |
"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", |
1030 |
skip_type_prefix (hostname), port, enc_keyid); |
skip_type_prefix (hostname), port, enc_patt); |
1031 |
} |
} |
1032 |
else { |
else { |
1033 |
_snprintf (request, n, |
_snprintf (request, n, |
1034 |
"GET /pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n", |
"GET /pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n", |
1035 |
enc_keyid); |
enc_patt); |
1036 |
} |
} |
1037 |
|
|
1038 |
log_debug ("kserver_search_init:\r\n%s\r\n", request); |
log_debug ("kserver_search_begin:\r\n%s\r\n", request); |
1039 |
|
|
1040 |
if (sock_write (sock_fd, request, strlen (request)) == SOCKET_ERROR) { |
if (sock_write (sock_fd, request, strlen (request)) == SOCKET_ERROR) { |
1041 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1042 |
goto leave; |
goto leave; |
1043 |
} |
} |
1044 |
|
|
1045 |
|
rc = kserver_search_chkresp (sock_fd); |
1046 |
|
if (rc) { |
1047 |
|
closesocket (sock_fd); |
1048 |
|
sock_fd = 0; |
1049 |
|
} |
1050 |
|
|
1051 |
*conn_fd = sock_fd; |
*conn_fd = sock_fd; |
1052 |
|
|
1053 |
leave: |
leave: |
1054 |
free_if_alloc (request); |
free_if_alloc (request); |
1055 |
free_if_alloc (enc_keyid); |
free_if_alloc (enc_patt); |
1056 |
return rc; |
return rc; |
1057 |
} |
} |
1058 |
|
|
1059 |
|
|
|
/* Check keyserver response. */ |
|
|
int |
|
|
kserver_search_chkresp (int fd) |
|
|
{ |
|
|
char buf[128]; |
|
|
int n=0; |
|
|
|
|
|
/* parse response 'HTTP/1.0 500 OK' */ |
|
|
if (sock_getline (fd, buf, 127, &n)) |
|
|
return WPTERR_KEYSERVER_NOTFOUND; |
|
|
|
|
|
log_debug ("kserver_search_chkpresp: %s\r\n", buf); |
|
|
if (strncmp (buf, "HTTP/1.", 7)) |
|
|
return WPTERR_KEYSERVER_NOTFOUND; |
|
|
if (strncmp (buf+(8+1), "200", 3)) |
|
|
return WPTERR_KEYSERVER_NOTFOUND; |
|
|
return 0; |
|
|
} |
|
1060 |
|
|
1061 |
|
|
1062 |
/* Convert an iso date @iso_date (YYYY-MM-DD) into the locale |
/* Convert an iso date @iso_date (YYYY-MM-DD) into the locale |
1091 |
|
|
1092 |
|
|
1093 |
int |
int |
1094 |
kserver_search (int fd, keyserver_key *key) |
kserver_search_next (int fd, keyserver_key *key) |
1095 |
{ |
{ |
1096 |
char buf[1024], *p; |
char buf[1024], *p; |
1097 |
int uidlen, nbytes, pos = 0; |
int uidlen, nbytes, pos = 0; |
1098 |
|
|
1099 |
log_debug ("keyserver_search:\r\n"); |
log_debug ("keyserver_search_next:\r\n"); |
1100 |
|
|
1101 |
if (sock_getline (fd, buf, sizeof (buf) - 1, &nbytes)) |
if (sock_getline (fd, buf, sizeof (buf) - 1, &nbytes)) |
1102 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
1103 |
|
|
1104 |
|
/* XXX: use maschine readable option. */ |
1105 |
log_debug ("%s\r\n", buf); |
log_debug ("%s\r\n", buf); |
1106 |
|
|
1107 |
if (!strncmp (buf, "pub", 3)) { |
if (!strncmp (buf, "pub", 3)) { |
1162 |
} |
} |
1163 |
|
|
1164 |
|
|
|
/* Replace all current proxy items with the items |
|
|
from the context @ctx. */ |
|
|
void |
|
|
kserver_change_proxy (keyserver_proxy_ctx *ctx) |
|
|
{ |
|
|
proxy.port = ctx->port; |
|
|
free_if_alloc (proxy.host); |
|
|
proxy.host = ctx->host? m_strdup (ctx->host) : NULL; |
|
|
free_if_alloc (proxy.pass); |
|
|
proxy.pass = ctx->pass? m_strdup (ctx->pass) : NULL; |
|
|
free_if_alloc (proxy.user); |
|
|
proxy.user = ctx->user? m_strdup (ctx->user) : NULL; |
|
|
set_reg_proxy_prefs (proxy.host, proxy.port, proxy.user, proxy.pass); |
|
|
} |
|
|
|
|
|
|
|
1165 |
/* Release mbmers in the proxy context @ctx. */ |
/* Release mbmers in the proxy context @ctx. */ |
1166 |
void |
void |
1167 |
proxy_release (keyserver_proxy_ctx *ctx) |
kserver_proxy_release (keyserver_proxy_t ctx) |
1168 |
{ |
{ |
1169 |
free_if_alloc (ctx->host); |
free_if_alloc (ctx->host); |
1170 |
free_if_alloc (ctx->pass); |
free_if_alloc (ctx->pass); |
1171 |
free_if_alloc (ctx->user); |
free_if_alloc (ctx->user); |
1172 |
|
ctx->port = ctx->proto = 0; |
1173 |
} |
} |
1174 |
|
|
1175 |
|
|
1205 |
int |
int |
1206 |
ldap_recvkey (const char *host, const char *keyid, char *key, int maxkeylen) |
ldap_recvkey (const char *host, const char *keyid, char *key, int maxkeylen) |
1207 |
{ |
{ |
1208 |
FILE *inp; |
FILE *fp; |
|
DWORD n; |
|
1209 |
const char *s; |
const char *s; |
1210 |
char *ksprg = NULL, *p = NULL, *sep; |
char *ksprg = NULL, *p = NULL, *sep; |
1211 |
char temp[512], outf[512]; |
char inf[256], outf[256], buf[512]; |
1212 |
|
DWORD n; |
1213 |
int start_key = 0, failed = 0; |
int start_key = 0, failed = 0; |
1214 |
int rc = 0; |
int rc = 0; |
1215 |
|
|
1229 |
"%s: could not find LDAP keyserver module!", ksprg); |
"%s: could not find LDAP keyserver module!", ksprg); |
1230 |
rc = -1; |
rc = -1; |
1231 |
goto leave; |
goto leave; |
1232 |
} |
} |
1233 |
GetTempPath (sizeof (temp)-1, temp); |
get_temp_name (outf, sizeof (outf)-1, keyid); |
1234 |
_snprintf (outf, sizeof (outf)-1, "%s%s.out", temp, keyid); |
get_temp_name (inf, sizeof (inf)-1, NULL); |
1235 |
strcat (temp, keyid); |
fp = fopen (inf, "w+b"); |
1236 |
inp = fopen (temp, "w+b"); |
if (!fp) { |
1237 |
if (!inp) { |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", inf, |
|
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", temp, |
|
1238 |
winpt_strerror (WPTERR_FILE_OPEN)); |
winpt_strerror (WPTERR_FILE_OPEN)); |
1239 |
rc = -1; |
rc = -1; |
1240 |
goto leave; |
goto leave; |
1241 |
} |
} |
1242 |
fprintf (inp, |
fprintf (fp, |
1243 |
"VERSION 1\n" |
"VERSION 1\n" |
1244 |
"PROGRAM 1.4.3-cvs\n" |
"PROGRAM 1.4.3-cvs\n" |
1245 |
"SCHEME ldap\n" |
"SCHEME ldap\n" |
1248 |
"\n" |
"\n" |
1249 |
"%s\n", |
"%s\n", |
1250 |
host? skip_type_prefix (host): "64.94.85.200", keyid); |
host? skip_type_prefix (host): "64.94.85.200", keyid); |
1251 |
fclose (inp); |
fclose (fp); |
1252 |
|
|
1253 |
p = new char[strlen (ksprg) + strlen (temp) + strlen (outf) + 32]; |
p = new char[strlen (ksprg) + strlen (inf) + strlen (outf) + 32]; |
1254 |
if (!p) |
if (!p) |
1255 |
BUG (NULL); |
BUG (NULL); |
1256 |
sprintf (p, "%s -o %s %s", ksprg, outf, temp); |
sprintf (p, "%s -o %s %s", ksprg, outf, inf); |
1257 |
if (spawn_application (p)) { |
if (spawn_application (p)) { |
1258 |
rc = -1; |
rc = -1; |
1259 |
goto leave; |
goto leave; |
1260 |
} |
} |
|
DeleteFile (temp); |
|
1261 |
|
|
1262 |
inp = fopen (outf, "rb"); |
fp = fopen (outf, "rb"); |
1263 |
if (!inp) { |
if (!fp) { |
1264 |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf, |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf, |
1265 |
winpt_strerror (WPTERR_FILE_OPEN)); |
winpt_strerror (WPTERR_FILE_OPEN)); |
1266 |
rc = -1; |
rc = -1; |
1267 |
goto leave; |
goto leave; |
1268 |
} |
} |
1269 |
memset (key, 0, maxkeylen); |
memset (key, 0, maxkeylen); |
1270 |
while (!feof (inp)) { |
while (!feof (fp)) { |
1271 |
s = fgets (temp, sizeof (temp)-1, inp); |
s = fgets (buf, sizeof (buf)-1, fp); |
1272 |
if (!s) |
if (!s) |
1273 |
break; |
break; |
1274 |
if (strstr (s, "KEY") && strstr (s, "FAILED")) { |
if (strstr (s, "KEY") && strstr (s, "FAILED")) { |
1281 |
} |
} |
1282 |
if (!start_key) |
if (!start_key) |
1283 |
continue; |
continue; |
1284 |
strcat (key, temp); |
strcat (key, buf); |
1285 |
maxkeylen -= strlen (temp); |
maxkeylen -= strlen (buf); |
1286 |
if (maxkeylen < 0 || (strstr (s, "KEY") && strstr (s, "END"))) |
if (maxkeylen < 0 || (strstr (s, "KEY") && strstr (s, "END"))) |
1287 |
break; |
break; |
1288 |
} |
} |
1289 |
fclose (inp); |
fclose (fp); |
1290 |
|
|
1291 |
leave: |
leave: |
1292 |
if (failed || !strlen (key)) |
if (failed || !strlen (key)) |
1293 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
1294 |
|
DeleteFile (inf); |
1295 |
DeleteFile (outf); |
DeleteFile (outf); |
1296 |
free_if_alloc (p); |
free_if_alloc (p); |
1297 |
free_if_alloc (ksprg); |
free_if_alloc (ksprg); |
1299 |
} |
} |
1300 |
|
|
1301 |
|
|
|
static int |
|
|
finger_readline (int fd, char *buf, int nbytes) |
|
|
{ |
|
|
char c = 0; |
|
|
int n, pos = 0; |
|
|
|
|
|
while (nbytes > 0) { |
|
|
n = recv (fd, &c, 1, 0); |
|
|
if (n <= 0) |
|
|
break; |
|
|
if (c == '\n') |
|
|
break; |
|
|
buf[pos++] = c; |
|
|
nbytes--; |
|
|
} |
|
|
if (nbytes > 0) |
|
|
buf[pos++] = '\0'; |
|
|
if (n <= 0) |
|
|
pos = n; |
|
|
return pos; |
|
|
} |
|
|
|
|
|
|
|
1302 |
/* Receive an key via FINGER from host @host with the user @user. |
/* Receive an key via FINGER from host @host with the user @user. |
1303 |
On success @key contains the key. */ |
On success @key contains the key. */ |
1304 |
int |
int |
1305 |
finger_recvkey (const char *host, const char *user, char *key, int maxkeylen) |
finger_recvkey (const char *host, const char *user, char *key, int maxkeylen) |
1306 |
{ |
{ |
|
struct hostent * hp; |
|
|
struct sockaddr_in saddr; |
|
1307 |
char buf[128]; |
char buf[128]; |
1308 |
int fd, nread; |
int fd, nread; |
1309 |
int start_key = 0; |
int start_key = 0; |
1310 |
int rc=0; |
int rc=0; |
1311 |
|
|
1312 |
fd = socket (PF_INET, SOCK_STREAM, 0); |
rc = kserver_connect (host, FINGER_PORT, &fd); |
1313 |
if (fd == -1) |
if (rc) |
1314 |
return WPTERR_WINSOCK_SOCKET; |
return rc; |
|
hp = gethostbyname (skip_type_prefix (host)); |
|
|
if (!hp) { |
|
|
closesocket (fd); |
|
|
return WPTERR_WINSOCK_RESOLVE; |
|
|
} |
|
1315 |
|
|
1316 |
memset (&saddr, 0, sizeof (saddr)); |
sock_write (fd, user, strlen (user)); |
1317 |
saddr.sin_family = AF_INET; |
sock_write (fd, "\r\n", 2); |
|
saddr.sin_port = htons (FINGER_PORT); |
|
|
saddr.sin_addr = *(struct in_addr *)hp->h_addr; |
|
|
if (connect (fd, (struct sockaddr *)&saddr, sizeof (saddr))) { |
|
|
closesocket (fd); |
|
|
return WPTERR_WINSOCK_CONNECT; |
|
|
} |
|
|
send (fd, user, strlen (user), 0); |
|
|
send (fd, "\r\n", 2, 0); |
|
1318 |
|
|
1319 |
memset (key, 0, maxkeylen); |
memset (key, 0, maxkeylen); |
1320 |
while (maxkeylen > 0) { |
while (maxkeylen > 0) { |
1321 |
nread = finger_readline (fd, buf, sizeof (buf)-1); |
if (sock_getline (fd, buf, sizeof (buf), &nread)) |
1322 |
if (nread <= 0) |
break; |
|
break; |
|
1323 |
strcat (buf, "\n"); |
strcat (buf, "\n"); |
1324 |
if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) { |
if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) { |
1325 |
strcat (key, buf); |
strcat (key, buf); |