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

Annotation of /trunk/Src/wptRegistry.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 36 - (hide annotations)
Thu Oct 27 15:25:13 2005 UTC (19 years, 4 months ago) by werner
File size: 16844 byte(s)
First set of changes to use autotools for building.
1 werner 36 /* wptRegistry.cpp - W32 Registry access
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
7     * modify it under the terms of the GNU General Public License
8     * as published by the Free Software Foundation; either version 2
9     * of the License, or (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 GNU
14     * 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    
21     #ifdef HAVE_CONFIG_H
22     #include <config.h>
23     #endif
24    
25     #include <windows.h>
26     #include <windows.h>
27     #include <stdio.h>
28    
29     #include "wptErrors.h"
30     #include "wptW32API.h"
31     #include "wptGPG.h"
32     #include "wptRegistry.h"
33     #include "wptKeyserver.h"
34     #include "wptTypes.h"
35     #include "wptNLS.h"
36     #include "wptVersion.h"
37    
38     #define rc_ok(rc) ((rc) == ERROR_SUCCESS)
39    
40     struct reg_hotkey_s reg_hotkeys[] = {
41     {"ClipEncrypt", NULL, 0},
42     {"ClipDecrypt", NULL, 0},
43     {"ClipSign", NULL, 0},
44     {"ClipSignEnc", NULL, 0},
45     {"CwsEncrypt", NULL, 0},
46     {"CwsDecrypt", NULL, 0},
47     {"CwsSign", NULL, 0},
48     {"CwsSignEnc", NULL, 0},
49     {0}
50     };
51    
52     winpt_reg_prefs_s reg_prefs;
53    
54     #define WINPT_REG "Software\\WinPT"
55    
56    
57     void
58     free_reg_prefs (void)
59     {
60     free_if_alloc (reg_prefs.backup.path);
61     free_if_alloc (reg_prefs.kserv_conf);
62     free_if_alloc (reg_prefs.homedir);
63     memset (&reg_prefs, 0, sizeof reg_prefs);
64     } /* free_reg_prefs */
65    
66    
67     /* Register the given WinPT filetype. */
68     static int
69     regist_single_filetype (gpg_filetype *gfile)
70     {
71     char icon[256], prog[256];
72    
73     memset (&icon, 0, sizeof (icon));
74     GetModuleFileName (glob_hinst, prog, sizeof (prog)-1);
75     _snprintf (icon, sizeof (icon) -1, "%s,%d", prog, gfile->nicon);
76     return create_file_type (prog, gfile->ext, gfile->descr, icon);
77     } /* regist_single_filetype */
78    
79    
80     /* Install the GPG related into the W32 resgistry, if the entry already
81     exists the function returns immediately. */
82     int
83     regist_inst_gnupg (int create_mokey)
84     {
85     int rc;
86     HKEY reg;
87    
88     rc = RegOpenKeyEx( HKEY_CURRENT_USER, "Software\\GNU\\GnuPG", 0, KEY_READ, &reg );
89     if( rc_ok( rc ) ) {
90     RegCloseKey( reg );
91     return 0;
92     }
93     rc = RegCreateKey( HKEY_CURRENT_USER, "Software\\GNU\\GnuPG", &reg );
94     if( !rc_ok( rc ) )
95     return WPTERR_REGISTRY;
96     RegCloseKey( reg );
97     if( create_mokey ) {
98     rc = RegOpenKeyEx( HKEY_CURRENT_USER, "Control Panel\\MingW32\\NLS", 0, KEY_READ, &reg );
99     if( rc_ok( rc ) ) {
100     RegCloseKey( reg );
101     return 0;
102     }
103     rc = RegCreateKey( HKEY_CURRENT_USER, "Control Panel\\MingW32\\NLS", &reg );
104     if( !rc_ok( rc ) )
105     return WPTERR_REGISTRY;
106     RegCloseKey( reg );
107     }
108    
109     return 0;
110     } /* regist_inst_gpg */
111    
112    
113     /* Install WinPT into the W32 registry, if the entry already
114     exists the function returns immediately.*/
115     int
116     regist_inst_winpt (int with_ext, int * created)
117     {
118     HKEY reg;
119     char * p = NULL;
120     int rc, i, id, n = 0;
121    
122     if( created )
123     *created = 0;
124    
125     p = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, "Extensions");
126     if (p && *p == '1')
127     with_ext = 0;
128     free_if_alloc( p );
129    
130     if( with_ext ) {
131     id = msg_box( NULL, _("WinPT can register some GPG file types for you so they can "
132     "be processed with a double click in the explorer.\n"
133     "Do you want to continue?"), _("WinPT"), MB_YESNO|MB_INFO );
134     if( id == IDNO ) {
135     set_reg_entry( HKEY_CURRENT_USER, WINPT_REG, "Extensions", "1" );
136     goto start;
137     }
138     for( i = 0; gpg_filetypes[i].ext; i++ ) {
139     rc = RegOpenKeyEx( HKEY_CLASSES_ROOT, gpg_filetypes[i].ext, 0, KEY_READ, &reg );
140     if( rc_ok( rc ) ) {
141     RegCloseKey( reg );
142     id = log_box( _("WinPT WARNING"), MB_YESNO|MB_INFO,
143     _("It seems there was already a '%s' file type registered by another application.\n"
144     "Do you want to overwrite it?"), gpg_filetypes[i].ext );
145     if( id == IDNO )
146     continue;
147     }
148     regist_single_filetype( &gpg_filetypes[i] );
149     n++;
150     }
151     }
152    
153     start:
154     rc = RegOpenKeyEx( HKEY_CURRENT_USER, WINPT_REG, 0, KEY_READ, &reg );
155     if( rc_ok( rc ) ) {
156     RegCloseKey( reg );
157     rc = RegOpenKeyEx( HKEY_CURRENT_USER, WINPT_REG"\\Keyserver", 0, KEY_READ, &reg );
158     if( !rc_ok( rc ) ) {
159     RegCreateKey( HKEY_CURRENT_USER, WINPT_REG"\\Keyserver", &reg );
160     RegCloseKey( reg );
161     }
162     p = get_reg_entry_keyserver ("Default");
163     if (!p) {
164     char buf[16];
165     sprintf (buf, "%d", HKP_PORT);
166     set_reg_entry_keyserver ("Default_Port", buf);
167     set_reg_entry_keyserver ("Default", DEF_HKP_KEYSERVER);
168     }
169     free_if_alloc( p );
170     if( n )
171     set_reg_entry( HKEY_CURRENT_USER, WINPT_REG, "Extensions", "1" );
172     return 0;
173     }
174     rc = RegCreateKey( HKEY_CURRENT_USER, WINPT_REG, &reg );
175     if( !rc_ok( rc ) )
176     return WPTERR_REGISTRY;
177     if( created )
178     *created = 1;
179     RegCloseKey( reg );
180     if( n )
181     set_reg_entry( HKEY_CURRENT_USER, WINPT_REG, "Extensions", "1" );
182     return 0;
183     } /* regist_inst_winpt */
184    
185    
186     /* Create a new filetype in the W32 registry.
187     We should really care of errors! Otherwise we can damage the registry! */
188     int
189     create_file_type( const char *exefile, const char *ext, const char *extname, char *iconfile )
190     {
191     int rc;
192     HKEY reg = NULL;
193     char deficon[256], defexec[256], p_exefile[256];
194    
195    
196     rc = RegCreateKey( HKEY_CLASSES_ROOT, ext, &reg );
197     if( rc_ok( rc ) )
198     rc = RegSetValueEx( reg, NULL, 0, REG_SZ, (byte *)extname, strlen( extname ) );
199     if( rc_ok( rc ) )
200     rc = RegCloseKey( reg );
201     if( rc_ok( rc ) )
202     rc = RegCreateKey( HKEY_CLASSES_ROOT, extname, &reg );
203     if( rc_ok( rc ) )
204     rc = RegSetValueEx( reg, NULL, 0, REG_SZ, (byte *) extname, strlen( extname ) );
205     if( rc_ok( rc ) )
206     rc = RegCloseKey( reg );
207     if( !rc_ok( rc ) ) {
208     rc = WPTERR_REGISTRY;
209     goto leave;
210     }
211    
212     memset( &deficon, 0, sizeof deficon );
213     _snprintf( deficon, sizeof deficon - 1, "%s\\DefaultIcon", extname );
214     memset( &defexec, 0, sizeof defexec );
215     _snprintf( defexec, sizeof defexec - 1, "%s\\shell\\open\\command", extname );
216     memset( &p_exefile, 0, sizeof p_exefile );
217     _snprintf( p_exefile, sizeof p_exefile - 1, "%s %%1", exefile );
218    
219     rc = RegCreateKey( HKEY_CLASSES_ROOT, deficon, &reg );
220     if( rc_ok( rc ) )
221     rc = RegSetValueEx(reg, NULL, 0, REG_SZ, (byte *)iconfile, strlen( iconfile ) );
222     if( rc_ok( rc ) )
223     rc = RegCloseKey( reg );
224     if( rc_ok( rc ) )
225     rc = RegCreateKey( HKEY_CLASSES_ROOT, defexec, &reg );
226     if( rc_ok( rc ) )
227     rc = RegSetValueEx( reg, NULL, 0, REG_SZ, (byte *)p_exefile, strlen( exefile ) );
228     if( rc_ok( rc ) )
229     rc = RegCloseKey( reg );
230     if( !rc_ok( rc ) ) {
231     rc = WPTERR_REGISTRY;
232     goto leave;
233     }
234    
235     leave:
236     if( reg )
237     RegCloseKey( reg );
238     return rc;
239     } /* create_file_type */
240    
241    
242     /* Expand a string with %foo% entries so that %foo% will
243     be replaced with its actual value. */
244     static char *
245     expand_path (const char *path)
246     {
247     DWORD len;
248     char *p;
249    
250     len = ExpandEnvironmentStrings (path, NULL, 0);
251     if (!len)
252     return NULL;
253     len += 1;
254     p = new char[len+1];
255     if( !p )
256     return NULL;
257     len = ExpandEnvironmentStrings (path, p, len);
258     if (!len) {
259     free_if_alloc (p);
260     return NULL;
261     }
262     return p;
263     } /* expand_path */
264    
265    
266     /* Retrieve a registry entry with the directory given in @dir
267     and the key given in @key.
268     Return value is the value or NULL otherwise. */
269     char*
270     get_reg_entry (HKEY root_key, const char * dir, const char * key)
271     {
272     int rc;
273     char text[384] = {0};
274     DWORD nbytes, type, n1 = 0;
275     HKEY reg_key = NULL;
276     char * p = NULL, * tmp = NULL;
277    
278     rc = RegOpenKeyEx (root_key, dir, 0, KEY_QUERY_VALUE, &reg_key);
279     if( !rc_ok( rc ) )
280     goto leave;
281     nbytes = sizeof (text) - 1;
282     type = REG_SZ;
283    
284     rc = RegQueryValueEx (reg_key, key, 0, &type, (BYTE *)&text, &nbytes);
285     if (!rc_ok (rc) || !nbytes)
286     goto leave;
287    
288     if (type == REG_EXPAND_SZ && strchr (text, '%'))
289     p = expand_path (text);
290     else {
291     p = new char[nbytes + 1];
292     if (!p)
293     BUG (0);
294     memcpy (p, text, nbytes);
295     p[nbytes] = '\0';
296     }
297    
298     leave:
299     if (reg_key)
300     RegCloseKey (reg_key);
301     return p;
302     }
303    
304    
305     /* XXX: use REG_EXPAND_SZ to support multi user environments.
306     keep this type and do not overwrite it with REG_SZ. */
307    
308     /* Set a registry entry. */
309     int
310     set_reg_entry (HKEY root_key, const char *dir, const char *key,
311     const char *value)
312     {
313     int rc = 0;
314     HKEY reg_key;
315    
316     rc = RegOpenKeyEx( root_key, dir, 0, KEY_WRITE, &reg_key );
317     if( !rc_ok( rc ) )
318     return WPTERR_REGISTRY;
319     rc = RegSetValueEx( reg_key, key, 0, REG_SZ, (BYTE *)value, strlen( value ) );
320     if( !rc_ok( rc ) )
321     rc = WPTERR_REGISTRY;
322     RegCloseKey( reg_key );
323     return rc;
324     } /* set_reg_entry */
325    
326    
327     int
328     set_reg_key( HKEY root_key, const char * dir, const char * key,
329     const char * value )
330     {
331     int rc = 0;
332     HKEY reg_key;
333    
334     rc = RegOpenKeyEx( root_key, dir, 0, KEY_WRITE, &reg_key );
335     if( !rc_ok( rc ) )
336     return WPTERR_REGISTRY;
337    
338     rc = RegSetValueEx( reg_key, key, 0, REG_SZ, (BYTE *)value, strlen( value ) );
339     if( !rc_ok( rc ) ) {
340     if ( RegCreateKey( root_key, key, &reg_key ) != ERROR_SUCCESS ) {
341     rc = WPTERR_REGISTRY;
342     goto leave;
343     }
344     rc = RegSetValueEx( reg_key, key, 0, REG_SZ, (BYTE *)value, strlen( value ) );
345     if ( !rc_ok( rc ) )
346     rc = WPTERR_REGISTRY;
347     }
348    
349     leave:
350     RegCloseKey( reg_key );
351     return rc;
352     } /* set_reg_key */
353    
354    
355     int
356     set_reg_entry_gpg (const char * key, const char * value)
357     {
358     return set_reg_entry (HKEY_CURRENT_USER, "Software\\GNU\\GnuPG", key, value);
359     }
360    
361    
362     int
363     set_reg_entry_mo (const char * value)
364     {
365     return set_reg_entry (HKEY_CURRENT_USER, "Control Panel\\Mingw32\\NLS",
366     "MODir", value);
367     }
368    
369    
370     char *
371     get_reg_entry_gpg (const char *key)
372     {
373     return get_reg_entry (HKEY_CURRENT_USER, "Software\\GNU\\GnuPG", key);
374     }
375    
376    
377     char *
378     get_reg_entry_mo(void)
379     {
380     return get_reg_entry(HKEY_CURRENT_USER,
381     "Control Panel\\Mingw32\\NLS", "MODir");
382     }
383    
384    
385     /* All valid configuration commands. */
386     static const char * cfg [] = {
387     NULL,
388     "CacheTime",
389     "WordWrap",
390     "FastMode",
391     "Viewer",
392     "KeylistMode",
393     "WipeMode",
394     "AlwaysTrust",
395     "AutoBackup",
396     "BackupMode",
397     "DisableHotkeys",
398     "NoCompressMultiMedia",
399     "Expert",
400     "FMProgressBar",
401     };
402    
403    
404     int
405     set_reg_winpt_single (int id, int val)
406     {
407     char buf[64];
408     int rc;
409    
410     sprintf (buf, "%d", val);
411     rc = set_reg_entry (HKEY_CURRENT_USER, WINPT_REG, cfg[id], buf);
412     return rc;
413     }
414    
415    
416     int
417     get_reg_winpt_single (int id)
418     {
419     char * buf = NULL;
420     int val = 0;
421    
422     buf = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, cfg[id]);
423     if (buf && *buf != ' ')
424     val = 1;
425     else if (!buf)
426     val = -1;
427     free_if_alloc (buf);
428     return val;
429     }
430    
431    
432     /* Saves the winpt preferences in the registry. */
433     int
434     set_reg_winpt_prefs (winpt_reg_prefs_s * opt)
435     {
436     char buf[128];
437     int rc = 0, i;
438    
439     for (i=1; i < DIM (cfg); i++) {
440     switch (i) {
441     case CFG_CACHETIME:
442     sprintf (buf, "%d", opt->cache_time);
443     break;
444     case CFG_WORDWRAP:
445     sprintf (buf, "%d", opt->word_wrap);
446     break;
447     case CFG_WIPEMODE:
448     sprintf (buf, "%d", opt->wipe_mode);
449     break;
450     case CFG_FASTMODE:
451     sprintf (buf, "%d", opt->use_tmpfiles);
452     break;
453     case CFG_NOZIP_MMEDIA:
454     sprintf (buf, "%d", opt->no_zip_mmedia);
455     break;
456     case CFG_VIEWER:
457     sprintf (buf, "%d", opt->use_viewer);
458     break;
459     case CFG_KEYLISTMODE:
460     sprintf (buf, "%d", opt->keylist_mode);
461     break;
462     case CFG_ALWAYSTRUST:
463     sprintf (buf, "%d", opt->always_trust);
464     break;
465     case CFG_AUTOBACKUP:
466     sprintf (buf, "%d", opt->auto_backup);
467     break;
468     case CFG_AUTOBAKMODE:
469     sprintf (buf, "%d", opt->backup.mode);
470     break;
471     case CFG_DISHOTKEYS:
472     sprintf (buf, "%d", opt->no_hotkeys);
473     break;
474     case CFG_EXPERT:
475     sprintf (buf, "%d", opt->expert);
476     break;
477    
478     case CFG_FM_PROGRESS:
479     sprintf (buf, "%d", opt->fm.progress);
480     break;
481     }
482     rc = set_reg_entry (HKEY_CURRENT_USER, WINPT_REG, cfg[i], buf);
483     if (rc)
484     goto leave;
485     }
486    
487     if (opt->backup.path) {
488     rc = set_reg_entry (HKEY_CURRENT_USER, WINPT_REG, "BackupPath",
489     opt->backup.path);
490     if (rc)
491     goto leave;
492     }
493     if (opt->kserv_conf) {
494     rc = set_reg_entry (HKEY_CURRENT_USER, WINPT_REG, "KeyserverConfig",
495     opt->kserv_conf);
496     if (rc)
497     goto leave;
498     }
499    
500     for (i=0; reg_hotkeys[i].reg_entry; i++) {
501     strcpy (buf, " ");
502     if (reg_hotkeys[i].enabled)
503     strcpy (buf, reg_hotkeys[i].key);
504     rc = set_reg_key (HKEY_CURRENT_USER, WINPT_REG,
505     reg_hotkeys[i].reg_entry, buf);
506     if (rc)
507     break;
508     }
509    
510     leave:
511     if (rc) {
512     msg_box (NULL, _("Could not write to Registry."), _("Preferences"), MB_ERR);
513     return rc;
514     }
515     return 0;
516     }
517    
518    
519     int
520     set_reg_winpt_flag (const char * name, int val)
521     {
522     return set_reg_entry (HKEY_CURRENT_USER, WINPT_REG, name, val? "1" : "0");
523     } /* set_reg_winpt_flag */
524    
525    
526     int
527     get_reg_winpt_flag (const char * name)
528     {
529     char * buf;
530     int flag = 0;
531    
532     buf = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, name);
533     if (buf && buf[0] == '1')
534     flag = 1;
535     else if (!buf || buf && buf[0] != '0')
536     flag = -1;
537     free_if_alloc (buf);
538     return flag;
539     } /* get_reg_winpt_flag */
540    
541    
542     /* Retrieve the winpt preferences from the registry. */
543     int
544     get_reg_winpt_prefs (winpt_reg_prefs_s * opt)
545     {
546     char * val = NULL;
547     int i;
548    
549     for (i=1; i < DIM (cfg); i++) {
550     val = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, cfg[i]);
551     if (!val || *val == ' ') {
552     free_if_alloc (val);
553     continue;
554     }
555     switch (i) {
556     case CFG_CACHETIME:
557     opt->cache_time = atol (val);
558     /* We do NOT support passphrase caching for more than an hour.
559     * Perhaps we should allow it, but for now we silently drop this.
560     */
561     if (opt->cache_time > 3600)
562     opt->cache_time = 3600;
563     break;
564     case CFG_WORDWRAP:
565     opt->word_wrap = atol (val);
566     break;
567     case CFG_FASTMODE:
568     opt->use_tmpfiles = atol (val);
569     break;
570     case CFG_NOZIP_MMEDIA:
571     opt->no_zip_mmedia = atol (val);
572     break;
573     case CFG_VIEWER:
574     opt->use_viewer = atol (val);
575     break;
576     case CFG_KEYLISTMODE:
577     opt->keylist_mode = atol (val);
578     break;
579     case CFG_WIPEMODE:
580     opt->wipe_mode = atol (val);
581     break;
582     case CFG_DISHOTKEYS:
583     opt->no_hotkeys = atol (val);
584     break;
585     case CFG_ALWAYSTRUST:
586     opt->always_trust = atol (val);
587     break;
588     case CFG_AUTOBACKUP:
589     opt->auto_backup = atol (val);
590     break;
591     case CFG_AUTOBAKMODE:
592     opt->backup.mode = atol (val);
593     break;
594     case CFG_EXPERT:
595     opt->expert = atol (val);
596     break;
597    
598     case CFG_FM_PROGRESS:
599     opt->fm.progress = atol (val);
600     break;
601     }
602     free_if_alloc (val);
603     }
604    
605     val = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, "BackupPath");
606     if (val && val[0] != ' ')
607     opt->backup.path = m_strdup (val);
608     free_if_alloc (val);
609    
610     val = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, "KeyserverConfig");
611     if (val && val[0] != ' ')
612     opt->kserv_conf = m_strdup (val);
613     free_if_alloc (val);
614    
615     for (i=0; reg_hotkeys[i].reg_entry; i++) {
616     val = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, reg_hotkeys[i].reg_entry);
617     if (val && val[0] != ' ') {
618     reg_hotkeys[i].key[0] = *val;
619     reg_hotkeys[i].enabled = 1;
620     }
621     else
622     reg_hotkeys[i].enabled = 0;
623     free_if_alloc (val);
624     }
625     return 0;
626     } /* get_reg_winpt_prefs */
627    
628    
629     char*
630     get_reg_entry_keyserver (const char *name)
631     {
632     char * p = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG"\\Keyserver", name);
633     if (p && !strcmp (p, "")) {
634     free_if_alloc (p);
635     return NULL;
636     }
637     return p;
638     }
639    
640    
641     void
642     get_reg_proxy_prefs (char ** host, int * port, char ** user, char ** pass)
643     {
644     if (host)
645     *host = get_reg_entry_keyserver ("Host");
646     if (user)
647     *user = get_reg_entry_keyserver ("User");
648     if (pass)
649     *pass = get_reg_entry_keyserver ("Pass");
650     if (port) {
651     char * p = get_reg_entry_keyserver ("Port");
652     if (p) {
653     *port = atol (p);
654     free_if_alloc (p);
655     }
656     }
657     } /* get_reg_proxy_prefs */
658    
659    
660     int
661     set_reg_entry_keyserver( const char * name, const char * val )
662     {
663     return set_reg_entry( HKEY_CURRENT_USER, WINPT_REG"\\Keyserver", name, val );
664     } /* set_reg_entry_keyserver */
665    
666    
667     int
668     set_reg_proxy_prefs( const char * host, int port, const char * user, const char * pass )
669     {
670     int rc;
671    
672     rc = set_reg_entry_keyserver( "Host", host? host : "" );
673     if( !rc ) {
674     char buf[32];
675     sprintf( buf, "%d", port );
676     rc = set_reg_entry_keyserver( "Port", buf );
677     }
678     if( !rc )
679     rc = set_reg_entry_keyserver( "User", user? user : "" );
680     if( !rc )
681     rc = set_reg_entry_keyserver( "Pass", pass? pass : "" );
682     return rc;
683     } /* set_reg_proxy_prefs */

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26