/[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 144 by twoaday, Thu Jan 12 16:28:06 2006 UTC revision 185 by twoaday, Mon Mar 20 12:48:52 2006 UTC
# Line 24  Line 24 
24  #endif  #endif
25    
26  #include <windows.h>  #include <windows.h>
27    #include <shlobj.h>
28  #include <stdio.h>  #include <stdio.h>
29  #include <sys/stat.h>  #include <sys/stat.h>
30  #include <ctype.h>  #include <ctype.h>
# Line 33  Line 34 
34  #include "wptTypes.h"  #include "wptTypes.h"
35  #include "wptNLS.h"  #include "wptNLS.h"
36  #include "wptW32API.h"  #include "wptW32API.h"
 #include "wptVersion.h"  
37  #include "wptGPG.h"  #include "wptGPG.h"
38  #include "wptRegistry.h"  #include "wptRegistry.h"
39    
40    /* just map net_errno to a winsock error. */
41  #define net_errno ((int)WSAGetLastError ())  #define net_errno ((int)WSAGetLastError ())
42    
 static char base64code[] =  
   "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";  
43    
44  keyserver server[MAX_KEYSERVERS] = {0};  keyserver server[MAX_KEYSERVERS] = {0};
45  static keyserver_proxy_ctx proxy = {0};  keyserver_proxy_s proxy = {0};
46  static const char *server_list[] = {  static const char *server_list[] = {
47      "hkp://wwwkeys.nl.pgp.net",      "hkp://wwwkeys.nl.pgp.net",
48      "hkp://wwwkeys.pl.pgp.net",      "hkp://wwwkeys.pl.pgp.net",
# Line 56  static const char *server_list[] = { Line 55  static const char *server_list[] = {
55      "hkp://wwwkeys.eu.pgp.net",      "hkp://wwwkeys.eu.pgp.net",
56      "hkp://wwwkeys.uk.pgp.net",      "hkp://wwwkeys.uk.pgp.net",
57      "hkp://wwwkeys.us.pgp.net",      "hkp://wwwkeys.us.pgp.net",
     "hkp://gnv.us.ks.cryptnet.net",  
58      "hkp://subkeys.pgp.net",      "hkp://subkeys.pgp.net",
59      "ldap://keyserver.pgp.com",      "ldap://keyserver.pgp.com",
60      NULL      NULL
# Line 67  static char  hkp_errmsg[1024]; /* Holds Line 65  static char  hkp_errmsg[1024]; /* Holds
65  static int   hkp_err = 0;       /* != 0 indicates an error occurred. */  static int   hkp_err = 0;       /* != 0 indicates an error occurred. */
66  static DWORD conf_timestamp = 0;/* timestamp of the configuration fiele. */  static DWORD conf_timestamp = 0;/* timestamp of the configuration fiele. */
67    
68  char    *default_keyserver = NULL;  /* Default keyserver and port. */
69  WORD    default_keyserver_port = 0;  char *default_keyserver = NULL;
70    WORD default_keyserver_port = 0;
71    
72    /* Default socket timeout. */
73    static int default_socket_timeout = 10;
74    
75  /* Basic64 encode the input @inputstr to @outputstr. */  /* Basic64 encode the input @inbuf to @outbuf. */
76  static void  static void
77  base64_encode (const char *inputstr, char *outputstr, int maxlen)  base64_encode (const char *inbuf, char *outbuf)
78  {        {
79      int index = 0, temp = 0, res = 0, i = 0, inputlen = 0, len = 0;      char base64code[] =
80            "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
81        int index = 0, temp = 0, res = 0;
82        int i = 0, inputlen = 0, len = 0;
83            
84      /* XXX: check len < maxlen. */      inputlen = strlen (inbuf);
85      inputlen = strlen (inputstr);      for (i = 0; i < inputlen; i++) {
     for (i = 0; i <inputlen; i++) {  
86          res = temp;          res = temp;
87          res = (res << 8) | (inputstr[i] & 0xFF);          res = (res << 8) | (inbuf[i] & 0xFF);
88          switch (index++) {          switch (index++) {
89          case 0: outputstr[len++] = base64code[res >> 2 & 0x3F]; res &= 0x3; break;                      case 0:
90          case 1: outputstr[len++] = base64code[res >> 4 & 0x3F]; res &= 0xF; break;                          outbuf[len++] = base64code[res >> 2 & 0x3F];
91          case 2: outputstr[len++] = base64code[res >> 6 & 0x3F];              res &= 0x3;
92                  outputstr[len++] = base64code[res & 0x3F]; res = index = 0; break;              break;
93            case 1:
94                outbuf[len++] = base64code[res >> 4 & 0x3F];
95                res &= 0xF;
96                break;
97            case 2:
98                outbuf[len++] = base64code[res >> 6 & 0x3F];
99                outbuf[len++] = base64code[res & 0x3F];
100                res = index = 0;
101                break;
102          }          }
103          temp = res;          temp = res;
104      }      }
105            
106      switch (index) {      if (index == 2) {      
107      case 0: break;                  outbuf[len++] = base64code[temp << 2 & 0x3F];  
108      case 2: outputstr[len++] = base64code[temp << 2 & 0x3F];              outbuf[len++] = '=';
             outputstr[len++] = '='; break;          
     case 1: outputstr[len++] = base64code[temp << 4 & 0x3F];      
             outputstr[len++] = '=';  
             outputstr[len++] = '=';  
109      }      }
110            else if (index == 1) {
111      outputstr[len] = '\0';          outbuf[len++] = base64code[temp << 4 & 0x3F];
112            outbuf[len++] = '=';
113            outbuf[len++] = '=';
114        }
115    
116        outbuf[len] = '\0';
117  }  }
118    
119    
# Line 155  static int Line 168  static int
168  sock_getline (int fd, char *buf, int buflen, int *nbytes)  sock_getline (int fd, char *buf, int buflen, int *nbytes)
169  {  {
170      char ch;      char ch;
171      int count = 0;            int nread = 0;      
172            
173      if (nbytes)      if (nbytes)
174          *nbytes = 0;          *nbytes = 0;
175      *buf = 0;      *buf = 0;
176      while (recv (fd, &ch, 1, 0) > 0) {      while (recv (fd, &ch, 1, 0) > 0) {
177          *buf++ = ch;          *buf++ = ch;
178          count++;          nread++;
179          /* XXX: remove the '\r' */          if (ch == '\r')
180          if (ch == '\n' || count == (buflen - 1)) {              continue; /* remove this char. */
181            if (ch == '\n' || nread == (buflen - 1)) {
182              *buf = 0;              *buf = 0;
183              if (nbytes)              if (nbytes)
184                  *nbytes = count;                  *nbytes = nread;
185              return 0;              return 0;
186          }          }
187      }      }
# Line 202  sock_select (int fd, int nsecs) Line 216  sock_select (int fd, int nsecs)
216     Return value: 0 on success. */     Return value: 0 on success. */
217  static int  static int
218  sock_read (int fd, char *buf, int buflen, int *nbytes)  sock_read (int fd, char *buf, int buflen, int *nbytes)
219  {        {
220      DWORD nread;      DWORD nread;
221      int nleft = buflen;      int nleft = buflen;
222      int rc, n = 0;      int rc, n = 0;
223    
224      while (nleft > 0) {      while (nleft > 0) {
225          if (n >= 10)          if (n >= default_socket_timeout)
226              return WPTERR_WINSOCK_TIMEOUT;              return WPTERR_WINSOCK_TIMEOUT;
227          rc = sock_select (fd, 1);          rc = sock_select (fd, 1);
228          if (rc == SOCKET_ERROR)          if (rc == SOCKET_ERROR)
# Line 233  sock_read (int fd, char *buf, int buflen Line 247  sock_read (int fd, char *buf, int buflen
247      return 0;      return 0;
248  }  }
249    
250    /* Read much data as possible from the socket @fd and
251       return the data in @buffer. Caller must free data.
252       Return value: 0 on success. */
253    int
254    sock_read_ext (int fd, char **buffer, int *r_bufferlen)
255    {
256        gpgme_data_t dh;
257        char buf[1024], *p;
258        size_t n=0;
259        int nread, rc;
260    
261        gpgme_data_new (&dh);
262        while (n < 10) {
263            rc = sock_select (fd, 1);
264            if (rc == SOCKET_ERROR) {
265                gpgme_data_release (dh);
266                return rc;
267            }
268            else if (!rc)
269                n++;
270            else {
271                nread = recv (fd, buf, sizeof (buf), 0);
272                if (nread == SOCKET_ERROR)
273                    return SOCKET_ERROR;
274                else if (!nread)
275                    break;
276                gpgme_data_write (dh, buf, nread);
277            }
278        }
279        gpg_data_putc (dh, '\0');
280        p = gpgme_data_release_and_get_mem (dh, &n);
281        *buffer = m_strdup (p);
282        if (r_bufferlen)
283            *r_bufferlen = n;
284        gpgme_free (p);
285        return 0;
286    }
287    
288    
289  /* Write the buffer @buf with the length @buflen to a socket @fd. */  /* Write the buffer @buf with the length @buflen to a socket @fd. */
290  static int  static int
# Line 248  sock_write (int fd, const char *buf, int Line 300  sock_write (int fd, const char *buf, int
300              return SOCKET_ERROR;              return SOCKET_ERROR;
301          }          }
302          nleft -= nwritten;          nleft -= nwritten;
303          buf += nwritten;                  buf += nwritten;
304      }      }
305    
306      return 0;      return 0;
307  }  }
308    
309    
 /* Set the default keyserver. */  
 void  
 set_default_kserver (void)  
 {  
     char *p = get_reg_entry_keyserver ("Default");  
     free_if_alloc (default_keyserver);  
     default_keyserver = p && *p != ' ' ? p : m_strdup (DEF_HKP_KEYSERVER);  
     p = get_reg_entry_keyserver ("Default_Port");  
     if (p && *p) {  
         default_keyserver_port = (WORD)strtoul (p, NULL, 10);  
         free_if_alloc (p);  
     }  
     else  
         default_keyserver_port = HKP_PORT;  
 }  
   
   
310  /* Initialize the Winsock2 interface.*/  /* Initialize the Winsock2 interface.*/
311  int  int
312  wsock_init (void)  wsock_init (void)
# Line 282  wsock_init (void) Line 317  wsock_init (void)
317          log_debug ("wsock_init: WSAStartup failed ec=%d\r\n", net_errno);          log_debug ("wsock_init: WSAStartup failed ec=%d\r\n", net_errno);
318          return WPTERR_WINSOCK_INIT;          return WPTERR_WINSOCK_INIT;
319      }      }
     set_default_kserver ();  
