1 |
/* wptKeyserver.cpp - W32 Keyserver Access |
2 |
* Copyright (C) 2000-2006 Timo Schulz |
3 |
* Copyright (C) 2001 Marco Cunha |
4 |
* |
5 |
* This file is part of WinPT. |
6 |
* |
7 |
* WinPT is free software; you can redistribute it and/or |
8 |
* modify it under the terms of the GNU General Public License |
9 |
* as published by the Free Software Foundation; either version 2 |
10 |
* of the License, or (at your option) any later version. |
11 |
* |
12 |
* WinPT is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 |
* General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with WinPT; if not, write to the Free Software Foundation, |
19 |
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
*/ |
21 |
|
22 |
#ifdef HAVE_CONFIG_H |
23 |
#include <config.h> |
24 |
#endif |
25 |
|
26 |
#include <windows.h> |
27 |
#include <shlobj.h> |
28 |
#include <stdio.h> |
29 |
#include <sys/stat.h> |
30 |
#include <ctype.h> |
31 |
|
32 |
#include "wptKeyserver.h" |
33 |
#include "wptErrors.h" |
34 |
#include "wptTypes.h" |
35 |
#include "wptNLS.h" |
36 |
#include "wptW32API.h" |
37 |
#include "wptGPG.h" |
38 |
#include "wptRegistry.h" |
39 |
|
40 |
/* Map net_errno to a winsock error. */ |
41 |
#define net_errno ((int)WSAGetLastError ()) |
42 |
|
43 |
|
44 |
keyserver server[MAX_KEYSERVERS] = {0}; |
45 |
keyserver_proxy_s proxy = {0}; |
46 |
static const char *server_list[] = { |
47 |
"hkp://gnv.us.ks.cryptnet.net", |
48 |
"hkp://keyserver.kjsl.com", |
49 |
"hkp://sks.keyserver.penguin.de", |
50 |
"hkp://subkeys.pgp.net", |
51 |
"ldap://keyserver.pgp.com", |
52 |
NULL |
53 |
}; |
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 |
/* Basic64 encode the input @inbuf to @outbuf. */ |
67 |
static void |
68 |
base64_encode (const char *inbuf, char *outbuf) |
69 |
{ |
70 |
char base64code[] = |
71 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
72 |
int index = 0, temp = 0, res = 0; |
73 |
int i = 0, inputlen = 0, len = 0; |
74 |
|
75 |
inputlen = strlen (inbuf); |
76 |
for (i = 0; i < inputlen; i++) { |
77 |
res = temp; |
78 |
res = (res << 8) | (inbuf[i] & 0xFF); |
79 |
switch (index++) { |
80 |
case 0: |
81 |
outbuf[len++] = base64code[res >> 2 & 0x3F]; |
82 |
res &= 0x3; |
83 |
break; |
84 |
case 1: |
85 |
outbuf[len++] = base64code[res >> 4 & 0x3F]; |
86 |
res &= 0xF; |
87 |
break; |
88 |
case 2: |
89 |
outbuf[len++] = base64code[res >> 6 & 0x3F]; |
90 |
outbuf[len++] = base64code[res & 0x3F]; |
91 |
res = index = 0; |
92 |
break; |
93 |
} |
94 |
temp = res; |
95 |
} |
96 |
|
97 |
if (index == 2) { |
98 |
outbuf[len++] = base64code[temp << 2 & 0x3F]; |
99 |
outbuf[len++] = '='; |
100 |
} |
101 |
else if (index == 1) { |
102 |
outbuf[len++] = base64code[temp << 4 & 0x3F]; |
103 |
outbuf[len++] = '='; |
104 |
outbuf[len++] = '='; |
105 |
} |
106 |
|
107 |
outbuf[len] = '\0'; |
108 |
} |
109 |
|
110 |
|
111 |
/* Skip the URL schema and return only the host part of it. */ |
112 |
static const char* |
113 |
skip_type_prefix (const char *hostname) |
114 |
{ |
115 |
if (hostname && !strncmp (hostname, "http://", 7)) |
116 |
hostname += 7; |
117 |
else if (hostname && !strncmp (hostname, "hkp://", 6)) |
118 |
hostname += 6; |
119 |
else if (hostname && !strncmp (hostname, "finger://", 9)) |
120 |
hostname += 9; |
121 |
else if (hostname && !strncmp (hostname, "ldap://", 7)) |
122 |
hostname += 7; |
123 |
return hostname; |
124 |
} |
125 |
|
126 |
|
127 |
/* Check that the keyserver response indicates an OK. |
128 |
Return 0 on success. */ |
129 |
static int |
130 |
check_hkp_response (const char *resp, int recv) |
131 |
{ |
132 |
char *p, *end; |
133 |
int ec, len; |
134 |
|
135 |
ec = recv ? WPTERR_WINSOCK_RECVKEY : WPTERR_WINSOCK_SENDKEY; |
136 |
if (!strstr (resp, "HTTP/1.0 200 OK") && |
137 |
!strstr (resp, "HTTP/1.1 200 OK")) /* http error */ |
138 |
return ec; |
139 |
if (strstr (resp, "Public Key Server -- Error") |
140 |
|| strstr (resp, "Public Key Server -- Error") |
141 |
|| strstr (resp, "No matching keys in database")) { |
142 |
p = strstr (resp, "<p>"); |
143 |
if (p && strlen (p) < sizeof (hkp_errmsg)) { |
144 |
end = strstr (p, "</p>"); |
145 |
len = end? (end - p + 1) : strlen (p); |
146 |
memset (hkp_errmsg, 0, sizeof (hkp_errmsg)); |
147 |
strncpy (hkp_errmsg, p, len); |
148 |
hkp_err = 1; |
149 |
} |
150 |
return ec; |
151 |
} |
152 |
return 0; |
153 |
} |
154 |
|
155 |
|
156 |
/* Read a single line (\r\n) from a socket. |
157 |
Return 0 on success. */ |
158 |
static int |
159 |
sock_getline (int fd, char *buf, int buflen, int *nbytes) |
160 |
{ |
161 |
char ch; |
162 |
int nread = 0; |
163 |
|
164 |
if (nbytes) |
165 |
*nbytes = 0; |
166 |
*buf = 0; |
167 |
while (recv (fd, &ch, 1, 0) > 0) { |
168 |
*buf++ = ch; |
169 |
nread++; |
170 |
if (ch == '\r') |
171 |
continue; /* remove this char. */ |
172 |
if (ch == '\n' || nread == (buflen - 1)) { |
173 |
*buf = 0; |
174 |
if (nbytes) |
175 |
*nbytes = nread; |
176 |
return 0; |
177 |
} |
178 |
} |
179 |
|
180 |
return -1; |
181 |
} |
182 |
|
183 |
|
184 |
/* Perform a select() on the given fd to find out |
185 |
is there is data for reading. Wait at least @nsecs seconds. */ |
186 |
static int |
187 |
sock_select (int fd, int nsecs) |
188 |
{ |
189 |
FD_SET rfd; |
190 |
timeval tv = {nsecs, 0}; |
191 |
|
192 |
FD_ZERO (&rfd); |
193 |
FD_SET (fd, &rfd); |
194 |
if (select (fd + 1, &rfd, NULL, NULL, &tv) == SOCKET_ERROR) { |
195 |
log_debug ("sock_select: select() failed ec=%d.\r\n", net_errno); |
196 |
return SOCKET_ERROR; |
197 |
} |
198 |
if (FD_ISSET (fd, &rfd)) |
199 |
return 1; |
200 |
return 0; |
201 |
} |
202 |
|
203 |
|
204 |
/* Read from a socket @fd to buffer @buf with a fixed timeout |
205 |
of 10 seconds. The amount of bytes which were read are |
206 |
returned in @nbytes. |
207 |
Return value: 0 on success. */ |
208 |
static int |
209 |
sock_read (int fd, char *buf, int buflen, int *nbytes) |
210 |
{ |
211 |
DWORD nread; |
212 |
int nleft = buflen; |
213 |
int rc, n = 0; |
214 |
|
215 |
if (nbytes) |
216 |
*nbytes = 0; |
217 |
while (nleft > 0) { |
218 |
if (n >= default_socket_timeout) |
219 |
return WPTERR_WINSOCK_TIMEOUT; |
220 |
rc = sock_select (fd, 1); |
221 |
if (rc == SOCKET_ERROR) |
222 |
return rc; |
223 |
else if (!rc) |
224 |
n++; |
225 |
else { |
226 |
nread = recv (fd, buf, nleft, 0); |
227 |
if (nread == SOCKET_ERROR) { |
228 |
log_debug ("sock_read: recv() failed ec=%d.\r\n", net_errno); |
229 |
return SOCKET_ERROR; |
230 |
} |
231 |
else if (!nread) |
232 |
break; |
233 |
nleft -= nread; |
234 |
buf += nread; |
235 |
} |
236 |
} |
237 |
if (nbytes) |
238 |
*nbytes = buflen - nleft; |
239 |
|
240 |
return 0; |
241 |
} |
242 |
|
243 |
|
244 |
/* Helper to create a string from the gpgme data and |
245 |
then release the gpgme data object. */ |
246 |
char* |
247 |
data_release_and_get_mem (gpgme_data_t in, int *r_bufferlen) |
248 |
{ |
249 |
size_t n; |
250 |
char *buffer, *p; |
251 |
|
252 |
gpg_data_putc (in, '\0'); |
253 |
p = gpgme_data_release_and_get_mem (in, &n); |
254 |
buffer = m_strdup (p); |
255 |
if (r_bufferlen) |
256 |
*r_bufferlen = (int)n; |
257 |
gpgme_free (p); |
258 |
return buffer; |
259 |
} |
260 |
|
261 |
|
262 |
/* Read much data as possible from the socket @fd and |
263 |
return the data in @buffer. Caller must free data. |
264 |
Return value: 0 on success. */ |
265 |
int |
266 |
sock_read_ext (int fd, char **buffer, int *r_bufferlen) |
267 |
{ |
268 |
gpgme_data_t dh; |
269 |
char buf[1024]; |
270 |
size_t n=0; |
271 |
int nread, rc; |
272 |
|
273 |
if (gpgme_data_new (&dh)) |
274 |
BUG (0); |
275 |
while (n < 10) { |
276 |
rc = sock_select (fd, 1); |
277 |
if (rc == SOCKET_ERROR) { |
278 |
gpgme_data_release (dh); |
279 |
return rc; |
280 |
} |
281 |
else if (!rc) |
282 |
n++; |
283 |
else { |
284 |
nread = recv (fd, buf, sizeof (buf), 0); |
285 |
if (nread == SOCKET_ERROR) |
286 |
return SOCKET_ERROR; |
287 |
else if (!nread) |
288 |
break; |
289 |
gpgme_data_write (dh, buf, nread); |
290 |
} |
291 |
} |
292 |
|
293 |
*buffer = data_release_and_get_mem (dh, r_bufferlen); |
294 |
return 0; |
295 |
} |
296 |
|
297 |
|
298 |
/* Write the buffer @buf with the length @buflen to a socket @fd. */ |
299 |
static int |
300 |
sock_write (int fd, const char *buf, int buflen) |
301 |
{ |
302 |
DWORD nwritten; |
303 |
int nleft = buflen; |
304 |
|
305 |
while (nleft > 0) { |
306 |
nwritten = send (fd, buf, nleft, 0); |
307 |
if (nwritten == SOCKET_ERROR) { |
308 |
log_debug ("sock_write: send() failed ec=%d\r\n", net_errno); |
309 |
return SOCKET_ERROR; |
310 |
} |
311 |
nleft -= nwritten; |
312 |
buf += nwritten; |
313 |
} |
314 |
|
315 |
return 0; |
316 |
} |
317 |
|
318 |
|
319 |
/* Initialize the Winsock2 interface.*/ |
320 |
int |
321 |
wsock_init (void) |
322 |
{ |
323 |
WSADATA wsa; |
324 |
|
325 |
if (WSAStartup (0x202, &wsa)) { |
326 |
log_debug ("wsock_init: WSAStartup failed ec=%d\r\n", net_errno); |
327 |
return WPTERR_WINSOCK_INIT; |
328 |
} |
329 |
return 0; |
330 |
} |
331 |
|
332 |
|
333 |
/* Cleanup the Winsock2 interface. */ |
334 |
void |
335 |
wsock_end (void) |
336 |
{ |
337 |
char *p; |
338 |
int i; |
339 |
|
340 |
p = make_special_filename (CSIDL_APPDATA, "winpt\\keyserver.conf", NULL); |
341 |
kserver_save_conf (p); |
342 |
free_if_alloc (p); |
343 |
free_if_alloc (default_keyserver); |
344 |
for (i=0; i < MAX_KEYSERVERS; i++) { |
345 |
if (server[i].used && server[i].name != NULL) |
346 |
free_if_alloc (server[i].name); |
347 |
} |
348 |
kserver_proxy_release (&proxy); |
349 |
WSACleanup (); |
350 |
} |
351 |
|
352 |
|
353 |
/* Return a string representation of a winsock error. */ |
354 |
const char* |
355 |
wsock_strerror (void) |
356 |
{ |
357 |
int ec = WSAGetLastError (); |
358 |
|
359 |
if (!ec) |
360 |
return ""; |
361 |
switch (ec) { |
362 |
case WSAENETDOWN: |
363 |
return _("Network unreachable"); |
364 |
|
365 |
case WSAEHOSTUNREACH: |
366 |
return _("Host unreachable"); |
367 |
|
368 |
case WSAHOST_NOT_FOUND: |
369 |
return _("Could not resolve host name"); |
370 |
|
371 |
case WSAECONNREFUSED: |
372 |
return _("Connection refused"); |
373 |
|
374 |
case WSAETIMEDOUT: |
375 |
case WSAECONNABORTED: |
376 |
return _("Connection timeout"); |
377 |
|
378 |
case WSAENETRESET: |
379 |
case WSAECONNRESET: |
380 |
return _("Connection resetted by peer"); |
381 |
|
382 |
case WSAESHUTDOWN: |
383 |
return _("Socket has been shutdown"); |
384 |
|
385 |
default: |
386 |
break; |
387 |
} |
388 |
|
389 |
return ""; |
390 |
} |
391 |
|
392 |
|
393 |
/* Set default socket timeout for all reading operations. */ |
394 |
void |
395 |
kserver_set_socket_timeout (int nsec) |
396 |
{ |
397 |
if (nsec < 0) |
398 |
nsec = 0; |
399 |
default_socket_timeout = nsec; |
400 |
} |
401 |
|
402 |
|
403 |
/* Return the last keyserver error as a string. */ |
404 |
const char* |
405 |
kserver_strerror (void) |
406 |
{ |
407 |
if (hkp_err) |
408 |
return hkp_errmsg; |
409 |
return NULL; |
410 |
} |
411 |
|
412 |
|
413 |
/* Read a keyserver from the list at index @idx. |
414 |
Return value: keyserver name at the given position. */ |
415 |
const char* |
416 |
kserver_get_hostname (int idx, int type, WORD *port) |
417 |
{ |
418 |
if (type == -1) { |
419 |
*port = default_keyserver_port; |
420 |
return default_keyserver; |
421 |
} |
422 |
else if (!type && idx < DIM (server_list)) { |
423 |
*port = HKP_PORT; |
424 |
return server_list[idx]; |
425 |
} |
426 |
return NULL; |
427 |
} |
428 |
|
429 |
|
430 |
/* Check if the computer is connected to the internet. |
431 |
Return 0 on success -1 otherwise. */ |
432 |
int |
433 |
kserver_check_inet_connection (void) |
434 |
{ |
435 |
int fd; |
436 |
|
437 |
if (!kserver_connect (default_keyserver, default_keyserver_port, &fd)) { |
438 |
closesocket (fd); |
439 |
return 0; |
440 |
} |
441 |
return -1; |
442 |
} |
443 |
|
444 |
|
445 |
/* If the request contains the keyid, it have to be |
446 |
always postfix with '0x'+keyid. This code checks |
447 |
if the keyid is a decimal value and if so if it contains |
448 |
the '0x'. If not it is inserted. */ |
449 |
const char* |
450 |
kserver_check_keyid (const char *keyid) |
451 |
{ |
452 |
static char id[21]; |
453 |
|
454 |
if (strstr (keyid, "@")) |
455 |
return keyid; /* email address */ |
456 |
if (strncmp (keyid, "0x", 2)) { |
457 |
memset (&id, 0, sizeof (id)); |
458 |
_snprintf (id, sizeof (id)-1, "0x%s", keyid); |
459 |
return id; |
460 |
} |
461 |
return keyid; |
462 |
} |
463 |
|
464 |
|
465 |
/* Update the keyserver proxy user. */ |
466 |
static void |
467 |
update_proxy_user (const char *proxy_user, const char *proxy_pass) |
468 |
{ |
469 |
char t[257]; /* user:pass = 127+1+127+1 = 257 */ |
470 |
int n = 0; |
471 |
|
472 |
n = 4*strlen (proxy_user) / 3 + 32 + strlen (proxy_pass) + 2; |
473 |
free_if_alloc (proxy.base64_user); |
474 |
proxy.base64_user = new char[n]; |
475 |
if (!proxy.base64_user) |
476 |
BUG (0); |
477 |
_snprintf (t, sizeof (t)-1, "%s:%s", proxy_user, proxy_pass); |
478 |
base64_encode (t, proxy.base64_user); |
479 |
free_if_alloc (proxy.user); |
480 |
free_if_alloc (proxy.pass); |
481 |
proxy.user = m_strdup (proxy_user); |
482 |
proxy.pass = m_strdup (proxy_pass); |
483 |
} |
484 |
|
485 |
|
486 |
/* Check that the given buffer contains a valid keyserver URL. */ |
487 |
static int |
488 |
check_URL (const char * buf) |
489 |
{ |
490 |
if (strlen (buf) < 7) |
491 |
return -1; |
492 |
if (!strstr (buf, "ldap://") |
493 |
&& !strstr (buf, "http://") |
494 |
&& !strstr (buf, "finger://") |
495 |
&& !strstr (buf, "hkp://")) |
496 |
return -1; |
497 |
return 0; |
498 |
} |
499 |
|
500 |
|
501 |
/* Get the port number from the given protocol. */ |
502 |
static int |
503 |
port_from_proto (int proto) |
504 |
{ |
505 |
switch (proto) { |
506 |
case KSPROTO_LDAP: return 0; |
507 |
case KSPROTO_FINGER: return FINGER_PORT; |
508 |
case KSPROTO_HTTP: return HKP_PORT; |
509 |
} |
510 |
return 0; |
511 |
} |
512 |
|
513 |
|
514 |
/* Get the port number from the given URL. */ |
515 |
static int |
516 |
proto_from_URL (const char *buf) |
517 |
{ |
518 |
if (strstr( buf, "ldap")) |
519 |
return KSPROTO_LDAP; |
520 |
else if (strstr( buf, "finger")) |
521 |
return KSPROTO_FINGER; |
522 |
return KSPROTO_HKP; |
523 |
} |
524 |
|
525 |
|
526 |
void |
527 |
keyserver_set_default (const char *hostname, WORD port) |
528 |
{ |
529 |
if (hostname != NULL) { |
530 |
free_if_alloc (default_keyserver); |
531 |
default_keyserver = m_strdup (hostname); |
532 |
default_keyserver_port = port; |
533 |
} |
534 |
if (!port) |
535 |
port = HKP_PORT; |
536 |
if (!default_keyserver) |
537 |
default_keyserver = m_strdup (DEF_HKP_KEYSERVER); |
538 |
server[0].name = m_strdup (default_keyserver); |
539 |
server[0].used = 1; |
540 |
server[0].port = port; |
541 |
server[0].proto = proto_from_URL (default_keyserver); |
542 |
} |
543 |
|
544 |
|
545 |
/* Skip all kind of whitespace chars in @str. */ |
546 |
static const char* |
547 |
skip_whitespace (const char *str) |
548 |
{ |
549 |
while (str && *str) { |
550 |
if (*str == ' ' || |
551 |
*str == '\t' || |
552 |
*str == '\n' || |
553 |
*str == '\r') { |
554 |
str++; |
555 |
continue; |
556 |
} |
557 |
break; |
558 |
} |
559 |
return str; |
560 |
} |
561 |
|
562 |
|
563 |
/* Save the keyserver config file in @conf. */ |
564 |
int |
565 |
kserver_save_conf (const char *conf) |
566 |
{ |
567 |
FILE *fp; |
568 |
int pos; |
569 |
|
570 |
fp = fopen (conf, "wb"); |
571 |
if (!fp) { |
572 |
msg_box (NULL, _("Could not save keyserver.conf file"), |
573 |
_("Keyserver"), MB_ERR); |
574 |
return -1; |
575 |
} |
576 |
|
577 |
fprintf (fp, "# do NOT manually modify this file, it will be " |
578 |
"generated automatically!!\r\n"); |
579 |
for (pos = 0; pos < MAX_KEYSERVERS; pos++) { |
580 |
if (!server[pos].used) |
581 |
continue; |
582 |
fprintf (fp, "%s:%d\r\n", server[pos].name, server[pos].port); |
583 |
} |
584 |
fclose (fp); |
585 |
return 0; |
586 |
} |
587 |
|
588 |
|
589 |
/* Load the keyserver config file @conf. */ |
590 |
int |
591 |
kserver_load_conf (const char *conf) |
592 |
{ |
593 |
FILE *fp; |
594 |
char buf[1024], *s, *p; |
595 |
char *user = NULL, *pass = NULL; |
596 |
int no_config=0, chk_pos=0; |
597 |
int pos; |
598 |
|
599 |
for (pos = 0; pos < MAX_KEYSERVERS; pos++) { |
600 |
server[pos].used = 0; |
601 |
server[pos].port = HKP_PORT; |
602 |
} |
603 |
|
604 |
fp = fopen (conf, "rb"); |
605 |
if (!fp) { |
606 |
for (pos = 0; server_list[pos]; pos++) { |
607 |
server[pos].used = 1; |
608 |
server[pos].name = m_strdup (server_list[pos]); |
609 |
server[pos].proto = proto_from_URL (server_list[pos]); |
610 |
} |
611 |
no_config=1; |
612 |
} |
613 |
get_reg_proxy_prefs (&proxy); |
614 |
if (user && pass) |
615 |
update_proxy_user (user, pass); |
616 |
else if (user && !pass || !user && pass) { |
617 |
msg_box (NULL, _("Invalid proxy configuration." |
618 |
"You need to set a user and a password" |
619 |
"to use proxy authentication!"), |
620 |
_("Proxy Error"), MB_ERR); |
621 |
} |
622 |
/* check if the host has a http:// prefix and skip it */ |
623 |
if (proxy.host && !strncmp (proxy.host, "http://", 7)) { |
624 |
char *host = m_strdup (proxy.host+7); |
625 |
free_if_alloc (proxy.host); |
626 |
proxy.host = host; |
627 |
} |
628 |
free_if_alloc (user); |
629 |
free_if_alloc (pass); |
630 |
|
631 |
pos = 0; |
632 |
while (fp && !feof (fp)) { |
633 |
s = fgets (buf, sizeof (buf)-1, fp); |
634 |
if (!s) |
635 |
break; |
636 |
if (strstr (buf, "\r\n")) |
637 |
buf[strlen (buf)-2] = '\0'; |
638 |
if (strstr (buf, "\n")) |
639 |
buf[strlen (buf)-1] = '\0'; |
640 |
s = (char *)skip_whitespace (buf); |
641 |
if (*s == '#' || strlen (s) < 7) |
642 |
continue; |
643 |
if (check_URL (s)) { |
644 |
msg_box (NULL, _("All entries of this file must have a valid prefix.\n" |
645 |
"Currently HKP/HTTP, LDAP and FINGER are supported.\n"), |
646 |
_("Keyserver Error"), MB_ERR); |
647 |
continue; |
648 |
} |
649 |
chk_pos = 6; |
650 |
if (strstr (s, "finger")) |
651 |
chk_pos = 10; |
652 |
p = strchr (s+chk_pos, ':'); |
653 |
server[pos].used = 1; |
654 |
server[pos].proto = proto_from_URL (s); |
655 |
server[pos].port = port_from_proto (server[pos].proto); |
656 |
if (!p) |
657 |
server[pos].name = m_strdup (s); |
658 |
else { |
659 |
server[pos].port = atol (p+1); |
660 |
if (server[pos].port < 0 || server[pos].port > 65536) |
661 |
server[pos].port = HKP_PORT; |
662 |
server[pos].name = new char [(p-s)+2]; |
663 |
if (!server[pos].name) |
664 |
BUG (0); |
665 |
memset (server[pos].name, 0, (p-s)+2); |
666 |
memcpy (server[pos].name, s, (p-s)); |
667 |
} |
668 |
pos++; |
669 |
if (pos > MAX_KEYSERVERS) { |
670 |
msg_box (NULL, _("The keyserver limit is exceeded"), |
671 |
_("Keyserver Warning"), MB_INFO); |
672 |
break; |
673 |
} |
674 |
} |
675 |
if (fp) |
676 |
fclose (fp); |
677 |
if (!pos) { |
678 |
/* only issue an error if the config file exist but |
679 |
it has no valid entries. */ |
680 |
keyserver_set_default (NULL, HKP_PORT); |
681 |
if (!no_config) |
682 |
return WPTERR_CONFIG_FILE; |
683 |
} |
684 |
|
685 |
return 0; |
686 |
} |
687 |
|
688 |
|
689 |
/* Connect to the keyserver @hostname (port @port). |
690 |
Return value: 0 on success */ |
691 |
int |
692 |
kserver_connect (const char *hostname, WORD port, int *conn_fd) |
693 |
{ |
694 |
struct hostent *hp; |
695 |
struct sockaddr_in sock; |
696 |
char host[128] = {0}; |
697 |
DWORD iaddr; |
698 |
int rc, fd; |
699 |
|
700 |
log_debug ("kserver_connect: %s:%d\r\n", hostname, port); |
701 |
|
702 |
if (!port) |
703 |
port = HKP_PORT; |
704 |
if (conn_fd) |
705 |
*conn_fd = 0; |
706 |
hostname = skip_type_prefix (hostname); |
707 |
|
708 |
if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) |
709 |
port = proxy.port; |
710 |
memset (&sock, 0, sizeof (sock)); |
711 |
sock.sin_family = AF_INET; |
712 |
sock.sin_port = htons (port); |
713 |
if (proxy.host) |
714 |
strncpy (host, proxy.host, 127); |
715 |
else |
716 |
strncpy (host, hostname, 127); |
717 |
|
718 |
if ((iaddr = inet_addr (host)) != INADDR_NONE) |
719 |
memcpy (&sock.sin_addr, &iaddr, sizeof (iaddr)); |
720 |
else if ((hp = gethostbyname (host))) { |
721 |
if (hp->h_addrtype != AF_INET || hp->h_length != 4) { |
722 |
log_debug ("gethostbyname: unknown address type.\r\n"); |
723 |
return WPTERR_WINSOCK_RESOLVE; |
724 |
} |
725 |
memcpy (&sock.sin_addr, hp->h_addr, hp->h_length); |
726 |
} |
727 |
else { |
728 |
log_debug ("gethostbyname: failed ec=%d.\r\n", net_errno); |
729 |
return WPTERR_WINSOCK_RESOLVE; |
730 |
} |
731 |
fd = socket (AF_INET, SOCK_STREAM, 0); |
732 |
if (fd == INVALID_SOCKET) |
733 |
return WPTERR_WINSOCK_SOCKET; |
734 |
rc = connect (fd, (struct sockaddr *) &sock, sizeof (sock)); |
735 |
if (rc == SOCKET_ERROR) { |
736 |
log_debug ("connect: failed ec=%d.\r\n", net_errno); |
737 |
closesocket (fd); |
738 |
return WPTERR_WINSOCK_CONNECT; |
739 |
} |
740 |
|
741 |
if (proxy.proto == PROXY_PROTO_SOCKS5) { |
742 |
rc = socks_handshake (&proxy, fd, hostname, port); |
743 |
if (rc) { |
744 |
closesocket (fd); |
745 |
return WPTERR_GENERAL; /* XXX: use better code. */ |
746 |
} |
747 |
} |
748 |
|
749 |
if (conn_fd) |
750 |
*conn_fd = fd; |
751 |
WSASetLastError (0); |
752 |
return 0; |
753 |
} |
754 |
|
755 |
|
756 |
static bool |
757 |
URL_must_encoded (const char *url) |
758 |
{ |
759 |
if (strchr (url, '.') || strchr (url, '@') || strchr (url, ' ')) |
760 |
return true; |
761 |
return false; |
762 |
} |
763 |
|
764 |
|
765 |
/* Perform URL encoding of the given data. */ |
766 |
static char* |
767 |
URL_encode (const char *url, size_t ulen, size_t *ret_nlen) |
768 |
{ |
769 |
gpgme_data_t hd; |
770 |
char numbuf[5], *p; |
771 |
size_t i; |
772 |
int nlen; |
773 |
|
774 |
if (gpgme_data_new (&hd)) |
775 |
BUG (0); |
776 |
for (i=0; i < ulen; i++) { |
777 |
if (isalnum (url[i]) || url[i] == '-') |
778 |
gpg_data_putc (hd, url[i]); |
779 |
else if (url[i] == ' ') |
780 |
gpg_data_putc (hd, '+'); |
781 |
else { |
782 |
sprintf (numbuf, "%%%02X", url[i]); |
783 |
gpgme_data_write (hd, numbuf, strlen (numbuf)); |
784 |
} |
785 |
} |
786 |
|
787 |
p = data_release_and_get_mem (hd, &nlen); |
788 |
if (ret_nlen) |
789 |
*ret_nlen = nlen; |
790 |
return p; |
791 |
} |
792 |
|
793 |
|
794 |
/* Format a request for the given keyid (send). */ |
795 |
static char* |
796 |
kserver_send_request (const char *hostname, WORD port, |
797 |
const char *pubkey, int octets) |
798 |
{ |
799 |
char *request = NULL; |
800 |
char *enc_pubkey = NULL; |
801 |
size_t enc_octets; |
802 |
int reqlen; |
803 |
|
804 |
log_debug ("kserver_send_request: %s:%d\r\n", hostname, port); |
805 |
|
806 |
if (!port) |
807 |
port = HKP_PORT; |
808 |
reqlen = 512 + strlen (hostname) + 2*strlen (pubkey); |
809 |
if (proxy.proto == PROXY_PROTO_HTTP && proxy.base64_user) |
810 |
reqlen += strlen (proxy.base64_user) + 1; |
811 |
request = new char[reqlen]; |
812 |
if (!request) |
813 |
BUG (0); |
814 |
enc_pubkey = URL_encode (pubkey, octets, &enc_octets); |
815 |
if (!enc_pubkey || !enc_octets) { |
816 |
free_if_alloc (request); |
817 |
return NULL; |
818 |
} |
819 |
|
820 |
if (proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
821 |
_snprintf (request, reqlen-1, |
822 |
"POST http://%s:%d/pks/add HTTP/1.0\r\n" |
823 |
"Referer: \r\n" |
824 |
"User-Agent: WinPT/W32\r\n" |
825 |
"Host: %s:%d\r\n" |
826 |
"Proxy-Authorization: Basic %s\r\n" |
827 |
"Content-type: application/x-www-form-urlencoded\r\n" |
828 |
"Content-length: %d\r\n" |
829 |
"\r\n" |
830 |
"keytext=%s" |
831 |
"\r\n", |
832 |
skip_type_prefix (hostname), port, hostname, port, |
833 |
proxy.base64_user, enc_octets+9, enc_pubkey); |
834 |
} |
835 |
else { |
836 |
_snprintf (request, reqlen-1, |
837 |
"POST /pks/add HTTP/1.0\r\n" |
838 |
"Referer: \r\n" |
839 |
"User-Agent: WinPT/W32\r\n" |
840 |
"Host: %s:%d\r\n" |
841 |
"Content-type: application/x-www-form-urlencoded\r\n" |
842 |
"Content-length: %d\r\n" |
843 |
"\r\n" |
844 |
"keytext=%s" |
845 |
"\r\n", |
846 |
skip_type_prefix (hostname), port, |
847 |
enc_octets+9, enc_pubkey); |
848 |
} |
849 |
free_if_alloc (enc_pubkey); |
850 |
|
851 |
log_debug ("%s\r\n", request); |
852 |
return request; |
853 |
} |
854 |
|
855 |
|
856 |
/* Interface for receiving a public key. */ |
857 |
int |
858 |
kserver_recvkey (const char *hostname, WORD port, const char *keyid, |
859 |
char **r_key, int *r_keylen) |
860 |
{ |
861 |
char *request = NULL; |
862 |
int conn_fd; |
863 |
int rc; |
864 |
|
865 |
if (!port) |
866 |
port = HKP_PORT; |
867 |
hkp_err = 0; /* reset */ |
868 |
rc = kserver_connect (hostname, port, &conn_fd); |
869 |
if (rc) |
870 |
goto leave; |
871 |
|
872 |
request = new char[300+1]; |
873 |
if (!request) |
874 |
BUG (0); |
875 |
|
876 |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
877 |
_snprintf (request, 300, |
878 |
"GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n" |
879 |
"Proxy-Authorization: Basic %s\r\n\r\n", |
880 |
skip_type_prefix (hostname), port, keyid, proxy.base64_user); |
881 |
} |
882 |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
883 |
_snprintf (request, 300, |
884 |
"GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n", |
885 |
skip_type_prefix (hostname), port, keyid); |
886 |
} |
887 |
else { |
888 |
_snprintf (request, 300, |
889 |
"GET /pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n", keyid); |
890 |
} |
891 |
|
892 |
log_debug ("%s\r\n", request); |
893 |
|
894 |
rc = sock_write (conn_fd, request, strlen (request)); |
895 |
if (rc == SOCKET_ERROR) { |
896 |
rc = WPTERR_WINSOCK_RECVKEY; |
897 |
goto leave; |
898 |
} |
899 |
|
900 |
rc = sock_read_ext (conn_fd, r_key, r_keylen); |
901 |
if (rc == SOCKET_ERROR) { |
902 |
rc = WPTERR_WINSOCK_RECVKEY; |
903 |
goto leave; |
904 |
} |
905 |
|
906 |
log_debug ("%s\r\n", *r_key); |
907 |
rc = check_hkp_response (*r_key, 1); |
908 |
if (rc) |
909 |
goto leave; |
910 |
|
911 |
WSASetLastError (0); |
912 |
|
913 |
leave: |
914 |
closesocket (conn_fd); |
915 |
free_if_alloc (request); |
916 |
return rc; |
917 |
} |
918 |
|
919 |
|
920 |
/* Interface to send a public key. */ |
921 |
int |
922 |
kserver_sendkey (const char *hostname, WORD port, const char *pubkey, int len ) |
923 |
{ |
924 |
char *request = NULL; |
925 |
char log[2048] = {0}; |
926 |
int conn_fd, n; |
927 |
int rc; |
928 |
|
929 |
hkp_err = 0; /* reset */ |
930 |
rc = kserver_connect (hostname, port, &conn_fd); |
931 |
if (rc) |
932 |
goto leave; |
933 |
|
934 |
request = kserver_send_request (hostname, port, pubkey, len); |
935 |
if (request == NULL) { |
936 |
rc = WPTERR_GENERAL; |
937 |
goto leave; |
938 |
} |
939 |
|
940 |
rc = sock_write (conn_fd, request, strlen (request)); |
941 |
if (rc == SOCKET_ERROR) { |
942 |
rc = WPTERR_WINSOCK_SENDKEY; |
943 |
goto leave; |
944 |
} |
945 |
|
946 |
rc = sock_read (conn_fd, log, sizeof (log)-1, &n); |
947 |
if (rc == SOCKET_ERROR) { |
948 |
rc = WPTERR_WINSOCK_SENDKEY; |
949 |
goto leave; |
950 |
} |
951 |
|
952 |
log_debug ("kserver_sendkey: read %d bytes\r\n%s\r\n", n, log); |
953 |
rc = check_hkp_response (log, 0); |
954 |
if (rc) |
955 |
goto leave; |
956 |
|
957 |
WSASetLastError (0); |
958 |
|
959 |
leave: |
960 |
closesocket (conn_fd); |
961 |
free_if_alloc (request); |
962 |
return rc; |
963 |
} |
964 |
|
965 |
|
966 |
/* Check keyserver response. */ |
967 |
static int |
968 |
kserver_search_chkresp (int fd) |
969 |
{ |
970 |
char buf[128]; |
971 |
int n=0; |
972 |
|
973 |
/* parse response 'HTTP/1.0 500 OK' */ |
974 |
if (sock_getline (fd, buf, 127, &n)) |
975 |
return WPTERR_KEYSERVER_NOTFOUND; |
976 |
|
977 |
log_debug ("kserver_search_chkpresp: %s\r\n", buf); |
978 |
if (strncmp (buf, "HTTP/1.", 7)) |
979 |
return WPTERR_KEYSERVER_NOTFOUND; |
980 |
if (strncmp (buf+(8+1), "200", 3)) |
981 |
return WPTERR_KEYSERVER_NOTFOUND; |
982 |
return 0; |
983 |
} |
984 |
|
985 |
|
986 |
/* End the keyserver search procedure. */ |
987 |
void |
988 |
kserver_search_end (int conn_fd) |
989 |
{ |
990 |
log_debug ("kserver_search_end: fd=%d\r\n", conn_fd); |
991 |
closesocket (conn_fd); |
992 |
} |
993 |
|
994 |
|
995 |
/* Begin keyserver search procedure. */ |
996 |
int |
997 |
kserver_search_begin (const char *hostname, WORD port, |
998 |
const char *pattern, int *conn_fd) |
999 |
{ |
1000 |
char *request = NULL; |
1001 |
char *enc_patt = NULL; |
1002 |
int n; |
1003 |
int rc, sock_fd; |
1004 |
|
1005 |
rc = kserver_connect (hostname, port, &sock_fd); |
1006 |
if (rc) { |
1007 |
*conn_fd = 0; |
1008 |
goto leave; |
1009 |
} |
1010 |
|
1011 |
enc_patt = URL_encode (pattern, strlen (pattern), NULL); |
1012 |
n = 140 + strlen (enc_patt) + strlen (hostname) + 32 + 2; |
1013 |
if (proxy.base64_user) |
1014 |
n += strlen (proxy.base64_user) + 1; |
1015 |
request = new char[n+1]; |
1016 |
if (!request) |
1017 |
BUG (0); |
1018 |
|
1019 |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
1020 |
_snprintf (request, n, |
1021 |
"GET http://%s:%d/pks/lookup?op=index&search=%s HTTP/1.0\r\n" |
1022 |
"Proxy-Authorization: Basic %s\r\n\r\n", |
1023 |
skip_type_prefix (hostname), port, enc_patt, proxy.base64_user); |
1024 |
} |
1025 |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
1026 |
_snprintf (request, n, |
1027 |
"GET http://%s:%d/pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n", |
1028 |
skip_type_prefix (hostname), port, enc_patt); |
1029 |
} |
1030 |
else { |
1031 |
_snprintf (request, n, |
1032 |
"GET /pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n", |
1033 |
enc_patt); |
1034 |
} |
1035 |
|
1036 |
log_debug ("kserver_search_begin:\r\n%s\r\n", request); |
1037 |
|
1038 |
if (sock_write (sock_fd, request, strlen (request)) == SOCKET_ERROR) { |
1039 |
rc = WPTERR_GENERAL; |
1040 |
goto leave; |
1041 |
} |
1042 |
|
1043 |
rc = kserver_search_chkresp (sock_fd); |
1044 |
if (rc) { |
1045 |
closesocket (sock_fd); |
1046 |
sock_fd = 0; |
1047 |
} |
1048 |
|
1049 |
*conn_fd = sock_fd; |
1050 |
|
1051 |
leave: |
1052 |
free_if_alloc (request); |
1053 |
free_if_alloc (enc_patt); |
1054 |
return rc; |
1055 |
} |
1056 |
|
1057 |
|
1058 |
|
1059 |
|
1060 |
/* Convert an iso date @iso_date (YYYY-MM-DD) into the locale |
1061 |
representation and store it into @loc_date. |
1062 |
Return value: 0 on success. */ |
1063 |
static int |
1064 |
parse_iso_date (const char *iso_date, char *loc_date, size_t loclen) |
1065 |
{ |
1066 |
SYSTEMTIME st; |
1067 |
char buf[16] = {0}, *p; |
1068 |
int pos=0; |
1069 |
|
1070 |
strncpy (buf, iso_date, sizeof (buf)-1); |
1071 |
p = strtok (buf, "-"); |
1072 |
while (p != NULL) { |
1073 |
switch (pos) { |
1074 |
case 0: st.wYear = (WORD)atoi (p); pos++; break; |
1075 |
case 1: st.wMonth = (WORD)atoi (p); pos++; break; |
1076 |
case 2: st.wDay = (WORD)atoi (p); pos++; break; |
1077 |
default: break; |
1078 |
} |
1079 |
p = strtok (NULL, "-"); |
1080 |
} |
1081 |
if (pos != 3) |
1082 |
return -1; |
1083 |
|
1084 |
if (!GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, |
1085 |
NULL, loc_date, loclen)) |
1086 |
return -1; |
1087 |
return 0; |
1088 |
} |
1089 |
|
1090 |
|
1091 |
int |
1092 |
kserver_search_next (int fd, keyserver_key *key) |
1093 |
{ |
1094 |
char buf[1024], *p; |
1095 |
int uidlen, nbytes, pos = 0; |
1096 |
|
1097 |
log_debug ("keyserver_search_next:\r\n"); |
1098 |
|
1099 |
if (sock_getline (fd, buf, sizeof (buf) - 1, &nbytes)) |
1100 |
return WPTERR_GENERAL; |
1101 |
|
1102 |
/* XXX: use maschine readable option. */ |
1103 |
log_debug ("%s\r\n", buf); |
1104 |
|
1105 |
if (!strncmp (buf, "pub", 3)) { |
1106 |
int revoked = strstr (buf, "KEY REVOKED") != NULL? 1 : 0; |
1107 |
key->bits = atol (buf+3); |
1108 |
p = strchr (buf, '>'); |
1109 |
if (!p) |
1110 |
goto fail; |
1111 |
pos = p - buf + 1; |
1112 |
memcpy (key->keyid, buf + pos, 8); |
1113 |
key->keyid[8] = '\0'; |
1114 |
p = strstr (buf, "</a>"); |
1115 |
if (!p) |
1116 |
goto fail; |
1117 |
pos = p - buf + 5; |
1118 |
memcpy (key->date, buf + pos, 10); |
1119 |
key->date[10] = '\0'; |
1120 |
parse_iso_date (key->date, key->date, sizeof (key->date)-1); |
1121 |
if (revoked) { |
1122 |
strcpy (key->uid, "KEY REVOKED: not checked"); |
1123 |
return 0; |
1124 |
} |
1125 |
pos += 10; |
1126 |
p = buf + pos + 1; |
1127 |
while (p && *p != '>') |
1128 |
p++; |
1129 |
p++; |
1130 |
uidlen = strlen (p) - 10; |
1131 |
if (!strstr (p, "<") && !strstr (p, ">")) { |
1132 |
pos=0; |
1133 |
while (p && *p && pos < sizeof (key->uid)-1) { |
1134 |
if (*p == '<') |
1135 |
break; |
1136 |
key->uid[pos++] = *p++; |
1137 |
} |
1138 |
key->uid[pos] ='\0'; |
1139 |
return 0; |
1140 |
} |
1141 |
|
1142 |
if (uidlen > sizeof (key->uid)-1) |
1143 |
uidlen = sizeof (key->uid)-1; |
1144 |
memcpy (key->uid, p, uidlen); |
1145 |
key->uid[uidlen] = '\0'; |
1146 |
strcat (key->uid, ">"); |
1147 |
p = strchr (key->uid, '&'); |
1148 |
if (p) { |
1149 |
key->uid[(p-key->uid)] = '<'; |
1150 |
memmove (key->uid+(p-key->uid)+1, key->uid+(p-key->uid)+4, |
1151 |
strlen (key->uid)-3); |
1152 |
} |
1153 |
return 0; |
1154 |
} |
1155 |
|
1156 |
fail: |
1157 |
key->bits = 0; |
1158 |
memset (key, 0, sizeof *key); |
1159 |
return 0; |
1160 |
} |
1161 |
|
1162 |
|
1163 |
/* Release mbmers in the proxy context @ctx. */ |
1164 |
void |
1165 |
kserver_proxy_release (keyserver_proxy_t ctx) |
1166 |
{ |
1167 |
free_if_alloc (ctx->host); |
1168 |
free_if_alloc (ctx->pass); |
1169 |
free_if_alloc (ctx->user); |
1170 |
ctx->port = ctx->proto = 0; |
1171 |
} |
1172 |
|
1173 |
|
1174 |
/* Spawn a process given by @cmdl. */ |
1175 |
static int |
1176 |
spawn_application (char *cmdl) |
1177 |
{ |
1178 |
STARTUPINFO si; |
1179 |
PROCESS_INFORMATION pi; |
1180 |
int rc = 0; |
1181 |
|
1182 |
memset (&si, 0, sizeof (si)); |
1183 |
si.cb = sizeof (si); |
1184 |
si.dwFlags = STARTF_USESHOWWINDOW; |
1185 |
si.wShowWindow = SW_HIDE; |
1186 |
memset (&pi, 0, sizeof (pi)); |
1187 |
|
1188 |
if (!CreateProcess (NULL, cmdl, NULL, NULL, FALSE, 0, |
1189 |
NULL, NULL, &si, &pi)) { |
1190 |
log_box ("Keyserver Plugin", MB_ERR, "Could not spawn helper process"); |
1191 |
rc = -1; |
1192 |
} |
1193 |
|
1194 |
CloseHandle (pi.hThread); |
1195 |
WaitForSingleObject (pi.hProcess, INFINITE); |
1196 |
CloseHandle (pi.hProcess); |
1197 |
return rc; |
1198 |
} |
1199 |
|
1200 |
|
1201 |
static FILE* |
1202 |
do_spawn_ldap_helper (const char *host, const char *keyid) |
1203 |
{ |
1204 |
FILE *fp = NULL; |
1205 |
char *p, *sep; |
1206 |
char *ksprg; |
1207 |
char outf[256], inf[256]; |
1208 |
size_t n; |
1209 |
|
1210 |
p = get_gnupg_prog (); |
1211 |
n = strlen (p) + 1 + 128; |
1212 |
ksprg = new char[n+1]; |
1213 |
if (!ksprg) |
1214 |
BUG (0); |
1215 |
sep = strrchr (p, '\\'); |
1216 |
if (sep != NULL) |
1217 |
p[(sep-p)] = 0; |
1218 |
|
1219 |
_snprintf (ksprg, n, "%s\\gpgkeys_ldap.exe", p); |
1220 |
free_if_alloc (p); |
1221 |
if (file_exist_check (ksprg)) { |
1222 |
log_box ("LDAP Keyserver Plugin", MB_ERR, |
1223 |
"%s: could not find LDAP keyserver module!", ksprg); |
1224 |
fp = NULL; |
1225 |
goto leave; |
1226 |
} |
1227 |
get_temp_name (outf, sizeof (outf)-1, keyid); |
1228 |
get_temp_name (inf, sizeof (inf)-1, NULL); |
1229 |
fp = fopen (inf, "w+b"); |
1230 |
if (!fp) { |
1231 |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", inf, |
1232 |
winpt_strerror (WPTERR_FILE_OPEN)); |
1233 |
goto leave; |
1234 |
} |
1235 |
fprintf (fp, |
1236 |
"VERSION 1\n" |
1237 |
"PROGRAM 1.4.3-cvs\n" |
1238 |
"SCHEME ldap\n" |
1239 |
"HOST %s\n" |
1240 |
"COMMAND GET\n" |
1241 |
"\n" |
1242 |
"%s\n", |
1243 |
host? skip_type_prefix (host): "64.94.85.200", keyid); |
1244 |
fclose (fp); |
1245 |
|
1246 |
p = new char[strlen (ksprg) + strlen (inf) + strlen (outf) + 32]; |
1247 |
if (!p) |
1248 |
BUG (NULL); |
1249 |
sprintf (p, "%s -o %s %s", ksprg, outf, inf); |
1250 |
if (spawn_application (p)) { |
1251 |
fp = NULL; |
1252 |
goto leave; |
1253 |
} |
1254 |
fp = fopen (outf, "rb"); |
1255 |
if (!fp) |
1256 |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf, |
1257 |
winpt_strerror (WPTERR_FILE_OPEN)); |
1258 |
|
1259 |
leave: |
1260 |
DeleteFile (inf); |
1261 |
DeleteFile (outf); |
1262 |
free_if_alloc (p); |
1263 |
free_if_alloc (ksprg); |
1264 |
return fp; |
1265 |
} |
1266 |
|
1267 |
/* Receive an key via LDAP from host @host with the keyid @keyid. |
1268 |
@key contains the key on success. */ |
1269 |
int |
1270 |
ldap_recvkey (const char *host, const char *keyid, |
1271 |
char **r_key, int *r_keylen) |
1272 |
{ |
1273 |
gpgme_data_t raw; |
1274 |
FILE *fp; |
1275 |
const char *s; |
1276 |
char buf[512]; |
1277 |
int start_key = 0, failed = 0; |
1278 |
int rc = 0; |
1279 |
|
1280 |
fp = do_spawn_ldap_helper (host, keyid); |
1281 |
if (!fp) |
1282 |
return WPTERR_GENERAL; |
1283 |
|
1284 |
if (gpgme_data_new (&raw)) |
1285 |
BUG (0); |
1286 |
while (!feof (fp)) { |
1287 |
s = fgets (buf, sizeof (buf)-1, fp); |
1288 |
if (!s) |
1289 |
break; |
1290 |
if (strstr (s, "KEY") && strstr (s, "FAILED")) { |
1291 |
failed = 1; |
1292 |
break; |
1293 |
} |
1294 |
if (!start_key && strstr (s, "KEY") && strstr (s, "BEGIN")) { |
1295 |
start_key = 1; |
1296 |
continue; |
1297 |
} |
1298 |
if (!start_key) |
1299 |
continue; |
1300 |
gpgme_data_write (raw, buf, strlen (buf)); |
1301 |
if (strstr (s, "KEY") && strstr (s, "END")) |
1302 |
break; |
1303 |
} |
1304 |
fclose (fp); |
1305 |
|
1306 |
if (failed) |
1307 |
rc = WPTERR_WINSOCK_RECVKEY; |
1308 |
*r_key = data_release_and_get_mem (raw, r_keylen); |
1309 |
return rc; |
1310 |
} |
1311 |
|
1312 |
|
1313 |
/* Receive an key via FINGER from host @host with the user @user. |
1314 |
On success @key contains the key. */ |
1315 |
int |
1316 |
finger_recvkey (const char *host, const char *user, |
1317 |
char **r_key, int *r_keylen) |
1318 |
{ |
1319 |
gpgme_data_t raw; |
1320 |
char buf[256]; |
1321 |
int fd, nread; |
1322 |
int start_key = 0; |
1323 |
int rc=0; |
1324 |
|
1325 |
rc = kserver_connect (host, FINGER_PORT, &fd); |
1326 |
if (rc) |
1327 |
return rc; |
1328 |
|
1329 |
sock_write (fd, user, strlen (user)); |
1330 |
sock_write (fd, "\r\n", 2); |
1331 |
|
1332 |
if (gpgme_data_new (&raw)) |
1333 |
BUG (0); |
1334 |
|
1335 |
for (;;) { |
1336 |
if (sock_getline (fd, buf, sizeof (buf), &nread)) |
1337 |
break; |
1338 |
strcat (buf, "\n"); |
1339 |
if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) { |
1340 |
gpgme_data_write (raw, buf, nread); |
1341 |
start_key = 1; |
1342 |
} |
1343 |
else if (strstr (buf, "END PGP PUBLIC KEY BLOCK" ) && start_key) { |
1344 |
gpgme_data_write (raw, buf, nread); |
1345 |
start_key--; |
1346 |
break; |
1347 |
} |
1348 |
else if (start_key) |
1349 |
gpgme_data_write (raw, buf, nread); |
1350 |
} |
1351 |
|
1352 |
closesocket (fd); |
1353 |
if (start_key != 0) |
1354 |
rc = WPTERR_WINSOCK_RECVKEY; |
1355 |
*r_key = data_release_and_get_mem (raw, r_keylen); |
1356 |
return rc; |
1357 |
} |
1358 |
|
1359 |
|
1360 |
/* Check if the given name @name is a valid hostname. */ |
1361 |
int |
1362 |
check_IP_or_hostname (const char *name) |
1363 |
{ |
1364 |
const char *not_allowed = "=!ยง$%&@#*~\\/}][{<>|,;:'"; |
1365 |
size_t i, j; |
1366 |
|
1367 |
for (i=0; i < strlen (name); i++) { |
1368 |
for (j =0; j < strlen (not_allowed); j++) { |
1369 |
if (name[i] == not_allowed[j]) |
1370 |
return -1; |
1371 |
} |
1372 |
} |
1373 |
return 0; |
1374 |
} |