1 |
/* wptKeyserver.cpp - W32 Keyserver Access |
/* wptKeyserver.cpp - W32 Keyserver Access |
2 |
* Copyright (C) 2000-2005 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 |
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 |
|
/* Map net_errno to a winsock error. */ |
40 |
|
#define net_errno ((int)WSAGetLastError ()) |
41 |
|
|
|
static char base64code[] = |
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
|
42 |
|
|
43 |
keyserver server[MAX_KEYSERVERS] = {0}; |
keyserver server[MAX_KEYSERVERS] = {0}; |
44 |
static keyserver_proxy_ctx proxy = {0}; |
keyserver_proxy_s proxy = {0}; |
45 |
static const char * server_list[] = { |
static const char *server_list[] = { |
46 |
"hkp://wwwkeys.nl.pgp.net", |
"hkp://sks.keyserver.penguin.de", |
|
"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", |
|
47 |
"hkp://subkeys.pgp.net", |
"hkp://subkeys.pgp.net", |
48 |
"ldap://keyserver.pgp.com", |
"ldap://keyserver.pgp.com", |
49 |
NULL |
NULL |
50 |
}; |
}; |
|
static char hkp_errmsg[1024]; |
|
|
static int hkp_err = 0; |
|
|
static u32 conf_timestamp = 0; |
|
|
char * default_keyserver = NULL; |
|
|
unsigned short default_keyserver_port = 0; |
|
51 |
|
|
52 |
|
|
53 |
|
static char hkp_errmsg[1024]; /* Holds the error message from the server */ |
54 |
|
static int hkp_err = 0; /* != 0 indicates an error occurred. */ |
55 |
|
|
56 |
|
/* Default keyserver and port. */ |
57 |
|
char *default_keyserver = NULL; |
58 |
|
WORD default_keyserver_port = 0; |
59 |
|
|
60 |
|
/* Default socket timeout (secs). */ |
61 |
|
static size_t default_socket_timeout = 6; |
62 |
|
|
63 |
|
|
64 |
|
/* Wrapper for safe memory allocation. */ |
65 |
|
static char* |
66 |
|
safe_alloc (DWORD n) |
67 |
|
{ |
68 |
|
char *p; |
69 |
|
|
70 |
|
p = new char[n+1]; |
71 |
|
if (!p) |
72 |
|
BUG (0); |
73 |
|
memset (p, 0, n); |
74 |
|
return p; |
75 |
|
} |
76 |
|
|
77 |
|
/* Basic64 encode the input @inbuf to @outbuf. */ |
78 |
static void |
static void |
79 |
base64_encode( const char *inputstr, char *outputstr, int maxlen ) |
base64_encode (const char *inbuf, char *outbuf) |
80 |
{ |
{ |
81 |
int index = 0, temp = 0, res = 0, i = 0, inputlen = 0, len = 0; |
char base64code[] = |
82 |
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
83 |
|
int index = 0, temp = 0, res = 0; |
84 |
|
int i, inputlen, len = 0; |
85 |
|
|
86 |
inputlen = strlen (inputstr); /* fixme: check if len > maxlen */ |
inputlen = strlen (inbuf); |
87 |
for (i = 0; i <inputlen; i++) { |
for (i = 0; i < inputlen; i++) { |
88 |
res = temp; |
res = temp; |
89 |
res = (res << 8) | (inputstr[i] & 0xFF); |
res = (res << 8) | (inbuf[i] & 0xFF); |
90 |
switch (index++) { |
switch (index++) { |
91 |
case 0: outputstr[len++] = base64code[res >> 2 & 0x3F]; res &= 0x3; break; |
case 0: |
92 |
case 1: outputstr[len++] = base64code[res >> 4 & 0x3F]; res &= 0xF; break; |
outbuf[len++] = base64code[res >> 2 & 0x3F]; |
93 |
case 2: outputstr[len++] = base64code[res >> 6 & 0x3F]; |
res &= 0x3; |
94 |
outputstr[len++] = base64code[res & 0x3F]; res = index = 0; break; |
break; |
95 |
|
case 1: |
96 |
|
outbuf[len++] = base64code[res >> 4 & 0x3F]; |
97 |
|
res &= 0xF; |
98 |
|
break; |
99 |
|
case 2: |
100 |
|
outbuf[len++] = base64code[res >> 6 & 0x3F]; |
101 |
|
outbuf[len++] = base64code[res & 0x3F]; |
102 |
|
res = index = 0; |
103 |
|
break; |
104 |
} |
} |
105 |
temp = res; |
temp = res; |
106 |
} |
} |
107 |
|
|
108 |
switch( index ) { |
if (index == 2) { |
109 |
case 0: break; |
outbuf[len++] = base64code[temp << 2 & 0x3F]; |
110 |
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++] = '='; |
|
111 |
} |
} |
112 |
|
else if (index == 1) { |
113 |
outputstr[len] = '\0'; |
outbuf[len++] = base64code[temp << 4 & 0x3F]; |
114 |
} /* base64_encode */ |
outbuf[len++] = '='; |
115 |
|
outbuf[len++] = '='; |
116 |
|
} |
117 |
|
|
118 |
|
outbuf[len] = '\0'; |
119 |
|
} |
120 |
|
|
121 |
|
|
122 |
|
/* Check that the given buffer contains a valid keyserver URL |
123 |
|
and return the prefix length, 0 in case of an error. */ |
124 |
|
static int |
125 |
|
check_URL (const char *buf) |
126 |
|
{ |
127 |
|
const char *proto[] = { |
128 |
|
"ldap://", |
129 |
|
"http://", |
130 |
|
"finger://", |
131 |
|
"hkp://", |
132 |
|
NULL}; |
133 |
|
int i; |
134 |
|
|
135 |
|
if (strlen (buf) < 7) |
136 |
|
return 0; |
137 |
|
|
138 |
|
for (i=0; proto[i] != NULL; i++) { |
139 |
|
if (strstr (buf, proto[i])) |
140 |
|
return strlen (proto[i]); |
141 |
|
} |
142 |
|
return 0; |
143 |
|
} |
144 |
|
|
145 |
|
|
146 |
/* Skip the URL schema and return only the host part of it. */ |
/* Skip the URL schema and return only the host part of it. */ |
147 |
static const char * |
static const char* |
148 |
skip_type_prefix (const char * hostname) |
skip_type_prefix (const char *hostname) |
149 |
{ |
{ |
150 |
if (hostname && !strncmp (hostname, "http://", 7)) |
int pos; |
151 |
hostname += 7; |
|
152 |
else if (hostname && !strncmp (hostname, "hkp://", 6)) |
if (!hostname) |
153 |
hostname += 6; |
return hostname; |
154 |
else if (hostname && !strncmp (hostname, "finger://", 9)) |
|
155 |
hostname += 9; |
pos = check_URL (hostname); |
156 |
else if (hostname && !strncmp (hostname, "ldap://", 7)) |
if (pos > 0) |
157 |
hostname += 7; |
hostname += pos; |
158 |
return hostname; |
return hostname; |
159 |
} |
} |
160 |
|
|
161 |
|
/* Parse the keyserver response and extract the human |
162 |
|
readable text passages. */ |
163 |
|
static int |
164 |
|
parse_keyserver_error (const char *resp, char *txt, size_t txtlen) |
165 |
|
{ |
166 |
|
char *p, *p2; |
167 |
|
|
168 |
|
/* If we find no 'Error' substring, we assume a success. */ |
169 |
|
if (!stristr (resp, "Error")) |
170 |
|
return -1; |
171 |
|
|
172 |
|
memset (txt, 0, txtlen); |
173 |
|
p = strstr (resp, "\r\n\r\n"); |
174 |
|
if (!p) |
175 |
|
return -1; |
176 |
|
resp += (p-resp); |
177 |
|
p = strstr (resp, "<h1>"); |
178 |
|
if (!p) |
179 |
|
return -1; |
180 |
|
resp += (p-resp)+strlen ("<h1>"); |
181 |
|
p = strstr (resp, "</h1>"); |
182 |
|
if (!p) |
183 |
|
return -1; |
184 |
|
resp += (p-resp)+strlen ("</h1>"); |
185 |
|
p2 = strstr (resp, "</body>"); |
186 |
|
if (p2 != NULL) |
187 |
|
memcpy (txt, resp, (p2-resp)); |
188 |
|
else { |
189 |
|
if (!strnicmp (resp, "<p>", 3)) |
190 |
|
resp += 3; |
191 |
|
while (resp && *resp == '\r' || *resp == '\n') |
192 |
|
resp++; |
193 |
|
memcpy (txt, resp, strlen (resp)); |
194 |
|
} |
195 |
|
return 0; |
196 |
|
} |
197 |
|
|
198 |
|
|
199 |
/* Check that the keyserver response indicates an OK. |
/* Check that the keyserver response indicates an OK. |
200 |
Return 0 on success. */ |
Return 0 on success. */ |
201 |
static int |
static int |
202 |
check_hkp_response (const char *resp, int recv) |
check_hkp_response (const char *resp, int recv) |
203 |
{ |
{ |
204 |
int ec, len; |
int ec; |
|
char *p, * end; |
|
205 |
|
|
206 |
|
log_debug ("check_hkp_response: '%s'\r\n", resp); |
207 |
ec = recv ? WPTERR_WINSOCK_RECVKEY : WPTERR_WINSOCK_SENDKEY; |
ec = recv ? WPTERR_WINSOCK_RECVKEY : WPTERR_WINSOCK_SENDKEY; |
208 |
if (!strstr (resp, "HTTP/1.0 200 OK") && |
if (!strstr (resp, "HTTP/1.0 200 OK") && |
209 |
!strstr (resp, "HTTP/1.1 200 OK")) /* http error */ |
!strstr (resp, "HTTP/1.1 200 OK") && |
210 |
return ec; |
!strstr (resp, "HTTP/1.0 500 OK") && |
211 |
if (strstr (resp, "Public Key Server -- Error") |
!strstr (resp, "HTTP/1.1 500 OK")) |
212 |
|| strstr (resp, "Public Key Server -- Error") |
return ec; /* http error */ |
213 |
|| strstr (resp, "No matching keys in database")) { |
|
214 |
p = strstr (resp, "<p>"); |
if (!parse_keyserver_error (resp, hkp_errmsg, DIM (hkp_errmsg)-2)) { |
215 |
if (p && strlen (p) < sizeof (hkp_errmsg)) { |
if (!strlen (hkp_errmsg)) |
216 |
end = strstr (p, "</p>"); |
_snprintf (hkp_errmsg, DIM (hkp_errmsg)-1, |
217 |
len = end? (end - p + 1) : strlen (p); |
"Unknown keyserver error"); |
218 |
strncpy (hkp_errmsg, p, len); |
hkp_err = 1; |
|
hkp_err = 1; |
|
|
} |
|
219 |
return ec; |
return ec; |
220 |
} |
} |
221 |
return 0; |
return 0; |
228 |
sock_getline (int fd, char *buf, int buflen, int *nbytes) |
sock_getline (int fd, char *buf, int buflen, int *nbytes) |
229 |
{ |
{ |
230 |
char ch; |
char ch; |
231 |
int count = 0; |
int nread = 0; |
232 |
|
|
233 |
if (nbytes) |
if (nbytes) |
234 |
*nbytes = 0; |
*nbytes = 0; |
235 |
*buf = 0; |
*buf = 0; |
236 |
while (recv (fd, &ch, 1, 0) > 0) { |
while (recv (fd, &ch, 1, 0) > 0) { |
237 |
*buf++ = ch; |
*buf++ = ch; |
238 |
count++; |
nread++; |
239 |
if (ch == '\n' || count == (buflen - 1)) { |
if (ch == '\r') |
240 |
|
continue; /* remove this char. */ |
241 |
|
if (ch == '\n' || nread == (buflen - 1)) { |
242 |
*buf = 0; |
*buf = 0; |
243 |
if (nbytes) |
if (nbytes) |
244 |
*nbytes = count; |
*nbytes = nread; |
245 |
return 0; |
return 0; |
246 |
} |
} |
247 |
} |
} |
260 |
|
|
261 |
FD_ZERO (&rfd); |
FD_ZERO (&rfd); |
262 |
FD_SET (fd, &rfd); |
FD_SET (fd, &rfd); |
263 |
if (select (fd + 1, &rfd, NULL, NULL, &tv) == SOCKET_ERROR) |
if (select (fd + 1, &rfd, NULL, NULL, &tv) == SOCKET_ERROR) { |
264 |
|
log_debug ("sock_select: select() failed ec=%d.\r\n", net_errno); |
265 |
return SOCKET_ERROR; |
return SOCKET_ERROR; |
266 |
|
} |
267 |
if (FD_ISSET (fd, &rfd)) |
if (FD_ISSET (fd, &rfd)) |
268 |
return 1; |
return 1; |
269 |
return 0; |
return 0; |
270 |
} |
} |
271 |
|
|
272 |
|
|
273 |
/* Read from a socket with a fixed timeout of 10 seconds. |
/* Read from a socket @fd to buffer @buf with a fixed timeout |
274 |
|
of 10 seconds. The amount of bytes which were read are |
275 |
|
returned in @nbytes. |
276 |
Return value: 0 on success. */ |
Return value: 0 on success. */ |
277 |
static int |
static int |
278 |
sock_read (int fd, char *buf, int buflen, int *nbytes) |
sock_read (int fd, char *buf, size_t buflen, int *nbytes) |
279 |
{ |
{ |
280 |
DWORD nread; |
int nread; |
281 |
int nleft = buflen; |
size_t nleft = buflen; |
282 |
int rc, n = 0; |
size_t n = 0; |
283 |
|
int rc; |
284 |
|
|
285 |
|
if (nbytes) |
286 |
|
*nbytes = 0; |
287 |
while (nleft > 0) { |
while (nleft > 0) { |
288 |
if (n >= 10) |
if (n >= default_socket_timeout) |
289 |
return WPTERR_WINSOCK_TIMEOUT; |
return WPTERR_WINSOCK_TIMEOUT; |
290 |
rc = sock_select (fd, 1); |
rc = sock_select (fd, 1); |
291 |
if (rc == SOCKET_ERROR) |
if (rc == SOCKET_ERROR) |
292 |
return SOCKET_ERROR; |
return rc; |
293 |
else if( !rc ) |
else if (!rc) |
294 |
n++; |
n++; |
295 |
else { |
else { |
296 |
nread = recv (fd, buf, nleft, 0); |
nread = recv (fd, buf, nleft, 0); |
297 |
if (nread == SOCKET_ERROR) |
if (nread == SOCKET_ERROR) { |
298 |
|
log_debug ("sock_read: recv() failed ec=%d.\r\n", net_errno); |
299 |
return SOCKET_ERROR; |
return SOCKET_ERROR; |
300 |
|
} |
301 |
else if (!nread) |
else if (!nread) |
302 |
break; |
break; |
303 |
nleft -= nread; |
nleft -= nread; |
311 |
} |
} |
312 |
|
|
313 |
|
|
314 |
/* Write @buf to a socket @fd. */ |
/* Helper to create a string from the gpgme data and |
315 |
|
then release the gpgme data object. */ |
316 |
|
char* |
317 |
|
data_release_and_get_mem (gpgme_data_t in, size_t *r_bufferlen) |
318 |
|
{ |
319 |
|
size_t n; |
320 |
|
char *buffer, *p; |
321 |
|
|
322 |
|
gpg_data_putc (in, '\0'); |
323 |
|
p = gpgme_data_release_and_get_mem (in, &n); |
324 |
|
buffer = m_strdup (p); |
325 |
|
if (r_bufferlen) |
326 |
|
*r_bufferlen = n; |
327 |
|
gpgme_free (p); |
328 |
|
return buffer; |
329 |
|
} |
330 |
|
|
331 |
|
|
332 |
|
/* Read much data as possible from the socket @fd and |
333 |
|
return the data in @buffer. Caller must free data. |
334 |
|
Return value: 0 on success. */ |
335 |
|
int |
336 |
|
sock_read_ext (int fd, char **buffer, size_t *r_bufferlen) |
337 |
|
{ |
338 |
|
gpgme_data_t dh; |
339 |
|
char buf[1024]; |
340 |
|
size_t timeout=0; |
341 |
|
int nread, rc; |
342 |
|
|
343 |
|
if (gpgme_data_new (&dh)) |
344 |
|
return SOCKET_ERROR; |
345 |
|
while (timeout < default_socket_timeout) { |
346 |
|
rc = sock_select (fd, 1); |
347 |
|
if (rc == SOCKET_ERROR) { |
348 |
|
gpgme_data_release (dh); |
349 |
|
return rc; |
350 |
|
} |
351 |
|
else if (!rc) |
352 |
|
timeout++; /* socket not ready yet. */ |
353 |
|
else { |
354 |
|
nread = recv (fd, buf, DIM (buf), 0); |
355 |
|
if (nread == SOCKET_ERROR) |
356 |
|
return SOCKET_ERROR; |
357 |
|
else if (!nread) |
358 |
|
break; |
359 |
|
gpgme_data_write (dh, buf, nread); |
360 |
|
} |
361 |
|
} |
362 |
|
if (timeout >= default_socket_timeout) |
363 |
|
return WPTERR_WINSOCK_TIMEOUT; |
364 |
|
*buffer = data_release_and_get_mem (dh, r_bufferlen); |
365 |
|
return 0; |
366 |
|
} |
367 |
|
|
368 |
|
|
369 |
|
/* Write the buffer @buf with the length @buflen to a socket @fd. */ |
370 |
static int |
static int |
371 |
sock_write (int fd, const char *buf, int buflen) |
sock_write (int fd, const char *buf, size_t buflen) |
372 |
{ |
{ |
373 |
DWORD nwritten; |
int nwritten; |
374 |
int nleft = buflen; |
int nleft = buflen; |
375 |
|
|
376 |
while (nleft > 0) { |
while (nleft > 0) { |
377 |
nwritten = send (fd, buf, nleft, 0); |
nwritten = send (fd, buf, nleft, 0); |
378 |
if (nwritten == SOCKET_ERROR) |
if (nwritten == SOCKET_ERROR) { |
379 |
|
log_debug ("sock_write: send() failed ec=%d\r\n", net_errno); |
380 |
return SOCKET_ERROR; |
return SOCKET_ERROR; |
381 |
|
} |
382 |
nleft -= nwritten; |
nleft -= nwritten; |
383 |
buf += nwritten; |
buf += nwritten; |
384 |
} |
} |
385 |
|
|
386 |
return 0; |
return 0; |
387 |
} |
} |
388 |
|
|
389 |
|
|
|
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; |
|
|
} |
|
|
|
|
|
|
|
390 |
/* Initialize the Winsock2 interface.*/ |
/* Initialize the Winsock2 interface.*/ |
391 |
int |
int |
392 |
wsock_init (void) |
wsock_init (void) |
393 |
{ |
{ |
394 |
WSADATA wsa; |
WSADATA wsa; |
395 |
|
|
396 |
if (WSAStartup (0x202, &wsa)) |
if (WSAStartup (0x202, &wsa)) { |
397 |
|
log_debug ("wsock_init: WSAStartup failed ec=%d\r\n", net_errno); |
398 |
return WPTERR_WINSOCK_INIT; |
return WPTERR_WINSOCK_INIT; |
399 |
set_default_kserver (); |
} |
400 |
return 0; |
return 0; |
401 |
} |
} |
402 |
|
|
405 |
void |
void |
406 |
wsock_end (void) |
wsock_end (void) |
407 |
{ |
{ |
408 |
|
char *p; |
409 |
int i; |
int i; |
410 |
|
|
411 |
|
#ifdef WINPT_MOBILE |
412 |
|
p = m_strdup ("keyserver.conf"); |
413 |
|
#else |
414 |
|
p = make_special_filename (CSIDL_APPDATA, "winpt\\keyserver.conf", NULL); |
415 |
|
#endif |
416 |
|
kserver_save_conf (p); |
417 |
|
free_if_alloc (p); |
418 |
free_if_alloc (default_keyserver); |
free_if_alloc (default_keyserver); |
|
default_keyserver = NULL; |
|
419 |
for (i=0; i < MAX_KEYSERVERS; i++) { |
for (i=0; i < MAX_KEYSERVERS; i++) { |
420 |
if (server[i].used) |
if (server[i].name != NULL) |
421 |
free_if_alloc (server[i].name); |
free_if_alloc (server[i].name); |
422 |
} |
} |
423 |
|
kserver_proxy_release (&proxy); |
424 |
WSACleanup (); |
WSACleanup (); |
425 |
|
log_debug ("wsock_end: cleanup finished.\r\n"); |
426 |
} |
} |
427 |
|
|
428 |
|
|
429 |
/* Return a string representation of a winsock error. */ |
/* Return a string representation of a winsock error. */ |
430 |
const char* |
const char* |
431 |
wsock_strerror (void) |
wsock_strerror (void) |
432 |
{ |
{ |
|
static char buf[384]; |
|
433 |
int ec = WSAGetLastError (); |
int ec = WSAGetLastError (); |
434 |
|
|
435 |
|
if (!ec) |
436 |
|
return ""; |
437 |
switch (ec) { |
switch (ec) { |
438 |
case WSAENETDOWN: |
case WSAENETDOWN: |
439 |
return _("The network subsystem has failed"); |
return _("Network unreachable"); |
440 |
case WSAHOST_NOT_FOUND: |
|
441 |
return _("Authoritative Answer Host not found"); |
case WSAEHOSTUNREACH: |
442 |
case WSAETIMEDOUT: |
return _("Host unreachable"); |
443 |
return _("The connection has been dropped because of a network failure"); |
|
444 |
default: |
case WSAHOST_NOT_FOUND: |
445 |
_snprintf (buf, sizeof (buf) -1, _("Unknown Winsock error ec=%d"),ec); |
return _("Could not resolve host name"); |
446 |
return buf; |
|
447 |
|
case WSAECONNREFUSED: |
448 |
|
return _("Connection refused"); |
449 |
|
|
450 |
|
case WSAETIMEDOUT: |
451 |
|
case WSAECONNABORTED: |
452 |
|
return _("Connection timeout"); |
453 |
|
|
454 |
|
case WSAENETRESET: |
455 |
|
case WSAECONNRESET: |
456 |
|
return _("Connection resetted by peer"); |
457 |
|
|
458 |
|
case WSAESHUTDOWN: |
459 |
|
return _("Socket has been shutdown"); |
460 |
|
|
461 |
|
default: |
462 |
|
break; |
463 |
} |
} |
464 |
return NULL; |
|
465 |
|
return ""; |
466 |
|
} |
467 |
|
|
468 |
|
|
469 |
|
/* Set default socket timeout for all reading operations. */ |
470 |
|
void |
471 |
|
kserver_set_socket_timeout (size_t nsec) |
472 |
|
{ |
473 |
|
if (nsec < 0 || nsec > 3600) |
474 |
|
nsec = 0; |
475 |
|
default_socket_timeout = nsec; |
476 |
} |
} |
477 |
|
|
478 |
|
|
489 |
/* Read a keyserver from the list at index @idx. |
/* Read a keyserver from the list at index @idx. |
490 |
Return value: keyserver name at the given position. */ |
Return value: keyserver name at the given position. */ |
491 |
const char* |
const char* |
492 |
kserver_get_hostname (int idx, int type, u16 *port) |
kserver_get_hostname (int idx, int type, WORD *port) |
493 |
{ |
{ |
494 |
if (type == -1) { |
if (type == -1) { |
495 |
*port = default_keyserver_port; |
*port = default_keyserver_port; |
496 |
return default_keyserver; |
return default_keyserver; |
497 |
} |
} |
498 |
else if (!type && idx < DIM (server_list)) { |
else if (!type && (size_t)idx < DIM (server_list)) { |
499 |
|
/* XXX: handle non HKP servers. */ |
500 |
*port = HKP_PORT; |
*port = HKP_PORT; |
501 |
return server_list[idx]; |
return server_list[idx]; |
502 |
} |
} |
515 |
closesocket (fd); |
closesocket (fd); |
516 |
return 0; |
return 0; |
517 |
} |
} |
518 |
|
log_debug ("kserver_check_inet_connection: no inet connection.\r\n"); |
519 |
return -1; |
return -1; |
520 |
} |
} |
521 |
|
|
522 |
|
|
523 |
/* |
/* If the request contains the keyid, it have to be |
524 |
* If the request contains the keyid, it have to be |
always postfix with '0x'+keyid. This code checks |
525 |
* always postfix with '0x'+keyid. This code checks |
if the keyid is a decimal value and if so if it contains |
526 |
* 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. |
|
|
*/ |
|
527 |
const char* |
const char* |
528 |
kserver_check_keyid (const char *keyid) |
kserver_check_keyid (const char *keyid) |
529 |
{ |
{ |
530 |
static char id[20]; |
static char id[22]; |
531 |
|
|
532 |
if (strstr (keyid, "@")) |
if (strstr (keyid, "@")) |
533 |
return keyid; /* email address */ |
return keyid; /* email address */ |
534 |
if (strncmp (keyid, "0x", 2)) { |
if (strncmp (keyid, "0x", 2)) { |
535 |
memset (&id, 0, sizeof (id)); |
memset (&id, 0, sizeof (id)); |
536 |
_snprintf (id, sizeof (id)-1, "0x%s", keyid); |
_snprintf (id, DIM (id)-1, "0x%s", keyid); |
537 |
return id; |
return id; |
538 |
} |
} |
539 |
return keyid; |
return keyid; |
540 |
} /* kserver_check_keyid */ |
} |
541 |
|
|
542 |
|
|
543 |
|
/* Update the keyserver proxy user. */ |
544 |
static void |
static void |
545 |
kserver_update_proxyuser (const char *proxy_user, const char *proxy_pass) |
update_proxy_user (const char *proxy_user, const char *proxy_pass) |
546 |
{ |
{ |
547 |
char t[257]; /* user:pass = 127+1+127+1 = 257 */ |
char t[257]; /* user:pass = 127+1+127+1 = 257 */ |
548 |
int n = 0; |
int n = 0; |
549 |
|
|
550 |
n = 4*strlen (proxy_user) / 3 + 32 + strlen (proxy_pass) + 2; |
n = 4*strlen (proxy_user) / 3 + 32 + strlen (proxy_pass) + 2; |
551 |
free_if_alloc (proxy.base64_user); |
free_if_alloc (proxy.base64_user); |
552 |
proxy.base64_user = new char[n]; |
proxy.base64_user = safe_alloc (n+1); |
553 |
if (!proxy.base64_user) |
_snprintf (t, DIM (t)-1, "%s:%s", proxy_user, proxy_pass); |
554 |
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); |
|
555 |
free_if_alloc (proxy.user); |
free_if_alloc (proxy.user); |
556 |
free_if_alloc (proxy.pass); |
free_if_alloc (proxy.pass); |
557 |
proxy.user = m_strdup (proxy_user); |
proxy.user = m_strdup (proxy_user); |
558 |
proxy.pass = m_strdup (proxy_pass); |
proxy.pass = m_strdup (proxy_pass); |
|
} /* kserver_update_proxyuser */ |
|
|
|
|
|
|
|
|
/* Check that the given buffer contains a valid keyserver URL. */ |
|
|
static int |
|
|
check_URL (const char * buf) |
|
|
{ |
|
|
if (strlen (buf) < 7) |
|
|
return -1; |
|
|
if (!strstr (buf, "ldap://") |
|
|
&& !strstr (buf, "http://") |
|
|
&& !strstr (buf, "finger://") |
|
|
&& !strstr (buf, "hkp://")) |
|
|
return -1; |
|
|
return 0; |
|
559 |
} |
} |
560 |
|
|
561 |
|
|
564 |
port_from_proto (int proto) |
port_from_proto (int proto) |
565 |
{ |
{ |
566 |
switch (proto) { |
switch (proto) { |
567 |
case KSPROTO_LDAP: return 0; |
case KSPROTO_LDAP: return LDAP_PORT; |
568 |
case KSPROTO_FINGER: return FINGER_PORT; |
case KSPROTO_FINGER: return FINGER_PORT; |
569 |
case KSPROTO_HTTP: return HKP_PORT; |
case KSPROTO_HTTP: return HKP_PORT; |
570 |
} |
} |
571 |
return 0; |
return 0; |
572 |
} |
} |
574 |
|
|
575 |
/* Get the port number from the given URL. */ |
/* Get the port number from the given URL. */ |
576 |
static int |
static int |
577 |
proto_from_URL (const char * buf) |
proto_from_URL (const char *buf) |
578 |
{ |
{ |
579 |
if (strstr( buf, "ldap")) |
if (strstr (buf, "ldap")) |
580 |
return KSPROTO_LDAP; |
return KSPROTO_LDAP; |
581 |
else if (strstr( buf, "finger")) |
else if (strstr (buf, "finger")) |
582 |
return KSPROTO_FINGER; |
return KSPROTO_FINGER; |
583 |
return KSPROTO_HKP; |
return KSPROTO_HKP; |
584 |
} |
} |
585 |
|
|
586 |
|
|
587 |
|
/* Set the default keyserver. |
588 |
|
If @hostname is NULL, use the predefined keyserver. */ |
589 |
void |
void |
590 |
keyserver_set_default (const char * hostname, u16 port) |
keyserver_set_default (const char *hostname, WORD port) |
591 |
{ |
{ |
592 |
if (hostname) { |
if (!port) |
593 |
|
port = HKP_PORT; |
594 |
|
if (hostname != NULL) { |
595 |
free_if_alloc (default_keyserver); |
free_if_alloc (default_keyserver); |
596 |
default_keyserver = m_strdup (hostname); |
default_keyserver = m_strdup (hostname); |
|
if (!default_keyserver) |
|
|
BUG (0); |
|
597 |
default_keyserver_port = port; |
default_keyserver_port = port; |
598 |
} |
} |
599 |
|
if (!default_keyserver) { |
600 |
|
default_keyserver = m_strdup (DEF_HKP_KEYSERVER); |
601 |
|
default_keyserver_port = HKP_PORT; |
602 |
|
} |
603 |
server[0].name = m_strdup (default_keyserver); |
server[0].name = m_strdup (default_keyserver); |
604 |
server[0].used = 1; |
server[0].used = 1; |
605 |
server[0].port = port; |
server[0].port = port; |
606 |
server[0].proto = proto_from_URL (default_keyserver); |
server[0].proto = proto_from_URL (default_keyserver); |
607 |
} /* keyserver_set_default */ |
} |
608 |
|
|
609 |
|
|
610 |
static const char * |
/* Skip all kind of whitespace chars in @str. */ |
611 |
skip_whitespace (const char * str) |
static const char* |
612 |
|
skip_whitespace (const char *str) |
613 |
{ |
{ |
614 |
while (str && *str) |
while (str && *str) { |
|
{ |
|
615 |
if (*str == ' ' || |
if (*str == ' ' || |
616 |
*str == '\t' || |
*str == '\t' || |
617 |
*str == '\n' || |
*str == '\n' || |
618 |
*str == '\r') |
*str == '\r') { |
|
{ |
|
619 |
str++; |
str++; |
620 |
continue; |
continue; |
621 |
} |
} |
625 |
} |
} |
626 |
|
|
627 |
|
|
628 |
|
/* Save the keyserver config file in @conf. */ |
629 |
int |
int |
630 |
kserver_load_conf (const char * conf) |
kserver_save_conf (const char *conf) |
631 |
|
{ |
632 |
|
FILE *fp; |
633 |
|
int pos; |
634 |
|
|
635 |
|
fp = fopen (conf, "wb"); |
636 |
|
if (!fp) { |
637 |
|
msg_box (NULL, _("Could not save keyserver.conf file"), |
638 |
|
_("Keyserver"), MB_ERR); |
639 |
|
return -1; |
640 |
|
} |
641 |
|
|
642 |
|
fprintf (fp, "# do NOT manually modify this file, it will be " |
643 |
|
"generated automatically!!\r\n"); |
644 |
|
for (pos = 0; pos < MAX_KEYSERVERS; pos++) { |
645 |
|
if (!server[pos].used) |
646 |
|
continue; |
647 |
|
fprintf (fp, "%s:%d\r\n", server[pos].name, server[pos].port); |
648 |
|
} |
649 |
|
fclose (fp); |
650 |
|
return 0; |
651 |
|
} |
652 |
|
|
653 |
|
|
654 |
|
/* Return the specified keyserver config setting @key as an integer. */ |
655 |
|
static int |
656 |
|
get_conf_kserver_int (const char *key) |
657 |
{ |
{ |
658 |
struct stat statbuf; |
char *p; |
659 |
|
int val = 0; |
660 |
|
|
661 |
|
p = get_reg_entry_keyserver (key); |
662 |
|
if (p && *p) |
663 |
|
val = atoi (p); |
664 |
|
free_if_alloc (p); |
665 |
|
return val; |
666 |
|
} |
667 |
|
|
668 |
|
|
669 |
|
/* Read the proxy configuration and store it into @prox. */ |
670 |
|
static void |
671 |
|
read_proxy_config (keyserver_proxy_t prox) |
672 |
|
{ |
673 |
|
char *proto; |
674 |
|
|
675 |
|
if (!prox) |
676 |
|
return; |
677 |
|
|
678 |
|
proto = get_reg_entry_keyserver("Proto"); |
679 |
|
if (proto != NULL && strlen (proto) > 0) |
680 |
|
prox->proto = atoi (proto); |
681 |
|
else |
682 |
|
prox->proto = PROXY_PROTO_NONE; |
683 |
|
free_if_alloc (proto); |
684 |
|
free_if_alloc (prox->host); |
685 |
|
prox->host = get_reg_entry_keyserver ("Host"); |
686 |
|
free_if_alloc (prox->user); |
687 |
|
prox->user = get_reg_entry_keyserver ("User"); |
688 |
|
free_if_alloc (prox->pass); |
689 |
|
prox->pass = get_reg_entry_keyserver ("Pass"); |
690 |
|
prox->port = get_conf_kserver_int ("Port"); |
691 |
|
} |
692 |
|
|
693 |
|
|
694 |
|
/* Load the keyserver config file @conf. */ |
695 |
|
int |
696 |
|
kserver_load_conf (const char *conf) |
697 |
|
{ |
698 |
FILE *fp; |
FILE *fp; |
699 |
char buf[1024], * s, *p; |
char buf[1024], *s, *p; |
700 |
char *user = NULL, *pass = NULL; |
char *user = NULL, *pass = NULL; |
701 |
|
int no_config=0, chk_pos; |
702 |
int pos; |
int pos; |
|
int no_config=0, chk_pos=0; |
|
703 |
|
|
704 |
for (pos = 0; pos < MAX_KEYSERVERS; pos++) { |
for (pos = 0; pos < MAX_KEYSERVERS; pos++) { |
705 |
server[pos].used = 0; |
server[pos].used = 0; |
706 |
server[pos].port = HKP_PORT; |
server[pos].port = HKP_PORT; |
707 |
|
server[pos].name = NULL; |
708 |
} |
} |
709 |
|
|
710 |
fp = fopen (conf, "rb"); |
fp = fopen (conf, "rb"); |
714 |
server[pos].name = m_strdup (server_list[pos]); |
server[pos].name = m_strdup (server_list[pos]); |
715 |
server[pos].proto = proto_from_URL (server_list[pos]); |
server[pos].proto = proto_from_URL (server_list[pos]); |
716 |
} |
} |
717 |
no_config=1; |
no_config = 1; |
718 |
} |
} |
719 |
get_reg_proxy_prefs (&proxy.host, &proxy.port, &user, &pass); |
read_proxy_config (&proxy); |
720 |
if (user && pass) |
if (user && pass) |
721 |
kserver_update_proxyuser (user, pass); |
update_proxy_user (user, pass); |
722 |
else if (user && !pass || !user && pass) { |
else if (user && !pass || !user && pass) { |
723 |
msg_box( NULL, _("Invalid proxy configuration." |
msg_box (NULL, _("Invalid proxy configuration. " |
724 |
"You need to set a user and a password" |
"You need to set a user and a password " |
725 |
"to use proxy authentication!"), _("Proxy Error"), MB_ERR ); |
"to use proxy authentication!"), |
726 |
|
_("Proxy Error"), MB_ERR); |
727 |
} |
} |
728 |
/* check if the host has a http:// prefix and skip it */ |
/* check if the host has a http:// prefix and skip it */ |
729 |
if( proxy.host && !strncmp( proxy.host, "http://", 7 ) ) { |
if (proxy.host && !strncmp (proxy.host, "http://", 7)) { |
730 |
char *pr = m_strdup (proxy.host+7); |
char *host = m_strdup (proxy.host+7); |
|
if (!pr) |
|
|
BUG (NULL); |
|
731 |
free_if_alloc (proxy.host); |
free_if_alloc (proxy.host); |
732 |
proxy.host = pr; |
proxy.host = host; |
733 |
} |
} |
734 |
free_if_alloc (user); |
free_if_alloc (user); |
735 |
free_if_alloc (pass); |
free_if_alloc (pass); |
736 |
|
|
737 |
pos = 0; |
pos = 0; |
738 |
while( fp && !feof( fp ) ) { |
while (fp && !feof (fp)) { |
739 |
s = fgets (buf, sizeof (buf)-1, fp); |
s = fgets (buf, DIM (buf)-1, fp); |
740 |
if (!s) |
if (!s) |
741 |
break; |
break; |
742 |
if (strstr (buf, "\r\n")) |
if (strstr (buf, "\r\n")) |
746 |
s = (char *)skip_whitespace (buf); |
s = (char *)skip_whitespace (buf); |
747 |
if (*s == '#' || strlen (s) < 7) |
if (*s == '#' || strlen (s) < 7) |
748 |
continue; |
continue; |
749 |
if (check_URL (s)) { |
chk_pos = check_URL (s); |
750 |
|
if (!chk_pos) { |
751 |
msg_box (NULL, _("All entries of this file must have a valid prefix.\n" |
msg_box (NULL, _("All entries of this file must have a valid prefix.\n" |
752 |
"Currently HKP/HTTP, LDAP and FINGER are supported.\n"), |
"Currently HKP/HTTP, LDAP and FINGER are supported.\n"), |
753 |
_("Keyserver Error"), MB_ERR); |
_("Keyserver Error"), MB_ERR); |
754 |
continue; |
continue; |
755 |
} |
} |
|
chk_pos=6; |
|
|
if (strstr (s, "finger")) |
|
|
chk_pos = 10; |
|
756 |
p = strchr (s+chk_pos, ':'); |
p = strchr (s+chk_pos, ':'); |
757 |
server[pos].used = 1; |
server[pos].used = 1; |
758 |
server[pos].proto = proto_from_URL (s); |
server[pos].proto = proto_from_URL (s); |
763 |
server[pos].port = atol (p+1); |
server[pos].port = atol (p+1); |
764 |
if (server[pos].port < 0 || server[pos].port > 65536) |
if (server[pos].port < 0 || server[pos].port > 65536) |
765 |
server[pos].port = HKP_PORT; |
server[pos].port = HKP_PORT; |
766 |
server[pos].name = new char [(p-s)+2]; |
server[pos].name = safe_alloc ((p-s)+2); |
|
if (!server[pos].name) |
|
|
BUG (0); |
|
767 |
memset (server[pos].name, 0, (p-s)+2); |
memset (server[pos].name, 0, (p-s)+2); |
768 |
memcpy (server[pos].name, s, (p-s)); |
memcpy (server[pos].name, s, (p-s)); |
769 |
} |
} |
770 |
pos++; |
pos++; |
771 |
if (pos > MAX_KEYSERVERS) { |
if (pos > MAX_KEYSERVERS) { |
772 |
msg_box (NULL, _("The keyserver limit is exceeded"), _("Keyserver Warning"), MB_INFO); |
msg_box (NULL, _("The keyserver limit is exceeded"), |
773 |
|
_("Keyserver Warning"), MB_INFO); |
774 |
break; |
break; |
775 |
} |
} |
776 |
} |
} |
777 |
if (fp) |
if (fp) |
778 |
fclose (fp); |
fclose (fp); |
779 |
if (!pos) { |
if (!pos) { |
780 |
/* only issue an error if the config file exist but it has no valid entries. */ |
/* Only issue an error if the config file exist but |
781 |
|
it has no valid entries. */ |
782 |
keyserver_set_default (NULL, HKP_PORT); |
keyserver_set_default (NULL, HKP_PORT); |
783 |
if (!no_config) |
if (!no_config) |
784 |
return WPTERR_CONFIG_FILE; |
return WPTERR_CONFIG_FILE; |
785 |
} |
} |
786 |
|
|
|
if (!stat (conf, &statbuf)) |
|
|
conf_timestamp = statbuf.st_mtime; |
|
787 |
return 0; |
return 0; |
788 |
} /* kserver_load_conf */ |
} |
|
|
|
|
|
|
|
const char * |
|
|
kserver_get_proxy (int * r_port) |
|
|
{ |
|
|
if (proxy.host) { |
|
|
if (r_port) |
|
|
*r_port = proxy.port; |
|
|
return proxy.host; |
|
|
} |
|
|
return NULL; |
|
|
} /* kserver_get_proxy */ |
|
|
|
|
|
|
|
|
const char * |
|
|
kserver_get_proxy_info (int id) |
|
|
{ |
|
|
switch (id) { |
|
|
case PROXY_USER: return proxy.user; |
|
|
case PROXY_PASS: return proxy.pass; |
|
|
} |
|
|
return NULL; |
|
|
} /* kserver_get_proxy_info */ |
|
789 |
|
|
790 |
|
|
791 |
/* Connect to the keyserver @hostname. */ |
/* Connect to the keyserver @hostname (port @port). |
792 |
|
Return value: 0 on success */ |
793 |
int |
int |
794 |
kserver_connect (const char *hostname, u16 port, int *conn_fd) |
kserver_connect (const char *hostname, WORD port, int *conn_fd) |
795 |
{ |
{ |
|
int rc, fd; |
|
|
u32 iaddr; |
|
|
char host[128] = {0}; |
|
796 |
struct hostent *hp; |
struct hostent *hp; |
797 |
struct sockaddr_in sock; |
struct sockaddr_in sock; |
798 |
|
char host[128] = {0}; |
799 |
log_debug ("kserver_connect: %s:%d\r\n", hostname, port); |
DWORD iaddr; |
800 |
|
bool use_proxy = proxy.host != NULL; |
801 |
|
int rc, fd; |
802 |
|
|
803 |
if (!port) |
if (!port) |
804 |
port = HKP_PORT; |
port = HKP_PORT; |
805 |
if (conn_fd) |
if (conn_fd) |
806 |
*conn_fd = 0; |
*conn_fd = 0; |
807 |
hostname = skip_type_prefix (hostname); |
hostname = skip_type_prefix (hostname); |
808 |
|
log_debug ("kserver_connect: %s:%d\r\n", hostname, port); |
809 |
|
|
810 |
|
if (use_proxy && proxy.proto == PROXY_PROTO_HTTP) |
811 |
|
port = proxy.port; |
812 |
memset (&sock, 0, sizeof (sock)); |
memset (&sock, 0, sizeof (sock)); |
813 |
sock.sin_family = AF_INET; |
sock.sin_family = AF_INET; |
814 |
sock.sin_port = proxy.host? htons (proxy.port) : htons (port); |
sock.sin_port = htons (port); |
815 |
if (proxy.host) |
strncpy (host, use_proxy? proxy.host : hostname, DIM (host)-1); |
816 |
strncpy (host, proxy.host, 127); |
|
|
else |
|
|
strncpy (host, hostname, 127); |
|
|
|
|
817 |
if ((iaddr = inet_addr (host)) != INADDR_NONE) |
if ((iaddr = inet_addr (host)) != INADDR_NONE) |
818 |
memcpy (&sock.sin_addr, &iaddr, sizeof (iaddr)); |
memcpy (&sock.sin_addr, &iaddr, sizeof (iaddr)); |
819 |
else if ((hp = gethostbyname (host))) { |
else if ((hp = gethostbyname (host))) { |
824 |
memcpy (&sock.sin_addr, hp->h_addr, hp->h_length); |
memcpy (&sock.sin_addr, hp->h_addr, hp->h_length); |
825 |
} |
} |
826 |
else { |
else { |
827 |
log_debug ("gethostbyname: failed.\r\n"); |
log_debug ("gethostbyname: failed ec=%d.\r\n", net_errno); |
828 |
return WPTERR_WINSOCK_RESOLVE; |
return use_proxy? WPTERR_WINSOCK_PROXY : WPTERR_WINSOCK_RESOLVE; |
829 |
} |
} |
830 |
fd = socket (AF_INET, SOCK_STREAM, 0); |
fd = socket (AF_INET, SOCK_STREAM, 0); |
831 |
if (fd == INVALID_SOCKET) |
if (fd == INVALID_SOCKET) |
832 |
return WPTERR_WINSOCK_SOCKET; |
return WPTERR_WINSOCK_SOCKET; |
833 |
rc = connect (fd, (struct sockaddr *) &sock, sizeof (sock)); |
rc = connect (fd, (struct sockaddr *) &sock, sizeof (sock)); |
834 |
if (rc == SOCKET_ERROR) { |
if (rc == SOCKET_ERROR) { |
835 |
log_debug ("connect: failed.\r\n"); |
log_debug ("connect: failed ec=%d.\r\n", net_errno); |
836 |
closesocket (fd); |
closesocket (fd); |
837 |
return WPTERR_WINSOCK_CONNECT; |
return use_proxy? WPTERR_WINSOCK_PROXY : WPTERR_WINSOCK_CONNECT; |
838 |
|
} |
839 |
|
|
840 |
|
if (proxy.proto == PROXY_PROTO_SOCKS5) { |
841 |
|
/* XXX: finish code and provide errors constants. */ |
842 |
|
rc = socks_handshake (&proxy, fd, hostname, port); |
843 |
|
if (rc) { |
844 |
|
closesocket (fd); |
845 |
|
return WPTERR_GENERAL; |
846 |
|
} |
847 |
} |
} |
848 |
|
|
849 |
if (conn_fd) |
if (conn_fd) |
850 |
*conn_fd = fd; |
*conn_fd = fd; |
851 |
WSASetLastError (0); |
WSASetLastError (0); |
852 |
return 0; |
return 0; |
853 |
} /* kserver_connect */ |
} |
854 |
|
|
855 |
|
|
856 |
|
/* Check if the URL needs to be encoded or not. */ |
857 |
|
static bool |
858 |
|
URL_must_encoded (const char *url) |
859 |
|
{ |
860 |
|
if (strchr (url, '.') || strchr (url, '@') || strchr (url, ' ')) |
861 |
|
return true; |
862 |
|
return false; |
863 |
|
} |
864 |
|
|
865 |
/* Perform URL-encoding on the given pubkey blob. */ |
|
866 |
|
/* Perform URL encoding of the given data. */ |
867 |
static char* |
static char* |
868 |
kserver_urlencode (const char *pubkey, size_t octets, size_t *newlen) |
URL_encode (const char *url, DWORD ulen, DWORD *ret_nlen) |
869 |
{ |
{ |
870 |
char *p, numbuf[5]; |
gpgme_data_t hd; |
871 |
size_t j = 0; |
char numbuf[5], *p; |
872 |
size_t i, size; |
size_t i, nlen; |
|
|
|
|
p = new char [2*octets]; |
|
|
if (!p) |
|
|
BUG (0); |
|
873 |
|
|
874 |
for (size=0, i=0; i < octets; i++) { |
if (gpgme_data_new (&hd)) |
875 |
if (isalnum (pubkey[i]) || pubkey[i] == '-') { |
BUG (0); |
876 |
p[size] = pubkey[i]; |
for (i=0; i < ulen; i++) { |
877 |
size++; |
if (isalnum (url[i]) || url[i] == '-') |
878 |
} |
gpg_data_putc (hd, url[i]); |
879 |
else if (pubkey[i] == ' ') { |
else if (url[i] == ' ') |
880 |
p[size] = '+'; |
gpg_data_putc (hd, '+'); |
|
size++; |
|
|
} |
|
881 |
else { |
else { |
882 |
sprintf(numbuf, "%%%02X", pubkey[i]); |
sprintf (numbuf, "%%%02X", url[i]); |
883 |
for (j = 0; j<strlen(numbuf); j++) { |
gpgme_data_write (hd, numbuf, strlen (numbuf)); |
|
p[size] = numbuf[j]; |
|
|
size++; |
|
|
} |
|
884 |
} |
} |
885 |
} |
} |
886 |
p[size] = '\0'; |
|
887 |
*newlen = size; |
p = data_release_and_get_mem (hd, &nlen); |
888 |
|
if (ret_nlen) |
889 |
|
*ret_nlen = nlen; |
890 |
return p; |
return p; |
891 |
} |
} |
892 |
|
|
893 |
|
|
894 |
/* Format a request for the given keyid (send). */ |
/* Format a request for the given keyid (send). */ |
895 |
static char* |
static char* |
896 |
kserver_send_request (const char *hostname, u16 port, const char *pubkey, int octets) |
kserver_send_request (const char *hostname, WORD port, |
897 |
|
const char *pubkey, DWORD octets) |
898 |
{ |
{ |
899 |
char *request = NULL, *enc_pubkey = NULL; |
const char *fmt; |
900 |
|
char *request; |
901 |
|
char *enc_pubkey; |
902 |
|
DWORD enc_octets; |
903 |
int reqlen; |
int reqlen; |
|
size_t enc_octets; |
|
904 |
|
|
905 |
log_debug ("kserver_send_request: %s:%d\n", hostname, port); |
log_debug ("kserver_send_request: %s:%d\r\n", hostname, port); |
906 |
|
|
907 |
if (!port) |
if (!port) |
908 |
port = HKP_PORT; |
port = HKP_PORT; |
909 |
reqlen = 512 + strlen (hostname) + 2*strlen (pubkey); |
enc_pubkey = URL_encode (pubkey, octets, &enc_octets); |
910 |
request = new char[reqlen]; |
reqlen = 2*strlen (hostname) + 2*32/*port*/ + enc_octets + 32 /*len*/ + 1; |
911 |
if (!request) |
|
912 |
BUG (0); |
if (proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
913 |
enc_pubkey = kserver_urlencode (pubkey, octets, &enc_octets); |
fmt = "POST http://%s:%d/pks/add HTTP/1.0\r\n" |
914 |
if (!enc_pubkey || !enc_octets) { |
"Referer: \r\n" |
915 |
free_if_alloc (request); |
"User-Agent: WinPT/W32\r\n" |
916 |
return NULL; |
"Host: %s:%d\r\n" |
917 |
} |
"Proxy-Authorization: Basic %s\r\n" |
918 |
|
"Content-type: application/x-www-form-urlencoded\r\n" |
919 |
if (proxy.user) { |
"Content-length: %d\r\n" |
920 |
_snprintf (request, reqlen-1, |
"\r\n" |
921 |
"POST http://%s:%d/pks/add HTTP/1.0\r\n" |
"keytext=%s" |
922 |
"Referer: \r\n" |
"\r\n"; |
923 |
"User-Agent: WinPT/W32\r\n" |
reqlen += strlen (fmt) + strlen (proxy.base64_user) + 1; |
924 |
"Host: %s:%d\r\n" |
request = safe_alloc (reqlen+1); |
925 |
"Proxy-Authorization: Basic %s\r\n" |
_snprintf (request, reqlen, fmt, |
926 |
"Content-type: application/x-www-form-urlencoded\r\n" |
skip_type_prefix (hostname), port, hostname, port, |
|
"Content-length: %d\r\n" |
|
|
"\r\n" |
|
|
"keytext=%s" |
|
|
"\n", |
|
|
skip_type_prefix (hostname), port, hostname, port, |
|
927 |
proxy.base64_user, enc_octets+9, enc_pubkey); |
proxy.base64_user, enc_octets+9, enc_pubkey); |
928 |
} |
} |
929 |
else { |
else { |
930 |
_snprintf (request, reqlen-1, |
fmt = "POST /pks/add HTTP/1.0\r\n" |
931 |
"POST /pks/add HTTP/1.0\r\n" |
"Referer: \r\n" |
932 |
"Referer: \r\n" |
"User-Agent: WinPT/W32\r\n" |
933 |
"User-Agent: WinPT/W32\r\n" |
"Host: %s:%d\r\n" |
934 |
"Host: %s:%d\r\n" |
"Content-type: application/x-www-form-urlencoded\r\n" |
935 |
"Content-type: application/x-www-form-urlencoded\r\n" |
"Content-length: %d\r\n" |
936 |
"Content-length: %d\r\n" |
"\r\n" |
937 |
"\r\n" |
"keytext=%s" |
938 |
"keytext=%s" |
"\r\n"; |
939 |
"\n", |
reqlen += strlen (fmt)+1; |
940 |
skip_type_prefix (hostname), port, enc_octets+9, enc_pubkey); |
request = safe_alloc (reqlen+1); |
941 |
|
_snprintf (request, reqlen, fmt, |
942 |
|
skip_type_prefix (hostname), port, |
943 |
|
enc_octets+9, enc_pubkey); |
944 |
} |
} |
|
free_if_alloc (enc_pubkey); |
|
945 |
|
|
946 |
log_debug ("%s\n", request); |
free_if_alloc (enc_pubkey); |
947 |
|
log_debug ("request:\r\n%s\r\n", request); |
948 |
return request; |
return request; |
949 |
} /* kserver_send_request */ |
} |
950 |
|
|
951 |
|
|
952 |
/* Interface receiving a public key. */ |
/* Interface for receiving a public key. */ |
953 |
int |
int |
954 |
kserver_recvkey (const char *hostname, u16 port, const char *keyid, char *key, int maxkeylen) |
kserver_recvkey (const char *hostname, WORD port, const char *keyid, |
955 |
{ |
char **r_key, size_t *r_keylen) |
956 |
int rc, n; |
{ |
957 |
|
StringBuffer req; |
958 |
|
const char *reqbuf; |
959 |
int conn_fd; |
int conn_fd; |
960 |
char *request = NULL; |
int rc; |
961 |
|
|
962 |
if (!port) |
if (!port) |
963 |
port = HKP_PORT; |
port = HKP_PORT; |
964 |
hkp_err = 0; /* reset */ |
hkp_err = 0; /* reset */ |
965 |
rc = kserver_connect (hostname, port, &conn_fd); |
rc = kserver_connect (hostname, port, &conn_fd); |
966 |
if (rc) |
if (rc) |
967 |
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); |
|
|
} |
|
968 |
|
|
969 |
log_debug ("%s\r\n", request); |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
970 |
|
req = req + "GET http://" + skip_type_prefix (hostname) + |
971 |
|
":" + port + |
972 |
|
"/pks/lookup?op=get&search=" + keyid + " HTTP/1.0\r\n" + |
973 |
|
"Proxy-Authorization: Basic " + proxy.base64_user + "\r\n\r\n"; |
974 |
|
} |
975 |
|
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
976 |
|
req = req + "GET http://" + skip_type_prefix (hostname) + |
977 |
|
":" + port + "/pks/lookup?op=get&search=" + keyid + |
978 |
|
" HTTP/1.0\r\n\r\n"; |
979 |
|
} |
980 |
|
else |
981 |
|
req = req + "GET /pks/lookup?op=get&search=" + keyid + |
982 |
|
" HTTP/1.0\r\n\r\n"; |
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; |
991 |
} |
} |
992 |
|
|
993 |
rc = sock_read( conn_fd, key, maxkeylen, &n ); |
rc = sock_read_ext (conn_fd, r_key, r_keylen); |
994 |
if( rc == SOCKET_ERROR ) { |
if (rc == SOCKET_ERROR) { |
995 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
996 |
goto leave; |
goto leave; |
997 |
} |
} |
998 |
|
|
999 |
log_debug("%s\r\n", key); |
log_debug ("response:\r\n%s\r\n", *r_key); |
1000 |
rc = check_hkp_response (key, 1); |
rc = check_hkp_response (*r_key, 1); |
1001 |
if (rc) |
if (rc) |
1002 |
goto leave; |
goto leave; |
1003 |
|
|
1005 |
|
|
1006 |
leave: |
leave: |
1007 |
closesocket (conn_fd); |
closesocket (conn_fd); |
|
free_if_alloc (request); |
|
1008 |
return rc; |
return rc; |
1009 |
} /* kserver_recvkey */ |
} |
1010 |
|
|
1011 |
|
|
1012 |
/* Interface to send a public key. */ |
/* Interface to send a public key. */ |
1013 |
int |
int |
1014 |
kserver_sendkey (const char *hostname, u16 port, const char *pubkey, int len ) |
kserver_sendkey (const char *hostname, WORD port, |
1015 |
|
const char *pubkey, size_t len) |
1016 |
{ |
{ |
|
int n, rc; |
|
|
int conn_fd; |
|
1017 |
char *request = NULL; |
char *request = NULL; |
1018 |
char log[2048]; |
char log[2048] = {0}; |
1019 |
|
int conn_fd, n; |
1020 |
|
int rc; |
1021 |
|
|
1022 |
hkp_err = 0; /* reset */ |
hkp_err = 0; /* reset */ |
1023 |
rc = kserver_connect (hostname, port, &conn_fd); |
rc = kserver_connect (hostname, port, &conn_fd); |
1025 |
goto leave; |
goto leave; |
1026 |
|
|
1027 |
request = kserver_send_request (hostname, port, pubkey, len); |
request = kserver_send_request (hostname, port, pubkey, len); |
1028 |
if (request == NULL) { |
rc = sock_write (conn_fd, request, strlen (request)); |
1029 |
rc = WPTERR_GENERAL; |
if (rc == SOCKET_ERROR) { |
|
goto leave; |
|
|
} |
|
|
|
|
|
rc = sock_write( conn_fd, request, lstrlen(request) ); |
|
|
if( rc == SOCKET_ERROR ) { |
|
1030 |
rc = WPTERR_WINSOCK_SENDKEY; |
rc = WPTERR_WINSOCK_SENDKEY; |
1031 |
goto leave; |
goto leave; |
1032 |
} |
} |
1033 |
|
|
1034 |
rc = sock_read( conn_fd, log, sizeof (log)-1, &n ); |
rc = sock_read (conn_fd, log, DIM (log)-1, &n); |
1035 |
if( rc == SOCKET_ERROR ) { |
if (rc == SOCKET_ERROR) { |
1036 |
rc = WPTERR_WINSOCK_SENDKEY; |
rc = WPTERR_WINSOCK_SENDKEY; |
1037 |
goto leave; |
goto leave; |
1038 |
} |
} |
1039 |
|
|
1040 |
log_debug ("kserver_sendkey:\r\n%s\r\n", log); |
log_debug ("kserver_sendkey: read %d bytes\r\n%s\r\n", n, log); |
|
|
|
1041 |
rc = check_hkp_response (log, 0); |
rc = check_hkp_response (log, 0); |
1042 |
if( rc ) |
if (rc) |
1043 |
goto leave; |
goto leave; |
1044 |
|
|
1045 |
WSASetLastError (0); |
WSASetLastError (0); |
1046 |
|
|
1047 |
leave: |
leave: |
1048 |
closesocket (conn_fd); |
closesocket (conn_fd); |
1049 |
free_if_alloc (request); |
free_if_alloc (request); |
1050 |
return rc; |
return rc; |
1051 |
} /* kserver_sendkey */ |
} |
1052 |
|
|
1053 |
|
|
1054 |
|
/* Check keyserver response. */ |
1055 |
|
static int |
1056 |
|
kserver_search_chkresp (int fd) |
1057 |
|
{ |
1058 |
|
char buf[128]; |
1059 |
|
int n=0; |
1060 |
|
|
1061 |
|
/* parse response 'HTTP/1.0 500 OK' */ |
1062 |
|
if (sock_getline (fd, buf, DIM (buf)-1, &n)) |
1063 |
|
return WPTERR_KEYSERVER_NOTFOUND; |
1064 |
|
|
1065 |
|
log_debug ("kserver_search_chkpresp: %s\r\n", buf); |
1066 |
|
if (strncmp (buf, "HTTP/1.", 7)) |
1067 |
|
return WPTERR_KEYSERVER_NOTFOUND; |
1068 |
|
if (strncmp (buf+(8+1), "200", 3)) |
1069 |
|
return WPTERR_KEYSERVER_NOTFOUND; |
1070 |
|
return 0; |
1071 |
|
} |
1072 |
|
|
1073 |
|
|
1074 |
|
/* End the keyserver search procedure. */ |
1075 |
|
void |
1076 |
|
kserver_search_end (int conn_fd) |
1077 |
|
{ |
1078 |
|
log_debug ("kserver_search_end: fd=%d\r\n", conn_fd); |
1079 |
|
closesocket (conn_fd); |
1080 |
|
} |
1081 |
|
|
1082 |
|
|
1083 |
|
/* Extract the amount of keys from the info record. */ |
1084 |
|
static size_t |
1085 |
|
count_keys_in_response (char *buf) |
1086 |
|
{ |
1087 |
|
char *p; |
1088 |
|
int recno = 0; |
1089 |
|
size_t n = 0; |
1090 |
|
|
1091 |
|
/* info:1:4 */ |
1092 |
|
if (strncmp (buf, "info", 4)) |
1093 |
|
return 0; |
1094 |
|
p = strtok (buf, ":"); |
1095 |
|
while (p != NULL) { |
1096 |
|
recno++; |
1097 |
|
if (recno == 3) |
1098 |
|
n = atoi (p); |
1099 |
|
p = strtok (NULL, ":"); |
1100 |
|
} |
1101 |
|
return n; |
1102 |
|
} |
1103 |
|
|
1104 |
|
|
1105 |
|
/* Start the keyserver search. |
1106 |
|
Connect to host @hostname and port @port. |
1107 |
|
The pattern are given in @pattern. |
1108 |
|
The socket is returned in @conn_fd and @nkeys contains |
1109 |
|
the amount of keys which were found. */ |
1110 |
int |
int |
1111 |
kserver_search_init (const char * hostname, u16 port, const char * keyid, int * conn_fd) |
kserver_search_begin (const char *hostname, WORD port, |
1112 |
|
const char *pattern, int *conn_fd, size_t *nkeys) |
1113 |
{ |
{ |
1114 |
|
|
1115 |
|
StringBuffer req; |
1116 |
|
const char *reqbuf; |
1117 |
|
char *enc_patt = NULL; |
1118 |
|
char status[128]; |
1119 |
int rc, sock_fd; |
int rc, sock_fd; |
1120 |
int n=0; |
int nread; |
|
char * request = NULL; |
|
1121 |
|
|
1122 |
|
*conn_fd = 0; |
1123 |
|
|
1124 |
rc = kserver_connect (hostname, port, &sock_fd); |
rc = kserver_connect (hostname, port, &sock_fd); |
1125 |
if (rc) { |
if (rc) |
|
*conn_fd = 0; |
|
1126 |
goto leave; |
goto leave; |
1127 |
} |
|
1128 |
|
enc_patt = URL_encode (pattern, strlen (pattern), NULL); |
1129 |
n=300; |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
1130 |
request = new char[n+1]; |
req = req + "GET http://" + skip_type_prefix (hostname) + ":" + port + |
1131 |
if (!request) |
"/pks/lookup?options=mr&op=index&search=" + enc_patt + |
1132 |
BUG (0); |
" HTTP/1.0\r\n" |
1133 |
|
"Proxy-Authorization: Basic " + proxy.base64_user + "\r\n\r\n"; |
|
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); |
|
1134 |
} |
} |
1135 |
else if (proxy.host) { |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
1136 |
_snprintf (request, n, |
req = req + "GET http://" + skip_type_prefix (hostname) + ":" + port + |
1137 |
"GET http://%s:%d/pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n", |
"/pks/lookup?options=mr&op=index&search=" + enc_patt + |
1138 |
skip_type_prefix (hostname), port, keyid); |
" HTTP/1.0\r\n\r\n"; |
1139 |
} |
} |
1140 |
else { |
else { |
1141 |
_snprintf (request, n, |
req = req + "GET /pks/lookup?options=mr&op=index&search=" + |
1142 |
"GET /pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n", keyid); |
enc_patt + " HTTP/1.0\r\n\r\n"; |
1143 |
} |
} |
1144 |
|
|
1145 |
log_debug ("kserver_search_init:\r\n%s\r\n", request); |
log_debug ("kserver_search_begin:\r\n%s\r\n", req.getBuffer ()); |
1146 |
|
reqbuf = req.getBuffer (); |
1147 |
if (sock_write (sock_fd, request, strlen (request)) == SOCKET_ERROR) { |
if (sock_write (sock_fd, reqbuf, strlen (reqbuf)) == SOCKET_ERROR) { |
1148 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1149 |
goto leave; |
goto leave; |
1150 |
} |
} |
1151 |
|
|
1152 |
|
rc = kserver_search_chkresp (sock_fd); |
1153 |
|
if (rc) { |
1154 |
|
closesocket (sock_fd); |
1155 |
|
sock_fd = 0; |
1156 |
|
goto leave; |
1157 |
|
} |
1158 |
|
|
1159 |
|
/* Skip all lines until we reach the "info:" record. */ |
1160 |
|
for (;;) { |
1161 |
|
if (sock_getline (sock_fd, status, DIM (status)-1, &nread)) { |
1162 |
|
log_debug ("kserver_search_begin: retrieving status line failed.\r\n"); |
1163 |
|
closesocket (sock_fd); |
1164 |
|
sock_fd = 0; |
1165 |
|
rc = WPTERR_GENERAL; |
1166 |
|
break; |
1167 |
|
} |
1168 |
|
if (!strncmp (status, "info:", 5)) |
1169 |
|
break; |
1170 |
|
} |
1171 |
|
|
1172 |
|
if (!rc) |
1173 |
|
*nkeys = count_keys_in_response (status); |
1174 |
*conn_fd = sock_fd; |
*conn_fd = sock_fd; |
1175 |
|
|
1176 |
leave: |
leave: |
1177 |
free_if_alloc (request); |
free_if_alloc (enc_patt); |
1178 |
return rc; |
return rc; |
1179 |
} /* kserver_search_init */ |
} |
1180 |
|
|
1181 |
|
|
1182 |
int |
/* Parse a single pub record returned by the keyserver. */ |
1183 |
kserver_search_chkresp (int fd) |
static void |
1184 |
|
parse_pub_record (keyserver_key_s *key, char *buf) |
1185 |
{ |
{ |
1186 |
char buf[128]; |
enum pub_rec_t {ID=1, KEYID, ALGO, SIZE, CREATE, EXPIRE}; |
1187 |
int n=0; |
char *p; |
1188 |
|
int recno = 0; |
1189 |
/* parse response 'HTTP/1.0 500 OK' */ |
int off = 0; |
1190 |
if (sock_getline (fd, buf, 127, &n)) |
|
1191 |
return WPTERR_KEYSERVER_NOTFOUND; |
/* pub:{BF3DF9B4, ED4681C9BF3DF9B4, FPR}:17:1024:925411133:: */ |
1192 |
|
p = strtok (buf, ":"); |
1193 |
|
while (p != NULL) { |
1194 |
|
recno++; |
1195 |
|
switch (recno) { |
1196 |
|
case ID: |
1197 |
|
break; |
1198 |
|
|
1199 |
log_debug ("kserver_search_chkpresp: %s\r\n", buf); |
case KEYID: |
1200 |
if (strncmp (buf, "HTTP/1.", 7)) |
/* If for any reason the returned record actually contains |
1201 |
return WPTERR_KEYSERVER_NOTFOUND; |
a fingerprint instead of a key ID, we truncate the fpr. */ |
1202 |
if (strncmp (buf+(8+1), "200", 3)) |
off = strlen (p) == 40? 32 : 0; |
1203 |
return WPTERR_KEYSERVER_NOTFOUND; |
free_if_alloc (key->keyid); |
1204 |
return 0; |
key->keyid = m_strdup (p+off); |
1205 |
} /* kserver_search_chkresp */ |
break; |
1206 |
|
|
1207 |
|
case ALGO: |
1208 |
|
key->algo = atoi (p); |
1209 |
|
break; |
1210 |
|
|
1211 |
|
case SIZE: |
1212 |
|
key->bits = atoi (p); |
1213 |
|
break; |
1214 |
|
|
1215 |
int |
case CREATE: |
1216 |
kserver_search (int fd, keyserver_key * key) |
key->creation = strtoul (p, NULL, 10); |
1217 |
|
break; |
1218 |
|
|
1219 |
|
case EXPIRE: |
1220 |
|
key->expires = strtoul (p, NULL, 10); |
1221 |
|
break; |
1222 |
|
} |
1223 |
|
p = strtok (NULL, ":"); |
1224 |
|
} |
1225 |
|
} |
1226 |
|
|
1227 |
|
|
1228 |
|
/* Parse a single user id returned by the keyserver. */ |
1229 |
|
static void |
1230 |
|
parse_uid_record (keyserver_key_s *key, char *buf) |
1231 |
{ |
{ |
1232 |
char buf[1024], *p; |
enum uid_rec_t {ID=1, UID, CREATE, EXPIRE}; |
1233 |
int uidlen, nbytes, pos = 0; |
keyserver_uid_s *u, *n; |
1234 |
|
char *p, *raw; |
1235 |
|
int recno = 0; |
1236 |
|
|
1237 |
|
/* uid:Timo Schulz <[email protected]>:1138440360:: */ |
1238 |
|
u = new keyserver_uid_s; |
1239 |
|
memset (u, 0, sizeof *u); |
1240 |
|
|
1241 |
|
p = strtok (buf, ":"); |
1242 |
|
while (p != NULL) { |
1243 |
|
recno++; |
1244 |
|
|
1245 |
log_debug ("keyserver_search:\n"); |
switch (recno) { |
1246 |
|
case ID: |
1247 |
if (sock_getline (fd, buf, sizeof (buf) - 1, &nbytes)) |
break; |
|
return WPTERR_GENERAL; |
|
1248 |
|
|
1249 |
log_debug ("%s\n", buf); |
case UID: |
1250 |
|
unhexify_buffer (p, &raw); |
1251 |
if (!strncmp (buf, "pub", 3)) { |
u->uid = utf8_to_native (raw); |
1252 |
int revoked = strstr (buf, "KEY REVOKED") != NULL? 1 : 0; |
free_if_alloc (raw); |
1253 |
key->bits = atol (buf+3); |
break; |
1254 |
p = strchr (buf, '>'); |
|
1255 |
if (!p) |
case CREATE: |
1256 |
goto fail; |
u->creation = strtoul (p, NULL, 10); |
1257 |
pos = p - buf + 1; |
break; |
1258 |
memcpy (key->keyid, buf + pos, 8); |
|
1259 |
key->keyid[8] = '\0'; |
case EXPIRE: |
1260 |
p = strstr (buf, "</a>"); |
u->expires = strtoul (p, NULL, 10); |
1261 |
if (!p) |
break; |
|
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); |
|
1262 |
} |
} |
|
return 0; |
|
|
} |
|
1263 |
|
|
1264 |
fail: |
p = strtok (NULL, ":"); |
1265 |
key->bits = 0; |
} |
1266 |
memset (key, 0, sizeof *key); |
if (!key->uids) |
1267 |
return 0; |
key->uids = u; |
1268 |
} /* kserver_search */ |
else { |
1269 |
|
for (n = key->uids; n->next; n=n->next) |
1270 |
|
; |
1271 |
|
n->next = u; |
1272 |
|
} |
1273 |
|
} |
1274 |
|
|
1275 |
|
|
1276 |
|
/* Peek the next 3 bytes to check if the next line |
1277 |
|
would be a new "pub" record. In this case, return |
1278 |
|
-1 as an EOF indication, 0 otherwise. */ |
1279 |
static int |
static int |
1280 |
add_node( keyserver_cfgfile *cfg, const char *entry ) |
is_key_eof (int fd) |
1281 |
{ |
{ |
1282 |
keyserver_node *node; |
char buf[64]; |
1283 |
char *p = NULL; |
int n; |
|
|
|
|
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 */ |
|
|
|
|
1284 |
|
|
1285 |
void |
n = recv (fd, buf, 3, MSG_PEEK); |
1286 |
kserver_change_proxy( keyserver_proxy_ctx *ctx ) |
if (n < 3 || strncmp (buf, "pub", 3)) |
1287 |
{ |
return 0; |
1288 |
proxy.port = ctx->port; |
return -1; |
1289 |
free_if_alloc (proxy.host); |
} |
|
proxy.host = ctx->host? m_strdup( ctx->host ) : NULL; |
|
|
free_if_alloc( proxy.pass ); |
|
|
proxy.pass = ctx->pass? m_strdup( ctx->pass ) : NULL; |
|
|
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 */ |
|
1290 |
|
|
1291 |
|
|
1292 |
|
/* Return the next key from the search response. */ |
1293 |
int |
int |
1294 |
kserver_read_config( const char *fname, keyserver_cfgfile **ret_cfg ) |
kserver_search_next (int fd, keyserver_key_s **r_key) |
1295 |
{ |
{ |
1296 |
FILE *fp; |
keyserver_uid_s *uid, *u = NULL; |
1297 |
keyserver_cfgfile *cfg; |
keyserver_key_s *key; |
1298 |
char buf[256]; |
char buf[512]; |
1299 |
int rc = 0; |
int n = 0; |
1300 |
|
long max = 0; |
1301 |
*ret_cfg = NULL; |
|
1302 |
fp = fopen( fname, "rb" ); |
log_debug ("keyserver_search_next:\r\n"); |
1303 |
if( fp == NULL ) |
*r_key = NULL; |
1304 |
return WPTERR_FILE_OPEN; |
|
1305 |
|
key = new keyserver_key_s; |
1306 |
cfg = new keyserver_cfgfile; |
memset (key, 0, sizeof *key); |
1307 |
if ( !cfg ) |
for (;;) { |
1308 |
BUG( NULL ); |
if (sock_getline (fd, buf, DIM (buf)-1, &n)) |
1309 |
memset( cfg, 0, sizeof *cfg ); |
break; |
1310 |
|
/*log_debug ("record: '%s'\r\n", buf); */ |
1311 |
while ( !feof( fp ) ) { |
if (!strncmp (buf, "pub", 3)) |
1312 |
if ( fgets( buf, sizeof(buf)-1, fp ) == NULL ) |
parse_pub_record (key, buf); |
1313 |
break; |
else |
1314 |
if ( *buf == '\r' || *buf == '\n' || *buf == '#' ) |
parse_uid_record (key, buf); |
1315 |
continue; |
if (is_key_eof (fd)) |
1316 |
buf[strlen(buf) -2] = '\0'; |
break; |
|
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++; |
|
|
} |
|
1317 |
} |
} |
|
|
|
|
fclose( fp ); |
|
|
*ret_cfg = cfg; |
|
|
|
|
|
return rc; |
|
|
} /* kserver_read_config */ |
|
1318 |
|
|
1319 |
|
/* the uid with the newest self sig is used as the |
1320 |
|
primary user id. */ |
1321 |
|
for (uid = key->uids; uid; uid = uid->next) { |
1322 |
|
if (uid->creation > max) { |
1323 |
|
max = uid->creation; |
1324 |
|
u = uid; |
1325 |
|
} |
1326 |
|
} |
1327 |
|
|
1328 |
int |
key->main_uid = u? u : key->uids; |
1329 |
kserver_write_config( const char * fname, keyserver_cfgfile * cfg ) |
*r_key = key; |
|
{ |
|
|
|
|
1330 |
return 0; |
return 0; |
1331 |
} |
} |
1332 |
|
|
1333 |
|
|
1334 |
|
/* Release the keyserver key @key and all its elements. */ |
1335 |
void |
void |
1336 |
kserver_cfgfile_release( keyserver_cfgfile *cfg ) |
kserver_release_key (keyserver_key_s *key) |
1337 |
{ |
{ |
1338 |
keyserver_node *k, *k2; |
keyserver_uid_s *u; |
1339 |
|
|
1340 |
proxy_release( &cfg->proxy ); |
while (key->uids) { |
1341 |
for( k = cfg->list; cfg->nlist--; k = k2 ) { |
u = key->uids->next; |
1342 |
k2 = k->next; |
free_if_alloc (key->uids->uid); |
1343 |
free_if_alloc( k ); |
free_if_alloc (key->uids); |
1344 |
|
key->uids = u; |
1345 |
} |
} |
1346 |
free_if_alloc( cfg ); |
free_if_alloc (key->keyid); |
1347 |
} /* kserver_cfgfile_release */ |
free_if_alloc (key); |
1348 |
|
} |
1349 |
|
|
1350 |
|
|
1351 |
|
/* Release mbmers in the proxy context @ctx. */ |
1352 |
void |
void |
1353 |
proxy_release( keyserver_proxy_ctx *ctx ) |
kserver_proxy_release (keyserver_proxy_t ctx) |
1354 |
{ |
{ |
1355 |
free_if_alloc( ctx->host ); |
free_if_alloc (ctx->host); |
1356 |
free_if_alloc( ctx->pass ); |
free_if_alloc (ctx->pass); |
1357 |
free_if_alloc( ctx->user ); |
free_if_alloc (ctx->user); |
1358 |
} /* proxy_release */ |
ctx->port = ctx->proto = 0; |
1359 |
|
} |
1360 |
|
|
1361 |
|
|
1362 |
int |
/* Spawn a process given by @cmdl. */ |
1363 |
ldap_recvkey (const char * host, const char * keyid, char * key, int maxkeylen) |
static int |
1364 |
|
spawn_application (char *cmdl) |
1365 |
{ |
{ |
|
const char *s; |
|
|
char *ksprg = NULL, *p = NULL; |
|
|
char temp[512], outf[512]; |
|
|
FILE * inp; |
|
|
int rc, start_key = 0; |
|
1366 |
STARTUPINFO si; |
STARTUPINFO si; |
1367 |
PROCESS_INFORMATION pi; |
PROCESS_INFORMATION pi; |
1368 |
|
int rc = 0; |
1369 |
|
|
1370 |
|
memset (&si, 0, sizeof (si)); |
1371 |
|
si.cb = sizeof (si); |
1372 |
|
si.dwFlags = STARTF_USESHOWWINDOW; |
1373 |
|
si.wShowWindow = SW_HIDE; |
1374 |
|
memset (&pi, 0, sizeof (pi)); |
1375 |
|
|
1376 |
|
if (!CreateProcess (NULL, cmdl, NULL, NULL, FALSE, 0, |
1377 |
|
NULL, NULL, &si, &pi)) { |
1378 |
|
log_box ("Keyserver Plugin", MB_ERR, "Could not spawn helper process"); |
1379 |
|
rc = -1; |
1380 |
|
} |
1381 |
|
|
1382 |
p = get_gnupg_path (); |
CloseHandle (pi.hThread); |
1383 |
ksprg = new char[strlen (p) + 1 + 128]; |
WaitForSingleObject (pi.hProcess, INFINITE); |
1384 |
|
CloseHandle (pi.hProcess); |
1385 |
|
return rc; |
1386 |
|
} |
1387 |
|
|
1388 |
|
|
1389 |
|
static FILE* |
1390 |
|
do_spawn_ldap_helper (const char *host, const char *keyid) |
1391 |
|
{ |
1392 |
|
FILE *fp = NULL; |
1393 |
|
StringBuffer cmd; |
1394 |
|
char *sep, *p, *ksprg; |
1395 |
|
char outf[256], inf[256]; |
1396 |
|
size_t n; |
1397 |
|
|
1398 |
|
p = get_gnupg_prog (); |
1399 |
|
n = strlen (p) + 1 + 128; |
1400 |
|
ksprg = safe_alloc (n+1); |
1401 |
if (!ksprg) |
if (!ksprg) |
1402 |
BUG (0); |
BUG (0); |
1403 |
strcpy (ksprg, p); |
sep = strrchr (p, '\\'); |
1404 |
strcat (ksprg, "\\"); |
if (sep != NULL) |
1405 |
strcat (ksprg, "gpgkeys_ldap.exe"); |
p[(sep-p)] = 0; |
1406 |
|
|
1407 |
|
_snprintf (ksprg, n, "%s\\gpgkeys_ldap.exe", p); |
1408 |
free_if_alloc (p); |
free_if_alloc (p); |
1409 |
if (file_exist_check (ksprg)) { |
if (file_exist_check (ksprg)) { |
1410 |
log_box ( "LDAP Keyserver Plugin", MB_ERR, "Could not find LDAP keyserver module!"); |
log_box ("LDAP Keyserver Plugin", MB_ERR, |
1411 |
rc = -1; |
"%s: could not find LDAP keyserver module!", ksprg); |
1412 |
|
fp = NULL; |
1413 |
goto leave; |
goto leave; |
1414 |
} |
} |
1415 |
GetTempPath (sizeof (temp)-1, temp); |
get_temp_name (outf, DIM (outf)-1, keyid); |
1416 |
strcpy (outf, temp); |
get_temp_name (inf, DIM (inf)-1, NULL); |
1417 |
strcat (outf, keyid); |
fp = fopen (inf, "w+b"); |
1418 |
strcat (outf, ".out"); |
if (!fp) { |
1419 |
strcat (temp, keyid); |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", inf, |
|
inp = fopen (temp, "w+b"); |
|
|
if( !inp ) { |
|
|
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", temp, |
|
1420 |
winpt_strerror (WPTERR_FILE_OPEN)); |
winpt_strerror (WPTERR_FILE_OPEN)); |
|
rc = -1; |
|
1421 |
goto leave; |
goto leave; |
1422 |
} |
} |
1423 |
fprintf (inp, |
fprintf (fp, |
1424 |
"VERSION 0\n" |
"VERSION 1\n" |
1425 |
|
"PROGRAM %d.%d.%d\n" |
1426 |
|
"SCHEME ldap\n" |
1427 |
"HOST %s\n" |
"HOST %s\n" |
|
"OPTION verbose\n" |
|
1428 |
"COMMAND GET\n" |
"COMMAND GET\n" |
1429 |
"\n" |
"\n" |
1430 |
"%s\n", host? skip_type_prefix (host): "64.94.85.200", keyid); |
"%s\n", |
1431 |
fclose (inp); |
gpgver[0], gpgver[1], gpgver[2], |
1432 |
|
host? skip_type_prefix (host): "64.94.85.200", keyid); |
1433 |
memset (&si, 0, sizeof (si)); |
fclose (fp); |
1434 |
si.cb = sizeof (si); |
|
1435 |
si.dwFlags = STARTF_USESHOWWINDOW; |
cmd = ksprg; |
1436 |
si.wShowWindow = SW_HIDE; |
cmd = cmd + " -o " + outf + " " + inf; |
1437 |
memset (&pi, 0, sizeof (pi)); |
if (spawn_application ((char*)cmd.getBuffer ())) { |
1438 |
p = new char[strlen (ksprg) + strlen (temp) + strlen (outf) + 32]; |
fp = NULL; |
|
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; |
|
1439 |
goto leave; |
goto leave; |
1440 |
} |
} |
1441 |
CloseHandle (pi.hThread); |
fp = fopen (outf, "rb"); |
1442 |
WaitForSingleObject (pi.hProcess, INFINITE); |
if (!fp) |
|
CloseHandle (pi.hProcess); |
|
|
|
|
|
inp = fopen (outf, "rb"); |
|
|
if (!inp) { |
|
1443 |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf, |
log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf, |
1444 |
winpt_strerror (WPTERR_FILE_OPEN)); |
winpt_strerror (WPTERR_FILE_OPEN)); |
1445 |
rc = -1; |
|
1446 |
goto leave; |
leave: |
1447 |
} |
DeleteFile (inf); |
1448 |
memset (key, 0, maxkeylen); |
DeleteFile (outf); |
1449 |
while( !feof( inp ) ) { |
free_if_alloc (ksprg); |
1450 |
s = fgets( temp, sizeof (temp)-1, inp ); |
return fp; |
1451 |
if( !s ) |
} |
1452 |
|
|
1453 |
|
/* Receive an key via LDAP from host @host with the keyid @keyid. |
1454 |
|
@key contains the key on success. */ |
1455 |
|
int |
1456 |
|
ldap_recvkey (const char *host, const char *keyid, |
1457 |
|
char **r_key, size_t *r_keylen) |
1458 |
|
{ |
1459 |
|
gpgme_data_t raw; |
1460 |
|
FILE *fp; |
1461 |
|
const char *s; |
1462 |
|
char buf[512]; |
1463 |
|
int start_key = 0, failed = 0; |
1464 |
|
int rc = 0; |
1465 |
|
|
1466 |
|
fp = do_spawn_ldap_helper (host, keyid); |
1467 |
|
if (!fp) |
1468 |
|
return WPTERR_GENERAL; |
1469 |
|
|
1470 |
|
if (gpgme_data_new (&raw)) |
1471 |
|
BUG (0); |
1472 |
|
while (!feof (fp)) { |
1473 |
|
s = fgets (buf, DIM (buf)-1, fp); |
1474 |
|
if (!s) |
1475 |
break; |
break; |
1476 |
if( !start_key && strstr( s, "KEY" ) && strstr( s, "BEGIN" ) ) { |
if (strstr (s, "KEY") && strstr (s, "FAILED")) { |
1477 |
|
failed = 1; |
1478 |
|
break; |
1479 |
|
} |
1480 |
|
if (!start_key && strstr (s, "KEY") && strstr (s, "BEGIN")) { |
1481 |
start_key = 1; |
start_key = 1; |
1482 |
continue; |
continue; |
1483 |
} |
} |
1484 |
if( !start_key ) |
if (!start_key) |
1485 |
continue; |
continue; |
1486 |
strcat( key, temp ); |
gpgme_data_write (raw, buf, strlen (buf)); |
1487 |
maxkeylen -= strlen( temp ); |
if (strstr (s, "KEY") && strstr (s, "END")) |
|
if( maxkeylen < 0 || (strstr( s, "KEY" ) && strstr( s, "END" )) ) |
|
1488 |
break; |
break; |
1489 |
} |
} |
1490 |
fclose( inp ); |
fclose (fp); |
1491 |
|
|
1492 |
leave: |
if (failed) |
|
if( !strlen( key ) ) |
|
1493 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
1494 |
free_if_alloc( p ); |
*r_key = data_release_and_get_mem (raw, r_keylen); |
|
free_if_alloc( ksprg ); |
|
1495 |
return rc; |
return rc; |
|
} /* ldap_recvkey */ |
|
|
|
|
|
|
|
|
static int |
|
|
finger_readline (int fd, char * buf, int nbytes) |
|
|
{ |
|
|
char c = 0; |
|
|
int n, pos = 0; |
|
|
|
|
|
while (nbytes > 0) { |
|
|
n = recv (fd, &c, 1, 0); |
|
|
if (n <= 0) |
|
|
break; |
|
|
if (c == '\n') |
|
|
break; |
|
|
buf[pos++] = c; |
|
|
nbytes--; |
|
|
} |
|
|
if (nbytes > 0) |
|
|
buf[pos++] = '\0'; |
|
|
if (n <= 0) |
|
|
pos = n; |
|
|
return pos; |
|
1496 |
} |
} |
1497 |
|
|
1498 |
|
|
1499 |
|
/* Receive an key via FINGER from host @host with the user @user. |
1500 |
|
On success @key contains the key. */ |
1501 |
int |
int |
1502 |
finger_recvkey (const char * host, const char * user, char * key, int maxkeylen) |
finger_recvkey (const char *host, const char *user, |
1503 |
|
char **r_key, size_t *r_keylen) |
1504 |
{ |
{ |
1505 |
struct hostent * hp; |
gpgme_data_t raw; |
1506 |
struct sockaddr_in saddr; |
char buf[256+1]; |
|
char buf[128]; |
|
1507 |
int fd, nread; |
int fd, nread; |
1508 |
int start_key = 0; |
int start_key = 0; |
1509 |
int rc=0; |
int rc=0; |
1510 |
|
|
1511 |
fd = socket (PF_INET, SOCK_STREAM, 0); |
rc = kserver_connect (host, FINGER_PORT, &fd); |
1512 |
if (fd == -1) |
if (rc) |
1513 |
return WPTERR_WINSOCK_SOCKET; |
return rc; |
|
hp = gethostbyname (skip_type_prefix (host)); |
|
|
if (!hp) { |
|
|
closesocket (fd); |
|
|
return WPTERR_WINSOCK_RESOLVE; |
|
|
} |
|
1514 |
|
|
1515 |
memset (&saddr, 0, sizeof (saddr)); |
sock_write (fd, user, strlen (user)); |
1516 |
saddr.sin_family = AF_INET; |
sock_write (fd, "\r\n", 2); |
1517 |
saddr.sin_port = htons (FINGER_PORT); |
|
1518 |
saddr.sin_addr = *(struct in_addr *)hp->h_addr; |
if (gpgme_data_new (&raw)) |
1519 |
if (connect (fd, (struct sockaddr *)&saddr, sizeof (saddr))) { |
return WPTERR_WINSOCK_RECVKEY; |
1520 |
closesocket (fd); |
|
1521 |
return WPTERR_WINSOCK_CONNECT; |
for (;;) { |
1522 |
} |
if (sock_getline (fd, buf, DIM (buf)-1, &nread)) |
1523 |
send (fd, user, strlen (user), 0); |
break; |
|
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; |
|
1524 |
strcat (buf, "\n"); |
strcat (buf, "\n"); |
1525 |
if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) { |
if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) { |
1526 |
strcat (key, buf); |
gpgme_data_write (raw, buf, nread); |
1527 |
start_key = 1; |
start_key = 1; |
|
maxkeylen -= nread; |
|
1528 |
} |
} |
1529 |
else if (strstr (buf, "END PGP PUBLIC KEY BLOCK" ) && start_key) { |
else if (strstr (buf, "END PGP PUBLIC KEY BLOCK" ) && start_key) { |
1530 |
strcat (key, buf); |
gpgme_data_write (raw, buf, nread); |
1531 |
start_key--; |
start_key--; |
|
maxkeylen -= nread; |
|
1532 |
break; |
break; |
1533 |
} |
} |
1534 |
else if (start_key) { |
else if (start_key) |
1535 |
strcat (key, buf); |
gpgme_data_write (raw, buf, nread); |
|
maxkeylen -= nread; |
|
|
} |
|
1536 |
} |
} |
1537 |
|
|
1538 |
closesocket (fd); |
closesocket (fd); |
1539 |
if (start_key != 0) |
if (start_key != 0) |
1540 |
rc = WPTERR_WINSOCK_RECVKEY; |
rc = WPTERR_WINSOCK_RECVKEY; |
1541 |
|
*r_key = data_release_and_get_mem (raw, r_keylen); |
1542 |
return rc; |
return rc; |
1543 |
} |
} |
1544 |
|
|
1545 |
|
|
1546 |
|
/* Check if the given name @name is a valid inernet address |
1547 |
|
which means an dotted IP or a valid DNS name. |
1548 |
|
Return value: 0 on success. */ |
1549 |
int |
int |
1550 |
check_IP_or_hostname (const char *name) |
check_inet_address (const char *addr) |
1551 |
{ |
{ |
1552 |
const char *not_allowed = "=!�$%&@#*~\\/}][{<>|,;:'"; |
const char *not_allowed = "=!�$%&@#*~\\/}][{<>|,;:'"; |
1553 |
size_t i, j; |
size_t i; |
1554 |
|
|
1555 |
for (i=0; i < strlen (name); i++) { |
for (i=0; i < strlen (addr); i++) { |
1556 |
for (j =0; j < strlen (not_allowed); j++) { |
if (strchr (not_allowed, addr[i])) |
1557 |
if (name[i] == not_allowed[j]) |
return -1; |
1558 |
return -1; |
} |
1559 |
} |
return 0; |
1560 |
|
} |
1561 |
|
|
1562 |
|
|
1563 |
|
/* Split the URL @r_keyserver into the host and the port |
1564 |
|
part if possible. */ |
1565 |
|
gpgme_error_t |
1566 |
|
parse_keyserver_url (char **r_keyserver, unsigned short *r_port) |
1567 |
|
{ |
1568 |
|
char *p; |
1569 |
|
char *url = *r_keyserver; |
1570 |
|
int off = 0; |
1571 |
|
|
1572 |
|
/* no port is given so use the default port. */ |
1573 |
|
p = strrchr (url, ':'); |
1574 |
|
if (p == strchr (url, ':')) { |
1575 |
|
int port = port_from_proto (proto_from_URL (url)); |
1576 |
|
if (!port) |
1577 |
|
port = HKP_PORT; |
1578 |
|
*r_port = port; |
1579 |
|
return 0; |
1580 |
} |
} |
1581 |
|
|
1582 |
|
if (url[(p-url)-1] == '/') /* remove / in .de/:11371 */ |
1583 |
|
off = 1; |
1584 |
|
|
1585 |
|
*r_keyserver = substr (url, 0, (p-url)-off); |
1586 |
|
*r_port = atoi (url+(p-url)+1); |
1587 |
|
free_if_alloc (url); |
1588 |
return 0; |
return 0; |
1589 |
} |
} |