1 |
/* wptKeyserver.cpp - W32 Keyserver Access |
/* wptKeyserver.cpp - W32 Keyserver Access |
2 |
* Copyright (C) 2000-2005 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 |
20 |
|
|
21 |
#include <windows.h> |
#include <windows.h> |
22 |
|
#include <shlobj.h> |
23 |
#include <stdio.h> |
#include <stdio.h> |
24 |
#include <sys/stat.h> |
#include <sys/stat.h> |
25 |
#include <ctype.h> |
#include <ctype.h> |
26 |
|
|
27 |
|
#include "wptGPG.h" |
28 |
#include "wptKeyserver.h" |
#include "wptKeyserver.h" |
29 |
#include "wptErrors.h" |
#include "wptErrors.h" |
30 |
#include "wptTypes.h" |
#include "wptTypes.h" |
31 |
#include "wptNLS.h" |
#include "wptNLS.h" |
32 |
#include "wptW32API.h" |
#include "wptW32API.h" |
|
#include "wptVersion.h" |
|
|
#include "wptGPG.h" |
|
33 |
#include "wptRegistry.h" |
#include "wptRegistry.h" |
34 |
|
#include "wptUTF8.h" |
35 |
|
#include "wptVersion.h" |
36 |
|
#include "StringBuffer.h" |
37 |
|
|
38 |
|
|
39 |
static char base64code[] = |
/* Map net_errno to a winsock error. */ |
40 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
#define net_errno ((int)WSAGetLastError ()) |
41 |
|
|
42 |
keyserver server[MAX_KEYSERVERS] = {0}; |
keyserver server[MAX_KEYSERVERS] = {0}; |
43 |
static keyserver_proxy_ctx proxy = {0}; |
keyserver_proxy_s proxy = {0}; |
44 |
static const char * server_list[] = { |
static const char *server_list[] = { |
45 |
"hkp://wwwkeys.nl.pgp.net", |
"hkp://pool.sks-keyservers.net", |
|
"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", |
|
|
"hkp://gnv.us.ks.cryptnet.net", |
|
46 |
"hkp://subkeys.pgp.net", |
"hkp://subkeys.pgp.net", |
47 |
"ldap://keyserver.pgp.com", |
"http://minsky.surfnet.nl", |
48 |
NULL |
NULL |
49 |
}; |
}; |
50 |
static char hkp_errmsg[1024]; |
#define NUM_DEF_KEYSERVERS 3 |
51 |
static int hkp_err = 0; |
|
52 |
static u32 conf_timestamp = 0; |
static char hkp_err_msg[384]; /* Holds the error message from the server */ |
53 |
char * default_keyserver = NULL; |
static int hkp_has_err = 0; /* != 0 indicates an error occurred. */ |
54 |
unsigned short default_keyserver_port = 0; |
|
55 |
|
/* Default keyserver and port. */ |
56 |
|
char *default_keyserver = NULL; |
57 |
|
WORD default_keyserver_port = 0; |
58 |
|
|
59 |
|
/* Default socket timeout (secs). */ |
60 |
|
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. */ |
94 |
|
static char* |
95 |
|
safe_alloc (DWORD n) |
96 |
|
{ |
97 |
|
char *p = new char[n+1]; |
98 |
|
if (!p) |
99 |
|
BUG (0); |
100 |
|
memset (p, 0, n); |
101 |
|
return p; |
102 |
|
} |
103 |
|
|
104 |
|
|
105 |
|
/* Basic64 encode the input @inbuf to @outbuf. */ |
106 |
static void |
static void |
107 |
base64_encode( const char *inputstr, char *outputstr, int maxlen ) |
base64_encode (const char *inbuf, char *outbuf) |
108 |
{ |
{ |
109 |
int index = 0, temp = 0, res = 0, i = 0, inputlen = 0, len = 0; |
char base64code[] = |
110 |
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
111 |
|
int index = 0, temp = 0, res = 0; |
112 |
|
int inputlen, len = 0; |
113 |
|
|
114 |
inputlen = strlen (inputstr); /* fixme: check if len > maxlen */ |
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) | (inputstr[i] & 0xFF); |
res = (res << 8) | (inbuf[i] & 0xFF); |
118 |
switch (index++) { |
switch (index++) { |
119 |
case 0: outputstr[len++] = base64code[res >> 2 & 0x3F]; res &= 0x3; break; |
case 0: |
120 |
case 1: outputstr[len++] = base64code[res >> 4 & 0x3F]; res &= 0xF; break; |
outbuf[len++] = base64code[res >> 2 & 0x3F]; |
121 |
case 2: outputstr[len++] = base64code[res >> 6 & 0x3F]; |
res &= 0x3; |
122 |
outputstr[len++] = base64code[res & 0x3F]; res = index = 0; break; |
break; |
123 |
|
case 1: |
124 |
|
outbuf[len++] = base64code[res >> 4 & 0x3F]; |
125 |
|
res &= 0xF; |
126 |
|
break; |
127 |
|
case 2: |
128 |
|
outbuf[len++] = base64code[res >> 6 & 0x3F]; |
129 |
|
outbuf[len++] = base64code[res & 0x3F]; |
130 |
|
res = index = 0; |
131 |
|
break; |
132 |
} |
} |
133 |
temp = res; |
temp = res; |
134 |
} |
} |
135 |
|
|
136 |
switch( index ) { |
if (index == 2) { |
137 |
case 0: break; |
outbuf[len++] = base64code[temp << 2 & 0x3F]; |
138 |
case 2: outputstr[len++] = base64code[temp << 2 & 0x3F]; |
outbuf[len++] = '='; |
139 |
outputstr[len++] = '='; break; |
} |
140 |
case 1: outputstr[len++] = base64code[temp << 4 & 0x3F]; |
else if (index == 1) { |
141 |
outputstr[len++] = '='; |
outbuf[len++] = base64code[temp << 4 & 0x3F]; |
142 |
outputstr[len++] = '='; |
outbuf[len++] = '='; |
143 |
|
outbuf[len++] = '='; |
144 |
} |
} |
145 |
|
|
146 |
outputstr[len] = '\0'; |
outbuf[len] = '\0'; |
147 |
} /* base64_encode */ |
} |
148 |
|
|
149 |
|
|
150 |
|
/* Check that the given buffer contains a valid keyserver URL |
151 |
|
and return the prefix length, 0 in case of an error. */ |
152 |
|
static int |
153 |
|
check_URL (const char *buf) |
154 |
|
{ |
155 |
|
if (strstr (buf, "hkp://")) |
156 |
|
return 6; |
157 |
|
if (strstr (buf, "http://")) |
158 |
|
return 7; |
159 |
|
return 0; |
160 |
|
} |
161 |
|
|
162 |
|
|
163 |
/* Skip the URL schema and return only the host part of it. */ |
/* Skip the URL schema and return only the host part of it. */ |
164 |
static const char * |
static const char* |
165 |
skip_type_prefix (const char * hostname) |
skip_type_prefix (const char *hostname) |
166 |
{ |
{ |
167 |
if (hostname && !strncmp (hostname, "http://", 7)) |
int pos; |
168 |
hostname += 7; |
|
169 |
else if (hostname && !strncmp (hostname, "hkp://", 6)) |
if (!hostname) |
170 |
hostname += 6; |
return hostname; |
171 |
else if (hostname && !strncmp (hostname, "finger://", 9)) |
|
172 |
hostname += 9; |
pos = check_URL (hostname); |
173 |
else if (hostname && !strncmp (hostname, "ldap://", 7)) |
if (pos > 0) |
174 |
hostname += 7; |
hostname += pos; |
175 |
return hostname; |
return hostname; |
176 |
} |
} |
177 |
|
|
178 |
|
|
179 |
|
/* Parse the keyserver response and extract the human |
180 |
|
readable text passages. */ |
181 |
|
static int |
182 |
|
parse_keyserver_error (const char *resp, char *txt, size_t txtlen) |
183 |
|
{ |
184 |
|
char *p, *p2; |
185 |
|
|
186 |
|
/* If we find no 'Error' substring we assume a success. */ |
187 |
|
if (!stristr (resp, "Error")) |
188 |
|
return -1; |
189 |
|
|
190 |
|
memset (txt, 0, txtlen); |
191 |
|
p = strstr (resp, "\r\n\r\n"); |
192 |
|
if (!p) |
193 |
|
return -1; |
194 |
|
resp += (p-resp); |
195 |
|
p = strstr (resp, "<h1>"); |
196 |
|
if (!p) |
197 |
|
return -1; |
198 |
|
resp += (p-resp)+strlen ("<h1>"); |
199 |
|
p = strstr (resp, "</h1>"); |
200 |
|
if (!p) |
201 |
|
return -1; |
202 |
|
resp += (p-resp)+strlen ("</h1>"); |
203 |
|
p2 = strstr (resp, "</body>"); |
204 |
|
if (p2 != NULL) |
205 |
|
memcpy (txt, resp, (p2-resp)); |
206 |
|
else { |
207 |
|
if (!strnicmp (resp, "<p>", 3)) |
208 |
|
resp += 3; |
209 |
|
while (resp && (*resp == '\r' || *resp == '\n')) |
210 |
|
resp++; |
211 |
|
memcpy (txt, resp, strlen (resp)); |
212 |
|
} |
213 |
|
return 0; |
214 |
|
} |
215 |
|
|
216 |
|
|
217 |
/* Check that the keyserver response indicates an OK. |
/* Check that the keyserver response indicates an OK. |
218 |
Return 0 on success. */ |
Return 0 on success. */ |
219 |
static int |
static int |
220 |
check_hkp_response (const char *resp, int recv) |
check_hkp_response (const char *resp, int recv) |
221 |
{ |
{ |
222 |
int ec, len; |
int ec; |
|
char *p, * end; |
|
223 |
|
|
224 |
ec = recv ? WPTERR_WINSOCK_RECVKEY : WPTERR_WINSOCK_SENDKEY; |
ec = recv ? WPTERR_WINSOCK_RECVKEY : WPTERR_WINSOCK_SENDKEY; |
225 |
if (!strstr (resp, "HTTP/1.0 200 OK") && |
if (!resp) |
|
!strstr (resp, "HTTP/1.1 200 OK")) /* http error */ |
|
226 |
return ec; |
return ec; |
227 |
if (strstr (resp, "Public Key Server -- Error") |
log_debug ("check_hkp_response: '%s'\r\n", resp); |
228 |
|| strstr (resp, "Public Key Server -- Error") |
if (!strstr (resp, "HTTP/1.0 200 OK") && |
229 |
|| strstr (resp, "No matching keys in database")) { |
!strstr (resp, "HTTP/1.1 200 OK") && |
230 |
p = strstr (resp, "<p>"); |
!strstr (resp, "HTTP/1.0 500 OK") && |
231 |
if (p && strlen (p) < sizeof (hkp_errmsg)) { |
!strstr (resp, "HTTP/1.1 500 OK")) |
232 |
end = strstr (p, "</p>"); |
return ec; /* http error */ |
233 |
len = end? (end - p + 1) : strlen (p); |
|
234 |
strncpy (hkp_errmsg, p, len); |
if (!parse_keyserver_error (resp, hkp_err_msg, DIM (hkp_err_msg)-2)) { |
235 |
hkp_err = 1; |
if (!strlen (hkp_err_msg)) |
236 |
} |
_snprintf (hkp_err_msg, DIM (hkp_err_msg)-1, |
237 |
|
"Unknown keyserver error"); |
238 |
|
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 count = 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 |
count++; |
nread++; |
260 |
if (ch == '\n' || count == (buflen - 1)) { |
if (ch == '\r') |
261 |
|
continue; /* remove this char. */ |
262 |
|
if (ch == '\n' || nread == (buflen - 1)) { |
263 |
*buf = 0; |
*buf = 0; |
264 |
if (nbytes) |
if (nbytes) |
265 |
*nbytes = count; |
*nbytes = nread; |
266 |
return 0; |
return 0; |
267 |
} |
} |
268 |
} |
} |
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); |
284 |
if (select (fd + 1, &rfd, NULL, NULL, &tv) == SOCKET_ERROR) |
if (select (fd + 1, &rfd, NULL, NULL, &tv) == SOCKET_ERROR) { |
285 |
|
log_debug ("sock_select: select() failed ec=%d.\r\n", net_errno); |
286 |
return SOCKET_ERROR; |
return SOCKET_ERROR; |
287 |
|
} |
288 |
if (FD_ISSET (fd, &rfd)) |
if (FD_ISSET (fd, &rfd)) |
289 |
return 1; |
return 1; |
290 |
return 0; |
return 0; |
291 |
} |
} |
292 |
|
|
293 |
|
|
294 |
/* Read from a socket with a fixed timeout of 10 seconds. |
/* Read from a socket @fd to buffer @buf with a fixed timeout |
295 |
|
of 10 seconds. The amount of bytes which were read are |
296 |
|
returned in @nbytes. |
297 |
Return value: 0 on success. */ |
Return value: 0 on success. */ |
298 |
static int |
static int |
299 |
sock_read (int fd, char *buf, int buflen, int *nbytes) |
sock_read (int fd, char *buf, size_t buflen, int *nbytes) |
300 |
{ |
{ |
301 |
DWORD nread; |
size_t nleft = buflen; |
302 |
int nleft = buflen; |
size_t n; |
303 |
int rc, n = 0; |
int nread; |
304 |
|
int rc; |
305 |
|
|
306 |
|
if (nbytes) |
307 |
|
*nbytes = 0; |
308 |
|
n = 0; |
309 |
while (nleft > 0) { |
while (nleft > 0) { |
310 |
if (n >= 10) |
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 SOCKET_ERROR; |
return rc; |
315 |
else if( !rc ) |
else if (!rc) |
316 |
n++; |
n++; |
317 |
else { |
else { |
318 |
nread = recv (fd, buf, nleft, 0); |
nread = recv (fd, buf, nleft, 0); |
319 |
if (nread == SOCKET_ERROR) |
if (nread == SOCKET_ERROR) { |
320 |
|
log_debug ("sock_read: recv() failed ec=%d.\r\n", net_errno); |
321 |
return SOCKET_ERROR; |
return SOCKET_ERROR; |
322 |
|
} |
323 |
else if (!nread) |
else if (!nread) |
324 |
break; |
break; |
325 |
nleft -= nread; |
nleft -= nread; |
333 |
} |
} |
334 |
|
|
335 |
|
|
336 |
/* Write @buf to a socket @fd. */ |
/* Helper to create a string from the gpgme data and |
337 |
|
then release the gpgme data object. */ |
338 |
|
char* |
339 |
|
data_release_and_get_mem (gpgme_data_t in, size_t *r_bufferlen) |
340 |
|
{ |
341 |
|
char *buffer, *p; |
342 |
|
size_t n; |
343 |
|
|
344 |
|
gpg_data_putc (in, '\0'); |
345 |
|
p = gpgme_data_release_and_get_mem (in, &n); |
346 |
|
buffer = m_strdup (p); |
347 |
|
if (r_bufferlen) |
348 |
|
*r_bufferlen = n; |
349 |
|
gpgme_free (p); |
350 |
|
return buffer; |
351 |
|
} |
352 |
|
|
353 |
|
|
354 |
|
/* Read much data as possible from the socket @fd and |
355 |
|
return the data in @buffer. Caller must free data. |
356 |
|
Return value: 0 on success. */ |
357 |
|
int |
358 |
|
sock_read_ext (int fd, char **buffer, size_t *r_bufferlen) |
359 |
|
{ |
360 |
|
gpgme_data_t dh; |
361 |
|
char buf[1024]; |
362 |
|
size_t timeout=0; |
363 |
|
int rc; |
364 |
|
|
365 |
|
*buffer = NULL; |
366 |
|
if (gpgme_data_new (&dh)) |
367 |
|
return SOCKET_ERROR; |
368 |
|
while (timeout < default_socket_timeout) { |
369 |
|
rc = sock_select (fd, 500); |
370 |
|
if (rc == SOCKET_ERROR) { |
371 |
|
gpgme_data_release (dh); |
372 |
|
return rc; |
373 |
|
} |
374 |
|
else if (!rc) |
375 |
|
timeout++; /* socket not ready yet. */ |
376 |
|
else { |
377 |
|
int nread = recv (fd, buf, DIM (buf), 0); |
378 |
|
log_debug ("recv n=%d bytes\r\n", nread); |
379 |
|
if (nread == SOCKET_ERROR) |
380 |
|
return SOCKET_ERROR; |
381 |
|
else if (!nread) |
382 |
|
break; |
383 |
|
gpgme_data_write (dh, buf, nread); |
384 |
|
} |
385 |
|
} |
386 |
|
|
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; |
391 |
|
} |
392 |
|
log_debug ("sock_read_ext: success\r\n"); |
393 |
|
*buffer = data_release_and_get_mem (dh, r_bufferlen); |
394 |
|
return 0; |
395 |
|
} |
396 |
|
|
397 |
|
|
398 |
|
/* Write the buffer @buf with the length @buflen to a socket @fd. */ |
399 |
static int |
static int |
400 |
sock_write (int fd, const char *buf, int buflen) |
sock_write (int fd, const char *buf, size_t buflen) |
401 |
{ |
{ |
402 |
DWORD nwritten; |
int nwritten; |
403 |
int nleft = buflen; |
int nleft = buflen; |
404 |
|
|
405 |
while (nleft > 0) { |
while (nleft > 0) { |
406 |
nwritten = send (fd, buf, nleft, 0); |
nwritten = send (fd, buf, nleft, 0); |
407 |
if (nwritten == SOCKET_ERROR) |
if (nwritten == SOCKET_ERROR) { |
408 |
|
log_debug ("sock_write: send() failed ec=%d\r\n", net_errno); |
409 |
return SOCKET_ERROR; |
return SOCKET_ERROR; |
410 |
|
} |
411 |
nleft -= nwritten; |
nleft -= nwritten; |
412 |
buf += nwritten; |
buf += nwritten; |
413 |
} |
} |
414 |
|
|
415 |
return 0; |
return 0; |
416 |
} |
} |
417 |
|
|
418 |
|
|
|
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 = (u16)strtoul (p, NULL, 10); |
|
|
free_if_alloc (p); |
|
|
} |
|
|
else |
|
|
default_keyserver_port = HKP_PORT; |
|
|
} |
|
|
|
|
|
|
|
419 |
/* Initialize the Winsock2 interface.*/ |
/* Initialize the Winsock2 interface.*/ |
420 |
int |
int |
421 |
wsock_init (void) |
wsock_init (void) |
422 |
{ |
{ |
423 |
WSADATA wsa; |
WSADATA wsa; |
424 |
|
|
425 |
if (WSAStartup (0x202, &wsa)) |
if (WSAStartup (0x202, &wsa)) { |
426 |
|
log_debug ("wsock_init: WSAStartup failed ec=%d\r\n", net_errno); |
427 |
return WPTERR_WINSOCK_INIT; |
return WPTERR_WINSOCK_INIT; |
428 |
set_default_kserver (); |
} |
429 |
return 0; |
return 0; |
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 |
int i; |
if (default_keyserver != NULL) { |
438 |
|
char str_port[64]; |
439 |
free_if_alloc (default_keyserver); |
set_reg_entry_keyserver ("Default", default_keyserver); |
440 |
default_keyserver = NULL; |
free_if_alloc (default_keyserver); |
441 |
for (i=0; i < MAX_KEYSERVERS; i++) { |
sprintf (str_port, "%d", default_keyserver_port); |
442 |
if (server[i].used) |
set_reg_entry_keyserver ("Default_Port", str_port); |
443 |
|
} |
444 |
|
for (int i=0; i < MAX_KEYSERVERS; i++) { |
445 |
|
if (server[i].name != NULL) |
446 |
free_if_alloc (server[i].name); |
free_if_alloc (server[i].name); |
447 |
} |
} |
448 |
|
kserver_proxy_release (&proxy); |
449 |
WSACleanup (); |
WSACleanup (); |
450 |
|
log_debug ("wsock_end: cleanup finished.\r\n"); |
451 |
} |
} |
452 |
|
|
453 |
|
|
454 |
/* Return a string representation of a winsock error. */ |
/* Return a string representation of a winsock error. */ |
455 |
const char* |
const char* |
456 |
wsock_strerror (void) |
wsock_strerror (void) |
457 |
{ |
{ |
|
static char buf[384]; |
|
458 |
int ec = WSAGetLastError (); |
int ec = WSAGetLastError (); |
459 |
|
|
460 |
|
if (!ec) |
461 |
|
return ""; |
462 |
switch (ec) { |
switch (ec) { |
463 |
case WSAENETDOWN: |
case WSANOTINITIALISED: |
464 |
return _("The network subsystem has failed"); |
return _("Winsock subsystem has not been initialized"); |
465 |
case WSAHOST_NOT_FOUND: |
|
466 |
return _("Authoritative Answer Host not found"); |
case WSAENETDOWN: |
467 |
case WSAETIMEDOUT: |
return _("Network subsystem has failed"); |
468 |
return _("The connection has been dropped because of a network failure"); |
|
469 |
default: |
|
470 |
_snprintf (buf, sizeof (buf) -1, _("Unknown Winsock error ec=%d"),ec); |
case WSATRY_AGAIN: |
471 |
return buf; |
return _("Nonauthoritative host not found, or server failure"); |
472 |
|
|
473 |
|
case WSAHOST_NOT_FOUND: |
474 |
|
return _("Could not resolve host name"); |
475 |
|
|
476 |
|
case WSAETIMEDOUT: |
477 |
|
case WSAECONNABORTED: |
478 |
|
return _("Connection timeout"); |
479 |
|
|
480 |
|
case WSAENETRESET: |
481 |
|
case WSAECONNRESET: |
482 |
|
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: |
494 |
|
return _("Socket has been shutdown"); |
495 |
|
|
496 |
|
default: |
497 |
|
break; |
498 |
} |
} |
499 |
return NULL; |
|
500 |
|
return _("Unknown network error"); |
501 |
|
} |
502 |
|
|
503 |
|
|
504 |
|
/* Set default socket timeout for all reading operations. */ |
505 |
|
void |
506 |
|
kserver_set_socket_timeout (size_t nsec) |
507 |
|
{ |
508 |
|
if (nsec < 0 || nsec > 3600) |
509 |
|
nsec = 10; |
510 |
|
default_socket_timeout = nsec; |
511 |
} |
} |
512 |
|
|
513 |
|
|
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 |
|
|
522 |
/* Read a keyserver from the list at index @idx. |
/* Read a keyserver from the list at index @idx. |
523 |
Return value: keyserver name at the given position. */ |
Return value: keyserver name at the given position. */ |
524 |
const char* |
const char* |
525 |
kserver_get_hostname (int idx, int type, u16 *port) |
kserver_get_hostname (int idx, int type, WORD *port) |
526 |
{ |
{ |
527 |
if (type == -1) { |
if (type == -1) { |
528 |
*port = default_keyserver_port; |
*port = default_keyserver_port; |
529 |
return default_keyserver; |
return default_keyserver; |
530 |
} |
} |
531 |
else if (!type && idx < DIM (server_list)) { |
else if (!type && (size_t)idx < DIM (server_list)) { |
532 |
*port = HKP_PORT; |
*port = HKP_PORT; |
533 |
return server_list[idx]; |
return server_list[idx]; |
534 |
} |
} |
547 |
closesocket (fd); |
closesocket (fd); |
548 |
return 0; |
return 0; |
549 |
} |
} |
550 |
|
log_debug ("kserver_check_inet_connection: no inet connection.\r\n"); |
551 |
return -1; |
return -1; |
552 |
} |
} |
553 |
|
|
554 |
|
|
555 |
/* |
/* If the request contains the keyid, it have to be |
556 |
* If the request contains the keyid, it have to be |
always postfix with '0x'+keyid. This code checks |
557 |
* always postfix with '0x'+keyid. This code checks |
if the keyid is a decimal value and if so if it contains |
558 |
* if the keyid is a decimal value and if so if it contains |
the '0x'. If not it is inserted. */ |
|
* the '0x'. If not it is inserted. |
|
|
*/ |
|
559 |
const char* |
const char* |
560 |
kserver_check_keyid (const char *keyid) |
kserver_check_keyid (const char *keyid) |
561 |
{ |
{ |
562 |
static char id[20]; |
static char id[64]; |
563 |
|
|
564 |
if (strstr (keyid, "@")) |
if (strstr (keyid, "@")) |
565 |
return keyid; /* email address */ |
return keyid; /* email address */ |
566 |
if (strncmp (keyid, "0x", 2)) { |
if (strncmp (keyid, "0x", 2)) { |
567 |
memset (&id, 0, sizeof (id)); |
memset (&id, 0, sizeof (id)); |
568 |
_snprintf (id, sizeof (id)-1, "0x%s", keyid); |
_snprintf (id, DIM (id)-1, "0x%s", keyid); |
569 |
return id; |
return id; |
570 |
} |
} |
571 |
return keyid; |
return keyid; |
572 |
} /* kserver_check_keyid */ |
} |
573 |
|
|
574 |
|
|
575 |
|
/* Update the keyserver proxy user. */ |
576 |
static void |
static void |
577 |
kserver_update_proxyuser (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 = new char[n]; |
proxy.base64_user = safe_alloc (n+1); |
585 |
if (!proxy.base64_user) |
_snprintf (t, DIM (t)-1, "%s:%s", proxy_user, proxy_pass); |
586 |
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); |
|
587 |
free_if_alloc (proxy.user); |
free_if_alloc (proxy.user); |
588 |
free_if_alloc (proxy.pass); |
free_if_alloc (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 |
} /* kserver_update_proxyuser */ |
} |
592 |
|
|
593 |
|
|
594 |
/* Check that the given buffer contains a valid keyserver URL. */ |
/* Set the default keyserver. The position is always one. */ |
595 |
static int |
void |
596 |
check_URL (const char * buf) |
keyserver_set_default (const char *hostname, WORD port) |
597 |
{ |
{ |
598 |
if (strlen (buf) < 7) |
int pos=0; |
|
return -1; |
|
|
if (!strstr (buf, "ldap://") |
|
|
&& !strstr (buf, "http://") |
|
|
&& !strstr (buf, "finger://") |
|
|
&& !strstr (buf, "hkp://")) |
|
|
return -1; |
|
|
return 0; |
|
|
} |
|
599 |
|
|
600 |
|
if (port == 0) |
601 |
/* Get the port number from the given protocol. */ |
port = HKP_PORT; |
602 |
static int |
|
603 |
port_from_proto (int proto) |
free_if_alloc (default_keyserver); |
604 |
{ |
default_keyserver = m_strdup (hostname); |
605 |
switch (proto) { |
default_keyserver_port = port; |
606 |
case KSPROTO_LDAP: return 0; |
|
607 |
case KSPROTO_FINGER: return FINGER_PORT; |
while (pos < MAX_KEYSERVERS) { |
608 |
case KSPROTO_HTTP: return HKP_PORT; |
if (server[pos].is_default) |
609 |
|
break; |
610 |
|
if (server[pos].used) { |
611 |
|
pos++; |
612 |
|
continue; |
613 |
|
} |
614 |
|
break; |
615 |
} |
} |
616 |
return 0; |
free_if_alloc (server[pos].name); |
617 |
} |
server[pos].name = m_strdup (default_keyserver); |
618 |
|
server[pos].used = 1; |
619 |
|
server[pos].port = port; |
620 |
/* Get the port number from the given URL. */ |
server[pos].is_default = 1; |
|
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; |
|
621 |
} |
} |
622 |
|
|
623 |
|
|
624 |
void |
/* Skip all kind of whitespace chars in @str. |
625 |
keyserver_set_default (const char * hostname, u16 port) |
static const char* |
626 |
|
skip_whitespace (const char *str) |
627 |
{ |
{ |
628 |
if (hostname) { |
while (str && *str) { |
|
free_if_alloc (default_keyserver); |
|
|
default_keyserver = m_strdup (hostname); |
|
|
if (!default_keyserver) |
|
|
BUG (0); |
|
|
default_keyserver_port = port; |
|
|
} |
|
|
server[0].name = m_strdup (default_keyserver); |
|
|
server[0].used = 1; |
|
|
server[0].port = port; |
|
|
server[0].proto = proto_from_URL (default_keyserver); |
|
|
} /* keyserver_set_default */ |
|
|
|
|
|
|
|
|
static const char * |
|
|
skip_whitespace (const char * str) |
|
|
{ |
|
|
while (str && *str) |
|
|
{ |
|
629 |
if (*str == ' ' || |
if (*str == ' ' || |
630 |
*str == '\t' || |
*str == '\t' || |
631 |
*str == '\n' || |
*str == '\n' || |
632 |
*str == '\r') |
*str == '\r') { |
|
{ |
|
633 |
str++; |
str++; |
634 |
continue; |
continue; |
635 |
} |
} |
636 |
break; |
break; |
637 |
} |
} |
638 |
return str; |
return str; |
639 |
|
}*/ |
640 |
|
|
641 |
|
|
642 |
|
/* Return the specified keyserver config setting @key as an integer. */ |
643 |
|
static int |
644 |
|
get_conf_kserver_int (const char *key) |
645 |
|
{ |
646 |
|
char *p; |
647 |
|
int val = 0; |
648 |
|
|
649 |
|
p = get_reg_entry_keyserver (key); |
650 |
|
if (p && *p) |
651 |
|
val = atoi (p); |
652 |
|
free_if_alloc (p); |
653 |
|
return val; |
654 |
} |
} |
655 |
|
|
656 |
|
|
657 |
int |
/* Read the proxy configuration and store it into @prox. */ |
658 |
kserver_load_conf (const char * conf) |
static void |
659 |
|
read_proxy_config (keyserver_proxy_t prox) |
660 |
{ |
{ |
661 |
struct stat statbuf; |
char *proto; |
662 |
FILE *fp; |
|
663 |
char buf[1024], * s, *p; |
if (!prox) |
664 |
char *user = NULL, *pass = NULL; |
return; |
|
int pos; |
|
|
int no_config=0, chk_pos=0; |
|
665 |
|
|
666 |
for (pos = 0; pos < MAX_KEYSERVERS; pos++) { |
proto = get_reg_entry_keyserver("Proto"); |
667 |
server[pos].used = 0; |
if (proto != NULL && strlen (proto) > 0) |
668 |
server[pos].port = HKP_PORT; |
prox->proto = atoi (proto); |
669 |
} |
else |
670 |
|
prox->proto = PROXY_PROTO_NONE; |
671 |
|
free_if_alloc (proto); |
672 |
|
|
673 |
fp = fopen (conf, "rb"); |
free_if_alloc (prox->host); |
674 |
if (!fp) { |
prox->host = get_reg_entry_keyserver ("Host"); |
675 |
for (pos = 0; server_list[pos]; pos++) { |
|
676 |
server[pos].used = 1; |
free_if_alloc (prox->user); |
677 |
server[pos].name = m_strdup (server_list[pos]); |
prox->user = get_reg_entry_keyserver ("User"); |
678 |
server[pos].proto = proto_from_URL (server_list[pos]); |
|
679 |
} |
free_if_alloc (prox->pass); |
680 |
no_config=1; |
prox->pass = get_reg_entry_keyserver ("Pass"); |
681 |
} |
|
682 |
get_reg_proxy_prefs (&proxy.host, &proxy.port, &user, &pass); |
prox->port = get_conf_kserver_int ("Port"); |
683 |
if (user && pass) |
} |
|
kserver_update_proxyuser (user, pass); |
|
|
else if (user && !pass || !user && pass) { |
|
|
msg_box( NULL, _("Invalid proxy configuration." |
|
|
"You need to set a user and a password" |
|
|
"to use proxy authentication!"), _("Proxy Error"), MB_ERR ); |
|
|
} |
|
|
/* check if the host has a http:// prefix and skip it */ |
|
|
if( proxy.host && !strncmp( proxy.host, "http://", 7 ) ) { |
|
|
char *pr = m_strdup (proxy.host+7); |
|
|
if (!pr) |
|
|
BUG (NULL); |
|
|
free_if_alloc (proxy.host); |
|
|
proxy.host = pr; |
|
|
} |
|
|
free_if_alloc (user); |
|
|
free_if_alloc (pass); |
|
|
|
|
|
pos = 0; |
|
|
while( fp && !feof( fp ) ) { |
|
|
s = fgets (buf, sizeof (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; |
|
|
if (check_URL (s)) { |
|
|
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); |
|
|
continue; |
|
|
} |
|
|
chk_pos=6; |
|
|
if (strstr (s, "finger")) |
|
|
chk_pos = 10; |
|
|
p = strchr (s+chk_pos, ':'); |
|
|
server[pos].used = 1; |
|
|
server[pos].proto = proto_from_URL (s); |
|
|
server[pos].port = port_from_proto (server[pos].proto); |
|
|
if (!p) |
|
|
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 = new char [(p-s)+2]; |
|
|
if (!server[pos].name) |
|
|
BUG (0); |
|
|
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); |
|
|
break; |
|
|
} |
|
|
} |
|
|
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; |
|
|
} |
|
|
|
|
|
if (!stat (conf, &statbuf)) |
|
|
conf_timestamp = statbuf.st_mtime; |
|
|
return 0; |
|
|
} /* kserver_load_conf */ |
|
684 |
|
|
685 |
|
|
686 |
const char * |
static int |
687 |
kserver_get_proxy (int * r_port) |
is_keyserver_present (const char *name) |
688 |
{ |
{ |
689 |
if (proxy.host) { |
for (int i=0; i < MAX_KEYSERVERS; i++) { |
690 |
if (r_port) |
if (server[i].name != NULL && |
691 |
*r_port = proxy.port; |
stricmp (server[i].name, name) == 0) |
692 |
return proxy.host; |
return 1; |
693 |
} |
} |
694 |
return NULL; |
return 0; |
695 |
} /* kserver_get_proxy */ |
} |
|
|
|
696 |
|
|
697 |
const char * |
/* Load the keyserver configuration. */ |
698 |
kserver_get_proxy_info (int id) |
int |
699 |
|
kserver_load_conf (void) |
700 |
{ |
{ |
701 |
switch (id) { |
char *str_server, *str_port; |
702 |
case PROXY_USER: return proxy.user; |
int port, list_pos; |
703 |
case PROXY_PASS: return proxy.pass; |
|
704 |
|
/* If a preferred keyserver is available, load |
705 |
|
* the values and store it at position 0. |
706 |
|
*/ |
707 |
|
str_server = get_reg_entry_keyserver("Default"); |
708 |
|
if (str_server && *str_server) { |
709 |
|
port = HKP_PORT; |
710 |
|
str_port = get_reg_entry_keyserver("Default_Port"); |
711 |
|
if (str_port && *str_port) |
712 |
|
port = atoi (str_port); |
713 |
|
keyserver_set_default (str_server, port); |
714 |
|
free_if_alloc (str_port); |
715 |
|
} |
716 |
|
free_if_alloc (str_server); |
717 |
|
|
718 |
|
/* Now add the default pool servers after the |
719 |
|
* preferred keyserver if present. |
720 |
|
*/ |
721 |
|
list_pos = 1; |
722 |
|
for (int i=0; server_list[i] != NULL; i++) { |
723 |
|
if (is_keyserver_present (server_list[i])) |
724 |
|
continue; |
725 |
|
server[list_pos].used = 1; |
726 |
|
server[list_pos].port = HKP_PORT; |
727 |
|
server[list_pos].name = m_strdup (server_list[i]); |
728 |
|
server[list_pos].is_default = 0; |
729 |
|
list_pos++; |
730 |
|
if (list_pos > MAX_KEYSERVERS) |
731 |
|
break; |
732 |
} |
} |
733 |
return NULL; |
return 0; |
734 |
} /* kserver_get_proxy_info */ |
} |
735 |
|
|
736 |
|
|
737 |
/* Connect to the keyserver @hostname. */ |
/* Connect to the keyserver @hostname (port @port). |
738 |
|
Return value: 0 on success */ |
739 |
int |
int |
740 |
kserver_connect (const char *hostname, u16 port, int *conn_fd) |
kserver_connect (const char *hostname, WORD port, int *conn_fd) |
741 |
{ |
{ |
|
int rc, fd; |
|
|
u32 iaddr; |
|
|
char host[128] = {0}; |
|
742 |
struct hostent *hp; |
struct hostent *hp; |
743 |
struct sockaddr_in sock; |
struct sockaddr_in sock; |
744 |
|
char host[128] = {0}; |
745 |
log_debug ("kserver_connect: %s:%d\r\n", hostname, port); |
DWORD iaddr; |
746 |
|
bool use_proxy = proxy.host != NULL; |
747 |
|
int rc, fd; |
748 |
|
|
749 |
if (!port) |
if (!port) |
750 |
port = HKP_PORT; |
port = HKP_PORT; |
751 |
if (conn_fd) |
if (conn_fd) |
752 |
*conn_fd = 0; |
*conn_fd = 0; |
753 |
hostname = skip_type_prefix (hostname); |
hostname = skip_type_prefix (hostname); |
754 |
|
log_debug ("kserver_connect: %s:%d\r\n", hostname, port); |
755 |
|
|
756 |
|
if (use_proxy && proxy.proto == PROXY_PROTO_HTTP) |
757 |
|
port = proxy.port; |
758 |
memset (&sock, 0, sizeof (sock)); |
memset (&sock, 0, sizeof (sock)); |
759 |
sock.sin_family = AF_INET; |
sock.sin_family = AF_INET; |
760 |
sock.sin_port = proxy.host? htons (proxy.port) : htons (port); |
sock.sin_port = htons (port); |
761 |
if (proxy.host) |
strncpy (host, use_proxy? proxy.host : hostname, DIM (host)-1); |
762 |
strncpy (host, proxy.host, 127); |
|
|
else |
|
|
strncpy (host, hostname, 127); |
|
|
|
|
763 |
if ((iaddr = inet_addr (host)) != INADDR_NONE) |
if ((iaddr = inet_addr (host)) != INADDR_NONE) |
764 |
memcpy (&sock.sin_addr, &iaddr, sizeof (iaddr)); |
memcpy (&sock.sin_addr, &iaddr, sizeof (iaddr)); |
765 |
else if ((hp = gethostbyname (host))) { |
else if ((hp = gethostbyname (host))) { |
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.\r\n"); |
log_debug ("gethostbyname(%s): failed ec=%d.\r\n", host, net_errno); |
774 |
return 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); |
777 |
if (fd == INVALID_SOCKET) |
if (fd == INVALID_SOCKET) |
778 |
return WPTERR_WINSOCK_SOCKET; |
return WPTERR_WINSOCK_SOCKET; |
779 |
rc = connect (fd, (struct sockaddr *) &sock, sizeof (sock)); |
rc = connect (fd, (struct sockaddr *) &sock, sizeof (sock)); |
780 |
if (rc == SOCKET_ERROR) { |
if (rc == SOCKET_ERROR) { |
781 |
log_debug ("connect: failed.\r\n"); |
log_debug ("connect: failed ec=%d.\r\n", net_errno); |
782 |
closesocket (fd); |
closesocket (fd); |
783 |
return WPTERR_WINSOCK_CONNECT; |
return use_proxy? WPTERR_WINSOCK_PROXY : WPTERR_WINSOCK_CONNECT; |
784 |
} |
} |
785 |
|
|
786 |
if (conn_fd) |
if (conn_fd) |
787 |
*conn_fd = fd; |
*conn_fd = fd; |
788 |
WSASetLastError (0); |
WSASetLastError (0); |
789 |
return 0; |
return 0; |
790 |
} /* kserver_connect */ |
} |
791 |
|
|
792 |
|
|
793 |
|
/* Check if the URL needs to be encoded or not. |
794 |
|
static bool |
795 |
|
URL_must_encoded (const char *url) |
796 |
|
{ |
797 |
|
if (strchr (url, '.') || strchr (url, '@') || strchr (url, ' ')) |
798 |
|
return true; |
799 |
|
return false; |
800 |
|
}*/ |
801 |
|
|
802 |
|
|
803 |
/* Perform URL-encoding on the given pubkey blob. */ |
/* Perform URL encoding of the given data. */ |
804 |
static char* |
static char* |
805 |
kserver_urlencode (const char *pubkey, size_t octets, size_t *newlen) |
URL_encode (const char *url, DWORD ulen, DWORD *ret_nlen) |
806 |
{ |
{ |
807 |
char *p, numbuf[5]; |
gpgme_data_t hd; |
808 |
size_t j = 0; |
char numbuf[8], *p; |
809 |
size_t i, size; |
size_t i, nlen; |
|
|
|
|
p = new char [2*octets]; |
|
|
if (!p) |
|
|
BUG (0); |
|
810 |
|
|
811 |
for (size=0, i=0; i < octets; i++) { |
if (gpgme_data_new (&hd)) |
812 |
if (isalnum (pubkey[i]) || pubkey[i] == '-') { |
BUG (0); |
813 |
p[size] = pubkey[i]; |
for (i=0; i < ulen; i++) { |
814 |
size++; |
if (isalnum (url[i]) || url[i] == '-') |
815 |
} |
gpg_data_putc (hd, url[i]); |
816 |
else if (pubkey[i] == ' ') { |
else if (url[i] == ' ') |
817 |
p[size] = '+'; |
gpg_data_putc (hd, '+'); |
|
size++; |
|
|
} |
|
818 |
else { |
else { |
819 |
sprintf(numbuf, "%%%02X", pubkey[i]); |
sprintf (numbuf, "%%%02X", url[i]); |
820 |
for (j = 0; j<strlen(numbuf); j++) { |
gpgme_data_write (hd, numbuf, strlen (numbuf)); |
|
p[size] = numbuf[j]; |
|
|
size++; |
|
|
} |
|
821 |
} |
} |
822 |
} |
} |
823 |
p[size] = '\0'; |
|
824 |
*newlen = size; |
p = data_release_and_get_mem (hd, &nlen); |
825 |
|
if (ret_nlen) |
826 |
|
*ret_nlen = nlen; |
827 |
return p; |
return p; |
828 |
} |
} |
829 |
|
|
830 |
|
|
831 |
/* Format a request for the given keyid (send). */ |
/* Format a request for the given keyid (send). */ |
832 |
static char* |
static char* |
833 |
kserver_send_request (const char *hostname, u16 port, const char *pubkey, int octets) |
kserver_send_request (const char *hostname, WORD port, |
834 |
|
const char *pubkey, DWORD octets) |
835 |
{ |
{ |
836 |
char *request = NULL, *enc_pubkey = NULL; |
const char *fmt; |
837 |
|
char *request; |
838 |
|
char *enc_pubkey; |
839 |
|
DWORD enc_octets; |
840 |
int reqlen; |
int reqlen; |
|
size_t enc_octets; |
|
841 |
|
|
842 |
log_debug ("kserver_send_request: %s:%d\n", hostname, port); |
log_debug ("kserver_send_request: %s:%d\r\n", hostname, port); |
843 |
|
|
844 |
if (!port) |
if (!port) |
845 |
port = HKP_PORT; |
port = HKP_PORT; |
846 |
reqlen = 512 + strlen (hostname) + 2*strlen (pubkey); |
enc_pubkey = URL_encode (pubkey, octets, &enc_octets); |
847 |
request = new char[reqlen]; |
reqlen = 2*strlen (hostname) + 2*32/*port*/ + enc_octets + 32 /*len*/ + 1; |
848 |
if (!request) |
|
849 |
BUG (0); |
if (proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
850 |
enc_pubkey = kserver_urlencode (pubkey, octets, &enc_octets); |
fmt = "POST http://%s:%d/pks/add HTTP/1.0\r\n" |
851 |
if (!enc_pubkey || !enc_octets) { |
"Referer: \r\n" |
852 |
free_if_alloc (request); |
"User-Agent: WinPT/W32\r\n" |
853 |
return NULL; |
"Host: %s:%d\r\n" |
854 |
} |
"Proxy-Authorization: Basic %s\r\n" |
855 |
|
"Content-type: application/x-www-form-urlencoded\r\n" |
856 |
if (proxy.user) { |
"Content-length: %d\r\n" |
857 |
_snprintf (request, reqlen-1, |
"Connection: close\r\n" |
858 |
"POST http://%s:%d/pks/add HTTP/1.0\r\n" |
"\r\n" |
859 |
"Referer: \r\n" |
"keytext=%s" |
860 |
"User-Agent: WinPT/W32\r\n" |
"\r\n"; |
861 |
"Host: %s:%d\r\n" |
reqlen += strlen (fmt) + strlen (proxy.base64_user) + 1; |
862 |
"Proxy-Authorization: Basic %s\r\n" |
request = safe_alloc (reqlen+1); |
863 |
"Content-type: application/x-www-form-urlencoded\r\n" |
_snprintf (request, reqlen, fmt, |
864 |
"Content-length: %d\r\n" |
skip_type_prefix (hostname), port, hostname, port, |
|
"\r\n" |
|
|
"keytext=%s" |
|
|
"\n", |
|
|
skip_type_prefix (hostname), port, hostname, port, |
|
865 |
proxy.base64_user, enc_octets+9, enc_pubkey); |
proxy.base64_user, enc_octets+9, enc_pubkey); |
866 |
} |
} |
867 |
else { |
else { |
868 |
_snprintf (request, reqlen-1, |
fmt = "POST /pks/add HTTP/1.0\r\n" |
869 |
"POST /pks/add HTTP/1.0\r\n" |
"Referer: \r\n" |
870 |
"Referer: \r\n" |
"User-Agent: WinPT/W32\r\n" |
871 |
"User-Agent: WinPT/W32\r\n" |
"Host: %s:%d\r\n" |
872 |
"Host: %s:%d\r\n" |
"Content-type: application/x-www-form-urlencoded\r\n" |
873 |
"Content-type: application/x-www-form-urlencoded\r\n" |
"Content-length: %d\r\n" |
874 |
"Content-length: %d\r\n" |
"Connection: close\r\n" |
875 |
"\r\n" |
"\r\n" |
876 |
"keytext=%s" |
"keytext=%s" |
877 |
"\n", |
"\r\n"; |
878 |
skip_type_prefix (hostname), port, enc_octets+9, enc_pubkey); |
reqlen += strlen (fmt)+1; |
879 |
|
request = safe_alloc (reqlen+1); |
880 |
|
_snprintf (request, reqlen, fmt, |
881 |
|
skip_type_prefix (hostname), port, |
882 |
|
enc_octets+9, enc_pubkey); |
883 |
} |
} |
|
free_if_alloc (enc_pubkey); |
|
884 |
|
|
885 |
log_debug ("%s\n", request); |
free_if_alloc (enc_pubkey); |
886 |
|
log_debug ("request:\r\n%s\r\n", request); |
887 |
return request; |
return request; |
888 |
} /* kserver_send_request */ |
} |
889 |
|
|
890 |
|
|
891 |
/* Interface receiving a public key. */ |
/* Interface for receiving a public key. */ |
892 |
int |
int |
893 |
kserver_recvkey (const char *hostname, u16 port, const char *keyid, char *key, int maxkeylen) |
kserver_recvkey (const char *hostname, WORD port, const char *keyid, |
894 |
{ |
char **r_key, size_t *r_keylen) |
895 |
int rc, n; |
{ |
896 |
|
StringBuffer req; |
897 |
|
const char *reqbuf; |
898 |
int conn_fd; |
int conn_fd; |
899 |
char *request = NULL; |
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; |
|
|
|
|
request = new char[300+1]; |
|
|
if (!request) |
|
|
BUG (0); |
|
|
|
|
|
if (proxy.host && proxy.user) { |
|
|
_snprintf (request, 300, |
|
|
"GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n" |
|
|
"Proxy-Authorization: Basic %s\r\n\r\n", |
|
|
skip_type_prefix (hostname), port, keyid, proxy.base64_user); |
|
|
} |
|
|
else if (proxy.host) { |
|
|
_snprintf (request, 300, |
|
|
"GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n", |
|
|
skip_type_prefix (hostname), port, keyid); |
|
|
} |
|
|
else { |
|
|
_snprintf (request, 300, |
|
|
"GET /pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n", keyid); |
|
|
} |
|
907 |
|
|
908 |
log_debug ("%s\r\n", request); |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
909 |
|
req = req + "GET http://" + skip_type_prefix (hostname) + |
910 |
|
":" + port + |
911 |
|
"/pks/lookup?op=get&search=" + keyid + " HTTP/1.0\r\n" + |
912 |
|
"Proxy-Authorization: Basic " + proxy.base64_user + "\r\n" + |
913 |
|
"Host: " + skip_type_prefix (hostname) + ":" + port + "\r\n"+ |
914 |
|
"Connection: close\r\n"+ |
915 |
|
"\r\n"; |
916 |
|
} |
917 |
|
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
918 |
|
req = req + "GET http://" + skip_type_prefix (hostname) + |
919 |
|
":" + port + "/pks/lookup?op=get&search=" + keyid + |
920 |
|
" HTTP/1.0\r\n" + |
921 |
|
"Host: " + skip_type_prefix (hostname) + ":" + port + "\r\n"+ |
922 |
|
"Connection: close\r\n"+ |
923 |
|
"\r\n"; |
924 |
|
} |
925 |
|
else |
926 |
|
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; |
939 |
} |
} |
940 |
|
|
941 |
rc = sock_read( conn_fd, key, maxkeylen, &n ); |
rc = sock_read_ext (conn_fd, r_key, r_keylen); |
942 |
if( rc == SOCKET_ERROR ) { |
if (rc == SOCKET_ERROR) { |
943 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
944 |
goto leave; |
goto leave; |
945 |
} |
} |
946 |
|
|
947 |
log_debug("%s\r\n", key); |
log_debug ("response:\r\n%s\r\n", *r_key); |
948 |
rc = check_hkp_response (key, 1); |
rc = check_hkp_response (*r_key, 1); |
949 |
if (rc) |
if (rc) |
950 |
goto leave; |
goto leave; |
951 |
|
|
953 |
|
|
954 |
leave: |
leave: |
955 |
closesocket (conn_fd); |
closesocket (conn_fd); |
|
free_if_alloc (request); |
|
956 |
return rc; |
return rc; |
957 |
} /* kserver_recvkey */ |
} |
958 |
|
|
959 |
|
|
960 |
/* Interface to send a public key. */ |
/* Interface to send a public key. */ |
961 |
int |
int |
962 |
kserver_sendkey (const char *hostname, u16 port, const char *pubkey, int len ) |
kserver_sendkey (const char *hostname, WORD port, |
963 |
|
const char *pubkey, size_t len) |
964 |
{ |
{ |
|
int n, rc; |
|
|
int conn_fd; |
|
965 |
char *request = NULL; |
char *request = NULL; |
966 |
char log[2048]; |
char log[2048] = {0}; |
967 |
|
int conn_fd, n; |
968 |
|
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; |
974 |
|
|
975 |
request = kserver_send_request (hostname, port, pubkey, len); |
request = kserver_send_request (hostname, port, pubkey, len); |
976 |
if (request == NULL) { |
rc = sock_write (conn_fd, request, strlen (request)); |
977 |
rc = WPTERR_GENERAL; |
if (rc == SOCKET_ERROR) { |
|
goto leave; |
|
|
} |
|
|
|
|
|
rc = sock_write( conn_fd, request, lstrlen(request) ); |
|
|
if( rc == SOCKET_ERROR ) { |
|
978 |
rc = WPTERR_WINSOCK_SENDKEY; |
rc = WPTERR_WINSOCK_SENDKEY; |
979 |
goto leave; |
goto leave; |
980 |
} |
} |
981 |
|
|
982 |
rc = sock_read( conn_fd, log, sizeof (log)-1, &n ); |
rc = sock_read (conn_fd, log, DIM (log)-1, &n); |
983 |
if( rc == SOCKET_ERROR ) { |
if (rc == SOCKET_ERROR) { |
984 |
rc = WPTERR_WINSOCK_SENDKEY; |
rc = WPTERR_WINSOCK_SENDKEY; |
985 |
goto leave; |
goto leave; |
986 |
} |
} |
987 |
|
|
988 |
log_debug ("kserver_sendkey:\r\n%s\r\n", log); |
log_debug ("kserver_sendkey: read %d bytes\r\n%s\r\n", n, log); |
|
|
|
989 |
rc = check_hkp_response (log, 0); |
rc = check_hkp_response (log, 0); |
990 |
if( rc ) |
if (rc) |
991 |
goto leave; |
goto leave; |
992 |
|
|
993 |
WSASetLastError (0); |
WSASetLastError (0); |
|
|
|
|
leave: |
|
|
closesocket (conn_fd); |
|
|
free_if_alloc (request); |
|
|
return rc; |
|
|
} /* kserver_sendkey */ |
|
994 |
|
|
|
|
|
|
int |
|
|
kserver_search_init (const char * hostname, u16 port, const char * keyid, int * conn_fd) |
|
|
{ |
|
|
int rc, sock_fd; |
|
|
int n=0; |
|
|
char * request = NULL; |
|
|
|
|
|
rc = kserver_connect (hostname, port, &sock_fd); |
|
|
if (rc) { |
|
|
*conn_fd = 0; |
|
|
goto leave; |
|
|
} |
|
|
|
|
|
n=300; |
|
|
request = new char[n+1]; |
|
|
if (!request) |
|
|
BUG (0); |
|
|
|
|
|
if (proxy.host && proxy.user) { |
|
|
_snprintf (request, n, |
|
|
"GET http://%s:%d/pks/lookup?op=index&search=%s HTTP/1.0\r\n" |
|
|
"Proxy-Authorization: Basic %s\r\n\r\n", |
|
|
skip_type_prefix (hostname), port, keyid, proxy.base64_user); |
|
|
} |
|
|
else if (proxy.host) { |
|
|
_snprintf (request, n, |
|
|
"GET http://%s:%d/pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n", |
|
|
skip_type_prefix (hostname), port, keyid); |
|
|
} |
|
|
else { |
|
|
_snprintf (request, n, |
|
|
"GET /pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n", keyid); |
|
|
} |
|
|
|
|
|
log_debug ("kserver_search_init:\r\n%s\r\n", request); |
|
|
|
|
|
if (sock_write (sock_fd, request, strlen (request)) == SOCKET_ERROR) { |
|
|
rc = WPTERR_GENERAL; |
|
|
goto leave; |
|
|
} |
|
|
|
|
|
*conn_fd = sock_fd; |
|
|
|
|
995 |
leave: |
leave: |
996 |
|
closesocket (conn_fd); |
997 |
free_if_alloc (request); |
free_if_alloc (request); |
998 |
return rc; |
return rc; |
999 |
} /* kserver_search_init */ |
} |
1000 |
|
|
1001 |
|
|
1002 |
int |
/* Check keyserver response. */ |
1003 |
|
static int |
1004 |
kserver_search_chkresp (int fd) |
kserver_search_chkresp (int fd) |
1005 |
{ |
{ |
1006 |
char buf[128]; |
char buf[128]; |
1007 |
int n=0; |
int n=0; |
1008 |
|
|
1009 |
/* parse response 'HTTP/1.0 500 OK' */ |
/* parse response 'HTTP/1.0 500 OK' */ |
1010 |
if (sock_getline (fd, buf, 127, &n)) |
if (sock_getline (fd, buf, DIM (buf)-1, &n)) |
1011 |
return WPTERR_KEYSERVER_NOTFOUND; |
return WPTERR_KEYSERVER_NOTFOUND; |
1012 |
|
|
1013 |
log_debug ("kserver_search_chkpresp: %s\r\n", buf); |
log_debug ("kserver_search_chkpresp: %s\r\n", buf); |
1016 |
if (strncmp (buf+(8+1), "200", 3)) |
if (strncmp (buf+(8+1), "200", 3)) |
1017 |
return WPTERR_KEYSERVER_NOTFOUND; |
return WPTERR_KEYSERVER_NOTFOUND; |
1018 |
return 0; |
return 0; |
1019 |
} /* kserver_search_chkresp */ |
} |
1020 |
|
|
1021 |
|
|
1022 |
int |
/* End the keyserver search procedure. */ |
1023 |
kserver_search (int fd, keyserver_key * key) |
void |
1024 |
|
kserver_search_end (int conn_fd) |
1025 |
{ |
{ |
1026 |
char buf[1024], *p; |
log_debug ("kserver_search_end: fd=%d\r\n", conn_fd); |
1027 |
int uidlen, nbytes, pos = 0; |
closesocket (conn_fd); |
1028 |
|
} |
1029 |
|
|
|
log_debug ("keyserver_search:\n"); |
|
|
|
|
|
if (sock_getline (fd, buf, sizeof (buf) - 1, &nbytes)) |
|
|
return WPTERR_GENERAL; |
|
|
|
|
|
log_debug ("%s\n", buf); |
|
|
|
|
|
if (!strncmp (buf, "pub", 3)) { |
|
|
int revoked = strstr (buf, "KEY REVOKED") != NULL? 1 : 0; |
|
|
key->bits = atol (buf+3); |
|
|
p = strchr (buf, '>'); |
|
|
if (!p) |
|
|
goto fail; |
|
|
pos = p - buf + 1; |
|
|
memcpy (key->keyid, buf + pos, 8); |
|
|
key->keyid[8] = '\0'; |
|
|
p = strstr (buf, "</a>"); |
|
|
if (!p) |
|
|
goto fail; |
|
|
pos = p - buf + 5; |
|
|
memcpy (key->date, buf + pos, 10); |
|
|
key->date[10] = '\0'; |
|
|
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; |
|
|
memcpy (key->uid, p, uidlen); |
|
|
key->uid[uidlen] = '\0'; |
|
|
strcat (key->uid, ">"); |
|
|
p = strchr (key->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); |
|
|
} |
|
|
return 0; |
|
|
} |
|
1030 |
|
|
1031 |
fail: |
/* Extract the amount of keys from the info record. */ |
1032 |
key->bits = 0; |
static size_t |
1033 |
memset (key, 0, sizeof *key); |
count_keys_in_response (char *buf) |
1034 |
return 0; |
{ |
1035 |
} /* kserver_search */ |
char *p; |
1036 |
|
int recno = 0; |
1037 |
|
size_t n = 0; |
1038 |
|
|
1039 |
|
/* info:1:4 */ |
1040 |
|
if (strncmp (buf, "info", 4)) |
1041 |
|
return 0; |
1042 |
|
p = strtok (buf, ":"); |
1043 |
|
while (p != NULL) { |
1044 |
|
recno++; |
1045 |
|
if (recno == 3) |
1046 |
|
n = atoi (p); |
1047 |
|
p = strtok (NULL, ":"); |
1048 |
|
} |
1049 |
|
return n; |
1050 |
|
} |
1051 |
|
|
|
static int |
|
|
add_node( keyserver_cfgfile *cfg, const char *entry ) |
|
|
{ |
|
|
keyserver_node *node; |
|
|
char *p = NULL; |
|
|
|
|
|
p = strchr( entry, '=' ); |
|
|
if( p == NULL ) |
|
|
return WPTERR_GENERAL; |
|
|
node = new keyserver_node; |
|
|
if( !node ) |
|
|
BUG( NULL ); |
|
|
|
|
|
memset( node, 0, sizeof *node ); |
|
|
node->used = 1; |
|
|
|
|
|
node->host.used = 1; |
|
|
node->host.proto = proto_from_URL( entry ); |
|
|
memcpy( node->host.name, entry+7, p-entry-7 ); |
|
|
node->next = cfg->list; |
|
|
cfg->list = node; |
|
|
|
|
|
return 0; |
|
|
} /* add_node */ |
|
1052 |
|
|
1053 |
|
/* Start the keyserver search. |
1054 |
|
Connect to host @hostname and port @port. |
1055 |
|
The pattern are given in @pattern. |
1056 |
|
The socket is returned in @conn_fd and @nkeys contains |
1057 |
|
the amount of keys which were found. */ |
1058 |
|
int |
1059 |
|
kserver_search_begin (const char *hostname, WORD port, |
1060 |
|
const char *pattern, int *conn_fd, size_t *nkeys) |
1061 |
|
{ |
1062 |
|
|
1063 |
void |
StringBuffer req; |
1064 |
kserver_change_proxy( keyserver_proxy_ctx *ctx ) |
const char *reqbuf; |
1065 |
{ |
char *enc_patt = NULL; |
1066 |
proxy.port = ctx->port; |
char status[128]; |
1067 |
free_if_alloc (proxy.host); |
int rc, sock_fd; |
1068 |
proxy.host = ctx->host? m_strdup( ctx->host ) : NULL; |
int nread; |
1069 |
free_if_alloc( proxy.pass ); |
|
1070 |
proxy.pass = ctx->pass? m_strdup( ctx->pass ) : NULL; |
*conn_fd = 0; |
|
free_if_alloc( proxy.user ); |
|
|
proxy.user = ctx->user? m_strdup( ctx->user ) : NULL; |
|
|
set_reg_proxy_prefs( proxy.host, proxy.port, proxy.user, proxy.pass ); |
|
|
} /* kserver_change_proxy */ |
|
1071 |
|
|
1072 |
|
rc = kserver_connect (hostname, port, &sock_fd); |
1073 |
|
if (rc) |
1074 |
|
goto leave; |
1075 |
|
|
1076 |
int |
enc_patt = URL_encode (pattern, strlen (pattern), NULL); |
1077 |
kserver_read_config( const char *fname, keyserver_cfgfile **ret_cfg ) |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
1078 |
{ |
req = req + "GET http://" + skip_type_prefix (hostname) + ":" + port + |
1079 |
FILE *fp; |
"/pks/lookup?options=mr&op=index&search=" + enc_patt + |
1080 |
keyserver_cfgfile *cfg; |
" HTTP/1.0\r\n" + |
1081 |
char buf[256]; |
"Host: " + skip_type_prefix (hostname) + ":" + port+ "\r\n"+ |
1082 |
int rc = 0; |
"Connection: close\r\n" + |
1083 |
|
"Proxy-Authorization: Basic " + proxy.base64_user + "\r\n\r\n"; |
1084 |
*ret_cfg = NULL; |
} |
1085 |
fp = fopen( fname, "rb" ); |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
1086 |
if( fp == NULL ) |
req = req + "GET http://" + skip_type_prefix (hostname) + ":" + port + |
1087 |
return WPTERR_FILE_OPEN; |
"/pks/lookup?options=mr&op=index&search=" + enc_patt + |
1088 |
|
" HTTP/1.0\r\n"+ |
1089 |
cfg = new keyserver_cfgfile; |
"Host: " + skip_type_prefix (hostname) + ":" + port+ "\r\n"+ |
1090 |
if ( !cfg ) |
"Connection: close\r\n"+ |
1091 |
BUG( NULL ); |
"\r\n"; |
1092 |
memset( cfg, 0, sizeof *cfg ); |
} |
1093 |
|
else { |
1094 |
while ( !feof( fp ) ) { |
req = req + "GET /pks/lookup?options=mr&op=index&search=" + |
1095 |
if ( fgets( buf, sizeof(buf)-1, fp ) == NULL ) |
enc_patt + " HTTP/1.0\r\n" + |
1096 |
break; |
"Host: " + skip_type_prefix (hostname) + ":" + port+ "\r\n"+ |
1097 |
if ( *buf == '\r' || *buf == '\n' || *buf == '#' ) |
"Connection: close\r\n"+ |
1098 |
continue; |
"\r\n"; |
|
buf[strlen(buf) -2] = '\0'; |
|
|
if ( !strncmp( buf, "use_proxy=", 10 ) ) { |
|
|
char *p = strchr(buf, ':'); |
|
|
if (!p) |
|
|
continue; |
|
|
cfg->proxy.port = atol( buf+(p-buf+1) ); |
|
|
buf[p-buf] = '\0'; |
|
|
cfg->proxy.host = m_strdup( buf+10 ); |
|
|
} |
|
|
else if ( !strncmp( buf, "proxy_user=", 11 ) ) |
|
|
cfg->proxy.user = m_strdup( buf+11 ); |
|
|
else if ( !strncmp( buf, "proxy_pass=", 11 ) ) |
|
|
cfg->proxy.pass = m_strdup( buf+11 ); |
|
|
if ( !strncmp( buf, "http://", 7 ) |
|
|
|| !strncmp( buf, "hkp://", 6 ) |
|
|
|| !strncmp( buf, "ldap://", 7 ) ) { |
|
|
add_node( cfg, buf ); |
|
|
cfg->nlist++; |
|
|
} |
|
1099 |
} |
} |
1100 |
|
|
1101 |
fclose( fp ); |
log_debug ("kserver_search_begin:\r\n%s\r\n", req.getBuffer ()); |
1102 |
*ret_cfg = cfg; |
reqbuf = req.getBuffer (); |
1103 |
|
if (sock_write (sock_fd, reqbuf, strlen (reqbuf)) == SOCKET_ERROR) { |
1104 |
|
rc = WPTERR_GENERAL; |
1105 |
|
goto leave; |
1106 |
|
} |
1107 |
|
|
1108 |
return rc; |
rc = kserver_search_chkresp (sock_fd); |
1109 |
} /* kserver_read_config */ |
if (rc) { |
1110 |
|
closesocket (sock_fd); |
1111 |
|
sock_fd = 0; |
1112 |
|
goto leave; |
1113 |
|
} |
1114 |
|
|
1115 |
|
/* Skip all lines until we reach the "info:" record. */ |
1116 |
|
for (;;) { |
1117 |
|
if (sock_getline (sock_fd, status, DIM (status)-1, &nread)) { |
1118 |
|
log_debug ("kserver_search_begin: retrieving status line failed.\r\n"); |
1119 |
|
closesocket (sock_fd); |
1120 |
|
sock_fd = 0; |
1121 |
|
rc = WPTERR_GENERAL; |
1122 |
|
break; |
1123 |
|
} |
1124 |
|
if (!strncmp (status, "info:", 5)) |
1125 |
|
break; |
1126 |
|
} |
1127 |
|
|
1128 |
int |
if (!rc) |
1129 |
kserver_write_config( const char * fname, keyserver_cfgfile * cfg ) |
*nkeys = count_keys_in_response (status); |
1130 |
{ |
*conn_fd = sock_fd; |
1131 |
|
|
1132 |
return 0; |
leave: |
1133 |
|
free_if_alloc (enc_patt); |
1134 |
|
return rc; |
1135 |
} |
} |
1136 |
|
|
1137 |
|
|
1138 |
void |
/* Parse a single pub record returned by the keyserver. */ |
1139 |
kserver_cfgfile_release( keyserver_cfgfile *cfg ) |
static void |
1140 |
{ |
parse_pub_record (keyserver_key_s *key, char *buf) |
1141 |
keyserver_node *k, *k2; |
{ |
1142 |
|
enum pub_rec_t {ID=1, KEYID, ALGO, SIZE, CREATE, EXPIRE}; |
1143 |
|
char *p; |
1144 |
|
int recno = 0; |
1145 |
|
int off = 0; |
1146 |
|
|
1147 |
|
/* pub:{BF3DF9B4, ED4681C9BF3DF9B4, FPR}:17:1024:925411133:: */ |
1148 |
|
p = strtok (buf, ":"); |
1149 |
|
while (p != NULL) { |
1150 |
|
recno++; |
1151 |
|
switch (recno) { |
1152 |
|
case ID: |
1153 |
|
break; |
1154 |
|
|
1155 |
|
case KEYID: |
1156 |
|
/* If for any reason the returned record actually contains |
1157 |
|
a fingerprint instead of a key ID, we truncate the fpr. */ |
1158 |
|
off = strlen (p) == 40? 32 : 0; |
1159 |
|
free_if_alloc (key->keyid); |
1160 |
|
key->keyid = m_strdup (p+off); |
1161 |
|
break; |
1162 |
|
|
1163 |
|
case ALGO: |
1164 |
|
key->algo = atoi (p); |
1165 |
|
break; |
1166 |
|
|
1167 |
|
case SIZE: |
1168 |
|
key->bits = atoi (p); |
1169 |
|
break; |
1170 |
|
|
1171 |
|
case CREATE: |
1172 |
|
key->creation = strtoul (p, NULL, 10); |
1173 |
|
break; |
1174 |
|
|
1175 |
proxy_release( &cfg->proxy ); |
case EXPIRE: |
1176 |
for( k = cfg->list; cfg->nlist--; k = k2 ) { |
key->expires = strtoul (p, NULL, 10); |
1177 |
k2 = k->next; |
break; |
1178 |
free_if_alloc( k ); |
} |
1179 |
|
p = strtok (NULL, ":"); |
1180 |
} |
} |
1181 |
free_if_alloc( cfg ); |
} |
|
} /* kserver_cfgfile_release */ |
|
1182 |
|
|
1183 |
|
|
1184 |
void |
/* Parse a single user id returned by the keyserver. */ |
1185 |
proxy_release( keyserver_proxy_ctx *ctx ) |
static void |
1186 |
|
parse_uid_record (keyserver_key_s *key, char *buf) |
1187 |
{ |
{ |
1188 |
free_if_alloc( ctx->host ); |
enum uid_rec_t {ID=1, UID, CREATE, EXPIRE}; |
1189 |
free_if_alloc( ctx->pass ); |
keyserver_uid_s *u, *n; |
1190 |
free_if_alloc( ctx->user ); |
char *p, *raw; |
1191 |
} /* proxy_release */ |
int recno = 0; |
1192 |
|
|
1193 |
|
/* uid:Timo Schulz <[email protected]>:1138440360:: */ |
1194 |
int |
u = new keyserver_uid_s; |
1195 |
ldap_recvkey (const char * host, const char * keyid, char * key, int maxkeylen) |
memset (u, 0, sizeof *u); |
1196 |
{ |
|
1197 |
const char *s; |
p = strtok (buf, ":"); |
1198 |
char *ksprg = NULL, *p = NULL; |
while (p != NULL) { |
1199 |
char temp[512], outf[512]; |
recno++; |
1200 |
FILE * inp; |
|
1201 |
int rc, start_key = 0; |
switch (recno) { |
1202 |
STARTUPINFO si; |
case ID: |
|
PROCESS_INFORMATION pi; |
|
|
|
|
|
p = get_gnupg_path (); |
|
|
ksprg = new char[strlen (p) + 1 + 128]; |
|
|
if (!ksprg) |
|
|
BUG (0); |
|
|
strcpy (ksprg, p); |
|
|
strcat (ksprg, "\\"); |
|
|
strcat (ksprg, "gpgkeys_ldap.exe"); |
|
|
free_if_alloc (p); |
|
|
if (file_exist_check (ksprg)) { |
|
|
log_box ( "LDAP Keyserver Plugin", MB_ERR, "Could not find LDAP keyserver module!"); |
|
|
rc = -1; |
|
|
goto leave; |
|
|
} |
|
|
GetTempPath (sizeof (temp)-1, temp); |
|
|
strcpy (outf, temp); |
|
|
strcat (outf, keyid); |
|
|
strcat (outf, ".out"); |
|
|
strcat (temp, keyid); |
|
|
inp = fopen (temp, "w+b"); |
|
|
if( !inp ) { |
|
|
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", temp, |
|
|
winpt_strerror (WPTERR_FILE_OPEN)); |
|
|
rc = -1; |
|
|
goto leave; |
|
|
} |
|
|
fprintf (inp, |
|
|
"VERSION 0\n" |
|
|
"HOST %s\n" |
|
|
"OPTION verbose\n" |
|
|
"COMMAND GET\n" |
|
|
"\n" |
|
|
"%s\n", host? skip_type_prefix (host): "64.94.85.200", keyid); |
|
|
fclose (inp); |
|
|
|
|
|
memset (&si, 0, sizeof (si)); |
|
|
si.cb = sizeof (si); |
|
|
si.dwFlags = STARTF_USESHOWWINDOW; |
|
|
si.wShowWindow = SW_HIDE; |
|
|
memset (&pi, 0, sizeof (pi)); |
|
|
p = new char[strlen (ksprg) + strlen (temp) + strlen (outf) + 32]; |
|
|
sprintf (p, "%s -o %s %s", ksprg, outf, temp); |
|
|
rc = CreateProcess (NULL, p, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi); |
|
|
if (!rc) { |
|
|
log_box ("LDAP Keyserver Plugin", MB_ERR, "Could not spawn process"); |
|
|
rc = -1; |
|
|
goto leave; |
|
|
} |
|
|
CloseHandle (pi.hThread); |
|
|
WaitForSingleObject (pi.hProcess, INFINITE); |
|
|
CloseHandle (pi.hProcess); |
|
|
|
|
|
inp = fopen (outf, "rb"); |
|
|
if (!inp) { |
|
|
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf, |
|
|
winpt_strerror (WPTERR_FILE_OPEN)); |
|
|
rc = -1; |
|
|
goto leave; |
|
|
} |
|
|
memset (key, 0, maxkeylen); |
|
|
while( !feof( inp ) ) { |
|
|
s = fgets( temp, sizeof (temp)-1, inp ); |
|
|
if( !s ) |
|
1203 |
break; |
break; |
1204 |
if( !start_key && strstr( s, "KEY" ) && strstr( s, "BEGIN" ) ) { |
|
1205 |
start_key = 1; |
case UID: |
1206 |
continue; |
unhexify_buffer (p, &raw); |
1207 |
} |
u->uid = utf8_to_native (raw); |
1208 |
if( !start_key ) |
free_if_alloc (raw); |
|
continue; |
|
|
strcat( key, temp ); |
|
|
maxkeylen -= strlen( temp ); |
|
|
if( maxkeylen < 0 || (strstr( s, "KEY" ) && strstr( s, "END" )) ) |
|
1209 |
break; |
break; |
|
} |
|
|
fclose( inp ); |
|
1210 |
|
|
1211 |
leave: |
case CREATE: |
1212 |
if( !strlen( key ) ) |
u->creation = strtoul (p, NULL, 10); |
1213 |
rc = WPTERR_WINSOCK_RECVKEY; |
break; |
|
free_if_alloc( p ); |
|
|
free_if_alloc( ksprg ); |
|
|
return rc; |
|
|
} /* ldap_recvkey */ |
|
1214 |
|
|
1215 |
|
case EXPIRE: |
1216 |
|
u->expires = strtoul (p, NULL, 10); |
1217 |
|
break; |
1218 |
|
} |
1219 |
|
|
1220 |
|
p = strtok (NULL, ":"); |
1221 |
|
} |
1222 |
|
if (!key->uids) |
1223 |
|
key->uids = u; |
1224 |
|
else { |
1225 |
|
for (n = key->uids; n->next; n=n->next) |
1226 |
|
; |
1227 |
|
n->next = u; |
1228 |
|
} |
1229 |
|
} |
1230 |
|
|
1231 |
|
|
1232 |
|
/* Peek the next 3 bytes to check if the next line |
1233 |
|
would be a new "pub" record. In this case, return |
1234 |
|
-1 as an EOF indication, 0 otherwise. */ |
1235 |
static int |
static int |
1236 |
finger_readline (int fd, char * buf, int nbytes) |
is_key_eof (int fd) |
1237 |
{ |
{ |
1238 |
char c = 0; |
char buf[64]; |
1239 |
int n, pos = 0; |
int n; |
1240 |
|
|
1241 |
while (nbytes > 0) { |
n = recv (fd, buf, 3, MSG_PEEK); |
1242 |
n = recv (fd, &c, 1, 0); |
if (n < 3 || strncmp (buf, "pub", 3)) |
1243 |
if (n <= 0) |
return 0; |
1244 |
break; |
return -1; |
|
if (c == '\n') |
|
|
break; |
|
|
buf[pos++] = c; |
|
|
nbytes--; |
|
|
} |
|
|
if (nbytes > 0) |
|
|
buf[pos++] = '\0'; |
|
|
if (n <= 0) |
|
|
pos = n; |
|
|
return pos; |
|
1245 |
} |
} |
1246 |
|
|
1247 |
|
|
1248 |
|
/* Return the next key from the search response. */ |
1249 |
int |
int |
1250 |
finger_recvkey (const char * host, const char * user, char * key, int maxkeylen) |
kserver_search_next (int fd, keyserver_key_s **r_key) |
1251 |
{ |
{ |
1252 |
struct hostent * hp; |
keyserver_uid_s *uid, *u = NULL; |
1253 |
struct sockaddr_in saddr; |
keyserver_key_s *key; |
1254 |
char buf[128]; |
char buf[512]; |
1255 |
int fd, nread; |
int n = 0; |
1256 |
int start_key = 0; |
long max = 0; |
1257 |
int rc=0; |
|
1258 |
|
log_debug ("keyserver_search_next:\r\n"); |
1259 |
fd = socket (PF_INET, SOCK_STREAM, 0); |
*r_key = NULL; |
1260 |
if (fd == -1) |
|
1261 |
return WPTERR_WINSOCK_SOCKET; |
key = new keyserver_key_s; |
1262 |
hp = gethostbyname (skip_type_prefix (host)); |
memset (key, 0, sizeof *key); |
1263 |
if (!hp) { |
for (;;) { |
1264 |
closesocket (fd); |
if (sock_getline (fd, buf, DIM (buf)-1, &n)) |
1265 |
return WPTERR_WINSOCK_RESOLVE; |
break; |
1266 |
|
/*log_debug ("record: '%s'\r\n", buf); */ |
1267 |
|
if (!strncmp (buf, "pub", 3)) |
1268 |
|
parse_pub_record (key, buf); |
1269 |
|
else |
1270 |
|
parse_uid_record (key, buf); |
1271 |
|
if (is_key_eof (fd)) |
1272 |
|
break; |
1273 |
} |
} |
1274 |
|
|
1275 |
memset (&saddr, 0, sizeof (saddr)); |
/* the uid with the newest self sig is used as the |
1276 |
saddr.sin_family = AF_INET; |
primary user id. */ |
1277 |
saddr.sin_port = htons (FINGER_PORT); |
for (uid = key->uids; uid; uid = uid->next) { |
1278 |
saddr.sin_addr = *(struct in_addr *)hp->h_addr; |
if (uid->creation > max) { |
1279 |
if (connect (fd, (struct sockaddr *)&saddr, sizeof (saddr))) { |
max = uid->creation; |
1280 |
closesocket (fd); |
u = uid; |
|
return WPTERR_WINSOCK_CONNECT; |
|
|
} |
|
|
send (fd, user, strlen (user), 0); |
|
|
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; |
|
|
strcat (buf, "\n"); |
|
|
if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) { |
|
|
strcat (key, buf); |
|
|
start_key = 1; |
|
|
maxkeylen -= nread; |
|
|
} |
|
|
else if (strstr (buf, "END PGP PUBLIC KEY BLOCK" ) && start_key) { |
|
|
strcat (key, buf); |
|
|
start_key--; |
|
|
maxkeylen -= nread; |
|
|
break; |
|
|
} |
|
|
else if (start_key) { |
|
|
strcat (key, buf); |
|
|
maxkeylen -= nread; |
|
1281 |
} |
} |
1282 |
} |
} |
1283 |
closesocket (fd); |
|
1284 |
if (start_key != 0) |
key->main_uid = u? u : key->uids; |
1285 |
rc = WPTERR_WINSOCK_RECVKEY; |
*r_key = key; |
1286 |
return rc; |
return 0; |
1287 |
|
} |
1288 |
|
|
1289 |
|
|
1290 |
|
/* Release the keyserver key @key and all its elements. */ |
1291 |
|
void |
1292 |
|
kserver_release_key (keyserver_key_s *key) |
1293 |
|
{ |
1294 |
|
keyserver_uid_s *u; |
1295 |
|
|
1296 |
|
while (key->uids) { |
1297 |
|
u = key->uids->next; |
1298 |
|
free_if_alloc (key->uids->uid); |
1299 |
|
free_if_alloc (key->uids); |
1300 |
|
key->uids = u; |
1301 |
|
} |
1302 |
|
free_if_alloc (key->keyid); |
1303 |
|
free_if_alloc (key); |
1304 |
|
} |
1305 |
|
|
1306 |
|
|
1307 |
|
/* Release mbmers in the proxy context @ctx. */ |
1308 |
|
void |
1309 |
|
kserver_proxy_release (keyserver_proxy_t ctx) |
1310 |
|
{ |
1311 |
|
free_if_alloc (ctx->host); |
1312 |
|
free_if_alloc (ctx->pass); |
1313 |
|
free_if_alloc (ctx->user); |
1314 |
|
ctx->port = ctx->proto = 0; |
1315 |
} |
} |
1316 |
|
|
1317 |
|
|
1318 |
|
/* Check if the given name @name is a valid inernet address |
1319 |
|
which means an dotted IP or a valid DNS name. |
1320 |
|
Return value: 0 on success. */ |
1321 |
int |
int |
1322 |
check_IP_or_hostname (const char *name) |
check_inet_address (const char *addr) |
1323 |
{ |
{ |
1324 |
const char *not_allowed = "=!�$%&@#*~\\/}][{<>|,;:'"; |
const char *not_allowed = "=!�$%&@#*~\\/}][{<>|,;:'"; |
|
size_t i, j; |
|
1325 |
|
|
1326 |
for (i=0; i < strlen (name); i++) { |
for (size_t i=0; i < strlen (addr); i++) { |
1327 |
for (j =0; j < strlen (not_allowed); j++) { |
if (strchr (not_allowed, addr[i])) |
1328 |
if (name[i] == not_allowed[j]) |
return -1; |
|
return -1; |
|
|
} |
|
1329 |
} |
} |
1330 |
return 0; |
return 0; |
1331 |
} |
} |
1332 |
|
|
1333 |
|
|
1334 |
|
/* Split the URL @r_keyserver into the host and the port |
1335 |
|
part if possible. */ |
1336 |
|
gpgme_error_t |
1337 |
|
parse_keyserver_url (char **r_keyserver, unsigned short *r_port) |
1338 |
|
{ |
1339 |
|
char *p; |
1340 |
|
char *url = *r_keyserver; |
1341 |
|
int off; |
1342 |
|
|
1343 |
|
/* no port is given so use the default port. */ |
1344 |
|
p = strrchr (url, ':'); |
1345 |
|
if (p == strchr (url, ':')) { |
1346 |
|
*r_port = HKP_PORT; |
1347 |
|
return 0; |
1348 |
|
} |
1349 |
|
|
1350 |
|
if (url[(p-url)-1] == '/') /* remove / in .de/:11371 */ |
1351 |
|
off = 1; |
1352 |
|
else |
1353 |
|
off = 0; |
1354 |
|
|
1355 |
|
*r_keyserver = substr (url, 0, (p-url)-off); |
1356 |
|
*r_port = atoi (url+(p-url)+1); |
1357 |
|
free_if_alloc (url); |
1358 |
|
return 0; |
1359 |
|
} |