/[winpt]/trunk/Src/wptKeyserverDlg.cpp
ViewVC logotype

Diff of /trunk/Src/wptKeyserverDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 47 by werner, Mon Oct 31 14:04:59 2005 UTC revision 256 by twoaday, Sat Aug 5 10:31:06 2006 UTC
# Line 1  Line 1 
1  /* wptKeyserverDlg.cpp - Keyserver dialog  /* wptKeyserverDlg.cpp - Keyserver dialog
2   *      Copyright (C) 2000-2005 Timo Schulz   *      Copyright (C) 2000-2006 Timo Schulz
3     *      Copyright (C) 2005, 2006 g10 Code GmbH
4   *   *
5   * This file is part of WinPT.   * This file is part of WinPT.
6   *   *
# Line 17  Line 18 
18   * along with WinPT; if not, write to the Free Software Foundation,   * along with WinPT; if not, write to the Free Software Foundation,
19   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20   */   */
   
21  #ifdef HAVE_CONFIG_H  #ifdef HAVE_CONFIG_H
22  #include <config.h>  #include <config.h>
23  #endif  #endif
24    
25  #include <windows.h>  #include <windows.h>
 #include <windows.h>  
26  #include <commctrl.h>  #include <commctrl.h>
27  #include <malloc.h>  #include <ctype.h>
28    
29  #include "resource.h"  #include "resource.h"
30  #include "wptKeyserver.h"  #include "wptKeyserver.h"
# Line 36  Line 35 
35  #include "wptW32API.h"  #include "wptW32API.h"
36  #include "wptVersion.h"  #include "wptVersion.h"
37  #include "wptGPG.h"  #include "wptGPG.h"
38  #include "wptKeyManager.h"  #include "wptGPGME.h"
39  #include "wptContext.h" /* for passphrase_s */  #include "wptContext.h" /* for passphrase_s */
40    #include "wptKeyManager.h"
41  #include "wptDlgs.h"  #include "wptDlgs.h"
42    #include "wptUTF8.h"
43    
44    
45  #define MAX_KEYSIZE 70000  char* get_reg_entry_keyserver (const char *);
   
 char * get_reg_entry_keyserver (const char *);  
46  int set_reg_entry_keyserver (const char *, const char *);  int set_reg_entry_keyserver (const char *, const char *);
47    
48    /* Symbolic column IDs */
49    enum { KS_COL_NAME = 0, KS_COL_PORT };
50    
51    /* Callback context to modify (add, edit) keyserver items. */
52    struct keyserver_rec_s {
53        char name[200];     /* hostname */
54        int  proto;         /* protocol */
55        int  port;          /* used port */
56        bool mode;          /* true = edit */
57        bool cancel;
58    };
59    typedef struct keyserver_rec_s *keyserver_rec_t;
60    
61    
62    /* Print out keyserver error and a possible Winsock error. */
63  static void  static void
64  hkp_err_box (HWND dlg, const char * host, u16 port, int rc)  hkp_err_box (HWND dlg, const char *host, WORD port, int rc)
65  {  {
66      const char *err = kserver_strerror();      const char *err = kserver_strerror ();
67        const char *ws_err = wsock_strerror ();
68      log_box( _("Keyserver"), MB_ERR, "%s:%d: %s", host, port, winpt_strerror (rc));      
69      if (err)      if (ws_err && strlen (ws_err) > 0)
70          msg_box (dlg, err, wsock_strerror (), MB_ERR);          log_box (_("Keyserver"), MB_ERR, "%s:%d: %s", host, port, ws_err);
71  } /* hkp_err_box */      else
72            log_box (_("Keyserver"), MB_ERR, "%s:%d: %s\n%s",
73                     host, port, winpt_strerror (rc), err? err : "");
74    }
75    
76    
77    /* Send the extracted key with keyid @pattern to the
78       keyserver @kserver (port @port).
79       Return value: 0 on success. */
80  int  int
81  hkp_send_key (HWND dlg, const char *kserver, u16 port, const char *pattern)  hkp_send_key (HWND dlg, const char *kserver, WORD port, const char *pattern)
82  {  {
83      gpgme_ctx_t c;      GPGME *gpg;
     gpgme_data_t keydata;  
84      gpgme_error_t ec;      gpgme_error_t ec;
85      char *rawkey = NULL, msg[1024];      char *rawkey = NULL;
86      size_t n;      char msg[256];
     int rc;  
87            
88      ec = gpgme_new( &c );      gpg = new GPGME ();
89      if( ec )      gpg->setArmor (true);
90          BUG( NULL );      ec = gpg->exportToBuffer (pattern, &rawkey);
91      gpgme_set_armor (c, 1);      if (ec) {
92      ec = gpgme_data_new( &keydata );          msg_box (dlg, gpgme_strerror (ec), _("Export"), MB_ERR);
     if( ec )  
         BUG( NULL );  
     rc = (int) gpgme_op_export( c, pattern, 0, keydata );  
     if( rc ) {  
         msg_box( dlg, gpgme_strerror( (gpgme_error_t)rc ), _("Export"), MB_ERR );  
93          goto leave;          goto leave;
94      }      }
95      rawkey = gpgme_data_release_and_get_mem (keydata, &n);      op_begin ();
96      rc = kserver_sendkey (kserver, port, rawkey, n);      ec = kserver_sendkey (kserver, port, rawkey, strlen (rawkey));
97      if (rc) {      op_end ();
98          hkp_err_box (dlg, kserver, port, rc);      if (ec) {
99            hkp_err_box (dlg, kserver, port, ec);
100          goto leave;          goto leave;
101      }      }
102            
# Line 91  hkp_send_key (HWND dlg, const char *kser Line 104  hkp_send_key (HWND dlg, const char *kser
104      status_box (dlg, msg, _("GnuPG status"));      status_box (dlg, msg, _("GnuPG status"));
105            
106  leave:  leave:
107      gpgme_release (c);      delete gpg;
108      if (rawkey)      free_if_alloc (rawkey);
109          gpgme_free (rawkey);      return ec? -1 : 0;
110      return rc;  }
 } /* hkp_send_key */  
