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> |
34 |
#include "wptTypes.h" |
#include "wptTypes.h" |
35 |
#include "wptNLS.h" |
#include "wptNLS.h" |
36 |
#include "wptW32API.h" |
#include "wptW32API.h" |
|
#include "wptVersion.h" |
|
37 |
#include "wptGPG.h" |
#include "wptGPG.h" |
38 |
#include "wptRegistry.h" |
#include "wptRegistry.h" |
39 |
|
|
40 |
static char base64code[] = |
/* Map net_errno to a winsock error. */ |
41 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
#define net_errno ((int)WSAGetLastError ()) |
42 |
|
|
43 |
|
|
44 |
keyserver server[MAX_KEYSERVERS] = {0}; |
keyserver server[MAX_KEYSERVERS] = {0}; |
45 |
static keyserver_proxy_ctx proxy = {0}; |
keyserver_proxy_s proxy = {0}; |
46 |
static const char * server_list[] = { |
static const char *server_list[] = { |
|
"hkp://wwwkeys.nl.pgp.net", |
|
|
"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", |
|
47 |
"hkp://gnv.us.ks.cryptnet.net", |
"hkp://gnv.us.ks.cryptnet.net", |
48 |
|
"hkp://keyserver.kjsl.com", |
49 |
|
"hkp://sks.keyserver.penguin.de", |
50 |
"hkp://subkeys.pgp.net", |
"hkp://subkeys.pgp.net", |
51 |
"ldap://keyserver.pgp.com", |
"ldap://keyserver.pgp.com", |
52 |
NULL |
NULL |
53 |
}; |
}; |
|
static char hkp_errmsg[1024]; |
|
|
static int hkp_err = 0; |
|
|
static u32 conf_timestamp = 0; |
|
|
char * default_keyserver = NULL; |
|
|
unsigned short default_keyserver_port = 0; |
|
54 |
|
|
55 |
|
|
56 |
|
static char hkp_errmsg[1024]; /* Holds the error message from the server */ |
57 |
|
static int hkp_err = 0; /* != 0 indicates an error occurred. */ |
58 |
|
|
59 |
|
/* Default keyserver and port. */ |
60 |
|
char *default_keyserver = NULL; |
61 |
|
WORD default_keyserver_port = 0; |
62 |
|
|
63 |
|
/* Default socket timeout. */ |
64 |
|
static int default_socket_timeout = 6; |
65 |
|
|
66 |
|
|
67 |
|
/* Wrapper for safe memory allocation. */ |
68 |
|
static char* |
69 |
|
safe_alloc (DWORD n) |
70 |
|
{ |
71 |
|
char *p; |
72 |
|
|
73 |
|
p = new char[n+1]; |
74 |
|
if (!p) |
75 |
|
BUG (0); |
76 |
|
memset (p, 0, n); |
77 |
|
return p; |
78 |
|
} |
79 |
|
|
80 |
|
/* Basic64 encode the input @inbuf to @outbuf. */ |
81 |
static void |
static void |
82 |
base64_encode( const char *inputstr, char *outputstr, int maxlen ) |
base64_encode (const char *inbuf, char *outbuf) |
83 |
{ |
{ |
84 |
int index = 0, temp = 0, res = 0, i = 0, inputlen = 0, len = 0; |
char base64code[] = |
85 |
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
86 |
|
int index = 0, temp = 0, res = 0; |
87 |
|
int i = 0, inputlen = 0, len = 0; |
88 |
|
|
89 |
inputlen = strlen (inputstr); /* fixme: check if len > maxlen */ |
inputlen = strlen (inbuf); |
90 |
for (i = 0; i <inputlen; i++) { |
for (i = 0; i < inputlen; i++) { |
91 |
res = temp; |
res = temp; |
92 |
res = (res << 8) | (inputstr[i] & 0xFF); |
res = (res << 8) | (inbuf[i] & 0xFF); |
93 |
switch (index++) { |
switch (index++) { |
94 |
case 0: outputstr[len++] = base64code[res >> 2 & 0x3F]; res &= 0x3; break; |
case 0: |
95 |
case 1: outputstr[len++] = base64code[res >> 4 & 0x3F]; res &= 0xF; break; |
outbuf[len++] = base64code[res >> 2 & 0x3F]; |
96 |
case 2: outputstr[len++] = base64code[res >> 6 & 0x3F]; |
res &= 0x3; |
97 |
outputstr[len++] = base64code[res & 0x3F]; res = index = 0; break; |
break; |
98 |
|
case 1: |
99 |
|
outbuf[len++] = base64code[res >> 4 & 0x3F]; |
100 |
|
res &= 0xF; |
101 |
|
break; |
102 |
|
case 2: |
103 |
|
outbuf[len++] = base64code[res >> 6 & 0x3F]; |
104 |
|
outbuf[len++] = base64code[res & 0x3F]; |
105 |
|
res = index = 0; |
106 |
|
break; |
107 |
} |
} |
108 |
temp = res; |
temp = res; |
109 |
} |
} |
110 |
|
|
111 |
switch( index ) { |
if (index == 2) { |
112 |
case 0: break; |
outbuf[len++] = base64code[temp << 2 & 0x3F]; |
113 |
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++] = '='; |
|
114 |
} |
} |
115 |
|
else if (index == 1) { |
116 |
outputstr[len] = '\0'; |
outbuf[len++] = base64code[temp << 4 & 0x3F]; |
117 |
} /* base64_encode */ |
outbuf[len++] = '='; |
118 |
|
outbuf[len++] = '='; |
119 |
|
} |
120 |
|
|
121 |
|
outbuf[len] = '\0'; |
122 |
|
} |
123 |
|
|
124 |
|
|
125 |
/* Skip the URL schema and return only the host part of it. */ |
/* Skip the URL schema and return only the host part of it. */ |
126 |
static const char * |
static const char* |
127 |
skip_type_prefix (const char * hostname) |
skip_type_prefix (const char *hostname) |
128 |
{ |
{ |
129 |
if (hostname && !strncmp (hostname, "http://", 7)) |
if (hostname && !strncmp (hostname, "http://", 7)) |
130 |
hostname += 7; |
hostname += 7; |
142 |
Return 0 on success. */ |
Return 0 on success. */ |
143 |
static int |
static int |
144 |
check_hkp_response (const char *resp, int recv) |
check_hkp_response (const char *resp, int recv) |
145 |
{ |
{ |
146 |
|
char *p, *end; |
147 |
int ec, len; |
int ec, len; |
|
char *p, * end; |
|
148 |
|
|
149 |
|
log_debug ("check_hkp_response: '%s'\r\n", resp); |
150 |
ec = recv ? WPTERR_WINSOCK_RECVKEY : WPTERR_WINSOCK_SENDKEY; |
ec = recv ? WPTERR_WINSOCK_RECVKEY : WPTERR_WINSOCK_SENDKEY; |
151 |
if (!strstr (resp, "HTTP/1.0 200 OK") && |
if (!strstr (resp, "HTTP/1.0 200 OK") && |
152 |
!strstr (resp, "HTTP/1.1 200 OK")) /* http error */ |
!strstr (resp, "HTTP/1.1 200 OK")) /* http error */ |
158 |
if (p && strlen (p) < sizeof (hkp_errmsg)) { |
if (p && strlen (p) < sizeof (hkp_errmsg)) { |
159 |
end = strstr (p, "</p>"); |
end = strstr (p, "</p>"); |
160 |
len = end? (end - p + 1) : strlen (p); |
len = end? (end - p + 1) : strlen (p); |
161 |
|
memset (hkp_errmsg, 0, sizeof (hkp_errmsg)); |
162 |
strncpy (hkp_errmsg, p, len); |
strncpy (hkp_errmsg, p, len); |
163 |
hkp_err = 1; |
hkp_err = 1; |
164 |
} |
} |
174 |
sock_getline (int fd, char *buf, int buflen, int *nbytes) |
sock_getline (int fd, char *buf, int buflen, int *nbytes) |
175 |
{ |
{ |
176 |
char ch; |
char ch; |
177 |
int count = 0; |
int nread = 0; |
178 |
|
|
179 |
if (nbytes) |
if (nbytes) |
180 |
*nbytes = 0; |
*nbytes = 0; |
181 |
*buf = 0; |
*buf = 0; |
182 |
while (recv (fd, &ch, 1, 0) > 0) { |
while (recv (fd, &ch, 1, 0) > 0) { |
183 |
*buf++ = ch; |
*buf++ = ch; |
184 |
count++; |
nread++; |
185 |
if (ch == '\n' || count == (buflen - 1)) { |
if (ch == '\r') |
186 |
|
continue; /* remove this char. */ |
187 |
|
if (ch == '\n' || nread == (buflen - 1)) { |
188 |
*buf = 0; |
*buf = 0; |
189 |
if (nbytes) |
if (nbytes) |
190 |
*nbytes = count; |
*nbytes = nread; |
191 |
return 0; |
return 0; |
192 |
} |
} |
193 |
} |
} |
206 |
|
|
207 |
FD_ZERO (&rfd); |
FD_ZERO (&rfd); |
208 |
FD_SET (fd, &rfd); |
FD_SET (fd, &rfd); |
209 |
if (select (fd + 1, &rfd, NULL, NULL, &tv) == SOCKET_ERROR) |
if (select (fd + 1, &rfd, NULL, NULL, &tv) == SOCKET_ERROR) { |
210 |
|
log_debug ("sock_select: select() failed ec=%d.\r\n", net_errno); |
211 |
return SOCKET_ERROR; |
return SOCKET_ERROR; |
212 |
|
} |
213 |
if (FD_ISSET (fd, &rfd)) |
if (FD_ISSET (fd, &rfd)) |
214 |
return 1; |
return 1; |
215 |
return 0; |
return 0; |
216 |
} |
} |
217 |
|
|
218 |
|
|
219 |
/* Read from a socket with a fixed timeout of 10 seconds. |
/* Read from a socket @fd to buffer @buf with a fixed timeout |
220 |
|
of 10 seconds. The amount of bytes which were read are |
221 |
|
returned in @nbytes. |
222 |
Return value: 0 on success. */ |
Return value: 0 on success. */ |
223 |
static int |
static int |
224 |
sock_read (int fd, char *buf, int buflen, int *nbytes) |
sock_read (int fd, char *buf, int buflen, int *nbytes) |
225 |
{ |
{ |
226 |
DWORD nread; |
DWORD nread; |
227 |
int nleft = buflen; |
int nleft = buflen; |
228 |
int rc, n = 0; |
int rc, n = 0; |
229 |
|
|
230 |
|
if (nbytes) |
231 |
|
*nbytes = 0; |
232 |
while (nleft > 0) { |
while (nleft > 0) { |
233 |
if (n >= 10) |
if (n >= default_socket_timeout) |
234 |
return WPTERR_WINSOCK_TIMEOUT; |
return WPTERR_WINSOCK_TIMEOUT; |
235 |
rc = sock_select (fd, 1); |
rc = sock_select (fd, 1); |
236 |
if (rc == SOCKET_ERROR) |
if (rc == SOCKET_ERROR) |
237 |
return SOCKET_ERROR; |
return rc; |
238 |
else if( !rc ) |
else if (!rc) |
239 |
n++; |
n++; |
240 |
else { |
else { |
241 |
nread = recv (fd, buf, nleft, 0); |
nread = recv (fd, buf, nleft, 0); |
242 |
if (nread == SOCKET_ERROR) |
if (nread == SOCKET_ERROR) { |
243 |
|
log_debug ("sock_read: recv() failed ec=%d.\r\n", net_errno); |
244 |
return SOCKET_ERROR; |
return SOCKET_ERROR; |
245 |
|
} |
246 |
else if (!nread) |
else if (!nread) |
247 |
break; |
break; |
248 |
nleft -= nread; |
nleft -= nread; |
256 |
} |
} |
257 |
|
|
258 |
|
|
259 |
/* Write @buf to a socket @fd. */ |
/* Helper to create a string from the gpgme data and |
260 |
|
then release the gpgme data object. */ |
261 |
|
char* |
262 |
|
data_release_and_get_mem (gpgme_data_t in, int *r_bufferlen) |
263 |
|
{ |
264 |
|
size_t n; |
265 |
|
char *buffer, *p; |
266 |
|
|
267 |
|
gpg_data_putc (in, '\0'); |
268 |
|
p = gpgme_data_release_and_get_mem (in, &n); |
269 |
|
buffer = m_strdup (p); |
270 |
|
if (r_bufferlen) |
271 |
|
*r_bufferlen = (int)n; |
272 |
|
gpgme_free (p); |
273 |
|
return buffer; |
274 |
|
} |
275 |
|
|
276 |
|
|
277 |
|
/* Read much data as possible from the socket @fd and |
278 |
|
return the data in @buffer. Caller must free data. |
279 |
|
Return value: 0 on success. */ |
280 |
|
int |
281 |
|
sock_read_ext (int fd, char **buffer, int *r_bufferlen) |
282 |
|
{ |
283 |
|
gpgme_data_t dh; |
284 |
|
char buf[1024]; |
285 |
|
size_t n=0; |
286 |
|
int nread, rc; |
287 |
|
|
288 |
|
if (gpgme_data_new (&dh)) |
289 |
|
BUG (0); |
290 |
|
while (n < 10) { |
291 |
|
rc = sock_select (fd, 1); |
292 |
|
if (rc == SOCKET_ERROR) { |
293 |
|
gpgme_data_release (dh); |
294 |
|
return rc; |
295 |
|
} |
296 |
|
else if (!rc) |
297 |
|
n++; |
298 |
|
else { |
299 |
|
nread = recv (fd, buf, sizeof (buf), 0); |
300 |
|
if (nread == SOCKET_ERROR) |
301 |
|
return SOCKET_ERROR; |
302 |
|
else if (!nread) |
303 |
|
break; |
304 |
|
gpgme_data_write (dh, buf, nread); |
305 |
|
} |
306 |
|
} |
307 |
|
|
308 |
|
*buffer = data_release_and_get_mem (dh, r_bufferlen); |
309 |
|
return 0; |
310 |
|
} |
311 |
|
|
312 |
|
|
313 |
|
/* Write the buffer @buf with the length @buflen to a socket @fd. */ |
314 |
static int |
static int |
315 |
sock_write (int fd, const char *buf, int buflen) |
sock_write (int fd, const char *buf, int buflen) |
316 |
{ |
{ |
319 |
|
|
320 |
while (nleft > 0) { |
while (nleft > 0) { |
321 |
nwritten = send (fd, buf, nleft, 0); |
nwritten = send (fd, buf, nleft, 0); |
322 |
if (nwritten == SOCKET_ERROR) |
if (nwritten == SOCKET_ERROR) { |
323 |
|
log_debug ("sock_write: send() failed ec=%d\r\n", net_errno); |
324 |
return SOCKET_ERROR; |
return SOCKET_ERROR; |
325 |
|
} |
326 |
nleft -= nwritten; |
nleft -= nwritten; |
327 |
buf += nwritten; |
buf += nwritten; |
328 |
} |
} |
329 |
|
|
330 |
return 0; |
return 0; |
331 |
} |
} |
332 |
|
|
333 |
|
|
|
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 = (u16)strtoul (p, NULL, 10); |
|
|
free_if_alloc (p); |
|
|
} |
|
|
else |
|
|
default_keyserver_port = HKP_PORT; |
|
|
} |
|
|
|
|
|
|
|
334 |
/* Initialize the Winsock2 interface.*/ |
/* Initialize the Winsock2 interface.*/ |
335 |
int |
int |
336 |
wsock_init (void) |
wsock_init (void) |
337 |
{ |
{ |
338 |
WSADATA wsa; |
WSADATA wsa; |
339 |
|
|
340 |
if (WSAStartup (0x202, &wsa)) |
if (WSAStartup (0x202, &wsa)) { |
341 |
|
log_debug ("wsock_init: WSAStartup failed ec=%d\r\n", net_errno); |
342 |
return WPTERR_WINSOCK_INIT; |
return WPTERR_WINSOCK_INIT; |
343 |
set_default_kserver (); |
} |
344 |
return 0; |
return 0; |
345 |
} |
} |
346 |
|
|
349 |
void |
void |
350 |
wsock_end (void) |
wsock_end (void) |
351 |
{ |
{ |
352 |
|
char *p; |
353 |
int i; |
int i; |
354 |
|
|
355 |
|
p = make_special_filename (CSIDL_APPDATA, "winpt\\keyserver.conf", NULL); |
356 |
|
kserver_save_conf (p); |
357 |
|
free_if_alloc (p); |
358 |
free_if_alloc (default_keyserver); |
free_if_alloc (default_keyserver); |
|
default_keyserver = NULL; |
|
359 |
for (i=0; i < MAX_KEYSERVERS; i++) { |
for (i=0; i < MAX_KEYSERVERS; i++) { |
360 |
if (server[i].used) |
if (server[i].used && server[i].name != NULL) |
361 |
free_if_alloc (server[i].name); |
free_if_alloc (server[i].name); |
362 |
} |
} |
363 |
|
kserver_proxy_release (&proxy); |
364 |
WSACleanup (); |
WSACleanup (); |
365 |
} |
} |
366 |
|
|
368 |
/* Return a string representation of a winsock error. */ |
/* Return a string representation of a winsock error. */ |
369 |
const char* |
const char* |
370 |
wsock_strerror (void) |
wsock_strerror (void) |
371 |
{ |
{ |
|
static char buf[384]; |
|
372 |
int ec = WSAGetLastError (); |
int ec = WSAGetLastError (); |
373 |
|
|
374 |
|
if (!ec) |
375 |
|
return ""; |
376 |
switch (ec) { |
switch (ec) { |
377 |
case WSAENETDOWN: |
case WSAENETDOWN: |
378 |
return _("The network subsystem has failed"); |
return _("Network unreachable"); |
379 |
case WSAHOST_NOT_FOUND: |
|
380 |
return _("Authoritative Answer Host not found"); |
case WSAEHOSTUNREACH: |
381 |
case WSAETIMEDOUT: |
return _("Host unreachable"); |
382 |
return _("The connection has been dropped because of a network failure"); |
|
383 |
default: |
case WSAHOST_NOT_FOUND: |
384 |
_snprintf (buf, sizeof (buf) -1, _("Unknown Winsock error ec=%d"),ec); |
return _("Could not resolve host name"); |
385 |
return buf; |
|
386 |
|
case WSAECONNREFUSED: |
387 |
|
return _("Connection refused"); |
388 |
|
|
389 |
|
case WSAETIMEDOUT: |
390 |
|
case WSAECONNABORTED: |
391 |
|
return _("Connection timeout"); |
392 |
|
|
393 |
|
case WSAENETRESET: |
394 |
|
case WSAECONNRESET: |
395 |
|
return _("Connection resetted by peer"); |
396 |
|
|
397 |
|
case WSAESHUTDOWN: |
398 |
|
return _("Socket has been shutdown"); |
399 |
|
|
400 |
|
default: |
401 |
|
break; |
402 |
} |
} |
403 |
return NULL; |
|
404 |
|
return ""; |
405 |
|
} |
406 |
|
|
407 |
|
|
408 |
|
/* Set default socket timeout for all reading operations. */ |
409 |
|
void |
410 |
|
kserver_set_socket_timeout (int nsec) |
411 |
|
{ |
412 |
|
if (nsec < 0) |
413 |
|
nsec = 0; |
414 |
|
default_socket_timeout = nsec; |
415 |
} |
} |
416 |
|
|
417 |
|
|
428 |
/* Read a keyserver from the list at index @idx. |
/* Read a keyserver from the list at index @idx. |
429 |
Return value: keyserver name at the given position. */ |
Return value: keyserver name at the given position. */ |
430 |
const char* |
const char* |
431 |
kserver_get_hostname (int idx, int type, u16 *port) |
kserver_get_hostname (int idx, int type, WORD *port) |
432 |
{ |
{ |
433 |
if (type == -1) { |
if (type == -1) { |
434 |
*port = default_keyserver_port; |
*port = default_keyserver_port; |
453 |
closesocket (fd); |
closesocket (fd); |
454 |
return 0; |
return 0; |
455 |
} |
} |
456 |
|
log_debug ("kserver_check_inet_connection: no inet connection.\r\n"); |
457 |
return -1; |
return -1; |
458 |
} |
} |
459 |
|
|
460 |
|
|
461 |
/* |
/* If the request contains the keyid, it have to be |
462 |
* If the request contains the keyid, it have to be |
always postfix with '0x'+keyid. This code checks |
463 |
* always postfix with '0x'+keyid. This code checks |
if the keyid is a decimal value and if so if it contains |
464 |
* if the keyid is a decimal value and if so if it contains |
the '0x'. If not it is inserted. */ |
|
* the '0x'. If not it is inserted. |
|
|
*/ |
|
465 |
const char* |
const char* |
466 |
kserver_check_keyid (const char *keyid) |
kserver_check_keyid (const char *keyid) |
467 |
{ |
{ |
468 |
static char id[20]; |
static char id[21]; |
469 |
|
|
470 |
if (strstr (keyid, "@")) |
if (strstr (keyid, "@")) |
471 |
return keyid; /* email address */ |
return keyid; /* email address */ |
475 |
return id; |
return id; |
476 |
} |
} |
477 |
return keyid; |
return keyid; |
478 |
} /* kserver_check_keyid */ |
} |
479 |
|
|
480 |
|
|
481 |
|
/* Update the keyserver proxy user. */ |
482 |
static void |
static void |
483 |
kserver_update_proxyuser (const char *proxy_user, const char *proxy_pass) |
update_proxy_user (const char *proxy_user, const char *proxy_pass) |
484 |
{ |
{ |
485 |
char t[257]; /* user:pass = 127+1+127+1 = 257 */ |
char t[257]; /* user:pass = 127+1+127+1 = 257 */ |
486 |
int n = 0; |
int n = 0; |
487 |
|
|
488 |
n = 4*strlen (proxy_user) / 3 + 32 + strlen (proxy_pass) + 2; |
n = 4*strlen (proxy_user) / 3 + 32 + strlen (proxy_pass) + 2; |
489 |
free_if_alloc (proxy.base64_user); |
free_if_alloc (proxy.base64_user); |
490 |
proxy.base64_user = new char[n]; |
proxy.base64_user = safe_alloc (n+1); |
|
if (!proxy.base64_user) |
|
|
BUG (0); |
|
491 |
_snprintf (t, sizeof (t)-1, "%s:%s", proxy_user, proxy_pass); |
_snprintf (t, sizeof (t)-1, "%s:%s", proxy_user, proxy_pass); |
492 |
base64_encode (t, proxy.base64_user, 257); |
base64_encode (t, proxy.base64_user); |
493 |
free_if_alloc (proxy.user); |
free_if_alloc (proxy.user); |
494 |
free_if_alloc (proxy.pass); |
free_if_alloc (proxy.pass); |
495 |
proxy.user = m_strdup (proxy_user); |
proxy.user = m_strdup (proxy_user); |
496 |
proxy.pass = m_strdup (proxy_pass); |
proxy.pass = m_strdup (proxy_pass); |
497 |
} /* kserver_update_proxyuser */ |
} |
498 |
|
|
499 |
|
|
500 |
/* Check that the given buffer contains a valid keyserver URL. */ |
/* Check that the given buffer contains a valid keyserver URL. */ |
517 |
port_from_proto (int proto) |
port_from_proto (int proto) |
518 |
{ |
{ |
519 |
switch (proto) { |
switch (proto) { |
520 |
case KSPROTO_LDAP: return 0; |
case KSPROTO_LDAP: return 0; |
521 |
case KSPROTO_FINGER: return FINGER_PORT; |
case KSPROTO_FINGER: return FINGER_PORT; |
522 |
case KSPROTO_HTTP: return HKP_PORT; |
case KSPROTO_HTTP: return HKP_PORT; |
523 |
} |
} |
524 |
return 0; |
return 0; |
525 |
} |
} |
527 |
|
|
528 |
/* Get the port number from the given URL. */ |
/* Get the port number from the given URL. */ |
529 |
static int |
static int |
530 |
proto_from_URL (const char * buf) |
proto_from_URL (const char *buf) |
531 |
{ |
{ |
532 |
if (strstr( buf, "ldap")) |
if (strstr (buf, "ldap")) |
533 |
return KSPROTO_LDAP; |
return KSPROTO_LDAP; |
534 |
else if (strstr( buf, "finger")) |
else if (strstr( buf, "finger")) |
535 |
return KSPROTO_FINGER; |
return KSPROTO_FINGER; |
537 |
} |
} |
538 |
|
|
539 |
|
|
540 |
|
/* Set the default keyserver. |
541 |
|
If @hostname is NULL, use the predefined keyserver. */ |
542 |
void |
void |
543 |
keyserver_set_default (const char * hostname, u16 port) |
keyserver_set_default (const char *hostname, WORD port) |
544 |
{ |
{ |
545 |
if (hostname) { |
if (!port) |
546 |
|
port = HKP_PORT; |
547 |
|
if (hostname != NULL) { |
548 |
free_if_alloc (default_keyserver); |
free_if_alloc (default_keyserver); |
549 |
default_keyserver = m_strdup (hostname); |
default_keyserver = m_strdup (hostname); |
|
if (!default_keyserver) |
|
|
BUG (0); |
|
550 |
default_keyserver_port = port; |
default_keyserver_port = port; |
551 |
} |
} |
552 |
|
if (!default_keyserver) { |
553 |
|
default_keyserver = m_strdup (DEF_HKP_KEYSERVER); |
554 |
|
default_keyserver_port = HKP_PORT; |
555 |
|
} |
556 |
server[0].name = m_strdup (default_keyserver); |
server[0].name = m_strdup (default_keyserver); |
557 |
server[0].used = 1; |
server[0].used = 1; |
558 |
server[0].port = port; |
server[0].port = port; |
559 |
server[0].proto = proto_from_URL (default_keyserver); |
server[0].proto = proto_from_URL (default_keyserver); |
560 |
} /* keyserver_set_default */ |
} |
561 |
|
|
562 |
|
|
563 |
static const char * |
/* Skip all kind of whitespace chars in @str. */ |
564 |
skip_whitespace (const char * str) |
static const char* |
565 |
|
skip_whitespace (const char *str) |
566 |
{ |
{ |
567 |
while (str && *str) |
while (str && *str) { |
|
{ |
|
568 |
if (*str == ' ' || |
if (*str == ' ' || |
569 |
*str == '\t' || |
*str == '\t' || |
570 |
*str == '\n' || |
*str == '\n' || |
571 |
*str == '\r') |
*str == '\r') { |
|
{ |
|
572 |
str++; |
str++; |
573 |
continue; |
continue; |
574 |
} |
} |
578 |
} |
} |
579 |
|
|
580 |
|
|
581 |
|
/* Save the keyserver config file in @conf. */ |
582 |
int |
int |
583 |
kserver_load_conf (const char * conf) |
kserver_save_conf (const char *conf) |
584 |
{ |
{ |
|
struct stat statbuf; |
|
585 |
FILE *fp; |
FILE *fp; |
|
char buf[1024], * s, *p; |
|
|
char *user = NULL, *pass = NULL; |
|
586 |
int pos; |
int pos; |
587 |
|
|
588 |
|
fp = fopen (conf, "wb"); |
589 |
|
if (!fp) { |
590 |
|
msg_box (NULL, _("Could not save keyserver.conf file"), |
591 |
|
_("Keyserver"), MB_ERR); |
592 |
|
return -1; |
593 |
|
} |
594 |
|
|
595 |
|
fprintf (fp, "# do NOT manually modify this file, it will be " |
596 |
|
"generated automatically!!\r\n"); |
597 |
|
for (pos = 0; pos < MAX_KEYSERVERS; pos++) { |
598 |
|
if (!server[pos].used) |
599 |
|
continue; |
600 |
|
fprintf (fp, "%s:%d\r\n", server[pos].name, server[pos].port); |
601 |
|
} |
602 |
|
fclose (fp); |
603 |
|
return 0; |
604 |
|
} |
605 |
|
|
606 |
|
|
607 |
|
/* Load the keyserver config file @conf. */ |
608 |
|
int |
609 |
|
kserver_load_conf (const char *conf) |
610 |
|
{ |
611 |
|
FILE *fp; |
612 |
|
char buf[1024], *s, *p; |
613 |
|
char *user = NULL, *pass = NULL; |
614 |
int no_config=0, chk_pos=0; |
int no_config=0, chk_pos=0; |
615 |
|
int pos; |
616 |
|
|
617 |
for (pos = 0; pos < MAX_KEYSERVERS; pos++) { |
for (pos = 0; pos < MAX_KEYSERVERS; pos++) { |
618 |
server[pos].used = 0; |
server[pos].used = 0; |
628 |
} |
} |
629 |
no_config=1; |
no_config=1; |
630 |
} |
} |
631 |
get_reg_proxy_prefs (&proxy.host, &proxy.port, &user, &pass); |
get_reg_proxy_prefs (&proxy); |
632 |
if (user && pass) |
if (user && pass) |
633 |
kserver_update_proxyuser (user, pass); |
update_proxy_user (user, pass); |
634 |
else if (user && !pass || !user && pass) { |
else if (user && !pass || !user && pass) { |
635 |
msg_box( NULL, _("Invalid proxy configuration." |
msg_box (NULL, _("Invalid proxy configuration." |
636 |
"You need to set a user and a password" |
"You need to set a user and a password" |
637 |
"to use proxy authentication!"), _("Proxy Error"), MB_ERR ); |
"to use proxy authentication!"), |
638 |
|
_("Proxy Error"), MB_ERR); |
639 |
} |
} |
640 |
/* check if the host has a http:// prefix and skip it */ |
/* check if the host has a http:// prefix and skip it */ |
641 |
if( proxy.host && !strncmp( proxy.host, "http://", 7 ) ) { |
if (proxy.host && !strncmp (proxy.host, "http://", 7)) { |
642 |
char *pr = m_strdup (proxy.host+7); |
char *host = m_strdup (proxy.host+7); |
|
if (!pr) |
|
|
BUG (NULL); |
|
643 |
free_if_alloc (proxy.host); |
free_if_alloc (proxy.host); |
644 |
proxy.host = pr; |
proxy.host = host; |
645 |
} |
} |
646 |
free_if_alloc (user); |
free_if_alloc (user); |
647 |
free_if_alloc (pass); |
free_if_alloc (pass); |
648 |
|
|
649 |
pos = 0; |
pos = 0; |
650 |
while( fp && !feof( fp ) ) { |
while (fp && !feof (fp)) { |
651 |
s = fgets (buf, sizeof (buf)-1, fp); |
s = fgets (buf, sizeof (buf)-1, fp); |
652 |
if (!s) |
if (!s) |
653 |
break; |
break; |
664 |
_("Keyserver Error"), MB_ERR); |
_("Keyserver Error"), MB_ERR); |
665 |
continue; |
continue; |
666 |
} |
} |
667 |
chk_pos=6; |
chk_pos = 6; |
668 |
if (strstr (s, "finger")) |
if (strstr (s, "finger")) |
669 |
chk_pos = 10; |
chk_pos = 10; |
670 |
p = strchr (s+chk_pos, ':'); |
p = strchr (s+chk_pos, ':'); |
677 |
server[pos].port = atol (p+1); |
server[pos].port = atol (p+1); |
678 |
if (server[pos].port < 0 || server[pos].port > 65536) |
if (server[pos].port < 0 || server[pos].port > 65536) |
679 |
server[pos].port = HKP_PORT; |
server[pos].port = HKP_PORT; |
680 |
server[pos].name = new char [(p-s)+2]; |
server[pos].name = safe_alloc ((p-s)+2); |
|
if (!server[pos].name) |
|
|
BUG (0); |
|
681 |
memset (server[pos].name, 0, (p-s)+2); |
memset (server[pos].name, 0, (p-s)+2); |
682 |
memcpy (server[pos].name, s, (p-s)); |
memcpy (server[pos].name, s, (p-s)); |
683 |
} |
} |
684 |
pos++; |
pos++; |
685 |
if (pos > MAX_KEYSERVERS) { |
if (pos > MAX_KEYSERVERS) { |
686 |
msg_box (NULL, _("The keyserver limit is exceeded"), _("Keyserver Warning"), MB_INFO); |
msg_box (NULL, _("The keyserver limit is exceeded"), |
687 |
|
_("Keyserver Warning"), MB_INFO); |
688 |
break; |
break; |
689 |
} |
} |
690 |
} |
} |
691 |
if (fp) |
if (fp) |
692 |
fclose (fp); |
fclose (fp); |
693 |
if (!pos) { |
if (!pos) { |
694 |
/* 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 |
695 |
|
it has no valid entries. */ |
696 |
keyserver_set_default (NULL, HKP_PORT); |
keyserver_set_default (NULL, HKP_PORT); |
697 |
if (!no_config) |
if (!no_config) |
698 |
return WPTERR_CONFIG_FILE; |
return WPTERR_CONFIG_FILE; |
699 |
} |
} |
700 |
|
|
|
if (!stat (conf, &statbuf)) |
|
|
conf_timestamp = statbuf.st_mtime; |
|
701 |
return 0; |
return 0; |
702 |
} /* kserver_load_conf */ |
} |
|
|
|
|
|
|
|
const char * |
|
|
kserver_get_proxy (int * r_port) |
|
|
{ |
|
|
if (proxy.host) { |
|
|
if (r_port) |
|
|
*r_port = proxy.port; |
|
|
return proxy.host; |
|
|
} |
|
|
return NULL; |
|
|
} /* kserver_get_proxy */ |
|
|
|
|
|
|
|
|
const char * |
|
|
kserver_get_proxy_info (int id) |
|
|
{ |
|
|
switch (id) { |
|
|
case PROXY_USER: return proxy.user; |
|
|
case PROXY_PASS: return proxy.pass; |
|
|
} |
|
|
return NULL; |
|
|
} /* kserver_get_proxy_info */ |
|
703 |
|
|
704 |
|
|
705 |
/* Connect to the keyserver @hostname. */ |
/* Connect to the keyserver @hostname (port @port). |
706 |
|
Return value: 0 on success */ |
707 |
int |
int |
708 |
kserver_connect (const char *hostname, u16 port, int *conn_fd) |
kserver_connect (const char *hostname, WORD port, int *conn_fd) |
709 |
{ |
{ |
|
int rc, fd; |
|
|
u32 iaddr; |
|
|
char host[128] = {0}; |
|
710 |
struct hostent *hp; |
struct hostent *hp; |
711 |
struct sockaddr_in sock; |
struct sockaddr_in sock; |
712 |
|
char host[128] = {0}; |
713 |
|
DWORD iaddr; |
714 |
|
int rc, fd; |
715 |
|
|
716 |
log_debug ("kserver_connect: %s:%d\r\n", hostname, port); |
log_debug ("kserver_connect: %s:%d\r\n", hostname, port); |
717 |
|
|
721 |
*conn_fd = 0; |
*conn_fd = 0; |
722 |
hostname = skip_type_prefix (hostname); |
hostname = skip_type_prefix (hostname); |
723 |
|
|
724 |
|
if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) |
725 |
|
port = proxy.port; |
726 |
memset (&sock, 0, sizeof (sock)); |
memset (&sock, 0, sizeof (sock)); |
727 |
sock.sin_family = AF_INET; |
sock.sin_family = AF_INET; |
728 |
sock.sin_port = proxy.host? htons (proxy.port) : htons (port); |
sock.sin_port = htons (port); |
729 |
if (proxy.host) |
if (proxy.host) |
730 |
strncpy (host, proxy.host, 127); |
strncpy (host, proxy.host, 127); |
731 |
else |
else |
732 |
strncpy (host, hostname, 127); |
strncpy (host, hostname, 127); |
733 |
|
|
734 |
if ((iaddr = inet_addr (host)) != INADDR_NONE) |
if ((iaddr = inet_addr (host)) != INADDR_NONE) |
735 |
memcpy (&sock.sin_addr, &iaddr, sizeof (iaddr)); |
memcpy (&sock.sin_addr, &iaddr, sizeof (iaddr)); |
736 |
else if ((hp = gethostbyname (host))) { |
else if ((hp = gethostbyname (host))) { |
741 |
memcpy (&sock.sin_addr, hp->h_addr, hp->h_length); |
memcpy (&sock.sin_addr, hp->h_addr, hp->h_length); |
742 |
} |
} |
743 |
else { |
else { |
744 |
log_debug ("gethostbyname: failed.\r\n"); |
log_debug ("gethostbyname: failed ec=%d.\r\n", net_errno); |
745 |
return WPTERR_WINSOCK_RESOLVE; |
return WPTERR_WINSOCK_RESOLVE; |
746 |
} |
} |
747 |
fd = socket (AF_INET, SOCK_STREAM, 0); |
fd = socket (AF_INET, SOCK_STREAM, 0); |
749 |
return WPTERR_WINSOCK_SOCKET; |
return WPTERR_WINSOCK_SOCKET; |
750 |
rc = connect (fd, (struct sockaddr *) &sock, sizeof (sock)); |
rc = connect (fd, (struct sockaddr *) &sock, sizeof (sock)); |
751 |
if (rc == SOCKET_ERROR) { |
if (rc == SOCKET_ERROR) { |
752 |
log_debug ("connect: failed.\r\n"); |
log_debug ("connect: failed ec=%d.\r\n", net_errno); |
753 |
closesocket (fd); |
closesocket (fd); |
754 |
return WPTERR_WINSOCK_CONNECT; |
return WPTERR_WINSOCK_CONNECT; |
755 |
} |
} |
756 |
|
|
757 |
|
if (proxy.proto == PROXY_PROTO_SOCKS5) { |
758 |
|
rc = socks_handshake (&proxy, fd, hostname, port); |
759 |
|
if (rc) { |
760 |
|
closesocket (fd); |
761 |
|
return WPTERR_GENERAL; /* XXX: use better code. */ |
762 |
|
} |
763 |
|
} |
764 |
|
|
765 |
if (conn_fd) |
if (conn_fd) |
766 |
*conn_fd = fd; |
*conn_fd = fd; |
767 |
WSASetLastError (0); |
WSASetLastError (0); |
768 |
return 0; |
return 0; |
769 |
} /* kserver_connect */ |
} |
770 |
|
|
771 |
|
|
772 |
/* Perform URL-encoding on the given pubkey blob. */ |
static bool |
773 |
|
URL_must_encoded (const char *url) |
774 |
|
{ |
775 |
|
if (strchr (url, '.') || strchr (url, '@') || strchr (url, ' ')) |
776 |
|
return true; |
777 |
|
return false; |
778 |
|
} |
779 |
|
|
780 |
|
|
781 |
|
/* Perform URL encoding of the given data. */ |
782 |
static char* |
static char* |
783 |
kserver_urlencode (const char *pubkey, size_t octets, size_t *newlen) |
URL_encode (const char *url, DWORD ulen, DWORD *ret_nlen) |
784 |
{ |
{ |
785 |
char *p, numbuf[5]; |
gpgme_data_t hd; |
786 |
size_t j = 0; |
char numbuf[5], *p; |
787 |
size_t i, size; |
size_t i; |
788 |
|
int nlen; |
|
p = new char [2*octets]; |
|
|
if (!p) |
|
|
BUG (0); |
|
789 |
|
|
790 |
for (size=0, i=0; i < octets; i++) { |
if (gpgme_data_new (&hd)) |
791 |
if (isalnum (pubkey[i]) || pubkey[i] == '-') { |
BUG (0); |
792 |
p[size] = pubkey[i]; |
for (i=0; i < ulen; i++) { |
793 |
size++; |
if (isalnum (url[i]) || url[i] == '-') |
794 |
} |
gpg_data_putc (hd, url[i]); |
795 |
else if (pubkey[i] == ' ') { |
else if (url[i] == ' ') |
796 |
p[size] = '+'; |
gpg_data_putc (hd, '+'); |
|
size++; |
|
|
} |
|
797 |
else { |
else { |
798 |
sprintf(numbuf, "%%%02X", pubkey[i]); |
sprintf (numbuf, "%%%02X", url[i]); |
799 |
for (j = 0; j<strlen(numbuf); j++) { |
gpgme_data_write (hd, numbuf, strlen (numbuf)); |
|
p[size] = numbuf[j]; |
|
|
size++; |
|
|
} |
|
800 |
} |
} |
801 |
} |
} |
802 |
p[size] = '\0'; |
|
803 |
*newlen = size; |
p = data_release_and_get_mem (hd, &nlen); |
804 |
|
if (ret_nlen) |
805 |
|
*ret_nlen = nlen; |
806 |
return p; |
return p; |
807 |
} |
} |
808 |
|
|
809 |
|
|
810 |
/* Format a request for the given keyid (send). */ |
/* Format a request for the given keyid (send). */ |
811 |
static char* |
static char* |
812 |
kserver_send_request (const char *hostname, u16 port, const char *pubkey, int octets) |
kserver_send_request (const char *hostname, WORD port, |
813 |
|
const char *pubkey, DWORD octets) |
814 |
{ |
{ |
815 |
char *request = NULL, *enc_pubkey = NULL; |
const char *fmt; |
816 |
|
char *request; |
817 |
|
char *enc_pubkey; |
818 |
|
DWORD enc_octets; |
819 |
int reqlen; |
int reqlen; |
|
size_t enc_octets; |
|
820 |
|
|
821 |
log_debug ("kserver_send_request: %s:%d\n", hostname, port); |
log_debug ("kserver_send_request: %s:%d\r\n", hostname, port); |
822 |
|
|
823 |
if (!port) |
if (!port) |
824 |
port = HKP_PORT; |
port = HKP_PORT; |
825 |
reqlen = 512 + strlen (hostname) + 2*strlen (pubkey); |
enc_pubkey = URL_encode (pubkey, octets, &enc_octets); |
826 |
request = new char[reqlen]; |
reqlen = 2*strlen (hostname) + 2*32/*port*/ + enc_octets + 32 /*len*/ + 1; |
827 |
if (!request) |
|
828 |
BUG (0); |
if (proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
829 |
enc_pubkey = kserver_urlencode (pubkey, octets, &enc_octets); |
fmt = "POST http://%s:%d/pks/add HTTP/1.0\r\n" |
830 |
if (!enc_pubkey || !enc_octets) { |
"Referer: \r\n" |
831 |
free_if_alloc (request); |
"User-Agent: WinPT/W32\r\n" |
832 |
return NULL; |
"Host: %s:%d\r\n" |
833 |
} |
"Proxy-Authorization: Basic %s\r\n" |
834 |
|
"Content-type: application/x-www-form-urlencoded\r\n" |
835 |
if (proxy.user) { |
"Content-length: %d\r\n" |
836 |
_snprintf (request, reqlen-1, |
"\r\n" |
837 |
"POST http://%s:%d/pks/add HTTP/1.0\r\n" |
"keytext=%s" |
838 |
"Referer: \r\n" |
"\r\n"; |
839 |
"User-Agent: WinPT/W32\r\n" |
reqlen += strlen (fmt) + strlen (proxy.base64_user) + 1; |
840 |
"Host: %s:%d\r\n" |
request = safe_alloc (reqlen+1); |
841 |
"Proxy-Authorization: Basic %s\r\n" |
_snprintf (request, reqlen, fmt, |
842 |
"Content-type: application/x-www-form-urlencoded\r\n" |
skip_type_prefix (hostname), port, hostname, port, |
|
"Content-length: %d\r\n" |
|
|
"\r\n" |
|
|
"keytext=%s" |
|
|
"\n", |
|
|
skip_type_prefix (hostname), port, hostname, port, |
|
843 |
proxy.base64_user, enc_octets+9, enc_pubkey); |
proxy.base64_user, enc_octets+9, enc_pubkey); |
844 |
} |
} |
845 |
else { |
else { |
846 |
_snprintf (request, reqlen-1, |
fmt = "POST /pks/add HTTP/1.0\r\n" |
847 |
"POST /pks/add HTTP/1.0\r\n" |
"Referer: \r\n" |
848 |
"Referer: \r\n" |
"User-Agent: WinPT/W32\r\n" |
849 |
"User-Agent: WinPT/W32\r\n" |
"Host: %s:%d\r\n" |
850 |
"Host: %s:%d\r\n" |
"Content-type: application/x-www-form-urlencoded\r\n" |
851 |
"Content-type: application/x-www-form-urlencoded\r\n" |
"Content-length: %d\r\n" |
852 |
"Content-length: %d\r\n" |
"\r\n" |
853 |
"\r\n" |
"keytext=%s" |
854 |
"keytext=%s" |
"\r\n"; |
855 |
"\n", |
reqlen += strlen (fmt)+1; |
856 |
skip_type_prefix (hostname), port, enc_octets+9, enc_pubkey); |
request = safe_alloc (reqlen+1); |
857 |
|
_snprintf (request, reqlen, fmt, |
858 |
|
skip_type_prefix (hostname), port, |
859 |
|
enc_octets+9, enc_pubkey); |
860 |
} |
} |
|
free_if_alloc (enc_pubkey); |
|
861 |
|
|
862 |
log_debug ("%s\n", request); |
free_if_alloc (enc_pubkey); |
863 |
|
log_debug ("request:\r\n%s\r\n", request); |
864 |
return request; |
return request; |
865 |
} /* kserver_send_request */ |
} |
866 |
|
|
867 |
|
|
868 |
/* Interface receiving a public key. */ |
/* Interface for receiving a public key. */ |
869 |
int |
int |
870 |
kserver_recvkey (const char *hostname, u16 port, const char *keyid, char *key, int maxkeylen) |
kserver_recvkey (const char *hostname, WORD port, const char *keyid, |
871 |
{ |
char **r_key, int *r_keylen) |
872 |
int rc, n; |
{ |
873 |
int conn_fd; |
const char *fmt; |
874 |
char *request = NULL; |
char *request = NULL; |
875 |
|
int conn_fd; |
876 |
|
int rc, reqlen; |
877 |
|
|
878 |
if (!port) |
if (!port) |
879 |
port = HKP_PORT; |
port = HKP_PORT; |
880 |
hkp_err = 0; /* reset */ |
hkp_err = 0; /* reset */ |
881 |
rc = kserver_connect (hostname, port, &conn_fd); |
rc = kserver_connect (hostname, port, &conn_fd); |
882 |
if (rc) |
if (rc) |
883 |
goto leave; |
goto leave; |
884 |
|
|
885 |
request = new char[300+1]; |
reqlen = strlen (hostname)+32+strlen (keyid)+1; |
886 |
if (!request) |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
887 |
BUG (0); |
fmt = "GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n" |
888 |
|
"Proxy-Authorization: Basic %s\r\n\r\n"; |
889 |
if (proxy.host && proxy.user) { |
reqlen =+ strlen (fmt) + strlen (proxy.base64_user)+1; |
890 |
_snprintf (request, 300, |
request = safe_alloc (reqlen+1); |
891 |
"GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n" |
_snprintf (request, reqlen, fmt, skip_type_prefix (hostname), port, |
892 |
"Proxy-Authorization: Basic %s\r\n\r\n", |
keyid, proxy.base64_user); |
893 |
skip_type_prefix (hostname), port, keyid, proxy.base64_user); |
} |
894 |
} |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
895 |
else if (proxy.host) { |
fmt = "GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n"; |
896 |
_snprintf (request, 300, |
reqlen += strlen (fmt)+1; |
897 |
"GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n", |
request = safe_alloc (reqlen+1); |
898 |
skip_type_prefix (hostname), port, keyid); |
_snprintf (request, reqlen, fmt, skip_type_prefix (hostname), |
899 |
} |
port, keyid); |
900 |
|
} |
901 |
else { |
else { |
902 |
_snprintf (request, 300, |
fmt = "GET /pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n"; |
903 |
"GET /pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n", keyid); |
reqlen += strlen (fmt)+1; |
904 |
|
request = safe_alloc (reqlen+1); |
905 |
|
_snprintf (request, reqlen, fmt, keyid); |
906 |
} |
} |
907 |
|
|
908 |
log_debug ("%s\r\n", request); |
log_debug ("request:\r\n%s\r\n", request); |
909 |
|
|
910 |
rc = sock_write (conn_fd, request, strlen (request)); |
rc = sock_write (conn_fd, request, strlen (request)); |
911 |
if (rc == SOCKET_ERROR) { |
if (rc == SOCKET_ERROR) { |
913 |
goto leave; |
goto leave; |
914 |
} |
} |
915 |
|
|
916 |
rc = sock_read( conn_fd, key, maxkeylen, &n ); |
rc = sock_read_ext (conn_fd, r_key, r_keylen); |
917 |
if( rc == SOCKET_ERROR ) { |
if (rc == SOCKET_ERROR) { |
918 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
919 |
goto leave; |
goto leave; |
920 |
} |
} |
921 |
|
|
922 |
log_debug("%s\r\n", key); |
log_debug ("response:\r\n%s\r\n", *r_key); |
923 |
rc = check_hkp_response (key, 1); |
rc = check_hkp_response (*r_key, 1); |
924 |
if (rc) |
if (rc) |
925 |
goto leave; |
goto leave; |
926 |
|
|
930 |
closesocket (conn_fd); |
closesocket (conn_fd); |
931 |
free_if_alloc (request); |
free_if_alloc (request); |
932 |
return rc; |
return rc; |
933 |
} /* kserver_recvkey */ |
} |
934 |
|
|
935 |
|
|
936 |
/* Interface to send a public key. */ |
/* Interface to send a public key. */ |
937 |
int |
int |
938 |
kserver_sendkey (const char *hostname, u16 port, const char *pubkey, int len ) |
kserver_sendkey (const char *hostname, WORD port, const char *pubkey, int len ) |
939 |
{ |
{ |
|
int n, rc; |
|
|
int conn_fd; |
|
940 |
char *request = NULL; |
char *request = NULL; |
941 |
char log[2048]; |
char log[2048] = {0}; |
942 |
|
int conn_fd, n; |
943 |
|
int rc; |
944 |
|
|
945 |
hkp_err = 0; /* reset */ |
hkp_err = 0; /* reset */ |
946 |
rc = kserver_connect (hostname, port, &conn_fd); |
rc = kserver_connect (hostname, port, &conn_fd); |
953 |
goto leave; |
goto leave; |
954 |
} |
} |
955 |
|
|
956 |
rc = sock_write( conn_fd, request, lstrlen(request) ); |
rc = sock_write (conn_fd, request, strlen (request)); |
957 |
if( rc == SOCKET_ERROR ) { |
if (rc == SOCKET_ERROR) { |
958 |
rc = WPTERR_WINSOCK_SENDKEY; |
rc = WPTERR_WINSOCK_SENDKEY; |
959 |
goto leave; |
goto leave; |
960 |
} |
} |
961 |
|
|
962 |
rc = sock_read( conn_fd, log, sizeof (log)-1, &n ); |
rc = sock_read (conn_fd, log, sizeof (log)-1, &n); |
963 |
if( rc == SOCKET_ERROR ) { |
if (rc == SOCKET_ERROR) { |
964 |
rc = WPTERR_WINSOCK_SENDKEY; |
rc = WPTERR_WINSOCK_SENDKEY; |
965 |
goto leave; |
goto leave; |
966 |
} |
} |
967 |
|
|
968 |
log_debug ("kserver_sendkey:\r\n%s\r\n", log); |
log_debug ("kserver_sendkey: read %d bytes\r\n%s\r\n", n, log); |
|
|
|
969 |
rc = check_hkp_response (log, 0); |
rc = check_hkp_response (log, 0); |
970 |
if( rc ) |
if (rc) |
971 |
goto leave; |
goto leave; |
972 |
|
|
973 |
WSASetLastError (0); |
WSASetLastError (0); |
976 |
closesocket (conn_fd); |
closesocket (conn_fd); |
977 |
free_if_alloc (request); |
free_if_alloc (request); |
978 |
return rc; |
return rc; |
979 |
} /* kserver_sendkey */ |
} |
980 |
|
|
981 |
|
|
982 |
|
/* Check keyserver response. */ |
983 |
|
static int |
984 |
|
kserver_search_chkresp (int fd) |
985 |
|
{ |
986 |
|
char buf[128]; |
987 |
|
int n=0; |
988 |
|
|
989 |
|
/* parse response 'HTTP/1.0 500 OK' */ |
990 |
|
if (sock_getline (fd, buf, sizeof (buf)-1, &n)) |
991 |
|
return WPTERR_KEYSERVER_NOTFOUND; |
992 |
|
|
993 |
|
log_debug ("kserver_search_chkpresp: %s\r\n", buf); |
994 |
|
if (strncmp (buf, "HTTP/1.", 7)) |
995 |
|
return WPTERR_KEYSERVER_NOTFOUND; |
996 |
|
if (strncmp (buf+(8+1), "200", 3)) |
997 |
|
return WPTERR_KEYSERVER_NOTFOUND; |
998 |
|
return 0; |
999 |
|
} |
1000 |
|
|
1001 |
|
|
1002 |
|
/* End the keyserver search procedure. */ |
1003 |
|
void |
1004 |
|
kserver_search_end (int conn_fd) |
1005 |
|
{ |
1006 |
|
log_debug ("kserver_search_end: fd=%d\r\n", conn_fd); |
1007 |
|
closesocket (conn_fd); |
1008 |
|
} |
1009 |
|
|
1010 |
|
|
1011 |
|
/* Extract the amount of keys from the info record. */ |
1012 |
|
static int |
1013 |
|
count_keys_in_response (char *buf) |
1014 |
|
{ |
1015 |
|
char *p; |
1016 |
|
int recno = 0; |
1017 |
|
int n = 0; |
1018 |
|
|
1019 |
|
/* info:1:4 */ |
1020 |
|
if (strncmp (buf, "info", 4)) |
1021 |
|
return 0; |
1022 |
|
p = strtok (buf, ":"); |
1023 |
|
while (p != NULL) { |
1024 |
|
recno++; |
1025 |
|
if (recno == 3) |
1026 |
|
n = atoi (p); |
1027 |
|
p = strtok (NULL, ":"); |
1028 |
|
} |
1029 |
|
return n; |
1030 |
|
} |
1031 |
|
|
1032 |
|
|
1033 |
|
/* Start the keyserver search. |
1034 |
|
Connect to host @hostname and port @port. |
1035 |
|
The pattern are given in @pattern. |
1036 |
|
The socket is returned in @conn_fd and @nkeys contains |
1037 |
|
the amount of keys which were found. */ |
1038 |
int |
int |
1039 |
kserver_search_init (const char * hostname, u16 port, const char * keyid, int * conn_fd) |
kserver_search_begin (const char *hostname, WORD port, |
1040 |
|
const char *pattern, int *conn_fd, int *nkeys) |
1041 |
{ |
{ |
1042 |
|
const char *fmt; |
1043 |
|
char *request = NULL; |
1044 |
|
char *enc_patt = NULL; |
1045 |
|
char status[128]; |
1046 |
int rc, sock_fd; |
int rc, sock_fd; |
1047 |
int n=0; |
int reqlen; |
|
char * request = NULL; |
|
1048 |
|
|
1049 |
|
*conn_fd = 0; |
1050 |
|
|
1051 |
rc = kserver_connect (hostname, port, &sock_fd); |
rc = kserver_connect (hostname, port, &sock_fd); |
1052 |
if (rc) { |
if (rc) |
|
*conn_fd = 0; |
|
1053 |
goto leave; |
goto leave; |
1054 |
} |
|
1055 |
|
enc_patt = URL_encode (pattern, strlen (pattern), NULL); |
1056 |
n=300; |
reqlen = strlen (enc_patt) + strlen (hostname) + 32 + 1; |
1057 |
request = new char[n+1]; |
|
1058 |
if (!request) |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
1059 |
BUG (0); |
fmt = "GET http://%s:%d/pks/lookup?options=mr&op=index&search=%s HTTP/1.0\r\n" |
1060 |
|
"Proxy-Authorization: Basic %s\r\n\r\n"; |
1061 |
if (proxy.host && proxy.user) { |
reqlen += strlen (proxy.base64_user) + strlen (fmt) + 1; |
1062 |
_snprintf (request, n, |
request = safe_alloc (reqlen+1); |
1063 |
"GET http://%s:%d/pks/lookup?op=index&search=%s HTTP/1.0\r\n" |
_snprintf (request, reqlen, fmt, skip_type_prefix (hostname), port, |
1064 |
"Proxy-Authorization: Basic %s\r\n\r\n", |
enc_patt, proxy.base64_user); |
|
skip_type_prefix (hostname), port, keyid, proxy.base64_user); |
|
1065 |
} |
} |
1066 |
else if (proxy.host) { |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
1067 |
_snprintf (request, n, |
fmt = "GET http://%s:%d/pks/lookup?options=mr&op=index&search=%s HTTP/1.0\r\n\r\n"; |
1068 |
"GET http://%s:%d/pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n", |
reqlen += strlen (fmt)+1; |
1069 |
skip_type_prefix (hostname), port, keyid); |
request = safe_alloc (reqlen+1); |
1070 |
|
_snprintf (request, reqlen, skip_type_prefix (hostname), port, |
1071 |
|
enc_patt); |
1072 |
} |
} |
1073 |
else { |
else { |
1074 |
_snprintf (request, n, |
fmt = "GET /pks/lookup?options=mr&op=index&search=%s HTTP/1.0\r\n\r\n"; |
1075 |
"GET /pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n", keyid); |
reqlen += strlen (fmt)+1; |
1076 |
|
request = safe_alloc (reqlen+1); |
1077 |
|
_snprintf (request, reqlen, fmt, enc_patt); |
1078 |
} |
} |
1079 |
|
|
1080 |
log_debug ("kserver_search_init:\r\n%s\r\n", request); |
log_debug ("kserver_search_begin:\r\n%s\r\n", request); |
|
|
|
1081 |
if (sock_write (sock_fd, request, strlen (request)) == SOCKET_ERROR) { |
if (sock_write (sock_fd, request, strlen (request)) == SOCKET_ERROR) { |
1082 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1083 |
goto leave; |
goto leave; |
1084 |
} |
} |
1085 |
|
|
1086 |
|
rc = kserver_search_chkresp (sock_fd); |
1087 |
|
if (rc) { |
1088 |
|
closesocket (sock_fd); |
1089 |
|
sock_fd = 0; |
1090 |
|
goto leave; |
1091 |
|
} |
1092 |
|
|
1093 |
|
/* Skip all lines until we reach the "info:" record. */ |
1094 |
|
for (;;) { |
1095 |
|
if (sock_getline (sock_fd, status, sizeof (status)-1, &reqlen)) { |
1096 |
|
log_debug ("kserver_search_begin: retrieving status line failed.\r\n"); |
1097 |
|
closesocket (sock_fd); |
1098 |
|
sock_fd = 0; |
1099 |
|
rc = WPTERR_GENERAL; |
1100 |
|
break; |
1101 |
|
} |
1102 |
|
if (!strncmp (status, "info:", 5)) |
1103 |
|
break; |
1104 |
|
} |
1105 |
|
|
1106 |
|
if (!rc) |
1107 |
|
*nkeys = count_keys_in_response (status); |
1108 |
*conn_fd = sock_fd; |
*conn_fd = sock_fd; |
1109 |
|
|
1110 |
leave: |
leave: |
1111 |
free_if_alloc (request); |
free_if_alloc (request); |
1112 |
|
free_if_alloc (enc_patt); |
1113 |
return rc; |
return rc; |
1114 |
} /* kserver_search_init */ |
} |
1115 |
|
|
1116 |
|
|
1117 |
int |
/* Parse a single pub record returned by the keyserver. */ |
1118 |
kserver_search_chkresp (int fd) |
static void |
1119 |
|
parse_pub_record (keyserver_key_s *key, char *buf) |
1120 |
{ |
{ |
1121 |
char buf[128]; |
enum pub_rec_t {ID=1, KEYID, ALGO, SIZE, CREATE, EXPIRE}; |
1122 |
int n=0; |
char *p; |
1123 |
|
int recno = 0; |
1124 |
/* parse response 'HTTP/1.0 500 OK' */ |
|
1125 |
if (sock_getline (fd, buf, 127, &n)) |
/* pub:BF3DF9B4:17:1024:925411133:: */ |
1126 |
return WPTERR_KEYSERVER_NOTFOUND; |
p = strtok (buf, ":"); |
1127 |
|
while (p != NULL) { |
1128 |
|
recno++; |
1129 |
|
switch (recno) { |
1130 |
|
case ID: |
1131 |
|
break; |
1132 |
|
|
1133 |
log_debug ("kserver_search_chkpresp: %s\r\n", buf); |
case KEYID: |
1134 |
if (strncmp (buf, "HTTP/1.", 7)) |
free_if_alloc (key->keyid); |
1135 |
return WPTERR_KEYSERVER_NOTFOUND; |
key->keyid = m_strdup (p); |
1136 |
if (strncmp (buf+(8+1), "200", 3)) |
break; |
|
return WPTERR_KEYSERVER_NOTFOUND; |
|
|
return 0; |
|
|
} /* kserver_search_chkresp */ |
|
1137 |
|
|
1138 |
|
case ALGO: |
1139 |
|
key->algo = atoi (p); |
1140 |
|
break; |
1141 |
|
|
1142 |
|
case SIZE: |
1143 |
|
key->bits = atoi (p); |
1144 |
|
break; |
1145 |
|
|
1146 |
int |
case CREATE: |
1147 |
kserver_search (int fd, keyserver_key * key) |
key->creation = strtoul (p, NULL, 10); |
1148 |
{ |
break; |
|
char buf[1024], *p; |
|
|
int uidlen, nbytes, pos = 0; |
|
1149 |
|
|
1150 |
log_debug ("keyserver_search:\n"); |
case EXPIRE: |
1151 |
|
key->expires = strtoul (p, NULL, 10); |
1152 |
|
break; |
1153 |
|
} |
1154 |
|
p = strtok (NULL, ":"); |
1155 |
|
} |
1156 |
|
|
1157 |
if (sock_getline (fd, buf, sizeof (buf) - 1, &nbytes)) |
} |
|
return WPTERR_GENERAL; |
|
1158 |
|
|
1159 |
log_debug ("%s\n", buf); |
|
1160 |
|
/* Parse a single user id returned by the keyserver. */ |
1161 |
if (!strncmp (buf, "pub", 3)) { |
static void |
1162 |
int revoked = strstr (buf, "KEY REVOKED") != NULL? 1 : 0; |
parse_uid_record (keyserver_key_s *key, char *buf) |
1163 |
key->bits = atol (buf+3); |
{ |
1164 |
p = strchr (buf, '>'); |
enum uid_rec_t {ID=1, UID, CREATE, EXPIRE}; |
1165 |
if (!p) |
keyserver_uid_s *u, *n; |
1166 |
goto fail; |
char *p; |
1167 |
pos = p - buf + 1; |
int recno = 0; |
1168 |
memcpy (key->keyid, buf + pos, 8); |
|
1169 |
key->keyid[8] = '\0'; |
/* uid:Timo Schulz <[email protected]>:1138440360:: */ |
1170 |
p = strstr (buf, "</a>"); |
u = new keyserver_uid_s; |
1171 |
if (!p) |
memset (u, 0, sizeof *u); |
1172 |
goto fail; |
|
1173 |
pos = p - buf + 5; |
p = strtok (buf, ":"); |
1174 |
memcpy (key->date, buf + pos, 10); |
while (p != NULL) { |
1175 |
key->date[10] = '\0'; |
recno++; |
1176 |
if (revoked) { |
|
1177 |
strcpy (key->uid, "KEY REVOKED: not checked"); |
switch (recno) { |
1178 |
return 0; |
case ID: |
1179 |
} |
break; |
1180 |
pos += 10; |
|
1181 |
p = buf + pos + 1; |
case UID: |
1182 |
while (p && *p != '>') |
unhexify_buffer (p, &u->uid); |
1183 |
p++; |
break; |
1184 |
p++; |
|
1185 |
uidlen = strlen (p) - 10; |
case CREATE: |
1186 |
memcpy (key->uid, p, uidlen); |
u->creation = strtoul (p, NULL, 10); |
1187 |
key->uid[uidlen] = '\0'; |
break; |
1188 |
strcat (key->uid, ">"); |
|
1189 |
p = strchr (key->uid, '&'); |
case EXPIRE: |
1190 |
if (p) { |
u->expires = strtoul (p, NULL, 10); |
1191 |
key->uid[(p-key->uid)] = '<'; |
break; |
|
memmove (key->uid+(p-key->uid)+1, key->uid+(p-key->uid)+4, strlen (key->uid)-3); |
|
1192 |
} |
} |
|
return 0; |
|
|
} |
|
1193 |
|
|
1194 |
fail: |
p = strtok (NULL, ":"); |
1195 |
key->bits = 0; |
} |
1196 |
memset (key, 0, sizeof *key); |
if (!key->uids) |
1197 |
return 0; |
key->uids = u; |
1198 |
} /* kserver_search */ |
else { |
1199 |
|
for (n = key->uids; n->next; n=n->next) |
1200 |
|
; |
1201 |
|
n->next = u; |
1202 |
|
} |
1203 |
|
} |
1204 |
|
|
1205 |
|
|
1206 |
|
/* Peek the next 3 bytes to check if the next line |
1207 |
|
would be a new "pub" record. In this case, return |
1208 |
|
-1 as an EOF indication, 0 otherwise. */ |
1209 |
static int |
static int |
1210 |
add_node( keyserver_cfgfile *cfg, const char *entry ) |
is_key_eof (int fd) |
1211 |
{ |
{ |
1212 |
keyserver_node *node; |
char buf[64]; |
1213 |
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 */ |
|
|
|
|
1214 |
|
|
1215 |
void |
n = recv (fd, buf, 3, MSG_PEEK); |
1216 |
kserver_change_proxy( keyserver_proxy_ctx *ctx ) |
if (n < 3 || strncmp (buf, "pub", 3)) |
1217 |
{ |
return 0; |
1218 |
proxy.port = ctx->port; |
return -1; |
1219 |
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 */ |
|
1220 |
|
|
1221 |
|
|
1222 |
|
/* Return the next key from the search response. */ |
1223 |
int |
int |
1224 |
kserver_read_config( const char *fname, keyserver_cfgfile **ret_cfg ) |
kserver_search_next (int fd, keyserver_key_s **r_key) |
1225 |
{ |
{ |
1226 |
FILE *fp; |
keyserver_uid_s *uid, *u = NULL; |
1227 |
keyserver_cfgfile *cfg; |
keyserver_key_s *key; |
1228 |
char buf[256]; |
char buf[512]; |
1229 |
int rc = 0; |
int n = 0; |
1230 |
|
long max = 0; |
1231 |
*ret_cfg = NULL; |
|
1232 |
fp = fopen( fname, "rb" ); |
log_debug ("keyserver_search_next:\r\n"); |
1233 |
if( fp == NULL ) |
*r_key = NULL; |
1234 |
return WPTERR_FILE_OPEN; |
|
1235 |
|
key = new keyserver_key_s; |
1236 |
cfg = new keyserver_cfgfile; |
memset (key, 0, sizeof *key); |
1237 |
if ( !cfg ) |
for (;;) { |
1238 |
BUG( NULL ); |
if (sock_getline (fd, buf, sizeof (buf)-1, &n)) |
1239 |
memset( cfg, 0, sizeof *cfg ); |
break; |
1240 |
|
/*log_debug ("record: '%s'\r\n", buf); */ |
1241 |
while ( !feof( fp ) ) { |
if (!strncmp (buf, "pub", 3)) |
1242 |
if ( fgets( buf, sizeof(buf)-1, fp ) == NULL ) |
parse_pub_record (key, buf); |
1243 |
break; |
else |
1244 |
if ( *buf == '\r' || *buf == '\n' || *buf == '#' ) |
parse_uid_record (key, buf); |
1245 |
continue; |
if (is_key_eof (fd)) |
1246 |
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++; |
|
|
} |
|
1247 |
} |
} |
|
|
|
|
fclose( fp ); |
|
|
*ret_cfg = cfg; |
|
|
|
|
|
return rc; |
|
|
} /* kserver_read_config */ |
|
1248 |
|
|
1249 |
|
/* the uid with the newest self sig is used as the |
1250 |
|
primary user id. */ |
1251 |
|
for (uid = key->uids; uid; uid = uid->next) { |
1252 |
|
if (uid->creation > max) { |
1253 |
|
max = uid->creation; |
1254 |
|
u = uid; |
1255 |
|
} |
1256 |
|
} |
1257 |
|
|
1258 |
int |
key->main_uid = u? u : key->uids; |
1259 |
kserver_write_config( const char * fname, keyserver_cfgfile * cfg ) |
*r_key = key; |
|
{ |
|
|
|
|
1260 |
return 0; |
return 0; |
1261 |
} |
} |
1262 |
|
|
1263 |
|
|
1264 |
|
/* Release the keyserver key @key and all its elements. */ |
1265 |
void |
void |
1266 |
kserver_cfgfile_release( keyserver_cfgfile *cfg ) |
kserver_release_key (keyserver_key_s *key) |
1267 |
{ |
{ |
1268 |
keyserver_node *k, *k2; |
keyserver_uid_s *u; |
1269 |
|
|
1270 |
proxy_release( &cfg->proxy ); |
while (key->uids) { |
1271 |
for( k = cfg->list; cfg->nlist--; k = k2 ) { |
u = key->uids->next; |
1272 |
k2 = k->next; |
free_if_alloc (key->uids->uid); |
1273 |
free_if_alloc( k ); |
free_if_alloc (key->uids); |
1274 |
|
key->uids = u; |
1275 |
} |
} |
1276 |
free_if_alloc( cfg ); |
free_if_alloc (key->keyid); |
1277 |
} /* kserver_cfgfile_release */ |
free_if_alloc (key); |
1278 |
|
} |
1279 |
|
|
1280 |
|
|
1281 |
|
/* Release mbmers in the proxy context @ctx. */ |
1282 |
void |
void |
1283 |
proxy_release( keyserver_proxy_ctx *ctx ) |
kserver_proxy_release (keyserver_proxy_t ctx) |
1284 |
{ |
{ |
1285 |
free_if_alloc( ctx->host ); |
free_if_alloc (ctx->host); |
1286 |
free_if_alloc( ctx->pass ); |
free_if_alloc (ctx->pass); |
1287 |
free_if_alloc( ctx->user ); |
free_if_alloc (ctx->user); |
1288 |
} /* proxy_release */ |
ctx->port = ctx->proto = 0; |
1289 |
|
} |
1290 |
|
|
1291 |
|
|
1292 |
int |
/* Spawn a process given by @cmdl. */ |
1293 |
ldap_recvkey (const char * host, const char * keyid, char * key, int maxkeylen) |
static int |
1294 |
|
spawn_application (char *cmdl) |
1295 |
{ |
{ |
|
const char *s; |
|
|
char *ksprg = NULL, *p = NULL; |
|
|
char temp[512], outf[512]; |
|
|
FILE * inp; |
|
|
int rc, start_key = 0; |
|
1296 |
STARTUPINFO si; |
STARTUPINFO si; |
1297 |
PROCESS_INFORMATION pi; |
PROCESS_INFORMATION pi; |
1298 |
|
int rc = 0; |
1299 |
|
|
1300 |
|
memset (&si, 0, sizeof (si)); |
1301 |
|
si.cb = sizeof (si); |
1302 |
|
si.dwFlags = STARTF_USESHOWWINDOW; |
1303 |
|
si.wShowWindow = SW_HIDE; |
1304 |
|
memset (&pi, 0, sizeof (pi)); |
1305 |
|
|
1306 |
p = get_gnupg_path (); |
if (!CreateProcess (NULL, cmdl, NULL, NULL, FALSE, 0, |
1307 |
ksprg = new char[strlen (p) + 1 + 128]; |
NULL, NULL, &si, &pi)) { |
1308 |
|
log_box ("Keyserver Plugin", MB_ERR, "Could not spawn helper process"); |
1309 |
|
rc = -1; |
1310 |
|
} |
1311 |
|
|
1312 |
|
CloseHandle (pi.hThread); |
1313 |
|
WaitForSingleObject (pi.hProcess, INFINITE); |
1314 |
|
CloseHandle (pi.hProcess); |
1315 |
|
return rc; |
1316 |
|
} |
1317 |
|
|
1318 |
|
|
1319 |
|
static FILE* |
1320 |
|
do_spawn_ldap_helper (const char *host, const char *keyid) |
1321 |
|
{ |
1322 |
|
FILE *fp = NULL; |
1323 |
|
char *p, *sep; |
1324 |
|
char *ksprg; |
1325 |
|
char outf[256], inf[256]; |
1326 |
|
size_t n; |
1327 |
|
|
1328 |
|
p = get_gnupg_prog (); |
1329 |
|
n = strlen (p) + 1 + 128; |
1330 |
|
ksprg = safe_alloc (n+1); |
1331 |
if (!ksprg) |
if (!ksprg) |
1332 |
BUG (0); |
BUG (0); |
1333 |
strcpy (ksprg, p); |
sep = strrchr (p, '\\'); |
1334 |
strcat (ksprg, "\\"); |
if (sep != NULL) |
1335 |
strcat (ksprg, "gpgkeys_ldap.exe"); |
p[(sep-p)] = 0; |
1336 |
|
|
1337 |
|
_snprintf (ksprg, n, "%s\\gpgkeys_ldap.exe", p); |
1338 |
free_if_alloc (p); |
free_if_alloc (p); |
1339 |
if (file_exist_check (ksprg)) { |
if (file_exist_check (ksprg)) { |
1340 |
log_box ( "LDAP Keyserver Plugin", MB_ERR, "Could not find LDAP keyserver module!"); |
log_box ("LDAP Keyserver Plugin", MB_ERR, |
1341 |
rc = -1; |
"%s: could not find LDAP keyserver module!", ksprg); |
1342 |
|
fp = NULL; |
1343 |
goto leave; |
goto leave; |
1344 |
} |
} |
1345 |
GetTempPath (sizeof (temp)-1, temp); |
get_temp_name (outf, sizeof (outf)-1, keyid); |
1346 |
strcpy (outf, temp); |
get_temp_name (inf, sizeof (inf)-1, NULL); |
1347 |
strcat (outf, keyid); |
fp = fopen (inf, "w+b"); |
1348 |
strcat (outf, ".out"); |
if (!fp) { |
1349 |
strcat (temp, keyid); |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", inf, |
|
inp = fopen (temp, "w+b"); |
|
|
if( !inp ) { |
|
|
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", temp, |
|
1350 |
winpt_strerror (WPTERR_FILE_OPEN)); |
winpt_strerror (WPTERR_FILE_OPEN)); |
|
rc = -1; |
|
1351 |
goto leave; |
goto leave; |
1352 |
} |
} |
1353 |
fprintf (inp, |
fprintf (fp, |
1354 |
"VERSION 0\n" |
"VERSION 1\n" |
1355 |
|
"PROGRAM 1.4.3-cvs\n" |
1356 |
|
"SCHEME ldap\n" |
1357 |
"HOST %s\n" |
"HOST %s\n" |
|
"OPTION verbose\n" |
|
1358 |
"COMMAND GET\n" |
"COMMAND GET\n" |
1359 |
"\n" |
"\n" |
1360 |
"%s\n", host? skip_type_prefix (host): "64.94.85.200", keyid); |
"%s\n", |
1361 |
fclose (inp); |
host? skip_type_prefix (host): "64.94.85.200", keyid); |
1362 |
|
fclose (fp); |
1363 |
memset (&si, 0, sizeof (si)); |
|
1364 |
si.cb = sizeof (si); |
p = safe_alloc (strlen (ksprg) + strlen (inf) + strlen (outf) + 32); |
1365 |
si.dwFlags = STARTF_USESHOWWINDOW; |
sprintf (p, "%s -o %s %s", ksprg, outf, inf); |
1366 |
si.wShowWindow = SW_HIDE; |
if (spawn_application (p)) { |
1367 |
memset (&pi, 0, sizeof (pi)); |
fp = NULL; |
|
p = new char[strlen (ksprg) + strlen (temp) + strlen (outf) + 32]; |
|
|
sprintf (p, "%s -o %s %s", ksprg, outf, temp); |
|
|
rc = CreateProcess (NULL, p, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi); |
|
|
if (!rc) { |
|
|
log_box ("LDAP Keyserver Plugin", MB_ERR, "Could not spawn process"); |
|
|
rc = -1; |
|
1368 |
goto leave; |
goto leave; |
1369 |
} |
} |
1370 |
CloseHandle (pi.hThread); |
fp = fopen (outf, "rb"); |
1371 |
WaitForSingleObject (pi.hProcess, INFINITE); |
if (!fp) |
|
CloseHandle (pi.hProcess); |
|
|
|
|
|
inp = fopen (outf, "rb"); |
|
|
if (!inp) { |
|
1372 |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf, |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf, |
1373 |
winpt_strerror (WPTERR_FILE_OPEN)); |
winpt_strerror (WPTERR_FILE_OPEN)); |
1374 |
rc = -1; |
|
1375 |
goto leave; |
leave: |
1376 |
} |
DeleteFile (inf); |
1377 |
memset (key, 0, maxkeylen); |
DeleteFile (outf); |
1378 |
while( !feof( inp ) ) { |
free_if_alloc (p); |
1379 |
s = fgets( temp, sizeof (temp)-1, inp ); |
free_if_alloc (ksprg); |
1380 |
if( !s ) |
return fp; |
1381 |
|
} |
1382 |
|
|
1383 |
|
/* Receive an key via LDAP from host @host with the keyid @keyid. |
1384 |
|
@key contains the key on success. */ |
1385 |
|
int |
1386 |
|
ldap_recvkey (const char *host, const char *keyid, |
1387 |
|
char **r_key, int *r_keylen) |
1388 |
|
{ |
1389 |
|
gpgme_data_t raw; |
1390 |
|
FILE *fp; |
1391 |
|
const char *s; |
1392 |
|
char buf[512]; |
1393 |
|
int start_key = 0, failed = 0; |
1394 |
|
int rc = 0; |
1395 |
|
|
1396 |
|
fp = do_spawn_ldap_helper (host, keyid); |
1397 |
|
if (!fp) |
1398 |
|
return WPTERR_GENERAL; |
1399 |
|
|
1400 |
|
if (gpgme_data_new (&raw)) |
1401 |
|
BUG (0); |
1402 |
|
while (!feof (fp)) { |
1403 |
|
s = fgets (buf, sizeof (buf)-1, fp); |
1404 |
|
if (!s) |
1405 |
|
break; |
1406 |
|
if (strstr (s, "KEY") && strstr (s, "FAILED")) { |
1407 |
|
failed = 1; |
1408 |
break; |
break; |
1409 |
if( !start_key && strstr( s, "KEY" ) && strstr( s, "BEGIN" ) ) { |
} |
1410 |
|
if (!start_key && strstr (s, "KEY") && strstr (s, "BEGIN")) { |
1411 |
start_key = 1; |
start_key = 1; |
1412 |
continue; |
continue; |
1413 |
} |
} |
1414 |
if( !start_key ) |
if (!start_key) |
1415 |
continue; |
continue; |
1416 |
strcat( key, temp ); |
gpgme_data_write (raw, buf, strlen (buf)); |
1417 |
maxkeylen -= strlen( temp ); |
if (strstr (s, "KEY") && strstr (s, "END")) |
|
if( maxkeylen < 0 || (strstr( s, "KEY" ) && strstr( s, "END" )) ) |
|
1418 |
break; |
break; |
1419 |
} |
} |
1420 |
fclose( inp ); |
fclose (fp); |
1421 |
|
|
1422 |
leave: |
if (failed) |
|
if( !strlen( key ) ) |
|
1423 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
1424 |
free_if_alloc( p ); |
*r_key = data_release_and_get_mem (raw, r_keylen); |
|
free_if_alloc( ksprg ); |
|
1425 |
return rc; |
return rc; |
|
} /* ldap_recvkey */ |
|
|
|
|
|
|
|
|
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; |
|
1426 |
} |
} |
1427 |
|
|
1428 |
|
|
1429 |
|
/* Receive an key via FINGER from host @host with the user @user. |
1430 |
|
On success @key contains the key. */ |
1431 |
int |
int |
1432 |
finger_recvkey (const char * host, const char * user, char * key, int maxkeylen) |
finger_recvkey (const char *host, const char *user, |
1433 |
|
char **r_key, int *r_keylen) |
1434 |
{ |
{ |
1435 |
struct hostent * hp; |
gpgme_data_t raw; |
1436 |
struct sockaddr_in saddr; |
char buf[256]; |
|
char buf[128]; |
|
1437 |
int fd, nread; |
int fd, nread; |
1438 |
int start_key = 0; |
int start_key = 0; |
1439 |
int rc=0; |
int rc=0; |
1440 |
|
|
1441 |
fd = socket (PF_INET, SOCK_STREAM, 0); |
rc = kserver_connect (host, FINGER_PORT, &fd); |
1442 |
if (fd == -1) |
if (rc) |
1443 |
return WPTERR_WINSOCK_SOCKET; |
return rc; |
|
hp = gethostbyname (skip_type_prefix (host)); |
|
|
if (!hp) { |
|
|
closesocket (fd); |
|
|
return WPTERR_WINSOCK_RESOLVE; |
|
|
} |
|
1444 |
|
|
1445 |
memset (&saddr, 0, sizeof (saddr)); |
sock_write (fd, user, strlen (user)); |
1446 |
saddr.sin_family = AF_INET; |
sock_write (fd, "\r\n", 2); |
1447 |
saddr.sin_port = htons (FINGER_PORT); |
|
1448 |
saddr.sin_addr = *(struct in_addr *)hp->h_addr; |
if (gpgme_data_new (&raw)) |
1449 |
if (connect (fd, (struct sockaddr *)&saddr, sizeof (saddr))) { |
BUG (0); |
1450 |
closesocket (fd); |
|
1451 |
return WPTERR_WINSOCK_CONNECT; |
for (;;) { |
1452 |
} |
if (sock_getline (fd, buf, sizeof (buf), &nread)) |
1453 |
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; |
|
1454 |
strcat (buf, "\n"); |
strcat (buf, "\n"); |
1455 |
if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) { |
if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) { |
1456 |
strcat (key, buf); |
gpgme_data_write (raw, buf, nread); |
1457 |
start_key = 1; |
start_key = 1; |
|
maxkeylen -= nread; |
|
1458 |
} |
} |
1459 |
else if (strstr (buf, "END PGP PUBLIC KEY BLOCK" ) && start_key) { |
else if (strstr (buf, "END PGP PUBLIC KEY BLOCK" ) && start_key) { |
1460 |
strcat (key, buf); |
gpgme_data_write (raw, buf, nread); |
1461 |
start_key--; |
start_key--; |
|
maxkeylen -= nread; |
|
1462 |
break; |
break; |
1463 |
} |
} |
1464 |
else if (start_key) { |
else if (start_key) |
1465 |
strcat (key, buf); |
gpgme_data_write (raw, buf, nread); |
|
maxkeylen -= nread; |
|
|
} |
|
1466 |
} |
} |
1467 |
|
|
1468 |
closesocket (fd); |
closesocket (fd); |
1469 |
if (start_key != 0) |
if (start_key != 0) |
1470 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
1471 |
|
*r_key = data_release_and_get_mem (raw, r_keylen); |
1472 |
return rc; |
return rc; |
1473 |
} |
} |
1474 |
|
|
1475 |
|
|
1476 |
|
/* Check if the given name @name is a valid hostname. */ |
1477 |
int |
int |
1478 |
check_IP_or_hostname (const char *name) |
check_IP_or_hostname (const char *name) |
1479 |
{ |
{ |
1488 |
} |
} |
1489 |
return 0; |
return 0; |
1490 |
} |
} |
1491 |
|
|
1492 |
|
|
1493 |
|
/* Split the URL @r_keyserver into the host and the port |
1494 |
|
part if possible. */ |
1495 |
|
gpgme_error_t |
1496 |
|
parse_keyserver_url (char **r_keyserver, unsigned short *r_port) |
1497 |
|
{ |
1498 |
|
char *p; |
1499 |
|
char *url = *r_keyserver; |
1500 |
|
|
1501 |
|
/* no port is given so use the default port. */ |
1502 |
|
p = strrchr (url, ':'); |
1503 |
|
if (p == strchr (url, ':')) { |
1504 |
|
int port = port_from_proto (proto_from_URL (url)); |
1505 |
|
if (!port) |
1506 |
|
port = HKP_PORT; |
1507 |
|
*r_port = port; |
1508 |
|
return 0; |
1509 |
|
} |
1510 |
|
/* XXX: remove / in .de/:11371 */ |
1511 |
|
*r_keyserver = substr (url, 0, (p-url)); |
1512 |
|
*r_port = atoi (url+(p-url)+1); |
1513 |
|
free_if_alloc (url); |
1514 |
|
return 0; |
1515 |
|
} |