/[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 59 by twoaday, Wed Nov 2 13:50:48 2005 UTC revision 133 by twoaday, Mon Jan 9 09:15:29 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    
43  #define MIN_GPG_VER   "1.4.3"   /* Minimal GPG version. */  #define MIN_GPG_VER   "1.4.2"   /* Minimal GPG version. */
44  #define MIN_GPGME_VER "1.2.0"   /* Minimal GPGME version. */  #define MIN_GPGME_VER "1.2.0"   /* Minimal GPGME version. */
 #define MIN_PTD_VER   "0.8.1"   /* Minimal PTD version. */  
45    
46    
47  HINSTANCE glob_hinst;   /* global instance for the dialogs */  HINSTANCE glob_hinst;   /* global instance for the dialogs */
# Line 115  load_gettext (int prev_inst) Line 114  load_gettext (int prev_inst)
114  }  }
115    
116    
117    /* Load the GPG environment. On the first start, some
118       checks are performed to find out in what state GPG is.
119       Return value: 0  everything OK.
120                     >0  fatal error.
121                     -1 public keyring is empty or does not exist. */
122    static int
123    load_gpg_env (void)
124    {
125        SECURITY_ATTRIBUTES sec_attr;
126        char *p;
127        char *pkr;
128    
129        p = get_reg_entry_gpg4win ("gpg.exe");
130        if (!p)
131            return (1);
132        if (file_exist_check (p)) {
133            free_if_alloc (p);
134            return (1);
135        }
136        free_if_alloc (p);
137        p = multi_gnupg_path (0);
138        if (p && dir_exist_check (p)) {
139            memset (&sec_attr, 0, sizeof (sec_attr));
140            sec_attr.nLength = sizeof (sec_attr);
141            if (!CreateDirectory (p, &sec_attr)) {
142                msg_box (NULL, _("Could not create GPG home directory"),
143                         _("WinPT Error"), MB_ERR);
144                free_if_alloc (p);
145                return (2);
146            }
147        }
148        pkr = make_filename (p, "pubring", "gpg");
149        free_if_alloc (p);
150        if (!pkr)
151            return -1;
152        if (get_file_size (pkr) == 0) {
153            free_if_alloc (pkr);
154            return -1;
155        }
156        return 0;
157    }
158    
159    
160  /* 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
161     keyring. if not, bail out because encryption won't work properly then. */     keyring. if not, bail out because encryption won't work properly then. */
162  static int  static int
# Line 122  check_default_key (gpg_keycache_t kc) Line 164  check_default_key (gpg_keycache_t kc)
164  {  {
165      gpgme_key_t key;      gpgme_key_t key;
166      gpgme_error_t err = GPG_ERR_NO_ERROR;      gpgme_error_t err = GPG_ERR_NO_ERROR;
167      char * defkey;      char *defkey;
168    
169      defkey = get_gnupg_default_key ();      defkey = get_gnupg_default_key ();
170      if (defkey)      if (defkey)
171          err = gpg_keycache_find_key (kc, defkey, 0, &key);          err = gpg_keycache_find_key (kc, defkey, 0, &key);
172        else
173            msg_box (NULL, _("No useable secret key found."),
174                     _("WinPT Error"), MB_ERR);
175      free_if_alloc (defkey);      free_if_alloc (defkey);
176      return err? -1 : 0;      return err? -1 : 0;
177  }  }
178    
179    
180  /* Return the WinPT program file name (with full pathname). */  /* Return the WinPT program file name (with full pathname). */
181  static const char *  static const char*
182  get_prog_part (const char * fname, int use_cwd)  get_prog_part (const char * fname, int use_cwd)
183  {  {
184      static char program[512];      static char program[512];
# Line 167  get_prog_part (const char * fname, int u Line 212  get_prog_part (const char * fname, int u
212    
213  /* Check that the underlying crypto engine fullfills the minimal  /* Check that the underlying crypto engine fullfills the minimal
214     requirements so all commands work properly. */     requirements so all commands work properly. */
215  static int  static bool
216  check_crypto_engine (void)  check_crypto_engine (void)
217  {  {
218      int ma=1, mi=4, pa=3; /* GPG 1.4.3 */      int ma=1, mi=4, pa=2; /* GPG 1.4.2 */
219      int rc;      int rc;
220    
221      rc = check_gnupg_engine (&ma, &mi, &pa);      rc = check_gnupg_engine (&ma, &mi, &pa);
222      if (rc == -1) {      if (rc == -1) {
223          msg_box (NULL, _("Could not read GnuPG version."),          msg_box (NULL, _("Could not read GnuPG version."),
224                   _("WinPT Error"), MB_ERR);                   _("WinPT Error"), MB_ERR);
225          return rc;          return false;
226      }      }
227      else if (rc) {      else if (rc) {
228          log_box (_("WinPT Error"), MB_ERR,          log_box (_("WinPT Error"), MB_ERR,
229                   _("Sorry, you need a newer GPG version.\n"                   _("Sorry, you need a newer GPG version.\n"
230                     "GPG version %d.%d.%d required GPG version "MIN_GPG_VER),                     "GPG version %d.%d.%d required GPG version "MIN_GPG_VER),
231                     ma, mi, pa);                     ma, mi, pa);
232          return rc;          return false;
233      }      }
234      /* We enable smartcard support for GPG: 1.9 or >= 1.4 */      /* We enable smartcard support for GPG: >= 2 or >= 1.4.3 */
235      if ((ma == 1 && mi >= 4) || ma > 1)      if (ma > 1 || pa >= 3)    
236          scard_support = 1;          scard_support = 1;
237    
238      gpgver[0] = ma;      gpgver[0] = ma;
239      gpgver[1] = mi;      gpgver[1] = mi;
240      gpgver[2] = pa;      gpgver[2] = pa;
241      return rc;      return true;
242  }  }
243    
244    
# Line 202  check_crypto_engine (void) Line 247  check_crypto_engine (void)
247  static int  static int
248  load_keyserver_conf (int quiet)  load_keyserver_conf (int quiet)
249  {  {
250      const char * t;      const char *t;
251      int rc;      int rc;
252    
253      if (reg_prefs.kserv_conf)      if (reg_prefs.kserv_conf)
# Line 218  load_keyserver_conf (int quiet) Line 263  load_keyserver_conf (int quiet)
263  }  }
264    
265    
266    /* Check if both keyrings are empty. This indicates that
267       WinPT should offer to generate a key pair. */
268    static bool
269    check_for_empty_keyrings (bool pub_only)
270    {
271        char *p;
272        int n = 0;
273    
274        p = get_gnupg_keyring (1, 0);
275        if (file_exist_check (p) == 0 && get_file_size (p) == 0)
276            n++;
277        free_if_alloc (p);
278        if (pub_only)
279            return n == 1? true : false;
280        p = get_gnupg_keyring (0, 0);
281        if (file_exist_check (p) == 0 && get_file_size (p) == 0)
282            n++;
283        free_if_alloc (p);
284        return n==2? true : false;
285    }
286    
287    
288  /* Enable the mobility mode. */  /* Enable the mobility mode. */
289  static void  static void
290  enable_mobile_mode (void)  enable_mobile_mode (void)
# Line 235  enable_mobile_mode (void) Line 302  enable_mobile_mode (void)
302      reg_prefs.use_viewer = 0; /* XXX */      reg_prefs.use_viewer = 0; /* XXX */
303  }  }
304    
 char* multi_gnupg_path (void);  
   
 const char * fm_get_file_type (const char *fname, int *r_type);  
