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