/[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 144 by twoaday, Thu Jan 12 16:28:06 2006 UTC revision 150 by twoaday, Wed Jan 18 11:52:45 2006 UTC
# Line 24  Line 24 
24  #endif  #endif
25    
26  #include <windows.h>  #include <windows.h>
27    #include <shlobj.h>
28  #include <stdio.h>  #include <stdio.h>
29  #include <sys/stat.h>  #include <sys/stat.h>
30  #include <ctype.h>  #include <ctype.h>
# Line 33  Line 34 
34  #include "wptTypes.h"  #include "wptTypes.h"
35  #include "wptNLS.h"  #include "wptNLS.h"
36  #include "wptW32API.h"  #include "wptW32API.h"
 #include "wptVersion.h"  
37  #include "wptGPG.h"  #include "wptGPG.h"
38  #include "wptRegistry.h"  #include "wptRegistry.h"
39    
# Line 56  static const char *server_list[] = { Line 56  static const char *server_list[] = {
56      "hkp://wwwkeys.eu.pgp.net",      "hkp://wwwkeys.eu.pgp.net",
57      "hkp://wwwkeys.uk.pgp.net",      "hkp://wwwkeys.uk.pgp.net",
58      "hkp://wwwkeys.us.pgp.net",      "hkp://wwwkeys.us.pgp.net",
     "hkp://gnv.us.ks.cryptnet.net",  
59      "hkp://subkeys.pgp.net",      "hkp://subkeys.pgp.net",
60      "ldap://keyserver.pgp.com",      "ldap://keyserver.pgp.com",
61      NULL      NULL
# Line 67  static char  hkp_errmsg[1024]; /* Holds Line 66  static char  hkp_errmsg[1024]; /* Holds
66  static int   hkp_err = 0;       /* != 0 indicates an error occurred. */  static int   hkp_err = 0;       /* != 0 indicates an error occurred. */
67  static DWORD conf_timestamp = 0;/* timestamp of the configuration fiele. */  static DWORD conf_timestamp = 0;/* timestamp of the configuration fiele. */
68    
69  char    *default_keyserver = NULL;  /* Default keyserver and port. */
70  WORD    default_keyserver_port = 0;  char *default_keyserver = NULL;
71    WORD default_keyserver_port = 0;
72    
73    
74  /* Basic64 encode the input @inputstr to @outputstr. */  /* Basic64 encode the input @inputstr to @outputstr. */
# Line 83  base64_encode (const char *inputstr, cha Line 83  base64_encode (const char *inputstr, cha
83          res = temp;          res = temp;
84          res = (res << 8) | (inputstr[i] & 0xFF);          res = (res << 8) | (inputstr[i] & 0xFF);
85          switch (index++) {          switch (index++) {
86          case 0: outputstr[len++] = base64code[res >> 2 & 0x3F]; res &= 0x3; break;                      case 0: outputstr[len++] = base64code[res >> 2 & 0x3F]; res &= 0x3; break;
87          case 1: outputstr[len++] = base64code[res >> 4 & 0x3F]; res &= 0xF; break;                      case 1: outputstr[len++] = base64code[res >> 4 & 0x3F]; res &= 0xF; break;
88          case 2: outputstr[len++] = base64code[res >> 6 & 0x3F];          case 2: outputstr[len++] = base64code[res >> 6 & 0x3F];
89                  outputstr[len++] = base64code[res & 0x3F]; res = index = 0; break;                  outputstr[len++] = base64code[res & 0x3F]; res = index = 0; break;
90          }          }
# Line 233  sock_read (int fd, char *buf, int buflen Line 233  sock_read (int fd, char *buf, int buflen
233      return 0;      return 0;
234  }  }
235    
236    /* Read much data as possible from the socket @fd and
237       return the data in @buffer. Caller must free data.
238       Return value: 0 on success. */
239    int
240    sock_read_ext (int fd, char **buffer, int *r_bufferlen)
241    {
242        gpgme_data_t dh;
243        char buf[1024], *p;
244        size_t n=0;
245        int nread, rc;
246    
247        gpgme_data_new (&dh);
248        while (n < 10) {
249            rc = sock_select (fd, 1);
250            if (rc == SOCKET_ERROR) {
251                gpgme_data_release (dh);
252                return rc;
253            }
254            else if (!rc)
255                n++;
256            else {
257                nread = recv (fd, buf, sizeof (buf), 0);
258                if (nread == SOCKET_ERROR)
259                    return SOCKET_ERROR;
260                else if (!nread)
261                    break;
262                gpgme_data_write (dh, buf, nread);
263            }
264        }
265        gpg_data_putc (dh, '\0');
266        p = gpgme_data_release_and_get_mem (dh, &n);
267        *buffer = m_strdup (p);
268        if (r_bufferlen)
269            *r_bufferlen = n;
270        gpgme_free (p);
271        return 0;
272    }
273    
274    
275  /* Write the buffer @buf with the length @buflen to a socket @fd. */  /* Write the buffer @buf with the length @buflen to a socket @fd. */
276  static int  static int
# Line 291  wsock_init (void) Line 329  wsock_init (void)
329  void  void
330  wsock_end (void)  wsock_end (void)
331  {  {
332        char *p;
333      int i;      int i;
334    
335        p = make_special_filename (CSIDL_APPDATA, "winpt\\keyserver.conf", NULL);
336        kserver_save_conf (p);
337        free_if_alloc (p);
338      free_if_alloc (default_keyserver);      free_if_alloc (default_keyserver);
     default_keyserver = NULL;  
339      for (i=0; i < MAX_KEYSERVERS; i++) {      for (i=0; i < MAX_KEYSERVERS; i++) {
340          if (server[i].used)          if (server[i].used)
341              free_if_alloc (server[i].name);              free_if_alloc (server[i].name);
# Line 483  skip_whitespace (const char *str) Line 524  skip_whitespace (const char *str)
524  }  }
525    
526    
527    /* Save the keyserver config file in @conf. */
528  int  int
529  kserver_load_conf (const char * conf)  kserver_save_conf (const char *conf)
530    {
531        FILE *fp;
532        int pos;
533    
534        fp = fopen (conf, "wb");
535        if (!fp) {
536            msg_box (NULL, _("Could not save keyserver.conf file"),
537                     _("Keyserver"), MB_ERR);
538            return -1;
539        }
540    
541        fprintf (fp, "# do NOT manually modify this file, it will be generated automatically!!\r\n");
542        for (pos = 0; pos < MAX_KEYSERVERS; pos++) {
543            if (!server[pos].used)
544                continue;
545            fprintf (fp, "%s\r\n", server[pos].name);
546        }
547        fclose (fp);
548        return 0;
549    }
550    
551    
552    /* Load the keyserver config file @conf. */
553    int
554    kserver_load_conf (const char *conf)
555  {  {
556      struct stat statbuf;      struct stat statbuf;
557      FILE *fp;      FILE *fp;
# Line 511  kserver_load_conf (const char * conf) Line 578  kserver_load_conf (const char * conf)
578      if (user && pass)      if (user && pass)
579          kserver_update_proxyuser (user, pass);            kserver_update_proxyuser (user, pass);  
580      else if (user && !pass || !user && pass) {      else if (user && !pass || !user && pass) {
581          msg_box( NULL, _("Invalid proxy configuration."          msg_box (NULL, _("Invalid proxy configuration."
582                           "You need to set a user and a password"                           "You need to set a user and a password"
583                           "to use proxy authentication!"), _("Proxy Error"), MB_ERR );                           "to use proxy authentication!"),
584                             _("Proxy Error"), MB_ERR);
585      }      }
586      /* check if the host has a http:// prefix and skip it */      /* check if the host has a http:// prefix and skip it */
587      if( proxy.host && !strncmp( proxy.host, "http://", 7 ) ) {      if (proxy.host && !strncmp (proxy.host, "http://", 7)) {
588          char *host = m_strdup (proxy.host+7);          char *host = m_strdup (proxy.host+7);
589          if (!host)          if (!host)
590              BUG (0);              BUG (0);
# Line 527  kserver_load_conf (const char * conf) Line 595  kserver_load_conf (const char * conf)
595      free_if_alloc (pass);      free_if_alloc (pass);
596            
597      pos = 0;      pos = 0;
598      while( fp && !feof( fp ) ) {      while (fp && !feof (fp)) {
599          s = fgets (buf, sizeof (buf)-1, fp);          s = fgets (buf, sizeof (buf)-1, fp);
600          if (!s)          if (!s)
601              break;              break;
# Line 564  kserver_load_conf (const char * conf) Line 632  kserver_load_conf (const char * conf)
632              memcpy (server[pos].name, s, (p-s));              memcpy (server[pos].name, s, (p-s));
633          }          }
634          pos++;          pos++;
635          if (pos > MAX_KEYSERVERS)          if (pos > MAX_KEYSERVERS) {
636          {              msg_box (NULL, _("The keyserver limit is exceeded"),
637              msg_box (NULL, _("The keyserver limit is exceeded"), _("Keyserver Warning"), MB_INFO);                       _("Keyserver Warning"), MB_INFO);
638              break;              break;
639          }          }
640      }      }
# Line 576  kserver_load_conf (const char * conf) Line 644  kserver_load_conf (const char * conf)
644          /* only issue an error if the config file exist but it has no valid entries. */          /* only issue an error if the config file exist but it has no valid entries. */
645          keyserver_set_default (NULL, HKP_PORT);          keyserver_set_default (NULL, HKP_PORT);
646          if (!no_config)          if (!no_config)
647              return WPTERR_CONFIG_FILE;                return WPTERR_CONFIG_FILE;
648      }      }
649    
650      if (!stat (conf, &statbuf))      if (!stat (conf, &statbuf))
651          conf_timestamp = statbuf.st_mtime;          conf_timestamp = statbuf.st_mtime;
652      return 0;      return 0;
653  } /* kserver_load_conf */  }
654    
655    
656  /* Return the proxy host and port if available. Null otherwise. */  /* Return the proxy host and port if available. Null otherwise. */
# Line 767  kserver_send_request (const char *hostna Line 835  kserver_send_request (const char *hostna
835  }  }
836    
837    
 /* Interface for receiving a public key. */  
