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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26