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

Legend:
Removed from v.34  
changed lines
  Added in v.328

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26