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