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. |
24 |
#endif |
#endif |
25 |
|
|
26 |
#include <windows.h> |
#include <windows.h> |
27 |
|
#include <shlobj.h> |
28 |
#include <stdio.h> |
#include <stdio.h> |
29 |
#include <sys/stat.h> |
#include <sys/stat.h> |
30 |
#include <ctype.h> |
#include <ctype.h> |
31 |
|
|
32 |
|
#include "wptGPG.h" |
33 |
#include "wptKeyserver.h" |
#include "wptKeyserver.h" |
34 |
#include "wptErrors.h" |
#include "wptErrors.h" |
35 |
#include "wptTypes.h" |
#include "wptTypes.h" |
36 |
#include "wptNLS.h" |
#include "wptNLS.h" |
37 |
#include "wptW32API.h" |
#include "wptW32API.h" |
|
#include "wptVersion.h" |
|
|
#include "wptGPG.h" |
|
38 |
#include "wptRegistry.h" |
#include "wptRegistry.h" |
39 |
|
#include "wptUTF8.h" |
40 |
|
|
41 |
|
/* Map net_errno to a winsock error. */ |
42 |
#define net_errno ((int)WSAGetLastError ()) |
#define net_errno ((int)WSAGetLastError ()) |
43 |
|
|
|
static char base64code[] = |
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
|
44 |
|
|
45 |
keyserver server[MAX_KEYSERVERS] = {0}; |
keyserver server[MAX_KEYSERVERS] = {0}; |
46 |
static keyserver_proxy_ctx proxy = {0}; |
keyserver_proxy_s proxy = {0}; |
47 |
static const char *server_list[] = { |
static const char *server_list[] = { |
48 |
"hkp://wwwkeys.nl.pgp.net", |
"hkp://sks.keyserver.penguin.de", |
|
"hkp://wwwkeys.pl.pgp.net", |
|
|
"hkp://wwwkeys.at.pgp.net", |
|
|
"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", |
|
|
"hkp://gnv.us.ks.cryptnet.net", |
|
49 |
"hkp://subkeys.pgp.net", |
"hkp://subkeys.pgp.net", |
50 |
"ldap://keyserver.pgp.com", |
"ldap://keyserver.pgp.com", |
51 |
NULL |
NULL |
54 |
|
|
55 |
static char hkp_errmsg[1024]; /* Holds the error message from the server */ |
static char hkp_errmsg[1024]; /* Holds the error message from the server */ |
56 |
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. */ |
|
57 |
|
|
58 |
char *default_keyserver = NULL; |
/* Default keyserver and port. */ |
59 |
WORD default_keyserver_port = 0; |
char *default_keyserver = NULL; |
60 |
|
WORD default_keyserver_port = 0; |
61 |
|
|
62 |
|
/* Default socket timeout. */ |
63 |
|
static int default_socket_timeout = 6; |
64 |
|
|
65 |
/* Basic64 encode the input @inputstr to @outputstr. */ |
|
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. */ |
80 |
static void |
static void |
81 |
base64_encode (const char *inputstr, char *outputstr, int maxlen) |
base64_encode (const char *inbuf, char *outbuf) |
82 |
{ |
{ |
83 |
int index = 0, temp = 0, res = 0, i = 0, inputlen = 0, len = 0; |
char base64code[] = |
84 |
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
85 |
|
int index = 0, temp = 0, res = 0; |
86 |
|
int i, inputlen, len = 0; |
87 |
|
|
88 |
/* XXX: check len < maxlen. */ |
inputlen = strlen (inbuf); |
89 |
inputlen = strlen (inputstr); |
for (i = 0; i < inputlen; i++) { |
|
for (i = 0; i <inputlen; i++) { |
|
90 |
res = temp; |
res = temp; |
91 |
res = (res << 8) | (inputstr[i] & 0xFF); |
res = (res << 8) | (inbuf[i] & 0xFF); |
92 |
switch (index++) { |
switch (index++) { |
93 |
case 0: outputstr[len++] = base64code[res >> 2 & 0x3F]; res &= 0x3; break; |
case 0: |
94 |
case 1: outputstr[len++] = base64code[res >> 4 & 0x3F]; res &= 0xF; break; |
outbuf[len++] = base64code[res >> 2 & 0x3F]; |
95 |
case 2: outputstr[len++] = base64code[res >> 6 & 0x3F]; |
res &= 0x3; |
96 |
outputstr[len++] = base64code[res & 0x3F]; res = index = 0; break; |
break; |
97 |
|
case 1: |
98 |
|
outbuf[len++] = base64code[res >> 4 & 0x3F]; |
99 |
|
res &= 0xF; |
100 |
|
break; |
101 |
|
case 2: |
102 |
|
outbuf[len++] = base64code[res >> 6 & 0x3F]; |
103 |
|
outbuf[len++] = base64code[res & 0x3F]; |
104 |
|
res = index = 0; |
105 |
|
break; |
106 |
} |
} |
107 |
temp = res; |
temp = res; |
108 |
} |
} |
109 |
|
|
110 |
switch (index) { |
if (index == 2) { |
111 |
case 0: break; |
outbuf[len++] = base64code[temp << 2 & 0x3F]; |
112 |
case 2: outputstr[len++] = base64code[temp << 2 & 0x3F]; |
outbuf[len++] = '='; |
|
outputstr[len++] = '='; break; |
|
|
case 1: outputstr[len++] = base64code[temp << 4 & 0x3F]; |
|
|
outputstr[len++] = '='; |
|
|
outputstr[len++] = '='; |
|
113 |
} |
} |
114 |
|
else if (index == 1) { |
115 |
outputstr[len] = '\0'; |
outbuf[len++] = base64code[temp << 4 & 0x3F]; |
116 |
|
outbuf[len++] = '='; |
117 |
|
outbuf[len++] = '='; |
118 |
|
} |
119 |
|
|
120 |
|
outbuf[len] = '\0'; |
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 |
|
|
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 -1; |
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, DIM (hkp_errmsg)-2)) { |
217 |
if (p && strlen (p) < sizeof (hkp_errmsg)) { |
if (!strlen (hkp_errmsg)) |
218 |
end = strstr (p, "</p>"); |
_snprintf (hkp_errmsg, DIM (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; |
230 |
sock_getline (int fd, char *buf, int buflen, int *nbytes) |
sock_getline (int fd, char *buf, int buflen, int *nbytes) |
231 |
{ |
{ |
232 |
char ch; |
char ch; |
233 |
int count = 0; |
int nread = 0; |
234 |
|
|
235 |
if (nbytes) |
if (nbytes) |
236 |
*nbytes = 0; |
*nbytes = 0; |
237 |
*buf = 0; |
*buf = 0; |
238 |
while (recv (fd, &ch, 1, 0) > 0) { |
while (recv (fd, &ch, 1, 0) > 0) { |
239 |
*buf++ = ch; |
*buf++ = ch; |
240 |
count++; |
nread++; |
241 |
/* XXX: remove the '\r' */ |
if (ch == '\r') |
242 |
if (ch == '\n' || count == (buflen - 1)) { |
continue; /* remove this char. */ |
243 |
|
if (ch == '\n' || nread == (buflen - 1)) { |
244 |
*buf = 0; |
*buf = 0; |
245 |
if (nbytes) |
if (nbytes) |
246 |
*nbytes = count; |
*nbytes = nread; |
247 |
return 0; |
return 0; |
248 |
} |
} |
249 |
} |
} |
278 |
Return value: 0 on success. */ |
Return value: 0 on success. */ |
279 |
static int |
static int |
280 |
sock_read (int fd, char *buf, int buflen, int *nbytes) |
sock_read (int fd, char *buf, int buflen, int *nbytes) |
281 |
{ |
{ |
282 |
DWORD nread; |
DWORD nread; |
283 |
int nleft = buflen; |
int nleft = buflen; |
284 |
int rc, n = 0; |
int rc, n = 0; |
285 |
|
|
286 |
|
if (nbytes) |
287 |
|
*nbytes = 0; |
288 |
while (nleft > 0) { |
while (nleft > 0) { |
289 |
if (n >= 10) |
if (n >= default_socket_timeout) |
290 |
return WPTERR_WINSOCK_TIMEOUT; |
return WPTERR_WINSOCK_TIMEOUT; |
291 |
rc = sock_select (fd, 1); |
rc = sock_select (fd, 1); |
292 |
if (rc == SOCKET_ERROR) |
if (rc == SOCKET_ERROR) |
312 |
} |
} |
313 |
|
|
314 |
|
|
315 |
|
/* Helper to create a string from the gpgme data and |
316 |
|
then release the gpgme data object. */ |
317 |
|
char* |
318 |
|
data_release_and_get_mem (gpgme_data_t in, int *r_bufferlen) |
319 |
|
{ |
320 |
|
size_t n; |
321 |
|
char *buffer, *p; |
322 |
|
|
323 |
|
gpg_data_putc (in, '\0'); |
324 |
|
p = gpgme_data_release_and_get_mem (in, &n); |
325 |
|
buffer = m_strdup (p); |
326 |
|
if (r_bufferlen) |
327 |
|
*r_bufferlen = (int)n; |
328 |
|
gpgme_free (p); |
329 |
|
return buffer; |
330 |
|
} |
331 |
|
|
332 |
|
|
333 |
|
/* Read much data as possible from the socket @fd and |
334 |
|
return the data in @buffer. Caller must free data. |
335 |
|
Return value: 0 on success. */ |
336 |
|
int |
337 |
|
sock_read_ext (int fd, char **buffer, int *r_bufferlen) |
338 |
|
{ |
339 |
|
gpgme_data_t dh; |
340 |
|
char buf[1024]; |
341 |
|
size_t n=0; |
342 |
|
int nread, rc; |
343 |
|
|
344 |
|
if (gpgme_data_new (&dh)) |
345 |
|
BUG (0); |
346 |
|
while (n < 10) { |
347 |
|
rc = sock_select (fd, 1); |
348 |
|
if (rc == SOCKET_ERROR) { |
349 |
|
gpgme_data_release (dh); |
350 |
|
return rc; |
351 |
|
} |
352 |
|
else if (!rc) |
353 |
|
n++; |
354 |
|
else { |
355 |
|
nread = recv (fd, buf, DIM (buf), 0); |
356 |
|
if (nread == SOCKET_ERROR) |
357 |
|
return SOCKET_ERROR; |
358 |
|
else if (!nread) |
359 |
|
break; |
360 |
|
gpgme_data_write (dh, buf, nread); |
361 |
|
} |
362 |
|
} |
363 |
|
|
364 |
|
*buffer = data_release_and_get_mem (dh, r_bufferlen); |
365 |
|
return 0; |
366 |
|
} |
367 |
|
|
368 |
|
|
369 |
/* Write the buffer @buf with the length @buflen to a socket @fd. */ |
/* Write the buffer @buf with the length @buflen to a socket @fd. */ |
370 |
static int |
static int |
371 |
sock_write (int fd, const char *buf, int buflen) |
sock_write (int fd, const char *buf, int buflen) |
380 |
return SOCKET_ERROR; |
return SOCKET_ERROR; |
381 |
} |
} |
382 |
nleft -= nwritten; |
nleft -= nwritten; |
383 |
buf += nwritten; |
buf += nwritten; |
384 |
} |
} |
385 |
|
|
386 |
return 0; |
return 0; |
387 |
} |
} |
388 |
|
|
389 |
|
|
|
/* Set the default keyserver. */ |
|
|
void |
|
|
set_default_kserver (void) |
|
|
{ |
|
|
char *p = get_reg_entry_keyserver ("Default"); |
|
|
free_if_alloc (default_keyserver); |
|
|
default_keyserver = p && *p != ' ' ? p : m_strdup (DEF_HKP_KEYSERVER); |
|
|
p = get_reg_entry_keyserver ("Default_Port"); |
|
|
if (p && *p) { |
|
|
default_keyserver_port = (WORD)strtoul (p, NULL, 10); |
|
|
free_if_alloc (p); |
|
|
} |
|
|
else |
|
|
default_keyserver_port = HKP_PORT; |
|
|
} |
|
|
|
|
|
|
|
390 |
/* Initialize the Winsock2 interface.*/ |
/* Initialize the Winsock2 interface.*/ |
391 |
int |
int |
392 |
wsock_init (void) |
wsock_init (void) |
397 |
log_debug ("wsock_init: WSAStartup failed ec=%d\r\n", net_errno); |
log_debug ("wsock_init: WSAStartup failed ec=%d\r\n", net_errno); |
398 |
return WPTERR_WINSOCK_INIT; |
return WPTERR_WINSOCK_INIT; |
399 |
} |
} |
|
set_default_kserver (); |
|
400 |
return 0; |
return 0; |
401 |
} |
} |
402 |
|
|
405 |
void |
void |
406 |
wsock_end (void) |
wsock_end (void) |
407 |
{ |
{ |
408 |
|
char *p; |
409 |
int i; |
int i; |
410 |
|
|
411 |
|
#ifdef WINPT_MOBILE |
412 |
|
p = m_strdup ("keyserver.conf"); |
413 |
|
#else |
414 |
|
p = make_special_filename (CSIDL_APPDATA, "winpt\\keyserver.conf", NULL); |
415 |
|
#endif |
416 |
|
kserver_save_conf (p); |
417 |
|
free_if_alloc (p); |
418 |
free_if_alloc (default_keyserver); |
free_if_alloc (default_keyserver); |
|
default_keyserver = NULL; |
|
419 |
for (i=0; i < MAX_KEYSERVERS; i++) { |
for (i=0; i < MAX_KEYSERVERS; i++) { |
420 |
if (server[i].used) |
if (server[i].used && server[i].name != NULL) |
421 |
free_if_alloc (server[i].name); |
free_if_alloc (server[i].name); |
422 |
} |
} |
423 |
|
kserver_proxy_release (&proxy); |
424 |
WSACleanup (); |
WSACleanup (); |
425 |
|
log_debug ("wsock_end: cleanup finished.\r\n"); |
426 |
} |
} |
427 |
|
|
428 |
|
|
429 |
/* Return a string representation of a winsock error. */ |
/* Return a string representation of a winsock error. */ |
430 |
const char* |
const char* |
431 |
wsock_strerror (void) |
wsock_strerror (void) |
432 |
{ |
{ |
|
static char buf[384]; |
|
433 |
int ec = WSAGetLastError (); |
int ec = WSAGetLastError (); |
434 |
|
|
435 |
|
if (!ec) |
436 |
|
return ""; |
437 |
switch (ec) { |
switch (ec) { |
438 |
case WSAENETDOWN: |
case WSAENETDOWN: |
439 |
return _("The network subsystem has failed"); |
return _("Network unreachable"); |
440 |
|
|
441 |
|
case WSAEHOSTUNREACH: |
442 |
|
return _("Host unreachable"); |
443 |
|
|
444 |
case WSAHOST_NOT_FOUND: |
case WSAHOST_NOT_FOUND: |
445 |
return _("Authoritative Answer Host not found"); |
return _("Could not resolve host name"); |
446 |
|
|
447 |
|
case WSAECONNREFUSED: |
448 |
|
return _("Connection refused"); |
449 |
|
|
450 |
case WSAETIMEDOUT: |
case WSAETIMEDOUT: |
451 |
return _("The connection has been dropped because of a network failure"); |
case WSAECONNABORTED: |
452 |
|
return _("Connection timeout"); |
453 |
|
|
454 |
|
case WSAENETRESET: |
455 |
|
case WSAECONNRESET: |
456 |
|
return _("Connection resetted by peer"); |
457 |
|
|
458 |
|
case WSAESHUTDOWN: |
459 |
|
return _("Socket has been shutdown"); |
460 |
|
|
461 |
default: |
default: |
462 |
_snprintf (buf, sizeof (buf) -1, _("Unknown Winsock error ec=%d"),ec); |
break; |
|
return buf; |
|
463 |
} |
} |
464 |
return NULL; |
|
465 |
|
return ""; |
466 |
|
} |
467 |
|
|
468 |
|
|
469 |
|
/* Set default socket timeout for all reading operations. */ |
470 |
|
void |
471 |
|
kserver_set_socket_timeout (int nsec) |
472 |
|
{ |
473 |
|
if (nsec < 0 || nsec > 3600) |
474 |
|
nsec = 0; |
475 |
|
default_socket_timeout = nsec; |
476 |
} |
} |
477 |
|
|
478 |
|
|
496 |
return default_keyserver; |
return default_keyserver; |
497 |
} |
} |
498 |
else if (!type && idx < DIM (server_list)) { |
else if (!type && idx < DIM (server_list)) { |
499 |
|
/* XXX: handle non HKP servers. */ |
500 |
*port = HKP_PORT; |
*port = HKP_PORT; |
501 |
return server_list[idx]; |
return server_list[idx]; |
502 |
} |
} |
515 |
closesocket (fd); |
closesocket (fd); |
516 |
return 0; |
return 0; |
517 |
} |
} |
518 |
|
log_debug ("kserver_check_inet_connection: no inet connection.\r\n"); |
519 |
return -1; |
return -1; |
520 |
} |
} |
521 |
|
|
523 |
/* If the request contains the keyid, it have to be |
/* If the request contains the keyid, it have to be |
524 |
always postfix with '0x'+keyid. This code checks |
always postfix with '0x'+keyid. This code checks |
525 |
if the keyid is a decimal value and if so if it contains |
if the keyid is a decimal value and if so if it contains |
526 |
the '0x'. If not it is inserted. */ |
the '0x'. If not it is inserted. */ |
527 |
const char* |
const char* |
528 |
kserver_check_keyid (const char *keyid) |
kserver_check_keyid (const char *keyid) |
529 |
{ |
{ |
530 |
static char id[21]; |
static char id[22]; |
531 |
|
|
532 |
if (strstr (keyid, "@")) |
if (strstr (keyid, "@")) |
533 |
return keyid; /* email address */ |
return keyid; /* email address */ |
534 |
if (strncmp (keyid, "0x", 2)) { |
if (strncmp (keyid, "0x", 2)) { |
535 |
memset (&id, 0, sizeof (id)); |
memset (&id, 0, sizeof (id)); |
536 |
_snprintf (id, sizeof (id)-1, "0x%s", keyid); |
_snprintf (id, DIM (id)-1, "0x%s", keyid); |
537 |
return id; |
return id; |
538 |
} |
} |
539 |
return keyid; |
return keyid; |
542 |
|
|
543 |
/* Update the keyserver proxy user. */ |
/* Update the keyserver proxy user. */ |
544 |
static void |
static void |
545 |
kserver_update_proxyuser (const char *proxy_user, const char *proxy_pass) |
update_proxy_user (const char *proxy_user, const char *proxy_pass) |
546 |
{ |
{ |
547 |
char t[257]; /* user:pass = 127+1+127+1 = 257 */ |
char t[257]; /* user:pass = 127+1+127+1 = 257 */ |
548 |
int n = 0; |
int n = 0; |
549 |
|
|
550 |
n = 4*strlen (proxy_user) / 3 + 32 + strlen (proxy_pass) + 2; |
n = 4*strlen (proxy_user) / 3 + 32 + strlen (proxy_pass) + 2; |
551 |
free_if_alloc (proxy.base64_user); |
free_if_alloc (proxy.base64_user); |
552 |
proxy.base64_user = new char[n]; |
proxy.base64_user = safe_alloc (n+1); |
553 |
if (!proxy.base64_user) |
_snprintf (t, DIM (t)-1, "%s:%s", proxy_user, proxy_pass); |
554 |
BUG (0); |
base64_encode (t, proxy.base64_user); |
|
_snprintf (t, sizeof (t)-1, "%s:%s", proxy_user, proxy_pass); |
|
|
base64_encode (t, proxy.base64_user, 257); |
|
555 |
free_if_alloc (proxy.user); |
free_if_alloc (proxy.user); |
556 |
free_if_alloc (proxy.pass); |
free_if_alloc (proxy.pass); |
557 |
proxy.user = m_strdup (proxy_user); |
proxy.user = m_strdup (proxy_user); |
558 |
proxy.pass = m_strdup (proxy_pass); |
proxy.pass = m_strdup (proxy_pass); |
559 |
} |
} |
|
|
|
|
|
|
|
/* 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; |
|
|
} |
|
560 |
|
|
561 |
|
|
562 |
/* Get the port number from the given protocol. */ |
/* Get the port number from the given protocol. */ |
564 |
port_from_proto (int proto) |
port_from_proto (int proto) |
565 |
{ |
{ |
566 |
switch (proto) { |
switch (proto) { |
567 |
case KSPROTO_LDAP: return 0; |
case KSPROTO_LDAP: return LDAP_PORT; |
568 |
case KSPROTO_FINGER: return FINGER_PORT; |
case KSPROTO_FINGER: return FINGER_PORT; |
569 |
case KSPROTO_HTTP: return HKP_PORT; |
case KSPROTO_HTTP: return HKP_PORT; |
570 |
} |
} |
571 |
return 0; |
return 0; |
572 |
} |
} |
574 |
|
|
575 |
/* Get the port number from the given URL. */ |
/* Get the port number from the given URL. */ |
576 |
static int |
static int |
577 |
proto_from_URL (const char * buf) |
proto_from_URL (const char *buf) |
578 |
{ |
{ |
579 |
if (strstr( buf, "ldap")) |
if (strstr (buf, "ldap")) |
580 |
return KSPROTO_LDAP; |
return KSPROTO_LDAP; |
581 |
else if (strstr( buf, "finger")) |
else if (strstr (buf, "finger")) |
582 |
return KSPROTO_FINGER; |
return KSPROTO_FINGER; |
583 |
return KSPROTO_HKP; |
return KSPROTO_HKP; |
584 |
} |
} |
585 |
|
|
586 |
|
|
587 |
|
/* Set the default keyserver. |
588 |
|
If @hostname is NULL, use the predefined keyserver. */ |
589 |
void |
void |
590 |
keyserver_set_default (const char *hostname, WORD port) |
keyserver_set_default (const char *hostname, WORD port) |
591 |
{ |
{ |
592 |
if (hostname) { |
if (!port) |
593 |
|
port = HKP_PORT; |
594 |
|
if (hostname != NULL) { |
595 |
free_if_alloc (default_keyserver); |
free_if_alloc (default_keyserver); |
596 |
default_keyserver = m_strdup (hostname); |
default_keyserver = m_strdup (hostname); |
|
if (!default_keyserver) |
|
|
BUG (0); |
|
597 |
default_keyserver_port = port; |
default_keyserver_port = port; |
598 |
} |
} |
599 |
|
if (!default_keyserver) { |
600 |
|
default_keyserver = m_strdup (DEF_HKP_KEYSERVER); |
601 |
|
default_keyserver_port = HKP_PORT; |
602 |
|
} |
603 |
server[0].name = m_strdup (default_keyserver); |
server[0].name = m_strdup (default_keyserver); |
604 |
server[0].used = 1; |
server[0].used = 1; |
605 |
server[0].port = port; |
server[0].port = port; |
625 |
} |
} |
626 |
|
|
627 |
|
|
628 |
|
/* Save the keyserver config file in @conf. */ |
629 |
int |
int |
630 |
kserver_load_conf (const char * conf) |
kserver_save_conf (const char *conf) |
631 |
|
{ |
632 |
|
FILE *fp; |
633 |
|
int pos; |
634 |
|
|
635 |
|
fp = fopen (conf, "wb"); |
636 |
|
if (!fp) { |
637 |
|
msg_box (NULL, _("Could not save keyserver.conf file"), |
638 |
|
_("Keyserver"), MB_ERR); |
639 |
|
return -1; |
640 |
|
} |
641 |
|
|
642 |
|
fprintf (fp, "# do NOT manually modify this file, it will be " |
643 |
|
"generated automatically!!\r\n"); |
644 |
|
for (pos = 0; pos < MAX_KEYSERVERS; pos++) { |
645 |
|
if (!server[pos].used) |
646 |
|
continue; |
647 |
|
fprintf (fp, "%s:%d\r\n", server[pos].name, server[pos].port); |
648 |
|
} |
649 |
|
fclose (fp); |
650 |
|
return 0; |
651 |
|
} |
652 |
|
|
653 |
|
|
654 |
|
/* Return the specified keyserver config setting @key as an integer. */ |
655 |
|
static int |
656 |
|
get_conf_kserver_int (const char *key) |
657 |
|
{ |
658 |
|
char *p; |
659 |
|
int val = 0; |
660 |
|
|
661 |
|
p = get_reg_entry_keyserver (key); |
662 |
|
if (p && *p) |
663 |
|
val = atoi (p); |
664 |
|
free_if_alloc (p); |
665 |
|
return val; |
666 |
|
} |
667 |
|
|
668 |
|
|
669 |
|
/* Read the proxy configuration and store it into @prox. */ |
670 |
|
static void |
671 |
|
read_proxy_config (keyserver_proxy_t prox) |
672 |
{ |
{ |
673 |
struct stat statbuf; |
char *proto; |
674 |
|
|
675 |
|
if (!prox) |
676 |
|
return; |
677 |
|
|
678 |
|
proto = get_reg_entry_keyserver("Proto"); |
679 |
|
if (proto != NULL && strlen (proto) > 0) |
680 |
|
prox->proto = atoi (proto); |
681 |
|
else |
682 |
|
prox->proto = PROXY_PROTO_NONE; |
683 |
|
free_if_alloc (proto); |
684 |
|
free_if_alloc (prox->host); |
685 |
|
prox->host = get_reg_entry_keyserver ("Host"); |
686 |
|
free_if_alloc (prox->user); |
687 |
|
prox->user = get_reg_entry_keyserver ("User"); |
688 |
|
free_if_alloc (prox->pass); |
689 |
|
prox->pass = get_reg_entry_keyserver ("Pass"); |
690 |
|
prox->port = get_conf_kserver_int ("Port"); |
691 |
|
} |
692 |
|
|
693 |
|
|
694 |
|
/* Load the keyserver config file @conf. */ |
695 |
|
int |
696 |
|
kserver_load_conf (const char *conf) |
697 |
|
{ |
698 |
FILE *fp; |
FILE *fp; |
699 |
char buf[1024], * s, * p; |
char buf[1024], *s, *p; |
700 |
char *user = NULL, *pass = NULL; |
char *user = NULL, *pass = NULL; |
701 |
int no_config=0, chk_pos=0; |
int no_config=0, chk_pos=0; |
702 |
int pos; |
int pos; |
715 |
} |
} |
716 |
no_config=1; |
no_config=1; |
717 |
} |
} |
718 |
get_reg_proxy_prefs (&proxy.host, &proxy.port, &user, &pass); |
read_proxy_config (&proxy); |
719 |
if (user && pass) |
if (user && pass) |
720 |
kserver_update_proxyuser (user, pass); |
update_proxy_user (user, pass); |
721 |
else if (user && !pass || !user && pass) { |
else if (user && !pass || !user && pass) { |
722 |
msg_box( NULL, _("Invalid proxy configuration." |
msg_box (NULL, _("Invalid proxy configuration. " |
723 |
"You need to set a user and a password" |
"You need to set a user and a password " |
724 |
"to use proxy authentication!"), _("Proxy Error"), MB_ERR ); |
"to use proxy authentication!"), |
725 |
|
_("Proxy Error"), MB_ERR); |
726 |
} |
} |
727 |
/* check if the host has a http:// prefix and skip it */ |
/* check if the host has a http:// prefix and skip it */ |
728 |
if( proxy.host && !strncmp( proxy.host, "http://", 7 ) ) { |
if (proxy.host && !strncmp (proxy.host, "http://", 7)) { |
729 |
char *host = m_strdup (proxy.host+7); |
char *host = m_strdup (proxy.host+7); |
|
if (!host) |
|
|
BUG (0); |
|
730 |
free_if_alloc (proxy.host); |
free_if_alloc (proxy.host); |
731 |
proxy.host = host; |
proxy.host = host; |
732 |
} |
} |
734 |
free_if_alloc (pass); |
free_if_alloc (pass); |
735 |
|
|
736 |
pos = 0; |
pos = 0; |
737 |
while( fp && !feof( fp ) ) { |
while (fp && !feof (fp)) { |
738 |
s = fgets (buf, sizeof (buf)-1, fp); |
s = fgets (buf, DIM (buf)-1, fp); |
739 |
if (!s) |
if (!s) |
740 |
break; |
break; |
741 |
if (strstr (buf, "\r\n")) |
if (strstr (buf, "\r\n")) |
745 |
s = (char *)skip_whitespace (buf); |
s = (char *)skip_whitespace (buf); |
746 |
if (*s == '#' || strlen (s) < 7) |
if (*s == '#' || strlen (s) < 7) |
747 |
continue; |
continue; |
748 |
if (check_URL (s)) { |
chk_pos = check_URL (s); |
749 |
|
if (!chk_pos) { |
750 |
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" |
751 |
"Currently HKP/HTTP, LDAP and FINGER are supported.\n"), |
"Currently HKP/HTTP, LDAP and FINGER are supported.\n"), |
752 |
_("Keyserver Error"), MB_ERR); |
_("Keyserver Error"), MB_ERR); |
753 |
continue; |
continue; |
754 |
} |
} |
|
chk_pos=6; |
|
|
if (strstr (s, "finger")) |
|
|
chk_pos = 10; |
|
755 |
p = strchr (s+chk_pos, ':'); |
p = strchr (s+chk_pos, ':'); |
756 |
server[pos].used = 1; |
server[pos].used = 1; |
757 |
server[pos].proto = proto_from_URL (s); |
server[pos].proto = proto_from_URL (s); |
762 |
server[pos].port = atol (p+1); |
server[pos].port = atol (p+1); |
763 |
if (server[pos].port < 0 || server[pos].port > 65536) |
if (server[pos].port < 0 || server[pos].port > 65536) |
764 |
server[pos].port = HKP_PORT; |
server[pos].port = HKP_PORT; |
765 |
server[pos].name = new char [(p-s)+2]; |
server[pos].name = safe_alloc ((p-s)+2); |
|
if (!server[pos].name) |
|
|
BUG (0); |
|
766 |
memset (server[pos].name, 0, (p-s)+2); |
memset (server[pos].name, 0, (p-s)+2); |
767 |
memcpy (server[pos].name, s, (p-s)); |
memcpy (server[pos].name, s, (p-s)); |
768 |
} |
} |
769 |
pos++; |
pos++; |
770 |
if (pos > MAX_KEYSERVERS) |
if (pos > MAX_KEYSERVERS) { |
771 |
{ |
msg_box (NULL, _("The keyserver limit is exceeded"), |
772 |
msg_box (NULL, _("The keyserver limit is exceeded"), _("Keyserver Warning"), MB_INFO); |
_("Keyserver Warning"), MB_INFO); |
773 |
break; |
break; |
774 |
} |
} |
775 |
} |
} |
776 |
if (fp) |
if (fp) |
777 |
fclose (fp); |
fclose (fp); |
778 |
if (!pos) { |
if (!pos) { |
779 |
/* only issue an error if the config file exist but it has no valid entries. */ |
/* only issue an error if the config file exist but |
780 |
|
it has no valid entries. */ |
781 |
keyserver_set_default (NULL, HKP_PORT); |
keyserver_set_default (NULL, HKP_PORT); |
782 |
if (!no_config) |
if (!no_config) |
783 |
return WPTERR_CONFIG_FILE; |
return WPTERR_CONFIG_FILE; |
784 |
} |
} |
785 |
|
|
|
if (!stat (conf, &statbuf)) |
|
|
conf_timestamp = statbuf.st_mtime; |
|
786 |
return 0; |
return 0; |
|
} /* kserver_load_conf */ |
|
|
|
|
|
|
|
|
/* Return the proxy host and port if available. Null otherwise. */ |
|
|
const char* |
|
|
kserver_get_proxy (int *r_port) |
|
|
{ |
|
|
if (proxy.host) { |
|
|
if (r_port) |
|
|
*r_port = proxy.port; |
|
|
return proxy.host; |
|
|
} |
|
|
return NULL; |
|
|
} |
|
|
|
|
|
|
|
|
/* Return a requested item from the proxy environment. */ |
|
|
const char* |
|
|
kserver_get_proxy_info (int id) |
|
|
{ |
|
|
switch (id) { |
|
|
case PROXY_USER: return proxy.user; |
|
|
case PROXY_PASS: return proxy.pass; |
|
|
} |
|
|
return NULL; |
|
787 |
} |
} |
788 |
|
|
789 |
|
|
791 |
Return value: 0 on success */ |
Return value: 0 on success */ |
792 |
int |
int |
793 |
kserver_connect (const char *hostname, WORD port, int *conn_fd) |
kserver_connect (const char *hostname, WORD port, int *conn_fd) |
794 |
{ |
{ |
|
int rc, fd; |
|
|
DWORD iaddr; |
|
|
char host[128] = {0}; |
|
795 |
struct hostent *hp; |
struct hostent *hp; |
796 |
struct sockaddr_in sock; |
struct sockaddr_in sock; |
797 |
|
char host[128] = {0}; |
798 |
log_debug ("kserver_connect: %s:%d\r\n", hostname, port); |
DWORD iaddr; |
799 |
|
bool use_proxy = proxy.host != NULL; |
800 |
|
int rc, fd; |
801 |
|
|
802 |
if (!port) |
if (!port) |
803 |
port = HKP_PORT; |
port = HKP_PORT; |
804 |
if (conn_fd) |
if (conn_fd) |
805 |
*conn_fd = 0; |
*conn_fd = 0; |
806 |
hostname = skip_type_prefix (hostname); |
hostname = skip_type_prefix (hostname); |
807 |
|
log_debug ("kserver_connect: %s:%d\r\n", hostname, port); |
808 |
|
|
809 |
|
if (use_proxy && proxy.proto == PROXY_PROTO_HTTP) |
810 |
|
port = proxy.port; |
811 |
memset (&sock, 0, sizeof (sock)); |
memset (&sock, 0, sizeof (sock)); |
812 |
sock.sin_family = AF_INET; |
sock.sin_family = AF_INET; |
813 |
sock.sin_port = proxy.host? htons (proxy.port) : htons (port); |
sock.sin_port = htons (port); |
814 |
if (proxy.host) |
if (use_proxy) |
815 |
strncpy (host, proxy.host, 127); |
strncpy (host, proxy.host, DIM (host)-1); |
816 |
else |
else |
817 |
strncpy (host, hostname, 127); |
strncpy (host, hostname, DIM (host)-1); |
818 |
|
|
819 |
if ((iaddr = inet_addr (host)) != INADDR_NONE) |
if ((iaddr = inet_addr (host)) != INADDR_NONE) |
820 |
memcpy (&sock.sin_addr, &iaddr, sizeof (iaddr)); |
memcpy (&sock.sin_addr, &iaddr, sizeof (iaddr)); |
821 |
else if ((hp = gethostbyname (host))) { |
else if ((hp = gethostbyname (host))) { |
827 |
} |
} |
828 |
else { |
else { |
829 |
log_debug ("gethostbyname: failed ec=%d.\r\n", net_errno); |
log_debug ("gethostbyname: failed ec=%d.\r\n", net_errno); |
830 |
return WPTERR_WINSOCK_RESOLVE; |
return use_proxy? WPTERR_WINSOCK_PROXY : WPTERR_WINSOCK_RESOLVE; |
831 |
} |
} |
832 |
fd = socket (AF_INET, SOCK_STREAM, 0); |
fd = socket (AF_INET, SOCK_STREAM, 0); |
833 |
if (fd == INVALID_SOCKET) |
if (fd == INVALID_SOCKET) |
836 |
if (rc == SOCKET_ERROR) { |
if (rc == SOCKET_ERROR) { |
837 |
log_debug ("connect: failed ec=%d.\r\n", net_errno); |
log_debug ("connect: failed ec=%d.\r\n", net_errno); |
838 |
closesocket (fd); |
closesocket (fd); |
839 |
return WPTERR_WINSOCK_CONNECT; |
return use_proxy? WPTERR_WINSOCK_PROXY : WPTERR_WINSOCK_CONNECT; |
840 |
|
} |
841 |
|
|
842 |
|
if (proxy.proto == PROXY_PROTO_SOCKS5) { |
843 |
|
/* XXX: finish code and provide errors constants. */ |
844 |
|
rc = socks_handshake (&proxy, fd, hostname, port); |
845 |
|
if (rc) { |
846 |
|
closesocket (fd); |
847 |
|
return WPTERR_GENERAL; |
848 |
|
} |
849 |
} |
} |
850 |
|
|
851 |
if (conn_fd) |
if (conn_fd) |
855 |
} |
} |
856 |
|
|
857 |
|
|
858 |
|
/* Check if the URL needs to be encoded or not. */ |
859 |
static bool |
static bool |
860 |
URL_must_encoded (const char *url) |
URL_must_encoded (const char *url) |
861 |
{ |
{ |
867 |
|
|
868 |
/* Perform URL encoding of the given data. */ |
/* Perform URL encoding of the given data. */ |
869 |
static char* |
static char* |
870 |
URL_encode (const char *url, size_t ulen, size_t *ret_nlen) |
URL_encode (const char *url, DWORD ulen, DWORD *ret_nlen) |
871 |
{ |
{ |
872 |
gpgme_data_t hd; |
gpgme_data_t hd; |
873 |
char numbuf[5], *pp, *p; |
char numbuf[5], *p; |
874 |
size_t i, n; |
size_t i; |
875 |
|
int nlen; |
876 |
|
|
877 |
gpgme_data_new (&hd); |
if (gpgme_data_new (&hd)) |
878 |
|
BUG (0); |
879 |
for (i=0; i < ulen; i++) { |
for (i=0; i < ulen; i++) { |
880 |
if (isalnum (url[i]) || url[i] == '-') |
if (isalnum (url[i]) || url[i] == '-') |
881 |
gpg_data_putc (hd, url[i]); |
gpg_data_putc (hd, url[i]); |
886 |
gpgme_data_write (hd, numbuf, strlen (numbuf)); |
gpgme_data_write (hd, numbuf, strlen (numbuf)); |
887 |
} |
} |
888 |
} |
} |
|
gpg_data_putc (hd, '\0'); |
|
889 |
|
|
890 |
/* 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); |
|
891 |
if (ret_nlen) |
if (ret_nlen) |
892 |
*ret_nlen = n; |
*ret_nlen = nlen; |
893 |
return p; |
return p; |
894 |
} |
} |
895 |
|
|
897 |
/* Format a request for the given keyid (send). */ |
/* Format a request for the given keyid (send). */ |
898 |
static char* |
static char* |
899 |
kserver_send_request (const char *hostname, WORD port, |
kserver_send_request (const char *hostname, WORD port, |
900 |
const char *pubkey, int octets) |
const char *pubkey, DWORD octets) |
901 |
{ |
{ |
902 |
char *request = NULL; |
const char *fmt; |
903 |
char *enc_pubkey = NULL; |
char *request; |
904 |
size_t enc_octets; |
char *enc_pubkey; |
905 |
|
DWORD enc_octets; |
906 |
int reqlen; |
int reqlen; |
907 |
|
|
908 |
log_debug ("kserver_send_request: %s:%d\r\n", hostname, port); |
log_debug ("kserver_send_request: %s:%d\r\n", hostname, port); |
909 |
|
|
910 |
if (!port) |
if (!port) |
911 |
port = HKP_PORT; |
port = HKP_PORT; |
|
reqlen = 512 + strlen (hostname) + 2*strlen (pubkey); |
|
|
request = new char[reqlen]; |
|
|
if (!request) |
|
|
BUG (0); |
|
912 |
enc_pubkey = URL_encode (pubkey, octets, &enc_octets); |
enc_pubkey = URL_encode (pubkey, octets, &enc_octets); |
913 |
if (!enc_pubkey || !enc_octets) { |
reqlen = 2*strlen (hostname) + 2*32/*port*/ + enc_octets + 32 /*len*/ + 1; |
914 |
free_if_alloc (request); |
|
915 |
return NULL; |
if (proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
916 |
} |
fmt = "POST http://%s:%d/pks/add HTTP/1.0\r\n" |
917 |
|
"Referer: \r\n" |
918 |
if (proxy.user) { |
"User-Agent: WinPT/W32\r\n" |
919 |
_snprintf (request, reqlen-1, |
"Host: %s:%d\r\n" |
920 |
"POST http://%s:%d/pks/add HTTP/1.0\r\n" |
"Proxy-Authorization: Basic %s\r\n" |
921 |
"Referer: \r\n" |
"Content-type: application/x-www-form-urlencoded\r\n" |
922 |
"User-Agent: WinPT/W32\r\n" |
"Content-length: %d\r\n" |
923 |
"Host: %s:%d\r\n" |
"\r\n" |
924 |
"Proxy-Authorization: Basic %s\r\n" |
"keytext=%s" |
925 |
"Content-type: application/x-www-form-urlencoded\r\n" |
"\r\n"; |
926 |
"Content-length: %d\r\n" |
reqlen += strlen (fmt) + strlen (proxy.base64_user) + 1; |
927 |
"\r\n" |
request = safe_alloc (reqlen+1); |
928 |
"keytext=%s" |
_snprintf (request, reqlen, fmt, |
929 |
"\n", |
skip_type_prefix (hostname), port, hostname, port, |
|
skip_type_prefix (hostname), port, hostname, port, |
|
930 |
proxy.base64_user, enc_octets+9, enc_pubkey); |
proxy.base64_user, enc_octets+9, enc_pubkey); |
931 |
} |
} |
932 |
else { |
else { |
933 |
_snprintf (request, reqlen-1, |
fmt = "POST /pks/add HTTP/1.0\r\n" |
934 |
"POST /pks/add HTTP/1.0\r\n" |
"Referer: \r\n" |
935 |
"Referer: \r\n" |
"User-Agent: WinPT/W32\r\n" |
936 |
"User-Agent: WinPT/W32\r\n" |
"Host: %s:%d\r\n" |
937 |
"Host: %s:%d\r\n" |
"Content-type: application/x-www-form-urlencoded\r\n" |
938 |
"Content-type: application/x-www-form-urlencoded\r\n" |
"Content-length: %d\r\n" |
939 |
"Content-length: %d\r\n" |
"\r\n" |
940 |
"\r\n" |
"keytext=%s" |
941 |
"keytext=%s" |
"\r\n"; |
942 |
"\n", |
reqlen += strlen (fmt)+1; |
943 |
skip_type_prefix (hostname), port, |
request = safe_alloc (reqlen+1); |
944 |
|
_snprintf (request, reqlen, fmt, |
945 |
|
skip_type_prefix (hostname), port, |
946 |
enc_octets+9, enc_pubkey); |
enc_octets+9, enc_pubkey); |
947 |
} |
} |
|
free_if_alloc (enc_pubkey); |
|
948 |
|
|
949 |
log_debug ("%s\r\n", request); |
free_if_alloc (enc_pubkey); |
950 |
|
log_debug ("request:\r\n%s\r\n", request); |
951 |
return request; |
return request; |
952 |
} |
} |
953 |
|
|
955 |
/* Interface for receiving a public key. */ |
/* Interface for receiving a public key. */ |
956 |
int |
int |
957 |
kserver_recvkey (const char *hostname, WORD port, const char *keyid, |
kserver_recvkey (const char *hostname, WORD port, const char *keyid, |
958 |
char *key, int maxkeylen) |
char **r_key, int *r_keylen) |
959 |
{ |
{ |
960 |
|
const char *fmt; |
961 |
char *request = NULL; |
char *request = NULL; |
962 |
int conn_fd; |
int conn_fd; |
963 |
int rc, n; |
int rc, reqlen; |
964 |
|
|
965 |
if (!port) |
if (!port) |
966 |
port = HKP_PORT; |
port = HKP_PORT; |
967 |
hkp_err = 0; /* reset */ |
hkp_err = 0; /* reset */ |
968 |
rc = kserver_connect (hostname, port, &conn_fd); |
rc = kserver_connect (hostname, port, &conn_fd); |
969 |
if (rc) |
if (rc) |
970 |
goto leave; |
goto leave; |
971 |
|
|
972 |
request = new char[300+1]; |
reqlen = strlen (hostname)+32+strlen (keyid)+1; |
973 |
if (!request) |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
974 |
BUG (0); |
fmt = "GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n" |
975 |
|
"Proxy-Authorization: Basic %s\r\n\r\n"; |
976 |
if (proxy.host && proxy.user) { |
reqlen =+ strlen (fmt) + strlen (proxy.base64_user)+1; |
977 |
_snprintf (request, 300, |
request = safe_alloc (reqlen+1); |
978 |
"GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n" |
_snprintf (request, reqlen, fmt, skip_type_prefix (hostname), port, |
979 |
"Proxy-Authorization: Basic %s\r\n\r\n", |
keyid, proxy.base64_user); |
980 |
skip_type_prefix (hostname), port, keyid, proxy.base64_user); |
} |
981 |
} |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
982 |
else if (proxy.host) { |
fmt = "GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n"; |
983 |
_snprintf (request, 300, |
reqlen += strlen (fmt)+1; |
984 |
"GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n", |
request = safe_alloc (reqlen+1); |
985 |
skip_type_prefix (hostname), port, keyid); |
_snprintf (request, reqlen, fmt, skip_type_prefix (hostname), |
986 |
} |
port, keyid); |
987 |
|
} |
988 |
else { |
else { |
989 |
_snprintf (request, 300, |
fmt = "GET /pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n"; |
990 |
"GET /pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n", keyid); |
reqlen += strlen (fmt)+1; |
991 |
|
request = safe_alloc (reqlen+1); |
992 |
|
_snprintf (request, reqlen, fmt, keyid); |
993 |
} |
} |
994 |
|
|
995 |
log_debug ("%s\r\n", request); |
log_debug ("request:\r\n%s\r\n", request); |
996 |
|
|
997 |
rc = sock_write (conn_fd, request, strlen (request)); |
rc = sock_write (conn_fd, request, strlen (request)); |
998 |
if (rc == SOCKET_ERROR) { |
if (rc == SOCKET_ERROR) { |
1000 |
goto leave; |
goto leave; |
1001 |
} |
} |
1002 |
|
|
1003 |
rc = sock_read( conn_fd, key, maxkeylen, &n ); |
rc = sock_read_ext (conn_fd, r_key, r_keylen); |
1004 |
if( rc == SOCKET_ERROR ) { |
if (rc == SOCKET_ERROR) { |
1005 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
1006 |
goto leave; |
goto leave; |
1007 |
} |
} |
1008 |
|
|
1009 |
log_debug("%s\r\n", key); |
log_debug ("response:\r\n%s\r\n", *r_key); |
1010 |
rc = check_hkp_response (key, 1); |
rc = check_hkp_response (*r_key, 1); |
1011 |
if (rc) |
if (rc) |
1012 |
goto leave; |
goto leave; |
1013 |
|
|
1025 |
kserver_sendkey (const char *hostname, WORD port, const char *pubkey, int len ) |
kserver_sendkey (const char *hostname, WORD port, const char *pubkey, int len ) |
1026 |
{ |
{ |
1027 |
char *request = NULL; |
char *request = NULL; |
1028 |
char log[2048]; |
char log[2048] = {0}; |
1029 |
int conn_fd, n; |
int conn_fd, n; |
1030 |
int rc; |
int rc; |
1031 |
|
|
1046 |
goto leave; |
goto leave; |
1047 |
} |
} |
1048 |
|
|
1049 |
rc = sock_read (conn_fd, log, sizeof (log)-1, &n); |
rc = sock_read (conn_fd, log, DIM (log)-1, &n); |
1050 |
if (rc == SOCKET_ERROR) { |
if (rc == SOCKET_ERROR) { |
1051 |
rc = WPTERR_WINSOCK_SENDKEY; |
rc = WPTERR_WINSOCK_SENDKEY; |
1052 |
goto leave; |
goto leave; |
1053 |
} |
} |
1054 |
|
|
1055 |
log_debug ("kserver_sendkey:\r\n%s\r\n", log); |
log_debug ("kserver_sendkey: read %d bytes\r\n%s\r\n", n, log); |
1056 |
rc = check_hkp_response (log, 0); |
rc = check_hkp_response (log, 0); |
1057 |
if (rc) |
if (rc) |
1058 |
goto leave; |
goto leave; |
1066 |
} |
} |
1067 |
|
|
1068 |
|
|
1069 |
|
/* Check keyserver response. */ |
1070 |
|
static int |
1071 |
|
kserver_search_chkresp (int fd) |
1072 |
|
{ |
1073 |
|
char buf[128]; |
1074 |
|
int n=0; |
1075 |
|
|
1076 |
|
/* parse response 'HTTP/1.0 500 OK' */ |
1077 |
|
if (sock_getline (fd, buf, DIM (buf)-1, &n)) |
1078 |
|
return WPTERR_KEYSERVER_NOTFOUND; |
1079 |
|
|
1080 |
|
log_debug ("kserver_search_chkpresp: %s\r\n", buf); |
1081 |
|
if (strncmp (buf, "HTTP/1.", 7)) |
1082 |
|
return WPTERR_KEYSERVER_NOTFOUND; |
1083 |
|
if (strncmp (buf+(8+1), "200", 3)) |
1084 |
|
return WPTERR_KEYSERVER_NOTFOUND; |
1085 |
|
return 0; |
1086 |
|
} |
1087 |
|
|
1088 |
|
|
1089 |
|
/* End the keyserver search procedure. */ |
1090 |
|
void |
1091 |
|
kserver_search_end (int conn_fd) |
1092 |
|
{ |
1093 |
|
log_debug ("kserver_search_end: fd=%d\r\n", conn_fd); |
1094 |
|
closesocket (conn_fd); |
1095 |
|
} |
1096 |
|
|
1097 |
|
|
1098 |
|
/* Extract the amount of keys from the info record. */ |
1099 |
|
static int |
1100 |
|
count_keys_in_response (char *buf) |
1101 |
|
{ |
1102 |
|
char *p; |
1103 |
|
int recno = 0; |
1104 |
|
int n = 0; |
1105 |
|
|
1106 |
|
/* info:1:4 */ |
1107 |
|
if (strncmp (buf, "info", 4)) |
1108 |
|
return 0; |
1109 |
|
p = strtok (buf, ":"); |
1110 |
|
while (p != NULL) { |
1111 |
|
recno++; |
1112 |
|
if (recno == 3) |
1113 |
|
n = atoi (p); |
1114 |
|
p = strtok (NULL, ":"); |
1115 |
|
} |
1116 |
|
return n; |
1117 |
|
} |
1118 |
|
|
1119 |
|
|
1120 |
|
/* Start the keyserver search. |
1121 |
|
Connect to host @hostname and port @port. |
1122 |
|
The pattern are given in @pattern. |
1123 |
|
The socket is returned in @conn_fd and @nkeys contains |
1124 |
|
the amount of keys which were found. */ |
1125 |
int |
int |
1126 |
kserver_search_init (const char *hostname, WORD port, |
kserver_search_begin (const char *hostname, WORD port, |
1127 |
const char *keyid, int *conn_fd) |
const char *pattern, int *conn_fd, int *nkeys) |
1128 |
{ |
{ |
1129 |
|
const char *fmt; |
1130 |
char *request = NULL; |
char *request = NULL; |
1131 |
char *enc_keyid = NULL; |
char *enc_patt = NULL; |
1132 |
int n = 0; |
char status[128]; |
1133 |
int rc, sock_fd; |
int rc, sock_fd; |
1134 |
|
int reqlen; |
1135 |
|
|
1136 |
|
*conn_fd = 0; |
1137 |
|
|
1138 |
rc = kserver_connect (hostname, port, &sock_fd); |
rc = kserver_connect (hostname, port, &sock_fd); |
1139 |
if (rc) { |
if (rc) |
|
*conn_fd = 0; |
|
1140 |
goto leave; |
goto leave; |
|
} |
|
1141 |
|
|
1142 |
enc_keyid = URL_encode (keyid, strlen (keyid), NULL); |
enc_patt = URL_encode (pattern, strlen (pattern), NULL); |
1143 |
n=300; |
reqlen = strlen (enc_patt) + strlen (hostname) + 32 + 1; |
1144 |
request = new char[n+1]; |
|
1145 |
if (!request) |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
1146 |
BUG (0); |
fmt = "GET http://%s:%d/pks/lookup?options=mr&op=index&search=%s HTTP/1.0\r\n" |
1147 |
|
"Proxy-Authorization: Basic %s\r\n\r\n"; |
1148 |
if (proxy.host && proxy.user) { |
reqlen += strlen (proxy.base64_user) + strlen (fmt) + 1; |
1149 |
_snprintf (request, n, |
request = safe_alloc (reqlen+1); |
1150 |
"GET http://%s:%d/pks/lookup?op=index&search=%s HTTP/1.0\r\n" |
_snprintf (request, reqlen, fmt, skip_type_prefix (hostname), port, |
1151 |
"Proxy-Authorization: Basic %s\r\n\r\n", |
enc_patt, proxy.base64_user); |
|
skip_type_prefix (hostname), port, enc_keyid, proxy.base64_user); |
|
1152 |
} |
} |
1153 |
else if (proxy.host) { |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
1154 |
_snprintf (request, n, |
fmt = "GET http://%s:%d/pks/lookup?options=mr&op=index&search=%s HTTP/1.0\r\n\r\n"; |
1155 |
"GET http://%s:%d/pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n", |
reqlen += strlen (fmt)+1; |
1156 |
skip_type_prefix (hostname), port, enc_keyid); |
request = safe_alloc (reqlen+1); |
1157 |
|
_snprintf (request, reqlen, skip_type_prefix (hostname), port, |
1158 |
|
enc_patt); |
1159 |
} |
} |
1160 |
else { |
else { |
1161 |
_snprintf (request, n, |
fmt = "GET /pks/lookup?options=mr&op=index&search=%s HTTP/1.0\r\n\r\n"; |
1162 |
"GET /pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n", enc_keyid); |
reqlen += strlen (fmt)+1; |
1163 |
|
request = safe_alloc (reqlen+1); |
1164 |
|
_snprintf (request, reqlen, fmt, enc_patt); |
1165 |
} |
} |
1166 |
|
|
1167 |
log_debug ("kserver_search_init:\r\n%s\r\n", request); |
log_debug ("kserver_search_begin:\r\n%s\r\n", request); |
|
|
|
1168 |
if (sock_write (sock_fd, request, strlen (request)) == SOCKET_ERROR) { |
if (sock_write (sock_fd, request, strlen (request)) == SOCKET_ERROR) { |
1169 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1170 |
goto leave; |
goto leave; |
1171 |
} |
} |
1172 |
|
|
1173 |
|
rc = kserver_search_chkresp (sock_fd); |
1174 |
|
if (rc) { |
1175 |
|
closesocket (sock_fd); |
1176 |
|
sock_fd = 0; |
1177 |
|
goto leave; |
1178 |
|
} |
1179 |
|
|
1180 |
|
/* Skip all lines until we reach the "info:" record. */ |
1181 |
|
for (;;) { |
1182 |
|
if (sock_getline (sock_fd, status, DIM (status)-1, &reqlen)) { |
1183 |
|
log_debug ("kserver_search_begin: retrieving status line failed.\r\n"); |
1184 |
|
closesocket (sock_fd); |
1185 |
|
sock_fd = 0; |
1186 |
|
rc = WPTERR_GENERAL; |
1187 |
|
break; |
1188 |
|
} |
1189 |
|
if (!strncmp (status, "info:", 5)) |
1190 |
|
break; |
1191 |
|
} |
1192 |
|
|
1193 |
|
if (!rc) |
1194 |
|
*nkeys = count_keys_in_response (status); |
1195 |
*conn_fd = sock_fd; |
*conn_fd = sock_fd; |
1196 |
|
|
1197 |
leave: |
leave: |
1198 |
free_if_alloc (request); |
free_if_alloc (request); |
1199 |
free_if_alloc (enc_keyid); |
free_if_alloc (enc_patt); |
1200 |
return rc; |
return rc; |
1201 |
} |
} |
1202 |
|
|
1203 |
|
|
1204 |
/* Check keyserver response. */ |
/* Parse a single pub record returned by the keyserver. */ |
1205 |
int |
static void |
1206 |
kserver_search_chkresp (int fd) |
parse_pub_record (keyserver_key_s *key, char *buf) |
1207 |
{ |
{ |
1208 |
char buf[128]; |
enum pub_rec_t {ID=1, KEYID, ALGO, SIZE, CREATE, EXPIRE}; |
1209 |
int n=0; |
char *p; |
1210 |
|
int recno = 0; |
1211 |
/* parse response 'HTTP/1.0 500 OK' */ |
int off = 0; |
|
if (sock_getline (fd, buf, 127, &n)) |
|
|
return WPTERR_KEYSERVER_NOTFOUND; |
|
1212 |
|
|
1213 |
log_debug ("kserver_search_chkpresp: %s\r\n", buf); |
/* pub:BF3DF9B4:17:1024:925411133:: */ |
1214 |
if (strncmp (buf, "HTTP/1.", 7)) |
p = strtok (buf, ":"); |
1215 |
return WPTERR_KEYSERVER_NOTFOUND; |
while (p != NULL) { |
1216 |
if (strncmp (buf+(8+1), "200", 3)) |
recno++; |
1217 |
return WPTERR_KEYSERVER_NOTFOUND; |
switch (recno) { |
1218 |
return 0; |
case ID: |
1219 |
} |
break; |
1220 |
|
|
1221 |
|
case KEYID: |
1222 |
|
/* If for any reason the returned record actually contains |
1223 |
|
a fingerprint instead of a key ID, we truncate the fpr. */ |
1224 |
|
off = strlen (p) == 40? 32 : 0; |
1225 |
|
free_if_alloc (key->keyid); |
1226 |
|
key->keyid = m_strdup (p+off); |
1227 |
|
break; |
1228 |
|
|
1229 |
|
case ALGO: |
1230 |
|
key->algo = atoi (p); |
1231 |
|
break; |
1232 |
|
|
1233 |
|
case SIZE: |
1234 |
|
key->bits = atoi (p); |
1235 |
|
break; |
1236 |
|
|
1237 |
/* Convert an iso date @iso_date (YYYY-MM-DD) into the locale |
case CREATE: |
1238 |
representation and store it into @loc_date. |
key->creation = strtoul (p, NULL, 10); |
1239 |
Return value: 0 on success. */ |
break; |
|
static int |
|
|
parse_iso_date (const char *iso_date, char *loc_date, size_t loclen) |
|
|
{ |
|
|
SYSTEMTIME st; |
|
|
char buf[16] = {0}, *p; |
|
|
int pos=0; |
|
1240 |
|
|
1241 |
strncpy (buf, iso_date, sizeof (buf)-1); |
case EXPIRE: |
1242 |
p = strtok (buf, "-"); |
key->expires = strtoul (p, NULL, 10); |
1243 |
while (p != NULL) { |
break; |
|
switch (pos) { |
|
|
case 0: st.wYear = (WORD)atoi (p); pos++; break; |
|
|
case 1: st.wMonth = (WORD)atoi (p); pos++; break; |
|
|
case 2: st.wDay = (WORD)atoi (p); pos++; break; |
|
|
default: break; |
|
1244 |
} |
} |
1245 |
p = strtok (NULL, "-"); |
p = strtok (NULL, ":"); |
1246 |
} |
} |
|
if (pos != 3) |
|
|
return -1; |
|
|
|
|
|
if (!GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, |
|
|
NULL, loc_date, loclen)) |
|
|
return -1; |
|
|
return 0; |
|
1247 |
} |
} |
1248 |
|
|
1249 |
|
|
1250 |
int |
/* Parse a single user id returned by the keyserver. */ |
1251 |
kserver_search (int fd, keyserver_key *key) |
static void |
1252 |
|
parse_uid_record (keyserver_key_s *key, char *buf) |
1253 |
{ |
{ |
1254 |
char buf[1024], *p; |
enum uid_rec_t {ID=1, UID, CREATE, EXPIRE}; |
1255 |
int uidlen, nbytes, pos = 0; |
keyserver_uid_s *u, *n; |
1256 |
|
char *p, *raw; |
1257 |
|
int recno = 0; |
1258 |
|
|
1259 |
|
/* uid:Timo Schulz <[email protected]>:1138440360:: */ |
1260 |
|
u = new keyserver_uid_s; |
1261 |
|
memset (u, 0, sizeof *u); |
1262 |
|
|
1263 |
log_debug ("keyserver_search:\r\n"); |
p = strtok (buf, ":"); |
1264 |
|
while (p != NULL) { |
1265 |
if (sock_getline (fd, buf, sizeof (buf) - 1, &nbytes)) |
recno++; |
|
return WPTERR_GENERAL; |
|
1266 |
|
|
1267 |
log_debug ("%s\r\n", buf); |
switch (recno) { |
1268 |
|
case ID: |
1269 |
if (!strncmp (buf, "pub", 3)) { |
break; |
1270 |
int revoked = strstr (buf, "KEY REVOKED") != NULL? 1 : 0; |
|
1271 |
key->bits = atol (buf+3); |
case UID: |
1272 |
p = strchr (buf, '>'); |
unhexify_buffer (p, &raw); |
1273 |
if (!p) |
u->uid = utf8_to_native (raw); |
1274 |
goto fail; |
free_if_alloc (raw); |
1275 |
pos = p - buf + 1; |
break; |
|
memcpy (key->keyid, buf + pos, 8); |
|
|
key->keyid[8] = '\0'; |
|
|
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; |
|
|
} |
|
1276 |
|
|
1277 |
if (uidlen > sizeof (key->uid)-1) |
case CREATE: |
1278 |
uidlen = sizeof (key->uid)-1; |
u->creation = strtoul (p, NULL, 10); |
1279 |
memcpy (key->uid, p, uidlen); |
break; |
1280 |
key->uid[uidlen] = '\0'; |
|
1281 |
strcat (key->uid, ">"); |
case EXPIRE: |
1282 |
p = strchr (key->uid, '&'); |
u->expires = strtoul (p, NULL, 10); |
1283 |
if (p) { |
break; |
|
key->uid[(p-key->uid)] = '<'; |
|
|
memmove (key->uid+(p-key->uid)+1, key->uid+(p-key->uid)+4, |
|
|
strlen (key->uid)-3); |
|
1284 |
} |
} |
|
return 0; |
|
|
} |
|
1285 |
|
|
1286 |
fail: |
p = strtok (NULL, ":"); |
1287 |
key->bits = 0; |
} |
1288 |
memset (key, 0, sizeof *key); |
if (!key->uids) |
1289 |
return 0; |
key->uids = u; |
1290 |
|
else { |
1291 |
|
for (n = key->uids; n->next; n=n->next) |
1292 |
|
; |
1293 |
|
n->next = u; |
1294 |
|
} |
1295 |
} |
} |
1296 |
|
|
1297 |
|
|
1298 |
|
/* Peek the next 3 bytes to check if the next line |
1299 |
|
would be a new "pub" record. In this case, return |
1300 |
|
-1 as an EOF indication, 0 otherwise. */ |
1301 |
static int |
static int |
1302 |
add_node( keyserver_cfgfile *cfg, const char *entry ) |
is_key_eof (int fd) |
1303 |
{ |
{ |
1304 |
keyserver_node *node; |
char buf[64]; |
1305 |
char *p = NULL; |
int n; |
|
|
|
|
p = strchr( entry, '=' ); |
|
|
if( p == NULL ) |
|
|
return WPTERR_GENERAL; |
|
|
node = new keyserver_node; |
|
|
if( !node ) |
|
|
BUG (NULL); |
|
|
|
|
|
memset( node, 0, sizeof *node ); |
|
|
node->used = 1; |
|
|
|
|
|
node->host.used = 1; |
|
|
node->host.proto = proto_from_URL( entry ); |
|
|
memcpy( node->host.name, entry+7, p-entry-7 ); |
|
|
node->next = cfg->list; |
|
|
cfg->list = node; |
|
|
|
|
|
return 0; |
|
|
} /* add_node */ |
|
|
|
|
1306 |
|
|
1307 |
void |
n = recv (fd, buf, 3, MSG_PEEK); |
1308 |
kserver_change_proxy( keyserver_proxy_ctx *ctx ) |
if (n < 3 || strncmp (buf, "pub", 3)) |
1309 |
{ |
return 0; |
1310 |
proxy.port = ctx->port; |
return -1; |
1311 |
free_if_alloc (proxy.host); |
} |
|
proxy.host = ctx->host? m_strdup( ctx->host ) : NULL; |
|
|
free_if_alloc( proxy.pass ); |
|
|
proxy.pass = ctx->pass? m_strdup( ctx->pass ) : NULL; |
|
|
free_if_alloc( proxy.user ); |
|
|
proxy.user = ctx->user? m_strdup( ctx->user ) : NULL; |
|
|
set_reg_proxy_prefs( proxy.host, proxy.port, proxy.user, proxy.pass ); |
|
|
} /* kserver_change_proxy */ |
|
1312 |
|
|
1313 |
|
|
1314 |
|
/* Return the next key from the search response. */ |
1315 |
int |
int |
1316 |
kserver_read_config( const char *fname, keyserver_cfgfile **ret_cfg ) |
kserver_search_next (int fd, keyserver_key_s **r_key) |
1317 |
{ |
{ |
1318 |
FILE *fp; |
keyserver_uid_s *uid, *u = NULL; |
1319 |
keyserver_cfgfile *cfg; |
keyserver_key_s *key; |
1320 |
char buf[256]; |
char buf[512]; |
1321 |
int rc = 0; |
int n = 0; |
1322 |
|
long max = 0; |
1323 |
*ret_cfg = NULL; |
|
1324 |
fp = fopen( fname, "rb" ); |
log_debug ("keyserver_search_next:\r\n"); |
1325 |
if( fp == NULL ) |
*r_key = NULL; |
1326 |
return WPTERR_FILE_OPEN; |
|
1327 |
|
key = new keyserver_key_s; |
1328 |
cfg = new keyserver_cfgfile; |
memset (key, 0, sizeof *key); |
1329 |
if ( !cfg ) |
for (;;) { |
1330 |
BUG( NULL ); |
if (sock_getline (fd, buf, DIM (buf)-1, &n)) |
1331 |
memset( cfg, 0, sizeof *cfg ); |
break; |
1332 |
|
/*log_debug ("record: '%s'\r\n", buf); */ |
1333 |
while ( !feof( fp ) ) { |
if (!strncmp (buf, "pub", 3)) |
1334 |
if ( fgets( buf, sizeof(buf)-1, fp ) == NULL ) |
parse_pub_record (key, buf); |
1335 |
break; |
else |
1336 |
if ( *buf == '\r' || *buf == '\n' || *buf == '#' ) |
parse_uid_record (key, buf); |
1337 |
continue; |
if (is_key_eof (fd)) |
1338 |
buf[strlen(buf) -2] = '\0'; |
break; |
|
if ( !strncmp( buf, "use_proxy=", 10 ) ) { |
|
|
char *p = strchr(buf, ':'); |
|
|
if (!p) |
|
|
continue; |
|
|
cfg->proxy.port = atol( buf+(p-buf+1) ); |
|
|
buf[p-buf] = '\0'; |
|
|
cfg->proxy.host = m_strdup( buf+10 ); |
|
|
} |
|
|
else if ( !strncmp( buf, "proxy_user=", 11 ) ) |
|
|
cfg->proxy.user = m_strdup( buf+11 ); |
|
|
else if ( !strncmp( buf, "proxy_pass=", 11 ) ) |
|
|
cfg->proxy.pass = m_strdup( buf+11 ); |
|
|
if ( !strncmp( buf, "http://", 7 ) |
|
|
|| !strncmp( buf, "hkp://", 6 ) |
|
|
|| !strncmp( buf, "ldap://", 7 ) ) { |
|
|
add_node( cfg, buf ); |
|
|
cfg->nlist++; |
|
|
} |
|
1339 |
} |
} |
|
|
|
|
fclose( fp ); |
|
|
*ret_cfg = cfg; |
|
|
|
|
|
return rc; |
|
|
} /* kserver_read_config */ |
|
1340 |
|
|
1341 |
|
/* the uid with the newest self sig is used as the |
1342 |
|
primary user id. */ |
1343 |
|
for (uid = key->uids; uid; uid = uid->next) { |
1344 |
|
if (uid->creation > max) { |
1345 |
|
max = uid->creation; |
1346 |
|
u = uid; |
1347 |
|
} |
1348 |
|
} |
1349 |
|
|
1350 |
int |
key->main_uid = u? u : key->uids; |
1351 |
kserver_write_config( const char * fname, keyserver_cfgfile * cfg ) |
*r_key = key; |
|
{ |
|
|
|
|
1352 |
return 0; |
return 0; |
1353 |
} |
} |
1354 |
|
|
1355 |
|
|
1356 |
|
/* Release the keyserver key @key and all its elements. */ |
1357 |
void |
void |
1358 |
kserver_cfgfile_release( keyserver_cfgfile *cfg ) |
kserver_release_key (keyserver_key_s *key) |
1359 |
{ |
{ |
1360 |
keyserver_node *k, *k2; |
keyserver_uid_s *u; |
1361 |
|
|
1362 |
proxy_release( &cfg->proxy ); |
while (key->uids) { |
1363 |
for( k = cfg->list; cfg->nlist--; k = k2 ) { |
u = key->uids->next; |
1364 |
k2 = k->next; |
free_if_alloc (key->uids->uid); |
1365 |
free_if_alloc( k ); |
free_if_alloc (key->uids); |
1366 |
|
key->uids = u; |
1367 |
} |
} |
1368 |
free_if_alloc( cfg ); |
free_if_alloc (key->keyid); |
1369 |
} /* kserver_cfgfile_release */ |
free_if_alloc (key); |
1370 |
|
} |
1371 |
|
|
1372 |
|
|
1373 |
/* Release mbmers in the proxy context @ctx. */ |
/* Release mbmers in the proxy context @ctx. */ |
1374 |
void |
void |
1375 |
proxy_release( keyserver_proxy_ctx *ctx ) |
kserver_proxy_release (keyserver_proxy_t ctx) |
1376 |
{ |
{ |
1377 |
free_if_alloc (ctx->host); |
free_if_alloc (ctx->host); |
1378 |
free_if_alloc (ctx->pass); |
free_if_alloc (ctx->pass); |
1379 |
free_if_alloc (ctx->user); |
free_if_alloc (ctx->user); |
1380 |
|
ctx->port = ctx->proto = 0; |
1381 |
} |
} |
1382 |
|
|
1383 |
|
|
1408 |
} |
} |
1409 |
|
|
1410 |
|
|
1411 |
/* Receive an key via LDAP from host @host with the keyid @keyid. |
static FILE* |
1412 |
@key contains the key on success. */ |
do_spawn_ldap_helper (const char *host, const char *keyid) |
1413 |
int |
{ |
1414 |
ldap_recvkey (const char *host, const char *keyid, char *key, int maxkeylen) |
FILE *fp = NULL; |
1415 |
{ |
char *p, *sep; |
1416 |
FILE *inp; |
char *ksprg; |
1417 |
DWORD n; |
char outf[256], inf[256]; |
1418 |
const char *s; |
size_t n; |
|
char *ksprg = NULL, *p = NULL, *sep; |
|
|
char temp[512], outf[512]; |
|
|
int start_key = 0, failed = 0; |
|
|
int rc = 0; |
|
1419 |
|
|
1420 |
p = get_gnupg_prog (); |
p = get_gnupg_prog (); |
1421 |
n = strlen (p) + 1 + 128; |
n = strlen (p) + 1 + 128; |
1422 |
ksprg = new char[n+1]; |
ksprg = safe_alloc (n+1); |
1423 |
if (!ksprg) |
if (!ksprg) |
1424 |
BUG (0); |
BUG (0); |
1425 |
sep = strrchr (p, '\\'); |
sep = strrchr (p, '\\'); |
1431 |
if (file_exist_check (ksprg)) { |
if (file_exist_check (ksprg)) { |
1432 |
log_box ("LDAP Keyserver Plugin", MB_ERR, |
log_box ("LDAP Keyserver Plugin", MB_ERR, |
1433 |
"%s: could not find LDAP keyserver module!", ksprg); |
"%s: could not find LDAP keyserver module!", ksprg); |
1434 |
rc = -1; |
fp = NULL; |
1435 |
goto leave; |
goto leave; |
1436 |
} |
} |
1437 |
GetTempPath (sizeof (temp)-1, temp); |
get_temp_name (outf, DIM (outf)-1, keyid); |
1438 |
_snprintf (outf, sizeof (outf)-1, "%s%s.out", temp, keyid); |
get_temp_name (inf, DIM (inf)-1, NULL); |
1439 |
strcat (temp, keyid); |
fp = fopen (inf, "w+b"); |
1440 |
inp = fopen (temp, "w+b"); |
if (!fp) { |
1441 |
if (!inp) { |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", inf, |
|
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", temp, |
|
1442 |
winpt_strerror (WPTERR_FILE_OPEN)); |
winpt_strerror (WPTERR_FILE_OPEN)); |
|
rc = -1; |
|
1443 |
goto leave; |
goto leave; |
1444 |
} |
} |
1445 |
fprintf (inp, |
fprintf (fp, |
1446 |
"VERSION 1\n" |
"VERSION 1\n" |
1447 |
"PROGRAM 1.4.3-cvs\n" |
"PROGRAM 1.4.3-cvs\n" |
1448 |
"SCHEME ldap\n" |
"SCHEME ldap\n" |
1451 |
"\n" |
"\n" |
1452 |
"%s\n", |
"%s\n", |
1453 |
host? skip_type_prefix (host): "64.94.85.200", keyid); |
host? skip_type_prefix (host): "64.94.85.200", keyid); |
1454 |
fclose (inp); |
fclose (fp); |
1455 |
|
|
1456 |
p = new char[strlen (ksprg) + strlen (temp) + strlen (outf) + 32]; |
p = safe_alloc (strlen (ksprg) + strlen (inf) + strlen (outf) + 32); |
1457 |
if (!p) |
sprintf (p, "%s -o %s %s", ksprg, outf, inf); |
|
BUG (NULL); |
|
|
sprintf (p, "%s -o %s %s", ksprg, outf, temp); |
|
1458 |
if (spawn_application (p)) { |
if (spawn_application (p)) { |
1459 |
rc = -1; |
fp = NULL; |
1460 |
goto leave; |
goto leave; |
1461 |
} |
} |
1462 |
DeleteFile (temp); |
fp = fopen (outf, "rb"); |
1463 |
|
if (!fp) |
|
inp = fopen (outf, "rb"); |
|
|
if (!inp) { |
|
1464 |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf, |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf, |
1465 |
winpt_strerror (WPTERR_FILE_OPEN)); |
winpt_strerror (WPTERR_FILE_OPEN)); |
1466 |
rc = -1; |
|
1467 |
goto leave; |
leave: |
1468 |
} |
DeleteFile (inf); |
1469 |
memset (key, 0, maxkeylen); |
DeleteFile (outf); |
1470 |
while (!feof (inp)) { |
free_if_alloc (p); |
1471 |
s = fgets (temp, sizeof (temp)-1, inp); |
free_if_alloc (ksprg); |
1472 |
|
return fp; |
1473 |
|
} |
1474 |
|
|
1475 |
|
/* Receive an key via LDAP from host @host with the keyid @keyid. |
1476 |
|
@key contains the key on success. */ |
1477 |
|
int |
1478 |
|
ldap_recvkey (const char *host, const char *keyid, |
1479 |
|
char **r_key, int *r_keylen) |
1480 |
|
{ |
1481 |
|
gpgme_data_t raw; |
1482 |
|
FILE *fp; |
1483 |
|
const char *s; |
1484 |
|
char buf[512]; |
1485 |
|
int start_key = 0, failed = 0; |
1486 |
|
int rc = 0; |
1487 |
|
|
1488 |
|
fp = do_spawn_ldap_helper (host, keyid); |
1489 |
|
if (!fp) |
1490 |
|
return WPTERR_GENERAL; |
1491 |
|
|
1492 |
|
if (gpgme_data_new (&raw)) |
1493 |
|
BUG (0); |
1494 |
|
while (!feof (fp)) { |
1495 |
|
s = fgets (buf, DIM (buf)-1, fp); |
1496 |
if (!s) |
if (!s) |
1497 |
break; |
break; |
1498 |
if (strstr (s, "KEY") && strstr (s, "FAILED")) { |
if (strstr (s, "KEY") && strstr (s, "FAILED")) { |
1502 |
if (!start_key && strstr (s, "KEY") && strstr (s, "BEGIN")) { |
if (!start_key && strstr (s, "KEY") && strstr (s, "BEGIN")) { |
1503 |
start_key = 1; |
start_key = 1; |
1504 |
continue; |
continue; |
1505 |
} |
} |
1506 |
if (!start_key) |
if (!start_key) |
1507 |
continue; |
continue; |
1508 |
strcat (key, temp); |
gpgme_data_write (raw, buf, strlen (buf)); |
1509 |
maxkeylen -= strlen (temp); |
if (strstr (s, "KEY") && strstr (s, "END")) |
|
if (maxkeylen < 0 || (strstr (s, "KEY") && strstr (s, "END"))) |
|
1510 |
break; |
break; |
1511 |
} |
} |
1512 |
fclose (inp); |
fclose (fp); |
1513 |
|
|
1514 |
leave: |
if (failed) |
|
if (failed || !strlen (key)) |
|
1515 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
1516 |
DeleteFile (outf); |
*r_key = data_release_and_get_mem (raw, r_keylen); |
|
free_if_alloc (p); |
|
|
free_if_alloc (ksprg); |
|
1517 |
return rc; |
return rc; |
1518 |
} |
} |
1519 |
|
|
1520 |
|
|
|
static int |
|
|
finger_readline (int fd, char *buf, int nbytes) |
|
|
{ |
|
|
char c = 0; |
|
|
int n, pos = 0; |
|
|
|
|
|
while (nbytes > 0) { |
|
|
n = recv (fd, &c, 1, 0); |
|
|
if (n <= 0) |
|
|
break; |
|
|
if (c == '\n') |
|
|
break; |
|
|
buf[pos++] = c; |
|
|
nbytes--; |
|
|
} |
|
|
if (nbytes > 0) |
|
|
buf[pos++] = '\0'; |
|
|
if (n <= 0) |
|
|
pos = n; |
|
|
return pos; |
|
|
} |
|
|
|
|
|
|
|
1521 |
/* Receive an key via FINGER from host @host with the user @user. |
/* Receive an key via FINGER from host @host with the user @user. |
1522 |
On success @key contains the key. */ |
On success @key contains the key. */ |
1523 |
int |
int |
1524 |
finger_recvkey (const char *host, const char *user, char *key, int maxkeylen) |
finger_recvkey (const char *host, const char *user, |
1525 |
|
char **r_key, int *r_keylen) |
1526 |
{ |
{ |
1527 |
struct hostent * hp; |
gpgme_data_t raw; |
1528 |
struct sockaddr_in saddr; |
char buf[256+1]; |
|
char buf[128]; |
|
1529 |
int fd, nread; |
int fd, nread; |
1530 |
int start_key = 0; |
int start_key = 0; |
1531 |
int rc=0; |
int rc=0; |
1532 |
|
|
1533 |
fd = socket (PF_INET, SOCK_STREAM, 0); |
rc = kserver_connect (host, FINGER_PORT, &fd); |
1534 |
if (fd == -1) |
if (rc) |
1535 |
return WPTERR_WINSOCK_SOCKET; |
return rc; |
|
hp = gethostbyname (skip_type_prefix (host)); |
|
|
if (!hp) { |
|
|
closesocket (fd); |
|
|
return WPTERR_WINSOCK_RESOLVE; |
|
|
} |
|
1536 |
|
|
1537 |
memset (&saddr, 0, sizeof (saddr)); |
sock_write (fd, user, strlen (user)); |
1538 |
saddr.sin_family = AF_INET; |
sock_write (fd, "\r\n", 2); |
1539 |
saddr.sin_port = htons (FINGER_PORT); |
|
1540 |
saddr.sin_addr = *(struct in_addr *)hp->h_addr; |
if (gpgme_data_new (&raw)) |
1541 |
if (connect (fd, (struct sockaddr *)&saddr, sizeof (saddr))) { |
BUG (0); |
1542 |
closesocket (fd); |
|
1543 |
return WPTERR_WINSOCK_CONNECT; |
for (;;) { |
1544 |
} |
if (sock_getline (fd, buf, DIM (buf)-1, &nread)) |
1545 |
send (fd, user, strlen (user), 0); |
break; |
|
send (fd, "\r\n", 2, 0); |
|
|
|
|
|
memset (key, 0, maxkeylen); |
|
|
while (maxkeylen > 0) { |
|
|
nread = finger_readline (fd, buf, sizeof (buf)-1); |
|
|
if (nread <= 0) |
|
|
break; |
|
1546 |
strcat (buf, "\n"); |
strcat (buf, "\n"); |
1547 |
if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) { |
if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) { |
1548 |
strcat (key, buf); |
gpgme_data_write (raw, buf, nread); |
1549 |
start_key = 1; |
start_key = 1; |
|
maxkeylen -= nread; |
|
1550 |
} |
} |
1551 |
else if (strstr (buf, "END PGP PUBLIC KEY BLOCK" ) && start_key) { |
else if (strstr (buf, "END PGP PUBLIC KEY BLOCK" ) && start_key) { |
1552 |
strcat (key, buf); |
gpgme_data_write (raw, buf, nread); |
1553 |
start_key--; |
start_key--; |
|
maxkeylen -= nread; |
|
1554 |
break; |
break; |
1555 |
} |
} |
1556 |
else if (start_key) { |
else if (start_key) |
1557 |
strcat (key, buf); |
gpgme_data_write (raw, buf, nread); |
|
maxkeylen -= nread; |
|
|
} |
|
1558 |
} |
} |
1559 |
|
|
1560 |
closesocket (fd); |
closesocket (fd); |
1561 |
if (start_key != 0) |
if (start_key != 0) |
1562 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
1563 |
|
*r_key = data_release_and_get_mem (raw, r_keylen); |
1564 |
return rc; |
return rc; |
1565 |
} |
} |
1566 |
|
|
1580 |
} |
} |
1581 |
return 0; |
return 0; |
1582 |
} |
} |
1583 |
|
|
1584 |
|
|
1585 |
|
/* Split the URL @r_keyserver into the host and the port |
1586 |
|
part if possible. */ |
1587 |
|
gpgme_error_t |
1588 |
|
parse_keyserver_url (char **r_keyserver, unsigned short *r_port) |
1589 |
|
{ |
1590 |
|
char *p; |
1591 |
|
char *url = *r_keyserver; |
1592 |
|
int off = 0; |
1593 |
|
|
1594 |
|
/* no port is given so use the default port. */ |
1595 |
|
p = strrchr (url, ':'); |
1596 |
|
if (p == strchr (url, ':')) { |
1597 |
|
int port = port_from_proto (proto_from_URL (url)); |
1598 |
|
if (!port) |
1599 |
|
port = HKP_PORT; |
1600 |
|
*r_port = port; |
1601 |
|
return 0; |
1602 |
|
} |
1603 |
|
|
1604 |
|
if (url[(p-url)-1] == '/') /* remove / in .de/:11371 */ |
1605 |
|
off = 1; |
1606 |
|
|
1607 |
|
*r_keyserver = substr (url, 0, (p-url)-off); |
1608 |
|
*r_port = atoi (url+(p-url)+1); |
1609 |
|
free_if_alloc (url); |
1610 |
|
return 0; |
1611 |
|
} |