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

Legend:
Removed from v.42  
changed lines
  Added in v.248

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26