/[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 220 by twoaday, Tue May 30 15:31:49 2006 UTC revision 229 by twoaday, Mon Jun 19 14:04:31 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 0;
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);      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 362  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 409  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 435  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 465  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 495  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 517  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 531  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  }  }
# Line 658  kserver_load_conf (const char *conf) Line 701  kserver_load_conf (const char *conf)
701          s = (char *)skip_whitespace (buf);          s = (char *)skip_whitespace (buf);
702          if (*s == '#' || strlen (s) < 7)          if (*s == '#' || strlen (s) < 7)
703              continue;              continue;
704          if (check_URL (s)) {          chk_pos = check_URL (s);
705            if (!chk_pos) {
706              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"
707                               "Currently HKP/HTTP, LDAP and FINGER are supported.\n"),                               "Currently HKP/HTTP, LDAP and FINGER are supported.\n"),
708                               _("Keyserver Error"), MB_ERR);                               _("Keyserver Error"), MB_ERR);
709              continue;              continue;
710          }          }
         chk_pos = 6;  
         if (strstr (s, "finger"))  
             chk_pos = 10;  
711          p = strchr (s+chk_pos, ':');          p = strchr (s+chk_pos, ':');
712          server[pos].used = 1;          server[pos].used = 1;
713          server[pos].proto = proto_from_URL (s);          server[pos].proto = proto_from_URL (s);
# Line 769  kserver_connect (const char *hostname, W Line 810  kserver_connect (const char *hostname, W
810  }  }
811    
812    
813    /* Check if the URL needs to be encoded or not. */
814  static bool  static bool
815  URL_must_encoded (const char *url)  URL_must_encoded (const char *url)
816  {  {
# Line 1163  parse_uid_record (keyserver_key_s *key, Line 1205  parse_uid_record (keyserver_key_s *key,
1205  {  {
1206      enum uid_rec_t {ID=1, UID, CREATE, EXPIRE};      enum uid_rec_t {ID=1, UID, CREATE, EXPIRE};
1207      keyserver_uid_s *u, *n;      keyserver_uid_s *u, *n;
1208      char *p;      char *p, *raw;
1209      int recno = 0;      int recno = 0;
1210    
1211      /* uid:Timo Schulz <[email protected]>:1138440360:: */      /* uid:Timo Schulz <[email protected]>:1138440360:: */
# Line 1179  parse_uid_record (keyserver_key_s *key, Line 1221  parse_uid_record (keyserver_key_s *key,
1221              break;              break;
1222    
1223          case UID:          case UID:
1224              unhexify_buffer (p, &u->uid);              unhexify_buffer (p, &raw);
1225                u->uid = utf8_to_native (raw);
1226                free_if_alloc (raw);
1227              break;              break;
1228    
1229          case CREATE:          case CREATE:
# Line 1497  parse_keyserver_url (char **r_keyserver, Line 1541  parse_keyserver_url (char **r_keyserver,
1541  {  {
1542      char *p;      char *p;
1543      char *url = *r_keyserver;      char *url = *r_keyserver;
1544        int off = 0;
1545    
1546      /* no port is given so use the default port. */      /* no port is given so use the default port. */
1547      p = strrchr (url, ':');      p = strrchr (url, ':');
# Line 1507  parse_keyserver_url (char **r_keyserver, Line 1552  parse_keyserver_url (char **r_keyserver,
1552          *r_port = port;          *r_port = port;
1553          return 0;          return 0;
1554      }      }
1555      /* XXX: remove / in .de/:11371 */  
1556      *r_keyserver = substr (url, 0, (p-url));          if (url[(p-url)-1] == '/') /* remove / in .de/:11371 */
1557            off = 1;
1558    
1559        *r_keyserver = substr (url, 0, (p-url)-off);
1560      *r_port = atoi (url+(p-url)+1);      *r_port = atoi (url+(p-url)+1);
1561      free_if_alloc (url);      free_if_alloc (url);
1562      return 0;      return 0;

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26