/[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 180 by twoaday, Mon Mar 6 14:41:58 2006 UTC revision 209 by twoaday, Wed May 3 14:34:08 2006 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-2006 Timo Schulz
3   *      Copyright (C) 2001 Marco Cunha   *      Copyright (C) 2001 Marco Cunha
4   *   *
5   * This file is part of WinPT.   * This file is part of WinPT.
# Line 37  Line 37 
37  #include "wptGPG.h"  #include "wptGPG.h"
38  #include "wptRegistry.h"  #include "wptRegistry.h"
39    
40    /* 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  keyserver_proxy_s 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://gnv.us.ks.cryptnet.net",
48      "hkp://wwwkeys.pl.pgp.net",      "hkp://keyserver.kjsl.com",
49      "hkp://wwwkeys.at.pgp.net",      "hkp://sks.keyserver.penguin.de",
     "hkp://wwwkeys.ch.pgp.net",  
     "hkp://wwwkeys.de.pgp.net",  
     "hkp://wwwkeys.dk.pgp.net",  
     "hkp://wwwkeys.cz.pgp.net",  
     "hkp://wwwkeys.es.pgp.net",  
     "hkp://wwwkeys.eu.pgp.net",  
     "hkp://wwwkeys.uk.pgp.net",  
     "hkp://wwwkeys.us.pgp.net",  
50      "hkp://subkeys.pgp.net",      "hkp://subkeys.pgp.net",
51      "ldap://keyserver.pgp.com",      "ldap://keyserver.pgp.com",
52      NULL      NULL
# Line 64  static const char *server_list[] = { Line 55  static const char *server_list[] = {
55    
56  static char  hkp_errmsg[1024];  /* Holds the error message from the server */  static char  hkp_errmsg[1024];  /* Holds the error message from the server */
57  static int   hkp_err = 0;       /* != 0 indicates an error occurred. */  static int   hkp_err = 0;       /* != 0 indicates an error occurred. */
 static DWORD conf_timestamp = 0;/* timestamp of the configuration fiele. */  
58    
59  /* Default keyserver and port. */  /* Default keyserver and port. */
60  char *default_keyserver = NULL;  char *default_keyserver = NULL;
61  WORD default_keyserver_port = 0;  WORD default_keyserver_port = 0;
62    
63    /* Default socket timeout. */
64    static int default_socket_timeout = 6;
65    
66  /* Basic64 encode the input @inbuf to @outbuf. */  /* Basic64 encode the input @inbuf to @outbuf. */
67  static void  static void
68  base64_encode (const char *inbuf, char *outbuf)  base64_encode (const char *inbuf, char *outbuf)
69  {        {
70        char base64code[] =
71            "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
72      int index = 0, temp = 0, res = 0;      int index = 0, temp = 0, res = 0;
73      int i = 0, inputlen = 0, len = 0;      int i = 0, inputlen = 0, len = 0;
74            
# Line 213  sock_select (int fd, int nsecs) Line 207  sock_select (int fd, int nsecs)
207     Return value: 0 on success. */     Return value: 0 on success. */
208  static int  static int
209  sock_read (int fd, char *buf, int buflen, int *nbytes)  sock_read (int fd, char *buf, int buflen, int *nbytes)
210  {        {
211      DWORD nread;      DWORD nread;
212      int nleft = buflen;      int nleft = buflen;
213      int rc, n = 0;      int rc, n = 0;
214        
215        if (nbytes)
216            *nbytes = 0;
217      while (nleft > 0) {      while (nleft > 0) {
218          if (n >= 10)          if (n >= default_socket_timeout)
219              return WPTERR_WINSOCK_TIMEOUT;              return WPTERR_WINSOCK_TIMEOUT;
220          rc = sock_select (fd, 1);          rc = sock_select (fd, 1);
221          if (rc == SOCKET_ERROR)          if (rc == SOCKET_ERROR)
# Line 244  sock_read (int fd, char *buf, int buflen Line 240  sock_read (int fd, char *buf, int buflen
240      return 0;      return 0;
241  }  }
242    
243    
244    /* Helper to create a string from the gpgme data and
245       then release the gpgme data object. */
246    char*
247    data_release_and_get_mem (gpgme_data_t in, int *r_bufferlen)
248    {
249        size_t n;
250        char *buffer, *p;
251    
252        gpg_data_putc (in, '\0');
253        p = gpgme_data_release_and_get_mem (in, &n);
254        buffer = m_strdup (p);
255        if (r_bufferlen)
256            *r_bufferlen = (int)n;
257        gpgme_free (p);
258        return buffer;
259    }
260    
261    
262  /* Read much data as possible from the socket @fd and  /* Read much data as possible from the socket @fd and
263     return the data in @buffer. Caller must free data.     return the data in @buffer. Caller must free data.
264     Return value: 0 on success. */     Return value: 0 on success. */
# Line 251  int Line 266  int
266  sock_read_ext (int fd, char **buffer, int *r_bufferlen)  sock_read_ext (int fd, char **buffer, int *r_bufferlen)
267  {  {
268      gpgme_data_t dh;      gpgme_data_t dh;
269      char buf[1024], *p;      char buf[1024];
270      size_t n=0;      size_t n=0;
271      int nread, rc;      int nread, rc;
272    
273      gpgme_data_new (&dh);      if (gpgme_data_new (&dh))
274            BUG (0);
275      while (n < 10) {      while (n < 10) {
276          rc = sock_select (fd, 1);          rc = sock_select (fd, 1);
277          if (rc == SOCKET_ERROR) {          if (rc == SOCKET_ERROR) {
# Line 273  sock_read_ext (int fd, char **buffer, in Line 289  sock_read_ext (int fd, char **buffer, in
289              gpgme_data_write (dh, buf, nread);              gpgme_data_write (dh, buf, nread);
290          }          }
291      }      }
292      gpg_data_putc (dh, '\0');  
293      p = gpgme_data_release_and_get_mem (dh, &n);      *buffer = data_release_and_get_mem (dh, r_bufferlen);
     *buffer = m_strdup (p);  
     if (r_bufferlen)  
         *r_bufferlen = n;  
     gpgme_free (p);  
294      return 0;      return 0;
295  }  }
296    
# Line 304  sock_write (int fd, const char *buf, int Line 316  sock_write (int fd, const char *buf, int
316  }  }
317    
318    
 /* 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;  
 }  
   
   
319  /* Initialize the Winsock2 interface.*/  /* Initialize the Winsock2 interface.*/
320  int  int
321  wsock_init (void)  wsock_init (void)
# Line 331  wsock_init (void) Line 326  wsock_init (void)
326          log_debug ("wsock_init: WSAStartup failed ec=%d\r\n", net_errno);          log_debug ("wsock_init: WSAStartup failed ec=%d\r\n", net_errno);
327          return WPTERR_WINSOCK_INIT;          return WPTERR_WINSOCK_INIT;
328      }      }
     set_default_kserver ();  
329      return 0;      return 0;
330  }  }
331    
# Line 348  wsock_end (void) Line 342  wsock_end (void)
342      free_if_alloc (p);      free_if_alloc (p);
343      free_if_alloc (default_keyserver);      free_if_alloc (default_keyserver);
344      for (i=0; i < MAX_KEYSERVERS; i++) {      for (i=0; i < MAX_KEYSERVERS; i++) {
345          if (server[i].used)          if (server[i].used && server[i].name != NULL)
346              free_if_alloc (server[i].name);              free_if_alloc (server[i].name);
347      }      }
348      kserver_proxy_release (&proxy);      kserver_proxy_release (&proxy);
# Line 378  wsock_strerror (void) Line 372  wsock_strerror (void)
372  }  }
373    
374    
375    /* Set default socket timeout for all reading operations. */
376    void
377    kserver_set_socket_timeout (int nsec)
378    {
379        if (nsec < 0)
380            nsec = 0;
381        default_socket_timeout = nsec;
382    }
383    
384    
385  /* Return the last keyserver error as a string. */  /* Return the last keyserver error as a string. */
386  const char*  const char*
387  kserver_strerror (void)  kserver_strerror (void)
# Line 504  proto_from_URL (const char *buf) Line 508  proto_from_URL (const char *buf)
508  void  void
509  keyserver_set_default (const char *hostname, WORD port)  keyserver_set_default (const char *hostname, WORD port)
510  {  {
511      if (hostname) {      if (hostname != NULL) {
512          free_if_alloc (default_keyserver);          free_if_alloc (default_keyserver);
513          default_keyserver = m_strdup (hostname);          default_keyserver = m_strdup (hostname);
         if (!default_keyserver)  
             BUG (0);  
514          default_keyserver_port = port;          default_keyserver_port = port;
515      }      }
516        if (!port)
517            port = HKP_PORT;
518        if (!default_keyserver)
519            default_keyserver = m_strdup (DEF_HKP_KEYSERVER);
520      server[0].name =  m_strdup (default_keyserver);      server[0].name =  m_strdup (default_keyserver);
521      server[0].used = 1;      server[0].used = 1;
522      server[0].port = port;      server[0].port = port;
# Line 565  kserver_save_conf (const char *conf) Line 571  kserver_save_conf (const char *conf)
571  /* Load the keyserver config file @conf. */  /* Load the keyserver config file @conf. */
572  int  int
573  kserver_load_conf (const char *conf)  kserver_load_conf (const char *conf)
574  {  {    
     struct stat statbuf;  
575      FILE *fp;      FILE *fp;
576      char buf[1024], *s, *p;      char buf[1024], *s, *p;
577      char *user = NULL, *pass = NULL;          char *user = NULL, *pass = NULL;    
# Line 599  kserver_load_conf (const char *conf) Line 604  kserver_load_conf (const char *conf)
604      /* check if the host has a http:// prefix and skip it */      /* check if the host has a http:// prefix and skip it */
605      if (proxy.host && !strncmp (proxy.host, "http://", 7)) {      if (proxy.host && !strncmp (proxy.host, "http://", 7)) {
606          char *host = m_strdup (proxy.host+7);          char *host = m_strdup (proxy.host+7);
         if (!host)  
             BUG (0);  
607          free_if_alloc (proxy.host);          free_if_alloc (proxy.host);
608          proxy.host = host;          proxy.host = host;
609      }      }
# Line 661  kserver_load_conf (const char *conf) Line 664  kserver_load_conf (const char *conf)
664              return WPTERR_CONFIG_FILE;              return WPTERR_CONFIG_FILE;
665      }      }
666    
     if (!stat (conf, &statbuf))  
         conf_timestamp = statbuf.st_mtime;  
667      return 0;      return 0;
668  }  }
669    
# Line 671  kserver_load_conf (const char *conf) Line 672  kserver_load_conf (const char *conf)
672     Return value: 0 on success */     Return value: 0 on success */
673  int  int
674  kserver_connect (const char *hostname, WORD port, int *conn_fd)  kserver_connect (const char *hostname, WORD port, int *conn_fd)
675  {  {        
     int rc, fd;  
     DWORD iaddr;  
     char host[128] = {0};  
676      struct hostent *hp;      struct hostent *hp;
677      struct sockaddr_in sock;      struct sockaddr_in sock;
678        char host[128] = {0};
679        DWORD iaddr;
680        int rc, fd;
681    
682      log_debug ("kserver_connect: %s:%d\r\n", hostname, port);      log_debug ("kserver_connect: %s:%d\r\n", hostname, port);
683    
# Line 686  kserver_connect (const char *hostname, W Line 687  kserver_connect (const char *hostname, W
687          *conn_fd = 0;          *conn_fd = 0;
688      hostname = skip_type_prefix (hostname);      hostname = skip_type_prefix (hostname);
689            
690        if (proxy.host && proxy.proto == PROXY_PROTO_HTTP)
691            port = proxy.port;
692      memset (&sock, 0, sizeof (sock));      memset (&sock, 0, sizeof (sock));
693      sock.sin_family = AF_INET;      sock.sin_family = AF_INET;
694      sock.sin_port = proxy.host? htons (proxy.port) : htons (port);      sock.sin_port = htons (port);
695      if (proxy.host)      if (proxy.host)
696          strncpy (host, proxy.host, 127);          strncpy (host, proxy.host, 127);
697      else      else
698          strncpy (host, hostname, 127);          strncpy (host, hostname, 127);
699        
700      if ((iaddr = inet_addr (host)) != INADDR_NONE)      if ((iaddr = inet_addr (host)) != INADDR_NONE)
701          memcpy (&sock.sin_addr, &iaddr, sizeof (iaddr));          memcpy (&sock.sin_addr, &iaddr, sizeof (iaddr));
702      else if ((hp = gethostbyname (host))) {      else if ((hp = gethostbyname (host))) {
# Line 717  kserver_connect (const char *hostname, W Line 720  kserver_connect (const char *hostname, W
720          return WPTERR_WINSOCK_CONNECT;          return WPTERR_WINSOCK_CONNECT;
721      }      }
722    
723      if (proxy.proto != PROXY_PROTO_HTTP) {      if (proxy.proto == PROXY_PROTO_SOCKS5) {
724          rc = socks_handshake (&proxy, fd, hostname, port);          rc = socks_handshake (&proxy, fd, hostname, port);
725          if (rc) {          if (rc) {
726              closesocket (fd);              closesocket (fd);
# Line 746  static char* Line 749  static char*
749  URL_encode (const char *url, size_t ulen, size_t *ret_nlen)  URL_encode (const char *url, size_t ulen, size_t *ret_nlen)
750  {  {
751      gpgme_data_t hd;      gpgme_data_t hd;
752      char numbuf[5], *pp, *p;      char numbuf[5], *p;
753      size_t i, n;      size_t i;
754        int nlen;
755    
756      gpgme_data_new (&hd);      if (gpgme_data_new (&hd))
757            BUG (0);
758      for (i=0; i < ulen; i++) {      for (i=0; i < ulen; i++) {
759          if (isalnum (url[i]) || url[i] == '-')          if (isalnum (url[i]) || url[i] == '-')
760              gpg_data_putc (hd, url[i]);              gpg_data_putc (hd, url[i]);
# Line 760  URL_encode (const char *url, size_t ulen Line 765  URL_encode (const char *url, size_t ulen
765              gpgme_data_write (hd, numbuf, strlen (numbuf));              gpgme_data_write (hd, numbuf, strlen (numbuf));
766          }          }
767      }      }
     gpg_data_putc (hd, '\0');  
768    
769      /* Copy memory to avoid that we need to use gpgme_free later. */      p = data_release_and_get_mem (hd, &nlen);
     pp = gpgme_data_release_and_get_mem (hd, &n);  
     p = m_strdup (pp);  
     gpgme_free (pp);  
770      if (ret_nlen)      if (ret_nlen)
771          *ret_nlen = n;          *ret_nlen = nlen;
772      return p;      return p;
773  }  }
774    
# Line 787  kserver_send_request (const char *hostna Line 788  kserver_send_request (const char *hostna
788      if (!port)      if (!port)
789          port = HKP_PORT;          port = HKP_PORT;
790      reqlen = 512 + strlen (hostname) + 2*strlen (pubkey);      reqlen = 512 + strlen (hostname) + 2*strlen (pubkey);
791        if (proxy.proto == PROXY_PROTO_HTTP && proxy.base64_user)
792            reqlen += strlen (proxy.base64_user) + 1;
793      request = new char[reqlen];      request = new char[reqlen];
794      if (!request)      if (!request)
795          BUG (0);          BUG (0);
# Line 807  kserver_send_request (const char *hostna Line 810  kserver_send_request (const char *hostna
810                     "Content-length: %d\r\n"                     "Content-length: %d\r\n"
811                     "\r\n"                     "\r\n"
812                     "keytext=%s"                     "keytext=%s"
813                     "\n",                     "\r\n",
814                     skip_type_prefix (hostname), port, hostname, port,                     skip_type_prefix (hostname), port, hostname, port,
815                     proxy.base64_user, enc_octets+9, enc_pubkey);                     proxy.base64_user, enc_octets+9, enc_pubkey);
816      }      }
# Line 821  kserver_send_request (const char *hostna Line 824  kserver_send_request (const char *hostna
824                     "Content-length: %d\r\n"                     "Content-length: %d\r\n"
825                     "\r\n"                     "\r\n"
826                     "keytext=%s"                     "keytext=%s"
827                     "\n",                     "\r\n",
828                     skip_type_prefix (hostname), port,                     skip_type_prefix (hostname), port,
829                     enc_octets+9, enc_pubkey);                     enc_octets+9, enc_pubkey);
830      }      }
# Line 832  kserver_send_request (const char *hostna Line 835  kserver_send_request (const char *hostna
835  }  }
836    
837    
838    /* Interface for receiving a public key. */
839  int  int
840  kserver_recvkey_ext (const char *hostname, WORD port, const char *keyid,  kserver_recvkey (const char *hostname, WORD port, const char *keyid,
841                       char **r_key, int *r_keylen)                   char **r_key, int *r_keylen)
842  {  {
843      char *request = NULL;      char *request = NULL;
844      int conn_fd;      int conn_fd;
845      int rc, n;      int rc;
846    
847      if (!port)      if (!port)
848          port = HKP_PORT;          port = HKP_PORT;
# Line 875  kserver_recvkey_ext (const char *hostnam Line 879  kserver_recvkey_ext (const char *hostnam
879          goto leave;          goto leave;
880      }      }
881            
882      rc = sock_read_ext (conn_fd, r_key, &n);      rc = sock_read_ext (conn_fd, r_key, r_keylen);
883      if (rc == SOCKET_ERROR) {      if (rc == SOCKET_ERROR) {
884          rc = WPTERR_WINSOCK_RECVKEY;          rc = WPTERR_WINSOCK_RECVKEY;
885          goto leave;          goto leave;
886      }      }
887    
888      if (r_keylen)      log_debug ("%s\r\n", *r_key);
         *r_keylen = n;  
     log_debug("%s\r\n", *r_key);  
