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 |
/* just map net_errno to a winsock error. */ |
/* Map net_errno to a winsock error. */ |
41 |
#define net_errno ((int)WSAGetLastError ()) |
#define net_errno ((int)WSAGetLastError ()) |
42 |
|
|
43 |
|
|
44 |
keyserver server[MAX_KEYSERVERS] = {0}; |
keyserver server[MAX_KEYSERVERS] = {0}; |
45 |
keyserver_proxy_s 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. */ |
/* Default socket timeout. */ |
64 |
static int default_socket_timeout = 10; |
static int default_socket_timeout = 6; |
65 |
|
|
66 |
|
|
67 |
|
/* Wrapper for safe memory allocation. */ |
68 |
|
static char* |
69 |
|
safe_alloc (DWORD n) |
70 |
|
{ |
71 |
|
char *p; |
72 |
|
|
73 |
|
p = new char[n+1]; |
74 |
|
if (!p) |
75 |
|
BUG (0); |
76 |
|
memset (p, 0, n); |
77 |
|
return p; |
78 |
|
} |
79 |
|
|
80 |
/* Basic64 encode the input @inbuf to @outbuf. */ |
/* Basic64 encode the input @inbuf to @outbuf. */ |
81 |
static void |
static void |
146 |
char *p, *end; |
char *p, *end; |
147 |
int ec, len; |
int ec, len; |
148 |
|
|
149 |
|
log_debug ("check_hkp_response: '%s'\r\n", resp); |
150 |
ec = recv ? WPTERR_WINSOCK_RECVKEY : WPTERR_WINSOCK_SENDKEY; |
ec = recv ? WPTERR_WINSOCK_RECVKEY : WPTERR_WINSOCK_SENDKEY; |
151 |
if (!strstr (resp, "HTTP/1.0 200 OK") && |
if (!strstr (resp, "HTTP/1.0 200 OK") && |
152 |
!strstr (resp, "HTTP/1.1 200 OK")) /* http error */ |
!strstr (resp, "HTTP/1.1 200 OK")) /* http error */ |
226 |
DWORD nread; |
DWORD nread; |
227 |
int nleft = buflen; |
int nleft = buflen; |
228 |
int rc, n = 0; |
int rc, n = 0; |
229 |
|
|
230 |
|
if (nbytes) |
231 |
|
*nbytes = 0; |
232 |
while (nleft > 0) { |
while (nleft > 0) { |
233 |
if (n >= default_socket_timeout) |
if (n >= default_socket_timeout) |
234 |
return WPTERR_WINSOCK_TIMEOUT; |
return WPTERR_WINSOCK_TIMEOUT; |
255 |
return 0; |
return 0; |
256 |
} |
} |
257 |
|
|
258 |
|
|
259 |
|
/* Helper to create a string from the gpgme data and |
260 |
|
then release the gpgme data object. */ |
261 |
|
char* |
262 |
|
data_release_and_get_mem (gpgme_data_t in, int *r_bufferlen) |
263 |
|
{ |
264 |
|
size_t n; |
265 |
|
char *buffer, *p; |
266 |
|
|
267 |
|
gpg_data_putc (in, '\0'); |
268 |
|
p = gpgme_data_release_and_get_mem (in, &n); |
269 |
|
buffer = m_strdup (p); |
270 |
|
if (r_bufferlen) |
271 |
|
*r_bufferlen = (int)n; |
272 |
|
gpgme_free (p); |
273 |
|
return buffer; |
274 |
|
} |
275 |
|
|
276 |
|
|
277 |
/* Read much data as possible from the socket @fd and |
/* Read much data as possible from the socket @fd and |
278 |
return the data in @buffer. Caller must free data. |
return the data in @buffer. Caller must free data. |
279 |
Return value: 0 on success. */ |
Return value: 0 on success. */ |
281 |
sock_read_ext (int fd, char **buffer, int *r_bufferlen) |
sock_read_ext (int fd, char **buffer, int *r_bufferlen) |
282 |
{ |
{ |
283 |
gpgme_data_t dh; |
gpgme_data_t dh; |
284 |
char buf[1024], *p; |
char buf[1024]; |
285 |
size_t n=0; |
size_t n=0; |
286 |
int nread, rc; |
int nread, rc; |
287 |
|
|
288 |
gpgme_data_new (&dh); |
if (gpgme_data_new (&dh)) |
289 |
|
BUG (0); |
290 |
while (n < 10) { |
while (n < 10) { |
291 |
rc = sock_select (fd, 1); |
rc = sock_select (fd, 1); |
292 |
if (rc == SOCKET_ERROR) { |
if (rc == SOCKET_ERROR) { |
304 |
gpgme_data_write (dh, buf, nread); |
gpgme_data_write (dh, buf, nread); |
305 |
} |
} |
306 |
} |
} |
307 |
gpg_data_putc (dh, '\0'); |
|
308 |
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); |
|
309 |
return 0; |
return 0; |
310 |
} |
} |
311 |
|
|
368 |
/* Return a string representation of a winsock error. */ |
/* Return a string representation of a winsock error. */ |
369 |
const char* |
const char* |
370 |
wsock_strerror (void) |
wsock_strerror (void) |
371 |
{ |
{ |
|
static char buf[384]; |
|
372 |
int ec = WSAGetLastError (); |
int ec = WSAGetLastError (); |
373 |
|
|
374 |
|
if (!ec) |
375 |
|
return ""; |
376 |
switch (ec) { |
switch (ec) { |
377 |
case WSAENETDOWN: |
case WSAENETDOWN: |
378 |
return _("The network subsystem has failed"); |
return _("Network unreachable"); |
379 |
|
|
380 |
|
case WSAEHOSTUNREACH: |
381 |
|
return _("Host unreachable"); |
382 |
|
|
383 |
case WSAHOST_NOT_FOUND: |
case WSAHOST_NOT_FOUND: |
384 |
return _("Authoritative Answer Host not found"); |
return _("Could not resolve host name"); |
385 |
|
|
386 |
|
case WSAECONNREFUSED: |
387 |
|
return _("Connection refused"); |
388 |
|
|
389 |
case WSAETIMEDOUT: |
case WSAETIMEDOUT: |
390 |
return _("The connection has been dropped because of a network failure"); |
case WSAECONNABORTED: |
391 |
|
return _("Connection timeout"); |
392 |
|
|
393 |
|
case WSAENETRESET: |
394 |
|
case WSAECONNRESET: |
395 |
|
return _("Connection resetted by peer"); |
396 |
|
|
397 |
|
case WSAESHUTDOWN: |
398 |
|
return _("Socket has been shutdown"); |
399 |
|
|
400 |
default: |
default: |
401 |
_snprintf (buf, sizeof (buf) -1, _("Unknown Winsock error ec=%d"),ec); |
break; |
|
return buf; |
|
402 |
} |
} |
403 |
return NULL; |
|
404 |
|
return ""; |
405 |
} |
} |
406 |
|
|
407 |
|
|
453 |
closesocket (fd); |
closesocket (fd); |
454 |
return 0; |
return 0; |
455 |
} |
} |
456 |
|
log_debug ("kserver_check_inet_connection: no inet connection.\r\n"); |
457 |
return -1; |
return -1; |
458 |
} |
} |
459 |
|
|
487 |
|
|
488 |
n = 4*strlen (proxy_user) / 3 + 32 + strlen (proxy_pass) + 2; |
n = 4*strlen (proxy_user) / 3 + 32 + strlen (proxy_pass) + 2; |
489 |
free_if_alloc (proxy.base64_user); |
free_if_alloc (proxy.base64_user); |
490 |
proxy.base64_user = new char[n]; |
proxy.base64_user = safe_alloc (n+1); |
|
if (!proxy.base64_user) |
|
|
BUG (0); |
|
491 |
_snprintf (t, sizeof (t)-1, "%s:%s", proxy_user, proxy_pass); |
_snprintf (t, sizeof (t)-1, "%s:%s", proxy_user, proxy_pass); |
492 |
base64_encode (t, proxy.base64_user); |
base64_encode (t, proxy.base64_user); |
493 |
free_if_alloc (proxy.user); |
free_if_alloc (proxy.user); |
517 |
port_from_proto (int proto) |
port_from_proto (int proto) |
518 |
{ |
{ |
519 |
switch (proto) { |
switch (proto) { |
520 |
case KSPROTO_LDAP: return 0; |
case KSPROTO_LDAP: return 0; |
521 |
case KSPROTO_FINGER: return FINGER_PORT; |
case KSPROTO_FINGER: return FINGER_PORT; |
522 |
case KSPROTO_HTTP: return HKP_PORT; |
case KSPROTO_HTTP: return HKP_PORT; |
523 |
} |
} |
524 |
return 0; |
return 0; |
525 |
} |
} |
529 |
static int |
static int |
530 |
proto_from_URL (const char *buf) |
proto_from_URL (const char *buf) |
531 |
{ |
{ |
532 |
if (strstr( buf, "ldap")) |
if (strstr (buf, "ldap")) |
533 |
return KSPROTO_LDAP; |
return KSPROTO_LDAP; |
534 |
else if (strstr( buf, "finger")) |
else if (strstr( buf, "finger")) |
535 |
return KSPROTO_FINGER; |
return KSPROTO_FINGER; |
537 |
} |
} |
538 |
|
|
539 |
|
|
540 |
|
/* Set the default keyserver. |
541 |
|
If @hostname is NULL, use the predefined keyserver. */ |
542 |
void |
void |
543 |
keyserver_set_default (const char *hostname, WORD port) |
keyserver_set_default (const char *hostname, WORD port) |
544 |
{ |
{ |
545 |
|
if (!port) |
546 |
|
port = HKP_PORT; |
547 |
if (hostname != NULL) { |
if (hostname != NULL) { |
548 |
free_if_alloc (default_keyserver); |
free_if_alloc (default_keyserver); |
549 |
default_keyserver = m_strdup (hostname); |
default_keyserver = m_strdup (hostname); |
|
if (!default_keyserver) |
|
|
BUG (0); |
|
550 |
default_keyserver_port = port; |
default_keyserver_port = port; |
551 |
} |
} |
552 |
if (!port) |
if (!default_keyserver) { |
|
port = HKP_PORT; |
|
|
if (!default_keyserver) |
|
553 |
default_keyserver = m_strdup (DEF_HKP_KEYSERVER); |
default_keyserver = m_strdup (DEF_HKP_KEYSERVER); |
554 |
|
default_keyserver_port = HKP_PORT; |
555 |
|
} |
556 |
server[0].name = m_strdup (default_keyserver); |
server[0].name = m_strdup (default_keyserver); |
557 |
server[0].used = 1; |
server[0].used = 1; |
558 |
server[0].port = port; |
server[0].port = port; |
607 |
/* Load the keyserver config file @conf. */ |
/* Load the keyserver config file @conf. */ |
608 |
int |
int |
609 |
kserver_load_conf (const char *conf) |
kserver_load_conf (const char *conf) |
610 |
{ |
{ |
|
struct stat statbuf; |
|
611 |
FILE *fp; |
FILE *fp; |
612 |
char buf[1024], *s, *p; |
char buf[1024], *s, *p; |
613 |
char *user = NULL, *pass = NULL; |
char *user = NULL, *pass = NULL; |
640 |
/* check if the host has a http:// prefix and skip it */ |
/* check if the host has a http:// prefix and skip it */ |
641 |
if (proxy.host && !strncmp (proxy.host, "http://", 7)) { |
if (proxy.host && !strncmp (proxy.host, "http://", 7)) { |
642 |
char *host = m_strdup (proxy.host+7); |
char *host = m_strdup (proxy.host+7); |
|
if (!host) |
|
|
BUG (0); |
|
643 |
free_if_alloc (proxy.host); |
free_if_alloc (proxy.host); |
644 |
proxy.host = host; |
proxy.host = host; |
645 |
} |
} |
677 |
server[pos].port = atol (p+1); |
server[pos].port = atol (p+1); |
678 |
if (server[pos].port < 0 || server[pos].port > 65536) |
if (server[pos].port < 0 || server[pos].port > 65536) |
679 |
server[pos].port = HKP_PORT; |
server[pos].port = HKP_PORT; |
680 |
server[pos].name = new char [(p-s)+2]; |
server[pos].name = safe_alloc ((p-s)+2); |
|
if (!server[pos].name) |
|
|
BUG (0); |
|
681 |
memset (server[pos].name, 0, (p-s)+2); |
memset (server[pos].name, 0, (p-s)+2); |
682 |
memcpy (server[pos].name, s, (p-s)); |
memcpy (server[pos].name, s, (p-s)); |
683 |
} |
} |
698 |
return WPTERR_CONFIG_FILE; |
return WPTERR_CONFIG_FILE; |
699 |
} |
} |
700 |
|
|
|
if (!stat (conf, &statbuf)) |
|
|
conf_timestamp = statbuf.st_mtime; |
|
701 |
return 0; |
return 0; |
702 |
} |
} |
703 |
|
|
706 |
Return value: 0 on success */ |
Return value: 0 on success */ |
707 |
int |
int |
708 |
kserver_connect (const char *hostname, WORD port, int *conn_fd) |
kserver_connect (const char *hostname, WORD port, int *conn_fd) |
709 |
{ |
{ |
|
int rc, fd; |
|
|
DWORD iaddr; |
|
|
char host[128] = {0}; |
|
710 |
struct hostent *hp; |
struct hostent *hp; |
711 |
struct sockaddr_in sock; |
struct sockaddr_in sock; |
712 |
|
char host[128] = {0}; |
713 |
|
DWORD iaddr; |
714 |
|
int rc, fd; |
715 |
|
|
716 |
log_debug ("kserver_connect: %s:%d\r\n", hostname, port); |
log_debug ("kserver_connect: %s:%d\r\n", hostname, port); |
717 |
|
|
721 |
*conn_fd = 0; |
*conn_fd = 0; |
722 |
hostname = skip_type_prefix (hostname); |
hostname = skip_type_prefix (hostname); |
723 |
|
|
724 |
|
if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) |
725 |
|
port = proxy.port; |
726 |
memset (&sock, 0, sizeof (sock)); |
memset (&sock, 0, sizeof (sock)); |
727 |
sock.sin_family = AF_INET; |
sock.sin_family = AF_INET; |
728 |
sock.sin_port = proxy.host? htons (proxy.port) : htons (port); |
sock.sin_port = htons (port); |
729 |
if (proxy.host) |
if (proxy.host) |
730 |
strncpy (host, proxy.host, 127); |
strncpy (host, proxy.host, 127); |
731 |
else |
else |
732 |
strncpy (host, hostname, 127); |
strncpy (host, hostname, 127); |
733 |
|
|
734 |
if ((iaddr = inet_addr (host)) != INADDR_NONE) |
if ((iaddr = inet_addr (host)) != INADDR_NONE) |
735 |
memcpy (&sock.sin_addr, &iaddr, sizeof (iaddr)); |
memcpy (&sock.sin_addr, &iaddr, sizeof (iaddr)); |
736 |
else if ((hp = gethostbyname (host))) { |
else if ((hp = gethostbyname (host))) { |
754 |
return WPTERR_WINSOCK_CONNECT; |
return WPTERR_WINSOCK_CONNECT; |
755 |
} |
} |
756 |
|
|
757 |
if (proxy.proto != PROXY_PROTO_HTTP) { |
if (proxy.proto == PROXY_PROTO_SOCKS5) { |
758 |
rc = socks_handshake (&proxy, fd, hostname, port); |
rc = socks_handshake (&proxy, fd, hostname, port); |
759 |
if (rc) { |
if (rc) { |
760 |
closesocket (fd); |
closesocket (fd); |
780 |
|
|
781 |
/* Perform URL encoding of the given data. */ |
/* Perform URL encoding of the given data. */ |
782 |
static char* |
static char* |
783 |
URL_encode (const char *url, size_t ulen, size_t *ret_nlen) |
URL_encode (const char *url, DWORD ulen, DWORD *ret_nlen) |
784 |
{ |
{ |
785 |
gpgme_data_t hd; |
gpgme_data_t hd; |
786 |
char numbuf[5], *pp, *p; |
char numbuf[5], *p; |
787 |
size_t i, n; |
size_t i; |
788 |
|
int nlen; |
789 |
|
|
790 |
gpgme_data_new (&hd); |
if (gpgme_data_new (&hd)) |
791 |
|
BUG (0); |
792 |
for (i=0; i < ulen; i++) { |
for (i=0; i < ulen; i++) { |
793 |
if (isalnum (url[i]) || url[i] == '-') |
if (isalnum (url[i]) || url[i] == '-') |
794 |
gpg_data_putc (hd, url[i]); |
gpg_data_putc (hd, url[i]); |
799 |
gpgme_data_write (hd, numbuf, strlen (numbuf)); |
gpgme_data_write (hd, numbuf, strlen (numbuf)); |
800 |
} |
} |
801 |
} |
} |
|
gpg_data_putc (hd, '\0'); |
|
802 |
|
|
803 |
/* 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); |
|
804 |
if (ret_nlen) |
if (ret_nlen) |
805 |
*ret_nlen = n; |
*ret_nlen = nlen; |
806 |
return p; |
return p; |
807 |
} |
} |
808 |
|
|
810 |
/* Format a request for the given keyid (send). */ |
/* Format a request for the given keyid (send). */ |
811 |
static char* |
static char* |
812 |
kserver_send_request (const char *hostname, WORD port, |
kserver_send_request (const char *hostname, WORD port, |
813 |
const char *pubkey, int octets) |
const char *pubkey, DWORD octets) |
814 |
{ |
{ |
815 |
char *request = NULL; |
const char *fmt; |
816 |
char *enc_pubkey = NULL; |
char *request; |
817 |
size_t enc_octets; |
char *enc_pubkey; |
818 |
|
DWORD enc_octets; |
819 |
int reqlen; |
int reqlen; |
820 |
|
|
821 |
log_debug ("kserver_send_request: %s:%d\r\n", hostname, port); |
log_debug ("kserver_send_request: %s:%d\r\n", hostname, port); |
822 |
|
|
823 |
if (!port) |
if (!port) |
824 |
port = HKP_PORT; |
port = HKP_PORT; |
|
reqlen = 512 + strlen (hostname) + 2*strlen (pubkey); |
|
|
request = new char[reqlen]; |
|
|
if (!request) |
|
|
BUG (0); |
|
825 |
enc_pubkey = URL_encode (pubkey, octets, &enc_octets); |
enc_pubkey = URL_encode (pubkey, octets, &enc_octets); |
826 |
if (!enc_pubkey || !enc_octets) { |
reqlen = 2*strlen (hostname) + 2*32/*port*/ + enc_octets + 32 /*len*/ + 1; |
|
free_if_alloc (request); |
|
|
return NULL; |
|
|
} |
|
827 |
|
|
828 |
if (proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
if (proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
829 |
_snprintf (request, reqlen-1, |
fmt = "POST http://%s:%d/pks/add HTTP/1.0\r\n" |
830 |
"POST http://%s:%d/pks/add HTTP/1.0\r\n" |
"Referer: \r\n" |
831 |
"Referer: \r\n" |
"User-Agent: WinPT/W32\r\n" |
832 |
"User-Agent: WinPT/W32\r\n" |
"Host: %s:%d\r\n" |
833 |
"Host: %s:%d\r\n" |
"Proxy-Authorization: Basic %s\r\n" |
834 |
"Proxy-Authorization: Basic %s\r\n" |
"Content-type: application/x-www-form-urlencoded\r\n" |
835 |
"Content-type: application/x-www-form-urlencoded\r\n" |
"Content-length: %d\r\n" |
836 |
"Content-length: %d\r\n" |
"\r\n" |
837 |
"\r\n" |
"keytext=%s" |
838 |
"keytext=%s" |
"\r\n"; |
839 |
"\n", |
reqlen += strlen (fmt) + strlen (proxy.base64_user) + 1; |
840 |
skip_type_prefix (hostname), port, hostname, port, |
request = safe_alloc (reqlen+1); |
841 |
|
_snprintf (request, reqlen, fmt, |
842 |
|
skip_type_prefix (hostname), port, hostname, port, |
843 |
proxy.base64_user, enc_octets+9, enc_pubkey); |
proxy.base64_user, enc_octets+9, enc_pubkey); |
844 |
} |
} |
845 |
else { |
else { |
846 |
_snprintf (request, reqlen-1, |
fmt = "POST /pks/add HTTP/1.0\r\n" |
847 |
"POST /pks/add HTTP/1.0\r\n" |
"Referer: \r\n" |
848 |
"Referer: \r\n" |
"User-Agent: WinPT/W32\r\n" |
849 |
"User-Agent: WinPT/W32\r\n" |
"Host: %s:%d\r\n" |
850 |
"Host: %s:%d\r\n" |
"Content-type: application/x-www-form-urlencoded\r\n" |
851 |
"Content-type: application/x-www-form-urlencoded\r\n" |
"Content-length: %d\r\n" |
852 |
"Content-length: %d\r\n" |
"\r\n" |
853 |
"\r\n" |
"keytext=%s" |
854 |
"keytext=%s" |
"\r\n"; |
855 |
"\n", |
reqlen += strlen (fmt)+1; |
856 |
skip_type_prefix (hostname), port, |
request = safe_alloc (reqlen+1); |
857 |
|
_snprintf (request, reqlen, fmt, |
858 |
|
skip_type_prefix (hostname), port, |
859 |
enc_octets+9, enc_pubkey); |
enc_octets+9, enc_pubkey); |
860 |
} |
} |
|
free_if_alloc (enc_pubkey); |
|
861 |
|
|
862 |
log_debug ("%s\r\n", request); |
free_if_alloc (enc_pubkey); |
863 |
|
log_debug ("request:\r\n%s\r\n", request); |
864 |
return request; |
return request; |
865 |
} |
} |
866 |
|
|
867 |
|
|
868 |
|
/* Interface for receiving a public key. */ |
869 |
int |
int |
870 |
kserver_recvkey_ext (const char *hostname, WORD port, const char *keyid, |
kserver_recvkey (const char *hostname, WORD port, const char *keyid, |
871 |
char **r_key, int *r_keylen) |
char **r_key, int *r_keylen) |
872 |
{ |
{ |
873 |
|
const char *fmt; |
874 |
char *request = NULL; |
char *request = NULL; |
875 |
int conn_fd; |
int conn_fd; |
876 |
int rc, n; |
int rc, reqlen; |
877 |
|
|
878 |
if (!port) |
if (!port) |
879 |
port = HKP_PORT; |
port = HKP_PORT; |
880 |
hkp_err = 0; /* reset */ |
hkp_err = 0; /* reset */ |
881 |
rc = kserver_connect (hostname, port, &conn_fd); |
rc = kserver_connect (hostname, port, &conn_fd); |
882 |
if (rc) |
if (rc) |
883 |
goto leave; |
goto leave; |
884 |
|
|
885 |
request = new char[300+1]; |
reqlen = strlen (hostname)+32+strlen (keyid)+1; |
|
if (!request) |
|
|
BUG (0); |
|
|
|
|
886 |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
887 |
_snprintf (request, 300, |
fmt = "GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n" |
888 |
"GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n" |
"Proxy-Authorization: Basic %s\r\n\r\n"; |
889 |
"Proxy-Authorization: Basic %s\r\n\r\n", |
reqlen =+ strlen (fmt) + strlen (proxy.base64_user)+1; |
890 |
skip_type_prefix (hostname), port, keyid, proxy.base64_user); |
request = safe_alloc (reqlen+1); |
891 |
|
_snprintf (request, reqlen, fmt, skip_type_prefix (hostname), port, |
892 |
|
keyid, proxy.base64_user); |
893 |
} |
} |
894 |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
895 |
_snprintf (request, 300, |
fmt = "GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n"; |
896 |
"GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n", |
reqlen += strlen (fmt)+1; |
897 |
skip_type_prefix (hostname), port, keyid); |
request = safe_alloc (reqlen+1); |
898 |
} |
_snprintf (request, reqlen, fmt, skip_type_prefix (hostname), |
899 |
|
port, keyid); |
900 |
|
} |
901 |
else { |
else { |
902 |
_snprintf (request, 300, |
fmt = "GET /pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n"; |
903 |
"GET /pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n", keyid); |
reqlen += strlen (fmt)+1; |
904 |
|
request = safe_alloc (reqlen+1); |
905 |
|
_snprintf (request, reqlen, fmt, keyid); |
906 |
} |
} |
907 |
|
|
908 |
log_debug ("%s\r\n", request); |
log_debug ("request:\r\n%s\r\n", request); |
909 |
|
|
910 |
rc = sock_write (conn_fd, request, strlen (request)); |
rc = sock_write (conn_fd, request, strlen (request)); |
911 |
if (rc == SOCKET_ERROR) { |
if (rc == SOCKET_ERROR) { |
913 |
goto leave; |
goto leave; |
914 |
} |
} |
915 |
|
|
916 |
rc = sock_read_ext (conn_fd, r_key, &n); |
rc = sock_read_ext (conn_fd, r_key, r_keylen); |
917 |
if (rc == SOCKET_ERROR) { |
if (rc == SOCKET_ERROR) { |
918 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
919 |
goto leave; |
goto leave; |
920 |
} |
} |
921 |
|
|
922 |
if (r_keylen) |
log_debug ("response:\r\n%s\r\n", *r_key); |
|
*r_keylen = n; |
|
|
log_debug("%s\r\n", *r_key); |
|
923 |
rc = check_hkp_response (*r_key, 1); |
rc = check_hkp_response (*r_key, 1); |
924 |
if (rc) |
if (rc) |
925 |
goto leave; |
goto leave; |
932 |
return rc; |
return rc; |
933 |
} |
} |
934 |
|
|
|
/* 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; |
|
|
} |
|
|
|
|
935 |
|
|
936 |
/* Interface to send a public key. */ |
/* Interface to send a public key. */ |
937 |
int |
int |
938 |
kserver_sendkey (const char *hostname, WORD port, const char *pubkey, int len ) |
kserver_sendkey (const char *hostname, WORD port, const char *pubkey, int len ) |
939 |
{ |
{ |
940 |
char *request = NULL; |
char *request = NULL; |
941 |
char log[2048]; |
char log[2048] = {0}; |
942 |
int conn_fd, n; |
int conn_fd, n; |
943 |
int rc; |
int rc; |
944 |
|
|
965 |
goto leave; |
goto leave; |
966 |
} |
} |
967 |
|
|
968 |
log_debug ("kserver_sendkey:\r\n%s\r\n", log); |
log_debug ("kserver_sendkey: read %d bytes\r\n%s\r\n", n, log); |
969 |
rc = check_hkp_response (log, 0); |
rc = check_hkp_response (log, 0); |
970 |
if (rc) |
if (rc) |
971 |
goto leave; |
goto leave; |
979 |
} |
} |
980 |
|
|
981 |
|
|
982 |
|
/* Check keyserver response. */ |
983 |
|
static int |
984 |
|
kserver_search_chkresp (int fd) |
985 |
|
{ |
986 |
|
char buf[128]; |
987 |
|
int n=0; |
988 |
|
|
989 |
|
/* parse response 'HTTP/1.0 500 OK' */ |
990 |
|
if (sock_getline (fd, buf, sizeof (buf)-1, &n)) |
991 |
|
return WPTERR_KEYSERVER_NOTFOUND; |
992 |
|
|
993 |
|
log_debug ("kserver_search_chkpresp: %s\r\n", buf); |
994 |
|
if (strncmp (buf, "HTTP/1.", 7)) |
995 |
|
return WPTERR_KEYSERVER_NOTFOUND; |
996 |
|
if (strncmp (buf+(8+1), "200", 3)) |
997 |
|
return WPTERR_KEYSERVER_NOTFOUND; |
998 |
|
return 0; |
999 |
|
} |
1000 |
|
|
1001 |
|
|
1002 |
|
/* End the keyserver search procedure. */ |
1003 |
|
void |
1004 |
|
kserver_search_end (int conn_fd) |
1005 |
|
{ |
1006 |
|
log_debug ("kserver_search_end: fd=%d\r\n", conn_fd); |
1007 |
|
closesocket (conn_fd); |
1008 |
|
} |
1009 |
|
|
1010 |
|
|
1011 |
|
/* Extract the amount of keys from the info record. */ |
1012 |
|
static int |
1013 |
|
count_keys_in_response (char *buf) |
1014 |
|
{ |
1015 |
|
char *p; |
1016 |
|
int recno = 0; |
1017 |
|
int n = 0; |
1018 |
|
|
1019 |
|
/* info:1:4 */ |
1020 |
|
if (strncmp (buf, "info", 4)) |
1021 |
|
return 0; |
1022 |
|
p = strtok (buf, ":"); |
1023 |
|
while (p != NULL) { |
1024 |
|
recno++; |
1025 |
|
if (recno == 3) |
1026 |
|
n = atoi (p); |
1027 |
|
p = strtok (NULL, ":"); |
1028 |
|
} |
1029 |
|
return n; |
1030 |
|
} |
1031 |
|
|
1032 |
|
|
1033 |
|
/* Start the keyserver search. |
1034 |
|
Connect to host @hostname and port @port. |
1035 |
|
The pattern are given in @pattern. |
1036 |
|
The socket is returned in @conn_fd and @nkeys contains |
1037 |
|
the amount of keys which were found. */ |
1038 |
int |
int |
1039 |
kserver_search_init (const char *hostname, WORD port, |
kserver_search_begin (const char *hostname, WORD port, |
1040 |
const char *keyid, int *conn_fd) |
const char *pattern, int *conn_fd, int *nkeys) |
1041 |
{ |
{ |
1042 |
|
const char *fmt; |
1043 |
char *request = NULL; |
char *request = NULL; |
1044 |
char *enc_keyid = NULL; |
char *enc_patt = NULL; |
1045 |
int n = 0; |
char status[128]; |
1046 |
int rc, sock_fd; |
int rc, sock_fd; |
1047 |
|
int reqlen; |
1048 |
|
|
1049 |
|
*conn_fd = 0; |
1050 |
|
|
1051 |
rc = kserver_connect (hostname, port, &sock_fd); |
rc = kserver_connect (hostname, port, &sock_fd); |
1052 |
if (rc) { |
if (rc) |
|
*conn_fd = 0; |
|
1053 |
goto leave; |
goto leave; |
|
} |
|
1054 |
|
|
1055 |
enc_keyid = URL_encode (keyid, strlen (keyid), NULL); |
enc_patt = URL_encode (pattern, strlen (pattern), NULL); |
1056 |
n=300; |
reqlen = strlen (enc_patt) + strlen (hostname) + 32 + 1; |
1057 |
request = new char[n+1]; |
|
|
if (!request) |
|
|
BUG (0); |
|
|
|
|
1058 |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
1059 |
_snprintf (request, n, |
fmt = "GET http://%s:%d/pks/lookup?options=mr&op=index&search=%s HTTP/1.0\r\n" |
1060 |
"GET http://%s:%d/pks/lookup?op=index&search=%s HTTP/1.0\r\n" |
"Proxy-Authorization: Basic %s\r\n\r\n"; |
1061 |
"Proxy-Authorization: Basic %s\r\n\r\n", |
reqlen += strlen (proxy.base64_user) + strlen (fmt) + 1; |
1062 |
skip_type_prefix (hostname), port, enc_keyid, proxy.base64_user); |
request = safe_alloc (reqlen+1); |
1063 |
|
_snprintf (request, reqlen, fmt, skip_type_prefix (hostname), port, |
1064 |
|
enc_patt, proxy.base64_user); |
1065 |
} |
} |
1066 |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
1067 |
_snprintf (request, n, |
fmt = "GET http://%s:%d/pks/lookup?options=mr&op=index&search=%s HTTP/1.0\r\n\r\n"; |
1068 |
"GET http://%s:%d/pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n", |
reqlen += strlen (fmt)+1; |
1069 |
skip_type_prefix (hostname), port, enc_keyid); |
request = safe_alloc (reqlen+1); |
1070 |
|
_snprintf (request, reqlen, skip_type_prefix (hostname), port, |
1071 |
|
enc_patt); |
1072 |
} |
} |
1073 |
else { |
else { |
1074 |
_snprintf (request, n, |
fmt = "GET /pks/lookup?options=mr&op=index&search=%s HTTP/1.0\r\n\r\n"; |
1075 |
"GET /pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n", |
reqlen += strlen (fmt)+1; |
1076 |
enc_keyid); |
request = safe_alloc (reqlen+1); |
1077 |
|
_snprintf (request, reqlen, fmt, enc_patt); |
1078 |
} |
} |
1079 |
|
|
1080 |
log_debug ("kserver_search_init:\r\n%s\r\n", request); |
log_debug ("kserver_search_begin:\r\n%s\r\n", request); |
|
|
|
1081 |
if (sock_write (sock_fd, request, strlen (request)) == SOCKET_ERROR) { |
if (sock_write (sock_fd, request, strlen (request)) == SOCKET_ERROR) { |
1082 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1083 |
goto leave; |
goto leave; |
1084 |
} |
} |
1085 |
|
|
1086 |
|
rc = kserver_search_chkresp (sock_fd); |
1087 |
|
if (rc) { |
1088 |
|
closesocket (sock_fd); |
1089 |
|
sock_fd = 0; |
1090 |
|
goto leave; |
1091 |
|
} |
1092 |
|
|
1093 |
|
/* Skip all lines until we reach the "info:" record. */ |
1094 |
|
for (;;) { |
1095 |
|
if (sock_getline (sock_fd, status, sizeof (status)-1, &reqlen)) { |
1096 |
|
log_debug ("kserver_search_begin: retrieving status line failed.\r\n"); |
1097 |
|
closesocket (sock_fd); |
1098 |
|
sock_fd = 0; |
1099 |
|
rc = WPTERR_GENERAL; |
1100 |
|
break; |
1101 |
|
} |
1102 |
|
if (!strncmp (status, "info:", 5)) |
1103 |
|
break; |
1104 |
|
} |
1105 |
|
|
1106 |
|
if (!rc) |
1107 |
|
*nkeys = count_keys_in_response (status); |
1108 |
*conn_fd = sock_fd; |
*conn_fd = sock_fd; |
1109 |
|
|
1110 |
leave: |
leave: |
1111 |
free_if_alloc (request); |
free_if_alloc (request); |
1112 |
free_if_alloc (enc_keyid); |
free_if_alloc (enc_patt); |
1113 |
return rc; |
return rc; |
1114 |
} |
} |
1115 |
|
|
1116 |
|
|
1117 |
/* Check keyserver response. */ |
/* Parse a single pub record returned by the keyserver. */ |
1118 |
int |
static void |
1119 |
kserver_search_chkresp (int fd) |
parse_pub_record (keyserver_key_s *key, char *buf) |
1120 |
{ |
{ |
1121 |
char buf[128]; |
enum pub_rec_t {ID=1, KEYID, ALGO, SIZE, CREATE, EXPIRE}; |
1122 |
int n=0; |
char *p; |
1123 |
|
int recno = 0; |
|
/* parse response 'HTTP/1.0 500 OK' */ |
|
|
if (sock_getline (fd, buf, 127, &n)) |
|
|
return WPTERR_KEYSERVER_NOTFOUND; |
|
1124 |
|
|
1125 |
log_debug ("kserver_search_chkpresp: %s\r\n", buf); |
/* pub:BF3DF9B4:17:1024:925411133:: */ |
1126 |
if (strncmp (buf, "HTTP/1.", 7)) |
p = strtok (buf, ":"); |
1127 |
return WPTERR_KEYSERVER_NOTFOUND; |
while (p != NULL) { |
1128 |
if (strncmp (buf+(8+1), "200", 3)) |
recno++; |
1129 |
return WPTERR_KEYSERVER_NOTFOUND; |
switch (recno) { |
1130 |
return 0; |
case ID: |
1131 |
|
break; |
1132 |
|
|
1133 |
|
case KEYID: |
1134 |
|
free_if_alloc (key->keyid); |
1135 |
|
key->keyid = m_strdup (p); |
1136 |
|
break; |
1137 |
|
|
1138 |
|
case ALGO: |
1139 |
|
key->algo = atoi (p); |
1140 |
|
break; |
1141 |
|
|
1142 |
|
case SIZE: |
1143 |
|
key->bits = atoi (p); |
1144 |
|
break; |
1145 |
|
|
1146 |
|
case CREATE: |
1147 |
|
key->creation = strtoul (p, NULL, 10); |
1148 |
|
break; |
1149 |
|
|
1150 |
|
case EXPIRE: |
1151 |
|
key->expires = strtoul (p, NULL, 10); |
1152 |
|
break; |
1153 |
|
} |
1154 |
|
p = strtok (NULL, ":"); |
1155 |
|
} |
1156 |
|
|
1157 |
} |
} |
1158 |
|
|
1159 |
|
|
1160 |
/* Convert an iso date @iso_date (YYYY-MM-DD) into the locale |
/* Parse a single user id returned by the keyserver. */ |
1161 |
representation and store it into @loc_date. |
static void |
1162 |
Return value: 0 on success. */ |
parse_uid_record (keyserver_key_s *key, char *buf) |
|
static int |
|
|
parse_iso_date (const char *iso_date, char *loc_date, size_t loclen) |
|
1163 |
{ |
{ |
1164 |
SYSTEMTIME st; |
enum uid_rec_t {ID=1, UID, CREATE, EXPIRE}; |
1165 |
char buf[16] = {0}, *p; |
keyserver_uid_s *u, *n; |
1166 |
int pos=0; |
char *p; |
1167 |
|
int recno = 0; |
1168 |
|
|
1169 |
|
/* uid:Timo Schulz <[email protected]>:1138440360:: */ |
1170 |
|
u = new keyserver_uid_s; |
1171 |
|
memset (u, 0, sizeof *u); |
1172 |
|
|
1173 |
strncpy (buf, iso_date, sizeof (buf)-1); |
p = strtok (buf, ":"); |
|
p = strtok (buf, "-"); |
|
1174 |
while (p != NULL) { |
while (p != NULL) { |
1175 |
switch (pos) { |
recno++; |
1176 |
case 0: st.wYear = (WORD)atoi (p); pos++; break; |
|
1177 |
case 1: st.wMonth = (WORD)atoi (p); pos++; break; |
switch (recno) { |
1178 |
case 2: st.wDay = (WORD)atoi (p); pos++; break; |
case ID: |
1179 |
default: break; |
break; |
1180 |
|
|
1181 |
|
case UID: |
1182 |
|
unhexify_buffer (p, &u->uid); |
1183 |
|
break; |
1184 |
|
|
1185 |
|
case CREATE: |
1186 |
|
u->creation = strtoul (p, NULL, 10); |
1187 |
|
break; |
1188 |
|
|
1189 |
|
case EXPIRE: |
1190 |
|
u->expires = strtoul (p, NULL, 10); |
1191 |
|
break; |
1192 |
} |
} |
1193 |
p = strtok (NULL, "-"); |
|
1194 |
|
p = strtok (NULL, ":"); |
1195 |
} |
} |
1196 |
if (pos != 3) |
if (!key->uids) |
1197 |
return -1; |
key->uids = u; |
1198 |
|
else { |
1199 |
if (!GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, |
for (n = key->uids; n->next; n=n->next) |
1200 |
NULL, loc_date, loclen)) |
; |
1201 |
return -1; |
n->next = u; |
1202 |
return 0; |
} |
1203 |
|
} |
1204 |
|
|
1205 |
|
|
1206 |
|
/* Peek the next 3 bytes to check if the next line |
1207 |
|
would be a new "pub" record. In this case, return |
1208 |
|
-1 as an EOF indication, 0 otherwise. */ |
1209 |
|
static int |
1210 |
|
is_key_eof (int fd) |
1211 |
|
{ |
1212 |
|
char buf[64]; |
1213 |
|
int n; |
1214 |
|
|
1215 |
|
n = recv (fd, buf, 3, MSG_PEEK); |
1216 |
|
if (n < 3 || strncmp (buf, "pub", 3)) |
1217 |
|
return 0; |
1218 |
|
return -1; |
1219 |
} |
} |
1220 |
|
|
1221 |
|
|
1222 |
|
/* Return the next key from the search response. */ |
1223 |
int |
int |
1224 |
kserver_search (int fd, keyserver_key *key) |
kserver_search_next (int fd, keyserver_key_s **r_key) |
1225 |
{ |
{ |
1226 |
char buf[1024], *p; |
keyserver_uid_s *uid, *u = NULL; |
1227 |
int uidlen, nbytes, pos = 0; |
keyserver_key_s *key; |
1228 |
|
char buf[512]; |
1229 |
|
int n = 0; |
1230 |
|
long max = 0; |
1231 |
|
|
1232 |
log_debug ("keyserver_search:\r\n"); |
log_debug ("keyserver_search_next:\r\n"); |
1233 |
|
*r_key = NULL; |
|
if (sock_getline (fd, buf, sizeof (buf) - 1, &nbytes)) |
|
|
return WPTERR_GENERAL; |
|
1234 |
|
|
1235 |
log_debug ("%s\r\n", buf); |
key = new keyserver_key_s; |
1236 |
|
memset (key, 0, sizeof *key); |
1237 |
if (!strncmp (buf, "pub", 3)) { |
for (;;) { |
1238 |
int revoked = strstr (buf, "KEY REVOKED") != NULL? 1 : 0; |
if (sock_getline (fd, buf, sizeof (buf)-1, &n)) |
1239 |
key->bits = atol (buf+3); |
break; |
1240 |
p = strchr (buf, '>'); |
/*log_debug ("record: '%s'\r\n", buf); */ |
1241 |
if (!p) |
if (!strncmp (buf, "pub", 3)) |
1242 |
goto fail; |
parse_pub_record (key, buf); |
1243 |
pos = p - buf + 1; |
else |
1244 |
memcpy (key->keyid, buf + pos, 8); |
parse_uid_record (key, buf); |
1245 |
key->keyid[8] = '\0'; |
if (is_key_eof (fd)) |
1246 |
p = strstr (buf, "</a>"); |
break; |
1247 |
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; |
|
|
} |
|
1248 |
|
|
1249 |
if (uidlen > sizeof (key->uid)-1) |
/* the uid with the newest self sig is used as the |
1250 |
uidlen = sizeof (key->uid)-1; |
primary user id. */ |
1251 |
memcpy (key->uid, p, uidlen); |
for (uid = key->uids; uid; uid = uid->next) { |
1252 |
key->uid[uidlen] = '\0'; |
if (uid->creation > max) { |
1253 |
strcat (key->uid, ">"); |
max = uid->creation; |
1254 |
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); |
|
1255 |
} |
} |
|
return 0; |
|
1256 |
} |
} |
1257 |
|
|
1258 |
fail: |
key->main_uid = u? u : key->uids; |
1259 |
key->bits = 0; |
*r_key = key; |
|
memset (key, 0, sizeof *key); |
|
1260 |
return 0; |
return 0; |
1261 |
} |
} |
1262 |
|
|
1263 |
|
|
1264 |
|
/* Release the keyserver key @key and all its elements. */ |
1265 |
|
void |
1266 |
|
kserver_release_key (keyserver_key_s *key) |
1267 |
|
{ |
1268 |
|
keyserver_uid_s *u; |
1269 |
|
|
1270 |
|
while (key->uids) { |
1271 |
|
u = key->uids->next; |
1272 |
|
free_if_alloc (key->uids->uid); |
1273 |
|
free_if_alloc (key->uids); |
1274 |
|
key->uids = u; |
1275 |
|
} |
1276 |
|
free_if_alloc (key->keyid); |
1277 |
|
free_if_alloc (key); |
1278 |
|
} |
1279 |
|
|
1280 |
|
|
1281 |
/* Release mbmers in the proxy context @ctx. */ |
/* Release mbmers in the proxy context @ctx. */ |
1282 |
void |
void |
1283 |
kserver_proxy_release (keyserver_proxy_t ctx) |
kserver_proxy_release (keyserver_proxy_t ctx) |
1285 |
free_if_alloc (ctx->host); |
free_if_alloc (ctx->host); |
1286 |
free_if_alloc (ctx->pass); |
free_if_alloc (ctx->pass); |
1287 |
free_if_alloc (ctx->user); |
free_if_alloc (ctx->user); |
1288 |
|
ctx->port = ctx->proto = 0; |
1289 |
} |
} |
1290 |
|
|
1291 |
|
|
1316 |
} |
} |
1317 |
|
|
1318 |
|
|
1319 |
/* Receive an key via LDAP from host @host with the keyid @keyid. |
static FILE* |
1320 |
@key contains the key on success. */ |
do_spawn_ldap_helper (const char *host, const char *keyid) |
1321 |
int |
{ |
1322 |
ldap_recvkey (const char *host, const char *keyid, char *key, int maxkeylen) |
FILE *fp = NULL; |
1323 |
{ |
char *p, *sep; |
1324 |
FILE *fp; |
char *ksprg; |
1325 |
const char *s; |
char outf[256], inf[256]; |
1326 |
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; |
|
1327 |
|
|
1328 |
p = get_gnupg_prog (); |
p = get_gnupg_prog (); |
1329 |
n = strlen (p) + 1 + 128; |
n = strlen (p) + 1 + 128; |
1330 |
ksprg = new char[n+1]; |
ksprg = safe_alloc (n+1); |
1331 |
if (!ksprg) |
if (!ksprg) |
1332 |
BUG (0); |
BUG (0); |
1333 |
sep = strrchr (p, '\\'); |
sep = strrchr (p, '\\'); |
1339 |
if (file_exist_check (ksprg)) { |
if (file_exist_check (ksprg)) { |
1340 |
log_box ("LDAP Keyserver Plugin", MB_ERR, |
log_box ("LDAP Keyserver Plugin", MB_ERR, |
1341 |
"%s: could not find LDAP keyserver module!", ksprg); |
"%s: could not find LDAP keyserver module!", ksprg); |
1342 |
rc = -1; |
fp = NULL; |
1343 |
goto leave; |
goto leave; |
1344 |
} |
} |
1345 |
get_temp_name (outf, sizeof (outf)-1, keyid); |
get_temp_name (outf, sizeof (outf)-1, keyid); |
1348 |
if (!fp) { |
if (!fp) { |
1349 |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", inf, |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", inf, |
1350 |
winpt_strerror (WPTERR_FILE_OPEN)); |
winpt_strerror (WPTERR_FILE_OPEN)); |
|
rc = -1; |
|
1351 |
goto leave; |
goto leave; |
1352 |
} |
} |
1353 |
fprintf (fp, |
fprintf (fp, |
1361 |
host? skip_type_prefix (host): "64.94.85.200", keyid); |
host? skip_type_prefix (host): "64.94.85.200", keyid); |
1362 |
fclose (fp); |
fclose (fp); |
1363 |
|
|
1364 |
p = new char[strlen (ksprg) + strlen (inf) + strlen (outf) + 32]; |
p = safe_alloc (strlen (ksprg) + strlen (inf) + strlen (outf) + 32); |
|
if (!p) |
|
|
BUG (NULL); |
|
1365 |
sprintf (p, "%s -o %s %s", ksprg, outf, inf); |
sprintf (p, "%s -o %s %s", ksprg, outf, inf); |
1366 |
if (spawn_application (p)) { |
if (spawn_application (p)) { |
1367 |
rc = -1; |
fp = NULL; |
1368 |
goto leave; |
goto leave; |
1369 |
} |
} |
|
|
|
1370 |
fp = fopen (outf, "rb"); |
fp = fopen (outf, "rb"); |
1371 |
if (!fp) { |
if (!fp) |
1372 |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf, |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf, |
1373 |
winpt_strerror (WPTERR_FILE_OPEN)); |
winpt_strerror (WPTERR_FILE_OPEN)); |
1374 |
rc = -1; |
|
1375 |
goto leave; |
leave: |
1376 |
} |
DeleteFile (inf); |
1377 |
memset (key, 0, maxkeylen); |
DeleteFile (outf); |
1378 |
|
free_if_alloc (p); |
1379 |
|
free_if_alloc (ksprg); |
1380 |
|
return fp; |
1381 |
|
} |
1382 |
|
|
1383 |
|
/* Receive an key via LDAP from host @host with the keyid @keyid. |
1384 |
|
@key contains the key on success. */ |
1385 |
|
int |
1386 |
|
ldap_recvkey (const char *host, const char *keyid, |
1387 |
|
char **r_key, int *r_keylen) |
1388 |
|
{ |
1389 |
|
gpgme_data_t raw; |
1390 |
|
FILE *fp; |
1391 |
|
const char *s; |
1392 |
|
char buf[512]; |
1393 |
|
int start_key = 0, failed = 0; |
1394 |
|
int rc = 0; |
1395 |
|
|
1396 |
|
fp = do_spawn_ldap_helper (host, keyid); |
1397 |
|
if (!fp) |
1398 |
|
return WPTERR_GENERAL; |
1399 |
|
|
1400 |
|
if (gpgme_data_new (&raw)) |
1401 |
|
BUG (0); |
1402 |
while (!feof (fp)) { |
while (!feof (fp)) { |
1403 |
s = fgets (buf, sizeof (buf)-1, fp); |
s = fgets (buf, sizeof (buf)-1, fp); |
1404 |
if (!s) |
if (!s) |
1410 |
if (!start_key && strstr (s, "KEY") && strstr (s, "BEGIN")) { |
if (!start_key && strstr (s, "KEY") && strstr (s, "BEGIN")) { |
1411 |
start_key = 1; |
start_key = 1; |
1412 |
continue; |
continue; |
1413 |
} |
} |
1414 |
if (!start_key) |
if (!start_key) |
1415 |
continue; |
continue; |
1416 |
strcat (key, buf); |
gpgme_data_write (raw, buf, strlen (buf)); |
1417 |
maxkeylen -= strlen (buf); |
if (strstr (s, "KEY") && strstr (s, "END")) |
|
if (maxkeylen < 0 || (strstr (s, "KEY") && strstr (s, "END"))) |
|
1418 |
break; |
break; |
1419 |
} |
} |
1420 |
fclose (fp); |
fclose (fp); |
1421 |
|
|
1422 |
leave: |
if (failed) |
|
if (failed || !strlen (key)) |
|
1423 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
1424 |
DeleteFile (inf); |
*r_key = data_release_and_get_mem (raw, r_keylen); |
|
DeleteFile (outf); |
|
|
free_if_alloc (p); |
|
|
free_if_alloc (ksprg); |
|
1425 |
return rc; |
return rc; |
1426 |
} |
} |
1427 |
|
|
1429 |
/* Receive an key via FINGER from host @host with the user @user. |
/* Receive an key via FINGER from host @host with the user @user. |
1430 |
On success @key contains the key. */ |
On success @key contains the key. */ |
1431 |
int |
int |
1432 |
finger_recvkey (const char *host, const char *user, char *key, int maxkeylen) |
finger_recvkey (const char *host, const char *user, |
1433 |
|
char **r_key, int *r_keylen) |
1434 |
{ |
{ |
1435 |
char buf[128]; |
gpgme_data_t raw; |
1436 |
|
char buf[256]; |
1437 |
int fd, nread; |
int fd, nread; |
1438 |
int start_key = 0; |
int start_key = 0; |
1439 |
int rc=0; |
int rc=0; |
1445 |
sock_write (fd, user, strlen (user)); |
sock_write (fd, user, strlen (user)); |
1446 |
sock_write (fd, "\r\n", 2); |
sock_write (fd, "\r\n", 2); |
1447 |
|
|
1448 |
memset (key, 0, maxkeylen); |
if (gpgme_data_new (&raw)) |
1449 |
while (maxkeylen > 0) { |
BUG (0); |
1450 |
|
|
1451 |
|
for (;;) { |
1452 |
if (sock_getline (fd, buf, sizeof (buf), &nread)) |
if (sock_getline (fd, buf, sizeof (buf), &nread)) |
1453 |
break; |
break; |
1454 |
strcat (buf, "\n"); |
strcat (buf, "\n"); |
1455 |
if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) { |
if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) { |
1456 |
strcat (key, buf); |
gpgme_data_write (raw, buf, nread); |
1457 |
start_key = 1; |
start_key = 1; |
|
maxkeylen -= nread; |
|
1458 |
} |
} |
1459 |
else if (strstr (buf, "END PGP PUBLIC KEY BLOCK" ) && start_key) { |
else if (strstr (buf, "END PGP PUBLIC KEY BLOCK" ) && start_key) { |
1460 |
strcat (key, buf); |
gpgme_data_write (raw, buf, nread); |
1461 |
start_key--; |
start_key--; |
|
maxkeylen -= nread; |
|
1462 |
break; |
break; |
1463 |
} |
} |
1464 |
else if (start_key) { |
else if (start_key) |
1465 |
strcat (key, buf); |
gpgme_data_write (raw, buf, nread); |
|
maxkeylen -= nread; |
|
|
} |
|
1466 |
} |
} |
1467 |
|
|
1468 |
closesocket (fd); |
closesocket (fd); |
1469 |
if (start_key != 0) |
if (start_key != 0) |
1470 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
1471 |
|
*r_key = data_release_and_get_mem (raw, r_keylen); |
1472 |
return rc; |
return rc; |
1473 |
} |
} |
1474 |
|
|
1488 |
} |
} |
1489 |
return 0; |
return 0; |
1490 |
} |
} |
1491 |
|
|
1492 |
|
|
1493 |
|
/* Split the URL @r_keyserver into the host and the port |
1494 |
|
part if possible. */ |
1495 |
|
gpgme_error_t |
1496 |
|
parse_keyserver_url (char **r_keyserver, unsigned short *r_port) |
1497 |
|
{ |
1498 |
|
char *p; |
1499 |
|
char *url = *r_keyserver; |
1500 |
|
|
1501 |
|
/* no port is given so use the default port. */ |
1502 |
|
p = strrchr (url, ':'); |
1503 |
|
if (p == strchr (url, ':')) { |
1504 |
|
int port = port_from_proto (proto_from_URL (url)); |
1505 |
|
if (!port) |
1506 |
|
port = HKP_PORT; |
1507 |
|
*r_port = port; |
1508 |
|
return 0; |
1509 |
|
} |
1510 |
|
/* XXX: remove / in .de/:11371 */ |
1511 |
|
*r_keyserver = substr (url, 0, (p-url)); |
1512 |
|
*r_port = atoi (url+(p-url)+1); |
1513 |
|
free_if_alloc (url); |
1514 |
|
return 0; |
1515 |
|
} |