/[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 290 by twoaday, Sat Mar 10 11:18:34 2007 UTC revision 328 by twoaday, Fri Sep 25 16:07:38 2009 UTC
# Line 1  Line 1 
1  /* wptKeyserver.cpp - W32 Keyserver Access  /* wptKeyserver.cpp - W32 Keyserver Access
2   *      Copyright (C) 2000-2007 Timo Schulz   *      Copyright (C) 2000-2009 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 39  Line 39 
39  /* Map net_errno to a winsock error. */  /* Map net_errno to a winsock error. */
40  #define net_errno ((int)WSAGetLastError ())  #define net_errno ((int)WSAGetLastError ())
41    
   
42  keyserver server[MAX_KEYSERVERS] = {0};  keyserver server[MAX_KEYSERVERS] = {0};
43  keyserver_proxy_s proxy = {0};  keyserver_proxy_s proxy = {0};
44  static const char *server_list[] = {  static const char *server_list[] = {
45      "hkp://sks.keyserver.penguin.de",      "hkp://pool.sks-keyservers.net",
46      "hkp://subkeys.pgp.net",      "hkp://subkeys.pgp.net",
47      "ldap://keyserver.pgp.com",      "http://minsky.surfnet.nl",
48      NULL      NULL
49  };  };
50    #define NUM_DEF_KEYSERVERS 3
51    
52    static char  hkp_err_msg[384];  /* Holds the error message from the server */
53  static char  hkp_errmsg[1024];  /* Holds the error message from the server */  static int   hkp_has_err = 0;   /* != 0 indicates an error occurred. */
 static int   hkp_err = 0;       /* != 0 indicates an error occurred. */  
54    
55  /* Default keyserver and port. */  /* Default keyserver and port. */
56  char *default_keyserver = NULL;  char *default_keyserver = NULL;
# Line 65  static size_t default_socket_timeout = 6 Line 64  static size_t default_socket_timeout = 6
64  static char*  static char*
65  safe_alloc (DWORD n)  safe_alloc (DWORD n)
66  {  {
67      char *p;      char *p = new char[n+1];
   
     p = new char[n+1];  
68      if (!p)      if (!p)
69          BUG (0);          BUG (0);
70      memset (p, 0, n);      memset (p, 0, n);
71      return p;      return p;
72  }  }
73    
74    
75  /* Basic64 encode the input @inbuf to @outbuf. */  /* Basic64 encode the input @inbuf to @outbuf. */
76  static void  static void
77  base64_encode (const char *inbuf, char *outbuf)  base64_encode (const char *inbuf, char *outbuf)
# Line 81  base64_encode (const char *inbuf, char * Line 79  base64_encode (const char *inbuf, char *
79      char base64code[] =      char base64code[] =
80          "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";          "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
81      int index = 0, temp = 0, res = 0;      int index = 0, temp = 0, res = 0;
82      int i, inputlen, len = 0;      int inputlen, len = 0;
83            
84      inputlen = strlen (inbuf);      inputlen = strlen (inbuf);
85      for (i = 0; i < inputlen; i++) {      for (int i = 0; i < inputlen; i++) {
86          res = temp;          res = temp;
87          res = (res << 8) | (inbuf[i] & 0xFF);          res = (res << 8) | (inbuf[i] & 0xFF);
88          switch (index++) {          switch (index++) {
# Line 94  base64_encode (const char *inbuf, char * Line 92  base64_encode (const char *inbuf, char *
92              break;              break;
93          case 1:          case 1:
94              outbuf[len++] = base64code[res >> 4 & 0x3F];              outbuf[len++] = base64code[res >> 4 & 0x3F];
95              res &= 0xF;              res &= 0xF;
96              break;              break;
97          case 2:          case 2:
98              outbuf[len++] = base64code[res >> 6 & 0x3F];              outbuf[len++] = base64code[res >> 6 & 0x3F];
# Line 124  base64_encode (const char *inbuf, char * Line 122  base64_encode (const char *inbuf, char *
122  static int  static int
123  check_URL (const char *buf)  check_URL (const char *buf)
124  {  {
125      const char *proto[] = {      if (strstr (buf, "hkp://"))
126          "ldap://",          return 6;
127          "http://",      if (strstr (buf, "http://"))
128          "finger://",          return 7;
         "hkp://",  
         NULL};  
     int i;  
   
     if (strlen (buf) < 7)  
         return 0;  
   
     for (i=0; proto[i] != NULL; i++) {  
         if (strstr (buf, proto[i]))  
             return strlen (proto[i]);  
     }  
129      return 0;      return 0;
130  }  }
131    
# Line 158  skip_type_prefix (const char *hostname) Line 145  skip_type_prefix (const char *hostname)
145      return hostname;      return hostname;
146  }  }
147    
148    
149  /* Parse the keyserver response and extract the human  /* Parse the keyserver response and extract the human
150     readable text passages. */     readable text passages. */
151  static int  static int
# Line 165  parse_keyserver_error (const char *resp, Line 153  parse_keyserver_error (const char *resp,
153  {  {
154      char *p, *p2;      char *p, *p2;
155    
156      /* If we find no 'Error' substring, we assume a success. */      /* If we find no 'Error' substring we assume a success. */
157      if (!stristr (resp, "Error"))      if (!stristr (resp, "Error"))
158          return -1;          return -1;
159    
# Line 188  parse_keyserver_error (const char *resp, Line 176  parse_keyserver_error (const char *resp,
176      else {      else {
177          if (!strnicmp (resp, "<p>", 3))          if (!strnicmp (resp, "<p>", 3))
178              resp += 3;              resp += 3;
179          while (resp && *resp == '\r' || *resp == '\n')          while (resp && (*resp == '\r' || *resp == '\n'))
180              resp++;              resp++;
181          memcpy (txt, resp, strlen (resp));          memcpy (txt, resp, strlen (resp));
182      }      }
# Line 203  check_hkp_response (const char *resp, in Line 191  check_hkp_response (const char *resp, in
191  {  {
192      int ec;      int ec;
193    
     log_debug ("check_hkp_response: '%s'\r\n", resp);  
194      ec = recv ? WPTERR_WINSOCK_RECVKEY : WPTERR_WINSOCK_SENDKEY;      ec = recv ? WPTERR_WINSOCK_RECVKEY : WPTERR_WINSOCK_SENDKEY;
195        if (!resp)
196            return ec;
197        log_debug ("check_hkp_response: '%s'\r\n", resp);    
198      if (!strstr (resp, "HTTP/1.0 200 OK") &&      if (!strstr (resp, "HTTP/1.0 200 OK") &&
199          !strstr (resp, "HTTP/1.1 200 OK") &&          !strstr (resp, "HTTP/1.1 200 OK") &&
200          !strstr (resp, "HTTP/1.0 500 OK") &&          !strstr (resp, "HTTP/1.0 500 OK") &&
201          !strstr (resp, "HTTP/1.1 500 OK"))          !strstr (resp, "HTTP/1.1 500 OK"))
202          return ec; /* http error */          return ec; /* http error */
203    
204      if (!parse_keyserver_error (resp, hkp_errmsg, DIM (hkp_errmsg)-2)) {      if (!parse_keyserver_error (resp, hkp_err_msg, DIM (hkp_err_msg)-2)) {
205          if (!strlen (hkp_errmsg))          if (!strlen (hkp_err_msg))
206              _snprintf (hkp_errmsg, DIM (hkp_errmsg)-1,              _snprintf (hkp_err_msg, DIM (hkp_err_msg)-1,
207                          "Unknown keyserver error");                          "Unknown keyserver error");
208          hkp_err = 1;          hkp_has_err = 1;
209          return ec;          return ec;
210      }      }
211      return 0;      return 0;
# Line 228  static int Line 218  static int
218  sock_getline (int fd, char *buf, int buflen, int *nbytes)  sock_getline (int fd, char *buf, int buflen, int *nbytes)
219  {  {
220      char ch;      char ch;
221      int nread = 0;            int nread;
222            
223      if (nbytes)      if (nbytes)
224          *nbytes = 0;          *nbytes = 0;
225      *buf = 0;      *buf = 0;
226        nread = 0;
227      while (recv (fd, &ch, 1, 0) > 0) {      while (recv (fd, &ch, 1, 0) > 0) {
228          *buf++ = ch;          *buf++ = ch;
229          nread++;          nread++;
# Line 251  sock_getline (int fd, char *buf, int buf Line 242  sock_getline (int fd, char *buf, int buf
242    
243    
244  /* Perform a select() on the given fd to find out  /* Perform a select() on the given fd to find out
245     is there is data for reading. Wait at least @nsecs seconds. */     is there is data for reading. Wait at least @nsecs milli seconds. */
246  static int  static int
247  sock_select (int fd, int nsecs)  sock_select (int fd, int nmsecs)
248  {  {
249      FD_SET rfd;      FD_SET rfd;
250      timeval tv = {nsecs, 0};      timeval tv = {0, nmsecs*1000};
251    
252      FD_ZERO (&rfd);      FD_ZERO (&rfd);
253      FD_SET (fd, &rfd);      FD_SET (fd, &rfd);
# Line 276  sock_select (int fd, int nsecs) Line 267  sock_select (int fd, int nsecs)
267     Return value: 0 on success. */     Return value: 0 on success. */
268  static int  static int
269  sock_read (int fd, char *buf, size_t buflen, int *nbytes)  sock_read (int fd, char *buf, size_t buflen, int *nbytes)
270  {  {    
     int nread;  
271      size_t nleft = buflen;      size_t nleft = buflen;
272      size_t n = 0;      size_t n;
273        int nread;
274      int rc;      int rc;
275            
276      if (nbytes)      if (nbytes)
277          *nbytes = 0;          *nbytes = 0;
278        n = 0;
279      while (nleft > 0) {      while (nleft > 0) {
280          if (n >= default_socket_timeout)          if (n >= default_socket_timeout)
281              return WPTERR_WINSOCK_TIMEOUT;              return WPTERR_WINSOCK_TIMEOUT;
282          rc = sock_select (fd, 1);          rc = sock_select (fd, 400);
283          if (rc == SOCKET_ERROR)          if (rc == SOCKET_ERROR)
284              return rc;              return rc;
285          else if (!rc)          else if (!rc)
# Line 315  sock_read (int fd, char *buf, size_t buf Line 307  sock_read (int fd, char *buf, size_t buf
307     then release the gpgme data object. */     then release the gpgme data object. */
308  char*  char*
309  data_release_and_get_mem (gpgme_data_t in, size_t *r_bufferlen)  data_release_and_get_mem (gpgme_data_t in, size_t *r_bufferlen)
310  {  {    
     size_t n;  
311      char *buffer, *p;      char *buffer, *p;
312        size_t n;
313    
314      gpg_data_putc (in, '\0');      gpg_data_putc (in, '\0');
315      p = gpgme_data_release_and_get_mem (in, &n);      p = gpgme_data_release_and_get_mem (in, &n);
# Line 338  sock_read_ext (int fd, char **buffer, si Line 330  sock_read_ext (int fd, char **buffer, si
330      gpgme_data_t dh;      gpgme_data_t dh;
331      char buf[1024];      char buf[1024];
332      size_t timeout=0;      size_t timeout=0;
333      int nread, rc;      int rc;
334    
335        *buffer = NULL;
336      if (gpgme_data_new (&dh))      if (gpgme_data_new (&dh))
337          return SOCKET_ERROR;          return SOCKET_ERROR;
338      while (timeout < default_socket_timeout) {      while (timeout < default_socket_timeout) {
339          rc = sock_select (fd, 1);          rc = sock_select (fd, 500);
340          if (rc == SOCKET_ERROR) {          if (rc == SOCKET_ERROR) {
341              gpgme_data_release (dh);              gpgme_data_release (dh);
342              return rc;              return rc;
# Line 351  sock_read_ext (int fd, char **buffer, si Line 344  sock_read_ext (int fd, char **buffer, si
344          else if (!rc)          else if (!rc)
345              timeout++; /* socket not ready yet. */              timeout++; /* socket not ready yet. */
346          else {          else {
347              nread = recv (fd, buf, DIM (buf), 0);              int nread = recv (fd, buf, DIM (buf), 0);
348                log_debug ("recv n=%d bytes\r\n", nread);
349              if (nread == SOCKET_ERROR)              if (nread == SOCKET_ERROR)
350                  return SOCKET_ERROR;                  return SOCKET_ERROR;
351              else if (!nread)              else if (!nread)
# Line 359  sock_read_ext (int fd, char **buffer, si Line 353  sock_read_ext (int fd, char **buffer, si
353              gpgme_data_write (dh, buf, nread);              gpgme_data_write (dh, buf, nread);
354          }          }
355      }      }
356      if (timeout >= default_socket_timeout)      
357        if (timeout >= default_socket_timeout) {
358            gpgme_data_release (dh);
359            log_debug ("sock_read_ext: timeout\r\n");
360          return WPTERR_WINSOCK_TIMEOUT;          return WPTERR_WINSOCK_TIMEOUT;
361        }
362        log_debug ("sock_read_ext: success\r\n");
363      *buffer = data_release_and_get_mem (dh, r_bufferlen);      *buffer = data_release_and_get_mem (dh, r_bufferlen);
364      return 0;      return 0;
365  }  }
# Line 401  wsock_init (void) Line 400  wsock_init (void)
400  }  }
401    
402    
403  /* Cleanup the Winsock2 interface. */  /* Cleanup the Winsock2 interface and free all resources. */
404  void  void
405  wsock_end (void)  wsock_end (void)
406  {  {
407      char *p;      if (default_keyserver != NULL) {
408      int i;          char str_port[64];
409            set_reg_entry_keyserver ("Default", default_keyserver);
410  #ifdef WINPT_MOBILE          free_if_alloc (default_keyserver);
411      p = m_strdup ("keyserver.conf");          sprintf (str_port, "%d", default_keyserver_port);
412  #else          set_reg_entry_keyserver ("Default_Port", str_port);
413      p = make_special_filename (CSIDL_APPDATA, "winpt\\keyserver.conf", NULL);      }  
414  #endif      for (int i=0; i < MAX_KEYSERVERS; i++) {
     kserver_save_conf (p);  
     free_if_alloc (p);  
     free_if_alloc (default_keyserver);  
     for (i=0; i < MAX_KEYSERVERS; i++) {  
415          if (server[i].name != NULL)          if (server[i].name != NULL)
416              free_if_alloc (server[i].name);              free_if_alloc (server[i].name);
417      }      }
# Line 435  wsock_strerror (void) Line 430  wsock_strerror (void)
430      if (!ec)      if (!ec)
431          return "";          return "";
432      switch (ec) {      switch (ec) {
433        case WSANOTINITIALISED:
434            return _("Winsock subsystem has not been initialized");
435            
436      case WSAENETDOWN:      case WSAENETDOWN:
437          return _("Network unreachable");          return _("Network subsystem has failed");
438    
     case WSAEHOSTUNREACH:  
         return _("Host unreachable");  
439    
440        case WSATRY_AGAIN:
441            return _("Nonauthoritative host not found, or server failure");
442            
443      case WSAHOST_NOT_FOUND:      case WSAHOST_NOT_FOUND:
444          return _("Could not resolve host name");          return _("Could not resolve host name");
445    
     case WSAECONNREFUSED:  
         return _("Connection refused");  
   
446      case WSAETIMEDOUT:      case WSAETIMEDOUT:
447      case WSAECONNABORTED:      case WSAECONNABORTED:
448          return _("Connection timeout");          return _("Connection timeout");
449    
450      case WSAENETRESET:      case WSAENETRESET:
451      case WSAECONNRESET:          case WSAECONNRESET:
452          return _("Connection resetted by peer");          return _("Connection resetted by peer");
453    
454        case WSAENETUNREACH:
455            return _("The network cannot be reached from this host at this time");
456            
457        case WSAEHOSTUNREACH:
458            return _("A socket operation was attempted to an unreachable host");
459            
460        case WSAECONNREFUSED:
461            return _("The attempt to connect was forcefully rejected");
462            
463      case WSAESHUTDOWN:      case WSAESHUTDOWN:
464          return _("Socket has been shutdown");          return _("Socket has been shutdown");
465    
# Line 462  wsock_strerror (void) Line 467  wsock_strerror (void)
467          break;          break;
468      }      }
469    
470      return "";      return _("Unknown network error");
471  }  }
472    
473    
# Line 471  void Line 476  void
476  kserver_set_socket_timeout (size_t nsec)  kserver_set_socket_timeout (size_t nsec)
477  {  {
478      if (nsec < 0 || nsec > 3600)      if (nsec < 0 || nsec > 3600)
479          nsec = 0;          nsec = 10;
480      default_socket_timeout = nsec;      default_socket_timeout = nsec;
481  }  }
482    
# Line 480  kserver_set_socket_timeout (size_t nsec) Line 485  kserver_set_socket_timeout (size_t nsec)
485  const char*  const char*
486  kserver_strerror (void)  kserver_strerror (void)
487  {        {      
488      if (hkp_err)      return hkp_has_err? hkp_err_msg : NULL;
         return hkp_errmsg;  
     return NULL;  
489  }  }
490    
491    
# Line 496  kserver_get_hostname (int idx, int type, Line 499  kserver_get_hostname (int idx, int type,
499          return default_keyserver;          return default_keyserver;
500      }      }
501      else if (!type && (size_t)idx < DIM (server_list)) {      else if (!type && (size_t)idx < DIM (server_list)) {
         /* XXX: handle non HKP servers. */  
502          *port = HKP_PORT;          *port = HKP_PORT;
503          return server_list[idx];          return server_list[idx];
504      }      }
# Line 527  kserver_check_inet_connection (void) Line 529  kserver_check_inet_connection (void)
529  const char*  const char*
530  kserver_check_keyid (const char *keyid)  kserver_check_keyid (const char *keyid)
531  {        {      
532      static char id[22];      static char id[64];
533    
534      if (strstr (keyid, "@"))      if (strstr (keyid, "@"))
535          return keyid; /* email address */          return keyid; /* email address */
# Line 545  static void Line 547  static void
547  update_proxy_user (const char *proxy_user, const char *proxy_pass)  update_proxy_user (const char *proxy_user, const char *proxy_pass)
548  {  {
549      char t[257]; /* user:pass = 127+1+127+1 = 257 */      char t[257]; /* user:pass = 127+1+127+1 = 257 */
550      int n = 0;      size_t n;
551    
552      n = 4*strlen (proxy_user) / 3 + 32 + strlen (proxy_pass) + 2;      n = 4*(strlen (proxy_user) + strlen (proxy_pass))/3 + 32 + 2;
553      free_if_alloc (proxy.base64_user);      free_if_alloc (proxy.base64_user);
554      proxy.base64_user = safe_alloc (n+1);      proxy.base64_user = safe_alloc (n+1);
555      _snprintf (t, DIM (t)-1, "%s:%s", proxy_user, proxy_pass);      _snprintf (t, DIM (t)-1, "%s:%s", proxy_user, proxy_pass);
# Line 557  update_proxy_user (const char *proxy_use Line 559  update_proxy_user (const char *proxy_use
559      proxy.user = m_strdup (proxy_user);      proxy.user = m_strdup (proxy_user);
560      proxy.pass = m_strdup (proxy_pass);      proxy.pass = m_strdup (proxy_pass);
561  }  }
       
   
 /* Get the port number from the given protocol. */  
 static int  
 port_from_proto (int proto)  
 {  
     switch (proto) {  
     case KSPROTO_LDAP:   return LDAP_PORT;  
     case KSPROTO_FINGER: return FINGER_PORT;  
     case KSPROTO_HTTP:   return HKP_PORT;  
     }  
     return 0;  
 }  
562    
563    
564  /* Get the port number from the given URL. */  /* Set the default keyserver. The position is always one. */
 static int  
 proto_from_URL (const char *buf)  
 {  
     if (strstr (buf, "ldap"))  
         return KSPROTO_LDAP;  
     else if (strstr (buf, "finger"))  
         return KSPROTO_FINGER;  
     return KSPROTO_HKP;  
 }  
   
   
 /* Set the default keyserver.  
    If @hostname is NULL, use the predefined keyserver. */  
565  void  void
566  keyserver_set_default (const char *hostname, WORD port)  keyserver_set_default (const char *hostname, WORD port)
567  {  {
568      if (!port)      int pos=0;
569        
570        if (port == 0)
571          port = HKP_PORT;          port = HKP_PORT;
572      if (hostname != NULL) {      
573          free_if_alloc (default_keyserver);      free_if_alloc (default_keyserver);
574          default_keyserver = m_strdup (hostname);      default_keyserver = m_strdup (hostname);
575          default_keyserver_port = port;      default_keyserver_port = port;
576      }      
577      if (!default_keyserver) {      while (pos < MAX_KEYSERVERS) {
578          default_keyserver = m_strdup (DEF_HKP_KEYSERVER);          if (server[pos].is_default)
579          default_keyserver_port = HKP_PORT;              break;
580            if (server[pos].used) {
581                pos++;
582                continue;
583            }
584            break;
585      }      }
586      server[0].name =  m_strdup (default_keyserver);      free_if_alloc (server[pos].name);
587      server[0].used = 1;      server[pos].name =  m_strdup (default_keyserver);
588      server[0].port = port;      server[pos].used = 1;
589      server[0].proto = proto_from_URL (default_keyserver);      server[pos].port = port;
590        server[pos].is_default = 1;
591  }  }
592    
593    
594  /* Skip all kind of whitespace chars in @str. */  /* Skip all kind of whitespace chars in @str.
595  static const char*  static const char*
596  skip_whitespace (const char *str)  skip_whitespace (const char *str)
597  {  {
# Line 622  skip_whitespace (const char *str) Line 606  skip_whitespace (const char *str)
606          break;          break;
607      }      }
608      return str;      return str;
609  }  }*/
   
   
 /* Save the keyserver config file in @conf. */  
 int  
 kserver_save_conf (const char *conf)  
 {  
     FILE *fp;  
     int pos;  
   
     fp = fopen (conf, "wb");  
     if (!fp) {  
         msg_box (NULL, _("Could not save keyserver.conf file"),  
                  _("Keyserver"), MB_ERR);  
         return -1;  
     }  
   
     fprintf (fp, "# do NOT manually modify this file, it will be "  
                  "generated automatically!!\r\n");  
     for (pos = 0; pos < MAX_KEYSERVERS; pos++) {  
         if (!server[pos].used)  
             continue;  
         fprintf (fp, "%s:%d\r\n", server[pos].name, server[pos].port);  
     }  
     fclose (fp);  
     return 0;  
 }  
610    
611    
612  /* Return the specified keyserver config setting @key as an integer. */  /* Return the specified keyserver config setting @key as an integer. */
# Line 681  read_proxy_config (keyserver_proxy_t pro Line 639  read_proxy_config (keyserver_proxy_t pro
639      else      else
640          prox->proto = PROXY_PROTO_NONE;          prox->proto = PROXY_PROTO_NONE;
641      free_if_alloc (proto);      free_if_alloc (proto);
642        
643      free_if_alloc (prox->host);      free_if_alloc (prox->host);
644      prox->host = get_reg_entry_keyserver ("Host");      prox->host = get_reg_entry_keyserver ("Host");
645        
646      free_if_alloc (prox->user);      free_if_alloc (prox->user);
647      prox->user = get_reg_entry_keyserver ("User");      prox->user = get_reg_entry_keyserver ("User");
648        
649      free_if_alloc (prox->pass);      free_if_alloc (prox->pass);
650      prox->pass = get_reg_entry_keyserver ("Pass");      prox->pass = get_reg_entry_keyserver ("Pass");
651        
652      prox->port = get_conf_kserver_int ("Port");      prox->port = get_conf_kserver_int ("Port");
653  }  }
654    
655    
656  /* Load the keyserver config file @conf. */  static int
657  int  is_keyserver_present (const char *name)
658  kserver_load_conf (const char *conf)  {
659  {          for (int i=0; i < MAX_KEYSERVERS; i++) {
660      FILE *fp;          if (server[i].name != NULL &&
661      char buf[1024], *s, *p;              stricmp (server[i].name, name) == 0)
662      char *user = NULL, *pass = NULL;                  return 1;
     int no_config=0, chk_pos;  
     int pos;  
       
     for (pos = 0; pos < MAX_KEYSERVERS; pos++) {  
         server[pos].used = 0;  
         server[pos].port = HKP_PORT;  
         server[pos].name = NULL;  
663      }      }
664        return 0;
665    }
666    
667    /* Load the keyserver configuration. */
668    int
669    kserver_load_conf (void)
670    {
671        char *str_server, *str_port;
672        int port, list_pos;    
673            
674      fp = fopen (conf, "rb");      /* If a preferred keyserver is available, load
675      if (!fp) {       * the values and store it at position 0.
676          for (pos = 0; server_list[pos]; pos++) {       */
677              server[pos].used = 1;      str_server = get_reg_entry_keyserver("Default");
678              server[pos].name = m_strdup (server_list[pos]);      if (str_server && *str_server) {
679              server[pos].proto = proto_from_URL (server_list[pos]);          port = HKP_PORT;
680          }          str_port = get_reg_entry_keyserver("Default_Port");
681          no_config = 1;          if (str_port && *str_port)
682      }              port = atoi (str_port);        
683      read_proxy_config (&proxy);          keyserver_set_default (str_server, port);
684      if (user && pass)          free_if_alloc (str_port);
685          update_proxy_user (user, pass);      }
686      else if (user && !pass || !user && pass) {      free_if_alloc (str_server);
687          msg_box (NULL, _("Invalid proxy configuration. "      
688                           "You need to set a user and a password "      /* Now add the default pool servers after the
689                           "to use proxy authentication!"),       * preferred keyserver if present.
690                           _("Proxy Error"), MB_ERR);       */
691      }      list_pos = 1;
692      /* check if the host has a http:// prefix and skip it */      for (int i=0; server_list[i] != NULL; i++) {
693      if (proxy.host && !strncmp (proxy.host, "http://", 7)) {          if (is_keyserver_present (server_list[i]))
         char *host = m_strdup (proxy.host+7);  
         free_if_alloc (proxy.host);  
         proxy.host = host;  
     }  
     free_if_alloc (user);  
     free_if_alloc (pass);  
       
     pos = 0;  
     while (fp && !feof (fp)) {  
         s = fgets (buf, DIM (buf)-1, fp);  
         if (!s)  
             break;  
         if (strstr (buf, "\r\n"))  
             buf[strlen (buf)-2] = '\0';  
         if (strstr (buf, "\n"))  
             buf[strlen (buf)-1] = '\0';  
         s = (char *)skip_whitespace (buf);  
         if (*s == '#' || strlen (s) < 7)  
             continue;  
         chk_pos = check_URL (s);  
         if (!chk_pos) {  
             msg_box (NULL, _("All entries of this file must have a valid prefix.\n"  
                              "Currently HKP/HTTP, LDAP and FINGER are supported.\n"),  
                              _("Keyserver Error"), MB_ERR);  
694              continue;              continue;
695          }          server[list_pos].used = 1;
696          p = strchr (s+chk_pos, ':');          server[list_pos].port = HKP_PORT;
697          server[pos].used = 1;          server[list_pos].name = m_strdup (server_list[i]);
698          server[pos].proto = proto_from_URL (s);          server[list_pos].is_default = 0;
699          server[pos].port = port_from_proto (server[pos].proto);          list_pos++;
700          if (!p)          if (list_pos > MAX_KEYSERVERS)
             server[pos].name = m_strdup (s);  
         else {        
             server[pos].port = atol (p+1);  
             if (server[pos].port < 0 || server[pos].port > 65536)  
                 server[pos].port = HKP_PORT;  
             server[pos].name = safe_alloc ((p-s)+2);  
             memset (server[pos].name, 0, (p-s)+2);  
             memcpy (server[pos].name, s, (p-s));  
         }  
         pos++;  
         if (pos > MAX_KEYSERVERS) {  
             msg_box (NULL, _("The keyserver limit is exceeded"),  
                      _("Keyserver Warning"), MB_INFO);  
701              break;              break;
         }  
702      }      }
     if (fp)  
         fclose (fp);  
     if (!pos) {  
         /* Only issue an error if the config file exist but  
            it has no valid entries. */  
         keyserver_set_default (NULL, HKP_PORT);  
         if (!no_config)  
             return WPTERR_CONFIG_FILE;  
     }  
   
703      return 0;      return 0;
704  }  }
705    
# Line 824  kserver_connect (const char *hostname, W Line 740  kserver_connect (const char *hostname, W
740          memcpy (&sock.sin_addr, hp->h_addr, hp->h_length);          memcpy (&sock.sin_addr, hp->h_addr, hp->h_length);
741      }      }
742      else {      else {
743          log_debug ("gethostbyname: failed ec=%d.\r\n", net_errno);          log_debug ("gethostbyname(%s): failed ec=%d.\r\n", host, net_errno);
744          return use_proxy? WPTERR_WINSOCK_PROXY : WPTERR_WINSOCK_RESOLVE;          return use_proxy? WPTERR_WINSOCK_PROXY : WPTERR_WINSOCK_RESOLVE;
745      }      }
746      fd = socket (AF_INET, SOCK_STREAM, 0);      fd = socket (AF_INET, SOCK_STREAM, 0);
# Line 836  kserver_connect (const char *hostname, W Line 752  kserver_connect (const char *hostname, W
752          closesocket (fd);          closesocket (fd);
753          return use_proxy? WPTERR_WINSOCK_PROXY : WPTERR_WINSOCK_CONNECT;          return use_proxy? WPTERR_WINSOCK_PROXY : WPTERR_WINSOCK_CONNECT;
754      }      }
755        
     if (proxy.proto == PROXY_PROTO_SOCKS5) {  
         /* XXX: finish code and provide errors constants. */  
         rc = socks_handshake (&proxy, fd, hostname, port);  
         if (rc) {  
             closesocket (fd);  
             return WPTERR_GENERAL;  
         }  
     }  
   
756      if (conn_fd)      if (conn_fd)
757          *conn_fd = fd;          *conn_fd = fd;
758      WSASetLastError (0);      WSASetLastError (0);
# Line 853  kserver_connect (const char *hostname, W Line 760  kserver_connect (const char *hostname, W
760  }  }
761    
762    
763  /* Check if the URL needs to be encoded or not. */  /* Check if the URL needs to be encoded or not.
764  static bool  static bool
765  URL_must_encoded (const char *url)  URL_must_encoded (const char *url)
766  {  {
767      if (strchr (url, '.') || strchr (url, '@') || strchr (url, ' '))      if (strchr (url, '.') || strchr (url, '@') || strchr (url, ' '))
768          return true;          return true;
769      return false;      return false;
770  }  }*/
771    
772    
773  /* Perform URL encoding of the given data. */  /* Perform URL encoding of the given data. */
# Line 868  static char* Line 775  static char*
775  URL_encode (const char *url, DWORD ulen, DWORD *ret_nlen)  URL_encode (const char *url, DWORD ulen, DWORD *ret_nlen)
776  {  {
777      gpgme_data_t hd;      gpgme_data_t hd;
778      char numbuf[5], *p;      char numbuf[8], *p;
779      size_t i, nlen;      size_t i, nlen;
780    
781      if (gpgme_data_new (&hd))      if (gpgme_data_new (&hd))
# Line 917  kserver_send_request (const char *hostna Line 824  kserver_send_request (const char *hostna
824                "Proxy-Authorization: Basic %s\r\n"                "Proxy-Authorization: Basic %s\r\n"
825                "Content-type: application/x-www-form-urlencoded\r\n"                "Content-type: application/x-www-form-urlencoded\r\n"
826                "Content-length: %d\r\n"                "Content-length: %d\r\n"
827                  "Connection: close\r\n"
828                "\r\n"                "\r\n"
829                "keytext=%s"                "keytext=%s"
830                "\r\n";                "\r\n";
# Line 933  kserver_send_request (const char *hostna Line 841  kserver_send_request (const char *hostna
841                "Host: %s:%d\r\n"                "Host: %s:%d\r\n"
842                "Content-type: application/x-www-form-urlencoded\r\n"                "Content-type: application/x-www-form-urlencoded\r\n"
843                "Content-length: %d\r\n"                "Content-length: %d\r\n"
844                  "Connection: close\r\n"
845                "\r\n"                "\r\n"
846                "keytext=%s"                "keytext=%s"
847                "\r\n";                "\r\n";
# Line 961  kserver_recvkey (const char *hostname, W Line 870  kserver_recvkey (const char *hostname, W
870    
871      if (!port)      if (!port)
872          port = HKP_PORT;          port = HKP_PORT;
873      hkp_err = 0; /* reset */      hkp_has_err = 0; /* reset */
874      rc = kserver_connect (hostname, port, &conn_fd);      rc = kserver_connect (hostname, port, &conn_fd);
875      if (rc)      if (rc)
876          goto leave;          goto leave;
# Line 970  kserver_recvkey (const char *hostname, W Line 879  kserver_recvkey (const char *hostname, W
879          req = req + "GET http://" + skip_type_prefix (hostname) +          req = req + "GET http://" + skip_type_prefix (hostname) +
880              ":" + port +              ":" + port +
881              "/pks/lookup?op=get&search=" + keyid + " HTTP/1.0\r\n" +              "/pks/lookup?op=get&search=" + keyid + " HTTP/1.0\r\n" +
882              "Proxy-Authorization: Basic " + proxy.base64_user + "\r\n\r\n";              "Proxy-Authorization: Basic " + proxy.base64_user + "\r\n" +
883                "Host: " + skip_type_prefix (hostname) + ":" + port + "\r\n"+
884                "Connection: close\r\n"+
885                "\r\n";
886      }      }
887      else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) {      else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) {
888          req = req + "GET http://" + skip_type_prefix (hostname) +          req = req + "GET http://" + skip_type_prefix (hostname) +
889              ":" + port + "/pks/lookup?op=get&search=" + keyid +              ":" + port + "/pks/lookup?op=get&search=" + keyid +
890              " HTTP/1.0\r\n\r\n";              " HTTP/1.0\r\n" +
891                "Host: " + skip_type_prefix (hostname) + ":" + port + "\r\n"+
892                "Connection: close\r\n"+
893                "\r\n";
894      }      }
895      else      else
896          req = req + "GET /pks/lookup?op=get&search=" + keyid +          req = req + "GET /pks/lookup?op=get&search=" + keyid +    
897              " HTTP/1.0\r\n\r\n";              " HTTP/1.0\r\n"+ // FIXME
898                "Host: " + skip_type_prefix (hostname) + ":" + port + "\r\n"+
899                "Connection: close\r\n"+
900                "\r\n";
901            
902      log_debug ("req:\r\n%s\r\n", req.getBuffer ());      log_debug ("req:\r\n%s\r\n", req.getBuffer ());  
903    
904      reqbuf = req.getBuffer ();      reqbuf = req.getBuffer ();
905      rc = sock_write (conn_fd, reqbuf, strlen (reqbuf));      rc = sock_write (conn_fd, reqbuf, strlen (reqbuf));
# Line 1019  kserver_sendkey (const char *hostname, W Line 937  kserver_sendkey (const char *hostname, W
937      int conn_fd, n;      int conn_fd, n;
938      int rc;      int rc;
939            
940      hkp_err = 0; /* reset */      hkp_has_err = 0; /* reset */
941      rc = kserver_connect (hostname, port, &conn_fd);      rc = kserver_connect (hostname, port, &conn_fd);
942      if (rc)      if (rc)
943          goto leave;          goto leave;
# Line 1129  kserver_search_begin (const char *hostna Line 1047  kserver_search_begin (const char *hostna
1047      if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) {      if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) {
1048          req = req + "GET http://" + skip_type_prefix (hostname) + ":" + port +          req = req + "GET http://" + skip_type_prefix (hostname) + ":" + port +
1049              "/pks/lookup?options=mr&op=index&search=" + enc_patt +              "/pks/lookup?options=mr&op=index&search=" + enc_patt +
1050              " HTTP/1.0\r\n"              " HTTP/1.0\r\n" +
1051                "Host: " + skip_type_prefix (hostname)  + ":" + port+ "\r\n"+
1052                "Connection: close\r\n" +
1053              "Proxy-Authorization: Basic " + proxy.base64_user + "\r\n\r\n";              "Proxy-Authorization: Basic " + proxy.base64_user + "\r\n\r\n";
1054      }          }    
1055      else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) {      else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) {
1056          req = req + "GET http://" + skip_type_prefix (hostname) + ":" + port +          req = req + "GET http://" + skip_type_prefix (hostname) + ":" + port +
1057              "/pks/lookup?options=mr&op=index&search=" + enc_patt +              "/pks/lookup?options=mr&op=index&search=" + enc_patt +
1058              " HTTP/1.0\r\n\r\n";              " HTTP/1.0\r\n"+
1059                "Host: " + skip_type_prefix (hostname)  + ":" + port+ "\r\n"+
1060                "Connection: close\r\n"+
1061                "\r\n";
1062      }      }
1063      else {      else {
1064          req = req + "GET /pks/lookup?options=mr&op=index&search=" +          req = req + "GET /pks/lookup?options=mr&op=index&search=" +
1065              enc_patt + " HTTP/1.0\r\n\r\n";           enc_patt + " HTTP/1.0\r\n" +
1066             "Host: " + skip_type_prefix (hostname)  + ":" + port+ "\r\n"+
1067             "Connection: close\r\n"+
1068             "\r\n";
1069      }      }
1070            
1071      log_debug ("kserver_search_begin:\r\n%s\r\n", req.getBuffer ());      log_debug ("kserver_search_begin:\r\n%s\r\n", req.getBuffer ());
# Line 1359  kserver_proxy_release (keyserver_proxy_t Line 1285  kserver_proxy_release (keyserver_proxy_t
1285  }  }
1286    
1287    
 /* Spawn a process given by @cmdl. */  
 static int  
 spawn_application (char *cmdl)  
 {  
     STARTUPINFO si;  
     PROCESS_INFORMATION pi;  
     int rc = 0;  
   
     memset (&si, 0, sizeof (si));  
     si.cb = sizeof (si);  
     si.dwFlags = STARTF_USESHOWWINDOW;  
     si.wShowWindow = SW_HIDE;  
     memset (&pi, 0, sizeof (pi));  
   
     if (!CreateProcess (NULL, cmdl, NULL, NULL, FALSE, 0,  
                         NULL, NULL, &si, &pi)) {  
         log_box ("Keyserver Plugin", MB_ERR, "Could not spawn helper process");  
         rc = -1;  
     }  
   
     CloseHandle (pi.hThread);  
     WaitForSingleObject (pi.hProcess, INFINITE);  
     CloseHandle (pi.hProcess);  
     return rc;  
 }  
   
   
 static FILE*  
 do_spawn_ldap_helper (const char *host, const char *keyid)  
 {  
     FILE *fp = NULL;  
     StringBuffer cmd;  
     char *sep, *p, *ksprg;  
     char outf[256], inf[256];  
     size_t n;  
   
     p = get_gnupg_prog ();  
     n = strlen (p) + 1 + 128;  
     ksprg = safe_alloc (n+1);  
     if (!ksprg)  
         BUG (0);  
     sep = strrchr (p, '\\');  
     if (sep != NULL)  
         p[(sep-p)] = 0;  
   
     _snprintf (ksprg, n, "%s\\gpgkeys_ldap.exe", p);  
     free_if_alloc (p);  
     if (file_exist_check (ksprg)) {  
         log_box ("LDAP Keyserver Plugin", MB_ERR,  
                  "%s: could not find LDAP keyserver module!", ksprg);  
         fp = NULL;  
         goto leave;  
     }  
     get_temp_name (outf, DIM (outf)-1, keyid);  
     get_temp_name (inf, DIM (inf)-1, NULL);  
     fp = fopen (inf, "w+b");  
     if (!fp) {  
         log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", inf,  
                  winpt_strerror (WPTERR_FILE_OPEN));  
         goto leave;  
     }  
     fprintf (fp,  
         "VERSION 1\n"  
         "PROGRAM %d.%d.%d\n"  
         "SCHEME ldap\n"  
         "HOST %s\n"  
         "COMMAND GET\n"  
         "\n"  
         "%s\n",  
         gpgver[0], gpgver[1], gpgver[2],  
         host? skip_type_prefix (host): "64.94.85.200", keyid);  
     fclose (fp);  
   
     cmd = ksprg;  
     cmd = cmd + " -o " + outf + " " + inf;  
     if (spawn_application ((char*)cmd.getBuffer ())) {  
         fp = NULL;  
         goto leave;  
     }  
     fp = fopen (outf, "rb");  
     if (!fp)  
         log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", outf,  
                  winpt_strerror (WPTERR_FILE_OPEN));  
   
 leave:  
     DeleteFile (inf);  
     DeleteFile (outf);  
     free_if_alloc (ksprg);  
     return fp;  
 }  
   
 /* Receive an key via LDAP from host @host with the keyid @keyid.  
    @key contains the key on success. */  
 int  
 ldap_recvkey (const char *host, const char *keyid,  
               char **r_key, size_t *r_keylen)  
 {    
     gpgme_data_t raw;  
     FILE *fp;  
     const char *s;  
     char buf[512];  
     int start_key = 0, failed = 0;  
     int rc = 0;  
   
     fp = do_spawn_ldap_helper (host, keyid);  
     if (!fp)  
         return WPTERR_GENERAL;  
   
     if (gpgme_data_new (&raw))  
         BUG (0);  
     while (!feof (fp)) {  
         s = fgets (buf, DIM (buf)-1, fp);  
         if (!s)  
             break;  
         if (strstr (s, "KEY") && strstr (s, "FAILED")) {  
             failed = 1;  
             break;  
         }  
         if (!start_key && strstr (s, "KEY") && strstr (s, "BEGIN")) {  
             start_key = 1;  
             continue;  
         }  
         if (!start_key)  
             continue;  
         gpgme_data_write (raw, buf, strlen (buf));  
         if (strstr (s, "KEY") && strstr (s, "END"))  
             break;  
     }  
     fclose (fp);  
   
     if (failed)  
         rc = WPTERR_WINSOCK_RECVKEY;  
     *r_key = data_release_and_get_mem (raw, r_keylen);  
     return rc;  
 }  
   
   
 /* Receive an key via FINGER from host @host with the user @user.  
    On success @key contains the key. */  
 int  
 finger_recvkey (const char *host, const char *user,  
                 char **r_key, size_t *r_keylen)  
 {  
     gpgme_data_t raw;  
     char buf[256+1];  
     int fd, nread;  
     int start_key = 0;  
     int rc=0;  
   
     rc = kserver_connect (host, FINGER_PORT, &fd);  
     if (rc)  
         return rc;  
   
     sock_write (fd, user, strlen (user));  
     sock_write (fd, "\r\n", 2);  
   
     if (gpgme_data_new (&raw))  
         return WPTERR_WINSOCK_RECVKEY;  
   
     for (;;) {  
         if (sock_getline (fd, buf, DIM (buf)-1, &nread))  
             break;  
         strcat (buf, "\n");  
         if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) {  
             gpgme_data_write (raw, buf, nread);  
             start_key = 1;  
         }  
         else if (strstr (buf, "END PGP PUBLIC KEY BLOCK" ) && start_key) {  
             gpgme_data_write (raw, buf, nread);  
             start_key--;  
             break;  
         }  
         else if (start_key)  
             gpgme_data_write (raw, buf, nread);  
     }  
   
     closesocket (fd);  
     if (start_key != 0)  
         rc = WPTERR_WINSOCK_RECVKEY;  
     *r_key = data_release_and_get_mem (raw, r_keylen);  
     return rc;  
 }  
   
   