305    
306  /* Main entry point. */  /* Main entry point. */
307  int WINAPI  int WINAPI
# Line 245  WinMain (HINSTANCE hinst, HINSTANCE hpre Line 309  WinMain (HINSTANCE hinst, HINSTANCE hpre
309  {  {
310      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};
311      HACCEL accel_tab;      HACCEL accel_tab;
312      int rc, ec, created = 0, nfiles = 0;      MSG msg;
313        HWND hwnd = NULL;
314        WORD ver[3], ptdver[4];
315        int rc, ec, created = 0;
316      int first_start = 0, start_gpgprefs = 0;      int first_start = 0, start_gpgprefs = 0;
317      int winpt_inst_found = 0;      int winpt_inst_found = 0;
318        int start_manager = 0;
319      const char *s;      const char *s;
     MSG msg;  
     HWND hwnd = NULL;  
320    
321      glob_hinst = hinst;      glob_hinst = hinst;
322        if (cmdline && stristr (cmdline, "--stop")) {
323            hwnd = FindWindow ("WinPT", "WinPT");
324            if (hwnd != NULL)
325                PostMessage (hwnd, WM_DESTROY, 0, 0);
326            return 0;
327        }
328    
329  #ifdef _DEBUG      /*
330        OSVERSIONINFO osinf;
331        memset (&osinf, 0, sizeof (osinf));
332        if (GetVersionEx (&osinf) &&
333            osinf.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS &&
334            osinf.dwMinorVersion == 0) {
335            msg_box (NULL, "WinPT propably does not work on Windows 95 without restrictions",
336                     "WinPT Warning", MB_INFO);
337        }
338        */
339    
340        #ifdef _DEBUG
341      gpg_set_debug_mode (1);      gpg_set_debug_mode (1);
342      debug = 1;      debug = 1;
343  #endif      #endif
344    
345        get_file_version ("WinPT.exe", &ver[0], &ver[1], &ver[2], &ver[3]);
346        get_file_version ("PTD.dll", &ptdver[0], &ptdver[1],
347                                     &ptdver[2], &ptdver[3]);
348        /* XXX
349        if (ptdver[0] != ver[0] || ptdver[1] != ver[1]|| ptdver[2] != ver[2]) {
350            log_box (_("WinPT Error"), MB_ERR,
351                     _("The PTD.dll file has a different version than WinPT.exe\n"
352                       "Please update the PTD.dll to version %d.%d.%d"),
353                       ver[0], ver[1], ver[2]);
354            return 0;
355        }
356        */
357    
358      if (gpg_md_selftest ()) {      if (gpg_md_selftest ()) {
359          msg_box (NULL, _("Cryptographic selftest failed."),          msg_box (NULL, _("Cryptographic selftest failed."),
# Line 283  WinMain (HINSTANCE hinst, HINSTANCE hpre Line 379  WinMain (HINSTANCE hinst, HINSTANCE hpre
379      }      }
380    
381      set_default_kserver ();      set_default_kserver ();
382        load_gettext (winpt_inst_found);
383    
384      if (!mobile) {      if (!mobile) {
385          regist_inst_gnupg (1);          regist_inst_gnupg (1);
# Line 304  WinMain (HINSTANCE hinst, HINSTANCE hpre Line 401  WinMain (HINSTANCE hinst, HINSTANCE hpre
401          gnupg_load_config ();          gnupg_load_config ();
402      }      }
403    
404        if (is_gpg4win_installed ())
405            load_gpg_env (); /* XXX: check return code. */
406    
407      rc = gnupg_check_homedir ();      rc = gnupg_check_homedir ();
408      if (rc) {      if (rc) {
409          log_box (_("WinPT Error"), MB_ERR,          log_box (_("WinPT Error"), MB_ERR,
# Line 311  WinMain (HINSTANCE hinst, HINSTANCE hpre Line 411  WinMain (HINSTANCE hinst, HINSTANCE hpre
411                     "Please check the GPG registry settings:\n%s."),                     "Please check the GPG registry settings:\n%s."),
412                   winpt_strerror (rc));                   winpt_strerror (rc));
413          s = get_fileopen_dlg (GetActiveWindow (),          s = get_fileopen_dlg (GetActiveWindow (),
414                                _("Select GPG Public Keyring"),                                _("Select GPG Public Keyring"),
415                                _("GPG Keyrings (*.gpg)\0*.gpg\0\0"),                                _("GPG Keyrings (*.gpg)\0*.gpg\0\0"),
416                                NULL);                                NULL);
417          if (s != NULL) {          if (s != NULL) {
418              size_t n;              size_t n;
419              char * p = strrchr (s, '\\');              char *p = strrchr (s, '\\');
420              if (!p)              if (!p)
421                  BUG (0);                  BUG (0);
422              n = p - s;              n = p - s;
423              if (n) {              if (n) {
424                  char * file = new char[n+1];                  char *file = new char[n+1];
425                  if (!file)                  if (!file)
426                      BUG (NULL);                      BUG (NULL);
427                  memset (file, 0, n);                  memset (file, 0, n);
# Line 346  WinMain (HINSTANCE hinst, HINSTANCE hpre Line 446  WinMain (HINSTANCE hinst, HINSTANCE hpre
446                               "correct  this problem?"), _("WinPT Error"),                               "correct  this problem?"), _("WinPT Error"),
447                               MB_INFO|MB_YESNO) == IDYES)                               MB_INFO|MB_YESNO) == IDYES)
448              start_gpgprefs = 1;              start_gpgprefs = 1;
449          else          else {
         {  
450              msg_box (NULL, winpt_strerror (rc), _("WinPT Error"), MB_ERR);              msg_box (NULL, winpt_strerror (rc), _("WinPT Error"), MB_ERR);
451              return 0;              return 0;
452          }          }
# Line 369  WinMain (HINSTANCE hinst, HINSTANCE hpre Line 468  WinMain (HINSTANCE hinst, HINSTANCE hpre
468              return 0;              return 0;
469          }          }
470      }      }
471        if (check_for_empty_keyrings (false))
472            first_start = 1;
473    
474      if (!first_start) {      if (!first_start) {
475          rc = gpg_check_permissions (1);          rc = gpg_check_permissions (1);
# Line 377  WinMain (HINSTANCE hinst, HINSTANCE hpre Line 478  WinMain (HINSTANCE hinst, HINSTANCE hpre
478          else if (rc)          else if (rc)
479              return 0;              return 0;
480      }      }
481        
     load_gettext (winpt_inst_found);  
