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", |
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 |
|
|
163 |
|
/* Parse the keyserver response and extract the human |
164 |
|
readable text passages. */ |
165 |
|
static int |
166 |
|
parse_keyserver_error (const char *resp, char *txt, size_t txtlen) |
167 |
|
{ |
168 |
|
char *p, *p2; |
169 |
|
|
170 |
|
/* If we find no 'Error' substring, we assume a success. */ |
171 |
|
if (!stristr (resp, "Error")) |
172 |
|
return 0; |
173 |
|
|
174 |
|
memset (txt, 0, txtlen); |
175 |
|
p = strstr (resp, "\r\n\r\n"); |
176 |
|
if (!p) |
177 |
|
return -1; |
178 |
|
resp += (p-resp); |
179 |
|
p = strstr (resp, "<h1>"); |
180 |
|
if (!p) |
181 |
|
return -1; |
182 |
|
resp += (p-resp)+strlen ("<h1>"); |
183 |
|
p = strstr (resp, "</h1>"); |
184 |
|
if (!p) |
185 |
|
return -1; |
186 |
|
resp += (p-resp)+strlen ("</h1>"); |
187 |
|
p2 = strstr (resp, "</body>"); |
188 |
|
if (p2 != NULL) |
189 |
|
memcpy (txt, resp, (p2-resp)); |
190 |
|
else { |
191 |
|
if (!strnicmp (resp, "<p>", 3)) |
192 |
|
resp += 3; |
193 |
|
while (resp && *resp == '\r' || *resp == '\n') |
194 |
|
resp++; |
195 |
|
memcpy (txt, resp, strlen (resp)); |
196 |
|
} |
197 |
|
return 0; |
198 |
|
} |
199 |
|
|
200 |
|
|
201 |
/* Check that the keyserver response indicates an OK. |
/* Check that the keyserver response indicates an OK. |
202 |
Return 0 on success. */ |
Return 0 on success. */ |
203 |
static int |
static int |
204 |
check_hkp_response (const char *resp, int recv) |
check_hkp_response (const char *resp, int recv) |
205 |
{ |
{ |
206 |
char *p, *end; |
int ec; |
|
int ec, len; |
|
207 |
|
|
208 |
|
log_debug ("check_hkp_response: '%s'\r\n", resp); |
209 |
ec = recv ? WPTERR_WINSOCK_RECVKEY : WPTERR_WINSOCK_SENDKEY; |
ec = recv ? WPTERR_WINSOCK_RECVKEY : WPTERR_WINSOCK_SENDKEY; |
210 |
if (!strstr (resp, "HTTP/1.0 200 OK") && |
if (!strstr (resp, "HTTP/1.0 200 OK") && |
211 |
!strstr (resp, "HTTP/1.1 200 OK")) /* http error */ |
!strstr (resp, "HTTP/1.1 200 OK") && |
212 |
return ec; |
!strstr (resp, "HTTP/1.0 500 OK") && |
213 |
if (strstr (resp, "Public Key Server -- Error") |
!strstr (resp, "HTTP/1.1 500 OK")) |
214 |
|| strstr (resp, "Public Key Server -- Error") |
return ec; /* http error */ |
215 |
|| strstr (resp, "No matching keys in database")) { |
|
216 |
p = strstr (resp, "<p>"); |
if (parse_keyserver_error (resp, hkp_errmsg, sizeof (hkp_errmsg)-2)) { |
217 |
if (p && strlen (p) < sizeof (hkp_errmsg)) { |
if (!strlen (hkp_errmsg)) |
218 |
end = strstr (p, "</p>"); |
_snprintf (hkp_errmsg, sizeof (hkp_errmsg)-1, |
219 |
len = end? (end - p + 1) : strlen (p); |
"Unknown keyserver error"); |
220 |
memset (hkp_errmsg, 0, sizeof (hkp_errmsg)); |
hkp_err = 1; |
|
strncpy (hkp_errmsg, p, len); |
|
|
hkp_err = 1; |
|
|
} |
|
221 |
return ec; |
return ec; |
222 |
} |
} |
223 |
return 0; |
return 0; |
418 |
} |
} |
419 |
kserver_proxy_release (&proxy); |
kserver_proxy_release (&proxy); |
420 |
WSACleanup (); |
WSACleanup (); |
421 |
|
log_debug ("wsock_end: cleanup finished.\r\n"); |
422 |
} |
} |
423 |
|
|
424 |
|
|
466 |
void |
void |
467 |
kserver_set_socket_timeout (int nsec) |
kserver_set_socket_timeout (int nsec) |
468 |
{ |
{ |
469 |
if (nsec < 0) |
if (nsec < 0 || nsec > 3600) |
470 |
nsec = 0; |
nsec = 0; |
471 |
default_socket_timeout = nsec; |
default_socket_timeout = nsec; |
472 |
} |
} |
492 |
return default_keyserver; |
return default_keyserver; |
493 |
} |
} |
494 |
else if (!type && idx < DIM (server_list)) { |
else if (!type && idx < DIM (server_list)) { |
495 |
|
/* XXX: handle non HKP servers. */ |
496 |
*port = HKP_PORT; |
*port = HKP_PORT; |
497 |
return server_list[idx]; |
return server_list[idx]; |
498 |
} |
} |
511 |
closesocket (fd); |
closesocket (fd); |
512 |
return 0; |
return 0; |
513 |
} |
} |
514 |
|
log_debug ("kserver_check_inet_connection: no inet connection.\r\n"); |
515 |
return -1; |
return -1; |
516 |
} |
} |
517 |
|
|
523 |
const char* |
const char* |
524 |
kserver_check_keyid (const char *keyid) |
kserver_check_keyid (const char *keyid) |
525 |
{ |
{ |
526 |
static char id[21]; |
static char id[22]; |
527 |
|
|
528 |
if (strstr (keyid, "@")) |
if (strstr (keyid, "@")) |
529 |
return keyid; /* email address */ |
return keyid; /* email address */ |
553 |
proxy.user = m_strdup (proxy_user); |
proxy.user = m_strdup (proxy_user); |
554 |
proxy.pass = m_strdup (proxy_pass); |
proxy.pass = m_strdup (proxy_pass); |
555 |
} |
} |
|
|
|
|
|
|
|
/* 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; |
|
|
} |
|
556 |
|
|
557 |
|
|
558 |
/* Get the port number from the given protocol. */ |
/* Get the port number from the given protocol. */ |
560 |
port_from_proto (int proto) |
port_from_proto (int proto) |
561 |
{ |
{ |
562 |
switch (proto) { |
switch (proto) { |
563 |
case KSPROTO_LDAP: return 0; |
case KSPROTO_LDAP: return LDAP_PORT; |
564 |
case KSPROTO_FINGER: return FINGER_PORT; |
case KSPROTO_FINGER: return FINGER_PORT; |
565 |
case KSPROTO_HTTP: return HKP_PORT; |
case KSPROTO_HTTP: return HKP_PORT; |
566 |
} |
} |
574 |
{ |
{ |
575 |
if (strstr (buf, "ldap")) |
if (strstr (buf, "ldap")) |
576 |
return KSPROTO_LDAP; |
return KSPROTO_LDAP; |
577 |
else if (strstr( buf, "finger")) |
else if (strstr (buf, "finger")) |
578 |
return KSPROTO_FINGER; |
return KSPROTO_FINGER; |
579 |
return KSPROTO_HKP; |
return KSPROTO_HKP; |
580 |
} |
} |
581 |
|
|
582 |
|
|
583 |
|
/* Set the default keyserver. |
584 |
|
If @hostname is NULL, use the predefined keyserver. */ |
585 |
void |
void |
586 |
keyserver_set_default (const char *hostname, WORD port) |
keyserver_set_default (const char *hostname, WORD port) |
587 |
{ |
{ |
588 |
|
if (!port) |
589 |
|
port = HKP_PORT; |
590 |
if (hostname != NULL) { |
if (hostname != NULL) { |
591 |
free_if_alloc (default_keyserver); |
free_if_alloc (default_keyserver); |
592 |
default_keyserver = m_strdup (hostname); |
default_keyserver = m_strdup (hostname); |
593 |
default_keyserver_port = port; |
default_keyserver_port = port; |
594 |
} |
} |
595 |
if (!port) |
if (!default_keyserver) { |
|
port = HKP_PORT; |
|
|
if (!default_keyserver) |
|
596 |
default_keyserver = m_strdup (DEF_HKP_KEYSERVER); |
default_keyserver = m_strdup (DEF_HKP_KEYSERVER); |
597 |
|
default_keyserver_port = HKP_PORT; |
598 |
|
} |
599 |
server[0].name = m_strdup (default_keyserver); |
server[0].name = m_strdup (default_keyserver); |
600 |
server[0].used = 1; |
server[0].used = 1; |
601 |
server[0].port = port; |
server[0].port = port; |
701 |
s = (char *)skip_whitespace (buf); |
s = (char *)skip_whitespace (buf); |
702 |
if (*s == '#' || strlen (s) < 7) |
if (*s == '#' || strlen (s) < 7) |
703 |
continue; |
continue; |
704 |
if (check_URL (s)) { |
chk_pos = check_URL (s); |
705 |
|
if (!chk_pos) { |
706 |
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" |
707 |
"Currently HKP/HTTP, LDAP and FINGER are supported.\n"), |
"Currently HKP/HTTP, LDAP and FINGER are supported.\n"), |
708 |
_("Keyserver Error"), MB_ERR); |
_("Keyserver Error"), MB_ERR); |
709 |
continue; |
continue; |
710 |
} |
} |
|
chk_pos = 6; |
|
|
if (strstr (s, "finger")) |
|
|
chk_pos = 10; |
|
711 |
p = strchr (s+chk_pos, ':'); |
p = strchr (s+chk_pos, ':'); |
712 |
server[pos].used = 1; |
server[pos].used = 1; |
713 |
server[pos].proto = proto_from_URL (s); |
server[pos].proto = proto_from_URL (s); |
810 |
} |
} |
811 |
|
|
812 |
|
|
813 |
|
/* Check if the URL needs to be encoded or not. */ |
814 |
static bool |
static bool |
815 |
URL_must_encoded (const char *url) |
URL_must_encoded (const char *url) |
816 |
{ |
{ |
902 |
} |
} |
903 |
|
|
904 |
free_if_alloc (enc_pubkey); |
free_if_alloc (enc_pubkey); |
905 |
log_debug ("%s\r\n", request); |
log_debug ("request:\r\n%s\r\n", request); |
906 |
return request; |
return request; |
907 |
} |
} |
908 |
|
|
947 |
_snprintf (request, reqlen, fmt, keyid); |
_snprintf (request, reqlen, fmt, keyid); |
948 |
} |
} |
949 |
|
|
950 |
log_debug ("%s\r\n", request); |
log_debug ("request:\r\n%s\r\n", request); |
951 |
|
|
952 |
rc = sock_write (conn_fd, request, strlen (request)); |
rc = sock_write (conn_fd, request, strlen (request)); |
953 |
if (rc == SOCKET_ERROR) { |
if (rc == SOCKET_ERROR) { |
961 |
goto leave; |
goto leave; |
962 |
} |
} |
963 |
|
|
964 |
log_debug ("%s\r\n", *r_key); |
log_debug ("response:\r\n%s\r\n", *r_key); |
965 |
rc = check_hkp_response (*r_key, 1); |
rc = check_hkp_response (*r_key, 1); |
966 |
if (rc) |
if (rc) |
967 |
goto leave; |
goto leave; |
1205 |
{ |
{ |
1206 |
enum uid_rec_t {ID=1, UID, CREATE, EXPIRE}; |
enum uid_rec_t {ID=1, UID, CREATE, EXPIRE}; |
1207 |
keyserver_uid_s *u, *n; |
keyserver_uid_s *u, *n; |
1208 |
char *p; |
char *p, *raw; |
1209 |
int recno = 0; |
int recno = 0; |
1210 |
|
|
1211 |
/* uid:Timo Schulz <[email protected]>:1138440360:: */ |
/* uid:Timo Schulz <[email protected]>:1138440360:: */ |
1221 |
break; |
break; |
1222 |
|
|
1223 |
case UID: |
case UID: |
1224 |
/* XXX: search for %3A and decode it back to ':' */ |
unhexify_buffer (p, &raw); |
1225 |
u->uid = m_strdup (p); |
u->uid = utf8_to_native (raw); |
1226 |
|
free_if_alloc (raw); |
1227 |
break; |
break; |
1228 |
|
|
1229 |
case CREATE: |
case CREATE: |
1281 |
for (;;) { |
for (;;) { |
1282 |
if (sock_getline (fd, buf, sizeof (buf)-1, &n)) |
if (sock_getline (fd, buf, sizeof (buf)-1, &n)) |
1283 |
break; |
break; |
1284 |
|
/*log_debug ("record: '%s'\r\n", buf); */ |
1285 |
if (!strncmp (buf, "pub", 3)) |
if (!strncmp (buf, "pub", 3)) |
1286 |
parse_pub_record (key, buf); |
parse_pub_record (key, buf); |
1287 |
else |
else |
1541 |
{ |
{ |
1542 |
char *p; |
char *p; |
1543 |
char *url = *r_keyserver; |
char *url = *r_keyserver; |
1544 |
|
int off = 0; |
1545 |
|
|
1546 |
/* no port is given so use the default port. */ |
/* no port is given so use the default port. */ |
1547 |
p = strrchr (url, ':'); |
p = strrchr (url, ':'); |
1552 |
*r_port = port; |
*r_port = port; |
1553 |
return 0; |
return 0; |
1554 |
} |
} |
1555 |
/* XXX: remove / in .de/:11371 */ |
|
1556 |
*r_keyserver = substr (url, 0, (p-url)); |
if (url[(p-url)-1] == '/') /* remove / in .de/:11371 */ |
1557 |
|
off = 1; |
1558 |
|
|
1559 |
|
*r_keyserver = substr (url, 0, (p-url)-off); |
1560 |
*r_port = atoi (url+(p-url)+1); |
*r_port = atoi (url+(p-url)+1); |
1561 |
safe_free (url); |
free_if_alloc (url); |
1562 |
return 0; |
return 0; |
1563 |
} |
} |