1 |
/* wptKeyserver.cpp - W32 Keyserver Access |
/* wptKeyserver.cpp - W32 Keyserver Access |
2 |
* Copyright (C) 2000-2005 Timo Schulz |
* Copyright (C) 2000-2006 Timo Schulz |
3 |
* Copyright (C) 2001 Marco Cunha |
* Copyright (C) 2001 Marco Cunha |
4 |
* |
* |
5 |
* This file is part of WinPT. |
* This file is part of WinPT. |
37 |
#include "wptGPG.h" |
#include "wptGPG.h" |
38 |
#include "wptRegistry.h" |
#include "wptRegistry.h" |
39 |
|
|
40 |
/* just map net_errno to a winsock error. */ |
/* Map net_errno to a winsock error. */ |
41 |
#define net_errno ((int)WSAGetLastError ()) |
#define net_errno ((int)WSAGetLastError ()) |
42 |
|
|
43 |
|
|
240 |
return 0; |
return 0; |
241 |
} |
} |
242 |
|
|
243 |
|
|
244 |
|
/* Helper to create a string from the gpgme data and |
245 |
|
then release the gpgme data object. */ |
246 |
|
char* |
247 |
|
data_release_and_get_mem (gpgme_data_t in, int *r_bufferlen) |
248 |
|
{ |
249 |
|
size_t n; |
250 |
|
char *buffer, *p; |
251 |
|
|
252 |
|
gpg_data_putc (in, '\0'); |
253 |
|
p = gpgme_data_release_and_get_mem (in, &n); |
254 |
|
buffer = m_strdup (p); |
255 |
|
if (r_bufferlen) |
256 |
|
*r_bufferlen = (int)n; |
257 |
|
gpgme_free (p); |
258 |
|
return buffer; |
259 |
|
} |
260 |
|
|
261 |
|
|
262 |
/* Read much data as possible from the socket @fd and |
/* Read much data as possible from the socket @fd and |
263 |
return the data in @buffer. Caller must free data. |
return the data in @buffer. Caller must free data. |
264 |
Return value: 0 on success. */ |
Return value: 0 on success. */ |
266 |
sock_read_ext (int fd, char **buffer, int *r_bufferlen) |
sock_read_ext (int fd, char **buffer, int *r_bufferlen) |
267 |
{ |
{ |
268 |
gpgme_data_t dh; |
gpgme_data_t dh; |
269 |
char buf[1024], *p; |
char buf[1024]; |
270 |
size_t n=0; |
size_t n=0; |
271 |
int nread, rc; |
int nread, rc; |
272 |
|
|
273 |
gpgme_data_new (&dh); |
if (gpgme_data_new (&dh)) |
274 |
|
BUG (0); |
275 |
while (n < 10) { |
while (n < 10) { |
276 |
rc = sock_select (fd, 1); |
rc = sock_select (fd, 1); |
277 |
if (rc == SOCKET_ERROR) { |
if (rc == SOCKET_ERROR) { |
289 |
gpgme_data_write (dh, buf, nread); |
gpgme_data_write (dh, buf, nread); |
290 |
} |
} |
291 |
} |
} |
292 |
gpg_data_putc (dh, '\0'); |
|
293 |
p = gpgme_data_release_and_get_mem (dh, &n); |
*buffer = data_release_and_get_mem (dh, r_bufferlen); |
|
*buffer = m_strdup (p); |
|
|
if (r_bufferlen) |
|
|
*r_bufferlen = n; |
|
|
gpgme_free (p); |
|
294 |
return 0; |
return 0; |
295 |
} |
} |
296 |
|
|
353 |
/* Return a string representation of a winsock error. */ |
/* Return a string representation of a winsock error. */ |
354 |
const char* |
const char* |
355 |
wsock_strerror (void) |
wsock_strerror (void) |
356 |
{ |
{ |
|
static char buf[384]; |
|
357 |
int ec = WSAGetLastError (); |
int ec = WSAGetLastError (); |
358 |
|
|
359 |
|
if (!ec) |
360 |
|
return ""; |
361 |
switch (ec) { |
switch (ec) { |
362 |
case WSAENETDOWN: |
case WSAENETDOWN: |
363 |
return _("The network subsystem has failed"); |
return _("Network unreachable"); |
364 |
|
|
365 |
|
case WSAEHOSTUNREACH: |
366 |
|
return _("Host unreachable"); |
367 |
|
|
368 |
case WSAHOST_NOT_FOUND: |
case WSAHOST_NOT_FOUND: |
369 |
return _("Authoritative Answer Host not found"); |
return _("Could not resolve host name"); |
370 |
|
|
371 |
|
case WSAECONNREFUSED: |
372 |
|
return _("Connection refused"); |
373 |
|
|
374 |
case WSAETIMEDOUT: |
case WSAETIMEDOUT: |
375 |
return _("The connection has been dropped because of a network failure"); |
case WSAECONNABORTED: |
376 |
|
return _("Connection timeout"); |
377 |
|
|
378 |
|
case WSAENETRESET: |
379 |
|
case WSAECONNRESET: |
380 |
|
return _("Connection resetted by peer"); |
381 |
|
|
382 |
|
case WSAESHUTDOWN: |
383 |
|
return _("Socket has been shutdown"); |
384 |
|
|
385 |
default: |
default: |
386 |
_snprintf (buf, sizeof (buf) -1, _("Unknown Winsock error ec=%d"),ec); |
break; |
|
return buf; |
|
387 |
} |
} |
388 |
return NULL; |
|
389 |
|
return ""; |
390 |
} |
} |
391 |
|
|
392 |
|
|
529 |
if (hostname != NULL) { |
if (hostname != NULL) { |
530 |
free_if_alloc (default_keyserver); |
free_if_alloc (default_keyserver); |
531 |
default_keyserver = m_strdup (hostname); |
default_keyserver = m_strdup (hostname); |
|
if (!default_keyserver) |
|
|
BUG (0); |
|
532 |
default_keyserver_port = port; |
default_keyserver_port = port; |
533 |
} |
} |
534 |
if (!port) |
if (!port) |
622 |
/* check if the host has a http:// prefix and skip it */ |
/* check if the host has a http:// prefix and skip it */ |
623 |
if (proxy.host && !strncmp (proxy.host, "http://", 7)) { |
if (proxy.host && !strncmp (proxy.host, "http://", 7)) { |
624 |
char *host = m_strdup (proxy.host+7); |
char *host = m_strdup (proxy.host+7); |
|
if (!host) |
|
|
BUG (0); |
|
625 |
free_if_alloc (proxy.host); |
free_if_alloc (proxy.host); |
626 |
proxy.host = host; |
proxy.host = host; |
627 |
} |
} |
767 |
URL_encode (const char *url, size_t ulen, size_t *ret_nlen) |
URL_encode (const char *url, size_t ulen, size_t *ret_nlen) |
768 |
{ |
{ |
769 |
gpgme_data_t hd; |
gpgme_data_t hd; |
770 |
char numbuf[5], *pp, *p; |
char numbuf[5], *p; |
771 |
size_t i, n; |
size_t i; |
772 |
|
int nlen; |
773 |
|
|
774 |
gpgme_data_new (&hd); |
if (gpgme_data_new (&hd)) |
775 |
|
BUG (0); |
776 |
for (i=0; i < ulen; i++) { |
for (i=0; i < ulen; i++) { |
777 |
if (isalnum (url[i]) || url[i] == '-') |
if (isalnum (url[i]) || url[i] == '-') |
778 |
gpg_data_putc (hd, url[i]); |
gpg_data_putc (hd, url[i]); |
783 |
gpgme_data_write (hd, numbuf, strlen (numbuf)); |
gpgme_data_write (hd, numbuf, strlen (numbuf)); |
784 |
} |
} |
785 |
} |
} |
|
gpg_data_putc (hd, '\0'); |
|
786 |
|
|
787 |
/* Copy memory to avoid that we need to use gpgme_free later. */ |
p = data_release_and_get_mem (hd, &nlen); |
|
pp = gpgme_data_release_and_get_mem (hd, &n); |
|
|
p = m_strdup (pp); |
|
|
gpgme_free (pp); |
|
788 |
if (ret_nlen) |
if (ret_nlen) |
789 |
*ret_nlen = n; |
*ret_nlen = nlen; |
790 |
return p; |
return p; |
791 |
} |
} |
792 |
|
|
853 |
} |
} |
854 |
|
|
855 |
|
|
856 |
|
/* Interface for receiving a public key. */ |
857 |
int |
int |
858 |
kserver_recvkey_ext (const char *hostname, WORD port, const char *keyid, |
kserver_recvkey (const char *hostname, WORD port, const char *keyid, |
859 |
char **r_key, int *r_keylen) |
char **r_key, int *r_keylen) |
860 |
{ |
{ |
861 |
char *request = NULL; |
char *request = NULL; |
862 |
int conn_fd; |
int conn_fd; |
863 |
int rc, n; |
int rc; |
864 |
|
|
865 |
if (!port) |
if (!port) |
866 |
port = HKP_PORT; |
port = HKP_PORT; |
897 |
goto leave; |
goto leave; |
898 |
} |
} |
899 |
|
|
900 |
rc = sock_read_ext (conn_fd, r_key, &n); |
rc = sock_read_ext (conn_fd, r_key, r_keylen); |
901 |
if (rc == SOCKET_ERROR) { |
if (rc == SOCKET_ERROR) { |
902 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
903 |
goto leave; |
goto leave; |
904 |
} |
} |
905 |
|
|
|
if (r_keylen) |
|
|
*r_keylen = n; |
|
906 |
log_debug ("%s\r\n", *r_key); |
log_debug ("%s\r\n", *r_key); |
907 |
rc = check_hkp_response (*r_key, 1); |
rc = check_hkp_response (*r_key, 1); |
908 |
if (rc) |
if (rc) |
916 |
return rc; |
return rc; |
917 |
} |
} |
918 |
|
|
|
/* Interface for receiving a public key. */ |
|
|
int |
|
|
kserver_recvkey (const char *hostname, WORD port, const char *keyid, |
|
|
char *key, int maxkeylen) |
|
|
{ |
|
|
char *tmpkey; |
|
|
int rc, nread=0; |
|
|
|
|
|
/* XXX: just a wrapper around the new method, replace it |
|
|
soon as possible. */ |
|
|
rc = kserver_recvkey_ext (hostname, port, keyid, &tmpkey, &nread); |
|
|
if (rc) |
|
|
return rc; |
|
|
|
|
|
if (nread > maxkeylen) { |
|
|
free_if_alloc (tmpkey); |
|
|
return WPTERR_GENERAL; |
|
|
} |
|
|
strcpy (key, tmpkey); |
|
|
free_if_alloc (tmpkey); |
|
|
return 0; |
|
|
} |
|
|
|
|
919 |
|
|
920 |
/* Interface to send a public key. */ |
/* Interface to send a public key. */ |
921 |
int |
int |
1198 |
} |
} |
1199 |
|
|
1200 |
|
|
1201 |
/* Receive an key via LDAP from host @host with the keyid @keyid. |
static FILE* |
1202 |
@key contains the key on success. */ |
do_spawn_ldap_helper (const char *host, const char *keyid) |
1203 |
int |
{ |
1204 |
ldap_recvkey (const char *host, const char *keyid, char *key, int maxkeylen) |
FILE *fp = NULL; |
1205 |
{ |
char *p, *sep; |
1206 |
FILE *fp; |
char *ksprg; |
1207 |
const char *s; |
char outf[256], inf[256]; |
1208 |
char *ksprg = NULL, *p = NULL, *sep; |
size_t n; |
|
char inf[256], outf[256], buf[512]; |
|
|
DWORD n; |
|
|
int start_key = 0, failed = 0; |
|
|
int rc = 0; |
|
1209 |
|
|
1210 |
p = get_gnupg_prog (); |
p = get_gnupg_prog (); |
1211 |
n = strlen (p) + 1 + 128; |
n = strlen (p) + 1 + 128; |
1221 |
if (file_exist_check (ksprg)) { |
if (file_exist_check (ksprg)) { |
1222 |
log_box ("LDAP Keyserver Plugin", MB_ERR, |
log_box ("LDAP Keyserver Plugin", MB_ERR, |
1223 |
"%s: could not find LDAP keyserver module!", ksprg); |
"%s: could not find LDAP keyserver module!", ksprg); |
1224 |
rc = -1; |
fp = NULL; |
1225 |
goto leave; |
goto leave; |
1226 |
} |
} |
1227 |
get_temp_name (outf, sizeof (outf)-1, keyid); |
get_temp_name (outf, sizeof (outf)-1, keyid); |
1230 |
if (!fp) { |
if (!fp) { |
1231 |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", inf, |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", inf, |
1232 |
winpt_strerror (WPTERR_FILE_OPEN)); |
winpt_strerror (WPTERR_FILE_OPEN)); |
|
rc = -1; |
|
1233 |
goto leave; |
goto leave; |
1234 |
} |
} |
1235 |
fprintf (fp, |
fprintf (fp, |
1248 |
BUG (NULL); |
BUG (NULL); |
1249 |
sprintf (p, "%s -o %s %s", ksprg, outf, inf); |
sprintf (p, "%s -o %s %s", ksprg, outf, inf); |
1250 |
if (spawn_application (p)) { |
if (spawn_application (p)) { |
1251 |
rc = -1; |
fp = NULL; |
1252 |
goto leave; |
goto leave; |
1253 |
} |
} |
|
|
|
1254 |
fp = fopen (outf, "rb"); |
fp = fopen (outf, "rb"); |
1255 |
if (!fp) { |
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; |
|
1259 |
goto leave; |
leave: |
1260 |
} |
DeleteFile (inf); |
1261 |
memset (key, 0, maxkeylen); |
DeleteFile (outf); |
1262 |
|
free_if_alloc (p); |
1263 |
|
free_if_alloc (ksprg); |
1264 |
|
return fp; |
1265 |
|
} |
1266 |
|
|
1267 |
|
/* Receive an key via LDAP from host @host with the keyid @keyid. |
1268 |
|
@key contains the key on success. */ |
1269 |
|
int |
1270 |
|
ldap_recvkey (const char *host, const char *keyid, |
1271 |
|
char **r_key, int *r_keylen) |
1272 |
|
{ |
1273 |
|
gpgme_data_t raw; |
1274 |
|
FILE *fp; |
1275 |
|
const char *s; |
1276 |
|
char buf[512]; |
1277 |
|
int start_key = 0, failed = 0; |
1278 |
|
int rc = 0; |
1279 |
|
|
1280 |
|
fp = do_spawn_ldap_helper (host, keyid); |
1281 |
|
if (!fp) |
1282 |
|
return WPTERR_GENERAL; |
1283 |
|
|
1284 |
|
if (gpgme_data_new (&raw)) |
1285 |
|
BUG (0); |
1286 |
while (!feof (fp)) { |
while (!feof (fp)) { |
1287 |
s = fgets (buf, sizeof (buf)-1, fp); |
s = fgets (buf, sizeof (buf)-1, fp); |
1288 |
if (!s) |
if (!s) |
1294 |
if (!start_key && strstr (s, "KEY") && strstr (s, "BEGIN")) { |
if (!start_key && strstr (s, "KEY") && strstr (s, "BEGIN")) { |
1295 |
start_key = 1; |
start_key = 1; |
1296 |
continue; |
continue; |
1297 |
} |
} |
1298 |
if (!start_key) |
if (!start_key) |
1299 |
continue; |
continue; |
1300 |
strcat (key, buf); |
gpgme_data_write (raw, buf, strlen (buf)); |
1301 |
maxkeylen -= strlen (buf); |
if (strstr (s, "KEY") && strstr (s, "END")) |
|
if (maxkeylen < 0 || (strstr (s, "KEY") && strstr (s, "END"))) |
|
1302 |
break; |
break; |
1303 |
} |
} |
1304 |
fclose (fp); |
fclose (fp); |
1305 |
|
|
1306 |
leave: |
if (failed) |
|
if (failed || !strlen (key)) |
|
1307 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
1308 |
DeleteFile (inf); |
*r_key = data_release_and_get_mem (raw, r_keylen); |
|
DeleteFile (outf); |
|
|
free_if_alloc (p); |
|
|
free_if_alloc (ksprg); |
|
1309 |
return rc; |
return rc; |
1310 |
} |
} |
1311 |
|
|
1313 |
/* Receive an key via FINGER from host @host with the user @user. |
/* Receive an key via FINGER from host @host with the user @user. |
1314 |
On success @key contains the key. */ |
On success @key contains the key. */ |
1315 |
int |
int |
1316 |
finger_recvkey (const char *host, const char *user, char *key, int maxkeylen) |
finger_recvkey (const char *host, const char *user, |
1317 |
|
char **r_key, int *r_keylen) |
1318 |
{ |
{ |
1319 |
char buf[128]; |
gpgme_data_t raw; |
1320 |
|
char buf[256]; |
1321 |
int fd, nread; |
int fd, nread; |
1322 |
int start_key = 0; |
int start_key = 0; |
1323 |
int rc=0; |
int rc=0; |
1329 |
sock_write (fd, user, strlen (user)); |
sock_write (fd, user, strlen (user)); |
1330 |
sock_write (fd, "\r\n", 2); |
sock_write (fd, "\r\n", 2); |
1331 |
|
|
1332 |
memset (key, 0, maxkeylen); |
if (gpgme_data_new (&raw)) |
1333 |
while (maxkeylen > 0) { |
BUG (0); |
1334 |
|
|
1335 |
|
for (;;) { |
1336 |
if (sock_getline (fd, buf, sizeof (buf), &nread)) |
if (sock_getline (fd, buf, sizeof (buf), &nread)) |
1337 |
break; |
break; |
1338 |
strcat (buf, "\n"); |
strcat (buf, "\n"); |
1339 |
if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) { |
if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) { |
1340 |
strcat (key, buf); |
gpgme_data_write (raw, buf, nread); |
1341 |
start_key = 1; |
start_key = 1; |
|
maxkeylen -= nread; |
|
1342 |
} |
} |
1343 |
else if (strstr (buf, "END PGP PUBLIC KEY BLOCK" ) && start_key) { |
else if (strstr (buf, "END PGP PUBLIC KEY BLOCK" ) && start_key) { |
1344 |
strcat (key, buf); |
gpgme_data_write (raw, buf, nread); |
1345 |
start_key--; |
start_key--; |
|
maxkeylen -= nread; |
|
1346 |
break; |
break; |
1347 |
} |
} |
1348 |
else if (start_key) { |
else if (start_key) |
1349 |
strcat (key, buf); |
gpgme_data_write (raw, buf, nread); |
|
maxkeylen -= nread; |
|
|
} |
|
1350 |
} |
} |
1351 |
|
|
1352 |
closesocket (fd); |
closesocket (fd); |
1353 |
if (start_key != 0) |
if (start_key != 0) |
1354 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
1355 |
|
*r_key = data_release_and_get_mem (raw, r_keylen); |
1356 |
return rc; |
return rc; |
1357 |
} |
} |
1358 |
|
|