/[winpt]/trunk/Src/wptKeyserver.cpp
ViewVC logotype

Diff of /trunk/Src/wptKeyserver.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.24  
changed lines
  Added in v.36

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26