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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26