320      return 0;      return 0;
321  }  }
322    
# Line 291  wsock_init (void) Line 325  wsock_init (void)
325  void  void
326  wsock_end (void)  wsock_end (void)
327  {  {
328        char *p;
329      int i;      int i;
330    
331        p = make_special_filename (CSIDL_APPDATA, "winpt\\keyserver.conf", NULL);
332        kserver_save_conf (p);
333        free_if_alloc (p);
334      free_if_alloc (default_keyserver);      free_if_alloc (default_keyserver);
     default_keyserver = NULL;  
335      for (i=0; i < MAX_KEYSERVERS; i++) {      for (i=0; i < MAX_KEYSERVERS; i++) {
336          if (server[i].used)          if (server[i].used && server[i].name != NULL)
337              free_if_alloc (server[i].name);              free_if_alloc (server[i].name);
338      }      }
339        kserver_proxy_release (&proxy);
340      WSACleanup ();      WSACleanup ();
341  }  }
342    
# Line 325  wsock_strerror (void) Line 363  wsock_strerror (void)
363  }  }
364    
365    
366    /* Set default socket timeout for all reading operations. */
367    void
368    kserver_set_socket_timeout (int nsec)
369    {
370        if (nsec < 0)
371            nsec = 0;
372        default_socket_timeout = nsec;
373    }
374    
375    
376  /* Return the last keyserver error as a string. */  /* Return the last keyserver error as a string. */
377  const char*  const char*
378  kserver_strerror (void)  kserver_strerror (void)
# Line 370  kserver_check_inet_connection (void) Line 418  kserver_check_inet_connection (void)
418  /* If the request contains the keyid, it have to be  /* If the request contains the keyid, it have to be
419     always postfix with '0x'+keyid. This code checks     always postfix with '0x'+keyid. This code checks
420     if the keyid is a decimal value and if so if it contains     if the keyid is a decimal value and if so if it contains
421    the '0x'. If not it is inserted. */     the '0x'. If not it is inserted. */
422  const char*  const char*
423  kserver_check_keyid (const char *keyid)  kserver_check_keyid (const char *keyid)
424  {        {      
# Line 389  kserver_check_keyid (const char *keyid) Line 437  kserver_check_keyid (const char *keyid)
437    
438  /* Update the keyserver proxy user. */  /* Update the keyserver proxy user. */
439  static void  static void
440  kserver_update_proxyuser (const char *proxy_user, const char *proxy_pass)  update_proxy_user (const char *proxy_user, const char *proxy_pass)
441  {  {
442      char t[257]; /* user:pass = 127+1+127+1 = 257 */      char t[257]; /* user:pass = 127+1+127+1 = 257 */
443      int n = 0;      int n = 0;
# Line 400  kserver_update_proxyuser (const char *pr Line 448  kserver_update_proxyuser (const char *pr
448      if (!proxy.base64_user)      if (!proxy.base64_user)
449          BUG (0);          BUG (0);
450      _snprintf (t, sizeof (t)-1, "%s:%s", proxy_user, proxy_pass);      _snprintf (t, sizeof (t)-1, "%s:%s", proxy_user, proxy_pass);
451      base64_encode (t, proxy.base64_user, 257);      base64_encode (t, proxy.base64_user);
452      free_if_alloc (proxy.user);      free_if_alloc (proxy.user);
453      free_if_alloc (proxy.pass);      free_if_alloc (proxy.pass);
454      proxy.user = m_strdup (proxy_user);      proxy.user = m_strdup (proxy_user);
# Line 438  port_from_proto (int proto) Line 486  port_from_proto (int proto)
486    
487  /* Get the port number from the given URL. */  /* Get the port number from the given URL. */
488  static int  static int
489  proto_from_URL (const char * buf)  proto_from_URL (const char *buf)
490  {  {
491      if (strstr( buf, "ldap"))      if (strstr( buf, "ldap"))
492          return KSPROTO_LDAP;          return KSPROTO_LDAP;
# Line 451  proto_from_URL (const char * buf) Line 499  proto_from_URL (const char * buf)
499  void  void
500  keyserver_set_default (const char *hostname, WORD port)  keyserver_set_default (const char *hostname, WORD port)
501  {  {
502      if (hostname) {      if (hostname != NULL) {
503          free_if_alloc (default_keyserver);          free_if_alloc (default_keyserver);
504          default_keyserver = m_strdup (hostname);          default_keyserver = m_strdup (hostname);
505          if (!default_keyserver)          if (!default_keyserver)
506              BUG (0);              BUG (0);
507          default_keyserver_port = port;          default_keyserver_port = port;
508      }      }
509        if (!port)
510            port = HKP_PORT;
511        if (!default_keyserver)
512            default_keyserver = m_strdup (DEF_HKP_KEYSERVER);
513      server[0].name =  m_strdup (default_keyserver);      server[0].name =  m_strdup (default_keyserver);
514      server[0].used = 1;      server[0].used = 1;
515      server[0].port = port;      server[0].port = port;
# Line 483  skip_whitespace (const char *str) Line 535  skip_whitespace (const char *str)
535  }  }
536    
537    
538    /* Save the keyserver config file in @conf. */
539    int
540    kserver_save_conf (const char *conf)
541    {
542        FILE *fp;
543        int pos;
544    
545        fp = fopen (conf, "wb");
546        if (!fp) {
547            msg_box (NULL, _("Could not save keyserver.conf file"),
548                     _("Keyserver"), MB_ERR);
549            return -1;
550        }
551    
552        fprintf (fp, "# do NOT manually modify this file, it will be "
553                     "generated automatically!!\r\n");
554        for (pos = 0; pos < MAX_KEYSERVERS; pos++) {
555            if (!server[pos].used)
556                continue;
557            fprintf (fp, "%s:%d\r\n", server[pos].name, server[pos].port);
558        }
559        fclose (fp);
560        return 0;
561    }
562    
563    
564    /* Load the keyserver config file @conf. */
565  int  int
566  kserver_load_conf (const char * conf)  kserver_load_conf (const char *conf)
567  {  {
568      struct stat statbuf;      struct stat statbuf;
569      FILE *fp;      FILE *fp;
570      char buf[1024], * s, * p;      char buf[1024], *s, *p;
571      char *user = NULL, *pass = NULL;          char *user = NULL, *pass = NULL;    
572      int no_config=0, chk_pos=0;      int no_config=0, chk_pos=0;
573      int pos;      int pos;
# Line 507  kserver_load_conf (const char * conf) Line 586  kserver_load_conf (const char * conf)
586          }          }
587          no_config=1;          no_config=1;
588      }      }
589      get_reg_proxy_prefs (&proxy.host, &proxy.port, &user, &pass);      get_reg_proxy_prefs (&proxy);
590      if (user && pass)      if (user && pass)
591          kserver_update_proxyuser (user, pass);            update_proxy_user (user, pass);
592      else if (user && !pass || !user && pass) {      else if (user && !pass || !user && pass) {
593          msg_box( NULL, _("Invalid proxy configuration."          msg_box (NULL, _("Invalid proxy configuration."
594                           "You need to set a user and a password"                           "You need to set a user and a password"
595                           "to use proxy authentication!"), _("Proxy Error"), MB_ERR );                           "to use proxy authentication!"),
596                             _("Proxy Error"), MB_ERR);
597      }      }
598      /* check if the host has a http:// prefix and skip it */      /* check if the host has a http:// prefix and skip it */
599      if( proxy.host && !strncmp( proxy.host, "http://", 7 ) ) {      if (proxy.host && !strncmp (proxy.host, "http://", 7)) {
600          char *host = m_strdup (proxy.host+7);          char *host = m_strdup (proxy.host+7);
601          if (!host)          if (!host)
602              BUG (0);              BUG (0);
# Line 527  kserver_load_conf (const char * conf) Line 607  kserver_load_conf (const char * conf)
607      free_if_alloc (pass);      free_if_alloc (pass);
608            
609      pos = 0;      pos = 0;
610      while( fp && !feof( fp ) ) {      while (fp && !feof (fp)) {
611          s = fgets (buf, sizeof (buf)-1, fp);          s = fgets (buf, sizeof (buf)-1, fp);
612          if (!s)          if (!s)
613              break;              break;
# Line 544  kserver_load_conf (const char * conf) Line 624  kserver_load_conf (const char * conf)
624                               _("Keyserver Error"), MB_ERR);                               _("Keyserver Error"), MB_ERR);
625              continue;              continue;
626          }          }
627          chk_pos=6;          chk_pos = 6;
628          if (strstr (s, "finger"))          if (strstr (s, "finger"))
629              chk_pos = 10;              chk_pos = 10;
630          p = strchr (s+chk_pos, ':');          p = strchr (s+chk_pos, ':');
# Line 564  kserver_load_conf (const char * conf) Line 644  kserver_load_conf (const char * conf)
644              memcpy (server[pos].name, s, (p-s));              memcpy (server[pos].name, s, (p-s));
645          }          }
646          pos++;          pos++;
647          if (pos > MAX_KEYSERVERS)          if (pos > MAX_KEYSERVERS) {
648          {              msg_box (NULL, _("The keyserver limit is exceeded"),
649              msg_box (NULL, _("The keyserver limit is exceeded"), _("Keyserver Warning"), MB_INFO);                       _("Keyserver Warning"), MB_INFO);
650              break;              break;
651          }          }
652      }      }
653      if (fp)      if (fp)
654          fclose (fp);          fclose (fp);
655      if (!pos) {      if (!pos) {
656          /* only issue an error if the config file exist but it has no valid entries. */          /* only issue an error if the config file exist but
657               it has no valid entries. */
658          keyserver_set_default (NULL, HKP_PORT);          keyserver_set_default (NULL, HKP_PORT);
659          if (!no_config)          if (!no_config)
660              return WPTERR_CONFIG_FILE;                return WPTERR_CONFIG_FILE;
661      }      }
662    
663      if (!stat (conf, &statbuf))      if (!stat (conf, &statbuf))
664          conf_timestamp = statbuf.st_mtime;          conf_timestamp = statbuf.st_mtime;
665      return 0;      return 0;
 } /* kserver_load_conf */  
   
   
 /* Return the proxy host and port if available. Null otherwise. */  
 const char*  
 kserver_get_proxy (int *r_port)  
 {  
     if (proxy.host) {  
         if (r_port)  
             *r_port = proxy.port;  
         return proxy.host;  
     }  
     return NULL;  
 }  
   
   
 /* Return a requested item from the proxy environment. */  
 const char*  
 kserver_get_proxy_info (int id)  
 {  
     switch (id) {    
     case PROXY_USER: return proxy.user;  
     case PROXY_PASS: return proxy.pass;  
     }  
     return NULL;  
666  }  }
667    
668    
# Line 660  kserver_connect (const char *hostname, W Line 716  kserver_connect (const char *hostname, W
716          return WPTERR_WINSOCK_CONNECT;          return WPTERR_WINSOCK_CONNECT;
717      }      }
718    
719        if (proxy.proto == PROXY_PROTO_SOCKS5) {
720            rc = socks_handshake (&proxy, fd, hostname, port);
721            if (rc) {
722                closesocket (fd);
723                return WPTERR_GENERAL; /* XXX: use better code. */
724            }
725        }
726    
727      if (conn_fd)      if (conn_fd)
728          *conn_fd = fd;          *conn_fd = fd;
729      WSASetLastError (0);      WSASetLastError (0);
# Line 731  kserver_send_request (const char *hostna Line 795  kserver_send_request (const char *hostna
795          return NULL;          return NULL;
796      }      }
797            
798      if (proxy.user) {      if (proxy.user && proxy.proto == PROXY_PROTO_HTTP) {
799          _snprintf (request, reqlen-1,          _snprintf (request, reqlen-1,
800                     "POST http://%s:%d/pks/add HTTP/1.0\r\n"                     "POST http://%s:%d/pks/add HTTP/1.0\r\n"
801                     "Referer: \r\n"                     "Referer: \r\n"
# Line 767  kserver_send_request (const char *hostna Line 831  kserver_send_request (const char *hostna
831  }  }
832    
833    
 /* Interface for receiving a public key. */  
834  int  int
835  kserver_recvkey (const char *hostname, WORD port, const char *keyid,  kserver_recvkey_ext (const char *hostname, WORD port, const char *keyid,
836                   char *key, int maxkeylen)                       char **r_key, int *r_keylen)
837  {  {
838      char *request = NULL;      char *request = NULL;
839      int conn_fd;      int conn_fd;
# Line 787  kserver_recvkey (const char *hostname, W Line 850  kserver_recvkey (const char *hostname, W
850      if (!request)      if (!request)
851          BUG (0);          BUG (0);
852            
853      if (proxy.host && proxy.user) {      if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) {
854          _snprintf (request, 300,          _snprintf (request, 300,
855              "GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n"              "GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n"
856              "Proxy-Authorization: Basic %s\r\n\r\n",              "Proxy-Authorization: Basic %s\r\n\r\n",
857              skip_type_prefix (hostname), port, keyid, proxy.base64_user);              skip_type_prefix (hostname), port, keyid, proxy.base64_user);
858      }      }
859      else if (proxy.host) {      else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) {
860          _snprintf (request, 300,          _snprintf (request, 300,
861              "GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n",              "GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n",
862              skip_type_prefix (hostname), port, keyid);              skip_type_prefix (hostname), port, keyid);
# Line 811  kserver_recvkey (const char *hostname, W Line 874  kserver_recvkey (const char *hostname, W
874          goto leave;          goto leave;
875      }      }
876            
877      rc = sock_read( conn_fd, key, maxkeylen, &n );      rc = sock_read_ext (conn_fd, r_key, &n);
878      if( rc == SOCKET_ERROR ) {      if (rc == SOCKET_ERROR) {
879          rc = WPTERR_WINSOCK_RECVKEY;          rc = WPTERR_WINSOCK_RECVKEY;
880          goto leave;          goto leave;
881      }      }
882    
883      log_debug("%s\r\n", key);      if (r_keylen)
884      rc = check_hkp_response (key, 1);          *r_keylen = n;
885        log_debug ("%s\r\n", *r_key);
886        rc = check_hkp_response (*r_key, 1);
887      if (rc)      if (rc)
888          goto leave;          goto leave;
889            
# Line 830  leave: Line 895  leave:
895      return rc;      return rc;
896  }  }
897    
898    /* Interface for receiving a public key. */
899    int
900    kserver_recvkey (const char *hostname, WORD port, const char *keyid,
901                     char *key, int maxkeylen)
902    {
903        char *tmpkey;
904        int rc, nread=0;
905    
906        /* XXX: just a wrapper around the new method, replace it
907                soon as possible. */
908        rc = kserver_recvkey_ext (hostname, port, keyid, &tmpkey, &nread);
909        if (rc)
910            return rc;
911    
912        if (nread > maxkeylen) {
913            free_if_alloc (tmpkey);
914            return WPTERR_GENERAL;
915        }
916        strcpy (key, tmpkey);
917        free_if_alloc (tmpkey);
918        return 0;
919    }
920    
921    
922  /* Interface to send a public key. */  /* Interface to send a public key. */
923  int  int
# Line 877  leave: Line 965  leave:
965  }  }
966    
967    
968    /* Check keyserver response. */
969    static int
970    kserver_search_chkresp (int fd)
971    {
972        char buf[128];
973        int n=0;
974        
975        /* parse response 'HTTP/1.0 500 OK' */
976        if (sock_getline (fd, buf, 127, &n))
977            return WPTERR_KEYSERVER_NOTFOUND;
978    
979        log_debug ("kserver_search_chkpresp: %s\r\n", buf);
980        if (strncmp (buf, "HTTP/1.", 7))
981            return WPTERR_KEYSERVER_NOTFOUND;
982        if (strncmp (buf+(8+1), "200", 3))
983            return WPTERR_KEYSERVER_NOTFOUND;
984        return 0;
985    }
986    
987    
988    /* End the keyserver search procedure. */
989    void
990    kserver_search_end (int conn_fd)
991    {
992        log_debug ("kserver_search_end: fd=%d\r\n", conn_fd);
993        closesocket (conn_fd);
994    }
995    
996    
997    /* Begin keyserver search procedure. */
998  int  int
999  kserver_search_init (const char *hostname, WORD port,  kserver_search_begin (const char *hostname, WORD port,
1000                       const char *keyid, int *conn_fd)                        const char *pattern, int *conn_fd)
1001  {  {
1002      char *request = NULL;      char *request = NULL;
1003      char *enc_keyid = NULL;      char *enc_patt = NULL;
1004      int n = 0;      int n;
1005      int rc, sock_fd;      int rc, sock_fd;
1006            
1007      rc = kserver_connect (hostname, port, &sock_fd);      rc = kserver_connect (hostname, port, &sock_fd);
# Line 892  kserver_search_init (const char *hostnam Line 1010  kserver_search_init (const char *hostnam
1010          goto leave;          goto leave;
1011      }      }
1012    
1013      enc_keyid = URL_encode (keyid, strlen (keyid), NULL);      enc_patt = URL_encode (pattern, strlen (pattern), NULL);
1014      n=300;      n = 140 + strlen (enc_patt) + strlen (hostname) + 32 + 2;
1015        if (proxy.base64_user)
1016            n += strlen (proxy.base64_user) + 1;
1017      request = new char[n+1];      request = new char[n+1];
1018      if (!request)      if (!request)
1019          BUG (0);          BUG (0);
1020            
1021      if (proxy.host && proxy.user) {      if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) {
1022          _snprintf (request, n,          _snprintf (request, n,
1023              "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"
1024              "Proxy-Authorization: Basic %s\r\n\r\n",              "Proxy-Authorization: Basic %s\r\n\r\n",
1025              skip_type_prefix (hostname), port, enc_keyid, proxy.base64_user);              skip_type_prefix (hostname), port, enc_patt, proxy.base64_user);
1026      }          }    
1027      else if (proxy.host) {      else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) {
1028          _snprintf (request, n,          _snprintf (request, n,
1029              "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",
1030              skip_type_prefix (hostname), port, enc_keyid);              skip_type_prefix (hostname), port, enc_patt);
1031      }      }
1032      else {      else {
1033          _snprintf (request, n,          _snprintf (request, n,
1034                     "GET /pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n", enc_keyid);                     "GET /pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n",
1035                       enc_patt);
1036      }      }
1037            
1038      log_debug ("kserver_search_init:\r\n%s\r\n", request);      log_debug ("kserver_search_begin:\r\n%s\r\n", request);
1039            
1040      if (sock_write (sock_fd, request, strlen (request)) == SOCKET_ERROR) {      if (sock_write (sock_fd, request, strlen (request)) == SOCKET_ERROR) {
1041          rc = WPTERR_GENERAL;          rc = WPTERR_GENERAL;
1042          goto leave;          goto leave;
1043      }      }
1044            
1045        rc = kserver_search_chkresp (sock_fd);
1046        if (rc) {
1047            closesocket (sock_fd);
1048            sock_fd = 0;
1049        }
1050    
1051      *conn_fd = sock_fd;      *conn_fd = sock_fd;
1052            
1053  leave:  leave:
1054      free_if_alloc (request);      free_if_alloc (request);
1055      free_if_alloc (enc_keyid);      free_if_alloc (enc_patt);
1056      return rc;      return rc;
1057  }  }
1058    
1059    
 /* Check keyserver response. */  
 int  
 kserver_search_chkresp (int fd)  
 {  
     char buf[128];  
     int n=0;  
       
     /* parse response 'HTTP/1.0 500 OK' */  
     if (sock_getline (fd, buf, 127, &n))  
         return WPTERR_KEYSERVER_NOTFOUND;  
   
     log_debug ("kserver_search_chkpresp: %s\r\n", buf);  
     if (strncmp (buf, "HTTP/1.", 7))  
         return WPTERR_KEYSERVER_NOTFOUND;  
     if (strncmp (buf+(8+1), "200", 3))  
         return WPTERR_KEYSERVER_NOTFOUND;  
     return 0;  
 }  
