/[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 270 by twoaday, Sat Oct 21 18:08:57 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    
163    /* Parse the keyserver response and extract the human
164       readable text passages. */
165    static int
166    parse_keyserver_error (const char *resp, char *txt, size_t txtlen)
167    {
168        char *p, *p2;
169    
170        /* If we find no 'Error' substring, we assume a success. */
171        if (!stristr (resp, "Error"))
172            return -1;
173    
174        memset (txt, 0, txtlen);
175        p = strstr (resp, "\r\n\r\n");
176        if (!p)
177            return -1;
178        resp += (p-resp);
179        p = strstr (resp, "<h1>");
180        if (!p)
181            return -1;
182        resp += (p-resp)+strlen ("<h1>");
183        p = strstr (resp, "</h1>");
184        if (!p)
185            return -1;
186        resp += (p-resp)+strlen ("</h1>");
187        p2 = strstr (resp, "</body>");
188        if (p2 != NULL)
189            memcpy (txt, resp, (p2-resp));
190        else {
191            if (!strnicmp (resp, "<p>", 3))
192                resp += 3;
193            while (resp && *resp == '\r' || *resp == '\n')
194                resp++;
195            memcpy (txt, resp, strlen (resp));
196        }
197        return 0;
198    }
199    
200    
201  /* Check that the keyserver response indicates an OK.  /* Check that the keyserver response indicates an OK.
202     Return 0 on success. */     Return 0 on success. */
203  static int  static int
204  check_hkp_response (const char *resp, int recv)  check_hkp_response (const char *resp, int recv)
205  {      {
206      char *p, *end;      int ec;
     int ec, len;  
207    
208        log_debug ("check_hkp_response: '%s'\r\n", resp);
209      ec = recv ? WPTERR_WINSOCK_RECVKEY : WPTERR_WINSOCK_SENDKEY;      ec = recv ? WPTERR_WINSOCK_RECVKEY : WPTERR_WINSOCK_SENDKEY;
210      if (!strstr (resp, "HTTP/1.0 200 OK") &&      if (!strstr (resp, "HTTP/1.0 200 OK") &&
211          !strstr (resp, "HTTP/1.1 200 OK")) /* http error */          !strstr (resp, "HTTP/1.1 200 OK") &&
212          return ec;          !strstr (resp, "HTTP/1.0 500 OK") &&
213      if (strstr (resp, "Public Key Server -- Error")          !strstr (resp, "HTTP/1.1 500 OK"))
214          || strstr (resp, "Public Key Server -- Error")          return ec; /* http error */
215          || strstr (resp, "No matching keys in database")) {  
216          p = strstr (resp, "<p>");      if (!parse_keyserver_error (resp, hkp_errmsg, sizeof (hkp_errmsg)-2)) {
217          if (p && strlen (p) < sizeof (hkp_errmsg)) {          if (!strlen (hkp_errmsg))
218              end = strstr (p, "</p>");              _snprintf (hkp_errmsg, sizeof (hkp_errmsg)-1,
219              len = end? (end - p + 1) : strlen (p);                          "Unknown keyserver error");
220              memset (hkp_errmsg, 0, sizeof (hkp_errmsg));          hkp_err = 1;
             strncpy (hkp_errmsg, p, len);  
             hkp_err = 1;  
         }  
221          return ec;          return ec;
222      }      }
223      return 0;      return 0;
# Line 361  wsock_end (void) Line 418  wsock_end (void)
418      }      }
419      kserver_proxy_release (&proxy);      kserver_proxy_release (&proxy);
420      WSACleanup ();      WSACleanup ();
421        log_debug ("wsock_end: cleanup finished.\r\n");
422  }  }
423    
424    
# Line 408  wsock_strerror (void) Line 466  wsock_strerror (void)
466  void  void
467  kserver_set_socket_timeout (int nsec)  kserver_set_socket_timeout (int nsec)
468  {  {
469      if (nsec < 0)      if (nsec < 0 || nsec > 3600)
470          nsec = 0;          nsec = 0;
471      default_socket_timeout = nsec;      default_socket_timeout = nsec;
472  }  }
# Line 434  kserver_get_hostname (int idx, int type, Line 492  kserver_get_hostname (int idx, int type,
492          return default_keyserver;          return default_keyserver;
493      }      }
494      else if (!type && idx < DIM (server_list)) {      else if (!type && idx < DIM (server_list)) {
495            /* XXX: handle non HKP servers. */
496          *port = HKP_PORT;          *port = HKP_PORT;
497          return server_list[idx];          return server_list[idx];
498      }      }
# Line 452  kserver_check_inet_connection (void) Line 511  kserver_check_inet_connection (void)
511          closesocket (fd);          closesocket (fd);
512          return 0;          return 0;
513      }      }
514        log_debug ("kserver_check_inet_connection: no inet connection.\r\n");
515      return -1;      return -1;
516  }  }
517    
# Line 463  kserver_check_inet_connection (void) Line 523  kserver_check_inet_connection (void)
523  const char*  const char*
524  kserver_check_keyid (const char *keyid)  kserver_check_keyid (const char *keyid)
525  {        {      
526      static char id[21];      static char id[22];
527    
528      if (strstr (keyid, "@"))      if (strstr (keyid, "@"))
529          return keyid; /* email address */          return keyid; /* email address */
# Line 493  update_proxy_user (const char *proxy_use Line 553  update_proxy_user (const char *proxy_use
553      proxy.user = m_strdup (proxy_user);      proxy.user = m_strdup (proxy_user);
554      proxy.pass = m_strdup (proxy_pass);      proxy.pass = m_strdup (proxy_pass);
555  }  }
   
   
 /* 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;  
 }  
556            
557    
558  /* Get the port number from the given protocol. */  /* Get the port number from the given protocol. */
# Line 515  static int Line 560  static int
560  port_from_proto (int proto)  port_from_proto (int proto)
561  {  {
562      switch (proto) {      switch (proto) {
563      case KSPROTO_LDAP:   return 0;      case KSPROTO_LDAP:   return LDAP_PORT;
564      case KSPROTO_FINGER: return FINGER_PORT;      case KSPROTO_FINGER: return FINGER_PORT;
565      case KSPROTO_HTTP:   return HKP_PORT;      case KSPROTO_HTTP:   return HKP_PORT;
566      }      }
# Line 529  proto_from_URL (const char *buf) Line 574  proto_from_URL (const char *buf)
574  {  {
575      if (strstr (buf, "ldap"))      if (strstr (buf, "ldap"))
576          return KSPROTO_LDAP;          return KSPROTO_LDAP;
577      else if (strstr( buf, "finger"))      else if (strstr (buf, "finger"))
578          return KSPROTO_FINGER;          return KSPROTO_FINGER;
579      return KSPROTO_HKP;      return KSPROTO_HKP;
580  }  }
581    
582    
583    /* Set the default keyserver.
584       If @hostname is NULL, use the predefined keyserver. */
585  void  void
586  keyserver_set_default (const char *hostname, WORD port)  keyserver_set_default (const char *hostname, WORD port)
587  {  {
588        if (!port)
589            port = HKP_PORT;
590      if (hostname != NULL) {      if (hostname != NULL) {
591          free_if_alloc (default_keyserver);          free_if_alloc (default_keyserver);
592          default_keyserver = m_strdup (hostname);          default_keyserver = m_strdup (hostname);
593          default_keyserver_port = port;          default_keyserver_port = port;
594      }      }
595      if (!port)      if (!default_keyserver) {
         port = HKP_PORT;  
     if (!default_keyserver)  
596          default_keyserver = m_strdup (DEF_HKP_KEYSERVER);          default_keyserver = m_strdup (DEF_HKP_KEYSERVER);
597            default_keyserver_port = HKP_PORT;
598        }
599      server[0].name =  m_strdup (default_keyserver);      server[0].name =  m_strdup (default_keyserver);
600      server[0].used = 1;      server[0].used = 1;
601      server[0].port = port;      server[0].port = port;
# Line 598  kserver_save_conf (const char *conf) Line 647  kserver_save_conf (const char *conf)
647  }  }
648    
649    
650    /* Return the specified keyserver config setting @key as an integer. */
651    static int
652    get_conf_kserver_int (const char *key)
653    {
654        char *p;
655        int val = 0;
656    
657        p = get_reg_entry_keyserver (key);
658        if (p && *p)
659            val = atoi (p);
660        free_if_alloc (p);
661        return val;
662    }
663    
664    
665    /* Read the proxy configuration and store it into @prox. */
666    static void
667    read_proxy_config (keyserver_proxy_t prox)
668    {
669        char *proto;
670    
671        if (!prox)
672            return;
673        
674        proto = get_reg_entry_keyserver("Proto");
675        if (proto != NULL && strlen (proto) > 0)
676            prox->proto = atoi (proto);
677        else
678            prox->proto = PROXY_PROTO_NONE;
679        free_if_alloc (proto);
680        free_if_alloc (prox->host);
681        prox->host = get_reg_entry_keyserver ("Host");
682        free_if_alloc (prox->user);
683        prox->user = get_reg_entry_keyserver ("User");
684        free_if_alloc (prox->pass);
685        prox->pass = get_reg_entry_keyserver ("Pass");
686        prox->port = get_conf_kserver_int ("Port");
687    }
688    
689    
690  /* Load the keyserver config file @conf. */  /* Load the keyserver config file @conf. */
691  int  int
692  kserver_load_conf (const char *conf)  kserver_load_conf (const char *conf)
# Line 622  kserver_load_conf (const char *conf) Line 711  kserver_load_conf (const char *conf)
711          }          }
712          no_config=1;          no_config=1;
713      }      }
714      get_reg_proxy_prefs (&proxy);      read_proxy_config (&proxy);
715      if (user && pass)      if (user && pass)
716          update_proxy_user (user, pass);          update_proxy_user (user, pass);
717      else if (user && !pass || !user && pass) {      else if (user && !pass || !user && pass) {
718          msg_box (NULL, _("Invalid proxy configuration."          msg_box (NULL, _("Invalid proxy configuration. "
719                           "You need to set a user and a password"                           "You need to set a user and a password "
720                           "to use proxy authentication!"),                           "to use proxy authentication!"),
721                           _("Proxy Error"), MB_ERR);                           _("Proxy Error"), MB_ERR);
722      }      }
# Line 652  kserver_load_conf (const char *conf) Line 741  kserver_load_conf (const char *conf)
741          s = (char *)skip_whitespace (buf);          s = (char *)skip_whitespace (buf);
742          if (*s == '#' || strlen (s) < 7)          if (*s == '#' || strlen (s) < 7)
743              continue;              continue;
744          if (check_URL (s)) {          chk_pos = check_URL (s);
745            if (!chk_pos) {
746              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"
747                               "Currently HKP/HTTP, LDAP and FINGER are supported.\n"),                               "Currently HKP/HTTP, LDAP and FINGER are supported.\n"),
748                               _("Keyserver Error"), MB_ERR);                               _("Keyserver Error"), MB_ERR);
749              continue;              continue;
750          }          }
         chk_pos = 6;  
         if (strstr (s, "finger"))  
             chk_pos = 10;  
