/[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 87 by twoaday, Mon Nov 21 11:44:25 2005 UTC revision 137 by twoaday, Mon Jan 9 14:01:51 2006 UTC
# Line 1  Line 1 
1  /* WinPT.cpp - Windows Privacy Tray (WinPT)  /* WinPT.cpp - Windows Privacy Tray (WinPT)
2   *      Copyright (C) 2000-2005 Timo Schulz   *      Copyright (C) 2000-2006 Timo Schulz
3   *   *
4   * This file is part of WinPT.   * This file is part of WinPT.
5   *   *
# Line 40  Line 40 
40  #include "wptCardEdit.h"  #include "wptCardEdit.h"
41  #include "wptCrypto.h"  #include "wptCrypto.h"
42    
 #define MIN_GPG_VER   "1.4.2"   /* Minimal GPG version. */  
 #define MIN_GPGME_VER "1.2.0"   /* Minimal GPGME version. */  
   
   
43  HINSTANCE glob_hinst;   /* global instance for the dialogs */  HINSTANCE glob_hinst;   /* global instance for the dialogs */
44  HWND glob_hwnd;         /* global window handle for the dialogs */  HWND glob_hwnd;         /* global window handle for the dialogs */
45  HWND activ_hwnd;  HWND activ_hwnd;
 LOCK mo_file;  
46  int scard_support = 0;  int scard_support = 0;
47  int debug = 0;  int debug = 0;
48  int mobile = 0;  int mobile = 0;
# Line 96  static void Line 91  static void
91  load_gettext (int prev_inst)  load_gettext (int prev_inst)
92  {  {
93      char *nls = NULL;      char *nls = NULL;
     char *file = NULL;  
94    
95      nls = get_gettext_lang ();      nls = get_gettext_lang ();
96      if (nls) {      if (nls != NULL) {
97          set_gettext_file ("winpt", nls);          set_gettext_file ("winpt", nls);
         file = make_filename (nls, "winpt", "mo");  
         if (!file_exist_check (nls) && init_file_lock (&mo_file, file))  {  
             if (!prev_inst)  
                 msg_box (NULL, _("Could not initizalize file lock.\n"  
                                  "Native Language Support"),  
                          _("WinPT Error"), MB_ERR);  
         }  
98          free_if_alloc (nls);          free_if_alloc (nls);
         free_if_alloc (file);  
99      }      }
100  }  }
101    
102    
103    /* Load the GPG environment. On the first start, some
104       checks are performed to find out in what state GPG is.
105       Return value: 0  everything OK.
106                     >0  fatal error.
107                     -1 public keyring is empty or does not exist. */
108    static int
109    load_gpg_env (void)
110    {
111        SECURITY_ATTRIBUTES sec_attr;
112        char *p;
113        char *pkr;
114    
115        p = get_reg_entry_gpg4win ("gpg.exe");
116        if (!p)
117            return (1);
118        if (file_exist_check (p)) {
119            free_if_alloc (p);
120            return (1);
121        }
122        free_if_alloc (p);
123        p = multi_gnupg_path (0);
124        if (p && dir_exist_check (p)) {
125            memset (&sec_attr, 0, sizeof (sec_attr));
126            sec_attr.nLength = sizeof (sec_attr);
127            if (!CreateDirectory (p, &sec_attr)) {
128                msg_box (NULL, _("Could not create GPG home directory"),
129                         _("WinPT Error"), MB_ERR);
130                free_if_alloc (p);
131                return (2);
132            }
133        }
134        pkr = make_filename (p, "pubring", "gpg");
135        free_if_alloc (p);
136        if (!pkr)
137            return -1;
138        if (get_file_size (pkr) == 0) {
139            free_if_alloc (pkr);
140            return -1;
141        }
142        return 0;
143    }
144    
145    
146  /* 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
147     keyring. if not, bail out because encryption won't work properly then. */     keyring. if not, bail out because encryption won't work properly then. */
148  static int  static int
# Line 121  check_default_key (gpg_keycache_t kc) Line 150  check_default_key (gpg_keycache_t kc)
150  {  {
151      gpgme_key_t key;      gpgme_key_t key;
152      gpgme_error_t err = GPG_ERR_NO_ERROR;      gpgme_error_t err = GPG_ERR_NO_ERROR;
153      char * defkey;      char *defkey;
154    
155      defkey = get_gnupg_default_key ();      defkey = get_gnupg_default_key ();
156      if (defkey)      if (defkey)
157          err = gpg_keycache_find_key (kc, defkey, 0, &key);          err = gpg_keycache_find_key (kc, defkey, 0, &key);
158      else      else
159          msg_box (NULL, _("No useable secret key found."), _("WinPT Error"), MB_ERR);          msg_box (NULL, _("No useable secret key found."),
160                     _("WinPT Error"), MB_ERR);
161      free_if_alloc (defkey);      free_if_alloc (defkey);
162      return err? -1 : 0;      return err? -1 : 0;
163  }  }
164    
165    
166  /* Return the WinPT program file name (with full pathname). */  /* Return the WinPT program file name (with full pathname). */
167  static const char *  static const char*
168  get_prog_part (const char * fname, int use_cwd)  get_prog_part (const char * fname, int use_cwd)
169  {  {
170      static char program[512];      static char program[512];
# Line 168  get_prog_part (const char * fname, int u Line 198  get_prog_part (const char * fname, int u
198    
199  /* Check that the underlying crypto engine fullfills the minimal  /* Check that the underlying crypto engine fullfills the minimal
200     requirements so all commands work properly. */     requirements so all commands work properly. */
201  static int  static bool
202  check_crypto_engine (void)  check_crypto_engine (void)
203  {  {
204      int ma=1, mi=4, pa=2; /* GPG 1.4.2 */      int ma=0, mi=0, pa=0;
205      int rc;      int rc;
206    
207      rc = check_gnupg_engine (&ma, &mi, &pa);      rc = check_gnupg_engine (NEED_GPG_VERSION, &ma, &mi, &pa);
208      if (rc == -1) {      if (rc == -1) {
209          msg_box (NULL, _("Could not read GnuPG version."),          msg_box (NULL, _("Could not read GnuPG version."),
210                   _("WinPT Error"), MB_ERR);                   _("WinPT Error"), MB_ERR);
211          return rc;          return false;
212      }      }
213      else if (rc) {      else if (rc) {
214          log_box (_("WinPT Error"), MB_ERR,          log_box (_("WinPT Error"), MB_ERR,
215                   _("Sorry, you need a newer GPG version.\n"                   _("Sorry, you need a newer GPG version.\n"
216                     "GPG version %d.%d.%d required GPG version "MIN_GPG_VER),                     "GPG version %d.%d.%d required GPG version "NEED_GPG_VERSION),
217                     ma, mi, pa);                     ma, mi, pa);
218          return rc;          return false;
219      }      }
220      /* We enable smartcard support for GPG: >= 2 or >= 1.4.3 */      /* We enable smartcard support for GPG: >= 2 or >= 1.4.3 */
221      if (ma > 1 || pa >= 3)      if (ma > 1 || pa >= 3)
# Line 194  check_crypto_engine (void) Line 224  check_crypto_engine (void)
224      gpgver[0] = ma;      gpgver[0] = ma;
225      gpgver[1] = mi;      gpgver[1] = mi;
226      gpgver[2] = pa;      gpgver[2] = pa;
227      return rc;      return true;
228  }  }
229    
230    
# Line 203  check_crypto_engine (void) Line 233  check_crypto_engine (void)
233  static int  static int
234  load_keyserver_conf (int quiet)  load_keyserver_conf (int quiet)
235  {  {
236      const char * t;      const char *t;
237      int rc;      int rc;
238    
239      if (reg_prefs.kserv_conf)      if (reg_prefs.kserv_conf)
# Line 219  load_keyserver_conf (int quiet) Line 249  load_keyserver_conf (int quiet)
249  }  }
250    
251    
252    /* Check if both keyrings are empty. This indicates that
253       WinPT should offer to generate a key pair. */
254    static bool
255    check_for_empty_keyrings (bool pub_only)
256    {
257        char *p;
258        int n = 0;
259    
260        p = get_gnupg_keyring (1, 0);
261        if (file_exist_check (p) == 0 && get_file_size (p) == 0)
262            n++;
263        free_if_alloc (p);
264        if (pub_only)
265            return n == 1? true : false;
266        p = get_gnupg_keyring (0, 0);
267        if (file_exist_check (p) == 0 && get_file_size (p) == 0)
268            n++;
269        free_if_alloc (p);
270        return n==2? true : false;
271    }
272    
273    
274  /* Enable the mobility mode. */  /* Enable the mobility mode. */
275  static void  static void
276  enable_mobile_mode (void)  enable_mobile_mode (void)
# Line 236  enable_mobile_mode (void) Line 288  enable_mobile_mode (void)
288      reg_prefs.use_viewer = 0; /* XXX */      reg_prefs.use_viewer = 0; /* XXX */
289  }  }
290    
 char* multi_gnupg_path (void);  
   
 const char * fm_get_file_type (const char *fname, int *r_type);  
291    
292  /* Main entry point. */  /* Main entry point. */
293  int WINAPI  int WINAPI
# Line 246  WinMain (HINSTANCE hinst, HINSTANCE hpre Line 295  WinMain (HINSTANCE hinst, HINSTANCE hpre
295  {  {
296      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};
297      HACCEL accel_tab;      HACCEL accel_tab;
298      int rc, ec, created = 0, nfiles = 0;      MSG msg;
299        HWND hwnd = NULL;
300        WORD ver[3], ptdver[4];
301        int rc, ec, created = 0;
302      int first_start = 0, start_gpgprefs = 0;      int first_start = 0, start_gpgprefs = 0;
303      int winpt_inst_found = 0;      int winpt_inst_found = 0;
304        int start_manager = 0;
305      const char *s;      const char *s;
     MSG msg;  
     HWND hwnd = NULL;  
306    
307      glob_hinst = hinst;      glob_hinst = hinst;
   
308      if (cmdline && stristr (cmdline, "--stop")) {      if (cmdline && stristr (cmdline, "--stop")) {
309          hwnd = FindWindow ("WinPT", "WinPT");          hwnd = FindWindow ("WinPT", "WinPT");
310          if (hwnd != NULL)          if (hwnd != NULL)
311              PostMessage (hwnd, WM_DESTROY, 0, 0);              PostMessage (hwnd, WM_DESTROY, 0, 0);
312          return 0;          return 0;
313      }      }
314        
315  #ifdef _DEBUG      /*
316        OSVERSIONINFO osinf;
317        memset (&osinf, 0, sizeof (osinf));
318        if (GetVersionEx (&osinf) &&
319            osinf.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS &&
320            osinf.dwMinorVersion == 0) {
321            msg_box (NULL, "WinPT propably does not work on Windows 95 without restrictions",
322                     "WinPT Warning", MB_INFO);
323        }
324        */
325    
326        #ifdef _DEBUG
327      gpg_set_debug_mode (1);      gpg_set_debug_mode (1);
328      debug = 1;      debug = 1;
329  #endif      #endif
330    
331        get_file_version ("WinPT.exe", &ver[0], &ver[1], &ver[2], &ver[3]);
332        get_file_version ("PTD.dll", &ptdver[0], &ptdver[1],
333                                     &ptdver[2], &ptdver[3]);
334        /* XXX
335        if (ptdver[0] != ver[0] || ptdver[1] != ver[1]|| ptdver[2] != ver[2]) {
336            log_box (_("WinPT Error"), MB_ERR,
337                     _("The PTD.dll file has a different version than WinPT.exe\n"
338                       "Please update the PTD.dll to version %d.%d.%d"),
339                       ver[0], ver[1], ver[2]);
340            return 0;
341        }
342        */
343    
344      if (gpg_md_selftest ()) {      if (gpg_md_selftest ()) {
345          msg_box (NULL, _("Cryptographic selftest failed."),          msg_box (NULL, _("Cryptographic selftest failed."),
# Line 273  WinMain (HINSTANCE hinst, HINSTANCE hpre Line 347  WinMain (HINSTANCE hinst, HINSTANCE hpre
347          return 0;          return 0;
348      }      }
349    
350      s = gpgme_check_version (MIN_GPGME_VER);      s = gpgme_check_version (NEED_GPGME_VERSION);
351      if (!s || !*s) {      if (!s || !*s) {
352          msg_box (NULL, _("A newer GPGME version is needed; at least "MIN_GPGME_VER),          msg_box (NULL, _("A newer GPGME version is needed; at least "NEED_GPGME_VERSION),
353                   _("WinPT Error"), MB_ERR);                   _("WinPT Error"), MB_ERR);
354          return 0;          return 0;
355      }      }
# Line 291  WinMain (HINSTANCE hinst, HINSTANCE hpre Line 365  WinMain (HINSTANCE hinst, HINSTANCE hpre
365      }      }
366    
367      set_default_kserver ();      set_default_kserver ();
368        load_gettext (winpt_inst_found);
369    
370      if (!mobile) {      if (!mobile) {
371          regist_inst_gnupg (1);          regist_inst_gnupg (1);
# Line 312  WinMain (HINSTANCE hinst, HINSTANCE hpre Line 387  WinMain (HINSTANCE hinst, HINSTANCE hpre
387          gnupg_load_config ();          gnupg_load_config ();
388      }      }
389    
390        if (is_gpg4win_installed ())
391            load_gpg_env (); /* XXX: check return code. */
392    
393      rc = gnupg_check_homedir ();      rc = gnupg_check_homedir ();
394      if (rc) {      if (rc) {
395          log_box (_("WinPT Error"), MB_ERR,          log_box (_("WinPT Error"), MB_ERR,
# Line 319  WinMain (HINSTANCE hinst, HINSTANCE hpre Line 397  WinMain (HINSTANCE hinst, HINSTANCE hpre
397                     "Please check the GPG registry settings:\n%s."),                     "Please check the GPG registry settings:\n%s."),
398                   winpt_strerror (rc));                   winpt_strerror (rc));
399          s = get_fileopen_dlg (GetActiveWindow (),          s = get_fileopen_dlg (GetActiveWindow (),
400                                _("Select GPG Public Keyring"),                                _("Select GPG Public Keyring"),
401                                _("GPG Keyrings (*.gpg)\0*.gpg\0\0"),                                _("GPG Keyrings (*.gpg)\0*.gpg\0\0"),
402                                NULL);                                NULL);
403          if (s != NULL) {          if (s != NULL) {
404              size_t n;              size_t n;
405              char * p = strrchr (s, '\\');              char *p = strrchr (s, '\\');
406              if (!p)              if (!p)
407                  BUG (0);                  BUG (0);
408              n = p - s;              n = p - s;
409              if (n) {              if (n) {
410                  char * file = new char[n+1];                  char *file = new char[n+1];
411                  if (!file)                  if (!file)
412                      BUG (NULL);                      BUG (NULL);
413                  memset (file, 0, n);                  memset (file, 0, n);
# Line 354  WinMain (HINSTANCE hinst, HINSTANCE hpre Line 432  WinMain (HINSTANCE hinst, HINSTANCE hpre
432                               "correct  this problem?"), _("WinPT Error"),                               "correct  this problem?"), _("WinPT Error"),
433                               MB_INFO|MB_YESNO) == IDYES)                               MB_INFO|MB_YESNO) == IDYES)
434              start_gpgprefs = 1;              start_gpgprefs = 1;
435          else          else {
         {  
436              msg_box (NULL, winpt_strerror (rc), _("WinPT Error"), MB_ERR);              msg_box (NULL, winpt_strerror (rc), _("WinPT Error"), MB_ERR);
437              return 0;              return 0;
438          }          }
# Line 377  WinMain (HINSTANCE hinst, HINSTANCE hpre Line 454  WinMain (HINSTANCE hinst, HINSTANCE hpre
454              return 0;              return 0;
455          }          }
456      }      }
457        if (check_for_empty_keyrings (false))
458            first_start = 1;
459    
460      if (!first_start) {      if (!first_start) {
461          rc = gpg_check_permissions (1);          rc = gpg_check_permissions (1);
# Line 385  WinMain (HINSTANCE hinst, HINSTANCE hpre Line 464  WinMain (HINSTANCE hinst, HINSTANCE hpre
464          else if (rc)          else if (rc)
465              return 0;              return 0;
466      }      }
467        
     load_gettext (winpt_inst_found);  
