/[winpt]/trunk/Src/WinPT.cpp
ViewVC logotype

Contents of /trunk/Src/WinPT.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 14 - (show annotations)
Wed May 4 07:59:42 2005 UTC (19 years, 9 months ago) by twoaday
File size: 14496 byte(s)
2005-05-03  Timo Schulz  <twoaday@freakmail.de>
 
        * wptFileManager.cpp (fm_parse_command_line): Handle
        'SYMKEYENC' files. Thanks to the user who reported it.


1 /* WinPT.cpp - Windows Privacy Tray (WinPT)
2 * Copyright (C) 2000-2005 Timo Schulz
3 *
4 * This file is part of WinPT.
5 *
6 * 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
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * WinPT is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with WinPT; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20 #include <windows.h>
21
22 #include "../resource.h"
23 #include "wptTypes.h"
24 #include "wptW32API.h"
25 #include "wptVersion.h"
26 #include "wptErrors.h"
27 #include "wptGPG.h"
28 #include "wptRegistry.h"
29 #include "wptCommonCtl.h"
30 #include "wptDlgs.h"
31 #include "wptNLS.h"
32 #include "wptKeyserver.h"
33 #include "wptCard.h"
34 #include "wptFileManager.h"
35 #include "wptContext.h"
36
37 HINSTANCE glob_hinst; /* global instance for the dialogs */
38 HWND glob_hwnd; /* global window handle for the dialogs */
39 HWND activ_hwnd;
40 LOCK mo_file;
41 int scard_support = 0;
42 int debug = 0;
43 int mobile = 0;
44 int gpg_read_only = 0;
45 char gpgver[3];
46
47 /* Internal IPC */
48 int start_keymanager = 0;
49
50 static void
51 update_keycache (HWND hwnd)
52 {
53 refresh_cache_s rcs = {0};
54 rcs.kr_reload = 0;
55 rcs.kr_update = 1;
56 rcs.tr_update = 1;
57 DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_KEYCACHE, hwnd,
58 keycache_dlg_proc, (LPARAM)&rcs);
59 } /* update_keycache */
60
61
62 static char *
63 get_gettext_lang (void)
64 {
65 char * fname;
66 fname = get_reg_entry_mo ();
67 if (!fname)
68 return NULL;
69 return fname;
70 } /* get_gettext_lang */
71
72
73 static void
74 load_gettext (void)
75 {
76 char * nls = NULL;
77 char * file = NULL;
78
79 nls = get_gettext_lang ();
80 if (nls) {
81 set_gettext_file ("winpt", nls);
82 file = make_filename (nls, "winpt", "mo");
83 if (!file_exist_check (nls) && init_file_lock (&mo_file, file)) {
84 msg_box (NULL, _("Could not initizalize file lock.\n"
85 "Native Language Support"),
86 _("WinPT Error"), MB_ERR);
87 }
88 free_if_alloc (nls);
89 free_if_alloc (file);
90 }
91 } /* load_gettext */
92
93
94 /* check if the default key from the gpg.conf file is available in the
95 keyring. if not, bail out because encryption won't work properly then. */
96 static int
97 check_default_key (gpgme_keycache_t kc)
98 {
99 gpgme_key_t key;
100 gpgme_error_t err = GPGME_No_Error;
101 char * defkey;
102
103 defkey = get_gnupg_default_key ();
104 if (defkey)
105 err = gpgme_keycache_find_key (kc, defkey, 0, &key);
106 free_if_alloc (defkey);
107 return err? -1 : 0;
108 } /* check_default_key */
109
110
111 /* Return the WinPT program file name (with full pathname). */
112 static const char *
113 get_prog_part (const char * fname, int use_cwd)
114 {
115 static char program[1024];
116 char currdir[256], * cmd = NULL;
117 int j;
118
119 memset (currdir, 0, DIM (currdir));
120 memset (program, 0, DIM (program));
121
122 if (use_cwd) {
123 GetCurrentDirectory (DIM (currdir)-1, currdir);
124 _snprintf (program, DIM (program)-1, "%s\\%s", currdir, fname);
125 }
126 else {
127 cmd = GetCommandLine ();
128 if (cmd == NULL)
129 return NULL;
130 strncpy (currdir, cmd, 255);
131 j = strlen (currdir);
132 while (j--) {
133 if (currdir[j] == '\\')
134 break;
135 }
136 currdir[j] = 0;
137 _snprintf (program, DIM (program)-1, "%s\\%s", currdir + 1, fname);
138 }
139 return program;
140 } /* get_prog_part */
141
142
143 static int
144 check_crypto_engine (void)
145 {
146 int ma=1, mi=2, pa=4; /* GPG 1.2.4 */
147 int rc;
148
149 rc = check_gnupg_engine (&ma, &mi, &pa);
150 if (rc == -1) {
151 msg_box (NULL, _("Could not read GnuPG version."), _("WinPT Error"), MB_ERR);
152 return rc;
153 }
154 else if (rc) {
155 log_box (_("WinPT Error"), MB_ERR,
156 _("Sorry, you need a newer GPG version.\n"
157 "GPG version %d.%d.%d requred GPG version 1.2.4"),
158 ma, mi, pa);
159 return rc;
160 }
161 /* We enable smartcard support for GPG: 1.9.x or >= 1.4.0 */
162 if (ma >= 1 && mi >= 4)
163 scard_support = 1;
164
165 gpgver[0] = ma;
166 gpgver[1] = mi;
167 gpgver[2] = pa;
168 return rc;
169 } /* check_crypto_engine */
170
171
172 static int
173 load_keyserver_conf (int quiet)
174 {
175 const char * t;
176 int rc;
177
178 if (reg_prefs.kserv_conf)
179 t = reg_prefs.kserv_conf;
180 else if (!file_exist_check (get_prog_part ("keyserver.conf", 0)))
181 t = get_prog_part ("keyserver.conf", 0);
182 else
183 t = "keyserver.conf";
184 rc = kserver_load_conf (t);
185 if (rc && !quiet)
186 msg_box (NULL, winpt_strerror (rc), _("Keyserver"), MB_ERR);
187 return rc;
188 }
189
190
191 static void
192 enable_mobile_mode (void)
193 {
194 memset (&reg_prefs, 0, sizeof (reg_prefs));
195 reg_prefs.always_trust = 0;
196 reg_prefs.auto_backup = 0;
197 reg_prefs.cache_time = 0;
198 reg_prefs.expert = 0;
199 reg_prefs.keylist_mode = 1;
200 reg_prefs.kserv_conf = m_strdup ("keyserver.conf");
201 reg_prefs.no_zip_mmedia = 1;
202 reg_prefs.use_tmpfiles = 1;
203 reg_prefs.word_wrap = 80;
204 reg_prefs.use_viewer = 0; /* XXX */
205 }
206
207 char* get_subkey_fingerprint (gpgme_ctx_t ctx, const char *keyid);
208
209 int WINAPI
210 #ifndef WINPT_IPC
211 WinMain (HINSTANCE hinst, HINSTANCE hprev, LPSTR cmdline, int showcmd)
212 #else
213 win_main (HINSTANCE hinst, HINSTANCE hprev, LPSTR cmdline, int showcmd)
214 #endif
215 {
216 WNDCLASS wc = {0, winpt_main_proc, 0, 0, hinst, 0, 0, 0, 0, PGM_NAME};
217 HACCEL accel_tab;
218 int rc, ec, created = 0, use_cwd = 0, nfiles = 0;
219 int first_start = 0, start_gpgprefs = 0;
220 const char * s;
221 MSG msg;
222 HWND hwnd;
223
224 glob_hinst = hinst;
225
226 gpgme_lib_init ();
227 #ifdef _DEBUG
228 gpgme_set_debug_mode (1);
229 #endif
230 gpgme_set_pgm_string ("WinPT "PGM_VERSION);
231
232 s = PTD_get_version ();
233 if (strcmp (s, "0.8.0")) {
234 log_box (_("Privacy Tray Dynamic (PTD)"), MB_ERR,
235 _("Please update your PTD.dll to the newest version, "
236 "the version (%s) you use is too old."), s);
237 return 0;
238 }
239
240 if (gpg_md_selftest ()) {
241 msg_box (NULL, _("Cryptographic selftest failed."),
242 _("WinPT Error"), MB_ERR);
243 return 0;
244 }
245
246 if (cmdline && stristr (cmdline, "--mobile"))
247 mobile = 1;
248
249 set_default_kserver ();
250
251 if (!mobile) {
252 regist_inst_gnupg (1);
253 regist_inst_winpt (1, &created);
254 }
255 else {
256 enable_mobile_mode ();
257 /* XXX: ask for GPG path */
258 created = 1; /* Disable registry writing */
259 }
260
261 if (!created) {
262 memset (&reg_prefs, 0, sizeof (reg_prefs));
263 reg_prefs.use_tmpfiles = 1; /* default */
264 reg_prefs.fm.progress = 0; /* XXX: fix the bug and enable it again */
265 get_reg_winpt_prefs (&reg_prefs);
266 if (!reg_prefs.no_hotkeys)
267 hotkeys_modify ();
268 }
269
270 rc = gnupg_check_homedir ();
271 if (rc) {
272 log_box (_("WinPT Error"), MB_ERR,
273 _("GPG home directory is not set correctly.\n"
274 "Please check the GPG registry settings:\n%s."),
275 winpt_strerror (rc));
276 const char * s = get_filename_dlg (GetActiveWindow (), FILE_OPEN,
277 _("Select GPG Public Keyring"),
278 _("GPG Keyrings (*.gpg)\0*.gpg\0\0"),
279 NULL);
280 if (s && !file_exist_check (s))
281 {
282 size_t n;
283 char * p = strrchr (s, '\\');
284 if (!p)
285 BUG (0);
286 n = p - s;
287 if (n)
288 {
289 char * file = new char[n+1];
290 if (!file)
291 BUG (NULL);
292 memset (file, 0, n);
293 memcpy (file, s, n);
294 file[n] = '\0';
295 set_reg_entry_gpg ("HomeDir", file);
296 free_if_alloc (file);
297 gnupg_check_homedir (); /* change gpgProgram if needed */
298 }
299 }
300 else {
301 msg_box (NULL, _("GPG home directory could not be determited."),
302 _("WinPT Error"), MB_ERR);
303 goto start;
304 }
305 }
306
307 rc = check_gnupg_prog ();
308 if (rc) {
309 if (msg_box (NULL, _("Could not find the GPG binary (gpg.exe).\n"
310 "Do you want to start the GPG preferences to "
311 "correct this problem?"), _("WinPT Error"),
312 MB_INFO|MB_YESNO) == IDYES)
313 start_gpgprefs = 1;
314 else
315 {
316 msg_box (NULL, winpt_strerror (rc), _("WinPT Error"), MB_ERR);
317 return 0;
318 }
319 }
320
321 rc = gnupg_access_files ();
322 if (!start_gpgprefs && rc)
323 {
324 if (rc == WPTERR_GPG_KEYRINGS || rc == WPTERR_GPG_OPT_KEYRINGS)
325 {
326 ec = msg_box (NULL,
327 _("Could not access and/or find the public and secret keyring.\n"
328 "If this is an accident, quit the program and fix it.\n\n"
329 "Continue if you want that WinPT offers you more choices.\n"),
330 "WinPT", MB_INFO|MB_YESNO);
331 if (ec == IDYES)
332 first_start = 1;
333 }
334 if (!first_start)
335 {
336 msg_box (NULL, winpt_strerror (rc), _("WinPT Error"), MB_ERR);
337 return 0;
338 }
339 }
340
341 if (!first_start)
342 {
343 rc = gpg_check_permissions (1);
344 if (rc && rc == 2)
345 gpg_read_only = 1;
346 else if (rc)
347 return 0;
348 }
349
350 load_gettext ();
351 init_gnupg_table ();
352
353 nfiles = fm_parse_command_line (cmdline);
354 if (nfiles > 0)
355 return 0;
356
357 if (cmdline && stristr (cmdline, "--wipe-freespace")) {
358 dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_SPACE_SECDEL,
359 GetDesktopWindow(), space_wipefrees_dlg_proc, NULL,
360 _("Wipe Free Space"), IDS_WINPT_SPACE_SECDEL);
361 free_gnupg_table ();
362 return 0;
363 }
364
365 load_keyserver_conf (cmdline? 1 : 0);
366 if (start_keymanager) {
367 dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_KEYMISC,
368 GetDesktopWindow(), keymanager_dlg_proc, NULL,
369 _("Key Manager"), IDS_WINPT_KEYMISC);
370 keycache_release ();
371 free_gnupg_table ();
372 return 0;
373 }
374
375 if (cmdline && (stristr (cmdline, "--keymanager")
376 || stristr (cmdline, "--cardmanager"))) {
377 update_keycache (GetDesktopWindow ());
378 if (stristr (cmdline, "keymanager"))
379 dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_KEYMISC,
380 GetDesktopWindow(), keymanager_dlg_proc, NULL,
381 _("Key Manager"), IDS_WINPT_KEYMISC);
382 else {
383 gpgme_card_t crd = smartcard_init ();
384 if (crd)
385 dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_CARD_EDIT,
386 GetDesktopWindow(), card_edit_dlg_proc,
387 (LPARAM)crd, _("Card Manager"),
388 IDS_WINPT_CARD_EDIT);
389 gpgme_card_release (crd);
390 }
391 keycache_release ();
392 free_gnupg_table ();
393 return 0;
394 }
395
396 CreateMutex (NULL, TRUE, PGM_NAME);
397 if (GetLastError () == ERROR_ALREADY_EXISTS) {
398 free_gnupg_table ();
399 return 0;
400 }
401
402 if (cmdline) {
403 if (stristr (cmdline, "--enable-debug") || stristr (cmdline, "--debug")) {
404 gpgme_set_debug_mode (1);
405 winpt_debug_msg ();
406 debug = 1;
407 }
408 }
409
410 wc.hIcon = LoadIcon (glob_hinst, MAKEINTRESOURCE (IDI_WINPT));
411 rc = RegisterClass (&wc);
412 if (rc == FALSE) {
413 msg_box (NULL, _("Could not register window class"), _("WinPT Error"), MB_ERR);
414 free_gnupg_table ();
415 return 0;
416 }
417
418 hwnd = CreateWindow (PGM_NAME,
419 PGM_NAME,
420 0, 0, 0, 0, 0,
421 NULL,
422 NULL,
423 hinst,
424 NULL);
425 if (hwnd == NULL) {
426 msg_box (NULL, _("Could not create window"), _("WinPT Error"), MB_ERR);
427 free_gnupg_table ();
428 return 0;
429 }
430 glob_hwnd = hwnd;
431 UpdateWindow (hwnd);
432
433 if (!first_start && !start_gpgprefs) {
434 gnupg_backup_options (1);
435 gnupg_backup_options (0);
436
437 rc = check_crypto_engine ();
438 if (rc) {
439 DestroyWindow (hwnd);
440 free_gnupg_table ();
441 return 0;
442 }
443 }
444
445 if (start_gpgprefs)
446 {
447 char *ring;
448 size_t size = 0;
449 DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_GPGPREFS, hwnd,
450 gpgprefs_dlg_proc, NULL);
451 ring = get_gnupg_keyring (0, !NO_STRICT);
452 if (gnupg_access_keyring (0) == -1 && get_file_size (ring) == 0)
453 first_start = 1; /* The keyring is empty! */
454 free_if_alloc (ring);
455 }
456
457 if (first_start) {
458 struct key_wizard_s c, dummy;
459 start:
460 DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_FIRST, hwnd,
461 first_run_dlg_proc, (LPARAM)&dummy);
462 switch (dummy.interactive)
463 {
464 case SETUP_KEYGEN:
465 c.interactive = 1;
466 rc = DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_KEYWIZARD,
467 hwnd, keygen_wizard_dlg_proc, (LPARAM)&c);
468 if (!rc)
469 goto start;
470 break;
471
472 case SETUP_IMPORT:
473 rc = gnupg_copy_keyrings ();
474 if (rc) {
475 msg_box (hwnd, winpt_strerror (rc), _("WinPT Error"), MB_ERR);
476 goto start;
477 }
478 break;
479
480 case SETUP_EXISTING:
481 DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_GPGPREFS, hwnd,
482 gpgprefs_dlg_proc, NULL);
483 break;
484
485 case -1:
486 DestroyWindow (hwnd);
487 free_gnupg_table ();
488 return 0;
489 }
490 update_keycache (hwnd);
491 check_crypto_engine ();
492 }
493 else {
494 gpgme_keycache_t c;
495 update_keycache (hwnd);
496 c = keycache_get_ctx (1);
497 if (!c || !gpgme_keycache_count (c)) {
498 gnupg_display_error ();
499 msg_box (hwnd, _("The keycache was not initialized or is empty.\n"
500 "Please check your GPG config (keyrings, pathes...)"),
501 _("WinPT Error"), MB_ERR);
502 ec = msg_box (NULL, _("It seems that GPG is not set properly.\n"
503 "Do you want to start the GPG preferences dialog?"),
504 "WinPT", MB_INFO|MB_YESNO);
505 if (ec == IDYES) {
506 DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_GPGPREFS, hwnd,
507 gpgprefs_dlg_proc, NULL);
508 update_keycache (hwnd);
509 }
510 else {
511 DestroyWindow (hwnd);
512 free_gnupg_table ();
513 return 0;
514 }
515 }
516 if (check_default_key (c)) {
517 char * p = get_gnupg_default_key ();
518 log_box (_("WinPT Error"), MB_ERR,
519 _("Default key from the GPG options file could not be found.\n"
520 "Please check your gpg.conf (options) to correct this:\n\n"
521 "%s: public key not found."), p? p : "[null]");
522 free_if_alloc (p);
523 DestroyWindow (hwnd);
524 free_gnupg_table ();
525 return 0;
526 }
527 if (count_insecure_elgkeys ())
528 DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_ELGWARN, glob_hwnd,
529 elgamal_warn_dlg_proc, NULL);
530 }
531
532 accel_tab = LoadAccelerators (glob_hinst, (LPCTSTR)IDR_WINPT_ACCELERATOR);
533 keyring_check_last_access (); /* init */
534 while (GetMessage (&msg, hwnd, 0, 0)) {
535 if (!TranslateAccelerator (msg.hwnd, accel_tab, &msg)) {
536 TranslateMessage (&msg);
537 DispatchMessage (&msg);
538 }
539 }
540
541 return 0;
542 } /* WinMain */

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26