1060    
1061    
1062  /* Convert an iso date @iso_date (YYYY-MM-DD) into the locale  /* Convert an iso date @iso_date (YYYY-MM-DD) into the locale
# Line 964  parse_iso_date (const char *iso_date, ch Line 1073  parse_iso_date (const char *iso_date, ch
1073      p = strtok (buf, "-");      p = strtok (buf, "-");
1074      while (p != NULL) {      while (p != NULL) {
1075          switch (pos) {          switch (pos) {
1076          case 0: st.wYear = (WORD)atoi (p); pos++; break;          case 0: st.wYear  = (WORD)atoi (p); pos++; break;
1077          case 1: st.wMonth = (WORD)atoi (p); pos++; break;          case 1: st.wMonth = (WORD)atoi (p); pos++; break;
1078          case 2: st.wDay = (WORD)atoi (p); pos++; break;          case 2: st.wDay   = (WORD)atoi (p); pos++; break;
1079          default: break;          default: break;
1080          }          }
1081          p = strtok (NULL, "-");          p = strtok (NULL, "-");
# Line 982  parse_iso_date (const char *iso_date, ch Line 1091  parse_iso_date (const char *iso_date, ch
1091    
1092    
1093  int  int
1094  kserver_search (int fd, keyserver_key *key)  kserver_search_next (int fd, keyserver_key *key)
1095  {  {
1096      char buf[1024], *p;      char buf[1024], *p;
1097      int uidlen, nbytes, pos = 0;      int uidlen, nbytes, pos = 0;
1098    
1099      log_debug ("keyserver_search:\r\n");      log_debug ("keyserver_search_next:\r\n");
1100            
1101      if (sock_getline (fd, buf, sizeof (buf) - 1, &nbytes))      if (sock_getline (fd, buf, sizeof (buf) - 1, &nbytes))
1102          return WPTERR_GENERAL;          return WPTERR_GENERAL;
1103    
1104        /* XXX: use maschine readable option. */
1105      log_debug ("%s\r\n", buf);      log_debug ("%s\r\n", buf);
1106            
1107      if (!strncmp (buf, "pub", 3)) {      if (!strncmp (buf, "pub", 3)) {
# Line 1052  fail: Line 1162  fail:
1162  }  }
1163    
1164    
 static int  
 add_node( keyserver_cfgfile *cfg, const char *entry )  
 {  
     keyserver_node *node;  
     char *p = NULL;  
       
     p = strchr( entry, '=' );  
     if( p == NULL )  
         return WPTERR_GENERAL;    
     node = new keyserver_node;  
     if( !node )  
         BUG (NULL);  
       
     memset( node, 0, sizeof *node );  
     node->used = 1;  
       
     node->host.used = 1;  
     node->host.proto = proto_from_URL( entry );  
     memcpy( node->host.name, entry+7, p-entry-7 );  
     node->next = cfg->list;  
     cfg->list = node;  
       
     return 0;  
 } /* add_node */  
   
   
 void  
 kserver_change_proxy( keyserver_proxy_ctx *ctx )  
 {      
     proxy.port = ctx->port;  
     free_if_alloc (proxy.host);  
     proxy.host = ctx->host? m_strdup( ctx->host ) : NULL;  
     free_if_alloc( proxy.pass );  
     proxy.pass = ctx->pass? m_strdup( ctx->pass ) : NULL;  
     free_if_alloc( proxy.user );  
     proxy.user = ctx->user? m_strdup( ctx->user ) : NULL;  
     set_reg_proxy_prefs( proxy.host, proxy.port, proxy.user, proxy.pass );  
 } /* kserver_change_proxy */  
   
   
 int  
 kserver_read_config( const char *fname, keyserver_cfgfile **ret_cfg )  
 {  
     FILE *fp;  
     keyserver_cfgfile *cfg;  
     char buf[256];  
     int rc = 0;  
       
     *ret_cfg = NULL;  
     fp = fopen( fname, "rb" );  
     if( fp == NULL )  
         return WPTERR_FILE_OPEN;  
       
     cfg = new keyserver_cfgfile;  
     if ( !cfg )  
         BUG( NULL );  
     memset( cfg, 0, sizeof *cfg );  
       
     while ( !feof( fp ) ) {  
         if ( fgets( buf, sizeof(buf)-1, fp ) == NULL )  
             break;  
         if ( *buf == '\r' || *buf == '\n' || *buf == '#' )  
             continue;  
         buf[strlen(buf) -2] = '\0';  
         if ( !strncmp( buf, "use_proxy=", 10 ) ) {  
             char *p = strchr(buf, ':');  
             if (!p)  
                 continue;  
             cfg->proxy.port = atol( buf+(p-buf+1) );  
             buf[p-buf] = '\0';  
             cfg->proxy.host = m_strdup( buf+10 );  
         }  
         else if ( !strncmp( buf, "proxy_user=", 11 ) )  
             cfg->proxy.user = m_strdup( buf+11 );  
         else if ( !strncmp( buf, "proxy_pass=", 11 ) )  
             cfg->proxy.pass = m_strdup( buf+11 );  
         if ( !strncmp( buf, "http://", 7 )  
             || !strncmp( buf, "hkp://", 6 )  
             || !strncmp( buf, "ldap://", 7 ) ) {  
             add_node( cfg, buf );  
             cfg->nlist++;  
         }  
     }  
       
     fclose( fp );  
     *ret_cfg = cfg;  
       
     return rc;  
 } /* kserver_read_config */  
   
   
 int  
 kserver_write_config( const char * fname, keyserver_cfgfile * cfg )  
 {  
       
     return 0;  
 }  
   
   
 void  
 kserver_cfgfile_release( keyserver_cfgfile *cfg )  
 {        
     keyserver_node *k, *k2;  
   
     proxy_release( &cfg->proxy );  
     for( k = cfg->list; cfg->nlist--; k = k2 ) {  
         k2 = k->next;  
         free_if_alloc( k );      
     }  
     free_if_alloc( cfg );  
 } /* kserver_cfgfile_release */  
   
   