111    
112    
113  int  
114  hkp_recv_key (HWND dlg, const char *kserver, u16 port,  /* Show all received keys from the keyserver. */
115                const char *pattern, int proto, int flags)  static void
116    show_imported_keys (gpgme_import_result_t res)
117  {  {
118        gpgme_import_status_t t;
119        gpgme_key_t key;
120        gpgme_data_t msg;
121      gpgme_ctx_t ctx;      gpgme_ctx_t ctx;
122      gpgme_data_t keydata;      gpgme_error_t err;
123      gpgme_error_t ec;      const char *s;
124      gpgme_import_result_t import_res;      char *p, *uid;
125        size_t n=0;
126    
127        if (!res)
128            return;
129    
130        err = gpgme_data_new (&msg);
131        if (err)
132            BUG (NULL);
133        err = gpgme_new (&ctx);
134        if (err)
135            BUG (NULL);
136        if (res->considered > 1) {
137            s = _("WARNING: multiple keys matched request.\n\n");
138            gpgme_data_write (msg, s, strlen (s));
139        }
140    
141        if (res->unchanged == res->considered)
142            s = _("Key(s) successfully received but nothing was changed.");
143        else
144            s = _("Key(s) sucessfully received and imported.");
145            // nice to have the amount of new signatures...
146        gpgme_data_write (msg, s, strlen (s));
147        gpgme_data_write (msg, "\n\n", 2);
148    
149        for (t=res->imports; t; t = t->next) {
150            if (!gpgme_get_key (ctx, t->fpr, &key, 0)) {
151                s = uid = utf8_to_native (key->uids->uid);
152                gpgme_data_write (msg, s, strlen (s));
153                gpgme_data_write (msg, "\n", 1);
154                gpgme_key_release (key);
155                safe_free (uid);
156            }
157        }
158        gpgme_data_write (msg, "\0", 1);
159        p = gpgme_data_release_and_get_mem (msg, &n);
160        if (p != NULL) {
161            msg_box (NULL, p, _("Imported Keys"), MB_INFO);
162            gpgme_free (p);
163        }
164        gpgme_release (ctx);
165    }
166    
167    
168    /* Receive a key from the keyserver @kserver (port @port)
169       with the pattern @pattern.
170       Return value: 0 on success. */
171    static int
172    keyserver_recv_key (HWND dlg, const char *kserver, WORD port,
173                        const char *pattern, int proto, int flags,
174                        char **r_fpr)
175    {
176        gpgme_ctx_t ctx = NULL;
177        gpgme_data_t keydata = NULL;
178        gpgme_import_result_t import_res = NULL;
179        gpgme_error_t err;
180        char *rawkey = NULL;
181        int keylen = 0;
182      int rc;      int rc;
183      char *rawkey = NULL, msg[384];  
184            if (proto == KSPROTO_LDAP) {
185      rawkey = new char[MAX_KEYSIZE];          rc = ldap_recvkey (kserver, pattern, &rawkey, &keylen);
186      if (!rawkey)          if (rc) {
187          BUG (0);              msg_box (dlg, _("LDAP key import failed.\n"
     memset (rawkey, 0, MAX_KEYSIZE);  
     if( proto == KSPROTO_LDAP ) {  
         rc = ldap_recvkey( kserver, pattern, rawkey, MAX_KEYSIZE-1 );  
         if( rc ) {  
             msg_box( dlg, _("LDAP key import failed.\n"  
188                              "Please make sure you have an online connection"                              "Please make sure you have an online connection"
189                              " and gpgkeys_ldap.exe is installed"),                              " and gpgkeys_ldap.exe is installed"),
190                              _("Keyserver"), MB_ERR );                              _("Keyserver"), MB_ERR);
191              free_if_alloc( rawkey );              free_if_alloc (rawkey);
192              return rc;              return rc;
193          }          }
194      }      }
195      else if (proto == KSPROTO_FINGER) {      else if (proto == KSPROTO_FINGER) {
196          rc = finger_recvkey (kserver, pattern, rawkey, MAX_KEYSIZE-1);          rc = finger_recvkey (kserver, pattern, &rawkey, &keylen);
197          if (rc) {          if (rc) {
198              log_box (_("Keyserver"), MB_ERR, _("Finger key import failed: %s\n"),              log_box (_("Keyserver"), MB_ERR,
199                       winpt_strerror (rc));                       _("Finger key import failed: %s\n"), winpt_strerror (rc));
200              free_if_alloc (rawkey);              free_if_alloc (rawkey);
201              return rc;              return rc;
202          }          }
203      }      }
204      else if( ( rc = kserver_recvkey( kserver, port, kserver_check_keyid( pattern ),      else if ((rc = kserver_recvkey (kserver, port,
205                                       rawkey, MAX_KEYSIZE-1 ) ) ) {                                      kserver_check_keyid (pattern),
206                                        &rawkey, &keylen))) {
207          hkp_err_box (dlg, kserver, port, rc);          hkp_err_box (dlg, kserver, port, rc);
208          free_if_alloc (rawkey);          free_if_alloc (rawkey);
209          return rc;          return rc;
210      }      }
211      else {  
212          if( !strstr( rawkey, "BEGIN PGP PUBLIC KEY BLOCK" ) ) {      if (!strstr (rawkey, "BEGIN PGP PUBLIC KEY BLOCK")) {
213              msg_box( dlg, _("This is not a valid OpenPGP key."), _("Keyserver"), MB_ERR );          msg_box (dlg, _("This is not a valid OpenPGP key."),
214              goto leave;                   _("Keyserver"), MB_ERR);
215          }          goto leave;
216          ec = gpgme_new( &ctx );      }
217          if( ec )      if (gpgme_new (&ctx))
218              BUG( NULL );          BUG (NULL);
219          gpgme_data_new_from_mem( &keydata, rawkey, strlen( rawkey ), 1 );      gpgme_data_new_from_mem (&keydata, rawkey, strlen (rawkey), 1);
220          rc = gpgme_op_import( ctx, keydata );      err = gpgme_op_import (ctx, keydata);
221          if( rc ) {      if (err) {
222              msg_box( dlg, gpgme_strerror( (gpgme_error_t)rc ), _("Import"), MB_ERR );          msg_box (dlg, gpgme_strerror (err), _("Import"), MB_ERR);
223              goto leave;          goto leave;
224          }      }
225          import_res = gpgme_op_import_result (ctx);      import_res = gpgme_op_import_result (ctx);
226      }      if (import_res && r_fpr)
227            *r_fpr = m_strdup (import_res->imports->fpr);
228            
229      /* if we use the refresh mode, a lot of keys will be fetched and thus only      /* if we use the refresh mode, a lot of keys will be fetched and thus only
230         a summarize at the end is presented and not for each key. */         a summarize at the end is presented and not for each key. */
231      if (!(flags & KM_KS_REFRESH)) {      if (!(flags & KM_KS_REFRESH)) {
232          if (import_res->new_user_ids == 0) {          show_imported_keys (import_res);
233              _snprintf (msg, DIM (msg)-1,          if (import_res && import_res->unchanged == import_res->considered) {
234                         _("Key '%s' successfully received but nothing was changed."), pattern );              rc = WPTERR_GENERAL; /* no keys updated. */
             status_box (dlg, msg, _("GnuPG Status"));  
             rc = WPTERR_GENERAL;  
235              goto leave;              goto leave;
236          }          }
         _snprintf (msg, DIM (msg)-1, _("Key '%s' sucessfully received and imported."), pattern);  
         status_box (dlg, msg, _("GnuPG Status"));  
237      }      }
238        
239  leave:  leave:
240      free_if_alloc (rawkey);      free_if_alloc (rawkey);
241      gpgme_release (ctx);      if (ctx != NULL)
242      gpgme_data_release (keydata);          gpgme_release (ctx);
243        if (keydata != NULL)
244            gpgme_data_release (keydata);
245      return rc;      return rc;
246  } /* hkp_recv_key */  }
247    
248    
249  #define my_iskeychar(a) ( ( (a) >='0' && (a) <= '9' ) || ( (a) >= 'A' && (a) <= 'F' ) )  /* Alias for keyserver_recv_key but without the ability to return
250       the fingerprint of the key. */
251    int
252    hkp_recv_key (HWND dlg, const char *kserver, WORD port,
253                  const char *pattern, int proto, int flags)
254    {
255        return keyserver_recv_key (dlg, kserver, port, pattern,
256                                   proto, flags, NULL);
257    }
258    
259  static int  int
260  check_pattern( const char *pattern )  hkp_recv_key2 (HWND dlg, const char *kserver, WORD port,
261                   const char *pattern, int proto, char **r_fpr)
262  {  {
263      int rc = 1;      return keyserver_recv_key (dlg, kserver, port, pattern,
264                                       proto, 0, r_fpr);
265      /* Whitespace are not allowed! */  }
266      if( strchr( pattern, ' ') ) {  
267          rc = WPTERR_GENERAL;  
268          goto leave;  /* Check if the given pattern are either a valid
269      }     email address, a {long, short} keyid or a fingerprint.
270           Return 0 on success. */
271      if( (( strstr( pattern, "0x" ) ) && ( strlen( pattern ) == 10 ) )  static int
272           || (strstr(pattern, "0x")) && ( strlen( pattern )  == 18 ) ) {  check_pattern (const char *pattern)
273          rc = 0;  {    
274          goto leave;      size_t i;
275      }  
276            if (strchr (pattern, ' '))
277      if( (( my_iskeychar( pattern[0] )) && ( strlen( pattern )  == 8 ) )          return WPTERR_GENERAL; /* do not allow white spaces. */
278           || (my_iskeychar(pattern[0])) && ( strlen( pattern ) == 16) ) {      
279          rc = 0;      if (strchr (pattern, '@') && strlen (pattern) >= 3 &&
280          goto leave;          !check_email_address (pattern))
281      }          return 0;
282        
283      if( ( strchr( pattern, '@' ) ) && ( strlen( pattern ) >= 3 ) ) {      if (strstr (pattern, "0x"))
284          rc = 0;          pattern += 2;
285          goto leave;      if (strlen (pattern) != 8 &&
286            strlen (pattern) != 16 &&
287            strlen (pattern) != 40)
288            return WPTERR_GENERAL;
289    
290        for (i=0; i < strlen (pattern); i++) {
291            if (!isxdigit (pattern[i]))
292                return WPTERR_GENERAL;
293      }      }
294            return 0;
295  leave:  }
     return rc;  
 } /* check_pattern */  
296    
297    
298  static void  /* Return human readable name for the proxy protocol. */
299  set_proxy (HWND dlg)  static const char*
300  {  name_from_proto (int proto)
301      char proxy[384];  {
302      int port = 0;      const char *s;
303        
304      strcpy (proxy, "HTTP proxy: ");      switch (proto) {
305      if (kserver_get_proxy (&port)) {      case PROXY_PROTO_NONE:      s = ""; break;
306          char t[256];      case PROXY_PROTO_HTTP:      s = "HTTP"; break;
307          const char *http = kserver_get_proxy (&port);      case PROXY_PROTO_SOCKS5:    s = "SOCKS5"; break;
308          _snprintf (t, sizeof (t) - 1, "\"%s:%d\"", http, port);      default:                    s = "HTTP"; break;
         strcat (proxy, t);  
309      }      }
310      else      return s;
311          strcat( proxy, "none" );  }
     SetDlgItemText( dlg, IDC_KEYSERVER_PROXY, proxy );  
 } /* set_proxy */  
312    
313    
314  static int inline  static int inline
# Line 242  kserver_get_pos (listview_ctrl_t lv) Line 320  kserver_get_pos (listview_ctrl_t lv)
320  }  }
321    
322    
323  static u16 inline  static WORD inline
324  kserver_get_port (listview_ctrl_t lv)  kserver_get_port (listview_ctrl_t lv)
325  {  {
326      char buf[16];      char buf[16];
327    
328      listview_get_item_text (lv, kserver_get_pos (lv), 3, buf, 15);      listview_get_item_text (lv, kserver_get_pos (lv),
329      return (u16)strtoul (buf, NULL, 10);                              KS_COL_PORT, buf, sizeof (buf)-1);
330        return (WORD)strtoul (buf, NULL, 10);
331  }  }
332    
333    
334  static void  static void
335  load_default_ks (listview_ctrl_t lv)  set_default_ks (listview_ctrl_t lv)
336  {  {
337      char * p, buf[192];      char *p, buf[128];
338      int i;      int i;
339    
340      p = get_reg_entry_keyserver ("Default");      p = get_reg_entry_keyserver ("Default");
341      if (!p)      if (!p)
342          return;          return;
343      for( i = 0; i < listview_count_items( lv, 0 ); i++ ) {      for (i=0; i < listview_count_items (lv, 0); i++) {
344          listview_get_item_text( lv, i, 0, buf, sizeof (buf)-1 );          listview_get_item_text (lv, i, KS_COL_NAME, buf, sizeof (buf)-1);      
345          if( !strncmp( p, buf, strlen( p ) ) ) {          if (!stricmp (buf, p)) {
346              listview_add_sub_item( lv, i, 2, "x" );              listview_select_one (lv, i);
347              break;              break;
348          }          }
349      }      }
350      free_if_alloc (p);      free_if_alloc (p);
351  } /* load_default_ks */  }
352    
353    
354    /* Save the selected keyserver from @lv as the default server. */
355  static int  static int
356  save_default_ks (listview_ctrl_t lv)  save_default_ks (listview_ctrl_t lv)
357  {  {
358      char buf[192], port[32];      char buf[192], port[32];
359      int idx, i;        int idx, i;  
360    
361      idx = listview_get_curr_pos( lv );      idx = listview_get_curr_pos (lv);
362      if( idx == -1 ) {      if (idx == -1) {
363          msg_box( NULL, _("Please select one of the servers."), _("Keyserver"), MB_ERR );          msg_box (NULL, _("Please select one of the servers."),
364                     _("Keyserver"), MB_ERR);
365          return -1;          return -1;
366      }      }
367      listview_get_item_text( lv, idx, 1, buf, sizeof (buf)-1 );      listview_get_item_text (lv, idx, KS_COL_NAME, buf, sizeof (buf)-1);
368      if( *buf != 'H' ) {      if (strnicmp (buf, "http", 4) && strnicmp (buf, "hkp", 3)) {
369          msg_box( NULL, _("Only HTTP keyserver can be used."), _("Keyserver"), MB_ERR );          msg_box (NULL, _("Only HKP keyserver can be used."),
370                     _("Keyserver"), MB_ERR);
371          return -1;          return -1;
372      }      }
     for (i = 0; i < listview_count_items( lv, 0 ); i++)  
         listview_add_sub_item (lv, i, 2, "");  
     listview_add_sub_item (lv, idx, 2, "x");  
     listview_get_item_text (lv, idx, 0, buf, sizeof (buf)-1);  
373      set_reg_entry_keyserver ("Default", buf);      set_reg_entry_keyserver ("Default", buf);
374      i = kserver_get_port (lv);      i = kserver_get_port (lv);
375      sprintf (port, "%d", i);      sprintf (port, "%d", i);
376      set_reg_entry_keyserver ("Default_Port", port);      set_reg_entry_keyserver ("Default_Port", port);
377      keyserver_set_default (buf, (u16)i);      keyserver_set_default (buf, (WORD)i);
378      return 0;      return 0;
379  } /* save_default_ks */  }
380    
381    
382  int  int
383  keyserver_list_build (listview_ctrl_t *r_lv, HWND hwnd)  keyserver_list_build (listview_ctrl_t *r_lv, HWND hwnd)
384  {  {
385      struct listview_column_s keyserver[] = {      struct listview_column_s keyserver[] = {
386          {0, 160, (char *)_("DNS Name")},                  {0, 180, (char *)_("DNS Name")},
387          {1,  52, (char *)_("Protocol")},          {1,  55, (char *)_("Port")},
         {2,  46, (char *)_("Default")},  
         {3,  46, (char *)_("Port")},  
388          {0,   0, NULL}          {0,   0, NULL}
389      };      };
390        HICON ico[1];
391      listview_ctrl_t lv;      listview_ctrl_t lv;
392      char buf[32];      char buf[32];
393      int j;      int j;
394    
395      listview_new (&lv);          ico[0] = LoadIcon (glob_hinst, (LPCTSTR)IDI_COMPUTER);
396      lv->ctrl = hwnd;            listview_new (&lv, hwnd);    
397      for (j=0; keyserver[j].fieldname; j++)      for (j=0; keyserver[j].fieldname; j++)
398          listview_add_column (lv, &keyserver[j]);                  listview_add_column (lv, &keyserver[j]);
399      for (j = 0; j<MAX_KEYSERVERS; j++) {      listview_set_image_list (lv, 16, 16, ico, 1);
400        for (j = 0; j < MAX_KEYSERVERS; j++) {
401          if (!server[j].used)          if (!server[j].used)
402              continue;              continue;
403          listview_add_item (lv, " ");          listview_add_item_image (lv, " ", 0);
404          listview_add_sub_item (lv, 0, 0, server[j].name);          listview_add_sub_item (lv, 0, KS_COL_NAME, server[j].name);
         switch (server[j].proto) {  
         case KSPROTO_HTTP:  
             listview_add_sub_item( lv, 0, 1, "H" ); break;  
         case KSPROTO_LDAP:  
             listview_add_sub_item( lv, 0, 1, "L" ); break;  
         case KSPROTO_FINGER:  
             listview_add_sub_item( lv, 0, 1, "F" ); break;  
         }  
405          sprintf (buf, "%d", server[j].port);          sprintf (buf, "%d", server[j].port);
406          listview_add_sub_item (lv, 0, 3, buf);          listview_add_sub_item (lv, 0, KS_COL_PORT, buf);
407      }      }
     load_default_ks (lv);  
408      if (listview_count_items (lv, 0) == 0) {      if (listview_count_items (lv, 0) == 0) {
409          listview_add_item (lv, "");          listview_add_item (lv, "");
410          listview_add_sub_item (lv, 0, 0, DEF_HKP_KEYSERVER);          listview_add_sub_item (lv, 0, KS_COL_NAME, DEF_HKP_KEYSERVER);
         listview_add_sub_item (lv, 0, 1, "H");  
411      }      }
412      listview_set_ext_style (lv);      listview_set_ext_style (lv);
413      *r_lv = lv;      *r_lv = lv;
# Line 346  keyserver_list_build (listview_ctrl_t *r Line 415  keyserver_list_build (listview_ctrl_t *r
415  }  }
416    
417    
418    static void
419    keyserver_remove (listview_ctrl_t lv)
420    {
421        char name[100];
422        int pos = listview_get_curr_pos (lv);
423        int i;
424    
425        listview_get_item_text (lv, pos, KS_COL_NAME, name, sizeof (name)-1);
426        for (i=0; i < MAX_KEYSERVERS; i++) {
427            if (server[i].name && stricmp (server[i].name, name) == 0)
428                server[i].used = 0;
429        }
430        listview_del_item (lv, pos);
431    }
432    
433    
434    static void
435    keyserver_edit (listview_ctrl_t lv, keyserver_rec_t ctx)
436    {
437        char buf[16];
438        bool fnd = false;
439        int i;
440    
441        for (i=0; i < MAX_KEYSERVERS; i++) {
442            if (server[i].name && stricmp (server[i].name, ctx->name) == 0) {
443                fnd = true;
444                break;
445            }
446        }
447        if (!fnd)
448            return;
449        server[i].port = ctx->port;
450    
451        i = listview_get_curr_pos (lv);
452        sprintf (buf, "%d", ctx->port);
453        listview_add_sub_item (lv, i, KS_COL_NAME, ctx->name);
454        listview_add_sub_item (lv, i, KS_COL_PORT, buf);
455    }
456    
457    static void
458    keyserver_add (listview_ctrl_t lv, keyserver_rec_t ctx)
459    {
460        char buf[16];
461        bool fnd = false;
462        int i;
463    
464        for (i=0; i < MAX_KEYSERVERS; i++) {
465            if (server[i].used == 0) {
466                fnd = true;
467                break;
468            }
469        }
470        if (!fnd) {
471            msg_box (NULL, _("No space for new keyserver entry"),
472                     _("Keyserver"), MB_ERR);
473            return;
474        }
475        free_if_alloc (server[i].name);
476        server[i].name = m_strdup (ctx->name);
477        server[i].port = ctx->port;
478        server[i].proto = ctx->proto;
479        server[i].used = 1;
480    
481        sprintf (buf, "%d", ctx->port);
482        listview_add_item (lv, " ");
483        listview_add_sub_item (lv, 0, KS_COL_NAME, ctx->name);
484        listview_add_sub_item (lv, 0, KS_COL_PORT, buf);
485    }
486    
487    
488    static void
489    fill_keyserv_types (HWND dlg)
490    {
491        HWND h = GetDlgItem (dlg, IDC_KSERVADD_TYPES);
492        combox_add_string (h, _("HKP Keyserver"));
493        combox_add_string (h, _("LDAP Keyserver"));
494        combox_add_string (h, _("Finger Keyserver"));
495        SendMessage (h, CB_SETCURSEL, 0, 0);
496    }
497    
498    
499    /* Dialog box procedure for modify keyservers. */
500    BOOL CALLBACK
501    keyserver_modify_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
502    {
503        static keyserver_rec_t ctx;
504        char tmp[128];
505    
506        switch (msg) {
507        case WM_INITDIALOG:
508            fill_keyserv_types (dlg);
509            ctx = (keyserver_rec_t)lparam;
510            if (ctx->mode) {
511                SendDlgItemMessage (dlg, IDC_KSERVADD_TYPES, CB_SETCURSEL, ctx->proto, 0);
512                SetDlgItemText (dlg, IDC_KSERVADD_HOST, ctx->name);
513                SetDlgItemInt (dlg, IDC_KSERVADD_PORT, ctx->port, FALSE);
514                EnableWindow (GetDlgItem (dlg, IDC_KSERVADD_HOST), FALSE);
515                EnableWindow (GetDlgItem (dlg, IDC_KSERVADD_TYPES), FALSE);
516            }
517            else
518                SetDlgItemInt (dlg, IDC_KSERVADD_PORT, 11371, FALSE);      
519            SetWindowText (dlg, _("Edit Keyserver"));
520            SetDlgItemText (dlg, IDOK, _("&Add"));
521            SetDlgItemText (dlg, IDCANCEL, _("&Cancel"));
522            SetDlgItemText (dlg, IDC_KSERVADD_TYPEINF, _("Type:"));
523            SetDlgItemText (dlg, IDC_KSERVADD_PORTINF, _("Port:"));
524            SetDlgItemText (dlg, IDC_KSERVADD_HOSTINF, _("Host name:"));
525            SetForegroundWindow (dlg);
526            break;
527    
528        case WM_COMMAND:
529            switch (LOWORD (wparam)) {
530            case IDOK:
531                if (!GetDlgItemText (dlg, IDC_KSERVADD_HOST, tmp, sizeof (tmp)-1)) {
532                    msg_box (dlg, _("Please enter a host name"), _("Keyserver"), MB_ERR);
533                    return FALSE;
534                }      
535                ctx->port = GetDlgItemInt (dlg, IDC_KSERVADD_PORT, NULL, FALSE);
536                if (ctx->port > 65535) {
537                    msg_box (dlg, _("Invalid port, valid numbers are < 65535"), _("Keyserver"), MB_ERR);
538                    return FALSE;
539                }
540                ctx->proto = SendDlgItemMessage (dlg, IDC_KSERVADD_TYPES, CB_GETCURSEL, 0, 0);
541                ctx->cancel = false;
542                ctx->name[0] = '\0';
543                if (!strstr (tmp, "://")) {
544                    switch (ctx->proto) {
545                    case KSPROTO_HTTP: strcpy (ctx->name, "http://"); break;
546                    case KSPROTO_LDAP: strcpy (ctx->name, "ldap://"); break;
547                    case KSPROTO_FINGER:strcpy (ctx->name, "finger://"); break;
548                    }
549                }
550                strcat (ctx->name, tmp);
551                ctx->cancel = false;
552                EndDialog (dlg, TRUE);
553                break;
554    
555            case IDCANCEL:
556                ctx->cancel = true;
557                EndDialog (dlg, FALSE);
558                break;
559            }
560            break;
561        }
562    
563        return FALSE;
564    }
565    
566    /* Dialog box procedure to access keyservers. */
567  BOOL CALLBACK  BOOL CALLBACK
568  keyserver_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)  keyserver_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
569  {  {
570      static listview_ctrl_t lv = NULL;      static listview_ctrl_t lv = NULL;
571      static int lv_idx = 0;      static int lv_idx = 0;
     int rc, proto_nr = 0;  
     char kserver[128], pattern[128];  
     char proto[16];      
572      keyserver_ctx ksc;      keyserver_ctx ksc;
573        struct keyserver_rec_s edit;
574        char kserver[128], pattern[128];
575        char proto[16], *fpr = NULL;
576        int rc, proto_nr = 0;
577            
578      switch ( msg ) {      switch (msg) {
579      case WM_INITDIALOG:      case WM_INITDIALOG:
     #ifndef LANG_DE  
580          SetWindowText (dlg, _("Keyserver Access"));          SetWindowText (dlg, _("Keyserver Access"));
581          SetDlgItemText (dlg, IDC_KEYSERVER_RECV, _("&Receive"));          SetDlgItemText (dlg, IDC_KEYSERVER_RECV, _("&Receive"));
582          SetDlgItemText (dlg, IDC_KEYSERVER_SEND,          SetDlgItemText (dlg, IDC_KEYSERVER_SEND,
583                          _("Send key (default is receiving)"));                          _("Send key (default is receiving)"));
584          SetDlgItemText (dlg, IDC_KEYSERVER_INFO,          SetDlgItemText (dlg, IDC_KEYSERVER_INFO,
585                          _("Please enter the key ID or email address you search for"));                          _("Key ID or email address you want to search for"));
586          SetDlgItemText (dlg, IDC_KEYSERVER_INDEX, _("&Search"));          SetDlgItemText (dlg, IDC_KEYSERVER_INDEX, _("&Search"));
587          SetDlgItemText (dlg, IDC_KEYSERVER_PROXSETT, _("C&hange"));          SetDlgItemText (dlg, IDC_KEYSERVER_PROXSETT, _("C&hange proxy"));
588          SetDlgItemText (dlg, IDC_KEYSERVER_DEFAULT, _("Set &default"));          SetDlgItemText (dlg, IDC_KEYSERVER_DEFAULT, _("Set &default"));
589      #endif          SetDlgItemText (dlg, IDCANCEL, _("&Close"));
590          set_proxy (dlg);          
591          keyserver_list_build (&lv, GetDlgItem (dlg, IDC_KEYSERVER_LIST));          keyserver_list_build (&lv, GetDlgItem (dlg, IDC_KEYSERVER_LIST));
592            set_default_ks (lv);
593          center_window (dlg, NULL);          center_window (dlg, NULL);
594          SetForegroundWindow (dlg);          SetForegroundWindow (dlg);
595          return TRUE;          return TRUE;
# Line 378  keyserver_dlg_proc (HWND dlg, UINT msg, Line 597  keyserver_dlg_proc (HWND dlg, UINT msg,
597      case WM_NOTIFY:      case WM_NOTIFY:
598          NMHDR *notify;          NMHDR *notify;
599          notify = (NMHDR *)lparam;          notify = (NMHDR *)lparam;
600          if( notify && notify->code == NM_CLICK          if (!notify)
601              && notify->idFrom == IDC_KEYSERVER_LIST )              break;
602              lv_idx = listview_get_curr_pos( lv );          if (notify->code == NM_CLICK
603                && notify->idFrom == IDC_KEYSERVER_LIST)
604                lv_idx = listview_get_curr_pos (lv);
605            else if (notify->code == NM_RCLICK &&
606                     notify->idFrom == IDC_KEYSERVER_LIST) {
607                POINT p;
608                GetCursorPos (&p);
609                HMENU hm, pop;
610                hm = LoadMenu (glob_hinst, (LPCTSTR)IDR_WINPT_KEYSERVER_CTX);
611                pop = GetSubMenu (hm, 0);
612                set_menu_text (pop, ID_KSERVCTX_ADD, _("&Add"));
613                set_menu_text (pop, ID_KSERVCTX_DEL, _("&Remove"));
614                set_menu_text (pop, ID_KSERVCTX_EDIT, _("&Edit"));
615                TrackPopupMenu (pop, 0, p.x, p.y, 0, dlg, NULL);
616                DestroyMenu (hm);
617                DestroyMenu (pop);
618            }
619            else if (notify->code == LVN_ITEMCHANGED &&
620                     ((LPNMLISTVIEW)lparam)->uNewState) {
621                listview_get_item_text (lv, listview_get_curr_pos (lv),
622                                        KS_COL_NAME, kserver, sizeof (kserver)-1);
623                if (!strnicmp (kserver, "ldap://", 7))
624                    EnableWindow (GetDlgItem (dlg, IDC_KEYSERVER_INDEX), FALSE);
625                else
626                    EnableWindow (GetDlgItem (dlg, IDC_KEYSERVER_INDEX), TRUE);
627            }
628          return TRUE;          return TRUE;
629                    
630      case WM_DESTROY:      case WM_DESTROY:
631          if( lv ) {          if (lv) {
632              listview_release( lv );              listview_release (lv);
633              lv = NULL;              lv = NULL;
634          }          }
635          lv_idx = 0;          lv_idx = 0;
636          return FALSE;          return FALSE;
637                    
638      case WM_SYSCOMMAND:      case WM_SYSCOMMAND:
639          if( LOWORD( wparam ) == SC_CLOSE )          if (LOWORD (wparam) == SC_CLOSE)
640              EndDialog( dlg, TRUE );              EndDialog (dlg, TRUE);
641          return FALSE;          return FALSE;  
642            
643      case WM_COMMAND:      case WM_COMMAND:
644          switch( LOWORD( wparam ) ) {          switch (LOWORD (wparam)) {
645          case IDC_KEYSERVER_PROXSETT:          case IDC_KEYSERVER_PROXSETT:
646              dialog_box_param( glob_hinst, (LPCTSTR)IDD_WINPT_KEYSERVER_PROXY, glob_hwnd,              dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_KEYSERVER_PROXY,
647                                keyserver_proxy_dlg_proc, NULL,                                dlg, keyserver_proxy_dlg_proc, 0,
648                                _("Proxy Settings"), IDS_WINPT_KEYSERVER_PROXY );                                _("Proxy Settings"), IDS_WINPT_KEYSERVER_PROXY);
             set_proxy( dlg );  
649              return TRUE;              return TRUE;
650                            
651          case IDC_KEYSERVER_INDEX:          case IDC_KEYSERVER_INDEX:
652              if (!lv_idx) {              if (!lv_idx)
653                  lv_idx = kserver_get_pos (lv);                  lv_idx = kserver_get_pos (lv);
654                  if (lv_idx == -1) {              
                     msg_box (dlg, _("Please select one of the keyservers."), _("Keyserver"), MB_INFO);  
                     return FALSE;  
                 }  
             }  
             listview_get_item_text (lv, lv_idx, 1, proto, sizeof (proto)-1);  
             if (*proto == 'L') {  
                 msg_box( dlg, _("This is not implemented yet!"), _("Keyserver"), MB_ERR );  
                 return FALSE;  
             }  
             listview_get_item_text (lv, lv_idx, 0, kserver, sizeof (kserver)-1);  
655              if (!GetDlgItemText (dlg, IDC_KEYSERVER_SEARCH, pattern, sizeof (pattern)-1)) {              if (!GetDlgItemText (dlg, IDC_KEYSERVER_SEARCH, pattern, sizeof (pattern)-1)) {
656                  msg_box (dlg, _("Please enter the search pattern."), _("Keyserver"), MB_INFO);                  msg_box (dlg, _("Please enter the search pattern."),
657                             _("Keyserver"), MB_INFO);
658                  return FALSE;                  return FALSE;
659              }              }
660              ksc.name = kserver;  
661                if (lv_idx != -1) {
662                    listview_get_item_text (lv, lv_idx, KS_COL_NAME,
663                                            kserver, sizeof (kserver)-1);
664                    ksc.name = kserver;
665                    ksc.port = kserver_get_port (lv);
666                }
667                else {
668                    ksc.name = DEF_HKP_KEYSERVER;
669                    ksc.port = HKP_PORT;
670                }
671              ksc.pattern = pattern;              ksc.pattern = pattern;
672              ksc.port = kserver_get_port (lv);              DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_HKPSEARCH, dlg,
673              DialogBoxParam( glob_hinst, (LPCSTR)IDD_WINPT_HKPSEARCH, dlg,                              hkpsearch_dlg_proc, (LPARAM) &ksc);
                             hkpsearch_dlg_proc, (LPARAM) &ksc );  
674              return TRUE;              return TRUE;
675                            
676          case IDC_KEYSERVER_RECV:          case IDC_KEYSERVER_RECV:
# Line 435  keyserver_dlg_proc (HWND dlg, UINT msg, Line 678  keyserver_dlg_proc (HWND dlg, UINT msg,
678              if (!lv_idx) {              if (!lv_idx) {
679                  lv_idx = kserver_get_pos (lv);                  lv_idx = kserver_get_pos (lv);
680                  if (lv_idx == -1) {                  if (lv_idx == -1) {
681                      msg_box( dlg, _("Please select one of the keyservers."), _("Keyserver"), MB_INFO );                      msg_box (dlg, _("Please select one of the keyservers."),
682                                _("Keyserver"), MB_INFO);
683                      return FALSE;                      return FALSE;
684                  }                  }
685              }              }
686              listview_get_item_text( lv, lv_idx, 1, proto, sizeof (proto)-1 );              listview_get_item_text (lv, lv_idx, KS_COL_NAME,
687                                        proto, sizeof (proto)-1);
688              proto_nr = KSPROTO_HTTP;              proto_nr = KSPROTO_HTTP;
689              if( *proto == 'L' )              if (!strnicmp  (proto, "ldap", 4))
690                  proto_nr = KSPROTO_LDAP;                  proto_nr = KSPROTO_LDAP;
691              else if( *proto == 'F' )              else if (!strnicmp (proto, "finger", 6))
692                  proto_nr = KSPROTO_FINGER;                  proto_nr = KSPROTO_FINGER;
693              listview_get_item_text( lv, lv_idx, 0, kserver, sizeof (kserver)-1 );              listview_get_item_text (lv, lv_idx, KS_COL_NAME,
694              if( !GetDlgItemText( dlg, IDC_KEYSERVER_SEARCH, pattern, sizeof (pattern)-1 ) ) {                                      kserver, sizeof (kserver)-1);
695                  msg_box( dlg, _("Please enter the search pattern."), _("Keyserver"), MB_INFO );              if (!GetDlgItemText(dlg, IDC_KEYSERVER_SEARCH,
696                                    pattern, sizeof (pattern)-1)) {
697                    msg_box (dlg, _("Please enter the search pattern."),
698                             _("Keyserver"), MB_INFO);
699                  return FALSE;                  return FALSE;
700              }              }
701              if( proto_nr == KSPROTO_LDAP && strchr( pattern, '@' ) ) {              if (proto_nr == KSPROTO_LDAP && strchr (pattern, '@')) {
702                  msg_box( dlg, _("Only keyids are allowed."), _("LDAP Keyserver"), MB_INFO );                  msg_box (dlg, _("Only keyids are allowed."),
703                             _("Keyserver"), MB_INFO);
704                  return FALSE;                  return FALSE;
705              }              }
706              else if (proto_nr == KSPROTO_FINGER) {              else if (proto_nr == KSPROTO_FINGER) {
707                  if (strchr (pattern, '@') || strchr (pattern, ' ')) {                  if (strchr (pattern, '@') || strchr (pattern, ' ')) {
708                      msg_box (dlg, _("Only enter the name of the user."), _("FINGER Keyserver"), MB_INFO);                      msg_box (dlg, _("Only enter the name of the user."),
709                                 _("Keyserver"), MB_INFO);
710                      return FALSE;                      return FALSE;
711                  }                  }
712              }              }
713              else if( check_pattern( pattern ) ) {              else if (check_pattern (pattern)) {
714                  msg_box( dlg, _("Only email addresses or keyids are allowed."), _("HKP Keyserver"), MB_INFO );                  msg_box (dlg, _("Only email addresses or keyids are allowed."),
715                            _("Keyserver"), MB_INFO);
716                  return FALSE;                  return FALSE;
717              }              }
718              rc = hkp_recv_key (dlg, kserver, kserver_get_port (lv), pattern, proto_nr, 0);              rc = keyserver_recv_key (dlg, kserver, kserver_get_port (lv),
719              if (!rc)                                       pattern, proto_nr, 0,
720                  keycache_set_reload (1);                                       &fpr);
721                if (!rc && fpr != NULL) {
722                    keycache_update (0, fpr);
723                    free_if_alloc (fpr);
724                }
725              return TRUE;              return TRUE;
726    
727          case IDC_KEYSERVER_DEFAULT:          case IDC_KEYSERVER_DEFAULT:
728              save_default_ks( lv );              save_default_ks (lv);
729                return TRUE;
730    
731            case ID_KSERVCTX_ADD:
732                memset (&edit, 0, sizeof (edit));
733                edit.mode = false;
734                dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_KEYSERVER_ADD, dlg,
735                                    keyserver_modify_dlg_proc,
736                                    (LPARAM)&edit, _("Edit Keyserver"), 0);
737                if (!edit.cancel)
738                    keyserver_add (lv, &edit);
739                return TRUE;
740    
741            case ID_KSERVCTX_EDIT:
742                memset (&edit, 0, sizeof (edit));
743                edit.mode = true;
744                listview_get_item_text (lv, lv_idx, KS_COL_NAME,
745                                        edit.name, sizeof (edit.name)-1);
746                listview_get_item_text (lv, lv_idx, KS_COL_PORT, pattern, 32);
747                edit.port = atoi (pattern);
748                if (!strnicmp (edit.name, "hkp", 3) || !strnicmp (edit.name, "http", 4))
749                    edit.proto = KSPROTO_HTTP;
750                else if (!strnicmp (edit.name, "ldap", 4))
751                    edit.proto = KSPROTO_LDAP;
752                else
753                    edit.proto = KSPROTO_FINGER;
754                dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_KEYSERVER_ADD, dlg,
755                                    keyserver_modify_dlg_proc,
756                                    (LPARAM)&edit, _("Edit Keyserver"), 0);
757                if (!edit.cancel)
758                    keyserver_edit (lv, &edit);
759              break;              break;
760    
761            case ID_KSERVCTX_DEL:
762                keyserver_remove (lv);
763                return TRUE;
764                            
765          case IDCANCEL:          case IDCANCEL:
766              EndDialog( dlg, FALSE );              EndDialog (dlg, FALSE);
767              return FALSE;              return TRUE;
768          }          }
769          break;          break;
770      }      }
771            
772      return FALSE;      return FALSE;
773  } /* keyserver_dlg_proc */  }

Legend:
Removed from v.47  
changed lines
  Added in v.256

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26