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 |
|
|
511 |
if (hostname != NULL) { |
if (hostname != NULL) { |
512 |
free_if_alloc (default_keyserver); |
free_if_alloc (default_keyserver); |
513 |
default_keyserver = m_strdup (hostname); |
default_keyserver = m_strdup (hostname); |
|
if (!default_keyserver) |
|
|
BUG (0); |
|
514 |
default_keyserver_port = port; |
default_keyserver_port = port; |
515 |
} |
} |
516 |
if (!port) |
if (!port) |
604 |
/* check if the host has a http:// prefix and skip it */ |
/* check if the host has a http:// prefix and skip it */ |
605 |
if (proxy.host && !strncmp (proxy.host, "http://", 7)) { |
if (proxy.host && !strncmp (proxy.host, "http://", 7)) { |
606 |
char *host = m_strdup (proxy.host+7); |
char *host = m_strdup (proxy.host+7); |
|
if (!host) |
|
|
BUG (0); |
|
607 |
free_if_alloc (proxy.host); |
free_if_alloc (proxy.host); |
608 |
proxy.host = host; |
proxy.host = host; |
609 |
} |
} |
749 |
URL_encode (const char *url, size_t ulen, size_t *ret_nlen) |
URL_encode (const char *url, size_t ulen, size_t *ret_nlen) |
750 |
{ |
{ |
751 |
gpgme_data_t hd; |
gpgme_data_t hd; |
752 |
char numbuf[5], *pp, *p; |
char numbuf[5], *p; |
753 |
size_t i, n; |
size_t i; |
754 |
|
int nlen; |
755 |
|
|
756 |
gpgme_data_new (&hd); |
if (gpgme_data_new (&hd)) |
757 |
|
BUG (0); |
758 |
for (i=0; i < ulen; i++) { |
for (i=0; i < ulen; i++) { |
759 |
if (isalnum (url[i]) || url[i] == '-') |
if (isalnum (url[i]) || url[i] == '-') |
760 |
gpg_data_putc (hd, url[i]); |
gpg_data_putc (hd, url[i]); |
765 |
gpgme_data_write (hd, numbuf, strlen (numbuf)); |
gpgme_data_write (hd, numbuf, strlen (numbuf)); |
766 |
} |
} |
767 |
} |
} |
|
gpg_data_putc (hd, '\0'); |
|
768 |
|
|
769 |
/* 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); |
|
770 |
if (ret_nlen) |
if (ret_nlen) |
771 |
*ret_nlen = n; |
*ret_nlen = nlen; |
772 |
return p; |
return p; |
773 |
} |
} |
774 |
|
|
835 |
} |
} |
836 |
|
|
837 |
|
|
838 |
|
/* Interface for receiving a public key. */ |
839 |
int |
int |
840 |
kserver_recvkey_ext (const char *hostname, WORD port, const char *keyid, |
kserver_recvkey (const char *hostname, WORD port, const char *keyid, |
841 |
char **r_key, int *r_keylen) |
char **r_key, int *r_keylen) |
842 |
{ |
{ |
843 |
char *request = NULL; |
char *request = NULL; |
844 |
int conn_fd; |
int conn_fd; |
845 |
int rc, n; |
int rc; |
846 |
|
|
847 |
if (!port) |
if (!port) |
848 |
port = HKP_PORT; |
port = HKP_PORT; |
879 |
goto leave; |
goto leave; |
880 |
} |
} |
881 |
|
|
882 |
rc = sock_read_ext (conn_fd, r_key, &n); |
rc = sock_read_ext (conn_fd, r_key, r_keylen); |
883 |
if (rc == SOCKET_ERROR) { |
if (rc == SOCKET_ERROR) { |
884 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
885 |
goto leave; |
goto leave; |
886 |
} |
} |
887 |
|
|
|
if (r_keylen) |
|
|
*r_keylen = n; |
|
888 |
log_debug ("%s\r\n", *r_key); |
log_debug ("%s\r\n", *r_key); |
889 |
rc = check_hkp_response (*r_key, 1); |
rc = check_hkp_response (*r_key, 1); |
890 |
if (rc) |
if (rc) |
898 |
return rc; |
return rc; |
899 |
} |
} |
900 |
|
|
|
/* 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; |
|
|
} |
|
|
|
|
901 |
|
|
902 |
/* Interface to send a public key. */ |
/* Interface to send a public key. */ |
903 |
int |
int |
1180 |
} |
} |
1181 |
|
|
1182 |
|
|
1183 |
/* Receive an key via LDAP from host @host with the keyid @keyid. |
static FILE* |
1184 |
@key contains the key on success. */ |
do_spawn_ldap_helper (const char *host, const char *keyid) |
1185 |
int |
{ |
1186 |
ldap_recvkey (const char *host, const char *keyid, char *key, int maxkeylen) |
FILE *fp = NULL; |
1187 |
{ |
char *p, *sep; |
1188 |
FILE *fp; |
char *ksprg; |
1189 |
const char *s; |
char outf[256], inf[256]; |
1190 |
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; |
|
1191 |
|
|
1192 |
p = get_gnupg_prog (); |
p = get_gnupg_prog (); |
1193 |
n = strlen (p) + 1 + 128; |
n = strlen (p) + 1 + 128; |
1203 |
if (file_exist_check (ksprg)) { |
if (file_exist_check (ksprg)) { |
1204 |
log_box ("LDAP Keyserver Plugin", MB_ERR, |
log_box ("LDAP Keyserver Plugin", MB_ERR, |
1205 |
"%s: could not find LDAP keyserver module!", ksprg); |
"%s: could not find LDAP keyserver module!", ksprg); |
1206 |
rc = -1; |
fp = NULL; |
1207 |
goto leave; |
goto leave; |
1208 |
} |
} |
1209 |
get_temp_name (outf, sizeof (outf)-1, keyid); |
get_temp_name (outf, sizeof (outf)-1, keyid); |
1212 |
if (!fp) { |
if (!fp) { |
1213 |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", inf, |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", inf, |
1214 |
winpt_strerror (WPTERR_FILE_OPEN)); |
winpt_strerror (WPTERR_FILE_OPEN)); |
|
rc = -1; |
|
1215 |
goto leave; |
goto leave; |
1216 |
} |
} |
1217 |
fprintf (fp, |
fprintf (fp, |
1230 |
BUG (NULL); |
BUG (NULL); |
1231 |
sprintf (p, "%s -o %s %s", ksprg, outf, inf); |
sprintf (p, "%s -o %s %s", ksprg, outf, inf); |
1232 |
if (spawn_application (p)) { |
if (spawn_application (p)) { |
1233 |
rc = -1; |
fp = NULL; |
1234 |
goto leave; |
goto leave; |
1235 |
} |
} |
|
|
|
1236 |
fp = fopen (outf, "rb"); |
fp = fopen (outf, "rb"); |
1237 |
if (!fp) { |
if (!fp) |
1238 |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf, |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf, |
1239 |
winpt_strerror (WPTERR_FILE_OPEN)); |
winpt_strerror (WPTERR_FILE_OPEN)); |
1240 |
rc = -1; |
|
1241 |
goto leave; |
leave: |
1242 |
} |
DeleteFile (inf); |
1243 |
memset (key, 0, maxkeylen); |
DeleteFile (outf); |
1244 |
|
free_if_alloc (p); |
1245 |
|
free_if_alloc (ksprg); |
1246 |
|
return fp; |
1247 |
|
} |
1248 |
|
|
1249 |
|
/* Receive an key via LDAP from host @host with the keyid @keyid. |
1250 |
|
@key contains the key on success. */ |
1251 |
|
int |
1252 |
|
ldap_recvkey (const char *host, const char *keyid, |
1253 |
|
char **r_key, int *r_keylen) |
1254 |
|
{ |
1255 |
|
gpgme_data_t raw; |
1256 |
|
FILE *fp; |
1257 |
|
const char *s; |
1258 |
|
char buf[512]; |
1259 |
|
int start_key = 0, failed = 0; |
1260 |
|
int rc = 0; |
1261 |
|
|
1262 |
|
fp = do_spawn_ldap_helper (host, keyid); |
1263 |
|
if (!fp) |
1264 |
|
return WPTERR_GENERAL; |
1265 |
|
|
1266 |
|
if (gpgme_data_new (&raw)) |
1267 |
|
BUG (0); |
1268 |
while (!feof (fp)) { |
while (!feof (fp)) { |
1269 |
s = fgets (buf, sizeof (buf)-1, fp); |
s = fgets (buf, sizeof (buf)-1, fp); |
1270 |
if (!s) |
if (!s) |
1276 |
if (!start_key && strstr (s, "KEY") && strstr (s, "BEGIN")) { |
if (!start_key && strstr (s, "KEY") && strstr (s, "BEGIN")) { |
1277 |
start_key = 1; |
start_key = 1; |
1278 |
continue; |
continue; |
1279 |
} |
} |
1280 |
if (!start_key) |
if (!start_key) |
1281 |
continue; |
continue; |
1282 |
strcat (key, buf); |
gpgme_data_write (raw, buf, strlen (buf)); |
1283 |
maxkeylen -= strlen (buf); |
if (strstr (s, "KEY") && strstr (s, "END")) |
|
if (maxkeylen < 0 || (strstr (s, "KEY") && strstr (s, "END"))) |
|
1284 |
break; |
break; |
1285 |
} |
} |
1286 |
fclose (fp); |
fclose (fp); |
1287 |
|
|
1288 |
leave: |
if (failed) |
|
if (failed || !strlen (key)) |
|
1289 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
1290 |
DeleteFile (inf); |
*r_key = data_release_and_get_mem (raw, r_keylen); |
|
DeleteFile (outf); |
|
|
free_if_alloc (p); |
|
|
free_if_alloc (ksprg); |
|
1291 |
return rc; |
return rc; |
1292 |
} |
} |
1293 |
|
|
1295 |
/* Receive an key via FINGER from host @host with the user @user. |
/* Receive an key via FINGER from host @host with the user @user. |
1296 |
On success @key contains the key. */ |
On success @key contains the key. */ |
1297 |
int |
int |
1298 |
finger_recvkey (const char *host, const char *user, char *key, int maxkeylen) |
finger_recvkey (const char *host, const char *user, |
1299 |
|
char **r_key, int *r_keylen) |
1300 |
{ |
{ |
1301 |
char buf[128]; |
gpgme_data_t raw; |
1302 |
|
char buf[256]; |
1303 |
int fd, nread; |
int fd, nread; |
1304 |
int start_key = 0; |
int start_key = 0; |
1305 |
int rc=0; |
int rc=0; |
1311 |
sock_write (fd, user, strlen (user)); |
sock_write (fd, user, strlen (user)); |
1312 |
sock_write (fd, "\r\n", 2); |
sock_write (fd, "\r\n", 2); |
1313 |
|
|
1314 |
memset (key, 0, maxkeylen); |
if (gpgme_data_new (&raw)) |
1315 |
while (maxkeylen > 0) { |
BUG (0); |
1316 |
|
|
1317 |
|
for (;;) { |
1318 |
if (sock_getline (fd, buf, sizeof (buf), &nread)) |
if (sock_getline (fd, buf, sizeof (buf), &nread)) |
1319 |
break; |
break; |
1320 |
strcat (buf, "\n"); |
strcat (buf, "\n"); |
1321 |
if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) { |
if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) { |
1322 |
strcat (key, buf); |
gpgme_data_write (raw, buf, nread); |
1323 |
start_key = 1; |
start_key = 1; |
|
maxkeylen -= nread; |
|
1324 |
} |
} |
1325 |
else if (strstr (buf, "END PGP PUBLIC KEY BLOCK" ) && start_key) { |
else if (strstr (buf, "END PGP PUBLIC KEY BLOCK" ) && start_key) { |
1326 |
strcat (key, buf); |
gpgme_data_write (raw, buf, nread); |
1327 |
start_key--; |
start_key--; |
|
maxkeylen -= nread; |
|
1328 |
break; |
break; |
1329 |
} |
} |
1330 |
else if (start_key) { |
else if (start_key) |
1331 |
strcat (key, buf); |
gpgme_data_write (raw, buf, nread); |
|
maxkeylen -= nread; |
|
|
} |
|
1332 |
} |
} |
1333 |
|
|
1334 |
closesocket (fd); |
closesocket (fd); |
1335 |
if (start_key != 0) |
if (start_key != 0) |
1336 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
1337 |
|
*r_key = data_release_and_get_mem (raw, r_keylen); |
1338 |
return rc; |
return rc; |
1339 |
} |
} |
1340 |
|
|