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

Legend:
Removed from v.30  
changed lines
  Added in v.102

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26