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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26