29 |
#include <sys/stat.h> |
#include <sys/stat.h> |
30 |
#include <ctype.h> |
#include <ctype.h> |
31 |
|
|
32 |
|
#include "wptGPG.h" |
33 |
#include "wptKeyserver.h" |
#include "wptKeyserver.h" |
34 |
#include "wptErrors.h" |
#include "wptErrors.h" |
35 |
#include "wptTypes.h" |
#include "wptTypes.h" |
36 |
#include "wptNLS.h" |
#include "wptNLS.h" |
37 |
#include "wptW32API.h" |
#include "wptW32API.h" |
|
#include "wptGPG.h" |
|
38 |
#include "wptRegistry.h" |
#include "wptRegistry.h" |
39 |
|
#include "wptUTF8.h" |
40 |
|
#include "wptVersion.h" |
41 |
|
|
42 |
|
|
43 |
/* Map net_errno to a winsock error. */ |
/* Map net_errno to a winsock error. */ |
44 |
#define net_errno ((int)WSAGetLastError ()) |
#define net_errno ((int)WSAGetLastError ()) |
61 |
char *default_keyserver = NULL; |
char *default_keyserver = NULL; |
62 |
WORD default_keyserver_port = 0; |
WORD default_keyserver_port = 0; |
63 |
|
|
64 |
/* Default socket timeout. */ |
/* Default socket timeout (secs). */ |
65 |
static int default_socket_timeout = 6; |
static size_t default_socket_timeout = 6; |
66 |
|
|
67 |
|
|
68 |
/* Wrapper for safe memory allocation. */ |
/* Wrapper for safe memory allocation. */ |
85 |
char base64code[] = |
char base64code[] = |
86 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
87 |
int index = 0, temp = 0, res = 0; |
int index = 0, temp = 0, res = 0; |
88 |
int i = 0, inputlen = 0, len = 0; |
int i, inputlen, len = 0; |
89 |
|
|
90 |
inputlen = strlen (inbuf); |
inputlen = strlen (inbuf); |
91 |
for (i = 0; i < inputlen; i++) { |
for (i = 0; i < inputlen; i++) { |
162 |
return hostname; |
return hostname; |
163 |
} |
} |
164 |
|
|
165 |
|
/* Parse the keyserver response and extract the human |
166 |
|
readable text passages. */ |
167 |
|
static int |
168 |
|
parse_keyserver_error (const char *resp, char *txt, size_t txtlen) |
169 |
|
{ |
170 |
|
char *p, *p2; |
171 |
|
|
172 |
|
/* If we find no 'Error' substring, we assume a success. */ |
173 |
|
if (!stristr (resp, "Error")) |
174 |
|
return -1; |
175 |
|
|
176 |
|
memset (txt, 0, txtlen); |
177 |
|
p = strstr (resp, "\r\n\r\n"); |
178 |
|
if (!p) |
179 |
|
return -1; |
180 |
|
resp += (p-resp); |
181 |
|
p = strstr (resp, "<h1>"); |
182 |
|
if (!p) |
183 |
|
return -1; |
184 |
|
resp += (p-resp)+strlen ("<h1>"); |
185 |
|
p = strstr (resp, "</h1>"); |
186 |
|
if (!p) |
187 |
|
return -1; |
188 |
|
resp += (p-resp)+strlen ("</h1>"); |
189 |
|
p2 = strstr (resp, "</body>"); |
190 |
|
if (p2 != NULL) |
191 |
|
memcpy (txt, resp, (p2-resp)); |
192 |
|
else { |
193 |
|
if (!strnicmp (resp, "<p>", 3)) |
194 |
|
resp += 3; |
195 |
|
while (resp && *resp == '\r' || *resp == '\n') |
196 |
|
resp++; |
197 |
|
memcpy (txt, resp, strlen (resp)); |
198 |
|
} |
199 |
|
return 0; |
200 |
|
} |
201 |
|
|
202 |
|
|
203 |
/* Check that the keyserver response indicates an OK. |
/* Check that the keyserver response indicates an OK. |
204 |
Return 0 on success. */ |
Return 0 on success. */ |
205 |
static int |
static int |
206 |
check_hkp_response (const char *resp, int recv) |
check_hkp_response (const char *resp, int recv) |
207 |
{ |
{ |
208 |
char *p, *end; |
int ec; |
|
int ec, len; |
|
209 |
|
|
210 |
log_debug ("check_hkp_response: '%s'\r\n", resp); |
log_debug ("check_hkp_response: '%s'\r\n", resp); |
211 |
ec = recv ? WPTERR_WINSOCK_RECVKEY : WPTERR_WINSOCK_SENDKEY; |
ec = recv ? WPTERR_WINSOCK_RECVKEY : WPTERR_WINSOCK_SENDKEY; |
212 |
if (!strstr (resp, "HTTP/1.0 200 OK") && |
if (!strstr (resp, "HTTP/1.0 200 OK") && |
213 |
!strstr (resp, "HTTP/1.1 200 OK")) /* http error */ |
!strstr (resp, "HTTP/1.1 200 OK") && |
214 |
return ec; |
!strstr (resp, "HTTP/1.0 500 OK") && |
215 |
if (strstr (resp, "Public Key Server -- Error") |
!strstr (resp, "HTTP/1.1 500 OK")) |
216 |
|| strstr (resp, "Public Key Server -- Error") |
return ec; /* http error */ |
217 |
|| strstr (resp, "No matching keys in database")) { |
|
218 |
p = strstr (resp, "<p>"); |
if (!parse_keyserver_error (resp, hkp_errmsg, DIM (hkp_errmsg)-2)) { |
219 |
if (p && strlen (p) < sizeof (hkp_errmsg)-1) { |
if (!strlen (hkp_errmsg)) |
220 |
end = strstr (p, "</p>"); |
_snprintf (hkp_errmsg, DIM (hkp_errmsg)-1, |
221 |
len = end? (end - p + 1) : strlen (p); |
"Unknown keyserver error"); |
222 |
memset (hkp_errmsg, 0, sizeof (hkp_errmsg)); |
hkp_err = 1; |
|
strncpy (hkp_errmsg, p, len); |
|
|
hkp_err = 1; |
|
|
} |
|
223 |
return ec; |
return ec; |
224 |
} |
} |
225 |
return 0; |
return 0; |
279 |
returned in @nbytes. |
returned in @nbytes. |
280 |
Return value: 0 on success. */ |
Return value: 0 on success. */ |
281 |
static int |
static int |
282 |
sock_read (int fd, char *buf, int buflen, int *nbytes) |
sock_read (int fd, char *buf, size_t buflen, int *nbytes) |
283 |
{ |
{ |
284 |
DWORD nread; |
int nread; |
285 |
int nleft = buflen; |
size_t nleft = buflen; |
286 |
int rc, n = 0; |
size_t n = 0; |
287 |
|
int rc; |
288 |
|
|
289 |
if (nbytes) |
if (nbytes) |
290 |
*nbytes = 0; |
*nbytes = 0; |
318 |
/* Helper to create a string from the gpgme data and |
/* Helper to create a string from the gpgme data and |
319 |
then release the gpgme data object. */ |
then release the gpgme data object. */ |
320 |
char* |
char* |
321 |
data_release_and_get_mem (gpgme_data_t in, int *r_bufferlen) |
data_release_and_get_mem (gpgme_data_t in, size_t *r_bufferlen) |
322 |
{ |
{ |
323 |
size_t n; |
size_t n; |
324 |
char *buffer, *p; |
char *buffer, *p; |
327 |
p = gpgme_data_release_and_get_mem (in, &n); |
p = gpgme_data_release_and_get_mem (in, &n); |
328 |
buffer = m_strdup (p); |
buffer = m_strdup (p); |
329 |
if (r_bufferlen) |
if (r_bufferlen) |
330 |
*r_bufferlen = (int)n; |
*r_bufferlen = n; |
331 |
gpgme_free (p); |
gpgme_free (p); |
332 |
return buffer; |
return buffer; |
333 |
} |
} |
337 |
return the data in @buffer. Caller must free data. |
return the data in @buffer. Caller must free data. |
338 |
Return value: 0 on success. */ |
Return value: 0 on success. */ |
339 |
int |
int |
340 |
sock_read_ext (int fd, char **buffer, int *r_bufferlen) |
sock_read_ext (int fd, char **buffer, size_t *r_bufferlen) |
341 |
{ |
{ |
342 |
gpgme_data_t dh; |
gpgme_data_t dh; |
343 |
char buf[1024]; |
char buf[1024]; |
344 |
size_t n=0; |
size_t timeout=0; |
345 |
int nread, rc; |
int nread, rc; |
346 |
|
|
347 |
if (gpgme_data_new (&dh)) |
if (gpgme_data_new (&dh)) |
348 |
BUG (0); |
return SOCKET_ERROR; |
349 |
while (n < 10) { |
while (timeout < default_socket_timeout) { |
350 |
rc = sock_select (fd, 1); |
rc = sock_select (fd, 1); |
351 |
if (rc == SOCKET_ERROR) { |
if (rc == SOCKET_ERROR) { |
352 |
gpgme_data_release (dh); |
gpgme_data_release (dh); |
353 |
return rc; |
return rc; |
354 |
} |
} |
355 |
else if (!rc) |
else if (!rc) |
356 |
n++; |
timeout++; /* socket not ready yet. */ |
357 |
else { |
else { |
358 |
nread = recv (fd, buf, sizeof (buf), 0); |
nread = recv (fd, buf, DIM (buf), 0); |
359 |
if (nread == SOCKET_ERROR) |
if (nread == SOCKET_ERROR) |
360 |
return SOCKET_ERROR; |
return SOCKET_ERROR; |
361 |
else if (!nread) |
else if (!nread) |
363 |
gpgme_data_write (dh, buf, nread); |
gpgme_data_write (dh, buf, nread); |
364 |
} |
} |
365 |
} |
} |
366 |
|
if (timeout >= default_socket_timeout) |
367 |
|
return WPTERR_WINSOCK_TIMEOUT; |
368 |
*buffer = data_release_and_get_mem (dh, r_bufferlen); |
*buffer = data_release_and_get_mem (dh, r_bufferlen); |
369 |
return 0; |
return 0; |
370 |
} |
} |
372 |
|
|
373 |
/* Write the buffer @buf with the length @buflen to a socket @fd. */ |
/* Write the buffer @buf with the length @buflen to a socket @fd. */ |
374 |
static int |
static int |
375 |
sock_write (int fd, const char *buf, int buflen) |
sock_write (int fd, const char *buf, size_t buflen) |
376 |
{ |
{ |
377 |
DWORD nwritten; |
int nwritten; |
378 |
int nleft = buflen; |
int nleft = buflen; |
379 |
|
|
380 |
while (nleft > 0) { |
while (nleft > 0) { |
412 |
char *p; |
char *p; |
413 |
int i; |
int i; |
414 |
|
|
415 |
|
#ifdef WINPT_MOBILE |
416 |
|
p = m_strdup ("keyserver.conf"); |
417 |
|
#else |
418 |
p = make_special_filename (CSIDL_APPDATA, "winpt\\keyserver.conf", NULL); |
p = make_special_filename (CSIDL_APPDATA, "winpt\\keyserver.conf", NULL); |
419 |
|
#endif |
420 |
kserver_save_conf (p); |
kserver_save_conf (p); |
421 |
free_if_alloc (p); |
free_if_alloc (p); |
422 |
free_if_alloc (default_keyserver); |
free_if_alloc (default_keyserver); |
423 |
for (i=0; i < MAX_KEYSERVERS; i++) { |
for (i=0; i < MAX_KEYSERVERS; i++) { |
424 |
if (server[i].used && server[i].name != NULL) |
if (server[i].name != NULL) |
425 |
free_if_alloc (server[i].name); |
free_if_alloc (server[i].name); |
426 |
} |
} |
427 |
kserver_proxy_release (&proxy); |
kserver_proxy_release (&proxy); |
472 |
|
|
473 |
/* Set default socket timeout for all reading operations. */ |
/* Set default socket timeout for all reading operations. */ |
474 |
void |
void |
475 |
kserver_set_socket_timeout (int nsec) |
kserver_set_socket_timeout (size_t nsec) |
476 |
{ |
{ |
477 |
if (nsec < 0 || nsec > 3600) |
if (nsec < 0 || nsec > 3600) |
478 |
nsec = 0; |
nsec = 0; |
499 |
*port = default_keyserver_port; |
*port = default_keyserver_port; |
500 |
return default_keyserver; |
return default_keyserver; |
501 |
} |
} |
502 |
else if (!type && idx < DIM (server_list)) { |
else if (!type && (size_t)idx < DIM (server_list)) { |
503 |
/* XXX: handle non HKP servers. */ |
/* XXX: handle non HKP servers. */ |
504 |
*port = HKP_PORT; |
*port = HKP_PORT; |
505 |
return server_list[idx]; |
return server_list[idx]; |
537 |
return keyid; /* email address */ |
return keyid; /* email address */ |
538 |
if (strncmp (keyid, "0x", 2)) { |
if (strncmp (keyid, "0x", 2)) { |
539 |
memset (&id, 0, sizeof (id)); |
memset (&id, 0, sizeof (id)); |
540 |
_snprintf (id, sizeof (id)-1, "0x%s", keyid); |
_snprintf (id, DIM (id)-1, "0x%s", keyid); |
541 |
return id; |
return id; |
542 |
} |
} |
543 |
return keyid; |
return keyid; |
554 |
n = 4*strlen (proxy_user) / 3 + 32 + strlen (proxy_pass) + 2; |
n = 4*strlen (proxy_user) / 3 + 32 + strlen (proxy_pass) + 2; |
555 |
free_if_alloc (proxy.base64_user); |
free_if_alloc (proxy.base64_user); |
556 |
proxy.base64_user = safe_alloc (n+1); |
proxy.base64_user = safe_alloc (n+1); |
557 |
_snprintf (t, sizeof (t)-1, "%s:%s", proxy_user, proxy_pass); |
_snprintf (t, DIM (t)-1, "%s:%s", proxy_user, proxy_pass); |
558 |
base64_encode (t, proxy.base64_user); |
base64_encode (t, proxy.base64_user); |
559 |
free_if_alloc (proxy.user); |
free_if_alloc (proxy.user); |
560 |
free_if_alloc (proxy.pass); |
free_if_alloc (proxy.pass); |
568 |
port_from_proto (int proto) |
port_from_proto (int proto) |
569 |
{ |
{ |
570 |
switch (proto) { |
switch (proto) { |
571 |
case KSPROTO_LDAP: return 0; |
case KSPROTO_LDAP: return LDAP_PORT; |
572 |
case KSPROTO_FINGER: return FINGER_PORT; |
case KSPROTO_FINGER: return FINGER_PORT; |
573 |
case KSPROTO_HTTP: return HKP_PORT; |
case KSPROTO_HTTP: return HKP_PORT; |
574 |
} |
} |
582 |
{ |
{ |
583 |
if (strstr (buf, "ldap")) |
if (strstr (buf, "ldap")) |
584 |
return KSPROTO_LDAP; |
return KSPROTO_LDAP; |
585 |
else if (strstr( buf, "finger")) |
else if (strstr (buf, "finger")) |
586 |
return KSPROTO_FINGER; |
return KSPROTO_FINGER; |
587 |
return KSPROTO_HKP; |
return KSPROTO_HKP; |
588 |
} |
} |
655 |
} |
} |
656 |
|
|
657 |
|
|
658 |
|
/* Return the specified keyserver config setting @key as an integer. */ |
659 |
|
static int |
660 |
|
get_conf_kserver_int (const char *key) |
661 |
|
{ |
662 |
|
char *p; |
663 |
|
int val = 0; |
664 |
|
|
665 |
|
p = get_reg_entry_keyserver (key); |
666 |
|
if (p && *p) |
667 |
|
val = atoi (p); |
668 |
|
free_if_alloc (p); |
669 |
|
return val; |
670 |
|
} |
671 |
|
|
672 |
|
|
673 |
|
/* Read the proxy configuration and store it into @prox. */ |
674 |
|
static void |
675 |
|
read_proxy_config (keyserver_proxy_t prox) |
676 |
|
{ |
677 |
|
char *proto; |
678 |
|
|
679 |
|
if (!prox) |
680 |
|
return; |
681 |
|
|
682 |
|
proto = get_reg_entry_keyserver("Proto"); |
683 |
|
if (proto != NULL && strlen (proto) > 0) |
684 |
|
prox->proto = atoi (proto); |
685 |
|
else |
686 |
|
prox->proto = PROXY_PROTO_NONE; |
687 |
|
free_if_alloc (proto); |
688 |
|
free_if_alloc (prox->host); |
689 |
|
prox->host = get_reg_entry_keyserver ("Host"); |
690 |
|
free_if_alloc (prox->user); |
691 |
|
prox->user = get_reg_entry_keyserver ("User"); |
692 |
|
free_if_alloc (prox->pass); |
693 |
|
prox->pass = get_reg_entry_keyserver ("Pass"); |
694 |
|
prox->port = get_conf_kserver_int ("Port"); |
695 |
|
} |
696 |
|
|
697 |
|
|
698 |
/* Load the keyserver config file @conf. */ |
/* Load the keyserver config file @conf. */ |
699 |
int |
int |
700 |
kserver_load_conf (const char *conf) |
kserver_load_conf (const char *conf) |
702 |
FILE *fp; |
FILE *fp; |
703 |
char buf[1024], *s, *p; |
char buf[1024], *s, *p; |
704 |
char *user = NULL, *pass = NULL; |
char *user = NULL, *pass = NULL; |
705 |
int no_config=0, chk_pos=0; |
int no_config=0, chk_pos; |
706 |
int pos; |
int pos; |
707 |
|
|
708 |
for (pos = 0; pos < MAX_KEYSERVERS; pos++) { |
for (pos = 0; pos < MAX_KEYSERVERS; pos++) { |
709 |
server[pos].used = 0; |
server[pos].used = 0; |
710 |
server[pos].port = HKP_PORT; |
server[pos].port = HKP_PORT; |
711 |
|
server[pos].name = NULL; |
712 |
} |
} |
713 |
|
|
714 |
fp = fopen (conf, "rb"); |
fp = fopen (conf, "rb"); |
718 |
server[pos].name = m_strdup (server_list[pos]); |
server[pos].name = m_strdup (server_list[pos]); |
719 |
server[pos].proto = proto_from_URL (server_list[pos]); |
server[pos].proto = proto_from_URL (server_list[pos]); |
720 |
} |
} |
721 |
no_config=1; |
no_config = 1; |
722 |
} |
} |
723 |
get_reg_proxy_prefs (&proxy); |
read_proxy_config (&proxy); |
724 |
if (user && pass) |
if (user && pass) |
725 |
update_proxy_user (user, pass); |
update_proxy_user (user, pass); |
726 |
else if (user && !pass || !user && pass) { |
else if (user && !pass || !user && pass) { |
727 |
msg_box (NULL, _("Invalid proxy configuration." |
msg_box (NULL, _("Invalid proxy configuration. " |
728 |
"You need to set a user and a password" |
"You need to set a user and a password " |
729 |
"to use proxy authentication!"), |
"to use proxy authentication!"), |
730 |
_("Proxy Error"), MB_ERR); |
_("Proxy Error"), MB_ERR); |
731 |
} |
} |
740 |
|
|
741 |
pos = 0; |
pos = 0; |
742 |
while (fp && !feof (fp)) { |
while (fp && !feof (fp)) { |
743 |
s = fgets (buf, sizeof (buf)-1, fp); |
s = fgets (buf, DIM (buf)-1, fp); |
744 |
if (!s) |
if (!s) |
745 |
break; |
break; |
746 |
if (strstr (buf, "\r\n")) |
if (strstr (buf, "\r\n")) |
781 |
if (fp) |
if (fp) |
782 |
fclose (fp); |
fclose (fp); |
783 |
if (!pos) { |
if (!pos) { |
784 |
/* only issue an error if the config file exist but |
/* Only issue an error if the config file exist but |
785 |
it has no valid entries. */ |
it has no valid entries. */ |
786 |
keyserver_set_default (NULL, HKP_PORT); |
keyserver_set_default (NULL, HKP_PORT); |
787 |
if (!no_config) |
if (!no_config) |
801 |
struct sockaddr_in sock; |
struct sockaddr_in sock; |
802 |
char host[128] = {0}; |
char host[128] = {0}; |
803 |
DWORD iaddr; |
DWORD iaddr; |
804 |
|
bool use_proxy = proxy.host != NULL; |
805 |
int rc, fd; |
int rc, fd; |
806 |
|
|
|
log_debug ("kserver_connect: %s:%d\r\n", hostname, port); |
|
|
|
|
807 |
if (!port) |
if (!port) |
808 |
port = HKP_PORT; |
port = HKP_PORT; |
809 |
if (conn_fd) |
if (conn_fd) |
810 |
*conn_fd = 0; |
*conn_fd = 0; |
811 |
hostname = skip_type_prefix (hostname); |
hostname = skip_type_prefix (hostname); |
812 |
|
log_debug ("kserver_connect: %s:%d\r\n", hostname, port); |
813 |
|
|
814 |
if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) |
if (use_proxy && proxy.proto == PROXY_PROTO_HTTP) |
815 |
port = proxy.port; |
port = proxy.port; |
816 |
memset (&sock, 0, sizeof (sock)); |
memset (&sock, 0, sizeof (sock)); |
817 |
sock.sin_family = AF_INET; |
sock.sin_family = AF_INET; |
818 |
sock.sin_port = htons (port); |
sock.sin_port = htons (port); |
819 |
if (proxy.host) |
strncpy (host, use_proxy? proxy.host : hostname, DIM (host)-1); |
|
strncpy (host, proxy.host, 127); |
|
|
else |
|
|
strncpy (host, hostname, 127); |
|
820 |
|
|
821 |
if ((iaddr = inet_addr (host)) != INADDR_NONE) |
if ((iaddr = inet_addr (host)) != INADDR_NONE) |
822 |
memcpy (&sock.sin_addr, &iaddr, sizeof (iaddr)); |
memcpy (&sock.sin_addr, &iaddr, sizeof (iaddr)); |
829 |
} |
} |
830 |
else { |
else { |
831 |
log_debug ("gethostbyname: failed ec=%d.\r\n", net_errno); |
log_debug ("gethostbyname: failed ec=%d.\r\n", net_errno); |
832 |
return WPTERR_WINSOCK_RESOLVE; |
return use_proxy? WPTERR_WINSOCK_PROXY : WPTERR_WINSOCK_RESOLVE; |
833 |
} |
} |
834 |
fd = socket (AF_INET, SOCK_STREAM, 0); |
fd = socket (AF_INET, SOCK_STREAM, 0); |
835 |
if (fd == INVALID_SOCKET) |
if (fd == INVALID_SOCKET) |
838 |
if (rc == SOCKET_ERROR) { |
if (rc == SOCKET_ERROR) { |
839 |
log_debug ("connect: failed ec=%d.\r\n", net_errno); |
log_debug ("connect: failed ec=%d.\r\n", net_errno); |
840 |
closesocket (fd); |
closesocket (fd); |
841 |
return WPTERR_WINSOCK_CONNECT; |
return use_proxy? WPTERR_WINSOCK_PROXY : WPTERR_WINSOCK_CONNECT; |
842 |
} |
} |
843 |
|
|
844 |
if (proxy.proto == PROXY_PROTO_SOCKS5) { |
if (proxy.proto == PROXY_PROTO_SOCKS5) { |
845 |
|
/* XXX: finish code and provide errors constants. */ |
846 |
rc = socks_handshake (&proxy, fd, hostname, port); |
rc = socks_handshake (&proxy, fd, hostname, port); |
847 |
if (rc) { |
if (rc) { |
848 |
closesocket (fd); |
closesocket (fd); |
849 |
return WPTERR_GENERAL; /* XXX: use better code. */ |
return WPTERR_GENERAL; |
850 |
} |
} |
851 |
} |
} |
852 |
|
|
873 |
{ |
{ |
874 |
gpgme_data_t hd; |
gpgme_data_t hd; |
875 |
char numbuf[5], *p; |
char numbuf[5], *p; |
876 |
size_t i; |
size_t i, nlen; |
|
int nlen; |
|
877 |
|
|
878 |
if (gpgme_data_new (&hd)) |
if (gpgme_data_new (&hd)) |
879 |
BUG (0); |
BUG (0); |
956 |
/* Interface for receiving a public key. */ |
/* Interface for receiving a public key. */ |
957 |
int |
int |
958 |
kserver_recvkey (const char *hostname, WORD port, const char *keyid, |
kserver_recvkey (const char *hostname, WORD port, const char *keyid, |
959 |
char **r_key, int *r_keylen) |
char **r_key, size_t *r_keylen) |
960 |
{ |
{ |
961 |
const char *fmt; |
const char *fmt; |
962 |
char *request = NULL; |
char *request = NULL; |
1023 |
|
|
1024 |
/* Interface to send a public key. */ |
/* Interface to send a public key. */ |
1025 |
int |
int |
1026 |
kserver_sendkey (const char *hostname, WORD port, const char *pubkey, int len ) |
kserver_sendkey (const char *hostname, WORD port, |
1027 |
|
const char *pubkey, size_t len) |
1028 |
{ |
{ |
1029 |
char *request = NULL; |
char *request = NULL; |
1030 |
char log[2048] = {0}; |
char log[2048] = {0}; |
1037 |
goto leave; |
goto leave; |
1038 |
|
|
1039 |
request = kserver_send_request (hostname, port, pubkey, len); |
request = kserver_send_request (hostname, port, pubkey, len); |
|
if (request == NULL) { |
|
|
rc = WPTERR_GENERAL; |
|
|
goto leave; |
|
|
} |
|
|
|
|
1040 |
rc = sock_write (conn_fd, request, strlen (request)); |
rc = sock_write (conn_fd, request, strlen (request)); |
1041 |
if (rc == SOCKET_ERROR) { |
if (rc == SOCKET_ERROR) { |
1042 |
rc = WPTERR_WINSOCK_SENDKEY; |
rc = WPTERR_WINSOCK_SENDKEY; |
1043 |
goto leave; |
goto leave; |
1044 |
} |
} |
1045 |
|
|
1046 |
rc = sock_read (conn_fd, log, sizeof (log)-1, &n); |
rc = sock_read (conn_fd, log, DIM (log)-1, &n); |
1047 |
if (rc == SOCKET_ERROR) { |
if (rc == SOCKET_ERROR) { |
1048 |
rc = WPTERR_WINSOCK_SENDKEY; |
rc = WPTERR_WINSOCK_SENDKEY; |
1049 |
goto leave; |
goto leave; |
1055 |
goto leave; |
goto leave; |
1056 |
|
|
1057 |
WSASetLastError (0); |
WSASetLastError (0); |
1058 |
|
|
1059 |
leave: |
leave: |
1060 |
closesocket (conn_fd); |
closesocket (conn_fd); |
1061 |
free_if_alloc (request); |
free_if_alloc (request); |
1071 |
int n=0; |
int n=0; |
1072 |
|
|
1073 |
/* parse response 'HTTP/1.0 500 OK' */ |
/* parse response 'HTTP/1.0 500 OK' */ |
1074 |
if (sock_getline (fd, buf, sizeof (buf)-1, &n)) |
if (sock_getline (fd, buf, DIM (buf)-1, &n)) |
1075 |
return WPTERR_KEYSERVER_NOTFOUND; |
return WPTERR_KEYSERVER_NOTFOUND; |
1076 |
|
|
1077 |
log_debug ("kserver_search_chkpresp: %s\r\n", buf); |
log_debug ("kserver_search_chkpresp: %s\r\n", buf); |
1093 |
|
|
1094 |
|
|
1095 |
/* Extract the amount of keys from the info record. */ |
/* Extract the amount of keys from the info record. */ |
1096 |
static int |
static size_t |
1097 |
count_keys_in_response (char *buf) |
count_keys_in_response (char *buf) |
1098 |
{ |
{ |
1099 |
char *p; |
char *p; |
1100 |
int recno = 0; |
int recno = 0; |
1101 |
int n = 0; |
size_t n = 0; |
1102 |
|
|
1103 |
/* info:1:4 */ |
/* info:1:4 */ |
1104 |
if (strncmp (buf, "info", 4)) |
if (strncmp (buf, "info", 4)) |
1121 |
the amount of keys which were found. */ |
the amount of keys which were found. */ |
1122 |
int |
int |
1123 |
kserver_search_begin (const char *hostname, WORD port, |
kserver_search_begin (const char *hostname, WORD port, |
1124 |
const char *pattern, int *conn_fd, int *nkeys) |
const char *pattern, int *conn_fd, size_t *nkeys) |
1125 |
{ |
{ |
1126 |
const char *fmt; |
const char *fmt; |
1127 |
char *request = NULL; |
char *request = NULL; |
1176 |
|
|
1177 |
/* Skip all lines until we reach the "info:" record. */ |
/* Skip all lines until we reach the "info:" record. */ |
1178 |
for (;;) { |
for (;;) { |
1179 |
if (sock_getline (sock_fd, status, sizeof (status)-1, &reqlen)) { |
if (sock_getline (sock_fd, status, DIM (status)-1, &reqlen)) { |
1180 |
log_debug ("kserver_search_begin: retrieving status line failed.\r\n"); |
log_debug ("kserver_search_begin: retrieving status line failed.\r\n"); |
1181 |
closesocket (sock_fd); |
closesocket (sock_fd); |
1182 |
sock_fd = 0; |
sock_fd = 0; |
1205 |
enum pub_rec_t {ID=1, KEYID, ALGO, SIZE, CREATE, EXPIRE}; |
enum pub_rec_t {ID=1, KEYID, ALGO, SIZE, CREATE, EXPIRE}; |
1206 |
char *p; |
char *p; |
1207 |
int recno = 0; |
int recno = 0; |
1208 |
|
int off = 0; |
1209 |
|
|
1210 |
/* pub:BF3DF9B4:17:1024:925411133:: */ |
/* pub:BF3DF9B4:17:1024:925411133:: */ |
1211 |
p = strtok (buf, ":"); |
p = strtok (buf, ":"); |
1215 |
case ID: |
case ID: |
1216 |
break; |
break; |
1217 |
|
|
1218 |
case KEYID: |
case KEYID: |
1219 |
|
/* If for any reason the returned record actually contains |
1220 |
|
a fingerprint instead of a key ID, we truncate the fpr. */ |
1221 |
|
off = strlen (p) == 40? 32 : 0; |
1222 |
free_if_alloc (key->keyid); |
free_if_alloc (key->keyid); |
1223 |
key->keyid = m_strdup (p); |
key->keyid = m_strdup (p+off); |
1224 |
break; |
break; |
1225 |
|
|
1226 |
case ALGO: |
case ALGO: |
1241 |
} |
} |
1242 |
p = strtok (NULL, ":"); |
p = strtok (NULL, ":"); |
1243 |
} |
} |
|
|
|
1244 |
} |
} |
1245 |
|
|
1246 |
|
|
1250 |
{ |
{ |
1251 |
enum uid_rec_t {ID=1, UID, CREATE, EXPIRE}; |
enum uid_rec_t {ID=1, UID, CREATE, EXPIRE}; |
1252 |
keyserver_uid_s *u, *n; |
keyserver_uid_s *u, *n; |
1253 |
char *p; |
char *p, *raw; |
1254 |
int recno = 0; |
int recno = 0; |
1255 |
|
|
1256 |
/* uid:Timo Schulz <[email protected]>:1138440360:: */ |
/* uid:Timo Schulz <[email protected]>:1138440360:: */ |
1266 |
break; |
break; |
1267 |
|
|
1268 |
case UID: |
case UID: |
1269 |
unhexify_buffer (p, &u->uid); |
unhexify_buffer (p, &raw); |
1270 |
|
u->uid = utf8_to_native (raw); |
1271 |
|
free_if_alloc (raw); |
1272 |
break; |
break; |
1273 |
|
|
1274 |
case CREATE: |
case CREATE: |
1324 |
key = new keyserver_key_s; |
key = new keyserver_key_s; |
1325 |
memset (key, 0, sizeof *key); |
memset (key, 0, sizeof *key); |
1326 |
for (;;) { |
for (;;) { |
1327 |
if (sock_getline (fd, buf, sizeof (buf)-1, &n)) |
if (sock_getline (fd, buf, DIM (buf)-1, &n)) |
1328 |
break; |
break; |
1329 |
/*log_debug ("record: '%s'\r\n", buf); */ |
/*log_debug ("record: '%s'\r\n", buf); */ |
1330 |
if (!strncmp (buf, "pub", 3)) |
if (!strncmp (buf, "pub", 3)) |
1431 |
fp = NULL; |
fp = NULL; |
1432 |
goto leave; |
goto leave; |
1433 |
} |
} |
1434 |
get_temp_name (outf, sizeof (outf)-1, keyid); |
get_temp_name (outf, DIM (outf)-1, keyid); |
1435 |
get_temp_name (inf, sizeof (inf)-1, NULL); |
get_temp_name (inf, DIM (inf)-1, NULL); |
1436 |
fp = fopen (inf, "w+b"); |
fp = fopen (inf, "w+b"); |
1437 |
if (!fp) { |
if (!fp) { |
1438 |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", inf, |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", inf, |
1441 |
} |
} |
1442 |
fprintf (fp, |
fprintf (fp, |
1443 |
"VERSION 1\n" |
"VERSION 1\n" |
1444 |
"PROGRAM 1.4.3-cvs\n" |
"PROGRAM %d.%d.%d\n" |
1445 |
"SCHEME ldap\n" |
"SCHEME ldap\n" |
1446 |
"HOST %s\n" |
"HOST %s\n" |
1447 |
"COMMAND GET\n" |
"COMMAND GET\n" |
1448 |
"\n" |
"\n" |
1449 |
"%s\n", |
"%s\n", |
1450 |
|
gpgver[0], gpgver[1], gpgver[2], |
1451 |
host? skip_type_prefix (host): "64.94.85.200", keyid); |
host? skip_type_prefix (host): "64.94.85.200", keyid); |
1452 |
fclose (fp); |
fclose (fp); |
1453 |
|
|
1474 |
@key contains the key on success. */ |
@key contains the key on success. */ |
1475 |
int |
int |
1476 |
ldap_recvkey (const char *host, const char *keyid, |
ldap_recvkey (const char *host, const char *keyid, |
1477 |
char **r_key, int *r_keylen) |
char **r_key, size_t *r_keylen) |
1478 |
{ |
{ |
1479 |
gpgme_data_t raw; |
gpgme_data_t raw; |
1480 |
FILE *fp; |
FILE *fp; |
1490 |
if (gpgme_data_new (&raw)) |
if (gpgme_data_new (&raw)) |
1491 |
BUG (0); |
BUG (0); |
1492 |
while (!feof (fp)) { |
while (!feof (fp)) { |
1493 |
s = fgets (buf, sizeof (buf)-1, fp); |
s = fgets (buf, DIM (buf)-1, fp); |
1494 |
if (!s) |
if (!s) |
1495 |
break; |
break; |
1496 |
if (strstr (s, "KEY") && strstr (s, "FAILED")) { |
if (strstr (s, "KEY") && strstr (s, "FAILED")) { |
1520 |
On success @key contains the key. */ |
On success @key contains the key. */ |
1521 |
int |
int |
1522 |
finger_recvkey (const char *host, const char *user, |
finger_recvkey (const char *host, const char *user, |
1523 |
char **r_key, int *r_keylen) |
char **r_key, size_t *r_keylen) |
1524 |
{ |
{ |
1525 |
gpgme_data_t raw; |
gpgme_data_t raw; |
1526 |
char buf[256]; |
char buf[256+1]; |
1527 |
int fd, nread; |
int fd, nread; |
1528 |
int start_key = 0; |
int start_key = 0; |
1529 |
int rc=0; |
int rc=0; |
1536 |
sock_write (fd, "\r\n", 2); |
sock_write (fd, "\r\n", 2); |
1537 |
|
|
1538 |
if (gpgme_data_new (&raw)) |
if (gpgme_data_new (&raw)) |
1539 |
BUG (0); |
return WPTERR_WINSOCK_RECVKEY; |
1540 |
|
|
1541 |
for (;;) { |
for (;;) { |
1542 |
if (sock_getline (fd, buf, sizeof (buf), &nread)) |
if (sock_getline (fd, buf, DIM (buf)-2, &nread)) |
1543 |
break; |
break; |
1544 |
strcat (buf, "\n"); |
strcat (buf, "\n"); |
1545 |
if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) { |
if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) { |
1563 |
} |
} |
1564 |
|
|
1565 |
|
|
1566 |
/* Check if the given name @name is a valid hostname. */ |
/* Check if the given name @name is a valid inernet address |
1567 |
|
which means an dotted IP or a valid DNS name. |
1568 |
|
Return value: 0 on success. */ |
1569 |
int |
int |
1570 |
check_IP_or_hostname (const char *name) |
check_inet_address (const char *addr) |
1571 |
{ |
{ |
1572 |
const char *not_allowed = "=!�$%&@#*~\\/}][{<>|,;:'"; |
const char *not_allowed = "=!�$%&@#*~\\/}][{<>|,;:'"; |
1573 |
size_t i, j; |
size_t i; |
1574 |
|
|
1575 |
for (i=0; i < strlen (name); i++) { |
for (i=0; i < strlen (addr); i++) { |
1576 |
for (j =0; j < strlen (not_allowed); j++) { |
if (strchr (not_allowed, addr[i])) |
1577 |
if (name[i] == not_allowed[j]) |
return -1; |
|
return -1; |
|
|
} |
|
1578 |
} |
} |
1579 |
return 0; |
return 0; |
1580 |
} |
} |
1587 |
{ |
{ |
1588 |
char *p; |
char *p; |
1589 |
char *url = *r_keyserver; |
char *url = *r_keyserver; |
1590 |
|
int off = 0; |
1591 |
|
|
1592 |
/* no port is given so use the default port. */ |
/* no port is given so use the default port. */ |
1593 |
p = strrchr (url, ':'); |
p = strrchr (url, ':'); |
1598 |
*r_port = port; |
*r_port = port; |
1599 |
return 0; |
return 0; |
1600 |
} |
} |
1601 |
/* XXX: remove / in .de/:11371 */ |
|
1602 |
*r_keyserver = substr (url, 0, (p-url)); |
if (url[(p-url)-1] == '/') /* remove / in .de/:11371 */ |
1603 |
|
off = 1; |
1604 |
|
|
1605 |
|
*r_keyserver = substr (url, 0, (p-url)-off); |
1606 |
*r_port = atoi (url+(p-url)+1); |
*r_port = atoi (url+(p-url)+1); |
1607 |
free_if_alloc (url); |
free_if_alloc (url); |
1608 |
return 0; |
return 0; |