/[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 229 by twoaday, Mon Jun 19 14:04:31 2006 UTC revision 276 by twoaday, Thu Dec 21 08:24:59 2006 UTC
# Line 29  Line 29 
29  #include <sys/stat.h>  #include <sys/stat.h>
30  #include <ctype.h>  #include <ctype.h>
31    
32    #include "wptGPG.h"
33  #include "wptKeyserver.h"  #include "wptKeyserver.h"
34  #include "wptErrors.h"  #include "wptErrors.h"
35  #include "wptTypes.h"  #include "wptTypes.h"
36  #include "wptNLS.h"  #include "wptNLS.h"
37  #include "wptW32API.h"  #include "wptW32API.h"
 #include "wptGPG.h"  
38  #include "wptRegistry.h"  #include "wptRegistry.h"
39  #include "wptUTF8.h"  #include "wptUTF8.h"
40    #include "wptVersion.h"
41    
42    
43  /* Map net_errno to a winsock error. */  /* Map net_errno to a winsock error. */
44  #define net_errno ((int)WSAGetLastError ())  #define net_errno ((int)WSAGetLastError ())
# Line 59  static int   hkp_err = 0;      /* != 0 indica Line 61  static int   hkp_err = 0;      /* != 0 indica
61  char *default_keyserver = NULL;  char *default_keyserver = NULL;
62  WORD default_keyserver_port = 0;  WORD default_keyserver_port = 0;
63    
64  /* Default socket timeout. */  /* Default socket timeout (secs). */
65  static int default_socket_timeout = 6;  static size_t default_socket_timeout = 6;
66    
67    
68  /* Wrapper for safe memory allocation. */  /* Wrapper for safe memory allocation. */
# Line 83  base64_encode (const char *inbuf, char * Line 85  base64_encode (const char *inbuf, char *
85      char base64code[] =      char base64code[] =
86          "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";          "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
87      int index = 0, temp = 0, res = 0;      int index = 0, temp = 0, res = 0;
88      int i = 0, inputlen = 0, len = 0;      int i, inputlen, len = 0;
89            
90      inputlen = strlen (inbuf);      inputlen = strlen (inbuf);
91      for (i = 0; i < inputlen; i++) {      for (i = 0; i < inputlen; i++) {
# Line 169  parse_keyserver_error (const char *resp, Line 171  parse_keyserver_error (const char *resp,
171    
172      /* If we find no 'Error' substring, we assume a success. */      /* If we find no 'Error' substring, we assume a success. */
173      if (!stristr (resp, "Error"))      if (!stristr (resp, "Error"))
174          return 0;          return -1;
175    
176      memset (txt, 0, txtlen);      memset (txt, 0, txtlen);
177      p = strstr (resp, "\r\n\r\n");      p = strstr (resp, "\r\n\r\n");
# Line 213  check_hkp_response (const char *resp, in Line 215  check_hkp_response (const char *resp, in
215          !strstr (resp, "HTTP/1.1 500 OK"))          !strstr (resp, "HTTP/1.1 500 OK"))
216          return ec; /* http error */          return ec; /* http error */
217    
218      if (parse_keyserver_error (resp, hkp_errmsg, sizeof (hkp_errmsg)-2)) {      if (!parse_keyserver_error (resp, hkp_errmsg, DIM (hkp_errmsg)-2)) {
219          if (!strlen (hkp_errmsg))          if (!strlen (hkp_errmsg))
220              _snprintf (hkp_errmsg, sizeof (hkp_errmsg)-1,              _snprintf (hkp_errmsg, DIM (hkp_errmsg)-1,
221                          "Unknown keyserver error");                          "Unknown keyserver error");
222          hkp_err = 1;          hkp_err = 1;
223          return ec;          return ec;
# Line 277  sock_select (int fd, int nsecs) Line 279  sock_select (int fd, int nsecs)
279     returned in @nbytes.     returned in @nbytes.
280     Return value: 0 on success. */     Return value: 0 on success. */
281  static int  static int
282  sock_read (int fd, char *buf, int buflen, int *nbytes)  sock_read (int fd, char *buf, size_t buflen, int *nbytes)
283  {  {
284      DWORD nread;      int nread;
285      int nleft = buflen;      size_t nleft = buflen;
286      int rc, n = 0;      size_t n = 0;
287        int rc;
288            
289      if (nbytes)      if (nbytes)
290          *nbytes = 0;          *nbytes = 0;
# Line 315  sock_read (int fd, char *buf, int buflen Line 318  sock_read (int fd, char *buf, int buflen
318  /* Helper to create a string from the gpgme data and  /* Helper to create a string from the gpgme data and
319     then release the gpgme data object. */     then release the gpgme data object. */
320  char*  char*
321  data_release_and_get_mem (gpgme_data_t in, int *r_bufferlen)  data_release_and_get_mem (gpgme_data_t in, size_t *r_bufferlen)
322  {  {
323      size_t n;      size_t n;
324      char *buffer, *p;      char *buffer, *p;
# Line 324  data_release_and_get_mem (gpgme_data_t i Line 327  data_release_and_get_mem (gpgme_data_t i
327      p = gpgme_data_release_and_get_mem (in, &n);      p = gpgme_data_release_and_get_mem (in, &n);
328      buffer = m_strdup (p);      buffer = m_strdup (p);
329      if (r_bufferlen)      if (r_bufferlen)
330          *r_bufferlen = (int)n;          *r_bufferlen = n;
331      gpgme_free (p);      gpgme_free (p);
332      return buffer;      return buffer;
333  }  }
# Line 334  data_release_and_get_mem (gpgme_data_t i Line 337  data_release_and_get_mem (gpgme_data_t i
337     return the data in @buffer. Caller must free data.     return the data in @buffer. Caller must free data.
338     Return value: 0 on success. */     Return value: 0 on success. */
339  int  int
340  sock_read_ext (int fd, char **buffer, int *r_bufferlen)  sock_read_ext (int fd, char **buffer, size_t *r_bufferlen)
341  {  {
342      gpgme_data_t dh;      gpgme_data_t dh;
343      char buf[1024];      char buf[1024];
344      size_t n=0;      size_t timeout=0;
345      int nread, rc;      int nread, rc;
346    
347      if (gpgme_data_new (&dh))      if (gpgme_data_new (&dh))
348          BUG (0);          return SOCKET_ERROR;
349      while (n < 10) {      while (timeout < default_socket_timeout) {
350          rc = sock_select (fd, 1);          rc = sock_select (fd, 1);
351          if (rc == SOCKET_ERROR) {          if (rc == SOCKET_ERROR) {
352              gpgme_data_release (dh);              gpgme_data_release (dh);
353              return rc;              return rc;
354          }          }
355          else if (!rc)          else if (!rc)
356              n++;              timeout++; /* socket not ready yet. */
357          else {          else {
358              nread = recv (fd, buf, sizeof (buf), 0);              nread = recv (fd, buf, DIM (buf), 0);
359              if (nread == SOCKET_ERROR)              if (nread == SOCKET_ERROR)
360                  return SOCKET_ERROR;                  return SOCKET_ERROR;
361              else if (!nread)              else if (!nread)
# Line 360  sock_read_ext (int fd, char **buffer, in Line 363  sock_read_ext (int fd, char **buffer, in
363              gpgme_data_write (dh, buf, nread);              gpgme_data_write (dh, buf, nread);
364          }          }
365      }      }
366        if (timeout >= default_socket_timeout)
367            return WPTERR_WINSOCK_TIMEOUT;
368      *buffer = data_release_and_get_mem (dh, r_bufferlen);      *buffer = data_release_and_get_mem (dh, r_bufferlen);
369      return 0;      return 0;
370  }  }
# Line 368  sock_read_ext (int fd, char **buffer, in Line 372  sock_read_ext (int fd, char **buffer, in
372    
373  /* Write the buffer @buf with the length @buflen to a socket @fd. */  /* Write the buffer @buf with the length @buflen to a socket @fd. */
374  static int  static int
375  sock_write (int fd, const char *buf, int buflen)  sock_write (int fd, const char *buf, size_t buflen)
376  {  {
377      DWORD nwritten;      int nwritten;
378      int nleft = buflen;      int nleft = buflen;
379    
380      while (nleft > 0) {      while (nleft > 0) {
# Line 408  wsock_end (void) Line 412  wsock_end (void)
412      char *p;      char *p;
413      int i;      int i;
414    
415    #ifdef WINPT_MOBILE
416        p = m_strdup ("keyserver.conf");
417    #else
418      p = make_special_filename (CSIDL_APPDATA, "winpt\\keyserver.conf", NULL);      p = make_special_filename (CSIDL_APPDATA, "winpt\\keyserver.conf", NULL);
419    #endif
420      kserver_save_conf (p);      kserver_save_conf (p);
421      free_if_alloc (p);      free_if_alloc (p);
422      free_if_alloc (default_keyserver);      free_if_alloc (default_keyserver);
423      for (i=0; i < MAX_KEYSERVERS; i++) {      for (i=0; i < MAX_KEYSERVERS; i++) {
424          if (server[i].used && server[i].name != NULL)          if (server[i].name != NULL)
425              free_if_alloc (server[i].name);              free_if_alloc (server[i].name);
426      }      }
427      kserver_proxy_release (&proxy);      kserver_proxy_release (&proxy);
# Line 464  wsock_strerror (void) Line 472  wsock_strerror (void)
472    
473  /* Set default socket timeout for all reading operations. */  /* Set default socket timeout for all reading operations. */
474  void  void
475  kserver_set_socket_timeout (int nsec)  kserver_set_socket_timeout (size_t nsec)
476  {  {
477      if (nsec < 0 || nsec > 3600)      if (nsec < 0 || nsec > 3600)
478          nsec = 0;          nsec = 0;
# Line 491  kserver_get_hostname (int idx, int type, Line 499  kserver_get_hostname (int idx, int type,
499          *port = default_keyserver_port;          *port = default_keyserver_port;
500          return default_keyserver;          return default_keyserver;
501      }      }
502      else if (!type && idx < DIM (server_list)) {      else if (!type && (size_t)idx < DIM (server_list)) {
503          /* XXX: handle non HKP servers. */          /* XXX: handle non HKP servers. */
504          *port = HKP_PORT;          *port = HKP_PORT;
505          return server_list[idx];          return server_list[idx];
# Line 529  kserver_check_keyid (const char *keyid) Line 537  kserver_check_keyid (const char *keyid)
537          return keyid; /* email address */          return keyid; /* email address */
538      if (strncmp (keyid, "0x", 2)) {      if (strncmp (keyid, "0x", 2)) {
539          memset (&id, 0, sizeof (id));          memset (&id, 0, sizeof (id));
540          _snprintf (id, sizeof (id)-1, "0x%s", keyid);          _snprintf (id, DIM (id)-1, "0x%s", keyid);
541          return id;          return id;
542      }      }
543      return keyid;      return keyid;
# Line 546  update_proxy_user (const char *proxy_use Line 554  update_proxy_user (const char *proxy_use
554      n = 4*strlen (proxy_user) / 3 + 32 + strlen (proxy_pass) + 2;      n = 4*strlen (proxy_user) / 3 + 32 + strlen (proxy_pass) + 2;
555      free_if_alloc (proxy.base64_user);      free_if_alloc (proxy.base64_user);
556      proxy.base64_user = safe_alloc (n+1);      proxy.base64_user = safe_alloc (n+1);
557      _snprintf (t, sizeof (t)-1, "%s:%s", proxy_user, proxy_pass);      _snprintf (t, DIM (t)-1, "%s:%s", proxy_user, proxy_pass);
558      base64_encode (t, proxy.base64_user);      base64_encode (t, proxy.base64_user);
559      free_if_alloc (proxy.user);      free_if_alloc (proxy.user);
560      free_if_alloc (proxy.pass);      free_if_alloc (proxy.pass);
# Line 647  kserver_save_conf (const char *conf) Line 655  kserver_save_conf (const char *conf)
655  }  }
656    
657    
658    /* Return the specified keyserver config setting @key as an integer. */
659    static int
660    get_conf_kserver_int (const char *key)
661    {
662        char *p;
663        int val = 0;
664    
665        p = get_reg_entry_keyserver (key);
666        if (p && *p)
667            val = atoi (p);
668        free_if_alloc (p);
669        return val;
670    }
671    
672    
673    /* Read the proxy configuration and store it into @prox. */
674    static void
675    read_proxy_config (keyserver_proxy_t prox)
676    {
677        char *proto;
678    
679        if (!prox)
680            return;
681        
682        proto = get_reg_entry_keyserver("Proto");
683        if (proto != NULL && strlen (proto) > 0)
684            prox->proto = atoi (proto);
685        else
686            prox->proto = PROXY_PROTO_NONE;
687        free_if_alloc (proto);
688        free_if_alloc (prox->host);
689        prox->host = get_reg_entry_keyserver ("Host");
690        free_if_alloc (prox->user);
691        prox->user = get_reg_entry_keyserver ("User");
692        free_if_alloc (prox->pass);
693        prox->pass = get_reg_entry_keyserver ("Pass");
694        prox->port = get_conf_kserver_int ("Port");
695    }
696    
697    
698  /* Load the keyserver config file @conf. */  /* Load the keyserver config file @conf. */
699  int  int
700  kserver_load_conf (const char *conf)  kserver_load_conf (const char *conf)
# Line 654  kserver_load_conf (const char *conf) Line 702  kserver_load_conf (const char *conf)
702      FILE *fp;      FILE *fp;
703      char buf[1024], *s, *p;      char buf[1024], *s, *p;
704      char *user = NULL, *pass = NULL;          char *user = NULL, *pass = NULL;    
705      int no_config=0, chk_pos=0;      int no_config=0, chk_pos;
706      int pos;      int pos;
707            
708      for (pos = 0; pos < MAX_KEYSERVERS; pos++) {      for (pos = 0; pos < MAX_KEYSERVERS; pos++) {
709          server[pos].used = 0;          server[pos].used = 0;
710          server[pos].port = HKP_PORT;          server[pos].port = HKP_PORT;
711            server[pos].name = NULL;
712      }      }
713            
714      fp = fopen (conf, "rb");      fp = fopen (conf, "rb");
# Line 669  kserver_load_conf (const char *conf) Line 718  kserver_load_conf (const char *conf)
718              server[pos].name = m_strdup (server_list[pos]);              server[pos].name = m_strdup (server_list[pos]);
719              server[pos].proto = proto_from_URL (server_list[pos]);              server[pos].proto = proto_from_URL (server_list[pos]);
720          }          }
721          no_config=1;          no_config = 1;
722      }      }
723      get_reg_proxy_prefs (&proxy);      read_proxy_config (&proxy);
724      if (user && pass)      if (user && pass)
725          update_proxy_user (user, pass);          update_proxy_user (user, pass);
726      else if (user && !pass || !user && pass) {      else if (user && !pass || !user && pass) {
727          msg_box (NULL, _("Invalid proxy configuration."          msg_box (NULL, _("Invalid proxy configuration. "
728                           "You need to set a user and a password"                           "You need to set a user and a password "
729                           "to use proxy authentication!"),                           "to use proxy authentication!"),
730                           _("Proxy Error"), MB_ERR);                           _("Proxy Error"), MB_ERR);
731      }      }
# Line 691  kserver_load_conf (const char *conf) Line 740  kserver_load_conf (const char *conf)
740            
741      pos = 0;      pos = 0;
742      while (fp && !feof (fp)) {      while (fp && !feof (fp)) {
743          s = fgets (buf, sizeof (buf)-1, fp);          s = fgets (buf, DIM (buf)-1, fp);
744          if (!s)          if (!s)
745              break;              break;
746          if (strstr (buf, "\r\n"))          if (strstr (buf, "\r\n"))
# Line 732  kserver_load_conf (const char *conf) Line 781  kserver_load_conf (const char *conf)
781      if (fp)      if (fp)
782          fclose (fp);          fclose (fp);
783      if (!pos) {      if (!pos) {
784          /* only issue an error if the config file exist but          /* Only issue an error if the config file exist but
785             it has no valid entries. */             it has no valid entries. */
786          keyserver_set_default (NULL, HKP_PORT);          keyserver_set_default (NULL, HKP_PORT);
787          if (!no_config)          if (!no_config)
# Line 752  kserver_connect (const char *hostname, W Line 801  kserver_connect (const char *hostname, W
801      struct sockaddr_in sock;      struct sockaddr_in sock;
802      char host[128] = {0};      char host[128] = {0};
803      DWORD iaddr;      DWORD iaddr;
804        bool use_proxy = proxy.host != NULL;
805      int rc, fd;      int rc, fd;
806    
     log_debug ("kserver_connect: %s:%d\r\n", hostname, port);  
   
807      if (!port)      if (!port)
808          port = HKP_PORT;          port = HKP_PORT;
809      if (conn_fd)      if (conn_fd)
810          *conn_fd = 0;          *conn_fd = 0;
811      hostname = skip_type_prefix (hostname);      hostname = skip_type_prefix (hostname);
812        log_debug ("kserver_connect: %s:%d\r\n", hostname, port);
813            
814      if (proxy.host && proxy.proto == PROXY_PROTO_HTTP)      if (use_proxy && proxy.proto == PROXY_PROTO_HTTP)
815          port = proxy.port;          port = proxy.port;
816      memset (&sock, 0, sizeof (sock));      memset (&sock, 0, sizeof (sock));
817      sock.sin_family = AF_INET;      sock.sin_family = AF_INET;
818      sock.sin_port = htons (port);      sock.sin_port = htons (port);
819      if (proxy.host)      strncpy (host, use_proxy? proxy.host : hostname, DIM (host)-1);
         strncpy (host, proxy.host, 127);  
     else  
         strncpy (host, hostname, 127);  
820    
821      if ((iaddr = inet_addr (host)) != INADDR_NONE)      if ((iaddr = inet_addr (host)) != INADDR_NONE)
822          memcpy (&sock.sin_addr, &iaddr, sizeof (iaddr));          memcpy (&sock.sin_addr, &iaddr, sizeof (iaddr));
# Line 783  kserver_connect (const char *hostname, W Line 829  kserver_connect (const char *hostname, W
829      }      }
830      else {      else {
831          log_debug ("gethostbyname: failed ec=%d.\r\n", net_errno);          log_debug ("gethostbyname: failed ec=%d.\r\n", net_errno);
832          return WPTERR_WINSOCK_RESOLVE;          return use_proxy? WPTERR_WINSOCK_PROXY : WPTERR_WINSOCK_RESOLVE;
833      }      }
834      fd = socket (AF_INET, SOCK_STREAM, 0);      fd = socket (AF_INET, SOCK_STREAM, 0);
835      if (fd == INVALID_SOCKET)        if (fd == INVALID_SOCKET)  
# Line 792  kserver_connect (const char *hostname, W Line 838  kserver_connect (const char *hostname, W
838      if (rc == SOCKET_ERROR) {      if (rc == SOCKET_ERROR) {
839          log_debug ("connect: failed ec=%d.\r\n", net_errno);          log_debug ("connect: failed ec=%d.\r\n", net_errno);
840          closesocket (fd);          closesocket (fd);
841          return WPTERR_WINSOCK_CONNECT;          return use_proxy? WPTERR_WINSOCK_PROXY : WPTERR_WINSOCK_CONNECT;
842      }      }
843    
844      if (proxy.proto == PROXY_PROTO_SOCKS5) {      if (proxy.proto == PROXY_PROTO_SOCKS5) {
845            /* XXX: finish code and provide errors constants. */
846          rc = socks_handshake (&proxy, fd, hostname, port);          rc = socks_handshake (&proxy, fd, hostname, port);
847          if (rc) {          if (rc) {
848              closesocket (fd);              closesocket (fd);
849              return WPTERR_GENERAL; /* XXX: use better code. */              return WPTERR_GENERAL;
850          }          }
851      }      }
852    
# Line 826  URL_encode (const char *url, DWORD ulen, Line 873  URL_encode (const char *url, DWORD ulen,
873  {  {
874      gpgme_data_t hd;      gpgme_data_t hd;
875      char numbuf[5], *p;      char numbuf[5], *p;
876      size_t i;      size_t i, nlen;
     int nlen;  
877    
878      if (gpgme_data_new (&hd))      if (gpgme_data_new (&hd))
879          BUG (0);          BUG (0);
# Line 910  kserver_send_request (const char *hostna Line 956  kserver_send_request (const char *hostna
956  /* Interface for receiving a public key. */  /* Interface for receiving a public key. */
957  int  int
958  kserver_recvkey (const char *hostname, WORD port, const char *keyid,  kserver_recvkey (const char *hostname, WORD port, const char *keyid,
959                   char **r_key, int *r_keylen)                   char **r_key, size_t *r_keylen)
960  {  {
961      const char *fmt;      const char *fmt;
962      char *request = NULL;      char *request = NULL;
# Line 977  leave: Line 1023  leave:
1023    
1024  /* Interface to send a public key. */  /* Interface to send a public key. */
1025  int  int
1026  kserver_sendkey (const char *hostname, WORD port, const char *pubkey, int len )  kserver_sendkey (const char *hostname, WORD port,
1027                     const char *pubkey, size_t len)
1028  {  {
1029      char *request = NULL;      char *request = NULL;
1030      char log[2048] = {0};      char log[2048] = {0};
# Line 990  kserver_sendkey (const char *hostname, W Line 1037  kserver_sendkey (const char *hostname, W
1037          goto leave;          goto leave;
1038            
1039      request = kserver_send_request (hostname, port, pubkey, len);      request = kserver_send_request (hostname, port, pubkey, len);
     if (request == NULL) {  
         rc = WPTERR_GENERAL;  
         goto leave;      
     }  
       
1040      rc = sock_write (conn_fd, request, strlen (request));      rc = sock_write (conn_fd, request, strlen (request));
1041      if (rc == SOCKET_ERROR) {      if (rc == SOCKET_ERROR) {
1042          rc = WPTERR_WINSOCK_SENDKEY;          rc = WPTERR_WINSOCK_SENDKEY;
1043          goto leave;              goto leave;    
1044      }      }
1045            
1046      rc = sock_read (conn_fd, log, sizeof (log)-1, &n);      rc = sock_read (conn_fd, log, DIM (log)-1, &n);
1047      if (rc == SOCKET_ERROR) {      if (rc == SOCKET_ERROR) {
1048          rc = WPTERR_WINSOCK_SENDKEY;          rc = WPTERR_WINSOCK_SENDKEY;
1049          goto leave;          goto leave;
# Line 1013  kserver_sendkey (const char *hostname, W Line 1055  kserver_sendkey (const char *hostname, W
1055          goto leave;          goto leave;
1056            
1057      WSASetLastError (0);      WSASetLastError (0);
1058        
1059  leave:  leave:
1060      closesocket (conn_fd);      closesocket (conn_fd);
1061      free_if_alloc (request);      free_if_alloc (request);
# Line 1029  kserver_search_chkresp (int fd) Line 1071  kserver_search_chkresp (int fd)
1071      int n=0;      int n=0;
1072            
1073      /* parse response 'HTTP/1.0 500 OK' */      /* parse response 'HTTP/1.0 500 OK' */
1074      if (sock_getline (fd, buf, sizeof (buf)-1, &n))      if (sock_getline (fd, buf, DIM (buf)-1, &n))
1075          return WPTERR_KEYSERVER_NOTFOUND;          return WPTERR_KEYSERVER_NOTFOUND;
1076    
1077      log_debug ("kserver_search_chkpresp: %s\r\n", buf);      log_debug ("kserver_search_chkpresp: %s\r\n", buf);
# Line 1051  kserver_search_end (int conn_fd) Line 1093  kserver_search_end (int conn_fd)
1093    
1094    
1095  /* Extract the amount of keys from the info record. */  /* Extract the amount of keys from the info record. */
1096  static int  static size_t
1097  count_keys_in_response (char *buf)  count_keys_in_response (char *buf)
1098  {  {
1099      char *p;      char *p;
1100      int recno = 0;      int recno = 0;
1101      int n = 0;      size_t n = 0;
1102    
1103      /* info:1:4 */      /* info:1:4 */
1104      if (strncmp (buf, "info", 4))      if (strncmp (buf, "info", 4))
# Line 1079  count_keys_in_response (char *buf) Line 1121  count_keys_in_response (char *buf)
1121     the amount of keys which were found. */     the amount of keys which were found. */
1122  int  int
1123  kserver_search_begin (const char *hostname, WORD port,  kserver_search_begin (const char *hostname, WORD port,
1124                        const char *pattern, int *conn_fd, int *nkeys)                        const char *pattern, int *conn_fd, size_t *nkeys)
1125  {  {
1126      const char *fmt;      const char *fmt;
1127      char *request = NULL;      char *request = NULL;
# Line 1134  kserver_search_begin (const char *hostna Line 1176  kserver_search_begin (const char *hostna
1176    
1177      /* Skip all lines until we reach the "info:" record. */      /* Skip all lines until we reach the "info:" record. */
1178      for (;;) {      for (;;) {
1179          if (sock_getline (sock_fd, status, sizeof (status)-1, &reqlen)) {                if (sock_getline (sock_fd, status, DIM (status)-1, &reqlen)) {  
1180              log_debug ("kserver_search_begin: retrieving status line failed.\r\n");              log_debug ("kserver_search_begin: retrieving status line failed.\r\n");
1181              closesocket (sock_fd);              closesocket (sock_fd);
1182              sock_fd = 0;              sock_fd = 0;
# Line 1163  parse_pub_record (keyserver_key_s *key, Line 1205  parse_pub_record (keyserver_key_s *key,
1205      enum pub_rec_t {ID=1, KEYID, ALGO, SIZE, CREATE, EXPIRE};      enum pub_rec_t {ID=1, KEYID, ALGO, SIZE, CREATE, EXPIRE};
1206      char *p;      char *p;
1207      int recno = 0;      int recno = 0;
1208        int off = 0;
1209    
1210      /* pub:BF3DF9B4:17:1024:925411133:: */      /* pub:BF3DF9B4:17:1024:925411133:: */
1211      p = strtok (buf, ":");      p = strtok (buf, ":");
# Line 1172  parse_pub_record (keyserver_key_s *key, Line 1215  parse_pub_record (keyserver_key_s *key,
1215          case ID:          case ID:
1216              break;              break;
1217    
1218          case KEYID:          case KEYID:
1219                /* If for any reason the returned record actually contains
1220                   a fingerprint instead of a key ID, we truncate the fpr. */
1221                off = strlen (p) == 40? 32 : 0;
1222              free_if_alloc (key->keyid);              free_if_alloc (key->keyid);
1223              key->keyid = m_strdup (p);              key->keyid = m_strdup (p+off);
1224              break;              break;
1225    
1226          case ALGO:          case ALGO:
# Line 1195  parse_pub_record (keyserver_key_s *key, Line 1241  parse_pub_record (keyserver_key_s *key,
1241          }          }
1242          p = strtok (NULL, ":");          p = strtok (NULL, ":");
1243      }      }
       
1244  }  }
1245    
1246    
# Line 1279  kserver_search_next (int fd, keyserver_k Line 1324  kserver_search_next (int fd, keyserver_k
1324      key = new keyserver_key_s;      key = new keyserver_key_s;
1325      memset (key, 0, sizeof *key);      memset (key, 0, sizeof *key);
1326      for (;;) {      for (;;) {
1327          if (sock_getline (fd, buf, sizeof (buf)-1, &n))          if (sock_getline (fd, buf, DIM (buf)-1, &n))
1328              break;              break;
1329          /*log_debug ("record: '%s'\r\n", buf); */          /*log_debug ("record: '%s'\r\n", buf); */
1330          if (!strncmp (buf, "pub", 3))          if (!strncmp (buf, "pub", 3))
# Line 1386  do_spawn_ldap_helper (const char *host, Line 1431  do_spawn_ldap_helper (const char *host,
1431          fp = NULL;          fp = NULL;
1432          goto leave;          goto leave;
1433      }      }
1434      get_temp_name (outf, sizeof (outf)-1, keyid);      get_temp_name (outf, DIM (outf)-1, keyid);
1435      get_temp_name (inf, sizeof (inf)-1, NULL);      get_temp_name (inf, DIM (inf)-1, NULL);
1436      fp = fopen (inf, "w+b");      fp = fopen (inf, "w+b");
1437      if (!fp) {      if (!fp) {
1438          log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", inf,          log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", inf,
# Line 1396  do_spawn_ldap_helper (const char *host, Line 1441  do_spawn_ldap_helper (const char *host,
1441      }      }
1442      fprintf (fp,      fprintf (fp,
1443          "VERSION 1\n"          "VERSION 1\n"
1444          "PROGRAM 1.4.3-cvs\n"          "PROGRAM %d.%d.%d\n"
1445          "SCHEME ldap\n"          "SCHEME ldap\n"
1446          "HOST %s\n"          "HOST %s\n"
1447          "COMMAND GET\n"          "COMMAND GET\n"
1448          "\n"          "\n"
1449          "%s\n",          "%s\n",
1450            gpgver[0], gpgver[1], gpgver[2],
1451          host? skip_type_prefix (host): "64.94.85.200", keyid);          host? skip_type_prefix (host): "64.94.85.200", keyid);
1452      fclose (fp);      fclose (fp);
1453    
# Line 1428  leave: Line 1474  leave:
1474     @key contains the key on success. */     @key contains the key on success. */
1475  int  int
1476  ldap_recvkey (const char *host, const char *keyid,  ldap_recvkey (const char *host, const char *keyid,
1477                char **r_key, int *r_keylen)                char **r_key, size_t *r_keylen)
1478  {    {  
1479      gpgme_data_t raw;      gpgme_data_t raw;
1480      FILE *fp;      FILE *fp;
# Line 1444  ldap_recvkey (const char *host, const ch Line 1490  ldap_recvkey (const char *host, const ch
1490      if (gpgme_data_new (&raw))      if (gpgme_data_new (&raw))
1491          BUG (0);          BUG (0);
1492      while (!feof (fp)) {      while (!feof (fp)) {
1493          s = fgets (buf, sizeof (buf)-1, fp);          s = fgets (buf, DIM (buf)-1, fp);
1494          if (!s)          if (!s)
1495              break;              break;
1496          if (strstr (s, "KEY") && strstr (s, "FAILED")) {          if (strstr (s, "KEY") && strstr (s, "FAILED")) {
# Line 1474  ldap_recvkey (const char *host, const ch Line 1520  ldap_recvkey (const char *host, const ch
1520     On success @key contains the key. */     On success @key contains the key. */
1521  int  int
1522  finger_recvkey (const char *host, const char *user,  finger_recvkey (const char *host, const char *user,
1523                  char **r_key, int *r_keylen)                  char **r_key, size_t *r_keylen)
1524  {  {
1525      gpgme_data_t raw;      gpgme_data_t raw;
1526      char buf[256];      char buf[256+1];
1527      int fd, nread;      int fd, nread;
1528      int start_key = 0;      int start_key = 0;
1529      int rc=0;      int rc=0;
# Line 1490  finger_recvkey (const char *host, const Line 1536  finger_recvkey (const char *host, const
1536      sock_write (fd, "\r\n", 2);      sock_write (fd, "\r\n", 2);
1537    
1538      if (gpgme_data_new (&raw))      if (gpgme_data_new (&raw))
1539          BUG (0);          return WPTERR_WINSOCK_RECVKEY;
1540    
1541      for (;;) {      for (;;) {
1542          if (sock_getline (fd, buf, sizeof (buf), &nread))          if (sock_getline (fd, buf, DIM (buf)-2, &nread))
1543              break;              break;
1544          strcat (buf, "\n");          strcat (buf, "\n");
1545          if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) {          if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) {
# Line 1517  finger_recvkey (const char *host, const Line 1563  finger_recvkey (const char *host, const
1563  }  }
1564    
1565    
1566  /* Check if the given name @name is a valid hostname. */  /* Check if the given name @name is a valid inernet address
1567       which means an dotted IP or a valid DNS name.
1568       Return value: 0 on success. */
1569  int  int
1570  check_IP_or_hostname (const char *name)  check_inet_address (const char *addr)
1571  {  {
1572      const char *not_allowed = "=!�$%&@#*~\\/}][{<>|,;:'";      const char *not_allowed = "=!�$%&@#*~\\/}][{<>|,;:'";
1573      size_t i, j;      size_t i;
1574    
1575      for (i=0; i < strlen (name); i++) {      for (i=0; i < strlen (addr); i++) {
1576          for (j =0; j < strlen (not_allowed); j++) {          if (strchr (not_allowed, addr[i]))
1577              if (name[i] == not_allowed[j])              return -1;
                 return -1;  
         }  
1578      }      }
1579      return 0;      return 0;
1580  }  }

Legend:
Removed from v.229  
changed lines
  Added in v.276

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26