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