/[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 147 by twoaday, Fri Jan 13 14:21:16 2006 UTC
# 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 294  wsock_end (void) Line 332  wsock_end (void)
332      int i;      int i;
333    
334      free_if_alloc (default_keyserver);      free_if_alloc (default_keyserver);
     default_keyserver = NULL;  
335      for (i=0; i < MAX_KEYSERVERS; i++) {      for (i=0; i < MAX_KEYSERVERS; i++) {
336          if (server[i].used)          if (server[i].used)
337              free_if_alloc (server[i].name);              free_if_alloc (server[i].name);
# Line 483  skip_whitespace (const char *str) Line 520  skip_whitespace (const char *str)
520  }  }
521    
522    
523    /* Load the keyserver config file @conf. */
524  int  int
525  kserver_load_conf (const char * conf)  kserver_load_conf (const char *conf)
526  {  {
527      struct stat statbuf;      struct stat statbuf;
528      FILE *fp;      FILE *fp;
# Line 511  kserver_load_conf (const char * conf) Line 549  kserver_load_conf (const char * conf)
549      if (user && pass)      if (user && pass)
550          kserver_update_proxyuser (user, pass);            kserver_update_proxyuser (user, pass);  
551      else if (user && !pass || !user && pass) {      else if (user && !pass || !user && pass) {
552          msg_box( NULL, _("Invalid proxy configuration."          msg_box (NULL, _("Invalid proxy configuration."
553                           "You need to set a user and a password"                           "You need to set a user and a password"
554                           "to use proxy authentication!"), _("Proxy Error"), MB_ERR );                           "to use proxy authentication!"),
555                             _("Proxy Error"), MB_ERR);
556      }      }
557      /* check if the host has a http:// prefix and skip it */      /* check if the host has a http:// prefix and skip it */
558      if( proxy.host && !strncmp( proxy.host, "http://", 7 ) ) {      if (proxy.host && !strncmp (proxy.host, "http://", 7)) {
559          char *host = m_strdup (proxy.host+7);          char *host = m_strdup (proxy.host+7);
560          if (!host)          if (!host)
561              BUG (0);              BUG (0);
# Line 527  kserver_load_conf (const char * conf) Line 566  kserver_load_conf (const char * conf)
566      free_if_alloc (pass);      free_if_alloc (pass);
567            
568      pos = 0;      pos = 0;
569      while( fp && !feof( fp ) ) {      while (fp && !feof (fp)) {
570          s = fgets (buf, sizeof (buf)-1, fp);          s = fgets (buf, sizeof (buf)-1, fp);
571          if (!s)          if (!s)
572              break;              break;
# Line 564  kserver_load_conf (const char * conf) Line 603  kserver_load_conf (const char * conf)
603              memcpy (server[pos].name, s, (p-s));              memcpy (server[pos].name, s, (p-s));
604          }          }
605          pos++;          pos++;
606          if (pos > MAX_KEYSERVERS)          if (pos > MAX_KEYSERVERS) {
607          {              msg_box (NULL, _("The keyserver limit is exceeded"),
608              msg_box (NULL, _("The keyserver limit is exceeded"), _("Keyserver Warning"), MB_INFO);                       _("Keyserver Warning"), MB_INFO);
609              break;              break;
610          }          }
611      }      }
# Line 576  kserver_load_conf (const char * conf) Line 615  kserver_load_conf (const char * conf)
615          /* 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. */
616          keyserver_set_default (NULL, HKP_PORT);          keyserver_set_default (NULL, HKP_PORT);
617          if (!no_config)          if (!no_config)
618              return WPTERR_CONFIG_FILE;                return WPTERR_CONFIG_FILE;
619      }      }
620    
621      if (!stat (conf, &statbuf))      if (!stat (conf, &statbuf))
622          conf_timestamp = statbuf.st_mtime;          conf_timestamp = statbuf.st_mtime;
623      return 0;      return 0;
624  } /* kserver_load_conf */  }
625    
626    
627  /* 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 806  kserver_send_request (const char *hostna
806  }  }
807    
808    
 /* Interface for receiving a public key. */  
