/[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 214 by twoaday, Sun May 14 18:40:36 2006 UTC revision 226 by twoaday, Mon Jun 12 13:40:21 2006 UTC
# Line 36  Line 36 
36  #include "wptW32API.h"  #include "wptW32API.h"
37  #include "wptGPG.h"  #include "wptGPG.h"
38  #include "wptRegistry.h"  #include "wptRegistry.h"
39    #include "wptUTF8.h"
40    
41  /* Map net_errno to a winsock error. */  /* Map net_errno to a winsock error. */
42  #define net_errno ((int)WSAGetLastError ())  #define net_errno ((int)WSAGetLastError ())
# Line 44  Line 45 
45  keyserver server[MAX_KEYSERVERS] = {0};  keyserver server[MAX_KEYSERVERS] = {0};
46  keyserver_proxy_s proxy = {0};  keyserver_proxy_s proxy = {0};
47  static const char *server_list[] = {  static const char *server_list[] = {
     "hkp://gnv.us.ks.cryptnet.net",  
     "hkp://keyserver.kjsl.com",  
48      "hkp://sks.keyserver.penguin.de",      "hkp://sks.keyserver.penguin.de",
49      "hkp://subkeys.pgp.net",      "hkp://subkeys.pgp.net",
50      "ldap://keyserver.pgp.com",      "ldap://keyserver.pgp.com",
# Line 122  base64_encode (const char *inbuf, char * Line 121  base64_encode (const char *inbuf, char *
121  }  }
122    
123    
124    /* Check that the given buffer contains a valid keyserver URL
125       and return the prefix length, 0 in case of an error. */
126    static int
127    check_URL (const char *buf)
128    {
129        const char *proto[] = {
130            "ldap://",
131            "http://",
132            "finger://",
133            "hkp://",
134            NULL};
135        int i;
136    
137        if (strlen (buf) < 7)
138            return 0;
139    
140        for (i=0; proto[i] != NULL; i++) {
141            if (strstr (buf, proto[i]))
142                return strlen (proto[i]);
143        }
144        return 0;
145    }
146    
147    
148  /* Skip the URL schema and return only the host part of it. */  /* Skip the URL schema and return only the host part of it. */
149  static const char*  static const char*
150  skip_type_prefix (const char *hostname)  skip_type_prefix (const char *hostname)
151  {  {
152      if (hostname && !strncmp (hostname, "http://", 7))      int pos;
153          hostname += 7;  
154      else if (hostname && !strncmp (hostname, "hkp://", 6))      if (!hostname)
155          hostname += 6;          return hostname;
156      else if (hostname && !strncmp (hostname, "finger://", 9))  
157          hostname += 9;      pos = check_URL (hostname);
158      else if (hostname && !strncmp (hostname, "ldap://", 7))      if (pos > 0)
159          hostname += 7;          hostname += pos;
160      return hostname;      return hostname;
161  }  }
162    
# Line 146  check_hkp_response (const char *resp, in Line 169  check_hkp_response (const char *resp, in
169      char *p, *end;      char *p, *end;
170      int ec, len;      int ec, len;
171    
172        log_debug ("check_hkp_response: '%s'\r\n", resp);
173      ec = recv ? WPTERR_WINSOCK_RECVKEY : WPTERR_WINSOCK_SENDKEY;      ec = recv ? WPTERR_WINSOCK_RECVKEY : WPTERR_WINSOCK_SENDKEY;
174      if (!strstr (resp, "HTTP/1.0 200 OK") &&      if (!strstr (resp, "HTTP/1.0 200 OK") &&
175          !strstr (resp, "HTTP/1.1 200 OK")) /* http error */          !strstr (resp, "HTTP/1.1 200 OK")) /* http error */
# Line 154  check_hkp_response (const char *resp, in Line 178  check_hkp_response (const char *resp, in
178          || strstr (resp, "Public Key Server -- Error")          || strstr (resp, "Public Key Server -- Error")
179          || strstr (resp, "No matching keys in database")) {          || strstr (resp, "No matching keys in database")) {
180          p = strstr (resp, "<p>");          p = strstr (resp, "<p>");
181          if (p && strlen (p) < sizeof (hkp_errmsg)) {          if (p && strlen (p) < sizeof (hkp_errmsg)-1) {
182              end = strstr (p, "</p>");              end = strstr (p, "</p>");
183              len = end? (end - p + 1) : strlen (p);              len = end? (end - p + 1) : strlen (p);
184              memset (hkp_errmsg, 0, sizeof (hkp_errmsg));              memset (hkp_errmsg, 0, sizeof (hkp_errmsg));
# Line 361  wsock_end (void) Line 385  wsock_end (void)
385      }      }
386      kserver_proxy_release (&proxy);      kserver_proxy_release (&proxy);
387      WSACleanup ();      WSACleanup ();
388        log_debug ("wsock_end: cleanup finished.\r\n");
389  }  }
390    
391    
# Line 408  wsock_strerror (void) Line 433  wsock_strerror (void)
433  void  void
434  kserver_set_socket_timeout (int nsec)  kserver_set_socket_timeout (int nsec)
435  {  {
436      if (nsec < 0)      if (nsec < 0 || nsec > 3600)
437          nsec = 0;          nsec = 0;
438      default_socket_timeout = nsec;      default_socket_timeout = nsec;
439  }  }
# Line 434  kserver_get_hostname (int idx, int type, Line 459  kserver_get_hostname (int idx, int type,
459          return default_keyserver;          return default_keyserver;
460      }      }
461      else if (!type && idx < DIM (server_list)) {      else if (!type && idx < DIM (server_list)) {
462            /* XXX: handle non HKP servers. */
463          *port = HKP_PORT;          *port = HKP_PORT;
464          return server_list[idx];          return server_list[idx];
465      }      }
# Line 452  kserver_check_inet_connection (void) Line 478  kserver_check_inet_connection (void)
478          closesocket (fd);          closesocket (fd);
479          return 0;          return 0;
480      }      }
481        log_debug ("kserver_check_inet_connection: no inet connection.\r\n");
482      return -1;      return -1;
483  }  }
484    
# Line 463  kserver_check_inet_connection (void) Line 490  kserver_check_inet_connection (void)
490  const char*  const char*
491  kserver_check_keyid (const char *keyid)  kserver_check_keyid (const char *keyid)
492  {        {      
493      static char id[21];      static char id[22];
494    
495      if (strstr (keyid, "@"))      if (strstr (keyid, "@"))
496          return keyid; /* email address */          return keyid; /* email address */
# Line 493  update_proxy_user (const char *proxy_use Line 520  update_proxy_user (const char *proxy_use
520      proxy.user = m_strdup (proxy_user);      proxy.user = m_strdup (proxy_user);
521      proxy.pass = m_strdup (proxy_pass);      proxy.pass = m_strdup (proxy_pass);
522  }  }
   
   
 /* Check that the given buffer contains a valid keyserver URL. */  
 static int  
 check_URL (const char * buf)  
 {  
     if (strlen (buf) < 7)  
         return -1;  
     if (!strstr (buf, "ldap://")  
         && !strstr (buf, "http://")  
         && !strstr (buf, "finger://")  
         && !strstr (buf, "hkp://"))  
         return -1;  
     return 0;  
 }  
