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

Diff of /trunk/Src/WinPT.cpp

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

revision 16 by twoaday, Mon May 9 08:54:21 2005 UTC revision 32 by twoaday, Mon Oct 24 08:03:48 2005 UTC
# Line 33  Line 33 
33  #include "wptCard.h"  #include "wptCard.h"
34  #include "wptFileManager.h"  #include "wptFileManager.h"
35  #include "wptContext.h"  #include "wptContext.h"
36    #include "wptCardEdit.h"
37    
38  HINSTANCE glob_hinst; /* global instance for the dialogs */  
39  HWND glob_hwnd; /* global window handle for the dialogs */  #define MIN_GPG_VER   "1.4.3"   /* Minimal GPG version. */
40    #define MIN_GPGME_VER "1.2.0"   /* Minimal GPGME version. */
41    #define MIN_PTD_VER   "0.8.1"   /* Minimal PTD version. */
42    
43    
44    HINSTANCE glob_hinst;   /* global instance for the dialogs */
45    HWND glob_hwnd;         /* global window handle for the dialogs */
46  HWND activ_hwnd;  HWND activ_hwnd;
47  LOCK mo_file;  LOCK mo_file;
48  int scard_support = 0;  int scard_support = 0;
# Line 44  int mobile = 0; Line 51  int mobile = 0;
51  int gpg_read_only = 0;  int gpg_read_only = 0;
52  char gpgver[3];  char gpgver[3];
53    
 /* Internal IPC */  
 int start_keymanager = 0;  
