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. |
36 |
#include "wptW32API.h" |
#include "wptW32API.h" |
37 |
#include "wptGPG.h" |
#include "wptGPG.h" |
38 |
#include "wptRegistry.h" |
#include "wptRegistry.h" |
39 |
|
#include "wptUTF8.h" |
40 |
|
|
41 |
/* just map net_errno to a winsock error. */ |
/* Map net_errno to a winsock error. */ |
42 |
#define net_errno ((int)WSAGetLastError ()) |
#define net_errno ((int)WSAGetLastError ()) |
43 |
|
|
44 |
|
|
45 |
keyserver server[MAX_KEYSERVERS] = {0}; |
keyserver server[MAX_KEYSERVERS] = {0}; |
46 |
keyserver_proxy_s proxy = {0}; |
keyserver_proxy_s proxy = {0}; |
47 |
static const char *server_list[] = { |
static const char *server_list[] = { |
|
"hkp://gnv.us.ks.cryptnet.net", |
|
|
"hkp://keyserver.kjsl.com", |
|
48 |
"hkp://sks.keyserver.penguin.de", |
"hkp://sks.keyserver.penguin.de", |
49 |
"hkp://subkeys.pgp.net", |
"hkp://subkeys.pgp.net", |
50 |
"ldap://keyserver.pgp.com", |
"ldap://keyserver.pgp.com", |
60 |
WORD default_keyserver_port = 0; |
WORD default_keyserver_port = 0; |
61 |
|
|
62 |
/* Default socket timeout. */ |
/* Default socket timeout. */ |
63 |
static int default_socket_timeout = 10; |
static int default_socket_timeout = 6; |
64 |
|
|
65 |
|
|
66 |
|
/* Wrapper for safe memory allocation. */ |
67 |
|
static char* |
68 |
|
safe_alloc (DWORD n) |
69 |
|
{ |
70 |
|
char *p; |
71 |
|
|
72 |
|
p = new char[n+1]; |
73 |
|
if (!p) |
74 |
|
BUG (0); |
75 |
|
memset (p, 0, n); |
76 |
|
return p; |
77 |
|
} |
78 |
|
|
79 |
/* Basic64 encode the input @inbuf to @outbuf. */ |
/* Basic64 encode the input @inbuf to @outbuf. */ |
80 |
static void |
static void |
121 |
} |
} |
122 |
|
|
123 |
|
|
124 |
|
/* Check that the given buffer contains a valid keyserver URL |
125 |
|
and return the prefix length, 0 in case of an error. */ |
126 |
|
static int |
127 |
|
check_URL (const char *buf) |
128 |
|
{ |
129 |
|
const char *proto[] = { |
130 |
|
"ldap://", |
131 |
|
"http://", |
132 |
|
"finger://", |
133 |
|
"hkp://", |
134 |
|
NULL}; |
135 |
|
int i; |
136 |
|
|
137 |
|
if (strlen (buf) < 7) |
138 |
|
return 0; |
139 |
|
|
140 |
|
for (i=0; proto[i] != NULL; i++) { |
141 |
|
if (strstr (buf, proto[i])) |
142 |
|
return strlen (proto[i]); |
143 |
|
} |
144 |
|
return 0; |
145 |
|
} |
146 |
|
|
147 |
|
|
148 |
/* Skip the URL schema and return only the host part of it. */ |
/* Skip the URL schema and return only the host part of it. */ |
149 |
static const char* |
static const char* |
150 |
skip_type_prefix (const char *hostname) |
skip_type_prefix (const char *hostname) |
151 |
{ |
{ |
152 |
if (hostname && !strncmp (hostname, "http://", 7)) |
int pos; |
153 |
hostname += 7; |
|
154 |
else if (hostname && !strncmp (hostname, "hkp://", 6)) |
if (!hostname) |
155 |
hostname += 6; |
return hostname; |
156 |
else if (hostname && !strncmp (hostname, "finger://", 9)) |
|
157 |
hostname += 9; |
pos = check_URL (hostname); |
158 |
else if (hostname && !strncmp (hostname, "ldap://", 7)) |
if (pos > 0) |
159 |
hostname += 7; |
hostname += pos; |
160 |
return hostname; |
return hostname; |
161 |
} |
} |
162 |
|
|
169 |
char *p, *end; |
char *p, *end; |
170 |
int ec, len; |
int ec, len; |
171 |
|
|
172 |
|
log_debug ("check_hkp_response: '%s'\r\n", resp); |
173 |
ec = recv ? WPTERR_WINSOCK_RECVKEY : WPTERR_WINSOCK_SENDKEY; |
ec = recv ? WPTERR_WINSOCK_RECVKEY : WPTERR_WINSOCK_SENDKEY; |
174 |
if (!strstr (resp, "HTTP/1.0 200 OK") && |
if (!strstr (resp, "HTTP/1.0 200 OK") && |
175 |
!strstr (resp, "HTTP/1.1 200 OK")) /* http error */ |
!strstr (resp, "HTTP/1.1 200 OK")) /* http error */ |
178 |
|| strstr (resp, "Public Key Server -- Error") |
|| strstr (resp, "Public Key Server -- Error") |
179 |
|| strstr (resp, "No matching keys in database")) { |
|| strstr (resp, "No matching keys in database")) { |
180 |
p = strstr (resp, "<p>"); |
p = strstr (resp, "<p>"); |
181 |
if (p && strlen (p) < sizeof (hkp_errmsg)) { |
if (p && strlen (p) < sizeof (hkp_errmsg)-1) { |
182 |
end = strstr (p, "</p>"); |
end = strstr (p, "</p>"); |
183 |
len = end? (end - p + 1) : strlen (p); |
len = end? (end - p + 1) : strlen (p); |
184 |
memset (hkp_errmsg, 0, sizeof (hkp_errmsg)); |
memset (hkp_errmsg, 0, sizeof (hkp_errmsg)); |
249 |
DWORD nread; |
DWORD nread; |
250 |
int nleft = buflen; |
int nleft = buflen; |
251 |
int rc, n = 0; |
int rc, n = 0; |
252 |
|
|
253 |
|
if (nbytes) |
254 |
|
*nbytes = 0; |
255 |
while (nleft > 0) { |
while (nleft > 0) { |
256 |
if (n >= default_socket_timeout) |
if (n >= default_socket_timeout) |
257 |
return WPTERR_WINSOCK_TIMEOUT; |
return WPTERR_WINSOCK_TIMEOUT; |
278 |
return 0; |
return 0; |
279 |
} |
} |
280 |
|
|
281 |
|
|
282 |
|
/* Helper to create a string from the gpgme data and |
283 |
|
then release the gpgme data object. */ |
284 |
|
char* |
285 |
|
data_release_and_get_mem (gpgme_data_t in, int *r_bufferlen) |
286 |
|
{ |
287 |
|
size_t n; |
288 |
|
char *buffer, *p; |
289 |
|
|
290 |
|
gpg_data_putc (in, '\0'); |
291 |
|
p = gpgme_data_release_and_get_mem (in, &n); |
292 |
|
buffer = m_strdup (p); |
293 |
|
if (r_bufferlen) |
294 |
|
*r_bufferlen = (int)n; |
295 |
|
gpgme_free (p); |
296 |
|
return buffer; |
297 |
|
} |
298 |
|
|
299 |
|
|
300 |
/* Read much data as possible from the socket @fd and |
/* Read much data as possible from the socket @fd and |
301 |
return the data in @buffer. Caller must free data. |
return the data in @buffer. Caller must free data. |
302 |
Return value: 0 on success. */ |
Return value: 0 on success. */ |
304 |
sock_read_ext (int fd, char **buffer, int *r_bufferlen) |
sock_read_ext (int fd, char **buffer, int *r_bufferlen) |
305 |
{ |
{ |
306 |
gpgme_data_t dh; |
gpgme_data_t dh; |
307 |
char buf[1024], *p; |
char buf[1024]; |
308 |
size_t n=0; |
size_t n=0; |
309 |
int nread, rc; |
int nread, rc; |
310 |
|
|
311 |
gpgme_data_new (&dh); |
if (gpgme_data_new (&dh)) |
312 |
|
BUG (0); |
313 |
while (n < 10) { |
while (n < 10) { |
314 |
rc = sock_select (fd, 1); |
rc = sock_select (fd, 1); |
315 |
if (rc == SOCKET_ERROR) { |
if (rc == SOCKET_ERROR) { |
327 |
gpgme_data_write (dh, buf, nread); |
gpgme_data_write (dh, buf, nread); |
328 |
} |
} |
329 |
} |
} |
330 |
gpg_data_putc (dh, '\0'); |
|
331 |
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); |
|
332 |
return 0; |
return 0; |
333 |
} |
} |
334 |
|
|
385 |
} |
} |
386 |
kserver_proxy_release (&proxy); |
kserver_proxy_release (&proxy); |
387 |
WSACleanup (); |
WSACleanup (); |
388 |
|
log_debug ("wsock_end: cleanup finished.\r\n"); |
389 |
} |
} |
390 |
|
|
391 |
|
|
392 |
/* Return a string representation of a winsock error. */ |
/* Return a string representation of a winsock error. */ |
393 |
const char* |
const char* |
394 |
wsock_strerror (void) |
wsock_strerror (void) |
395 |
{ |
{ |
|
static char buf[384]; |
|
396 |
int ec = WSAGetLastError (); |
int ec = WSAGetLastError (); |
397 |
|
|
398 |
|
if (!ec) |
399 |
|
return ""; |
400 |
switch (ec) { |
switch (ec) { |
401 |
case WSAENETDOWN: |
case WSAENETDOWN: |
402 |
return _("The network subsystem has failed"); |
return _("Network unreachable"); |
403 |
|
|
404 |
|
case WSAEHOSTUNREACH: |
405 |
|
return _("Host unreachable"); |
406 |
|
|
407 |
case WSAHOST_NOT_FOUND: |
case WSAHOST_NOT_FOUND: |
408 |
return _("Authoritative Answer Host not found"); |
return _("Could not resolve host name"); |
409 |
|
|
410 |
|
case WSAECONNREFUSED: |
411 |
|
return _("Connection refused"); |
412 |
|
|
413 |
case WSAETIMEDOUT: |
case WSAETIMEDOUT: |
414 |
return _("The connection has been dropped because of a network failure"); |
case WSAECONNABORTED: |
415 |
|
return _("Connection timeout"); |
416 |
|
|
417 |
|
case WSAENETRESET: |
418 |
|
case WSAECONNRESET: |
419 |
|
return _("Connection resetted by peer"); |
420 |
|
|
421 |
|
case WSAESHUTDOWN: |
422 |
|
return _("Socket has been shutdown"); |
423 |
|
|
424 |
default: |
default: |
425 |
_snprintf (buf, sizeof (buf) -1, _("Unknown Winsock error ec=%d"),ec); |
break; |
|
return buf; |
|
426 |
} |
} |
427 |
return NULL; |
|
428 |
|
return ""; |
429 |
} |
} |
430 |
|
|
431 |
|
|
433 |
void |
void |
434 |
kserver_set_socket_timeout (int nsec) |
kserver_set_socket_timeout (int nsec) |
435 |
{ |
{ |
436 |
if (nsec < 0) |
if (nsec < 0 || nsec > 3600) |
437 |
nsec = 0; |
nsec = 0; |
438 |
default_socket_timeout = nsec; |
default_socket_timeout = nsec; |
439 |
} |
} |
459 |
return default_keyserver; |
return default_keyserver; |
460 |
} |
} |
461 |
else if (!type && idx < DIM (server_list)) { |
else if (!type && idx < DIM (server_list)) { |
462 |
|
/* XXX: handle non HKP servers. */ |
463 |
*port = HKP_PORT; |
*port = HKP_PORT; |
464 |
return server_list[idx]; |
return server_list[idx]; |
465 |
} |
} |
478 |
closesocket (fd); |
closesocket (fd); |
479 |
return 0; |
return 0; |
480 |
} |
} |
481 |
|
log_debug ("kserver_check_inet_connection: no inet connection.\r\n"); |
482 |
return -1; |
return -1; |
483 |
} |
} |
484 |
|
|
490 |
const char* |
const char* |
491 |
kserver_check_keyid (const char *keyid) |
kserver_check_keyid (const char *keyid) |
492 |
{ |
{ |
493 |
static char id[21]; |
static char id[22]; |
494 |
|
|
495 |
if (strstr (keyid, "@")) |
if (strstr (keyid, "@")) |
496 |
return keyid; /* email address */ |
return keyid; /* email address */ |
512 |
|
|
513 |
n = 4*strlen (proxy_user) / 3 + 32 + strlen (proxy_pass) + 2; |
n = 4*strlen (proxy_user) / 3 + 32 + strlen (proxy_pass) + 2; |
514 |
free_if_alloc (proxy.base64_user); |
free_if_alloc (proxy.base64_user); |
515 |
proxy.base64_user = new char[n]; |
proxy.base64_user = safe_alloc (n+1); |
|
if (!proxy.base64_user) |
|
|
BUG (0); |
|
516 |
_snprintf (t, sizeof (t)-1, "%s:%s", proxy_user, proxy_pass); |
_snprintf (t, sizeof (t)-1, "%s:%s", proxy_user, proxy_pass); |
517 |
base64_encode (t, proxy.base64_user); |
base64_encode (t, proxy.base64_user); |
518 |
free_if_alloc (proxy.user); |
free_if_alloc (proxy.user); |
520 |
proxy.user = m_strdup (proxy_user); |
proxy.user = m_strdup (proxy_user); |
521 |
proxy.pass = m_strdup (proxy_pass); |
proxy.pass = m_strdup (proxy_pass); |
522 |
} |
} |
|
|
|
|
|
|
|
/* Check that the given buffer contains a valid keyserver URL. */ |
|
|
static int |
|
|
check_URL (const char * buf) |
|
|
{ |
|
|
if (strlen (buf) < 7) |
|
|
return -1; |
|
|
if (!strstr (buf, "ldap://") |
|
|
&& !strstr (buf, "http://") |
|
|
&& !strstr (buf, "finger://") |
|
|
&& !strstr (buf, "hkp://")) |
|
|
return -1; |
|
|
return 0; |
|
|
} |
|
523 |
|
|
524 |
|
|
525 |
/* Get the port number from the given protocol. */ |
/* Get the port number from the given protocol. */ |
527 |
port_from_proto (int proto) |
port_from_proto (int proto) |
528 |
{ |
{ |
529 |
switch (proto) { |
switch (proto) { |
530 |
case KSPROTO_LDAP: return 0; |
case KSPROTO_LDAP: return LDAP_PORT; |
531 |
case KSPROTO_FINGER: return FINGER_PORT; |
case KSPROTO_FINGER: return FINGER_PORT; |
532 |
case KSPROTO_HTTP: return HKP_PORT; |
case KSPROTO_HTTP: return HKP_PORT; |
533 |
} |
} |
534 |
return 0; |
return 0; |
535 |
} |
} |
539 |
static int |
static int |
540 |
proto_from_URL (const char *buf) |
proto_from_URL (const char *buf) |
541 |
{ |
{ |
542 |
if (strstr( buf, "ldap")) |
if (strstr (buf, "ldap")) |
543 |
return KSPROTO_LDAP; |
return KSPROTO_LDAP; |
544 |
else if (strstr( buf, "finger")) |
else if (strstr (buf, "finger")) |
545 |
return KSPROTO_FINGER; |
return KSPROTO_FINGER; |
546 |
return KSPROTO_HKP; |
return KSPROTO_HKP; |
547 |
} |
} |
548 |
|
|
549 |
|
|
550 |
|
/* Set the default keyserver. |
551 |
|
If @hostname is NULL, use the predefined keyserver. */ |
552 |
void |
void |
553 |
keyserver_set_default (const char *hostname, WORD port) |
keyserver_set_default (const char *hostname, WORD port) |
554 |
{ |
{ |
555 |
|
if (!port) |
556 |
|
port = HKP_PORT; |
557 |
if (hostname != NULL) { |
if (hostname != NULL) { |
558 |
free_if_alloc (default_keyserver); |
free_if_alloc (default_keyserver); |
559 |
default_keyserver = m_strdup (hostname); |
default_keyserver = m_strdup (hostname); |
|
if (!default_keyserver) |
|
|
BUG (0); |
|
560 |
default_keyserver_port = port; |
default_keyserver_port = port; |
561 |
} |
} |
562 |
if (!port) |
if (!default_keyserver) { |
|
port = HKP_PORT; |
|
|
if (!default_keyserver) |
|
563 |
default_keyserver = m_strdup (DEF_HKP_KEYSERVER); |
default_keyserver = m_strdup (DEF_HKP_KEYSERVER); |
564 |
|
default_keyserver_port = HKP_PORT; |
565 |
|
} |
566 |
server[0].name = m_strdup (default_keyserver); |
server[0].name = m_strdup (default_keyserver); |
567 |
server[0].used = 1; |
server[0].used = 1; |
568 |
server[0].port = port; |
server[0].port = port; |
650 |
/* check if the host has a http:// prefix and skip it */ |
/* check if the host has a http:// prefix and skip it */ |
651 |
if (proxy.host && !strncmp (proxy.host, "http://", 7)) { |
if (proxy.host && !strncmp (proxy.host, "http://", 7)) { |
652 |
char *host = m_strdup (proxy.host+7); |
char *host = m_strdup (proxy.host+7); |
|
if (!host) |
|
|
BUG (0); |
|
653 |
free_if_alloc (proxy.host); |
free_if_alloc (proxy.host); |
654 |
proxy.host = host; |
proxy.host = host; |
655 |
} |
} |
668 |
s = (char *)skip_whitespace (buf); |
s = (char *)skip_whitespace (buf); |
669 |
if (*s == '#' || strlen (s) < 7) |
if (*s == '#' || strlen (s) < 7) |
670 |
continue; |
continue; |
671 |
if (check_URL (s)) { |
chk_pos = check_URL (s); |
672 |
|
if (!chk_pos) { |
673 |
msg_box (NULL, _("All entries of this file must have a valid prefix.\n" |
msg_box (NULL, _("All entries of this file must have a valid prefix.\n" |
674 |
"Currently HKP/HTTP, LDAP and FINGER are supported.\n"), |
"Currently HKP/HTTP, LDAP and FINGER are supported.\n"), |
675 |
_("Keyserver Error"), MB_ERR); |
_("Keyserver Error"), MB_ERR); |
676 |
continue; |
continue; |
677 |
} |
} |
|
chk_pos = 6; |
|
|
if (strstr (s, "finger")) |
|
|
chk_pos = 10; |
|
678 |
p = strchr (s+chk_pos, ':'); |
p = strchr (s+chk_pos, ':'); |
679 |
server[pos].used = 1; |
server[pos].used = 1; |
680 |
server[pos].proto = proto_from_URL (s); |
server[pos].proto = proto_from_URL (s); |
685 |
server[pos].port = atol (p+1); |
server[pos].port = atol (p+1); |
686 |
if (server[pos].port < 0 || server[pos].port > 65536) |
if (server[pos].port < 0 || server[pos].port > 65536) |
687 |
server[pos].port = HKP_PORT; |
server[pos].port = HKP_PORT; |
688 |
server[pos].name = new char [(p-s)+2]; |
server[pos].name = safe_alloc ((p-s)+2); |
|
if (!server[pos].name) |
|
|
BUG (0); |
|
689 |
memset (server[pos].name, 0, (p-s)+2); |
memset (server[pos].name, 0, (p-s)+2); |
690 |
memcpy (server[pos].name, s, (p-s)); |
memcpy (server[pos].name, s, (p-s)); |
691 |
} |
} |
714 |
Return value: 0 on success */ |
Return value: 0 on success */ |
715 |
int |
int |
716 |
kserver_connect (const char *hostname, WORD port, int *conn_fd) |
kserver_connect (const char *hostname, WORD port, int *conn_fd) |
717 |
{ |
{ |
|
int rc, fd; |
|
|
DWORD iaddr; |
|
|
char host[128] = {0}; |
|
718 |
struct hostent *hp; |
struct hostent *hp; |
719 |
struct sockaddr_in sock; |
struct sockaddr_in sock; |
720 |
|
char host[128] = {0}; |
721 |
|
DWORD iaddr; |
722 |
|
int rc, fd; |
723 |
|
|
724 |
log_debug ("kserver_connect: %s:%d\r\n", hostname, port); |
log_debug ("kserver_connect: %s:%d\r\n", hostname, port); |
725 |
|
|
729 |
*conn_fd = 0; |
*conn_fd = 0; |
730 |
hostname = skip_type_prefix (hostname); |
hostname = skip_type_prefix (hostname); |
731 |
|
|
732 |
|
if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) |
733 |
|
port = proxy.port; |
734 |
memset (&sock, 0, sizeof (sock)); |
memset (&sock, 0, sizeof (sock)); |
735 |
sock.sin_family = AF_INET; |
sock.sin_family = AF_INET; |
736 |
sock.sin_port = proxy.host? htons (proxy.port) : htons (port); |
sock.sin_port = htons (port); |
737 |
if (proxy.host) |
if (proxy.host) |
738 |
strncpy (host, proxy.host, 127); |
strncpy (host, proxy.host, 127); |
739 |
else |
else |
740 |
strncpy (host, hostname, 127); |
strncpy (host, hostname, 127); |
741 |
|
|
742 |
if ((iaddr = inet_addr (host)) != INADDR_NONE) |
if ((iaddr = inet_addr (host)) != INADDR_NONE) |
743 |
memcpy (&sock.sin_addr, &iaddr, sizeof (iaddr)); |
memcpy (&sock.sin_addr, &iaddr, sizeof (iaddr)); |
744 |
else if ((hp = gethostbyname (host))) { |
else if ((hp = gethostbyname (host))) { |
777 |
} |
} |
778 |
|
|
779 |
|
|
780 |
|
/* Check if the URL needs to be encoded or not. */ |
781 |
static bool |
static bool |
782 |
URL_must_encoded (const char *url) |
URL_must_encoded (const char *url) |
783 |
{ |
{ |
789 |
|
|
790 |
/* Perform URL encoding of the given data. */ |
/* Perform URL encoding of the given data. */ |
791 |
static char* |
static char* |
792 |
URL_encode (const char *url, size_t ulen, size_t *ret_nlen) |
URL_encode (const char *url, DWORD ulen, DWORD *ret_nlen) |
793 |
{ |
{ |
794 |
gpgme_data_t hd; |
gpgme_data_t hd; |
795 |
char numbuf[5], *pp, *p; |
char numbuf[5], *p; |
796 |
size_t i, n; |
size_t i; |
797 |
|
int nlen; |
798 |
|
|
799 |
gpgme_data_new (&hd); |
if (gpgme_data_new (&hd)) |
800 |
|
BUG (0); |
801 |
for (i=0; i < ulen; i++) { |
for (i=0; i < ulen; i++) { |
802 |
if (isalnum (url[i]) || url[i] == '-') |
if (isalnum (url[i]) || url[i] == '-') |
803 |
gpg_data_putc (hd, url[i]); |
gpg_data_putc (hd, url[i]); |
808 |
gpgme_data_write (hd, numbuf, strlen (numbuf)); |
gpgme_data_write (hd, numbuf, strlen (numbuf)); |
809 |
} |
} |
810 |
} |
} |
|
gpg_data_putc (hd, '\0'); |
|
811 |
|
|
812 |
/* 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); |
|
813 |
if (ret_nlen) |
if (ret_nlen) |
814 |
*ret_nlen = n; |
*ret_nlen = nlen; |
815 |
return p; |
return p; |
816 |
} |
} |
817 |
|
|
819 |
/* Format a request for the given keyid (send). */ |
/* Format a request for the given keyid (send). */ |
820 |
static char* |
static char* |
821 |
kserver_send_request (const char *hostname, WORD port, |
kserver_send_request (const char *hostname, WORD port, |
822 |
const char *pubkey, int octets) |
const char *pubkey, DWORD octets) |
823 |
{ |
{ |
824 |
char *request = NULL; |
const char *fmt; |
825 |
char *enc_pubkey = NULL; |
char *request; |
826 |
size_t enc_octets; |
char *enc_pubkey; |
827 |
|
DWORD enc_octets; |
828 |
int reqlen; |
int reqlen; |
829 |
|
|
830 |
log_debug ("kserver_send_request: %s:%d\r\n", hostname, port); |
log_debug ("kserver_send_request: %s:%d\r\n", hostname, port); |
831 |
|
|
832 |
if (!port) |
if (!port) |
833 |
port = HKP_PORT; |
port = HKP_PORT; |
|
reqlen = 512 + strlen (hostname) + 2*strlen (pubkey); |
|
|
request = new char[reqlen]; |
|
|
if (!request) |
|
|
BUG (0); |
|
834 |
enc_pubkey = URL_encode (pubkey, octets, &enc_octets); |
enc_pubkey = URL_encode (pubkey, octets, &enc_octets); |
835 |
if (!enc_pubkey || !enc_octets) { |
reqlen = 2*strlen (hostname) + 2*32/*port*/ + enc_octets + 32 /*len*/ + 1; |
|
free_if_alloc (request); |
|
|
return NULL; |
|
|
} |
|
836 |
|
|
837 |
if (proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
if (proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
838 |
_snprintf (request, reqlen-1, |
fmt = "POST http://%s:%d/pks/add HTTP/1.0\r\n" |
839 |
"POST http://%s:%d/pks/add HTTP/1.0\r\n" |
"Referer: \r\n" |
840 |
"Referer: \r\n" |
"User-Agent: WinPT/W32\r\n" |
841 |
"User-Agent: WinPT/W32\r\n" |
"Host: %s:%d\r\n" |
842 |
"Host: %s:%d\r\n" |
"Proxy-Authorization: Basic %s\r\n" |
843 |
"Proxy-Authorization: Basic %s\r\n" |
"Content-type: application/x-www-form-urlencoded\r\n" |
844 |
"Content-type: application/x-www-form-urlencoded\r\n" |
"Content-length: %d\r\n" |
845 |
"Content-length: %d\r\n" |
"\r\n" |
846 |
"\r\n" |
"keytext=%s" |
847 |
"keytext=%s" |
"\r\n"; |
848 |
"\n", |
reqlen += strlen (fmt) + strlen (proxy.base64_user) + 1; |
849 |
skip_type_prefix (hostname), port, hostname, port, |
request = safe_alloc (reqlen+1); |
850 |
|
_snprintf (request, reqlen, fmt, |
851 |
|
skip_type_prefix (hostname), port, hostname, port, |
852 |
proxy.base64_user, enc_octets+9, enc_pubkey); |
proxy.base64_user, enc_octets+9, enc_pubkey); |
853 |
} |
} |
854 |
else { |
else { |
855 |
_snprintf (request, reqlen-1, |
fmt = "POST /pks/add HTTP/1.0\r\n" |
856 |
"POST /pks/add HTTP/1.0\r\n" |
"Referer: \r\n" |
857 |
"Referer: \r\n" |
"User-Agent: WinPT/W32\r\n" |
858 |
"User-Agent: WinPT/W32\r\n" |
"Host: %s:%d\r\n" |
859 |
"Host: %s:%d\r\n" |
"Content-type: application/x-www-form-urlencoded\r\n" |
860 |
"Content-type: application/x-www-form-urlencoded\r\n" |
"Content-length: %d\r\n" |
861 |
"Content-length: %d\r\n" |
"\r\n" |
862 |
"\r\n" |
"keytext=%s" |
863 |
"keytext=%s" |
"\r\n"; |
864 |
"\n", |
reqlen += strlen (fmt)+1; |
865 |
skip_type_prefix (hostname), port, |
request = safe_alloc (reqlen+1); |
866 |
|
_snprintf (request, reqlen, fmt, |
867 |
|
skip_type_prefix (hostname), port, |
868 |
enc_octets+9, enc_pubkey); |
enc_octets+9, enc_pubkey); |
869 |
} |
} |
|
free_if_alloc (enc_pubkey); |
|
870 |
|
|
871 |
log_debug ("%s\r\n", request); |
free_if_alloc (enc_pubkey); |
872 |
|
log_debug ("request:\r\n%s\r\n", request); |
873 |
return request; |
return request; |
874 |
} |
} |
875 |
|
|
876 |
|
|
877 |
|
/* Interface for receiving a public key. */ |
878 |
int |
int |
879 |
kserver_recvkey_ext (const char *hostname, WORD port, const char *keyid, |
kserver_recvkey (const char *hostname, WORD port, const char *keyid, |
880 |
char **r_key, int *r_keylen) |
char **r_key, int *r_keylen) |
881 |
{ |
{ |
882 |
|
const char *fmt; |
883 |
char *request = NULL; |
char *request = NULL; |
884 |
int conn_fd; |
int conn_fd; |
885 |
int rc, n; |
int rc, reqlen; |
886 |
|
|
887 |
if (!port) |
if (!port) |
888 |
port = HKP_PORT; |
port = HKP_PORT; |
889 |
hkp_err = 0; /* reset */ |
hkp_err = 0; /* reset */ |
890 |
rc = kserver_connect (hostname, port, &conn_fd); |
rc = kserver_connect (hostname, port, &conn_fd); |
891 |
if (rc) |
if (rc) |
892 |
goto leave; |
goto leave; |
893 |
|
|
894 |
request = new char[300+1]; |
reqlen = strlen (hostname)+32+strlen (keyid)+1; |
|
if (!request) |
|
|
BUG (0); |
|
|
|
|
895 |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
896 |
_snprintf (request, 300, |
fmt = "GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n" |
897 |
"GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n" |
"Proxy-Authorization: Basic %s\r\n\r\n"; |
898 |
"Proxy-Authorization: Basic %s\r\n\r\n", |
reqlen =+ strlen (fmt) + strlen (proxy.base64_user)+1; |
899 |
skip_type_prefix (hostname), port, keyid, proxy.base64_user); |
request = safe_alloc (reqlen+1); |
900 |
|
_snprintf (request, reqlen, fmt, skip_type_prefix (hostname), port, |
901 |
|
keyid, proxy.base64_user); |
902 |
} |
} |
903 |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
904 |
_snprintf (request, 300, |
fmt = "GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n"; |
905 |
"GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n", |
reqlen += strlen (fmt)+1; |
906 |
skip_type_prefix (hostname), port, keyid); |
request = safe_alloc (reqlen+1); |
907 |
} |
_snprintf (request, reqlen, fmt, skip_type_prefix (hostname), |
908 |
|
port, keyid); |
909 |
|
} |
910 |
else { |
else { |
911 |
_snprintf (request, 300, |
fmt = "GET /pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n"; |
912 |
"GET /pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n", keyid); |
reqlen += strlen (fmt)+1; |
913 |
|
request = safe_alloc (reqlen+1); |
914 |
|
_snprintf (request, reqlen, fmt, keyid); |
915 |
} |
} |
916 |
|
|
917 |
log_debug ("%s\r\n", request); |
log_debug ("request:\r\n%s\r\n", request); |
918 |
|
|
919 |
rc = sock_write (conn_fd, request, strlen (request)); |
rc = sock_write (conn_fd, request, strlen (request)); |
920 |
if (rc == SOCKET_ERROR) { |
if (rc == SOCKET_ERROR) { |
922 |
goto leave; |
goto leave; |
923 |
} |
} |
924 |
|
|
925 |
rc = sock_read_ext (conn_fd, r_key, &n); |
rc = sock_read_ext (conn_fd, r_key, r_keylen); |
926 |
if (rc == SOCKET_ERROR) { |
if (rc == SOCKET_ERROR) { |
927 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
928 |
goto leave; |
goto leave; |
929 |
} |
} |
930 |
|
|
931 |
if (r_keylen) |
log_debug ("response:\r\n%s\r\n", *r_key); |
|
*r_keylen = n; |
|
|
log_debug ("%s\r\n", *r_key); |
|
932 |
rc = check_hkp_response (*r_key, 1); |
rc = check_hkp_response (*r_key, 1); |
933 |
if (rc) |
if (rc) |
934 |
goto leave; |
goto leave; |
941 |
return rc; |
return rc; |
942 |
} |
} |
943 |
|
|
|
/* 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; |
|
|
} |
|
|
|
|
944 |
|
|
945 |
/* Interface to send a public key. */ |
/* Interface to send a public key. */ |
946 |
int |
int |
947 |
kserver_sendkey (const char *hostname, WORD port, const char *pubkey, int len ) |
kserver_sendkey (const char *hostname, WORD port, const char *pubkey, int len ) |
948 |
{ |
{ |
949 |
char *request = NULL; |
char *request = NULL; |
950 |
char log[2048]; |
char log[2048] = {0}; |
951 |
int conn_fd, n; |
int conn_fd, n; |
952 |
int rc; |
int rc; |
953 |
|
|
974 |
goto leave; |
goto leave; |
975 |
} |
} |
976 |
|
|
977 |
log_debug ("kserver_sendkey:\r\n%s\r\n", log); |
log_debug ("kserver_sendkey: read %d bytes\r\n%s\r\n", n, log); |
978 |
rc = check_hkp_response (log, 0); |
rc = check_hkp_response (log, 0); |
979 |
if (rc) |
if (rc) |
980 |
goto leave; |
goto leave; |
996 |
int n=0; |
int n=0; |
997 |
|
|
998 |
/* parse response 'HTTP/1.0 500 OK' */ |
/* parse response 'HTTP/1.0 500 OK' */ |
999 |
if (sock_getline (fd, buf, 127, &n)) |
if (sock_getline (fd, buf, sizeof (buf)-1, &n)) |
1000 |
return WPTERR_KEYSERVER_NOTFOUND; |
return WPTERR_KEYSERVER_NOTFOUND; |
1001 |
|
|
1002 |
log_debug ("kserver_search_chkpresp: %s\r\n", buf); |
log_debug ("kserver_search_chkpresp: %s\r\n", buf); |
1017 |
} |
} |
1018 |
|
|
1019 |
|
|
1020 |
/* Begin keyserver search procedure. */ |
/* Extract the amount of keys from the info record. */ |
1021 |
|
static int |
1022 |
|
count_keys_in_response (char *buf) |
1023 |
|
{ |
1024 |
|
char *p; |
1025 |
|
int recno = 0; |
1026 |
|
int n = 0; |
1027 |
|
|
1028 |
|
/* info:1:4 */ |
1029 |
|
if (strncmp (buf, "info", 4)) |
1030 |
|
return 0; |
1031 |
|
p = strtok (buf, ":"); |
1032 |
|
while (p != NULL) { |
1033 |
|
recno++; |
1034 |
|
if (recno == 3) |
1035 |
|
n = atoi (p); |
1036 |
|
p = strtok (NULL, ":"); |
1037 |
|
} |
1038 |
|
return n; |
1039 |
|
} |
1040 |
|
|
1041 |
|
|
1042 |
|
/* Start the keyserver search. |
1043 |
|
Connect to host @hostname and port @port. |
1044 |
|
The pattern are given in @pattern. |
1045 |
|
The socket is returned in @conn_fd and @nkeys contains |
1046 |
|
the amount of keys which were found. */ |
1047 |
int |
int |
1048 |
kserver_search_begin (const char *hostname, WORD port, |
kserver_search_begin (const char *hostname, WORD port, |
1049 |
const char *pattern, int *conn_fd) |
const char *pattern, int *conn_fd, int *nkeys) |
1050 |
{ |
{ |
1051 |
|
const char *fmt; |
1052 |
char *request = NULL; |
char *request = NULL; |
1053 |
char *enc_patt = NULL; |
char *enc_patt = NULL; |
1054 |
int n; |
char status[128]; |
1055 |
int rc, sock_fd; |
int rc, sock_fd; |
1056 |
|
int reqlen; |
1057 |
|
|
1058 |
|
*conn_fd = 0; |
1059 |
|
|
1060 |
rc = kserver_connect (hostname, port, &sock_fd); |
rc = kserver_connect (hostname, port, &sock_fd); |
1061 |
if (rc) { |
if (rc) |
|
*conn_fd = 0; |
|
1062 |
goto leave; |
goto leave; |
|
} |
|
1063 |
|
|
1064 |
enc_patt = URL_encode (pattern, strlen (pattern), NULL); |
enc_patt = URL_encode (pattern, strlen (pattern), NULL); |
1065 |
n = 140 + strlen (enc_patt) + strlen (hostname) + 32 + 2; |
reqlen = strlen (enc_patt) + strlen (hostname) + 32 + 1; |
1066 |
if (proxy.base64_user) |
|
|
n += strlen (proxy.base64_user) + 1; |
|
|
request = new char[n+1]; |
|
|
if (!request) |
|
|
BUG (0); |
|
|
|
|
1067 |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
1068 |
_snprintf (request, n, |
fmt = "GET http://%s:%d/pks/lookup?options=mr&op=index&search=%s HTTP/1.0\r\n" |
1069 |
"GET http://%s:%d/pks/lookup?op=index&search=%s HTTP/1.0\r\n" |
"Proxy-Authorization: Basic %s\r\n\r\n"; |
1070 |
"Proxy-Authorization: Basic %s\r\n\r\n", |
reqlen += strlen (proxy.base64_user) + strlen (fmt) + 1; |
1071 |
skip_type_prefix (hostname), port, enc_patt, proxy.base64_user); |
request = safe_alloc (reqlen+1); |
1072 |
|
_snprintf (request, reqlen, fmt, skip_type_prefix (hostname), port, |
1073 |
|
enc_patt, proxy.base64_user); |
1074 |
} |
} |
1075 |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
1076 |
_snprintf (request, n, |
fmt = "GET http://%s:%d/pks/lookup?options=mr&op=index&search=%s HTTP/1.0\r\n\r\n"; |
1077 |
"GET http://%s:%d/pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n", |
reqlen += strlen (fmt)+1; |
1078 |
skip_type_prefix (hostname), port, enc_patt); |
request = safe_alloc (reqlen+1); |
1079 |
|
_snprintf (request, reqlen, skip_type_prefix (hostname), port, |
1080 |
|
enc_patt); |
1081 |
} |
} |
1082 |
else { |
else { |
1083 |
_snprintf (request, n, |
fmt = "GET /pks/lookup?options=mr&op=index&search=%s HTTP/1.0\r\n\r\n"; |
1084 |
"GET /pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n", |
reqlen += strlen (fmt)+1; |
1085 |
enc_patt); |
request = safe_alloc (reqlen+1); |
1086 |
|
_snprintf (request, reqlen, fmt, enc_patt); |
1087 |
} |
} |
1088 |
|
|
1089 |
log_debug ("kserver_search_begin:\r\n%s\r\n", request); |
log_debug ("kserver_search_begin:\r\n%s\r\n", request); |
|
|
|
1090 |
if (sock_write (sock_fd, request, strlen (request)) == SOCKET_ERROR) { |
if (sock_write (sock_fd, request, strlen (request)) == SOCKET_ERROR) { |
1091 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1092 |
goto leave; |
goto leave; |
1096 |
if (rc) { |
if (rc) { |
1097 |
closesocket (sock_fd); |
closesocket (sock_fd); |
1098 |
sock_fd = 0; |
sock_fd = 0; |
1099 |
|
goto leave; |
1100 |
|
} |
1101 |
|
|
1102 |
|
/* Skip all lines until we reach the "info:" record. */ |
1103 |
|
for (;;) { |
1104 |
|
if (sock_getline (sock_fd, status, sizeof (status)-1, &reqlen)) { |
1105 |
|
log_debug ("kserver_search_begin: retrieving status line failed.\r\n"); |
1106 |
|
closesocket (sock_fd); |
1107 |
|
sock_fd = 0; |
1108 |
|
rc = WPTERR_GENERAL; |
1109 |
|
break; |
1110 |
|
} |
1111 |
|
if (!strncmp (status, "info:", 5)) |
1112 |
|
break; |
1113 |
} |
} |
1114 |
|
|
1115 |
|
if (!rc) |
1116 |
|
*nkeys = count_keys_in_response (status); |
1117 |
*conn_fd = sock_fd; |
*conn_fd = sock_fd; |
1118 |
|
|
1119 |
leave: |
leave: |
1123 |
} |
} |
1124 |
|
|
1125 |
|
|
1126 |
|
/* Parse a single pub record returned by the keyserver. */ |
1127 |
|
static void |
1128 |
|
parse_pub_record (keyserver_key_s *key, char *buf) |
1129 |
|
{ |
1130 |
|
enum pub_rec_t {ID=1, KEYID, ALGO, SIZE, CREATE, EXPIRE}; |
1131 |
|
char *p; |
1132 |
|
int recno = 0; |
1133 |
|
|
1134 |
|
/* pub:BF3DF9B4:17:1024:925411133:: */ |
1135 |
|
p = strtok (buf, ":"); |
1136 |
|
while (p != NULL) { |
1137 |
|
recno++; |
1138 |
|
switch (recno) { |
1139 |
|
case ID: |
1140 |
|
break; |
1141 |
|
|
1142 |
/* Convert an iso date @iso_date (YYYY-MM-DD) into the locale |
case KEYID: |
1143 |
representation and store it into @loc_date. |
free_if_alloc (key->keyid); |
1144 |
Return value: 0 on success. */ |
key->keyid = m_strdup (p); |
1145 |
static int |
break; |
1146 |
parse_iso_date (const char *iso_date, char *loc_date, size_t loclen) |
|
1147 |
|
case ALGO: |
1148 |
|
key->algo = atoi (p); |
1149 |
|
break; |
1150 |
|
|
1151 |
|
case SIZE: |
1152 |
|
key->bits = atoi (p); |
1153 |
|
break; |
1154 |
|
|
1155 |
|
case CREATE: |
1156 |
|
key->creation = strtoul (p, NULL, 10); |
1157 |
|
break; |
1158 |
|
|
1159 |
|
case EXPIRE: |
1160 |
|
key->expires = strtoul (p, NULL, 10); |
1161 |
|
break; |
1162 |
|
} |
1163 |
|
p = strtok (NULL, ":"); |
1164 |
|
} |
1165 |
|
|
1166 |
|
} |
1167 |
|
|
1168 |
|
|
1169 |
|
/* Parse a single user id returned by the keyserver. */ |
1170 |
|
static void |
1171 |
|
parse_uid_record (keyserver_key_s *key, char *buf) |
1172 |
{ |
{ |
1173 |
SYSTEMTIME st; |
enum uid_rec_t {ID=1, UID, CREATE, EXPIRE}; |
1174 |
char buf[16] = {0}, *p; |
keyserver_uid_s *u, *n; |
1175 |
int pos=0; |
char *p, *raw; |
1176 |
|
int recno = 0; |
1177 |
|
|
1178 |
|
/* uid:Timo Schulz <[email protected]>:1138440360:: */ |
1179 |
|
u = new keyserver_uid_s; |
1180 |
|
memset (u, 0, sizeof *u); |
1181 |
|
|
1182 |
strncpy (buf, iso_date, sizeof (buf)-1); |
p = strtok (buf, ":"); |
|
p = strtok (buf, "-"); |
|
1183 |
while (p != NULL) { |
while (p != NULL) { |
1184 |
switch (pos) { |
recno++; |
1185 |
case 0: st.wYear = (WORD)atoi (p); pos++; break; |
|
1186 |
case 1: st.wMonth = (WORD)atoi (p); pos++; break; |
switch (recno) { |
1187 |
case 2: st.wDay = (WORD)atoi (p); pos++; break; |
case ID: |
1188 |
default: break; |
break; |
1189 |
|
|
1190 |
|
case UID: |
1191 |
|
unhexify_buffer (p, &raw); |
1192 |
|
u->uid = utf8_to_native (raw); |
1193 |
|
free_if_alloc (raw); |
1194 |
|
break; |
1195 |
|
|
1196 |
|
case CREATE: |
1197 |
|
u->creation = strtoul (p, NULL, 10); |
1198 |
|
break; |
1199 |
|
|
1200 |
|
case EXPIRE: |
1201 |
|
u->expires = strtoul (p, NULL, 10); |
1202 |
|
break; |
1203 |
} |
} |
1204 |
p = strtok (NULL, "-"); |
|
1205 |
|
p = strtok (NULL, ":"); |
1206 |
|
} |
1207 |
|
if (!key->uids) |
1208 |
|
key->uids = u; |
1209 |
|
else { |
1210 |
|
for (n = key->uids; n->next; n=n->next) |
1211 |
|
; |
1212 |
|
n->next = u; |
1213 |
} |
} |
|
if (pos != 3) |
|
|
return -1; |
|
|
|
|
|
if (!GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, |
|
|
NULL, loc_date, loclen)) |
|
|
return -1; |
|
|
return 0; |
|
1214 |
} |
} |
1215 |
|
|
1216 |
|
|
1217 |
|
/* Peek the next 3 bytes to check if the next line |
1218 |
|
would be a new "pub" record. In this case, return |
1219 |
|
-1 as an EOF indication, 0 otherwise. */ |
1220 |
|
static int |
1221 |
|
is_key_eof (int fd) |
1222 |
|
{ |
1223 |
|
char buf[64]; |
1224 |
|
int n; |
1225 |
|
|
1226 |
|
n = recv (fd, buf, 3, MSG_PEEK); |
1227 |
|
if (n < 3 || strncmp (buf, "pub", 3)) |
1228 |
|
return 0; |
1229 |
|
return -1; |
1230 |
|
} |
1231 |
|
|
1232 |
|
|
1233 |
|
/* Return the next key from the search response. */ |
1234 |
int |
int |
1235 |
kserver_search_next (int fd, keyserver_key *key) |
kserver_search_next (int fd, keyserver_key_s **r_key) |
1236 |
{ |
{ |
1237 |
char buf[1024], *p; |
keyserver_uid_s *uid, *u = NULL; |
1238 |
int uidlen, nbytes, pos = 0; |
keyserver_key_s *key; |
1239 |
|
char buf[512]; |
1240 |
|
int n = 0; |
1241 |
|
long max = 0; |
1242 |
|
|
1243 |
log_debug ("keyserver_search_next:\r\n"); |
log_debug ("keyserver_search_next:\r\n"); |
1244 |
|
*r_key = NULL; |
|
if (sock_getline (fd, buf, sizeof (buf) - 1, &nbytes)) |
|
|
return WPTERR_GENERAL; |
|
1245 |
|
|
1246 |
/* XXX: use maschine readable option. */ |
key = new keyserver_key_s; |
1247 |
log_debug ("%s\r\n", buf); |
memset (key, 0, sizeof *key); |
1248 |
|
for (;;) { |
1249 |
if (!strncmp (buf, "pub", 3)) { |
if (sock_getline (fd, buf, sizeof (buf)-1, &n)) |
1250 |
int revoked = strstr (buf, "KEY REVOKED") != NULL? 1 : 0; |
break; |
1251 |
key->bits = atol (buf+3); |
/*log_debug ("record: '%s'\r\n", buf); */ |
1252 |
p = strchr (buf, '>'); |
if (!strncmp (buf, "pub", 3)) |
1253 |
if (!p) |
parse_pub_record (key, buf); |
1254 |
goto fail; |
else |
1255 |
pos = p - buf + 1; |
parse_uid_record (key, buf); |
1256 |
memcpy (key->keyid, buf + pos, 8); |
if (is_key_eof (fd)) |
1257 |
key->keyid[8] = '\0'; |
break; |
1258 |
p = strstr (buf, "</a>"); |
} |
|
if (!p) |
|
|
goto fail; |
|
|
pos = p - buf + 5; |
|
|
memcpy (key->date, buf + pos, 10); |
|
|
key->date[10] = '\0'; |
|
|
parse_iso_date (key->date, key->date, sizeof (key->date)-1); |
|
|
if (revoked) { |
|
|
strcpy (key->uid, "KEY REVOKED: not checked"); |
|
|
return 0; |
|
|
} |
|
|
pos += 10; |
|
|
p = buf + pos + 1; |
|
|
while (p && *p != '>') |
|
|
p++; |
|
|
p++; |
|
|
uidlen = strlen (p) - 10; |
|
|
if (!strstr (p, "<") && !strstr (p, ">")) { |
|
|
pos=0; |
|
|
while (p && *p && pos < sizeof (key->uid)-1) { |
|
|
if (*p == '<') |
|
|
break; |
|
|
key->uid[pos++] = *p++; |
|
|
} |
|
|
key->uid[pos] ='\0'; |
|
|
return 0; |
|
|
} |
|
1259 |
|
|
1260 |
if (uidlen > sizeof (key->uid)-1) |
/* the uid with the newest self sig is used as the |
1261 |
uidlen = sizeof (key->uid)-1; |
primary user id. */ |
1262 |
memcpy (key->uid, p, uidlen); |
for (uid = key->uids; uid; uid = uid->next) { |
1263 |
key->uid[uidlen] = '\0'; |
if (uid->creation > max) { |
1264 |
strcat (key->uid, ">"); |
max = uid->creation; |
1265 |
p = strchr (key->uid, '&'); |
u = uid; |
|
if (p) { |
|
|
key->uid[(p-key->uid)] = '<'; |
|
|
memmove (key->uid+(p-key->uid)+1, key->uid+(p-key->uid)+4, |
|
|
strlen (key->uid)-3); |
|
1266 |
} |
} |
|
return 0; |
|
1267 |
} |
} |
1268 |
|
|
1269 |
fail: |
key->main_uid = u? u : key->uids; |
1270 |
key->bits = 0; |
*r_key = key; |
|
memset (key, 0, sizeof *key); |
|
1271 |
return 0; |
return 0; |
1272 |
} |
} |
1273 |
|
|
1274 |
|
|
1275 |
|
/* Release the keyserver key @key and all its elements. */ |
1276 |
|
void |
1277 |
|
kserver_release_key (keyserver_key_s *key) |
1278 |
|
{ |
1279 |
|
keyserver_uid_s *u; |
1280 |
|
|
1281 |
|
while (key->uids) { |
1282 |
|
u = key->uids->next; |
1283 |
|
free_if_alloc (key->uids->uid); |
1284 |
|
free_if_alloc (key->uids); |
1285 |
|
key->uids = u; |
1286 |
|
} |
1287 |
|
free_if_alloc (key->keyid); |
1288 |
|
free_if_alloc (key); |
1289 |
|
} |
1290 |
|
|
1291 |
|
|
1292 |
/* Release mbmers in the proxy context @ctx. */ |
/* Release mbmers in the proxy context @ctx. */ |
1293 |
void |
void |
1294 |
kserver_proxy_release (keyserver_proxy_t ctx) |
kserver_proxy_release (keyserver_proxy_t ctx) |
1327 |
} |
} |
1328 |
|
|
1329 |
|
|
1330 |
/* Receive an key via LDAP from host @host with the keyid @keyid. |
static FILE* |
1331 |
@key contains the key on success. */ |
do_spawn_ldap_helper (const char *host, const char *keyid) |
1332 |
int |
{ |
1333 |
ldap_recvkey (const char *host, const char *keyid, char *key, int maxkeylen) |
FILE *fp = NULL; |
1334 |
{ |
char *p, *sep; |
1335 |
FILE *fp; |
char *ksprg; |
1336 |
const char *s; |
char outf[256], inf[256]; |
1337 |
char *ksprg = NULL, *p = NULL, *sep; |
size_t n; |
|
char inf[256], outf[256], buf[512]; |
|
|
DWORD n; |
|
|
int start_key = 0, failed = 0; |
|
|
int rc = 0; |
|
1338 |
|
|
1339 |
p = get_gnupg_prog (); |
p = get_gnupg_prog (); |
1340 |
n = strlen (p) + 1 + 128; |
n = strlen (p) + 1 + 128; |
1341 |
ksprg = new char[n+1]; |
ksprg = safe_alloc (n+1); |
1342 |
if (!ksprg) |
if (!ksprg) |
1343 |
BUG (0); |
BUG (0); |
1344 |
sep = strrchr (p, '\\'); |
sep = strrchr (p, '\\'); |
1350 |
if (file_exist_check (ksprg)) { |
if (file_exist_check (ksprg)) { |
1351 |
log_box ("LDAP Keyserver Plugin", MB_ERR, |
log_box ("LDAP Keyserver Plugin", MB_ERR, |
1352 |
"%s: could not find LDAP keyserver module!", ksprg); |
"%s: could not find LDAP keyserver module!", ksprg); |
1353 |
rc = -1; |
fp = NULL; |
1354 |
goto leave; |
goto leave; |
1355 |
} |
} |
1356 |
get_temp_name (outf, sizeof (outf)-1, keyid); |
get_temp_name (outf, sizeof (outf)-1, keyid); |
1359 |
if (!fp) { |
if (!fp) { |
1360 |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", inf, |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", inf, |
1361 |
winpt_strerror (WPTERR_FILE_OPEN)); |
winpt_strerror (WPTERR_FILE_OPEN)); |
|
rc = -1; |
|
1362 |
goto leave; |
goto leave; |
1363 |
} |
} |
1364 |
fprintf (fp, |
fprintf (fp, |
1372 |
host? skip_type_prefix (host): "64.94.85.200", keyid); |
host? skip_type_prefix (host): "64.94.85.200", keyid); |
1373 |
fclose (fp); |
fclose (fp); |
1374 |
|
|
1375 |
p = new char[strlen (ksprg) + strlen (inf) + strlen (outf) + 32]; |
p = safe_alloc (strlen (ksprg) + strlen (inf) + strlen (outf) + 32); |
|
if (!p) |
|
|
BUG (NULL); |
|
1376 |
sprintf (p, "%s -o %s %s", ksprg, outf, inf); |
sprintf (p, "%s -o %s %s", ksprg, outf, inf); |
1377 |
if (spawn_application (p)) { |
if (spawn_application (p)) { |
1378 |
rc = -1; |
fp = NULL; |
1379 |
goto leave; |
goto leave; |
1380 |
} |
} |
|
|
|
1381 |
fp = fopen (outf, "rb"); |
fp = fopen (outf, "rb"); |
1382 |
if (!fp) { |
if (!fp) |
1383 |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf, |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf, |
1384 |
winpt_strerror (WPTERR_FILE_OPEN)); |
winpt_strerror (WPTERR_FILE_OPEN)); |
1385 |
rc = -1; |
|
1386 |
goto leave; |
leave: |
1387 |
} |
DeleteFile (inf); |
1388 |
memset (key, 0, maxkeylen); |
DeleteFile (outf); |
1389 |
|
free_if_alloc (p); |
1390 |
|
free_if_alloc (ksprg); |
1391 |
|
return fp; |
1392 |
|
} |
1393 |
|
|
1394 |
|
/* Receive an key via LDAP from host @host with the keyid @keyid. |
1395 |
|
@key contains the key on success. */ |
1396 |
|
int |
1397 |
|
ldap_recvkey (const char *host, const char *keyid, |
1398 |
|
char **r_key, int *r_keylen) |
1399 |
|
{ |
1400 |
|
gpgme_data_t raw; |
1401 |
|
FILE *fp; |
1402 |
|
const char *s; |
1403 |
|
char buf[512]; |
1404 |
|
int start_key = 0, failed = 0; |
1405 |
|
int rc = 0; |
1406 |
|
|
1407 |
|
fp = do_spawn_ldap_helper (host, keyid); |
1408 |
|
if (!fp) |
1409 |
|
return WPTERR_GENERAL; |
1410 |
|
|
1411 |
|
if (gpgme_data_new (&raw)) |
1412 |
|
BUG (0); |
1413 |
while (!feof (fp)) { |
while (!feof (fp)) { |
1414 |
s = fgets (buf, sizeof (buf)-1, fp); |
s = fgets (buf, sizeof (buf)-1, fp); |
1415 |
if (!s) |
if (!s) |
1421 |
if (!start_key && strstr (s, "KEY") && strstr (s, "BEGIN")) { |
if (!start_key && strstr (s, "KEY") && strstr (s, "BEGIN")) { |
1422 |
start_key = 1; |
start_key = 1; |
1423 |
continue; |
continue; |
1424 |
} |
} |
1425 |
if (!start_key) |
if (!start_key) |
1426 |
continue; |
continue; |
1427 |
strcat (key, buf); |
gpgme_data_write (raw, buf, strlen (buf)); |
1428 |
maxkeylen -= strlen (buf); |
if (strstr (s, "KEY") && strstr (s, "END")) |
|
if (maxkeylen < 0 || (strstr (s, "KEY") && strstr (s, "END"))) |
|
1429 |
break; |
break; |
1430 |
} |
} |
1431 |
fclose (fp); |
fclose (fp); |
1432 |
|
|
1433 |
leave: |
if (failed) |
|
if (failed || !strlen (key)) |
|
1434 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
1435 |
DeleteFile (inf); |
*r_key = data_release_and_get_mem (raw, r_keylen); |
|
DeleteFile (outf); |
|
|
free_if_alloc (p); |
|
|
free_if_alloc (ksprg); |
|
1436 |
return rc; |
return rc; |
1437 |
} |
} |
1438 |
|
|
1440 |
/* Receive an key via FINGER from host @host with the user @user. |
/* Receive an key via FINGER from host @host with the user @user. |
1441 |
On success @key contains the key. */ |
On success @key contains the key. */ |
1442 |
int |
int |
1443 |
finger_recvkey (const char *host, const char *user, char *key, int maxkeylen) |
finger_recvkey (const char *host, const char *user, |
1444 |
|
char **r_key, int *r_keylen) |
1445 |
{ |
{ |
1446 |
char buf[128]; |
gpgme_data_t raw; |
1447 |
|
char buf[256]; |
1448 |
int fd, nread; |
int fd, nread; |
1449 |
int start_key = 0; |
int start_key = 0; |
1450 |
int rc=0; |
int rc=0; |
1456 |
sock_write (fd, user, strlen (user)); |
sock_write (fd, user, strlen (user)); |
1457 |
sock_write (fd, "\r\n", 2); |
sock_write (fd, "\r\n", 2); |
1458 |
|
|
1459 |
memset (key, 0, maxkeylen); |
if (gpgme_data_new (&raw)) |
1460 |
while (maxkeylen > 0) { |
BUG (0); |
1461 |
|
|
1462 |
|
for (;;) { |
1463 |
if (sock_getline (fd, buf, sizeof (buf), &nread)) |
if (sock_getline (fd, buf, sizeof (buf), &nread)) |
1464 |
break; |
break; |
1465 |
strcat (buf, "\n"); |
strcat (buf, "\n"); |
1466 |
if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) { |
if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) { |
1467 |
strcat (key, buf); |
gpgme_data_write (raw, buf, nread); |
1468 |
start_key = 1; |
start_key = 1; |
|
maxkeylen -= nread; |
|
1469 |
} |
} |
1470 |
else if (strstr (buf, "END PGP PUBLIC KEY BLOCK" ) && start_key) { |
else if (strstr (buf, "END PGP PUBLIC KEY BLOCK" ) && start_key) { |
1471 |
strcat (key, buf); |
gpgme_data_write (raw, buf, nread); |
1472 |
start_key--; |
start_key--; |
|
maxkeylen -= nread; |
|
1473 |
break; |
break; |
1474 |
} |
} |
1475 |
else if (start_key) { |
else if (start_key) |
1476 |
strcat (key, buf); |
gpgme_data_write (raw, buf, nread); |
|
maxkeylen -= nread; |
|
|
} |
|
1477 |
} |
} |
1478 |
|
|
1479 |
closesocket (fd); |
closesocket (fd); |
1480 |
if (start_key != 0) |
if (start_key != 0) |
1481 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
1482 |
|
*r_key = data_release_and_get_mem (raw, r_keylen); |
1483 |
return rc; |
return rc; |
1484 |
} |
} |
1485 |
|
|
1499 |
} |
} |
1500 |
return 0; |
return 0; |
1501 |
} |
} |
1502 |
|
|
1503 |
|
|
1504 |
|
/* Split the URL @r_keyserver into the host and the port |
1505 |
|
part if possible. */ |
1506 |
|
gpgme_error_t |
1507 |
|
parse_keyserver_url (char **r_keyserver, unsigned short *r_port) |
1508 |
|
{ |
1509 |
|
char *p; |
1510 |
|
char *url = *r_keyserver; |
1511 |
|
int off = 0; |
1512 |
|
|
1513 |
|
/* no port is given so use the default port. */ |
1514 |
|
p = strrchr (url, ':'); |
1515 |
|
if (p == strchr (url, ':')) { |
1516 |
|
int port = port_from_proto (proto_from_URL (url)); |
1517 |
|
if (!port) |
1518 |
|
port = HKP_PORT; |
1519 |
|
*r_port = port; |
1520 |
|
return 0; |
1521 |
|
} |
1522 |
|
|
1523 |
|
if (url[(p-url)-1] == '/') /* remove / in .de/:11371 */ |
1524 |
|
off = 1; |
1525 |
|
|
1526 |
|
*r_keyserver = substr (url, 0, (p-url)-off); |
1527 |
|
*r_port = atoi (url+(p-url)+1); |
1528 |
|
free_if_alloc (url); |
1529 |
|
return 0; |
1530 |
|
} |