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