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