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

Legend:
Removed from v.5  
changed lines
  Added in v.159

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26