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 |
/* 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 ()) |
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", |
62 |
/* Default socket timeout. */ |
/* Default socket timeout. */ |
63 |
static int default_socket_timeout = 6; |
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 |
81 |
base64_encode (const char *inbuf, char *outbuf) |
base64_encode (const char *inbuf, char *outbuf) |
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)); |
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); |
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; |
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 |
} |
} |
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], *p; |
char numbuf[5], *p; |
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); |
|
|
if (proxy.proto == PROXY_PROTO_HTTP && proxy.base64_user) |
|
|
reqlen += strlen (proxy.base64_user) + 1; |
|
|
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 |
"\r\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 |
"\r\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 |
|
|
879 |
kserver_recvkey (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; |
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) { |
928 |
goto leave; |
goto leave; |
929 |
} |
} |
930 |
|
|
931 |
log_debug ("%s\r\n", *r_key); |
log_debug ("response:\r\n%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; |
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) |
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, '\\'); |
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 |
fp = NULL; |
fp = NULL; |
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 |
|
} |