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; |
557 |
/* Load the keyserver config file @conf. */ |
/* Load the keyserver config file @conf. */ |
558 |
int |
int |
559 |
kserver_load_conf (const char *conf) |
kserver_load_conf (const char *conf) |
560 |
{ |
{ |
|
struct stat statbuf; |
|
561 |
FILE *fp; |
FILE *fp; |
562 |
char buf[1024], *s, *p; |
char buf[1024], *s, *p; |
563 |
char *user = NULL, *pass = NULL; |
char *user = NULL, *pass = NULL; |
652 |
return WPTERR_CONFIG_FILE; |
return WPTERR_CONFIG_FILE; |
653 |
} |
} |
654 |
|
|
|
if (!stat (conf, &statbuf)) |
|
|
conf_timestamp = statbuf.st_mtime; |
|
655 |
return 0; |
return 0; |
656 |
} |
} |
657 |
|
|
660 |
Return value: 0 on success */ |
Return value: 0 on success */ |
661 |
int |
int |
662 |
kserver_connect (const char *hostname, WORD port, int *conn_fd) |
kserver_connect (const char *hostname, WORD port, int *conn_fd) |
663 |
{ |
{ |
|
int rc, fd; |
|
|
DWORD iaddr; |
|
|
char host[128] = {0}; |
|
664 |
struct hostent *hp; |
struct hostent *hp; |
665 |
struct sockaddr_in sock; |
struct sockaddr_in sock; |
666 |
|
char host[128] = {0}; |
667 |
|
DWORD iaddr; |
668 |
|
int rc, fd; |
669 |
|
|
670 |
log_debug ("kserver_connect: %s:%d\r\n", hostname, port); |
log_debug ("kserver_connect: %s:%d\r\n", hostname, port); |
671 |
|
|
675 |
*conn_fd = 0; |
*conn_fd = 0; |
676 |
hostname = skip_type_prefix (hostname); |
hostname = skip_type_prefix (hostname); |
677 |
|
|
678 |
|
if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) |
679 |
|
port = proxy.port; |
680 |
memset (&sock, 0, sizeof (sock)); |
memset (&sock, 0, sizeof (sock)); |
681 |
sock.sin_family = AF_INET; |
sock.sin_family = AF_INET; |
682 |
sock.sin_port = proxy.host? htons (proxy.port) : htons (port); |
sock.sin_port = htons (port); |
683 |
if (proxy.host) |
if (proxy.host) |
684 |
strncpy (host, proxy.host, 127); |
strncpy (host, proxy.host, 127); |
685 |
else |
else |
686 |
strncpy (host, hostname, 127); |
strncpy (host, hostname, 127); |
687 |
|
|
688 |
if ((iaddr = inet_addr (host)) != INADDR_NONE) |
if ((iaddr = inet_addr (host)) != INADDR_NONE) |
689 |
memcpy (&sock.sin_addr, &iaddr, sizeof (iaddr)); |
memcpy (&sock.sin_addr, &iaddr, sizeof (iaddr)); |
690 |
else if ((hp = gethostbyname (host))) { |
else if ((hp = gethostbyname (host))) { |
708 |
return WPTERR_WINSOCK_CONNECT; |
return WPTERR_WINSOCK_CONNECT; |
709 |
} |
} |
710 |
|
|
711 |
if (proxy.proto != PROXY_PROTO_HTTP) { |
if (proxy.proto == PROXY_PROTO_SOCKS5) { |
712 |
rc = socks_handshake (&proxy, fd, hostname, port); |
rc = socks_handshake (&proxy, fd, hostname, port); |
713 |
if (rc) { |
if (rc) { |
714 |
closesocket (fd); |
closesocket (fd); |
778 |
if (!port) |
if (!port) |
779 |
port = HKP_PORT; |
port = HKP_PORT; |
780 |
reqlen = 512 + strlen (hostname) + 2*strlen (pubkey); |
reqlen = 512 + strlen (hostname) + 2*strlen (pubkey); |
781 |
|
if (proxy.proto == PROXY_PROTO_HTTP && proxy.base64_user) |
782 |
|
reqlen += strlen (proxy.base64_user) + 1; |
783 |
request = new char[reqlen]; |
request = new char[reqlen]; |
784 |
if (!request) |
if (!request) |
785 |
BUG (0); |
BUG (0); |
800 |
"Content-length: %d\r\n" |
"Content-length: %d\r\n" |
801 |
"\r\n" |
"\r\n" |
802 |
"keytext=%s" |
"keytext=%s" |
803 |
"\n", |
"\r\n", |
804 |
skip_type_prefix (hostname), port, hostname, port, |
skip_type_prefix (hostname), port, hostname, port, |
805 |
proxy.base64_user, enc_octets+9, enc_pubkey); |
proxy.base64_user, enc_octets+9, enc_pubkey); |
806 |
} |
} |
814 |
"Content-length: %d\r\n" |
"Content-length: %d\r\n" |
815 |
"\r\n" |
"\r\n" |
816 |
"keytext=%s" |
"keytext=%s" |
817 |
"\n", |
"\r\n", |
818 |
skip_type_prefix (hostname), port, |
skip_type_prefix (hostname), port, |
819 |
enc_octets+9, enc_pubkey); |
enc_octets+9, enc_pubkey); |
820 |
} |
} |
876 |
|
|
877 |
if (r_keylen) |
if (r_keylen) |
878 |
*r_keylen = n; |
*r_keylen = n; |
879 |
log_debug("%s\r\n", *r_key); |
log_debug ("%s\r\n", *r_key); |
880 |
rc = check_hkp_response (*r_key, 1); |
rc = check_hkp_response (*r_key, 1); |
881 |
if (rc) |
if (rc) |
882 |
goto leave; |
goto leave; |
918 |
kserver_sendkey (const char *hostname, WORD port, const char *pubkey, int len ) |
kserver_sendkey (const char *hostname, WORD port, const char *pubkey, int len ) |
919 |
{ |
{ |
920 |
char *request = NULL; |
char *request = NULL; |
921 |
char log[2048]; |
char log[2048] = {0}; |
922 |
int conn_fd, n; |
int conn_fd, n; |
923 |
int rc; |
int rc; |
924 |
|
|
945 |
goto leave; |
goto leave; |
946 |
} |
} |
947 |
|
|
948 |
log_debug ("kserver_sendkey:\r\n%s\r\n", log); |
log_debug ("kserver_sendkey: read %d bytes\r\n%s\r\n", n, log); |
949 |
rc = check_hkp_response (log, 0); |
rc = check_hkp_response (log, 0); |
950 |
if (rc) |
if (rc) |
951 |
goto leave; |
goto leave; |
959 |
} |
} |
960 |
|
|
961 |
|
|
962 |
|
/* Check keyserver response. */ |
963 |
|
static int |
964 |
|
kserver_search_chkresp (int fd) |
965 |
|
{ |
966 |
|
char buf[128]; |
967 |
|
int n=0; |
968 |
|
|
969 |
|
/* parse response 'HTTP/1.0 500 OK' */ |
970 |
|
if (sock_getline (fd, buf, 127, &n)) |
971 |
|
return WPTERR_KEYSERVER_NOTFOUND; |
972 |
|
|
973 |
|
log_debug ("kserver_search_chkpresp: %s\r\n", buf); |
974 |
|
if (strncmp (buf, "HTTP/1.", 7)) |
975 |
|
return WPTERR_KEYSERVER_NOTFOUND; |
976 |
|
if (strncmp (buf+(8+1), "200", 3)) |
977 |
|
return WPTERR_KEYSERVER_NOTFOUND; |
978 |
|
return 0; |
979 |
|
} |
980 |
|
|
981 |
|
|
982 |
|
/* End the keyserver search procedure. */ |
983 |
|
void |
984 |
|
kserver_search_end (int conn_fd) |
985 |
|
{ |
986 |
|
log_debug ("kserver_search_end: fd=%d\r\n", conn_fd); |
987 |
|
closesocket (conn_fd); |
988 |
|
} |
989 |
|
|
990 |
|
|
991 |
|
/* Begin keyserver search procedure. */ |
992 |
int |
int |
993 |
kserver_search_init (const char *hostname, WORD port, |
kserver_search_begin (const char *hostname, WORD port, |
994 |
const char *keyid, int *conn_fd) |
const char *pattern, int *conn_fd) |
995 |
{ |
{ |
996 |
char *request = NULL; |
char *request = NULL; |
997 |
char *enc_keyid = NULL; |
char *enc_patt = NULL; |
998 |
int n = 0; |
int n; |
999 |
int rc, sock_fd; |
int rc, sock_fd; |
1000 |
|
|
1001 |
rc = kserver_connect (hostname, port, &sock_fd); |
rc = kserver_connect (hostname, port, &sock_fd); |
1004 |
goto leave; |
goto leave; |
1005 |
} |
} |
1006 |
|
|
1007 |
enc_keyid = URL_encode (keyid, strlen (keyid), NULL); |
enc_patt = URL_encode (pattern, strlen (pattern), NULL); |
1008 |
n=300; |
n = 140 + strlen (enc_patt) + strlen (hostname) + 32 + 2; |
1009 |
|
if (proxy.base64_user) |
1010 |
|
n += strlen (proxy.base64_user) + 1; |
1011 |
request = new char[n+1]; |
request = new char[n+1]; |
1012 |
if (!request) |
if (!request) |
1013 |
BUG (0); |
BUG (0); |
1016 |
_snprintf (request, n, |
_snprintf (request, n, |
1017 |
"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" |
1018 |
"Proxy-Authorization: Basic %s\r\n\r\n", |
"Proxy-Authorization: Basic %s\r\n\r\n", |
1019 |
skip_type_prefix (hostname), port, enc_keyid, proxy.base64_user); |
skip_type_prefix (hostname), port, enc_patt, proxy.base64_user); |
1020 |
} |
} |
1021 |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
1022 |
_snprintf (request, n, |
_snprintf (request, n, |
1023 |
"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", |
1024 |
skip_type_prefix (hostname), port, enc_keyid); |
skip_type_prefix (hostname), port, enc_patt); |
1025 |
} |
} |
1026 |
else { |
else { |
1027 |
_snprintf (request, n, |
_snprintf (request, n, |
1028 |
"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", |
1029 |
enc_keyid); |
enc_patt); |
1030 |
} |
} |
1031 |
|
|
1032 |
log_debug ("kserver_search_init:\r\n%s\r\n", request); |
log_debug ("kserver_search_begin:\r\n%s\r\n", request); |
1033 |
|
|
1034 |
if (sock_write (sock_fd, request, strlen (request)) == SOCKET_ERROR) { |
if (sock_write (sock_fd, request, strlen (request)) == SOCKET_ERROR) { |
1035 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1036 |
goto leave; |
goto leave; |
1037 |
} |
} |
1038 |
|
|
1039 |
|
rc = kserver_search_chkresp (sock_fd); |
1040 |
|
if (rc) { |
1041 |
|
closesocket (sock_fd); |
1042 |
|
sock_fd = 0; |
1043 |
|
} |
1044 |
|
|
1045 |
*conn_fd = sock_fd; |
*conn_fd = sock_fd; |
1046 |
|
|
1047 |
leave: |
leave: |
1048 |
free_if_alloc (request); |
free_if_alloc (request); |
1049 |
free_if_alloc (enc_keyid); |
free_if_alloc (enc_patt); |
1050 |
return rc; |
return rc; |
1051 |
} |
} |
1052 |
|
|
1053 |
|
|
|
/* 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; |
|
|
} |
|
1054 |
|
|
1055 |
|
|
1056 |
/* Convert an iso date @iso_date (YYYY-MM-DD) into the locale |
/* Convert an iso date @iso_date (YYYY-MM-DD) into the locale |
1085 |
|
|
1086 |
|
|
1087 |
int |
int |
1088 |
kserver_search (int fd, keyserver_key *key) |
kserver_search_next (int fd, keyserver_key *key) |
1089 |
{ |
{ |
1090 |
char buf[1024], *p; |
char buf[1024], *p; |
1091 |
int uidlen, nbytes, pos = 0; |
int uidlen, nbytes, pos = 0; |
1092 |
|
|
1093 |
log_debug ("keyserver_search:\r\n"); |
log_debug ("keyserver_search_next:\r\n"); |
1094 |
|
|
1095 |
if (sock_getline (fd, buf, sizeof (buf) - 1, &nbytes)) |
if (sock_getline (fd, buf, sizeof (buf) - 1, &nbytes)) |
1096 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
1097 |
|
|
1098 |
|
/* XXX: use maschine readable option. */ |
1099 |
log_debug ("%s\r\n", buf); |
log_debug ("%s\r\n", buf); |
1100 |
|
|
1101 |
if (!strncmp (buf, "pub", 3)) { |
if (!strncmp (buf, "pub", 3)) { |
1163 |
free_if_alloc (ctx->host); |
free_if_alloc (ctx->host); |
1164 |
free_if_alloc (ctx->pass); |
free_if_alloc (ctx->pass); |
1165 |
free_if_alloc (ctx->user); |
free_if_alloc (ctx->user); |
1166 |
|
ctx->port = ctx->proto = 0; |
1167 |
} |
} |
1168 |
|
|
1169 |
|
|