838  int  int
839  kserver_recvkey (const char *hostname, WORD port, const char *keyid,  kserver_recvkey_ext (const char *hostname, WORD port, const char *keyid,
840                   char *key, int maxkeylen)                       char **r_key, int *r_keylen)
841  {  {
842      char *request = NULL;      char *request = NULL;
843      int conn_fd;      int conn_fd;
# Line 811  kserver_recvkey (const char *hostname, W Line 878  kserver_recvkey (const char *hostname, W
878          goto leave;          goto leave;
879      }      }
880            
881      rc = sock_read( conn_fd, key, maxkeylen, &n );      rc = sock_read_ext (conn_fd, r_key, &n);
882      if( rc == SOCKET_ERROR ) {      if (rc == SOCKET_ERROR) {
883          rc = WPTERR_WINSOCK_RECVKEY;          rc = WPTERR_WINSOCK_RECVKEY;
884          goto leave;          goto leave;
885      }      }
886    
887      log_debug("%s\r\n", key);      if (r_keylen)
888      rc = check_hkp_response (key, 1);          *r_keylen = n;
889        log_debug("%s\r\n", *r_key);
890        rc = check_hkp_response (*r_key, 1);
891      if (rc)      if (rc)
892          goto leave;          goto leave;
893            
# Line 830  leave: Line 899  leave:
899      return rc;      return rc;
900  }  }
901    
902    /* Interface for receiving a public key. */
903    int
904    kserver_recvkey (const char *hostname, WORD port, const char *keyid,
905                     char *key, int maxkeylen)
906    {
907        char *tmpkey;
908        int rc, nread=0;
909    
910        /* XXX: just a wrapper around the new method, replace it
911                soon as possible. */
912        rc = kserver_recvkey_ext (hostname, port, keyid, &tmpkey, &nread);
913        if (rc)
914            return rc;
915    
916        if (nread > maxkeylen) {
917            free_if_alloc (tmpkey);
918            return WPTERR_GENERAL;
919        }
920        strcpy (key, tmpkey);
921        free_if_alloc (tmpkey);
922        return 0;
923    }
924    
925    
926  /* Interface to send a public key. */  /* Interface to send a public key. */
927  int  int
# Line 911  kserver_search_init (const char *hostnam Line 1003  kserver_search_init (const char *hostnam
1003      }      }
1004      else {      else {
1005          _snprintf (request, n,          _snprintf (request, n,
1006                     "GET /pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n", enc_keyid);                     "GET /pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n",
1007                       enc_keyid);
1008      }      }
1009            
1010      log_debug ("kserver_search_init:\r\n%s\r\n", request);      log_debug ("kserver_search_init:\r\n%s\r\n", request);
# Line 964  parse_iso_date (const char *iso_date, ch Line 1057  parse_iso_date (const char *iso_date, ch
1057      p = strtok (buf, "-");      p = strtok (buf, "-");
1058      while (p != NULL) {      while (p != NULL) {
1059          switch (pos) {          switch (pos) {
1060          case 0: st.wYear = (WORD)atoi (p); pos++; break;          case 0: st.wYear  = (WORD)atoi (p); pos++; break;
1061          case 1: st.wMonth = (WORD)atoi (p); pos++; break;          case 1: st.wMonth = (WORD)atoi (p); pos++; break;
1062          case 2: st.wDay = (WORD)atoi (p); pos++; break;          case 2: st.wDay   = (WORD)atoi (p); pos++; break;
1063          default: break;          default: break;
1064          }          }
1065          p = strtok (NULL, "-");          p = strtok (NULL, "-");
# Line 1052  fail: Line 1145  fail:
1145  }  }
1146    
1147    
1148  static int  /* Replace all current proxy items with the items
1149  add_node( keyserver_cfgfile *cfg, const char *entry )     from the context @ctx. */
 {  
     keyserver_node *node;  
     char *p = NULL;  
       
     p = strchr( entry, '=' );  
     if( p == NULL )  
         return WPTERR_GENERAL;    
     node = new keyserver_node;  
     if( !node )  
         BUG (NULL);  
       
     memset( node, 0, sizeof *node );  
     node->used = 1;  
       
     node->host.used = 1;  
     node->host.proto = proto_from_URL( entry );  
     memcpy( node->host.name, entry+7, p-entry-7 );  
     node->next = cfg->list;  
     cfg->list = node;  
       
     return 0;  
 } /* add_node */  
   
   
