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[] = { |
|
"hkp://wwwkeys.nl.pgp.net", |
|
|
"hkp://wwwkeys.pl.pgp.net", |
|
|
"hkp://wwwkeys.at.pgp.net", |
|
|
"hkp://wwwkeys.ch.pgp.net", |
|
|
"hkp://wwwkeys.de.pgp.net", |
|
|
"hkp://wwwkeys.dk.pgp.net", |
|
|
"hkp://wwwkeys.cz.pgp.net", |
|
|
"hkp://wwwkeys.es.pgp.net", |
|
|
"hkp://wwwkeys.eu.pgp.net", |
|
|
"hkp://wwwkeys.uk.pgp.net", |
|
|
"hkp://wwwkeys.us.pgp.net", |
|
47 |
"hkp://gnv.us.ks.cryptnet.net", |
"hkp://gnv.us.ks.cryptnet.net", |
48 |
|
"hkp://keyserver.kjsl.com", |
49 |
|
"hkp://sks.keyserver.penguin.de", |
50 |
"hkp://subkeys.pgp.net", |
"hkp://subkeys.pgp.net", |
51 |
"ldap://keyserver.pgp.com", |
"ldap://keyserver.pgp.com", |
52 |
NULL |
NULL |
55 |
|
|
56 |
static char hkp_errmsg[1024]; /* Holds the error message from the server */ |
static char hkp_errmsg[1024]; /* Holds the error message from the server */ |
57 |
static int hkp_err = 0; /* != 0 indicates an error occurred. */ |
static int hkp_err = 0; /* != 0 indicates an error occurred. */ |
|
static DWORD conf_timestamp = 0;/* timestamp of the configuration fiele. */ |
|
58 |
|
|
59 |
char *default_keyserver = NULL; |
/* Default keyserver and port. */ |
60 |
WORD default_keyserver_port = 0; |
char *default_keyserver = NULL; |
61 |
|
WORD default_keyserver_port = 0; |
62 |
|
|
63 |
|
/* Default socket timeout. */ |
64 |
|
static int default_socket_timeout = 10; |
65 |
|
|
66 |
/* Basic64 encode the input @inputstr to @outputstr. */ |
/* Basic64 encode the input @inbuf to @outbuf. */ |
67 |
static void |
static void |
68 |
base64_encode (const char *inputstr, char *outputstr, int maxlen) |
base64_encode (const char *inbuf, char *outbuf) |
69 |
{ |
{ |
70 |
int index = 0, temp = 0, res = 0, i = 0, inputlen = 0, len = 0; |
char base64code[] = |
71 |
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
72 |
|
int index = 0, temp = 0, res = 0; |
73 |
|
int i = 0, inputlen = 0, len = 0; |
74 |
|
|
75 |
/* XXX: check len < maxlen. */ |
inputlen = strlen (inbuf); |
76 |
inputlen = strlen (inputstr); |
for (i = 0; i < inputlen; i++) { |
|
for (i = 0; i <inputlen; i++) { |
|
77 |
res = temp; |
res = temp; |
78 |
res = (res << 8) | (inputstr[i] & 0xFF); |
res = (res << 8) | (inbuf[i] & 0xFF); |
79 |
switch (index++) { |
switch (index++) { |
80 |
case 0: outputstr[len++] = base64code[res >> 2 & 0x3F]; res &= 0x3; break; |
case 0: |
81 |
case 1: outputstr[len++] = base64code[res >> 4 & 0x3F]; res &= 0xF; break; |
outbuf[len++] = base64code[res >> 2 & 0x3F]; |
82 |
case 2: outputstr[len++] = base64code[res >> 6 & 0x3F]; |
res &= 0x3; |
83 |
outputstr[len++] = base64code[res & 0x3F]; res = index = 0; break; |
break; |
84 |
|
case 1: |
85 |
|
outbuf[len++] = base64code[res >> 4 & 0x3F]; |
86 |
|
res &= 0xF; |
87 |
|
break; |
88 |
|
case 2: |
89 |
|
outbuf[len++] = base64code[res >> 6 & 0x3F]; |
90 |
|
outbuf[len++] = base64code[res & 0x3F]; |
91 |
|
res = index = 0; |
92 |
|
break; |
93 |
} |
} |
94 |
temp = res; |
temp = res; |
95 |
} |
} |
96 |
|
|
97 |
switch (index) { |
if (index == 2) { |
98 |
case 0: break; |
outbuf[len++] = base64code[temp << 2 & 0x3F]; |
99 |
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++] = '='; |
|
100 |
} |
} |
101 |
|
else if (index == 1) { |
102 |
outputstr[len] = '\0'; |
outbuf[len++] = base64code[temp << 4 & 0x3F]; |
103 |
|
outbuf[len++] = '='; |
104 |
|
outbuf[len++] = '='; |
105 |
|
} |
106 |
|
|
107 |
|
outbuf[len] = '\0'; |
108 |
} |
} |
109 |
|
|
110 |
|
|
159 |
sock_getline (int fd, char *buf, int buflen, int *nbytes) |
sock_getline (int fd, char *buf, int buflen, int *nbytes) |
160 |
{ |
{ |
161 |
char ch; |
char ch; |
162 |
int count = 0; |
int nread = 0; |
163 |
|
|
164 |
if (nbytes) |
if (nbytes) |
165 |
*nbytes = 0; |
*nbytes = 0; |
166 |
*buf = 0; |
*buf = 0; |
167 |
while (recv (fd, &ch, 1, 0) > 0) { |
while (recv (fd, &ch, 1, 0) > 0) { |
168 |
*buf++ = ch; |
*buf++ = ch; |
169 |
count++; |
nread++; |
170 |
/* XXX: remove the '\r' */ |
if (ch == '\r') |
171 |
if (ch == '\n' || count == (buflen - 1)) { |
continue; /* remove this char. */ |
172 |
|
if (ch == '\n' || nread == (buflen - 1)) { |
173 |
*buf = 0; |
*buf = 0; |
174 |
if (nbytes) |
if (nbytes) |
175 |
*nbytes = count; |
*nbytes = nread; |
176 |
return 0; |
return 0; |
177 |
} |
} |
178 |
} |
} |
207 |
Return value: 0 on success. */ |
Return value: 0 on success. */ |
208 |
static int |
static int |
209 |
sock_read (int fd, char *buf, int buflen, int *nbytes) |
sock_read (int fd, char *buf, int buflen, int *nbytes) |
210 |
{ |
{ |
211 |
DWORD nread; |
DWORD nread; |
212 |
int nleft = buflen; |
int nleft = buflen; |
213 |
int rc, n = 0; |
int rc, n = 0; |
214 |
|
|
215 |
while (nleft > 0) { |
while (nleft > 0) { |
216 |
if (n >= 10) |
if (n >= default_socket_timeout) |
217 |
return WPTERR_WINSOCK_TIMEOUT; |
return WPTERR_WINSOCK_TIMEOUT; |
218 |
rc = sock_select (fd, 1); |
rc = sock_select (fd, 1); |
219 |
if (rc == SOCKET_ERROR) |
if (rc == SOCKET_ERROR) |
238 |
return 0; |
return 0; |
239 |
} |
} |
240 |
|
|
241 |
|
/* Read much data as possible from the socket @fd and |
242 |
|
return the data in @buffer. Caller must free data. |
243 |
|
Return value: 0 on success. */ |
244 |
|
int |
245 |
|
sock_read_ext (int fd, char **buffer, int *r_bufferlen) |
246 |
|
{ |
247 |
|
gpgme_data_t dh; |
248 |
|
char buf[1024], *p; |
249 |
|
size_t n=0; |
250 |
|
int nread, rc; |
251 |
|
|
252 |
|
gpgme_data_new (&dh); |
253 |
|
while (n < 10) { |
254 |
|
rc = sock_select (fd, 1); |
255 |
|
if (rc == SOCKET_ERROR) { |
256 |
|
gpgme_data_release (dh); |
257 |
|
return rc; |
258 |
|
} |
259 |
|
else if (!rc) |
260 |
|
n++; |
261 |
|
else { |
262 |
|
nread = recv (fd, buf, sizeof (buf), 0); |
263 |
|
if (nread == SOCKET_ERROR) |
264 |
|
return SOCKET_ERROR; |
265 |
|
else if (!nread) |
266 |
|
break; |
267 |
|
gpgme_data_write (dh, buf, nread); |
268 |
|
} |
269 |
|
} |
270 |
|
gpg_data_putc (dh, '\0'); |
271 |
|
p = gpgme_data_release_and_get_mem (dh, &n); |
272 |
|
*buffer = m_strdup (p); |
273 |
|
if (r_bufferlen) |
274 |
|
*r_bufferlen = n; |
275 |
|
gpgme_free (p); |
276 |
|
return 0; |
277 |
|
} |
278 |
|
|
279 |
|
|
280 |
/* Write the buffer @buf with the length @buflen to a socket @fd. */ |
/* Write the buffer @buf with the length @buflen to a socket @fd. */ |
281 |
static int |
static int |
291 |
return SOCKET_ERROR; |
return SOCKET_ERROR; |
292 |
} |
} |
293 |
nleft -= nwritten; |
nleft -= nwritten; |
294 |
buf += nwritten; |
buf += nwritten; |
295 |
} |
} |
296 |
|
|
297 |
return 0; |
return 0; |
298 |
} |
} |
299 |
|
|
300 |
|
|
|
/* 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; |
|
|
} |
|
|
|
|
|
|
|
301 |
/* Initialize the Winsock2 interface.*/ |
/* Initialize the Winsock2 interface.*/ |
302 |
int |
int |
303 |
wsock_init (void) |
wsock_init (void) |
308 |
log_debug ("wsock_init: WSAStartup failed ec=%d\r\n", net_errno); |
log_debug ("wsock_init: WSAStartup failed ec=%d\r\n", net_errno); |
309 |
return WPTERR_WINSOCK_INIT; |
return WPTERR_WINSOCK_INIT; |
310 |
} |
} |
|
set_default_kserver (); |
|
311 |
return 0; |
return 0; |
312 |
} |
} |
313 |
|
|
316 |
void |
void |
317 |
wsock_end (void) |
wsock_end (void) |
318 |
{ |
{ |
319 |
|
char *p; |
320 |
int i; |
int i; |
321 |
|
|
322 |
|
p = make_special_filename (CSIDL_APPDATA, "winpt\\keyserver.conf", NULL); |
323 |
|
kserver_save_conf (p); |
324 |
|
free_if_alloc (p); |
325 |
free_if_alloc (default_keyserver); |
free_if_alloc (default_keyserver); |
|
default_keyserver = NULL; |
|
326 |
for (i=0; i < MAX_KEYSERVERS; i++) { |
for (i=0; i < MAX_KEYSERVERS; i++) { |
327 |
if (server[i].used) |
if (server[i].used && server[i].name != NULL) |
328 |
free_if_alloc (server[i].name); |
free_if_alloc (server[i].name); |
329 |
} |
} |
330 |
|
kserver_proxy_release (&proxy); |
331 |
WSACleanup (); |
WSACleanup (); |
332 |
} |
} |
333 |
|
|
354 |
} |
} |
355 |
|
|
356 |
|
|
357 |
|
/* Set default socket timeout for all reading operations. */ |
358 |
|
void |
359 |
|
kserver_set_socket_timeout (int nsec) |
360 |
|
{ |
361 |
|
if (nsec < 0) |
362 |
|
nsec = 0; |
363 |
|
default_socket_timeout = nsec; |
364 |
|
} |
365 |
|
|
366 |
|
|
367 |
/* Return the last keyserver error as a string. */ |
/* Return the last keyserver error as a string. */ |
368 |
const char* |
const char* |
369 |
kserver_strerror (void) |
kserver_strerror (void) |
409 |
/* If the request contains the keyid, it have to be |
/* If the request contains the keyid, it have to be |
410 |
always postfix with '0x'+keyid. This code checks |
always postfix with '0x'+keyid. This code checks |
411 |
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 |
412 |
the '0x'. If not it is inserted. */ |
the '0x'. If not it is inserted. */ |
413 |
const char* |
const char* |
414 |
kserver_check_keyid (const char *keyid) |
kserver_check_keyid (const char *keyid) |
415 |
{ |
{ |
428 |
|
|
429 |
/* Update the keyserver proxy user. */ |
/* Update the keyserver proxy user. */ |
430 |
static void |
static void |
431 |
kserver_update_proxyuser (const char *proxy_user, const char *proxy_pass) |
update_proxy_user (const char *proxy_user, const char *proxy_pass) |
432 |
{ |
{ |
433 |
char t[257]; /* user:pass = 127+1+127+1 = 257 */ |
char t[257]; /* user:pass = 127+1+127+1 = 257 */ |
434 |
int n = 0; |
int n = 0; |
439 |
if (!proxy.base64_user) |
if (!proxy.base64_user) |
440 |
BUG (0); |
BUG (0); |
441 |
_snprintf (t, sizeof (t)-1, "%s:%s", proxy_user, proxy_pass); |
_snprintf (t, sizeof (t)-1, "%s:%s", proxy_user, proxy_pass); |
442 |
base64_encode (t, proxy.base64_user, 257); |
base64_encode (t, proxy.base64_user); |
443 |
free_if_alloc (proxy.user); |
free_if_alloc (proxy.user); |
444 |
free_if_alloc (proxy.pass); |
free_if_alloc (proxy.pass); |
445 |
proxy.user = m_strdup (proxy_user); |
proxy.user = m_strdup (proxy_user); |
477 |
|
|
478 |
/* Get the port number from the given URL. */ |
/* Get the port number from the given URL. */ |
479 |
static int |
static int |
480 |
proto_from_URL (const char * buf) |
proto_from_URL (const char *buf) |
481 |
{ |
{ |
482 |
if (strstr( buf, "ldap")) |
if (strstr( buf, "ldap")) |
483 |
return KSPROTO_LDAP; |
return KSPROTO_LDAP; |
490 |
void |
void |
491 |
keyserver_set_default (const char *hostname, WORD port) |
keyserver_set_default (const char *hostname, WORD port) |
492 |
{ |
{ |
493 |
if (hostname) { |
if (hostname != NULL) { |
494 |
free_if_alloc (default_keyserver); |
free_if_alloc (default_keyserver); |
495 |
default_keyserver = m_strdup (hostname); |
default_keyserver = m_strdup (hostname); |
496 |
if (!default_keyserver) |
if (!default_keyserver) |
497 |
BUG (0); |
BUG (0); |
498 |
default_keyserver_port = port; |
default_keyserver_port = port; |
499 |
} |
} |
500 |
|
if (!port) |
501 |
|
port = HKP_PORT; |
502 |
|
if (!default_keyserver) |
503 |
|
default_keyserver = m_strdup (DEF_HKP_KEYSERVER); |
504 |
server[0].name = m_strdup (default_keyserver); |
server[0].name = m_strdup (default_keyserver); |
505 |
server[0].used = 1; |
server[0].used = 1; |
506 |
server[0].port = port; |
server[0].port = port; |
526 |
} |
} |
527 |
|
|
528 |
|
|
529 |
|
/* Save the keyserver config file in @conf. */ |
530 |
int |
int |
531 |
kserver_load_conf (const char * conf) |
kserver_save_conf (const char *conf) |
532 |
{ |
{ |
|
struct stat statbuf; |
|
533 |
FILE *fp; |
FILE *fp; |
534 |
char buf[1024], * s, * p; |
int pos; |
535 |
|
|
536 |
|
fp = fopen (conf, "wb"); |
537 |
|
if (!fp) { |
538 |
|
msg_box (NULL, _("Could not save keyserver.conf file"), |
539 |
|
_("Keyserver"), MB_ERR); |
540 |
|
return -1; |
541 |
|
} |
542 |
|
|
543 |
|
fprintf (fp, "# do NOT manually modify this file, it will be " |
544 |
|
"generated automatically!!\r\n"); |
545 |
|
for (pos = 0; pos < MAX_KEYSERVERS; pos++) { |
546 |
|
if (!server[pos].used) |
547 |
|
continue; |
548 |
|
fprintf (fp, "%s:%d\r\n", server[pos].name, server[pos].port); |
549 |
|
} |
550 |
|
fclose (fp); |
551 |
|
return 0; |
552 |
|
} |
553 |
|
|
554 |
|
|
555 |
|
/* Load the keyserver config file @conf. */ |
556 |
|
int |
557 |
|
kserver_load_conf (const char *conf) |
558 |
|
{ |
559 |
|
FILE *fp; |
560 |
|
char buf[1024], *s, *p; |
561 |
char *user = NULL, *pass = NULL; |
char *user = NULL, *pass = NULL; |
562 |
int no_config=0, chk_pos=0; |
int no_config=0, chk_pos=0; |
563 |
int pos; |
int pos; |
576 |
} |
} |
577 |
no_config=1; |
no_config=1; |
578 |
} |
} |
579 |
get_reg_proxy_prefs (&proxy.host, &proxy.port, &user, &pass); |
get_reg_proxy_prefs (&proxy); |
580 |
if (user && pass) |
if (user && pass) |
581 |
kserver_update_proxyuser (user, pass); |
update_proxy_user (user, pass); |
582 |
else if (user && !pass || !user && pass) { |
else if (user && !pass || !user && pass) { |
583 |
msg_box( NULL, _("Invalid proxy configuration." |
msg_box (NULL, _("Invalid proxy configuration." |
584 |
"You need to set a user and a password" |
"You need to set a user and a password" |
585 |
"to use proxy authentication!"), _("Proxy Error"), MB_ERR ); |
"to use proxy authentication!"), |
586 |
|
_("Proxy Error"), MB_ERR); |
587 |
} |
} |
588 |
/* check if the host has a http:// prefix and skip it */ |
/* check if the host has a http:// prefix and skip it */ |
589 |
if( proxy.host && !strncmp( proxy.host, "http://", 7 ) ) { |
if (proxy.host && !strncmp (proxy.host, "http://", 7)) { |
590 |
char *host = m_strdup (proxy.host+7); |
char *host = m_strdup (proxy.host+7); |
591 |
if (!host) |
if (!host) |
592 |
BUG (0); |
BUG (0); |
597 |
free_if_alloc (pass); |
free_if_alloc (pass); |
598 |
|
|
599 |
pos = 0; |
pos = 0; |
600 |
while( fp && !feof( fp ) ) { |
while (fp && !feof (fp)) { |
601 |
s = fgets (buf, sizeof (buf)-1, fp); |
s = fgets (buf, sizeof (buf)-1, fp); |
602 |
if (!s) |
if (!s) |
603 |
break; |
break; |
614 |
_("Keyserver Error"), MB_ERR); |
_("Keyserver Error"), MB_ERR); |
615 |
continue; |
continue; |
616 |
} |
} |
617 |
chk_pos=6; |
chk_pos = 6; |
618 |
if (strstr (s, "finger")) |
if (strstr (s, "finger")) |
619 |
chk_pos = 10; |
chk_pos = 10; |
620 |
p = strchr (s+chk_pos, ':'); |
p = strchr (s+chk_pos, ':'); |
634 |
memcpy (server[pos].name, s, (p-s)); |
memcpy (server[pos].name, s, (p-s)); |
635 |
} |
} |
636 |
pos++; |
pos++; |
637 |
if (pos > MAX_KEYSERVERS) |
if (pos > MAX_KEYSERVERS) { |
638 |
{ |
msg_box (NULL, _("The keyserver limit is exceeded"), |
639 |
msg_box (NULL, _("The keyserver limit is exceeded"), _("Keyserver Warning"), MB_INFO); |
_("Keyserver Warning"), MB_INFO); |
640 |
break; |
break; |
641 |
} |
} |
642 |
} |
} |
643 |
if (fp) |
if (fp) |
644 |
fclose (fp); |
fclose (fp); |
645 |
if (!pos) { |
if (!pos) { |
646 |
/* 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 |
647 |
|
it has no valid entries. */ |
648 |
keyserver_set_default (NULL, HKP_PORT); |
keyserver_set_default (NULL, HKP_PORT); |
649 |
if (!no_config) |
if (!no_config) |
650 |
return WPTERR_CONFIG_FILE; |
return WPTERR_CONFIG_FILE; |
651 |
} |
} |
652 |
|
|
|
if (!stat (conf, &statbuf)) |
|
|
conf_timestamp = statbuf.st_mtime; |
|
653 |
return 0; |
return 0; |
|
} /* kserver_load_conf */ |
|
|
|
|
|
|
|
|
/* 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; |
|
654 |
} |
} |
655 |
|
|
656 |
|
|
704 |
return WPTERR_WINSOCK_CONNECT; |
return WPTERR_WINSOCK_CONNECT; |
705 |
} |
} |
706 |
|
|
707 |
|
if (proxy.proto == PROXY_PROTO_SOCKS5) { |
708 |
|
rc = socks_handshake (&proxy, fd, hostname, port); |
709 |
|
if (rc) { |
710 |
|
closesocket (fd); |
711 |
|
return WPTERR_GENERAL; /* XXX: use better code. */ |
712 |
|
} |
713 |
|
} |
714 |
|
|
715 |
if (conn_fd) |
if (conn_fd) |
716 |
*conn_fd = fd; |
*conn_fd = fd; |
717 |
WSASetLastError (0); |
WSASetLastError (0); |
719 |
} |
} |
720 |
|
|
721 |
|
|
722 |
/* Perform URL-encoding on the given pubkey blob. */ |
static bool |
723 |
static char* |
URL_must_encoded (const char *url) |
|
kserver_urlencode (const char *pubkey, size_t octets, size_t *newlen) |
|
724 |
{ |
{ |
725 |
char *p, numbuf[5]; |
if (strchr (url, '.') || strchr (url, '@') || strchr (url, ' ')) |
726 |
size_t j; |
return true; |
727 |
size_t i, size; |
return false; |
728 |
|
} |
|
p = new char [2*octets+1]; |
|
|
if (!p) |
|
|
BUG (0); |
|
729 |
|
|
730 |
for (size = 0, i = 0; i < octets; i++) { |
|
731 |
if (isalnum (pubkey[i]) || pubkey[i] == '-') { |
/* Perform URL encoding of the given data. */ |
732 |
p[size] = pubkey[i]; |
static char* |
733 |
size++; |
URL_encode (const char *url, size_t ulen, size_t *ret_nlen) |
734 |
} |
{ |
735 |
else if (pubkey[i] == ' ') { |
gpgme_data_t hd; |
736 |
p[size] = '+'; |
char numbuf[5], *pp, *p; |
737 |
size++; |
size_t i, n; |
738 |
} |
|
739 |
|
gpgme_data_new (&hd); |
740 |
|
for (i=0; i < ulen; i++) { |
741 |
|
if (isalnum (url[i]) || url[i] == '-') |
742 |
|
gpg_data_putc (hd, url[i]); |
743 |
|
else if (url[i] == ' ') |
744 |
|
gpg_data_putc (hd, '+'); |
745 |
else { |
else { |
746 |
sprintf (numbuf, "%%%02X", pubkey[i]); |
sprintf (numbuf, "%%%02X", url[i]); |
747 |
for (j = 0; j < strlen (numbuf); j++) { |
gpgme_data_write (hd, numbuf, strlen (numbuf)); |
|
p[size] = numbuf[j]; |
|
|
size++; |
|
|
} |
|
748 |
} |
} |
749 |
} |
} |
750 |
p[size] = '\0'; |
gpg_data_putc (hd, '\0'); |
751 |
*newlen = size; |
|
752 |
|
/* Copy memory to avoid that we need to use gpgme_free later. */ |
753 |
|
pp = gpgme_data_release_and_get_mem (hd, &n); |
754 |
|
p = m_strdup (pp); |
755 |
|
gpgme_free (pp); |
756 |
|
if (ret_nlen) |
757 |
|
*ret_nlen = n; |
758 |
return p; |
return p; |
759 |
} |
} |
760 |
|
|
777 |
request = new char[reqlen]; |
request = new char[reqlen]; |
778 |
if (!request) |
if (!request) |
779 |
BUG (0); |
BUG (0); |
780 |
enc_pubkey = kserver_urlencode (pubkey, octets, &enc_octets); |
enc_pubkey = URL_encode (pubkey, octets, &enc_octets); |
781 |
if (!enc_pubkey || !enc_octets) { |
if (!enc_pubkey || !enc_octets) { |
782 |
free_if_alloc (request); |
free_if_alloc (request); |
783 |
return NULL; |
return NULL; |
784 |
} |
} |
785 |
|
|
786 |
if (proxy.user) { |
if (proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
787 |
_snprintf (request, reqlen-1, |
_snprintf (request, reqlen-1, |
788 |
"POST http://%s:%d/pks/add HTTP/1.0\r\n" |
"POST http://%s:%d/pks/add HTTP/1.0\r\n" |
789 |
"Referer: \r\n" |
"Referer: \r\n" |
819 |
} |
} |
820 |
|
|
821 |
|
|
|
/* Interface for receiving a public key. */ |
|
822 |
int |
int |
823 |
kserver_recvkey (const char *hostname, WORD port, const char *keyid, |
kserver_recvkey_ext (const char *hostname, WORD port, const char *keyid, |
824 |
char *key, int maxkeylen) |
char **r_key, int *r_keylen) |
825 |
{ |
{ |
826 |
char *request = NULL; |
char *request = NULL; |
827 |
int conn_fd; |
int conn_fd; |
838 |
if (!request) |
if (!request) |
839 |
BUG (0); |
BUG (0); |
840 |
|
|
841 |
if (proxy.host && proxy.user) { |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
842 |
_snprintf (request, 300, |
_snprintf (request, 300, |
843 |
"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" |
844 |
"Proxy-Authorization: Basic %s\r\n\r\n", |
"Proxy-Authorization: Basic %s\r\n\r\n", |
845 |
skip_type_prefix (hostname), port, keyid, proxy.base64_user); |
skip_type_prefix (hostname), port, keyid, proxy.base64_user); |
846 |
} |
} |
847 |
else if (proxy.host) { |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
848 |
_snprintf (request, 300, |
_snprintf (request, 300, |
849 |
"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", |
850 |
skip_type_prefix (hostname), port, keyid); |
skip_type_prefix (hostname), port, keyid); |
862 |
goto leave; |
goto leave; |
863 |
} |
} |
864 |
|
|
865 |
rc = sock_read( conn_fd, key, maxkeylen, &n ); |
rc = sock_read_ext (conn_fd, r_key, &n); |
866 |
if( rc == SOCKET_ERROR ) { |
if (rc == SOCKET_ERROR) { |
867 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
868 |
goto leave; |
goto leave; |
869 |
} |
} |
870 |
|
|
871 |
log_debug("%s\r\n", key); |
if (r_keylen) |
872 |
rc = check_hkp_response (key, 1); |
*r_keylen = n; |
873 |
|
log_debug ("%s\r\n", *r_key); |
874 |
|
rc = check_hkp_response (*r_key, 1); |
875 |
if (rc) |
if (rc) |
876 |
goto leave; |
goto leave; |
877 |
|
|
883 |
return rc; |
return rc; |
884 |
} |
} |
885 |
|
|
886 |
|
/* Interface for receiving a public key. */ |
887 |
|
int |
888 |
|
kserver_recvkey (const char *hostname, WORD port, const char *keyid, |
889 |
|
char *key, int maxkeylen) |
890 |
|
{ |
891 |
|
char *tmpkey; |
892 |
|
int rc, nread=0; |
893 |
|
|
894 |
|
/* XXX: just a wrapper around the new method, replace it |
895 |
|
soon as possible. */ |
896 |
|
rc = kserver_recvkey_ext (hostname, port, keyid, &tmpkey, &nread); |
897 |
|
if (rc) |
898 |
|
return rc; |
899 |
|
|
900 |
|
if (nread > maxkeylen) { |
901 |
|
free_if_alloc (tmpkey); |
902 |
|
return WPTERR_GENERAL; |
903 |
|
} |
904 |
|
strcpy (key, tmpkey); |
905 |
|
free_if_alloc (tmpkey); |
906 |
|
return 0; |
907 |
|
} |
908 |
|
|
909 |
|
|
910 |
/* Interface to send a public key. */ |
/* Interface to send a public key. */ |
911 |
int |
int |
953 |
} |
} |
954 |
|
|
955 |
|
|
956 |
|
/* Check keyserver response. */ |
957 |
|
static int |
958 |
|
kserver_search_chkresp (int fd) |
959 |
|
{ |
960 |
|
char buf[128]; |
961 |
|
int n=0; |
962 |
|
|
963 |
|
/* parse response 'HTTP/1.0 500 OK' */ |
964 |
|
if (sock_getline (fd, buf, 127, &n)) |
965 |
|
return WPTERR_KEYSERVER_NOTFOUND; |
966 |
|
|
967 |
|
log_debug ("kserver_search_chkpresp: %s\r\n", buf); |
968 |
|
if (strncmp (buf, "HTTP/1.", 7)) |
969 |
|
return WPTERR_KEYSERVER_NOTFOUND; |
970 |
|
if (strncmp (buf+(8+1), "200", 3)) |
971 |
|
return WPTERR_KEYSERVER_NOTFOUND; |
972 |
|
return 0; |
973 |
|
} |
974 |
|
|
975 |
|
|
976 |
|
/* End the keyserver search procedure. */ |
977 |
|
void |
978 |
|
kserver_search_end (int conn_fd) |
979 |
|
{ |
980 |
|
log_debug ("kserver_search_end: fd=%d\r\n", conn_fd); |
981 |
|
closesocket (conn_fd); |
982 |
|
} |
983 |
|
|
984 |
|
|
985 |
|
/* Begin keyserver search procedure. */ |
986 |
int |
int |
987 |
kserver_search_init (const char *hostname, WORD port, |
kserver_search_begin (const char *hostname, WORD port, |
988 |
const char *keyid, int *conn_fd) |
const char *pattern, int *conn_fd) |
989 |
{ |
{ |
990 |
char *request = NULL; |
char *request = NULL; |
991 |
int n=0; |
char *enc_patt = NULL; |
992 |
|
int n; |
993 |
int rc, sock_fd; |
int rc, sock_fd; |
994 |
|
|
995 |
rc = kserver_connect (hostname, port, &sock_fd); |
rc = kserver_connect (hostname, port, &sock_fd); |
996 |
if (rc) { |
if (rc) { |
997 |
*conn_fd = 0; |
*conn_fd = 0; |
998 |
goto leave; |
goto leave; |
999 |
} |
} |
1000 |
|
|
1001 |
n=300; |
enc_patt = URL_encode (pattern, strlen (pattern), NULL); |
1002 |
|
n = 140 + strlen (enc_patt) + strlen (hostname) + 32 + 2; |
1003 |
|
if (proxy.base64_user) |
1004 |
|
n += strlen (proxy.base64_user) + 1; |
1005 |
request = new char[n+1]; |
request = new char[n+1]; |
1006 |
if (!request) |
if (!request) |
1007 |
BUG (0); |
BUG (0); |
1008 |
|
|
1009 |
if (proxy.host && proxy.user) { |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
1010 |
_snprintf (request, n, |
_snprintf (request, n, |
1011 |
"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" |
1012 |
"Proxy-Authorization: Basic %s\r\n\r\n", |
"Proxy-Authorization: Basic %s\r\n\r\n", |
1013 |
skip_type_prefix (hostname), port, keyid, proxy.base64_user); |
skip_type_prefix (hostname), port, enc_patt, proxy.base64_user); |
1014 |
} |
} |
1015 |
else if (proxy.host) { |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
1016 |
_snprintf (request, n, |
_snprintf (request, n, |
1017 |
"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", |
1018 |
skip_type_prefix (hostname), port, keyid); |
skip_type_prefix (hostname), port, enc_patt); |
1019 |
} |
} |
1020 |
else { |
else { |
1021 |
_snprintf (request, n, |
_snprintf (request, n, |
1022 |
"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", |
1023 |
|
enc_patt); |
1024 |
} |
} |
1025 |
|
|
1026 |
log_debug ("kserver_search_init:\r\n%s\r\n", request); |
log_debug ("kserver_search_begin:\r\n%s\r\n", request); |
1027 |
|
|
1028 |
if (sock_write (sock_fd, request, strlen (request)) == SOCKET_ERROR) { |
if (sock_write (sock_fd, request, strlen (request)) == SOCKET_ERROR) { |
1029 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1030 |
goto leave; |
goto leave; |
1031 |
} |
} |
1032 |
|
|
1033 |
|
rc = kserver_search_chkresp (sock_fd); |
1034 |
|
if (rc) { |
1035 |
|
closesocket (sock_fd); |
1036 |
|
sock_fd = 0; |
1037 |
|
} |
1038 |
|
|
1039 |
*conn_fd = sock_fd; |
*conn_fd = sock_fd; |
1040 |
|
|
1041 |
leave: |
leave: |
1042 |
free_if_alloc (request); |
free_if_alloc (request); |
1043 |
|
free_if_alloc (enc_patt); |
1044 |
return rc; |
return rc; |
1045 |
} |
} |
1046 |
|
|
1047 |
|
|
1048 |
/* Check keyserver response. */ |
|
1049 |
int |
|
1050 |
kserver_search_chkresp (int fd) |
/* Convert an iso date @iso_date (YYYY-MM-DD) into the locale |
1051 |
|
representation and store it into @loc_date. |
1052 |
|
Return value: 0 on success. */ |
1053 |
|
static int |
1054 |
|
parse_iso_date (const char *iso_date, char *loc_date, size_t loclen) |
1055 |
{ |
{ |
1056 |
char buf[128]; |
SYSTEMTIME st; |
1057 |
int n=0; |
char buf[16] = {0}, *p; |
1058 |
|
int pos=0; |
1059 |
|
|
1060 |
|
strncpy (buf, iso_date, sizeof (buf)-1); |
1061 |
|
p = strtok (buf, "-"); |
1062 |
|
while (p != NULL) { |
1063 |
|
switch (pos) { |
1064 |
|
case 0: st.wYear = (WORD)atoi (p); pos++; break; |
1065 |
|
case 1: st.wMonth = (WORD)atoi (p); pos++; break; |
1066 |
|
case 2: st.wDay = (WORD)atoi (p); pos++; break; |
1067 |
|
default: break; |
1068 |
|
} |
1069 |
|
p = strtok (NULL, "-"); |
1070 |
|
} |
1071 |
|
if (pos != 3) |
1072 |
|
return -1; |
1073 |
|
|
1074 |
/* parse response 'HTTP/1.0 500 OK' */ |
if (!GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, |
1075 |
if (sock_getline (fd, buf, 127, &n)) |
NULL, loc_date, loclen)) |
1076 |
return WPTERR_KEYSERVER_NOTFOUND; |
return -1; |
|
|
|
|
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; |
|
1077 |
return 0; |
return 0; |
1078 |
} |
} |
1079 |
|
|
1080 |
|
|
1081 |
int |
int |
1082 |
kserver_search (int fd, keyserver_key * key) |
kserver_search_next (int fd, keyserver_key *key) |
1083 |
{ |
{ |
1084 |
char buf[1024], *p; |
char buf[1024], *p; |
1085 |
int uidlen, nbytes, pos = 0; |
int uidlen, nbytes, pos = 0; |
1086 |
|
|
1087 |
log_debug ("keyserver_search:\r\n"); |
log_debug ("keyserver_search_next:\r\n"); |
1088 |
|
|
1089 |
if (sock_getline (fd, buf, sizeof (buf) - 1, &nbytes)) |
if (sock_getline (fd, buf, sizeof (buf) - 1, &nbytes)) |
1090 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
1091 |
|
|
1092 |
|
/* XXX: use maschine readable option. */ |
1093 |
log_debug ("%s\r\n", buf); |
log_debug ("%s\r\n", buf); |
1094 |
|
|
1095 |
if (!strncmp (buf, "pub", 3)) { |
if (!strncmp (buf, "pub", 3)) { |
1107 |
pos = p - buf + 5; |
pos = p - buf + 5; |
1108 |
memcpy (key->date, buf + pos, 10); |
memcpy (key->date, buf + pos, 10); |
1109 |
key->date[10] = '\0'; |
key->date[10] = '\0'; |
1110 |
|
parse_iso_date (key->date, key->date, sizeof (key->date)-1); |
1111 |
if (revoked) { |
if (revoked) { |
1112 |
strcpy (key->uid, "KEY REVOKED: not checked"); |
strcpy (key->uid, "KEY REVOKED: not checked"); |
1113 |
return 0; |
return 0; |
1118 |
p++; |
p++; |
1119 |
p++; |
p++; |
1120 |
uidlen = strlen (p) - 10; |
uidlen = strlen (p) - 10; |
1121 |
|
if (!strstr (p, "<") && !strstr (p, ">")) { |
1122 |
|
pos=0; |
1123 |
|
while (p && *p && pos < sizeof (key->uid)-1) { |
1124 |
|
if (*p == '<') |
1125 |
|
break; |
1126 |
|
key->uid[pos++] = *p++; |
1127 |
|
} |
1128 |
|
key->uid[pos] ='\0'; |
1129 |
|
return 0; |
1130 |
|
} |
1131 |
|
|
1132 |
|
if (uidlen > sizeof (key->uid)-1) |
1133 |
|
uidlen = sizeof (key->uid)-1; |
1134 |
memcpy (key->uid, p, uidlen); |
memcpy (key->uid, p, uidlen); |
1135 |
key->uid[uidlen] = '\0'; |
key->uid[uidlen] = '\0'; |
1136 |
strcat (key->uid, ">"); |
strcat (key->uid, ">"); |
1147 |
key->bits = 0; |
key->bits = 0; |
1148 |
memset (key, 0, sizeof *key); |
memset (key, 0, sizeof *key); |
1149 |
return 0; |
return 0; |
|
} /* 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 */ |
|
|
|
|
|
|
|
|
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 ); |
|
|
} /* kserver_change_proxy */ |
|
|
|
|
|
|
|
|
int |
|
|
kserver_read_config( const char *fname, keyserver_cfgfile **ret_cfg ) |
|
|
{ |
|
|
FILE *fp; |
|
|
keyserver_cfgfile *cfg; |
|
|
char buf[256]; |
|
|
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 */ |
|
|
|
|
|
|
|
|
int |
|
|
kserver_write_config( const char * fname, keyserver_cfgfile * cfg ) |
|
|
{ |
|
|
|
|
|
return 0; |
|
1150 |
} |
} |
1151 |
|
|
1152 |
|
|
|
void |
|
|
kserver_cfgfile_release( keyserver_cfgfile *cfg ) |
|
|
{ |
|
|
keyserver_node *k, *k2; |
|
|
|
|
|
proxy_release( &cfg->proxy ); |
|
|
for( k = cfg->list; cfg->nlist--; k = k2 ) { |
|
|
k2 = k->next; |
|
|
free_if_alloc( k ); |
|
|
} |
|
|
free_if_alloc( cfg ); |
|
|
} /* kserver_cfgfile_release */ |
|
|
|
|
|
|
|
1153 |
/* Release mbmers in the proxy context @ctx. */ |
/* Release mbmers in the proxy context @ctx. */ |
1154 |
void |
void |
1155 |
proxy_release( keyserver_proxy_ctx *ctx ) |
kserver_proxy_release (keyserver_proxy_t ctx) |
1156 |
{ |
{ |
1157 |
free_if_alloc (ctx->host); |
free_if_alloc (ctx->host); |
1158 |
free_if_alloc (ctx->pass); |
free_if_alloc (ctx->pass); |
1159 |
free_if_alloc (ctx->user); |
free_if_alloc (ctx->user); |
1160 |
|
ctx->port = ctx->proto = 0; |
1161 |
} |
} |
1162 |
|
|
1163 |
|
|
1190 |
|
|
1191 |
/* Receive an key via LDAP from host @host with the keyid @keyid. |
/* Receive an key via LDAP from host @host with the keyid @keyid. |
1192 |
@key contains the key on success. */ |
@key contains the key on success. */ |
1193 |
int |
int |
1194 |
ldap_recvkey (const char *host, const char *keyid, char *key, int maxkeylen) |
ldap_recvkey (const char *host, const char *keyid, char *key, int maxkeylen) |
1195 |
{ |
{ |
1196 |
FILE *inp; |
FILE *fp; |
|
DWORD n; |
|
1197 |
const char *s; |
const char *s; |
1198 |
char *ksprg = NULL, *p = NULL, *sep; |
char *ksprg = NULL, *p = NULL, *sep; |
1199 |
char temp[512], outf[512]; |
char inf[256], outf[256], buf[512]; |
1200 |
|
DWORD n; |
1201 |
int start_key = 0, failed = 0; |
int start_key = 0, failed = 0; |
1202 |
int rc = 0; |
int rc = 0; |
1203 |
|
|
1217 |
"%s: could not find LDAP keyserver module!", ksprg); |
"%s: could not find LDAP keyserver module!", ksprg); |
1218 |
rc = -1; |
rc = -1; |
1219 |
goto leave; |
goto leave; |
1220 |
} |
} |
1221 |
GetTempPath (sizeof (temp)-1, temp); |
get_temp_name (outf, sizeof (outf)-1, keyid); |
1222 |
_snprintf (outf, sizeof (outf)-1, "%s%s.out", temp, keyid); |
get_temp_name (inf, sizeof (inf)-1, NULL); |
1223 |
strcat (temp, keyid); |
fp = fopen (inf, "w+b"); |
1224 |
inp = fopen (temp, "w+b"); |
if (!fp) { |
1225 |
if (!inp) { |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", inf, |
|
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", temp, |
|
1226 |
winpt_strerror (WPTERR_FILE_OPEN)); |
winpt_strerror (WPTERR_FILE_OPEN)); |
1227 |
rc = -1; |
rc = -1; |
1228 |
goto leave; |
goto leave; |
1229 |
} |
} |
1230 |
fprintf (inp, |
fprintf (fp, |
1231 |
"VERSION 1\n" |
"VERSION 1\n" |
1232 |
"PROGRAM 1.4.3-cvs\n" |
"PROGRAM 1.4.3-cvs\n" |
1233 |
"SCHEME ldap\n" |
"SCHEME ldap\n" |
1236 |
"\n" |
"\n" |
1237 |
"%s\n", |
"%s\n", |
1238 |
host? skip_type_prefix (host): "64.94.85.200", keyid); |
host? skip_type_prefix (host): "64.94.85.200", keyid); |
1239 |
fclose (inp); |
fclose (fp); |
1240 |
|
|
1241 |
p = new char[strlen (ksprg) + strlen (temp) + strlen (outf) + 32]; |
p = new char[strlen (ksprg) + strlen (inf) + strlen (outf) + 32]; |
1242 |
if (!p) |
if (!p) |
1243 |
BUG (NULL); |
BUG (NULL); |
1244 |
sprintf (p, "%s -o %s %s", ksprg, outf, temp); |
sprintf (p, "%s -o %s %s", ksprg, outf, inf); |
1245 |
if (spawn_application (p)) { |
if (spawn_application (p)) { |
1246 |
rc = -1; |
rc = -1; |
1247 |
goto leave; |
goto leave; |
1248 |
} |
} |
|
DeleteFile (temp); |
|
1249 |
|
|
1250 |
inp = fopen (outf, "rb"); |
fp = fopen (outf, "rb"); |
1251 |
if (!inp) { |
if (!fp) { |
1252 |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf, |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf, |
1253 |
winpt_strerror (WPTERR_FILE_OPEN)); |
winpt_strerror (WPTERR_FILE_OPEN)); |
1254 |
rc = -1; |
rc = -1; |
1255 |
goto leave; |
goto leave; |
1256 |
} |
} |
1257 |
memset (key, 0, maxkeylen); |
memset (key, 0, maxkeylen); |
1258 |
while (!feof (inp)) { |
while (!feof (fp)) { |
1259 |
s = fgets (temp, sizeof (temp)-1, inp); |
s = fgets (buf, sizeof (buf)-1, fp); |
1260 |
if (!s) |
if (!s) |
1261 |
break; |
break; |
1262 |
if (strstr (s, "KEY") && strstr (s, "FAILED")) { |
if (strstr (s, "KEY") && strstr (s, "FAILED")) { |
1269 |
} |
} |
1270 |
if (!start_key) |
if (!start_key) |
1271 |
continue; |
continue; |
1272 |
strcat (key, temp); |
strcat (key, buf); |
1273 |
maxkeylen -= strlen (temp); |
maxkeylen -= strlen (buf); |
1274 |
if (maxkeylen < 0 || (strstr (s, "KEY") && strstr (s, "END"))) |
if (maxkeylen < 0 || (strstr (s, "KEY") && strstr (s, "END"))) |
1275 |
break; |
break; |
1276 |
} |
} |
1277 |
fclose (inp); |
fclose (fp); |
1278 |
|
|
1279 |
leave: |
leave: |
1280 |
if (failed || !strlen (key)) |
if (failed || !strlen (key)) |
1281 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
1282 |
|
DeleteFile (inf); |
1283 |
DeleteFile (outf); |
DeleteFile (outf); |
1284 |
free_if_alloc (p); |
free_if_alloc (p); |
1285 |
free_if_alloc (ksprg); |
free_if_alloc (ksprg); |
1287 |
} |
} |
1288 |
|
|
1289 |
|
|
|
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; |
|
|
} |
|
|
|
|
|
|
|
1290 |
/* Receive an key via FINGER from host @host with the user @user. |
/* Receive an key via FINGER from host @host with the user @user. |
1291 |
On success @key contains the key. */ |
On success @key contains the key. */ |
1292 |
int |
int |
1293 |
finger_recvkey (const char *host, const char *user, char *key, int maxkeylen) |
finger_recvkey (const char *host, const char *user, char *key, int maxkeylen) |
1294 |
{ |
{ |
|
struct hostent * hp; |
|
|
struct sockaddr_in saddr; |
|
1295 |
char buf[128]; |
char buf[128]; |
1296 |
int fd, nread; |
int fd, nread; |
1297 |
int start_key = 0; |
int start_key = 0; |
1298 |
int rc=0; |
int rc=0; |
1299 |
|
|
1300 |
fd = socket (PF_INET, SOCK_STREAM, 0); |
rc = kserver_connect (host, FINGER_PORT, &fd); |
1301 |
if (fd == -1) |
if (rc) |
1302 |
return WPTERR_WINSOCK_SOCKET; |
return rc; |
|
hp = gethostbyname (skip_type_prefix (host)); |
|
|
if (!hp) { |
|
|
closesocket (fd); |
|
|
return WPTERR_WINSOCK_RESOLVE; |
|
|
} |
|
1303 |
|
|
1304 |
memset (&saddr, 0, sizeof (saddr)); |
sock_write (fd, user, strlen (user)); |
1305 |
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); |
|
1306 |
|
|
1307 |
memset (key, 0, maxkeylen); |
memset (key, 0, maxkeylen); |
1308 |
while (maxkeylen > 0) { |
while (maxkeylen > 0) { |
1309 |
nread = finger_readline (fd, buf, sizeof (buf)-1); |
if (sock_getline (fd, buf, sizeof (buf), &nread)) |
1310 |
if (nread <= 0) |
break; |
|
break; |
|
1311 |
strcat (buf, "\n"); |
strcat (buf, "\n"); |
1312 |
if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) { |
if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) { |
1313 |
strcat (key, buf); |
strcat (key, buf); |