54    
55    /* Load the key cache and rebuild the signature cache. */
56  static void  static void
57  update_keycache (HWND hwnd)  update_keycache (HWND hwnd)
58  {  {
# Line 56  update_keycache (HWND hwnd) Line 62  update_keycache (HWND hwnd)
62      rcs.tr_update = 1;      rcs.tr_update = 1;
63      DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_KEYCACHE, hwnd,      DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_KEYCACHE, hwnd,
64                      keycache_dlg_proc, (LPARAM)&rcs);                      keycache_dlg_proc, (LPARAM)&rcs);
65  } /* update_keycache */  }
66    
67    
68    /* Set GPGME debug mode. If @val is 0, the debug mode is disabled. */
69    void
70    gpg_set_debug_mode (int val)
71    {      
72        if (val)
73            putenv ("GPGME_DEBUG=5:gpgme.dbg");
74        else
75            putenv ("GPGME_DEBUG=");
76    }
77    
78    
79  static char *  /* Return the name of the gettext language file. */
80    static char*
81  get_gettext_lang (void)  get_gettext_lang (void)
82  {      {    
83      char * fname;      char *fname;
84      fname = get_reg_entry_mo ();      fname = get_reg_entry_mo ();
85      if (!fname)      if (!fname)
86          return NULL;          return NULL;
87      return fname;      return fname;
88  } /* get_gettext_lang */  }
89    
90    
91    /* Initialize the gettext sub system. */
92  static void  static void
93  load_gettext (void)  load_gettext (void)
94  {  {
95      char * nls = NULL;      char *nls = NULL;
96      char * file = NULL;      char *file = NULL;
97    
98      nls = get_gettext_lang ();      nls = get_gettext_lang ();
99      if (nls) {      if (nls) {
# Line 88  load_gettext (void) Line 107  load_gettext (void)
107          free_if_alloc (nls);          free_if_alloc (nls);
108          free_if_alloc (file);          free_if_alloc (file);
109      }      }
110  } /* load_gettext */  }
111    
112    
113  /* check if the default key from the gpg.conf file is available in the  /* check if the default key from the gpg.conf file is available in the
114     keyring. if not, bail out because encryption won't work properly then. */     keyring. if not, bail out because encryption won't work properly then. */
115  static int  static int
116  check_default_key (gpgme_keycache_t kc)  check_default_key (gpg_keycache_t kc)
117  {  {
118      gpgme_key_t key;      gpgme_key_t key;
119      gpgme_error_t err = GPGME_No_Error;      gpgme_error_t err = GPG_ERR_NO_ERROR;
120      char * defkey;      char * defkey;
121    
122      defkey = get_gnupg_default_key ();      defkey = get_gnupg_default_key ();
123      if (defkey)      if (defkey)
124          err = gpgme_keycache_find_key (kc, defkey, 0, &key);          err = gpg_keycache_find_key (kc, defkey, 0, &key);
125      free_if_alloc (defkey);      free_if_alloc (defkey);
126      return err? -1 : 0;      return err? -1 : 0;
127  } /* check_default_key */  }
128    
129    
130  /* Return the WinPT program file name (with full pathname). */  /* Return the WinPT program file name (with full pathname). */
131  static const char *  static const char *
132  get_prog_part (const char * fname, int use_cwd)  get_prog_part (const char * fname, int use_cwd)
133  {  {
134      static char program[1024];      static char program[512];
135      char currdir[256], * cmd = NULL;      char currdir[256];
136        char *cmd = NULL;
137      int j;      int j;
138                    
139      memset (currdir, 0, DIM (currdir));      memset (currdir, 0, DIM (currdir));
# Line 127  get_prog_part (const char * fname, int u Line 147  get_prog_part (const char * fname, int u
147          cmd = GetCommandLine ();          cmd = GetCommandLine ();
148          if (cmd == NULL)          if (cmd == NULL)
149              return NULL;              return NULL;
150          strncpy (currdir, cmd, 255);          strncpy (currdir, cmd, sizeof (currdir)-1);
151          j = strlen (currdir);          j = strlen (currdir);
152          while (j--) {          while (j--) {
153              if (currdir[j] == '\\')              if (currdir[j] == '\\')
154                  break;                            break;
155          }          }
156          currdir[j] = 0;          currdir[j] = 0;
157          _snprintf (program, DIM (program)-1, "%s\\%s", currdir + 1, fname);          _snprintf (program, DIM (program)-1, "%s\\%s", currdir + 1, fname);
158      }      }
159      return program;      return program;
160  } /* get_prog_part */  }
161    
162    
163    /* Check that the underlying crypto engine fullfills the minimal
164       requirements so all commands work properly. */
165  static int  static int
166  check_crypto_engine (void)  check_crypto_engine (void)
167  {  {
168      int ma=1, mi=2, pa=4; /* GPG 1.2.4 */      int ma=1, mi=4, pa=3; /* GPG 1.4.3 */
169      int rc;      int rc;
170    
171      rc = check_gnupg_engine (&ma, &mi, &pa);      rc = check_gnupg_engine (&ma, &mi, &pa);
172      if (rc == -1) {      if (rc == -1) {
173          msg_box (NULL, _("Could not read GnuPG version."), _("WinPT Error"), MB_ERR);          msg_box (NULL, _("Could not read GnuPG version."),
174                     _("WinPT Error"), MB_ERR);
175          return rc;          return rc;
176      }      }
177      else if (rc) {      else if (rc) {
178          log_box (_("WinPT Error"), MB_ERR,          log_box (_("WinPT Error"), MB_ERR,
179                   _("Sorry, you need a newer GPG version.\n"                   _("Sorry, you need a newer GPG version.\n"
180                     "GPG version %d.%d.%d requred GPG version 1.2.4"),                     "GPG version %d.%d.%d required GPG version "MIN_GPG_VER),
181                     ma, mi, pa);                     ma, mi, pa);
182          return rc;          return rc;
183      }      }
184      /* We enable smartcard support for GPG: 1.9.x or >= 1.4.0 */      /* We enable smartcard support for GPG: 1.9 or >= 1.4 */
185      if (ma >= 1 && mi >= 4)      if (ma >= 1 && mi >= 4)
186          scard_support = 1;          scard_support = 1;
187    
# Line 166  check_crypto_engine (void) Line 189  check_crypto_engine (void)
189      gpgver[1] = mi;      gpgver[1] = mi;
190      gpgver[2] = pa;      gpgver[2] = pa;
191      return rc;      return rc;
192  } /* check_crypto_engine */  }
193    
194    
195    /* Try to load the keyserver config file. If @quiet is 1
196       do not show any errors. */
197  static int  static int
198  load_keyserver_conf (int quiet)  load_keyserver_conf (int quiet)
199  {  {
# Line 188  load_keyserver_conf (int quiet) Line 213  load_keyserver_conf (int quiet)
213  }  }
214    
215    
216    /* Enable the mobility mode. */
217  static void  static void
218  enable_mobile_mode (void)  enable_mobile_mode (void)
219  {  {
# Line 204  enable_mobile_mode (void) Line 230  enable_mobile_mode (void)
230      reg_prefs.use_viewer = 0; /* XXX */      reg_prefs.use_viewer = 0; /* XXX */
231  }  }
232    
 char* get_subkey_fingerprint (gpgme_ctx_t ctx, const char *keyid);  
233    
234    /* Main entry point. */
235  int WINAPI  int WINAPI
 #ifndef WINPT_IPC  
236  WinMain (HINSTANCE hinst, HINSTANCE hprev, LPSTR cmdline, int showcmd)  WinMain (HINSTANCE hinst, HINSTANCE hprev, LPSTR cmdline, int showcmd)
 #else  
 win_main (HINSTANCE hinst, HINSTANCE hprev, LPSTR cmdline, int showcmd)  
 #endif  