1150  void  void
1151  kserver_change_proxy( keyserver_proxy_ctx *ctx )  kserver_change_proxy (keyserver_proxy_ctx *ctx)
1152  {      {    
1153      proxy.port = ctx->port;      proxy.port = ctx->port;
1154      free_if_alloc (proxy.host);      free_if_alloc (proxy.host);
1155      proxy.host = ctx->host? m_strdup( ctx->host ) : NULL;      proxy.host = ctx->host? m_strdup (ctx->host) : NULL;
1156      free_if_alloc( proxy.pass );      free_if_alloc (proxy.pass);
1157      proxy.pass = ctx->pass? m_strdup( ctx->pass ) : NULL;      proxy.pass = ctx->pass? m_strdup (ctx->pass) : NULL;
1158      free_if_alloc( proxy.user );      free_if_alloc (proxy.user);
1159      proxy.user = ctx->user? m_strdup( ctx->user ) : NULL;      proxy.user = ctx->user? m_strdup (ctx->user) : NULL;
1160      set_reg_proxy_prefs( proxy.host, proxy.port, proxy.user, proxy.pass );      set_reg_proxy_prefs (proxy.host, proxy.port, proxy.user, proxy.pass);
 } /* kserver_change_proxy */  
   
   
 int  
 kserver_read_config( const char *fname, keyserver_cfgfile **ret_cfg )  
 {  
     FILE *fp;  
     keyserver_cfgfile *cfg;  
     char buf[256];  
     int rc = 0;  
       
     *ret_cfg = NULL;  
     fp = fopen( fname, "rb" );  
     if( fp == NULL )  
         return WPTERR_FILE_OPEN;  
       
     cfg = new keyserver_cfgfile;  
     if ( !cfg )  
         BUG( NULL );  
     memset( cfg, 0, sizeof *cfg );  
       
     while ( !feof( fp ) ) {  
         if ( fgets( buf, sizeof(buf)-1, fp ) == NULL )  
             break;  
         if ( *buf == '\r' || *buf == '\n' || *buf == '#' )  
             continue;  
         buf[strlen(buf) -2] = '\0';  
         if ( !strncmp( buf, "use_proxy=", 10 ) ) {  
             char *p = strchr(buf, ':');  
             if (!p)  
                 continue;  
             cfg->proxy.port = atol( buf+(p-buf+1) );  
             buf[p-buf] = '\0';  
             cfg->proxy.host = m_strdup( buf+10 );  
         }  
         else if ( !strncmp( buf, "proxy_user=", 11 ) )  
             cfg->proxy.user = m_strdup( buf+11 );  
         else if ( !strncmp( buf, "proxy_pass=", 11 ) )  
             cfg->proxy.pass = m_strdup( buf+11 );  
         if ( !strncmp( buf, "http://", 7 )  
             || !strncmp( buf, "hkp://", 6 )  
             || !strncmp( buf, "ldap://", 7 ) ) {  
             add_node( cfg, buf );  
             cfg->nlist++;  
         }  
     }  
       
     fclose( fp );  
     *ret_cfg = cfg;  
       
     return rc;  
 } /* kserver_read_config */  
   
   
 int  
 kserver_write_config( const char * fname, keyserver_cfgfile * cfg )  
 {  
       
     return 0;  
1161  }  }
1162    
1163    
 void  
 kserver_cfgfile_release( keyserver_cfgfile *cfg )  
 {        
     keyserver_node *k, *k2;  
   
     proxy_release( &cfg->proxy );  
     for( k = cfg->list; cfg->nlist--; k = k2 ) {  
         k2 = k->next;  
         free_if_alloc( k );      
     }  
     free_if_alloc( cfg );  
 } /* kserver_cfgfile_release */  
   
   
1164  /* Release mbmers in the proxy context @ctx. */  /* Release mbmers in the proxy context @ctx. */
1165  void  void
1166  proxy_release( keyserver_proxy_ctx *ctx )  proxy_release (keyserver_proxy_ctx *ctx)
1167  {  {
1168      free_if_alloc (ctx->host);      free_if_alloc (ctx->host);
1169      free_if_alloc (ctx->pass);      free_if_alloc (ctx->pass);
# Line 1204  spawn_application (char *cmdl) Line 1200  spawn_application (char *cmdl)
1200    
1201  /* Receive an key via LDAP from host @host with the keyid @keyid.  /* Receive an key via LDAP from host @host with the keyid @keyid.
1202     @key contains the key on success. */     @key contains the key on success. */
1203  int  int
1204  ldap_recvkey (const char *host, const char *keyid, char *key, int maxkeylen)  ldap_recvkey (const char *host, const char *keyid, char *key, int maxkeylen)
1205  {      {    
1206      FILE *inp;      FILE *inp;

Legend:
Removed from v.144  
changed lines
  Added in v.150

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26