482      init_gnupg_table ();      init_gnupg_table ();
483    
484      nfiles = fm_parse_command_line (cmdline);      if (fm_parse_command_line (cmdline) > 0) {
     if (nfiles > 0) {  
485          free_gnupg_table ();          free_gnupg_table ();
486          return 0;          return 0;
487      }      }
488    
489      if (cmdline && stristr (cmdline, "--wipe-freespace")) {      if (cmdline && stristr (cmdline, "--wipe-freespace")) {
490          dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_SPACE_SECDEL,          dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_SPACE_SECDEL,
491                              GetDesktopWindow(), space_wipefrees_dlg_proc, NULL,                              GetDesktopWindow(), space_wipefrees_dlg_proc, 0,
492                              _("Wipe Free Space"), IDS_WINPT_SPACE_SECDEL);                              _("Wipe Free Space"), IDS_WINPT_SPACE_SECDEL);
493          free_gnupg_table ();          free_gnupg_table ();
494          return 0;          return 0;
# Line 399  WinMain (HINSTANCE hinst, HINSTANCE hpre Line 498  WinMain (HINSTANCE hinst, HINSTANCE hpre
498    
499      if (cmdline && (stristr (cmdline, "--keymanager")      if (cmdline && (stristr (cmdline, "--keymanager")
500                  || stristr (cmdline, "--cardmanager"))) {                  || stristr (cmdline, "--cardmanager"))) {
501          update_keycache (GetDesktopWindow ());          /* If an instance of WinPT is running, just send the command
502               to open the key manager. Otherwise start a new instance.
503             */
504            HWND tray = FindWindow ("WinPT", "WinPT");
505          if (stristr (cmdline, "keymanager"))          if (stristr (cmdline, "keymanager"))
506              dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_KEYMISC,              start_manager = ID_WINPT_KEY;
507                              GetDesktopWindow(), keymanager_dlg_proc, NULL,          else
508                              _("Key Manager"), IDS_WINPT_KEYMISC);                start_manager = ID_WINPT_CARD;
509          else {          if (tray != NULL) {
510              gpg_card_t crd = gpg_card_load ();              PostMessage (tray, WM_COMMAND, start_manager, 0);
511              if (crd)              free_gnupg_table ();
512                  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);  
513          }          }
         keycache_release (0);  
         free_gnupg_table ();  
         return 0;  
