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