468      init_gnupg_table ();      init_gnupg_table ();
469    
470      nfiles = fm_parse_command_line (cmdline);      if (fm_parse_command_line (cmdline) > 0) {
     if (nfiles > 0) {  
471          free_gnupg_table ();          free_gnupg_table ();
472          return 0;          return 0;
473      }      }
# Line 407  WinMain (HINSTANCE hinst, HINSTANCE hpre Line 484  WinMain (HINSTANCE hinst, HINSTANCE hpre
484    
485      if (cmdline && (stristr (cmdline, "--keymanager")      if (cmdline && (stristr (cmdline, "--keymanager")
486                  || stristr (cmdline, "--cardmanager"))) {                  || stristr (cmdline, "--cardmanager"))) {
487          update_keycache (GetDesktopWindow ());          /* If an instance of WinPT is running, just send the command
488               to open the key manager. Otherwise start a new instance.
489             */
490            HWND tray = FindWindow ("WinPT", "WinPT");
491          if (stristr (cmdline, "keymanager"))          if (stristr (cmdline, "keymanager"))
492              dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_KEYMISC,              start_manager = ID_WINPT_KEY;
493                              GetDesktopWindow(), keymanager_dlg_proc, 0,          else
494                              _("Key Manager"), IDS_WINPT_KEYMISC);                start_manager = ID_WINPT_CARD;
495          else {          if (tray != NULL) {
496              gpg_card_t crd = gpg_card_load ();              PostMessage (tray, WM_COMMAND, start_manager, 0);
497              if (crd)              free_gnupg_table ();
498                  dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_CARD_EDIT,              return 0;
                                   GetDesktopWindow(), card_edit_dlg_proc,  
                                   (LPARAM)crd, _("Card Manager"),  
                                   IDS_WINPT_CARD_EDIT);  
             gpg_card_release (crd);  
499          }          }
         keycache_release (0);  
         free_gnupg_table ();  
         return 0;  
