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

Annotation of /trunk/Src/WinPT.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 28 - (hide annotations)
Thu Oct 20 12:35:59 2005 UTC (19 years, 4 months ago) by twoaday
File size: 14547 byte(s)
Minor cleanups and prepare the translation.

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26