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 |
/* Basic64 encode the input @inbuf to @outbuf. */ |
/* Basic64 encode the input @inbuf to @outbuf. */ |
67 |
static void |
static void |
211 |
DWORD nread; |
DWORD nread; |
212 |
int nleft = buflen; |
int nleft = buflen; |
213 |
int rc, n = 0; |
int rc, n = 0; |
214 |
|
|
215 |
|
if (nbytes) |
216 |
|
*nbytes = 0; |
217 |
while (nleft > 0) { |
while (nleft > 0) { |
218 |
if (n >= default_socket_timeout) |
if (n >= default_socket_timeout) |
219 |
return WPTERR_WINSOCK_TIMEOUT; |
return WPTERR_WINSOCK_TIMEOUT; |
240 |
return 0; |
return 0; |
241 |
} |
} |
242 |
|
|
243 |
|
|
244 |
|
/* Helper to create a string from the gpgme data and |
245 |
|
then release the gpgme data object. */ |
246 |
|
char* |
247 |
|
data_release_and_get_mem (gpgme_data_t in, int *r_bufferlen) |
248 |
|
{ |
249 |
|
size_t n; |
250 |
|
char *buffer, *p; |
251 |
|
|
252 |
|
gpg_data_putc (in, '\0'); |
253 |
|
p = gpgme_data_release_and_get_mem (in, &n); |
254 |
|
buffer = m_strdup (p); |
255 |
|
if (r_bufferlen) |
256 |
|
*r_bufferlen = (int)n; |
257 |
|
gpgme_free (p); |
258 |
|
return buffer; |
259 |
|
} |
260 |
|
|
261 |
|
|
262 |
/* Read much data as possible from the socket @fd and |
/* Read much data as possible from the socket @fd and |
263 |
return the data in @buffer. Caller must free data. |
return the data in @buffer. Caller must free data. |
264 |
Return value: 0 on success. */ |
Return value: 0 on success. */ |
266 |
sock_read_ext (int fd, char **buffer, int *r_bufferlen) |
sock_read_ext (int fd, char **buffer, int *r_bufferlen) |
267 |
{ |
{ |
268 |
gpgme_data_t dh; |
gpgme_data_t dh; |
269 |
char buf[1024], *p; |
char buf[1024]; |
270 |
size_t n=0; |
size_t n=0; |
271 |
int nread, rc; |
int nread, rc; |
272 |
|
|
273 |
gpgme_data_new (&dh); |
if (gpgme_data_new (&dh)) |
274 |
|
BUG (0); |
275 |
while (n < 10) { |
while (n < 10) { |
276 |
rc = sock_select (fd, 1); |
rc = sock_select (fd, 1); |
277 |
if (rc == SOCKET_ERROR) { |
if (rc == SOCKET_ERROR) { |
289 |
gpgme_data_write (dh, buf, nread); |
gpgme_data_write (dh, buf, nread); |
290 |
} |
} |
291 |
} |
} |
292 |
gpg_data_putc (dh, '\0'); |
|
293 |
p = gpgme_data_release_and_get_mem (dh, &n); |
*buffer = data_release_and_get_mem (dh, r_bufferlen); |
|
*buffer = m_strdup (p); |
|
|
if (r_bufferlen) |
|
|
*r_bufferlen = n; |
|
|
gpgme_free (p); |
|
294 |
return 0; |
return 0; |
295 |
} |
} |
296 |
|
|
353 |
/* Return a string representation of a winsock error. */ |
/* Return a string representation of a winsock error. */ |
354 |
const char* |
const char* |
355 |
wsock_strerror (void) |
wsock_strerror (void) |
356 |
{ |
{ |
|
static char buf[384]; |
|
357 |
int ec = WSAGetLastError (); |
int ec = WSAGetLastError (); |
358 |
|
|
359 |
|
if (!ec) |
360 |
|
return ""; |
361 |
switch (ec) { |
switch (ec) { |
362 |
case WSAENETDOWN: |
case WSAENETDOWN: |
363 |
return _("The network subsystem has failed"); |
return _("Network unreachable"); |
364 |
|
|
365 |
|
case WSAEHOSTUNREACH: |
366 |
|
return _("Host unreachable"); |
367 |
|
|
368 |
case WSAHOST_NOT_FOUND: |
case WSAHOST_NOT_FOUND: |
369 |
return _("Authoritative Answer Host not found"); |
return _("Could not resolve host name"); |
370 |
|
|
371 |
|
case WSAECONNREFUSED: |
372 |
|
return _("Connection refused"); |
373 |
|
|
374 |
case WSAETIMEDOUT: |
case WSAETIMEDOUT: |
375 |
return _("The connection has been dropped because of a network failure"); |
case WSAECONNABORTED: |
376 |
|
return _("Connection timeout"); |
377 |
|
|
378 |
|
case WSAENETRESET: |
379 |
|
case WSAECONNRESET: |
380 |
|
return _("Connection resetted by peer"); |
381 |
|
|
382 |
|
case WSAESHUTDOWN: |
383 |
|
return _("Socket has been shutdown"); |
384 |
|
|
385 |
default: |
default: |
386 |
_snprintf (buf, sizeof (buf) -1, _("Unknown Winsock error ec=%d"),ec); |
break; |
|
return buf; |
|
387 |
} |
} |
388 |
return NULL; |
|
389 |
|
return ""; |
390 |
} |
} |
391 |
|
|
392 |
|
|
529 |
if (hostname != NULL) { |
if (hostname != NULL) { |
530 |
free_if_alloc (default_keyserver); |
free_if_alloc (default_keyserver); |
531 |
default_keyserver = m_strdup (hostname); |
default_keyserver = m_strdup (hostname); |
|
if (!default_keyserver) |
|
|
BUG (0); |
|
532 |
default_keyserver_port = port; |
default_keyserver_port = port; |
533 |
} |
} |
534 |
if (!port) |
if (!port) |
589 |
/* Load the keyserver config file @conf. */ |
/* Load the keyserver config file @conf. */ |
590 |
int |
int |
591 |
kserver_load_conf (const char *conf) |
kserver_load_conf (const char *conf) |
592 |
{ |
{ |
|
struct stat statbuf; |
|
593 |
FILE *fp; |
FILE *fp; |
594 |
char buf[1024], *s, *p; |
char buf[1024], *s, *p; |
595 |
char *user = NULL, *pass = NULL; |
char *user = NULL, *pass = NULL; |
622 |
/* check if the host has a http:// prefix and skip it */ |
/* check if the host has a http:// prefix and skip it */ |
623 |
if (proxy.host && !strncmp (proxy.host, "http://", 7)) { |
if (proxy.host && !strncmp (proxy.host, "http://", 7)) { |
624 |
char *host = m_strdup (proxy.host+7); |
char *host = m_strdup (proxy.host+7); |
|
if (!host) |
|
|
BUG (0); |
|
625 |
free_if_alloc (proxy.host); |
free_if_alloc (proxy.host); |
626 |
proxy.host = host; |
proxy.host = host; |
627 |
} |
} |
682 |
return WPTERR_CONFIG_FILE; |
return WPTERR_CONFIG_FILE; |
683 |
} |
} |
684 |
|
|
|
if (!stat (conf, &statbuf)) |
|
|
conf_timestamp = statbuf.st_mtime; |
|
685 |
return 0; |
return 0; |
686 |
} |
} |
687 |
|
|
690 |
Return value: 0 on success */ |
Return value: 0 on success */ |
691 |
int |
int |
692 |
kserver_connect (const char *hostname, WORD port, int *conn_fd) |
kserver_connect (const char *hostname, WORD port, int *conn_fd) |
693 |
{ |
{ |
|
int rc, fd; |
|
|
DWORD iaddr; |
|
|
char host[128] = {0}; |
|
694 |
struct hostent *hp; |
struct hostent *hp; |
695 |
struct sockaddr_in sock; |
struct sockaddr_in sock; |
696 |
|
char host[128] = {0}; |
697 |
|
DWORD iaddr; |
698 |
|
int rc, fd; |
699 |
|
|
700 |
log_debug ("kserver_connect: %s:%d\r\n", hostname, port); |
log_debug ("kserver_connect: %s:%d\r\n", hostname, port); |
701 |
|
|
705 |
*conn_fd = 0; |
*conn_fd = 0; |
706 |
hostname = skip_type_prefix (hostname); |
hostname = skip_type_prefix (hostname); |
707 |
|
|
708 |
|
if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) |
709 |
|
port = proxy.port; |
710 |
memset (&sock, 0, sizeof (sock)); |
memset (&sock, 0, sizeof (sock)); |
711 |
sock.sin_family = AF_INET; |
sock.sin_family = AF_INET; |
712 |
sock.sin_port = proxy.host? htons (proxy.port) : htons (port); |
sock.sin_port = htons (port); |
713 |
if (proxy.host) |
if (proxy.host) |
714 |
strncpy (host, proxy.host, 127); |
strncpy (host, proxy.host, 127); |
715 |
else |
else |
716 |
strncpy (host, hostname, 127); |
strncpy (host, hostname, 127); |
717 |
|
|
718 |
if ((iaddr = inet_addr (host)) != INADDR_NONE) |
if ((iaddr = inet_addr (host)) != INADDR_NONE) |
719 |
memcpy (&sock.sin_addr, &iaddr, sizeof (iaddr)); |
memcpy (&sock.sin_addr, &iaddr, sizeof (iaddr)); |
720 |
else if ((hp = gethostbyname (host))) { |
else if ((hp = gethostbyname (host))) { |
738 |
return WPTERR_WINSOCK_CONNECT; |
return WPTERR_WINSOCK_CONNECT; |
739 |
} |
} |
740 |
|
|
741 |
if (proxy.proto != PROXY_PROTO_HTTP) { |
if (proxy.proto == PROXY_PROTO_SOCKS5) { |
742 |
rc = socks_handshake (&proxy, fd, hostname, port); |
rc = socks_handshake (&proxy, fd, hostname, port); |
743 |
if (rc) { |
if (rc) { |
744 |
closesocket (fd); |
closesocket (fd); |
767 |
URL_encode (const char *url, size_t ulen, size_t *ret_nlen) |
URL_encode (const char *url, size_t ulen, size_t *ret_nlen) |
768 |
{ |
{ |
769 |
gpgme_data_t hd; |
gpgme_data_t hd; |
770 |
char numbuf[5], *pp, *p; |
char numbuf[5], *p; |
771 |
size_t i, n; |
size_t i; |
772 |
|
int nlen; |
773 |
|
|
774 |
gpgme_data_new (&hd); |
if (gpgme_data_new (&hd)) |
775 |
|
BUG (0); |
776 |
for (i=0; i < ulen; i++) { |
for (i=0; i < ulen; i++) { |
777 |
if (isalnum (url[i]) || url[i] == '-') |
if (isalnum (url[i]) || url[i] == '-') |
778 |
gpg_data_putc (hd, url[i]); |
gpg_data_putc (hd, url[i]); |
783 |
gpgme_data_write (hd, numbuf, strlen (numbuf)); |
gpgme_data_write (hd, numbuf, strlen (numbuf)); |
784 |
} |
} |
785 |
} |
} |
|
gpg_data_putc (hd, '\0'); |
|
786 |
|
|
787 |
/* Copy memory to avoid that we need to use gpgme_free later. */ |
p = data_release_and_get_mem (hd, &nlen); |
|
pp = gpgme_data_release_and_get_mem (hd, &n); |
|
|
p = m_strdup (pp); |
|
|
gpgme_free (pp); |
|
788 |
if (ret_nlen) |
if (ret_nlen) |
789 |
*ret_nlen = n; |
*ret_nlen = nlen; |
790 |
return p; |
return p; |
791 |
} |
} |
792 |
|
|
806 |
if (!port) |
if (!port) |
807 |
port = HKP_PORT; |
port = HKP_PORT; |
808 |
reqlen = 512 + strlen (hostname) + 2*strlen (pubkey); |
reqlen = 512 + strlen (hostname) + 2*strlen (pubkey); |
809 |
|
if (proxy.proto == PROXY_PROTO_HTTP && proxy.base64_user) |
810 |
|
reqlen += strlen (proxy.base64_user) + 1; |
811 |
request = new char[reqlen]; |
request = new char[reqlen]; |
812 |
if (!request) |
if (!request) |
813 |
BUG (0); |
BUG (0); |
828 |
"Content-length: %d\r\n" |
"Content-length: %d\r\n" |
829 |
"\r\n" |
"\r\n" |
830 |
"keytext=%s" |
"keytext=%s" |
831 |
"\n", |
"\r\n", |
832 |
skip_type_prefix (hostname), port, hostname, port, |
skip_type_prefix (hostname), port, hostname, port, |
833 |
proxy.base64_user, enc_octets+9, enc_pubkey); |
proxy.base64_user, enc_octets+9, enc_pubkey); |
834 |
} |
} |
842 |
"Content-length: %d\r\n" |
"Content-length: %d\r\n" |
843 |
"\r\n" |
"\r\n" |
844 |
"keytext=%s" |
"keytext=%s" |
845 |
"\n", |
"\r\n", |
846 |
skip_type_prefix (hostname), port, |
skip_type_prefix (hostname), port, |
847 |
enc_octets+9, enc_pubkey); |
enc_octets+9, enc_pubkey); |
848 |
} |
} |
853 |
} |
} |
854 |
|
|
855 |
|
|
856 |
|
/* Interface for receiving a public key. */ |
857 |
int |
int |
858 |
kserver_recvkey_ext (const char *hostname, WORD port, const char *keyid, |
kserver_recvkey (const char *hostname, WORD port, const char *keyid, |
859 |
char **r_key, int *r_keylen) |
char **r_key, int *r_keylen) |
860 |
{ |
{ |
861 |
char *request = NULL; |
char *request = NULL; |
862 |
int conn_fd; |
int conn_fd; |
863 |
int rc, n; |
int rc; |
864 |
|
|
865 |
if (!port) |
if (!port) |
866 |
port = HKP_PORT; |
port = HKP_PORT; |
897 |
goto leave; |
goto leave; |
898 |
} |
} |
899 |
|
|
900 |
rc = sock_read_ext (conn_fd, r_key, &n); |
rc = sock_read_ext (conn_fd, r_key, r_keylen); |
901 |
if (rc == SOCKET_ERROR) { |
if (rc == SOCKET_ERROR) { |
902 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
903 |
goto leave; |
goto leave; |
904 |
} |
} |
905 |
|
|
906 |
if (r_keylen) |
log_debug ("%s\r\n", *r_key); |
|
*r_keylen = n; |
|
|
log_debug("%s\r\n", *r_key); |
|
907 |
rc = check_hkp_response (*r_key, 1); |
rc = check_hkp_response (*r_key, 1); |
908 |
if (rc) |
if (rc) |
909 |
goto leave; |
goto leave; |
916 |
return rc; |
return rc; |
917 |
} |
} |
918 |
|
|
|
/* Interface for receiving a public key. */ |
|
|
int |
|
|
kserver_recvkey (const char *hostname, WORD port, const char *keyid, |
|
|
char *key, int maxkeylen) |
|
|
{ |
|
|
char *tmpkey; |
|
|
int rc, nread=0; |
|
|
|
|
|
/* XXX: just a wrapper around the new method, replace it |
|
|
soon as possible. */ |
|
|
rc = kserver_recvkey_ext (hostname, port, keyid, &tmpkey, &nread); |
|
|
if (rc) |
|
|
return rc; |
|
|
|
|
|
if (nread > maxkeylen) { |
|
|
free_if_alloc (tmpkey); |
|
|
return WPTERR_GENERAL; |
|
|
} |
|
|
strcpy (key, tmpkey); |
|
|
free_if_alloc (tmpkey); |
|
|
return 0; |
|
|
} |
|
|
|
|
919 |
|
|
920 |
/* Interface to send a public key. */ |
/* Interface to send a public key. */ |
921 |
int |
int |
922 |
kserver_sendkey (const char *hostname, WORD port, const char *pubkey, int len ) |
kserver_sendkey (const char *hostname, WORD port, const char *pubkey, int len ) |
923 |
{ |
{ |
924 |
char *request = NULL; |
char *request = NULL; |
925 |
char log[2048]; |
char log[2048] = {0}; |
926 |
int conn_fd, n; |
int conn_fd, n; |
927 |
int rc; |
int rc; |
928 |
|
|
949 |
goto leave; |
goto leave; |
950 |
} |
} |
951 |
|
|
952 |
log_debug ("kserver_sendkey:\r\n%s\r\n", log); |
log_debug ("kserver_sendkey: read %d bytes\r\n%s\r\n", n, log); |
953 |
rc = check_hkp_response (log, 0); |
rc = check_hkp_response (log, 0); |
954 |
if (rc) |
if (rc) |
955 |
goto leave; |
goto leave; |
963 |
} |
} |
964 |
|
|
965 |
|
|
966 |
|
/* Check keyserver response. */ |
967 |
|
static int |
968 |
|
kserver_search_chkresp (int fd) |
969 |
|
{ |
970 |
|
char buf[128]; |
971 |
|
int n=0; |
972 |
|
|
973 |
|
/* parse response 'HTTP/1.0 500 OK' */ |
974 |
|
if (sock_getline (fd, buf, 127, &n)) |
975 |
|
return WPTERR_KEYSERVER_NOTFOUND; |
976 |
|
|
977 |
|
log_debug ("kserver_search_chkpresp: %s\r\n", buf); |
978 |
|
if (strncmp (buf, "HTTP/1.", 7)) |
979 |
|
return WPTERR_KEYSERVER_NOTFOUND; |
980 |
|
if (strncmp (buf+(8+1), "200", 3)) |
981 |
|
return WPTERR_KEYSERVER_NOTFOUND; |
982 |
|
return 0; |
983 |
|
} |
984 |
|
|
985 |
|
|
986 |
|
/* End the keyserver search procedure. */ |
987 |
|
void |
988 |
|
kserver_search_end (int conn_fd) |
989 |
|
{ |
990 |
|
log_debug ("kserver_search_end: fd=%d\r\n", conn_fd); |
991 |
|
closesocket (conn_fd); |
992 |
|
} |
993 |
|
|
994 |
|
|
995 |
|
/* Begin keyserver search procedure. */ |
996 |
int |
int |
997 |
kserver_search_init (const char *hostname, WORD port, |
kserver_search_begin (const char *hostname, WORD port, |
998 |
const char *keyid, int *conn_fd) |
const char *pattern, int *conn_fd) |
999 |
{ |
{ |
1000 |
char *request = NULL; |
char *request = NULL; |
1001 |
char *enc_keyid = NULL; |
char *enc_patt = NULL; |
1002 |
int n = 0; |
int n; |
1003 |
int rc, sock_fd; |
int rc, sock_fd; |
1004 |
|
|
1005 |
rc = kserver_connect (hostname, port, &sock_fd); |
rc = kserver_connect (hostname, port, &sock_fd); |
1008 |
goto leave; |
goto leave; |
1009 |
} |
} |
1010 |
|
|
1011 |
enc_keyid = URL_encode (keyid, strlen (keyid), NULL); |
enc_patt = URL_encode (pattern, strlen (pattern), NULL); |
1012 |
n=300; |
n = 140 + strlen (enc_patt) + strlen (hostname) + 32 + 2; |
1013 |
|
if (proxy.base64_user) |
1014 |
|
n += strlen (proxy.base64_user) + 1; |
1015 |
request = new char[n+1]; |
request = new char[n+1]; |
1016 |
if (!request) |
if (!request) |
1017 |
BUG (0); |
BUG (0); |
1020 |
_snprintf (request, n, |
_snprintf (request, n, |
1021 |
"GET http://%s:%d/pks/lookup?op=index&search=%s HTTP/1.0\r\n" |
"GET http://%s:%d/pks/lookup?op=index&search=%s HTTP/1.0\r\n" |
1022 |
"Proxy-Authorization: Basic %s\r\n\r\n", |
"Proxy-Authorization: Basic %s\r\n\r\n", |
1023 |
skip_type_prefix (hostname), port, enc_keyid, proxy.base64_user); |
skip_type_prefix (hostname), port, enc_patt, proxy.base64_user); |
1024 |
} |
} |
1025 |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
1026 |
_snprintf (request, n, |
_snprintf (request, n, |
1027 |
"GET http://%s:%d/pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n", |
"GET http://%s:%d/pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n", |
1028 |
skip_type_prefix (hostname), port, enc_keyid); |
skip_type_prefix (hostname), port, enc_patt); |
1029 |
} |
} |
1030 |
else { |
else { |
1031 |
_snprintf (request, n, |
_snprintf (request, n, |
1032 |
"GET /pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n", |
"GET /pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n", |
1033 |
enc_keyid); |
enc_patt); |
1034 |
} |
} |
1035 |
|
|
1036 |
log_debug ("kserver_search_init:\r\n%s\r\n", request); |
log_debug ("kserver_search_begin:\r\n%s\r\n", request); |
1037 |
|
|
1038 |
if (sock_write (sock_fd, request, strlen (request)) == SOCKET_ERROR) { |
if (sock_write (sock_fd, request, strlen (request)) == SOCKET_ERROR) { |
1039 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1040 |
goto leave; |
goto leave; |
1041 |
} |
} |
1042 |
|
|
1043 |
|
rc = kserver_search_chkresp (sock_fd); |
1044 |
|
if (rc) { |
1045 |
|
closesocket (sock_fd); |
1046 |
|
sock_fd = 0; |
1047 |
|
} |
1048 |
|
|
1049 |
*conn_fd = sock_fd; |
*conn_fd = sock_fd; |
1050 |
|
|
1051 |
leave: |
leave: |
1052 |
free_if_alloc (request); |
free_if_alloc (request); |
1053 |
free_if_alloc (enc_keyid); |
free_if_alloc (enc_patt); |
1054 |
return rc; |
return rc; |
1055 |
} |
} |
1056 |
|
|
1057 |
|
|
|
/* Check keyserver response. */ |
|
|
int |
|
|
kserver_search_chkresp (int fd) |
|
|
{ |
|
|
char buf[128]; |
|
|
int n=0; |
|
|
|
|
|
/* parse response 'HTTP/1.0 500 OK' */ |
|
|
if (sock_getline (fd, buf, 127, &n)) |
|
|
return WPTERR_KEYSERVER_NOTFOUND; |
|
|
|
|
|
log_debug ("kserver_search_chkpresp: %s\r\n", buf); |
|
|
if (strncmp (buf, "HTTP/1.", 7)) |
|
|
return WPTERR_KEYSERVER_NOTFOUND; |
|
|
if (strncmp (buf+(8+1), "200", 3)) |
|
|
return WPTERR_KEYSERVER_NOTFOUND; |
|
|
return 0; |
|
|
} |
|
1058 |
|
|
1059 |
|
|
1060 |
/* Convert an iso date @iso_date (YYYY-MM-DD) into the locale |
/* Convert an iso date @iso_date (YYYY-MM-DD) into the locale |
1089 |
|
|
1090 |
|
|
1091 |
int |
int |
1092 |
kserver_search (int fd, keyserver_key *key) |
kserver_search_next (int fd, keyserver_key *key) |
1093 |
{ |
{ |
1094 |
char buf[1024], *p; |
char buf[1024], *p; |
1095 |
int uidlen, nbytes, pos = 0; |
int uidlen, nbytes, pos = 0; |
1096 |
|
|
1097 |
log_debug ("keyserver_search:\r\n"); |
log_debug ("keyserver_search_next:\r\n"); |
1098 |
|
|
1099 |
if (sock_getline (fd, buf, sizeof (buf) - 1, &nbytes)) |
if (sock_getline (fd, buf, sizeof (buf) - 1, &nbytes)) |
1100 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
1101 |
|
|
1102 |
|
/* XXX: use maschine readable option. */ |
1103 |
log_debug ("%s\r\n", buf); |
log_debug ("%s\r\n", buf); |
1104 |
|
|
1105 |
if (!strncmp (buf, "pub", 3)) { |
if (!strncmp (buf, "pub", 3)) { |
1167 |
free_if_alloc (ctx->host); |
free_if_alloc (ctx->host); |
1168 |
free_if_alloc (ctx->pass); |
free_if_alloc (ctx->pass); |
1169 |
free_if_alloc (ctx->user); |
free_if_alloc (ctx->user); |
1170 |
|
ctx->port = ctx->proto = 0; |
1171 |
} |
} |
1172 |
|
|
1173 |
|
|
1198 |
} |
} |
1199 |
|
|
1200 |
|
|
1201 |
/* Receive an key via LDAP from host @host with the keyid @keyid. |
static FILE* |
1202 |
@key contains the key on success. */ |
do_spawn_ldap_helper (const char *host, const char *keyid) |
1203 |
int |
{ |
1204 |
ldap_recvkey (const char *host, const char *keyid, char *key, int maxkeylen) |
FILE *fp = NULL; |
1205 |
{ |
char *p, *sep; |
1206 |
FILE *fp; |
char *ksprg; |
1207 |
const char *s; |
char outf[256], inf[256]; |
1208 |
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; |
|
1209 |
|
|
1210 |
p = get_gnupg_prog (); |
p = get_gnupg_prog (); |
1211 |
n = strlen (p) + 1 + 128; |
n = strlen (p) + 1 + 128; |
1221 |
if (file_exist_check (ksprg)) { |
if (file_exist_check (ksprg)) { |
1222 |
log_box ("LDAP Keyserver Plugin", MB_ERR, |
log_box ("LDAP Keyserver Plugin", MB_ERR, |
1223 |
"%s: could not find LDAP keyserver module!", ksprg); |
"%s: could not find LDAP keyserver module!", ksprg); |
1224 |
rc = -1; |
fp = NULL; |
1225 |
goto leave; |
goto leave; |
1226 |
} |
} |
1227 |
get_temp_name (outf, sizeof (outf)-1, keyid); |
get_temp_name (outf, sizeof (outf)-1, keyid); |
1230 |
if (!fp) { |
if (!fp) { |
1231 |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", inf, |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", inf, |
1232 |
winpt_strerror (WPTERR_FILE_OPEN)); |
winpt_strerror (WPTERR_FILE_OPEN)); |
|
rc = -1; |
|
1233 |
goto leave; |
goto leave; |
1234 |
} |
} |
1235 |
fprintf (fp, |
fprintf (fp, |
1248 |
BUG (NULL); |
BUG (NULL); |
1249 |
sprintf (p, "%s -o %s %s", ksprg, outf, inf); |
sprintf (p, "%s -o %s %s", ksprg, outf, inf); |
1250 |
if (spawn_application (p)) { |
if (spawn_application (p)) { |
1251 |
rc = -1; |
fp = NULL; |
1252 |
goto leave; |
goto leave; |
1253 |
} |
} |
|
|
|
1254 |
fp = fopen (outf, "rb"); |
fp = fopen (outf, "rb"); |
1255 |
if (!fp) { |
if (!fp) |
1256 |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf, |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf, |
1257 |
winpt_strerror (WPTERR_FILE_OPEN)); |
winpt_strerror (WPTERR_FILE_OPEN)); |
1258 |
rc = -1; |
|
1259 |
goto leave; |
leave: |
1260 |
} |
DeleteFile (inf); |
1261 |
memset (key, 0, maxkeylen); |
DeleteFile (outf); |
1262 |
|
free_if_alloc (p); |
1263 |
|
free_if_alloc (ksprg); |
1264 |
|
return fp; |
1265 |
|
} |
1266 |
|
|
1267 |
|
/* Receive an key via LDAP from host @host with the keyid @keyid. |
1268 |
|
@key contains the key on success. */ |
1269 |
|
int |
1270 |
|
ldap_recvkey (const char *host, const char *keyid, |
1271 |
|
char **r_key, int *r_keylen) |
1272 |
|
{ |
1273 |
|
gpgme_data_t raw; |
1274 |
|
FILE *fp; |
1275 |
|
const char *s; |
1276 |
|
char buf[512]; |
1277 |
|
int start_key = 0, failed = 0; |
1278 |
|
int rc = 0; |
1279 |
|
|
1280 |
|
fp = do_spawn_ldap_helper (host, keyid); |
1281 |
|
if (!fp) |
1282 |
|
return WPTERR_GENERAL; |
1283 |
|
|
1284 |
|
if (gpgme_data_new (&raw)) |
1285 |
|
BUG (0); |
1286 |
while (!feof (fp)) { |
while (!feof (fp)) { |
1287 |
s = fgets (buf, sizeof (buf)-1, fp); |
s = fgets (buf, sizeof (buf)-1, fp); |
1288 |
if (!s) |
if (!s) |
1294 |
if (!start_key && strstr (s, "KEY") && strstr (s, "BEGIN")) { |
if (!start_key && strstr (s, "KEY") && strstr (s, "BEGIN")) { |
1295 |
start_key = 1; |
start_key = 1; |
1296 |
continue; |
continue; |
1297 |
} |
} |
1298 |
if (!start_key) |
if (!start_key) |
1299 |
continue; |
continue; |
1300 |
strcat (key, buf); |
gpgme_data_write (raw, buf, strlen (buf)); |
1301 |
maxkeylen -= strlen (buf); |
if (strstr (s, "KEY") && strstr (s, "END")) |
|
if (maxkeylen < 0 || (strstr (s, "KEY") && strstr (s, "END"))) |
|
1302 |
break; |
break; |
1303 |
} |
} |
1304 |
fclose (fp); |
fclose (fp); |
1305 |
|
|
1306 |
leave: |
if (failed) |
|
if (failed || !strlen (key)) |
|
1307 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
1308 |
DeleteFile (inf); |
*r_key = data_release_and_get_mem (raw, r_keylen); |
|
DeleteFile (outf); |
|
|
free_if_alloc (p); |
|
|
free_if_alloc (ksprg); |
|
1309 |
return rc; |
return rc; |
1310 |
} |
} |
1311 |
|
|
1313 |
/* Receive an key via FINGER from host @host with the user @user. |
/* Receive an key via FINGER from host @host with the user @user. |
1314 |
On success @key contains the key. */ |
On success @key contains the key. */ |
1315 |
int |
int |
1316 |
finger_recvkey (const char *host, const char *user, char *key, int maxkeylen) |
finger_recvkey (const char *host, const char *user, |
1317 |
|
char **r_key, int *r_keylen) |
1318 |
{ |
{ |
1319 |
char buf[128]; |
gpgme_data_t raw; |
1320 |
|
char buf[256]; |
1321 |
int fd, nread; |
int fd, nread; |
1322 |
int start_key = 0; |
int start_key = 0; |
1323 |
int rc=0; |
int rc=0; |
1329 |
sock_write (fd, user, strlen (user)); |
sock_write (fd, user, strlen (user)); |
1330 |
sock_write (fd, "\r\n", 2); |
sock_write (fd, "\r\n", 2); |
1331 |
|
|
1332 |
memset (key, 0, maxkeylen); |
if (gpgme_data_new (&raw)) |
1333 |
while (maxkeylen > 0) { |
BUG (0); |
1334 |
|
|
1335 |
|
for (;;) { |
1336 |
if (sock_getline (fd, buf, sizeof (buf), &nread)) |
if (sock_getline (fd, buf, sizeof (buf), &nread)) |
1337 |
break; |
break; |
1338 |
strcat (buf, "\n"); |
strcat (buf, "\n"); |
1339 |
if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) { |
if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) { |
1340 |
strcat (key, buf); |
gpgme_data_write (raw, buf, nread); |
1341 |
start_key = 1; |
start_key = 1; |
|
maxkeylen -= nread; |
|
1342 |
} |
} |
1343 |
else if (strstr (buf, "END PGP PUBLIC KEY BLOCK" ) && start_key) { |
else if (strstr (buf, "END PGP PUBLIC KEY BLOCK" ) && start_key) { |
1344 |
strcat (key, buf); |
gpgme_data_write (raw, buf, nread); |
1345 |
start_key--; |
start_key--; |
|
maxkeylen -= nread; |
|
1346 |
break; |
break; |
1347 |
} |
} |
1348 |
else if (start_key) { |
else if (start_key) |
1349 |
strcat (key, buf); |
gpgme_data_write (raw, buf, nread); |
|
maxkeylen -= nread; |
|
|
} |
|
1350 |
} |
} |
1351 |
|
|
1352 |
closesocket (fd); |
closesocket (fd); |
1353 |
if (start_key != 0) |
if (start_key != 0) |
1354 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
1355 |
|
*r_key = data_release_and_get_mem (raw, r_keylen); |
1356 |
return rc; |
return rc; |
1357 |
} |
} |
1358 |
|
|