1288  /* Check if the given name @name is a valid inernet address  /* Check if the given name @name is a valid inernet address
1289     which means an dotted IP or a valid DNS name.     which means an dotted IP or a valid DNS name.
1290     Return value: 0 on success. */     Return value: 0 on success. */
# Line 1550  int Line 1292  int
1292  check_inet_address (const char *addr)  check_inet_address (const char *addr)
1293  {  {
1294      const char *not_allowed = "=!�$%&@#*~\\/}][{<>|,;:'";      const char *not_allowed = "=!�$%&@#*~\\/}][{<>|,;:'";
     size_t i;  
1295    
1296      for (i=0; i < strlen (addr); i++) {      for (size_t i=0; i < strlen (addr); i++) {
1297          if (strchr (not_allowed, addr[i]))          if (strchr (not_allowed, addr[i]))
1298              return -1;              return -1;
1299      }      }
# Line 1567  parse_keyserver_url (char **r_keyserver, Line 1308  parse_keyserver_url (char **r_keyserver,
1308  {  {
1309      char *p;      char *p;
1310      char *url = *r_keyserver;      char *url = *r_keyserver;
1311      int off = 0;      int off;
1312    
1313      /* no port is given so use the default port. */      /* no port is given so use the default port. */
1314      p = strrchr (url, ':');      p = strrchr (url, ':');
1315      if (p == strchr (url, ':')) {      if (p == strchr (url, ':')) {
1316          int port = port_from_proto (proto_from_URL (url));          *r_port = HKP_PORT;
         if (!port)  
             port = HKP_PORT;  
         *r_port = port;  
1317          return 0;          return 0;
1318      }      }
1319    
1320      if (url[(p-url)-1] == '/') /* remove / in .de/:11371 */      if (url[(p-url)-1] == '/') /* remove / in .de/:11371 */
1321          off = 1;          off = 1;
1322        else
1323            off = 0;
1324    
1325      *r_keyserver = substr (url, 0, (p-url)-off);      *r_keyserver = substr (url, 0, (p-url)-off);
1326      *r_port = atoi (url+(p-url)+1);      *r_port = atoi (url+(p-url)+1);

Legend:
Removed from v.290  
changed lines
  Added in v.328

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26