1 |
/* wptKeyserver.cpp - W32 Keyserver Access |
/* wptKeyserver.cpp - W32 Keyserver Access |
2 |
* Copyright (C) 2000-2006 Timo Schulz |
* Copyright (C) 2000-2007 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. |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 |
* General Public License for more details. |
* General Public License for more details. |
|
* |
|
|
* You should have received a copy of the GNU General Public License |
|
|
* along with WinPT; if not, write to the Free Software Foundation, |
|
|
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
16 |
*/ |
*/ |
|
|
|
17 |
#ifdef HAVE_CONFIG_H |
#ifdef HAVE_CONFIG_H |
18 |
#include <config.h> |
#include <config.h> |
19 |
#endif |
#endif |
954 |
kserver_recvkey (const char *hostname, WORD port, const char *keyid, |
kserver_recvkey (const char *hostname, WORD port, const char *keyid, |
955 |
char **r_key, size_t *r_keylen) |
char **r_key, size_t *r_keylen) |
956 |
{ |
{ |
957 |
const char *fmt; |
StringBuffer req; |
958 |
char *request = NULL; |
const char *reqbuf; |
959 |
int conn_fd; |
int conn_fd; |
960 |
int rc, reqlen; |
int rc; |
961 |
|
|
962 |
if (!port) |
if (!port) |
963 |
port = HKP_PORT; |
port = HKP_PORT; |
966 |
if (rc) |
if (rc) |
967 |
goto leave; |
goto leave; |
968 |
|
|
|
reqlen = strlen (hostname)+32+strlen (keyid)+1; |
|
969 |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
970 |
fmt = "GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n" |
req = req + "GET http://" + skip_type_prefix (hostname) + |
971 |
"Proxy-Authorization: Basic %s\r\n\r\n"; |
":" + port + |
972 |
reqlen =+ strlen (fmt) + strlen (proxy.base64_user)+1; |
"/pks/lookup?op=get&search=" + keyid + " HTTP/1.0\r\n" + |
973 |
request = safe_alloc (reqlen+1); |
"Proxy-Authorization: Basic " + proxy.base64_user + "\r\n\r\n"; |
|
_snprintf (request, reqlen, fmt, skip_type_prefix (hostname), port, |
|
|
keyid, proxy.base64_user); |
|
974 |
} |
} |
975 |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
976 |
fmt = "GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n"; |
req = req + "GET http://" + skip_type_prefix (hostname) + |
977 |
reqlen += strlen (fmt)+1; |
":" + port + "/pks/lookup?op=get&search=" + keyid + |
978 |
request = safe_alloc (reqlen+1); |
" HTTP/1.0\r\n\r\n"; |
|
_snprintf (request, reqlen, fmt, skip_type_prefix (hostname), |
|
|
port, keyid); |
|
979 |
} |
} |
980 |
else { |
else |
981 |
fmt = "GET /pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n"; |
req = req + "GET /pks/lookup?op=get&search=" + keyid + |
982 |
reqlen += strlen (fmt)+1; |
" HTTP/1.0\r\n\r\n"; |
|
request = safe_alloc (reqlen+1); |
|
|
_snprintf (request, reqlen, fmt, keyid); |
|
|
} |
|
|
|
|
|
log_debug ("request:\r\n%s\r\n", request); |
|
983 |
|
|
984 |
rc = sock_write (conn_fd, request, strlen (request)); |
log_debug ("req:\r\n%s\r\n", req.getBuffer ()); |
985 |
|
|
986 |
|
reqbuf = req.getBuffer (); |
987 |
|
rc = sock_write (conn_fd, reqbuf, strlen (reqbuf)); |
988 |
if (rc == SOCKET_ERROR) { |
if (rc == SOCKET_ERROR) { |
989 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
990 |
goto leave; |
goto leave; |
1005 |
|
|
1006 |
leave: |
leave: |
1007 |
closesocket (conn_fd); |
closesocket (conn_fd); |
|
free_if_alloc (request); |
|
1008 |
return rc; |
return rc; |
1009 |
} |
} |
1010 |
|
|
1111 |
kserver_search_begin (const char *hostname, WORD port, |
kserver_search_begin (const char *hostname, WORD port, |
1112 |
const char *pattern, int *conn_fd, size_t *nkeys) |
const char *pattern, int *conn_fd, size_t *nkeys) |
1113 |
{ |
{ |
1114 |
const char *fmt; |
|
1115 |
char *request = NULL; |
StringBuffer req; |
1116 |
|
const char *reqbuf; |
1117 |
char *enc_patt = NULL; |
char *enc_patt = NULL; |
1118 |
char status[128]; |
char status[128]; |
1119 |
int rc, sock_fd; |
int rc, sock_fd; |
1120 |
int reqlen; |
int nread; |
1121 |
|
|
1122 |
*conn_fd = 0; |
*conn_fd = 0; |
1123 |
|
|
1126 |
goto leave; |
goto leave; |
1127 |
|
|
1128 |
enc_patt = URL_encode (pattern, strlen (pattern), NULL); |
enc_patt = URL_encode (pattern, strlen (pattern), NULL); |
|
reqlen = strlen (enc_patt) + strlen (hostname) + 32 + 1; |
|
|
|
|
1129 |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
1130 |
fmt = "GET http://%s:%d/pks/lookup?options=mr&op=index&search=%s HTTP/1.0\r\n" |
req = req + "GET http://" + skip_type_prefix (hostname) + ":" + port + |
1131 |
"Proxy-Authorization: Basic %s\r\n\r\n"; |
"/pks/lookup?options=mr&op=index&search=" + enc_patt + |
1132 |
reqlen += strlen (proxy.base64_user) + strlen (fmt) + 1; |
" HTTP/1.0\r\n" |
1133 |
request = safe_alloc (reqlen+1); |
"Proxy-Authorization: Basic " + proxy.base64_user + "\r\n\r\n"; |
|
_snprintf (request, reqlen, fmt, skip_type_prefix (hostname), port, |
|
|
enc_patt, proxy.base64_user); |
|
1134 |
} |
} |
1135 |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
1136 |
fmt = "GET http://%s:%d/pks/lookup?options=mr&op=index&search=%s HTTP/1.0\r\n\r\n"; |
req = req + "GET http://" + skip_type_prefix (hostname) + ":" + port + |
1137 |
reqlen += strlen (fmt)+1; |
"/pks/lookup?options=mr&op=index&search=" + enc_patt + |
1138 |
request = safe_alloc (reqlen+1); |
" HTTP/1.0\r\n\r\n"; |
|
_snprintf (request, reqlen, skip_type_prefix (hostname), port, |
|
|
enc_patt); |
|
1139 |
} |
} |
1140 |
else { |
else { |
1141 |
fmt = "GET /pks/lookup?options=mr&op=index&search=%s HTTP/1.0\r\n\r\n"; |
req = req + "GET /pks/lookup?options=mr&op=index&search=" + |
1142 |
reqlen += strlen (fmt)+1; |
enc_patt + " HTTP/1.0\r\n\r\n"; |
|
request = safe_alloc (reqlen+1); |
|
|
_snprintf (request, reqlen, fmt, enc_patt); |
|
1143 |
} |
} |
1144 |
|
|
1145 |
log_debug ("kserver_search_begin:\r\n%s\r\n", request); |
log_debug ("kserver_search_begin:\r\n%s\r\n", req.getBuffer ()); |
1146 |
if (sock_write (sock_fd, request, strlen (request)) == SOCKET_ERROR) { |
reqbuf = req.getBuffer (); |
1147 |
|
if (sock_write (sock_fd, reqbuf, strlen (reqbuf)) == SOCKET_ERROR) { |
1148 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1149 |
goto leave; |
goto leave; |
1150 |
} |
} |
1158 |
|
|
1159 |
/* Skip all lines until we reach the "info:" record. */ |
/* Skip all lines until we reach the "info:" record. */ |
1160 |
for (;;) { |
for (;;) { |
1161 |
if (sock_getline (sock_fd, status, DIM (status)-1, &reqlen)) { |
if (sock_getline (sock_fd, status, DIM (status)-1, &nread)) { |
1162 |
log_debug ("kserver_search_begin: retrieving status line failed.\r\n"); |
log_debug ("kserver_search_begin: retrieving status line failed.\r\n"); |
1163 |
closesocket (sock_fd); |
closesocket (sock_fd); |
1164 |
sock_fd = 0; |
sock_fd = 0; |
1174 |
*conn_fd = sock_fd; |
*conn_fd = sock_fd; |
1175 |
|
|
1176 |
leave: |
leave: |
|
free_if_alloc (request); |
|
1177 |
free_if_alloc (enc_patt); |
free_if_alloc (enc_patt); |
1178 |
return rc; |
return rc; |
1179 |
} |
} |
1188 |
int recno = 0; |
int recno = 0; |
1189 |
int off = 0; |
int off = 0; |
1190 |
|
|
1191 |
/* pub:BF3DF9B4:17:1024:925411133:: */ |
/* pub:{BF3DF9B4, ED4681C9BF3DF9B4, FPR}:17:1024:925411133:: */ |
1192 |
p = strtok (buf, ":"); |
p = strtok (buf, ":"); |
1193 |
while (p != NULL) { |
while (p != NULL) { |
1194 |
recno++; |
recno++; |