37 |
#include "wptGPG.h" |
#include "wptGPG.h" |
38 |
#include "wptRegistry.h" |
#include "wptRegistry.h" |
39 |
|
|
40 |
|
/* just map net_errno to a winsock error. */ |
41 |
#define net_errno ((int)WSAGetLastError ()) |
#define net_errno ((int)WSAGetLastError ()) |
42 |
|
|
|
static char base64code[] = |
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
|
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[] = { |
47 |
"hkp://wwwkeys.nl.pgp.net", |
"hkp://wwwkeys.nl.pgp.net", |
48 |
"hkp://wwwkeys.pl.pgp.net", |
"hkp://wwwkeys.pl.pgp.net", |
69 |
char *default_keyserver = NULL; |
char *default_keyserver = NULL; |
70 |
WORD default_keyserver_port = 0; |
WORD default_keyserver_port = 0; |
71 |
|
|
72 |
|
/* Default socket timeout. */ |
73 |
|
static int default_socket_timeout = 10; |
74 |
|
|
75 |
/* Basic64 encode the input @inputstr to @outputstr. */ |
/* Basic64 encode the input @inbuf to @outbuf. */ |
76 |
static void |
static void |
77 |
base64_encode (const char *inputstr, char *outputstr, int maxlen) |
base64_encode (const char *inbuf, char *outbuf) |
78 |
{ |
{ |
79 |
int index = 0, temp = 0, res = 0, i = 0, inputlen = 0, len = 0; |
char base64code[] = |
80 |
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
81 |
|
int index = 0, temp = 0, res = 0; |
82 |
|
int i = 0, inputlen = 0, len = 0; |
83 |
|
|
84 |
/* XXX: check len < maxlen. */ |
inputlen = strlen (inbuf); |
85 |
inputlen = strlen (inputstr); |
for (i = 0; i < inputlen; i++) { |
|
for (i = 0; i <inputlen; i++) { |
|
86 |
res = temp; |
res = temp; |
87 |
res = (res << 8) | (inputstr[i] & 0xFF); |
res = (res << 8) | (inbuf[i] & 0xFF); |
88 |
switch (index++) { |
switch (index++) { |
89 |
case 0: outputstr[len++] = base64code[res >> 2 & 0x3F]; res &= 0x3; break; |
case 0: |
90 |
case 1: outputstr[len++] = base64code[res >> 4 & 0x3F]; res &= 0xF; break; |
outbuf[len++] = base64code[res >> 2 & 0x3F]; |
91 |
case 2: outputstr[len++] = base64code[res >> 6 & 0x3F]; |
res &= 0x3; |
92 |
outputstr[len++] = base64code[res & 0x3F]; res = index = 0; break; |
break; |
93 |
|
case 1: |
94 |
|
outbuf[len++] = base64code[res >> 4 & 0x3F]; |
95 |
|
res &= 0xF; |
96 |
|
break; |
97 |
|
case 2: |
98 |
|
outbuf[len++] = base64code[res >> 6 & 0x3F]; |
99 |
|
outbuf[len++] = base64code[res & 0x3F]; |
100 |
|
res = index = 0; |
101 |
|
break; |
102 |
} |
} |
103 |
temp = res; |
temp = res; |
104 |
} |
} |
105 |
|
|
106 |
switch (index) { |
if (index == 2) { |
107 |
case 0: break; |
outbuf[len++] = base64code[temp << 2 & 0x3F]; |
108 |
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++] = '='; |
|
109 |
} |
} |
110 |
|
else if (index == 1) { |
111 |
outputstr[len] = '\0'; |
outbuf[len++] = base64code[temp << 4 & 0x3F]; |
112 |
|
outbuf[len++] = '='; |
113 |
|
outbuf[len++] = '='; |
114 |
|
} |
115 |
|
|
116 |
|
outbuf[len] = '\0'; |
117 |
} |
} |
118 |
|
|
119 |
|
|
168 |
sock_getline (int fd, char *buf, int buflen, int *nbytes) |
sock_getline (int fd, char *buf, int buflen, int *nbytes) |
169 |
{ |
{ |
170 |
char ch; |
char ch; |
171 |
int count = 0; |
int nread = 0; |
172 |
|
|
173 |
if (nbytes) |
if (nbytes) |
174 |
*nbytes = 0; |
*nbytes = 0; |
175 |
*buf = 0; |
*buf = 0; |
176 |
while (recv (fd, &ch, 1, 0) > 0) { |
while (recv (fd, &ch, 1, 0) > 0) { |
177 |
*buf++ = ch; |
*buf++ = ch; |
178 |
count++; |
nread++; |
179 |
/* XXX: remove the '\r' */ |
if (ch == '\r') |
180 |
if (ch == '\n' || count == (buflen - 1)) { |
continue; /* remove this char. */ |
181 |
|
if (ch == '\n' || nread == (buflen - 1)) { |
182 |
*buf = 0; |
*buf = 0; |
183 |
if (nbytes) |
if (nbytes) |
184 |
*nbytes = count; |
*nbytes = nread; |
185 |
return 0; |
return 0; |
186 |
} |
} |
187 |
} |
} |
216 |
Return value: 0 on success. */ |
Return value: 0 on success. */ |
217 |
static int |
static int |
218 |
sock_read (int fd, char *buf, int buflen, int *nbytes) |
sock_read (int fd, char *buf, int buflen, int *nbytes) |
219 |
{ |
{ |
220 |
DWORD nread; |
DWORD nread; |
221 |
int nleft = buflen; |
int nleft = buflen; |
222 |
int rc, n = 0; |
int rc, n = 0; |
223 |
|
|
224 |
while (nleft > 0) { |
while (nleft > 0) { |
225 |
if (n >= 10) |
if (n >= default_socket_timeout) |
226 |
return WPTERR_WINSOCK_TIMEOUT; |
return WPTERR_WINSOCK_TIMEOUT; |
227 |
rc = sock_select (fd, 1); |
rc = sock_select (fd, 1); |
228 |
if (rc == SOCKET_ERROR) |
if (rc == SOCKET_ERROR) |
300 |
return SOCKET_ERROR; |
return SOCKET_ERROR; |
301 |
} |
} |
302 |
nleft -= nwritten; |
nleft -= nwritten; |
303 |
buf += nwritten; |
buf += nwritten; |
304 |
} |
} |
305 |
|
|
306 |
return 0; |
return 0; |
307 |
} |
} |
308 |
|
|
309 |
|
|
|
/* Set the default keyserver. */ |
|
|
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 = (WORD)strtoul (p, NULL, 10); |
|
|
free_if_alloc (p); |
|
|
} |
|
|
else |
|
|
default_keyserver_port = HKP_PORT; |
|
|
} |
|
|
|
|
|
|
|
310 |
/* Initialize the Winsock2 interface.*/ |
/* Initialize the Winsock2 interface.*/ |
311 |
int |
int |
312 |
wsock_init (void) |
wsock_init (void) |
317 |
log_debug ("wsock_init: WSAStartup failed ec=%d\r\n", net_errno); |
log_debug ("wsock_init: WSAStartup failed ec=%d\r\n", net_errno); |
318 |
return WPTERR_WINSOCK_INIT; |
return WPTERR_WINSOCK_INIT; |
319 |
} |
} |
|
set_default_kserver (); |
|
320 |
return 0; |
return 0; |
321 |
} |
} |
322 |
|
|
333 |
free_if_alloc (p); |
free_if_alloc (p); |
334 |
free_if_alloc (default_keyserver); |
free_if_alloc (default_keyserver); |
335 |
for (i=0; i < MAX_KEYSERVERS; i++) { |
for (i=0; i < MAX_KEYSERVERS; i++) { |
336 |
if (server[i].used) |
if (server[i].used && server[i].name != NULL) |
337 |
free_if_alloc (server[i].name); |
free_if_alloc (server[i].name); |
338 |
} |
} |
339 |
|
kserver_proxy_release (&proxy); |
340 |
WSACleanup (); |
WSACleanup (); |
341 |
} |
} |
342 |
|
|
363 |
} |
} |
364 |
|
|
365 |
|
|
366 |
|
/* Set default socket timeout for all reading operations. */ |
367 |
|
void |
368 |
|
kserver_set_socket_timeout (int nsec) |
369 |
|
{ |
370 |
|
if (nsec < 0) |
371 |
|
nsec = 0; |
372 |
|
default_socket_timeout = nsec; |
373 |
|
} |
374 |
|
|
375 |
|
|
376 |
/* Return the last keyserver error as a string. */ |
/* Return the last keyserver error as a string. */ |
377 |
const char* |
const char* |
378 |
kserver_strerror (void) |
kserver_strerror (void) |
418 |
/* If the request contains the keyid, it have to be |
/* If the request contains the keyid, it have to be |
419 |
always postfix with '0x'+keyid. This code checks |
always postfix with '0x'+keyid. This code checks |
420 |
if the keyid is a decimal value and if so if it contains |
if the keyid is a decimal value and if so if it contains |
421 |
the '0x'. If not it is inserted. */ |
the '0x'. If not it is inserted. */ |
422 |
const char* |
const char* |
423 |
kserver_check_keyid (const char *keyid) |
kserver_check_keyid (const char *keyid) |
424 |
{ |
{ |
437 |
|
|
438 |
/* Update the keyserver proxy user. */ |
/* Update the keyserver proxy user. */ |
439 |
static void |
static void |
440 |
kserver_update_proxyuser (const char *proxy_user, const char *proxy_pass) |
update_proxy_user (const char *proxy_user, const char *proxy_pass) |
441 |
{ |
{ |
442 |
char t[257]; /* user:pass = 127+1+127+1 = 257 */ |
char t[257]; /* user:pass = 127+1+127+1 = 257 */ |
443 |
int n = 0; |
int n = 0; |
448 |
if (!proxy.base64_user) |
if (!proxy.base64_user) |
449 |
BUG (0); |
BUG (0); |
450 |
_snprintf (t, sizeof (t)-1, "%s:%s", proxy_user, proxy_pass); |
_snprintf (t, sizeof (t)-1, "%s:%s", proxy_user, proxy_pass); |
451 |
base64_encode (t, proxy.base64_user, 257); |
base64_encode (t, proxy.base64_user); |
452 |
free_if_alloc (proxy.user); |
free_if_alloc (proxy.user); |
453 |
free_if_alloc (proxy.pass); |
free_if_alloc (proxy.pass); |
454 |
proxy.user = m_strdup (proxy_user); |
proxy.user = m_strdup (proxy_user); |
486 |
|
|
487 |
/* Get the port number from the given URL. */ |
/* Get the port number from the given URL. */ |
488 |
static int |
static int |
489 |
proto_from_URL (const char * buf) |
proto_from_URL (const char *buf) |
490 |
{ |
{ |
491 |
if (strstr( buf, "ldap")) |
if (strstr( buf, "ldap")) |
492 |
return KSPROTO_LDAP; |
return KSPROTO_LDAP; |
499 |
void |
void |
500 |
keyserver_set_default (const char *hostname, WORD port) |
keyserver_set_default (const char *hostname, WORD port) |
501 |
{ |
{ |
502 |
if (hostname) { |
if (hostname != NULL) { |
503 |
free_if_alloc (default_keyserver); |
free_if_alloc (default_keyserver); |
504 |
default_keyserver = m_strdup (hostname); |
default_keyserver = m_strdup (hostname); |
505 |
if (!default_keyserver) |
if (!default_keyserver) |
506 |
BUG (0); |
BUG (0); |
507 |
default_keyserver_port = port; |
default_keyserver_port = port; |
508 |
} |
} |
509 |
|
if (!port) |
510 |
|
port = HKP_PORT; |
511 |
|
if (!default_keyserver) |
512 |
|
default_keyserver = m_strdup (DEF_HKP_KEYSERVER); |
513 |
server[0].name = m_strdup (default_keyserver); |
server[0].name = m_strdup (default_keyserver); |
514 |
server[0].used = 1; |
server[0].used = 1; |
515 |
server[0].port = port; |
server[0].port = port; |
549 |
return -1; |
return -1; |
550 |
} |
} |
551 |
|
|
552 |
fprintf (fp, "# do NOT manually modify this file, it will be generated automatically!!\r\n"); |
fprintf (fp, "# do NOT manually modify this file, it will be " |
553 |
|
"generated automatically!!\r\n"); |
554 |
for (pos = 0; pos < MAX_KEYSERVERS; pos++) { |
for (pos = 0; pos < MAX_KEYSERVERS; pos++) { |
555 |
if (!server[pos].used) |
if (!server[pos].used) |
556 |
continue; |
continue; |
567 |
{ |
{ |
568 |
struct stat statbuf; |
struct stat statbuf; |
569 |
FILE *fp; |
FILE *fp; |
570 |
char buf[1024], * s, * p; |
char buf[1024], *s, *p; |
571 |
char *user = NULL, *pass = NULL; |
char *user = NULL, *pass = NULL; |
572 |
int no_config=0, chk_pos=0; |
int no_config=0, chk_pos=0; |
573 |
int pos; |
int pos; |
586 |
} |
} |
587 |
no_config=1; |
no_config=1; |
588 |
} |
} |
589 |
get_reg_proxy_prefs (&proxy.host, &proxy.port, &user, &pass); |
get_reg_proxy_prefs (&proxy); |
590 |
if (user && pass) |
if (user && pass) |
591 |
kserver_update_proxyuser (user, pass); |
update_proxy_user (user, pass); |
592 |
else if (user && !pass || !user && pass) { |
else if (user && !pass || !user && pass) { |
593 |
msg_box (NULL, _("Invalid proxy configuration." |
msg_box (NULL, _("Invalid proxy configuration." |
594 |
"You need to set a user and a password" |
"You need to set a user and a password" |
624 |
_("Keyserver Error"), MB_ERR); |
_("Keyserver Error"), MB_ERR); |
625 |
continue; |
continue; |
626 |
} |
} |
627 |
chk_pos=6; |
chk_pos = 6; |
628 |
if (strstr (s, "finger")) |
if (strstr (s, "finger")) |
629 |
chk_pos = 10; |
chk_pos = 10; |
630 |
p = strchr (s+chk_pos, ':'); |
p = strchr (s+chk_pos, ':'); |
653 |
if (fp) |
if (fp) |
654 |
fclose (fp); |
fclose (fp); |
655 |
if (!pos) { |
if (!pos) { |
656 |
/* 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 |
657 |
|
it has no valid entries. */ |
658 |
keyserver_set_default (NULL, HKP_PORT); |
keyserver_set_default (NULL, HKP_PORT); |
659 |
if (!no_config) |
if (!no_config) |
660 |
return WPTERR_CONFIG_FILE; |
return WPTERR_CONFIG_FILE; |
666 |
} |
} |
667 |
|
|
668 |
|
|
|
/* Return the proxy host and port if available. Null otherwise. */ |
|
|
const char* |
|
|
kserver_get_proxy (int *r_port) |
|
|
{ |
|
|
if (proxy.host) { |
|
|
if (r_port) |
|
|
*r_port = proxy.port; |
|
|
return proxy.host; |
|
|
} |
|
|
return NULL; |
|
|
} |
|
|
|
|
|
|
|
|
/* Return a requested item from the proxy environment. */ |
|
|
const char* |
|
|
kserver_get_proxy_info (int id) |
|
|
{ |
|
|
switch (id) { |
|
|
case PROXY_USER: return proxy.user; |
|
|
case PROXY_PASS: return proxy.pass; |
|
|
} |
|
|
return NULL; |
|
|
} |
|
|
|
|
|
|
|
669 |
/* Connect to the keyserver @hostname (port @port). |
/* Connect to the keyserver @hostname (port @port). |
670 |
Return value: 0 on success */ |
Return value: 0 on success */ |
671 |
int |
int |
716 |
return WPTERR_WINSOCK_CONNECT; |
return WPTERR_WINSOCK_CONNECT; |
717 |
} |
} |
718 |
|
|
719 |
|
if (proxy.proto != PROXY_PROTO_HTTP) { |
720 |
|
rc = socks_handshake (&proxy, fd, hostname, port); |
721 |
|
if (rc) { |
722 |
|
closesocket (fd); |
723 |
|
return WPTERR_GENERAL; /* XXX: use better code. */ |
724 |
|
} |
725 |
|
} |
726 |
|
|
727 |
if (conn_fd) |
if (conn_fd) |
728 |
*conn_fd = fd; |
*conn_fd = fd; |
729 |
WSASetLastError (0); |
WSASetLastError (0); |
795 |
return NULL; |
return NULL; |
796 |
} |
} |
797 |
|
|
798 |
if (proxy.user) { |
if (proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
799 |
_snprintf (request, reqlen-1, |
_snprintf (request, reqlen-1, |
800 |
"POST http://%s:%d/pks/add HTTP/1.0\r\n" |
"POST http://%s:%d/pks/add HTTP/1.0\r\n" |
801 |
"Referer: \r\n" |
"Referer: \r\n" |
850 |
if (!request) |
if (!request) |
851 |
BUG (0); |
BUG (0); |
852 |
|
|
853 |
if (proxy.host && proxy.user) { |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
854 |
_snprintf (request, 300, |
_snprintf (request, 300, |
855 |
"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" |
856 |
"Proxy-Authorization: Basic %s\r\n\r\n", |
"Proxy-Authorization: Basic %s\r\n\r\n", |
857 |
skip_type_prefix (hostname), port, keyid, proxy.base64_user); |
skip_type_prefix (hostname), port, keyid, proxy.base64_user); |
858 |
} |
} |
859 |
else if (proxy.host) { |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
860 |
_snprintf (request, 300, |
_snprintf (request, 300, |
861 |
"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", |
862 |
skip_type_prefix (hostname), port, keyid); |
skip_type_prefix (hostname), port, keyid); |
986 |
if (!request) |
if (!request) |
987 |
BUG (0); |
BUG (0); |
988 |
|
|
989 |
if (proxy.host && proxy.user) { |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
990 |
_snprintf (request, n, |
_snprintf (request, n, |
991 |
"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" |
992 |
"Proxy-Authorization: Basic %s\r\n\r\n", |
"Proxy-Authorization: Basic %s\r\n\r\n", |
993 |
skip_type_prefix (hostname), port, enc_keyid, proxy.base64_user); |
skip_type_prefix (hostname), port, enc_keyid, proxy.base64_user); |
994 |
} |
} |
995 |
else if (proxy.host) { |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
996 |
_snprintf (request, n, |
_snprintf (request, n, |
997 |
"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", |
998 |
skip_type_prefix (hostname), port, enc_keyid); |
skip_type_prefix (hostname), port, enc_keyid); |
1141 |
} |
} |
1142 |
|
|
1143 |
|
|
|
/* Replace all current proxy items with the items |
|
|
from the context @ctx. */ |
|
|
void |
|
|
kserver_change_proxy (keyserver_proxy_ctx *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); |
|
|
} |
|
|
|
|
|
|
|
1144 |
/* Release mbmers in the proxy context @ctx. */ |
/* Release mbmers in the proxy context @ctx. */ |
1145 |
void |
void |
1146 |
proxy_release (keyserver_proxy_ctx *ctx) |
kserver_proxy_release (keyserver_proxy_t ctx) |
1147 |
{ |
{ |
1148 |
free_if_alloc (ctx->host); |
free_if_alloc (ctx->host); |
1149 |
free_if_alloc (ctx->pass); |
free_if_alloc (ctx->pass); |
1183 |
int |
int |
1184 |
ldap_recvkey (const char *host, const char *keyid, char *key, int maxkeylen) |
ldap_recvkey (const char *host, const char *keyid, char *key, int maxkeylen) |
1185 |
{ |
{ |
1186 |
FILE *inp; |
FILE *fp; |
|
DWORD n; |
|
1187 |
const char *s; |
const char *s; |
1188 |
char *ksprg = NULL, *p = NULL, *sep; |
char *ksprg = NULL, *p = NULL, *sep; |
1189 |
char temp[512], outf[512]; |
char inf[256], outf[256], buf[512]; |
1190 |
|
DWORD n; |
1191 |
int start_key = 0, failed = 0; |
int start_key = 0, failed = 0; |
1192 |
int rc = 0; |
int rc = 0; |
1193 |
|
|
1207 |
"%s: could not find LDAP keyserver module!", ksprg); |
"%s: could not find LDAP keyserver module!", ksprg); |
1208 |
rc = -1; |
rc = -1; |
1209 |
goto leave; |
goto leave; |
1210 |
} |
} |
1211 |
GetTempPath (sizeof (temp)-1, temp); |
get_temp_name (outf, sizeof (outf)-1, keyid); |
1212 |
_snprintf (outf, sizeof (outf)-1, "%s%s.out", temp, keyid); |
get_temp_name (inf, sizeof (inf)-1, NULL); |
1213 |
strcat (temp, keyid); |
fp = fopen (inf, "w+b"); |
1214 |
inp = fopen (temp, "w+b"); |
if (!fp) { |
1215 |
if (!inp) { |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", inf, |
|
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", temp, |
|
1216 |
winpt_strerror (WPTERR_FILE_OPEN)); |
winpt_strerror (WPTERR_FILE_OPEN)); |
1217 |
rc = -1; |
rc = -1; |
1218 |
goto leave; |
goto leave; |
1219 |
} |
} |
1220 |
fprintf (inp, |
fprintf (fp, |
1221 |
"VERSION 1\n" |
"VERSION 1\n" |
1222 |
"PROGRAM 1.4.3-cvs\n" |
"PROGRAM 1.4.3-cvs\n" |
1223 |
"SCHEME ldap\n" |
"SCHEME ldap\n" |
1226 |
"\n" |
"\n" |
1227 |
"%s\n", |
"%s\n", |
1228 |
host? skip_type_prefix (host): "64.94.85.200", keyid); |
host? skip_type_prefix (host): "64.94.85.200", keyid); |
1229 |
fclose (inp); |
fclose (fp); |
1230 |
|
|
1231 |
p = new char[strlen (ksprg) + strlen (temp) + strlen (outf) + 32]; |
p = new char[strlen (ksprg) + strlen (inf) + strlen (outf) + 32]; |
1232 |
if (!p) |
if (!p) |
1233 |
BUG (NULL); |
BUG (NULL); |
1234 |
sprintf (p, "%s -o %s %s", ksprg, outf, temp); |
sprintf (p, "%s -o %s %s", ksprg, outf, inf); |
1235 |
if (spawn_application (p)) { |
if (spawn_application (p)) { |
1236 |
rc = -1; |
rc = -1; |
1237 |
goto leave; |
goto leave; |
1238 |
} |
} |
|
DeleteFile (temp); |
|
1239 |
|
|
1240 |
inp = fopen (outf, "rb"); |
fp = fopen (outf, "rb"); |
1241 |
if (!inp) { |
if (!fp) { |
1242 |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf, |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf, |
1243 |
winpt_strerror (WPTERR_FILE_OPEN)); |
winpt_strerror (WPTERR_FILE_OPEN)); |
1244 |
rc = -1; |
rc = -1; |
1245 |
goto leave; |
goto leave; |
1246 |
} |
} |
1247 |
memset (key, 0, maxkeylen); |
memset (key, 0, maxkeylen); |
1248 |
while (!feof (inp)) { |
while (!feof (fp)) { |
1249 |
s = fgets (temp, sizeof (temp)-1, inp); |
s = fgets (buf, sizeof (buf)-1, fp); |
1250 |
if (!s) |
if (!s) |
1251 |
break; |
break; |
1252 |
if (strstr (s, "KEY") && strstr (s, "FAILED")) { |
if (strstr (s, "KEY") && strstr (s, "FAILED")) { |
1259 |
} |
} |
1260 |
if (!start_key) |
if (!start_key) |
1261 |
continue; |
continue; |
1262 |
strcat (key, temp); |
strcat (key, buf); |
1263 |
maxkeylen -= strlen (temp); |
maxkeylen -= strlen (buf); |
1264 |
if (maxkeylen < 0 || (strstr (s, "KEY") && strstr (s, "END"))) |
if (maxkeylen < 0 || (strstr (s, "KEY") && strstr (s, "END"))) |
1265 |
break; |
break; |
1266 |
} |
} |
1267 |
fclose (inp); |
fclose (fp); |
1268 |
|
|
1269 |
leave: |
leave: |
1270 |
if (failed || !strlen (key)) |
if (failed || !strlen (key)) |
1271 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
1272 |
|
DeleteFile (inf); |
1273 |
DeleteFile (outf); |
DeleteFile (outf); |
1274 |
free_if_alloc (p); |
free_if_alloc (p); |
1275 |
free_if_alloc (ksprg); |
free_if_alloc (ksprg); |
1277 |
} |
} |
1278 |
|
|
1279 |
|
|
|
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; |
|
|
} |
|
|
|
|
|
|
|
1280 |
/* Receive an key via FINGER from host @host with the user @user. |
/* Receive an key via FINGER from host @host with the user @user. |
1281 |
On success @key contains the key. */ |
On success @key contains the key. */ |
1282 |
int |
int |
1283 |
finger_recvkey (const char *host, const char *user, char *key, int maxkeylen) |
finger_recvkey (const char *host, const char *user, char *key, int maxkeylen) |
1284 |
{ |
{ |
|
struct hostent * hp; |
|
|
struct sockaddr_in saddr; |
|
1285 |
char buf[128]; |
char buf[128]; |
1286 |
int fd, nread; |
int fd, nread; |
1287 |
int start_key = 0; |
int start_key = 0; |
1288 |
int rc=0; |
int rc=0; |
1289 |
|
|
1290 |
fd = socket (PF_INET, SOCK_STREAM, 0); |
rc = kserver_connect (host, FINGER_PORT, &fd); |
1291 |
if (fd == -1) |
if (rc) |
1292 |
return WPTERR_WINSOCK_SOCKET; |
return rc; |
|
hp = gethostbyname (skip_type_prefix (host)); |
|
|
if (!hp) { |
|
|
closesocket (fd); |
|
|
return WPTERR_WINSOCK_RESOLVE; |
|
|
} |
|
1293 |
|
|
1294 |
memset (&saddr, 0, sizeof (saddr)); |
sock_write (fd, user, strlen (user)); |
1295 |
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); |
|
1296 |
|
|
1297 |
memset (key, 0, maxkeylen); |
memset (key, 0, maxkeylen); |
1298 |
while (maxkeylen > 0) { |
while (maxkeylen > 0) { |
1299 |
nread = finger_readline (fd, buf, sizeof (buf)-1); |
if (sock_getline (fd, buf, sizeof (buf), &nread)) |
1300 |
if (nread <= 0) |
break; |
|
break; |
|
1301 |
strcat (buf, "\n"); |
strcat (buf, "\n"); |
1302 |
if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) { |
if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) { |
1303 |
strcat (key, buf); |
strcat (key, buf); |