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