1 |
/* wptKeyserver.cpp - W32 Keyserver Access |
/* wptKeyserver.cpp - W32 Keyserver Access |
2 |
* Copyright (C) 2000-2005 Timo Schulz |
* Copyright (C) 2000-2009 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 |
20 |
|
|
21 |
#include <windows.h> |
#include <windows.h> |
22 |
|
#include <shlobj.h> |
23 |
#include <stdio.h> |
#include <stdio.h> |
24 |
#include <sys/stat.h> |
#include <sys/stat.h> |
25 |
#include <ctype.h> |
#include <ctype.h> |
26 |
|
|
27 |
|
#include "wptGPG.h" |
28 |
#include "wptKeyserver.h" |
#include "wptKeyserver.h" |
29 |
#include "wptErrors.h" |
#include "wptErrors.h" |
30 |
#include "wptTypes.h" |
#include "wptTypes.h" |
31 |
#include "wptNLS.h" |
#include "wptNLS.h" |
32 |
#include "wptW32API.h" |
#include "wptW32API.h" |
|
#include "wptVersion.h" |
|
|
#include "wptGPG.h" |
|
33 |
#include "wptRegistry.h" |
#include "wptRegistry.h" |
34 |
|
#include "wptUTF8.h" |
35 |
|
#include "wptVersion.h" |
36 |
|
#include "StringBuffer.h" |
37 |
|
|
38 |
|
|
39 |
static char base64code[] = |
/* Map net_errno to a winsock error. */ |
40 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
#define net_errno ((int)WSAGetLastError ()) |
41 |
|
|
42 |
keyserver server[MAX_KEYSERVERS] = {0}; |
keyserver server[MAX_KEYSERVERS] = {0}; |
43 |
static keyserver_proxy_ctx proxy = {0}; |
keyserver_proxy_s proxy = {0}; |
44 |
static const char * server_list[] = { |
static const char *server_list[] = { |
45 |
"hkp://wwwkeys.nl.pgp.net", |
"hkp://pool.sks-keyservers.net", |
|
"hkp://wwwkeys.pl.pgp.net", |
|
|
"hkp://wwwkeys.at.pgp.net", |
|
|
"hkp://wwwkeys.ch.pgp.net", |
|
|
"hkp://wwwkeys.de.pgp.net", |
|
|
"hkp://wwwkeys.dk.pgp.net", |
|
|
"hkp://wwwkeys.cz.pgp.net", |
|
|
"hkp://wwwkeys.es.pgp.net", |
|
|
"hkp://wwwkeys.eu.pgp.net", |
|
|
"hkp://wwwkeys.uk.pgp.net", |
|
|
"hkp://wwwkeys.us.pgp.net", |
|
|
"hkp://gnv.us.ks.cryptnet.net", |
|
46 |
"hkp://subkeys.pgp.net", |
"hkp://subkeys.pgp.net", |
47 |
"ldap://keyserver.pgp.com", |
"http://minsky.surfnet.nl", |
48 |
NULL |
NULL |
49 |
}; |
}; |
50 |
static char hkp_errmsg[1024]; |
#define NUM_DEF_KEYSERVERS 3 |
|
static int hkp_err = 0; |
|
|
static u32 conf_timestamp = 0; |
|
|
char * default_keyserver = NULL; |
|
|
unsigned short default_keyserver_port = 0; |
|
51 |
|
|
52 |
|
static char hkp_err_msg[384]; /* Holds the error message from the server */ |
53 |
|
static int hkp_has_err = 0; /* != 0 indicates an error occurred. */ |
54 |
|
|
55 |
|
/* Default keyserver and port. */ |
56 |
|
char *default_keyserver = NULL; |
57 |
|
WORD default_keyserver_port = 0; |
58 |
|
|
59 |
|
/* Default socket timeout (secs). */ |
60 |
|
static size_t default_socket_timeout = 6; |
61 |
|
|
62 |
|
|
63 |
|
/* Wrapper for safe memory allocation. */ |
64 |
|
static char* |
65 |
|
safe_alloc (DWORD n) |
66 |
|
{ |
67 |
|
char *p = new char[n+1]; |
68 |
|
if (!p) |
69 |
|
BUG (0); |
70 |
|
memset (p, 0, n); |
71 |
|
return p; |
72 |
|
} |
73 |
|
|
74 |
|
|
75 |
|
/* Basic64 encode the input @inbuf to @outbuf. */ |
76 |
static void |
static void |
77 |
base64_encode( const char *inputstr, char *outputstr, int maxlen ) |
base64_encode (const char *inbuf, char *outbuf) |
78 |
{ |
{ |
79 |
int index = 0, temp = 0, res = 0, i = 0, inputlen = 0, len = 0; |
char base64code[] = |
80 |
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
81 |
|
int index = 0, temp = 0, res = 0; |
82 |
|
int inputlen, len = 0; |
83 |
|
|
84 |
inputlen = strlen (inputstr); /* fixme: check if len > maxlen */ |
inputlen = strlen (inbuf); |
85 |
for (i = 0; i <inputlen; i++) { |
for (int i = 0; i < inputlen; i++) { |
86 |
res = temp; |
res = temp; |
87 |
res = (res << 8) | (inputstr[i] & 0xFF); |
res = (res << 8) | (inbuf[i] & 0xFF); |
88 |
switch (index++) { |
switch (index++) { |
89 |
case 0: outputstr[len++] = base64code[res >> 2 & 0x3F]; res &= 0x3; break; |
case 0: |
90 |
case 1: outputstr[len++] = base64code[res >> 4 & 0x3F]; res &= 0xF; break; |
outbuf[len++] = base64code[res >> 2 & 0x3F]; |
91 |
case 2: outputstr[len++] = base64code[res >> 6 & 0x3F]; |
res &= 0x3; |
92 |
outputstr[len++] = base64code[res & 0x3F]; res = index = 0; break; |
break; |
93 |
|
case 1: |
94 |
|
outbuf[len++] = base64code[res >> 4 & 0x3F]; |
95 |
|
res &= 0xF; |
96 |
|
break; |
97 |
|
case 2: |
98 |
|
outbuf[len++] = base64code[res >> 6 & 0x3F]; |
99 |
|
outbuf[len++] = base64code[res & 0x3F]; |
100 |
|
res = index = 0; |
101 |
|
break; |
102 |
} |
} |
103 |
temp = res; |
temp = res; |
104 |
} |
} |
105 |
|
|
106 |
switch( index ) { |
if (index == 2) { |
107 |
case 0: break; |
outbuf[len++] = base64code[temp << 2 & 0x3F]; |
108 |
case 2: outputstr[len++] = base64code[temp << 2 & 0x3F]; |
outbuf[len++] = '='; |
|
outputstr[len++] = '='; break; |
|
|
case 1: outputstr[len++] = base64code[temp << 4 & 0x3F]; |
|
|
outputstr[len++] = '='; |
|
|
outputstr[len++] = '='; |
|
109 |
} |
} |
110 |
|
else if (index == 1) { |
111 |
outputstr[len] = '\0'; |
outbuf[len++] = base64code[temp << 4 & 0x3F]; |
112 |
} /* base64_encode */ |
outbuf[len++] = '='; |
113 |
|
outbuf[len++] = '='; |
114 |
|
} |
115 |
|
|
116 |
|
outbuf[len] = '\0'; |
117 |
|
} |
118 |
|
|
119 |
|
|
120 |
|
/* Check that the given buffer contains a valid keyserver URL |
121 |
|
and return the prefix length, 0 in case of an error. */ |
122 |
|
static int |
123 |
|
check_URL (const char *buf) |
124 |
|
{ |
125 |
|
if (strstr (buf, "hkp://")) |
126 |
|
return 6; |
127 |
|
if (strstr (buf, "http://")) |
128 |
|
return 7; |
129 |
|
return 0; |
130 |
|
} |
131 |
|
|
132 |
|
|
133 |
/* Skip the URL schema and return only the host part of it. */ |
/* Skip the URL schema and return only the host part of it. */ |
134 |
static const char * |
static const char* |
135 |
skip_type_prefix (const char * hostname) |
skip_type_prefix (const char *hostname) |
136 |
{ |
{ |
137 |
if (hostname && !strncmp (hostname, "http://", 7)) |
int pos; |
138 |
hostname += 7; |
|
139 |
else if (hostname && !strncmp (hostname, "hkp://", 6)) |
if (!hostname) |
140 |
hostname += 6; |
return hostname; |
141 |
else if (hostname && !strncmp (hostname, "finger://", 9)) |
|
142 |
hostname += 9; |
pos = check_URL (hostname); |
143 |
else if (hostname && !strncmp (hostname, "ldap://", 7)) |
if (pos > 0) |
144 |
hostname += 7; |
hostname += pos; |
145 |
return hostname; |
return hostname; |
146 |
} |
} |
147 |
|
|
148 |
|
|
149 |
|
/* Parse the keyserver response and extract the human |
150 |
|
readable text passages. */ |
151 |
|
static int |
152 |
|
parse_keyserver_error (const char *resp, char *txt, size_t txtlen) |
153 |
|
{ |
154 |
|
char *p, *p2; |
155 |
|
|
156 |
|
/* If we find no 'Error' substring we assume a success. */ |
157 |
|
if (!stristr (resp, "Error")) |
158 |
|
return -1; |
159 |
|
|
160 |
|
memset (txt, 0, txtlen); |
161 |
|
p = strstr (resp, "\r\n\r\n"); |
162 |
|
if (!p) |
163 |
|
return -1; |
164 |
|
resp += (p-resp); |
165 |
|
p = strstr (resp, "<h1>"); |
166 |
|
if (!p) |
167 |
|
return -1; |
168 |
|
resp += (p-resp)+strlen ("<h1>"); |
169 |
|
p = strstr (resp, "</h1>"); |
170 |
|
if (!p) |
171 |
|
return -1; |
172 |
|
resp += (p-resp)+strlen ("</h1>"); |
173 |
|
p2 = strstr (resp, "</body>"); |
174 |
|
if (p2 != NULL) |
175 |
|
memcpy (txt, resp, (p2-resp)); |
176 |
|
else { |
177 |
|
if (!strnicmp (resp, "<p>", 3)) |
178 |
|
resp += 3; |
179 |
|
while (resp && (*resp == '\r' || *resp == '\n')) |
180 |
|
resp++; |
181 |
|
memcpy (txt, resp, strlen (resp)); |
182 |
|
} |
183 |
|
return 0; |
184 |
|
} |
185 |
|
|
186 |
|
|
187 |
/* Check that the keyserver response indicates an OK. |
/* Check that the keyserver response indicates an OK. |
188 |
Return 0 on success. */ |
Return 0 on success. */ |
189 |
static int |
static int |
190 |
check_hkp_response (const char *resp, int recv) |
check_hkp_response (const char *resp, int recv) |
191 |
{ |
{ |
192 |
int ec, len; |
int ec; |
|
char *p, * end; |
|
193 |
|
|
194 |
ec = recv ? WPTERR_WINSOCK_RECVKEY : WPTERR_WINSOCK_SENDKEY; |
ec = recv ? WPTERR_WINSOCK_RECVKEY : WPTERR_WINSOCK_SENDKEY; |
195 |
if (!strstr (resp, "HTTP/1.0 200 OK") && |
if (!resp) |
|
!strstr (resp, "HTTP/1.1 200 OK")) /* http error */ |
|
196 |
return ec; |
return ec; |
197 |
if (strstr (resp, "Public Key Server -- Error") |
log_debug ("check_hkp_response: '%s'\r\n", resp); |
198 |
|| strstr (resp, "Public Key Server -- Error") |
if (!strstr (resp, "HTTP/1.0 200 OK") && |
199 |
|| strstr (resp, "No matching keys in database")) { |
!strstr (resp, "HTTP/1.1 200 OK") && |
200 |
p = strstr (resp, "<p>"); |
!strstr (resp, "HTTP/1.0 500 OK") && |
201 |
if (p && strlen (p) < sizeof (hkp_errmsg)) { |
!strstr (resp, "HTTP/1.1 500 OK")) |
202 |
end = strstr (p, "</p>"); |
return ec; /* http error */ |
203 |
len = end? (end - p + 1) : strlen (p); |
|
204 |
strncpy (hkp_errmsg, p, len); |
if (!parse_keyserver_error (resp, hkp_err_msg, DIM (hkp_err_msg)-2)) { |
205 |
hkp_err = 1; |
if (!strlen (hkp_err_msg)) |
206 |
} |
_snprintf (hkp_err_msg, DIM (hkp_err_msg)-1, |
207 |
|
"Unknown keyserver error"); |
208 |
|
hkp_has_err = 1; |
209 |
return ec; |
return ec; |
210 |
} |
} |
211 |
return 0; |
return 0; |
218 |
sock_getline (int fd, char *buf, int buflen, int *nbytes) |
sock_getline (int fd, char *buf, int buflen, int *nbytes) |
219 |
{ |
{ |
220 |
char ch; |
char ch; |
221 |
int count = 0; |
int nread; |
222 |
|
|
223 |
if (nbytes) |
if (nbytes) |
224 |
*nbytes = 0; |
*nbytes = 0; |
225 |
*buf = 0; |
*buf = 0; |
226 |
|
nread = 0; |
227 |
while (recv (fd, &ch, 1, 0) > 0) { |
while (recv (fd, &ch, 1, 0) > 0) { |
228 |
*buf++ = ch; |
*buf++ = ch; |
229 |
count++; |
nread++; |
230 |
if (ch == '\n' || count == (buflen - 1)) { |
if (ch == '\r') |
231 |
|
continue; /* remove this char. */ |
232 |
|
if (ch == '\n' || nread == (buflen - 1)) { |
233 |
*buf = 0; |
*buf = 0; |
234 |
if (nbytes) |
if (nbytes) |
235 |
*nbytes = count; |
*nbytes = nread; |
236 |
return 0; |
return 0; |
237 |
} |
} |
238 |
} |
} |
242 |
|
|
243 |
|
|
244 |
/* Perform a select() on the given fd to find out |
/* Perform a select() on the given fd to find out |
245 |
is there is data for reading. Wait at least @nsecs seconds. */ |
is there is data for reading. Wait at least @nsecs milli seconds. */ |
246 |
static int |
static int |
247 |
sock_select (int fd, int nsecs) |
sock_select (int fd, int nmsecs) |
248 |
{ |
{ |
249 |
FD_SET rfd; |
FD_SET rfd; |
250 |
timeval tv = {nsecs, 0}; |
timeval tv = {0, nmsecs*1000}; |
251 |
|
|
252 |
FD_ZERO (&rfd); |
FD_ZERO (&rfd); |
253 |
FD_SET (fd, &rfd); |
FD_SET (fd, &rfd); |
254 |
if (select (fd + 1, &rfd, NULL, NULL, &tv) == SOCKET_ERROR) |
if (select (fd + 1, &rfd, NULL, NULL, &tv) == SOCKET_ERROR) { |
255 |
|
log_debug ("sock_select: select() failed ec=%d.\r\n", net_errno); |
256 |
return SOCKET_ERROR; |
return SOCKET_ERROR; |
257 |
|
} |
258 |
if (FD_ISSET (fd, &rfd)) |
if (FD_ISSET (fd, &rfd)) |
259 |
return 1; |
return 1; |
260 |
return 0; |
return 0; |
261 |
} |
} |
262 |
|
|
263 |
|
|
264 |
/* Read from a socket with a fixed timeout of 10 seconds. |
/* Read from a socket @fd to buffer @buf with a fixed timeout |
265 |
|
of 10 seconds. The amount of bytes which were read are |
266 |
|
returned in @nbytes. |
267 |
Return value: 0 on success. */ |
Return value: 0 on success. */ |
268 |
static int |
static int |
269 |
sock_read (int fd, char *buf, int buflen, int *nbytes) |
sock_read (int fd, char *buf, size_t buflen, int *nbytes) |
270 |
{ |
{ |
271 |
DWORD nread; |
size_t nleft = buflen; |
272 |
int nleft = buflen; |
size_t n; |
273 |
int rc, n = 0; |
int nread; |
274 |
|
int rc; |
275 |
|
|
276 |
|
if (nbytes) |
277 |
|
*nbytes = 0; |
278 |
|
n = 0; |
279 |
while (nleft > 0) { |
while (nleft > 0) { |
280 |
if (n >= 10) |
if (n >= default_socket_timeout) |
281 |
return WPTERR_WINSOCK_TIMEOUT; |
return WPTERR_WINSOCK_TIMEOUT; |
282 |
rc = sock_select (fd, 1); |
rc = sock_select (fd, 400); |
283 |
if (rc == SOCKET_ERROR) |
if (rc == SOCKET_ERROR) |
284 |
return SOCKET_ERROR; |
return rc; |
285 |
else if( !rc ) |
else if (!rc) |
286 |
n++; |
n++; |
287 |
else { |
else { |
288 |
nread = recv (fd, buf, nleft, 0); |
nread = recv (fd, buf, nleft, 0); |
289 |
if (nread == SOCKET_ERROR) |
if (nread == SOCKET_ERROR) { |
290 |
|
log_debug ("sock_read: recv() failed ec=%d.\r\n", net_errno); |
291 |
return SOCKET_ERROR; |
return SOCKET_ERROR; |
292 |
|
} |
293 |
else if (!nread) |
else if (!nread) |
294 |
break; |
break; |
295 |
nleft -= nread; |
nleft -= nread; |
303 |
} |
} |
304 |
|
|
305 |
|
|
306 |
/* Write @buf to a socket @fd. */ |
/* Helper to create a string from the gpgme data and |
307 |
|
then release the gpgme data object. */ |
308 |
|
char* |
309 |
|
data_release_and_get_mem (gpgme_data_t in, size_t *r_bufferlen) |
310 |
|
{ |
311 |
|
char *buffer, *p; |
312 |
|
size_t n; |
313 |
|
|
314 |
|
gpg_data_putc (in, '\0'); |
315 |
|
p = gpgme_data_release_and_get_mem (in, &n); |
316 |
|
buffer = m_strdup (p); |
317 |
|
if (r_bufferlen) |
318 |
|
*r_bufferlen = n; |
319 |
|
gpgme_free (p); |
320 |
|
return buffer; |
321 |
|
} |
322 |
|
|
323 |
|
|
324 |
|
/* Read much data as possible from the socket @fd and |
325 |
|
return the data in @buffer. Caller must free data. |
326 |
|
Return value: 0 on success. */ |
327 |
|
int |
328 |
|
sock_read_ext (int fd, char **buffer, size_t *r_bufferlen) |
329 |
|
{ |
330 |
|
gpgme_data_t dh; |
331 |
|
char buf[1024]; |
332 |
|
size_t timeout=0; |
333 |
|
int rc; |
334 |
|
|
335 |
|
*buffer = NULL; |
336 |
|
if (gpgme_data_new (&dh)) |
337 |
|
return SOCKET_ERROR; |
338 |
|
while (timeout < default_socket_timeout) { |
339 |
|
rc = sock_select (fd, 500); |
340 |
|
if (rc == SOCKET_ERROR) { |
341 |
|
gpgme_data_release (dh); |
342 |
|
return rc; |
343 |
|
} |
344 |
|
else if (!rc) |
345 |
|
timeout++; /* socket not ready yet. */ |
346 |
|
else { |
347 |
|
int nread = recv (fd, buf, DIM (buf), 0); |
348 |
|
log_debug ("recv n=%d bytes\r\n", nread); |
349 |
|
if (nread == SOCKET_ERROR) |
350 |
|
return SOCKET_ERROR; |
351 |
|
else if (!nread) |
352 |
|
break; |
353 |
|
gpgme_data_write (dh, buf, nread); |
354 |
|
} |
355 |
|
} |
356 |
|
|
357 |
|
if (timeout >= default_socket_timeout) { |
358 |
|
gpgme_data_release (dh); |
359 |
|
log_debug ("sock_read_ext: timeout\r\n"); |
360 |
|
return WPTERR_WINSOCK_TIMEOUT; |
361 |
|
} |
362 |
|
log_debug ("sock_read_ext: success\r\n"); |
363 |
|
*buffer = data_release_and_get_mem (dh, r_bufferlen); |
364 |
|
return 0; |
365 |
|
} |
366 |
|
|
367 |
|
|
368 |
|
/* Write the buffer @buf with the length @buflen to a socket @fd. */ |
369 |
static int |
static int |
370 |
sock_write (int fd, const char *buf, int buflen) |
sock_write (int fd, const char *buf, size_t buflen) |
371 |
{ |
{ |
372 |
DWORD nwritten; |
int nwritten; |
373 |
int nleft = buflen; |
int nleft = buflen; |
374 |
|
|
375 |
while (nleft > 0) { |
while (nleft > 0) { |
376 |
nwritten = send (fd, buf, nleft, 0); |
nwritten = send (fd, buf, nleft, 0); |
377 |
if (nwritten == SOCKET_ERROR) |
if (nwritten == SOCKET_ERROR) { |
378 |
|
log_debug ("sock_write: send() failed ec=%d\r\n", net_errno); |
379 |
return SOCKET_ERROR; |
return SOCKET_ERROR; |
380 |
|
} |
381 |
nleft -= nwritten; |
nleft -= nwritten; |
382 |
buf += nwritten; |
buf += nwritten; |
383 |
} |
} |
384 |
|
|
385 |
return 0; |
return 0; |
386 |
} |
} |
387 |
|
|
388 |
|
|
|
void |
|
|
set_default_kserver (void) |
|
|
{ |
|
|
char * p = get_reg_entry_keyserver ("Default"); |
|
|
free_if_alloc (default_keyserver); |
|
|
default_keyserver = p && *p != ' ' ? p : m_strdup (DEF_HKP_KEYSERVER); |
|
|
p = get_reg_entry_keyserver ("Default_Port"); |
|
|
if (p && *p) { |
|
|
default_keyserver_port = (u16)strtoul (p, NULL, 10); |
|
|
free_if_alloc (p); |
|
|
} |
|
|
else |
|
|
default_keyserver_port = HKP_PORT; |
|
|
} |
|
|
|
|
|
|
|
389 |
/* Initialize the Winsock2 interface.*/ |
/* Initialize the Winsock2 interface.*/ |
390 |
int |
int |
391 |
wsock_init (void) |
wsock_init (void) |
392 |
{ |
{ |
393 |
WSADATA wsa; |
WSADATA wsa; |
394 |
|
|
395 |
if (WSAStartup (0x202, &wsa)) |
if (WSAStartup (0x202, &wsa)) { |
396 |
|
log_debug ("wsock_init: WSAStartup failed ec=%d\r\n", net_errno); |
397 |
return WPTERR_WINSOCK_INIT; |
return WPTERR_WINSOCK_INIT; |
398 |
set_default_kserver (); |
} |
399 |
return 0; |
return 0; |
400 |
} |
} |
401 |
|
|
402 |
|
|
403 |
/* Cleanup the Winsock2 interface. */ |
/* Cleanup the Winsock2 interface and free all resources. */ |
404 |
void |
void |
405 |
wsock_end (void) |
wsock_end (void) |
406 |
{ |
{ |
407 |
int i; |
if (default_keyserver != NULL) { |
408 |
|
char str_port[64]; |
409 |
free_if_alloc (default_keyserver); |
set_reg_entry_keyserver ("Default", default_keyserver); |
410 |
default_keyserver = NULL; |
free_if_alloc (default_keyserver); |
411 |
for (i=0; i < MAX_KEYSERVERS; i++) { |
sprintf (str_port, "%d", default_keyserver_port); |
412 |
if (server[i].used) |
set_reg_entry_keyserver ("Default_Port", str_port); |
413 |
|
} |
414 |
|
for (int i=0; i < MAX_KEYSERVERS; i++) { |
415 |
|
if (server[i].name != NULL) |
416 |
free_if_alloc (server[i].name); |
free_if_alloc (server[i].name); |
417 |
} |
} |
418 |
|
kserver_proxy_release (&proxy); |
419 |
WSACleanup (); |
WSACleanup (); |
420 |
|
log_debug ("wsock_end: cleanup finished.\r\n"); |
421 |
} |
} |
422 |
|
|
423 |
|
|
424 |
/* Return a string representation of a winsock error. */ |
/* Return a string representation of a winsock error. */ |
425 |
const char* |
const char* |
426 |
wsock_strerror (void) |
wsock_strerror (void) |
427 |
{ |
{ |
|
static char buf[384]; |
|
428 |
int ec = WSAGetLastError (); |
int ec = WSAGetLastError (); |
429 |
|
|
430 |
|
if (!ec) |
431 |
|
return ""; |
432 |
switch (ec) { |
switch (ec) { |
433 |
case WSAENETDOWN: |
case WSANOTINITIALISED: |
434 |
return _("The network subsystem has failed"); |
return _("Winsock subsystem has not been initialized"); |
435 |
case WSAHOST_NOT_FOUND: |
|
436 |
return _("Authoritative Answer Host not found"); |
case WSAENETDOWN: |
437 |
case WSAETIMEDOUT: |
return _("Network subsystem has failed"); |
438 |
return _("The connection has been dropped because of a network failure"); |
|
439 |
default: |
|
440 |
_snprintf (buf, sizeof (buf) -1, _("Unknown Winsock error ec=%d"),ec); |
case WSATRY_AGAIN: |
441 |
return buf; |
return _("Nonauthoritative host not found, or server failure"); |
442 |
|
|
443 |
|
case WSAHOST_NOT_FOUND: |
444 |
|
return _("Could not resolve host name"); |
445 |
|
|
446 |
|
case WSAETIMEDOUT: |
447 |
|
case WSAECONNABORTED: |
448 |
|
return _("Connection timeout"); |
449 |
|
|
450 |
|
case WSAENETRESET: |
451 |
|
case WSAECONNRESET: |
452 |
|
return _("Connection resetted by peer"); |
453 |
|
|
454 |
|
case WSAENETUNREACH: |
455 |
|
return _("The network cannot be reached from this host at this time"); |
456 |
|
|
457 |
|
case WSAEHOSTUNREACH: |
458 |
|
return _("A socket operation was attempted to an unreachable host"); |
459 |
|
|
460 |
|
case WSAECONNREFUSED: |
461 |
|
return _("The attempt to connect was forcefully rejected"); |
462 |
|
|
463 |
|
case WSAESHUTDOWN: |
464 |
|
return _("Socket has been shutdown"); |
465 |
|
|
466 |
|
default: |
467 |
|
break; |
468 |
} |
} |
469 |
return NULL; |
|
470 |
|
return _("Unknown network error"); |
471 |
|
} |
472 |
|
|
473 |
|
|
474 |
|
/* Set default socket timeout for all reading operations. */ |
475 |
|
void |
476 |
|
kserver_set_socket_timeout (size_t nsec) |
477 |
|
{ |
478 |
|
if (nsec < 0 || nsec > 3600) |
479 |
|
nsec = 10; |
480 |
|
default_socket_timeout = nsec; |
481 |
} |
} |
482 |
|
|
483 |
|
|
485 |
const char* |
const char* |
486 |
kserver_strerror (void) |
kserver_strerror (void) |
487 |
{ |
{ |
488 |
if (hkp_err) |
return hkp_has_err? hkp_err_msg : NULL; |
|
return hkp_errmsg; |
|
|
return NULL; |
|
489 |
} |
} |
490 |
|
|
491 |
|
|
492 |
/* Read a keyserver from the list at index @idx. |
/* Read a keyserver from the list at index @idx. |
493 |
Return value: keyserver name at the given position. */ |
Return value: keyserver name at the given position. */ |
494 |
const char* |
const char* |
495 |
kserver_get_hostname (int idx, int type, u16 *port) |
kserver_get_hostname (int idx, int type, WORD *port) |
496 |
{ |
{ |
497 |
if (type == -1) { |
if (type == -1) { |
498 |
*port = default_keyserver_port; |
*port = default_keyserver_port; |
499 |
return default_keyserver; |
return default_keyserver; |
500 |
} |
} |
501 |
else if (!type && idx < DIM (server_list)) { |
else if (!type && (size_t)idx < DIM (server_list)) { |
502 |
*port = HKP_PORT; |
*port = HKP_PORT; |
503 |
return server_list[idx]; |
return server_list[idx]; |
504 |
} |
} |
517 |
closesocket (fd); |
closesocket (fd); |
518 |
return 0; |
return 0; |
519 |
} |
} |
520 |
|
log_debug ("kserver_check_inet_connection: no inet connection.\r\n"); |
521 |
return -1; |
return -1; |
522 |
} |
} |
523 |
|
|
524 |
|
|
525 |
/* |
/* If the request contains the keyid, it have to be |
526 |
* If the request contains the keyid, it have to be |
always postfix with '0x'+keyid. This code checks |
527 |
* always postfix with '0x'+keyid. This code checks |
if the keyid is a decimal value and if so if it contains |
528 |
* if the keyid is a decimal value and if so if it contains |
the '0x'. If not it is inserted. */ |
|
* the '0x'. If not it is inserted. |
|
|
*/ |
|
529 |
const char* |
const char* |
530 |
kserver_check_keyid (const char *keyid) |
kserver_check_keyid (const char *keyid) |
531 |
{ |
{ |
532 |
static char id[20]; |
static char id[64]; |
533 |
|
|
534 |
if (strstr (keyid, "@")) |
if (strstr (keyid, "@")) |
535 |
return keyid; /* email address */ |
return keyid; /* email address */ |
536 |
if (strncmp (keyid, "0x", 2)) { |
if (strncmp (keyid, "0x", 2)) { |
537 |
memset (&id, 0, sizeof (id)); |
memset (&id, 0, sizeof (id)); |
538 |
_snprintf (id, sizeof (id)-1, "0x%s", keyid); |
_snprintf (id, DIM (id)-1, "0x%s", keyid); |
539 |
return id; |
return id; |
540 |
} |
} |
541 |
return keyid; |
return keyid; |
542 |
} /* kserver_check_keyid */ |
} |
543 |
|
|
544 |
|
|
545 |
|
/* Update the keyserver proxy user. */ |
546 |
static void |
static void |
547 |
kserver_update_proxyuser (const char *proxy_user, const char *proxy_pass) |
update_proxy_user (const char *proxy_user, const char *proxy_pass) |
548 |
{ |
{ |
549 |
char t[257]; /* user:pass = 127+1+127+1 = 257 */ |
char t[257]; /* user:pass = 127+1+127+1 = 257 */ |
550 |
int n = 0; |
size_t n; |
551 |
|
|
552 |
n = 4*strlen (proxy_user) / 3 + 32 + strlen (proxy_pass) + 2; |
n = 4*(strlen (proxy_user) + strlen (proxy_pass))/3 + 32 + 2; |
553 |
free_if_alloc (proxy.base64_user); |
free_if_alloc (proxy.base64_user); |
554 |
proxy.base64_user = new char[n]; |
proxy.base64_user = safe_alloc (n+1); |
555 |
if (!proxy.base64_user) |
_snprintf (t, DIM (t)-1, "%s:%s", proxy_user, proxy_pass); |
556 |
BUG (0); |
base64_encode (t, proxy.base64_user); |
|
_snprintf (t, sizeof (t)-1, "%s:%s", proxy_user, proxy_pass); |
|
|
base64_encode (t, proxy.base64_user, 257); |
|
557 |
free_if_alloc (proxy.user); |
free_if_alloc (proxy.user); |
558 |
free_if_alloc (proxy.pass); |
free_if_alloc (proxy.pass); |
559 |
proxy.user = m_strdup (proxy_user); |
proxy.user = m_strdup (proxy_user); |
560 |
proxy.pass = m_strdup (proxy_pass); |
proxy.pass = m_strdup (proxy_pass); |
561 |
} /* kserver_update_proxyuser */ |
} |
562 |
|
|
563 |
|
|
564 |
/* Check that the given buffer contains a valid keyserver URL. */ |
/* Set the default keyserver. The position is always one. */ |
565 |
static int |
void |
566 |
check_URL (const char * buf) |
keyserver_set_default (const char *hostname, WORD port) |
567 |
{ |
{ |
568 |
if (strlen (buf) < 7) |
int pos=0; |
|
return -1; |
|
|
if (!strstr (buf, "ldap://") |
|
|
&& !strstr (buf, "http://") |
|
|
&& !strstr (buf, "finger://") |
|
|
&& !strstr (buf, "hkp://")) |
|
|
return -1; |
|
|
return 0; |
|
|
} |
|
569 |
|
|
570 |
|
if (port == 0) |
571 |
/* Get the port number from the given protocol. */ |
port = HKP_PORT; |
572 |
static int |
|
573 |
port_from_proto (int proto) |
free_if_alloc (default_keyserver); |
574 |
{ |
default_keyserver = m_strdup (hostname); |
575 |
switch (proto) { |
default_keyserver_port = port; |
576 |
case KSPROTO_LDAP: return 0; |
|
577 |
case KSPROTO_FINGER: return FINGER_PORT; |
while (pos < MAX_KEYSERVERS) { |
578 |
case KSPROTO_HTTP: return HKP_PORT; |
if (server[pos].is_default) |
579 |
|
break; |
580 |
|
if (server[pos].used) { |
581 |
|
pos++; |
582 |
|
continue; |
583 |
|
} |
584 |
|
break; |
585 |
} |
} |
586 |
return 0; |
free_if_alloc (server[pos].name); |
587 |
|
server[pos].name = m_strdup (default_keyserver); |
588 |
|
server[pos].used = 1; |
589 |
|
server[pos].port = port; |
590 |
|
server[pos].is_default = 1; |
591 |
} |
} |
592 |
|
|
593 |
|
|
594 |
/* Get the port number from the given URL. */ |
/* Skip all kind of whitespace chars in @str. |
595 |
static int |
static const char* |
596 |
proto_from_URL (const char * buf) |
skip_whitespace (const char *str) |
597 |
{ |
{ |
598 |
if (strstr( buf, "ldap")) |
while (str && *str) { |
|
return KSPROTO_LDAP; |
|
|
else if (strstr( buf, "finger")) |
|
|
return KSPROTO_FINGER; |
|
|
return KSPROTO_HKP; |
|
|
} |
|
|
|
|
|
|
|
|
void |
|
|
keyserver_set_default (const char * hostname, u16 port) |
|
|
{ |
|
|
if (hostname) { |
|
|
free_if_alloc (default_keyserver); |
|
|
default_keyserver = m_strdup (hostname); |
|
|
if (!default_keyserver) |
|
|
BUG (0); |
|
|
default_keyserver_port = port; |
|
|
} |
|
|
server[0].name = m_strdup (default_keyserver); |
|
|
server[0].used = 1; |
|
|
server[0].port = port; |
|
|
server[0].proto = proto_from_URL (default_keyserver); |
|
|
} /* keyserver_set_default */ |
|
|
|
|
|
|
|
|
static const char * |
|
|
skip_whitespace (const char * str) |
|
|
{ |
|
|
while (str && *str) |
|
|
{ |
|
599 |
if (*str == ' ' || |
if (*str == ' ' || |
600 |
*str == '\t' || |
*str == '\t' || |
601 |
*str == '\n' || |
*str == '\n' || |
602 |
*str == '\r') |
*str == '\r') { |
|
{ |
|
603 |
str++; |
str++; |
604 |
continue; |
continue; |
605 |
} |
} |
606 |
break; |
break; |
607 |
} |
} |
608 |
return str; |
return str; |
609 |
|
}*/ |
610 |
|
|
611 |
|
|
612 |
|
/* Return the specified keyserver config setting @key as an integer. */ |
613 |
|
static int |
614 |
|
get_conf_kserver_int (const char *key) |
615 |
|
{ |
616 |
|
char *p; |
617 |
|
int val = 0; |
618 |
|
|
619 |
|
p = get_reg_entry_keyserver (key); |
620 |
|
if (p && *p) |
621 |
|
val = atoi (p); |
622 |
|
free_if_alloc (p); |
623 |
|
return val; |
624 |
} |
} |
625 |
|
|
626 |
|
|
627 |
int |
/* Read the proxy configuration and store it into @prox. */ |
628 |
kserver_load_conf (const char * conf) |
static void |
629 |
|
read_proxy_config (keyserver_proxy_t prox) |
630 |
{ |
{ |
631 |
struct stat statbuf; |
char *proto; |
632 |
FILE *fp; |
|
633 |
char buf[1024], * s, *p; |
if (!prox) |
634 |
char *user = NULL, *pass = NULL; |
return; |
|
int pos; |
|
|
int no_config=0, chk_pos=0; |
|
635 |
|
|
636 |
for (pos = 0; pos < MAX_KEYSERVERS; pos++) { |
proto = get_reg_entry_keyserver("Proto"); |
637 |
server[pos].used = 0; |
if (proto != NULL && strlen (proto) > 0) |
638 |
server[pos].port = HKP_PORT; |
prox->proto = atoi (proto); |
639 |
} |
else |
640 |
|
prox->proto = PROXY_PROTO_NONE; |
641 |
|
free_if_alloc (proto); |
642 |
|
|
643 |
fp = fopen (conf, "rb"); |
free_if_alloc (prox->host); |
644 |
if (!fp) { |
prox->host = get_reg_entry_keyserver ("Host"); |
645 |
for (pos = 0; server_list[pos]; pos++) { |
|
646 |
server[pos].used = 1; |
free_if_alloc (prox->user); |
647 |
server[pos].name = m_strdup (server_list[pos]); |
prox->user = get_reg_entry_keyserver ("User"); |
648 |
server[pos].proto = proto_from_URL (server_list[pos]); |
|
649 |
} |
free_if_alloc (prox->pass); |
650 |
no_config=1; |
prox->pass = get_reg_entry_keyserver ("Pass"); |
651 |
} |
|
652 |
get_reg_proxy_prefs (&proxy.host, &proxy.port, &user, &pass); |
prox->port = get_conf_kserver_int ("Port"); |
653 |
if (user && pass) |
} |
|
kserver_update_proxyuser (user, pass); |
|
|
else if (user && !pass || !user && pass) { |
|
|
msg_box( NULL, _("Invalid proxy configuration." |
|
|
"You need to set a user and a password" |
|
|
"to use proxy authentication!"), _("Proxy Error"), MB_ERR ); |
|
|
} |
|
|
/* check if the host has a http:// prefix and skip it */ |
|
|
if( proxy.host && !strncmp( proxy.host, "http://", 7 ) ) { |
|
|
char *pr = m_strdup (proxy.host+7); |
|
|
if (!pr) |
|
|
BUG (NULL); |
|
|
free_if_alloc (proxy.host); |
|
|
proxy.host = pr; |
|
|
} |
|
|
free_if_alloc (user); |
|
|
free_if_alloc (pass); |
|
|
|
|
|
pos = 0; |
|
|
while( fp && !feof( fp ) ) { |
|
|
s = fgets (buf, sizeof (buf)-1, fp); |
|
|
if (!s) |
|
|
break; |
|
|
if (strstr (buf, "\r\n")) |
|
|
buf[strlen (buf)-2] = '\0'; |
|
|
if (strstr (buf, "\n")) |
|
|
buf[strlen (buf)-1] = '\0'; |
|
|
s = (char *)skip_whitespace (buf); |
|
|
if (*s == '#' || strlen (s) < 7) |
|
|
continue; |
|
|
if (check_URL (s)) { |
|
|
msg_box (NULL, _("All entries of this file must have a valid prefix.\n" |
|
|
"Currently HKP/HTTP, LDAP and FINGER are supported.\n"), |
|
|
_("Keyserver Error"), MB_ERR); |
|
|
continue; |
|
|
} |
|
|
chk_pos=6; |
|
|
if (strstr (s, "finger")) |
|
|
chk_pos = 10; |
|
|
p = strchr (s+chk_pos, ':'); |
|
|
server[pos].used = 1; |
|
|
server[pos].proto = proto_from_URL (s); |
|
|
server[pos].port = port_from_proto (server[pos].proto); |
|
|
if (!p) |
|
|
server[pos].name = m_strdup (s); |
|
|
else { |
|
|
server[pos].port = atol (p+1); |
|
|
if (server[pos].port < 0 || server[pos].port > 65536) |
|
|
server[pos].port = HKP_PORT; |
|
|
server[pos].name = new char [(p-s)+2]; |
|
|
if (!server[pos].name) |
|
|
BUG (0); |
|
|
memset (server[pos].name, 0, (p-s)+2); |
|
|
memcpy (server[pos].name, s, (p-s)); |
|
|
} |
|
|
pos++; |
|
|
if (pos > MAX_KEYSERVERS) { |
|
|
msg_box (NULL, _("The keyserver limit is exceeded"), _("Keyserver Warning"), MB_INFO); |
|
|
break; |
|
|
} |
|
|
} |
|
|
if (fp) |
|
|
fclose (fp); |
|
|
if (!pos) { |
|
|
/* only issue an error if the config file exist but it has no valid entries. */ |
|
|
keyserver_set_default (NULL, HKP_PORT); |
|
|
if (!no_config) |
|
|
return WPTERR_CONFIG_FILE; |
|
|
} |
|
|
|
|
|
if (!stat (conf, &statbuf)) |
|
|
conf_timestamp = statbuf.st_mtime; |
|
|
return 0; |
|
|
} /* kserver_load_conf */ |
|
654 |
|
|
655 |
|
|
656 |
const char * |
static int |
657 |
kserver_get_proxy (int * r_port) |
is_keyserver_present (const char *name) |
658 |
{ |
{ |
659 |
if (proxy.host) { |
for (int i=0; i < MAX_KEYSERVERS; i++) { |
660 |
if (r_port) |
if (server[i].name != NULL && |
661 |
*r_port = proxy.port; |
stricmp (server[i].name, name) == 0) |
662 |
return proxy.host; |
return 1; |
663 |
} |
} |
664 |
return NULL; |
return 0; |
665 |
} /* kserver_get_proxy */ |
} |
|
|
|
666 |
|
|
667 |
const char * |
/* Load the keyserver configuration. */ |
668 |
kserver_get_proxy_info (int id) |
int |
669 |
|
kserver_load_conf (void) |
670 |
{ |
{ |
671 |
switch (id) { |
char *str_server, *str_port; |
672 |
case PROXY_USER: return proxy.user; |
int port, list_pos; |
673 |
case PROXY_PASS: return proxy.pass; |
|
674 |
|
/* If a preferred keyserver is available, load |
675 |
|
* the values and store it at position 0. |
676 |
|
*/ |
677 |
|
str_server = get_reg_entry_keyserver("Default"); |
678 |
|
if (str_server && *str_server) { |
679 |
|
port = HKP_PORT; |
680 |
|
str_port = get_reg_entry_keyserver("Default_Port"); |
681 |
|
if (str_port && *str_port) |
682 |
|
port = atoi (str_port); |
683 |
|
keyserver_set_default (str_server, port); |
684 |
|
free_if_alloc (str_port); |
685 |
|
} |
686 |
|
free_if_alloc (str_server); |
687 |
|
|
688 |
|
/* Now add the default pool servers after the |
689 |
|
* preferred keyserver if present. |
690 |
|
*/ |
691 |
|
list_pos = 1; |
692 |
|
for (int i=0; server_list[i] != NULL; i++) { |
693 |
|
if (is_keyserver_present (server_list[i])) |
694 |
|
continue; |
695 |
|
server[list_pos].used = 1; |
696 |
|
server[list_pos].port = HKP_PORT; |
697 |
|
server[list_pos].name = m_strdup (server_list[i]); |
698 |
|
server[list_pos].is_default = 0; |
699 |
|
list_pos++; |
700 |
|
if (list_pos > MAX_KEYSERVERS) |
701 |
|
break; |
702 |
} |
} |
703 |
return NULL; |
return 0; |
704 |
} /* kserver_get_proxy_info */ |
} |
705 |
|
|
706 |
|
|
707 |
/* Connect to the keyserver @hostname. */ |
/* Connect to the keyserver @hostname (port @port). |
708 |
|
Return value: 0 on success */ |
709 |
int |
int |
710 |
kserver_connect (const char *hostname, u16 port, int *conn_fd) |
kserver_connect (const char *hostname, WORD port, int *conn_fd) |
711 |
{ |
{ |
|
int rc, fd; |
|
|
u32 iaddr; |
|
|
char host[128] = {0}; |
|
712 |
struct hostent *hp; |
struct hostent *hp; |
713 |
struct sockaddr_in sock; |
struct sockaddr_in sock; |
714 |
|
char host[128] = {0}; |
715 |
log_debug ("kserver_connect: %s:%d\r\n", hostname, port); |
DWORD iaddr; |
716 |
|
bool use_proxy = proxy.host != NULL; |
717 |
|
int rc, fd; |
718 |
|
|
719 |
if (!port) |
if (!port) |
720 |
port = HKP_PORT; |
port = HKP_PORT; |
721 |
if (conn_fd) |
if (conn_fd) |
722 |
*conn_fd = 0; |
*conn_fd = 0; |
723 |
hostname = skip_type_prefix (hostname); |
hostname = skip_type_prefix (hostname); |
724 |
|
log_debug ("kserver_connect: %s:%d\r\n", hostname, port); |
725 |
|
|
726 |
|
if (use_proxy && proxy.proto == PROXY_PROTO_HTTP) |
727 |
|
port = proxy.port; |
728 |
memset (&sock, 0, sizeof (sock)); |
memset (&sock, 0, sizeof (sock)); |
729 |
sock.sin_family = AF_INET; |
sock.sin_family = AF_INET; |
730 |
sock.sin_port = proxy.host? htons (proxy.port) : htons (port); |
sock.sin_port = htons (port); |
731 |
if (proxy.host) |
strncpy (host, use_proxy? proxy.host : hostname, DIM (host)-1); |
732 |
strncpy (host, proxy.host, 127); |
|
|
else |
|
|
strncpy (host, hostname, 127); |
|
|
|
|
733 |
if ((iaddr = inet_addr (host)) != INADDR_NONE) |
if ((iaddr = inet_addr (host)) != INADDR_NONE) |
734 |
memcpy (&sock.sin_addr, &iaddr, sizeof (iaddr)); |
memcpy (&sock.sin_addr, &iaddr, sizeof (iaddr)); |
735 |
else if ((hp = gethostbyname (host))) { |
else if ((hp = gethostbyname (host))) { |
740 |
memcpy (&sock.sin_addr, hp->h_addr, hp->h_length); |
memcpy (&sock.sin_addr, hp->h_addr, hp->h_length); |
741 |
} |
} |
742 |
else { |
else { |
743 |
log_debug ("gethostbyname: failed.\r\n"); |
log_debug ("gethostbyname(%s): failed ec=%d.\r\n", host, net_errno); |
744 |
return WPTERR_WINSOCK_RESOLVE; |
return use_proxy? WPTERR_WINSOCK_PROXY : WPTERR_WINSOCK_RESOLVE; |
745 |
} |
} |
746 |
fd = socket (AF_INET, SOCK_STREAM, 0); |
fd = socket (AF_INET, SOCK_STREAM, 0); |
747 |
if (fd == INVALID_SOCKET) |
if (fd == INVALID_SOCKET) |
748 |
return WPTERR_WINSOCK_SOCKET; |
return WPTERR_WINSOCK_SOCKET; |
749 |
rc = connect (fd, (struct sockaddr *) &sock, sizeof (sock)); |
rc = connect (fd, (struct sockaddr *) &sock, sizeof (sock)); |
750 |
if (rc == SOCKET_ERROR) { |
if (rc == SOCKET_ERROR) { |
751 |
log_debug ("connect: failed.\r\n"); |
log_debug ("connect: failed ec=%d.\r\n", net_errno); |
752 |
closesocket (fd); |
closesocket (fd); |
753 |
return WPTERR_WINSOCK_CONNECT; |
return use_proxy? WPTERR_WINSOCK_PROXY : WPTERR_WINSOCK_CONNECT; |
754 |
} |
} |
755 |
|
|
756 |
if (conn_fd) |
if (conn_fd) |
757 |
*conn_fd = fd; |
*conn_fd = fd; |
758 |
WSASetLastError (0); |
WSASetLastError (0); |
759 |
return 0; |
return 0; |
760 |
} /* kserver_connect */ |
} |
761 |
|
|
762 |
|
|
763 |
/* Perform URL-encoding on the given pubkey blob. */ |
/* Check if the URL needs to be encoded or not. |
764 |
|
static bool |
765 |
|
URL_must_encoded (const char *url) |
766 |
|
{ |
767 |
|
if (strchr (url, '.') || strchr (url, '@') || strchr (url, ' ')) |
768 |
|
return true; |
769 |
|
return false; |
770 |
|
}*/ |
771 |
|
|
772 |
|
|
773 |
|
/* Perform URL encoding of the given data. */ |
774 |
static char* |
static char* |
775 |
kserver_urlencode (const char *pubkey, size_t octets, size_t *newlen) |
URL_encode (const char *url, DWORD ulen, DWORD *ret_nlen) |
776 |
{ |
{ |
777 |
char *p, numbuf[5]; |
gpgme_data_t hd; |
778 |
size_t j = 0; |
char numbuf[8], *p; |
779 |
size_t i, size; |
size_t i, nlen; |
|
|
|
|
p = new char [2*octets]; |
|
|
if (!p) |
|
|
BUG (0); |
|
780 |
|
|
781 |
for (size=0, i=0; i < octets; i++) { |
if (gpgme_data_new (&hd)) |
782 |
if (isalnum (pubkey[i]) || pubkey[i] == '-') { |
BUG (0); |
783 |
p[size] = pubkey[i]; |
for (i=0; i < ulen; i++) { |
784 |
size++; |
if (isalnum (url[i]) || url[i] == '-') |
785 |
} |
gpg_data_putc (hd, url[i]); |
786 |
else if (pubkey[i] == ' ') { |
else if (url[i] == ' ') |
787 |
p[size] = '+'; |
gpg_data_putc (hd, '+'); |
|
size++; |
|
|
} |
|
788 |
else { |
else { |
789 |
sprintf(numbuf, "%%%02X", pubkey[i]); |
sprintf (numbuf, "%%%02X", url[i]); |
790 |
for (j = 0; j<strlen(numbuf); j++) { |
gpgme_data_write (hd, numbuf, strlen (numbuf)); |
|
p[size] = numbuf[j]; |
|
|
size++; |
|
|
} |
|
791 |
} |
} |
792 |
} |
} |
793 |
p[size] = '\0'; |
|
794 |
*newlen = size; |
p = data_release_and_get_mem (hd, &nlen); |
795 |
|
if (ret_nlen) |
796 |
|
*ret_nlen = nlen; |
797 |
return p; |
return p; |
798 |
} |
} |
799 |
|
|
800 |
|
|
801 |
/* Format a request for the given keyid (send). */ |
/* Format a request for the given keyid (send). */ |
802 |
static char* |
static char* |
803 |
kserver_send_request (const char *hostname, u16 port, const char *pubkey, int octets) |
kserver_send_request (const char *hostname, WORD port, |
804 |
|
const char *pubkey, DWORD octets) |
805 |
{ |
{ |
806 |
char *request = NULL, *enc_pubkey = NULL; |
const char *fmt; |
807 |
|
char *request; |
808 |
|
char *enc_pubkey; |
809 |
|
DWORD enc_octets; |
810 |
int reqlen; |
int reqlen; |
|
size_t enc_octets; |
|
811 |
|
|
812 |
log_debug ("kserver_send_request: %s:%d\n", hostname, port); |
log_debug ("kserver_send_request: %s:%d\r\n", hostname, port); |
813 |
|
|
814 |
if (!port) |
if (!port) |
815 |
port = HKP_PORT; |
port = HKP_PORT; |
816 |
reqlen = 512 + strlen (hostname) + 2*strlen (pubkey); |
enc_pubkey = URL_encode (pubkey, octets, &enc_octets); |
817 |
request = new char[reqlen]; |
reqlen = 2*strlen (hostname) + 2*32/*port*/ + enc_octets + 32 /*len*/ + 1; |
818 |
if (!request) |
|
819 |
BUG (0); |
if (proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
820 |
enc_pubkey = kserver_urlencode (pubkey, octets, &enc_octets); |
fmt = "POST http://%s:%d/pks/add HTTP/1.0\r\n" |
821 |
if (!enc_pubkey || !enc_octets) { |
"Referer: \r\n" |
822 |
free_if_alloc (request); |
"User-Agent: WinPT/W32\r\n" |
823 |
return NULL; |
"Host: %s:%d\r\n" |
824 |
} |
"Proxy-Authorization: Basic %s\r\n" |
825 |
|
"Content-type: application/x-www-form-urlencoded\r\n" |
826 |
if (proxy.user) { |
"Content-length: %d\r\n" |
827 |
_snprintf (request, reqlen-1, |
"Connection: close\r\n" |
828 |
"POST http://%s:%d/pks/add HTTP/1.0\r\n" |
"\r\n" |
829 |
"Referer: \r\n" |
"keytext=%s" |
830 |
"User-Agent: WinPT/W32\r\n" |
"\r\n"; |
831 |
"Host: %s:%d\r\n" |
reqlen += strlen (fmt) + strlen (proxy.base64_user) + 1; |
832 |
"Proxy-Authorization: Basic %s\r\n" |
request = safe_alloc (reqlen+1); |
833 |
"Content-type: application/x-www-form-urlencoded\r\n" |
_snprintf (request, reqlen, fmt, |
834 |
"Content-length: %d\r\n" |
skip_type_prefix (hostname), port, hostname, port, |
|
"\r\n" |
|
|
"keytext=%s" |
|
|
"\n", |
|
|
skip_type_prefix (hostname), port, hostname, port, |
|
835 |
proxy.base64_user, enc_octets+9, enc_pubkey); |
proxy.base64_user, enc_octets+9, enc_pubkey); |
836 |
} |
} |
837 |
else { |
else { |
838 |
_snprintf (request, reqlen-1, |
fmt = "POST /pks/add HTTP/1.0\r\n" |
839 |
"POST /pks/add HTTP/1.0\r\n" |
"Referer: \r\n" |
840 |
"Referer: \r\n" |
"User-Agent: WinPT/W32\r\n" |
841 |
"User-Agent: WinPT/W32\r\n" |
"Host: %s:%d\r\n" |
842 |
"Host: %s:%d\r\n" |
"Content-type: application/x-www-form-urlencoded\r\n" |
843 |
"Content-type: application/x-www-form-urlencoded\r\n" |
"Content-length: %d\r\n" |
844 |
"Content-length: %d\r\n" |
"Connection: close\r\n" |
845 |
"\r\n" |
"\r\n" |
846 |
"keytext=%s" |
"keytext=%s" |
847 |
"\n", |
"\r\n"; |
848 |
skip_type_prefix (hostname), port, enc_octets+9, enc_pubkey); |
reqlen += strlen (fmt)+1; |
849 |
|
request = safe_alloc (reqlen+1); |
850 |
|
_snprintf (request, reqlen, fmt, |
851 |
|
skip_type_prefix (hostname), port, |
852 |
|
enc_octets+9, enc_pubkey); |
853 |
} |
} |
|
free_if_alloc (enc_pubkey); |
|
854 |
|
|
855 |
log_debug ("%s\n", request); |
free_if_alloc (enc_pubkey); |
856 |
|
log_debug ("request:\r\n%s\r\n", request); |
857 |
return request; |
return request; |
858 |
} /* kserver_send_request */ |
} |
859 |
|
|
860 |
|
|
861 |
/* Interface receiving a public key. */ |
/* Interface for receiving a public key. */ |
862 |
int |
int |
863 |
kserver_recvkey (const char *hostname, u16 port, const char *keyid, char *key, int maxkeylen) |
kserver_recvkey (const char *hostname, WORD port, const char *keyid, |
864 |
{ |
char **r_key, size_t *r_keylen) |
865 |
int rc, n; |
{ |
866 |
|
StringBuffer req; |
867 |
|
const char *reqbuf; |
868 |
int conn_fd; |
int conn_fd; |
869 |
char *request = NULL; |
int rc; |
870 |
|
|
871 |
if (!port) |
if (!port) |
872 |
port = HKP_PORT; |
port = HKP_PORT; |
873 |
hkp_err = 0; /* reset */ |
hkp_has_err = 0; /* reset */ |
874 |
rc = kserver_connect (hostname, port, &conn_fd); |
rc = kserver_connect (hostname, port, &conn_fd); |
875 |
if (rc) |
if (rc) |
876 |
goto leave; |
goto leave; |
|
|
|
|
request = new char[300+1]; |
|
|
if (!request) |
|
|
BUG (0); |
|
|
|
|
|
if (proxy.host && proxy.user) { |
|
|
_snprintf (request, 300, |
|
|
"GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n" |
|
|
"Proxy-Authorization: Basic %s\r\n\r\n", |
|
|
skip_type_prefix (hostname), port, keyid, proxy.base64_user); |
|
|
} |
|
|
else if (proxy.host) { |
|
|
_snprintf (request, 300, |
|
|
"GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n", |
|
|
skip_type_prefix (hostname), port, keyid); |
|
|
} |
|
|
else { |
|
|
_snprintf (request, 300, |
|
|
"GET /pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n", keyid); |
|
|
} |
|
877 |
|
|
878 |
log_debug ("%s\r\n", request); |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
879 |
|
req = req + "GET http://" + skip_type_prefix (hostname) + |
880 |
|
":" + port + |
881 |
|
"/pks/lookup?op=get&search=" + keyid + " HTTP/1.0\r\n" + |
882 |
|
"Proxy-Authorization: Basic " + proxy.base64_user + "\r\n" + |
883 |
|
"Host: " + skip_type_prefix (hostname) + ":" + port + "\r\n"+ |
884 |
|
"Connection: close\r\n"+ |
885 |
|
"\r\n"; |
886 |
|
} |
887 |
|
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
888 |
|
req = req + "GET http://" + skip_type_prefix (hostname) + |
889 |
|
":" + port + "/pks/lookup?op=get&search=" + keyid + |
890 |
|
" HTTP/1.0\r\n" + |
891 |
|
"Host: " + skip_type_prefix (hostname) + ":" + port + "\r\n"+ |
892 |
|
"Connection: close\r\n"+ |
893 |
|
"\r\n"; |
894 |
|
} |
895 |
|
else |
896 |
|
req = req + "GET /pks/lookup?op=get&search=" + keyid + |
897 |
|
" HTTP/1.0\r\n"+ // FIXME |
898 |
|
"Host: " + skip_type_prefix (hostname) + ":" + port + "\r\n"+ |
899 |
|
"Connection: close\r\n"+ |
900 |
|
"\r\n"; |
901 |
|
|
902 |
rc = sock_write (conn_fd, request, strlen (request)); |
log_debug ("req:\r\n%s\r\n", req.getBuffer ()); |
903 |
|
|
904 |
|
reqbuf = req.getBuffer (); |
905 |
|
rc = sock_write (conn_fd, reqbuf, strlen (reqbuf)); |
906 |
if (rc == SOCKET_ERROR) { |
if (rc == SOCKET_ERROR) { |
907 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
908 |
goto leave; |
goto leave; |
909 |
} |
} |
910 |
|
|
911 |
rc = sock_read( conn_fd, key, maxkeylen, &n ); |
rc = sock_read_ext (conn_fd, r_key, r_keylen); |
912 |
if( rc == SOCKET_ERROR ) { |
if (rc == SOCKET_ERROR) { |
913 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
914 |
goto leave; |
goto leave; |
915 |
} |
} |
916 |
|
|
917 |
log_debug("%s\r\n", key); |
log_debug ("response:\r\n%s\r\n", *r_key); |
918 |
rc = check_hkp_response (key, 1); |
rc = check_hkp_response (*r_key, 1); |
919 |
if (rc) |
if (rc) |
920 |
goto leave; |
goto leave; |
921 |
|
|
923 |
|
|
924 |
leave: |
leave: |
925 |
closesocket (conn_fd); |
closesocket (conn_fd); |
|
free_if_alloc (request); |
|
926 |
return rc; |
return rc; |
927 |
} /* kserver_recvkey */ |
} |
928 |
|
|
929 |
|
|
930 |
/* Interface to send a public key. */ |
/* Interface to send a public key. */ |
931 |
int |
int |
932 |
kserver_sendkey (const char *hostname, u16 port, const char *pubkey, int len ) |
kserver_sendkey (const char *hostname, WORD port, |
933 |
|
const char *pubkey, size_t len) |
934 |
{ |
{ |
|
int n, rc; |
|
|
int conn_fd; |
|
935 |
char *request = NULL; |
char *request = NULL; |
936 |
char log[2048]; |
char log[2048] = {0}; |
937 |
|
int conn_fd, n; |
938 |
|
int rc; |
939 |
|
|
940 |
hkp_err = 0; /* reset */ |
hkp_has_err = 0; /* reset */ |
941 |
rc = kserver_connect (hostname, port, &conn_fd); |
rc = kserver_connect (hostname, port, &conn_fd); |
942 |
if (rc) |
if (rc) |
943 |
goto leave; |
goto leave; |
944 |
|
|
945 |
request = kserver_send_request (hostname, port, pubkey, len); |
request = kserver_send_request (hostname, port, pubkey, len); |
946 |
if (request == NULL) { |
rc = sock_write (conn_fd, request, strlen (request)); |
947 |
rc = WPTERR_GENERAL; |
if (rc == SOCKET_ERROR) { |
|
goto leave; |
|
|
} |
|
|
|
|
|
rc = sock_write( conn_fd, request, lstrlen(request) ); |
|
|
if( rc == SOCKET_ERROR ) { |
|
948 |
rc = WPTERR_WINSOCK_SENDKEY; |
rc = WPTERR_WINSOCK_SENDKEY; |
949 |
goto leave; |
goto leave; |
950 |
} |
} |
951 |
|
|
952 |
rc = sock_read( conn_fd, log, sizeof (log)-1, &n ); |
rc = sock_read (conn_fd, log, DIM (log)-1, &n); |
953 |
if( rc == SOCKET_ERROR ) { |
if (rc == SOCKET_ERROR) { |
954 |
rc = WPTERR_WINSOCK_SENDKEY; |
rc = WPTERR_WINSOCK_SENDKEY; |
955 |
goto leave; |
goto leave; |
956 |
} |
} |
957 |
|
|
958 |
log_debug ("kserver_sendkey:\r\n%s\r\n", log); |
log_debug ("kserver_sendkey: read %d bytes\r\n%s\r\n", n, log); |
|
|
|
959 |
rc = check_hkp_response (log, 0); |
rc = check_hkp_response (log, 0); |
960 |
if( rc ) |
if (rc) |
961 |
goto leave; |
goto leave; |
962 |
|
|
963 |
WSASetLastError (0); |
WSASetLastError (0); |
|
|
|
|
leave: |
|
|
closesocket (conn_fd); |
|
|
free_if_alloc (request); |
|
|
return rc; |
|
|
} /* kserver_sendkey */ |
|
964 |
|
|
|
|
|
|
int |
|
|
kserver_search_init (const char * hostname, u16 port, const char * keyid, int * conn_fd) |
|
|
{ |
|
|
int rc, sock_fd; |
|
|
int n=0; |
|
|
char * request = NULL; |
|
|
|
|
|
rc = kserver_connect (hostname, port, &sock_fd); |
|
|
if (rc) { |
|
|
*conn_fd = 0; |
|
|
goto leave; |
|
|
} |
|
|
|
|
|
n=300; |
|
|
request = new char[n+1]; |
|
|
if (!request) |
|
|
BUG (0); |
|
|
|
|
|
if (proxy.host && proxy.user) { |
|
|
_snprintf (request, n, |
|
|
"GET http://%s:%d/pks/lookup?op=index&search=%s HTTP/1.0\r\n" |
|
|
"Proxy-Authorization: Basic %s\r\n\r\n", |
|
|
skip_type_prefix (hostname), port, keyid, proxy.base64_user); |
|
|
} |
|
|
else if (proxy.host) { |
|
|
_snprintf (request, n, |
|
|
"GET http://%s:%d/pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n", |
|
|
skip_type_prefix (hostname), port, keyid); |
|
|
} |
|
|
else { |
|
|
_snprintf (request, n, |
|
|
"GET /pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n", keyid); |
|
|
} |
|
|
|
|
|
log_debug ("kserver_search_init:\r\n%s\r\n", request); |
|
|
|
|
|
if (sock_write (sock_fd, request, strlen (request)) == SOCKET_ERROR) { |
|
|
rc = WPTERR_GENERAL; |
|
|
goto leave; |
|
|
} |
|
|
|
|
|
*conn_fd = sock_fd; |
|
|
|
|
965 |
leave: |
leave: |
966 |
|
closesocket (conn_fd); |
967 |
free_if_alloc (request); |
free_if_alloc (request); |
968 |
return rc; |
return rc; |
969 |
} /* kserver_search_init */ |
} |
970 |
|
|
971 |
|
|
972 |
int |
/* Check keyserver response. */ |
973 |
|
static int |
974 |
kserver_search_chkresp (int fd) |
kserver_search_chkresp (int fd) |
975 |
{ |
{ |
976 |
char buf[128]; |
char buf[128]; |
977 |
int n=0; |
int n=0; |
978 |
|
|
979 |
/* parse response 'HTTP/1.0 500 OK' */ |
/* parse response 'HTTP/1.0 500 OK' */ |
980 |
if (sock_getline (fd, buf, 127, &n)) |
if (sock_getline (fd, buf, DIM (buf)-1, &n)) |
981 |
return WPTERR_KEYSERVER_NOTFOUND; |
return WPTERR_KEYSERVER_NOTFOUND; |
982 |
|
|
983 |
log_debug ("kserver_search_chkpresp: %s\r\n", buf); |
log_debug ("kserver_search_chkpresp: %s\r\n", buf); |
986 |
if (strncmp (buf+(8+1), "200", 3)) |
if (strncmp (buf+(8+1), "200", 3)) |
987 |
return WPTERR_KEYSERVER_NOTFOUND; |
return WPTERR_KEYSERVER_NOTFOUND; |
988 |
return 0; |
return 0; |
989 |
} /* kserver_search_chkresp */ |
} |
990 |
|
|
991 |
|
|
992 |
int |
/* End the keyserver search procedure. */ |
993 |
kserver_search (int fd, keyserver_key * key) |
void |
994 |
|
kserver_search_end (int conn_fd) |
995 |
{ |
{ |
996 |
char buf[1024], *p; |
log_debug ("kserver_search_end: fd=%d\r\n", conn_fd); |
997 |
int uidlen, nbytes, pos = 0; |
closesocket (conn_fd); |
998 |
|
} |
999 |
|
|
|
log_debug ("keyserver_search:\n"); |
|
|
|
|
|
if (sock_getline (fd, buf, sizeof (buf) - 1, &nbytes)) |
|
|
return WPTERR_GENERAL; |
|
|
|
|
|
log_debug ("%s\n", buf); |
|
|
|
|
|
if (!strncmp (buf, "pub", 3)) { |
|
|
int revoked = strstr (buf, "KEY REVOKED") != NULL? 1 : 0; |
|
|
key->bits = atol (buf+3); |
|
|
p = strchr (buf, '>'); |
|
|
if (!p) |
|
|
goto fail; |
|
|
pos = p - buf + 1; |
|
|
memcpy (key->keyid, buf + pos, 8); |
|
|
key->keyid[8] = '\0'; |
|
|
p = strstr (buf, "</a>"); |
|
|
if (!p) |
|
|
goto fail; |
|
|
pos = p - buf + 5; |
|
|
memcpy (key->date, buf + pos, 10); |
|
|
key->date[10] = '\0'; |
|
|
if (revoked) { |
|
|
strcpy (key->uid, "KEY REVOKED: not checked"); |
|
|
return 0; |
|
|
} |
|
|
pos += 10; |
|
|
p = buf + pos + 1; |
|
|
while (p && *p != '>') |
|
|
p++; |
|
|
p++; |
|
|
uidlen = strlen (p) - 10; |
|
|
memcpy (key->uid, p, uidlen); |
|
|
key->uid[uidlen] = '\0'; |
|
|
strcat (key->uid, ">"); |
|
|
p = strchr (key->uid, '&'); |
|
|
if (p) { |
|
|
key->uid[(p-key->uid)] = '<'; |
|
|
memmove (key->uid+(p-key->uid)+1, key->uid+(p-key->uid)+4, strlen (key->uid)-3); |
|
|
} |
|
|
return 0; |
|
|
} |
|
1000 |
|
|
1001 |
fail: |
/* Extract the amount of keys from the info record. */ |
1002 |
key->bits = 0; |
static size_t |
1003 |
memset (key, 0, sizeof *key); |
count_keys_in_response (char *buf) |
1004 |
return 0; |
{ |
1005 |
} /* kserver_search */ |
char *p; |
1006 |
|
int recno = 0; |
1007 |
|
size_t n = 0; |
1008 |
|
|
1009 |
|
/* info:1:4 */ |
1010 |
|
if (strncmp (buf, "info", 4)) |
1011 |
|
return 0; |
1012 |
|
p = strtok (buf, ":"); |
1013 |
|
while (p != NULL) { |
1014 |
|
recno++; |
1015 |
|
if (recno == 3) |
1016 |
|
n = atoi (p); |
1017 |
|
p = strtok (NULL, ":"); |
1018 |
|
} |
1019 |
|
return n; |
1020 |
|
} |
1021 |
|
|
1022 |
|
|
1023 |
static int |
/* Start the keyserver search. |
1024 |
add_node( keyserver_cfgfile *cfg, const char *entry ) |
Connect to host @hostname and port @port. |
1025 |
|
The pattern are given in @pattern. |
1026 |
|
The socket is returned in @conn_fd and @nkeys contains |
1027 |
|
the amount of keys which were found. */ |
1028 |
|
int |
1029 |
|
kserver_search_begin (const char *hostname, WORD port, |
1030 |
|
const char *pattern, int *conn_fd, size_t *nkeys) |
1031 |
{ |
{ |
|
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 */ |
|
|
|
|
1032 |
|
|
1033 |
void |
StringBuffer req; |
1034 |
kserver_change_proxy( keyserver_proxy_ctx *ctx ) |
const char *reqbuf; |
1035 |
{ |
char *enc_patt = NULL; |
1036 |
proxy.port = ctx->port; |
char status[128]; |
1037 |
free_if_alloc (proxy.host); |
int rc, sock_fd; |
1038 |
proxy.host = ctx->host? m_strdup( ctx->host ) : NULL; |
int nread; |
1039 |
free_if_alloc( proxy.pass ); |
|
1040 |
proxy.pass = ctx->pass? m_strdup( ctx->pass ) : NULL; |
*conn_fd = 0; |
|
free_if_alloc( proxy.user ); |
|
|
proxy.user = ctx->user? m_strdup( ctx->user ) : NULL; |
|
|
set_reg_proxy_prefs( proxy.host, proxy.port, proxy.user, proxy.pass ); |
|
|
} /* kserver_change_proxy */ |
|
1041 |
|
|
1042 |
|
rc = kserver_connect (hostname, port, &sock_fd); |
1043 |
|
if (rc) |
1044 |
|
goto leave; |
1045 |
|
|
1046 |
int |
enc_patt = URL_encode (pattern, strlen (pattern), NULL); |
1047 |
kserver_read_config( const char *fname, keyserver_cfgfile **ret_cfg ) |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
1048 |
{ |
req = req + "GET http://" + skip_type_prefix (hostname) + ":" + port + |
1049 |
FILE *fp; |
"/pks/lookup?options=mr&op=index&search=" + enc_patt + |
1050 |
keyserver_cfgfile *cfg; |
" HTTP/1.0\r\n" + |
1051 |
char buf[256]; |
"Host: " + skip_type_prefix (hostname) + ":" + port+ "\r\n"+ |
1052 |
int rc = 0; |
"Connection: close\r\n" + |
1053 |
|
"Proxy-Authorization: Basic " + proxy.base64_user + "\r\n\r\n"; |
1054 |
*ret_cfg = NULL; |
} |
1055 |
fp = fopen( fname, "rb" ); |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
1056 |
if( fp == NULL ) |
req = req + "GET http://" + skip_type_prefix (hostname) + ":" + port + |
1057 |
return WPTERR_FILE_OPEN; |
"/pks/lookup?options=mr&op=index&search=" + enc_patt + |
1058 |
|
" HTTP/1.0\r\n"+ |
1059 |
cfg = new keyserver_cfgfile; |
"Host: " + skip_type_prefix (hostname) + ":" + port+ "\r\n"+ |
1060 |
if ( !cfg ) |
"Connection: close\r\n"+ |
1061 |
BUG( NULL ); |
"\r\n"; |
1062 |
memset( cfg, 0, sizeof *cfg ); |
} |
1063 |
|
else { |
1064 |
while ( !feof( fp ) ) { |
req = req + "GET /pks/lookup?options=mr&op=index&search=" + |
1065 |
if ( fgets( buf, sizeof(buf)-1, fp ) == NULL ) |
enc_patt + " HTTP/1.0\r\n" + |
1066 |
break; |
"Host: " + skip_type_prefix (hostname) + ":" + port+ "\r\n"+ |
1067 |
if ( *buf == '\r' || *buf == '\n' || *buf == '#' ) |
"Connection: close\r\n"+ |
1068 |
continue; |
"\r\n"; |
|
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++; |
|
|
} |
|
1069 |
} |
} |
1070 |
|
|
1071 |
fclose( fp ); |
log_debug ("kserver_search_begin:\r\n%s\r\n", req.getBuffer ()); |
1072 |
*ret_cfg = cfg; |
reqbuf = req.getBuffer (); |
1073 |
|
if (sock_write (sock_fd, reqbuf, strlen (reqbuf)) == SOCKET_ERROR) { |
1074 |
|
rc = WPTERR_GENERAL; |
1075 |
|
goto leave; |
1076 |
|
} |
1077 |
|
|
1078 |
return rc; |
rc = kserver_search_chkresp (sock_fd); |
1079 |
} /* kserver_read_config */ |
if (rc) { |
1080 |
|
closesocket (sock_fd); |
1081 |
|
sock_fd = 0; |
1082 |
|
goto leave; |
1083 |
|
} |
1084 |
|
|
1085 |
|
/* Skip all lines until we reach the "info:" record. */ |
1086 |
|
for (;;) { |
1087 |
|
if (sock_getline (sock_fd, status, DIM (status)-1, &nread)) { |
1088 |
|
log_debug ("kserver_search_begin: retrieving status line failed.\r\n"); |
1089 |
|
closesocket (sock_fd); |
1090 |
|
sock_fd = 0; |
1091 |
|
rc = WPTERR_GENERAL; |
1092 |
|
break; |
1093 |
|
} |
1094 |
|
if (!strncmp (status, "info:", 5)) |
1095 |
|
break; |
1096 |
|
} |
1097 |
|
|
1098 |
int |
if (!rc) |
1099 |
kserver_write_config( const char * fname, keyserver_cfgfile * cfg ) |
*nkeys = count_keys_in_response (status); |
1100 |
{ |
*conn_fd = sock_fd; |
1101 |
|
|
1102 |
return 0; |
leave: |
1103 |
|
free_if_alloc (enc_patt); |
1104 |
|
return rc; |
1105 |
} |
} |
1106 |
|
|
1107 |
|
|
1108 |
void |
/* Parse a single pub record returned by the keyserver. */ |
1109 |
kserver_cfgfile_release( keyserver_cfgfile *cfg ) |
static void |
1110 |
{ |
parse_pub_record (keyserver_key_s *key, char *buf) |
1111 |
keyserver_node *k, *k2; |
{ |
1112 |
|
enum pub_rec_t {ID=1, KEYID, ALGO, SIZE, CREATE, EXPIRE}; |
1113 |
|
char *p; |
1114 |
|
int recno = 0; |
1115 |
|
int off = 0; |
1116 |
|
|
1117 |
|
/* pub:{BF3DF9B4, ED4681C9BF3DF9B4, FPR}:17:1024:925411133:: */ |
1118 |
|
p = strtok (buf, ":"); |
1119 |
|
while (p != NULL) { |
1120 |
|
recno++; |
1121 |
|
switch (recno) { |
1122 |
|
case ID: |
1123 |
|
break; |
1124 |
|
|
1125 |
|
case KEYID: |
1126 |
|
/* If for any reason the returned record actually contains |
1127 |
|
a fingerprint instead of a key ID, we truncate the fpr. */ |
1128 |
|
off = strlen (p) == 40? 32 : 0; |
1129 |
|
free_if_alloc (key->keyid); |
1130 |
|
key->keyid = m_strdup (p+off); |
1131 |
|
break; |
1132 |
|
|
1133 |
proxy_release( &cfg->proxy ); |
case ALGO: |
1134 |
for( k = cfg->list; cfg->nlist--; k = k2 ) { |
key->algo = atoi (p); |
1135 |
k2 = k->next; |
break; |
1136 |
free_if_alloc( k ); |
|
1137 |
|
case SIZE: |
1138 |
|
key->bits = atoi (p); |
1139 |
|
break; |
1140 |
|
|
1141 |
|
case CREATE: |
1142 |
|
key->creation = strtoul (p, NULL, 10); |
1143 |
|
break; |
1144 |
|
|
1145 |
|
case EXPIRE: |
1146 |
|
key->expires = strtoul (p, NULL, 10); |
1147 |
|
break; |
1148 |
|
} |
1149 |
|
p = strtok (NULL, ":"); |
1150 |
} |
} |
1151 |
free_if_alloc( cfg ); |
} |
|
} /* kserver_cfgfile_release */ |
|
1152 |
|
|
1153 |
|
|
1154 |
void |
/* Parse a single user id returned by the keyserver. */ |
1155 |
proxy_release( keyserver_proxy_ctx *ctx ) |
static void |
1156 |
|
parse_uid_record (keyserver_key_s *key, char *buf) |
1157 |
{ |
{ |
1158 |
free_if_alloc( ctx->host ); |
enum uid_rec_t {ID=1, UID, CREATE, EXPIRE}; |
1159 |
free_if_alloc( ctx->pass ); |
keyserver_uid_s *u, *n; |
1160 |
free_if_alloc( ctx->user ); |
char *p, *raw; |
1161 |
} /* proxy_release */ |
int recno = 0; |
1162 |
|
|
1163 |
|
/* uid:Timo Schulz <[email protected]>:1138440360:: */ |
1164 |
int |
u = new keyserver_uid_s; |
1165 |
ldap_recvkey (const char * host, const char * keyid, char * key, int maxkeylen) |
memset (u, 0, sizeof *u); |
1166 |
{ |
|
1167 |
const char *s; |
p = strtok (buf, ":"); |
1168 |
char *ksprg = NULL, *p = NULL; |
while (p != NULL) { |
1169 |
char temp[512], outf[512]; |
recno++; |
1170 |
FILE * inp; |
|
1171 |
int rc, start_key = 0; |
switch (recno) { |
1172 |
STARTUPINFO si; |
case ID: |
|
PROCESS_INFORMATION pi; |
|
|
|
|
|
p = get_gnupg_path (); |
|
|
ksprg = new char[strlen (p) + 1 + 128]; |
|
|
if (!ksprg) |
|
|
BUG (0); |
|
|
strcpy (ksprg, p); |
|
|
strcat (ksprg, "\\"); |
|
|
strcat (ksprg, "gpgkeys_ldap.exe"); |
|
|
free_if_alloc (p); |
|
|
if (file_exist_check (ksprg)) { |
|
|
log_box ( "LDAP Keyserver Plugin", MB_ERR, "Could not find LDAP keyserver module!"); |
|
|
rc = -1; |
|
|
goto leave; |
|
|
} |
|
|
GetTempPath (sizeof (temp)-1, temp); |
|
|
strcpy (outf, temp); |
|
|
strcat (outf, keyid); |
|
|
strcat (outf, ".out"); |
|
|
strcat (temp, keyid); |
|
|
inp = fopen (temp, "w+b"); |
|
|
if( !inp ) { |
|
|
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", temp, |
|
|
winpt_strerror (WPTERR_FILE_OPEN)); |
|
|
rc = -1; |
|
|
goto leave; |
|
|
} |
|
|
fprintf (inp, |
|
|
"VERSION 0\n" |
|
|
"HOST %s\n" |
|
|
"OPTION verbose\n" |
|
|
"COMMAND GET\n" |
|
|
"\n" |
|
|
"%s\n", host? skip_type_prefix (host): "64.94.85.200", keyid); |
|
|
fclose (inp); |
|
|
|
|
|
memset (&si, 0, sizeof (si)); |
|
|
si.cb = sizeof (si); |
|
|
si.dwFlags = STARTF_USESHOWWINDOW; |
|
|
si.wShowWindow = SW_HIDE; |
|
|
memset (&pi, 0, sizeof (pi)); |
|
|
p = new char[strlen (ksprg) + strlen (temp) + strlen (outf) + 32]; |
|
|
sprintf (p, "%s -o %s %s", ksprg, outf, temp); |
|
|
rc = CreateProcess (NULL, p, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi); |
|
|
if (!rc) { |
|
|
log_box ("LDAP Keyserver Plugin", MB_ERR, "Could not spawn process"); |
|
|
rc = -1; |
|
|
goto leave; |
|
|
} |
|
|
CloseHandle (pi.hThread); |
|
|
WaitForSingleObject (pi.hProcess, INFINITE); |
|
|
CloseHandle (pi.hProcess); |
|
|
|
|
|
inp = fopen (outf, "rb"); |
|
|
if (!inp) { |
|
|
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf, |
|
|
winpt_strerror (WPTERR_FILE_OPEN)); |
|
|
rc = -1; |
|
|
goto leave; |
|
|
} |
|
|
memset (key, 0, maxkeylen); |
|
|
while( !feof( inp ) ) { |
|
|
s = fgets( temp, sizeof (temp)-1, inp ); |
|
|
if( !s ) |
|
1173 |
break; |
break; |
1174 |
if( !start_key && strstr( s, "KEY" ) && strstr( s, "BEGIN" ) ) { |
|
1175 |
start_key = 1; |
case UID: |
1176 |
continue; |
unhexify_buffer (p, &raw); |
1177 |
} |
u->uid = utf8_to_native (raw); |
1178 |
if( !start_key ) |
free_if_alloc (raw); |
|
continue; |
|
|
strcat( key, temp ); |
|
|
maxkeylen -= strlen( temp ); |
|
|
if( maxkeylen < 0 || (strstr( s, "KEY" ) && strstr( s, "END" )) ) |
|
1179 |
break; |
break; |
|
} |
|
|
fclose( inp ); |
|
1180 |
|
|
1181 |
leave: |
case CREATE: |
1182 |
if( !strlen( key ) ) |
u->creation = strtoul (p, NULL, 10); |
1183 |
rc = WPTERR_WINSOCK_RECVKEY; |
break; |
1184 |
free_if_alloc( p ); |
|
1185 |
free_if_alloc( ksprg ); |
case EXPIRE: |
1186 |
return rc; |
u->expires = strtoul (p, NULL, 10); |
1187 |
} /* ldap_recvkey */ |
break; |
1188 |
|
} |
1189 |
|
|
1190 |
|
p = strtok (NULL, ":"); |
1191 |
|
} |
1192 |
|
if (!key->uids) |
1193 |
|
key->uids = u; |
1194 |
|
else { |
1195 |
|
for (n = key->uids; n->next; n=n->next) |
1196 |
|
; |
1197 |
|
n->next = u; |
1198 |
|
} |
1199 |
|
} |
1200 |
|
|
1201 |
|
|
1202 |
|
/* Peek the next 3 bytes to check if the next line |
1203 |
|
would be a new "pub" record. In this case, return |
1204 |
|
-1 as an EOF indication, 0 otherwise. */ |
1205 |
static int |
static int |
1206 |
finger_readline (int fd, char * buf, int nbytes) |
is_key_eof (int fd) |
1207 |
{ |
{ |
1208 |
char c = 0; |
char buf[64]; |
1209 |
int n, pos = 0; |
int n; |
1210 |
|
|
1211 |
while (nbytes > 0) { |
n = recv (fd, buf, 3, MSG_PEEK); |
1212 |
n = recv (fd, &c, 1, 0); |
if (n < 3 || strncmp (buf, "pub", 3)) |
1213 |
if (n <= 0) |
return 0; |
1214 |
break; |
return -1; |
|
if (c == '\n') |
|
|
break; |
|
|
buf[pos++] = c; |
|
|
nbytes--; |
|
|
} |
|
|
if (nbytes > 0) |
|
|
buf[pos++] = '\0'; |
|
|
if (n <= 0) |
|
|
pos = n; |
|
|
return pos; |
|
1215 |
} |
} |
1216 |
|
|
1217 |
|
|
1218 |
|
/* Return the next key from the search response. */ |
1219 |
int |
int |
1220 |
finger_recvkey (const char * host, const char * user, char * key, int maxkeylen) |
kserver_search_next (int fd, keyserver_key_s **r_key) |
1221 |
{ |
{ |
1222 |
struct hostent * hp; |
keyserver_uid_s *uid, *u = NULL; |
1223 |
struct sockaddr_in saddr; |
keyserver_key_s *key; |
1224 |
char buf[128]; |
char buf[512]; |
1225 |
int fd, nread; |
int n = 0; |
1226 |
int start_key = 0; |
long max = 0; |
1227 |
int rc=0; |
|
1228 |
|
log_debug ("keyserver_search_next:\r\n"); |
1229 |
fd = socket (PF_INET, SOCK_STREAM, 0); |
*r_key = NULL; |
1230 |
if (fd == -1) |
|
1231 |
return WPTERR_WINSOCK_SOCKET; |
key = new keyserver_key_s; |
1232 |
hp = gethostbyname (skip_type_prefix (host)); |
memset (key, 0, sizeof *key); |
1233 |
if (!hp) { |
for (;;) { |
1234 |
closesocket (fd); |
if (sock_getline (fd, buf, DIM (buf)-1, &n)) |
1235 |
return WPTERR_WINSOCK_RESOLVE; |
break; |
1236 |
|
/*log_debug ("record: '%s'\r\n", buf); */ |
1237 |
|
if (!strncmp (buf, "pub", 3)) |
1238 |
|
parse_pub_record (key, buf); |
1239 |
|
else |
1240 |
|
parse_uid_record (key, buf); |
1241 |
|
if (is_key_eof (fd)) |
1242 |
|
break; |
1243 |
} |
} |
1244 |
|
|
1245 |
memset (&saddr, 0, sizeof (saddr)); |
/* the uid with the newest self sig is used as the |
1246 |
saddr.sin_family = AF_INET; |
primary user id. */ |
1247 |
saddr.sin_port = htons (FINGER_PORT); |
for (uid = key->uids; uid; uid = uid->next) { |
1248 |
saddr.sin_addr = *(struct in_addr *)hp->h_addr; |
if (uid->creation > max) { |
1249 |
if (connect (fd, (struct sockaddr *)&saddr, sizeof (saddr))) { |
max = uid->creation; |
1250 |
closesocket (fd); |
u = uid; |
|
return WPTERR_WINSOCK_CONNECT; |
|
|
} |
|
|
send (fd, user, strlen (user), 0); |
|
|
send (fd, "\r\n", 2, 0); |
|
|
|
|
|
memset (key, 0, maxkeylen); |
|
|
while (maxkeylen > 0) { |
|
|
nread = finger_readline (fd, buf, sizeof (buf)-1); |
|
|
if (nread <= 0) |
|
|
break; |
|
|
strcat (buf, "\n"); |
|
|
if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) { |
|
|
strcat (key, buf); |
|
|
start_key = 1; |
|
|
maxkeylen -= nread; |
|
|
} |
|
|
else if (strstr (buf, "END PGP PUBLIC KEY BLOCK" ) && start_key) { |
|
|
strcat (key, buf); |
|
|
start_key--; |
|
|
maxkeylen -= nread; |
|
|
break; |
|
|
} |
|
|
else if (start_key) { |
|
|
strcat (key, buf); |
|
|
maxkeylen -= nread; |
|
1251 |
} |
} |
1252 |
} |
} |
1253 |
closesocket (fd); |
|
1254 |
if (start_key != 0) |
key->main_uid = u? u : key->uids; |
1255 |
rc = WPTERR_WINSOCK_RECVKEY; |
*r_key = key; |
1256 |
return rc; |
return 0; |
1257 |
|
} |
1258 |
|
|
1259 |
|
|
1260 |
|
/* Release the keyserver key @key and all its elements. */ |
1261 |
|
void |
1262 |
|
kserver_release_key (keyserver_key_s *key) |
1263 |
|
{ |
1264 |
|
keyserver_uid_s *u; |
1265 |
|
|
1266 |
|
while (key->uids) { |
1267 |
|
u = key->uids->next; |
1268 |
|
free_if_alloc (key->uids->uid); |
1269 |
|
free_if_alloc (key->uids); |
1270 |
|
key->uids = u; |
1271 |
|
} |
1272 |
|
free_if_alloc (key->keyid); |
1273 |
|
free_if_alloc (key); |
1274 |
|
} |
1275 |
|
|
1276 |
|
|
1277 |
|
/* Release mbmers in the proxy context @ctx. */ |
1278 |
|
void |
1279 |
|
kserver_proxy_release (keyserver_proxy_t ctx) |
1280 |
|
{ |
1281 |
|
free_if_alloc (ctx->host); |
1282 |
|
free_if_alloc (ctx->pass); |
1283 |
|
free_if_alloc (ctx->user); |
1284 |
|
ctx->port = ctx->proto = 0; |
1285 |
} |
} |
1286 |
|
|
1287 |
|
|
1288 |
|
/* Check if the given name @name is a valid inernet address |
1289 |
|
which means an dotted IP or a valid DNS name. |
1290 |
|
Return value: 0 on success. */ |
1291 |
int |
int |
1292 |
check_IP_or_hostname (const char *name) |
check_inet_address (const char *addr) |
1293 |
{ |
{ |
1294 |
const char *not_allowed = "=!�$%&@#*~\\/}][{<>|,;:'"; |
const char *not_allowed = "=!�$%&@#*~\\/}][{<>|,;:'"; |
|
size_t i, j; |
|
1295 |
|
|
1296 |
for (i=0; i < strlen (name); i++) { |
for (size_t i=0; i < strlen (addr); i++) { |
1297 |
for (j =0; j < strlen (not_allowed); j++) { |
if (strchr (not_allowed, addr[i])) |
1298 |
if (name[i] == not_allowed[j]) |
return -1; |
|
return -1; |
|
|
} |
|
1299 |
} |
} |
1300 |
return 0; |
return 0; |
1301 |
} |
} |
1302 |
|
|
1303 |
|
|
1304 |
|
/* Split the URL @r_keyserver into the host and the port |
1305 |
|
part if possible. */ |
1306 |
|
gpgme_error_t |
1307 |
|
parse_keyserver_url (char **r_keyserver, unsigned short *r_port) |
1308 |
|
{ |
1309 |
|
char *p; |
1310 |
|
char *url = *r_keyserver; |
1311 |
|
int off; |
1312 |
|
|
1313 |
|
/* no port is given so use the default port. */ |
1314 |
|
p = strrchr (url, ':'); |
1315 |
|
if (p == strchr (url, ':')) { |
1316 |
|
*r_port = HKP_PORT; |
1317 |
|
return 0; |
1318 |
|
} |
1319 |
|
|
1320 |
|
if (url[(p-url)-1] == '/') /* remove / in .de/:11371 */ |
1321 |
|
off = 1; |
1322 |
|
else |
1323 |
|
off = 0; |
1324 |
|
|
1325 |
|
*r_keyserver = substr (url, 0, (p-url)-off); |
1326 |
|
*r_port = atoi (url+(p-url)+1); |
1327 |
|
free_if_alloc (url); |
1328 |
|
return 0; |
1329 |
|
} |