1 |
werner |
36 |
/* wptKeyserver.cpp - W32 Keyserver Access |
2 |
|
|
* Copyright (C) 2000-2005 Timo Schulz |
3 |
|
|
* Copyright (C) 2001 Marco Cunha |
4 |
|
|
* |
5 |
|
|
* This file is part of WinPT. |
6 |
|
|
* |
7 |
|
|
* WinPT is free software; you can redistribute it and/or |
8 |
|
|
* modify it under the terms of the GNU General Public License |
9 |
|
|
* as published by the Free Software Foundation; either version 2 |
10 |
|
|
* of the License, or (at your option) any later version. |
11 |
|
|
* |
12 |
|
|
* WinPT is distributed in the hope that it will be useful, |
13 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 |
|
|
* General Public License for more details. |
16 |
|
|
* |
17 |
|
|
* You should have received a copy of the GNU General Public License |
18 |
|
|
* along with WinPT; if not, write to the Free Software Foundation, |
19 |
|
|
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
|
|
*/ |
21 |
|
|
|
22 |
|
|
#ifdef HAVE_CONFIG_H |
23 |
|
|
#include <config.h> |
24 |
|
|
#endif |
25 |
|
|
|
26 |
|
|
#include <windows.h> |
27 |
twoaday |
150 |
#include <shlobj.h> |
28 |
werner |
36 |
#include <stdio.h> |
29 |
|
|
#include <sys/stat.h> |
30 |
|
|
#include <ctype.h> |
31 |
|
|
|
32 |
|
|
#include "wptKeyserver.h" |
33 |
|
|
#include "wptErrors.h" |
34 |
|
|
#include "wptTypes.h" |
35 |
|
|
#include "wptNLS.h" |
36 |
|
|
#include "wptW32API.h" |
37 |
|
|
#include "wptGPG.h" |
38 |
|
|
#include "wptRegistry.h" |
39 |
|
|
|
40 |
twoaday |
119 |
#define net_errno ((int)WSAGetLastError ()) |
41 |
|
|
|
42 |
werner |
36 |
static char base64code[] = |
43 |
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
44 |
|
|
|
45 |
|
|
keyserver server[MAX_KEYSERVERS] = {0}; |
46 |
twoaday |
180 |
keyserver_proxy_s proxy = {0}; |
47 |
twoaday |
119 |
static const char *server_list[] = { |
48 |
werner |
36 |
"hkp://wwwkeys.nl.pgp.net", |
49 |
|
|
"hkp://wwwkeys.pl.pgp.net", |
50 |
|
|
"hkp://wwwkeys.at.pgp.net", |
51 |
|
|
"hkp://wwwkeys.ch.pgp.net", |
52 |
|
|
"hkp://wwwkeys.de.pgp.net", |
53 |
|
|
"hkp://wwwkeys.dk.pgp.net", |
54 |
|
|
"hkp://wwwkeys.cz.pgp.net", |
55 |
|
|
"hkp://wwwkeys.es.pgp.net", |
56 |
|
|
"hkp://wwwkeys.eu.pgp.net", |
57 |
|
|
"hkp://wwwkeys.uk.pgp.net", |
58 |
|
|
"hkp://wwwkeys.us.pgp.net", |
59 |
|
|
"hkp://subkeys.pgp.net", |
60 |
|
|
"ldap://keyserver.pgp.com", |
61 |
|
|
NULL |
62 |
|
|
}; |
63 |
|
|
|
64 |
|
|
|
65 |
twoaday |
119 |
static char hkp_errmsg[1024]; /* Holds the error message from the server */ |
66 |
|
|
static int hkp_err = 0; /* != 0 indicates an error occurred. */ |
67 |
|
|
static DWORD conf_timestamp = 0;/* timestamp of the configuration fiele. */ |
68 |
|
|
|
69 |
twoaday |
147 |
/* Default keyserver and port. */ |
70 |
|
|
char *default_keyserver = NULL; |
71 |
|
|
WORD default_keyserver_port = 0; |
72 |
twoaday |
119 |
|
73 |
|
|
|
74 |
twoaday |
180 |
/* Basic64 encode the input @inbuf to @outbuf. */ |
75 |
werner |
36 |
static void |
76 |
twoaday |
180 |
base64_encode (const char *inbuf, char *outbuf) |
77 |
werner |
36 |
{ |
78 |
twoaday |
180 |
int index = 0, temp = 0, res = 0; |
79 |
|
|
int i = 0, inputlen = 0, len = 0; |
80 |
werner |
36 |
|
81 |
twoaday |
180 |
inputlen = strlen (inbuf); |
82 |
|
|
for (i = 0; i < inputlen; i++) { |
83 |
werner |
36 |
res = temp; |
84 |
twoaday |
180 |
res = (res << 8) | (inbuf[i] & 0xFF); |
85 |
werner |
36 |
switch (index++) { |
86 |
twoaday |
180 |
case 0: |
87 |
|
|
outbuf[len++] = base64code[res >> 2 & 0x3F]; |
88 |
|
|
res &= 0x3; |
89 |
|
|
break; |
90 |
|
|
case 1: |
91 |
|
|
outbuf[len++] = base64code[res >> 4 & 0x3F]; |
92 |
|
|
res &= 0xF; |
93 |
|
|
break; |
94 |
|
|
case 2: |
95 |
|
|
outbuf[len++] = base64code[res >> 6 & 0x3F]; |
96 |
|
|
outbuf[len++] = base64code[res & 0x3F]; |
97 |
|
|
res = index = 0; |
98 |
|
|
break; |
99 |
werner |
36 |
} |
100 |
|
|
temp = res; |
101 |
|
|
} |
102 |
|
|
|
103 |
twoaday |
180 |
if (index == 2) { |
104 |
|
|
outbuf[len++] = base64code[temp << 2 & 0x3F]; |
105 |
|
|
outbuf[len++] = '='; |
106 |
werner |
36 |
} |
107 |
twoaday |
180 |
else if (index == 1) { |
108 |
|
|
outbuf[len++] = base64code[temp << 4 & 0x3F]; |
109 |
|
|
outbuf[len++] = '='; |
110 |
|
|
outbuf[len++] = '='; |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
outbuf[len] = '\0'; |
114 |
twoaday |
119 |
} |
115 |
werner |
36 |
|
116 |
|
|
|
117 |
|
|
/* Skip the URL schema and return only the host part of it. */ |
118 |
twoaday |
119 |
static const char* |
119 |
|
|
skip_type_prefix (const char *hostname) |
120 |
werner |
36 |
{ |
121 |
|
|
if (hostname && !strncmp (hostname, "http://", 7)) |
122 |
|
|
hostname += 7; |
123 |
|
|
else if (hostname && !strncmp (hostname, "hkp://", 6)) |
124 |
|
|
hostname += 6; |
125 |
|
|
else if (hostname && !strncmp (hostname, "finger://", 9)) |
126 |
|
|
hostname += 9; |
127 |
|
|
else if (hostname && !strncmp (hostname, "ldap://", 7)) |
128 |
|
|
hostname += 7; |
129 |
|
|
return hostname; |
130 |
|
|
} |
131 |
|
|
|
132 |
|
|
|
133 |
|
|
/* Check that the keyserver response indicates an OK. |
134 |
|
|
Return 0 on success. */ |
135 |
|
|
static int |
136 |
|
|
check_hkp_response (const char *resp, int recv) |
137 |
twoaday |
119 |
{ |
138 |
|
|
char *p, *end; |
139 |
werner |
36 |
int ec, len; |
140 |
|
|
|
141 |
|
|
ec = recv ? WPTERR_WINSOCK_RECVKEY : WPTERR_WINSOCK_SENDKEY; |
142 |
|
|
if (!strstr (resp, "HTTP/1.0 200 OK") && |
143 |
|
|
!strstr (resp, "HTTP/1.1 200 OK")) /* http error */ |
144 |
|
|
return ec; |
145 |
|
|
if (strstr (resp, "Public Key Server -- Error") |
146 |
|
|
|| strstr (resp, "Public Key Server -- Error") |
147 |
|
|
|| strstr (resp, "No matching keys in database")) { |
148 |
|
|
p = strstr (resp, "<p>"); |
149 |
|
|
if (p && strlen (p) < sizeof (hkp_errmsg)) { |
150 |
|
|
end = strstr (p, "</p>"); |
151 |
|
|
len = end? (end - p + 1) : strlen (p); |
152 |
twoaday |
119 |
memset (hkp_errmsg, 0, sizeof (hkp_errmsg)); |
153 |
werner |
36 |
strncpy (hkp_errmsg, p, len); |
154 |
|
|
hkp_err = 1; |
155 |
|
|
} |
156 |
|
|
return ec; |
157 |
|
|
} |
158 |
|
|
return 0; |
159 |
|
|
} |
160 |
|
|
|
161 |
|
|
|
162 |
|
|
/* Read a single line (\r\n) from a socket. |
163 |
|
|
Return 0 on success. */ |
164 |
|
|
static int |
165 |
|
|
sock_getline (int fd, char *buf, int buflen, int *nbytes) |
166 |
|
|
{ |
167 |
|
|
char ch; |
168 |
twoaday |
180 |
int nread = 0; |
169 |
werner |
36 |
|
170 |
|
|
if (nbytes) |
171 |
|
|
*nbytes = 0; |
172 |
|
|
*buf = 0; |
173 |
|
|
while (recv (fd, &ch, 1, 0) > 0) { |
174 |
|
|
*buf++ = ch; |
175 |
twoaday |
180 |
nread++; |
176 |
|
|
if (ch == '\r') |
177 |
|
|
continue; /* remove this char. */ |
178 |
|
|
if (ch == '\n' || nread == (buflen - 1)) { |
179 |
werner |
36 |
*buf = 0; |
180 |
|
|
if (nbytes) |
181 |
twoaday |
180 |
*nbytes = nread; |
182 |
werner |
36 |
return 0; |
183 |
|
|
} |
184 |
|
|
} |
185 |
|
|
|
186 |
|
|
return -1; |
187 |
|
|
} |
188 |
|
|
|
189 |
|
|
|
190 |
|
|
/* Perform a select() on the given fd to find out |
191 |
|
|
is there is data for reading. Wait at least @nsecs seconds. */ |
192 |
|
|
static int |
193 |
|
|
sock_select (int fd, int nsecs) |
194 |
|
|
{ |
195 |
|
|
FD_SET rfd; |
196 |
|
|
timeval tv = {nsecs, 0}; |
197 |
|
|
|
198 |
|
|
FD_ZERO (&rfd); |
199 |
|
|
FD_SET (fd, &rfd); |
200 |
twoaday |
119 |
if (select (fd + 1, &rfd, NULL, NULL, &tv) == SOCKET_ERROR) { |
201 |
|
|
log_debug ("sock_select: select() failed ec=%d.\r\n", net_errno); |
202 |
werner |
36 |
return SOCKET_ERROR; |
203 |
twoaday |
119 |
} |
204 |
werner |
36 |
if (FD_ISSET (fd, &rfd)) |
205 |
|
|
return 1; |
206 |
|
|
return 0; |
207 |
|
|
} |
208 |
|
|
|
209 |
|
|
|
210 |
twoaday |
119 |
/* Read from a socket @fd to buffer @buf with a fixed timeout |
211 |
|
|
of 10 seconds. The amount of bytes which were read are |
212 |
|
|
returned in @nbytes. |
213 |
werner |
36 |
Return value: 0 on success. */ |
214 |
|
|
static int |
215 |
|
|
sock_read (int fd, char *buf, int buflen, int *nbytes) |
216 |
|
|
{ |
217 |
twoaday |
119 |
DWORD nread; |
218 |
werner |
36 |
int nleft = buflen; |
219 |
|
|
int rc, n = 0; |
220 |
|
|
|
221 |
|
|
while (nleft > 0) { |
222 |
|
|
if (n >= 10) |
223 |
|
|
return WPTERR_WINSOCK_TIMEOUT; |
224 |
|
|
rc = sock_select (fd, 1); |
225 |
|
|
if (rc == SOCKET_ERROR) |
226 |
twoaday |
119 |
return rc; |
227 |
|
|
else if (!rc) |
228 |
werner |
36 |
n++; |
229 |
|
|
else { |
230 |
|
|
nread = recv (fd, buf, nleft, 0); |
231 |
twoaday |
119 |
if (nread == SOCKET_ERROR) { |
232 |
|
|
log_debug ("sock_read: recv() failed ec=%d.\r\n", net_errno); |
233 |
werner |
36 |
return SOCKET_ERROR; |
234 |
twoaday |
119 |
} |
235 |
werner |
36 |
else if (!nread) |
236 |
|
|
break; |
237 |
|
|
nleft -= nread; |
238 |
|
|
buf += nread; |
239 |
|
|
} |
240 |
|
|
} |
241 |
|
|
if (nbytes) |
242 |
|
|
*nbytes = buflen - nleft; |
243 |
|
|
|
244 |
|
|
return 0; |
245 |
|
|
} |
246 |
|
|
|
247 |
twoaday |
147 |
/* Read much data as possible from the socket @fd and |
248 |
|
|
return the data in @buffer. Caller must free data. |
249 |
|
|
Return value: 0 on success. */ |
250 |
|
|
int |
251 |
|
|
sock_read_ext (int fd, char **buffer, int *r_bufferlen) |
252 |
|
|
{ |
253 |
|
|
gpgme_data_t dh; |
254 |
|
|
char buf[1024], *p; |
255 |
|
|
size_t n=0; |
256 |
|
|
int nread, rc; |
257 |
werner |
36 |
|
258 |
twoaday |
147 |
gpgme_data_new (&dh); |
259 |
|
|
while (n < 10) { |
260 |
|
|
rc = sock_select (fd, 1); |
261 |
|
|
if (rc == SOCKET_ERROR) { |
262 |
|
|
gpgme_data_release (dh); |
263 |
|
|
return rc; |
264 |
|
|
} |
265 |
|
|
else if (!rc) |
266 |
|
|
n++; |
267 |
|
|
else { |
268 |
|
|
nread = recv (fd, buf, sizeof (buf), 0); |
269 |
|
|
if (nread == SOCKET_ERROR) |
270 |
|
|
return SOCKET_ERROR; |
271 |
|
|
else if (!nread) |
272 |
|
|
break; |
273 |
|
|
gpgme_data_write (dh, buf, nread); |
274 |
|
|
} |
275 |
|
|
} |
276 |
|
|
gpg_data_putc (dh, '\0'); |
277 |
|
|
p = gpgme_data_release_and_get_mem (dh, &n); |
278 |
|
|
*buffer = m_strdup (p); |
279 |
|
|
if (r_bufferlen) |
280 |
|
|
*r_bufferlen = n; |
281 |
|
|
gpgme_free (p); |
282 |
|
|
return 0; |
283 |
|
|
} |
284 |
|
|
|
285 |
|
|
|
286 |
twoaday |
119 |
/* Write the buffer @buf with the length @buflen to a socket @fd. */ |
287 |
werner |
36 |
static int |
288 |
|
|
sock_write (int fd, const char *buf, int buflen) |
289 |
|
|
{ |
290 |
twoaday |
119 |
DWORD nwritten; |
291 |
werner |
36 |
int nleft = buflen; |
292 |
|
|
|
293 |
|
|
while (nleft > 0) { |
294 |
|
|
nwritten = send (fd, buf, nleft, 0); |
295 |
twoaday |
119 |
if (nwritten == SOCKET_ERROR) { |
296 |
|
|
log_debug ("sock_write: send() failed ec=%d\r\n", net_errno); |
297 |
werner |
36 |
return SOCKET_ERROR; |
298 |
twoaday |
119 |
} |
299 |
werner |
36 |
nleft -= nwritten; |
300 |
twoaday |
180 |
buf += nwritten; |
301 |
werner |
36 |
} |
302 |
|
|
|
303 |
|
|
return 0; |
304 |
|
|
} |
305 |
|
|
|
306 |
|
|
|
307 |
twoaday |
119 |
/* Set the default keyserver. */ |
308 |
werner |
36 |
void |
309 |
|
|
set_default_kserver (void) |
310 |
|
|
{ |
311 |
twoaday |
119 |
char *p = get_reg_entry_keyserver ("Default"); |
312 |
werner |
36 |
free_if_alloc (default_keyserver); |
313 |
|
|
default_keyserver = p && *p != ' ' ? p : m_strdup (DEF_HKP_KEYSERVER); |
314 |
|
|
p = get_reg_entry_keyserver ("Default_Port"); |
315 |
|
|
if (p && *p) { |
316 |
twoaday |
119 |
default_keyserver_port = (WORD)strtoul (p, NULL, 10); |
317 |
werner |
36 |
free_if_alloc (p); |
318 |
|
|
} |
319 |
|
|
else |
320 |
|
|
default_keyserver_port = HKP_PORT; |
321 |
|
|
} |
322 |
|
|
|
323 |
|
|
|
324 |
|
|
/* Initialize the Winsock2 interface.*/ |
325 |
|
|
int |
326 |
|
|
wsock_init (void) |
327 |
|
|
{ |
328 |
|
|
WSADATA wsa; |
329 |
|
|
|
330 |
twoaday |
119 |
if (WSAStartup (0x202, &wsa)) { |
331 |
|
|
log_debug ("wsock_init: WSAStartup failed ec=%d\r\n", net_errno); |
332 |
werner |
36 |
return WPTERR_WINSOCK_INIT; |
333 |
twoaday |
119 |
} |
334 |
werner |
36 |
set_default_kserver (); |
335 |
|
|
return 0; |
336 |
|
|
} |
337 |
|
|
|
338 |
|
|
|
339 |
|
|
/* Cleanup the Winsock2 interface. */ |
340 |
|
|
void |
341 |
|
|
wsock_end (void) |
342 |
|
|
{ |
343 |
twoaday |
150 |
char *p; |
344 |
werner |
36 |
int i; |
345 |
|
|
|
346 |
twoaday |
150 |
p = make_special_filename (CSIDL_APPDATA, "winpt\\keyserver.conf", NULL); |
347 |
|
|
kserver_save_conf (p); |
348 |
|
|
free_if_alloc (p); |
349 |
werner |
36 |
free_if_alloc (default_keyserver); |
350 |
|
|
for (i=0; i < MAX_KEYSERVERS; i++) { |
351 |
|
|
if (server[i].used) |
352 |
|
|
free_if_alloc (server[i].name); |
353 |
|
|
} |
354 |
twoaday |
180 |
kserver_proxy_release (&proxy); |
355 |
werner |
36 |
WSACleanup (); |
356 |
|
|
} |
357 |
|
|
|
358 |
|
|
|
359 |
|
|
/* Return a string representation of a winsock error. */ |
360 |
|
|
const char* |
361 |
|
|
wsock_strerror (void) |
362 |
|
|
{ |
363 |
|
|
static char buf[384]; |
364 |
|
|
int ec = WSAGetLastError (); |
365 |
|
|
|
366 |
|
|
switch (ec) { |
367 |
twoaday |
119 |
case WSAENETDOWN: |
368 |
|
|
return _("The network subsystem has failed"); |
369 |
|
|
case WSAHOST_NOT_FOUND: |
370 |
werner |
36 |
return _("Authoritative Answer Host not found"); |
371 |
twoaday |
119 |
case WSAETIMEDOUT: |
372 |
werner |
36 |
return _("The connection has been dropped because of a network failure"); |
373 |
twoaday |
119 |
default: |
374 |
werner |
36 |
_snprintf (buf, sizeof (buf) -1, _("Unknown Winsock error ec=%d"),ec); |
375 |
|
|
return buf; |
376 |
|
|
} |
377 |
|
|
return NULL; |
378 |
|
|
} |
379 |
|
|
|
380 |
|
|
|
381 |
|
|
/* Return the last keyserver error as a string. */ |
382 |
|
|
const char* |
383 |
|
|
kserver_strerror (void) |
384 |
|
|
{ |
385 |
|
|
if (hkp_err) |
386 |
|
|
return hkp_errmsg; |
387 |
|
|
return NULL; |
388 |
|
|
} |
389 |
|
|
|
390 |
|
|
|
391 |
|
|
/* Read a keyserver from the list at index @idx. |
392 |
|
|
Return value: keyserver name at the given position. */ |
393 |
|
|
const char* |
394 |
twoaday |
119 |
kserver_get_hostname (int idx, int type, WORD *port) |
395 |
werner |
36 |
{ |
396 |
|
|
if (type == -1) { |
397 |
|
|
*port = default_keyserver_port; |
398 |
|
|
return default_keyserver; |
399 |
|
|
} |
400 |
|
|
else if (!type && idx < DIM (server_list)) { |
401 |
|
|
*port = HKP_PORT; |
402 |
|
|
return server_list[idx]; |
403 |
|
|
} |
404 |
|
|
return NULL; |
405 |
|
|
} |
406 |
|
|
|
407 |
|
|
|
408 |
|
|
/* Check if the computer is connected to the internet. |
409 |
|
|
Return 0 on success -1 otherwise. */ |
410 |
|
|
int |
411 |
|
|
kserver_check_inet_connection (void) |
412 |
|
|
{ |
413 |
|
|
int fd; |
414 |
|
|
|
415 |
|
|
if (!kserver_connect (default_keyserver, default_keyserver_port, &fd)) { |
416 |
|
|
closesocket (fd); |
417 |
|
|
return 0; |
418 |
|
|
} |
419 |
|
|
return -1; |
420 |
|
|
} |
421 |
|
|
|
422 |
|
|
|
423 |
twoaday |
119 |
/* If the request contains the keyid, it have to be |
424 |
|
|
always postfix with '0x'+keyid. This code checks |
425 |
|
|
if the keyid is a decimal value and if so if it contains |
426 |
twoaday |
180 |
the '0x'. If not it is inserted. */ |
427 |
werner |
36 |
const char* |
428 |
|
|
kserver_check_keyid (const char *keyid) |
429 |
|
|
{ |
430 |
twoaday |
119 |
static char id[21]; |
431 |
werner |
36 |
|
432 |
|
|
if (strstr (keyid, "@")) |
433 |
|
|
return keyid; /* email address */ |
434 |
|
|
if (strncmp (keyid, "0x", 2)) { |
435 |
|
|
memset (&id, 0, sizeof (id)); |
436 |
|
|
_snprintf (id, sizeof (id)-1, "0x%s", keyid); |
437 |
|
|
return id; |
438 |
|
|
} |
439 |
|
|
return keyid; |
440 |
twoaday |
119 |
} |
441 |
werner |
36 |
|
442 |
|
|
|
443 |
twoaday |
119 |
/* Update the keyserver proxy user. */ |
444 |
werner |
36 |
static void |
445 |
twoaday |
180 |
update_proxy_user (const char *proxy_user, const char *proxy_pass) |
446 |
werner |
36 |
{ |
447 |
|
|
char t[257]; /* user:pass = 127+1+127+1 = 257 */ |
448 |
|
|
int n = 0; |
449 |
|
|
|
450 |
|
|
n = 4*strlen (proxy_user) / 3 + 32 + strlen (proxy_pass) + 2; |
451 |
|
|
free_if_alloc (proxy.base64_user); |
452 |
|
|
proxy.base64_user = new char[n]; |
453 |
|
|
if (!proxy.base64_user) |
454 |
|
|
BUG (0); |
455 |
|
|
_snprintf (t, sizeof (t)-1, "%s:%s", proxy_user, proxy_pass); |
456 |
twoaday |
180 |
base64_encode (t, proxy.base64_user); |
457 |
werner |
36 |
free_if_alloc (proxy.user); |
458 |
|
|
free_if_alloc (proxy.pass); |
459 |
|
|
proxy.user = m_strdup (proxy_user); |
460 |
|
|
proxy.pass = m_strdup (proxy_pass); |
461 |
twoaday |
119 |
} |
462 |
werner |
36 |
|
463 |
|
|
|
464 |
|
|
/* Check that the given buffer contains a valid keyserver URL. */ |
465 |
|
|
static int |
466 |
|
|
check_URL (const char * buf) |
467 |
|
|
{ |
468 |
|
|
if (strlen (buf) < 7) |
469 |
|
|
return -1; |
470 |
|
|
if (!strstr (buf, "ldap://") |
471 |
|
|
&& !strstr (buf, "http://") |
472 |
|
|
&& !strstr (buf, "finger://") |
473 |
|
|
&& !strstr (buf, "hkp://")) |
474 |
|
|
return -1; |
475 |
|
|
return 0; |
476 |
|
|
} |
477 |
|
|
|
478 |
|
|
|
479 |
|
|
/* Get the port number from the given protocol. */ |
480 |
|
|
static int |
481 |
|
|
port_from_proto (int proto) |
482 |
|
|
{ |
483 |
|
|
switch (proto) { |
484 |
|
|
case KSPROTO_LDAP: return 0; |
485 |
|
|
case KSPROTO_FINGER: return FINGER_PORT; |
486 |
|
|
case KSPROTO_HTTP: return HKP_PORT; |
487 |
|
|
} |
488 |
|
|
return 0; |
489 |
|
|
} |
490 |
|
|
|
491 |
|
|
|
492 |
|
|
/* Get the port number from the given URL. */ |
493 |
|
|
static int |
494 |
twoaday |
180 |
proto_from_URL (const char *buf) |
495 |
werner |
36 |
{ |
496 |
|
|
if (strstr( buf, "ldap")) |
497 |
|
|
return KSPROTO_LDAP; |
498 |
|
|
else if (strstr( buf, "finger")) |
499 |
|
|
return KSPROTO_FINGER; |
500 |
|
|
return KSPROTO_HKP; |
501 |
|
|
} |
502 |
|
|
|
503 |
|
|
|
504 |
|
|
void |
505 |
twoaday |
119 |
keyserver_set_default (const char *hostname, WORD port) |
506 |
werner |
36 |
{ |
507 |
|
|
if (hostname) { |
508 |
|
|
free_if_alloc (default_keyserver); |
509 |
|
|
default_keyserver = m_strdup (hostname); |
510 |
|
|
if (!default_keyserver) |
511 |
|
|
BUG (0); |
512 |
|
|
default_keyserver_port = port; |
513 |
|
|
} |
514 |
|
|
server[0].name = m_strdup (default_keyserver); |
515 |
|
|
server[0].used = 1; |
516 |
|
|
server[0].port = port; |
517 |
|
|
server[0].proto = proto_from_URL (default_keyserver); |
518 |
twoaday |
119 |
} |
519 |
werner |
36 |
|
520 |
|
|
|
521 |
twoaday |
119 |
/* Skip all kind of whitespace chars in @str. */ |
522 |
|
|
static const char* |
523 |
|
|
skip_whitespace (const char *str) |
524 |
werner |
36 |
{ |
525 |
twoaday |
119 |
while (str && *str) { |
526 |
werner |
36 |
if (*str == ' ' || |
527 |
|
|
*str == '\t' || |
528 |
|
|
*str == '\n' || |
529 |
twoaday |
119 |
*str == '\r') { |
530 |
werner |
36 |
str++; |
531 |
|
|
continue; |
532 |
|
|
} |
533 |
|
|
break; |
534 |
|
|
} |
535 |
|
|
return str; |
536 |
|
|
} |
537 |
|
|
|
538 |
|
|
|
539 |
twoaday |
150 |
/* Save the keyserver config file in @conf. */ |
540 |
|
|
int |
541 |
|
|
kserver_save_conf (const char *conf) |
542 |
|
|
{ |
543 |
|
|
FILE *fp; |
544 |
|
|
int pos; |
545 |
|
|
|
546 |
|
|
fp = fopen (conf, "wb"); |
547 |
|
|
if (!fp) { |
548 |
|
|
msg_box (NULL, _("Could not save keyserver.conf file"), |
549 |
|
|
_("Keyserver"), MB_ERR); |
550 |
|
|
return -1; |
551 |
|
|
} |
552 |
|
|
|
553 |
twoaday |
180 |
fprintf (fp, "# do NOT manually modify this file, it will be " |
554 |
|
|
"generated automatically!!\r\n"); |
555 |
twoaday |
150 |
for (pos = 0; pos < MAX_KEYSERVERS; pos++) { |
556 |
|
|
if (!server[pos].used) |
557 |
|
|
continue; |
558 |
twoaday |
165 |
fprintf (fp, "%s:%d\r\n", server[pos].name, server[pos].port); |
559 |
twoaday |
150 |
} |
560 |
|
|
fclose (fp); |
561 |
|
|
return 0; |
562 |
|
|
} |
563 |
|
|
|
564 |
|
|
|
565 |
twoaday |
147 |
/* Load the keyserver config file @conf. */ |
566 |
werner |
36 |
int |
567 |
twoaday |
147 |
kserver_load_conf (const char *conf) |
568 |
werner |
36 |
{ |
569 |
|
|
struct stat statbuf; |
570 |
|
|
FILE *fp; |
571 |
twoaday |
180 |
char buf[1024], *s, *p; |
572 |
werner |
36 |
char *user = NULL, *pass = NULL; |
573 |
twoaday |
119 |
int no_config=0, chk_pos=0; |
574 |
twoaday |
73 |
int pos; |
575 |
werner |
36 |
|
576 |
|
|
for (pos = 0; pos < MAX_KEYSERVERS; pos++) { |
577 |
|
|
server[pos].used = 0; |
578 |
|
|
server[pos].port = HKP_PORT; |
579 |
|
|
} |
580 |
|
|
|
581 |
|
|
fp = fopen (conf, "rb"); |
582 |
|
|
if (!fp) { |
583 |
|
|
for (pos = 0; server_list[pos]; pos++) { |
584 |
|
|
server[pos].used = 1; |
585 |
|
|
server[pos].name = m_strdup (server_list[pos]); |
586 |
|
|
server[pos].proto = proto_from_URL (server_list[pos]); |
587 |
|
|
} |
588 |
|
|
no_config=1; |
589 |
|
|
} |
590 |
twoaday |
180 |
get_reg_proxy_prefs (&proxy); |
591 |
werner |
36 |
if (user && pass) |
592 |
twoaday |
180 |
update_proxy_user (user, pass); |
593 |
werner |
36 |
else if (user && !pass || !user && pass) { |
594 |
twoaday |
147 |
msg_box (NULL, _("Invalid proxy configuration." |
595 |
werner |
36 |
"You need to set a user and a password" |
596 |
twoaday |
147 |
"to use proxy authentication!"), |
597 |
|
|
_("Proxy Error"), MB_ERR); |
598 |
werner |
36 |
} |
599 |
|
|
/* check if the host has a http:// prefix and skip it */ |
600 |
twoaday |
147 |
if (proxy.host && !strncmp (proxy.host, "http://", 7)) { |
601 |
twoaday |
119 |
char *host = m_strdup (proxy.host+7); |
602 |
|
|
if (!host) |
603 |
|
|
BUG (0); |
604 |
werner |
36 |
free_if_alloc (proxy.host); |
605 |
twoaday |
119 |
proxy.host = host; |
606 |
werner |
36 |
} |
607 |
|
|
free_if_alloc (user); |
608 |
|
|
free_if_alloc (pass); |
609 |
|
|
|
610 |
|
|
pos = 0; |
611 |
twoaday |
147 |
while (fp && !feof (fp)) { |
612 |
werner |
36 |
s = fgets (buf, sizeof (buf)-1, fp); |
613 |
|
|
if (!s) |
614 |
|
|
break; |
615 |
|
|
if (strstr (buf, "\r\n")) |
616 |
|
|
buf[strlen (buf)-2] = '\0'; |
617 |
|
|
if (strstr (buf, "\n")) |
618 |
|
|
buf[strlen (buf)-1] = '\0'; |
619 |
|
|
s = (char *)skip_whitespace (buf); |
620 |
|
|
if (*s == '#' || strlen (s) < 7) |
621 |
|
|
continue; |
622 |
|
|
if (check_URL (s)) { |
623 |
|
|
msg_box (NULL, _("All entries of this file must have a valid prefix.\n" |
624 |
|
|
"Currently HKP/HTTP, LDAP and FINGER are supported.\n"), |
625 |
|
|
_("Keyserver Error"), MB_ERR); |
626 |
|
|
continue; |
627 |
|
|
} |
628 |
twoaday |
180 |
chk_pos = 6; |
629 |
werner |
36 |
if (strstr (s, "finger")) |
630 |
|
|
chk_pos = 10; |
631 |
|
|
p = strchr (s+chk_pos, ':'); |
632 |
|
|
server[pos].used = 1; |
633 |
|
|
server[pos].proto = proto_from_URL (s); |
634 |
|
|
server[pos].port = port_from_proto (server[pos].proto); |
635 |
|
|
if (!p) |
636 |
|
|
server[pos].name = m_strdup (s); |
637 |
|
|
else { |
638 |
|
|
server[pos].port = atol (p+1); |
639 |
|
|
if (server[pos].port < 0 || server[pos].port > 65536) |
640 |
|
|
server[pos].port = HKP_PORT; |
641 |
|
|
server[pos].name = new char [(p-s)+2]; |
642 |
|
|
if (!server[pos].name) |
643 |
|
|
BUG (0); |
644 |
|
|
memset (server[pos].name, 0, (p-s)+2); |
645 |
|
|
memcpy (server[pos].name, s, (p-s)); |
646 |
|
|
} |
647 |
|
|
pos++; |
648 |
twoaday |
147 |
if (pos > MAX_KEYSERVERS) { |
649 |
|
|
msg_box (NULL, _("The keyserver limit is exceeded"), |
650 |
|
|
_("Keyserver Warning"), MB_INFO); |
651 |
werner |
36 |
break; |
652 |
|
|
} |
653 |
|
|
} |
654 |
|
|
if (fp) |
655 |
|
|
fclose (fp); |
656 |
twoaday |
73 |
if (!pos) { |
657 |
twoaday |
180 |
/* only issue an error if the config file exist but |
658 |
|
|
it has no valid entries. */ |
659 |
werner |
36 |
keyserver_set_default (NULL, HKP_PORT); |
660 |
|
|
if (!no_config) |
661 |
twoaday |
147 |
return WPTERR_CONFIG_FILE; |
662 |
werner |
36 |
} |
663 |
|
|
|
664 |
|
|
if (!stat (conf, &statbuf)) |
665 |
|
|
conf_timestamp = statbuf.st_mtime; |
666 |
|
|
return 0; |
667 |
twoaday |
147 |
} |
668 |
werner |
36 |
|
669 |
|
|
|
670 |
twoaday |
119 |
/* Connect to the keyserver @hostname (port @port). |
671 |
|
|
Return value: 0 on success */ |
672 |
werner |
36 |
int |
673 |
twoaday |
119 |
kserver_connect (const char *hostname, WORD port, int *conn_fd) |
674 |
werner |
36 |
{ |
675 |
|
|
int rc, fd; |
676 |
twoaday |
119 |
DWORD iaddr; |
677 |
werner |
36 |
char host[128] = {0}; |
678 |
|
|
struct hostent *hp; |
679 |
|
|
struct sockaddr_in sock; |
680 |
|
|
|
681 |
|
|
log_debug ("kserver_connect: %s:%d\r\n", hostname, port); |
682 |
|
|
|
683 |
|
|
if (!port) |
684 |
|
|
port = HKP_PORT; |
685 |
|
|
if (conn_fd) |
686 |
|
|
*conn_fd = 0; |
687 |
|
|
hostname = skip_type_prefix (hostname); |
688 |
|
|
|
689 |
|
|
memset (&sock, 0, sizeof (sock)); |
690 |
|
|
sock.sin_family = AF_INET; |
691 |
|
|
sock.sin_port = proxy.host? htons (proxy.port) : htons (port); |
692 |
|
|
if (proxy.host) |
693 |
|
|
strncpy (host, proxy.host, 127); |
694 |
|
|
else |
695 |
|
|
strncpy (host, hostname, 127); |
696 |
|
|
|
697 |
|
|
if ((iaddr = inet_addr (host)) != INADDR_NONE) |
698 |
|
|
memcpy (&sock.sin_addr, &iaddr, sizeof (iaddr)); |
699 |
|
|
else if ((hp = gethostbyname (host))) { |
700 |
|
|
if (hp->h_addrtype != AF_INET || hp->h_length != 4) { |
701 |
|
|
log_debug ("gethostbyname: unknown address type.\r\n"); |
702 |
|
|
return WPTERR_WINSOCK_RESOLVE; |
703 |
|
|
} |
704 |
|
|
memcpy (&sock.sin_addr, hp->h_addr, hp->h_length); |
705 |
|
|
} |
706 |
|
|
else { |
707 |
twoaday |
119 |
log_debug ("gethostbyname: failed ec=%d.\r\n", net_errno); |
708 |
werner |
36 |
return WPTERR_WINSOCK_RESOLVE; |
709 |
|
|
} |
710 |
|
|
fd = socket (AF_INET, SOCK_STREAM, 0); |
711 |
|
|
if (fd == INVALID_SOCKET) |
712 |
|
|
return WPTERR_WINSOCK_SOCKET; |
713 |
|
|
rc = connect (fd, (struct sockaddr *) &sock, sizeof (sock)); |
714 |
|
|
if (rc == SOCKET_ERROR) { |
715 |
twoaday |
119 |
log_debug ("connect: failed ec=%d.\r\n", net_errno); |
716 |
werner |
36 |
closesocket (fd); |
717 |
|
|
return WPTERR_WINSOCK_CONNECT; |
718 |
|
|
} |
719 |
|
|
|
720 |
twoaday |
180 |
if (proxy.proto != PROXY_PROTO_HTTP) { |
721 |
|
|
rc = socks_handshake (&proxy, fd, hostname, port); |
722 |
|
|
if (rc) { |
723 |
|
|
closesocket (fd); |
724 |
|
|
return WPTERR_GENERAL; /* XXX: use better code. */ |
725 |
|
|
} |
726 |
|
|
} |
727 |
|
|
|
728 |
werner |
36 |
if (conn_fd) |
729 |
|
|
*conn_fd = fd; |
730 |
|
|
WSASetLastError (0); |
731 |
|
|
return 0; |
732 |
twoaday |
119 |
} |
733 |
werner |
36 |
|
734 |
|
|
|
735 |
twoaday |
144 |
static bool |
736 |
|
|
URL_must_encoded (const char *url) |
737 |
|
|
{ |
738 |
|
|
if (strchr (url, '.') || strchr (url, '@') || strchr (url, ' ')) |
739 |
|
|
return true; |
740 |
|
|
return false; |
741 |
|
|
} |
742 |
|
|
|
743 |
|
|
|
744 |
|
|
/* Perform URL encoding of the given data. */ |
745 |
werner |
36 |
static char* |
746 |
twoaday |
144 |
URL_encode (const char *url, size_t ulen, size_t *ret_nlen) |
747 |
werner |
36 |
{ |
748 |
twoaday |
144 |
gpgme_data_t hd; |
749 |
|
|
char numbuf[5], *pp, *p; |
750 |
|
|
size_t i, n; |
751 |
werner |
36 |
|
752 |
twoaday |
144 |
gpgme_data_new (&hd); |
753 |
|
|
for (i=0; i < ulen; i++) { |
754 |
|
|
if (isalnum (url[i]) || url[i] == '-') |
755 |
|
|
gpg_data_putc (hd, url[i]); |
756 |
|
|
else if (url[i] == ' ') |
757 |
|
|
gpg_data_putc (hd, '+'); |
758 |
werner |
36 |
else { |
759 |
twoaday |
144 |
sprintf (numbuf, "%%%02X", url[i]); |
760 |
|
|
gpgme_data_write (hd, numbuf, strlen (numbuf)); |
761 |
werner |
36 |
} |
762 |
|
|
} |
763 |
twoaday |
144 |
gpg_data_putc (hd, '\0'); |
764 |
|
|
|
765 |
|
|
/* Copy memory to avoid that we need to use gpgme_free later. */ |
766 |
|
|
pp = gpgme_data_release_and_get_mem (hd, &n); |
767 |
|
|
p = m_strdup (pp); |
768 |
|
|
gpgme_free (pp); |
769 |
|
|
if (ret_nlen) |
770 |
|
|
*ret_nlen = n; |
771 |
werner |
36 |
return p; |
772 |
|
|
} |
773 |
|
|
|
774 |
|
|
|
775 |
|
|
/* Format a request for the given keyid (send). */ |
776 |
|
|
static char* |
777 |
twoaday |
119 |
kserver_send_request (const char *hostname, WORD port, |
778 |
|
|
const char *pubkey, int octets) |
779 |
werner |
36 |
{ |
780 |
twoaday |
119 |
char *request = NULL; |
781 |
|
|
char *enc_pubkey = NULL; |
782 |
|
|
size_t enc_octets; |
783 |
werner |
36 |
int reqlen; |
784 |
|
|
|
785 |
twoaday |
119 |
log_debug ("kserver_send_request: %s:%d\r\n", hostname, port); |
786 |
werner |
36 |
|
787 |
|
|
if (!port) |
788 |
|
|
port = HKP_PORT; |
789 |
|
|
reqlen = 512 + strlen (hostname) + 2*strlen (pubkey); |
790 |
|
|
request = new char[reqlen]; |
791 |
|
|
if (!request) |
792 |
|
|
BUG (0); |
793 |
twoaday |
144 |
enc_pubkey = URL_encode (pubkey, octets, &enc_octets); |
794 |
werner |
36 |
if (!enc_pubkey || !enc_octets) { |
795 |
|
|
free_if_alloc (request); |
796 |
|
|
return NULL; |
797 |
|
|
} |
798 |
|
|
|
799 |
twoaday |
180 |
if (proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
800 |
werner |
36 |
_snprintf (request, reqlen-1, |
801 |
|
|
"POST http://%s:%d/pks/add HTTP/1.0\r\n" |
802 |
|
|
"Referer: \r\n" |
803 |
|
|
"User-Agent: WinPT/W32\r\n" |
804 |
|
|
"Host: %s:%d\r\n" |
805 |
|
|
"Proxy-Authorization: Basic %s\r\n" |
806 |
|
|
"Content-type: application/x-www-form-urlencoded\r\n" |
807 |
|
|
"Content-length: %d\r\n" |
808 |
|
|
"\r\n" |
809 |
|
|
"keytext=%s" |
810 |
|
|
"\n", |
811 |
|
|
skip_type_prefix (hostname), port, hostname, port, |
812 |
|
|
proxy.base64_user, enc_octets+9, enc_pubkey); |
813 |
|
|
} |
814 |
|
|
else { |
815 |
|
|
_snprintf (request, reqlen-1, |
816 |
|
|
"POST /pks/add HTTP/1.0\r\n" |
817 |
|
|
"Referer: \r\n" |
818 |
|
|
"User-Agent: WinPT/W32\r\n" |
819 |
|
|
"Host: %s:%d\r\n" |
820 |
|
|
"Content-type: application/x-www-form-urlencoded\r\n" |
821 |
|
|
"Content-length: %d\r\n" |
822 |
|
|
"\r\n" |
823 |
|
|
"keytext=%s" |
824 |
|
|
"\n", |
825 |
twoaday |
119 |
skip_type_prefix (hostname), port, |
826 |
|
|
enc_octets+9, enc_pubkey); |
827 |
werner |
36 |
} |
828 |
|
|
free_if_alloc (enc_pubkey); |
829 |
|
|
|
830 |
twoaday |
119 |
log_debug ("%s\r\n", request); |
831 |
werner |
36 |
return request; |
832 |
twoaday |
119 |
} |
833 |
werner |
36 |
|
834 |
|
|
|
835 |
|
|
int |
836 |
twoaday |
147 |
kserver_recvkey_ext (const char *hostname, WORD port, const char *keyid, |
837 |
|
|
char **r_key, int *r_keylen) |
838 |
twoaday |
119 |
{ |
839 |
|
|
char *request = NULL; |
840 |
|
|
int conn_fd; |
841 |
werner |
36 |
int rc, n; |
842 |
twoaday |
119 |
|
843 |
werner |
36 |
if (!port) |
844 |
|
|
port = HKP_PORT; |
845 |
|
|
hkp_err = 0; /* reset */ |
846 |
|
|
rc = kserver_connect (hostname, port, &conn_fd); |
847 |
|
|
if (rc) |
848 |
|
|
goto leave; |
849 |
|
|
|
850 |
|
|
request = new char[300+1]; |
851 |
|
|
if (!request) |
852 |
|
|
BUG (0); |
853 |
|
|
|
854 |
twoaday |
180 |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
855 |
werner |
36 |
_snprintf (request, 300, |
856 |
|
|
"GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n" |
857 |
|
|
"Proxy-Authorization: Basic %s\r\n\r\n", |
858 |
|
|
skip_type_prefix (hostname), port, keyid, proxy.base64_user); |
859 |
|
|
} |
860 |
twoaday |
180 |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
861 |
werner |
36 |
_snprintf (request, 300, |
862 |
|
|
"GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n", |
863 |
|
|
skip_type_prefix (hostname), port, keyid); |
864 |
|
|
} |
865 |
|
|
else { |
866 |
|
|
_snprintf (request, 300, |
867 |
|
|
"GET /pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n", keyid); |
868 |
|
|
} |
869 |
|
|
|
870 |
|
|
log_debug ("%s\r\n", request); |
871 |
|
|
|
872 |
|
|
rc = sock_write (conn_fd, request, strlen (request)); |
873 |
|
|
if (rc == SOCKET_ERROR) { |
874 |
|
|
rc = WPTERR_WINSOCK_RECVKEY; |
875 |
|
|
goto leave; |
876 |
|
|
} |
877 |
|
|
|
878 |
twoaday |
147 |
rc = sock_read_ext (conn_fd, r_key, &n); |
879 |
|
|
if (rc == SOCKET_ERROR) { |
880 |
werner |
36 |
rc = WPTERR_WINSOCK_RECVKEY; |
881 |
|
|
goto leave; |
882 |
|
|
} |
883 |
|
|
|
884 |
twoaday |
147 |
if (r_keylen) |
885 |
|
|
*r_keylen = n; |
886 |
|
|
log_debug("%s\r\n", *r_key); |
887 |
|
|
rc = check_hkp_response (*r_key, 1); |
888 |
werner |
36 |
if (rc) |
889 |
|
|
goto leave; |
890 |
|
|
|
891 |
|
|
WSASetLastError (0); |
892 |
|
|
|
893 |
|
|
leave: |
894 |
|
|
closesocket (conn_fd); |
895 |
|
|
free_if_alloc (request); |
896 |
|
|
return rc; |
897 |
twoaday |
119 |
} |
898 |
werner |
36 |
|
899 |
twoaday |
147 |
/* Interface for receiving a public key. */ |
900 |
|
|
int |
901 |
|
|
kserver_recvkey (const char *hostname, WORD port, const char *keyid, |
902 |
|
|
char *key, int maxkeylen) |
903 |
|
|
{ |
904 |
|
|
char *tmpkey; |
905 |
|
|
int rc, nread=0; |
906 |
werner |
36 |
|
907 |
twoaday |
147 |
/* XXX: just a wrapper around the new method, replace it |
908 |
|
|
soon as possible. */ |
909 |
|
|
rc = kserver_recvkey_ext (hostname, port, keyid, &tmpkey, &nread); |
910 |
|
|
if (rc) |
911 |
|
|
return rc; |
912 |
|
|
|
913 |
|
|
if (nread > maxkeylen) { |
914 |
|
|
free_if_alloc (tmpkey); |
915 |
|
|
return WPTERR_GENERAL; |
916 |
|
|
} |
917 |
|
|
strcpy (key, tmpkey); |
918 |
|
|
free_if_alloc (tmpkey); |
919 |
|
|
return 0; |
920 |
|
|
} |
921 |
|
|
|
922 |
|
|
|
923 |
werner |
36 |
/* Interface to send a public key. */ |
924 |
|
|
int |
925 |
twoaday |
119 |
kserver_sendkey (const char *hostname, WORD port, const char *pubkey, int len ) |
926 |
werner |
36 |
{ |
927 |
|
|
char *request = NULL; |
928 |
|
|
char log[2048]; |
929 |
twoaday |
119 |
int conn_fd, n; |
930 |
|
|
int rc; |
931 |
werner |
36 |
|
932 |
|
|
hkp_err = 0; /* reset */ |
933 |
|
|
rc = kserver_connect (hostname, port, &conn_fd); |
934 |
|
|
if (rc) |
935 |
|
|
goto leave; |
936 |
|
|
|
937 |
|
|
request = kserver_send_request (hostname, port, pubkey, len); |
938 |
|
|
if (request == NULL) { |
939 |
|
|
rc = WPTERR_GENERAL; |
940 |
|
|
goto leave; |
941 |
|
|
} |
942 |
|
|
|
943 |
twoaday |
119 |
rc = sock_write (conn_fd, request, strlen (request)); |
944 |
|
|
if (rc == SOCKET_ERROR) { |
945 |
werner |
36 |
rc = WPTERR_WINSOCK_SENDKEY; |
946 |
|
|
goto leave; |
947 |
|
|
} |
948 |
|
|
|
949 |
twoaday |
119 |
rc = sock_read (conn_fd, log, sizeof (log)-1, &n); |
950 |
|
|
if (rc == SOCKET_ERROR) { |
951 |
werner |
36 |
rc = WPTERR_WINSOCK_SENDKEY; |
952 |
|
|
goto leave; |
953 |
|
|
} |
954 |
|
|
|
955 |
|
|
log_debug ("kserver_sendkey:\r\n%s\r\n", log); |
956 |
|
|
rc = check_hkp_response (log, 0); |
957 |
twoaday |
119 |
if (rc) |
958 |
werner |
36 |
goto leave; |
959 |
|
|
|
960 |
|
|
WSASetLastError (0); |
961 |
|
|
|
962 |
|
|
leave: |
963 |
|
|
closesocket (conn_fd); |
964 |
|
|
free_if_alloc (request); |
965 |
|
|
return rc; |
966 |
twoaday |
119 |
} |
967 |
werner |
36 |
|
968 |
|
|
|
969 |
|
|
int |
970 |
twoaday |
119 |
kserver_search_init (const char *hostname, WORD port, |
971 |
|
|
const char *keyid, int *conn_fd) |
972 |
werner |
36 |
{ |
973 |
twoaday |
119 |
char *request = NULL; |
974 |
twoaday |
144 |
char *enc_keyid = NULL; |
975 |
|
|
int n = 0; |
976 |
werner |
36 |
int rc, sock_fd; |
977 |
twoaday |
144 |
|
978 |
werner |
36 |
rc = kserver_connect (hostname, port, &sock_fd); |
979 |
|
|
if (rc) { |
980 |
|
|
*conn_fd = 0; |
981 |
|
|
goto leave; |
982 |
|
|
} |
983 |
twoaday |
144 |
|
984 |
|
|
enc_keyid = URL_encode (keyid, strlen (keyid), NULL); |
985 |
werner |
36 |
n=300; |
986 |
|
|
request = new char[n+1]; |
987 |
|
|
if (!request) |
988 |
|
|
BUG (0); |
989 |
|
|
|
990 |
twoaday |
180 |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
991 |
werner |
36 |
_snprintf (request, n, |
992 |
|
|
"GET http://%s:%d/pks/lookup?op=index&search=%s HTTP/1.0\r\n" |
993 |
|
|
"Proxy-Authorization: Basic %s\r\n\r\n", |
994 |
twoaday |
144 |
skip_type_prefix (hostname), port, enc_keyid, proxy.base64_user); |
995 |
werner |
36 |
} |
996 |
twoaday |
180 |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
997 |
werner |
36 |
_snprintf (request, n, |
998 |
|
|
"GET http://%s:%d/pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n", |
999 |
twoaday |
144 |
skip_type_prefix (hostname), port, enc_keyid); |
1000 |
werner |
36 |
} |
1001 |
|
|
else { |
1002 |
|
|
_snprintf (request, n, |
1003 |
twoaday |
147 |
"GET /pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n", |
1004 |
|
|
enc_keyid); |
1005 |
werner |
36 |
} |
1006 |
|
|
|
1007 |
|
|
log_debug ("kserver_search_init:\r\n%s\r\n", request); |
1008 |
|
|
|
1009 |
|
|
if (sock_write (sock_fd, request, strlen (request)) == SOCKET_ERROR) { |
1010 |
|
|
rc = WPTERR_GENERAL; |
1011 |
|
|
goto leave; |
1012 |
|
|
} |
1013 |
|
|
|
1014 |
|
|
*conn_fd = sock_fd; |
1015 |
|
|
|
1016 |
|
|
leave: |
1017 |
|
|
free_if_alloc (request); |
1018 |
twoaday |
144 |
free_if_alloc (enc_keyid); |
1019 |
werner |
36 |
return rc; |
1020 |
twoaday |
119 |
} |
1021 |
werner |
36 |
|
1022 |
|
|
|
1023 |
twoaday |
119 |
/* Check keyserver response. */ |
1024 |
werner |
36 |
int |
1025 |
|
|
kserver_search_chkresp (int fd) |
1026 |
|
|
{ |
1027 |
|
|
char buf[128]; |
1028 |
|
|
int n=0; |
1029 |
|
|
|
1030 |
|
|
/* parse response 'HTTP/1.0 500 OK' */ |
1031 |
|
|
if (sock_getline (fd, buf, 127, &n)) |
1032 |
|
|
return WPTERR_KEYSERVER_NOTFOUND; |
1033 |
|
|
|
1034 |
|
|
log_debug ("kserver_search_chkpresp: %s\r\n", buf); |
1035 |
|
|
if (strncmp (buf, "HTTP/1.", 7)) |
1036 |
|
|
return WPTERR_KEYSERVER_NOTFOUND; |
1037 |
|
|
if (strncmp (buf+(8+1), "200", 3)) |
1038 |
|
|
return WPTERR_KEYSERVER_NOTFOUND; |
1039 |
|
|
return 0; |
1040 |
twoaday |
119 |
} |
1041 |
werner |
36 |
|
1042 |
|
|
|
1043 |
twoaday |
144 |
/* Convert an iso date @iso_date (YYYY-MM-DD) into the locale |
1044 |
|
|
representation and store it into @loc_date. |
1045 |
|
|
Return value: 0 on success. */ |
1046 |
|
|
static int |
1047 |
|
|
parse_iso_date (const char *iso_date, char *loc_date, size_t loclen) |
1048 |
|
|
{ |
1049 |
|
|
SYSTEMTIME st; |
1050 |
|
|
char buf[16] = {0}, *p; |
1051 |
|
|
int pos=0; |
1052 |
|
|
|
1053 |
|
|
strncpy (buf, iso_date, sizeof (buf)-1); |
1054 |
|
|
p = strtok (buf, "-"); |
1055 |
|
|
while (p != NULL) { |
1056 |
|
|
switch (pos) { |
1057 |
twoaday |
147 |
case 0: st.wYear = (WORD)atoi (p); pos++; break; |
1058 |
twoaday |
144 |
case 1: st.wMonth = (WORD)atoi (p); pos++; break; |
1059 |
twoaday |
147 |
case 2: st.wDay = (WORD)atoi (p); pos++; break; |
1060 |
twoaday |
144 |
default: break; |
1061 |
|
|
} |
1062 |
|
|
p = strtok (NULL, "-"); |
1063 |
|
|
} |
1064 |
|
|
if (pos != 3) |
1065 |
|
|
return -1; |
1066 |
|
|
|
1067 |
|
|
if (!GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, |
1068 |
|
|
NULL, loc_date, loclen)) |
1069 |
|
|
return -1; |
1070 |
|
|
return 0; |
1071 |
|
|
} |
1072 |
|
|
|
1073 |
|
|
|
1074 |
werner |
36 |
int |
1075 |
twoaday |
144 |
kserver_search (int fd, keyserver_key *key) |
1076 |
werner |
36 |
{ |
1077 |
|
|
char buf[1024], *p; |
1078 |
|
|
int uidlen, nbytes, pos = 0; |
1079 |
|
|
|
1080 |
twoaday |
119 |
log_debug ("keyserver_search:\r\n"); |
1081 |
werner |
36 |
|
1082 |
|
|
if (sock_getline (fd, buf, sizeof (buf) - 1, &nbytes)) |
1083 |
|
|
return WPTERR_GENERAL; |
1084 |
|
|
|
1085 |
twoaday |
119 |
log_debug ("%s\r\n", buf); |
1086 |
werner |
36 |
|
1087 |
|
|
if (!strncmp (buf, "pub", 3)) { |
1088 |
|
|
int revoked = strstr (buf, "KEY REVOKED") != NULL? 1 : 0; |
1089 |
|
|
key->bits = atol (buf+3); |
1090 |
|
|
p = strchr (buf, '>'); |
1091 |
|
|
if (!p) |
1092 |
|
|
goto fail; |
1093 |
|
|
pos = p - buf + 1; |
1094 |
|
|
memcpy (key->keyid, buf + pos, 8); |
1095 |
|
|
key->keyid[8] = '\0'; |
1096 |
|
|
p = strstr (buf, "</a>"); |
1097 |
|
|
if (!p) |
1098 |
|
|
goto fail; |
1099 |
|
|
pos = p - buf + 5; |
1100 |
|
|
memcpy (key->date, buf + pos, 10); |
1101 |
|
|
key->date[10] = '\0'; |
1102 |
twoaday |
144 |
parse_iso_date (key->date, key->date, sizeof (key->date)-1); |
1103 |
werner |
36 |
if (revoked) { |
1104 |
|
|
strcpy (key->uid, "KEY REVOKED: not checked"); |
1105 |
|
|
return 0; |
1106 |
|
|
} |
1107 |
|
|
pos += 10; |
1108 |
|
|
p = buf + pos + 1; |
1109 |
|
|
while (p && *p != '>') |
1110 |
|
|
p++; |
1111 |
|
|
p++; |
1112 |
|
|
uidlen = strlen (p) - 10; |
1113 |
twoaday |
144 |
if (!strstr (p, "<") && !strstr (p, ">")) { |
1114 |
|
|
pos=0; |
1115 |
|
|
while (p && *p && pos < sizeof (key->uid)-1) { |
1116 |
|
|
if (*p == '<') |
1117 |
|
|
break; |
1118 |
|
|
key->uid[pos++] = *p++; |
1119 |
|
|
} |
1120 |
|
|
key->uid[pos] ='\0'; |
1121 |
|
|
return 0; |
1122 |
|
|
} |
1123 |
|
|
|
1124 |
|
|
if (uidlen > sizeof (key->uid)-1) |
1125 |
|
|
uidlen = sizeof (key->uid)-1; |
1126 |
werner |
36 |
memcpy (key->uid, p, uidlen); |
1127 |
|
|
key->uid[uidlen] = '\0'; |
1128 |
|
|
strcat (key->uid, ">"); |
1129 |
|
|
p = strchr (key->uid, '&'); |
1130 |
|
|
if (p) { |
1131 |
|
|
key->uid[(p-key->uid)] = '<'; |
1132 |
twoaday |
119 |
memmove (key->uid+(p-key->uid)+1, key->uid+(p-key->uid)+4, |
1133 |
|
|
strlen (key->uid)-3); |
1134 |
werner |
36 |
} |
1135 |
|
|
return 0; |
1136 |
|
|
} |
1137 |
|
|
|
1138 |
|
|
fail: |
1139 |
|
|
key->bits = 0; |
1140 |
|
|
memset (key, 0, sizeof *key); |
1141 |
|
|
return 0; |
1142 |
twoaday |
144 |
} |
1143 |
werner |
36 |
|
1144 |
|
|
|
1145 |
twoaday |
119 |
/* Release mbmers in the proxy context @ctx. */ |
1146 |
werner |
36 |
void |
1147 |
twoaday |
180 |
kserver_proxy_release (keyserver_proxy_t ctx) |
1148 |
werner |
36 |
{ |
1149 |
twoaday |
119 |
free_if_alloc (ctx->host); |
1150 |
|
|
free_if_alloc (ctx->pass); |
1151 |
|
|
free_if_alloc (ctx->user); |
1152 |
|
|
} |
1153 |
werner |
36 |
|
1154 |
|
|
|
1155 |
twoaday |
133 |
/* Spawn a process given by @cmdl. */ |
1156 |
|
|
static int |
1157 |
|
|
spawn_application (char *cmdl) |
1158 |
|
|
{ |
1159 |
|
|
STARTUPINFO si; |
1160 |
|
|
PROCESS_INFORMATION pi; |
1161 |
|
|
int rc = 0; |
1162 |
|
|
|
1163 |
|
|
memset (&si, 0, sizeof (si)); |
1164 |
|
|
si.cb = sizeof (si); |
1165 |
|
|
si.dwFlags = STARTF_USESHOWWINDOW; |
1166 |
|
|
si.wShowWindow = SW_HIDE; |
1167 |
|
|
memset (&pi, 0, sizeof (pi)); |
1168 |
|
|
|
1169 |
|
|
if (!CreateProcess (NULL, cmdl, NULL, NULL, FALSE, 0, |
1170 |
|
|
NULL, NULL, &si, &pi)) { |
1171 |
|
|
log_box ("Keyserver Plugin", MB_ERR, "Could not spawn helper process"); |
1172 |
|
|
rc = -1; |
1173 |
|
|
} |
1174 |
|
|
|
1175 |
|
|
CloseHandle (pi.hThread); |
1176 |
|
|
WaitForSingleObject (pi.hProcess, INFINITE); |
1177 |
|
|
CloseHandle (pi.hProcess); |
1178 |
|
|
return rc; |
1179 |
|
|
} |
1180 |
|
|
|
1181 |
|
|
|
1182 |
twoaday |
119 |
/* Receive an key via LDAP from host @host with the keyid @keyid. |
1183 |
|
|
@key contains the key on success. */ |
1184 |
twoaday |
147 |
int |
1185 |
twoaday |
119 |
ldap_recvkey (const char *host, const char *keyid, char *key, int maxkeylen) |
1186 |
twoaday |
133 |
{ |
1187 |
twoaday |
175 |
FILE *fp; |
1188 |
werner |
36 |
const char *s; |
1189 |
twoaday |
133 |
char *ksprg = NULL, *p = NULL, *sep; |
1190 |
twoaday |
175 |
char inf[256], outf[256], buf[512]; |
1191 |
|
|
DWORD n; |
1192 |
twoaday |
133 |
int start_key = 0, failed = 0; |
1193 |
|
|
int rc = 0; |
1194 |
werner |
36 |
|
1195 |
twoaday |
133 |
p = get_gnupg_prog (); |
1196 |
|
|
n = strlen (p) + 1 + 128; |
1197 |
|
|
ksprg = new char[n+1]; |
1198 |
werner |
36 |
if (!ksprg) |
1199 |
|
|
BUG (0); |
1200 |
twoaday |
133 |
sep = strrchr (p, '\\'); |
1201 |
|
|
if (sep != NULL) |
1202 |
|
|
p[(sep-p)] = 0; |
1203 |
|
|
|
1204 |
|
|
_snprintf (ksprg, n, "%s\\gpgkeys_ldap.exe", p); |
1205 |
werner |
36 |
free_if_alloc (p); |
1206 |
|
|
if (file_exist_check (ksprg)) { |
1207 |
twoaday |
133 |
log_box ("LDAP Keyserver Plugin", MB_ERR, |
1208 |
|
|
"%s: could not find LDAP keyserver module!", ksprg); |
1209 |
|
|
rc = -1; |
1210 |
werner |
36 |
goto leave; |
1211 |
twoaday |
175 |
} |
1212 |
|
|
get_temp_name (outf, sizeof (outf)-1, keyid); |
1213 |
|
|
get_temp_name (inf, sizeof (inf)-1, NULL); |
1214 |
|
|
fp = fopen (inf, "w+b"); |
1215 |
|
|
if (!fp) { |
1216 |
|
|
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", inf, |
1217 |
werner |
36 |
winpt_strerror (WPTERR_FILE_OPEN)); |
1218 |
|
|
rc = -1; |
1219 |
|
|
goto leave; |
1220 |
|
|
} |
1221 |
twoaday |
175 |
fprintf (fp, |
1222 |
twoaday |
133 |
"VERSION 1\n" |
1223 |
|
|
"PROGRAM 1.4.3-cvs\n" |
1224 |
|
|
"SCHEME ldap\n" |
1225 |
werner |
36 |
"HOST %s\n" |
1226 |
|
|
"COMMAND GET\n" |
1227 |
|
|
"\n" |
1228 |
twoaday |
133 |
"%s\n", |
1229 |
|
|
host? skip_type_prefix (host): "64.94.85.200", keyid); |
1230 |
twoaday |
175 |
fclose (fp); |
1231 |
werner |
36 |
|
1232 |
twoaday |
175 |
p = new char[strlen (ksprg) + strlen (inf) + strlen (outf) + 32]; |
1233 |
twoaday |
133 |
if (!p) |
1234 |
|
|
BUG (NULL); |
1235 |
twoaday |
175 |
sprintf (p, "%s -o %s %s", ksprg, outf, inf); |
1236 |
twoaday |
133 |
if (spawn_application (p)) { |
1237 |
werner |
36 |
rc = -1; |
1238 |
|
|
goto leave; |
1239 |
|
|
} |
1240 |
|
|
|
1241 |
twoaday |
175 |
fp = fopen (outf, "rb"); |
1242 |
|
|
if (!fp) { |
1243 |
werner |
36 |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf, |
1244 |
|
|
winpt_strerror (WPTERR_FILE_OPEN)); |
1245 |
|
|
rc = -1; |
1246 |
|
|
goto leave; |
1247 |
|
|
} |
1248 |
|
|
memset (key, 0, maxkeylen); |
1249 |
twoaday |
175 |
while (!feof (fp)) { |
1250 |
|
|
s = fgets (buf, sizeof (buf)-1, fp); |
1251 |
twoaday |
133 |
if (!s) |
1252 |
werner |
36 |
break; |
1253 |
twoaday |
133 |
if (strstr (s, "KEY") && strstr (s, "FAILED")) { |
1254 |
|
|
failed = 1; |
1255 |
|
|
break; |
1256 |
|
|
} |
1257 |
|
|
if (!start_key && strstr (s, "KEY") && strstr (s, "BEGIN")) { |
1258 |
werner |
36 |
start_key = 1; |
1259 |
|
|
continue; |
1260 |
|
|
} |
1261 |
twoaday |
133 |
if (!start_key) |
1262 |
werner |
36 |
continue; |
1263 |
twoaday |
175 |
strcat (key, buf); |
1264 |
|
|
maxkeylen -= strlen (buf); |
1265 |
twoaday |
133 |
if (maxkeylen < 0 || (strstr (s, "KEY") && strstr (s, "END"))) |
1266 |
werner |
36 |
break; |
1267 |
|
|
} |
1268 |
twoaday |
175 |
fclose (fp); |
1269 |
werner |
36 |
|
1270 |
|
|
leave: |
1271 |
twoaday |
133 |
if (failed || !strlen (key)) |
1272 |
werner |
36 |
rc = WPTERR_WINSOCK_RECVKEY; |
1273 |
twoaday |
175 |
DeleteFile (inf); |
1274 |
twoaday |
133 |
DeleteFile (outf); |
1275 |
|
|
free_if_alloc (p); |
1276 |
|
|
free_if_alloc (ksprg); |
1277 |
werner |
36 |
return rc; |
1278 |
twoaday |
119 |
} |
1279 |
werner |
36 |
|
1280 |
|
|
|
1281 |
twoaday |
119 |
/* Receive an key via FINGER from host @host with the user @user. |
1282 |
|
|
On success @key contains the key. */ |
1283 |
werner |
36 |
int |
1284 |
twoaday |
119 |
finger_recvkey (const char *host, const char *user, char *key, int maxkeylen) |
1285 |
werner |
36 |
{ |
1286 |
|
|
char buf[128]; |
1287 |
|
|
int fd, nread; |
1288 |
|
|
int start_key = 0; |
1289 |
|
|
int rc=0; |
1290 |
|
|
|
1291 |
twoaday |
180 |
rc = kserver_connect (host, FINGER_PORT, &fd); |
1292 |
|
|
if (rc) |
1293 |
|
|
return rc; |
1294 |
werner |
36 |
|
1295 |
twoaday |
180 |
sock_write (fd, user, strlen (user)); |
1296 |
|
|
sock_write (fd, "\r\n", 2); |
1297 |
werner |
36 |
|
1298 |
|
|
memset (key, 0, maxkeylen); |
1299 |
|
|
while (maxkeylen > 0) { |
1300 |
twoaday |
180 |
if (sock_getline (fd, buf, sizeof (buf), &nread)) |
1301 |
|
|
break; |
1302 |
werner |
36 |
strcat (buf, "\n"); |
1303 |
|
|
if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) { |
1304 |
|
|
strcat (key, buf); |
1305 |
|
|
start_key = 1; |
1306 |
|
|
maxkeylen -= nread; |
1307 |
|
|
} |
1308 |
|
|
else if (strstr (buf, "END PGP PUBLIC KEY BLOCK" ) && start_key) { |
1309 |
|
|
strcat (key, buf); |
1310 |
|
|
start_key--; |
1311 |
|
|
maxkeylen -= nread; |
1312 |
|
|
break; |
1313 |
|
|
} |
1314 |
|
|
else if (start_key) { |
1315 |
|
|
strcat (key, buf); |
1316 |
|
|
maxkeylen -= nread; |
1317 |
|
|
} |
1318 |
|
|
} |
1319 |
|
|
closesocket (fd); |
1320 |
|
|
if (start_key != 0) |
1321 |
|
|
rc = WPTERR_WINSOCK_RECVKEY; |
1322 |
|
|
return rc; |
1323 |
|
|
} |
1324 |
|
|
|
1325 |
|
|
|
1326 |
twoaday |
119 |
/* Check if the given name @name is a valid hostname. */ |
1327 |
werner |
36 |
int |
1328 |
|
|
check_IP_or_hostname (const char *name) |
1329 |
|
|
{ |
1330 |
|
|
const char *not_allowed = "=!ยง$%&@#*~\\/}][{<>|,;:'"; |
1331 |
|
|
size_t i, j; |
1332 |
|
|
|
1333 |
|
|
for (i=0; i < strlen (name); i++) { |
1334 |
|
|
for (j =0; j < strlen (not_allowed); j++) { |
1335 |
|
|
if (name[i] == not_allowed[j]) |
1336 |
|
|
return -1; |
1337 |
|
|
} |
1338 |
|
|
} |
1339 |
|
|
return 0; |
1340 |
|
|
} |