24 |
#endif |
#endif |
25 |
|
|
26 |
#include <windows.h> |
#include <windows.h> |
27 |
|
#include <shlobj.h> |
28 |
#include <stdio.h> |
#include <stdio.h> |
29 |
#include <sys/stat.h> |
#include <sys/stat.h> |
30 |
#include <ctype.h> |
#include <ctype.h> |
34 |
#include "wptTypes.h" |
#include "wptTypes.h" |
35 |
#include "wptNLS.h" |
#include "wptNLS.h" |
36 |
#include "wptW32API.h" |
#include "wptW32API.h" |
|
#include "wptVersion.h" |
|
37 |
#include "wptGPG.h" |
#include "wptGPG.h" |
38 |
#include "wptRegistry.h" |
#include "wptRegistry.h" |
39 |
|
|
40 |
static char base64code[] = |
/* just map net_errno to a winsock error. */ |
41 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
#define net_errno ((int)WSAGetLastError ()) |
42 |
|
|
43 |
|
|
44 |
keyserver server[MAX_KEYSERVERS] = {0}; |
keyserver server[MAX_KEYSERVERS] = {0}; |
45 |
static keyserver_proxy_ctx proxy = {0}; |
keyserver_proxy_s proxy = {0}; |
46 |
static const char * server_list[] = { |
static const char *server_list[] = { |
|
"hkp://wwwkeys.nl.pgp.net", |
|
|
"hkp://wwwkeys.pl.pgp.net", |
|
|
"hkp://wwwkeys.at.pgp.net", |
|
|
"hkp://wwwkeys.ch.pgp.net", |
|
|
"hkp://wwwkeys.de.pgp.net", |
|
|
"hkp://wwwkeys.dk.pgp.net", |
|
|
"hkp://wwwkeys.cz.pgp.net", |
|
|
"hkp://wwwkeys.es.pgp.net", |
|
|
"hkp://wwwkeys.eu.pgp.net", |
|
|
"hkp://wwwkeys.uk.pgp.net", |
|
|
"hkp://wwwkeys.us.pgp.net", |
|
47 |
"hkp://gnv.us.ks.cryptnet.net", |
"hkp://gnv.us.ks.cryptnet.net", |
48 |
|
"hkp://keyserver.kjsl.com", |
49 |
|
"hkp://sks.keyserver.penguin.de", |
50 |
"hkp://subkeys.pgp.net", |
"hkp://subkeys.pgp.net", |
51 |
"ldap://keyserver.pgp.com", |
"ldap://keyserver.pgp.com", |
52 |
NULL |
NULL |
53 |
}; |
}; |
|
static char hkp_errmsg[1024]; |
|
|
static int hkp_err = 0; |
|
|
static u32 conf_timestamp = 0; |
|
|
char * default_keyserver = NULL; |
|
|
unsigned short default_keyserver_port = 0; |
|
54 |
|
|
55 |
|
|
56 |
|
static char hkp_errmsg[1024]; /* Holds the error message from the server */ |
57 |
|
static int hkp_err = 0; /* != 0 indicates an error occurred. */ |
58 |
|
|
59 |
|
/* Default keyserver and port. */ |
60 |
|
char *default_keyserver = NULL; |
61 |
|
WORD default_keyserver_port = 0; |
62 |
|
|
63 |
|
/* Default socket timeout. */ |
64 |
|
static int default_socket_timeout = 6; |
65 |
|
|
66 |
|
/* Basic64 encode the input @inbuf to @outbuf. */ |
67 |
static void |
static void |
68 |
base64_encode( const char *inputstr, char *outputstr, int maxlen ) |
base64_encode (const char *inbuf, char *outbuf) |
69 |
{ |
{ |
70 |
int index = 0, temp = 0, res = 0, i = 0, inputlen = 0, len = 0; |
char base64code[] = |
71 |
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
72 |
|
int index = 0, temp = 0, res = 0; |
73 |
|
int i = 0, inputlen = 0, len = 0; |
74 |
|
|
75 |
inputlen = strlen (inputstr); /* fixme: check if len > maxlen */ |
inputlen = strlen (inbuf); |
76 |
for (i = 0; i <inputlen; i++) { |
for (i = 0; i < inputlen; i++) { |
77 |
res = temp; |
res = temp; |
78 |
res = (res << 8) | (inputstr[i] & 0xFF); |
res = (res << 8) | (inbuf[i] & 0xFF); |
79 |
switch (index++) { |
switch (index++) { |
80 |
case 0: outputstr[len++] = base64code[res >> 2 & 0x3F]; res &= 0x3; break; |
case 0: |
81 |
case 1: outputstr[len++] = base64code[res >> 4 & 0x3F]; res &= 0xF; break; |
outbuf[len++] = base64code[res >> 2 & 0x3F]; |
82 |
case 2: outputstr[len++] = base64code[res >> 6 & 0x3F]; |
res &= 0x3; |
83 |
outputstr[len++] = base64code[res & 0x3F]; res = index = 0; break; |
break; |
84 |
|
case 1: |
85 |
|
outbuf[len++] = base64code[res >> 4 & 0x3F]; |
86 |
|
res &= 0xF; |
87 |
|
break; |
88 |
|
case 2: |
89 |
|
outbuf[len++] = base64code[res >> 6 & 0x3F]; |
90 |
|
outbuf[len++] = base64code[res & 0x3F]; |
91 |
|
res = index = 0; |
92 |
|
break; |
93 |
} |
} |
94 |
temp = res; |
temp = res; |
95 |
} |
} |
96 |
|
|
97 |
switch( index ) { |
if (index == 2) { |
98 |
case 0: break; |
outbuf[len++] = base64code[temp << 2 & 0x3F]; |
99 |
case 2: outputstr[len++] = base64code[temp << 2 & 0x3F]; |
outbuf[len++] = '='; |
|
outputstr[len++] = '='; break; |
|
|
case 1: outputstr[len++] = base64code[temp << 4 & 0x3F]; |
|
|
outputstr[len++] = '='; |
|
|
outputstr[len++] = '='; |
|
100 |
} |
} |
101 |
|
else if (index == 1) { |
102 |
outputstr[len] = '\0'; |
outbuf[len++] = base64code[temp << 4 & 0x3F]; |
103 |
} /* base64_encode */ |
outbuf[len++] = '='; |
104 |
|
outbuf[len++] = '='; |
105 |
|
} |
106 |
|
|
107 |
|
outbuf[len] = '\0'; |
108 |
|
} |
109 |
|
|
110 |
|
|
111 |
/* Skip the URL schema and return only the host part of it. */ |
/* Skip the URL schema and return only the host part of it. */ |
112 |
static const char * |
static const char* |
113 |
skip_type_prefix (const char * hostname) |
skip_type_prefix (const char *hostname) |
114 |
{ |
{ |
115 |
if (hostname && !strncmp (hostname, "http://", 7)) |
if (hostname && !strncmp (hostname, "http://", 7)) |
116 |
hostname += 7; |
hostname += 7; |
128 |
Return 0 on success. */ |
Return 0 on success. */ |
129 |
static int |
static int |
130 |
check_hkp_response (const char *resp, int recv) |
check_hkp_response (const char *resp, int recv) |
131 |
{ |
{ |
132 |
|
char *p, *end; |
133 |
int ec, len; |
int ec, len; |
|
char *p, * end; |
|
134 |
|
|
135 |
ec = recv ? WPTERR_WINSOCK_RECVKEY : WPTERR_WINSOCK_SENDKEY; |
ec = recv ? WPTERR_WINSOCK_RECVKEY : WPTERR_WINSOCK_SENDKEY; |
136 |
if (!strstr (resp, "HTTP/1.0 200 OK") && |
if (!strstr (resp, "HTTP/1.0 200 OK") && |
143 |
if (p && strlen (p) < sizeof (hkp_errmsg)) { |
if (p && strlen (p) < sizeof (hkp_errmsg)) { |
144 |
end = strstr (p, "</p>"); |
end = strstr (p, "</p>"); |
145 |
len = end? (end - p + 1) : strlen (p); |
len = end? (end - p + 1) : strlen (p); |
146 |
|
memset (hkp_errmsg, 0, sizeof (hkp_errmsg)); |
147 |
strncpy (hkp_errmsg, p, len); |
strncpy (hkp_errmsg, p, len); |
148 |
hkp_err = 1; |
hkp_err = 1; |
149 |
} |
} |
159 |
sock_getline (int fd, char *buf, int buflen, int *nbytes) |
sock_getline (int fd, char *buf, int buflen, int *nbytes) |
160 |
{ |
{ |
161 |
char ch; |
char ch; |
162 |
int count = 0; |
int nread = 0; |
163 |
|
|
164 |
if (nbytes) |
if (nbytes) |
165 |
*nbytes = 0; |
*nbytes = 0; |
166 |
*buf = 0; |
*buf = 0; |
167 |
while (recv (fd, &ch, 1, 0) > 0) { |
while (recv (fd, &ch, 1, 0) > 0) { |
168 |
*buf++ = ch; |
*buf++ = ch; |
169 |
count++; |
nread++; |
170 |
if (ch == '\n' || count == (buflen - 1)) { |
if (ch == '\r') |
171 |
|
continue; /* remove this char. */ |
172 |
|
if (ch == '\n' || nread == (buflen - 1)) { |
173 |
*buf = 0; |
*buf = 0; |
174 |
if (nbytes) |
if (nbytes) |
175 |
*nbytes = count; |
*nbytes = nread; |
176 |
return 0; |
return 0; |
177 |
} |
} |
178 |
} |
} |
191 |
|
|
192 |
FD_ZERO (&rfd); |
FD_ZERO (&rfd); |
193 |
FD_SET (fd, &rfd); |
FD_SET (fd, &rfd); |
194 |
if (select (fd + 1, &rfd, NULL, NULL, &tv) == SOCKET_ERROR) |
if (select (fd + 1, &rfd, NULL, NULL, &tv) == SOCKET_ERROR) { |
195 |
|
log_debug ("sock_select: select() failed ec=%d.\r\n", net_errno); |
196 |
return SOCKET_ERROR; |
return SOCKET_ERROR; |
197 |
|
} |
198 |
if (FD_ISSET (fd, &rfd)) |
if (FD_ISSET (fd, &rfd)) |
199 |
return 1; |
return 1; |
200 |
return 0; |
return 0; |
201 |
} |
} |
202 |
|
|
203 |
|
|
204 |
/* Read from a socket with a fixed timeout of 10 seconds. |
/* Read from a socket @fd to buffer @buf with a fixed timeout |
205 |
|
of 10 seconds. The amount of bytes which were read are |
206 |
|
returned in @nbytes. |
207 |
Return value: 0 on success. */ |
Return value: 0 on success. */ |
208 |
static int |
static int |
209 |
sock_read (int fd, char *buf, int buflen, int *nbytes) |
sock_read (int fd, char *buf, int buflen, int *nbytes) |
210 |
{ |
{ |
211 |
DWORD nread; |
DWORD nread; |
212 |
int nleft = buflen; |
int nleft = buflen; |
213 |
int rc, n = 0; |
int rc, n = 0; |
214 |
|
|
215 |
|
if (nbytes) |
216 |
|
*nbytes = 0; |
217 |
while (nleft > 0) { |
while (nleft > 0) { |
218 |
if (n >= 10) |
if (n >= default_socket_timeout) |
219 |
return WPTERR_WINSOCK_TIMEOUT; |
return WPTERR_WINSOCK_TIMEOUT; |
220 |
rc = sock_select (fd, 1); |
rc = sock_select (fd, 1); |
221 |
if (rc == SOCKET_ERROR) |
if (rc == SOCKET_ERROR) |
222 |
return SOCKET_ERROR; |
return rc; |
223 |
else if( !rc ) |
else if (!rc) |
224 |
n++; |
n++; |
225 |
else { |
else { |
226 |
nread = recv (fd, buf, nleft, 0); |
nread = recv (fd, buf, nleft, 0); |
227 |
if (nread == SOCKET_ERROR) |
if (nread == SOCKET_ERROR) { |
228 |
|
log_debug ("sock_read: recv() failed ec=%d.\r\n", net_errno); |
229 |
return SOCKET_ERROR; |
return SOCKET_ERROR; |
230 |
|
} |
231 |
else if (!nread) |
else if (!nread) |
232 |
break; |
break; |
233 |
nleft -= nread; |
nleft -= nread; |
240 |
return 0; |
return 0; |
241 |
} |
} |
242 |
|
|
243 |
|
/* Read much data as possible from the socket @fd and |
244 |
|
return the data in @buffer. Caller must free data. |
245 |
|
Return value: 0 on success. */ |
246 |
|
int |
247 |
|
sock_read_ext (int fd, char **buffer, int *r_bufferlen) |
248 |
|
{ |
249 |
|
gpgme_data_t dh; |
250 |
|
char buf[1024], *p; |
251 |
|
size_t n=0; |
252 |
|
int nread, rc; |
253 |
|
|
254 |
/* Write @buf to a socket @fd. */ |
gpgme_data_new (&dh); |
255 |
|
while (n < 10) { |
256 |
|
rc = sock_select (fd, 1); |
257 |
|
if (rc == SOCKET_ERROR) { |
258 |
|
gpgme_data_release (dh); |
259 |
|
return rc; |
260 |
|
} |
261 |
|
else if (!rc) |
262 |
|
n++; |
263 |
|
else { |
264 |
|
nread = recv (fd, buf, sizeof (buf), 0); |
265 |
|
if (nread == SOCKET_ERROR) |
266 |
|
return SOCKET_ERROR; |
267 |
|
else if (!nread) |
268 |
|
break; |
269 |
|
gpgme_data_write (dh, buf, nread); |
270 |
|
} |
271 |
|
} |
272 |
|
gpg_data_putc (dh, '\0'); |
273 |
|
p = gpgme_data_release_and_get_mem (dh, &n); |
274 |
|
*buffer = m_strdup (p); |
275 |
|
if (r_bufferlen) |
276 |
|
*r_bufferlen = n; |
277 |
|
gpgme_free (p); |
278 |
|
return 0; |
279 |
|
} |
280 |
|
|
281 |
|
|
282 |
|
/* Write the buffer @buf with the length @buflen to a socket @fd. */ |
283 |
static int |
static int |
284 |
sock_write (int fd, const char *buf, int buflen) |
sock_write (int fd, const char *buf, int buflen) |
285 |
{ |
{ |
288 |
|
|
289 |
while (nleft > 0) { |
while (nleft > 0) { |
290 |
nwritten = send (fd, buf, nleft, 0); |
nwritten = send (fd, buf, nleft, 0); |
291 |
if (nwritten == SOCKET_ERROR) |
if (nwritten == SOCKET_ERROR) { |
292 |
|
log_debug ("sock_write: send() failed ec=%d\r\n", net_errno); |
293 |
return SOCKET_ERROR; |
return SOCKET_ERROR; |
294 |
|
} |
295 |
nleft -= nwritten; |
nleft -= nwritten; |
296 |
buf += nwritten; |
buf += nwritten; |
297 |
} |
} |
298 |
|
|
299 |
return 0; |
return 0; |
300 |
} |
} |
301 |
|
|
302 |
|
|
|
void |
|
|
set_default_kserver (void) |
|
|
{ |
|
|
char * p = get_reg_entry_keyserver ("Default"); |
|
|
free_if_alloc (default_keyserver); |
|
|
default_keyserver = p && *p != ' ' ? p : m_strdup (DEF_HKP_KEYSERVER); |
|
|
p = get_reg_entry_keyserver ("Default_Port"); |
|
|
if (p && *p) { |
|
|
default_keyserver_port = (u16)strtoul (p, NULL, 10); |
|
|
free_if_alloc (p); |
|
|
} |
|
|
else |
|
|
default_keyserver_port = HKP_PORT; |
|
|
} |
|
|
|
|
|
|
|
303 |
/* Initialize the Winsock2 interface.*/ |
/* Initialize the Winsock2 interface.*/ |
304 |
int |
int |
305 |
wsock_init (void) |
wsock_init (void) |
306 |
{ |
{ |
307 |
WSADATA wsa; |
WSADATA wsa; |
308 |
|
|
309 |
if (WSAStartup (0x202, &wsa)) |
if (WSAStartup (0x202, &wsa)) { |
310 |
|
log_debug ("wsock_init: WSAStartup failed ec=%d\r\n", net_errno); |
311 |
return WPTERR_WINSOCK_INIT; |
return WPTERR_WINSOCK_INIT; |
312 |
set_default_kserver (); |
} |
313 |
return 0; |
return 0; |
314 |
} |
} |
315 |
|
|
318 |
void |
void |
319 |
wsock_end (void) |
wsock_end (void) |
320 |
{ |
{ |
321 |
|
char *p; |
322 |
int i; |
int i; |
323 |
|
|
324 |
|
p = make_special_filename (CSIDL_APPDATA, "winpt\\keyserver.conf", NULL); |
325 |
|
kserver_save_conf (p); |
326 |
|
free_if_alloc (p); |
327 |
free_if_alloc (default_keyserver); |
free_if_alloc (default_keyserver); |
|
default_keyserver = NULL; |
|
328 |
for (i=0; i < MAX_KEYSERVERS; i++) { |
for (i=0; i < MAX_KEYSERVERS; i++) { |
329 |
if (server[i].used) |
if (server[i].used && server[i].name != NULL) |
330 |
free_if_alloc (server[i].name); |
free_if_alloc (server[i].name); |
331 |
} |
} |
332 |
|
kserver_proxy_release (&proxy); |
333 |
WSACleanup (); |
WSACleanup (); |
334 |
} |
} |
335 |
|
|
342 |
int ec = WSAGetLastError (); |
int ec = WSAGetLastError (); |
343 |
|
|
344 |
switch (ec) { |
switch (ec) { |
345 |
case WSAENETDOWN: |
case WSAENETDOWN: |
346 |
return _("The network subsystem has failed"); |
return _("The network subsystem has failed"); |
347 |
case WSAHOST_NOT_FOUND: |
case WSAHOST_NOT_FOUND: |
348 |
return _("Authoritative Answer Host not found"); |
return _("Authoritative Answer Host not found"); |
349 |
case WSAETIMEDOUT: |
case WSAETIMEDOUT: |
350 |
return _("The connection has been dropped because of a network failure"); |
return _("The connection has been dropped because of a network failure"); |
351 |
default: |
default: |
352 |
_snprintf (buf, sizeof (buf) -1, _("Unknown Winsock error ec=%d"),ec); |
_snprintf (buf, sizeof (buf) -1, _("Unknown Winsock error ec=%d"),ec); |
353 |
return buf; |
return buf; |
354 |
} |
} |
356 |
} |
} |
357 |
|
|
358 |
|
|
359 |
|
/* Set default socket timeout for all reading operations. */ |
360 |
|
void |
361 |
|
kserver_set_socket_timeout (int nsec) |
362 |
|
{ |
363 |
|
if (nsec < 0) |
364 |
|
nsec = 0; |
365 |
|
default_socket_timeout = nsec; |
366 |
|
} |
367 |
|
|
368 |
|
|
369 |
/* Return the last keyserver error as a string. */ |
/* Return the last keyserver error as a string. */ |
370 |
const char* |
const char* |
371 |
kserver_strerror (void) |
kserver_strerror (void) |
379 |
/* Read a keyserver from the list at index @idx. |
/* Read a keyserver from the list at index @idx. |
380 |
Return value: keyserver name at the given position. */ |
Return value: keyserver name at the given position. */ |
381 |
const char* |
const char* |
382 |
kserver_get_hostname (int idx, int type, u16 *port) |
kserver_get_hostname (int idx, int type, WORD *port) |
383 |
{ |
{ |
384 |
if (type == -1) { |
if (type == -1) { |
385 |
*port = default_keyserver_port; |
*port = default_keyserver_port; |
408 |
} |
} |
409 |
|
|
410 |
|
|
411 |
/* |
/* If the request contains the keyid, it have to be |
412 |
* If the request contains the keyid, it have to be |
always postfix with '0x'+keyid. This code checks |
413 |
* always postfix with '0x'+keyid. This code checks |
if the keyid is a decimal value and if so if it contains |
414 |
* if the keyid is a decimal value and if so if it contains |
the '0x'. If not it is inserted. */ |
|
* the '0x'. If not it is inserted. |
|
|
*/ |
|
415 |
const char* |
const char* |
416 |
kserver_check_keyid (const char *keyid) |
kserver_check_keyid (const char *keyid) |
417 |
{ |
{ |
418 |
static char id[20]; |
static char id[21]; |
419 |
|
|
420 |
if (strstr (keyid, "@")) |
if (strstr (keyid, "@")) |
421 |
return keyid; /* email address */ |
return keyid; /* email address */ |
425 |
return id; |
return id; |
426 |
} |
} |
427 |
return keyid; |
return keyid; |
428 |
} /* kserver_check_keyid */ |
} |
429 |
|
|
430 |
|
|
431 |
|
/* Update the keyserver proxy user. */ |
432 |
static void |
static void |
433 |
kserver_update_proxyuser (const char *proxy_user, const char *proxy_pass) |
update_proxy_user (const char *proxy_user, const char *proxy_pass) |
434 |
{ |
{ |
435 |
char t[257]; /* user:pass = 127+1+127+1 = 257 */ |
char t[257]; /* user:pass = 127+1+127+1 = 257 */ |
436 |
int n = 0; |
int n = 0; |
441 |
if (!proxy.base64_user) |
if (!proxy.base64_user) |
442 |
BUG (0); |
BUG (0); |
443 |
_snprintf (t, sizeof (t)-1, "%s:%s", proxy_user, proxy_pass); |
_snprintf (t, sizeof (t)-1, "%s:%s", proxy_user, proxy_pass); |
444 |
base64_encode (t, proxy.base64_user, 257); |
base64_encode (t, proxy.base64_user); |
445 |
free_if_alloc (proxy.user); |
free_if_alloc (proxy.user); |
446 |
free_if_alloc (proxy.pass); |
free_if_alloc (proxy.pass); |
447 |
proxy.user = m_strdup (proxy_user); |
proxy.user = m_strdup (proxy_user); |
448 |
proxy.pass = m_strdup (proxy_pass); |
proxy.pass = m_strdup (proxy_pass); |
449 |
} /* kserver_update_proxyuser */ |
} |
450 |
|
|
451 |
|
|
452 |
/* Check that the given buffer contains a valid keyserver URL. */ |
/* Check that the given buffer contains a valid keyserver URL. */ |
479 |
|
|
480 |
/* Get the port number from the given URL. */ |
/* Get the port number from the given URL. */ |
481 |
static int |
static int |
482 |
proto_from_URL (const char * buf) |
proto_from_URL (const char *buf) |
483 |
{ |
{ |
484 |
if (strstr( buf, "ldap")) |
if (strstr( buf, "ldap")) |
485 |
return KSPROTO_LDAP; |
return KSPROTO_LDAP; |
490 |
|
|
491 |
|
|
492 |
void |
void |
493 |
keyserver_set_default (const char * hostname, u16 port) |
keyserver_set_default (const char *hostname, WORD port) |
494 |
{ |
{ |
495 |
if (hostname) { |
if (hostname != NULL) { |
496 |
free_if_alloc (default_keyserver); |
free_if_alloc (default_keyserver); |
497 |
default_keyserver = m_strdup (hostname); |
default_keyserver = m_strdup (hostname); |
498 |
if (!default_keyserver) |
if (!default_keyserver) |
499 |
BUG (0); |
BUG (0); |
500 |
default_keyserver_port = port; |
default_keyserver_port = port; |
501 |
} |
} |
502 |
|
if (!port) |
503 |
|
port = HKP_PORT; |
504 |
|
if (!default_keyserver) |
505 |
|
default_keyserver = m_strdup (DEF_HKP_KEYSERVER); |
506 |
server[0].name = m_strdup (default_keyserver); |
server[0].name = m_strdup (default_keyserver); |
507 |
server[0].used = 1; |
server[0].used = 1; |
508 |
server[0].port = port; |
server[0].port = port; |
509 |
server[0].proto = proto_from_URL (default_keyserver); |
server[0].proto = proto_from_URL (default_keyserver); |
510 |
} /* keyserver_set_default */ |
} |
511 |
|
|
512 |
|
|
513 |
static const char * |
/* Skip all kind of whitespace chars in @str. */ |
514 |
skip_whitespace (const char * str) |
static const char* |
515 |
|
skip_whitespace (const char *str) |
516 |
{ |
{ |
517 |
while (str && *str) |
while (str && *str) { |
|
{ |
|
518 |
if (*str == ' ' || |
if (*str == ' ' || |
519 |
*str == '\t' || |
*str == '\t' || |
520 |
*str == '\n' || |
*str == '\n' || |
521 |
*str == '\r') |
*str == '\r') { |
|
{ |
|
522 |
str++; |
str++; |
523 |
continue; |
continue; |
524 |
} |
} |
528 |
} |
} |
529 |
|
|
530 |
|
|
531 |
|
/* Save the keyserver config file in @conf. */ |
532 |
int |
int |
533 |
kserver_load_conf (const char * conf) |
kserver_save_conf (const char *conf) |
534 |
{ |
{ |
|
struct stat statbuf; |
|
535 |
FILE *fp; |
FILE *fp; |
|
char buf[1024], * s, *p; |
|
|
char *user = NULL, *pass = NULL; |
|
536 |
int pos; |
int pos; |
537 |
|
|
538 |
|
fp = fopen (conf, "wb"); |
539 |
|
if (!fp) { |
540 |
|
msg_box (NULL, _("Could not save keyserver.conf file"), |
541 |
|
_("Keyserver"), MB_ERR); |
542 |
|
return -1; |
543 |
|
} |
544 |
|
|
545 |
|
fprintf (fp, "# do NOT manually modify this file, it will be " |
546 |
|
"generated automatically!!\r\n"); |
547 |
|
for (pos = 0; pos < MAX_KEYSERVERS; pos++) { |
548 |
|
if (!server[pos].used) |
549 |
|
continue; |
550 |
|
fprintf (fp, "%s:%d\r\n", server[pos].name, server[pos].port); |
551 |
|
} |
552 |
|
fclose (fp); |
553 |
|
return 0; |
554 |
|
} |
555 |
|
|
556 |
|
|
557 |
|
/* Load the keyserver config file @conf. */ |
558 |
|
int |
559 |
|
kserver_load_conf (const char *conf) |
560 |
|
{ |
561 |
|
FILE *fp; |
562 |
|
char buf[1024], *s, *p; |
563 |
|
char *user = NULL, *pass = NULL; |
564 |
int no_config=0, chk_pos=0; |
int no_config=0, chk_pos=0; |
565 |
|
int pos; |
566 |
|
|
567 |
for (pos = 0; pos < MAX_KEYSERVERS; pos++) { |
for (pos = 0; pos < MAX_KEYSERVERS; pos++) { |
568 |
server[pos].used = 0; |
server[pos].used = 0; |
578 |
} |
} |
579 |
no_config=1; |
no_config=1; |
580 |
} |
} |
581 |
get_reg_proxy_prefs (&proxy.host, &proxy.port, &user, &pass); |
get_reg_proxy_prefs (&proxy); |
582 |
if (user && pass) |
if (user && pass) |
583 |
kserver_update_proxyuser (user, pass); |
update_proxy_user (user, pass); |
584 |
else if (user && !pass || !user && pass) { |
else if (user && !pass || !user && pass) { |
585 |
msg_box( NULL, _("Invalid proxy configuration." |
msg_box (NULL, _("Invalid proxy configuration." |
586 |
"You need to set a user and a password" |
"You need to set a user and a password" |
587 |
"to use proxy authentication!"), _("Proxy Error"), MB_ERR ); |
"to use proxy authentication!"), |
588 |
|
_("Proxy Error"), MB_ERR); |
589 |
} |
} |
590 |
/* check if the host has a http:// prefix and skip it */ |
/* check if the host has a http:// prefix and skip it */ |
591 |
if( proxy.host && !strncmp( proxy.host, "http://", 7 ) ) { |
if (proxy.host && !strncmp (proxy.host, "http://", 7)) { |
592 |
char *pr = m_strdup (proxy.host+7); |
char *host = m_strdup (proxy.host+7); |
593 |
if (!pr) |
if (!host) |
594 |
BUG (NULL); |
BUG (0); |
595 |
free_if_alloc (proxy.host); |
free_if_alloc (proxy.host); |
596 |
proxy.host = pr; |
proxy.host = host; |
597 |
} |
} |
598 |
free_if_alloc (user); |
free_if_alloc (user); |
599 |
free_if_alloc (pass); |
free_if_alloc (pass); |
600 |
|
|
601 |
pos = 0; |
pos = 0; |
602 |
while( fp && !feof( fp ) ) { |
while (fp && !feof (fp)) { |
603 |
s = fgets (buf, sizeof (buf)-1, fp); |
s = fgets (buf, sizeof (buf)-1, fp); |
604 |
if (!s) |
if (!s) |
605 |
break; |
break; |
616 |
_("Keyserver Error"), MB_ERR); |
_("Keyserver Error"), MB_ERR); |
617 |
continue; |
continue; |
618 |
} |
} |
619 |
chk_pos=6; |
chk_pos = 6; |
620 |
if (strstr (s, "finger")) |
if (strstr (s, "finger")) |
621 |
chk_pos = 10; |
chk_pos = 10; |
622 |
p = strchr (s+chk_pos, ':'); |
p = strchr (s+chk_pos, ':'); |
637 |
} |
} |
638 |
pos++; |
pos++; |
639 |
if (pos > MAX_KEYSERVERS) { |
if (pos > MAX_KEYSERVERS) { |
640 |
msg_box (NULL, _("The keyserver limit is exceeded"), _("Keyserver Warning"), MB_INFO); |
msg_box (NULL, _("The keyserver limit is exceeded"), |
641 |
|
_("Keyserver Warning"), MB_INFO); |
642 |
break; |
break; |
643 |
} |
} |
644 |
} |
} |
645 |
if (fp) |
if (fp) |
646 |
fclose (fp); |
fclose (fp); |
647 |
if (!pos) { |
if (!pos) { |
648 |
/* only issue an error if the config file exist but it has no valid entries. */ |
/* only issue an error if the config file exist but |
649 |
|
it has no valid entries. */ |
650 |
keyserver_set_default (NULL, HKP_PORT); |
keyserver_set_default (NULL, HKP_PORT); |
651 |
if (!no_config) |
if (!no_config) |
652 |
return WPTERR_CONFIG_FILE; |
return WPTERR_CONFIG_FILE; |
653 |
} |
} |
654 |
|
|
|
if (!stat (conf, &statbuf)) |
|
|
conf_timestamp = statbuf.st_mtime; |
|
655 |
return 0; |
return 0; |
656 |
} /* kserver_load_conf */ |
} |
|
|
|
|
|
|
|
const char * |
|
|
kserver_get_proxy (int * r_port) |
|
|
{ |
|
|
if (proxy.host) { |
|
|
if (r_port) |
|
|
*r_port = proxy.port; |
|
|
return proxy.host; |
|
|
} |
|
|
return NULL; |
|
|
} /* kserver_get_proxy */ |
|
|
|
|
|
|
|
|
const char * |
|
|
kserver_get_proxy_info (int id) |
|
|
{ |
|
|
switch (id) { |
|
|
case PROXY_USER: return proxy.user; |
|
|
case PROXY_PASS: return proxy.pass; |
|
|
} |
|
|
return NULL; |
|
|
} /* kserver_get_proxy_info */ |
|
657 |
|
|
658 |
|
|
659 |
/* Connect to the keyserver @hostname. */ |
/* Connect to the keyserver @hostname (port @port). |
660 |
|
Return value: 0 on success */ |
661 |
int |
int |
662 |
kserver_connect (const char *hostname, u16 port, int *conn_fd) |
kserver_connect (const char *hostname, WORD port, int *conn_fd) |
663 |
{ |
{ |
664 |
int rc, fd; |
int rc, fd; |
665 |
u32 iaddr; |
DWORD iaddr; |
666 |
char host[128] = {0}; |
char host[128] = {0}; |
667 |
struct hostent *hp; |
struct hostent *hp; |
668 |
struct sockaddr_in sock; |
struct sockaddr_in sock; |
693 |
memcpy (&sock.sin_addr, hp->h_addr, hp->h_length); |
memcpy (&sock.sin_addr, hp->h_addr, hp->h_length); |
694 |
} |
} |
695 |
else { |
else { |
696 |
log_debug ("gethostbyname: failed.\r\n"); |
log_debug ("gethostbyname: failed ec=%d.\r\n", net_errno); |
697 |
return WPTERR_WINSOCK_RESOLVE; |
return WPTERR_WINSOCK_RESOLVE; |
698 |
} |
} |
699 |
fd = socket (AF_INET, SOCK_STREAM, 0); |
fd = socket (AF_INET, SOCK_STREAM, 0); |
701 |
return WPTERR_WINSOCK_SOCKET; |
return WPTERR_WINSOCK_SOCKET; |
702 |
rc = connect (fd, (struct sockaddr *) &sock, sizeof (sock)); |
rc = connect (fd, (struct sockaddr *) &sock, sizeof (sock)); |
703 |
if (rc == SOCKET_ERROR) { |
if (rc == SOCKET_ERROR) { |
704 |
log_debug ("connect: failed.\r\n"); |
log_debug ("connect: failed ec=%d.\r\n", net_errno); |
705 |
closesocket (fd); |
closesocket (fd); |
706 |
return WPTERR_WINSOCK_CONNECT; |
return WPTERR_WINSOCK_CONNECT; |
707 |
} |
} |
708 |
|
|
709 |
|
if (proxy.proto == PROXY_PROTO_SOCKS5) { |
710 |
|
rc = socks_handshake (&proxy, fd, hostname, port); |
711 |
|
if (rc) { |
712 |
|
closesocket (fd); |
713 |
|
return WPTERR_GENERAL; /* XXX: use better code. */ |
714 |
|
} |
715 |
|
} |
716 |
|
|
717 |
if (conn_fd) |
if (conn_fd) |
718 |
*conn_fd = fd; |
*conn_fd = fd; |
719 |
WSASetLastError (0); |
WSASetLastError (0); |
720 |
return 0; |
return 0; |
721 |
} /* kserver_connect */ |
} |
722 |
|
|
723 |
|
|
724 |
/* Perform URL-encoding on the given pubkey blob. */ |
static bool |
725 |
static char* |
URL_must_encoded (const char *url) |
|
kserver_urlencode (const char *pubkey, size_t octets, size_t *newlen) |
|
726 |
{ |
{ |
727 |
char *p, numbuf[5]; |
if (strchr (url, '.') || strchr (url, '@') || strchr (url, ' ')) |
728 |
size_t j = 0; |
return true; |
729 |
size_t i, size; |
return false; |
730 |
|
} |
|
p = new char [2*octets]; |
|
|
if (!p) |
|
|
BUG (0); |
|
731 |
|
|
732 |
for (size=0, i=0; i < octets; i++) { |
|
733 |
if (isalnum (pubkey[i]) || pubkey[i] == '-') { |
/* Perform URL encoding of the given data. */ |
734 |
p[size] = pubkey[i]; |
static char* |
735 |
size++; |
URL_encode (const char *url, size_t ulen, size_t *ret_nlen) |
736 |
} |
{ |
737 |
else if (pubkey[i] == ' ') { |
gpgme_data_t hd; |
738 |
p[size] = '+'; |
char numbuf[5], *pp, *p; |
739 |
size++; |
size_t i, n; |
740 |
} |
|
741 |
|
gpgme_data_new (&hd); |
742 |
|
for (i=0; i < ulen; i++) { |
743 |
|
if (isalnum (url[i]) || url[i] == '-') |
744 |
|
gpg_data_putc (hd, url[i]); |
745 |
|
else if (url[i] == ' ') |
746 |
|
gpg_data_putc (hd, '+'); |
747 |
else { |
else { |
748 |
sprintf(numbuf, "%%%02X", pubkey[i]); |
sprintf (numbuf, "%%%02X", url[i]); |
749 |
for (j = 0; j<strlen(numbuf); j++) { |
gpgme_data_write (hd, numbuf, strlen (numbuf)); |
|
p[size] = numbuf[j]; |
|
|
size++; |
|
|
} |
|
750 |
} |
} |
751 |
} |
} |
752 |
p[size] = '\0'; |
gpg_data_putc (hd, '\0'); |
753 |
*newlen = size; |
|
754 |
|
/* Copy memory to avoid that we need to use gpgme_free later. */ |
755 |
|
pp = gpgme_data_release_and_get_mem (hd, &n); |
756 |
|
p = m_strdup (pp); |
757 |
|
gpgme_free (pp); |
758 |
|
if (ret_nlen) |
759 |
|
*ret_nlen = n; |
760 |
return p; |
return p; |
761 |
} |
} |
762 |
|
|
763 |
|
|
764 |
/* Format a request for the given keyid (send). */ |
/* Format a request for the given keyid (send). */ |
765 |
static char* |
static char* |
766 |
kserver_send_request (const char *hostname, u16 port, const char *pubkey, int octets) |
kserver_send_request (const char *hostname, WORD port, |
767 |
|
const char *pubkey, int octets) |
768 |
{ |
{ |
769 |
char *request = NULL, *enc_pubkey = NULL; |
char *request = NULL; |
770 |
int reqlen; |
char *enc_pubkey = NULL; |
771 |
size_t enc_octets; |
size_t enc_octets; |
772 |
|
int reqlen; |
773 |
|
|
774 |
log_debug ("kserver_send_request: %s:%d\n", hostname, port); |
log_debug ("kserver_send_request: %s:%d\r\n", hostname, port); |
775 |
|
|
776 |
if (!port) |
if (!port) |
777 |
port = HKP_PORT; |
port = HKP_PORT; |
778 |
reqlen = 512 + strlen (hostname) + 2*strlen (pubkey); |
reqlen = 512 + strlen (hostname) + 2*strlen (pubkey); |
779 |
|
if (proxy.proto == PROXY_PROTO_HTTP && proxy.base64_user) |
780 |
|
reqlen += strlen (proxy.base64_user) + 1; |
781 |
request = new char[reqlen]; |
request = new char[reqlen]; |
782 |
if (!request) |
if (!request) |
783 |
BUG (0); |
BUG (0); |
784 |
enc_pubkey = kserver_urlencode (pubkey, octets, &enc_octets); |
enc_pubkey = URL_encode (pubkey, octets, &enc_octets); |
785 |
if (!enc_pubkey || !enc_octets) { |
if (!enc_pubkey || !enc_octets) { |
786 |
free_if_alloc (request); |
free_if_alloc (request); |
787 |
return NULL; |
return NULL; |
788 |
} |
} |
789 |
|
|
790 |
if (proxy.user) { |
if (proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
791 |
_snprintf (request, reqlen-1, |
_snprintf (request, reqlen-1, |
792 |
"POST http://%s:%d/pks/add HTTP/1.0\r\n" |
"POST http://%s:%d/pks/add HTTP/1.0\r\n" |
793 |
"Referer: \r\n" |
"Referer: \r\n" |
798 |
"Content-length: %d\r\n" |
"Content-length: %d\r\n" |
799 |
"\r\n" |
"\r\n" |
800 |
"keytext=%s" |
"keytext=%s" |
801 |
"\n", |
"\r\n", |
802 |
skip_type_prefix (hostname), port, hostname, port, |
skip_type_prefix (hostname), port, hostname, port, |
803 |
proxy.base64_user, enc_octets+9, enc_pubkey); |
proxy.base64_user, enc_octets+9, enc_pubkey); |
804 |
} |
} |
812 |
"Content-length: %d\r\n" |
"Content-length: %d\r\n" |
813 |
"\r\n" |
"\r\n" |
814 |
"keytext=%s" |
"keytext=%s" |
815 |
"\n", |
"\r\n", |
816 |
skip_type_prefix (hostname), port, enc_octets+9, enc_pubkey); |
skip_type_prefix (hostname), port, |
817 |
|
enc_octets+9, enc_pubkey); |
818 |
} |
} |
819 |
free_if_alloc (enc_pubkey); |
free_if_alloc (enc_pubkey); |
820 |
|
|
821 |
log_debug ("%s\n", request); |
log_debug ("%s\r\n", request); |
822 |
return request; |
return request; |
823 |
} /* kserver_send_request */ |
} |
824 |
|
|
825 |
|
|
|
/* Interface receiving a public key. */ |
|
826 |
int |
int |
827 |
kserver_recvkey (const char *hostname, u16 port, const char *keyid, char *key, int maxkeylen) |
kserver_recvkey_ext (const char *hostname, WORD port, const char *keyid, |
828 |
{ |
char **r_key, int *r_keylen) |
829 |
int rc, n; |
{ |
|
int conn_fd; |
|
830 |
char *request = NULL; |
char *request = NULL; |
831 |
|
int conn_fd; |
832 |
|
int rc, n; |
833 |
|
|
834 |
if (!port) |
if (!port) |
835 |
port = HKP_PORT; |
port = HKP_PORT; |
836 |
hkp_err = 0; /* reset */ |
hkp_err = 0; /* reset */ |
842 |
if (!request) |
if (!request) |
843 |
BUG (0); |
BUG (0); |
844 |
|
|
845 |
if (proxy.host && proxy.user) { |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
846 |
_snprintf (request, 300, |
_snprintf (request, 300, |
847 |
"GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n" |
"GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n" |
848 |
"Proxy-Authorization: Basic %s\r\n\r\n", |
"Proxy-Authorization: Basic %s\r\n\r\n", |
849 |
skip_type_prefix (hostname), port, keyid, proxy.base64_user); |
skip_type_prefix (hostname), port, keyid, proxy.base64_user); |
850 |
} |
} |
851 |
else if (proxy.host) { |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
852 |
_snprintf (request, 300, |
_snprintf (request, 300, |
853 |
"GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n", |
"GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n", |
854 |
skip_type_prefix (hostname), port, keyid); |
skip_type_prefix (hostname), port, keyid); |
866 |
goto leave; |
goto leave; |
867 |
} |
} |
868 |
|
|
869 |
rc = sock_read( conn_fd, key, maxkeylen, &n ); |
rc = sock_read_ext (conn_fd, r_key, &n); |
870 |
if( rc == SOCKET_ERROR ) { |
if (rc == SOCKET_ERROR) { |
871 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
872 |
goto leave; |
goto leave; |
873 |
} |
} |
874 |
|
|
875 |
log_debug("%s\r\n", key); |
if (r_keylen) |
876 |
rc = check_hkp_response (key, 1); |
*r_keylen = n; |
877 |
|
log_debug ("%s\r\n", *r_key); |
878 |
|
rc = check_hkp_response (*r_key, 1); |
879 |
if (rc) |
if (rc) |
880 |
goto leave; |
goto leave; |
881 |
|
|
885 |
closesocket (conn_fd); |
closesocket (conn_fd); |
886 |
free_if_alloc (request); |
free_if_alloc (request); |
887 |
return rc; |
return rc; |
888 |
} /* kserver_recvkey */ |
} |
889 |
|
|
890 |
|
/* Interface for receiving a public key. */ |
891 |
|
int |
892 |
|
kserver_recvkey (const char *hostname, WORD port, const char *keyid, |
893 |
|
char *key, int maxkeylen) |
894 |
|
{ |
895 |
|
char *tmpkey; |
896 |
|
int rc, nread=0; |
897 |
|
|
898 |
|
/* XXX: just a wrapper around the new method, replace it |
899 |
|
soon as possible. */ |
900 |
|
rc = kserver_recvkey_ext (hostname, port, keyid, &tmpkey, &nread); |
901 |
|
if (rc) |
902 |
|
return rc; |
903 |
|
|
904 |
|
if (nread > maxkeylen) { |
905 |
|
free_if_alloc (tmpkey); |
906 |
|
return WPTERR_GENERAL; |
907 |
|
} |
908 |
|
strcpy (key, tmpkey); |
909 |
|
free_if_alloc (tmpkey); |
910 |
|
return 0; |
911 |
|
} |
912 |
|
|
913 |
|
|
914 |
/* Interface to send a public key. */ |
/* Interface to send a public key. */ |
915 |
int |
int |
916 |
kserver_sendkey (const char *hostname, u16 port, const char *pubkey, int len ) |
kserver_sendkey (const char *hostname, WORD port, const char *pubkey, int len ) |
917 |
{ |
{ |
|
int n, rc; |
|
|
int conn_fd; |
|
918 |
char *request = NULL; |
char *request = NULL; |
919 |
char log[2048]; |
char log[2048] = {0}; |
920 |
|
int conn_fd, n; |
921 |
|
int rc; |
922 |
|
|
923 |
hkp_err = 0; /* reset */ |
hkp_err = 0; /* reset */ |
924 |
rc = kserver_connect (hostname, port, &conn_fd); |
rc = kserver_connect (hostname, port, &conn_fd); |
931 |
goto leave; |
goto leave; |
932 |
} |
} |
933 |
|
|
934 |
rc = sock_write( conn_fd, request, lstrlen(request) ); |
rc = sock_write (conn_fd, request, strlen (request)); |
935 |
if( rc == SOCKET_ERROR ) { |
if (rc == SOCKET_ERROR) { |
936 |
rc = WPTERR_WINSOCK_SENDKEY; |
rc = WPTERR_WINSOCK_SENDKEY; |
937 |
goto leave; |
goto leave; |
938 |
} |
} |
939 |
|
|
940 |
rc = sock_read( conn_fd, log, sizeof (log)-1, &n ); |
rc = sock_read (conn_fd, log, sizeof (log)-1, &n); |
941 |
if( rc == SOCKET_ERROR ) { |
if (rc == SOCKET_ERROR) { |
942 |
rc = WPTERR_WINSOCK_SENDKEY; |
rc = WPTERR_WINSOCK_SENDKEY; |
943 |
goto leave; |
goto leave; |
944 |
} |
} |
945 |
|
|
946 |
log_debug ("kserver_sendkey:\r\n%s\r\n", log); |
log_debug ("kserver_sendkey: read %d bytes\r\n%s\r\n", n, log); |
|
|
|
947 |
rc = check_hkp_response (log, 0); |
rc = check_hkp_response (log, 0); |
948 |
if( rc ) |
if (rc) |
949 |
goto leave; |
goto leave; |
950 |
|
|
951 |
WSASetLastError (0); |
WSASetLastError (0); |
954 |
closesocket (conn_fd); |
closesocket (conn_fd); |
955 |
free_if_alloc (request); |
free_if_alloc (request); |
956 |
return rc; |
return rc; |
957 |
} /* kserver_sendkey */ |
} |
958 |
|
|
959 |
|
|
960 |
|
/* Check keyserver response. */ |
961 |
|
static int |
962 |
|
kserver_search_chkresp (int fd) |
963 |
|
{ |
964 |
|
char buf[128]; |
965 |
|
int n=0; |
966 |
|
|
967 |
|
/* parse response 'HTTP/1.0 500 OK' */ |
968 |
|
if (sock_getline (fd, buf, 127, &n)) |
969 |
|
return WPTERR_KEYSERVER_NOTFOUND; |
970 |
|
|
971 |
|
log_debug ("kserver_search_chkpresp: %s\r\n", buf); |
972 |
|
if (strncmp (buf, "HTTP/1.", 7)) |
973 |
|
return WPTERR_KEYSERVER_NOTFOUND; |
974 |
|
if (strncmp (buf+(8+1), "200", 3)) |
975 |
|
return WPTERR_KEYSERVER_NOTFOUND; |
976 |
|
return 0; |
977 |
|
} |
978 |
|
|
979 |
|
|
980 |
|
/* End the keyserver search procedure. */ |
981 |
|
void |
982 |
|
kserver_search_end (int conn_fd) |
983 |
|
{ |
984 |
|
log_debug ("kserver_search_end: fd=%d\r\n", conn_fd); |
985 |
|
closesocket (conn_fd); |
986 |
|
} |
987 |
|
|
988 |
|
|
989 |
|
/* Begin keyserver search procedure. */ |
990 |
int |
int |
991 |
kserver_search_init (const char * hostname, u16 port, const char * keyid, int * conn_fd) |
kserver_search_begin (const char *hostname, WORD port, |
992 |
|
const char *pattern, int *conn_fd) |
993 |
{ |
{ |
994 |
|
char *request = NULL; |
995 |
|
char *enc_patt = NULL; |
996 |
|
int n; |
997 |
int rc, sock_fd; |
int rc, sock_fd; |
|
int n=0; |
|
|
char * request = NULL; |
|
998 |
|
|
999 |
rc = kserver_connect (hostname, port, &sock_fd); |
rc = kserver_connect (hostname, port, &sock_fd); |
1000 |
if (rc) { |
if (rc) { |
1001 |
*conn_fd = 0; |
*conn_fd = 0; |
1002 |
goto leave; |
goto leave; |
1003 |
} |
} |
1004 |
|
|
1005 |
n=300; |
enc_patt = URL_encode (pattern, strlen (pattern), NULL); |
1006 |
|
n = 140 + strlen (enc_patt) + strlen (hostname) + 32 + 2; |
1007 |
|
if (proxy.base64_user) |
1008 |
|
n += strlen (proxy.base64_user) + 1; |
1009 |
request = new char[n+1]; |
request = new char[n+1]; |
1010 |
if (!request) |
if (!request) |
1011 |
BUG (0); |
BUG (0); |
1012 |
|
|
1013 |
if (proxy.host && proxy.user) { |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
1014 |
_snprintf (request, n, |
_snprintf (request, n, |
1015 |
"GET http://%s:%d/pks/lookup?op=index&search=%s HTTP/1.0\r\n" |
"GET http://%s:%d/pks/lookup?op=index&search=%s HTTP/1.0\r\n" |
1016 |
"Proxy-Authorization: Basic %s\r\n\r\n", |
"Proxy-Authorization: Basic %s\r\n\r\n", |
1017 |
skip_type_prefix (hostname), port, keyid, proxy.base64_user); |
skip_type_prefix (hostname), port, enc_patt, proxy.base64_user); |
1018 |
} |
} |
1019 |
else if (proxy.host) { |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
1020 |
_snprintf (request, n, |
_snprintf (request, n, |
1021 |
"GET http://%s:%d/pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n", |
"GET http://%s:%d/pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n", |
1022 |
skip_type_prefix (hostname), port, keyid); |
skip_type_prefix (hostname), port, enc_patt); |
1023 |
} |
} |
1024 |
else { |
else { |
1025 |
_snprintf (request, n, |
_snprintf (request, n, |
1026 |
"GET /pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n", keyid); |
"GET /pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n", |
1027 |
|
enc_patt); |
1028 |
} |
} |
1029 |
|
|
1030 |
log_debug ("kserver_search_init:\r\n%s\r\n", request); |
log_debug ("kserver_search_begin:\r\n%s\r\n", request); |
1031 |
|
|
1032 |
if (sock_write (sock_fd, request, strlen (request)) == SOCKET_ERROR) { |
if (sock_write (sock_fd, request, strlen (request)) == SOCKET_ERROR) { |
1033 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1034 |
goto leave; |
goto leave; |
1035 |
} |
} |
1036 |
|
|
1037 |
|
rc = kserver_search_chkresp (sock_fd); |
1038 |
|
if (rc) { |
1039 |
|
closesocket (sock_fd); |
1040 |
|
sock_fd = 0; |
1041 |
|
} |
1042 |
|
|
1043 |
*conn_fd = sock_fd; |
*conn_fd = sock_fd; |
1044 |
|
|
1045 |
leave: |
leave: |
1046 |
free_if_alloc (request); |
free_if_alloc (request); |
1047 |
|
free_if_alloc (enc_patt); |
1048 |
return rc; |
return rc; |
1049 |
} /* kserver_search_init */ |
} |
1050 |
|
|
1051 |
|
|
1052 |
int |
|
1053 |
kserver_search_chkresp (int fd) |
|
1054 |
|
/* Convert an iso date @iso_date (YYYY-MM-DD) into the locale |
1055 |
|
representation and store it into @loc_date. |
1056 |
|
Return value: 0 on success. */ |
1057 |
|
static int |
1058 |
|
parse_iso_date (const char *iso_date, char *loc_date, size_t loclen) |
1059 |
{ |
{ |
1060 |
char buf[128]; |
SYSTEMTIME st; |
1061 |
int n=0; |
char buf[16] = {0}, *p; |
1062 |
|
int pos=0; |
1063 |
|
|
1064 |
|
strncpy (buf, iso_date, sizeof (buf)-1); |
1065 |
|
p = strtok (buf, "-"); |
1066 |
|
while (p != NULL) { |
1067 |
|
switch (pos) { |
1068 |
|
case 0: st.wYear = (WORD)atoi (p); pos++; break; |
1069 |
|
case 1: st.wMonth = (WORD)atoi (p); pos++; break; |
1070 |
|
case 2: st.wDay = (WORD)atoi (p); pos++; break; |
1071 |
|
default: break; |
1072 |
|
} |
1073 |
|
p = strtok (NULL, "-"); |
1074 |
|
} |
1075 |
|
if (pos != 3) |
1076 |
|
return -1; |
1077 |
|
|
1078 |
/* parse response 'HTTP/1.0 500 OK' */ |
if (!GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, |
1079 |
if (sock_getline (fd, buf, 127, &n)) |
NULL, loc_date, loclen)) |
1080 |
return WPTERR_KEYSERVER_NOTFOUND; |
return -1; |
|
|
|
|
log_debug ("kserver_search_chkpresp: %s\r\n", buf); |
|
|
if (strncmp (buf, "HTTP/1.", 7)) |
|
|
return WPTERR_KEYSERVER_NOTFOUND; |
|
|
if (strncmp (buf+(8+1), "200", 3)) |
|
|
return WPTERR_KEYSERVER_NOTFOUND; |
|
1081 |
return 0; |
return 0; |
1082 |
} /* kserver_search_chkresp */ |
} |
1083 |
|
|
1084 |
|
|
1085 |
int |
int |
1086 |
kserver_search (int fd, keyserver_key * key) |
kserver_search_next (int fd, keyserver_key *key) |
1087 |
{ |
{ |
1088 |
char buf[1024], *p; |
char buf[1024], *p; |
1089 |
int uidlen, nbytes, pos = 0; |
int uidlen, nbytes, pos = 0; |
1090 |
|
|
1091 |
log_debug ("keyserver_search:\n"); |
log_debug ("keyserver_search_next:\r\n"); |
1092 |
|
|
1093 |
if (sock_getline (fd, buf, sizeof (buf) - 1, &nbytes)) |
if (sock_getline (fd, buf, sizeof (buf) - 1, &nbytes)) |
1094 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
1095 |
|
|
1096 |
log_debug ("%s\n", buf); |
/* XXX: use maschine readable option. */ |
1097 |
|
log_debug ("%s\r\n", buf); |
1098 |
|
|
1099 |
if (!strncmp (buf, "pub", 3)) { |
if (!strncmp (buf, "pub", 3)) { |
1100 |
int revoked = strstr (buf, "KEY REVOKED") != NULL? 1 : 0; |
int revoked = strstr (buf, "KEY REVOKED") != NULL? 1 : 0; |
1111 |
pos = p - buf + 5; |
pos = p - buf + 5; |
1112 |
memcpy (key->date, buf + pos, 10); |
memcpy (key->date, buf + pos, 10); |
1113 |
key->date[10] = '\0'; |
key->date[10] = '\0'; |
1114 |
|
parse_iso_date (key->date, key->date, sizeof (key->date)-1); |
1115 |
if (revoked) { |
if (revoked) { |
1116 |
strcpy (key->uid, "KEY REVOKED: not checked"); |
strcpy (key->uid, "KEY REVOKED: not checked"); |
1117 |
return 0; |
return 0; |
1122 |
p++; |
p++; |
1123 |
p++; |
p++; |
1124 |
uidlen = strlen (p) - 10; |
uidlen = strlen (p) - 10; |
1125 |
|
if (!strstr (p, "<") && !strstr (p, ">")) { |
1126 |
|
pos=0; |
1127 |
|
while (p && *p && pos < sizeof (key->uid)-1) { |
1128 |
|
if (*p == '<') |
1129 |
|
break; |
1130 |
|
key->uid[pos++] = *p++; |
1131 |
|
} |
1132 |
|
key->uid[pos] ='\0'; |
1133 |
|
return 0; |
1134 |
|
} |
1135 |
|
|
1136 |
|
if (uidlen > sizeof (key->uid)-1) |
1137 |
|
uidlen = sizeof (key->uid)-1; |
1138 |
memcpy (key->uid, p, uidlen); |
memcpy (key->uid, p, uidlen); |
1139 |
key->uid[uidlen] = '\0'; |
key->uid[uidlen] = '\0'; |
1140 |
strcat (key->uid, ">"); |
strcat (key->uid, ">"); |
1141 |
p = strchr (key->uid, '&'); |
p = strchr (key->uid, '&'); |
1142 |
if (p) { |
if (p) { |
1143 |
key->uid[(p-key->uid)] = '<'; |
key->uid[(p-key->uid)] = '<'; |
1144 |
memmove (key->uid+(p-key->uid)+1, key->uid+(p-key->uid)+4, strlen (key->uid)-3); |
memmove (key->uid+(p-key->uid)+1, key->uid+(p-key->uid)+4, |
1145 |
|
strlen (key->uid)-3); |
1146 |
} |
} |
1147 |
return 0; |
return 0; |
1148 |
} |
} |
1151 |
key->bits = 0; |
key->bits = 0; |
1152 |
memset (key, 0, sizeof *key); |
memset (key, 0, sizeof *key); |
1153 |
return 0; |
return 0; |
1154 |
} /* kserver_search */ |
} |
|
|
|
|
|
|
|
static int |
|
|
add_node( keyserver_cfgfile *cfg, const char *entry ) |
|
|
{ |
|
|
keyserver_node *node; |
|
|
char *p = NULL; |
|
|
|
|
|
p = strchr( entry, '=' ); |
|
|
if( p == NULL ) |
|
|
return WPTERR_GENERAL; |
|
|
node = new keyserver_node; |
|
|
if( !node ) |
|
|
BUG( NULL ); |
|
|
|
|
|
memset( node, 0, sizeof *node ); |
|
|
node->used = 1; |
|
|
|
|
|
node->host.used = 1; |
|
|
node->host.proto = proto_from_URL( entry ); |
|
|
memcpy( node->host.name, entry+7, p-entry-7 ); |
|
|
node->next = cfg->list; |
|
|
cfg->list = node; |
|
|
|
|
|
return 0; |
|
|
} /* add_node */ |
|
1155 |
|
|
1156 |
|
|
1157 |
|
/* Release mbmers in the proxy context @ctx. */ |
1158 |
void |
void |
1159 |
kserver_change_proxy( keyserver_proxy_ctx *ctx ) |
kserver_proxy_release (keyserver_proxy_t ctx) |
|
{ |
|
|
proxy.port = ctx->port; |
|
|
free_if_alloc (proxy.host); |
|
|
proxy.host = ctx->host? m_strdup( ctx->host ) : NULL; |
|
|
free_if_alloc( proxy.pass ); |
|
|
proxy.pass = ctx->pass? m_strdup( ctx->pass ) : NULL; |
|
|
free_if_alloc( proxy.user ); |
|
|
proxy.user = ctx->user? m_strdup( ctx->user ) : NULL; |
|
|
set_reg_proxy_prefs( proxy.host, proxy.port, proxy.user, proxy.pass ); |
|
|
} /* kserver_change_proxy */ |
|
|
|
|
|
|
|
|
int |
|
|
kserver_read_config( const char *fname, keyserver_cfgfile **ret_cfg ) |
|
1160 |
{ |
{ |
1161 |
FILE *fp; |
free_if_alloc (ctx->host); |
1162 |
keyserver_cfgfile *cfg; |
free_if_alloc (ctx->pass); |
1163 |
char buf[256]; |
free_if_alloc (ctx->user); |
1164 |
int rc = 0; |
ctx->port = ctx->proto = 0; |
1165 |
|
} |
|
*ret_cfg = NULL; |
|
|
fp = fopen( fname, "rb" ); |
|
|
if( fp == NULL ) |
|
|
return WPTERR_FILE_OPEN; |
|
|
|
|
|
cfg = new keyserver_cfgfile; |
|
|
if ( !cfg ) |
|
|
BUG( NULL ); |
|
|
memset( cfg, 0, sizeof *cfg ); |
|
|
|
|
|
while ( !feof( fp ) ) { |
|
|
if ( fgets( buf, sizeof(buf)-1, fp ) == NULL ) |
|
|
break; |
|
|
if ( *buf == '\r' || *buf == '\n' || *buf == '#' ) |
|
|
continue; |
|
|
buf[strlen(buf) -2] = '\0'; |
|
|
if ( !strncmp( buf, "use_proxy=", 10 ) ) { |
|
|
char *p = strchr(buf, ':'); |
|
|
if (!p) |
|
|
continue; |
|
|
cfg->proxy.port = atol( buf+(p-buf+1) ); |
|
|
buf[p-buf] = '\0'; |
|
|
cfg->proxy.host = m_strdup( buf+10 ); |
|
|
} |
|
|
else if ( !strncmp( buf, "proxy_user=", 11 ) ) |
|
|
cfg->proxy.user = m_strdup( buf+11 ); |
|
|
else if ( !strncmp( buf, "proxy_pass=", 11 ) ) |
|
|
cfg->proxy.pass = m_strdup( buf+11 ); |
|
|
if ( !strncmp( buf, "http://", 7 ) |
|
|
|| !strncmp( buf, "hkp://", 6 ) |
|
|
|| !strncmp( buf, "ldap://", 7 ) ) { |
|
|
add_node( cfg, buf ); |
|
|
cfg->nlist++; |
|
|
} |
|
|
} |
|
|
|
|
|
fclose( fp ); |
|
|
*ret_cfg = cfg; |
|
|
|
|
|
return rc; |
|
|
} /* kserver_read_config */ |
|
1166 |
|
|
1167 |
|
|
1168 |
int |
/* Spawn a process given by @cmdl. */ |
1169 |
kserver_write_config( const char * fname, keyserver_cfgfile * cfg ) |
static int |
1170 |
|
spawn_application (char *cmdl) |
1171 |
{ |
{ |
1172 |
|
STARTUPINFO si; |
1173 |
return 0; |
PROCESS_INFORMATION pi; |
1174 |
} |
int rc = 0; |
|
|
|
1175 |
|
|
1176 |
void |
memset (&si, 0, sizeof (si)); |
1177 |
kserver_cfgfile_release( keyserver_cfgfile *cfg ) |
si.cb = sizeof (si); |
1178 |
{ |
si.dwFlags = STARTF_USESHOWWINDOW; |
1179 |
keyserver_node *k, *k2; |
si.wShowWindow = SW_HIDE; |
1180 |
|
memset (&pi, 0, sizeof (pi)); |
1181 |
|
|
1182 |
proxy_release( &cfg->proxy ); |
if (!CreateProcess (NULL, cmdl, NULL, NULL, FALSE, 0, |
1183 |
for( k = cfg->list; cfg->nlist--; k = k2 ) { |
NULL, NULL, &si, &pi)) { |
1184 |
k2 = k->next; |
log_box ("Keyserver Plugin", MB_ERR, "Could not spawn helper process"); |
1185 |
free_if_alloc( k ); |
rc = -1; |
1186 |
} |
} |
|
free_if_alloc( cfg ); |
|
|
} /* kserver_cfgfile_release */ |
|
1187 |
|
|
1188 |
|
CloseHandle (pi.hThread); |
1189 |
void |
WaitForSingleObject (pi.hProcess, INFINITE); |
1190 |
proxy_release( keyserver_proxy_ctx *ctx ) |
CloseHandle (pi.hProcess); |
1191 |
{ |
return rc; |
1192 |
free_if_alloc( ctx->host ); |
} |
|
free_if_alloc( ctx->pass ); |
|
|
free_if_alloc( ctx->user ); |
|
|
} /* proxy_release */ |
|
1193 |
|
|
1194 |
|
|
1195 |
int |
/* Receive an key via LDAP from host @host with the keyid @keyid. |
1196 |
ldap_recvkey (const char * host, const char * keyid, char * key, int maxkeylen) |
@key contains the key on success. */ |
1197 |
{ |
int |
1198 |
|
ldap_recvkey (const char *host, const char *keyid, char *key, int maxkeylen) |
1199 |
|
{ |
1200 |
|
FILE *fp; |
1201 |
const char *s; |
const char *s; |
1202 |
char *ksprg = NULL, *p = NULL; |
char *ksprg = NULL, *p = NULL, *sep; |
1203 |
char temp[512], outf[512]; |
char inf[256], outf[256], buf[512]; |
1204 |
FILE * inp; |
DWORD n; |
1205 |
int rc, start_key = 0; |
int start_key = 0, failed = 0; |
1206 |
STARTUPINFO si; |
int rc = 0; |
|
PROCESS_INFORMATION pi; |
|
1207 |
|
|
1208 |
p = get_gnupg_path (); |
p = get_gnupg_prog (); |
1209 |
ksprg = new char[strlen (p) + 1 + 128]; |
n = strlen (p) + 1 + 128; |
1210 |
|
ksprg = new char[n+1]; |
1211 |
if (!ksprg) |
if (!ksprg) |
1212 |
BUG (0); |
BUG (0); |
1213 |
strcpy (ksprg, p); |
sep = strrchr (p, '\\'); |
1214 |
strcat (ksprg, "\\"); |
if (sep != NULL) |
1215 |
strcat (ksprg, "gpgkeys_ldap.exe"); |
p[(sep-p)] = 0; |
1216 |
|
|
1217 |
|
_snprintf (ksprg, n, "%s\\gpgkeys_ldap.exe", p); |
1218 |
free_if_alloc (p); |
free_if_alloc (p); |
1219 |
if (file_exist_check (ksprg)) { |
if (file_exist_check (ksprg)) { |
1220 |
log_box ( "LDAP Keyserver Plugin", MB_ERR, "Could not find LDAP keyserver module!"); |
log_box ("LDAP Keyserver Plugin", MB_ERR, |
1221 |
rc = -1; |
"%s: could not find LDAP keyserver module!", ksprg); |
1222 |
|
rc = -1; |
1223 |
goto leave; |
goto leave; |
1224 |
} |
} |
1225 |
GetTempPath (sizeof (temp)-1, temp); |
get_temp_name (outf, sizeof (outf)-1, keyid); |
1226 |
strcpy (outf, temp); |
get_temp_name (inf, sizeof (inf)-1, NULL); |
1227 |
strcat (outf, keyid); |
fp = fopen (inf, "w+b"); |
1228 |
strcat (outf, ".out"); |
if (!fp) { |
1229 |
strcat (temp, keyid); |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", inf, |
|
inp = fopen (temp, "w+b"); |
|
|
if( !inp ) { |
|
|
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", temp, |
|
1230 |
winpt_strerror (WPTERR_FILE_OPEN)); |
winpt_strerror (WPTERR_FILE_OPEN)); |
1231 |
rc = -1; |
rc = -1; |
1232 |
goto leave; |
goto leave; |
1233 |
} |
} |
1234 |
fprintf (inp, |
fprintf (fp, |
1235 |
"VERSION 0\n" |
"VERSION 1\n" |
1236 |
|
"PROGRAM 1.4.3-cvs\n" |
1237 |
|
"SCHEME ldap\n" |
1238 |
"HOST %s\n" |
"HOST %s\n" |
|
"OPTION verbose\n" |
|
1239 |
"COMMAND GET\n" |
"COMMAND GET\n" |
1240 |
"\n" |
"\n" |
1241 |
"%s\n", host? skip_type_prefix (host): "64.94.85.200", keyid); |
"%s\n", |
1242 |
fclose (inp); |
host? skip_type_prefix (host): "64.94.85.200", keyid); |
1243 |
|
fclose (fp); |
1244 |
|
|
1245 |
memset (&si, 0, sizeof (si)); |
p = new char[strlen (ksprg) + strlen (inf) + strlen (outf) + 32]; |
1246 |
si.cb = sizeof (si); |
if (!p) |
1247 |
si.dwFlags = STARTF_USESHOWWINDOW; |
BUG (NULL); |
1248 |
si.wShowWindow = SW_HIDE; |
sprintf (p, "%s -o %s %s", ksprg, outf, inf); |
1249 |
memset (&pi, 0, sizeof (pi)); |
if (spawn_application (p)) { |
|
p = new char[strlen (ksprg) + strlen (temp) + strlen (outf) + 32]; |
|
|
sprintf (p, "%s -o %s %s", ksprg, outf, temp); |
|
|
rc = CreateProcess (NULL, p, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi); |
|
|
if (!rc) { |
|
|
log_box ("LDAP Keyserver Plugin", MB_ERR, "Could not spawn process"); |
|
1250 |
rc = -1; |
rc = -1; |
1251 |
goto leave; |
goto leave; |
1252 |
} |
} |
|
CloseHandle (pi.hThread); |
|
|
WaitForSingleObject (pi.hProcess, INFINITE); |
|
|
CloseHandle (pi.hProcess); |
|
1253 |
|
|
1254 |
inp = fopen (outf, "rb"); |
fp = fopen (outf, "rb"); |
1255 |
if (!inp) { |
if (!fp) { |
1256 |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf, |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf, |
1257 |
winpt_strerror (WPTERR_FILE_OPEN)); |
winpt_strerror (WPTERR_FILE_OPEN)); |
1258 |
rc = -1; |
rc = -1; |
1259 |
goto leave; |
goto leave; |
1260 |
} |
} |
1261 |
memset (key, 0, maxkeylen); |
memset (key, 0, maxkeylen); |
1262 |
while( !feof( inp ) ) { |
while (!feof (fp)) { |
1263 |
s = fgets( temp, sizeof (temp)-1, inp ); |
s = fgets (buf, sizeof (buf)-1, fp); |
1264 |
if( !s ) |
if (!s) |
1265 |
|
break; |
1266 |
|
if (strstr (s, "KEY") && strstr (s, "FAILED")) { |
1267 |
|
failed = 1; |
1268 |
break; |
break; |
1269 |
if( !start_key && strstr( s, "KEY" ) && strstr( s, "BEGIN" ) ) { |
} |
1270 |
|
if (!start_key && strstr (s, "KEY") && strstr (s, "BEGIN")) { |
1271 |
start_key = 1; |
start_key = 1; |
1272 |
continue; |
continue; |
1273 |
} |
} |
1274 |
if( !start_key ) |
if (!start_key) |
1275 |
continue; |
continue; |
1276 |
strcat( key, temp ); |
strcat (key, buf); |
1277 |
maxkeylen -= strlen( temp ); |
maxkeylen -= strlen (buf); |
1278 |
if( maxkeylen < 0 || (strstr( s, "KEY" ) && strstr( s, "END" )) ) |
if (maxkeylen < 0 || (strstr (s, "KEY") && strstr (s, "END"))) |
1279 |
break; |
break; |
1280 |
} |
} |
1281 |
fclose( inp ); |
fclose (fp); |
1282 |
|
|
1283 |
leave: |
leave: |
1284 |
if( !strlen( key ) ) |
if (failed || !strlen (key)) |
1285 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
1286 |
free_if_alloc( p ); |
DeleteFile (inf); |
1287 |
free_if_alloc( ksprg ); |
DeleteFile (outf); |
1288 |
|
free_if_alloc (p); |
1289 |
|
free_if_alloc (ksprg); |
1290 |
return rc; |
return rc; |
|
} /* ldap_recvkey */ |
|
|
|
|
|
|
|
|
static int |
|
|
finger_readline (int fd, char * buf, int nbytes) |
|
|
{ |
|
|
char c = 0; |
|
|
int n, pos = 0; |
|
|
|
|
|
while (nbytes > 0) { |
|
|
n = recv (fd, &c, 1, 0); |
|
|
if (n <= 0) |
|
|
break; |
|
|
if (c == '\n') |
|
|
break; |
|
|
buf[pos++] = c; |
|
|
nbytes--; |
|
|
} |
|
|
if (nbytes > 0) |
|
|
buf[pos++] = '\0'; |
|
|
if (n <= 0) |
|
|
pos = n; |
|
|
return pos; |
|
1291 |
} |
} |
1292 |
|
|
1293 |
|
|
1294 |
|
/* Receive an key via FINGER from host @host with the user @user. |
1295 |
|
On success @key contains the key. */ |
1296 |
int |
int |
1297 |
finger_recvkey (const char * host, const char * user, char * key, int maxkeylen) |
finger_recvkey (const char *host, const char *user, char *key, int maxkeylen) |
1298 |
{ |
{ |
|
struct hostent * hp; |
|
|
struct sockaddr_in saddr; |
|
1299 |
char buf[128]; |
char buf[128]; |
1300 |
int fd, nread; |
int fd, nread; |
1301 |
int start_key = 0; |
int start_key = 0; |
1302 |
int rc=0; |
int rc=0; |
1303 |
|
|
1304 |
fd = socket (PF_INET, SOCK_STREAM, 0); |
rc = kserver_connect (host, FINGER_PORT, &fd); |
1305 |
if (fd == -1) |
if (rc) |
1306 |
return WPTERR_WINSOCK_SOCKET; |
return rc; |
|
hp = gethostbyname (skip_type_prefix (host)); |
|
|
if (!hp) { |
|
|
closesocket (fd); |
|
|
return WPTERR_WINSOCK_RESOLVE; |
|
|
} |
|
1307 |
|
|
1308 |
memset (&saddr, 0, sizeof (saddr)); |
sock_write (fd, user, strlen (user)); |
1309 |
saddr.sin_family = AF_INET; |
sock_write (fd, "\r\n", 2); |
|
saddr.sin_port = htons (FINGER_PORT); |
|
|
saddr.sin_addr = *(struct in_addr *)hp->h_addr; |
|
|
if (connect (fd, (struct sockaddr *)&saddr, sizeof (saddr))) { |
|
|
closesocket (fd); |
|
|
return WPTERR_WINSOCK_CONNECT; |
|
|
} |
|
|
send (fd, user, strlen (user), 0); |
|
|
send (fd, "\r\n", 2, 0); |
|
1310 |
|
|
1311 |
memset (key, 0, maxkeylen); |
memset (key, 0, maxkeylen); |
1312 |
while (maxkeylen > 0) { |
while (maxkeylen > 0) { |
1313 |
nread = finger_readline (fd, buf, sizeof (buf)-1); |
if (sock_getline (fd, buf, sizeof (buf), &nread)) |
1314 |
if (nread <= 0) |
break; |
|
break; |
|
1315 |
strcat (buf, "\n"); |
strcat (buf, "\n"); |
1316 |
if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) { |
if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) { |
1317 |
strcat (key, buf); |
strcat (key, buf); |
1336 |
} |
} |
1337 |
|
|
1338 |
|
|
1339 |
|
/* Check if the given name @name is a valid hostname. */ |
1340 |
int |
int |
1341 |
check_IP_or_hostname (const char *name) |
check_IP_or_hostname (const char *name) |
1342 |
{ |
{ |