500      }      }
501    
502      /* If we found another WinPT instance, just quit to avoid it      /* If we found another WinPT instance, just quit to avoid it
# Line 435  WinMain (HINSTANCE hinst, HINSTANCE hpre Line 508  WinMain (HINSTANCE hinst, HINSTANCE hpre
508      }      }
509    
510      if (cmdline) {      if (cmdline) {
511          if (stristr (cmdline, "--enable-debug") || stristr (cmdline, "--debug")) {          if (stristr (cmdline, "--enable-debug") ||
512                stristr (cmdline, "--debug")) {
513              gpg_set_debug_mode (1);              gpg_set_debug_mode (1);
514              winpt_debug_msg ();              winpt_debug_msg ();
515              debug = 1;              debug = 1;
# Line 468  WinMain (HINSTANCE hinst, HINSTANCE hpre Line 542  WinMain (HINSTANCE hinst, HINSTANCE hpre
542    
543      if (!first_start && !start_gpgprefs) {      if (!first_start && !start_gpgprefs) {
544          gnupg_backup_options ();                  gnupg_backup_options ();        
545          rc = check_crypto_engine ();          if (!check_crypto_engine ()) {
         if (rc) {  
546              DestroyWindow (hwnd);              DestroyWindow (hwnd);
547              free_gnupg_table ();              free_gnupg_table ();
548              return 0;              return 0;
# Line 477  WinMain (HINSTANCE hinst, HINSTANCE hpre Line 550  WinMain (HINSTANCE hinst, HINSTANCE hpre
550      }      }
551            
552      if (start_gpgprefs) {      if (start_gpgprefs) {
         char *ring;  
   
553          DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_GPGPREFS, hwnd,          DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_GPGPREFS, hwnd,
554                          gpgprefs_dlg_proc, 0);                          gpgprefs_dlg_proc, 0);
555          ring = get_gnupg_keyring (0, !NO_STRICT);          if (check_for_empty_keyrings (true))
556          if (gnupg_access_keyring (0) == -1 && get_file_size (ring) == 0)              first_start = 1; /* The public keyring is empty! */
             first_start = 1; /* The keyring is empty! */  
         free_if_alloc (ring);  