1165  /* Release mbmers in the proxy context @ctx. */  /* Release mbmers in the proxy context @ctx. */
1166  void  void
1167  proxy_release( keyserver_proxy_ctx *ctx )  kserver_proxy_release (keyserver_proxy_t ctx)
1168  {  {
1169      free_if_alloc (ctx->host);      free_if_alloc (ctx->host);
1170      free_if_alloc (ctx->pass);      free_if_alloc (ctx->pass);
1171      free_if_alloc (ctx->user);      free_if_alloc (ctx->user);
1172        ctx->port = ctx->proto = 0;
1173  }  }
1174    
1175    
# Line 1204  spawn_application (char *cmdl) Line 1202  spawn_application (char *cmdl)
1202    
1203  /* Receive an key via LDAP from host @host with the keyid @keyid.  /* Receive an key via LDAP from host @host with the keyid @keyid.
1204     @key contains the key on success. */     @key contains the key on success. */
1205  int  int
1206  ldap_recvkey (const char *host, const char *keyid, char *key, int maxkeylen)  ldap_recvkey (const char *host, const char *keyid, char *key, int maxkeylen)
1207  {      {    
1208      FILE *inp;      FILE *fp;
     DWORD n;  
1209      const char *s;      const char *s;
1210      char *ksprg = NULL, *p = NULL, *sep;      char *ksprg = NULL, *p = NULL, *sep;
1211      char temp[512], outf[512];      char inf[256], outf[256], buf[512];
1212        DWORD n;
1213      int start_key = 0, failed = 0;      int start_key = 0, failed = 0;
1214      int rc = 0;      int rc = 0;
1215    
# Line 1231  ldap_recvkey (const char *host, const ch Line 1229  ldap_recvkey (const char *host, const ch
1229                   "%s: could not find LDAP keyserver module!", ksprg);                   "%s: could not find LDAP keyserver module!", ksprg);
1230          rc = -1;          rc = -1;
1231          goto leave;          goto leave;
1232      }          }
1233      GetTempPath (sizeof (temp)-1, temp);      get_temp_name (outf, sizeof (outf)-1, keyid);
1234      _snprintf (outf, sizeof (outf)-1, "%s%s.out", temp, keyid);      get_temp_name (inf, sizeof (inf)-1, NULL);
1235      strcat (temp, keyid);      fp = fopen (inf, "w+b");
1236      inp = fopen (temp, "w+b");      if (!fp) {
1237      if (!inp) {          log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", inf,
         log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", temp,  
1238                   winpt_strerror (WPTERR_FILE_OPEN));                   winpt_strerror (WPTERR_FILE_OPEN));
1239          rc = -1;          rc = -1;
1240          goto leave;          goto leave;
1241      }      }
1242      fprintf (inp,      fprintf (fp,
1243          "VERSION 1\n"          "VERSION 1\n"
1244          "PROGRAM 1.4.3-cvs\n"          "PROGRAM 1.4.3-cvs\n"
1245          "SCHEME ldap\n"          "SCHEME ldap\n"
# Line 1251  ldap_recvkey (const char *host, const ch Line 1248  ldap_recvkey (const char *host, const ch
1248          "\n"          "\n"
1249          "%s\n",          "%s\n",
1250          host? skip_type_prefix (host): "64.94.85.200", keyid);          host? skip_type_prefix (host): "64.94.85.200", keyid);
1251      fclose (inp);      fclose (fp);
1252    
1253      p = new char[strlen (ksprg) + strlen (temp) + strlen (outf) + 32];      p = new char[strlen (ksprg) + strlen (inf) + strlen (outf) + 32];
1254      if (!p)      if (!p)
1255          BUG (NULL);          BUG (NULL);
1256      sprintf (p, "%s -o %s %s", ksprg, outf, temp);      sprintf (p, "%s -o %s %s", ksprg, outf, inf);
1257      if (spawn_application (p)) {      if (spawn_application (p)) {
1258          rc = -1;          rc = -1;
1259          goto leave;          goto leave;
1260      }      }
     DeleteFile (temp);  
