1 |
/* wptKeyserver.cpp - W32 Keyserver Access |
/* wptKeyserver.cpp - W32 Keyserver Access |
2 |
* Copyright (C) 2000-2006 Timo Schulz |
* Copyright (C) 2000-2009 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 |
33 |
#include "wptRegistry.h" |
#include "wptRegistry.h" |
34 |
#include "wptUTF8.h" |
#include "wptUTF8.h" |
35 |
#include "wptVersion.h" |
#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 ()) |
41 |
|
|
|
|
|
42 |
keyserver server[MAX_KEYSERVERS] = {0}; |
keyserver server[MAX_KEYSERVERS] = {0}; |
43 |
keyserver_proxy_s proxy = {0}; |
keyserver_proxy_s proxy = {0}; |
44 |
static const char *server_list[] = { |
static const char *server_list[] = { |
45 |
"hkp://sks.keyserver.penguin.de", |
"hkp://pool.sks-keyservers.net", |
46 |
"hkp://subkeys.pgp.net", |
"hkp://subkeys.pgp.net", |
47 |
"ldap://keyserver.pgp.com", |
"http://minsky.surfnet.nl", |
48 |
NULL |
NULL |
49 |
}; |
}; |
50 |
|
#define NUM_DEF_KEYSERVERS 3 |
51 |
|
|
52 |
|
static char hkp_err_msg[384]; /* Holds the error message from the server */ |
53 |
static char hkp_errmsg[1024]; /* Holds the error message from the server */ |
static int hkp_has_err = 0; /* != 0 indicates an error occurred. */ |
|
static int hkp_err = 0; /* != 0 indicates an error occurred. */ |
|
54 |
|
|
55 |
/* Default keyserver and port. */ |
/* Default keyserver and port. */ |
56 |
char *default_keyserver = NULL; |
char *default_keyserver = NULL; |
60 |
static size_t default_socket_timeout = 6; |
static size_t default_socket_timeout = 6; |
61 |
|
|
62 |
|
|
63 |
|
/* Remove %AB sequences from the input buffer @in |
64 |
|
and store the raw data in @out. */ |
65 |
|
void |
66 |
|
unhexify_buffer (const char *in, char **r_out) |
67 |
|
{ |
68 |
|
char temp[3], *out; |
69 |
|
size_t len, pos, i=0; |
70 |
|
|
71 |
|
len = strlen (in); |
72 |
|
out = new char[len+1]; |
73 |
|
if (!out) |
74 |
|
BUG (0); |
75 |
|
memset (out, 0, len+1); |
76 |
|
for (pos = 0; pos < len; pos++) { |
77 |
|
if (in[pos] == '%' && in[pos+1] == '%') |
78 |
|
out[i++] = '%'; |
79 |
|
else if (in[pos] == '%') { |
80 |
|
temp[0] = in[++pos]; |
81 |
|
temp[1] = in[++pos]; |
82 |
|
temp[2] = 0; |
83 |
|
out[i++] = (char)strtoul (temp, NULL, 16); |
84 |
|
} |
85 |
|
else |
86 |
|
out[i++] = in[pos]; |
87 |
|
} |
88 |
|
out[i] = 0; |
89 |
|
*r_out = out; |
90 |
|
} |
91 |
|
|
92 |
|
|
93 |
/* Wrapper for safe memory allocation. */ |
/* Wrapper for safe memory allocation. */ |
94 |
static char* |
static char* |
95 |
safe_alloc (DWORD n) |
safe_alloc (DWORD n) |
96 |
{ |
{ |
97 |
char *p; |
char *p = new char[n+1]; |
|
|
|
|
p = new char[n+1]; |
|
98 |
if (!p) |
if (!p) |
99 |
BUG (0); |
BUG (0); |
100 |
memset (p, 0, n); |
memset (p, 0, n); |
101 |
return p; |
return p; |
102 |
} |
} |
103 |
|
|
104 |
|
|
105 |
/* Basic64 encode the input @inbuf to @outbuf. */ |
/* Basic64 encode the input @inbuf to @outbuf. */ |
106 |
static void |
static void |
107 |
base64_encode (const char *inbuf, char *outbuf) |
base64_encode (const char *inbuf, char *outbuf) |
109 |
char base64code[] = |
char base64code[] = |
110 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
111 |
int index = 0, temp = 0, res = 0; |
int index = 0, temp = 0, res = 0; |
112 |
int i, inputlen, len = 0; |
int inputlen, len = 0; |
113 |
|
|
114 |
inputlen = strlen (inbuf); |
inputlen = strlen (inbuf); |
115 |
for (i = 0; i < inputlen; i++) { |
for (int i = 0; i < inputlen; i++) { |
116 |
res = temp; |
res = temp; |
117 |
res = (res << 8) | (inbuf[i] & 0xFF); |
res = (res << 8) | (inbuf[i] & 0xFF); |
118 |
switch (index++) { |
switch (index++) { |
122 |
break; |
break; |
123 |
case 1: |
case 1: |
124 |
outbuf[len++] = base64code[res >> 4 & 0x3F]; |
outbuf[len++] = base64code[res >> 4 & 0x3F]; |
125 |
res &= 0xF; |
res &= 0xF; |
126 |
break; |
break; |
127 |
case 2: |
case 2: |
128 |
outbuf[len++] = base64code[res >> 6 & 0x3F]; |
outbuf[len++] = base64code[res >> 6 & 0x3F]; |
152 |
static int |
static int |
153 |
check_URL (const char *buf) |
check_URL (const char *buf) |
154 |
{ |
{ |
155 |
const char *proto[] = { |
if (strstr (buf, "hkp://")) |
156 |
"ldap://", |
return 6; |
157 |
"http://", |
if (strstr (buf, "http://")) |
158 |
"finger://", |
return 7; |
|
"hkp://", |
|
|
NULL}; |
|
|
int i; |
|
|
|
|
|
if (strlen (buf) < 7) |
|
|
return 0; |
|
|
|
|
|
for (i=0; proto[i] != NULL; i++) { |
|
|
if (strstr (buf, proto[i])) |
|
|
return strlen (proto[i]); |
|
|
} |
|
159 |
return 0; |
return 0; |
160 |
} |
} |
161 |
|
|
175 |
return hostname; |
return hostname; |
176 |
} |
} |
177 |
|
|
178 |
|
|
179 |
/* Parse the keyserver response and extract the human |
/* Parse the keyserver response and extract the human |
180 |
readable text passages. */ |
readable text passages. */ |
181 |
static int |
static int |
183 |
{ |
{ |
184 |
char *p, *p2; |
char *p, *p2; |
185 |
|
|
186 |
/* If we find no 'Error' substring, we assume a success. */ |
/* If we find no 'Error' substring we assume a success. */ |
187 |
if (!stristr (resp, "Error")) |
if (!stristr (resp, "Error")) |
188 |
return -1; |
return -1; |
189 |
|
|
206 |
else { |
else { |
207 |
if (!strnicmp (resp, "<p>", 3)) |
if (!strnicmp (resp, "<p>", 3)) |
208 |
resp += 3; |
resp += 3; |
209 |
while (resp && *resp == '\r' || *resp == '\n') |
while (resp && (*resp == '\r' || *resp == '\n')) |
210 |
resp++; |
resp++; |
211 |
memcpy (txt, resp, strlen (resp)); |
memcpy (txt, resp, strlen (resp)); |
212 |
} |
} |
221 |
{ |
{ |
222 |
int ec; |
int ec; |
223 |
|
|
|
log_debug ("check_hkp_response: '%s'\r\n", resp); |
|
224 |
ec = recv ? WPTERR_WINSOCK_RECVKEY : WPTERR_WINSOCK_SENDKEY; |
ec = recv ? WPTERR_WINSOCK_RECVKEY : WPTERR_WINSOCK_SENDKEY; |
225 |
|
if (!resp) |
226 |
|
return ec; |
227 |
|
log_debug ("check_hkp_response: '%s'\r\n", resp); |
228 |
if (!strstr (resp, "HTTP/1.0 200 OK") && |
if (!strstr (resp, "HTTP/1.0 200 OK") && |
229 |
!strstr (resp, "HTTP/1.1 200 OK") && |
!strstr (resp, "HTTP/1.1 200 OK") && |
230 |
!strstr (resp, "HTTP/1.0 500 OK") && |
!strstr (resp, "HTTP/1.0 500 OK") && |
231 |
!strstr (resp, "HTTP/1.1 500 OK")) |
!strstr (resp, "HTTP/1.1 500 OK")) |
232 |
return ec; /* http error */ |
return ec; /* http error */ |
233 |
|
|
234 |
if (!parse_keyserver_error (resp, hkp_errmsg, DIM (hkp_errmsg)-2)) { |
if (!parse_keyserver_error (resp, hkp_err_msg, DIM (hkp_err_msg)-2)) { |
235 |
if (!strlen (hkp_errmsg)) |
if (!strlen (hkp_err_msg)) |
236 |
_snprintf (hkp_errmsg, DIM (hkp_errmsg)-1, |
_snprintf (hkp_err_msg, DIM (hkp_err_msg)-1, |
237 |
"Unknown keyserver error"); |
"Unknown keyserver error"); |
238 |
hkp_err = 1; |
hkp_has_err = 1; |
239 |
return ec; |
return ec; |
240 |
} |
} |
241 |
return 0; |
return 0; |
248 |
sock_getline (int fd, char *buf, int buflen, int *nbytes) |
sock_getline (int fd, char *buf, int buflen, int *nbytes) |
249 |
{ |
{ |
250 |
char ch; |
char ch; |
251 |
int nread = 0; |
int nread; |
252 |
|
|
253 |
if (nbytes) |
if (nbytes) |
254 |
*nbytes = 0; |
*nbytes = 0; |
255 |
*buf = 0; |
*buf = 0; |
256 |
|
nread = 0; |
257 |
while (recv (fd, &ch, 1, 0) > 0) { |
while (recv (fd, &ch, 1, 0) > 0) { |
258 |
*buf++ = ch; |
*buf++ = ch; |
259 |
nread++; |
nread++; |
272 |
|
|
273 |
|
|
274 |
/* Perform a select() on the given fd to find out |
/* Perform a select() on the given fd to find out |
275 |
is there is data for reading. Wait at least @nsecs seconds. */ |
is there is data for reading. Wait at least @nsecs milli seconds. */ |
276 |
static int |
static int |
277 |
sock_select (int fd, int nsecs) |
sock_select (int fd, int nmsecs) |
278 |
{ |
{ |
279 |
FD_SET rfd; |
FD_SET rfd; |
280 |
timeval tv = {nsecs, 0}; |
timeval tv = {0, nmsecs*1000}; |
281 |
|
|
282 |
FD_ZERO (&rfd); |
FD_ZERO (&rfd); |
283 |
FD_SET (fd, &rfd); |
FD_SET (fd, &rfd); |
297 |
Return value: 0 on success. */ |
Return value: 0 on success. */ |
298 |
static int |
static int |
299 |
sock_read (int fd, char *buf, size_t buflen, int *nbytes) |
sock_read (int fd, char *buf, size_t buflen, int *nbytes) |
300 |
{ |
{ |
|
int nread; |
|
301 |
size_t nleft = buflen; |
size_t nleft = buflen; |
302 |
size_t n = 0; |
size_t n; |
303 |
|
int nread; |
304 |
int rc; |
int rc; |
305 |
|
|
306 |
if (nbytes) |
if (nbytes) |
307 |
*nbytes = 0; |
*nbytes = 0; |
308 |
|
n = 0; |
309 |
while (nleft > 0) { |
while (nleft > 0) { |
310 |
if (n >= default_socket_timeout) |
if (n >= default_socket_timeout) |
311 |
return WPTERR_WINSOCK_TIMEOUT; |
return WPTERR_WINSOCK_TIMEOUT; |
312 |
rc = sock_select (fd, 1); |
rc = sock_select (fd, 400); |
313 |
if (rc == SOCKET_ERROR) |
if (rc == SOCKET_ERROR) |
314 |
return rc; |
return rc; |
315 |
else if (!rc) |
else if (!rc) |
337 |
then release the gpgme data object. */ |
then release the gpgme data object. */ |
338 |
char* |
char* |
339 |
data_release_and_get_mem (gpgme_data_t in, size_t *r_bufferlen) |
data_release_and_get_mem (gpgme_data_t in, size_t *r_bufferlen) |
340 |
{ |
{ |
|
size_t n; |
|
341 |
char *buffer, *p; |
char *buffer, *p; |
342 |
|
size_t n; |
343 |
|
|
344 |
gpg_data_putc (in, '\0'); |
gpg_data_putc (in, '\0'); |
345 |
p = gpgme_data_release_and_get_mem (in, &n); |
p = gpgme_data_release_and_get_mem (in, &n); |
360 |
gpgme_data_t dh; |
gpgme_data_t dh; |
361 |
char buf[1024]; |
char buf[1024]; |
362 |
size_t timeout=0; |
size_t timeout=0; |
363 |
int nread, rc; |
int rc; |
364 |
|
|
365 |
|
*buffer = NULL; |
366 |
if (gpgme_data_new (&dh)) |
if (gpgme_data_new (&dh)) |
367 |
return SOCKET_ERROR; |
return SOCKET_ERROR; |
368 |
while (timeout < default_socket_timeout) { |
while (timeout < default_socket_timeout) { |
369 |
rc = sock_select (fd, 1); |
rc = sock_select (fd, 500); |
370 |
if (rc == SOCKET_ERROR) { |
if (rc == SOCKET_ERROR) { |
371 |
gpgme_data_release (dh); |
gpgme_data_release (dh); |
372 |
return rc; |
return rc; |
374 |
else if (!rc) |
else if (!rc) |
375 |
timeout++; /* socket not ready yet. */ |
timeout++; /* socket not ready yet. */ |
376 |
else { |
else { |
377 |
nread = recv (fd, buf, DIM (buf), 0); |
int nread = recv (fd, buf, DIM (buf), 0); |
378 |
|
log_debug ("recv n=%d bytes\r\n", nread); |
379 |
if (nread == SOCKET_ERROR) |
if (nread == SOCKET_ERROR) |
380 |
return SOCKET_ERROR; |
return SOCKET_ERROR; |
381 |
else if (!nread) |
else if (!nread) |
383 |
gpgme_data_write (dh, buf, nread); |
gpgme_data_write (dh, buf, nread); |
384 |
} |
} |
385 |
} |
} |
386 |
if (timeout >= default_socket_timeout) |
|
387 |
|
if (timeout >= default_socket_timeout) { |
388 |
|
gpgme_data_release (dh); |
389 |
|
log_debug ("sock_read_ext: timeout\r\n"); |
390 |
return WPTERR_WINSOCK_TIMEOUT; |
return WPTERR_WINSOCK_TIMEOUT; |
391 |
|
} |
392 |
|
log_debug ("sock_read_ext: success\r\n"); |
393 |
*buffer = data_release_and_get_mem (dh, r_bufferlen); |
*buffer = data_release_and_get_mem (dh, r_bufferlen); |
394 |
return 0; |
return 0; |
395 |
} |
} |
430 |
} |
} |
431 |
|
|
432 |
|
|
433 |
/* Cleanup the Winsock2 interface. */ |
/* Cleanup the Winsock2 interface and free all resources. */ |
434 |
void |
void |
435 |
wsock_end (void) |
wsock_end (void) |
436 |
{ |
{ |
437 |
char *p; |
if (default_keyserver != NULL) { |
438 |
int i; |
char str_port[64]; |
439 |
|
set_reg_entry_keyserver ("Default", default_keyserver); |
440 |
#ifdef WINPT_MOBILE |
free_if_alloc (default_keyserver); |
441 |
p = m_strdup ("keyserver.conf"); |
sprintf (str_port, "%d", default_keyserver_port); |
442 |
#else |
set_reg_entry_keyserver ("Default_Port", str_port); |
443 |
p = make_special_filename (CSIDL_APPDATA, "winpt\\keyserver.conf", NULL); |
} |
444 |
#endif |
for (int i=0; i < MAX_KEYSERVERS; i++) { |
|
kserver_save_conf (p); |
|
|
free_if_alloc (p); |
|
|
free_if_alloc (default_keyserver); |
|
|
for (i=0; i < MAX_KEYSERVERS; i++) { |
|
445 |
if (server[i].name != NULL) |
if (server[i].name != NULL) |
446 |
free_if_alloc (server[i].name); |
free_if_alloc (server[i].name); |
447 |
} |
} |
460 |
if (!ec) |
if (!ec) |
461 |
return ""; |
return ""; |
462 |
switch (ec) { |
switch (ec) { |
463 |
|
case WSANOTINITIALISED: |
464 |
|
return _("Winsock subsystem has not been initialized"); |
465 |
|
|
466 |
case WSAENETDOWN: |
case WSAENETDOWN: |
467 |
return _("Network unreachable"); |
return _("Network subsystem has failed"); |
468 |
|
|
|
case WSAEHOSTUNREACH: |
|
|
return _("Host unreachable"); |
|
469 |
|
|
470 |
|
case WSATRY_AGAIN: |
471 |
|
return _("Nonauthoritative host not found, or server failure"); |
472 |
|
|
473 |
case WSAHOST_NOT_FOUND: |
case WSAHOST_NOT_FOUND: |
474 |
return _("Could not resolve host name"); |
return _("Could not resolve host name"); |
475 |
|
|
|
case WSAECONNREFUSED: |
|
|
return _("Connection refused"); |
|
|
|
|
476 |
case WSAETIMEDOUT: |
case WSAETIMEDOUT: |
477 |
case WSAECONNABORTED: |
case WSAECONNABORTED: |
478 |
return _("Connection timeout"); |
return _("Connection timeout"); |
479 |
|
|
480 |
case WSAENETRESET: |
case WSAENETRESET: |
481 |
case WSAECONNRESET: |
case WSAECONNRESET: |
482 |
return _("Connection resetted by peer"); |
return _("Connection resetted by peer"); |
483 |
|
|
484 |
|
case WSAENETUNREACH: |
485 |
|
return _("The network cannot be reached from this host at this time"); |
486 |
|
|
487 |
|
case WSAEHOSTUNREACH: |
488 |
|
return _("A socket operation was attempted to an unreachable host"); |
489 |
|
|
490 |
|
case WSAECONNREFUSED: |
491 |
|
return _("The attempt to connect was forcefully rejected"); |
492 |
|
|
493 |
case WSAESHUTDOWN: |
case WSAESHUTDOWN: |
494 |
return _("Socket has been shutdown"); |
return _("Socket has been shutdown"); |
495 |
|
|
497 |
break; |
break; |
498 |
} |
} |
499 |
|
|
500 |
return ""; |
return _("Unknown network error"); |
501 |
} |
} |
502 |
|
|
503 |
|
|
506 |
kserver_set_socket_timeout (size_t nsec) |
kserver_set_socket_timeout (size_t nsec) |
507 |
{ |
{ |
508 |
if (nsec < 0 || nsec > 3600) |
if (nsec < 0 || nsec > 3600) |
509 |
nsec = 0; |
nsec = 10; |
510 |
default_socket_timeout = nsec; |
default_socket_timeout = nsec; |
511 |
} |
} |
512 |
|
|
515 |
const char* |
const char* |
516 |
kserver_strerror (void) |
kserver_strerror (void) |
517 |
{ |
{ |
518 |
if (hkp_err) |
return hkp_has_err? hkp_err_msg : NULL; |
|
return hkp_errmsg; |
|
|
return NULL; |
|
519 |
} |
} |
520 |
|
|
521 |
|
|
529 |
return default_keyserver; |
return default_keyserver; |
530 |
} |
} |
531 |
else if (!type && (size_t)idx < DIM (server_list)) { |
else if (!type && (size_t)idx < DIM (server_list)) { |
|
/* XXX: handle non HKP servers. */ |
|
532 |
*port = HKP_PORT; |
*port = HKP_PORT; |
533 |
return server_list[idx]; |
return server_list[idx]; |
534 |
} |
} |
559 |
const char* |
const char* |
560 |
kserver_check_keyid (const char *keyid) |
kserver_check_keyid (const char *keyid) |
561 |
{ |
{ |
562 |
static char id[22]; |
static char id[64]; |
563 |
|
|
564 |
if (strstr (keyid, "@")) |
if (strstr (keyid, "@")) |
565 |
return keyid; /* email address */ |
return keyid; /* email address */ |
577 |
update_proxy_user (const char *proxy_user, const char *proxy_pass) |
update_proxy_user (const char *proxy_user, const char *proxy_pass) |
578 |
{ |
{ |
579 |
char t[257]; /* user:pass = 127+1+127+1 = 257 */ |
char t[257]; /* user:pass = 127+1+127+1 = 257 */ |
580 |
int n = 0; |
size_t n; |
581 |
|
|
582 |
n = 4*strlen (proxy_user) / 3 + 32 + strlen (proxy_pass) + 2; |
n = 4*(strlen (proxy_user) + strlen (proxy_pass))/3 + 32 + 2; |
583 |
free_if_alloc (proxy.base64_user); |
free_if_alloc (proxy.base64_user); |
584 |
proxy.base64_user = safe_alloc (n+1); |
proxy.base64_user = safe_alloc (n+1); |
585 |
_snprintf (t, DIM (t)-1, "%s:%s", proxy_user, proxy_pass); |
_snprintf (t, DIM (t)-1, "%s:%s", proxy_user, proxy_pass); |
589 |
proxy.user = m_strdup (proxy_user); |
proxy.user = m_strdup (proxy_user); |
590 |
proxy.pass = m_strdup (proxy_pass); |
proxy.pass = m_strdup (proxy_pass); |
591 |
} |
} |
|
|
|
|
|
|
|
/* Get the port number from the given protocol. */ |
|
|
static int |
|
|
port_from_proto (int proto) |
|
|
{ |
|
|
switch (proto) { |
|
|
case KSPROTO_LDAP: return LDAP_PORT; |
|
|
case KSPROTO_FINGER: return FINGER_PORT; |
|
|
case KSPROTO_HTTP: return HKP_PORT; |
|
|
} |
|
|
return 0; |
|
|
} |
|
|
|
|
|
|
|
|
/* Get the port number from the given URL. */ |
|
|
static int |
|
|
proto_from_URL (const char *buf) |
|
|
{ |
|
|
if (strstr (buf, "ldap")) |
|
|
return KSPROTO_LDAP; |
|
|
else if (strstr (buf, "finger")) |
|
|
return KSPROTO_FINGER; |
|
|
return KSPROTO_HKP; |
|
|
} |
|
592 |
|
|
593 |
|
|
594 |
/* Set the default keyserver. |
/* Set the default keyserver. The position is always one. */ |
|
If @hostname is NULL, use the predefined keyserver. */ |
|
595 |
void |
void |
596 |
keyserver_set_default (const char *hostname, WORD port) |
keyserver_set_default (const char *hostname, WORD port) |
597 |
{ |
{ |
598 |
if (!port) |
int pos=0; |
599 |
|
|
600 |
|
if (port == 0) |
601 |
port = HKP_PORT; |
port = HKP_PORT; |
602 |
if (hostname != NULL) { |
|
603 |
free_if_alloc (default_keyserver); |
free_if_alloc (default_keyserver); |
604 |
default_keyserver = m_strdup (hostname); |
default_keyserver = m_strdup (hostname); |
605 |
default_keyserver_port = port; |
default_keyserver_port = port; |
606 |
} |
|
607 |
if (!default_keyserver) { |
while (pos < MAX_KEYSERVERS) { |
608 |
default_keyserver = m_strdup (DEF_HKP_KEYSERVER); |
if (server[pos].is_default) |
609 |
default_keyserver_port = HKP_PORT; |
break; |
610 |
|
if (server[pos].used) { |
611 |
|
pos++; |
612 |
|
continue; |
613 |
|
} |
614 |
|
break; |
615 |
} |
} |
616 |
server[0].name = m_strdup (default_keyserver); |
free_if_alloc (server[pos].name); |
617 |
server[0].used = 1; |
server[pos].name = m_strdup (default_keyserver); |
618 |
server[0].port = port; |
server[pos].used = 1; |
619 |
server[0].proto = proto_from_URL (default_keyserver); |
server[pos].port = port; |
620 |
|
server[pos].is_default = 1; |
621 |
} |
} |
622 |
|
|
623 |
|
|
624 |
/* Skip all kind of whitespace chars in @str. */ |
/* Skip all kind of whitespace chars in @str. |
625 |
static const char* |
static const char* |
626 |
skip_whitespace (const char *str) |
skip_whitespace (const char *str) |
627 |
{ |
{ |
636 |
break; |
break; |
637 |
} |
} |
638 |
return str; |
return str; |
639 |
} |
}*/ |
|
|
|
|
|
|
|
/* Save the keyserver config file in @conf. */ |
|
|
int |
|
|
kserver_save_conf (const char *conf) |
|
|
{ |
|
|
FILE *fp; |
|
|
int pos; |
|
|
|
|
|
fp = fopen (conf, "wb"); |
|
|
if (!fp) { |
|
|
msg_box (NULL, _("Could not save keyserver.conf file"), |
|
|
_("Keyserver"), MB_ERR); |
|
|
return -1; |
|
|
} |
|
|
|
|
|
fprintf (fp, "# do NOT manually modify this file, it will be " |
|
|
"generated automatically!!\r\n"); |
|
|
for (pos = 0; pos < MAX_KEYSERVERS; pos++) { |
|
|
if (!server[pos].used) |
|
|
continue; |
|
|
fprintf (fp, "%s:%d\r\n", server[pos].name, server[pos].port); |
|
|
} |
|
|
fclose (fp); |
|
|
return 0; |
|
|
} |
|
640 |
|
|
641 |
|
|
642 |
/* Return the specified keyserver config setting @key as an integer. */ |
/* Return the specified keyserver config setting @key as an integer. */ |
669 |
else |
else |
670 |
prox->proto = PROXY_PROTO_NONE; |
prox->proto = PROXY_PROTO_NONE; |
671 |
free_if_alloc (proto); |
free_if_alloc (proto); |
672 |
|
|
673 |
free_if_alloc (prox->host); |
free_if_alloc (prox->host); |
674 |
prox->host = get_reg_entry_keyserver ("Host"); |
prox->host = get_reg_entry_keyserver ("Host"); |
675 |
|
|
676 |
free_if_alloc (prox->user); |
free_if_alloc (prox->user); |
677 |
prox->user = get_reg_entry_keyserver ("User"); |
prox->user = get_reg_entry_keyserver ("User"); |
678 |
|
|
679 |
free_if_alloc (prox->pass); |
free_if_alloc (prox->pass); |
680 |
prox->pass = get_reg_entry_keyserver ("Pass"); |
prox->pass = get_reg_entry_keyserver ("Pass"); |
681 |
|
|
682 |
prox->port = get_conf_kserver_int ("Port"); |
prox->port = get_conf_kserver_int ("Port"); |
683 |
} |
} |
684 |
|
|
685 |
|
|
686 |
/* Load the keyserver config file @conf. */ |
static int |
687 |
int |
is_keyserver_present (const char *name) |
688 |
kserver_load_conf (const char *conf) |
{ |
689 |
{ |
for (int i=0; i < MAX_KEYSERVERS; i++) { |
690 |
FILE *fp; |
if (server[i].name != NULL && |
691 |
char buf[1024], *s, *p; |
stricmp (server[i].name, name) == 0) |
692 |
char *user = NULL, *pass = NULL; |
return 1; |
|
int no_config=0, chk_pos; |
|
|
int pos; |
|
|
|
|
|
for (pos = 0; pos < MAX_KEYSERVERS; pos++) { |
|
|
server[pos].used = 0; |
|
|
server[pos].port = HKP_PORT; |
|
|
server[pos].name = NULL; |
|
693 |
} |
} |
694 |
|
return 0; |
695 |
|
} |
696 |
|
|
697 |
|
/* Load the keyserver configuration. */ |
698 |
|
int |
699 |
|
kserver_load_conf (void) |
700 |
|
{ |
701 |
|
char *str_server, *str_port; |
702 |
|
int port, list_pos; |
703 |
|
|
704 |
fp = fopen (conf, "rb"); |
/* If a preferred keyserver is available, load |
705 |
if (!fp) { |
* the values and store it at position 0. |
706 |
for (pos = 0; server_list[pos]; pos++) { |
*/ |
707 |
server[pos].used = 1; |
str_server = get_reg_entry_keyserver("Default"); |
708 |
server[pos].name = m_strdup (server_list[pos]); |
if (str_server && *str_server) { |
709 |
server[pos].proto = proto_from_URL (server_list[pos]); |
port = HKP_PORT; |
710 |
} |
str_port = get_reg_entry_keyserver("Default_Port"); |
711 |
no_config = 1; |
if (str_port && *str_port) |
712 |
} |
port = atoi (str_port); |
713 |
read_proxy_config (&proxy); |
keyserver_set_default (str_server, port); |
714 |
if (user && pass) |
free_if_alloc (str_port); |
715 |
update_proxy_user (user, pass); |
} |
716 |
else if (user && !pass || !user && pass) { |
free_if_alloc (str_server); |
717 |
msg_box (NULL, _("Invalid proxy configuration. " |
|
718 |
"You need to set a user and a password " |
/* Now add the default pool servers after the |
719 |
"to use proxy authentication!"), |
* preferred keyserver if present. |
720 |
_("Proxy Error"), MB_ERR); |
*/ |
721 |
} |
list_pos = 1; |
722 |
/* check if the host has a http:// prefix and skip it */ |
for (int i=0; server_list[i] != NULL; i++) { |
723 |
if (proxy.host && !strncmp (proxy.host, "http://", 7)) { |
if (is_keyserver_present (server_list[i])) |
|
char *host = m_strdup (proxy.host+7); |
|
|
free_if_alloc (proxy.host); |
|
|
proxy.host = host; |
|
|
} |
|
|
free_if_alloc (user); |
|
|
free_if_alloc (pass); |
|
|
|
|
|
pos = 0; |
|
|
while (fp && !feof (fp)) { |
|
|
s = fgets (buf, DIM (buf)-1, fp); |
|
|
if (!s) |
|
|
break; |
|
|
if (strstr (buf, "\r\n")) |
|
|
buf[strlen (buf)-2] = '\0'; |
|
|
if (strstr (buf, "\n")) |
|
|
buf[strlen (buf)-1] = '\0'; |
|
|
s = (char *)skip_whitespace (buf); |
|
|
if (*s == '#' || strlen (s) < 7) |
|
|
continue; |
|
|
chk_pos = check_URL (s); |
|
|
if (!chk_pos) { |
|
|
msg_box (NULL, _("All entries of this file must have a valid prefix.\n" |
|
|
"Currently HKP/HTTP, LDAP and FINGER are supported.\n"), |
|
|
_("Keyserver Error"), MB_ERR); |
|
724 |
continue; |
continue; |
725 |
} |
server[list_pos].used = 1; |
726 |
p = strchr (s+chk_pos, ':'); |
server[list_pos].port = HKP_PORT; |
727 |
server[pos].used = 1; |
server[list_pos].name = m_strdup (server_list[i]); |
728 |
server[pos].proto = proto_from_URL (s); |
server[list_pos].is_default = 0; |
729 |
server[pos].port = port_from_proto (server[pos].proto); |
list_pos++; |
730 |
if (!p) |
if (list_pos > MAX_KEYSERVERS) |
|
server[pos].name = m_strdup (s); |
|
|
else { |
|
|
server[pos].port = atol (p+1); |
|
|
if (server[pos].port < 0 || server[pos].port > 65536) |
|
|
server[pos].port = HKP_PORT; |
|
|
server[pos].name = safe_alloc ((p-s)+2); |
|
|
memset (server[pos].name, 0, (p-s)+2); |
|
|
memcpy (server[pos].name, s, (p-s)); |
|
|
} |
|
|
pos++; |
|
|
if (pos > MAX_KEYSERVERS) { |
|
|
msg_box (NULL, _("The keyserver limit is exceeded"), |
|
|
_("Keyserver Warning"), MB_INFO); |
|
731 |
break; |
break; |
|
} |
|
732 |
} |
} |
|
if (fp) |
|
|
fclose (fp); |
|
|
if (!pos) { |
|
|
/* Only issue an error if the config file exist but |
|
|
it has no valid entries. */ |
|
|
keyserver_set_default (NULL, HKP_PORT); |
|
|
if (!no_config) |
|
|
return WPTERR_CONFIG_FILE; |
|
|
} |
|
|
|
|
733 |
return 0; |
return 0; |
734 |
} |
} |
735 |
|
|
770 |
memcpy (&sock.sin_addr, hp->h_addr, hp->h_length); |
memcpy (&sock.sin_addr, hp->h_addr, hp->h_length); |
771 |
} |
} |
772 |
else { |
else { |
773 |
log_debug ("gethostbyname: failed ec=%d.\r\n", net_errno); |
log_debug ("gethostbyname(%s): failed ec=%d.\r\n", host, net_errno); |
774 |
return use_proxy? WPTERR_WINSOCK_PROXY : WPTERR_WINSOCK_RESOLVE; |
return use_proxy? WPTERR_WINSOCK_PROXY : WPTERR_WINSOCK_RESOLVE; |
775 |
} |
} |
776 |
fd = socket (AF_INET, SOCK_STREAM, 0); |
fd = socket (AF_INET, SOCK_STREAM, 0); |
782 |
closesocket (fd); |
closesocket (fd); |
783 |
return use_proxy? WPTERR_WINSOCK_PROXY : WPTERR_WINSOCK_CONNECT; |
return use_proxy? WPTERR_WINSOCK_PROXY : WPTERR_WINSOCK_CONNECT; |
784 |
} |
} |
785 |
|
|
|
if (proxy.proto == PROXY_PROTO_SOCKS5) { |
|
|
/* XXX: finish code and provide errors constants. */ |
|
|
rc = socks_handshake (&proxy, fd, hostname, port); |
|
|
if (rc) { |
|
|
closesocket (fd); |
|
|
return WPTERR_GENERAL; |
|
|
} |
|
|
} |
|
|
|
|
786 |
if (conn_fd) |
if (conn_fd) |
787 |
*conn_fd = fd; |
*conn_fd = fd; |
788 |
WSASetLastError (0); |
WSASetLastError (0); |
790 |
} |
} |
791 |
|
|
792 |
|
|
793 |
/* Check if the URL needs to be encoded or not. */ |
/* Check if the URL needs to be encoded or not. |
794 |
static bool |
static bool |
795 |
URL_must_encoded (const char *url) |
URL_must_encoded (const char *url) |
796 |
{ |
{ |
797 |
if (strchr (url, '.') || strchr (url, '@') || strchr (url, ' ')) |
if (strchr (url, '.') || strchr (url, '@') || strchr (url, ' ')) |
798 |
return true; |
return true; |
799 |
return false; |
return false; |
800 |
} |
}*/ |
801 |
|
|
802 |
|
|
803 |
/* Perform URL encoding of the given data. */ |
/* Perform URL encoding of the given data. */ |
805 |
URL_encode (const char *url, DWORD ulen, DWORD *ret_nlen) |
URL_encode (const char *url, DWORD ulen, DWORD *ret_nlen) |
806 |
{ |
{ |
807 |
gpgme_data_t hd; |
gpgme_data_t hd; |
808 |
char numbuf[5], *p; |
char numbuf[8], *p; |
809 |
size_t i, nlen; |
size_t i, nlen; |
810 |
|
|
811 |
if (gpgme_data_new (&hd)) |
if (gpgme_data_new (&hd)) |
854 |
"Proxy-Authorization: Basic %s\r\n" |
"Proxy-Authorization: Basic %s\r\n" |
855 |
"Content-type: application/x-www-form-urlencoded\r\n" |
"Content-type: application/x-www-form-urlencoded\r\n" |
856 |
"Content-length: %d\r\n" |
"Content-length: %d\r\n" |
857 |
|
"Connection: close\r\n" |
858 |
"\r\n" |
"\r\n" |
859 |
"keytext=%s" |
"keytext=%s" |
860 |
"\r\n"; |
"\r\n"; |
871 |
"Host: %s:%d\r\n" |
"Host: %s:%d\r\n" |
872 |
"Content-type: application/x-www-form-urlencoded\r\n" |
"Content-type: application/x-www-form-urlencoded\r\n" |
873 |
"Content-length: %d\r\n" |
"Content-length: %d\r\n" |
874 |
|
"Connection: close\r\n" |
875 |
"\r\n" |
"\r\n" |
876 |
"keytext=%s" |
"keytext=%s" |
877 |
"\r\n"; |
"\r\n"; |
893 |
kserver_recvkey (const char *hostname, WORD port, const char *keyid, |
kserver_recvkey (const char *hostname, WORD port, const char *keyid, |
894 |
char **r_key, size_t *r_keylen) |
char **r_key, size_t *r_keylen) |
895 |
{ |
{ |
896 |
const char *fmt; |
StringBuffer req; |
897 |
char *request = NULL; |
const char *reqbuf; |
898 |
int conn_fd; |
int conn_fd; |
899 |
int rc, reqlen; |
int rc; |
900 |
|
|
901 |
if (!port) |
if (!port) |
902 |
port = HKP_PORT; |
port = HKP_PORT; |
903 |
hkp_err = 0; /* reset */ |
hkp_has_err = 0; /* reset */ |
904 |
rc = kserver_connect (hostname, port, &conn_fd); |
rc = kserver_connect (hostname, port, &conn_fd); |
905 |
if (rc) |
if (rc) |
906 |
goto leave; |
goto leave; |
907 |
|
|
|
reqlen = strlen (hostname)+32+strlen (keyid)+1; |
|
908 |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
909 |
fmt = "GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n" |
req = req + "GET http://" + skip_type_prefix (hostname) + |
910 |
"Proxy-Authorization: Basic %s\r\n\r\n"; |
":" + port + |
911 |
reqlen =+ strlen (fmt) + strlen (proxy.base64_user)+1; |
"/pks/lookup?op=get&search=" + keyid + " HTTP/1.0\r\n" + |
912 |
request = safe_alloc (reqlen+1); |
"Proxy-Authorization: Basic " + proxy.base64_user + "\r\n" + |
913 |
_snprintf (request, reqlen, fmt, skip_type_prefix (hostname), port, |
"Host: " + skip_type_prefix (hostname) + ":" + port + "\r\n"+ |
914 |
keyid, proxy.base64_user); |
"Connection: close\r\n"+ |
915 |
|
"\r\n"; |
916 |
} |
} |
917 |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
918 |
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) + |
919 |
reqlen += strlen (fmt)+1; |
":" + port + "/pks/lookup?op=get&search=" + keyid + |
920 |
request = safe_alloc (reqlen+1); |
" HTTP/1.0\r\n" + |
921 |
_snprintf (request, reqlen, fmt, skip_type_prefix (hostname), |
"Host: " + skip_type_prefix (hostname) + ":" + port + "\r\n"+ |
922 |
port, keyid); |
"Connection: close\r\n"+ |
923 |
} |
"\r\n"; |
|
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); |
|
924 |
} |
} |
925 |
|
else |
926 |
log_debug ("request:\r\n%s\r\n", request); |
req = req + "GET /pks/lookup?op=get&search=" + keyid + |
927 |
|
" HTTP/1.0\r\n"+ // FIXME |
928 |
|
"Host: " + skip_type_prefix (hostname) + ":" + port + "\r\n"+ |
929 |
|
"Connection: close\r\n"+ |
930 |
|
"\r\n"; |
931 |
|
|
932 |
rc = sock_write (conn_fd, request, strlen (request)); |
log_debug ("req:\r\n%s\r\n", req.getBuffer ()); |
933 |
|
|
934 |
|
reqbuf = req.getBuffer (); |
935 |
|
rc = sock_write (conn_fd, reqbuf, strlen (reqbuf)); |
936 |
if (rc == SOCKET_ERROR) { |
if (rc == SOCKET_ERROR) { |
937 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
938 |
goto leave; |
goto leave; |
953 |
|
|
954 |
leave: |
leave: |
955 |
closesocket (conn_fd); |
closesocket (conn_fd); |
|
free_if_alloc (request); |
|
956 |
return rc; |
return rc; |
957 |
} |
} |
958 |
|
|
967 |
int conn_fd, n; |
int conn_fd, n; |
968 |
int rc; |
int rc; |
969 |
|
|
970 |
hkp_err = 0; /* reset */ |
hkp_has_err = 0; /* reset */ |
971 |
rc = kserver_connect (hostname, port, &conn_fd); |
rc = kserver_connect (hostname, port, &conn_fd); |
972 |
if (rc) |
if (rc) |
973 |
goto leave; |
goto leave; |
1059 |
kserver_search_begin (const char *hostname, WORD port, |
kserver_search_begin (const char *hostname, WORD port, |
1060 |
const char *pattern, int *conn_fd, size_t *nkeys) |
const char *pattern, int *conn_fd, size_t *nkeys) |
1061 |
{ |
{ |
1062 |
const char *fmt; |
|
1063 |
char *request = NULL; |
StringBuffer req; |
1064 |
|
const char *reqbuf; |
1065 |
char *enc_patt = NULL; |
char *enc_patt = NULL; |
1066 |
char status[128]; |
char status[128]; |
1067 |
int rc, sock_fd; |
int rc, sock_fd; |
1068 |
int reqlen; |
int nread; |
1069 |
|
|
1070 |
*conn_fd = 0; |
*conn_fd = 0; |
1071 |
|
|
1074 |
goto leave; |
goto leave; |
1075 |
|
|
1076 |
enc_patt = URL_encode (pattern, strlen (pattern), NULL); |
enc_patt = URL_encode (pattern, strlen (pattern), NULL); |
|
reqlen = strlen (enc_patt) + strlen (hostname) + 32 + 1; |
|
|
|
|
1077 |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
1078 |
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 + |
1079 |
"Proxy-Authorization: Basic %s\r\n\r\n"; |
"/pks/lookup?options=mr&op=index&search=" + enc_patt + |
1080 |
reqlen += strlen (proxy.base64_user) + strlen (fmt) + 1; |
" HTTP/1.0\r\n" + |
1081 |
request = safe_alloc (reqlen+1); |
"Host: " + skip_type_prefix (hostname) + ":" + port+ "\r\n"+ |
1082 |
_snprintf (request, reqlen, fmt, skip_type_prefix (hostname), port, |
"Connection: close\r\n" + |
1083 |
enc_patt, proxy.base64_user); |
"Proxy-Authorization: Basic " + proxy.base64_user + "\r\n\r\n"; |
1084 |
} |
} |
1085 |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
1086 |
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 + |
1087 |
reqlen += strlen (fmt)+1; |
"/pks/lookup?options=mr&op=index&search=" + enc_patt + |
1088 |
request = safe_alloc (reqlen+1); |
" HTTP/1.0\r\n"+ |
1089 |
_snprintf (request, reqlen, skip_type_prefix (hostname), port, |
"Host: " + skip_type_prefix (hostname) + ":" + port+ "\r\n"+ |
1090 |
enc_patt); |
"Connection: close\r\n"+ |
1091 |
|
"\r\n"; |
1092 |
} |
} |
1093 |
else { |
else { |
1094 |
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=" + |
1095 |
reqlen += strlen (fmt)+1; |
enc_patt + " HTTP/1.0\r\n" + |
1096 |
request = safe_alloc (reqlen+1); |
"Host: " + skip_type_prefix (hostname) + ":" + port+ "\r\n"+ |
1097 |
_snprintf (request, reqlen, fmt, enc_patt); |
"Connection: close\r\n"+ |
1098 |
|
"\r\n"; |
1099 |
} |
} |
1100 |
|
|
1101 |
log_debug ("kserver_search_begin:\r\n%s\r\n", request); |
log_debug ("kserver_search_begin:\r\n%s\r\n", req.getBuffer ()); |
1102 |
if (sock_write (sock_fd, request, strlen (request)) == SOCKET_ERROR) { |
reqbuf = req.getBuffer (); |
1103 |
|
if (sock_write (sock_fd, reqbuf, strlen (reqbuf)) == SOCKET_ERROR) { |
1104 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1105 |
goto leave; |
goto leave; |
1106 |
} |
} |
1114 |
|
|
1115 |
/* Skip all lines until we reach the "info:" record. */ |
/* Skip all lines until we reach the "info:" record. */ |
1116 |
for (;;) { |
for (;;) { |
1117 |
if (sock_getline (sock_fd, status, DIM (status)-1, &reqlen)) { |
if (sock_getline (sock_fd, status, DIM (status)-1, &nread)) { |
1118 |
log_debug ("kserver_search_begin: retrieving status line failed.\r\n"); |
log_debug ("kserver_search_begin: retrieving status line failed.\r\n"); |
1119 |
closesocket (sock_fd); |
closesocket (sock_fd); |
1120 |
sock_fd = 0; |
sock_fd = 0; |
1130 |
*conn_fd = sock_fd; |
*conn_fd = sock_fd; |
1131 |
|
|
1132 |
leave: |
leave: |
|
free_if_alloc (request); |
|
1133 |
free_if_alloc (enc_patt); |
free_if_alloc (enc_patt); |
1134 |
return rc; |
return rc; |
1135 |
} |
} |
1144 |
int recno = 0; |
int recno = 0; |
1145 |
int off = 0; |
int off = 0; |
1146 |
|
|
1147 |
/* pub:BF3DF9B4:17:1024:925411133:: */ |
/* pub:{BF3DF9B4, ED4681C9BF3DF9B4, FPR}:17:1024:925411133:: */ |
1148 |
p = strtok (buf, ":"); |
p = strtok (buf, ":"); |
1149 |
while (p != NULL) { |
while (p != NULL) { |
1150 |
recno++; |
recno++; |
1315 |
} |
} |
1316 |
|
|
1317 |
|
|
|
/* Spawn a process given by @cmdl. */ |
|
|
static int |
|
|
spawn_application (char *cmdl) |
|
|
{ |
|
|
STARTUPINFO si; |
|
|
PROCESS_INFORMATION pi; |
|
|
int rc = 0; |
|
|
|
|
|
memset (&si, 0, sizeof (si)); |
|
|
si.cb = sizeof (si); |
|
|
si.dwFlags = STARTF_USESHOWWINDOW; |
|
|
si.wShowWindow = SW_HIDE; |
|
|
memset (&pi, 0, sizeof (pi)); |
|
|
|
|
|
if (!CreateProcess (NULL, cmdl, NULL, NULL, FALSE, 0, |
|
|
NULL, NULL, &si, &pi)) { |
|
|
log_box ("Keyserver Plugin", MB_ERR, "Could not spawn helper process"); |
|
|
rc = -1; |
|
|
} |
|
|
|
|
|
CloseHandle (pi.hThread); |
|
|
WaitForSingleObject (pi.hProcess, INFINITE); |
|
|
CloseHandle (pi.hProcess); |
|
|
return rc; |
|
|
} |
|
|
|
|
|
|
|
|
static FILE* |
|
|
do_spawn_ldap_helper (const char *host, const char *keyid) |
|
|
{ |
|
|
FILE *fp = NULL; |
|
|
char *p, *sep; |
|
|
char *ksprg; |
|
|
char outf[256], inf[256]; |
|
|
size_t n; |
|
|
|
|
|
p = get_gnupg_prog (); |
|
|
n = strlen (p) + 1 + 128; |
|
|
ksprg = safe_alloc (n+1); |
|
|
if (!ksprg) |
|
|
BUG (0); |
|
|
sep = strrchr (p, '\\'); |
|
|
if (sep != NULL) |
|
|
p[(sep-p)] = 0; |
|
|
|
|
|
_snprintf (ksprg, n, "%s\\gpgkeys_ldap.exe", p); |
|
|
free_if_alloc (p); |
|
|
if (file_exist_check (ksprg)) { |
|
|
log_box ("LDAP Keyserver Plugin", MB_ERR, |
|
|
"%s: could not find LDAP keyserver module!", ksprg); |
|
|
fp = NULL; |
|
|
goto leave; |
|
|
} |
|
|
get_temp_name (outf, DIM (outf)-1, keyid); |
|
|
get_temp_name (inf, DIM (inf)-1, NULL); |
|
|
fp = fopen (inf, "w+b"); |
|
|
if (!fp) { |
|
|
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", inf, |
|
|
winpt_strerror (WPTERR_FILE_OPEN)); |
|
|
goto leave; |
|
|
} |
|
|
fprintf (fp, |
|
|
"VERSION 1\n" |
|
|
"PROGRAM %d.%d.%d\n" |
|
|
"SCHEME ldap\n" |
|
|
"HOST %s\n" |
|
|
"COMMAND GET\n" |
|
|
"\n" |
|
|
"%s\n", |
|
|
gpgver[0], gpgver[1], gpgver[2], |
|
|
host? skip_type_prefix (host): "64.94.85.200", keyid); |
|
|
fclose (fp); |
|
|
|
|
|
p = safe_alloc (strlen (ksprg) + strlen (inf) + strlen (outf) + 32); |
|
|
sprintf (p, "%s -o %s %s", ksprg, outf, inf); |
|
|
if (spawn_application (p)) { |
|
|
fp = NULL; |
|
|
goto leave; |
|
|
} |
|
|
fp = fopen (outf, "rb"); |
|
|
if (!fp) |
|
|
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf, |
|
|
winpt_strerror (WPTERR_FILE_OPEN)); |
|
|
|
|
|
leave: |
|
|
DeleteFile (inf); |
|
|
DeleteFile (outf); |
|
|
free_if_alloc (p); |
|
|
free_if_alloc (ksprg); |
|
|
return fp; |
|
|
} |
|
|
|
|
|
/* Receive an key via LDAP from host @host with the keyid @keyid. |
|
|
@key contains the key on success. */ |
|
|
int |
|
|
ldap_recvkey (const char *host, const char *keyid, |
|
|
char **r_key, size_t *r_keylen) |
|
|
{ |
|
|
gpgme_data_t raw; |
|
|
FILE *fp; |
|
|
const char *s; |
|
|
char buf[512]; |
|
|
int start_key = 0, failed = 0; |
|
|
int rc = 0; |
|
|
|
|
|
fp = do_spawn_ldap_helper (host, keyid); |
|
|
if (!fp) |
|
|
return WPTERR_GENERAL; |
|
|
|
|
|
if (gpgme_data_new (&raw)) |
|
|
BUG (0); |
|
|
while (!feof (fp)) { |
|
|
s = fgets (buf, DIM (buf)-1, fp); |
|
|
if (!s) |
|
|
break; |
|
|
if (strstr (s, "KEY") && strstr (s, "FAILED")) { |
|
|
failed = 1; |
|
|
break; |
|
|
} |
|
|
if (!start_key && strstr (s, "KEY") && strstr (s, "BEGIN")) { |
|
|
start_key = 1; |
|
|
continue; |
|
|
} |
|
|
if (!start_key) |
|
|
continue; |
|
|
gpgme_data_write (raw, buf, strlen (buf)); |
|
|
if (strstr (s, "KEY") && strstr (s, "END")) |
|
|
break; |
|
|
} |
|
|
fclose (fp); |
|
|
|
|
|
if (failed) |
|
|
rc = WPTERR_WINSOCK_RECVKEY; |
|
|
*r_key = data_release_and_get_mem (raw, r_keylen); |
|
|
return rc; |
|
|
} |
|
|
|
|
|
|
|
|
/* Receive an key via FINGER from host @host with the user @user. |
|
|
On success @key contains the key. */ |
|
|
int |
|
|
finger_recvkey (const char *host, const char *user, |
|
|
char **r_key, size_t *r_keylen) |
|
|
{ |
|
|
gpgme_data_t raw; |
|
|
char buf[256+1]; |
|
|
int fd, nread; |
|
|
int start_key = 0; |
|
|
int rc=0; |
|
|
|
|
|
rc = kserver_connect (host, FINGER_PORT, &fd); |
|
|
if (rc) |
|
|
return rc; |
|
|
|
|
|
sock_write (fd, user, strlen (user)); |
|
|
sock_write (fd, "\r\n", 2); |
|
|
|
|
|
if (gpgme_data_new (&raw)) |
|
|
return WPTERR_WINSOCK_RECVKEY; |
|
|
|
|
|
for (;;) { |
|
|
if (sock_getline (fd, buf, DIM (buf)-2, &nread)) |
|
|
break; |
|
|
strcat (buf, "\n"); |
|
|
if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) { |
|
|
gpgme_data_write (raw, buf, nread); |
|
|
start_key = 1; |
|
|
} |
|
|
else if (strstr (buf, "END PGP PUBLIC KEY BLOCK" ) && start_key) { |
|
|
gpgme_data_write (raw, buf, nread); |
|
|
start_key--; |
|
|
break; |
|
|
} |
|
|
else if (start_key) |
|
|
gpgme_data_write (raw, buf, nread); |
|
|
} |
|
|
|
|
|
closesocket (fd); |
|
|
if (start_key != 0) |
|
|
rc = WPTERR_WINSOCK_RECVKEY; |
|
|
*r_key = data_release_and_get_mem (raw, r_keylen); |
|
|
return rc; |
|
|
} |
|
|
|
|
|
|
|
1318 |
/* Check if the given name @name is a valid inernet address |
/* Check if the given name @name is a valid inernet address |
1319 |
which means an dotted IP or a valid DNS name. |
which means an dotted IP or a valid DNS name. |
1320 |
Return value: 0 on success. */ |
Return value: 0 on success. */ |
1322 |
check_inet_address (const char *addr) |
check_inet_address (const char *addr) |
1323 |
{ |
{ |
1324 |
const char *not_allowed = "=!�$%&@#*~\\/}][{<>|,;:'"; |
const char *not_allowed = "=!�$%&@#*~\\/}][{<>|,;:'"; |
|
size_t i; |
|
1325 |
|
|
1326 |
for (i=0; i < strlen (addr); i++) { |
for (size_t i=0; i < strlen (addr); i++) { |
1327 |
if (strchr (not_allowed, addr[i])) |
if (strchr (not_allowed, addr[i])) |
1328 |
return -1; |
return -1; |
1329 |
} |
} |
1338 |
{ |
{ |
1339 |
char *p; |
char *p; |
1340 |
char *url = *r_keyserver; |
char *url = *r_keyserver; |
1341 |
int off = 0; |
int off; |
1342 |
|
|
1343 |
/* no port is given so use the default port. */ |
/* no port is given so use the default port. */ |
1344 |
p = strrchr (url, ':'); |
p = strrchr (url, ':'); |
1345 |
if (p == strchr (url, ':')) { |
if (p == strchr (url, ':')) { |
1346 |
int port = port_from_proto (proto_from_URL (url)); |
*r_port = HKP_PORT; |
|
if (!port) |
|
|
port = HKP_PORT; |
|
|
*r_port = port; |
|
1347 |
return 0; |
return 0; |
1348 |
} |
} |
1349 |
|
|
1350 |
if (url[(p-url)-1] == '/') /* remove / in .de/:11371 */ |
if (url[(p-url)-1] == '/') /* remove / in .de/:11371 */ |
1351 |
off = 1; |
off = 1; |
1352 |
|
else |
1353 |
|
off = 0; |
1354 |
|
|
1355 |
*r_keyserver = substr (url, 0, (p-url)-off); |
*r_keyserver = substr (url, 0, (p-url)-off); |
1356 |
*r_port = atoi (url+(p-url)+1); |
*r_port = atoi (url+(p-url)+1); |