557      }      }
558    
559      if (first_start) {      if (first_start) {
# Line 515  start: Line 584  start:
584              }              }
585              break;              break;
586    
587          case -1:          case -1: /* Cancel/Abort. */
588              DestroyWindow (hwnd);              DestroyWindow (hwnd);
589              free_gnupg_table ();              free_gnupg_table ();
590              return 0;              return 0;
# Line 547  start: Line 616  start:
616              }              }
617          }          }
618          if (check_default_key (c)) {          if (check_default_key (c)) {
619              char * p = get_gnupg_default_key ();              char *p = get_gnupg_default_key ();
620              log_box (_("WinPT Error"), MB_ERR,              log_box (_("WinPT Error"), MB_ERR,
621                       _("Default key from the GPG options file could not be found.\n"                       _("Default key from the GPG options file could not be found.\n"
622                         "Please check your gpg.conf (options) to correct this:\n\n"                         "Please check your gpg.conf (options) to correct this:\n\n"
# Line 562  start: Line 631  start:
631                              elgamal_warn_dlg_proc, 0);                              elgamal_warn_dlg_proc, 0);
632      }      }
633    
634        if (start_manager)
635            PostMessage (hwnd, WM_COMMAND, start_manager, 0);
636    
637      accel_tab = LoadAccelerators (glob_hinst, (LPCTSTR)IDR_WINPT_ACCELERATOR);      accel_tab = LoadAccelerators (glob_hinst, (LPCTSTR)IDR_WINPT_ACCELERATOR);
638      keyring_check_last_access (); /* init */      keyring_check_last_access (); /* init */
639      while (GetMessage (&msg, hwnd, 0, 0)) {      while (GetMessage (&msg, hwnd, 0, 0)) {

Legend:
Removed from v.87  
changed lines
  Added in v.137

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26