1261    
1262      inp = fopen (outf, "rb");      fp = fopen (outf, "rb");
1263      if (!inp) {      if (!fp) {
1264          log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf,          log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf,
1265                   winpt_strerror (WPTERR_FILE_OPEN));                   winpt_strerror (WPTERR_FILE_OPEN));
1266          rc = -1;          rc = -1;
1267          goto leave;          goto leave;
1268      }      }
1269      memset (key, 0, maxkeylen);      memset (key, 0, maxkeylen);
1270      while (!feof (inp)) {      while (!feof (fp)) {
1271          s = fgets (temp, sizeof (temp)-1, inp);          s = fgets (buf, sizeof (buf)-1, fp);
1272          if (!s)          if (!s)
1273              break;              break;
1274          if (strstr (s, "KEY") && strstr (s, "FAILED")) {          if (strstr (s, "KEY") && strstr (s, "FAILED")) {
# Line 1285  ldap_recvkey (const char *host, const ch Line 1281  ldap_recvkey (const char *host, const ch
1281          }                }      
1282          if (!start_key)          if (!start_key)
1283              continue;              continue;
1284          strcat (key, temp);          strcat (key, buf);
1285          maxkeylen -= strlen (temp);          maxkeylen -= strlen (buf);
1286          if (maxkeylen < 0 || (strstr (s, "KEY") && strstr (s, "END")))          if (maxkeylen < 0 || (strstr (s, "KEY") && strstr (s, "END")))
1287              break;              break;
1288      }      }
1289      fclose (inp);      fclose (fp);
1290    
1291  leave:  leave:
1292      if (failed || !strlen (key))      if (failed || !strlen (key))
1293          rc = WPTERR_WINSOCK_RECVKEY;          rc = WPTERR_WINSOCK_RECVKEY;
1294        DeleteFile (inf);
1295      DeleteFile (outf);      DeleteFile (outf);
1296      free_if_alloc (p);      free_if_alloc (p);
1297      free_if_alloc (ksprg);      free_if_alloc (ksprg);
# Line 1302  leave: Line 1299  leave:
1299  }  }
1300    
1301    
 static int  
 finger_readline (int fd, char *buf, int nbytes)  
 {  
     char c = 0;  
     int n, pos = 0;  
       
     while (nbytes > 0) {  
         n = recv (fd, &c, 1, 0);  
         if (n <= 0)  
             break;  
         if (c == '\n')  
             break;  
         buf[pos++] = c;  
         nbytes--;  
     }  
     if (nbytes > 0)  
         buf[pos++] = '\0';  
     if (n <= 0)  
         pos = n;  
     return pos;  
 }  
   
   