523            
524    
525  /* Get the port number from the given protocol. */  /* Get the port number from the given protocol. */
# Line 515  static int Line 527  static int
527  port_from_proto (int proto)  port_from_proto (int proto)
528  {  {
529      switch (proto) {      switch (proto) {
530      case KSPROTO_LDAP:   return 0;      case KSPROTO_LDAP:   return LDAP_PORT;
531      case KSPROTO_FINGER: return FINGER_PORT;      case KSPROTO_FINGER: return FINGER_PORT;
532      case KSPROTO_HTTP:   return HKP_PORT;      case KSPROTO_HTTP:   return HKP_PORT;
533      }      }
# Line 529  proto_from_URL (const char *buf) Line 541  proto_from_URL (const char *buf)
541  {  {
542      if (strstr (buf, "ldap"))      if (strstr (buf, "ldap"))
543          return KSPROTO_LDAP;          return KSPROTO_LDAP;
544      else if (strstr( buf, "finger"))      else if (strstr (buf, "finger"))
545          return KSPROTO_FINGER;          return KSPROTO_FINGER;
546      return KSPROTO_HKP;      return KSPROTO_HKP;
547  }  }
548    
549    
550    /* Set the default keyserver.
551       If @hostname is NULL, use the predefined keyserver. */
552  void  void
553  keyserver_set_default (const char *hostname, WORD port)  keyserver_set_default (const char *hostname, WORD port)
554  {  {
555        if (!port)
556            port = HKP_PORT;
557      if (hostname != NULL) {      if (hostname != NULL) {
558          free_if_alloc (default_keyserver);          free_if_alloc (default_keyserver);
559          default_keyserver = m_strdup (hostname);          default_keyserver = m_strdup (hostname);
560          default_keyserver_port = port;          default_keyserver_port = port;
561      }      }
562      if (!port)      if (!default_keyserver) {
         port = HKP_PORT;  
     if (!default_keyserver)  
563          default_keyserver = m_strdup (DEF_HKP_KEYSERVER);          default_keyserver = m_strdup (DEF_HKP_KEYSERVER);
564            default_keyserver_port = HKP_PORT;
565        }
566      server[0].name =  m_strdup (default_keyserver);      server[0].name =  m_strdup (default_keyserver);
567      server[0].used = 1;      server[0].used = 1;
568      server[0].port = port;      server[0].port = port;
# Line 652  kserver_load_conf (const char *conf) Line 668  kserver_load_conf (const char *conf)
668          s = (char *)skip_whitespace (buf);          s = (char *)skip_whitespace (buf);
669          if (*s == '#' || strlen (s) < 7)          if (*s == '#' || strlen (s) < 7)
670              continue;              continue;
671          if (check_URL (s)) {          chk_pos = check_URL (s);
672            if (!chk_pos) {
673              msg_box (NULL, _("All entries of this file must have a valid prefix.\n"              msg_box (NULL, _("All entries of this file must have a valid prefix.\n"
674                               "Currently HKP/HTTP, LDAP and FINGER are supported.\n"),                               "Currently HKP/HTTP, LDAP and FINGER are supported.\n"),
675                               _("Keyserver Error"), MB_ERR);                               _("Keyserver Error"), MB_ERR);
676              continue;              continue;
677          }          }
         chk_pos = 6;  
         if (strstr (s, "finger"))  
             chk_pos = 10;  
678          p = strchr (s+chk_pos, ':');          p = strchr (s+chk_pos, ':');
679          server[pos].used = 1;          server[pos].used = 1;
680          server[pos].proto = proto_from_URL (s);          server[pos].proto = proto_from_URL (s);
# Line 763  kserver_connect (const char *hostname, W Line 777  kserver_connect (const char *hostname, W
777  }  }
778    
779    
780    /* Check if the URL needs to be encoded or not. */
781  static bool  static bool
782  URL_must_encoded (const char *url)  URL_must_encoded (const char *url)
783  {  {
# Line 854  kserver_send_request (const char *hostna Line 869  kserver_send_request (const char *hostna
869      }      }
870    
871      free_if_alloc (enc_pubkey);      free_if_alloc (enc_pubkey);
872      log_debug ("%s\r\n", request);      log_debug ("request:\r\n%s\r\n", request);
873      return request;      return request;
874  }  }
875    
# Line 899  kserver_recvkey (const char *hostname, W Line 914  kserver_recvkey (const char *hostname, W
914          _snprintf (request, reqlen, fmt, keyid);          _snprintf (request, reqlen, fmt, keyid);
915      }      }
916    
917      log_debug ("%s\r\n", request);      log_debug ("request:\r\n%s\r\n", request);
918            
919      rc = sock_write (conn_fd, request, strlen (request));      rc = sock_write (conn_fd, request, strlen (request));
920      if (rc == SOCKET_ERROR) {      if (rc == SOCKET_ERROR) {
# Line 913  kserver_recvkey (const char *hostname, W Line 928  kserver_recvkey (const char *hostname, W
928          goto leave;          goto leave;
929      }      }
930    
931      log_debug ("%s\r\n", *r_key);      log_debug ("response:\r\n%s\r\n", *r_key);
932      rc = check_hkp_response (*r_key, 1);      rc = check_hkp_response (*r_key, 1);
933      if (rc)      if (rc)
934          goto leave;          goto leave;
# Line 1157  parse_uid_record (keyserver_key_s *key, Line 1172  parse_uid_record (keyserver_key_s *key,
1172  {  {
1173      enum uid_rec_t {ID=1, UID, CREATE, EXPIRE};      enum uid_rec_t {ID=1, UID, CREATE, EXPIRE};
1174      keyserver_uid_s *u, *n;      keyserver_uid_s *u, *n;
1175      char *p;      char *p, *raw;
1176      int recno = 0;      int recno = 0;
1177    
1178      /* uid:Timo Schulz <[email protected]>:1138440360:: */      /* uid:Timo Schulz <[email protected]>:1138440360:: */
# Line 1173  parse_uid_record (keyserver_key_s *key, Line 1188  parse_uid_record (keyserver_key_s *key,
1188              break;              break;
1189    
1190          case UID:          case UID:
1191              /* XXX: search for %3A and decode it back to ':' */              unhexify_buffer (p, &raw);
1192              u->uid = m_strdup (p);              u->uid = utf8_to_native (raw);
1193                free_if_alloc (raw);
1194              break;              break;
1195    
1196          case CREATE:          case CREATE:
# Line 1232  kserver_search_next (int fd, keyserver_k Line 1248  kserver_search_next (int fd, keyserver_k
1248      for (;;) {      for (;;) {
1249          if (sock_getline (fd, buf, sizeof (buf)-1, &n))          if (sock_getline (fd, buf, sizeof (buf)-1, &n))
1250              break;              break;
1251            /*log_debug ("record: '%s'\r\n", buf); */
1252          if (!strncmp (buf, "pub", 3))          if (!strncmp (buf, "pub", 3))
1253              parse_pub_record (key, buf);              parse_pub_record (key, buf);
1254          else          else
# Line 1491  parse_keyserver_url (char **r_keyserver, Line 1508  parse_keyserver_url (char **r_keyserver,
1508  {  {
1509      char *p;      char *p;
1510      char *url = *r_keyserver;      char *url = *r_keyserver;
1511        int off = 0;
1512    
1513      /* no port is given so use the default port. */      /* no port is given so use the default port. */
1514      p = strrchr (url, ':');      p = strrchr (url, ':');
# Line 1501  parse_keyserver_url (char **r_keyserver, Line 1519  parse_keyserver_url (char **r_keyserver,
1519          *r_port = port;          *r_port = port;
1520          return 0;          return 0;
1521      }      }
1522      /* XXX: remove / in .de/:11371 */  
1523      *r_keyserver = substr (url, 0, (p-url));          if (url[(p-url)-1] == '/') /* remove / in .de/:11371 */
1524            off = 1;
1525    
1526        *r_keyserver = substr (url, 0, (p-url)-off);
1527      *r_port = atoi (url+(p-url)+1);      *r_port = atoi (url+(p-url)+1);
1528      safe_free (url);      free_if_alloc (url);
1529      return 0;      return 0;
1530  }  }

Legend:
Removed from v.214  
changed lines
  Added in v.226

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26