/[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 179 by twoaday, Tue Feb 7 08:58:04 2006 UTC revision 180 by twoaday, Mon Mar 6 14:41:58 2006 UTC
# Line 43  static char base64code[] = Line 43  static char base64code[] =
43    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
44    
45  keyserver server[MAX_KEYSERVERS] = {0};  keyserver server[MAX_KEYSERVERS] = {0};
46  static keyserver_proxy_ctx proxy = {0};  keyserver_proxy_s proxy = {0};
47  static const char *server_list[] = {  static const char *server_list[] = {
48      "hkp://wwwkeys.nl.pgp.net",      "hkp://wwwkeys.nl.pgp.net",
49      "hkp://wwwkeys.pl.pgp.net",      "hkp://wwwkeys.pl.pgp.net",
# Line 71  char *default_keyserver = NULL; Line 71  char *default_keyserver = NULL;
71  WORD default_keyserver_port = 0;  WORD default_keyserver_port = 0;
72    
73    
74  /* Basic64 encode the input @inputstr to @outputstr. */  /* Basic64 encode the input @inbuf to @outbuf. */
75  static void  static void
76  base64_encode (const char *inputstr, char *outputstr, int maxlen)  base64_encode (const char *inbuf, char *outbuf)
77  {        {      
78      int index = 0, temp = 0, res = 0, i = 0, inputlen = 0, len = 0;      int index = 0, temp = 0, res = 0;
79        int i = 0, inputlen = 0, len = 0;
80            
81      /* XXX: check len < maxlen. */      inputlen = strlen (inbuf);
82      inputlen = strlen (inputstr);      for (i = 0; i < inputlen; i++) {
     for (i = 0; i <inputlen; i++) {  
83          res = temp;          res = temp;
84          res = (res << 8) | (inputstr[i] & 0xFF);          res = (res << 8) | (inbuf[i] & 0xFF);
85          switch (index++) {          switch (index++) {
86          case 0: outputstr[len++] = base64code[res >> 2 & 0x3F]; res &= 0x3; break;          case 0:
87          case 1: outputstr[len++] = base64code[res >> 4 & 0x3F]; res &= 0xF; break;              outbuf[len++] = base64code[res >> 2 & 0x3F];
88          case 2: outputstr[len++] = base64code[res >> 6 & 0x3F];              res &= 0x3;
89                  outputstr[len++] = base64code[res & 0x3F]; res = index = 0; break;              break;
90            case 1:
91                outbuf[len++] = base64code[res >> 4 & 0x3F];
92                res &= 0xF;
93                break;
94            case 2:
95                outbuf[len++] = base64code[res >> 6 & 0x3F];
96                outbuf[len++] = base64code[res & 0x3F];
97                res = index = 0;
98                break;
99          }          }
100          temp = res;          temp = res;
101      }      }
102            
103      switch (index) {      if (index == 2) {      
104      case 0: break;                  outbuf[len++] = base64code[temp << 2 & 0x3F];  
105      case 2: outputstr[len++] = base64code[temp << 2 & 0x3F];              outbuf[len++] = '=';
             outputstr[len++] = '='; break;          
     case 1: outputstr[len++] = base64code[temp << 4 & 0x3F];      
             outputstr[len++] = '=';  
             outputstr[len++] = '=';  
106      }      }
107            else if (index == 1) {
108      outputstr[len] = '\0';          outbuf[len++] = base64code[temp << 4 & 0x3F];
109            outbuf[len++] = '=';
110            outbuf[len++] = '=';
111        }
112    
113        outbuf[len] = '\0';
114  }  }
115    
116    
# Line 155  static int Line 165  static int
165  sock_getline (int fd, char *buf, int buflen, int *nbytes)  sock_getline (int fd, char *buf, int buflen, int *nbytes)
166  {  {
167      char ch;      char ch;
168      int count = 0;            int nread = 0;      
169            
170      if (nbytes)      if (nbytes)
171          *nbytes = 0;          *nbytes = 0;
172      *buf = 0;      *buf = 0;
173      while (recv (fd, &ch, 1, 0) > 0) {      while (recv (fd, &ch, 1, 0) > 0) {
174          *buf++ = ch;          *buf++ = ch;
175          count++;          nread++;
176          /* XXX: remove the '\r' */          if (ch == '\r')
177          if (ch == '\n' || count == (buflen - 1)) {              continue; /* remove this char. */
178            if (ch == '\n' || nread == (buflen - 1)) {
179              *buf = 0;              *buf = 0;
180              if (nbytes)              if (nbytes)
181                  *nbytes = count;                  *nbytes = nread;
182              return 0;              return 0;
183          }          }
184      }      }
# Line 286  sock_write (int fd, const char *buf, int Line 297  sock_write (int fd, const char *buf, int
297              return SOCKET_ERROR;              return SOCKET_ERROR;
298          }          }
299          nleft -= nwritten;          nleft -= nwritten;
300          buf += nwritten;                  buf += nwritten;
301      }      }
302    
303      return 0;      return 0;
# Line 340  wsock_end (void) Line 351  wsock_end (void)
351          if (server[i].used)          if (server[i].used)
352              free_if_alloc (server[i].name);              free_if_alloc (server[i].name);
353      }      }
354        kserver_proxy_release (&proxy);
355      WSACleanup ();      WSACleanup ();
356  }  }
357    
# Line 411  kserver_check_inet_connection (void) Line 423  kserver_check_inet_connection (void)
423  /* If the request contains the keyid, it have to be  /* If the request contains the keyid, it have to be
424     always postfix with '0x'+keyid. This code checks     always postfix with '0x'+keyid. This code checks
425     if the keyid is a decimal value and if so if it contains     if the keyid is a decimal value and if so if it contains
426    the '0x'. If not it is inserted. */     the '0x'. If not it is inserted. */
427  const char*  const char*
428  kserver_check_keyid (const char *keyid)  kserver_check_keyid (const char *keyid)
429  {        {      
# Line 430  kserver_check_keyid (const char *keyid) Line 442  kserver_check_keyid (const char *keyid)
442    
443  /* Update the keyserver proxy user. */  /* Update the keyserver proxy user. */
444  static void  static void
445  kserver_update_proxyuser (const char *proxy_user, const char *proxy_pass)  update_proxy_user (const char *proxy_user, const char *proxy_pass)
446  {  {
447      char t[257]; /* user:pass = 127+1+127+1 = 257 */      char t[257]; /* user:pass = 127+1+127+1 = 257 */
448      int n = 0;      int n = 0;
# Line 441  kserver_update_proxyuser (const char *pr Line 453  kserver_update_proxyuser (const char *pr
453      if (!proxy.base64_user)      if (!proxy.base64_user)
454          BUG (0);          BUG (0);
455      _snprintf (t, sizeof (t)-1, "%s:%s", proxy_user, proxy_pass);      _snprintf (t, sizeof (t)-1, "%s:%s", proxy_user, proxy_pass);
456      base64_encode (t, proxy.base64_user, 257);      base64_encode (t, proxy.base64_user);
457      free_if_alloc (proxy.user);      free_if_alloc (proxy.user);
458      free_if_alloc (proxy.pass);      free_if_alloc (proxy.pass);
459      proxy.user = m_strdup (proxy_user);      proxy.user = m_strdup (proxy_user);
# Line 479  port_from_proto (int proto) Line 491  port_from_proto (int proto)
491    
492  /* Get the port number from the given URL. */  /* Get the port number from the given URL. */
493  static int  static int
494  proto_from_URL (const char * buf)  proto_from_URL (const char *buf)
495  {  {
496      if (strstr( buf, "ldap"))      if (strstr( buf, "ldap"))
497          return KSPROTO_LDAP;          return KSPROTO_LDAP;
# Line 538  kserver_save_conf (const char *conf) Line 550  kserver_save_conf (const char *conf)
550          return -1;          return -1;
551      }      }
552    
553      fprintf (fp, "# do NOT manually modify this file, it will be generated automatically!!\r\n");      fprintf (fp, "# do NOT manually modify this file, it will be "
554                     "generated automatically!!\r\n");
555      for (pos = 0; pos < MAX_KEYSERVERS; pos++) {      for (pos = 0; pos < MAX_KEYSERVERS; pos++) {
556          if (!server[pos].used)          if (!server[pos].used)
557              continue;              continue;
# Line 555  kserver_load_conf (const char *conf) Line 568  kserver_load_conf (const char *conf)
568  {  {
569      struct stat statbuf;      struct stat statbuf;
570      FILE *fp;      FILE *fp;
571      char buf[1024], * s, * p;      char buf[1024], *s, *p;
572      char *user = NULL, *pass = NULL;          char *user = NULL, *pass = NULL;    
573      int no_config=0, chk_pos=0;      int no_config=0, chk_pos=0;
574      int pos;      int pos;
# Line 574  kserver_load_conf (const char *conf) Line 587  kserver_load_conf (const char *conf)
587          }          }
588          no_config=1;          no_config=1;
589      }      }
590      get_reg_proxy_prefs (&proxy.host, &proxy.port, &user, &pass);      get_reg_proxy_prefs (&proxy);
591      if (user && pass)      if (user && pass)
592          kserver_update_proxyuser (user, pass);            update_proxy_user (user, pass);
593      else if (user && !pass || !user && pass) {      else if (user && !pass || !user && pass) {
594          msg_box (NULL, _("Invalid proxy configuration."          msg_box (NULL, _("Invalid proxy configuration."
595                           "You need to set a user and a password"                           "You need to set a user and a password"
# Line 612  kserver_load_conf (const char *conf) Line 625  kserver_load_conf (const char *conf)
625                               _("Keyserver Error"), MB_ERR);                               _("Keyserver Error"), MB_ERR);
626              continue;              continue;
627          }          }
628          chk_pos=6;          chk_pos = 6;
629          if (strstr (s, "finger"))          if (strstr (s, "finger"))
630              chk_pos = 10;              chk_pos = 10;
631          p = strchr (s+chk_pos, ':');          p = strchr (s+chk_pos, ':');
# Line 641  kserver_load_conf (const char *conf) Line 654  kserver_load_conf (const char *conf)
654      if (fp)      if (fp)
655          fclose (fp);          fclose (fp);
656      if (!pos) {      if (!pos) {
657          /* 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
658               it has no valid entries. */
659          keyserver_set_default (NULL, HKP_PORT);          keyserver_set_default (NULL, HKP_PORT);
660          if (!no_config)          if (!no_config)
661              return WPTERR_CONFIG_FILE;              return WPTERR_CONFIG_FILE;
# Line 653  kserver_load_conf (const char *conf) Line 667  kserver_load_conf (const char *conf)
667  }  }
668    
669    
 /* Return the proxy host and port if available. Null otherwise. */  
 const char*  
 kserver_get_proxy (int *r_port)  
 {  
     if (proxy.host) {  
         if (r_port)  
             *r_port = proxy.port;  
         return proxy.host;  
     }  
     return NULL;  
 }  
   
   
 /* Return a requested item from the proxy environment. */  
 const char*  
 kserver_get_proxy_info (int id)  
 {  
     switch (id) {    
     case PROXY_USER: return proxy.user;  
     case PROXY_PASS: return proxy.pass;  
     }  
     return NULL;  
 }  
   
   
670  /* Connect to the keyserver @hostname (port @port).  /* Connect to the keyserver @hostname (port @port).
671     Return value: 0 on success */     Return value: 0 on success */
672  int  int
# Line 728  kserver_connect (const char *hostname, W Line 717  kserver_connect (const char *hostname, W
717          return WPTERR_WINSOCK_CONNECT;          return WPTERR_WINSOCK_CONNECT;
718      }      }
719    
720        if (proxy.proto != PROXY_PROTO_HTTP) {
721            rc = socks_handshake (&proxy, fd, hostname, port);
722            if (rc) {
723                closesocket (fd);
724                return WPTERR_GENERAL; /* XXX: use better code. */
725            }
726        }
727    
728      if (conn_fd)      if (conn_fd)
729          *conn_fd = fd;          *conn_fd = fd;
730      WSASetLastError (0);      WSASetLastError (0);
# Line 799  kserver_send_request (const char *hostna Line 796  kserver_send_request (const char *hostna
796          return NULL;          return NULL;
797      }      }
798            
799      if (proxy.user) {      if (proxy.user && proxy.proto == PROXY_PROTO_HTTP) {
800          _snprintf (request, reqlen-1,          _snprintf (request, reqlen-1,
801                     "POST http://%s:%d/pks/add HTTP/1.0\r\n"                     "POST http://%s:%d/pks/add HTTP/1.0\r\n"
802                     "Referer: \r\n"                     "Referer: \r\n"
# Line 854  kserver_recvkey_ext (const char *hostnam Line 851  kserver_recvkey_ext (const char *hostnam
851      if (!request)      if (!request)
852          BUG (0);          BUG (0);
853            
854      if (proxy.host && proxy.user) {      if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) {
855          _snprintf (request, 300,          _snprintf (request, 300,
856              "GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n"              "GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n"
857              "Proxy-Authorization: Basic %s\r\n\r\n",              "Proxy-Authorization: Basic %s\r\n\r\n",
858              skip_type_prefix (hostname), port, keyid, proxy.base64_user);              skip_type_prefix (hostname), port, keyid, proxy.base64_user);
859      }      }
860      else if (proxy.host) {      else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) {
861          _snprintf (request, 300,          _snprintf (request, 300,
862              "GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n",              "GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n",
863              skip_type_prefix (hostname), port, keyid);              skip_type_prefix (hostname), port, keyid);
# Line 990  kserver_search_init (const char *hostnam Line 987  kserver_search_init (const char *hostnam
987      if (!request)      if (!request)
988          BUG (0);          BUG (0);
989            
990      if (proxy.host && proxy.user) {      if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) {
991          _snprintf (request, n,          _snprintf (request, n,
992              "GET http://%s:%d/pks/lookup?op=index&search=%s HTTP/1.0\r\n"              "GET http://%s:%d/pks/lookup?op=index&search=%s HTTP/1.0\r\n"
993              "Proxy-Authorization: Basic %s\r\n\r\n",              "Proxy-Authorization: Basic %s\r\n\r\n",
994              skip_type_prefix (hostname), port, enc_keyid, proxy.base64_user);              skip_type_prefix (hostname), port, enc_keyid, proxy.base64_user);
995      }          }    
996      else if (proxy.host) {      else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) {
997          _snprintf (request, n,          _snprintf (request, n,
998              "GET http://%s:%d/pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n",              "GET http://%s:%d/pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n",
999              skip_type_prefix (hostname), port, enc_keyid);              skip_type_prefix (hostname), port, enc_keyid);
# Line 1145  fail: Line 1142  fail:
1142  }  }
1143    
1144    
 /* Replace all current proxy items with the items  
    from the context @ctx. */  
 void  
 kserver_change_proxy (keyserver_proxy_ctx *ctx)  
 {      
     proxy.port = ctx->port;  
     free_if_alloc (proxy.host);  
     proxy.host = ctx->host? m_strdup (ctx->host) : NULL;  
     free_if_alloc (proxy.pass);  
     proxy.pass = ctx->pass? m_strdup (ctx->pass) : NULL;  
     free_if_alloc (proxy.user);  
     proxy.user = ctx->user? m_strdup (ctx->user) : NULL;  
     set_reg_proxy_prefs (proxy.host, proxy.port, proxy.user, proxy.pass);  
 }  
   
   
1145  /* Release mbmers in the proxy context @ctx. */  /* Release mbmers in the proxy context @ctx. */
1146  void  void
1147  proxy_release (keyserver_proxy_ctx *ctx)  kserver_proxy_release (keyserver_proxy_t ctx)
1148  {  {
1149      free_if_alloc (ctx->host);      free_if_alloc (ctx->host);
1150      free_if_alloc (ctx->pass);      free_if_alloc (ctx->pass);
# Line 1297  leave: Line 1278  leave:
1278  }  }
1279    
1280    
 static int  
 finger_readline (int fd, char *buf, int nbytes)  
 {  
     char c = 0;  
     int n, pos = 0;  
       
     while (nbytes > 0) {  
         n = recv (fd, &c, 1, 0);  
         if (n <= 0)  
             break;  
         if (c == '\n')  
             break;  
         buf[pos++] = c;  
         nbytes--;  
     }  
     if (nbytes > 0)  
         buf[pos++] = '\0';  
     if (n <= 0)  
         pos = n;  
     return pos;  
 }  
   
   
1281  /* Receive an key via FINGER from host @host with the user @user.  /* Receive an key via FINGER from host @host with the user @user.
1282     On success @key contains the key. */     On success @key contains the key. */
1283  int  int
1284  finger_recvkey (const char *host, const char *user, char *key, int maxkeylen)  finger_recvkey (const char *host, const char *user, char *key, int maxkeylen)
1285  {  {
     struct hostent * hp;  
     struct sockaddr_in saddr;  
1286      char buf[128];      char buf[128];
1287      int fd, nread;      int fd, nread;
1288      int start_key = 0;      int start_key = 0;
1289      int rc=0;      int rc=0;
1290    
1291      fd = socket (PF_INET, SOCK_STREAM, 0);      rc = kserver_connect (host, FINGER_PORT, &fd);
1292      if (fd == -1)      if (rc)
1293          return WPTERR_WINSOCK_SOCKET;          return rc;
     hp = gethostbyname (skip_type_prefix (host));  
     if (!hp) {  
         closesocket (fd);  
         return WPTERR_WINSOCK_RESOLVE;  
     }  
1294    
1295      memset (&saddr, 0, sizeof (saddr));      sock_write (fd, user, strlen (user));
1296      saddr.sin_family = AF_INET;      sock_write (fd, "\r\n", 2);
     saddr.sin_port = htons (FINGER_PORT);  
     saddr.sin_addr = *(struct in_addr *)hp->h_addr;  
     if (connect (fd, (struct sockaddr *)&saddr, sizeof (saddr))) {  
         closesocket (fd);  
         return WPTERR_WINSOCK_CONNECT;  
     }  
     send (fd, user, strlen (user), 0);  
     send (fd, "\r\n", 2, 0);  
1297    
1298      memset (key, 0, maxkeylen);      memset (key, 0, maxkeylen);
1299      while (maxkeylen > 0) {      while (maxkeylen > 0) {
1300          nread = finger_readline (fd, buf, sizeof (buf)-1);          if (sock_getline (fd, buf, sizeof (buf), &nread))
1301          if (nread <= 0)              break;
             break;  
1302          strcat (buf, "\n");          strcat (buf, "\n");
1303          if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) {          if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) {
1304              strcat (key, buf);              strcat (key, buf);

Legend:
Removed from v.179  
changed lines
  Added in v.180

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26