1302  /* Receive an key via FINGER from host @host with the user @user.  /* Receive an key via FINGER from host @host with the user @user.
1303     On success @key contains the key. */     On success @key contains the key. */
1304  int  int
1305  finger_recvkey (const char *host, const char *user, char *key, int maxkeylen)  finger_recvkey (const char *host, const char *user, char *key, int maxkeylen)
1306  {  {
     struct hostent * hp;  
     struct sockaddr_in saddr;  
1307      char buf[128];      char buf[128];
1308      int fd, nread;      int fd, nread;
1309      int start_key = 0;      int start_key = 0;
1310      int rc=0;      int rc=0;
1311    
1312      fd = socket (PF_INET, SOCK_STREAM, 0);      rc = kserver_connect (host, FINGER_PORT, &fd);
1313      if (fd == -1)      if (rc)
1314          return WPTERR_WINSOCK_SOCKET;          return rc;
     hp = gethostbyname (skip_type_prefix (host));  
     if (!hp) {  
         closesocket (fd);  
         return WPTERR_WINSOCK_RESOLVE;  
     }  
1315    
1316      memset (&saddr, 0, sizeof (saddr));      sock_write (fd, user, strlen (user));
1317      saddr.sin_family = AF_INET;      sock_write (fd, "\r\n", 2);
     saddr.sin_port = htons (FINGER_PORT);  
     saddr.sin_addr = *(struct in_addr *)hp->h_addr;  
     if (connect (fd, (struct sockaddr *)&saddr, sizeof (saddr))) {  
         closesocket (fd);  
         return WPTERR_WINSOCK_CONNECT;  
     }  
     send (fd, user, strlen (user), 0);  
     send (fd, "\r\n", 2, 0);  
1318    
1319      memset (key, 0, maxkeylen);      memset (key, 0, maxkeylen);
1320      while (maxkeylen > 0) {      while (maxkeylen > 0) {
1321          nread = finger_readline (fd, buf, sizeof (buf)-1);          if (sock_getline (fd, buf, sizeof (buf), &nread))
1322          if (nread <= 0)              break;
             break;  
1323          strcat (buf, "\n");          strcat (buf, "\n");
1324          if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) {          if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) {
1325              strcat (key, buf);              strcat (key, buf);

Legend:
Removed from v.144  
changed lines
  Added in v.185

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26