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