237  {  {
238      WNDCLASS wc = {0, winpt_main_proc, 0, 0, hinst, 0, 0, 0, 0, PGM_NAME};      WNDCLASS wc = {0, winpt_main_proc, 0, 0, hinst, 0, 0, 0, 0, PGM_NAME};
239      HACCEL accel_tab;      HACCEL accel_tab;
# Line 219  win_main (HINSTANCE hinst, HINSTANCE hpr Line 241  win_main (HINSTANCE hinst, HINSTANCE hpr
241      int first_start = 0, start_gpgprefs = 0;      int first_start = 0, start_gpgprefs = 0;
242      const char * s;      const char * s;
243      MSG msg;      MSG msg;
244      HWND hwnd;      HWND hwnd = NULL;
245    
246      glob_hinst = hinst;      glob_hinst = hinst;
247        
     gpgme_lib_init ();  
248      #ifdef _DEBUG      #ifdef _DEBUG
249      gpgme_set_debug_mode (1);      gpg_set_debug_mode (1);
250        debug = 1;
251      #endif      #endif
     gpgme_set_pgm_string ("WinPT "PGM_VERSION);  
252    
253      s = PTD_get_version ();      s = PTD_get_version ();
254      if (strcmp (s, "0.8.0")) {      if (strcmp (s, MIN_PTD_VER)) {
255          log_box (_("Privacy Tray Dynamic (PTD)"), MB_ERR,          log_box (_("Privacy Tray Dynamic (PTD)"), MB_ERR,
256                   _("Please update your PTD.dll to the newest version, "                   _("Please update your PTD.dll to the newest version, "
257                     "the version (%s) you use is too old."), s);                     "the version (%s) you use is too old."), s);
# Line 243  win_main (HINSTANCE hinst, HINSTANCE hpr Line 264  win_main (HINSTANCE hinst, HINSTANCE hpr
264          return 0;          return 0;
265      }      }
266    
267        s = gpgme_check_version (MIN_GPGME_VER);
268        if (!s || !*s) {
269            msg_box (NULL, _("A newer GPGME version is needed; at least "MIN_GPGME_VER),
270                     _("WinPT Error"), MB_ERR);
271            return 0;
272        }
273    
274      if (cmdline && stristr (cmdline, "--mobile")) {      if (cmdline && stristr (cmdline, "--mobile")) {
275          msg_box (NULL, "WARNING: mobile modus is not fully implemented yet!", "WinPT", MB_INFO);          msg_box (NULL, "WARNING: mobile modus is not fully implemented yet!",
276                     "WinPT", MB_INFO);
277          mobile = 1;          mobile = 1;
278      }      }
279    
# Line 275  win_main (HINSTANCE hinst, HINSTANCE hpr Line 304  win_main (HINSTANCE hinst, HINSTANCE hpr
304                   _("GPG home directory is not set correctly.\n"                   _("GPG home directory is not set correctly.\n"
305                     "Please check the GPG registry settings:\n%s."),                     "Please check the GPG registry settings:\n%s."),
306                   winpt_strerror (rc));                   winpt_strerror (rc));
307          const char * s = get_filename_dlg (GetActiveWindow (), FILE_OPEN,          const char * s = get_fileopen_dlg (GetActiveWindow (),
308                                             _("Select GPG Public Keyring"),                                             _("Select GPG Public Keyring"),
309                                             _("GPG Keyrings (*.gpg)\0*.gpg\0\0"),                                             _("GPG Keyrings (*.gpg)\0*.gpg\0\0"),
310                                             NULL);                                             NULL);
311          if (s && !file_exist_check (s))          if (s != NULL) {
         {  
312              size_t n;              size_t n;
313              char * p = strrchr (s, '\\');              char * p = strrchr (s, '\\');
314              if (!p)              if (!p)
# Line 321  win_main (HINSTANCE hinst, HINSTANCE hpr Line 349  win_main (HINSTANCE hinst, HINSTANCE hpr
349      }      }
350    
351      rc = gnupg_access_files ();      rc = gnupg_access_files ();
352      if (!start_gpgprefs && rc)      if (!start_gpgprefs && rc) {
353      {          if (rc == WPTERR_GPG_KEYRINGS || rc == WPTERR_GPG_OPT_KEYRINGS) {
         if (rc == WPTERR_GPG_KEYRINGS || rc == WPTERR_GPG_OPT_KEYRINGS)  
         {  
354              ec = msg_box (NULL,              ec = msg_box (NULL,
355                  _("Could not access and/or find the public and secret keyring.\n"                  _("Could not access and/or find the public and secret keyring.\n"
356                    "If this is an accident, quit the program and fix it.\n\n"                    "If this is an accident, quit the program and fix it.\n\n"
# Line 333  win_main (HINSTANCE hinst, HINSTANCE hpr Line 359  win_main (HINSTANCE hinst, HINSTANCE hpr
359              if (ec == IDYES)              if (ec == IDYES)
360                  first_start = 1;                  first_start = 1;
361          }          }
362          if (!first_start)          if (!first_start) {
         {  
363              msg_box (NULL, winpt_strerror (rc), _("WinPT Error"), MB_ERR);              msg_box (NULL, winpt_strerror (rc), _("WinPT Error"), MB_ERR);
364              return 0;              return 0;
365          }          }
366      }      }
367    
368      if (!first_start)      if (!first_start) {
     {  
369          rc = gpg_check_permissions (1);          rc = gpg_check_permissions (1);
370          if (rc && rc == 2)          if (rc && rc == 2)
371              gpg_read_only = 1;              gpg_read_only = 1;
# Line 353  win_main (HINSTANCE hinst, HINSTANCE hpr Line 377  win_main (HINSTANCE hinst, HINSTANCE hpr
377      init_gnupg_table ();      init_gnupg_table ();
378    
379      nfiles = fm_parse_command_line (cmdline);      nfiles = fm_parse_command_line (cmdline);
380      if (nfiles > 0)      if (nfiles > 0) {
381            free_gnupg_table ();
382          return 0;          return 0;
383        }
384    
385      if (cmdline && stristr (cmdline, "--wipe-freespace")) {      if (cmdline && stristr (cmdline, "--wipe-freespace")) {
386          dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_SPACE_SECDEL,          dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_SPACE_SECDEL,
# Line 365  win_main (HINSTANCE hinst, HINSTANCE hpr Line 391  win_main (HINSTANCE hinst, HINSTANCE hpr
391      }      }
392    
393      load_keyserver_conf (cmdline? 1 : 0);      load_keyserver_conf (cmdline? 1 : 0);
     if (start_keymanager) {  
         dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_KEYMISC,  
                             GetDesktopWindow(), keymanager_dlg_proc, NULL,  
                             _("Key Manager"), IDS_WINPT_KEYMISC);  
         keycache_release ();  
         free_gnupg_table ();  
         return 0;  
     }  
394    
395      if (cmdline && (stristr (cmdline, "--keymanager")      if (cmdline && (stristr (cmdline, "--keymanager")
396                  || stristr (cmdline, "--cardmanager"))) {                  || stristr (cmdline, "--cardmanager"))) {
# Line 382  win_main (HINSTANCE hinst, HINSTANCE hpr Line 400  win_main (HINSTANCE hinst, HINSTANCE hpr
400                              GetDesktopWindow(), keymanager_dlg_proc, NULL,                              GetDesktopWindow(), keymanager_dlg_proc, NULL,
401                              _("Key Manager"), IDS_WINPT_KEYMISC);                                _("Key Manager"), IDS_WINPT_KEYMISC);  
402          else {          else {
403              gpgme_card_t crd = smartcard_init ();              gpg_card_t crd = gpg_card_load ();
404              if (crd)              if (crd)
405                  dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_CARD_EDIT,                  dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_CARD_EDIT,
406                                    GetDesktopWindow(), card_edit_dlg_proc,                                    GetDesktopWindow(), card_edit_dlg_proc,
407                                    (LPARAM)crd, _("Card Manager"),                                    (LPARAM)crd, _("Card Manager"),
408                                    IDS_WINPT_CARD_EDIT);                                    IDS_WINPT_CARD_EDIT);
409              gpgme_card_release (crd);              gpg_card_release (crd);
410          }          }
411          keycache_release ();          keycache_release (0);
412          free_gnupg_table ();          free_gnupg_table ();
413          return 0;          return 0;
414      }      }
# Line 403  win_main (HINSTANCE hinst, HINSTANCE hpr Line 421  win_main (HINSTANCE hinst, HINSTANCE hpr
421    
422      if (cmdline) {      if (cmdline) {
423          if (stristr (cmdline, "--enable-debug") || stristr (cmdline, "--debug")) {          if (stristr (cmdline, "--enable-debug") || stristr (cmdline, "--debug")) {
424              gpgme_set_debug_mode (1);              gpg_set_debug_mode (1);
425              winpt_debug_msg ();              winpt_debug_msg ();
426              debug = 1;              debug = 1;
427          }          }
# Line 412  win_main (HINSTANCE hinst, HINSTANCE hpr Line 430  win_main (HINSTANCE hinst, HINSTANCE hpr
430      wc.hIcon = LoadIcon (glob_hinst, MAKEINTRESOURCE (IDI_WINPT));      wc.hIcon = LoadIcon (glob_hinst, MAKEINTRESOURCE (IDI_WINPT));
431      rc = RegisterClass (&wc);      rc = RegisterClass (&wc);
432      if (rc == FALSE) {      if (rc == FALSE) {
433          msg_box (NULL, _("Could not register window class"), _("WinPT Error"), MB_ERR);          msg_box (NULL, _("Could not register window class"),
434                     _("WinPT Error"), MB_ERR);
435          free_gnupg_table ();          free_gnupg_table ();
436          return 0;          return 0;
437      }      }
# Line 433  win_main (HINSTANCE hinst, HINSTANCE hpr Line 452  win_main (HINSTANCE hinst, HINSTANCE hpr
452      UpdateWindow (hwnd);      UpdateWindow (hwnd);
453    
454      if (!first_start && !start_gpgprefs) {      if (!first_start && !start_gpgprefs) {
455          gnupg_backup_options (1);          gnupg_backup_options ();        
         gnupg_backup_options (0);  
           
456          rc = check_crypto_engine ();          rc = check_crypto_engine ();
457          if (rc) {          if (rc) {
458              DestroyWindow (hwnd);              DestroyWindow (hwnd);
# Line 443  win_main (HINSTANCE hinst, HINSTANCE hpr Line 460  win_main (HINSTANCE hinst, HINSTANCE hpr
460              return 0;              return 0;
461          }          }
462      }      }
463        
464      if (start_gpgprefs)      if (start_gpgprefs) {
     {  
465          char *ring;          char *ring;
466          size_t size = 0;          size_t size = 0;
467          DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_GPGPREFS, hwnd,          DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_GPGPREFS, hwnd,
# Line 457  win_main (HINSTANCE hinst, HINSTANCE hpr Line 473  win_main (HINSTANCE hinst, HINSTANCE hpr
473      }      }
474    
475      if (first_start) {      if (first_start) {
476          struct key_wizard_s c, dummy;          struct first_start_s fs;
477            struct genkey_s c;
478            HWND h;
479  start:  start:
480          DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_FIRST, hwnd,          h = GetDesktopWindow ();
481                          first_run_dlg_proc, (LPARAM)&dummy);          DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_GPGPREFS, h,
482          switch (dummy.interactive)                              gpgprefs_dlg_proc, NULL);
483          {          DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_FIRST, h,
484                            first_run_dlg_proc, (LPARAM)&fs);
485            switch (fs.choice) {
486          case SETUP_KEYGEN:          case SETUP_KEYGEN:
487              c.interactive = 1;              c.interactive = 1;
488                c.first_start = 1;
489              rc = DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_KEYWIZARD,              rc = DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_KEYWIZARD,
490                                   hwnd, keygen_wizard_dlg_proc, (LPARAM)&c);                                   h, keygen_wizard_dlg_proc, (LPARAM)&c);
491              if (!rc)              if (!rc)
492                  goto start;                  goto start;
493              break;              break;
# Line 479  start: Line 500  start:
500              }              }
501              break;              break;
502    
         case SETUP_EXISTING:  
             DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_GPGPREFS, hwnd,  
                             gpgprefs_dlg_proc, NULL);  
             break;  
   
503          case -1:          case -1:
504              DestroyWindow (hwnd);              DestroyWindow (hwnd);
505              free_gnupg_table ();              free_gnupg_table ();
# Line 493  start: Line 509  start:
509          check_crypto_engine ();          check_crypto_engine ();
510      }      }
511      else {      else {
512          gpgme_keycache_t c;          gpg_keycache_t c;
513          update_keycache (hwnd);          update_keycache (hwnd);
514          c = keycache_get_ctx (1);          c = keycache_get_ctx (1);
515          if (!c || !gpgme_keycache_count (c)) {          if (!c || !gpg_keycache_get_size (c)) {
516              gnupg_display_error ();              gnupg_display_error ();
517              msg_box (hwnd, _("The keycache was not initialized or is empty.\n"              msg_box (hwnd, _("The keycache was not initialized or is empty.\n"
518                               "Please check your GPG config (keyrings, pathes...)"),                               "Please check your GPG config (keyrings, pathes...)"),
# Line 541  start: Line 557  start:
557      }      }
558                    
559      return 0;      return 0;
560  } /* WinMain */  }

Legend:
Removed from v.16  
changed lines
  Added in v.32

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26