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

Legend:
Removed from v.73  
changed lines
  Added in v.200

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26