889      rc = check_hkp_response (*r_key, 1);      rc = check_hkp_response (*r_key, 1);
890      if (rc)      if (rc)
891          goto leave;          goto leave;
# Line 896  leave: Line 898  leave:
898      return rc;      return rc;
899  }  }
900    
 /* Interface for receiving a public key. */  
 int  
 kserver_recvkey (const char *hostname, WORD port, const char *keyid,  
                  char *key, int maxkeylen)  
 {  
     char *tmpkey;  
     int rc, nread=0;  
   
     /* XXX: just a wrapper around the new method, replace it  
             soon as possible. */  
     rc = kserver_recvkey_ext (hostname, port, keyid, &tmpkey, &nread);  
     if (rc)  
         return rc;  
   
     if (nread > maxkeylen) {  
         free_if_alloc (tmpkey);  
         return WPTERR_GENERAL;  
     }  
     strcpy (key, tmpkey);  
     free_if_alloc (tmpkey);  
     return 0;  
 }  
   
901    
902  /* Interface to send a public key. */  /* Interface to send a public key. */
903  int  int
904  kserver_sendkey (const char *hostname, WORD port, const char *pubkey, int len )  kserver_sendkey (const char *hostname, WORD port, const char *pubkey, int len )
905  {  {
906      char *request = NULL;      char *request = NULL;
907      char log[2048];      char log[2048] = {0};
908      int conn_fd, n;      int conn_fd, n;
909      int rc;      int rc;
910            
# Line 952  kserver_sendkey (const char *hostname, W Line 931  kserver_sendkey (const char *hostname, W
931          goto leave;          goto leave;
932      }      }
933    
934      log_debug ("kserver_sendkey:\r\n%s\r\n", log);      log_debug ("kserver_sendkey: read %d bytes\r\n%s\r\n", n, log);
935      rc = check_hkp_response (log, 0);      rc = check_hkp_response (log, 0);
936      if (rc)      if (rc)
937          goto leave;          goto leave;
# Line 966  leave: Line 945  leave:
945  }  }
946    
947    
948    /* Check keyserver response. */
949    static int
950    kserver_search_chkresp (int fd)
951    {
952        char buf[128];
953        int n=0;
954        
955        /* parse response 'HTTP/1.0 500 OK' */
956        if (sock_getline (fd, buf, 127, &n))
957            return WPTERR_KEYSERVER_NOTFOUND;
958    
959        log_debug ("kserver_search_chkpresp: %s\r\n", buf);
960        if (strncmp (buf, "HTTP/1.", 7))
961            return WPTERR_KEYSERVER_NOTFOUND;
962        if (strncmp (buf+(8+1), "200", 3))
963            return WPTERR_KEYSERVER_NOTFOUND;
964        return 0;
965    }
966    
967    
968    /* End the keyserver search procedure. */
969    void
970    kserver_search_end (int conn_fd)
971    {
972        log_debug ("kserver_search_end: fd=%d\r\n", conn_fd);
973        closesocket (conn_fd);
974    }
975    
976    
977    /* Begin keyserver search procedure. */
978  int  int
979  kserver_search_init (const char *hostname, WORD port,  kserver_search_begin (const char *hostname, WORD port,
980                       const char *keyid, int *conn_fd)                        const char *pattern, int *conn_fd)
981  {  {
982      char *request = NULL;      char *request = NULL;
983      char *enc_keyid = NULL;      char *enc_patt = NULL;
984      int n = 0;      int n;
985      int rc, sock_fd;      int rc, sock_fd;
986            
987      rc = kserver_connect (hostname, port, &sock_fd);      rc = kserver_connect (hostname, port, &sock_fd);
# Line 981  kserver_search_init (const char *hostnam Line 990  kserver_search_init (const char *hostnam
990          goto leave;          goto leave;
991      }      }
992    
993      enc_keyid = URL_encode (keyid, strlen (keyid), NULL);      enc_patt = URL_encode (pattern, strlen (pattern), NULL);
994      n=300;      n = 140 + strlen (enc_patt) + strlen (hostname) + 32 + 2;
995        if (proxy.base64_user)
996            n += strlen (proxy.base64_user) + 1;
997      request = new char[n+1];      request = new char[n+1];
998      if (!request)      if (!request)
999          BUG (0);          BUG (0);
# Line 991  kserver_search_init (const char *hostnam Line 1002  kserver_search_init (const char *hostnam
1002          _snprintf (request, n,          _snprintf (request, n,
1003              "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"
1004              "Proxy-Authorization: Basic %s\r\n\r\n",              "Proxy-Authorization: Basic %s\r\n\r\n",
1005              skip_type_prefix (hostname), port, enc_keyid, proxy.base64_user);              skip_type_prefix (hostname), port, enc_patt, proxy.base64_user);
1006      }          }    
1007      else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) {      else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) {
1008          _snprintf (request, n,          _snprintf (request, n,
1009              "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",
1010              skip_type_prefix (hostname), port, enc_keyid);              skip_type_prefix (hostname), port, enc_patt);
1011      }      }
1012      else {      else {
1013          _snprintf (request, n,          _snprintf (request, n,
1014                     "GET /pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n",                     "GET /pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n",
1015                     enc_keyid);                     enc_patt);
1016      }      }
1017            
1018      log_debug ("kserver_search_init:\r\n%s\r\n", request);      log_debug ("kserver_search_begin:\r\n%s\r\n", request);
1019            
1020      if (sock_write (sock_fd, request, strlen (request)) == SOCKET_ERROR) {      if (sock_write (sock_fd, request, strlen (request)) == SOCKET_ERROR) {
1021          rc = WPTERR_GENERAL;          rc = WPTERR_GENERAL;
1022          goto leave;          goto leave;
1023      }      }
1024            
1025        rc = kserver_search_chkresp (sock_fd);
1026        if (rc) {
1027            closesocket (sock_fd);
1028            sock_fd = 0;
1029        }
1030    
1031      *conn_fd = sock_fd;      *conn_fd = sock_fd;
1032            
1033  leave:  leave:
1034      free_if_alloc (request);      free_if_alloc (request);
1035      free_if_alloc (enc_keyid);      free_if_alloc (enc_patt);
1036      return rc;      return rc;
1037  }  }
1038    
1039    
 /* 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;  
 }  
1040    
1041    
1042  /* 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 1072  parse_iso_date (const char *iso_date, ch Line 1071  parse_iso_date (const char *iso_date, ch
1071    
1072    
1073  int  int
1074  kserver_search (int fd, keyserver_key *key)  kserver_search_next (int fd, keyserver_key *key)
1075  {  {
1076      char buf[1024], *p;      char buf[1024], *p;
1077      int uidlen, nbytes, pos = 0;      int uidlen, nbytes, pos = 0;
1078    
1079      log_debug ("keyserver_search:\r\n");      log_debug ("keyserver_search_next:\r\n");
1080            
1081      if (sock_getline (fd, buf, sizeof (buf) - 1, &nbytes))      if (sock_getline (fd, buf, sizeof (buf) - 1, &nbytes))
1082          return WPTERR_GENERAL;          return WPTERR_GENERAL;
1083    
1084        /* XXX: use maschine readable option. */
1085      log_debug ("%s\r\n", buf);      log_debug ("%s\r\n", buf);
1086            
1087      if (!strncmp (buf, "pub", 3)) {      if (!strncmp (buf, "pub", 3)) {
# Line 1149  kserver_proxy_release (keyserver_proxy_t Line 1149  kserver_proxy_release (keyserver_proxy_t
1149      free_if_alloc (ctx->host);      free_if_alloc (ctx->host);
1150      free_if_alloc (ctx->pass);      free_if_alloc (ctx->pass);
1151      free_if_alloc (ctx->user);      free_if_alloc (ctx->user);
1152        ctx->port = ctx->proto = 0;
1153  }  }
1154    
1155    
# Line 1179  spawn_application (char *cmdl) Line 1180  spawn_application (char *cmdl)
1180  }  }
1181    
1182    
1183  /* Receive an key via LDAP from host @host with the keyid @keyid.  static FILE*
1184     @key contains the key on success. */  do_spawn_ldap_helper (const char *host, const char *keyid)
1185  int  {
1186  ldap_recvkey (const char *host, const char *keyid, char *key, int maxkeylen)      FILE *fp = NULL;
1187  {          char *p, *sep;
1188      FILE *fp;      char *ksprg;
1189      const char *s;      char outf[256], inf[256];
1190      char *ksprg = NULL, *p = NULL, *sep;      size_t n;
     char inf[256], outf[256], buf[512];  
     DWORD n;  
     int start_key = 0, failed = 0;  
     int rc = 0;  
1191    
1192      p = get_gnupg_prog ();      p = get_gnupg_prog ();
1193      n = strlen (p) + 1 + 128;      n = strlen (p) + 1 + 128;
# Line 1206  ldap_recvkey (const char *host, const ch Line 1203  ldap_recvkey (const char *host, const ch
1203      if (file_exist_check (ksprg)) {      if (file_exist_check (ksprg)) {
1204          log_box ("LDAP Keyserver Plugin", MB_ERR,          log_box ("LDAP Keyserver Plugin", MB_ERR,
1205                   "%s: could not find LDAP keyserver module!", ksprg);                   "%s: could not find LDAP keyserver module!", ksprg);
1206          rc = -1;          fp = NULL;
1207          goto leave;          goto leave;
1208      }      }
1209      get_temp_name (outf, sizeof (outf)-1, keyid);      get_temp_name (outf, sizeof (outf)-1, keyid);
# Line 1215  ldap_recvkey (const char *host, const ch Line 1212  ldap_recvkey (const char *host, const ch
1212      if (!fp) {      if (!fp) {
1213          log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", inf,          log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", inf,
1214                   winpt_strerror (WPTERR_FILE_OPEN));                   winpt_strerror (WPTERR_FILE_OPEN));
         rc = -1;  
1215          goto leave;          goto leave;
1216      }      }
1217      fprintf (fp,      fprintf (fp,
# Line 1234  ldap_recvkey (const char *host, const ch Line 1230  ldap_recvkey (const char *host, const ch
1230          BUG (NULL);          BUG (NULL);
1231      sprintf (p, "%s -o %s %s", ksprg, outf, inf);      sprintf (p, "%s -o %s %s", ksprg, outf, inf);
1232      if (spawn_application (p)) {      if (spawn_application (p)) {
1233          rc = -1;          fp = NULL;
1234          goto leave;          goto leave;
1235      }      }
   
1236      fp = fopen (outf, "rb");      fp = fopen (outf, "rb");
1237      if (!fp) {      if (!fp)
1238          log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf,          log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf,
1239                   winpt_strerror (WPTERR_FILE_OPEN));                   winpt_strerror (WPTERR_FILE_OPEN));
1240          rc = -1;  
1241          goto leave;  leave:
1242      }      DeleteFile (inf);
1243      memset (key, 0, maxkeylen);      DeleteFile (outf);
1244        free_if_alloc (p);
1245        free_if_alloc (ksprg);
1246        return fp;
1247    }
1248    
1249    /* Receive an key via LDAP from host @host with the keyid @keyid.
1250       @key contains the key on success. */
1251    int
1252    ldap_recvkey (const char *host, const char *keyid,
1253                  char **r_key, int *r_keylen)
1254    {  
1255        gpgme_data_t raw;
1256        FILE *fp;
1257        const char *s;
1258        char buf[512];
1259        int start_key = 0, failed = 0;
1260        int rc = 0;
1261    
1262        fp = do_spawn_ldap_helper (host, keyid);
1263        if (!fp)
1264            return WPTERR_GENERAL;
1265    
1266        if (gpgme_data_new (&raw))
1267            BUG (0);
1268      while (!feof (fp)) {      while (!feof (fp)) {
1269          s = fgets (buf, sizeof (buf)-1, fp);          s = fgets (buf, sizeof (buf)-1, fp);
1270          if (!s)          if (!s)
# Line 1257  ldap_recvkey (const char *host, const ch Line 1276  ldap_recvkey (const char *host, const ch
1276          if (!start_key && strstr (s, "KEY") && strstr (s, "BEGIN")) {          if (!start_key && strstr (s, "KEY") && strstr (s, "BEGIN")) {
1277              start_key = 1;              start_key = 1;
1278              continue;              continue;
1279          }                }
1280          if (!start_key)          if (!start_key)
1281              continue;              continue;
1282          strcat (key, buf);          gpgme_data_write (raw, buf, strlen (buf));
1283          maxkeylen -= strlen (buf);          if (strstr (s, "KEY") && strstr (s, "END"))
         if (maxkeylen < 0 || (strstr (s, "KEY") && strstr (s, "END")))  
1284              break;              break;
1285      }      }
1286      fclose (fp);      fclose (fp);
1287    
1288  leave:      if (failed)
     if (failed || !strlen (key))  
1289          rc = WPTERR_WINSOCK_RECVKEY;          rc = WPTERR_WINSOCK_RECVKEY;
1290      DeleteFile (inf);      *r_key = data_release_and_get_mem (raw, r_keylen);
     DeleteFile (outf);  
     free_if_alloc (p);  
     free_if_alloc (ksprg);  
1291      return rc;      return rc;
1292  }  }
1293    
# Line 1281  leave: Line 1295  leave:
1295  /* Receive an key via FINGER from host @host with the user @user.  /* Receive an key via FINGER from host @host with the user @user.
1296     On success @key contains the key. */     On success @key contains the key. */
1297  int  int
1298  finger_recvkey (const char *host, const char *user, char *key, int maxkeylen)  finger_recvkey (const char *host, const char *user,
1299                    char **r_key, int *r_keylen)
1300  {  {
1301      char buf[128];      gpgme_data_t raw;
1302        char buf[256];
1303      int fd, nread;      int fd, nread;
1304      int start_key = 0;      int start_key = 0;
1305      int rc=0;      int rc=0;
# Line 1295  finger_recvkey (const char *host, const Line 1311  finger_recvkey (const char *host, const
1311      sock_write (fd, user, strlen (user));      sock_write (fd, user, strlen (user));
1312      sock_write (fd, "\r\n", 2);      sock_write (fd, "\r\n", 2);
1313    
1314      memset (key, 0, maxkeylen);      if (gpgme_data_new (&raw))
1315      while (maxkeylen > 0) {          BUG (0);
1316    
1317        for (;;) {
1318          if (sock_getline (fd, buf, sizeof (buf), &nread))          if (sock_getline (fd, buf, sizeof (buf), &nread))
1319              break;              break;
1320          strcat (buf, "\n");          strcat (buf, "\n");
1321          if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) {          if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) {
1322              strcat (key, buf);              gpgme_data_write (raw, buf, nread);
1323              start_key = 1;              start_key = 1;
             maxkeylen -= nread;  
1324          }          }
1325          else if (strstr (buf, "END PGP PUBLIC KEY BLOCK" ) && start_key) {          else if (strstr (buf, "END PGP PUBLIC KEY BLOCK" ) && start_key) {
1326              strcat (key, buf);              gpgme_data_write (raw, buf, nread);
1327              start_key--;              start_key--;
             maxkeylen -= nread;  
1328              break;              break;
1329          }          }
1330          else if (start_key) {          else if (start_key)
1331              strcat (key, buf);              gpgme_data_write (raw, buf, nread);
             maxkeylen -= nread;  
         }  
1332      }      }
1333    
1334      closesocket (fd);      closesocket (fd);
1335      if (start_key != 0)      if (start_key != 0)
1336          rc = WPTERR_WINSOCK_RECVKEY;          rc = WPTERR_WINSOCK_RECVKEY;
1337        *r_key = data_release_and_get_mem (raw, r_keylen);
1338      return rc;      return rc;
1339  }  }
1340    

Legend:
Removed from v.180  
changed lines
  Added in v.209

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26