1 |
/* wptKeyserver.cpp - W32 Keyserver Access |
/* wptKeyserver.cpp - W32 Keyserver Access |
2 |
* Copyright (C) 2000-2006 Timo Schulz |
* Copyright (C) 2000-2007 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. |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 |
* General Public License for more details. |
* General Public License for more details. |
|
* |
|
|
* You should have received a copy of the GNU General Public License |
|
|
* along with WinPT; if not, write to the Free Software Foundation, |
|
|
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
16 |
*/ |
*/ |
|
|
|
17 |
#ifdef HAVE_CONFIG_H |
#ifdef HAVE_CONFIG_H |
18 |
#include <config.h> |
#include <config.h> |
19 |
#endif |
#endif |
24 |
#include <sys/stat.h> |
#include <sys/stat.h> |
25 |
#include <ctype.h> |
#include <ctype.h> |
26 |
|
|
27 |
|
#include "wptGPG.h" |
28 |
#include "wptKeyserver.h" |
#include "wptKeyserver.h" |
29 |
#include "wptErrors.h" |
#include "wptErrors.h" |
30 |
#include "wptTypes.h" |
#include "wptTypes.h" |
31 |
#include "wptNLS.h" |
#include "wptNLS.h" |
32 |
#include "wptW32API.h" |
#include "wptW32API.h" |
|
#include "wptGPG.h" |
|
33 |
#include "wptRegistry.h" |
#include "wptRegistry.h" |
34 |
|
#include "wptUTF8.h" |
35 |
|
#include "wptVersion.h" |
36 |
|
#include "StringBuffer.h" |
37 |
|
|
38 |
|
|
39 |
/* Map net_errno to a winsock error. */ |
/* Map net_errno to a winsock error. */ |
40 |
#define net_errno ((int)WSAGetLastError ()) |
#define net_errno ((int)WSAGetLastError ()) |
43 |
keyserver server[MAX_KEYSERVERS] = {0}; |
keyserver server[MAX_KEYSERVERS] = {0}; |
44 |
keyserver_proxy_s proxy = {0}; |
keyserver_proxy_s proxy = {0}; |
45 |
static const char *server_list[] = { |
static const char *server_list[] = { |
|
"hkp://gnv.us.ks.cryptnet.net", |
|
|
"hkp://keyserver.kjsl.com", |
|
46 |
"hkp://sks.keyserver.penguin.de", |
"hkp://sks.keyserver.penguin.de", |
47 |
"hkp://subkeys.pgp.net", |
"hkp://subkeys.pgp.net", |
48 |
"ldap://keyserver.pgp.com", |
"ldap://keyserver.pgp.com", |
57 |
char *default_keyserver = NULL; |
char *default_keyserver = NULL; |
58 |
WORD default_keyserver_port = 0; |
WORD default_keyserver_port = 0; |
59 |
|
|
60 |
/* Default socket timeout. */ |
/* Default socket timeout (secs). */ |
61 |
static int default_socket_timeout = 6; |
static size_t default_socket_timeout = 6; |
62 |
|
|
63 |
|
|
64 |
/* Wrapper for safe memory allocation. */ |
/* Wrapper for safe memory allocation. */ |
81 |
char base64code[] = |
char base64code[] = |
82 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
83 |
int index = 0, temp = 0, res = 0; |
int index = 0, temp = 0, res = 0; |
84 |
int i = 0, inputlen = 0, len = 0; |
int i, inputlen, len = 0; |
85 |
|
|
86 |
inputlen = strlen (inbuf); |
inputlen = strlen (inbuf); |
87 |
for (i = 0; i < inputlen; i++) { |
for (i = 0; i < inputlen; i++) { |
119 |
} |
} |
120 |
|
|
121 |
|
|
122 |
|
/* Check that the given buffer contains a valid keyserver URL |
123 |
|
and return the prefix length, 0 in case of an error. */ |
124 |
|
static int |
125 |
|
check_URL (const char *buf) |
126 |
|
{ |
127 |
|
const char *proto[] = { |
128 |
|
"ldap://", |
129 |
|
"http://", |
130 |
|
"finger://", |
131 |
|
"hkp://", |
132 |
|
NULL}; |
133 |
|
int i; |
134 |
|
|
135 |
|
if (strlen (buf) < 7) |
136 |
|
return 0; |
137 |
|
|
138 |
|
for (i=0; proto[i] != NULL; i++) { |
139 |
|
if (strstr (buf, proto[i])) |
140 |
|
return strlen (proto[i]); |
141 |
|
} |
142 |
|
return 0; |
143 |
|
} |
144 |
|
|
145 |
|
|
146 |
/* Skip the URL schema and return only the host part of it. */ |
/* Skip the URL schema and return only the host part of it. */ |
147 |
static const char* |
static const char* |
148 |
skip_type_prefix (const char *hostname) |
skip_type_prefix (const char *hostname) |
149 |
{ |
{ |
150 |
if (hostname && !strncmp (hostname, "http://", 7)) |
int pos; |
151 |
hostname += 7; |
|
152 |
else if (hostname && !strncmp (hostname, "hkp://", 6)) |
if (!hostname) |
153 |
hostname += 6; |
return hostname; |
154 |
else if (hostname && !strncmp (hostname, "finger://", 9)) |
|
155 |
hostname += 9; |
pos = check_URL (hostname); |
156 |
else if (hostname && !strncmp (hostname, "ldap://", 7)) |
if (pos > 0) |
157 |
hostname += 7; |
hostname += pos; |
158 |
return hostname; |
return hostname; |
159 |
} |
} |
160 |
|
|
161 |
|
/* Parse the keyserver response and extract the human |
162 |
|
readable text passages. */ |
163 |
|
static int |
164 |
|
parse_keyserver_error (const char *resp, char *txt, size_t txtlen) |
165 |
|
{ |
166 |
|
char *p, *p2; |
167 |
|
|
168 |
|
/* If we find no 'Error' substring, we assume a success. */ |
169 |
|
if (!stristr (resp, "Error")) |
170 |
|
return -1; |
171 |
|
|
172 |
|
memset (txt, 0, txtlen); |
173 |
|
p = strstr (resp, "\r\n\r\n"); |
174 |
|
if (!p) |
175 |
|
return -1; |
176 |
|
resp += (p-resp); |
177 |
|
p = strstr (resp, "<h1>"); |
178 |
|
if (!p) |
179 |
|
return -1; |
180 |
|
resp += (p-resp)+strlen ("<h1>"); |
181 |
|
p = strstr (resp, "</h1>"); |
182 |
|
if (!p) |
183 |
|
return -1; |
184 |
|
resp += (p-resp)+strlen ("</h1>"); |
185 |
|
p2 = strstr (resp, "</body>"); |
186 |
|
if (p2 != NULL) |
187 |
|
memcpy (txt, resp, (p2-resp)); |
188 |
|
else { |
189 |
|
if (!strnicmp (resp, "<p>", 3)) |
190 |
|
resp += 3; |
191 |
|
while (resp && *resp == '\r' || *resp == '\n') |
192 |
|
resp++; |
193 |
|
memcpy (txt, resp, strlen (resp)); |
194 |
|
} |
195 |
|
return 0; |
196 |
|
} |
197 |
|
|
198 |
|
|
199 |
/* Check that the keyserver response indicates an OK. |
/* Check that the keyserver response indicates an OK. |
200 |
Return 0 on success. */ |
Return 0 on success. */ |
201 |
static int |
static int |
202 |
check_hkp_response (const char *resp, int recv) |
check_hkp_response (const char *resp, int recv) |
203 |
{ |
{ |
204 |
char *p, *end; |
int ec; |
|
int ec, len; |
|
205 |
|
|
206 |
|
log_debug ("check_hkp_response: '%s'\r\n", resp); |
207 |
ec = recv ? WPTERR_WINSOCK_RECVKEY : WPTERR_WINSOCK_SENDKEY; |
ec = recv ? WPTERR_WINSOCK_RECVKEY : WPTERR_WINSOCK_SENDKEY; |
208 |
if (!strstr (resp, "HTTP/1.0 200 OK") && |
if (!strstr (resp, "HTTP/1.0 200 OK") && |
209 |
!strstr (resp, "HTTP/1.1 200 OK")) /* http error */ |
!strstr (resp, "HTTP/1.1 200 OK") && |
210 |
return ec; |
!strstr (resp, "HTTP/1.0 500 OK") && |
211 |
if (strstr (resp, "Public Key Server -- Error") |
!strstr (resp, "HTTP/1.1 500 OK")) |
212 |
|| strstr (resp, "Public Key Server -- Error") |
return ec; /* http error */ |
213 |
|| strstr (resp, "No matching keys in database")) { |
|
214 |
p = strstr (resp, "<p>"); |
if (!parse_keyserver_error (resp, hkp_errmsg, DIM (hkp_errmsg)-2)) { |
215 |
if (p && strlen (p) < sizeof (hkp_errmsg)) { |
if (!strlen (hkp_errmsg)) |
216 |
end = strstr (p, "</p>"); |
_snprintf (hkp_errmsg, DIM (hkp_errmsg)-1, |
217 |
len = end? (end - p + 1) : strlen (p); |
"Unknown keyserver error"); |
218 |
memset (hkp_errmsg, 0, sizeof (hkp_errmsg)); |
hkp_err = 1; |
|
strncpy (hkp_errmsg, p, len); |
|
|
hkp_err = 1; |
|
|
} |
|
219 |
return ec; |
return ec; |
220 |
} |
} |
221 |
return 0; |
return 0; |
275 |
returned in @nbytes. |
returned in @nbytes. |
276 |
Return value: 0 on success. */ |
Return value: 0 on success. */ |
277 |
static int |
static int |
278 |
sock_read (int fd, char *buf, int buflen, int *nbytes) |
sock_read (int fd, char *buf, size_t buflen, int *nbytes) |
279 |
{ |
{ |
280 |
DWORD nread; |
int nread; |
281 |
int nleft = buflen; |
size_t nleft = buflen; |
282 |
int rc, n = 0; |
size_t n = 0; |
283 |
|
int rc; |
284 |
|
|
285 |
if (nbytes) |
if (nbytes) |
286 |
*nbytes = 0; |
*nbytes = 0; |
314 |
/* Helper to create a string from the gpgme data and |
/* Helper to create a string from the gpgme data and |
315 |
then release the gpgme data object. */ |
then release the gpgme data object. */ |
316 |
char* |
char* |
317 |
data_release_and_get_mem (gpgme_data_t in, int *r_bufferlen) |
data_release_and_get_mem (gpgme_data_t in, size_t *r_bufferlen) |
318 |
{ |
{ |
319 |
size_t n; |
size_t n; |
320 |
char *buffer, *p; |
char *buffer, *p; |
323 |
p = gpgme_data_release_and_get_mem (in, &n); |
p = gpgme_data_release_and_get_mem (in, &n); |
324 |
buffer = m_strdup (p); |
buffer = m_strdup (p); |
325 |
if (r_bufferlen) |
if (r_bufferlen) |
326 |
*r_bufferlen = (int)n; |
*r_bufferlen = n; |
327 |
gpgme_free (p); |
gpgme_free (p); |
328 |
return buffer; |
return buffer; |
329 |
} |
} |
333 |
return the data in @buffer. Caller must free data. |
return the data in @buffer. Caller must free data. |
334 |
Return value: 0 on success. */ |
Return value: 0 on success. */ |
335 |
int |
int |
336 |
sock_read_ext (int fd, char **buffer, int *r_bufferlen) |
sock_read_ext (int fd, char **buffer, size_t *r_bufferlen) |
337 |
{ |
{ |
338 |
gpgme_data_t dh; |
gpgme_data_t dh; |
339 |
char buf[1024]; |
char buf[1024]; |
340 |
size_t n=0; |
size_t timeout=0; |
341 |
int nread, rc; |
int nread, rc; |
342 |
|
|
343 |
if (gpgme_data_new (&dh)) |
if (gpgme_data_new (&dh)) |
344 |
BUG (0); |
return SOCKET_ERROR; |
345 |
while (n < 10) { |
while (timeout < default_socket_timeout) { |
346 |
rc = sock_select (fd, 1); |
rc = sock_select (fd, 1); |
347 |
if (rc == SOCKET_ERROR) { |
if (rc == SOCKET_ERROR) { |
348 |
gpgme_data_release (dh); |
gpgme_data_release (dh); |
349 |
return rc; |
return rc; |
350 |
} |
} |
351 |
else if (!rc) |
else if (!rc) |
352 |
n++; |
timeout++; /* socket not ready yet. */ |
353 |
else { |
else { |
354 |
nread = recv (fd, buf, sizeof (buf), 0); |
nread = recv (fd, buf, DIM (buf), 0); |
355 |
if (nread == SOCKET_ERROR) |
if (nread == SOCKET_ERROR) |
356 |
return SOCKET_ERROR; |
return SOCKET_ERROR; |
357 |
else if (!nread) |
else if (!nread) |
359 |
gpgme_data_write (dh, buf, nread); |
gpgme_data_write (dh, buf, nread); |
360 |
} |
} |
361 |
} |
} |
362 |
|
if (timeout >= default_socket_timeout) |
363 |
|
return WPTERR_WINSOCK_TIMEOUT; |
364 |
*buffer = data_release_and_get_mem (dh, r_bufferlen); |
*buffer = data_release_and_get_mem (dh, r_bufferlen); |
365 |
return 0; |
return 0; |
366 |
} |
} |
368 |
|
|
369 |
/* Write the buffer @buf with the length @buflen to a socket @fd. */ |
/* Write the buffer @buf with the length @buflen to a socket @fd. */ |
370 |
static int |
static int |
371 |
sock_write (int fd, const char *buf, int buflen) |
sock_write (int fd, const char *buf, size_t buflen) |
372 |
{ |
{ |
373 |
DWORD nwritten; |
int nwritten; |
374 |
int nleft = buflen; |
int nleft = buflen; |
375 |
|
|
376 |
while (nleft > 0) { |
while (nleft > 0) { |
408 |
char *p; |
char *p; |
409 |
int i; |
int i; |
410 |
|
|
411 |
|
#ifdef WINPT_MOBILE |
412 |
|
p = m_strdup ("keyserver.conf"); |
413 |
|
#else |
414 |
p = make_special_filename (CSIDL_APPDATA, "winpt\\keyserver.conf", NULL); |
p = make_special_filename (CSIDL_APPDATA, "winpt\\keyserver.conf", NULL); |
415 |
|
#endif |
416 |
kserver_save_conf (p); |
kserver_save_conf (p); |
417 |
free_if_alloc (p); |
free_if_alloc (p); |
418 |
free_if_alloc (default_keyserver); |
free_if_alloc (default_keyserver); |
419 |
for (i=0; i < MAX_KEYSERVERS; i++) { |
for (i=0; i < MAX_KEYSERVERS; i++) { |
420 |
if (server[i].used && server[i].name != NULL) |
if (server[i].name != NULL) |
421 |
free_if_alloc (server[i].name); |
free_if_alloc (server[i].name); |
422 |
} |
} |
423 |
kserver_proxy_release (&proxy); |
kserver_proxy_release (&proxy); |
424 |
WSACleanup (); |
WSACleanup (); |
425 |
|
log_debug ("wsock_end: cleanup finished.\r\n"); |
426 |
} |
} |
427 |
|
|
428 |
|
|
468 |
|
|
469 |
/* Set default socket timeout for all reading operations. */ |
/* Set default socket timeout for all reading operations. */ |
470 |
void |
void |
471 |
kserver_set_socket_timeout (int nsec) |
kserver_set_socket_timeout (size_t nsec) |
472 |
{ |
{ |
473 |
if (nsec < 0) |
if (nsec < 0 || nsec > 3600) |
474 |
nsec = 0; |
nsec = 0; |
475 |
default_socket_timeout = nsec; |
default_socket_timeout = nsec; |
476 |
} |
} |
495 |
*port = default_keyserver_port; |
*port = default_keyserver_port; |
496 |
return default_keyserver; |
return default_keyserver; |
497 |
} |
} |
498 |
else if (!type && idx < DIM (server_list)) { |
else if (!type && (size_t)idx < DIM (server_list)) { |
499 |
|
/* XXX: handle non HKP servers. */ |
500 |
*port = HKP_PORT; |
*port = HKP_PORT; |
501 |
return server_list[idx]; |
return server_list[idx]; |
502 |
} |
} |
515 |
closesocket (fd); |
closesocket (fd); |
516 |
return 0; |
return 0; |
517 |
} |
} |
518 |
|
log_debug ("kserver_check_inet_connection: no inet connection.\r\n"); |
519 |
return -1; |
return -1; |
520 |
} |
} |
521 |
|
|
527 |
const char* |
const char* |
528 |
kserver_check_keyid (const char *keyid) |
kserver_check_keyid (const char *keyid) |
529 |
{ |
{ |
530 |
static char id[21]; |
static char id[22]; |
531 |
|
|
532 |
if (strstr (keyid, "@")) |
if (strstr (keyid, "@")) |
533 |
return keyid; /* email address */ |
return keyid; /* email address */ |
534 |
if (strncmp (keyid, "0x", 2)) { |
if (strncmp (keyid, "0x", 2)) { |
535 |
memset (&id, 0, sizeof (id)); |
memset (&id, 0, sizeof (id)); |
536 |
_snprintf (id, sizeof (id)-1, "0x%s", keyid); |
_snprintf (id, DIM (id)-1, "0x%s", keyid); |
537 |
return id; |
return id; |
538 |
} |
} |
539 |
return keyid; |
return keyid; |
550 |
n = 4*strlen (proxy_user) / 3 + 32 + strlen (proxy_pass) + 2; |
n = 4*strlen (proxy_user) / 3 + 32 + strlen (proxy_pass) + 2; |
551 |
free_if_alloc (proxy.base64_user); |
free_if_alloc (proxy.base64_user); |
552 |
proxy.base64_user = safe_alloc (n+1); |
proxy.base64_user = safe_alloc (n+1); |
553 |
_snprintf (t, sizeof (t)-1, "%s:%s", proxy_user, proxy_pass); |
_snprintf (t, DIM (t)-1, "%s:%s", proxy_user, proxy_pass); |
554 |
base64_encode (t, proxy.base64_user); |
base64_encode (t, proxy.base64_user); |
555 |
free_if_alloc (proxy.user); |
free_if_alloc (proxy.user); |
556 |
free_if_alloc (proxy.pass); |
free_if_alloc (proxy.pass); |
557 |
proxy.user = m_strdup (proxy_user); |
proxy.user = m_strdup (proxy_user); |
558 |
proxy.pass = m_strdup (proxy_pass); |
proxy.pass = m_strdup (proxy_pass); |
559 |
} |
} |
|
|
|
|
|
|
|
/* Check that the given buffer contains a valid keyserver URL. */ |
|
|
static int |
|
|
check_URL (const char * buf) |
|
|
{ |
|
|
if (strlen (buf) < 7) |
|
|
return -1; |
|
|
if (!strstr (buf, "ldap://") |
|
|
&& !strstr (buf, "http://") |
|
|
&& !strstr (buf, "finger://") |
|
|
&& !strstr (buf, "hkp://")) |
|
|
return -1; |
|
|
return 0; |
|
|
} |
|
560 |
|
|
561 |
|
|
562 |
/* Get the port number from the given protocol. */ |
/* Get the port number from the given protocol. */ |
564 |
port_from_proto (int proto) |
port_from_proto (int proto) |
565 |
{ |
{ |
566 |
switch (proto) { |
switch (proto) { |
567 |
case KSPROTO_LDAP: return 0; |
case KSPROTO_LDAP: return LDAP_PORT; |
568 |
case KSPROTO_FINGER: return FINGER_PORT; |
case KSPROTO_FINGER: return FINGER_PORT; |
569 |
case KSPROTO_HTTP: return HKP_PORT; |
case KSPROTO_HTTP: return HKP_PORT; |
570 |
} |
} |
578 |
{ |
{ |
579 |
if (strstr (buf, "ldap")) |
if (strstr (buf, "ldap")) |
580 |
return KSPROTO_LDAP; |
return KSPROTO_LDAP; |
581 |
else if (strstr( buf, "finger")) |
else if (strstr (buf, "finger")) |
582 |
return KSPROTO_FINGER; |
return KSPROTO_FINGER; |
583 |
return KSPROTO_HKP; |
return KSPROTO_HKP; |
584 |
} |
} |
585 |
|
|
586 |
|
|
587 |
|
/* Set the default keyserver. |
588 |
|
If @hostname is NULL, use the predefined keyserver. */ |
589 |
void |
void |
590 |
keyserver_set_default (const char *hostname, WORD port) |
keyserver_set_default (const char *hostname, WORD port) |
591 |
{ |
{ |
592 |
|
if (!port) |
593 |
|
port = HKP_PORT; |
594 |
if (hostname != NULL) { |
if (hostname != NULL) { |
595 |
free_if_alloc (default_keyserver); |
free_if_alloc (default_keyserver); |
596 |
default_keyserver = m_strdup (hostname); |
default_keyserver = m_strdup (hostname); |
597 |
default_keyserver_port = port; |
default_keyserver_port = port; |
598 |
} |
} |
599 |
if (!port) |
if (!default_keyserver) { |
|
port = HKP_PORT; |
|
|
if (!default_keyserver) |
|
600 |
default_keyserver = m_strdup (DEF_HKP_KEYSERVER); |
default_keyserver = m_strdup (DEF_HKP_KEYSERVER); |
601 |
|
default_keyserver_port = HKP_PORT; |
602 |
|
} |
603 |
server[0].name = m_strdup (default_keyserver); |
server[0].name = m_strdup (default_keyserver); |
604 |
server[0].used = 1; |
server[0].used = 1; |
605 |
server[0].port = port; |
server[0].port = port; |
651 |
} |
} |
652 |
|
|
653 |
|
|
654 |
|
/* Return the specified keyserver config setting @key as an integer. */ |
655 |
|
static int |
656 |
|
get_conf_kserver_int (const char *key) |
657 |
|
{ |
658 |
|
char *p; |
659 |
|
int val = 0; |
660 |
|
|
661 |
|
p = get_reg_entry_keyserver (key); |
662 |
|
if (p && *p) |
663 |
|
val = atoi (p); |
664 |
|
free_if_alloc (p); |
665 |
|
return val; |
666 |
|
} |
667 |
|
|
668 |
|
|
669 |
|
/* Read the proxy configuration and store it into @prox. */ |
670 |
|
static void |
671 |
|
read_proxy_config (keyserver_proxy_t prox) |
672 |
|
{ |
673 |
|
char *proto; |
674 |
|
|
675 |
|
if (!prox) |
676 |
|
return; |
677 |
|
|
678 |
|
proto = get_reg_entry_keyserver("Proto"); |
679 |
|
if (proto != NULL && strlen (proto) > 0) |
680 |
|
prox->proto = atoi (proto); |
681 |
|
else |
682 |
|
prox->proto = PROXY_PROTO_NONE; |
683 |
|
free_if_alloc (proto); |
684 |
|
free_if_alloc (prox->host); |
685 |
|
prox->host = get_reg_entry_keyserver ("Host"); |
686 |
|
free_if_alloc (prox->user); |
687 |
|
prox->user = get_reg_entry_keyserver ("User"); |
688 |
|
free_if_alloc (prox->pass); |
689 |
|
prox->pass = get_reg_entry_keyserver ("Pass"); |
690 |
|
prox->port = get_conf_kserver_int ("Port"); |
691 |
|
} |
692 |
|
|
693 |
|
|
694 |
/* Load the keyserver config file @conf. */ |
/* Load the keyserver config file @conf. */ |
695 |
int |
int |
696 |
kserver_load_conf (const char *conf) |
kserver_load_conf (const char *conf) |
698 |
FILE *fp; |
FILE *fp; |
699 |
char buf[1024], *s, *p; |
char buf[1024], *s, *p; |
700 |
char *user = NULL, *pass = NULL; |
char *user = NULL, *pass = NULL; |
701 |
int no_config=0, chk_pos=0; |
int no_config=0, chk_pos; |
702 |
int pos; |
int pos; |
703 |
|
|
704 |
for (pos = 0; pos < MAX_KEYSERVERS; pos++) { |
for (pos = 0; pos < MAX_KEYSERVERS; pos++) { |
705 |
server[pos].used = 0; |
server[pos].used = 0; |
706 |
server[pos].port = HKP_PORT; |
server[pos].port = HKP_PORT; |
707 |
|
server[pos].name = NULL; |
708 |
} |
} |
709 |
|
|
710 |
fp = fopen (conf, "rb"); |
fp = fopen (conf, "rb"); |
714 |
server[pos].name = m_strdup (server_list[pos]); |
server[pos].name = m_strdup (server_list[pos]); |
715 |
server[pos].proto = proto_from_URL (server_list[pos]); |
server[pos].proto = proto_from_URL (server_list[pos]); |
716 |
} |
} |
717 |
no_config=1; |
no_config = 1; |
718 |
} |
} |
719 |
get_reg_proxy_prefs (&proxy); |
read_proxy_config (&proxy); |
720 |
if (user && pass) |
if (user && pass) |
721 |
update_proxy_user (user, pass); |
update_proxy_user (user, pass); |
722 |
else if (user && !pass || !user && pass) { |
else if (user && !pass || !user && pass) { |
723 |
msg_box (NULL, _("Invalid proxy configuration." |
msg_box (NULL, _("Invalid proxy configuration. " |
724 |
"You need to set a user and a password" |
"You need to set a user and a password " |
725 |
"to use proxy authentication!"), |
"to use proxy authentication!"), |
726 |
_("Proxy Error"), MB_ERR); |
_("Proxy Error"), MB_ERR); |
727 |
} |
} |
736 |
|
|
737 |
pos = 0; |
pos = 0; |
738 |
while (fp && !feof (fp)) { |
while (fp && !feof (fp)) { |
739 |
s = fgets (buf, sizeof (buf)-1, fp); |
s = fgets (buf, DIM (buf)-1, fp); |
740 |
if (!s) |
if (!s) |
741 |
break; |
break; |
742 |
if (strstr (buf, "\r\n")) |
if (strstr (buf, "\r\n")) |
746 |
s = (char *)skip_whitespace (buf); |
s = (char *)skip_whitespace (buf); |
747 |
if (*s == '#' || strlen (s) < 7) |
if (*s == '#' || strlen (s) < 7) |
748 |
continue; |
continue; |
749 |
if (check_URL (s)) { |
chk_pos = check_URL (s); |
750 |
|
if (!chk_pos) { |
751 |
msg_box (NULL, _("All entries of this file must have a valid prefix.\n" |
msg_box (NULL, _("All entries of this file must have a valid prefix.\n" |
752 |
"Currently HKP/HTTP, LDAP and FINGER are supported.\n"), |
"Currently HKP/HTTP, LDAP and FINGER are supported.\n"), |
753 |
_("Keyserver Error"), MB_ERR); |
_("Keyserver Error"), MB_ERR); |
754 |
continue; |
continue; |
755 |
} |
} |
|
chk_pos = 6; |
|
|
if (strstr (s, "finger")) |
|
|
chk_pos = 10; |
|
756 |
p = strchr (s+chk_pos, ':'); |
p = strchr (s+chk_pos, ':'); |
757 |
server[pos].used = 1; |
server[pos].used = 1; |
758 |
server[pos].proto = proto_from_URL (s); |
server[pos].proto = proto_from_URL (s); |
777 |
if (fp) |
if (fp) |
778 |
fclose (fp); |
fclose (fp); |
779 |
if (!pos) { |
if (!pos) { |
780 |
/* only issue an error if the config file exist but |
/* Only issue an error if the config file exist but |
781 |
it has no valid entries. */ |
it has no valid entries. */ |
782 |
keyserver_set_default (NULL, HKP_PORT); |
keyserver_set_default (NULL, HKP_PORT); |
783 |
if (!no_config) |
if (!no_config) |
797 |
struct sockaddr_in sock; |
struct sockaddr_in sock; |
798 |
char host[128] = {0}; |
char host[128] = {0}; |
799 |
DWORD iaddr; |
DWORD iaddr; |
800 |
|
bool use_proxy = proxy.host != NULL; |
801 |
int rc, fd; |
int rc, fd; |
802 |
|
|
|
log_debug ("kserver_connect: %s:%d\r\n", hostname, port); |
|
|
|
|
803 |
if (!port) |
if (!port) |
804 |
port = HKP_PORT; |
port = HKP_PORT; |
805 |
if (conn_fd) |
if (conn_fd) |
806 |
*conn_fd = 0; |
*conn_fd = 0; |
807 |
hostname = skip_type_prefix (hostname); |
hostname = skip_type_prefix (hostname); |
808 |
|
log_debug ("kserver_connect: %s:%d\r\n", hostname, port); |
809 |
|
|
810 |
if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) |
if (use_proxy && proxy.proto == PROXY_PROTO_HTTP) |
811 |
port = proxy.port; |
port = proxy.port; |
812 |
memset (&sock, 0, sizeof (sock)); |
memset (&sock, 0, sizeof (sock)); |
813 |
sock.sin_family = AF_INET; |
sock.sin_family = AF_INET; |
814 |
sock.sin_port = htons (port); |
sock.sin_port = htons (port); |
815 |
if (proxy.host) |
strncpy (host, use_proxy? proxy.host : hostname, DIM (host)-1); |
|
strncpy (host, proxy.host, 127); |
|
|
else |
|
|
strncpy (host, hostname, 127); |
|
816 |
|
|
817 |
if ((iaddr = inet_addr (host)) != INADDR_NONE) |
if ((iaddr = inet_addr (host)) != INADDR_NONE) |
818 |
memcpy (&sock.sin_addr, &iaddr, sizeof (iaddr)); |
memcpy (&sock.sin_addr, &iaddr, sizeof (iaddr)); |
825 |
} |
} |
826 |
else { |
else { |
827 |
log_debug ("gethostbyname: failed ec=%d.\r\n", net_errno); |
log_debug ("gethostbyname: failed ec=%d.\r\n", net_errno); |
828 |
return WPTERR_WINSOCK_RESOLVE; |
return use_proxy? WPTERR_WINSOCK_PROXY : WPTERR_WINSOCK_RESOLVE; |
829 |
} |
} |
830 |
fd = socket (AF_INET, SOCK_STREAM, 0); |
fd = socket (AF_INET, SOCK_STREAM, 0); |
831 |
if (fd == INVALID_SOCKET) |
if (fd == INVALID_SOCKET) |
834 |
if (rc == SOCKET_ERROR) { |
if (rc == SOCKET_ERROR) { |
835 |
log_debug ("connect: failed ec=%d.\r\n", net_errno); |
log_debug ("connect: failed ec=%d.\r\n", net_errno); |
836 |
closesocket (fd); |
closesocket (fd); |
837 |
return WPTERR_WINSOCK_CONNECT; |
return use_proxy? WPTERR_WINSOCK_PROXY : WPTERR_WINSOCK_CONNECT; |
838 |
} |
} |
839 |
|
|
840 |
if (proxy.proto == PROXY_PROTO_SOCKS5) { |
if (proxy.proto == PROXY_PROTO_SOCKS5) { |
841 |
|
/* XXX: finish code and provide errors constants. */ |
842 |
rc = socks_handshake (&proxy, fd, hostname, port); |
rc = socks_handshake (&proxy, fd, hostname, port); |
843 |
if (rc) { |
if (rc) { |
844 |
closesocket (fd); |
closesocket (fd); |
845 |
return WPTERR_GENERAL; /* XXX: use better code. */ |
return WPTERR_GENERAL; |
846 |
} |
} |
847 |
} |
} |
848 |
|
|
853 |
} |
} |
854 |
|
|
855 |
|
|
856 |
|
/* Check if the URL needs to be encoded or not. */ |
857 |
static bool |
static bool |
858 |
URL_must_encoded (const char *url) |
URL_must_encoded (const char *url) |
859 |
{ |
{ |
869 |
{ |
{ |
870 |
gpgme_data_t hd; |
gpgme_data_t hd; |
871 |
char numbuf[5], *p; |
char numbuf[5], *p; |
872 |
size_t i; |
size_t i, nlen; |
|
int nlen; |
|
873 |
|
|
874 |
if (gpgme_data_new (&hd)) |
if (gpgme_data_new (&hd)) |
875 |
BUG (0); |
BUG (0); |
944 |
} |
} |
945 |
|
|
946 |
free_if_alloc (enc_pubkey); |
free_if_alloc (enc_pubkey); |
947 |
log_debug ("%s\r\n", request); |
log_debug ("request:\r\n%s\r\n", request); |
948 |
return request; |
return request; |
949 |
} |
} |
950 |
|
|
952 |
/* Interface for receiving a public key. */ |
/* Interface for receiving a public key. */ |
953 |
int |
int |
954 |
kserver_recvkey (const char *hostname, WORD port, const char *keyid, |
kserver_recvkey (const char *hostname, WORD port, const char *keyid, |
955 |
char **r_key, int *r_keylen) |
char **r_key, size_t *r_keylen) |
956 |
{ |
{ |
957 |
const char *fmt; |
StringBuffer req; |
958 |
char *request = NULL; |
const char *reqbuf; |
959 |
int conn_fd; |
int conn_fd; |
960 |
int rc, reqlen; |
int rc; |
961 |
|
|
962 |
if (!port) |
if (!port) |
963 |
port = HKP_PORT; |
port = HKP_PORT; |
966 |
if (rc) |
if (rc) |
967 |
goto leave; |
goto leave; |
968 |
|
|
|
reqlen = strlen (hostname)+32+strlen (keyid)+1; |
|
969 |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
970 |
fmt = "GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n" |
req = req + "GET http://" + skip_type_prefix (hostname) + |
971 |
"Proxy-Authorization: Basic %s\r\n\r\n"; |
":" + port + |
972 |
reqlen =+ strlen (fmt) + strlen (proxy.base64_user)+1; |
"/pks/lookup?op=get&search=" + keyid + " HTTP/1.0\r\n" + |
973 |
request = safe_alloc (reqlen+1); |
"Proxy-Authorization: Basic " + proxy.base64_user + "\r\n\r\n"; |
|
_snprintf (request, reqlen, fmt, skip_type_prefix (hostname), port, |
|
|
keyid, proxy.base64_user); |
|
974 |
} |
} |
975 |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
976 |
fmt = "GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n"; |
req = req + "GET http://" + skip_type_prefix (hostname) + |
977 |
reqlen += strlen (fmt)+1; |
":" + port + "/pks/lookup?op=get&search=" + keyid + |
978 |
request = safe_alloc (reqlen+1); |
" HTTP/1.0\r\n\r\n"; |
|
_snprintf (request, reqlen, fmt, skip_type_prefix (hostname), |
|
|
port, keyid); |
|
|
} |
|
|
else { |
|
|
fmt = "GET /pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n"; |
|
|
reqlen += strlen (fmt)+1; |
|
|
request = safe_alloc (reqlen+1); |
|
|
_snprintf (request, reqlen, fmt, keyid); |
|
979 |
} |
} |
980 |
|
else |
981 |
log_debug ("%s\r\n", request); |
req = req + "GET /pks/lookup?op=get&search=" + keyid + |
982 |
|
" HTTP/1.0\r\n\r\n"; |
983 |
|
|
984 |
rc = sock_write (conn_fd, request, strlen (request)); |
log_debug ("req:\r\n%s\r\n", req.getBuffer ()); |
985 |
|
|
986 |
|
reqbuf = req.getBuffer (); |
987 |
|
rc = sock_write (conn_fd, reqbuf, strlen (reqbuf)); |
988 |
if (rc == SOCKET_ERROR) { |
if (rc == SOCKET_ERROR) { |
989 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
990 |
goto leave; |
goto leave; |
996 |
goto leave; |
goto leave; |
997 |
} |
} |
998 |
|
|
999 |
log_debug ("%s\r\n", *r_key); |
log_debug ("response:\r\n%s\r\n", *r_key); |
1000 |
rc = check_hkp_response (*r_key, 1); |
rc = check_hkp_response (*r_key, 1); |
1001 |
if (rc) |
if (rc) |
1002 |
goto leave; |
goto leave; |
1005 |
|
|
1006 |
leave: |
leave: |
1007 |
closesocket (conn_fd); |
closesocket (conn_fd); |
|
free_if_alloc (request); |
|
1008 |
return rc; |
return rc; |
1009 |
} |
} |
1010 |
|
|
1011 |
|
|
1012 |
/* Interface to send a public key. */ |
/* Interface to send a public key. */ |
1013 |
int |
int |
1014 |
kserver_sendkey (const char *hostname, WORD port, const char *pubkey, int len ) |
kserver_sendkey (const char *hostname, WORD port, |
1015 |
|
const char *pubkey, size_t len) |
1016 |
{ |
{ |
1017 |
char *request = NULL; |
char *request = NULL; |
1018 |
char log[2048] = {0}; |
char log[2048] = {0}; |
1025 |
goto leave; |
goto leave; |
1026 |
|
|
1027 |
request = kserver_send_request (hostname, port, pubkey, len); |
request = kserver_send_request (hostname, port, pubkey, len); |
|
if (request == NULL) { |
|
|
rc = WPTERR_GENERAL; |
|
|
goto leave; |
|
|
} |
|
|
|
|
1028 |
rc = sock_write (conn_fd, request, strlen (request)); |
rc = sock_write (conn_fd, request, strlen (request)); |
1029 |
if (rc == SOCKET_ERROR) { |
if (rc == SOCKET_ERROR) { |
1030 |
rc = WPTERR_WINSOCK_SENDKEY; |
rc = WPTERR_WINSOCK_SENDKEY; |
1031 |
goto leave; |
goto leave; |
1032 |
} |
} |
1033 |
|
|
1034 |
rc = sock_read (conn_fd, log, sizeof (log)-1, &n); |
rc = sock_read (conn_fd, log, DIM (log)-1, &n); |
1035 |
if (rc == SOCKET_ERROR) { |
if (rc == SOCKET_ERROR) { |
1036 |
rc = WPTERR_WINSOCK_SENDKEY; |
rc = WPTERR_WINSOCK_SENDKEY; |
1037 |
goto leave; |
goto leave; |
1043 |
goto leave; |
goto leave; |
1044 |
|
|
1045 |
WSASetLastError (0); |
WSASetLastError (0); |
1046 |
|
|
1047 |
leave: |
leave: |
1048 |
closesocket (conn_fd); |
closesocket (conn_fd); |
1049 |
free_if_alloc (request); |
free_if_alloc (request); |
1059 |
int n=0; |
int n=0; |
1060 |
|
|
1061 |
/* parse response 'HTTP/1.0 500 OK' */ |
/* parse response 'HTTP/1.0 500 OK' */ |
1062 |
if (sock_getline (fd, buf, sizeof (buf)-1, &n)) |
if (sock_getline (fd, buf, DIM (buf)-1, &n)) |
1063 |
return WPTERR_KEYSERVER_NOTFOUND; |
return WPTERR_KEYSERVER_NOTFOUND; |
1064 |
|
|
1065 |
log_debug ("kserver_search_chkpresp: %s\r\n", buf); |
log_debug ("kserver_search_chkpresp: %s\r\n", buf); |
1081 |
|
|
1082 |
|
|
1083 |
/* Extract the amount of keys from the info record. */ |
/* Extract the amount of keys from the info record. */ |
1084 |
static int |
static size_t |
1085 |
count_keys_in_response (char *buf) |
count_keys_in_response (char *buf) |
1086 |
{ |
{ |
1087 |
char *p; |
char *p; |
1088 |
int recno = 0; |
int recno = 0; |
1089 |
int n = 0; |
size_t n = 0; |
1090 |
|
|
1091 |
/* info:1:4 */ |
/* info:1:4 */ |
1092 |
if (strncmp (buf, "info", 4)) |
if (strncmp (buf, "info", 4)) |
1109 |
the amount of keys which were found. */ |
the amount of keys which were found. */ |
1110 |
int |
int |
1111 |
kserver_search_begin (const char *hostname, WORD port, |
kserver_search_begin (const char *hostname, WORD port, |
1112 |
const char *pattern, int *conn_fd, int *nkeys) |
const char *pattern, int *conn_fd, size_t *nkeys) |
1113 |
{ |
{ |
1114 |
const char *fmt; |
|
1115 |
char *request = NULL; |
StringBuffer req; |
1116 |
|
const char *reqbuf; |
1117 |
char *enc_patt = NULL; |
char *enc_patt = NULL; |
1118 |
char status[128]; |
char status[128]; |
1119 |
int rc, sock_fd; |
int rc, sock_fd; |
1120 |
int reqlen; |
int nread; |
1121 |
|
|
1122 |
*conn_fd = 0; |
*conn_fd = 0; |
1123 |
|
|
1126 |
goto leave; |
goto leave; |
1127 |
|
|
1128 |
enc_patt = URL_encode (pattern, strlen (pattern), NULL); |
enc_patt = URL_encode (pattern, strlen (pattern), NULL); |
|
reqlen = strlen (enc_patt) + strlen (hostname) + 32 + 1; |
|
|
|
|
1129 |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
1130 |
fmt = "GET http://%s:%d/pks/lookup?options=mr&op=index&search=%s HTTP/1.0\r\n" |
req = req + "GET http://" + skip_type_prefix (hostname) + ":" + port + |
1131 |
"Proxy-Authorization: Basic %s\r\n\r\n"; |
"/pks/lookup?options=mr&op=index&search=" + enc_patt + |
1132 |
reqlen += strlen (proxy.base64_user) + strlen (fmt) + 1; |
" HTTP/1.0\r\n" |
1133 |
request = safe_alloc (reqlen+1); |
"Proxy-Authorization: Basic " + proxy.base64_user + "\r\n\r\n"; |
|
_snprintf (request, reqlen, fmt, skip_type_prefix (hostname), port, |
|
|
enc_patt, proxy.base64_user); |
|
1134 |
} |
} |
1135 |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
1136 |
fmt = "GET http://%s:%d/pks/lookup?options=mr&op=index&search=%s HTTP/1.0\r\n\r\n"; |
req = req + "GET http://" + skip_type_prefix (hostname) + ":" + port + |
1137 |
reqlen += strlen (fmt)+1; |
"/pks/lookup?options=mr&op=index&search=" + enc_patt + |
1138 |
request = safe_alloc (reqlen+1); |
" HTTP/1.0\r\n\r\n"; |
|
_snprintf (request, reqlen, skip_type_prefix (hostname), port, |
|
|
enc_patt); |
|
1139 |
} |
} |
1140 |
else { |
else { |
1141 |
fmt = "GET /pks/lookup?options=mr&op=index&search=%s HTTP/1.0\r\n\r\n"; |
req = req + "GET /pks/lookup?options=mr&op=index&search=" + |
1142 |
reqlen += strlen (fmt)+1; |
enc_patt + " HTTP/1.0\r\n\r\n"; |
|
request = safe_alloc (reqlen+1); |
|
|
_snprintf (request, reqlen, fmt, enc_patt); |
|
1143 |
} |
} |
1144 |
|
|
1145 |
log_debug ("kserver_search_begin:\r\n%s\r\n", request); |
log_debug ("kserver_search_begin:\r\n%s\r\n", req.getBuffer ()); |
1146 |
if (sock_write (sock_fd, request, strlen (request)) == SOCKET_ERROR) { |
reqbuf = req.getBuffer (); |
1147 |
|
if (sock_write (sock_fd, reqbuf, strlen (reqbuf)) == SOCKET_ERROR) { |
1148 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1149 |
goto leave; |
goto leave; |
1150 |
} |
} |
1158 |
|
|
1159 |
/* Skip all lines until we reach the "info:" record. */ |
/* Skip all lines until we reach the "info:" record. */ |
1160 |
for (;;) { |
for (;;) { |
1161 |
if (sock_getline (sock_fd, status, sizeof (status)-1, &reqlen)) { |
if (sock_getline (sock_fd, status, DIM (status)-1, &nread)) { |
1162 |
log_debug ("kserver_search_begin: retrieving status line failed.\r\n"); |
log_debug ("kserver_search_begin: retrieving status line failed.\r\n"); |
1163 |
closesocket (sock_fd); |
closesocket (sock_fd); |
1164 |
sock_fd = 0; |
sock_fd = 0; |
1174 |
*conn_fd = sock_fd; |
*conn_fd = sock_fd; |
1175 |
|
|
1176 |
leave: |
leave: |
|
free_if_alloc (request); |
|
1177 |
free_if_alloc (enc_patt); |
free_if_alloc (enc_patt); |
1178 |
return rc; |
return rc; |
1179 |
} |
} |
1186 |
enum pub_rec_t {ID=1, KEYID, ALGO, SIZE, CREATE, EXPIRE}; |
enum pub_rec_t {ID=1, KEYID, ALGO, SIZE, CREATE, EXPIRE}; |
1187 |
char *p; |
char *p; |
1188 |
int recno = 0; |
int recno = 0; |
1189 |
|
int off = 0; |
1190 |
|
|
1191 |
/* pub:BF3DF9B4:17:1024:925411133:: */ |
/* pub:{BF3DF9B4, ED4681C9BF3DF9B4, FPR}:17:1024:925411133:: */ |
1192 |
p = strtok (buf, ":"); |
p = strtok (buf, ":"); |
1193 |
while (p != NULL) { |
while (p != NULL) { |
1194 |
recno++; |
recno++; |
1196 |
case ID: |
case ID: |
1197 |
break; |
break; |
1198 |
|
|
1199 |
case KEYID: |
case KEYID: |
1200 |
|
/* If for any reason the returned record actually contains |
1201 |
|
a fingerprint instead of a key ID, we truncate the fpr. */ |
1202 |
|
off = strlen (p) == 40? 32 : 0; |
1203 |
free_if_alloc (key->keyid); |
free_if_alloc (key->keyid); |
1204 |
key->keyid = m_strdup (p); |
key->keyid = m_strdup (p+off); |
1205 |
break; |
break; |
1206 |
|
|
1207 |
case ALGO: |
case ALGO: |
1222 |
} |
} |
1223 |
p = strtok (NULL, ":"); |
p = strtok (NULL, ":"); |
1224 |
} |
} |
|
|
|
1225 |
} |
} |
1226 |
|
|
1227 |
|
|
1231 |
{ |
{ |
1232 |
enum uid_rec_t {ID=1, UID, CREATE, EXPIRE}; |
enum uid_rec_t {ID=1, UID, CREATE, EXPIRE}; |
1233 |
keyserver_uid_s *u, *n; |
keyserver_uid_s *u, *n; |
1234 |
char *p; |
char *p, *raw; |
1235 |
int recno = 0; |
int recno = 0; |
1236 |
|
|
1237 |
/* uid:Timo Schulz <[email protected]>:1138440360:: */ |
/* uid:Timo Schulz <[email protected]>:1138440360:: */ |
1247 |
break; |
break; |
1248 |
|
|
1249 |
case UID: |
case UID: |
1250 |
/* XXX: search for %3A and decode it back to ':' */ |
unhexify_buffer (p, &raw); |
1251 |
u->uid = m_strdup (p); |
u->uid = utf8_to_native (raw); |
1252 |
|
free_if_alloc (raw); |
1253 |
break; |
break; |
1254 |
|
|
1255 |
case CREATE: |
case CREATE: |
1305 |
key = new keyserver_key_s; |
key = new keyserver_key_s; |
1306 |
memset (key, 0, sizeof *key); |
memset (key, 0, sizeof *key); |
1307 |
for (;;) { |
for (;;) { |
1308 |
if (sock_getline (fd, buf, sizeof (buf)-1, &n)) |
if (sock_getline (fd, buf, DIM (buf)-1, &n)) |
1309 |
break; |
break; |
1310 |
|
/*log_debug ("record: '%s'\r\n", buf); */ |
1311 |
if (!strncmp (buf, "pub", 3)) |
if (!strncmp (buf, "pub", 3)) |
1312 |
parse_pub_record (key, buf); |
parse_pub_record (key, buf); |
1313 |
else |
else |
1390 |
do_spawn_ldap_helper (const char *host, const char *keyid) |
do_spawn_ldap_helper (const char *host, const char *keyid) |
1391 |
{ |
{ |
1392 |
FILE *fp = NULL; |
FILE *fp = NULL; |
1393 |
char *p, *sep; |
StringBuffer cmd; |
1394 |
char *ksprg; |
char *sep, *p, *ksprg; |
1395 |
char outf[256], inf[256]; |
char outf[256], inf[256]; |
1396 |
size_t n; |
size_t n; |
1397 |
|
|
1412 |
fp = NULL; |
fp = NULL; |
1413 |
goto leave; |
goto leave; |
1414 |
} |
} |
1415 |
get_temp_name (outf, sizeof (outf)-1, keyid); |
get_temp_name (outf, DIM (outf)-1, keyid); |
1416 |
get_temp_name (inf, sizeof (inf)-1, NULL); |
get_temp_name (inf, DIM (inf)-1, NULL); |
1417 |
fp = fopen (inf, "w+b"); |
fp = fopen (inf, "w+b"); |
1418 |
if (!fp) { |
if (!fp) { |
1419 |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", inf, |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", inf, |
1422 |
} |
} |
1423 |
fprintf (fp, |
fprintf (fp, |
1424 |
"VERSION 1\n" |
"VERSION 1\n" |
1425 |
"PROGRAM 1.4.3-cvs\n" |
"PROGRAM %d.%d.%d\n" |
1426 |
"SCHEME ldap\n" |
"SCHEME ldap\n" |
1427 |
"HOST %s\n" |
"HOST %s\n" |
1428 |
"COMMAND GET\n" |
"COMMAND GET\n" |
1429 |
"\n" |
"\n" |
1430 |
"%s\n", |
"%s\n", |
1431 |
|
gpgver[0], gpgver[1], gpgver[2], |
1432 |
host? skip_type_prefix (host): "64.94.85.200", keyid); |
host? skip_type_prefix (host): "64.94.85.200", keyid); |
1433 |
fclose (fp); |
fclose (fp); |
1434 |
|
|
1435 |
p = safe_alloc (strlen (ksprg) + strlen (inf) + strlen (outf) + 32); |
cmd = ksprg; |
1436 |
sprintf (p, "%s -o %s %s", ksprg, outf, inf); |
cmd = cmd + " -o " + outf + " " + inf; |
1437 |
if (spawn_application (p)) { |
if (spawn_application ((char*)cmd.getBuffer ())) { |
1438 |
fp = NULL; |
fp = NULL; |
1439 |
goto leave; |
goto leave; |
1440 |
} |
} |
1446 |
leave: |
leave: |
1447 |
DeleteFile (inf); |
DeleteFile (inf); |
1448 |
DeleteFile (outf); |
DeleteFile (outf); |
|
free_if_alloc (p); |
|
1449 |
free_if_alloc (ksprg); |
free_if_alloc (ksprg); |
1450 |
return fp; |
return fp; |
1451 |
} |
} |
1454 |
@key contains the key on success. */ |
@key contains the key on success. */ |
1455 |
int |
int |
1456 |
ldap_recvkey (const char *host, const char *keyid, |
ldap_recvkey (const char *host, const char *keyid, |
1457 |
char **r_key, int *r_keylen) |
char **r_key, size_t *r_keylen) |
1458 |
{ |
{ |
1459 |
gpgme_data_t raw; |
gpgme_data_t raw; |
1460 |
FILE *fp; |
FILE *fp; |
1470 |
if (gpgme_data_new (&raw)) |
if (gpgme_data_new (&raw)) |
1471 |
BUG (0); |
BUG (0); |
1472 |
while (!feof (fp)) { |
while (!feof (fp)) { |
1473 |
s = fgets (buf, sizeof (buf)-1, fp); |
s = fgets (buf, DIM (buf)-1, fp); |
1474 |
if (!s) |
if (!s) |
1475 |
break; |
break; |
1476 |
if (strstr (s, "KEY") && strstr (s, "FAILED")) { |
if (strstr (s, "KEY") && strstr (s, "FAILED")) { |
1500 |
On success @key contains the key. */ |
On success @key contains the key. */ |
1501 |
int |
int |
1502 |
finger_recvkey (const char *host, const char *user, |
finger_recvkey (const char *host, const char *user, |
1503 |
char **r_key, int *r_keylen) |
char **r_key, size_t *r_keylen) |
1504 |
{ |
{ |
1505 |
gpgme_data_t raw; |
gpgme_data_t raw; |
1506 |
char buf[256]; |
char buf[256+1]; |
1507 |
int fd, nread; |
int fd, nread; |
1508 |
int start_key = 0; |
int start_key = 0; |
1509 |
int rc=0; |
int rc=0; |
1516 |
sock_write (fd, "\r\n", 2); |
sock_write (fd, "\r\n", 2); |
1517 |
|
|
1518 |
if (gpgme_data_new (&raw)) |
if (gpgme_data_new (&raw)) |
1519 |
BUG (0); |
return WPTERR_WINSOCK_RECVKEY; |
1520 |
|
|
1521 |
for (;;) { |
for (;;) { |
1522 |
if (sock_getline (fd, buf, sizeof (buf), &nread)) |
if (sock_getline (fd, buf, DIM (buf)-1, &nread)) |
1523 |
break; |
break; |
1524 |
strcat (buf, "\n"); |
strcat (buf, "\n"); |
1525 |
if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) { |
if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) { |
1543 |
} |
} |
1544 |
|
|
1545 |
|
|
1546 |
/* Check if the given name @name is a valid hostname. */ |
/* Check if the given name @name is a valid inernet address |
1547 |
|
which means an dotted IP or a valid DNS name. |
1548 |
|
Return value: 0 on success. */ |
1549 |
int |
int |
1550 |
check_IP_or_hostname (const char *name) |
check_inet_address (const char *addr) |
1551 |
{ |
{ |
1552 |
const char *not_allowed = "=!�$%&@#*~\\/}][{<>|,;:'"; |
const char *not_allowed = "=!�$%&@#*~\\/}][{<>|,;:'"; |
1553 |
size_t i, j; |
size_t i; |
1554 |
|
|
1555 |
for (i=0; i < strlen (name); i++) { |
for (i=0; i < strlen (addr); i++) { |
1556 |
for (j =0; j < strlen (not_allowed); j++) { |
if (strchr (not_allowed, addr[i])) |
1557 |
if (name[i] == not_allowed[j]) |
return -1; |
|
return -1; |
|
|
} |
|
1558 |
} |
} |
1559 |
return 0; |
return 0; |
1560 |
} |
} |
1567 |
{ |
{ |
1568 |
char *p; |
char *p; |
1569 |
char *url = *r_keyserver; |
char *url = *r_keyserver; |
1570 |
|
int off = 0; |
1571 |
|
|
1572 |
/* no port is given so use the default port. */ |
/* no port is given so use the default port. */ |
1573 |
p = strrchr (url, ':'); |
p = strrchr (url, ':'); |
1578 |
*r_port = port; |
*r_port = port; |
1579 |
return 0; |
return 0; |
1580 |
} |
} |
1581 |
/* XXX: remove / in .de/:11371 */ |
|
1582 |
*r_keyserver = substr (url, 0, (p-url)); |
if (url[(p-url)-1] == '/') /* remove / in .de/:11371 */ |
1583 |
|
off = 1; |
1584 |
|
|
1585 |
|
*r_keyserver = substr (url, 0, (p-url)-off); |
1586 |
*r_port = atoi (url+(p-url)+1); |
*r_port = atoi (url+(p-url)+1); |
1587 |
safe_free (url); |
free_if_alloc (url); |
1588 |
return 0; |
return 0; |
1589 |
} |
} |