/[winpt]/trunk/Src/wptKeyserver.cpp
ViewVC logotype

Annotation of /trunk/Src/wptKeyserver.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 270 - (hide annotations)
Sat Oct 21 18:08:57 2006 UTC (18 years, 4 months ago) by twoaday
File size: 37122 byte(s)


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26