/[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 270 by twoaday, Sat Oct 21 18:08:57 2006 UTC revision 271 by twoaday, Sun Nov 5 08:57:45 2006 UTC
# Line 29  Line 29 
29  #include <sys/stat.h>  #include <sys/stat.h>
30  #include <ctype.h>  #include <ctype.h>
31    
32    #include "wptGPG.h"
33  #include "wptKeyserver.h"  #include "wptKeyserver.h"
34  #include "wptErrors.h"  #include "wptErrors.h"
35  #include "wptTypes.h"  #include "wptTypes.h"
36  #include "wptNLS.h"  #include "wptNLS.h"
37  #include "wptW32API.h"  #include "wptW32API.h"
 #include "wptGPG.h"  
38  #include "wptRegistry.h"  #include "wptRegistry.h"
39  #include "wptUTF8.h"  #include "wptUTF8.h"
40    
# Line 83  base64_encode (const char *inbuf, char * Line 83  base64_encode (const char *inbuf, char *
83      char base64code[] =      char base64code[] =
84          "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";          "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
85      int index = 0, temp = 0, res = 0;      int index = 0, temp = 0, res = 0;
86      int i = 0, inputlen = 0, len = 0;      int i, inputlen, len = 0;
87            
88      inputlen = strlen (inbuf);      inputlen = strlen (inbuf);
89      for (i = 0; i < inputlen; i++) {      for (i = 0; i < inputlen; i++) {
# Line 213  check_hkp_response (const char *resp, in Line 213  check_hkp_response (const char *resp, in
213          !strstr (resp, "HTTP/1.1 500 OK"))          !strstr (resp, "HTTP/1.1 500 OK"))
214          return ec; /* http error */          return ec; /* http error */
215    
216      if (!parse_keyserver_error (resp, hkp_errmsg, sizeof (hkp_errmsg)-2)) {      if (!parse_keyserver_error (resp, hkp_errmsg, DIM (hkp_errmsg)-2)) {
217          if (!strlen (hkp_errmsg))          if (!strlen (hkp_errmsg))
218              _snprintf (hkp_errmsg, sizeof (hkp_errmsg)-1,              _snprintf (hkp_errmsg, DIM (hkp_errmsg)-1,
219                          "Unknown keyserver error");                          "Unknown keyserver error");
220          hkp_err = 1;          hkp_err = 1;
221          return ec;          return ec;
# Line 352  sock_read_ext (int fd, char **buffer, in Line 352  sock_read_ext (int fd, char **buffer, in
352          else if (!rc)          else if (!rc)
353              n++;              n++;
354          else {          else {
355              nread = recv (fd, buf, sizeof (buf), 0);              nread = recv (fd, buf, DIM (buf), 0);
356              if (nread == SOCKET_ERROR)              if (nread == SOCKET_ERROR)
357                  return SOCKET_ERROR;                  return SOCKET_ERROR;
358              else if (!nread)              else if (!nread)
# Line 408  wsock_end (void) Line 408  wsock_end (void)
408      char *p;      char *p;
409      int i;      int i;
410    
411    #ifdef WINPT_MOBILE
412        p = m_strdup ("keyserver.conf");
413    #else
414      p = make_special_filename (CSIDL_APPDATA, "winpt\\keyserver.conf", NULL);      p = make_special_filename (CSIDL_APPDATA, "winpt\\keyserver.conf", NULL);
415    #endif
416      kserver_save_conf (p);      kserver_save_conf (p);
417      free_if_alloc (p);      free_if_alloc (p);
418      free_if_alloc (default_keyserver);      free_if_alloc (default_keyserver);
# Line 529  kserver_check_keyid (const char *keyid) Line 533  kserver_check_keyid (const char *keyid)
533          return keyid; /* email address */          return keyid; /* email address */
534      if (strncmp (keyid, "0x", 2)) {      if (strncmp (keyid, "0x", 2)) {
535          memset (&id, 0, sizeof (id));          memset (&id, 0, sizeof (id));
536          _snprintf (id, sizeof (id)-1, "0x%s", keyid);          _snprintf (id, DIM (id)-1, "0x%s", keyid);
537          return id;          return id;
538      }      }
539      return keyid;      return keyid;
# Line 546  update_proxy_user (const char *proxy_use Line 550  update_proxy_user (const char *proxy_use
550      n = 4*strlen (proxy_user) / 3 + 32 + strlen (proxy_pass) + 2;      n = 4*strlen (proxy_user) / 3 + 32 + strlen (proxy_pass) + 2;
551      free_if_alloc (proxy.base64_user);      free_if_alloc (proxy.base64_user);
552      proxy.base64_user = safe_alloc (n+1);      proxy.base64_user = safe_alloc (n+1);
553      _snprintf (t, sizeof (t)-1, "%s:%s", proxy_user, proxy_pass);      _snprintf (t, DIM (t)-1, "%s:%s", proxy_user, proxy_pass);
554      base64_encode (t, proxy.base64_user);      base64_encode (t, proxy.base64_user);
555      free_if_alloc (proxy.user);      free_if_alloc (proxy.user);
556      free_if_alloc (proxy.pass);      free_if_alloc (proxy.pass);
# Line 731  kserver_load_conf (const char *conf) Line 735  kserver_load_conf (const char *conf)
735            
736      pos = 0;      pos = 0;
737      while (fp && !feof (fp)) {      while (fp && !feof (fp)) {
738          s = fgets (buf, sizeof (buf)-1, fp);          s = fgets (buf, DIM (buf)-1, fp);
739          if (!s)          if (!s)
740              break;              break;
741          if (strstr (buf, "\r\n"))          if (strstr (buf, "\r\n"))
# Line 792  kserver_connect (const char *hostname, W Line 796  kserver_connect (const char *hostname, W
796      struct sockaddr_in sock;      struct sockaddr_in sock;
797      char host[128] = {0};      char host[128] = {0};
798      DWORD iaddr;      DWORD iaddr;
799        bool use_proxy = proxy.host != NULL;
800      int rc, fd;      int rc, fd;
801    
     log_debug ("kserver_connect: %s:%d\r\n", hostname, port);  
   
802      if (!port)      if (!port)
803          port = HKP_PORT;          port = HKP_PORT;
804      if (conn_fd)      if (conn_fd)
805          *conn_fd = 0;          *conn_fd = 0;
806      hostname = skip_type_prefix (hostname);      hostname = skip_type_prefix (hostname);
807        log_debug ("kserver_connect: %s:%d\r\n", hostname, port);
808            
809      if (proxy.host && proxy.proto == PROXY_PROTO_HTTP)      if (use_proxy && proxy.proto == PROXY_PROTO_HTTP)
810          port = proxy.port;          port = proxy.port;
811      memset (&sock, 0, sizeof (sock));      memset (&sock, 0, sizeof (sock));
812      sock.sin_family = AF_INET;      sock.sin_family = AF_INET;
813      sock.sin_port = htons (port);      sock.sin_port = htons (port);
814      if (proxy.host)      if (use_proxy)
815          strncpy (host, proxy.host, 127);          strncpy (host, proxy.host, DIM (host)-1);
816      else      else
817          strncpy (host, hostname, 127);          strncpy (host, hostname, DIM (host)-1);
818    
819      if ((iaddr = inet_addr (host)) != INADDR_NONE)      if ((iaddr = inet_addr (host)) != INADDR_NONE)
820          memcpy (&sock.sin_addr, &iaddr, sizeof (iaddr));          memcpy (&sock.sin_addr, &iaddr, sizeof (iaddr));
# Line 823  kserver_connect (const char *hostname, W Line 827  kserver_connect (const char *hostname, W
827      }      }
828      else {      else {
829          log_debug ("gethostbyname: failed ec=%d.\r\n", net_errno);          log_debug ("gethostbyname: failed ec=%d.\r\n", net_errno);
830          return WPTERR_WINSOCK_RESOLVE;          return use_proxy? WPTERR_WINSOCK_PROXY : WPTERR_WINSOCK_RESOLVE;
831      }      }
832      fd = socket (AF_INET, SOCK_STREAM, 0);      fd = socket (AF_INET, SOCK_STREAM, 0);
833      if (fd == INVALID_SOCKET)        if (fd == INVALID_SOCKET)  
# Line 832  kserver_connect (const char *hostname, W Line 836  kserver_connect (const char *hostname, W
836      if (rc == SOCKET_ERROR) {      if (rc == SOCKET_ERROR) {
837          log_debug ("connect: failed ec=%d.\r\n", net_errno);          log_debug ("connect: failed ec=%d.\r\n", net_errno);
838          closesocket (fd);          closesocket (fd);
839          return WPTERR_WINSOCK_CONNECT;          return use_proxy? WPTERR_WINSOCK_PROXY : WPTERR_WINSOCK_CONNECT;
840      }      }
841    
842      if (proxy.proto == PROXY_PROTO_SOCKS5) {      if (proxy.proto == PROXY_PROTO_SOCKS5) {
843            /* XXX: finish code and provide errors constants. */
844          rc = socks_handshake (&proxy, fd, hostname, port);          rc = socks_handshake (&proxy, fd, hostname, port);
845          if (rc) {          if (rc) {
846              closesocket (fd);              closesocket (fd);
847              return WPTERR_GENERAL; /* XXX: use better code. */              return WPTERR_GENERAL;
848          }          }
849      }      }
850    
# Line 1041  kserver_sendkey (const char *hostname, W Line 1046  kserver_sendkey (const char *hostname, W
1046          goto leave;              goto leave;    
1047      }      }
1048            
1049      rc = sock_read (conn_fd, log, sizeof (log)-1, &n);      rc = sock_read (conn_fd, log, DIM (log)-1, &n);
1050      if (rc == SOCKET_ERROR) {      if (rc == SOCKET_ERROR) {
1051          rc = WPTERR_WINSOCK_SENDKEY;          rc = WPTERR_WINSOCK_SENDKEY;
1052          goto leave;          goto leave;
# Line 1069  kserver_search_chkresp (int fd) Line 1074  kserver_search_chkresp (int fd)
1074      int n=0;      int n=0;
1075            
1076      /* parse response 'HTTP/1.0 500 OK' */      /* parse response 'HTTP/1.0 500 OK' */
1077      if (sock_getline (fd, buf, sizeof (buf)-1, &n))      if (sock_getline (fd, buf, DIM (buf)-1, &n))
1078          return WPTERR_KEYSERVER_NOTFOUND;          return WPTERR_KEYSERVER_NOTFOUND;
1079    
1080      log_debug ("kserver_search_chkpresp: %s\r\n", buf);      log_debug ("kserver_search_chkpresp: %s\r\n", buf);
# Line 1174  kserver_search_begin (const char *hostna Line 1179  kserver_search_begin (const char *hostna
1179    
1180      /* Skip all lines until we reach the "info:" record. */      /* Skip all lines until we reach the "info:" record. */
1181      for (;;) {      for (;;) {
1182          if (sock_getline (sock_fd, status, sizeof (status)-1, &reqlen)) {                if (sock_getline (sock_fd, status, DIM (status)-1, &reqlen)) {  
1183              log_debug ("kserver_search_begin: retrieving status line failed.\r\n");              log_debug ("kserver_search_begin: retrieving status line failed.\r\n");
1184              closesocket (sock_fd);              closesocket (sock_fd);
1185              sock_fd = 0;              sock_fd = 0;
# Line 1203  parse_pub_record (keyserver_key_s *key, Line 1208  parse_pub_record (keyserver_key_s *key,
1208      enum pub_rec_t {ID=1, KEYID, ALGO, SIZE, CREATE, EXPIRE};      enum pub_rec_t {ID=1, KEYID, ALGO, SIZE, CREATE, EXPIRE};
1209      char *p;      char *p;
1210      int recno = 0;      int recno = 0;
1211        int off = 0;
1212    
1213      /* pub:BF3DF9B4:17:1024:925411133:: */      /* pub:BF3DF9B4:17:1024:925411133:: */
1214      p = strtok (buf, ":");      p = strtok (buf, ":");
# Line 1212  parse_pub_record (keyserver_key_s *key, Line 1218  parse_pub_record (keyserver_key_s *key,
1218          case ID:          case ID:
1219              break;              break;
1220    
1221          case KEYID:          case KEYID:
1222                /* If for any reason the returned record actually contains
1223                   a fingerprint instead of a key ID, we truncate the fpr. */
1224                off = strlen (p) == 40? 32 : 0;
1225              free_if_alloc (key->keyid);              free_if_alloc (key->keyid);
1226              key->keyid = m_strdup (p);              key->keyid = m_strdup (p+off);
1227              break;              break;
1228    
1229          case ALGO:          case ALGO:
# Line 1235  parse_pub_record (keyserver_key_s *key, Line 1244  parse_pub_record (keyserver_key_s *key,
1244          }          }
1245          p = strtok (NULL, ":");          p = strtok (NULL, ":");
1246      }      }
       
1247  }  }
1248    
1249    
# Line 1319  kserver_search_next (int fd, keyserver_k Line 1327  kserver_search_next (int fd, keyserver_k
1327      key = new keyserver_key_s;      key = new keyserver_key_s;
1328      memset (key, 0, sizeof *key);      memset (key, 0, sizeof *key);
1329      for (;;) {      for (;;) {
1330          if (sock_getline (fd, buf, sizeof (buf)-1, &n))          if (sock_getline (fd, buf, DIM (buf)-1, &n))
1331              break;              break;
1332          /*log_debug ("record: '%s'\r\n", buf); */          /*log_debug ("record: '%s'\r\n", buf); */
1333          if (!strncmp (buf, "pub", 3))          if (!strncmp (buf, "pub", 3))
# Line 1426  do_spawn_ldap_helper (const char *host, Line 1434  do_spawn_ldap_helper (const char *host,
1434          fp = NULL;          fp = NULL;
1435          goto leave;          goto leave;
1436      }      }
1437      get_temp_name (outf, sizeof (outf)-1, keyid);      get_temp_name (outf, DIM (outf)-1, keyid);
1438      get_temp_name (inf, sizeof (inf)-1, NULL);      get_temp_name (inf, DIM (inf)-1, NULL);
1439      fp = fopen (inf, "w+b");      fp = fopen (inf, "w+b");
1440      if (!fp) {      if (!fp) {
1441          log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", inf,          log_box ("LDAP Keyserver Plugin", MB_ERR, "%s: %s", inf,
# Line 1484  ldap_recvkey (const char *host, const ch Line 1492  ldap_recvkey (const char *host, const ch
1492      if (gpgme_data_new (&raw))      if (gpgme_data_new (&raw))
1493          BUG (0);          BUG (0);
1494      while (!feof (fp)) {      while (!feof (fp)) {
1495          s = fgets (buf, sizeof (buf)-1, fp);          s = fgets (buf, DIM (buf)-1, fp);
1496          if (!s)          if (!s)
1497              break;              break;
1498          if (strstr (s, "KEY") && strstr (s, "FAILED")) {          if (strstr (s, "KEY") && strstr (s, "FAILED")) {
# Line 1517  finger_recvkey (const char *host, const Line 1525  finger_recvkey (const char *host, const
1525                  char **r_key, int *r_keylen)                  char **r_key, int *r_keylen)
1526  {  {
1527      gpgme_data_t raw;      gpgme_data_t raw;
1528      char buf[256];      char buf[256+1];
1529      int fd, nread;      int fd, nread;
1530      int start_key = 0;      int start_key = 0;
1531      int rc=0;      int rc=0;
# Line 1533  finger_recvkey (const char *host, const Line 1541  finger_recvkey (const char *host, const
1541          BUG (0);          BUG (0);
1542    
1543      for (;;) {      for (;;) {
1544          if (sock_getline (fd, buf, sizeof (buf), &nread))          if (sock_getline (fd, buf, DIM (buf)-1, &nread))
1545              break;              break;
1546          strcat (buf, "\n");          strcat (buf, "\n");
1547          if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) {          if (strstr (buf, "BEGIN PGP PUBLIC KEY BLOCK")) {

Legend:
Removed from v.270  
changed lines
  Added in v.271

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26