809  int  int
810  kserver_recvkey (const char *hostname, WORD port, const char *keyid,  kserver_recvkey_ext (const char *hostname, WORD port, const char *keyid,
811                   char *key, int maxkeylen)                       char **r_key, int *r_keylen)
812  {  {
813      char *request = NULL;      char *request = NULL;
814      int conn_fd;      int conn_fd;
# Line 811  kserver_recvkey (const char *hostname, W Line 849  kserver_recvkey (const char *hostname, W
849          goto leave;          goto leave;
850      }      }
851            
852      rc = sock_read( conn_fd, key, maxkeylen, &n );      rc = sock_read_ext (conn_fd, r_key, &n);
853      if( rc == SOCKET_ERROR ) {      if (rc == SOCKET_ERROR) {
854          rc = WPTERR_WINSOCK_RECVKEY;          rc = WPTERR_WINSOCK_RECVKEY;
855          goto leave;          goto leave;
856      }      }
857    
858      log_debug("%s\r\n", key);      if (r_keylen)
859      rc = check_hkp_response (key, 1);          *r_keylen = n;
860        log_debug("%s\r\n", *r_key);
861        rc = check_hkp_response (*r_key, 1);
862      if (rc)      if (rc)
863          goto leave;          goto leave;
864            
# Line 830  leave: Line 870  leave:
870      return rc;      return rc;
871  }  }
872    
873    /* Interface for receiving a public key. */
874    int
875    kserver_recvkey (const char *hostname, WORD port, const char *keyid,
876                     char *key, int maxkeylen)
877    {
878        char *tmpkey;
879        int rc, nread=0;
880    
881        /* XXX: just a wrapper around the new method, replace it
882                soon as possible. */
883        rc = kserver_recvkey_ext (hostname, port, keyid, &tmpkey, &nread);
884        if (rc)
885            return rc;
886    
887        if (nread > maxkeylen) {
888            free_if_alloc (tmpkey);
889            return WPTERR_GENERAL;
890        }
891        strcpy (key, tmpkey);
892        free_if_alloc (tmpkey);
893        return 0;
894    }
895    
896    
897  /* Interface to send a public key. */  /* Interface to send a public key. */
898  int  int
# Line 911  kserver_search_init (const char *hostnam Line 974  kserver_search_init (const char *hostnam
974      }      }
975      else {      else {
976          _snprintf (request, n,          _snprintf (request, n,
977                     "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",
978                       enc_keyid);
979      }      }
980            
981      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 1028  parse_iso_date (const char *iso_date, ch
1028      p = strtok (buf, "-");      p = strtok (buf, "-");
1029      while (p != NULL) {      while (p != NULL) {
1030          switch (pos) {          switch (pos) {
1031          case 0: st.wYear = (WORD)atoi (p); pos++; break;          case 0: st.wYear  = (WORD)atoi (p); pos++; break;
1032          case 1: st.wMonth = (WORD)atoi (p); pos++; break;          case 1: st.wMonth = (WORD)atoi (p); pos++; break;
1033          case 2: st.wDay = (WORD)atoi (p); pos++; break;          case 2: st.wDay   = (WORD)atoi (p); pos++; break;
1034          default: break;          default: break;
1035          }          }
1036          p = strtok (NULL, "-");          p = strtok (NULL, "-");
# Line 1052  fail: Line 1116  fail:
1116  }  }
1117    
1118    
1119  static int  /* Replace all current proxy items with the items
1120  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 */  
   
   
1121  void  void
1122  kserver_change_proxy( keyserver_proxy_ctx *ctx )  kserver_change_proxy (keyserver_proxy_ctx *ctx)
1123  {      {    
1124      proxy.port = ctx->port;      proxy.port = ctx->port;
1125      free_if_alloc (proxy.host);      free_if_alloc (proxy.host);
1126      proxy.host = ctx->host? m_strdup( ctx->host ) : NULL;      proxy.host = ctx->host? m_strdup (ctx->host) : NULL;
1127      free_if_alloc( proxy.pass );      free_if_alloc (proxy.pass);
1128      proxy.pass = ctx->pass? m_strdup( ctx->pass ) : NULL;      proxy.pass = ctx->pass? m_strdup (ctx->pass) : NULL;
1129      free_if_alloc( proxy.user );      free_if_alloc (proxy.user);
1130      proxy.user = ctx->user? m_strdup( ctx->user ) : NULL;      proxy.user = ctx->user? m_strdup (ctx->user) : NULL;
1131      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;  
1132  }  }
1133    
1134    
 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 */  
   
   
1135  /* Release mbmers in the proxy context @ctx. */  /* Release mbmers in the proxy context @ctx. */
1136  void  void
1137  proxy_release( keyserver_proxy_ctx *ctx )  proxy_release (keyserver_proxy_ctx *ctx)
1138  {  {
1139      free_if_alloc (ctx->host);      free_if_alloc (ctx->host);
1140      free_if_alloc (ctx->pass);      free_if_alloc (ctx->pass);
# Line 1204  spawn_application (char *cmdl) Line 1171  spawn_application (char *cmdl)
1171    
1172  /* Receive an key via LDAP from host @host with the keyid @keyid.  /* Receive an key via LDAP from host @host with the keyid @keyid.
1173     @key contains the key on success. */     @key contains the key on success. */
1174  int  int
1175  ldap_recvkey (const char *host, const char *keyid, char *key, int maxkeylen)  ldap_recvkey (const char *host, const char *keyid, char *key, int maxkeylen)
1176  {      {    
1177      FILE *inp;      FILE *inp;

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26