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