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 |
|
|
56 |
"hkp://wwwkeys.eu.pgp.net", |
"hkp://wwwkeys.eu.pgp.net", |
57 |
"hkp://wwwkeys.uk.pgp.net", |
"hkp://wwwkeys.uk.pgp.net", |
58 |
"hkp://wwwkeys.us.pgp.net", |
"hkp://wwwkeys.us.pgp.net", |
|
"hkp://gnv.us.ks.cryptnet.net", |
|
59 |
"hkp://subkeys.pgp.net", |
"hkp://subkeys.pgp.net", |
60 |
"ldap://keyserver.pgp.com", |
"ldap://keyserver.pgp.com", |
61 |
NULL |
NULL |
66 |
static int hkp_err = 0; /* != 0 indicates an error occurred. */ |
static int hkp_err = 0; /* != 0 indicates an error occurred. */ |
67 |
static DWORD conf_timestamp = 0;/* timestamp of the configuration fiele. */ |
static DWORD conf_timestamp = 0;/* timestamp of the configuration fiele. */ |
68 |
|
|
69 |
char *default_keyserver = NULL; |
/* Default keyserver and port. */ |
70 |
WORD default_keyserver_port = 0; |
char *default_keyserver = NULL; |
71 |
|
WORD default_keyserver_port = 0; |
72 |
|
|
73 |
|
|
74 |
/* Basic64 encode the input @inputstr to @outputstr. */ |
/* Basic64 encode the input @inputstr to @outputstr. */ |
83 |
res = temp; |
res = temp; |
84 |
res = (res << 8) | (inputstr[i] & 0xFF); |
res = (res << 8) | (inputstr[i] & 0xFF); |
85 |
switch (index++) { |
switch (index++) { |
86 |
case 0: outputstr[len++] = base64code[res >> 2 & 0x3F]; res &= 0x3; break; |
case 0: outputstr[len++] = base64code[res >> 2 & 0x3F]; res &= 0x3; break; |
87 |
case 1: outputstr[len++] = base64code[res >> 4 & 0x3F]; res &= 0xF; break; |
case 1: outputstr[len++] = base64code[res >> 4 & 0x3F]; res &= 0xF; break; |
88 |
case 2: outputstr[len++] = base64code[res >> 6 & 0x3F]; |
case 2: outputstr[len++] = base64code[res >> 6 & 0x3F]; |
89 |
outputstr[len++] = base64code[res & 0x3F]; res = index = 0; break; |
outputstr[len++] = base64code[res & 0x3F]; res = index = 0; break; |
90 |
} |
} |
233 |
return 0; |
return 0; |
234 |
} |
} |
235 |
|
|
236 |
|
/* Read much data as possible from the socket @fd and |
237 |
|
return the data in @buffer. Caller must free data. |
238 |
|
Return value: 0 on success. */ |
239 |
|
int |
240 |
|
sock_read_ext (int fd, char **buffer, int *r_bufferlen) |
241 |
|
{ |
242 |
|
gpgme_data_t dh; |
243 |
|
char buf[1024], *p; |
244 |
|
size_t n=0; |
245 |
|
int nread, rc; |
246 |
|
|
247 |
|
gpgme_data_new (&dh); |
248 |
|
while (n < 10) { |
249 |
|
rc = sock_select (fd, 1); |
250 |
|
if (rc == SOCKET_ERROR) { |
251 |
|
gpgme_data_release (dh); |
252 |
|
return rc; |
253 |
|
} |
254 |
|
else if (!rc) |
255 |
|
n++; |
256 |
|
else { |
257 |
|
nread = recv (fd, buf, sizeof (buf), 0); |
258 |
|
if (nread == SOCKET_ERROR) |
259 |
|
return SOCKET_ERROR; |
260 |
|
else if (!nread) |
261 |
|
break; |
262 |
|
gpgme_data_write (dh, buf, nread); |
263 |
|
} |
264 |
|
} |
265 |
|
gpg_data_putc (dh, '\0'); |
266 |
|
p = gpgme_data_release_and_get_mem (dh, &n); |
267 |
|
*buffer = m_strdup (p); |
268 |
|
if (r_bufferlen) |
269 |
|
*r_bufferlen = n; |
270 |
|
gpgme_free (p); |
271 |
|
return 0; |
272 |
|
} |
273 |
|
|
274 |
|
|
275 |
/* Write the buffer @buf with the length @buflen to a socket @fd. */ |
/* Write the buffer @buf with the length @buflen to a socket @fd. */ |
276 |
static int |
static int |
329 |
void |
void |
330 |
wsock_end (void) |
wsock_end (void) |
331 |
{ |
{ |
332 |
|
char *p; |
333 |
int i; |
int i; |
334 |
|
|
335 |
|
p = make_special_filename (CSIDL_APPDATA, "winpt\\keyserver.conf", NULL); |
336 |
|
kserver_save_conf (p); |
337 |
|
free_if_alloc (p); |
338 |
free_if_alloc (default_keyserver); |
free_if_alloc (default_keyserver); |
|
default_keyserver = NULL; |
|
339 |
for (i=0; i < MAX_KEYSERVERS; i++) { |
for (i=0; i < MAX_KEYSERVERS; i++) { |
340 |
if (server[i].used) |
if (server[i].used) |
341 |
free_if_alloc (server[i].name); |
free_if_alloc (server[i].name); |
524 |
} |
} |
525 |
|
|
526 |
|
|
527 |
|
/* Save the keyserver config file in @conf. */ |
528 |
int |
int |
529 |
kserver_load_conf (const char * conf) |
kserver_save_conf (const char *conf) |
530 |
|
{ |
531 |
|
FILE *fp; |
532 |
|
int pos; |
533 |
|
|
534 |
|
fp = fopen (conf, "wb"); |
535 |
|
if (!fp) { |
536 |
|
msg_box (NULL, _("Could not save keyserver.conf file"), |
537 |
|
_("Keyserver"), MB_ERR); |
538 |
|
return -1; |
539 |
|
} |
540 |
|
|
541 |
|
fprintf (fp, "# do NOT manually modify this file, it will be generated automatically!!\r\n"); |
542 |
|
for (pos = 0; pos < MAX_KEYSERVERS; pos++) { |
543 |
|
if (!server[pos].used) |
544 |
|
continue; |
545 |
|
fprintf (fp, "%s:%d\r\n", server[pos].name, server[pos].port); |
546 |
|
} |
547 |
|
fclose (fp); |
548 |
|
return 0; |
549 |
|
} |
550 |
|
|
551 |
|
|
552 |
|
/* Load the keyserver config file @conf. */ |
553 |
|
int |
554 |
|
kserver_load_conf (const char *conf) |
555 |
{ |
{ |
556 |
struct stat statbuf; |
struct stat statbuf; |
557 |
FILE *fp; |
FILE *fp; |
578 |
if (user && pass) |
if (user && pass) |
579 |
kserver_update_proxyuser (user, pass); |
kserver_update_proxyuser (user, pass); |
580 |
else if (user && !pass || !user && pass) { |
else if (user && !pass || !user && pass) { |
581 |
msg_box( NULL, _("Invalid proxy configuration." |
msg_box (NULL, _("Invalid proxy configuration." |
582 |
"You need to set a user and a password" |
"You need to set a user and a password" |
583 |
"to use proxy authentication!"), _("Proxy Error"), MB_ERR ); |
"to use proxy authentication!"), |
584 |
|
_("Proxy Error"), MB_ERR); |
585 |
} |
} |
586 |
/* check if the host has a http:// prefix and skip it */ |
/* check if the host has a http:// prefix and skip it */ |
587 |
if( proxy.host && !strncmp( proxy.host, "http://", 7 ) ) { |
if (proxy.host && !strncmp (proxy.host, "http://", 7)) { |
588 |
char *host = m_strdup (proxy.host+7); |
char *host = m_strdup (proxy.host+7); |
589 |
if (!host) |
if (!host) |
590 |
BUG (0); |
BUG (0); |
595 |
free_if_alloc (pass); |
free_if_alloc (pass); |
596 |
|
|
597 |
pos = 0; |
pos = 0; |
598 |
while( fp && !feof( fp ) ) { |
while (fp && !feof (fp)) { |
599 |
s = fgets (buf, sizeof (buf)-1, fp); |
s = fgets (buf, sizeof (buf)-1, fp); |
600 |
if (!s) |
if (!s) |
601 |
break; |
break; |
632 |
memcpy (server[pos].name, s, (p-s)); |
memcpy (server[pos].name, s, (p-s)); |
633 |
} |
} |
634 |
pos++; |
pos++; |
635 |
if (pos > MAX_KEYSERVERS) |
if (pos > MAX_KEYSERVERS) { |
636 |
{ |
msg_box (NULL, _("The keyserver limit is exceeded"), |
637 |
msg_box (NULL, _("The keyserver limit is exceeded"), _("Keyserver Warning"), MB_INFO); |
_("Keyserver Warning"), MB_INFO); |
638 |
break; |
break; |
639 |
} |
} |
640 |
} |
} |
644 |
/* 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 it has no valid entries. */ |
645 |
keyserver_set_default (NULL, HKP_PORT); |
keyserver_set_default (NULL, HKP_PORT); |
646 |
if (!no_config) |
if (!no_config) |
647 |
return WPTERR_CONFIG_FILE; |
return WPTERR_CONFIG_FILE; |
648 |
} |
} |
649 |
|
|
650 |
if (!stat (conf, &statbuf)) |
if (!stat (conf, &statbuf)) |
651 |
conf_timestamp = statbuf.st_mtime; |
conf_timestamp = statbuf.st_mtime; |
652 |
return 0; |
return 0; |
653 |
} /* kserver_load_conf */ |
} |
654 |
|
|
655 |
|
|
656 |
/* Return the proxy host and port if available. Null otherwise. */ |
/* Return the proxy host and port if available. Null otherwise. */ |
735 |
} |
} |
736 |
|
|
737 |
|
|
738 |
/* Perform URL-encoding on the given pubkey blob. */ |
static bool |
739 |
static char* |
URL_must_encoded (const char *url) |
|
kserver_urlencode (const char *pubkey, size_t octets, size_t *newlen) |
|
740 |
{ |
{ |
741 |
char *p, numbuf[5]; |
if (strchr (url, '.') || strchr (url, '@') || strchr (url, ' ')) |
742 |
size_t j; |
return true; |
743 |
size_t i, size; |
return false; |
744 |
|
} |
|
p = new char [2*octets+1]; |
|
|
if (!p) |
|
|
BUG (0); |
|
745 |
|
|
746 |
for (size = 0, i = 0; i < octets; i++) { |
|
747 |
if (isalnum (pubkey[i]) || pubkey[i] == '-') { |
/* Perform URL encoding of the given data. */ |
748 |
p[size] = pubkey[i]; |
static char* |
749 |
size++; |
URL_encode (const char *url, size_t ulen, size_t *ret_nlen) |
750 |
} |
{ |
751 |
else if (pubkey[i] == ' ') { |
gpgme_data_t hd; |
752 |
p[size] = '+'; |
char numbuf[5], *pp, *p; |
753 |
size++; |
size_t i, n; |
754 |
} |
|
755 |
|
gpgme_data_new (&hd); |
756 |
|
for (i=0; i < ulen; i++) { |
757 |
|
if (isalnum (url[i]) || url[i] == '-') |
758 |
|
gpg_data_putc (hd, url[i]); |
759 |
|
else if (url[i] == ' ') |
760 |
|
gpg_data_putc (hd, '+'); |
761 |
else { |
else { |
762 |
sprintf (numbuf, "%%%02X", pubkey[i]); |
sprintf (numbuf, "%%%02X", url[i]); |
763 |
for (j = 0; j < strlen (numbuf); j++) { |
gpgme_data_write (hd, numbuf, strlen (numbuf)); |
|
p[size] = numbuf[j]; |
|
|
size++; |
|
|
} |
|
764 |
} |
} |
765 |
} |
} |
766 |
p[size] = '\0'; |
gpg_data_putc (hd, '\0'); |
767 |
*newlen = size; |
|
768 |
|
/* Copy memory to avoid that we need to use gpgme_free later. */ |
769 |
|
pp = gpgme_data_release_and_get_mem (hd, &n); |
770 |
|
p = m_strdup (pp); |
771 |
|
gpgme_free (pp); |
772 |
|
if (ret_nlen) |
773 |
|
*ret_nlen = n; |
774 |
return p; |
return p; |
775 |
} |
} |
776 |
|
|
793 |
request = new char[reqlen]; |
request = new char[reqlen]; |
794 |
if (!request) |
if (!request) |
795 |
BUG (0); |
BUG (0); |
796 |
enc_pubkey = kserver_urlencode (pubkey, octets, &enc_octets); |
enc_pubkey = URL_encode (pubkey, octets, &enc_octets); |
797 |
if (!enc_pubkey || !enc_octets) { |
if (!enc_pubkey || !enc_octets) { |
798 |
free_if_alloc (request); |
free_if_alloc (request); |
799 |
return NULL; |
return NULL; |
835 |
} |
} |
836 |
|
|
837 |
|
|
|
/* Interface for receiving a public key. */ |
|
838 |
int |
int |
839 |
kserver_recvkey (const char *hostname, WORD port, const char *keyid, |
kserver_recvkey_ext (const char *hostname, WORD port, const char *keyid, |
840 |
char *key, int maxkeylen) |
char **r_key, int *r_keylen) |
841 |
{ |
{ |
842 |
char *request = NULL; |
char *request = NULL; |
843 |
int conn_fd; |
int conn_fd; |
878 |
goto leave; |
goto leave; |
879 |
} |
} |
880 |
|
|
881 |
rc = sock_read( conn_fd, key, maxkeylen, &n ); |
rc = sock_read_ext (conn_fd, r_key, &n); |
882 |
if( rc == SOCKET_ERROR ) { |
if (rc == SOCKET_ERROR) { |
883 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
884 |
goto leave; |
goto leave; |
885 |
} |
} |
886 |
|
|
887 |
log_debug("%s\r\n", key); |
if (r_keylen) |
888 |
rc = check_hkp_response (key, 1); |
*r_keylen = n; |
889 |
|
log_debug("%s\r\n", *r_key); |
890 |
|
rc = check_hkp_response (*r_key, 1); |
891 |
if (rc) |
if (rc) |
892 |
goto leave; |
goto leave; |
893 |
|
|
899 |
return rc; |
return rc; |
900 |
} |
} |
901 |
|
|
902 |
|
/* Interface for receiving a public key. */ |
903 |
|
int |
904 |
|
kserver_recvkey (const char *hostname, WORD port, const char *keyid, |
905 |
|
char *key, int maxkeylen) |
906 |
|
{ |
907 |
|
char *tmpkey; |
908 |
|
int rc, nread=0; |
909 |
|
|
910 |
|
/* XXX: just a wrapper around the new method, replace it |
911 |
|
soon as possible. */ |
912 |
|
rc = kserver_recvkey_ext (hostname, port, keyid, &tmpkey, &nread); |
913 |
|
if (rc) |
914 |
|
return rc; |
915 |
|
|
916 |
|
if (nread > maxkeylen) { |
917 |
|
free_if_alloc (tmpkey); |
918 |
|
return WPTERR_GENERAL; |
919 |
|
} |
920 |
|
strcpy (key, tmpkey); |
921 |
|
free_if_alloc (tmpkey); |
922 |
|
return 0; |
923 |
|
} |
924 |
|
|
925 |
|
|
926 |
/* Interface to send a public key. */ |
/* Interface to send a public key. */ |
927 |
int |
int |
974 |
const char *keyid, int *conn_fd) |
const char *keyid, int *conn_fd) |
975 |
{ |
{ |
976 |
char *request = NULL; |
char *request = NULL; |
977 |
int n=0; |
char *enc_keyid = NULL; |
978 |
|
int n = 0; |
979 |
int rc, sock_fd; |
int rc, sock_fd; |
980 |
|
|
981 |
rc = kserver_connect (hostname, port, &sock_fd); |
rc = kserver_connect (hostname, port, &sock_fd); |
982 |
if (rc) { |
if (rc) { |
983 |
*conn_fd = 0; |
*conn_fd = 0; |
984 |
goto leave; |
goto leave; |
985 |
} |
} |
986 |
|
|
987 |
|
enc_keyid = URL_encode (keyid, strlen (keyid), NULL); |
988 |
n=300; |
n=300; |
989 |
request = new char[n+1]; |
request = new char[n+1]; |
990 |
if (!request) |
if (!request) |
994 |
_snprintf (request, n, |
_snprintf (request, n, |
995 |
"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" |
996 |
"Proxy-Authorization: Basic %s\r\n\r\n", |
"Proxy-Authorization: Basic %s\r\n\r\n", |
997 |
skip_type_prefix (hostname), port, keyid, proxy.base64_user); |
skip_type_prefix (hostname), port, enc_keyid, proxy.base64_user); |
998 |
} |
} |
999 |
else if (proxy.host) { |
else if (proxy.host) { |
1000 |
_snprintf (request, n, |
_snprintf (request, n, |
1001 |
"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", |
1002 |
skip_type_prefix (hostname), port, keyid); |
skip_type_prefix (hostname), port, enc_keyid); |
1003 |
} |
} |
1004 |
else { |
else { |
1005 |
_snprintf (request, n, |
_snprintf (request, n, |
1006 |
"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", |
1007 |
|
enc_keyid); |
1008 |
} |
} |
1009 |
|
|
1010 |
log_debug ("kserver_search_init:\r\n%s\r\n", request); |
log_debug ("kserver_search_init:\r\n%s\r\n", request); |
1018 |
|
|
1019 |
leave: |
leave: |
1020 |
free_if_alloc (request); |
free_if_alloc (request); |
1021 |
|
free_if_alloc (enc_keyid); |
1022 |
return rc; |
return rc; |
1023 |
} |
} |
1024 |
|
|
1043 |
} |
} |
1044 |
|
|
1045 |
|
|
1046 |
|
/* Convert an iso date @iso_date (YYYY-MM-DD) into the locale |
1047 |
|
representation and store it into @loc_date. |
1048 |
|
Return value: 0 on success. */ |
1049 |
|
static int |
1050 |
|
parse_iso_date (const char *iso_date, char *loc_date, size_t loclen) |
1051 |
|
{ |
1052 |
|
SYSTEMTIME st; |
1053 |
|
char buf[16] = {0}, *p; |
1054 |
|
int pos=0; |
1055 |
|
|
1056 |
|
strncpy (buf, iso_date, sizeof (buf)-1); |
1057 |
|
p = strtok (buf, "-"); |
1058 |
|
while (p != NULL) { |
1059 |
|
switch (pos) { |
1060 |
|
case 0: st.wYear = (WORD)atoi (p); pos++; break; |
1061 |
|
case 1: st.wMonth = (WORD)atoi (p); pos++; break; |
1062 |
|
case 2: st.wDay = (WORD)atoi (p); pos++; break; |
1063 |
|
default: break; |
1064 |
|
} |
1065 |
|
p = strtok (NULL, "-"); |
1066 |
|
} |
1067 |
|
if (pos != 3) |
1068 |
|
return -1; |
1069 |
|
|
1070 |
|
if (!GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, |
1071 |
|
NULL, loc_date, loclen)) |
1072 |
|
return -1; |
1073 |
|
return 0; |
1074 |
|
} |
1075 |
|
|
1076 |
|
|
1077 |
int |
int |
1078 |
kserver_search (int fd, keyserver_key * key) |
kserver_search (int fd, keyserver_key *key) |
1079 |
{ |
{ |
1080 |
char buf[1024], *p; |
char buf[1024], *p; |
1081 |
int uidlen, nbytes, pos = 0; |
int uidlen, nbytes, pos = 0; |
1102 |
pos = p - buf + 5; |
pos = p - buf + 5; |
1103 |
memcpy (key->date, buf + pos, 10); |
memcpy (key->date, buf + pos, 10); |
1104 |
key->date[10] = '\0'; |
key->date[10] = '\0'; |
1105 |
|
parse_iso_date (key->date, key->date, sizeof (key->date)-1); |
1106 |
if (revoked) { |
if (revoked) { |
1107 |
strcpy (key->uid, "KEY REVOKED: not checked"); |
strcpy (key->uid, "KEY REVOKED: not checked"); |
1108 |
return 0; |
return 0; |
1113 |
p++; |
p++; |
1114 |
p++; |
p++; |
1115 |
uidlen = strlen (p) - 10; |
uidlen = strlen (p) - 10; |
1116 |
|
if (!strstr (p, "<") && !strstr (p, ">")) { |
1117 |
|
pos=0; |
1118 |
|
while (p && *p && pos < sizeof (key->uid)-1) { |
1119 |
|
if (*p == '<') |
1120 |
|
break; |
1121 |
|
key->uid[pos++] = *p++; |
1122 |
|
} |
1123 |
|
key->uid[pos] ='\0'; |
1124 |
|
return 0; |
1125 |
|
} |
1126 |
|
|
1127 |
|
if (uidlen > sizeof (key->uid)-1) |
1128 |
|
uidlen = sizeof (key->uid)-1; |
1129 |
memcpy (key->uid, p, uidlen); |
memcpy (key->uid, p, uidlen); |
1130 |
key->uid[uidlen] = '\0'; |
key->uid[uidlen] = '\0'; |
1131 |
strcat (key->uid, ">"); |
strcat (key->uid, ">"); |
1142 |
key->bits = 0; |
key->bits = 0; |
1143 |
memset (key, 0, sizeof *key); |
memset (key, 0, sizeof *key); |
1144 |
return 0; |
return 0; |
1145 |
} /* 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 */ |
|
1146 |
|
|
1147 |
|
|
1148 |
|
/* Replace all current proxy items with the items |
1149 |
|
from the context @ctx. */ |
1150 |
void |
void |
1151 |
kserver_change_proxy( keyserver_proxy_ctx *ctx ) |
kserver_change_proxy (keyserver_proxy_ctx *ctx) |
1152 |
{ |
{ |
1153 |
proxy.port = ctx->port; |
proxy.port = ctx->port; |
1154 |
free_if_alloc (proxy.host); |
free_if_alloc (proxy.host); |
1155 |
proxy.host = ctx->host? m_strdup( ctx->host ) : NULL; |
proxy.host = ctx->host? m_strdup (ctx->host) : NULL; |
1156 |
free_if_alloc( proxy.pass ); |
free_if_alloc (proxy.pass); |
1157 |
proxy.pass = ctx->pass? m_strdup( ctx->pass ) : NULL; |
proxy.pass = ctx->pass? m_strdup (ctx->pass) : NULL; |
1158 |
free_if_alloc( proxy.user ); |
free_if_alloc (proxy.user); |
1159 |
proxy.user = ctx->user? m_strdup( ctx->user ) : NULL; |
proxy.user = ctx->user? m_strdup (ctx->user) : NULL; |
1160 |
set_reg_proxy_prefs( proxy.host, proxy.port, proxy.user, proxy.pass ); |
set_reg_proxy_prefs (proxy.host, proxy.port, proxy.user, proxy.pass); |
1161 |
} /* kserver_change_proxy */ |
} |
1162 |
|
|
1163 |
|
|
1164 |
int |
/* Release mbmers in the proxy context @ctx. */ |
1165 |
kserver_read_config( const char *fname, keyserver_cfgfile **ret_cfg ) |
void |
1166 |
|
proxy_release (keyserver_proxy_ctx *ctx) |
1167 |
{ |
{ |
1168 |
FILE *fp; |
free_if_alloc (ctx->host); |
1169 |
keyserver_cfgfile *cfg; |
free_if_alloc (ctx->pass); |
1170 |
char buf[256]; |
free_if_alloc (ctx->user); |
1171 |
int rc = 0; |
} |
|
|
|
|
*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 */ |
|
1172 |
|
|
1173 |
|
|
1174 |
int |
/* Spawn a process given by @cmdl. */ |
1175 |
kserver_write_config( const char * fname, keyserver_cfgfile * cfg ) |
static int |
1176 |
|
spawn_application (char *cmdl) |
1177 |
{ |
{ |
1178 |
|
STARTUPINFO si; |
1179 |
return 0; |
PROCESS_INFORMATION pi; |
1180 |
} |
int rc = 0; |
|
|
|
1181 |
|
|
1182 |
void |
memset (&si, 0, sizeof (si)); |
1183 |
kserver_cfgfile_release( keyserver_cfgfile *cfg ) |
si.cb = sizeof (si); |
1184 |
{ |
si.dwFlags = STARTF_USESHOWWINDOW; |
1185 |
keyserver_node *k, *k2; |
si.wShowWindow = SW_HIDE; |
1186 |
|
memset (&pi, 0, sizeof (pi)); |
1187 |
|
|
1188 |
proxy_release( &cfg->proxy ); |
if (!CreateProcess (NULL, cmdl, NULL, NULL, FALSE, 0, |
1189 |
for( k = cfg->list; cfg->nlist--; k = k2 ) { |
NULL, NULL, &si, &pi)) { |
1190 |
k2 = k->next; |
log_box ("Keyserver Plugin", MB_ERR, "Could not spawn helper process"); |
1191 |
free_if_alloc( k ); |
rc = -1; |
1192 |
} |
} |
|
free_if_alloc( cfg ); |
|
|
} /* kserver_cfgfile_release */ |
|
1193 |
|
|
1194 |
|
CloseHandle (pi.hThread); |
1195 |
/* Release mbmers in the proxy context @ctx. */ |
WaitForSingleObject (pi.hProcess, INFINITE); |
1196 |
void |
CloseHandle (pi.hProcess); |
1197 |
proxy_release( keyserver_proxy_ctx *ctx ) |
return rc; |
|
{ |
|
|
free_if_alloc (ctx->host); |
|
|
free_if_alloc (ctx->pass); |
|
|
free_if_alloc (ctx->user); |
|
1198 |
} |
} |
1199 |
|
|
1200 |
|
|
1201 |
/* Receive an key via LDAP from host @host with the keyid @keyid. |
/* Receive an key via LDAP from host @host with the keyid @keyid. |
1202 |
@key contains the key on success. */ |
@key contains the key on success. */ |
1203 |
int |
int |
1204 |
ldap_recvkey (const char *host, const char *keyid, char *key, int maxkeylen) |
ldap_recvkey (const char *host, const char *keyid, char *key, int maxkeylen) |
1205 |
{ |
{ |
1206 |
|
FILE *inp; |
1207 |
|
DWORD n; |
1208 |
const char *s; |
const char *s; |
1209 |
char *ksprg = NULL, *p = NULL; |
char *ksprg = NULL, *p = NULL, *sep; |
1210 |
char temp[512], outf[512]; |
char temp[512], outf[512]; |
1211 |
FILE * inp; |
int start_key = 0, failed = 0; |
1212 |
int rc, start_key = 0; |
int rc = 0; |
|
STARTUPINFO si; |
|
|
PROCESS_INFORMATION pi; |
|
1213 |
|
|
1214 |
p = get_gnupg_path (); |
p = get_gnupg_prog (); |
1215 |
ksprg = new char[strlen (p) + 1 + 128]; |
n = strlen (p) + 1 + 128; |
1216 |
|
ksprg = new char[n+1]; |
1217 |
if (!ksprg) |
if (!ksprg) |
1218 |
BUG (0); |
BUG (0); |
1219 |
strcpy (ksprg, p); |
sep = strrchr (p, '\\'); |
1220 |
strcat (ksprg, "\\"); |
if (sep != NULL) |
1221 |
strcat (ksprg, "gpgkeys_ldap.exe"); |
p[(sep-p)] = 0; |
1222 |
|
|
1223 |
|
_snprintf (ksprg, n, "%s\\gpgkeys_ldap.exe", p); |
1224 |
free_if_alloc (p); |
free_if_alloc (p); |
1225 |
if (file_exist_check (ksprg)) { |
if (file_exist_check (ksprg)) { |
1226 |
log_box ( "LDAP Keyserver Plugin", MB_ERR, "Could not find LDAP keyserver module!"); |
log_box ("LDAP Keyserver Plugin", MB_ERR, |
1227 |
rc = -1; |
"%s: could not find LDAP keyserver module!", ksprg); |
1228 |
|
rc = -1; |
1229 |
goto leave; |
goto leave; |
1230 |
} |
} |
1231 |
GetTempPath (sizeof (temp)-1, temp); |
GetTempPath (sizeof (temp)-1, temp); |
1232 |
strcpy (outf, temp); |
_snprintf (outf, sizeof (outf)-1, "%s%s.out", temp, keyid); |
|
strcat (outf, keyid); |
|
|
strcat (outf, ".out"); |
|
1233 |
strcat (temp, keyid); |
strcat (temp, keyid); |
1234 |
inp = fopen (temp, "w+b"); |
inp = fopen (temp, "w+b"); |
1235 |
if( !inp ) { |
if (!inp) { |
1236 |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", temp, |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", temp, |
1237 |
winpt_strerror (WPTERR_FILE_OPEN)); |
winpt_strerror (WPTERR_FILE_OPEN)); |
1238 |
rc = -1; |
rc = -1; |
1239 |
goto leave; |
goto leave; |
1240 |
} |
} |
1241 |
fprintf (inp, |
fprintf (inp, |
1242 |
"VERSION 0\n" |
"VERSION 1\n" |
1243 |
|
"PROGRAM 1.4.3-cvs\n" |
1244 |
|
"SCHEME ldap\n" |
1245 |
"HOST %s\n" |
"HOST %s\n" |
|
"OPTION verbose\n" |
|
1246 |
"COMMAND GET\n" |
"COMMAND GET\n" |
1247 |
"\n" |
"\n" |
1248 |
"%s\n", host? skip_type_prefix (host): "64.94.85.200", keyid); |
"%s\n", |
1249 |
|
host? skip_type_prefix (host): "64.94.85.200", keyid); |
1250 |
fclose (inp); |
fclose (inp); |
1251 |
|
|
|
memset (&si, 0, sizeof (si)); |
|
|
si.cb = sizeof (si); |
|
|
si.dwFlags = STARTF_USESHOWWINDOW; |
|
|
si.wShowWindow = SW_HIDE; |
|
|
memset (&pi, 0, sizeof (pi)); |
|
1252 |
p = new char[strlen (ksprg) + strlen (temp) + strlen (outf) + 32]; |
p = new char[strlen (ksprg) + strlen (temp) + strlen (outf) + 32]; |
1253 |
|
if (!p) |
1254 |
|
BUG (NULL); |
1255 |
sprintf (p, "%s -o %s %s", ksprg, outf, temp); |
sprintf (p, "%s -o %s %s", ksprg, outf, temp); |
1256 |
rc = CreateProcess (NULL, p, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi); |
if (spawn_application (p)) { |
|
if (!rc) { |
|
|
log_box ("LDAP Keyserver Plugin", MB_ERR, "Could not spawn process"); |
|
1257 |
rc = -1; |
rc = -1; |
1258 |
goto leave; |
goto leave; |
1259 |
} |
} |
1260 |
CloseHandle (pi.hThread); |
DeleteFile (temp); |
|
WaitForSingleObject (pi.hProcess, INFINITE); |
|
|
CloseHandle (pi.hProcess); |
|
1261 |
|
|
1262 |
inp = fopen (outf, "rb"); |
inp = fopen (outf, "rb"); |
1263 |
if (!inp) { |
if (!inp) { |
1267 |
goto leave; |
goto leave; |
1268 |
} |
} |
1269 |
memset (key, 0, maxkeylen); |
memset (key, 0, maxkeylen); |
1270 |
while( !feof( inp ) ) { |
while (!feof (inp)) { |
1271 |
s = fgets( temp, sizeof (temp)-1, inp ); |
s = fgets (temp, sizeof (temp)-1, inp); |
1272 |
if( !s ) |
if (!s) |
1273 |
|
break; |
1274 |
|
if (strstr (s, "KEY") && strstr (s, "FAILED")) { |
1275 |
|
failed = 1; |
1276 |
break; |
break; |
1277 |
if( !start_key && strstr( s, "KEY" ) && strstr( s, "BEGIN" ) ) { |
} |
1278 |
|
if (!start_key && strstr (s, "KEY") && strstr (s, "BEGIN")) { |
1279 |
start_key = 1; |
start_key = 1; |
1280 |
continue; |
continue; |
1281 |
} |
} |
1282 |
if( !start_key ) |
if (!start_key) |
1283 |
continue; |
continue; |
1284 |
strcat( key, temp ); |
strcat (key, temp); |
1285 |
maxkeylen -= strlen( temp ); |
maxkeylen -= strlen (temp); |
1286 |
if( maxkeylen < 0 || (strstr( s, "KEY" ) && strstr( s, "END" )) ) |
if (maxkeylen < 0 || (strstr (s, "KEY") && strstr (s, "END"))) |
1287 |
break; |
break; |
1288 |
} |
} |
1289 |
fclose( inp ); |
fclose (inp); |
1290 |
|
|
1291 |
leave: |
leave: |
1292 |
if( !strlen( key ) ) |
if (failed || !strlen (key)) |
1293 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
1294 |
free_if_alloc( p ); |
DeleteFile (outf); |
1295 |
free_if_alloc( ksprg ); |
free_if_alloc (p); |
1296 |
|
free_if_alloc (ksprg); |
1297 |
return rc; |
return rc; |
1298 |
} |
} |
1299 |
|
|