751          p = strchr (s+chk_pos, ':');          p = strchr (s+chk_pos, ':');
752          server[pos].used = 1;          server[pos].used = 1;
753          server[pos].proto = proto_from_URL (s);          server[pos].proto = proto_from_URL (s);
# Line 763  kserver_connect (const char *hostname, W Line 850  kserver_connect (const char *hostname, W
850  }  }
851    
852    
853    /* Check if the URL needs to be encoded or not. */
854  static bool  static bool
855  URL_must_encoded (const char *url)  URL_must_encoded (const char *url)
856  {  {
# Line 854  kserver_send_request (const char *hostna Line 942  kserver_send_request (const char *hostna
942      }      }
943    
944      free_if_alloc (enc_pubkey);      free_if_alloc (enc_pubkey);
945      log_debug ("%s\r\n", request);      log_debug ("request:\r\n%s\r\n", request);
946      return request;      return request;
947  }  }
948    
# Line 899  kserver_recvkey (const char *hostname, W Line 987  kserver_recvkey (const char *hostname, W
987          _snprintf (request, reqlen, fmt, keyid);          _snprintf (request, reqlen, fmt, keyid);
988      }      }
989    
990      log_debug ("%s\r\n", request);      log_debug ("request:\r\n%s\r\n", request);
991            
992      rc = sock_write (conn_fd, request, strlen (request));      rc = sock_write (conn_fd, request, strlen (request));
993      if (rc == SOCKET_ERROR) {      if (rc == SOCKET_ERROR) {
# Line 913  kserver_recvkey (const char *hostname, W Line 1001  kserver_recvkey (const char *hostname, W
1001          goto leave;          goto leave;
1002      }      }
1003    
1004      log_debug ("%s\r\n", *r_key);      log_debug ("response:\r\n%s\r\n", *r_key);
1005      rc = check_hkp_response (*r_key, 1);      rc = check_hkp_response (*r_key, 1);
1006      if (rc)      if (rc)
1007          goto leave;          goto leave;
# Line 1157  parse_uid_record (keyserver_key_s *key, Line 1245  parse_uid_record (keyserver_key_s *key,
1245  {  {
1246      enum uid_rec_t {ID=1, UID, CREATE, EXPIRE};      enum uid_rec_t {ID=1, UID, CREATE, EXPIRE};
1247      keyserver_uid_s *u, *n;      keyserver_uid_s *u, *n;
1248      char *p;      char *p, *raw;
1249      int recno = 0;      int recno = 0;
1250    
1251      /* uid:Timo Schulz <[email protected]>:1138440360:: */      /* uid:Timo Schulz <[email protected]>:1138440360:: */
# Line 1173  parse_uid_record (keyserver_key_s *key, Line 1261  parse_uid_record (keyserver_key_s *key,
1261              break;              break;
1262    
1263          case UID:          case UID:
1264              /* XXX: search for %3A and decode it back to ':' */              unhexify_buffer (p, &raw);
1265              u->uid = m_strdup (p);              u->uid = utf8_to_native (raw);
1266                free_if_alloc (raw);
1267              break;              break;
1268    
1269          case CREATE:          case CREATE:
# Line 1232  kserver_search_next (int fd, keyserver_k Line 1321  kserver_search_next (int fd, keyserver_k
1321      for (;;) {      for (;;) {
1322          if (sock_getline (fd, buf, sizeof (buf)-1, &n))          if (sock_getline (fd, buf, sizeof (buf)-1, &n))
1323              break;              break;
1324            /*log_debug ("record: '%s'\r\n", buf); */
1325          if (!strncmp (buf, "pub", 3))          if (!strncmp (buf, "pub", 3))
1326              parse_pub_record (key, buf);              parse_pub_record (key, buf);
1327          else          else
# Line 1491  parse_keyserver_url (char **r_keyserver, Line 1581  parse_keyserver_url (char **r_keyserver,
1581  {  {
1582      char *p;      char *p;
1583      char *url = *r_keyserver;      char *url = *r_keyserver;
1584        int off = 0;
1585    
1586      /* no port is given so use the default port. */      /* no port is given so use the default port. */
1587      p = strrchr (url, ':');      p = strrchr (url, ':');
# Line 1501  parse_keyserver_url (char **r_keyserver, Line 1592  parse_keyserver_url (char **r_keyserver,
1592          *r_port = port;          *r_port = port;
1593          return 0;          return 0;
1594      }      }
1595      /* XXX: remove / in .de/:11371 */  
1596      *r_keyserver = substr (url, 0, (p-url));          if (url[(p-url)-1] == '/') /* remove / in .de/:11371 */
1597            off = 1;
1598    
1599        *r_keyserver = substr (url, 0, (p-url)-off);
1600      *r_port = atoi (url+(p-url)+1);      *r_port = atoi (url+(p-url)+1);
1601      safe_free (url);      free_if_alloc (url);
1602      return 0;      return 0;
1603  }  }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26