514      }      }
515    
516      /* If we found another WinPT instance, just quit to avoid it      /* If we found another WinPT instance, just quit to avoid it
# Line 427  WinMain (HINSTANCE hinst, HINSTANCE hpre Line 522  WinMain (HINSTANCE hinst, HINSTANCE hpre
522      }      }
523    
524      if (cmdline) {      if (cmdline) {
525          if (stristr (cmdline, "--enable-debug") || stristr (cmdline, "--debug")) {          if (stristr (cmdline, "--enable-debug") ||
526                stristr (cmdline, "--debug")) {
527              gpg_set_debug_mode (1);              gpg_set_debug_mode (1);
528              winpt_debug_msg ();              winpt_debug_msg ();
529              debug = 1;              debug = 1;
# Line 460  WinMain (HINSTANCE hinst, HINSTANCE hpre Line 556  WinMain (HINSTANCE hinst, HINSTANCE hpre
556    
557      if (!first_start && !start_gpgprefs) {      if (!first_start && !start_gpgprefs) {
558          gnupg_backup_options ();                  gnupg_backup_options ();        
559          rc = check_crypto_engine ();          if (!check_crypto_engine ()) {
         if (rc) {  
560              DestroyWindow (hwnd);              DestroyWindow (hwnd);
561              free_gnupg_table ();              free_gnupg_table ();
562              return 0;              return 0;
# Line 469  WinMain (HINSTANCE hinst, HINSTANCE hpre Line 564  WinMain (HINSTANCE hinst, HINSTANCE hpre
564      }      }
565            
566      if (start_gpgprefs) {      if (start_gpgprefs) {
         char *ring;  
   
567          DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_GPGPREFS, hwnd,          DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_GPGPREFS, hwnd,
568                          gpgprefs_dlg_proc, 0);                          gpgprefs_dlg_proc, 0);
569          ring = get_gnupg_keyring (0, !NO_STRICT);          if (check_for_empty_keyrings (true))
570          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);  
571      }      }
572    
573      if (first_start) {      if (first_start) {
# Line 507  start: Line 598  start:
598              }              }
599              break;              break;
600    
601          case -1:          case -1: /* Cancel/Abort. */
602              DestroyWindow (hwnd);              DestroyWindow (hwnd);
603              free_gnupg_table ();              free_gnupg_table ();
604              return 0;              return 0;
# Line 539  start: Line 630  start:
630              }              }
631          }          }
632          if (check_default_key (c)) {          if (check_default_key (c)) {
633              char * p = get_gnupg_default_key ();              char *p = get_gnupg_default_key ();
634              log_box (_("WinPT Error"), MB_ERR,              log_box (_("WinPT Error"), MB_ERR,
635                       _("Default key from the GPG options file could not be found.\n"                       _("Default key from the GPG options file could not be found.\n"
636                         "Please check your gpg.conf (options) to correct this:\n\n"                         "Please check your gpg.conf (options) to correct this:\n\n"
# Line 554  start: Line 645  start:
645                              elgamal_warn_dlg_proc, 0);                              elgamal_warn_dlg_proc, 0);
646      }      }
647    
648        if (start_manager)
649            PostMessage (hwnd, WM_COMMAND, start_manager, 0);
650    
651      accel_tab = LoadAccelerators (glob_hinst, (LPCTSTR)IDR_WINPT_ACCELERATOR);      accel_tab = LoadAccelerators (glob_hinst, (LPCTSTR)IDR_WINPT_ACCELERATOR);
652      keyring_check_last_access (); /* init */      keyring_check_last_access (); /* init */
653      while (GetMessage (&msg, hwnd, 0, 0)) {      while (GetMessage (&msg, hwnd, 0, 0)) {

Legend:
Removed from v.59  
changed lines
  Added in v.133

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26