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. |
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[] = { |
47 |
"hkp://wwwkeys.nl.pgp.net", |
"hkp://gnv.us.ks.cryptnet.net", |
48 |
"hkp://wwwkeys.pl.pgp.net", |
"hkp://keyserver.kjsl.com", |
49 |
"hkp://wwwkeys.at.pgp.net", |
"hkp://sks.keyserver.penguin.de", |
|
"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", |
|
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 |
/* Default keyserver and port. */ |
/* Default keyserver and port. */ |
60 |
char *default_keyserver = NULL; |
char *default_keyserver = NULL; |
61 |
WORD default_keyserver_port = 0; |
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 |
|
|
244 |
|
/* Helper to create a string from the gpgme data and |
245 |
|
then release the gpgme data object. */ |
246 |
|
char* |
247 |
|
data_release_and_get_mem (gpgme_data_t in, int *r_bufferlen) |
248 |
|
{ |
249 |
|
size_t n; |
250 |
|
char *buffer, *p; |
251 |
|
|
252 |
|
gpg_data_putc (in, '\0'); |
253 |
|
p = gpgme_data_release_and_get_mem (in, &n); |
254 |
|
buffer = m_strdup (p); |
255 |
|
if (r_bufferlen) |
256 |
|
*r_bufferlen = (int)n; |
257 |
|
gpgme_free (p); |
258 |
|
return buffer; |
259 |
|
} |
260 |
|
|
261 |
|
|
262 |
/* Read much data as possible from the socket @fd and |
/* Read much data as possible from the socket @fd and |
263 |
return the data in @buffer. Caller must free data. |
return the data in @buffer. Caller must free data. |
264 |
Return value: 0 on success. */ |
Return value: 0 on success. */ |
266 |
sock_read_ext (int fd, char **buffer, int *r_bufferlen) |
sock_read_ext (int fd, char **buffer, int *r_bufferlen) |
267 |
{ |
{ |
268 |
gpgme_data_t dh; |
gpgme_data_t dh; |
269 |
char buf[1024], *p; |
char buf[1024]; |
270 |
size_t n=0; |
size_t n=0; |
271 |
int nread, rc; |
int nread, rc; |
272 |
|
|
273 |
gpgme_data_new (&dh); |
if (gpgme_data_new (&dh)) |
274 |
|
BUG (0); |
275 |
while (n < 10) { |
while (n < 10) { |
276 |
rc = sock_select (fd, 1); |
rc = sock_select (fd, 1); |
277 |
if (rc == SOCKET_ERROR) { |
if (rc == SOCKET_ERROR) { |
289 |
gpgme_data_write (dh, buf, nread); |
gpgme_data_write (dh, buf, nread); |
290 |
} |
} |
291 |
} |
} |
292 |
gpg_data_putc (dh, '\0'); |
|
293 |
p = gpgme_data_release_and_get_mem (dh, &n); |
*buffer = data_release_and_get_mem (dh, r_bufferlen); |
|
*buffer = m_strdup (p); |
|
|
if (r_bufferlen) |
|
|
*r_bufferlen = n; |
|
|
gpgme_free (p); |
|
294 |
return 0; |
return 0; |
295 |
} |
} |
296 |
|
|
309 |
return SOCKET_ERROR; |
return SOCKET_ERROR; |
310 |
} |
} |
311 |
nleft -= nwritten; |
nleft -= nwritten; |
312 |
buf += nwritten; |
buf += nwritten; |
313 |
} |
} |
314 |
|
|
315 |
return 0; |
return 0; |
316 |
} |
} |
317 |
|
|
318 |
|
|
|
/* 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; |
|
|
} |
|
|
|
|
|
|
|
319 |
/* Initialize the Winsock2 interface.*/ |
/* Initialize the Winsock2 interface.*/ |
320 |
int |
int |
321 |
wsock_init (void) |
wsock_init (void) |
326 |
log_debug ("wsock_init: WSAStartup failed ec=%d\r\n", net_errno); |
log_debug ("wsock_init: WSAStartup failed ec=%d\r\n", net_errno); |
327 |
return WPTERR_WINSOCK_INIT; |
return WPTERR_WINSOCK_INIT; |
328 |
} |
} |
|
set_default_kserver (); |
|
329 |
return 0; |
return 0; |
330 |
} |
} |
331 |
|
|
342 |
free_if_alloc (p); |
free_if_alloc (p); |
343 |
free_if_alloc (default_keyserver); |
free_if_alloc (default_keyserver); |
344 |
for (i=0; i < MAX_KEYSERVERS; i++) { |
for (i=0; i < MAX_KEYSERVERS; i++) { |
345 |
if (server[i].used) |
if (server[i].used && server[i].name != NULL) |
346 |
free_if_alloc (server[i].name); |
free_if_alloc (server[i].name); |
347 |
} |
} |
348 |
|
kserver_proxy_release (&proxy); |
349 |
WSACleanup (); |
WSACleanup (); |
350 |
} |
} |
351 |
|
|
353 |
/* Return a string representation of a winsock error. */ |
/* Return a string representation of a winsock error. */ |
354 |
const char* |
const char* |
355 |
wsock_strerror (void) |
wsock_strerror (void) |
356 |
{ |
{ |
|
static char buf[384]; |
|
357 |
int ec = WSAGetLastError (); |
int ec = WSAGetLastError (); |
358 |
|
|
359 |
|
if (!ec) |
360 |
|
return ""; |
361 |
switch (ec) { |
switch (ec) { |
362 |
case WSAENETDOWN: |
case WSAENETDOWN: |
363 |
return _("The network subsystem has failed"); |
return _("Network unreachable"); |
364 |
|
|
365 |
|
case WSAEHOSTUNREACH: |
366 |
|
return _("Host unreachable"); |
367 |
|
|
368 |
case WSAHOST_NOT_FOUND: |
case WSAHOST_NOT_FOUND: |
369 |
return _("Authoritative Answer Host not found"); |
return _("Could not resolve host name"); |
370 |
|
|
371 |
|
case WSAECONNREFUSED: |
372 |
|
return _("Connection refused"); |
373 |
|
|
374 |
case WSAETIMEDOUT: |
case WSAETIMEDOUT: |
375 |
return _("The connection has been dropped because of a network failure"); |
case WSAECONNABORTED: |
376 |
|
return _("Connection timeout"); |
377 |
|
|
378 |
|
case WSAENETRESET: |
379 |
|
case WSAECONNRESET: |
380 |
|
return _("Connection resetted by peer"); |
381 |
|
|
382 |
|
case WSAESHUTDOWN: |
383 |
|
return _("Socket has been shutdown"); |
384 |
|
|
385 |
default: |
default: |
386 |
_snprintf (buf, sizeof (buf) -1, _("Unknown Winsock error ec=%d"),ec); |
break; |
|
return buf; |
|
387 |
} |
} |
388 |
return NULL; |
|
389 |
|
return ""; |
390 |
|
} |
391 |
|
|
392 |
|
|
393 |
|
/* Set default socket timeout for all reading operations. */ |
394 |
|
void |
395 |
|
kserver_set_socket_timeout (int nsec) |
396 |
|
{ |
397 |
|
if (nsec < 0) |
398 |
|
nsec = 0; |
399 |
|
default_socket_timeout = nsec; |
400 |
} |
} |
401 |
|
|
402 |
|
|
445 |
/* If the request contains the keyid, it have to be |
/* If the request contains the keyid, it have to be |
446 |
always postfix with '0x'+keyid. This code checks |
always postfix with '0x'+keyid. This code checks |
447 |
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 |
448 |
the '0x'. If not it is inserted. */ |
the '0x'. If not it is inserted. */ |
449 |
const char* |
const char* |
450 |
kserver_check_keyid (const char *keyid) |
kserver_check_keyid (const char *keyid) |
451 |
{ |
{ |
464 |
|
|
465 |
/* Update the keyserver proxy user. */ |
/* Update the keyserver proxy user. */ |
466 |
static void |
static void |
467 |
kserver_update_proxyuser (const char *proxy_user, const char *proxy_pass) |
update_proxy_user (const char *proxy_user, const char *proxy_pass) |
468 |
{ |
{ |
469 |
char t[257]; /* user:pass = 127+1+127+1 = 257 */ |
char t[257]; /* user:pass = 127+1+127+1 = 257 */ |
470 |
int n = 0; |
int n = 0; |
475 |
if (!proxy.base64_user) |
if (!proxy.base64_user) |
476 |
BUG (0); |
BUG (0); |
477 |
_snprintf (t, sizeof (t)-1, "%s:%s", proxy_user, proxy_pass); |
_snprintf (t, sizeof (t)-1, "%s:%s", proxy_user, proxy_pass); |
478 |
base64_encode (t, proxy.base64_user, 257); |
base64_encode (t, proxy.base64_user); |
479 |
free_if_alloc (proxy.user); |
free_if_alloc (proxy.user); |
480 |
free_if_alloc (proxy.pass); |
free_if_alloc (proxy.pass); |
481 |
proxy.user = m_strdup (proxy_user); |
proxy.user = m_strdup (proxy_user); |
513 |
|
|
514 |
/* Get the port number from the given URL. */ |
/* Get the port number from the given URL. */ |
515 |
static int |
static int |
516 |
proto_from_URL (const char * buf) |
proto_from_URL (const char *buf) |
517 |
{ |
{ |
518 |
if (strstr( buf, "ldap")) |
if (strstr( buf, "ldap")) |
519 |
return KSPROTO_LDAP; |
return KSPROTO_LDAP; |
526 |
void |
void |
527 |
keyserver_set_default (const char *hostname, WORD port) |
keyserver_set_default (const char *hostname, WORD port) |
528 |
{ |
{ |
529 |
if (hostname) { |
if (hostname != NULL) { |
530 |
free_if_alloc (default_keyserver); |
free_if_alloc (default_keyserver); |
531 |
default_keyserver = m_strdup (hostname); |
default_keyserver = m_strdup (hostname); |
|
if (!default_keyserver) |
|
|
BUG (0); |
|
532 |
default_keyserver_port = port; |
default_keyserver_port = port; |
533 |
} |
} |
534 |
|
if (!port) |
535 |
|
port = HKP_PORT; |
536 |
|
if (!default_keyserver) |
537 |
|
default_keyserver = m_strdup (DEF_HKP_KEYSERVER); |
538 |
server[0].name = m_strdup (default_keyserver); |
server[0].name = m_strdup (default_keyserver); |
539 |
server[0].used = 1; |
server[0].used = 1; |
540 |
server[0].port = port; |
server[0].port = port; |
574 |
return -1; |
return -1; |
575 |
} |
} |
576 |
|
|
577 |
fprintf (fp, "# do NOT manually modify this file, it will be generated automatically!!\r\n"); |
fprintf (fp, "# do NOT manually modify this file, it will be " |
578 |
|
"generated automatically!!\r\n"); |
579 |
for (pos = 0; pos < MAX_KEYSERVERS; pos++) { |
for (pos = 0; pos < MAX_KEYSERVERS; pos++) { |
580 |
if (!server[pos].used) |
if (!server[pos].used) |
581 |
continue; |
continue; |
589 |
/* Load the keyserver config file @conf. */ |
/* Load the keyserver config file @conf. */ |
590 |
int |
int |
591 |
kserver_load_conf (const char *conf) |
kserver_load_conf (const char *conf) |
592 |
{ |
{ |
|
struct stat statbuf; |
|
593 |
FILE *fp; |
FILE *fp; |
594 |
char buf[1024], * s, * p; |
char buf[1024], *s, *p; |
595 |
char *user = NULL, *pass = NULL; |
char *user = NULL, *pass = NULL; |
596 |
int no_config=0, chk_pos=0; |
int no_config=0, chk_pos=0; |
597 |
int pos; |
int pos; |
610 |
} |
} |
611 |
no_config=1; |
no_config=1; |
612 |
} |
} |
613 |
get_reg_proxy_prefs (&proxy.host, &proxy.port, &user, &pass); |
get_reg_proxy_prefs (&proxy); |
614 |
if (user && pass) |
if (user && pass) |
615 |
kserver_update_proxyuser (user, pass); |
update_proxy_user (user, pass); |
616 |
else if (user && !pass || !user && pass) { |
else if (user && !pass || !user && pass) { |
617 |
msg_box (NULL, _("Invalid proxy configuration." |
msg_box (NULL, _("Invalid proxy configuration." |
618 |
"You need to set a user and a password" |
"You need to set a user and a password" |
622 |
/* check if the host has a http:// prefix and skip it */ |
/* check if the host has a http:// prefix and skip it */ |
623 |
if (proxy.host && !strncmp (proxy.host, "http://", 7)) { |
if (proxy.host && !strncmp (proxy.host, "http://", 7)) { |
624 |
char *host = m_strdup (proxy.host+7); |
char *host = m_strdup (proxy.host+7); |
|
if (!host) |
|
|
BUG (0); |
|
625 |
free_if_alloc (proxy.host); |
free_if_alloc (proxy.host); |
626 |
proxy.host = host; |
proxy.host = host; |
627 |
} |
} |
646 |
_("Keyserver Error"), MB_ERR); |
_("Keyserver Error"), MB_ERR); |
647 |
continue; |
continue; |
648 |
} |
} |
649 |
chk_pos=6; |
chk_pos = 6; |
650 |
if (strstr (s, "finger")) |
if (strstr (s, "finger")) |
651 |
chk_pos = 10; |
chk_pos = 10; |
652 |
p = strchr (s+chk_pos, ':'); |
p = strchr (s+chk_pos, ':'); |
675 |
if (fp) |
if (fp) |
676 |
fclose (fp); |
fclose (fp); |
677 |
if (!pos) { |
if (!pos) { |
678 |
/* 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 |
679 |
|
it has no valid entries. */ |
680 |
keyserver_set_default (NULL, HKP_PORT); |
keyserver_set_default (NULL, HKP_PORT); |
681 |
if (!no_config) |
if (!no_config) |
682 |
return WPTERR_CONFIG_FILE; |
return WPTERR_CONFIG_FILE; |
683 |
} |
} |
684 |
|
|
|
if (!stat (conf, &statbuf)) |
|
|
conf_timestamp = statbuf.st_mtime; |
|
685 |
return 0; |
return 0; |
686 |
} |
} |
687 |
|
|
688 |
|
|
|
/* 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; |
|
|
} |
|
|
|
|
|
|
|
689 |
/* Connect to the keyserver @hostname (port @port). |
/* Connect to the keyserver @hostname (port @port). |
690 |
Return value: 0 on success */ |
Return value: 0 on success */ |
691 |
int |
int |
692 |
kserver_connect (const char *hostname, WORD port, int *conn_fd) |
kserver_connect (const char *hostname, WORD port, int *conn_fd) |
693 |
{ |
{ |
|
int rc, fd; |
|
|
DWORD iaddr; |
|
|
char host[128] = {0}; |
|
694 |
struct hostent *hp; |
struct hostent *hp; |
695 |
struct sockaddr_in sock; |
struct sockaddr_in sock; |
696 |
|
char host[128] = {0}; |
697 |
|
DWORD iaddr; |
698 |
|
int rc, fd; |
699 |
|
|
700 |
log_debug ("kserver_connect: %s:%d\r\n", hostname, port); |
log_debug ("kserver_connect: %s:%d\r\n", hostname, port); |
701 |
|
|
705 |
*conn_fd = 0; |
*conn_fd = 0; |
706 |
hostname = skip_type_prefix (hostname); |
hostname = skip_type_prefix (hostname); |
707 |
|
|
708 |
|
if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) |
709 |
|
port = proxy.port; |
710 |
memset (&sock, 0, sizeof (sock)); |
memset (&sock, 0, sizeof (sock)); |
711 |
sock.sin_family = AF_INET; |
sock.sin_family = AF_INET; |
712 |
sock.sin_port = proxy.host? htons (proxy.port) : htons (port); |
sock.sin_port = htons (port); |
713 |
if (proxy.host) |
if (proxy.host) |
714 |
strncpy (host, proxy.host, 127); |
strncpy (host, proxy.host, 127); |
715 |
else |
else |
716 |
strncpy (host, hostname, 127); |
strncpy (host, hostname, 127); |
717 |
|
|
718 |
if ((iaddr = inet_addr (host)) != INADDR_NONE) |
if ((iaddr = inet_addr (host)) != INADDR_NONE) |
719 |
memcpy (&sock.sin_addr, &iaddr, sizeof (iaddr)); |
memcpy (&sock.sin_addr, &iaddr, sizeof (iaddr)); |
720 |
else if ((hp = gethostbyname (host))) { |
else if ((hp = gethostbyname (host))) { |
738 |
return WPTERR_WINSOCK_CONNECT; |
return WPTERR_WINSOCK_CONNECT; |
739 |
} |
} |
740 |
|
|
741 |
|
if (proxy.proto == PROXY_PROTO_SOCKS5) { |
742 |
|
rc = socks_handshake (&proxy, fd, hostname, port); |
743 |
|
if (rc) { |
744 |
|
closesocket (fd); |
745 |
|
return WPTERR_GENERAL; /* XXX: use better code. */ |
746 |
|
} |
747 |
|
} |
748 |
|
|
749 |
if (conn_fd) |
if (conn_fd) |
750 |
*conn_fd = fd; |
*conn_fd = fd; |
751 |
WSASetLastError (0); |
WSASetLastError (0); |
767 |
URL_encode (const char *url, size_t ulen, size_t *ret_nlen) |
URL_encode (const char *url, size_t ulen, size_t *ret_nlen) |
768 |
{ |
{ |
769 |
gpgme_data_t hd; |
gpgme_data_t hd; |
770 |
char numbuf[5], *pp, *p; |
char numbuf[5], *p; |
771 |
size_t i, n; |
size_t i; |
772 |
|
int nlen; |
773 |
|
|
774 |
gpgme_data_new (&hd); |
if (gpgme_data_new (&hd)) |
775 |
|
BUG (0); |
776 |
for (i=0; i < ulen; i++) { |
for (i=0; i < ulen; i++) { |
777 |
if (isalnum (url[i]) || url[i] == '-') |
if (isalnum (url[i]) || url[i] == '-') |
778 |
gpg_data_putc (hd, url[i]); |
gpg_data_putc (hd, url[i]); |
783 |
gpgme_data_write (hd, numbuf, strlen (numbuf)); |
gpgme_data_write (hd, numbuf, strlen (numbuf)); |
784 |
} |
} |
785 |
} |
} |
|
gpg_data_putc (hd, '\0'); |
|
786 |
|
|
787 |
/* Copy memory to avoid that we need to use gpgme_free later. */ |
p = data_release_and_get_mem (hd, &nlen); |
|
pp = gpgme_data_release_and_get_mem (hd, &n); |
|
|
p = m_strdup (pp); |
|
|
gpgme_free (pp); |
|
788 |
if (ret_nlen) |
if (ret_nlen) |
789 |
*ret_nlen = n; |
*ret_nlen = nlen; |
790 |
return p; |
return p; |
791 |
} |
} |
792 |
|
|
806 |
if (!port) |
if (!port) |
807 |
port = HKP_PORT; |
port = HKP_PORT; |
808 |
reqlen = 512 + strlen (hostname) + 2*strlen (pubkey); |
reqlen = 512 + strlen (hostname) + 2*strlen (pubkey); |
809 |
|
if (proxy.proto == PROXY_PROTO_HTTP && proxy.base64_user) |
810 |
|
reqlen += strlen (proxy.base64_user) + 1; |
811 |
request = new char[reqlen]; |
request = new char[reqlen]; |
812 |
if (!request) |
if (!request) |
813 |
BUG (0); |
BUG (0); |
817 |
return NULL; |
return NULL; |
818 |
} |
} |
819 |
|
|
820 |
if (proxy.user) { |
if (proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
821 |
_snprintf (request, reqlen-1, |
_snprintf (request, reqlen-1, |
822 |
"POST http://%s:%d/pks/add HTTP/1.0\r\n" |
"POST http://%s:%d/pks/add HTTP/1.0\r\n" |
823 |
"Referer: \r\n" |
"Referer: \r\n" |
828 |
"Content-length: %d\r\n" |
"Content-length: %d\r\n" |
829 |
"\r\n" |
"\r\n" |
830 |
"keytext=%s" |
"keytext=%s" |
831 |
"\n", |
"\r\n", |
832 |
skip_type_prefix (hostname), port, hostname, port, |
skip_type_prefix (hostname), port, hostname, port, |
833 |
proxy.base64_user, enc_octets+9, enc_pubkey); |
proxy.base64_user, enc_octets+9, enc_pubkey); |
834 |
} |
} |
842 |
"Content-length: %d\r\n" |
"Content-length: %d\r\n" |
843 |
"\r\n" |
"\r\n" |
844 |
"keytext=%s" |
"keytext=%s" |
845 |
"\n", |
"\r\n", |
846 |
skip_type_prefix (hostname), port, |
skip_type_prefix (hostname), port, |
847 |
enc_octets+9, enc_pubkey); |
enc_octets+9, enc_pubkey); |
848 |
} |
} |
853 |
} |
} |
854 |
|
|
855 |
|
|
856 |
|
/* Interface for receiving a public key. */ |
857 |
int |
int |
858 |
kserver_recvkey_ext (const char *hostname, WORD port, const char *keyid, |
kserver_recvkey (const char *hostname, WORD port, const char *keyid, |
859 |
char **r_key, int *r_keylen) |
char **r_key, int *r_keylen) |
860 |
{ |
{ |
861 |
char *request = NULL; |
char *request = NULL; |
862 |
int conn_fd; |
int conn_fd; |
863 |
int rc, n; |
int rc; |
864 |
|
|
865 |
if (!port) |
if (!port) |
866 |
port = HKP_PORT; |
port = HKP_PORT; |
873 |
if (!request) |
if (!request) |
874 |
BUG (0); |
BUG (0); |
875 |
|
|
876 |
if (proxy.host && proxy.user) { |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
877 |
_snprintf (request, 300, |
_snprintf (request, 300, |
878 |
"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" |
879 |
"Proxy-Authorization: Basic %s\r\n\r\n", |
"Proxy-Authorization: Basic %s\r\n\r\n", |
880 |
skip_type_prefix (hostname), port, keyid, proxy.base64_user); |
skip_type_prefix (hostname), port, keyid, proxy.base64_user); |
881 |
} |
} |
882 |
else if (proxy.host) { |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
883 |
_snprintf (request, 300, |
_snprintf (request, 300, |
884 |
"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", |
885 |
skip_type_prefix (hostname), port, keyid); |
skip_type_prefix (hostname), port, keyid); |
897 |
goto leave; |
goto leave; |
898 |
} |
} |
899 |
|
|
900 |
rc = sock_read_ext (conn_fd, r_key, &n); |
rc = sock_read_ext (conn_fd, r_key, r_keylen); |
901 |
if (rc == SOCKET_ERROR) { |
if (rc == SOCKET_ERROR) { |
902 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
903 |
goto leave; |
goto leave; |
904 |
} |
} |
905 |
|
|
906 |
if (r_keylen) |
log_debug ("%s\r\n", *r_key); |
|
*r_keylen = n; |
|
|
log_debug("%s\r\n", *r_key); |
|
907 |
rc = check_hkp_response (*r_key, 1); |
rc = check_hkp_response (*r_key, 1); |
908 |
if (rc) |
if (rc) |
909 |
goto leave; |
goto leave; |
916 |
return rc; |
return rc; |
917 |
} |
} |
918 |
|
|
|
/* Interface for receiving a public key. */ |
|
|
int |
|
|
kserver_recvkey (const char *hostname, WORD port, const char *keyid, |
|
|
char *key, int maxkeylen) |
|
|
{ |
|
|
char *tmpkey; |
|
|
int rc, nread=0; |
|
|
|
|
|
/* XXX: just a wrapper around the new method, replace it |
|
|
soon as possible. */ |
|
|
rc = kserver_recvkey_ext (hostname, port, keyid, &tmpkey, &nread); |
|
|
if (rc) |
|
|
return rc; |
|
|
|
|
|
if (nread > maxkeylen) { |
|
|
free_if_alloc (tmpkey); |
|
|
return WPTERR_GENERAL; |
|
|
} |
|
|
strcpy (key, tmpkey); |
|
|
free_if_alloc (tmpkey); |
|
|
return 0; |
|
|
} |
|
|
|
|
919 |
|
|
920 |
/* Interface to send a public key. */ |
/* Interface to send a public key. */ |
921 |
int |
int |
922 |
kserver_sendkey (const char *hostname, WORD port, const char *pubkey, int len ) |
kserver_sendkey (const char *hostname, WORD port, const char *pubkey, int len ) |
923 |
{ |
{ |
924 |
char *request = NULL; |
char *request = NULL; |
925 |
char log[2048]; |
char log[2048] = {0}; |
926 |
int conn_fd, n; |
int conn_fd, n; |
927 |
int rc; |
int rc; |
928 |
|
|
949 |
goto leave; |
goto leave; |
950 |
} |
} |
951 |
|
|
952 |
log_debug ("kserver_sendkey:\r\n%s\r\n", log); |
log_debug ("kserver_sendkey: read %d bytes\r\n%s\r\n", n, log); |
953 |
rc = check_hkp_response (log, 0); |
rc = check_hkp_response (log, 0); |
954 |
if (rc) |
if (rc) |
955 |
goto leave; |
goto leave; |
963 |
} |
} |
964 |
|
|
965 |
|
|
966 |
|
/* Check keyserver response. */ |
967 |
|
static int |
968 |
|
kserver_search_chkresp (int fd) |
969 |
|
{ |
970 |
|
char buf[128]; |
971 |
|
int n=0; |
972 |
|
|
973 |
|
/* parse response 'HTTP/1.0 500 OK' */ |
974 |
|
if (sock_getline (fd, buf, 127, &n)) |
975 |
|
return WPTERR_KEYSERVER_NOTFOUND; |
976 |
|
|
977 |
|
log_debug ("kserver_search_chkpresp: %s\r\n", buf); |
978 |
|
if (strncmp (buf, "HTTP/1.", 7)) |
979 |
|
return WPTERR_KEYSERVER_NOTFOUND; |
980 |
|
if (strncmp (buf+(8+1), "200", 3)) |
981 |
|
return WPTERR_KEYSERVER_NOTFOUND; |
982 |
|
return 0; |
983 |
|
} |
984 |
|
|
985 |
|
|
986 |
|
/* End the keyserver search procedure. */ |
987 |
|
void |
988 |
|
kserver_search_end (int conn_fd) |
989 |
|
{ |
990 |
|
log_debug ("kserver_search_end: fd=%d\r\n", conn_fd); |
991 |
|
closesocket (conn_fd); |
992 |
|
} |
993 |
|
|
994 |
|
|
995 |
|
/* Begin keyserver search procedure. */ |
996 |
int |
int |
997 |
kserver_search_init (const char *hostname, WORD port, |
kserver_search_begin (const char *hostname, WORD port, |
998 |
const char *keyid, int *conn_fd) |
const char *pattern, int *conn_fd) |
999 |
{ |
{ |
1000 |
char *request = NULL; |
char *request = NULL; |
1001 |
char *enc_keyid = NULL; |
char *enc_patt = NULL; |
1002 |
int n = 0; |
int n; |
1003 |
int rc, sock_fd; |
int rc, sock_fd; |
1004 |
|
|
1005 |
rc = kserver_connect (hostname, port, &sock_fd); |
rc = kserver_connect (hostname, port, &sock_fd); |
1008 |
goto leave; |
goto leave; |
1009 |
} |
} |
1010 |
|
|
1011 |
enc_keyid = URL_encode (keyid, strlen (keyid), NULL); |
enc_patt = URL_encode (pattern, strlen (pattern), NULL); |
1012 |
n=300; |
n = 140 + strlen (enc_patt) + strlen (hostname) + 32 + 2; |
1013 |
|
if (proxy.base64_user) |
1014 |
|
n += strlen (proxy.base64_user) + 1; |
1015 |
request = new char[n+1]; |
request = new char[n+1]; |
1016 |
if (!request) |
if (!request) |
1017 |
BUG (0); |
BUG (0); |
1018 |
|
|
1019 |
if (proxy.host && proxy.user) { |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
1020 |
_snprintf (request, n, |
_snprintf (request, n, |
1021 |
"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" |
1022 |
"Proxy-Authorization: Basic %s\r\n\r\n", |
"Proxy-Authorization: Basic %s\r\n\r\n", |
1023 |
skip_type_prefix (hostname), port, enc_keyid, proxy.base64_user); |
skip_type_prefix (hostname), port, enc_patt, proxy.base64_user); |
1024 |
} |
} |
1025 |
else if (proxy.host) { |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
1026 |
_snprintf (request, n, |
_snprintf (request, n, |
1027 |
"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", |
1028 |
skip_type_prefix (hostname), port, enc_keyid); |
skip_type_prefix (hostname), port, enc_patt); |
1029 |
} |
} |
1030 |
else { |
else { |
1031 |
_snprintf (request, n, |
_snprintf (request, n, |
1032 |
"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", |
1033 |
enc_keyid); |
enc_patt); |
1034 |
} |
} |
1035 |
|
|
1036 |
log_debug ("kserver_search_init:\r\n%s\r\n", request); |
log_debug ("kserver_search_begin:\r\n%s\r\n", request); |
1037 |
|
|
1038 |
if (sock_write (sock_fd, request, strlen (request)) == SOCKET_ERROR) { |
if (sock_write (sock_fd, request, strlen (request)) == SOCKET_ERROR) { |
1039 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1040 |
goto leave; |
goto leave; |
1041 |
} |
} |
1042 |
|
|
1043 |
|
rc = kserver_search_chkresp (sock_fd); |
1044 |
|
if (rc) { |
1045 |
|
closesocket (sock_fd); |
1046 |
|
sock_fd = 0; |
1047 |
|
} |
1048 |
|
|
1049 |
*conn_fd = sock_fd; |
*conn_fd = sock_fd; |
1050 |
|
|
1051 |
leave: |
leave: |
1052 |
free_if_alloc (request); |
free_if_alloc (request); |
1053 |
free_if_alloc (enc_keyid); |
free_if_alloc (enc_patt); |
1054 |
return rc; |
return rc; |
1055 |
} |
} |
1056 |
|
|
1057 |
|
|
|
/* 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; |
|
|
} |
|
1058 |
|
|
1059 |
|
|
1060 |
/* Convert an iso date @iso_date (YYYY-MM-DD) into the locale |
/* Convert an iso date @iso_date (YYYY-MM-DD) into the locale |
1089 |
|
|
1090 |
|
|
1091 |
int |
int |
1092 |
kserver_search (int fd, keyserver_key *key) |
kserver_search_next (int fd, keyserver_key *key) |
1093 |
{ |
{ |
1094 |
char buf[1024], *p; |
char buf[1024], *p; |
1095 |
int uidlen, nbytes, pos = 0; |
int uidlen, nbytes, pos = 0; |
1096 |
|
|
1097 |
log_debug ("keyserver_search:\r\n"); |
log_debug ("keyserver_search_next:\r\n"); |
1098 |
|
|
1099 |
if (sock_getline (fd, buf, sizeof (buf) - 1, &nbytes)) |
if (sock_getline (fd, buf, sizeof (buf) - 1, &nbytes)) |
1100 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
1101 |
|
|
1102 |
|
/* XXX: use maschine readable option. */ |
1103 |
log_debug ("%s\r\n", buf); |
log_debug ("%s\r\n", buf); |
1104 |
|
|
1105 |
if (!strncmp (buf, "pub", 3)) { |
if (!strncmp (buf, "pub", 3)) { |
1160 |
} |
} |
1161 |
|
|
1162 |
|
|
|
/* 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); |
|
|
} |
|
|
|
|
|
|
|
1163 |
/* Release mbmers in the proxy context @ctx. */ |
/* Release mbmers in the proxy context @ctx. */ |
1164 |
void |
void |
1165 |
proxy_release (keyserver_proxy_ctx *ctx) |
kserver_proxy_release (keyserver_proxy_t ctx) |
1166 |
{ |
{ |
1167 |
free_if_alloc (ctx->host); |
free_if_alloc (ctx->host); |
1168 |
free_if_alloc (ctx->pass); |
free_if_alloc (ctx->pass); |
1169 |
free_if_alloc (ctx->user); |
free_if_alloc (ctx->user); |
1170 |
|
ctx->port = ctx->proto = 0; |
1171 |
} |
} |
1172 |
|
|
1173 |
|
|
1198 |
} |
} |
1199 |
|
|
1200 |
|
|
1201 |
/* Receive an key via LDAP from host @host with the keyid @keyid. |
static FILE* |
1202 |
@key contains the key on success. */ |
do_spawn_ldap_helper (const char *host, const char *keyid) |
1203 |
int |
{ |
1204 |
ldap_recvkey (const char *host, const char *keyid, char *key, int maxkeylen) |
FILE *fp = NULL; |
1205 |
{ |
char *p, *sep; |
1206 |
FILE *inp; |
char *ksprg; |
1207 |
DWORD n; |
char outf[256], inf[256]; |
1208 |
const char *s; |
size_t n; |
|
char *ksprg = NULL, *p = NULL, *sep; |
|
|
char temp[512], outf[512]; |
|
|
int start_key = 0, failed = 0; |
|
|
int rc = 0; |
|
1209 |
|
|
1210 |
p = get_gnupg_prog (); |
p = get_gnupg_prog (); |
1211 |
n = strlen (p) + 1 + 128; |
n = strlen (p) + 1 + 128; |
1221 |
if (file_exist_check (ksprg)) { |
if (file_exist_check (ksprg)) { |
1222 |
log_box ("LDAP Keyserver Plugin", MB_ERR, |
log_box ("LDAP Keyserver Plugin", MB_ERR, |
1223 |
"%s: could not find LDAP keyserver module!", ksprg); |
"%s: could not find LDAP keyserver module!", ksprg); |
1224 |
rc = -1; |
fp = NULL; |
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)); |
|
rc = -1; |
|
1233 |
goto leave; |
goto leave; |
1234 |
} |
} |
1235 |
fprintf (inp, |
fprintf (fp, |
1236 |
"VERSION 1\n" |
"VERSION 1\n" |
1237 |
"PROGRAM 1.4.3-cvs\n" |
"PROGRAM 1.4.3-cvs\n" |
1238 |
"SCHEME ldap\n" |
"SCHEME ldap\n" |
1241 |
"\n" |
"\n" |
1242 |
"%s\n", |
"%s\n", |
1243 |
host? skip_type_prefix (host): "64.94.85.200", keyid); |
host? skip_type_prefix (host): "64.94.85.200", keyid); |
1244 |
fclose (inp); |
fclose (fp); |
1245 |
|
|
1246 |
p = new char[strlen (ksprg) + strlen (temp) + strlen (outf) + 32]; |
p = new char[strlen (ksprg) + strlen (inf) + strlen (outf) + 32]; |
1247 |
if (!p) |
if (!p) |
1248 |
BUG (NULL); |
BUG (NULL); |
1249 |
sprintf (p, "%s -o %s %s", ksprg, outf, temp); |
sprintf (p, "%s -o %s %s", ksprg, outf, inf); |
1250 |
if (spawn_application (p)) { |
if (spawn_application (p)) { |
1251 |
rc = -1; |
fp = NULL; |
1252 |
goto leave; |
goto leave; |
1253 |
} |
} |
1254 |
DeleteFile (temp); |
fp = fopen (outf, "rb"); |
1255 |
|
if (!fp) |
|
inp = fopen (outf, "rb"); |
|
|
if (!inp) { |
|
1256 |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf, |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf, |
1257 |
winpt_strerror (WPTERR_FILE_OPEN)); |
winpt_strerror (WPTERR_FILE_OPEN)); |
1258 |
rc = -1; |
|
1259 |
goto leave; |
leave: |
1260 |
} |
DeleteFile (inf); |
1261 |
memset (key, 0, maxkeylen); |
DeleteFile (outf); |
1262 |
while (!feof (inp)) { |
free_if_alloc (p); |
1263 |
s = fgets (temp, sizeof (temp)-1, inp); |
free_if_alloc (ksprg); |
1264 |
|
return fp; |
1265 |
|
} |
1266 |
|
|
1267 |
|
/* Receive an key via LDAP from host @host with the keyid @keyid. |
1268 |
|
@key contains the key on success. */ |
1269 |
|
int |
1270 |
|
ldap_recvkey (const char *host, const char *keyid, |
1271 |
|
char **r_key, int *r_keylen) |
1272 |
|
{ |
1273 |
|
gpgme_data_t raw; |
1274 |
|
FILE *fp; |
1275 |
|
const char *s; |
1276 |
|
char buf[512]; |
1277 |
|
int start_key = 0, failed = 0; |
1278 |
|
int rc = 0; |
1279 |
|
|
1280 |
|
fp = do_spawn_ldap_helper (host, keyid); |
1281 |
|
if (!fp) |
1282 |
|
return WPTERR_GENERAL; |
1283 |
|
|
1284 |
|
if (gpgme_data_new (&raw)) |
1285 |
|
BUG (0); |
1286 |
|
while (!feof (fp)) { |
1287 |
|
s = fgets (buf, sizeof (buf)-1, fp); |
1288 |
if (!s) |
if (!s) |
1289 |
break; |
break; |
1290 |
if (strstr (s, "KEY") && strstr (s, "FAILED")) { |
if (strstr (s, "KEY") && strstr (s, "FAILED")) { |
1294 |
if (!start_key && strstr (s, "KEY") && strstr (s, "BEGIN")) { |
if (!start_key && strstr (s, "KEY") && strstr (s, "BEGIN")) { |
1295 |
start_key = 1; |
start_key = 1; |
1296 |
continue; |
continue; |
1297 |
} |
} |
1298 |
if (!start_key) |
if (!start_key) |
1299 |
continue; |
continue; |
1300 |
strcat (key, temp); |
gpgme_data_write (raw, buf, strlen (buf)); |
1301 |
maxkeylen -= strlen (temp); |
if (strstr (s, "KEY") && strstr (s, "END")) |
|
if (maxkeylen < 0 || (strstr (s, "KEY") && strstr (s, "END"))) |
|
1302 |
break; |
break; |
1303 |
} |
} |
1304 |
fclose (inp); |
fclose (fp); |
1305 |
|
|
1306 |
leave: |
if (failed) |
|
if (failed || !strlen (key)) |
|
1307 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
1308 |
DeleteFile (outf); |
*r_key = data_release_and_get_mem (raw, r_keylen); |
|
free_if_alloc (p); |
|
|
free_if_alloc (ksprg); |
|
1309 |
return rc; |
return rc; |
1310 |
} |
} |
1311 |
|
|
1312 |
|
|
|
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; |
|
|
} |
|
|
|
|
|
|
|
1313 |
/* Receive an key via FINGER from host @host with the user @user. |
/* Receive an key via FINGER from host @host with the user @user. |
1314 |
On success @key contains the key. */ |
On success @key contains the key. */ |
1315 |
int |
int |
1316 |
finger_recvkey (const char *host, const char *user, char *key, int maxkeylen) |
finger_recvkey (const char *host, const char *user, |
1317 |
|
char **r_key, int *r_keylen) |
1318 |
{ |
{ |
1319 |
struct hostent * hp; |
gpgme_data_t raw; |
1320 |
struct sockaddr_in saddr; |
char buf[256]; |
|
char buf[128]; |
|
1321 |
int fd, nread; |
int fd, nread; |
1322 |
int start_key = 0; |
int start_key = 0; |
1323 |
int rc=0; |
int rc=0; |
1324 |
|
|
1325 |
fd = socket (PF_INET, SOCK_STREAM, 0); |
rc = kserver_connect (host, FINGER_PORT, &fd); |
1326 |
if (fd == -1) |
if (rc) |
1327 |
return WPTERR_WINSOCK_SOCKET; |
return rc; |
|
hp = gethostbyname (skip_type_prefix (host)); |
|
|
if (!hp) { |
|
|
closesocket (fd); |
|
|
return WPTERR_WINSOCK_RESOLVE; |
|
|
} |
|
1328 |
|
|
1329 |
memset (&saddr, 0, sizeof (saddr)); |
sock_write (fd, user, strlen (user)); |
1330 |
saddr.sin_family = AF_INET; |
sock_write (fd, "\r\n", 2); |
1331 |
saddr.sin_port = htons (FINGER_PORT); |
|
1332 |
saddr.sin_addr = *(struct in_addr *)hp->h_addr; |
if (gpgme_data_new (&raw)) |
1333 |
if (connect (fd, (struct sockaddr *)&saddr, sizeof (saddr))) { |
BUG (0); |
1334 |
closesocket (fd); |
|
1335 |
return WPTERR_WINSOCK_CONNECT; |
for (;;) { |
1336 |
} |
if (sock_getline (fd, buf, sizeof (buf), &nread)) |
1337 |
send (fd, user, strlen (user), 0); |
break; |
|
send (fd, "\r\n", 2, 0); |
|
|
|
|
|
memset (key, 0, maxkeylen); |
|
|
while (maxkeylen > 0) { |
|
|
nread = finger_readline (fd, buf, sizeof (buf)-1); |
|
|
if (nread <= 0) |
|
|
break; |
|
1338 |
strcat (buf, "\n"); |
strcat (buf, "\n"); |
1339 |
if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) { |
if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) { |
1340 |
strcat (key, buf); |
gpgme_data_write (raw, buf, nread); |
1341 |
start_key = 1; |
start_key = 1; |
|
maxkeylen -= nread; |
|
1342 |
} |
} |
1343 |
else if (strstr (buf, "END PGP PUBLIC KEY BLOCK" ) && start_key) { |
else if (strstr (buf, "END PGP PUBLIC KEY BLOCK" ) && start_key) { |
1344 |
strcat (key, buf); |
gpgme_data_write (raw, buf, nread); |
1345 |
start_key--; |
start_key--; |
|
maxkeylen -= nread; |
|
1346 |
break; |
break; |
1347 |
} |
} |
1348 |
else if (start_key) { |
else if (start_key) |
1349 |
strcat (key, buf); |
gpgme_data_write (raw, buf, nread); |
|
maxkeylen -= nread; |
|
|
} |
|
1350 |
} |
} |
1351 |
|
|
1352 |
closesocket (fd); |
closesocket (fd); |
1353 |
if (start_key != 0) |
if (start_key != 0) |
1354 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
1355 |
|
*r_key = data_release_and_get_mem (raw, r_keylen); |
1356 |
return rc; |
return rc; |
1357 |
} |
} |
1358 |
|
|