/[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 20 by twoaday, Wed Jul 27 11:17:22 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   *   *
6   * WinPT is free software; you can redistribute it and/or modify   * WinPT is free software; you can redistribute it and/or modify
7   * it under the terms of the GNU General Public License as published by   * it under the terms of the GNU General Public License as published by
8   * the Free Software Foundation; either version 2 of the License, or   * the Free Software Foundation; either version 2 of the License, or
9   * (at your option) any later version.   * (at your option) any later version.
10   *   *
11   * WinPT is distributed in the hope that it will be useful,   * WinPT is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.   * GNU General Public License for more details.
15   *   *
16   * You should have received a copy of the GNU General Public License   * You should have received a copy of the GNU General Public License
17   * along with WinPT; if not, write to the Free Software Foundation,   * along with WinPT; if not, write to the Free Software Foundation,
18   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19   */   */
20  #include <windows.h>  #ifdef HAVE_CONFIG_H
21    #include <config.h>
22  #include "../resource.h"  #endif
23  #include "wptTypes.h"  
24  #include "wptW32API.h"  #include <windows.h>
25  #include "wptVersion.h"  
26  #include "wptErrors.h"  #include "resource.h"
27  #include "wptGPG.h"  #include "wptTypes.h"
28  #include "wptRegistry.h"  #include "wptW32API.h"
29  #include "wptCommonCtl.h"  #include "wptVersion.h"
30  #include "wptDlgs.h"  #include "wptErrors.h"
31  #include "wptNLS.h"  #include "wptGPG.h"
32  #include "wptKeyserver.h"  #include "wptRegistry.h"
33  #include "wptCard.h"  #include "wptCommonCtl.h"
34  #include "wptFileManager.h"  #include "wptDlgs.h"
35  #include "wptContext.h"  #include "wptNLS.h"
36    #include "wptKeyserver.h"
37  HINSTANCE glob_hinst; /* global instance for the dialogs */  #include "wptCard.h"
38  HWND glob_hwnd; /* global window handle for the dialogs */  #include "wptFileManager.h"
39  HWND activ_hwnd;  #include "wptContext.h"
40  LOCK mo_file;  #include "wptCardEdit.h"
41  int scard_support = 0;  #include "wptCrypto.h"
42  int debug = 0;  
43  int mobile = 0;  HINSTANCE glob_hinst;   /* global instance for the dialogs */
44  int gpg_read_only = 0;  HWND glob_hwnd;         /* global window handle for the dialogs */
45  char gpgver[3];  HWND activ_hwnd;
46    int scard_support = 0;
47  /* Internal IPC */  int debug = 0;
48  int start_keymanager = 0;  int mobile = 0;
49    int gpg_read_only = 0;
50  static void  char gpgver[3];
51  update_keycache (HWND hwnd)  
52  {  
53      refresh_cache_s rcs = {0};  /* Load the key cache and rebuild the signature cache. */
54      rcs.kr_reload = 0;  static void
55      rcs.kr_update = 1;  update_keycache (HWND hwnd)
56      rcs.tr_update = 1;  {
57      DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_KEYCACHE, hwnd,      refresh_cache_s rcs = {0};
58                      keycache_dlg_proc, (LPARAM)&rcs);      rcs.kr_reload = 0;
59  } /* update_keycache */      rcs.kr_update = 1;
60        rcs.tr_update = 1;
61        DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_KEYCACHE, hwnd,
62  static char *                      keycache_dlg_proc, (LPARAM)&rcs);
63  get_gettext_lang (void)  }
64  {      
65      char * fname;  
66      fname = get_reg_entry_mo ();  /* Set GPGME debug mode. If @val is 0, the debug mode is disabled. */
67      if (!fname)  void
68          return NULL;  gpg_set_debug_mode (int val)
69      return fname;  {      
70  } /* get_gettext_lang */      if (val)
71            putenv ("GPGME_DEBUG=5:gpgme.dbg");
72        else
73  static void          putenv ("GPGME_DEBUG=");
74  load_gettext (void)  }
75  {  
76      char * nls = NULL;  
77      char * file = NULL;  /* Return the name of the gettext language file. */
78    static char*
79      nls = get_gettext_lang ();  get_gettext_lang (void)
80      if (nls) {  {    
81          set_gettext_file ("winpt", nls);      char *fname;
82          file = make_filename (nls, "winpt", "mo");      fname = get_reg_entry_mo ();
83          if (!file_exist_check (nls) && init_file_lock (&mo_file, file))  {      if (!fname)
84              msg_box (NULL, _("Could not initizalize file lock.\n"          return NULL;
85                               "Native Language Support"),      return fname;
86                       _("WinPT Error"), MB_ERR);  }
87          }  
88          free_if_alloc (nls);  
89          free_if_alloc (file);  /* Initialize the gettext sub system. */
90      }  static void
91  } /* load_gettext */  load_gettext (int prev_inst)
92    {
93        char *nls = NULL;
94  /* check if the default key from the gpg.conf file is available in the  
95     keyring. if not, bail out because encryption won't work properly then. */      nls = get_gettext_lang ();
96  static int      if (nls != NULL) {
97  check_default_key (gpgme_keycache_t kc)          set_gettext_file ("winpt", nls);
98  {          free_if_alloc (nls);
99      gpgme_key_t key;      }
100      gpgme_error_t err = GPGME_No_Error;  }
101      char * defkey;  
102    
103      defkey = get_gnupg_default_key ();  /* Load the GPG environment. On the first start, some
104      if (defkey)     checks are performed to find out in what state GPG is.
105          err = gpgme_keycache_find_key (kc, defkey, 0, &key);     Return value: 0  everything OK.
106      free_if_alloc (defkey);                   >0  fatal error.
107      return err? -1 : 0;                   -1 public keyring is empty or does not exist. */
108  } /* check_default_key */  static int
109    load_gpg_env (void)
110    {
111  /* Return the WinPT program file name (with full pathname). */      SECURITY_ATTRIBUTES sec_attr;
112  static const char *      char *p;
113  get_prog_part (const char * fname, int use_cwd)      char *pkr;
114  {  
115      static char program[1024];      p = get_reg_entry_gpg4win ("gpg.exe");
116      char currdir[256], * cmd = NULL;      if (!p)
117      int j;          return (1);
118                if (file_exist_check (p)) {
119      memset (currdir, 0, DIM (currdir));          free_if_alloc (p);
120      memset (program, 0, DIM (program));          return (1);
121                }
122      if (use_cwd) {      free_if_alloc (p);
123          GetCurrentDirectory (DIM (currdir)-1, currdir);      p = multi_gnupg_path (0);
124          _snprintf (program, DIM (program)-1, "%s\\%s", currdir, fname);      if (p && dir_exist_check (p)) {
125      }          memset (&sec_attr, 0, sizeof (sec_attr));
126      else {          sec_attr.nLength = sizeof (sec_attr);
127          cmd = GetCommandLine ();          if (!CreateDirectory (p, &sec_attr)) {
128          if (cmd == NULL)              msg_box (NULL, _("Could not create GPG home directory"),
129              return NULL;                       _("WinPT Error"), MB_ERR);
130          strncpy (currdir, cmd, 255);              free_if_alloc (p);
131          j = strlen (currdir);              return (2);
132          while (j--) {          }
133              if (currdir[j] == '\\')      }
134                  break;                pkr = make_filename (p, "pubring", "gpg");
135          }      free_if_alloc (p);
136          currdir[j] = 0;      if (!pkr)
137          _snprintf (program, DIM (program)-1, "%s\\%s", currdir + 1, fname);          return -1;
138      }      if (get_file_size (pkr) == 0) {
139      return program;          free_if_alloc (pkr);
140  } /* get_prog_part */          return -1;
141        }
142        return 0;
143  static int  }
144  check_crypto_engine (void)  
145  {  
146      int ma=1, mi=4, pa=0; /* GPG 1.4.0 */  /* check if the default key from the gpg.conf file is available in the
147      int rc;     keyring. if not, bail out because encryption won't work properly then. */
148    static int
149      rc = check_gnupg_engine (&ma, &mi, &pa);  check_default_key (gpg_keycache_t kc)
150      if (rc == -1) {  {
151          msg_box (NULL, _("Could not read GnuPG version."), _("WinPT Error"), MB_ERR);      gpgme_key_t key;
152          return rc;      gpgme_error_t err = GPG_ERR_NO_ERROR;
153      }      char *defkey;
154      else if (rc) {  
155          log_box (_("WinPT Error"), MB_ERR,      defkey = get_gnupg_default_key ();
156                   _("Sorry, you need a newer GPG version.\n"      if (defkey)
157                     "GPG version %d.%d.%d required GPG version 1.4.0"),          err = gpg_keycache_find_key (kc, defkey, 0, &key);
158                     ma, mi, pa);      else
159          return rc;          msg_box (NULL, _("No useable secret key found."),
160      }                   _("WinPT Error"), MB_ERR);
161      /* We enable smartcard support for GPG: 1.9.x or >= 1.4.0 */      free_if_alloc (defkey);
162      if (ma >= 1 && mi >= 4)      return err? -1 : 0;
163          scard_support = 1;  }
164    
165      gpgver[0] = ma;  
166      gpgver[1] = mi;  /* Return the WinPT program file name (with full pathname). */
167      gpgver[2] = pa;  static const char*
168      return rc;  get_prog_part (const char * fname, int use_cwd)
169  } /* check_crypto_engine */  {
170        static char program[512];
171        char currdir[256];
172  static int      char *cmd = NULL;
173  load_keyserver_conf (int quiet)      int j;
174  {          
175      const char * t;      memset (currdir, 0, DIM (currdir));
176      int rc;      memset (program, 0, DIM (program));
177            
178      if (reg_prefs.kserv_conf)      if (use_cwd) {
179          t = reg_prefs.kserv_conf;          GetCurrentDirectory (DIM (currdir)-1, currdir);
180      else if (!file_exist_check (get_prog_part ("keyserver.conf", 0)))          _snprintf (program, DIM (program)-1, "%s\\%s", currdir, fname);
181          t = get_prog_part ("keyserver.conf", 0);      }
182      else      else {
183          t = "keyserver.conf";          cmd = GetCommandLine ();
184      rc = kserver_load_conf (t);          if (cmd == NULL)
185      if (rc && !quiet)              return NULL;
186          msg_box (NULL, winpt_strerror (rc), _("Keyserver"), MB_ERR);          strncpy (currdir, cmd, sizeof (currdir)-1);
187      return rc;          j = strlen (currdir);
188  }          while (j--) {
189                if (currdir[j] == '\\')
190                    break;
191  static void          }
192  enable_mobile_mode (void)          currdir[j] = 0;
193  {          _snprintf (program, DIM (program)-1, "%s\\%s", currdir + 1, fname);
194      memset (&reg_prefs, 0, sizeof (reg_prefs));      }
195      reg_prefs.always_trust = 0;      return program;
196      reg_prefs.auto_backup = 0;  }
197      reg_prefs.cache_time = 0;  
198      reg_prefs.expert = 0;  
199      reg_prefs.keylist_mode = 1;  /* Check that the underlying crypto engine fullfills the minimal
200      reg_prefs.kserv_conf = m_strdup ("keyserver.conf");     requirements so all commands work properly. */
201      reg_prefs.no_zip_mmedia = 1;  static bool
202      reg_prefs.use_tmpfiles = 1;  check_crypto_engine (void)
203      reg_prefs.word_wrap = 80;  {
204      reg_prefs.use_viewer = 0; /* XXX */      int ma=0, mi=0, pa=0;
205  }      int rc;
206    
207  char* get_subkey_fingerprint (gpgme_ctx_t ctx, const char *keyid);      rc = check_gnupg_engine (NEED_GPG_VERSION, &ma, &mi, &pa);
208        if (rc == -1) {
209  int WINAPI          msg_box (NULL, _("Could not read GnuPG version."),
210  #ifndef WINPT_IPC                   _("WinPT Error"), MB_ERR);
211  WinMain (HINSTANCE hinst, HINSTANCE hprev, LPSTR cmdline, int showcmd)          return false;
212  #else      }
213  win_main (HINSTANCE hinst, HINSTANCE hprev, LPSTR cmdline, int showcmd)      else if (rc) {
214  #endif          log_box (_("WinPT Error"), MB_ERR,
215  {                   _("Sorry, you need a newer GPG version.\n"
216      WNDCLASS wc = {0, winpt_main_proc, 0, 0, hinst, 0, 0, 0, 0, PGM_NAME};                     "GPG version %d.%d.%d required GPG version "NEED_GPG_VERSION),
217      HACCEL accel_tab;                     ma, mi, pa);
218      int rc, ec, created = 0, use_cwd = 0, nfiles = 0;          return false;
219      int first_start = 0, start_gpgprefs = 0;      }
220      const char * s;      /* We enable smartcard support for GPG: >= 2 or >= 1.4.3 */
221      MSG msg;      if (ma > 1 || pa >= 3)
222      HWND hwnd;          scard_support = 1;
223    
224      glob_hinst = hinst;      gpgver[0] = ma;
225            gpgver[1] = mi;
226      gpgme_lib_init ();      gpgver[2] = pa;
227      #ifdef _DEBUG      return true;
228      gpgme_set_debug_mode (1);  }
229      #endif  
230      gpgme_set_pgm_string ("WinPT "PGM_VERSION);  
231    /* Try to load the keyserver config file. If @quiet is 1
232      s = PTD_get_version ();     do not show any errors. */
233      if (strcmp (s, "0.8.0")) {  static int
234          log_box (_("Privacy Tray Dynamic (PTD)"), MB_ERR,  load_keyserver_conf (int quiet)
235                   _("Please update your PTD.dll to the newest version, "  {
236                     "the version (%s) you use is too old."), s);      const char *t;
237          return 0;      int rc;
238      }  
239        if (reg_prefs.kserv_conf)
240      if (gpg_md_selftest ()) {          t = reg_prefs.kserv_conf;
241          msg_box (NULL, _("Cryptographic selftest failed."),      else if (!file_exist_check (get_prog_part ("keyserver.conf", 0)))
242                   _("WinPT Error"), MB_ERR);          t = get_prog_part ("keyserver.conf", 0);
243          return 0;      else
244      }          t = "keyserver.conf";
245        rc = kserver_load_conf (t);
246      if (cmdline && stristr (cmdline, "--mobile")) {      if (rc && !quiet)
247          msg_box (NULL, "WARNING: mobile modus is not fully implemented yet!", "WinPT", MB_INFO);          msg_box (NULL, winpt_strerror (rc), _("Keyserver"), MB_ERR);
248          mobile = 1;      return rc;
249      }  }
250    
251      set_default_kserver ();  
252    /* Check if both keyrings are empty. This indicates that
253      if (!mobile) {     WinPT should offer to generate a key pair. */
254          regist_inst_gnupg (1);  static bool
255          regist_inst_winpt (1, &created);  check_for_empty_keyrings (bool pub_only)
256      }  {
257      else {      char *p;
258          enable_mobile_mode ();      int n = 0;
259          /* XXX: ask for GPG path */  
260          created = 1; /* Disable registry writing */      p = get_gnupg_keyring (1, 0);
261      }      if (file_exist_check (p) == 0 && get_file_size (p) == 0)
262            n++;
263      if (!created) {      free_if_alloc (p);
264          memset (&reg_prefs, 0, sizeof (reg_prefs));      if (pub_only)
265          reg_prefs.use_tmpfiles = 1; /* default */          return n == 1? true : false;
266          reg_prefs.fm.progress = 0; /* XXX: fix the bug and enable it again */      p = get_gnupg_keyring (0, 0);
267          get_reg_winpt_prefs (&reg_prefs);      if (file_exist_check (p) == 0 && get_file_size (p) == 0)
268          if (!reg_prefs.no_hotkeys)          n++;
269              hotkeys_modify ();      free_if_alloc (p);
270      }      return n==2? true : false;
271    }
272      rc = gnupg_check_homedir ();  
273      if (rc) {  
274          log_box (_("WinPT Error"), MB_ERR,  /* Enable the mobility mode. */
275                   _("GPG home directory is not set correctly.\n"  static void
276                     "Please check the GPG registry settings:\n%s."),  enable_mobile_mode (void)
277                   winpt_strerror (rc));  {
278          const char * s = get_filename_dlg (GetActiveWindow (), FILE_OPEN,      memset (&reg_prefs, 0, sizeof (reg_prefs));
279                                             _("Select GPG Public Keyring"),      reg_prefs.always_trust = 0;
280                                             _("GPG Keyrings (*.gpg)\0*.gpg\0\0"),      reg_prefs.auto_backup = 0;
281                                             NULL);      reg_prefs.cache_time = 0;
282          if (s && !file_exist_check (s))      reg_prefs.expert = 0;
283          {      reg_prefs.keylist_mode = 1;
284              size_t n;      reg_prefs.kserv_conf = m_strdup ("keyserver.conf");
285              char * p = strrchr (s, '\\');      reg_prefs.no_zip_mmedia = 1;
286              if (!p)      reg_prefs.use_tmpfiles = 1;
287                  BUG (0);      reg_prefs.word_wrap = 80;
288              n = p - s;      reg_prefs.use_viewer = 0; /* XXX */
289              if (n)  }
290              {  
291                  char * file = new char[n+1];  
292                  if (!file)  /* Main entry point. */
293                      BUG (NULL);  int WINAPI
294                  memset (file, 0, n);  WinMain (HINSTANCE hinst, HINSTANCE hprev, LPSTR cmdline, int showcmd)
295                  memcpy (file, s, n);  {
296                  file[n] = '\0';              WNDCLASS wc = {0, winpt_main_proc, 0, 0, hinst, 0, 0, 0, 0, PGM_NAME};
297                  set_reg_entry_gpg ("HomeDir", file);      HACCEL accel_tab;
298                  free_if_alloc (file);      MSG msg;
299                  gnupg_check_homedir (); /* change gpgProgram if needed */      HWND hwnd = NULL;
300              }      WORD ver[3], ptdver[4];
301          }      int rc, ec, created = 0;
302          else {      int first_start = 0, start_gpgprefs = 0;
303              msg_box (NULL, _("GPG home directory could not be determited."),      int winpt_inst_found = 0;
304                       _("WinPT Error"), MB_ERR);      int start_manager = 0;
305              goto start;      const char *s;
306          }  
307      }      glob_hinst = hinst;
308        if (cmdline && stristr (cmdline, "--stop")) {
309      rc = check_gnupg_prog ();          hwnd = FindWindow ("WinPT", "WinPT");
310      if (rc) {          if (hwnd != NULL)
311          if (msg_box (NULL, _("Could not find the GPG binary (gpg.exe).\n"              PostMessage (hwnd, WM_DESTROY, 0, 0);
312                               "Do you want to start the GPG preferences to "          return 0;
313                               "correct  this problem?"), _("WinPT Error"),      }
314                               MB_INFO|MB_YESNO) == IDYES)  
315              start_gpgprefs = 1;      /*
316          else      OSVERSIONINFO osinf;
317          {      memset (&osinf, 0, sizeof (osinf));
318              msg_box (NULL, winpt_strerror (rc), _("WinPT Error"), MB_ERR);      if (GetVersionEx (&osinf) &&
319              return 0;          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      rc = gnupg_access_files ();      }
324      if (!start_gpgprefs && rc)      */
325      {  
326          if (rc == WPTERR_GPG_KEYRINGS || rc == WPTERR_GPG_OPT_KEYRINGS)      #ifdef _DEBUG
327          {      gpg_set_debug_mode (1);
328              ec = msg_box (NULL,      debug = 1;
329                  _("Could not access and/or find the public and secret keyring.\n"      #endif
330                    "If this is an accident, quit the program and fix it.\n\n"  
331                    "Continue if you want that WinPT offers you more choices.\n"),      get_file_version ("WinPT.exe", &ver[0], &ver[1], &ver[2], &ver[3]);
332                    "WinPT", MB_INFO|MB_YESNO);      get_file_version ("PTD.dll", &ptdver[0], &ptdver[1],
333              if (ec == IDYES)                                   &ptdver[2], &ptdver[3]);
334                  first_start = 1;      /* XXX
335          }      if (ptdver[0] != ver[0] || ptdver[1] != ver[1]|| ptdver[2] != ver[2]) {
336          if (!first_start)          log_box (_("WinPT Error"), MB_ERR,
337          {                   _("The PTD.dll file has a different version than WinPT.exe\n"
338              msg_box (NULL, winpt_strerror (rc), _("WinPT Error"), MB_ERR);                     "Please update the PTD.dll to version %d.%d.%d"),
339              return 0;                     ver[0], ver[1], ver[2]);
340          }          return 0;
341      }      }
342        */
343      if (!first_start)  
344      {      if (gpg_md_selftest ()) {
345          rc = gpg_check_permissions (1);          msg_box (NULL, _("Cryptographic selftest failed."),
346          if (rc && rc == 2)                   _("WinPT Error"), MB_ERR);
347              gpg_read_only = 1;          return 0;
348          else if (rc)      }
349              return 0;  
350      }      s = gpgme_check_version (NEED_GPGME_VERSION);
351        if (!s || !*s) {
352      load_gettext ();          msg_box (NULL, _("A newer GPGME version is needed; at least "NEED_GPGME_VERSION),
353      init_gnupg_table ();                   _("WinPT Error"), MB_ERR);
354            return 0;
355      nfiles = fm_parse_command_line (cmdline);      }
356      if (nfiles > 0)  
357          return 0;      CreateMutex (NULL, TRUE, PGM_NAME);
358        if (GetLastError () == ERROR_ALREADY_EXISTS)
359      if (cmdline && stristr (cmdline, "--wipe-freespace")) {          winpt_inst_found = 1;
360          dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_SPACE_SECDEL,  
361                              GetDesktopWindow(), space_wipefrees_dlg_proc, NULL,      if (cmdline && stristr (cmdline, "--mobile")) {
362                              _("Wipe Free Space"), IDS_WINPT_SPACE_SECDEL);          msg_box (NULL, "WARNING: mobile modus is not fully implemented yet!",
363          free_gnupg_table ();                   "WinPT", MB_INFO);
364          return 0;          mobile = 1;
365      }      }
366    
367      load_keyserver_conf (cmdline? 1 : 0);      set_default_kserver ();
368      if (start_keymanager) {      load_gettext (winpt_inst_found);
369          dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_KEYMISC,  
370                              GetDesktopWindow(), keymanager_dlg_proc, NULL,      if (!mobile) {
371                              _("Key Manager"), IDS_WINPT_KEYMISC);          regist_inst_gnupg (1);
372          keycache_release ();          regist_inst_winpt (1, &created);
373          free_gnupg_table ();      }
374          return 0;      else {
375      }          enable_mobile_mode ();
376            /* XXX: ask for GPG path */
377      if (cmdline && (stristr (cmdline, "--keymanager")          created = 1; /* Disable registry writing */
378                  || stristr (cmdline, "--cardmanager"))) {      }
379          update_keycache (GetDesktopWindow ());  
380          if (stristr (cmdline, "keymanager"))      if (!created) {
381              dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_KEYMISC,          memset (&reg_prefs, 0, sizeof (reg_prefs));
382                              GetDesktopWindow(), keymanager_dlg_proc, NULL,          reg_prefs.use_tmpfiles = 1; /* default */
383                              _("Key Manager"), IDS_WINPT_KEYMISC);            reg_prefs.fm.progress = 0; /* XXX: fix the bug and enable it again */
384          else {          get_reg_winpt_prefs (&reg_prefs);
385              gpgme_card_t crd = smartcard_init ();          if (!reg_prefs.no_hotkeys)
386              if (crd)              hotkeys_modify ();
387                  dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_CARD_EDIT,          gnupg_load_config ();
388                                    GetDesktopWindow(), card_edit_dlg_proc,      }
389                                    (LPARAM)crd, _("Card Manager"),  
390                                    IDS_WINPT_CARD_EDIT);      if (is_gpg4win_installed ())
391              gpgme_card_release (crd);          load_gpg_env (); /* XXX: check return code. */
392          }  
393          keycache_release ();      rc = gnupg_check_homedir ();
394          free_gnupg_table ();      if (rc) {
395          return 0;          log_box (_("WinPT Error"), MB_ERR,
396      }                   _("GPG home directory is not set correctly.\n"
397                       "Please check the GPG registry settings:\n%s."),
398      CreateMutex (NULL, TRUE, PGM_NAME);                   winpt_strerror (rc));
399      if (GetLastError () == ERROR_ALREADY_EXISTS) {          s = get_fileopen_dlg (GetActiveWindow (),
400          free_gnupg_table ();                                _("Select GPG Public Keyring"),
401          return 0;                                _("GPG Keyrings (*.gpg)\0*.gpg\0\0"),
402      }                                NULL);
403            if (s != NULL) {
404      if (cmdline) {              size_t n;
405          if (stristr (cmdline, "--enable-debug") || stristr (cmdline, "--debug")) {              char *p = strrchr (s, '\\');
406              gpgme_set_debug_mode (1);              if (!p)
407              winpt_debug_msg ();                  BUG (0);
408              debug = 1;              n = p - s;
409          }              if (n) {
410      }                  char *file = new char[n+1];
411                    if (!file)
412      wc.hIcon = LoadIcon (glob_hinst, MAKEINTRESOURCE (IDI_WINPT));                      BUG (NULL);
413      rc = RegisterClass (&wc);                  memset (file, 0, n);
414      if (rc == FALSE) {                  memcpy (file, s, n);
415          msg_box (NULL, _("Could not register window class"), _("WinPT Error"), MB_ERR);                  file[n] = '\0';        
416          free_gnupg_table ();                  set_reg_entry_gpg ("HomeDir", file);
417          return 0;                  free_if_alloc (file);
418      }                  gnupg_check_homedir (); /* change gpgProgram if needed */
419                }
420      hwnd = CreateWindow (PGM_NAME,          }
421                           PGM_NAME,          else {
422                           0, 0, 0, 0, 0,              msg_box (NULL, _("GPG home directory could not be determited."),
423                           NULL,                       _("WinPT Error"), MB_ERR);
424                           NULL,              goto start;
425                           hinst,          }
426                           NULL);      }
427      if (hwnd == NULL) {  
428          msg_box (NULL, _("Could not create window"), _("WinPT Error"), MB_ERR);      rc = check_gnupg_prog ();
429          free_gnupg_table ();      if (rc) {
430          return 0;          if (msg_box (NULL, _("Could not find the GPG binary (gpg.exe).\n"
431      }                               "Do you want to start the GPG preferences to "
432      glob_hwnd = hwnd;                               "correct  this problem?"), _("WinPT Error"),
433      UpdateWindow (hwnd);                               MB_INFO|MB_YESNO) == IDYES)
434                start_gpgprefs = 1;
435      if (!first_start && !start_gpgprefs) {          else {
436          gnupg_backup_options (1);              msg_box (NULL, winpt_strerror (rc), _("WinPT Error"), MB_ERR);
437          gnupg_backup_options (0);              return 0;
438                    }
439          rc = check_crypto_engine ();      }
440          if (rc) {  
441              DestroyWindow (hwnd);      rc = gnupg_access_files ();
442              free_gnupg_table ();      if (!start_gpgprefs && rc) {
443              return 0;          if (rc == WPTERR_GPG_KEYRINGS || rc == WPTERR_GPG_OPT_KEYRINGS) {
444          }              ec = msg_box (NULL,
445      }                  _("Could not access and/or find the public and secret keyring.\n"
446                      "If this is an accident, quit the program and fix it.\n\n"
447      if (start_gpgprefs)                    "Continue if you want that WinPT offers you more choices.\n"),
448      {                    "WinPT", MB_INFO|MB_YESNO);
449          char *ring;              if (ec == IDYES)
450          size_t size = 0;                  first_start = 1;
451          DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_GPGPREFS, hwnd,          }
452                          gpgprefs_dlg_proc, NULL);          if (!first_start) {
453          ring = get_gnupg_keyring (0, !NO_STRICT);              msg_box (NULL, winpt_strerror (rc), _("WinPT Error"), MB_ERR);
454          if (gnupg_access_keyring (0) == -1 && get_file_size (ring) == 0)              return 0;
455              first_start = 1; /* The keyring is empty! */          }
456          free_if_alloc (ring);      }
457      }      if (check_for_empty_keyrings (false))
458            first_start = 1;
459      if (first_start) {  
460          struct key_wizard_s c, dummy;      if (!first_start) {
461  start:          rc = gpg_check_permissions (1);
462          DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_FIRST, hwnd,          if (rc && rc == 2)
463                          first_run_dlg_proc, (LPARAM)&dummy);              gpg_read_only = 1;
464          switch (dummy.interactive)          else if (rc)
465          {              return 0;
466          case SETUP_KEYGEN:      }
467              c.interactive = 1;      
468              rc = DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_KEYWIZARD,      init_gnupg_table ();
469                                   hwnd, keygen_wizard_dlg_proc, (LPARAM)&c);  
470              if (!rc)      if (fm_parse_command_line (cmdline) > 0) {
471                  goto start;          free_gnupg_table ();
472              break;          return 0;
473        }
474          case SETUP_IMPORT:  
475              rc = gnupg_copy_keyrings ();      if (cmdline && stristr (cmdline, "--wipe-freespace")) {
476              if (rc) {          dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_SPACE_SECDEL,
477                  msg_box (hwnd, winpt_strerror (rc), _("WinPT Error"), MB_ERR);                              GetDesktopWindow(), space_wipefrees_dlg_proc, 0,
478                  goto start;                              _("Wipe Free Space"), IDS_WINPT_SPACE_SECDEL);
479              }          free_gnupg_table ();
480              break;          return 0;
481        }
482          case SETUP_EXISTING:  
483              DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_GPGPREFS, hwnd,      load_keyserver_conf (cmdline? 1 : 0);
484                              gpgprefs_dlg_proc, NULL);  
485              break;      if (cmdline && (stristr (cmdline, "--keymanager")
486                    || stristr (cmdline, "--cardmanager"))) {
487          case -1:          /* If an instance of WinPT is running, just send the command
488              DestroyWindow (hwnd);             to open the key manager. Otherwise start a new instance.
489              free_gnupg_table ();           */
490              return 0;          HWND tray = FindWindow ("WinPT", "WinPT");
491          }          if (stristr (cmdline, "keymanager"))
492          update_keycache (hwnd);              start_manager = ID_WINPT_KEY;
493          check_crypto_engine ();          else
494      }              start_manager = ID_WINPT_CARD;
495      else {          if (tray != NULL) {
496          gpgme_keycache_t c;              PostMessage (tray, WM_COMMAND, start_manager, 0);
497          update_keycache (hwnd);              free_gnupg_table ();
498          c = keycache_get_ctx (1);              return 0;
499          if (!c || !gpgme_keycache_count (c)) {          }
500              gnupg_display_error ();      }
501              msg_box (hwnd, _("The keycache was not initialized or is empty.\n"  
502                               "Please check your GPG config (keyrings, pathes...)"),      /* If we found another WinPT instance, just quit to avoid it
503                               _("WinPT Error"), MB_ERR);         will be executed twice. */
504              ec = msg_box (NULL, _("It seems that GPG is not set properly.\n"      if (winpt_inst_found) {
505                                    "Do you want to start the GPG preferences dialog?"),          log_debug ("%s", "WinMain: WinPT is already running.");
506                              "WinPT", MB_INFO|MB_YESNO);          free_gnupg_table ();
507              if (ec == IDYES) {          return 0;
508                  DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_GPGPREFS, hwnd,      }
509                                  gpgprefs_dlg_proc, NULL);  
510                  update_keycache (hwnd);      if (cmdline) {
511              }          if (stristr (cmdline, "--enable-debug") ||
512              else {              stristr (cmdline, "--debug")) {
513                  DestroyWindow (hwnd);              gpg_set_debug_mode (1);
514                  free_gnupg_table ();              winpt_debug_msg ();
515                  return 0;              debug = 1;
516              }          }
517          }      }
518          if (check_default_key (c)) {  
519              char * p = get_gnupg_default_key ();      wc.hIcon = LoadIcon (glob_hinst, MAKEINTRESOURCE (IDI_WINPT));
520              log_box (_("WinPT Error"), MB_ERR,      rc = RegisterClass (&wc);
521                       _("Default key from the GPG options file could not be found.\n"      if (rc == FALSE) {
522                         "Please check your gpg.conf (options) to correct this:\n\n"          msg_box (NULL, _("Could not register window class"),
523                         "%s: public key not found."), p? p : "[null]");                   _("WinPT Error"), MB_ERR);
524              free_if_alloc (p);          free_gnupg_table ();
525              DestroyWindow (hwnd);          return 0;
526              free_gnupg_table ();      }
527              return 0;  
528          }      hwnd = CreateWindow (PGM_NAME,
529          if (count_insecure_elgkeys ())                           PGM_NAME,
530              DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_ELGWARN, glob_hwnd,                           0, 0, 0, 0, 0,
531                              elgamal_warn_dlg_proc, NULL);                           NULL,
532      }                           NULL,
533                             hinst,
534      accel_tab = LoadAccelerators (glob_hinst, (LPCTSTR)IDR_WINPT_ACCELERATOR);                           NULL);
535      keyring_check_last_access (); /* init */      if (hwnd == NULL) {
536      while (GetMessage (&msg, hwnd, 0, 0)) {          msg_box (NULL, _("Could not create window"), _("WinPT Error"), MB_ERR);
537          if (!TranslateAccelerator (msg.hwnd, accel_tab, &msg)) {          free_gnupg_table ();
538              TranslateMessage (&msg);          return 0;
539              DispatchMessage (&msg);      }
540          }      glob_hwnd = hwnd;
541      }      UpdateWindow (hwnd);
542            
543      return 0;      if (!first_start && !start_gpgprefs) {
544  } /* WinMain */          gnupg_backup_options ();        
545            if (!check_crypto_engine ()) {
546                DestroyWindow (hwnd);
547                free_gnupg_table ();
548                return 0;
549            }
550        }
551        
552        if (start_gpgprefs) {
553            DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_GPGPREFS, hwnd,
554                            gpgprefs_dlg_proc, 0);
555            if (check_for_empty_keyrings (true))
556                first_start = 1; /* The public keyring is empty! */
557        }
558    
559        if (first_start) {
560            struct first_start_s fs;
561            struct genkey_s c;
562            HWND h;
563    start:
564            h = GetDesktopWindow ();
565            DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_GPGPREFS, h,
566                                gpgprefs_dlg_proc, 0);
567            DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_FIRST, h,
568                            first_run_dlg_proc, (LPARAM)&fs);
569            switch (fs.choice) {
570            case SETUP_KEYGEN:
571                c.interactive = 1;
572                c.first_start = 1;
573                rc = DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_KEYWIZARD,
574                                     h, keygen_wizard_dlg_proc, (LPARAM)&c);
575                if (!rc)
576                    goto start;
577                break;
578    
579            case SETUP_IMPORT:
580                rc = gnupg_copy_keyrings ();
581                if (rc) {
582                    msg_box (hwnd, winpt_strerror (rc), _("WinPT Error"), MB_ERR);
583                    goto start;
584                }
585                break;
586    
587            case -1: /* Cancel/Abort. */
588                DestroyWindow (hwnd);
589                free_gnupg_table ();
590                return 0;
591            }
592            update_keycache (hwnd);
593            check_crypto_engine ();
594        }
595        else {
596            gpg_keycache_t c;
597            update_keycache (hwnd);
598            c = keycache_get_ctx (1);
599            if (!c || !gpg_keycache_get_size (c)) {
600                gnupg_display_error ();
601                msg_box (hwnd, _("The keycache was not initialized or is empty.\n"
602                                 "Please check your GPG config (keyrings, pathes...)"),
603                                 _("WinPT Error"), MB_ERR);
604                ec = msg_box (NULL, _("It seems that GPG is not set properly.\n"
605                                      "Do you want to start the GPG preferences dialog?"),
606                                "WinPT", MB_INFO|MB_YESNO);
607                if (ec == IDYES) {
608                    DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_GPGPREFS, hwnd,
609                                    gpgprefs_dlg_proc, 0);
610                    update_keycache (hwnd);
611                }
612                else {
613                    DestroyWindow (hwnd);
614                    free_gnupg_table ();
615                    return 0;
616                }
617            }
618            if (check_default_key (c)) {
619                char *p = get_gnupg_default_key ();
620                log_box (_("WinPT Error"), MB_ERR,
621                         _("Default key from the GPG options file could not be found.\n"
622                           "Please check your gpg.conf (options) to correct this:\n\n"
623                           "%s: public key not found."), p? p : "[null]");
624                free_if_alloc (p);
625                DestroyWindow (hwnd);
626                free_gnupg_table ();
627                return 0;
628            }
629            if (count_insecure_elgkeys ())
630                DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_ELGWARN, glob_hwnd,
631                                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);
638        keyring_check_last_access (); /* init */
639        while (GetMessage (&msg, hwnd, 0, 0)) {
640            if (!TranslateAccelerator (msg.hwnd, accel_tab, &msg)) {
641                TranslateMessage (&msg);
642                DispatchMessage (&msg);
643            }
644        }
645            
646        return 0;
647    }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26