1 |
/* wptKeyserver.cpp - W32 Keyserver Access |
/* wptKeyserver.cpp - W32 Keyserver Access |
2 |
* Copyright (C) 2000-2005 Timo Schulz |
* Copyright (C) 2000-2006 Timo Schulz |
3 |
* Copyright (C) 2001 Marco Cunha |
* Copyright (C) 2001 Marco Cunha |
4 |
* |
* |
5 |
* This file is part of WinPT. |
* This file is part of WinPT. |
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 |
|
/* 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 = 6; |
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 |
|
if (nbytes) |
216 |
|
*nbytes = 0; |
217 |
while (nleft > 0) { |
while (nleft > 0) { |
218 |
if (n >= 10) |
if (n >= default_socket_timeout) |
219 |
return WPTERR_WINSOCK_TIMEOUT; |
return WPTERR_WINSOCK_TIMEOUT; |
220 |
rc = sock_select (fd, 1); |
rc = sock_select (fd, 1); |
221 |
if (rc == SOCKET_ERROR) |
if (rc == SOCKET_ERROR) |
240 |
return 0; |
return 0; |
241 |
} |
} |
242 |
|
|
243 |
|
/* Read much data as possible from the socket @fd and |
244 |
|
return the data in @buffer. Caller must free data. |
245 |
|
Return value: 0 on success. */ |
246 |
|
int |
247 |
|
sock_read_ext (int fd, char **buffer, int *r_bufferlen) |
248 |
|
{ |
249 |
|
gpgme_data_t dh; |
250 |
|
char buf[1024], *p; |
251 |
|
size_t n=0; |
252 |
|
int nread, rc; |
253 |
|
|
254 |
|
gpgme_data_new (&dh); |
255 |
|
while (n < 10) { |
256 |
|
rc = sock_select (fd, 1); |
257 |
|
if (rc == SOCKET_ERROR) { |
258 |
|
gpgme_data_release (dh); |
259 |
|
return rc; |
260 |
|
} |
261 |
|
else if (!rc) |
262 |
|
n++; |
263 |
|
else { |
264 |
|
nread = recv (fd, buf, sizeof (buf), 0); |
265 |
|
if (nread == SOCKET_ERROR) |
266 |
|
return SOCKET_ERROR; |
267 |
|
else if (!nread) |
268 |
|
break; |
269 |
|
gpgme_data_write (dh, buf, nread); |
270 |
|
} |
271 |
|
} |
272 |
|
gpg_data_putc (dh, '\0'); |
273 |
|
p = gpgme_data_release_and_get_mem (dh, &n); |
274 |
|
*buffer = m_strdup (p); |
275 |
|
if (r_bufferlen) |
276 |
|
*r_bufferlen = n; |
277 |
|
gpgme_free (p); |
278 |
|
return 0; |
279 |
|
} |
280 |
|
|
281 |
|
|
282 |
/* Write the buffer @buf with the length @buflen to a socket @fd. */ |
/* Write the buffer @buf with the length @buflen to a socket @fd. */ |
283 |
static int |
static int |
293 |
return SOCKET_ERROR; |
return SOCKET_ERROR; |
294 |
} |
} |
295 |
nleft -= nwritten; |
nleft -= nwritten; |
296 |
buf += nwritten; |
buf += nwritten; |
297 |
} |
} |
298 |
|
|
299 |
return 0; |
return 0; |
300 |
} |
} |
301 |
|
|
302 |
|
|
|
/* 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; |
|
|
} |
|
|
|
|
|
|
|
303 |
/* Initialize the Winsock2 interface.*/ |
/* Initialize the Winsock2 interface.*/ |
304 |
int |
int |
305 |
wsock_init (void) |
wsock_init (void) |
310 |
log_debug ("wsock_init: WSAStartup failed ec=%d\r\n", net_errno); |
log_debug ("wsock_init: WSAStartup failed ec=%d\r\n", net_errno); |
311 |
return WPTERR_WINSOCK_INIT; |
return WPTERR_WINSOCK_INIT; |
312 |
} |
} |
|
set_default_kserver (); |
|
313 |
return 0; |
return 0; |
314 |
} |
} |
315 |
|
|
318 |
void |
void |
319 |
wsock_end (void) |
wsock_end (void) |
320 |
{ |
{ |
321 |
|
char *p; |
322 |
int i; |
int i; |
323 |
|
|
324 |
|
p = make_special_filename (CSIDL_APPDATA, "winpt\\keyserver.conf", NULL); |
325 |
|
kserver_save_conf (p); |
326 |
|
free_if_alloc (p); |
327 |
free_if_alloc (default_keyserver); |
free_if_alloc (default_keyserver); |
|
default_keyserver = NULL; |
|
328 |
for (i=0; i < MAX_KEYSERVERS; i++) { |
for (i=0; i < MAX_KEYSERVERS; i++) { |
329 |
if (server[i].used) |
if (server[i].used && server[i].name != NULL) |
330 |
free_if_alloc (server[i].name); |
free_if_alloc (server[i].name); |
331 |
} |
} |
332 |
|
kserver_proxy_release (&proxy); |
333 |
WSACleanup (); |
WSACleanup (); |
334 |
} |
} |
335 |
|
|
356 |
} |
} |
357 |
|
|
358 |
|
|
359 |
|
/* Set default socket timeout for all reading operations. */ |
360 |
|
void |
361 |
|
kserver_set_socket_timeout (int nsec) |
362 |
|
{ |
363 |
|
if (nsec < 0) |
364 |
|
nsec = 0; |
365 |
|
default_socket_timeout = nsec; |
366 |
|
} |
367 |
|
|
368 |
|
|
369 |
/* Return the last keyserver error as a string. */ |
/* Return the last keyserver error as a string. */ |
370 |
const char* |
const char* |
371 |
kserver_strerror (void) |
kserver_strerror (void) |
411 |
/* If the request contains the keyid, it have to be |
/* If the request contains the keyid, it have to be |
412 |
always postfix with '0x'+keyid. This code checks |
always postfix with '0x'+keyid. This code checks |
413 |
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 |
414 |
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 |
{ |
{ |
430 |
|
|
431 |
/* Update the keyserver proxy user. */ |
/* Update the keyserver proxy user. */ |
432 |
static void |
static void |
433 |
kserver_update_proxyuser (const char *proxy_user, const char *proxy_pass) |
update_proxy_user (const char *proxy_user, const char *proxy_pass) |
434 |
{ |
{ |
435 |
char t[257]; /* user:pass = 127+1+127+1 = 257 */ |
char t[257]; /* user:pass = 127+1+127+1 = 257 */ |
436 |
int n = 0; |
int n = 0; |
441 |
if (!proxy.base64_user) |
if (!proxy.base64_user) |
442 |
BUG (0); |
BUG (0); |
443 |
_snprintf (t, sizeof (t)-1, "%s:%s", proxy_user, proxy_pass); |
_snprintf (t, sizeof (t)-1, "%s:%s", proxy_user, proxy_pass); |
444 |
base64_encode (t, proxy.base64_user, 257); |
base64_encode (t, proxy.base64_user); |
445 |
free_if_alloc (proxy.user); |
free_if_alloc (proxy.user); |
446 |
free_if_alloc (proxy.pass); |
free_if_alloc (proxy.pass); |
447 |
proxy.user = m_strdup (proxy_user); |
proxy.user = m_strdup (proxy_user); |
479 |
|
|
480 |
/* Get the port number from the given URL. */ |
/* Get the port number from the given URL. */ |
481 |
static int |
static int |
482 |
proto_from_URL (const char * buf) |
proto_from_URL (const char *buf) |
483 |
{ |
{ |
484 |
if (strstr( buf, "ldap")) |
if (strstr( buf, "ldap")) |
485 |
return KSPROTO_LDAP; |
return KSPROTO_LDAP; |
492 |
void |
void |
493 |
keyserver_set_default (const char *hostname, WORD port) |
keyserver_set_default (const char *hostname, WORD port) |
494 |
{ |
{ |
495 |
if (hostname) { |
if (hostname != NULL) { |
496 |
free_if_alloc (default_keyserver); |
free_if_alloc (default_keyserver); |
497 |
default_keyserver = m_strdup (hostname); |
default_keyserver = m_strdup (hostname); |
498 |
if (!default_keyserver) |
if (!default_keyserver) |
499 |
BUG (0); |
BUG (0); |
500 |
default_keyserver_port = port; |
default_keyserver_port = port; |
501 |
} |
} |
502 |
|
if (!port) |
503 |
|
port = HKP_PORT; |
504 |
|
if (!default_keyserver) |
505 |
|
default_keyserver = m_strdup (DEF_HKP_KEYSERVER); |
506 |
server[0].name = m_strdup (default_keyserver); |
server[0].name = m_strdup (default_keyserver); |
507 |
server[0].used = 1; |
server[0].used = 1; |
508 |
server[0].port = port; |
server[0].port = port; |
528 |
} |
} |
529 |
|
|
530 |
|
|
531 |
|
/* Save the keyserver config file in @conf. */ |
532 |
int |
int |
533 |
kserver_load_conf (const char * conf) |
kserver_save_conf (const char *conf) |
534 |
{ |
{ |
|
struct stat statbuf; |
|
535 |
FILE *fp; |
FILE *fp; |
536 |
char buf[1024], * s, * p; |
int pos; |
537 |
|
|
538 |
|
fp = fopen (conf, "wb"); |
539 |
|
if (!fp) { |
540 |
|
msg_box (NULL, _("Could not save keyserver.conf file"), |
541 |
|
_("Keyserver"), MB_ERR); |
542 |
|
return -1; |
543 |
|
} |
544 |
|
|
545 |
|
fprintf (fp, "# do NOT manually modify this file, it will be " |
546 |
|
"generated automatically!!\r\n"); |
547 |
|
for (pos = 0; pos < MAX_KEYSERVERS; pos++) { |
548 |
|
if (!server[pos].used) |
549 |
|
continue; |
550 |
|
fprintf (fp, "%s:%d\r\n", server[pos].name, server[pos].port); |
551 |
|
} |
552 |
|
fclose (fp); |
553 |
|
return 0; |
554 |
|
} |
555 |
|
|
556 |
|
|
557 |
|
/* Load the keyserver config file @conf. */ |
558 |
|
int |
559 |
|
kserver_load_conf (const char *conf) |
560 |
|
{ |
561 |
|
FILE *fp; |
562 |
|
char buf[1024], *s, *p; |
563 |
char *user = NULL, *pass = NULL; |
char *user = NULL, *pass = NULL; |
564 |
int no_config=0, chk_pos=0; |
int no_config=0, chk_pos=0; |
565 |
int pos; |
int pos; |
578 |
} |
} |
579 |
no_config=1; |
no_config=1; |
580 |
} |
} |
581 |
get_reg_proxy_prefs (&proxy.host, &proxy.port, &user, &pass); |
get_reg_proxy_prefs (&proxy); |
582 |
if (user && pass) |
if (user && pass) |
583 |
kserver_update_proxyuser (user, pass); |
update_proxy_user (user, pass); |
584 |
else if (user && !pass || !user && pass) { |
else if (user && !pass || !user && pass) { |
585 |
msg_box( NULL, _("Invalid proxy configuration." |
msg_box (NULL, _("Invalid proxy configuration." |
586 |
"You need to set a user and a password" |
"You need to set a user and a password" |
587 |
"to use proxy authentication!"), _("Proxy Error"), MB_ERR ); |
"to use proxy authentication!"), |
588 |
|
_("Proxy Error"), MB_ERR); |
589 |
} |
} |
590 |
/* check if the host has a http:// prefix and skip it */ |
/* check if the host has a http:// prefix and skip it */ |
591 |
if( proxy.host && !strncmp( proxy.host, "http://", 7 ) ) { |
if (proxy.host && !strncmp (proxy.host, "http://", 7)) { |
592 |
char *host = m_strdup (proxy.host+7); |
char *host = m_strdup (proxy.host+7); |
593 |
if (!host) |
if (!host) |
594 |
BUG (0); |
BUG (0); |
599 |
free_if_alloc (pass); |
free_if_alloc (pass); |
600 |
|
|
601 |
pos = 0; |
pos = 0; |
602 |
while( fp && !feof( fp ) ) { |
while (fp && !feof (fp)) { |
603 |
s = fgets (buf, sizeof (buf)-1, fp); |
s = fgets (buf, sizeof (buf)-1, fp); |
604 |
if (!s) |
if (!s) |
605 |
break; |
break; |
616 |
_("Keyserver Error"), MB_ERR); |
_("Keyserver Error"), MB_ERR); |
617 |
continue; |
continue; |
618 |
} |
} |
619 |
chk_pos=6; |
chk_pos = 6; |
620 |
if (strstr (s, "finger")) |
if (strstr (s, "finger")) |
621 |
chk_pos = 10; |
chk_pos = 10; |
622 |
p = strchr (s+chk_pos, ':'); |
p = strchr (s+chk_pos, ':'); |
636 |
memcpy (server[pos].name, s, (p-s)); |
memcpy (server[pos].name, s, (p-s)); |
637 |
} |
} |
638 |
pos++; |
pos++; |
639 |
if (pos > MAX_KEYSERVERS) |
if (pos > MAX_KEYSERVERS) { |
640 |
{ |
msg_box (NULL, _("The keyserver limit is exceeded"), |
641 |
msg_box (NULL, _("The keyserver limit is exceeded"), _("Keyserver Warning"), MB_INFO); |
_("Keyserver Warning"), MB_INFO); |
642 |
break; |
break; |
643 |
} |
} |
644 |
} |
} |
645 |
if (fp) |
if (fp) |
646 |
fclose (fp); |
fclose (fp); |
647 |
if (!pos) { |
if (!pos) { |
648 |
/* 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 |
649 |
|
it has no valid entries. */ |
650 |
keyserver_set_default (NULL, HKP_PORT); |
keyserver_set_default (NULL, HKP_PORT); |
651 |
if (!no_config) |
if (!no_config) |
652 |
return WPTERR_CONFIG_FILE; |
return WPTERR_CONFIG_FILE; |
653 |
} |
} |
654 |
|
|
|
if (!stat (conf, &statbuf)) |
|
|
conf_timestamp = statbuf.st_mtime; |
|
655 |
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; |
|
656 |
} |
} |
657 |
|
|
658 |
|
|
660 |
Return value: 0 on success */ |
Return value: 0 on success */ |
661 |
int |
int |
662 |
kserver_connect (const char *hostname, WORD port, int *conn_fd) |
kserver_connect (const char *hostname, WORD port, int *conn_fd) |
663 |
{ |
{ |
|
int rc, fd; |
|
|
DWORD iaddr; |
|
|
char host[128] = {0}; |
|
664 |
struct hostent *hp; |
struct hostent *hp; |
665 |
struct sockaddr_in sock; |
struct sockaddr_in sock; |
666 |
|
char host[128] = {0}; |
667 |
|
DWORD iaddr; |
668 |
|
int rc, fd; |
669 |
|
|
670 |
log_debug ("kserver_connect: %s:%d\r\n", hostname, port); |
log_debug ("kserver_connect: %s:%d\r\n", hostname, port); |
671 |
|
|
675 |
*conn_fd = 0; |
*conn_fd = 0; |
676 |
hostname = skip_type_prefix (hostname); |
hostname = skip_type_prefix (hostname); |
677 |
|
|
678 |
|
if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) |
679 |
|
port = proxy.port; |
680 |
memset (&sock, 0, sizeof (sock)); |
memset (&sock, 0, sizeof (sock)); |
681 |
sock.sin_family = AF_INET; |
sock.sin_family = AF_INET; |
682 |
sock.sin_port = proxy.host? htons (proxy.port) : htons (port); |
sock.sin_port = htons (port); |
683 |
if (proxy.host) |
if (proxy.host) |
684 |
strncpy (host, proxy.host, 127); |
strncpy (host, proxy.host, 127); |
685 |
else |
else |
686 |
strncpy (host, hostname, 127); |
strncpy (host, hostname, 127); |
687 |
|
|
688 |
if ((iaddr = inet_addr (host)) != INADDR_NONE) |
if ((iaddr = inet_addr (host)) != INADDR_NONE) |
689 |
memcpy (&sock.sin_addr, &iaddr, sizeof (iaddr)); |
memcpy (&sock.sin_addr, &iaddr, sizeof (iaddr)); |
690 |
else if ((hp = gethostbyname (host))) { |
else if ((hp = gethostbyname (host))) { |
708 |
return WPTERR_WINSOCK_CONNECT; |
return WPTERR_WINSOCK_CONNECT; |
709 |
} |
} |
710 |
|
|
711 |
|
if (proxy.proto == PROXY_PROTO_SOCKS5) { |
712 |
|
rc = socks_handshake (&proxy, fd, hostname, port); |
713 |
|
if (rc) { |
714 |
|
closesocket (fd); |
715 |
|
return WPTERR_GENERAL; /* XXX: use better code. */ |
716 |
|
} |
717 |
|
} |
718 |
|
|
719 |
if (conn_fd) |
if (conn_fd) |
720 |
*conn_fd = fd; |
*conn_fd = fd; |
721 |
WSASetLastError (0); |
WSASetLastError (0); |
723 |
} |
} |
724 |
|
|
725 |
|
|
726 |
/* Perform URL-encoding on the given pubkey blob. */ |
static bool |
727 |
static char* |
URL_must_encoded (const char *url) |
|
kserver_urlencode (const char *pubkey, size_t octets, size_t *newlen) |
|
728 |
{ |
{ |
729 |
char *p, numbuf[5]; |
if (strchr (url, '.') || strchr (url, '@') || strchr (url, ' ')) |
730 |
size_t j; |
return true; |
731 |
size_t i, size; |
return false; |
732 |
|
} |
|
p = new char [2*octets+1]; |
|
|
if (!p) |
|
|
BUG (0); |
|
733 |
|
|
734 |
for (size = 0, i = 0; i < octets; i++) { |
|
735 |
if (isalnum (pubkey[i]) || pubkey[i] == '-') { |
/* Perform URL encoding of the given data. */ |
736 |
p[size] = pubkey[i]; |
static char* |
737 |
size++; |
URL_encode (const char *url, size_t ulen, size_t *ret_nlen) |
738 |
} |
{ |
739 |
else if (pubkey[i] == ' ') { |
gpgme_data_t hd; |
740 |
p[size] = '+'; |
char numbuf[5], *pp, *p; |
741 |
size++; |
size_t i, n; |
742 |
} |
|
743 |
|
gpgme_data_new (&hd); |
744 |
|
for (i=0; i < ulen; i++) { |
745 |
|
if (isalnum (url[i]) || url[i] == '-') |
746 |
|
gpg_data_putc (hd, url[i]); |
747 |
|
else if (url[i] == ' ') |
748 |
|
gpg_data_putc (hd, '+'); |
749 |
else { |
else { |
750 |
sprintf (numbuf, "%%%02X", pubkey[i]); |
sprintf (numbuf, "%%%02X", url[i]); |
751 |
for (j = 0; j < strlen (numbuf); j++) { |
gpgme_data_write (hd, numbuf, strlen (numbuf)); |
|
p[size] = numbuf[j]; |
|
|
size++; |
|
|
} |
|
752 |
} |
} |
753 |
} |
} |
754 |
p[size] = '\0'; |
gpg_data_putc (hd, '\0'); |
755 |
*newlen = size; |
|
756 |
|
/* Copy memory to avoid that we need to use gpgme_free later. */ |
757 |
|
pp = gpgme_data_release_and_get_mem (hd, &n); |
758 |
|
p = m_strdup (pp); |
759 |
|
gpgme_free (pp); |
760 |
|
if (ret_nlen) |
761 |
|
*ret_nlen = n; |
762 |
return p; |
return p; |
763 |
} |
} |
764 |
|
|
778 |
if (!port) |
if (!port) |
779 |
port = HKP_PORT; |
port = HKP_PORT; |
780 |
reqlen = 512 + strlen (hostname) + 2*strlen (pubkey); |
reqlen = 512 + strlen (hostname) + 2*strlen (pubkey); |
781 |
|
if (proxy.proto == PROXY_PROTO_HTTP && proxy.base64_user) |
782 |
|
reqlen += strlen (proxy.base64_user) + 1; |
783 |
request = new char[reqlen]; |
request = new char[reqlen]; |
784 |
if (!request) |
if (!request) |
785 |
BUG (0); |
BUG (0); |
786 |
enc_pubkey = kserver_urlencode (pubkey, octets, &enc_octets); |
enc_pubkey = URL_encode (pubkey, octets, &enc_octets); |
787 |
if (!enc_pubkey || !enc_octets) { |
if (!enc_pubkey || !enc_octets) { |
788 |
free_if_alloc (request); |
free_if_alloc (request); |
789 |
return NULL; |
return NULL; |
790 |
} |
} |
791 |
|
|
792 |
if (proxy.user) { |
if (proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
793 |
_snprintf (request, reqlen-1, |
_snprintf (request, reqlen-1, |
794 |
"POST http://%s:%d/pks/add HTTP/1.0\r\n" |
"POST http://%s:%d/pks/add HTTP/1.0\r\n" |
795 |
"Referer: \r\n" |
"Referer: \r\n" |
800 |
"Content-length: %d\r\n" |
"Content-length: %d\r\n" |
801 |
"\r\n" |
"\r\n" |
802 |
"keytext=%s" |
"keytext=%s" |
803 |
"\n", |
"\r\n", |
804 |
skip_type_prefix (hostname), port, hostname, port, |
skip_type_prefix (hostname), port, hostname, port, |
805 |
proxy.base64_user, enc_octets+9, enc_pubkey); |
proxy.base64_user, enc_octets+9, enc_pubkey); |
806 |
} |
} |
814 |
"Content-length: %d\r\n" |
"Content-length: %d\r\n" |
815 |
"\r\n" |
"\r\n" |
816 |
"keytext=%s" |
"keytext=%s" |
817 |
"\n", |
"\r\n", |
818 |
skip_type_prefix (hostname), port, |
skip_type_prefix (hostname), port, |
819 |
enc_octets+9, enc_pubkey); |
enc_octets+9, enc_pubkey); |
820 |
} |
} |
825 |
} |
} |
826 |
|
|
827 |
|
|
|
/* Interface for receiving a public key. */ |
|
828 |
int |
int |
829 |
kserver_recvkey (const char *hostname, WORD port, const char *keyid, |
kserver_recvkey_ext (const char *hostname, WORD port, const char *keyid, |
830 |
char *key, int maxkeylen) |
char **r_key, int *r_keylen) |
831 |
{ |
{ |
832 |
char *request = NULL; |
char *request = NULL; |
833 |
int conn_fd; |
int conn_fd; |
844 |
if (!request) |
if (!request) |
845 |
BUG (0); |
BUG (0); |
846 |
|
|
847 |
if (proxy.host && proxy.user) { |
if (proxy.host && proxy.user && 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" |
"GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n" |
850 |
"Proxy-Authorization: Basic %s\r\n\r\n", |
"Proxy-Authorization: Basic %s\r\n\r\n", |
851 |
skip_type_prefix (hostname), port, keyid, proxy.base64_user); |
skip_type_prefix (hostname), port, keyid, proxy.base64_user); |
852 |
} |
} |
853 |
else if (proxy.host) { |
else if (proxy.host && 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\r\n", |
"GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n", |
856 |
skip_type_prefix (hostname), port, keyid); |
skip_type_prefix (hostname), port, keyid); |
868 |
goto leave; |
goto leave; |
869 |
} |
} |
870 |
|
|
871 |
rc = sock_read( conn_fd, key, maxkeylen, &n ); |
rc = sock_read_ext (conn_fd, r_key, &n); |
872 |
if( rc == SOCKET_ERROR ) { |
if (rc == SOCKET_ERROR) { |
873 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
874 |
goto leave; |
goto leave; |
875 |
} |
} |
876 |
|
|
877 |
log_debug("%s\r\n", key); |
if (r_keylen) |
878 |
rc = check_hkp_response (key, 1); |
*r_keylen = n; |
879 |
|
log_debug ("%s\r\n", *r_key); |
880 |
|
rc = check_hkp_response (*r_key, 1); |
881 |
if (rc) |
if (rc) |
882 |
goto leave; |
goto leave; |
883 |
|
|
889 |
return rc; |
return rc; |
890 |
} |
} |
891 |
|
|
892 |
|
/* Interface for receiving a public key. */ |
893 |
|
int |
894 |
|
kserver_recvkey (const char *hostname, WORD port, const char *keyid, |
895 |
|
char *key, int maxkeylen) |
896 |
|
{ |
897 |
|
char *tmpkey; |
898 |
|
int rc, nread=0; |
899 |
|
|
900 |
|
/* XXX: just a wrapper around the new method, replace it |
901 |
|
soon as possible. */ |
902 |
|
rc = kserver_recvkey_ext (hostname, port, keyid, &tmpkey, &nread); |
903 |
|
if (rc) |
904 |
|
return rc; |
905 |
|
|
906 |
|
if (nread > maxkeylen) { |
907 |
|
free_if_alloc (tmpkey); |
908 |
|
return WPTERR_GENERAL; |
909 |
|
} |
910 |
|
strcpy (key, tmpkey); |
911 |
|
free_if_alloc (tmpkey); |
912 |
|
return 0; |
913 |
|
} |
914 |
|
|
915 |
|
|
916 |
/* Interface to send a public key. */ |
/* Interface to send a public key. */ |
917 |
int |
int |
918 |
kserver_sendkey (const char *hostname, WORD port, const char *pubkey, int len ) |
kserver_sendkey (const char *hostname, WORD port, const char *pubkey, int len ) |
919 |
{ |
{ |
920 |
char *request = NULL; |
char *request = NULL; |
921 |
char log[2048]; |
char log[2048] = {0}; |
922 |
int conn_fd, n; |
int conn_fd, n; |
923 |
int rc; |
int rc; |
924 |
|
|
945 |
goto leave; |
goto leave; |
946 |
} |
} |
947 |
|
|
948 |
log_debug ("kserver_sendkey:\r\n%s\r\n", log); |
log_debug ("kserver_sendkey: read %d bytes\r\n%s\r\n", n, log); |
949 |
rc = check_hkp_response (log, 0); |
rc = check_hkp_response (log, 0); |
950 |
if (rc) |
if (rc) |
951 |
goto leave; |
goto leave; |
959 |
} |
} |
960 |
|
|
961 |
|
|
962 |
|
/* Check keyserver response. */ |
963 |
|
static int |
964 |
|
kserver_search_chkresp (int fd) |
965 |
|
{ |
966 |
|
char buf[128]; |
967 |
|
int n=0; |
968 |
|
|
969 |
|
/* parse response 'HTTP/1.0 500 OK' */ |
970 |
|
if (sock_getline (fd, buf, 127, &n)) |
971 |
|
return WPTERR_KEYSERVER_NOTFOUND; |
972 |
|
|
973 |
|
log_debug ("kserver_search_chkpresp: %s\r\n", buf); |
974 |
|
if (strncmp (buf, "HTTP/1.", 7)) |
975 |
|
return WPTERR_KEYSERVER_NOTFOUND; |
976 |
|
if (strncmp (buf+(8+1), "200", 3)) |
977 |
|
return WPTERR_KEYSERVER_NOTFOUND; |
978 |
|
return 0; |
979 |
|
} |
980 |
|
|
981 |
|
|
982 |
|
/* End the keyserver search procedure. */ |
983 |
|
void |
984 |
|
kserver_search_end (int conn_fd) |
985 |
|
{ |
986 |
|
log_debug ("kserver_search_end: fd=%d\r\n", conn_fd); |
987 |
|
closesocket (conn_fd); |
988 |
|
} |
989 |
|
|
990 |
|
|
991 |
|
/* Begin keyserver search procedure. */ |
992 |
int |
int |
993 |
kserver_search_init (const char *hostname, WORD port, |
kserver_search_begin (const char *hostname, WORD port, |
994 |
const char *keyid, int *conn_fd) |
const char *pattern, int *conn_fd) |
995 |
{ |
{ |
996 |
char *request = NULL; |
char *request = NULL; |
997 |
int n=0; |
char *enc_patt = NULL; |
998 |
|
int n; |
999 |
int rc, sock_fd; |
int rc, sock_fd; |
1000 |
|
|
1001 |
rc = kserver_connect (hostname, port, &sock_fd); |
rc = kserver_connect (hostname, port, &sock_fd); |
1002 |
if (rc) { |
if (rc) { |
1003 |
*conn_fd = 0; |
*conn_fd = 0; |
1004 |
goto leave; |
goto leave; |
1005 |
} |
} |
1006 |
|
|
1007 |
n=300; |
enc_patt = URL_encode (pattern, strlen (pattern), NULL); |
1008 |
|
n = 140 + strlen (enc_patt) + strlen (hostname) + 32 + 2; |
1009 |
|
if (proxy.base64_user) |
1010 |
|
n += strlen (proxy.base64_user) + 1; |
1011 |
request = new char[n+1]; |
request = new char[n+1]; |
1012 |
if (!request) |
if (!request) |
1013 |
BUG (0); |
BUG (0); |
1014 |
|
|
1015 |
if (proxy.host && proxy.user) { |
if (proxy.host && proxy.user && 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" |
"GET http://%s:%d/pks/lookup?op=index&search=%s HTTP/1.0\r\n" |
1018 |
"Proxy-Authorization: Basic %s\r\n\r\n", |
"Proxy-Authorization: Basic %s\r\n\r\n", |
1019 |
skip_type_prefix (hostname), port, keyid, proxy.base64_user); |
skip_type_prefix (hostname), port, enc_patt, proxy.base64_user); |
1020 |
} |
} |
1021 |
else if (proxy.host) { |
else if (proxy.host && 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\r\n", |
"GET http://%s:%d/pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n", |
1024 |
skip_type_prefix (hostname), port, keyid); |
skip_type_prefix (hostname), port, enc_patt); |
1025 |
} |
} |
1026 |
else { |
else { |
1027 |
_snprintf (request, n, |
_snprintf (request, n, |
1028 |
"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", |
1029 |
|
enc_patt); |
1030 |
} |
} |
1031 |
|
|
1032 |
log_debug ("kserver_search_init:\r\n%s\r\n", request); |
log_debug ("kserver_search_begin:\r\n%s\r\n", request); |
1033 |
|
|
1034 |
if (sock_write (sock_fd, request, strlen (request)) == SOCKET_ERROR) { |
if (sock_write (sock_fd, request, strlen (request)) == SOCKET_ERROR) { |
1035 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1036 |
goto leave; |
goto leave; |
1037 |
} |
} |
1038 |
|
|
1039 |
|
rc = kserver_search_chkresp (sock_fd); |
1040 |
|
if (rc) { |
1041 |
|
closesocket (sock_fd); |
1042 |
|
sock_fd = 0; |
1043 |
|
} |
1044 |
|
|
1045 |
*conn_fd = sock_fd; |
*conn_fd = sock_fd; |
1046 |
|
|
1047 |
leave: |
leave: |
1048 |
free_if_alloc (request); |
free_if_alloc (request); |
1049 |
|
free_if_alloc (enc_patt); |
1050 |
return rc; |
return rc; |
1051 |
} |
} |
1052 |
|
|
1053 |
|
|
1054 |
/* Check keyserver response. */ |
|
1055 |
int |
|
1056 |
kserver_search_chkresp (int fd) |
/* Convert an iso date @iso_date (YYYY-MM-DD) into the locale |
1057 |
|
representation and store it into @loc_date. |
1058 |
|
Return value: 0 on success. */ |
1059 |
|
static int |
1060 |
|
parse_iso_date (const char *iso_date, char *loc_date, size_t loclen) |
1061 |
{ |
{ |
1062 |
char buf[128]; |
SYSTEMTIME st; |
1063 |
int n=0; |
char buf[16] = {0}, *p; |
1064 |
|
int pos=0; |
1065 |
|
|
1066 |
|
strncpy (buf, iso_date, sizeof (buf)-1); |
1067 |
|
p = strtok (buf, "-"); |
1068 |
|
while (p != NULL) { |
1069 |
|
switch (pos) { |
1070 |
|
case 0: st.wYear = (WORD)atoi (p); pos++; break; |
1071 |
|
case 1: st.wMonth = (WORD)atoi (p); pos++; break; |
1072 |
|
case 2: st.wDay = (WORD)atoi (p); pos++; break; |
1073 |
|
default: break; |
1074 |
|
} |
1075 |
|
p = strtok (NULL, "-"); |
1076 |
|
} |
1077 |
|
if (pos != 3) |
1078 |
|
return -1; |
1079 |
|
|
1080 |
/* parse response 'HTTP/1.0 500 OK' */ |
if (!GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, |
1081 |
if (sock_getline (fd, buf, 127, &n)) |
NULL, loc_date, loclen)) |
1082 |
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; |
|
1083 |
return 0; |
return 0; |
1084 |
} |
} |
1085 |
|
|
1086 |
|
|
1087 |
int |
int |
1088 |
kserver_search (int fd, keyserver_key * key) |
kserver_search_next (int fd, keyserver_key *key) |
1089 |
{ |
{ |
1090 |
char buf[1024], *p; |
char buf[1024], *p; |
1091 |
int uidlen, nbytes, pos = 0; |
int uidlen, nbytes, pos = 0; |
1092 |
|
|
1093 |
log_debug ("keyserver_search:\r\n"); |
log_debug ("keyserver_search_next:\r\n"); |
1094 |
|
|
1095 |
if (sock_getline (fd, buf, sizeof (buf) - 1, &nbytes)) |
if (sock_getline (fd, buf, sizeof (buf) - 1, &nbytes)) |
1096 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
1097 |
|
|
1098 |
|
/* XXX: use maschine readable option. */ |
1099 |
log_debug ("%s\r\n", buf); |
log_debug ("%s\r\n", buf); |
1100 |
|
|
1101 |
if (!strncmp (buf, "pub", 3)) { |
if (!strncmp (buf, "pub", 3)) { |
1113 |
pos = p - buf + 5; |
pos = p - buf + 5; |
1114 |
memcpy (key->date, buf + pos, 10); |
memcpy (key->date, buf + pos, 10); |
1115 |
key->date[10] = '\0'; |
key->date[10] = '\0'; |
1116 |
|
parse_iso_date (key->date, key->date, sizeof (key->date)-1); |
1117 |
if (revoked) { |
if (revoked) { |
1118 |
strcpy (key->uid, "KEY REVOKED: not checked"); |
strcpy (key->uid, "KEY REVOKED: not checked"); |
1119 |
return 0; |
return 0; |
1124 |
p++; |
p++; |
1125 |
p++; |
p++; |
1126 |
uidlen = strlen (p) - 10; |
uidlen = strlen (p) - 10; |
1127 |
|
if (!strstr (p, "<") && !strstr (p, ">")) { |
1128 |
|
pos=0; |
1129 |
|
while (p && *p && pos < sizeof (key->uid)-1) { |
1130 |
|
if (*p == '<') |
1131 |
|
break; |
1132 |
|
key->uid[pos++] = *p++; |
1133 |
|
} |
1134 |
|
key->uid[pos] ='\0'; |
1135 |
|
return 0; |
1136 |
|
} |
1137 |
|
|
1138 |
|
if (uidlen > sizeof (key->uid)-1) |
1139 |
|
uidlen = sizeof (key->uid)-1; |
1140 |
memcpy (key->uid, p, uidlen); |
memcpy (key->uid, p, uidlen); |
1141 |
key->uid[uidlen] = '\0'; |
key->uid[uidlen] = '\0'; |
1142 |
strcat (key->uid, ">"); |
strcat (key->uid, ">"); |
1153 |
key->bits = 0; |
key->bits = 0; |
1154 |
memset (key, 0, sizeof *key); |
memset (key, 0, sizeof *key); |
1155 |
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; |
|
1156 |
} |
} |
1157 |
|
|
1158 |
|
|
|
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 */ |
|
|
|
|
|
|
|
1159 |
/* Release mbmers in the proxy context @ctx. */ |
/* Release mbmers in the proxy context @ctx. */ |
1160 |
void |
void |
1161 |
proxy_release( keyserver_proxy_ctx *ctx ) |
kserver_proxy_release (keyserver_proxy_t ctx) |
1162 |
{ |
{ |
1163 |
free_if_alloc (ctx->host); |
free_if_alloc (ctx->host); |
1164 |
free_if_alloc (ctx->pass); |
free_if_alloc (ctx->pass); |
1165 |
free_if_alloc (ctx->user); |
free_if_alloc (ctx->user); |
1166 |
|
ctx->port = ctx->proto = 0; |
1167 |
} |
} |
1168 |
|
|
1169 |
|
|
1196 |
|
|
1197 |
/* Receive an key via LDAP from host @host with the keyid @keyid. |
/* Receive an key via LDAP from host @host with the keyid @keyid. |
1198 |
@key contains the key on success. */ |
@key contains the key on success. */ |
1199 |
int |
int |
1200 |
ldap_recvkey (const char *host, const char *keyid, char *key, int maxkeylen) |
ldap_recvkey (const char *host, const char *keyid, char *key, int maxkeylen) |
1201 |
{ |
{ |
1202 |
FILE *inp; |
FILE *fp; |
|
DWORD n; |
|
1203 |
const char *s; |
const char *s; |
1204 |
char *ksprg = NULL, *p = NULL, *sep; |
char *ksprg = NULL, *p = NULL, *sep; |
1205 |
char temp[512], outf[512]; |
char inf[256], outf[256], buf[512]; |
1206 |
|
DWORD n; |
1207 |
int start_key = 0, failed = 0; |
int start_key = 0, failed = 0; |
1208 |
int rc = 0; |
int rc = 0; |
1209 |
|
|
1223 |
"%s: could not find LDAP keyserver module!", ksprg); |
"%s: could not find LDAP keyserver module!", ksprg); |
1224 |
rc = -1; |
rc = -1; |
1225 |
goto leave; |
goto leave; |
1226 |
} |
} |
1227 |
GetTempPath (sizeof (temp)-1, temp); |
get_temp_name (outf, sizeof (outf)-1, keyid); |
1228 |
_snprintf (outf, sizeof (outf)-1, "%s%s.out", temp, keyid); |
get_temp_name (inf, sizeof (inf)-1, NULL); |
1229 |
strcat (temp, keyid); |
fp = fopen (inf, "w+b"); |
1230 |
inp = fopen (temp, "w+b"); |
if (!fp) { |
1231 |
if (!inp) { |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", inf, |
|
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", temp, |
|
1232 |
winpt_strerror (WPTERR_FILE_OPEN)); |
winpt_strerror (WPTERR_FILE_OPEN)); |
1233 |
rc = -1; |
rc = -1; |
1234 |
goto leave; |
goto leave; |
1235 |
} |
} |
1236 |
fprintf (inp, |
fprintf (fp, |
1237 |
"VERSION 1\n" |
"VERSION 1\n" |
1238 |
"PROGRAM 1.4.3-cvs\n" |
"PROGRAM 1.4.3-cvs\n" |
1239 |
"SCHEME ldap\n" |
"SCHEME ldap\n" |
1242 |
"\n" |
"\n" |
1243 |
"%s\n", |
"%s\n", |
1244 |
host? skip_type_prefix (host): "64.94.85.200", keyid); |
host? skip_type_prefix (host): "64.94.85.200", keyid); |
1245 |
fclose (inp); |
fclose (fp); |
1246 |
|
|
1247 |
p = new char[strlen (ksprg) + strlen (temp) + strlen (outf) + 32]; |
p = new char[strlen (ksprg) + strlen (inf) + strlen (outf) + 32]; |
1248 |
if (!p) |
if (!p) |
1249 |
BUG (NULL); |
BUG (NULL); |
1250 |
sprintf (p, "%s -o %s %s", ksprg, outf, temp); |
sprintf (p, "%s -o %s %s", ksprg, outf, inf); |
1251 |
if (spawn_application (p)) { |
if (spawn_application (p)) { |
1252 |
rc = -1; |
rc = -1; |
1253 |
goto leave; |
goto leave; |
1254 |
} |
} |
|
DeleteFile (temp); |
|
1255 |
|
|
1256 |
inp = fopen (outf, "rb"); |
fp = fopen (outf, "rb"); |
1257 |
if (!inp) { |
if (!fp) { |
1258 |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf, |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf, |
1259 |
winpt_strerror (WPTERR_FILE_OPEN)); |
winpt_strerror (WPTERR_FILE_OPEN)); |
1260 |
rc = -1; |
rc = -1; |
1261 |
goto leave; |
goto leave; |
1262 |
} |
} |
1263 |
memset (key, 0, maxkeylen); |
memset (key, 0, maxkeylen); |
1264 |
while (!feof (inp)) { |
while (!feof (fp)) { |
1265 |
s = fgets (temp, sizeof (temp)-1, inp); |
s = fgets (buf, sizeof (buf)-1, fp); |
1266 |
if (!s) |
if (!s) |
1267 |
break; |
break; |
1268 |
if (strstr (s, "KEY") && strstr (s, "FAILED")) { |
if (strstr (s, "KEY") && strstr (s, "FAILED")) { |
1275 |
} |
} |
1276 |
if (!start_key) |
if (!start_key) |
1277 |
continue; |
continue; |
1278 |
strcat (key, temp); |
strcat (key, buf); |
1279 |
maxkeylen -= strlen (temp); |
maxkeylen -= strlen (buf); |
1280 |
if (maxkeylen < 0 || (strstr (s, "KEY") && strstr (s, "END"))) |
if (maxkeylen < 0 || (strstr (s, "KEY") && strstr (s, "END"))) |
1281 |
break; |
break; |
1282 |
} |
} |
1283 |
fclose (inp); |
fclose (fp); |
1284 |
|
|
1285 |
leave: |
leave: |
1286 |
if (failed || !strlen (key)) |
if (failed || !strlen (key)) |
1287 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
1288 |
|
DeleteFile (inf); |
1289 |
DeleteFile (outf); |
DeleteFile (outf); |
1290 |
free_if_alloc (p); |
free_if_alloc (p); |
1291 |
free_if_alloc (ksprg); |
free_if_alloc (ksprg); |
1293 |
} |
} |
1294 |
|
|
1295 |
|
|
|
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; |
|
|
} |
|
|
|
|
|
|
|
1296 |
/* Receive an key via FINGER from host @host with the user @user. |
/* Receive an key via FINGER from host @host with the user @user. |
1297 |
On success @key contains the key. */ |
On success @key contains the key. */ |
1298 |
int |
int |
1299 |
finger_recvkey (const char *host, const char *user, char *key, int maxkeylen) |
finger_recvkey (const char *host, const char *user, char *key, int maxkeylen) |
1300 |
{ |
{ |
|
struct hostent * hp; |
|
|
struct sockaddr_in saddr; |
|
1301 |
char buf[128]; |
char buf[128]; |
1302 |
int fd, nread; |
int fd, nread; |
1303 |
int start_key = 0; |
int start_key = 0; |
1304 |
int rc=0; |
int rc=0; |
1305 |
|
|
1306 |
fd = socket (PF_INET, SOCK_STREAM, 0); |
rc = kserver_connect (host, FINGER_PORT, &fd); |
1307 |
if (fd == -1) |
if (rc) |
1308 |
return WPTERR_WINSOCK_SOCKET; |
return rc; |
|
hp = gethostbyname (skip_type_prefix (host)); |
|
|
if (!hp) { |
|
|
closesocket (fd); |
|
|
return WPTERR_WINSOCK_RESOLVE; |
|
|
} |
|
1309 |
|
|
1310 |
memset (&saddr, 0, sizeof (saddr)); |
sock_write (fd, user, strlen (user)); |
1311 |
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); |
|
1312 |
|
|
1313 |
memset (key, 0, maxkeylen); |
memset (key, 0, maxkeylen); |
1314 |
while (maxkeylen > 0) { |
while (maxkeylen > 0) { |
1315 |
nread = finger_readline (fd, buf, sizeof (buf)-1); |
if (sock_getline (fd, buf, sizeof (buf), &nread)) |
1316 |
if (nread <= 0) |
break; |
|
break; |
|
1317 |
strcat (buf, "\n"); |
strcat (buf, "\n"); |
1318 |
if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) { |
if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) { |
1319 |
strcat (key, buf); |
strcat (key, buf); |