1 |
/* wptRegistry.cpp - W32 Registry access |
/* wptRegistry.cpp - Windows Registry access |
2 |
* Copyright (C) 2000-2005 Timo Schulz |
* Copyright (C) 2000-2006 Timo Schulz |
3 |
* |
* |
4 |
* This file is part of WinPT. |
* This file is part of WinPT. |
5 |
* |
* |
17 |
* along with WinPT; if not, write to the Free Software Foundation, |
* along with WinPT; if not, write to the Free Software Foundation, |
18 |
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
*/ |
*/ |
|
|
|
20 |
#ifdef HAVE_CONFIG_H |
#ifdef HAVE_CONFIG_H |
21 |
#include <config.h> |
#include <config.h> |
22 |
#endif |
#endif |
23 |
|
|
24 |
|
/* For the case WinPT will be compiled in mobile mode, we use a wrapper |
25 |
|
to avoid registry access (read, write) and thus this file will not |
26 |
|
be used. */ |
27 |
|
#ifndef WINPT_MOBILE |
28 |
#include <windows.h> |
#include <windows.h> |
29 |
#include <stdio.h> |
#include <stdio.h> |
30 |
|
|
36 |
#include "wptTypes.h" |
#include "wptTypes.h" |
37 |
#include "wptNLS.h" |
#include "wptNLS.h" |
38 |
#include "wptVersion.h" |
#include "wptVersion.h" |
39 |
|
#include "wptHotkey.h" |
40 |
|
|
41 |
#define rc_ok(rc) ((rc) == ERROR_SUCCESS) |
#define rc_ok(rc) ((rc) == ERROR_SUCCESS) |
42 |
|
|
43 |
static gpg_filetype gpg_filetypes[] = { |
/* WinPT registry path. */ |
44 |
{"GPG Detached Signature", ".sig", 1}, |
#define WINPT_REG "Software\\WinPT" |
|
{"GPG Encrypted Data", ".gpg", 2}, |
|
|
{"GPG Armored Data", ".asc", 2}, |
|
|
{0} |
|
|
}; |
|
45 |
|
|
46 |
struct reg_hotkey_s reg_hotkeys[] = { |
/* GPG registry path. */ |
47 |
{"ClipEncrypt", "", 0}, |
#define GPG_REG "Software\\GNU\\GnuPG" |
48 |
{"ClipDecrypt", "", 0}, |
|
49 |
{"ClipSign", "", 0}, |
#define GPG_MO_REG "Control Panel\\MingW32\\NLS" |
50 |
{"ClipSignEnc", "", 0}, |
|
51 |
{"CwsEncrypt", "", 0}, |
/* GPG file association context. */ |
52 |
{"CwsDecrypt", "", 0}, |
struct gpg_filetype_s { |
53 |
{"CwsSign", "", 0}, |
const char *descr; /* description. */ |
54 |
{"CwsSignEnc", "", 0}, |
const char *ext; /* extension. */ |
55 |
{NULL, "", 0} |
int nicon; /* number of the image icon. */ |
56 |
}; |
}; |
57 |
|
typedef struct gpg_filetype_s *gpg_filetype_t; |
58 |
|
|
59 |
winpt_reg_prefs_s reg_prefs; |
/* Global WinPT registry prefereneces. */ |
60 |
|
struct winpt_prefs_s reg_prefs; |
61 |
|
|
|
#define WINPT_REG "Software\\WinPT" |
|
62 |
|
|
63 |
|
/* Return != 0 if GPG4win is installed. */ |
64 |
|
int |
65 |
|
is_gpg4win_installed (void) |
66 |
|
{ |
67 |
|
char *p; |
68 |
|
|
69 |
|
p = get_reg_entry_gpg4win (NULL); |
70 |
|
if (!p) |
71 |
|
return 0; |
72 |
|
if (dir_exist_check (p)) { |
73 |
|
free_if_alloc (p); |
74 |
|
return 0; |
75 |
|
} |
76 |
|
free_if_alloc (p); |
77 |
|
return -1; |
78 |
|
} |
79 |
|
|
80 |
|
|
81 |
|
/* Return != 0 if GPGee is installed. */ |
82 |
|
static int |
83 |
|
is_gpgee_installed (void) |
84 |
|
{ |
85 |
|
HKEY hk; |
86 |
|
LONG ec; |
87 |
|
|
88 |
|
ec = RegOpenKeyEx (HKEY_CURRENT_USER, "Software\\GPGee", 0, KEY_READ, &hk); |
89 |
|
if (ec == ERROR_SUCCESS) { |
90 |
|
RegCloseKey (hk); |
91 |
|
return -1; |
92 |
|
} |
93 |
|
|
94 |
|
return 0; |
95 |
|
} |
96 |
|
|
97 |
|
|
98 |
|
/* Free all members in the registry preference struct. */ |
99 |
void |
void |
100 |
free_reg_prefs (void) |
free_reg_prefs (void) |
101 |
{ |
{ |
103 |
free_if_alloc (reg_prefs.kserv_conf); |
free_if_alloc (reg_prefs.kserv_conf); |
104 |
free_if_alloc (reg_prefs.homedir); |
free_if_alloc (reg_prefs.homedir); |
105 |
memset (®_prefs, 0, sizeof reg_prefs); |
memset (®_prefs, 0, sizeof reg_prefs); |
106 |
} /* free_reg_prefs */ |
} |
|
|
|
107 |
|
|
|
/* Register the given WinPT filetype. */ |
|
|
static int |
|
|
regist_single_filetype (gpg_filetype *gfile) |
|
|
{ |
|
|
char icon[256], prog[256]; |
|
|
|
|
|
memset (&icon, 0, sizeof (icon)); |
|
|
GetModuleFileName (glob_hinst, prog, sizeof (prog)-1); |
|
|
_snprintf (icon, sizeof (icon) -1, "%s,%d", prog, gfile->nicon); |
|
|
return create_file_type (prog, gfile->ext, gfile->descr, icon); |
|
|
} /* regist_single_filetype */ |
|
108 |
|
|
109 |
|
|
110 |
/* Install the GPG related into the W32 resgistry, if the entry already |
/* Install the GPG related into the W32 resgistry, if the entry already |
115 |
int rc; |
int rc; |
116 |
HKEY reg; |
HKEY reg; |
117 |
|
|
118 |
rc = RegOpenKeyEx( HKEY_CURRENT_USER, "Software\\GNU\\GnuPG", 0, KEY_READ, ® ); |
rc = RegOpenKeyEx (HKEY_CURRENT_USER, GPG_REG, 0, KEY_READ, ®); |
119 |
if( rc_ok( rc ) ) { |
if (rc == ERROR_SUCCESS) { |
120 |
RegCloseKey( reg ); |
RegCloseKey (reg); |
121 |
return 0; |
return 0; |
122 |
} |
} |
123 |
rc = RegCreateKey( HKEY_CURRENT_USER, "Software\\GNU\\GnuPG", ® ); |
rc = RegCreateKey (HKEY_CURRENT_USER, GPG_REG, ®); |
124 |
if( !rc_ok( rc ) ) |
if (rc != ERROR_SUCCESS) |
125 |
return WPTERR_REGISTRY; |
return WPTERR_REGISTRY; |
126 |
RegCloseKey( reg ); |
RegCloseKey (reg); |
|
if( create_mokey ) { |
|
|
rc = RegOpenKeyEx( HKEY_CURRENT_USER, "Control Panel\\MingW32\\NLS", 0, KEY_READ, ® ); |
|
|
if( rc_ok( rc ) ) { |
|
|
RegCloseKey( reg ); |
|
|
return 0; |
|
|
} |
|
|
rc = RegCreateKey( HKEY_CURRENT_USER, "Control Panel\\MingW32\\NLS", ® ); |
|
|
if( !rc_ok( rc ) ) |
|
|
return WPTERR_REGISTRY; |
|
|
RegCloseKey( reg ); |
|
|
} |
|
|
|
|
|
return 0; |
|
|
} /* regist_inst_gpg */ |
|
127 |
|
|
128 |
|
if (!create_mokey) |
|
/* Install WinPT into the W32 registry, if the entry already |
|
|
exists the function returns immediately.*/ |
|
|
int |
|
|
regist_inst_winpt (int with_ext, int * created) |
|
|
{ |
|
|
HKEY reg; |
|
|
char * p = NULL; |
|
|
int rc, i, id, n = 0; |
|
|
|
|
|
if( created ) |
|
|
*created = 0; |
|
|
|
|
|
p = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, "Extensions"); |
|
|
if (p && *p == '1') |
|
|
with_ext = 0; |
|
|
free_if_alloc( p ); |
|
|
|
|
|
if( with_ext ) { |
|
|
id = msg_box( NULL, _("WinPT can register some GPG file types for you so they can " |
|
|
"be processed with a double click in the explorer.\n" |
|
|
"Do you want to continue?"), _("WinPT"), MB_YESNO|MB_INFO ); |
|
|
if( id == IDNO ) { |
|
|
set_reg_entry( HKEY_CURRENT_USER, WINPT_REG, "Extensions", "1" ); |
|
|
goto start; |
|
|
} |
|
|
for( i = 0; gpg_filetypes[i].ext; i++ ) { |
|
|
rc = RegOpenKeyEx( HKEY_CLASSES_ROOT, gpg_filetypes[i].ext, 0, KEY_READ, ® ); |
|
|
if( rc_ok( rc ) ) { |
|
|
RegCloseKey( reg ); |
|
|
id = log_box( _("WinPT WARNING"), MB_YESNO|MB_INFO, |
|
|
_("It seems there was already a '%s' file type registered by another application.\n" |
|
|
"Do you want to overwrite it?"), gpg_filetypes[i].ext ); |
|
|
if( id == IDNO ) |
|
|
continue; |
|
|
} |
|
|
regist_single_filetype( &gpg_filetypes[i] ); |
|
|
n++; |
|
|
} |
|
|
} |
|
|
|
|
|
start: |
|
|
rc = RegOpenKeyEx( HKEY_CURRENT_USER, WINPT_REG, 0, KEY_READ, ® ); |
|
|
if( rc_ok( rc ) ) { |
|
|
RegCloseKey( reg ); |
|
|
rc = RegOpenKeyEx( HKEY_CURRENT_USER, WINPT_REG"\\Keyserver", 0, KEY_READ, ® ); |
|
|
if( !rc_ok( rc ) ) { |
|
|
RegCreateKey( HKEY_CURRENT_USER, WINPT_REG"\\Keyserver", ® ); |
|
|
RegCloseKey( reg ); |
|
|
} |
|
|
p = get_reg_entry_keyserver ("Default"); |
|
|
if (!p) { |
|
|
char buf[16]; |
|
|
sprintf (buf, "%d", HKP_PORT); |
|
|
set_reg_entry_keyserver ("Default_Port", buf); |
|
|
set_reg_entry_keyserver ("Default", DEF_HKP_KEYSERVER); |
|
|
} |
|
|
free_if_alloc( p ); |
|
|
if( n ) |
|
|
set_reg_entry( HKEY_CURRENT_USER, WINPT_REG, "Extensions", "1" ); |
|
129 |
return 0; |
return 0; |
130 |
|
|
131 |
|
rc = RegOpenKeyEx (HKEY_CURRENT_USER, GPG_MO_REG, 0, KEY_READ, ®); |
132 |
|
if (rc == ERROR_SUCCESS) { |
133 |
|
RegCloseKey (reg); |
134 |
|
return 0; |
135 |
} |
} |
136 |
rc = RegCreateKey( HKEY_CURRENT_USER, WINPT_REG, ® ); |
rc = RegCreateKey (HKEY_CURRENT_USER, GPG_MO_REG, ®); |
137 |
if( !rc_ok( rc ) ) |
if (rc != ERROR_SUCCESS) |
138 |
return WPTERR_REGISTRY; |
return WPTERR_REGISTRY; |
139 |
if( created ) |
|
140 |
*created = 1; |
RegCloseKey (reg); |
|
RegCloseKey( reg ); |
|
|
if( n ) |
|
|
set_reg_entry( HKEY_CURRENT_USER, WINPT_REG, "Extensions", "1" ); |
|
141 |
return 0; |
return 0; |
142 |
} /* regist_inst_winpt */ |
} |
143 |
|
|
144 |
|
|
145 |
/* Create a new filetype in the W32 registry. |
/* Create a new filetype in the W32 registry. |
146 |
We should really care of errors! Otherwise we can damage the registry! */ |
We should really care of errors! Otherwise we can damage the registry! */ |
147 |
int |
static int |
148 |
create_file_type( const char *exefile, const char *ext, const char *extname, char *iconfile ) |
create_file_type (const char *exefile, const char *ext, |
149 |
|
const char *extname, char *iconfile) |
150 |
{ |
{ |
|
int rc; |
|
151 |
HKEY reg = NULL; |
HKEY reg = NULL; |
152 |
char deficon[256], defexec[256], p_exefile[256]; |
char deficon[MAX_PATH+1]; |
153 |
|
char defexec[MAX_PATH+1]; |
154 |
|
char p_exefile[MAX_PATH+1]; |
155 |
|
int rc; |
156 |
|
|
157 |
rc = RegCreateKey( HKEY_CLASSES_ROOT, ext, ® ); |
rc = RegCreateKey (HKEY_CLASSES_ROOT, ext, ®); |
158 |
if( rc_ok( rc ) ) |
if( rc_ok( rc ) ) |
159 |
rc = RegSetValueEx( reg, NULL, 0, REG_SZ, (byte *)extname, strlen( extname ) ); |
rc = RegSetValueEx( reg, NULL, 0, REG_SZ, (BYTE *)extname, strlen( extname ) ); |
160 |
if( rc_ok( rc ) ) |
if( rc_ok( rc ) ) |
161 |
rc = RegCloseKey( reg ); |
rc = RegCloseKey( reg ); |
162 |
if( rc_ok( rc ) ) |
if( rc_ok( rc ) ) |
163 |
rc = RegCreateKey( HKEY_CLASSES_ROOT, extname, ® ); |
rc = RegCreateKey( HKEY_CLASSES_ROOT, extname, ® ); |
164 |
if( rc_ok( rc ) ) |
if( rc_ok( rc ) ) |
165 |
rc = RegSetValueEx( reg, NULL, 0, REG_SZ, (byte *) extname, strlen( extname ) ); |
rc = RegSetValueEx( reg, NULL, 0, REG_SZ, (BYTE *) extname, strlen( extname ) ); |
166 |
if( rc_ok( rc ) ) |
if( rc_ok( rc ) ) |
167 |
rc = RegCloseKey( reg ); |
rc = RegCloseKey( reg ); |
168 |
if( !rc_ok( rc ) ) { |
if( !rc_ok( rc ) ) { |
171 |
} |
} |
172 |
|
|
173 |
memset( &deficon, 0, sizeof deficon ); |
memset( &deficon, 0, sizeof deficon ); |
174 |
_snprintf( deficon, sizeof deficon - 1, "%s\\DefaultIcon", extname ); |
_snprintf( deficon, DIM (deficon) - 1, "%s\\DefaultIcon", extname ); |
175 |
memset( &defexec, 0, sizeof defexec ); |
memset( &defexec, 0, sizeof defexec ); |
176 |
_snprintf( defexec, sizeof defexec - 1, "%s\\shell\\open\\command", extname ); |
_snprintf( defexec, DIM (defexec) - 1, "%s\\shell\\open\\command", extname ); |
177 |
memset( &p_exefile, 0, sizeof p_exefile ); |
memset( &p_exefile, 0, sizeof p_exefile ); |
178 |
_snprintf( p_exefile, sizeof p_exefile - 1, "%s %%1", exefile ); |
_snprintf( p_exefile, DIM (p_exefile) - 1, "%s %%1", exefile ); |
179 |
|
|
180 |
rc = RegCreateKey( HKEY_CLASSES_ROOT, deficon, ® ); |
rc = RegCreateKey( HKEY_CLASSES_ROOT, deficon, ® ); |
181 |
if( rc_ok( rc ) ) |
if( rc_ok( rc ) ) |
182 |
rc = RegSetValueEx(reg, NULL, 0, REG_SZ, (byte *)iconfile, strlen( iconfile ) ); |
rc = RegSetValueEx(reg, NULL, 0, REG_SZ, (BYTE *)iconfile, strlen( iconfile ) ); |
183 |
if( rc_ok( rc ) ) |
if( rc_ok( rc ) ) |
184 |
rc = RegCloseKey( reg ); |
rc = RegCloseKey( reg ); |
185 |
if( rc_ok( rc ) ) |
if( rc_ok( rc ) ) |
186 |
rc = RegCreateKey( HKEY_CLASSES_ROOT, defexec, ® ); |
rc = RegCreateKey( HKEY_CLASSES_ROOT, defexec, ® ); |
187 |
if( rc_ok( rc ) ) |
if( rc_ok( rc ) ) |
188 |
rc = RegSetValueEx( reg, NULL, 0, REG_SZ, (byte *)p_exefile, strlen( exefile ) ); |
rc = RegSetValueEx( reg, NULL, 0, REG_SZ, (BYTE *)p_exefile, strlen( exefile ) ); |
189 |
if( rc_ok( rc ) ) |
if( rc_ok( rc ) ) |
190 |
rc = RegCloseKey( reg ); |
rc = RegCloseKey( reg ); |
191 |
if( !rc_ok( rc ) ) { |
if( !rc_ok( rc ) ) { |
194 |
} |
} |
195 |
|
|
196 |
leave: |
leave: |
197 |
if( reg ) |
if (reg) |
198 |
RegCloseKey( reg ); |
RegCloseKey (reg); |
199 |
return rc; |
return rc; |
200 |
} /* create_file_type */ |
} |
201 |
|
|
202 |
|
|
203 |
|
/* Register the given WinPT filetype. */ |
204 |
|
static int |
205 |
|
regist_single_filetype (gpg_filetype_t gfile) |
206 |
|
{ |
207 |
|
char icon[MAX_PATH+1] = {0}; |
208 |
|
char prog[MAX_PATH+1] = {0}; |
209 |
|
|
210 |
|
if (!GetModuleFileName (glob_hinst, prog, DIM (prog)-1)) |
211 |
|
return WPTERR_REGISTRY; |
212 |
|
_snprintf (icon, DIM (icon) -1, "%s,%d", prog, gfile->nicon); |
213 |
|
return create_file_type (prog, gfile->ext, gfile->descr, icon); |
214 |
|
} |
215 |
|
|
216 |
|
/* Install WinPT into the W32 registry, if the entry already |
217 |
|
exists the function returns immediately. @with_ext can be |
218 |
|
used to register some file types (if 1). @created contains |
219 |
|
1 if the registry key was created. |
220 |
|
Return value: 0 on success. */ |
221 |
|
int |
222 |
|
regist_inst_winpt (int with_ext, int *created) |
223 |
|
{ |
224 |
|
struct gpg_filetype_s gpg_filetypes[] = { |
225 |
|
{_("GPG Detached Signature"), ".sig", 1}, |
226 |
|
{_("GPG Encrypted Data"), ".gpg", 2}, |
227 |
|
{_("GPG Armored Data"), ".asc", 2}, |
228 |
|
{0} |
229 |
|
}; |
230 |
|
HKEY reg; |
231 |
|
char *p = NULL; |
232 |
|
char modpath[MAX_PATH+1]; |
233 |
|
int rc, i, id, n; |
234 |
|
|
235 |
|
if (created) |
236 |
|
*created = 0; |
237 |
|
|
238 |
|
/* If GPGee is already installed deactivate file extension registering |
239 |
|
because it is very likely this would overwrite the GPGee settings. */ |
240 |
|
if (is_gpgee_installed ()) |
241 |
|
with_ext = 0; |
242 |
|
|
243 |
|
rc = RegOpenKeyEx (HKEY_CURRENT_USER, WINPT_REG, 0, KEY_READ, ®); |
244 |
|
if (rc == ERROR_SUCCESS) { |
245 |
|
RegCloseKey (reg); |
246 |
|
rc = RegOpenKeyEx (HKEY_CURRENT_USER, WINPT_REG"\\Keyserver", 0, KEY_READ, ®); |
247 |
|
if (rc != ERROR_SUCCESS) { |
248 |
|
rc = RegCreateKey (HKEY_CURRENT_USER, WINPT_REG"\\Keyserver", ®); |
249 |
|
if (rc == ERROR_SUCCESS) |
250 |
|
RegCloseKey (reg); |
251 |
|
} |
252 |
|
else |
253 |
|
RegCloseKey (reg); |
254 |
|
p = get_reg_entry_keyserver ("Default"); |
255 |
|
if (!p) { |
256 |
|
char buf[16]; |
257 |
|
|
258 |
|
_snprintf (buf, DIM (buf)-1, "%d", HKP_PORT); |
259 |
|
set_reg_entry_keyserver ("Default_Port", buf); |
260 |
|
set_reg_entry_keyserver ("Default", DEF_HKP_KEYSERVER); |
261 |
|
} |
262 |
|
free_if_alloc (p); |
263 |
|
return 0; |
264 |
|
} |
265 |
|
rc = RegCreateKey (HKEY_CURRENT_USER, WINPT_REG, ®); |
266 |
|
if (rc != ERROR_SUCCESS) |
267 |
|
return WPTERR_REGISTRY; |
268 |
|
if (created) |
269 |
|
*created = 1; |
270 |
|
RegCloseKey (reg); |
271 |
|
if (with_ext) { |
272 |
|
id = msg_box (NULL, _("WinPT can register some GPG file types for you so they can " |
273 |
|
"be processed with a double click in the explorer.\n" |
274 |
|
"Do you want to continue?"), _("WinPT"), MB_YESNO|MB_INFO); |
275 |
|
if (id == IDYES) { |
276 |
|
for (i = 0; gpg_filetypes[i].ext; i++) { |
277 |
|
rc = RegOpenKeyEx (HKEY_CLASSES_ROOT, gpg_filetypes[i].ext, 0, KEY_READ, ®); |
278 |
|
if (rc == ERROR_SUCCESS) { |
279 |
|
RegCloseKey (reg); |
280 |
|
id = log_box (_("WinPT WARNING"), MB_YESNO|MB_INFO, |
281 |
|
_("It seems there was already a '%s' file type registered by another application.\n" |
282 |
|
"Do you want to overwrite it?"), gpg_filetypes[i].ext); |
283 |
|
if (id == IDNO) |
284 |
|
continue; |
285 |
|
} |
286 |
|
regist_single_filetype (&gpg_filetypes[i]); |
287 |
|
} |
288 |
|
} |
289 |
|
} |
290 |
|
/* Store the install directory for later use. */ |
291 |
|
if ((n=GetModuleFileName (NULL, modpath, MAX_PATH-1)) > 0) { |
292 |
|
while (n-- > 0) { |
293 |
|
if (modpath[n] == '\\') { |
294 |
|
modpath[n] = 0; |
295 |
|
break; |
296 |
|
} |
297 |
|
} |
298 |
|
set_reg_entry (HKEY_CURRENT_USER, WINPT_REG, "Install Directory", modpath); |
299 |
|
} |
300 |
|
return 0; |
301 |
|
} |
302 |
|
|
303 |
|
|
304 |
/* Expand a string with %foo% entries so that %foo% will |
/* Expand a string with %foo% entries so that %foo% will |
305 |
be replaced with its actual value. */ |
be replaced with its actual value. */ |
306 |
static char * |
static char* |
307 |
expand_path (const char *path) |
expand_path (const char *path) |
308 |
{ |
{ |
309 |
DWORD len; |
DWORD len; |
314 |
return NULL; |
return NULL; |
315 |
len += 1; |
len += 1; |
316 |
p = new char[len+1]; |
p = new char[len+1]; |
317 |
if( !p ) |
if (!p) |
318 |
return NULL; |
return NULL; |
319 |
len = ExpandEnvironmentStrings (path, p, len); |
len = ExpandEnvironmentStrings (path, p, len); |
320 |
if (!len) { |
if (!len) { |
322 |
return NULL; |
return NULL; |
323 |
} |
} |
324 |
return p; |
return p; |
325 |
} /* expand_path */ |
} |
326 |
|
|
327 |
|
|
328 |
/* Retrieve a registry entry with the directory given in @dir |
/* Retrieve a registry entry with the directory given in @dir |
329 |
and the key given in @key. |
and the key given in @key. |
330 |
Return value is the value or NULL otherwise. */ |
Return value is the value or NULL otherwise. */ |
331 |
char* |
char* |
332 |
get_reg_entry (HKEY root_key, const char * dir, const char * key) |
get_reg_entry (HKEY root_key, const char *dir, const char *key) |
333 |
{ |
{ |
|
int rc; |
|
|
char text[384] = {0}; |
|
|
DWORD nbytes, type; |
|
334 |
HKEY reg_key = NULL; |
HKEY reg_key = NULL; |
335 |
char *p = NULL; |
char *p = NULL, *pp; |
336 |
|
DWORD type = REG_SZ, nbytes = 0; |
337 |
|
int rc; |
338 |
|
|
339 |
rc = RegOpenKeyEx (root_key, dir, 0, KEY_QUERY_VALUE, ®_key); |
rc = RegOpenKeyEx (root_key, dir, 0, KEY_QUERY_VALUE, ®_key); |
340 |
if( !rc_ok( rc ) ) |
if (rc != ERROR_SUCCESS) |
341 |
goto leave; |
goto leave; |
342 |
nbytes = sizeof (text) - 1; |
rc = RegQueryValueEx (reg_key, key, NULL, &type, NULL, &nbytes); |
343 |
type = REG_SZ; |
if (rc != ERROR_SUCCESS) |
|
|
|
|
rc = RegQueryValueEx (reg_key, key, 0, &type, (BYTE *)&text, &nbytes); |
|
|
if (!rc_ok (rc) || !nbytes) |
|
344 |
goto leave; |
goto leave; |
345 |
|
if (!nbytes) |
346 |
if (type == REG_EXPAND_SZ && strchr (text, '%')) |
goto leave; /* empty */ |
347 |
p = expand_path (text); |
p = new char[nbytes+1]; |
348 |
else { |
if (!p) |
349 |
p = new char[nbytes + 1]; |
BUG (0); |
350 |
if (!p) |
rc = RegQueryValueEx (reg_key, key, NULL, &type, (BYTE*)p, &nbytes); |
351 |
BUG (0); |
if (rc != ERROR_SUCCESS) |
352 |
memcpy (p, text, nbytes); |
goto leave; |
353 |
p[nbytes] = '\0'; |
if (type == REG_EXPAND_SZ && strchr (p, '%')) { |
354 |
|
pp = p; |
355 |
|
p = expand_path (pp); |
356 |
|
free_if_alloc (pp); |
357 |
} |
} |
358 |
|
|
359 |
leave: |
leave: |
360 |
if (reg_key) |
if (reg_key != NULL) |
361 |
RegCloseKey (reg_key); |
RegCloseKey (reg_key); |
362 |
return p; |
return p; |
363 |
} |
} |
371 |
set_reg_entry (HKEY root_key, const char *dir, const char *key, |
set_reg_entry (HKEY root_key, const char *dir, const char *key, |
372 |
const char *value) |
const char *value) |
373 |
{ |
{ |
|
int rc = 0; |
|
374 |
HKEY reg_key; |
HKEY reg_key; |
375 |
|
int rc; |
376 |
|
|
377 |
rc = RegOpenKeyEx( root_key, dir, 0, KEY_WRITE, ®_key ); |
rc = RegOpenKeyEx (root_key, dir, 0, KEY_WRITE, ®_key); |
378 |
if( !rc_ok( rc ) ) |
if (rc != ERROR_SUCCESS) |
379 |
return WPTERR_REGISTRY; |
return WPTERR_REGISTRY; |
380 |
rc = RegSetValueEx( reg_key, key, 0, REG_SZ, (BYTE *)value, strlen( value ) ); |
rc = RegSetValueEx (reg_key, key, 0, REG_SZ, (BYTE *)value, strlen (value)); |
381 |
if( !rc_ok( rc ) ) |
if (rc != ERROR_SUCCESS) |
382 |
rc = WPTERR_REGISTRY; |
rc = WPTERR_REGISTRY; |
383 |
RegCloseKey( reg_key ); |
RegCloseKey (reg_key); |
384 |
return rc; |
return rc; |
385 |
} /* set_reg_entry */ |
} |
386 |
|
|
387 |
|
|
388 |
int |
int |
389 |
set_reg_key( HKEY root_key, const char * dir, const char * key, |
set_reg_key (HKEY root_key, const char *dir, const char *key, |
390 |
const char * value ) |
const char *value) |
391 |
{ |
{ |
|
int rc = 0; |
|
392 |
HKEY reg_key; |
HKEY reg_key; |
393 |
|
int rc; |
394 |
|
|
395 |
rc = RegOpenKeyEx( root_key, dir, 0, KEY_WRITE, ®_key ); |
rc = RegOpenKeyEx (root_key, dir, 0, KEY_WRITE, ®_key); |
396 |
if( !rc_ok( rc ) ) |
if (rc != ERROR_SUCCESS) |
397 |
return WPTERR_REGISTRY; |
return WPTERR_REGISTRY; |
398 |
|
|
399 |
rc = RegSetValueEx( reg_key, key, 0, REG_SZ, (BYTE *)value, strlen( value ) ); |
rc = RegSetValueEx (reg_key, key, 0, REG_SZ, (BYTE *)value, strlen (value)); |
400 |
if( !rc_ok( rc ) ) { |
if (rc != ERROR_SUCCESS) { |
401 |
if ( RegCreateKey( root_key, key, ®_key ) != ERROR_SUCCESS ) { |
if (RegCreateKey (root_key, key, ®_key) != ERROR_SUCCESS) { |
402 |
rc = WPTERR_REGISTRY; |
rc = WPTERR_REGISTRY; |
403 |
goto leave; |
goto leave; |
404 |
} |
} |
405 |
rc = RegSetValueEx( reg_key, key, 0, REG_SZ, (BYTE *)value, strlen( value ) ); |
rc = RegSetValueEx (reg_key, key, 0, REG_SZ, (BYTE *)value, strlen (value)); |
406 |
if ( !rc_ok( rc ) ) |
if (rc != ERROR_SUCCESS) |
407 |
rc = WPTERR_REGISTRY; |
rc = WPTERR_REGISTRY; |
408 |
} |
} |
409 |
|
|
410 |
leave: |
leave: |
411 |
RegCloseKey( reg_key ); |
RegCloseKey (reg_key); |
412 |
return rc; |
return rc; |
413 |
} /* set_reg_key */ |
} |
414 |
|
|
415 |
|
|
416 |
int |
int |
417 |
set_reg_entry_gpg (const char * key, const char * value) |
set_reg_entry_gpg (const char *key, const char *value) |
418 |
{ |
{ |
419 |
return set_reg_entry (HKEY_CURRENT_USER, "Software\\GNU\\GnuPG", key, value); |
return set_reg_entry (HKEY_CURRENT_USER, GPG_REG, key, value); |
420 |
} |
} |
421 |
|
|
422 |
|
|
423 |
int |
int |
424 |
set_reg_entry_mo (const char * value) |
set_reg_entry_mo (const char *value) |
425 |
{ |
{ |
426 |
return set_reg_entry (HKEY_CURRENT_USER, "Control Panel\\Mingw32\\NLS", |
return set_reg_entry (HKEY_CURRENT_USER, GPG_MO_REG, "MODir", value); |
|
"MODir", value); |
|
427 |
} |
} |
428 |
|
|
429 |
|
|
430 |
char * |
char* |
431 |
get_reg_entry_gpg (const char *key) |
get_reg_entry_gpg (const char *key) |
432 |
{ |
{ |
433 |
return get_reg_entry (HKEY_CURRENT_USER, "Software\\GNU\\GnuPG", key); |
char *p; |
434 |
|
|
435 |
|
p = get_reg_entry (HKEY_CURRENT_USER, GPG_REG, key); |
436 |
|
if (!p || strlen (p) == 0) { |
437 |
|
free_if_alloc (p); |
438 |
|
return NULL; |
439 |
|
} |
440 |
|
return p; |
441 |
} |
} |
442 |
|
|
443 |
|
|
444 |
char * |
/* Return if possible the GPG4Win installation directory concatenated |
445 |
get_reg_entry_mo(void) |
with the string in @path if given. */ |
446 |
{ |
char* |
447 |
return get_reg_entry(HKEY_CURRENT_USER, |
get_reg_entry_gpg4win (const char *path) |
448 |
"Control Panel\\Mingw32\\NLS", "MODir"); |
{ |
449 |
|
const char *fmt; |
450 |
|
char *p, *pp; |
451 |
|
|
452 |
|
p = get_reg_entry (HKEY_LOCAL_MACHINE, GPG_REG, "Install Directory"); |
453 |
|
if (!p) |
454 |
|
return NULL; |
455 |
|
if (!path) |
456 |
|
return p; |
457 |
|
fmt = "%s\\%s"; |
458 |
|
pp = new char[strlen (p) + strlen (path) + strlen (fmt) + 1]; |
459 |
|
if (!pp) |
460 |
|
BUG (NULL); |
461 |
|
sprintf (pp, fmt, p, path); |
462 |
|
free_if_alloc (p); |
463 |
|
return pp; |
464 |
} |
} |
465 |
|
|
466 |
|
|
467 |
|
char* |
468 |
|
get_reg_entry_mo (void) |
469 |
|
{ |
470 |
|
char *p, *pp; |
471 |
|
const char *fmt; |
472 |
|
const char *lang; |
473 |
|
|
474 |
|
p = get_reg_entry (HKEY_CURRENT_USER, GPG_MO_REG, "MODir"); |
475 |
|
if (p) |
476 |
|
return p; |
477 |
|
|
478 |
/* All valid configuration commands. */ |
lang = gettext_get_langid (); |
479 |
static const char * cfg [] = { |
if (!lang) |
480 |
NULL, |
return NULL; |
481 |
"CacheTime", |
fmt = "share\\locale\\%s\\LC_MESSAGES"; |
482 |
"WordWrap", |
pp = new char[strlen (fmt) + strlen (lang) + 4 + 1]; |
483 |
"FastMode", |
if (!pp) |
484 |
"Viewer", |
BUG (NULL); |
485 |
"KeylistMode", |
sprintf (pp, fmt, lang); |
486 |
"WipeMode", |
p = get_reg_entry_gpg4win (pp); |
487 |
"AlwaysTrust", |
free_if_alloc (pp); |
488 |
"AutoBackup", |
return p; |
489 |
"BackupMode", |
} |
|
"DisableHotkeys", |
|
|
"NoCompressMultiMedia", |
|
|
"Expert", |
|
|
"FMProgressBar", |
|
|
}; |
|
490 |
|
|
491 |
|
|
492 |
int |
int |
495 |
char buf[64]; |
char buf[64]; |
496 |
int rc; |
int rc; |
497 |
|
|
498 |
sprintf (buf, "%d", val); |
_snprintf (buf, DIM (buf)-1, "%d", val); |
499 |
rc = set_reg_entry (HKEY_CURRENT_USER, WINPT_REG, cfg[id], buf); |
rc = set_reg_entry (HKEY_CURRENT_USER, WINPT_REG, cfg[id], buf); |
500 |
return rc; |
return rc; |
501 |
} |
} |
504 |
int |
int |
505 |
get_reg_winpt_single (int id) |
get_reg_winpt_single (int id) |
506 |
{ |
{ |
507 |
char * buf = NULL; |
char *buf; |
508 |
int val = 0; |
int val = 0; |
509 |
|
|
510 |
buf = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, cfg[id]); |
buf = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, cfg[id]); |
519 |
|
|
520 |
/* Saves the winpt preferences in the registry. */ |
/* Saves the winpt preferences in the registry. */ |
521 |
int |
int |
522 |
set_reg_winpt_prefs (winpt_reg_prefs_s * opt) |
set_reg_winpt_prefs (winpt_prefs_t opt) |
523 |
{ |
{ |
524 |
char buf[128]; |
char buf[128]; |
525 |
int rc = 0, i; |
size_t i; |
526 |
|
int rc = 0; |
527 |
|
|
528 |
for (i=1; i < DIM (cfg); i++) { |
for (i=1; i < DIM (cfg); i++) { |
529 |
switch (i) { |
switch (i) { |
533 |
case CFG_WORDWRAP: |
case CFG_WORDWRAP: |
534 |
sprintf (buf, "%d", opt->word_wrap); |
sprintf (buf, "%d", opt->word_wrap); |
535 |
break; |
break; |
536 |
case CFG_WIPEMODE: |
case CFG_FILEEXT: |
537 |
sprintf (buf, "%d", opt->wipe_mode); |
sprintf (buf, "%d", opt->default_ext); |
|
break; |
|
|
case CFG_FASTMODE: |
|
|
sprintf (buf, "%d", opt->use_tmpfiles); |
|
538 |
break; |
break; |
539 |
case CFG_NOZIP_MMEDIA: |
case CFG_NOZIP_MMEDIA: |
540 |
sprintf (buf, "%d", opt->no_zip_mmedia); |
sprintf (buf, "%d", opt->no_zip_mmedia); |
542 |
case CFG_VIEWER: |
case CFG_VIEWER: |
543 |
sprintf (buf, "%d", opt->use_viewer); |
sprintf (buf, "%d", opt->use_viewer); |
544 |
break; |
break; |
|
case CFG_KEYLISTMODE: |
|
|
sprintf (buf, "%d", opt->keylist_mode); |
|
|
break; |
|
545 |
case CFG_ALWAYSTRUST: |
case CFG_ALWAYSTRUST: |
546 |
sprintf (buf, "%d", opt->always_trust); |
sprintf (buf, "%d", opt->always_trust); |
547 |
break; |
break; |
561 |
case CFG_FM_PROGRESS: |
case CFG_FM_PROGRESS: |
562 |
sprintf (buf, "%d", opt->fm.progress); |
sprintf (buf, "%d", opt->fm.progress); |
563 |
break; |
break; |
564 |
|
|
565 |
|
case CFG_BACKUP_INC_SKR: |
566 |
|
sprintf (buf, "%d", opt->backup.include_secr); |
567 |
|
break; |
568 |
} |
} |
569 |
rc = set_reg_entry (HKEY_CURRENT_USER, WINPT_REG, cfg[i], buf); |
rc = set_reg_entry (HKEY_CURRENT_USER, WINPT_REG, cfg[i], buf); |
570 |
if (rc) |
if (rc) |
577 |
if (rc) |
if (rc) |
578 |
goto leave; |
goto leave; |
579 |
} |
} |
|
if (opt->kserv_conf) { |
|
|
rc = set_reg_entry (HKEY_CURRENT_USER, WINPT_REG, "KeyserverConfig", |
|
|
opt->kserv_conf); |
|
|
if (rc) |
|
|
goto leave; |
|
|
} |
|
580 |
|
|
581 |
for (i=0; reg_hotkeys[i].reg_entry; i++) { |
for (i=0; wpt_hotkeys[i].name; i++) { |
582 |
strcpy (buf, " "); |
if (wpt_hotkeys[i].enabled) { |
583 |
if (reg_hotkeys[i].enabled) |
buf[0] = wpt_hotkeys[i].key; |
584 |
strcpy (buf, reg_hotkeys[i].key); |
buf[1] = 0; |
585 |
|
} |
586 |
|
else |
587 |
|
strcpy (buf, " "); |
588 |
rc = set_reg_key (HKEY_CURRENT_USER, WINPT_REG, |
rc = set_reg_key (HKEY_CURRENT_USER, WINPT_REG, |
589 |
reg_hotkeys[i].reg_entry, buf); |
wpt_hotkeys[i].name, buf); |
590 |
if (rc) |
if (rc) |
591 |
break; |
break; |
592 |
} |
} |
601 |
|
|
602 |
|
|
603 |
int |
int |
604 |
set_reg_winpt_flag (const char * name, int val) |
set_reg_winpt_flag (const char *name, int val) |
605 |
{ |
{ |
606 |
return set_reg_entry (HKEY_CURRENT_USER, WINPT_REG, name, val? "1" : "0"); |
return set_reg_entry (HKEY_CURRENT_USER, WINPT_REG, name, val? "1" : "0"); |
607 |
} /* set_reg_winpt_flag */ |
} |
608 |
|
|
609 |
|
|
610 |
int |
int |
611 |
get_reg_winpt_flag (const char * name) |
get_reg_winpt_flag (const char *name) |
612 |
{ |
{ |
613 |
char * buf; |
char *buf; |
614 |
int flag = 0; |
int flag = 0; |
615 |
|
|
616 |
buf = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, name); |
buf = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, name); |
620 |
flag = -1; |
flag = -1; |
621 |
free_if_alloc (buf); |
free_if_alloc (buf); |
622 |
return flag; |
return flag; |
623 |
} /* get_reg_winpt_flag */ |
} |
624 |
|
|
625 |
|
|
626 |
/* Retrieve the winpt preferences from the registry. */ |
/* Retrieve the winpt preferences from the registry. */ |
627 |
int |
int |
628 |
get_reg_winpt_prefs (winpt_reg_prefs_s * opt) |
get_reg_winpt_prefs (winpt_prefs_t opt) |
629 |
{ |
{ |
630 |
char * val = NULL; |
char *val = NULL; |
631 |
int i; |
size_t i; |
632 |
|
|
633 |
for (i=1; i < DIM (cfg); i++) { |
for (i=1; i < DIM (cfg); i++) { |
634 |
val = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, cfg[i]); |
val = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, cfg[i]); |
638 |
} |
} |
639 |
switch (i) { |
switch (i) { |
640 |
case CFG_CACHETIME: |
case CFG_CACHETIME: |
641 |
opt->cache_time = atol (val); |
opt->cache_time = atoi (val); |
|
/* We do NOT support passphrase caching for more than an hour. |
|
|
* Perhaps we should allow it, but for now we silently drop this. |
|
|
*/ |
|
|
if (opt->cache_time > 3600) |
|
|
opt->cache_time = 3600; |
|
642 |
break; |
break; |
643 |
case CFG_WORDWRAP: |
case CFG_WORDWRAP: |
644 |
opt->word_wrap = atol (val); |
opt->word_wrap = atoi (val); |
645 |
break; |
break; |
646 |
case CFG_FASTMODE: |
case CFG_FILEEXT: |
647 |
opt->use_tmpfiles = atol (val); |
opt->default_ext = atoi (val); |
648 |
break; |
break; |
649 |
case CFG_NOZIP_MMEDIA: |
case CFG_NOZIP_MMEDIA: |
650 |
opt->no_zip_mmedia = atol (val); |
opt->no_zip_mmedia = atoi (val); |
651 |
break; |
break; |
652 |
case CFG_VIEWER: |
case CFG_VIEWER: |
653 |
opt->use_viewer = atol (val); |
opt->use_viewer = atoi (val); |
|
break; |
|
|
case CFG_KEYLISTMODE: |
|
|
opt->keylist_mode = atol (val); |
|
|
break; |
|
|
case CFG_WIPEMODE: |
|
|
opt->wipe_mode = atol (val); |
|
654 |
break; |
break; |
655 |
case CFG_DISHOTKEYS: |
case CFG_DISHOTKEYS: |
656 |
opt->no_hotkeys = atol (val); |
opt->no_hotkeys = atoi (val); |
657 |
break; |
break; |
658 |
case CFG_ALWAYSTRUST: |
case CFG_ALWAYSTRUST: |
659 |
opt->always_trust = atol (val); |
opt->always_trust = atoi (val); |
660 |
break; |
break; |
661 |
case CFG_AUTOBACKUP: |
case CFG_AUTOBACKUP: |
662 |
opt->auto_backup = atol (val); |
opt->auto_backup = atoi (val); |
663 |
break; |
break; |
664 |
case CFG_AUTOBAKMODE: |
case CFG_AUTOBAKMODE: |
665 |
opt->backup.mode = atol (val); |
opt->backup.mode = atoi (val); |
666 |
break; |
break; |
667 |
case CFG_EXPERT: |
case CFG_EXPERT: |
668 |
opt->expert = atol (val); |
opt->expert = atoi (val); |
669 |
break; |
break; |
|
|
|
670 |
case CFG_FM_PROGRESS: |
case CFG_FM_PROGRESS: |
671 |
opt->fm.progress = atol (val); |
opt->fm.progress = atoi (val); |
672 |
|
break; |
673 |
|
|
674 |
|
case CFG_BACKUP_INC_SKR: |
675 |
|
opt->backup.include_secr = atoi (val); |
676 |
break; |
break; |
677 |
} |
} |
678 |
free_if_alloc (val); |
free_if_alloc (val); |
682 |
if (val && val[0] != ' ') |
if (val && val[0] != ' ') |
683 |
opt->backup.path = m_strdup (val); |
opt->backup.path = m_strdup (val); |
684 |
free_if_alloc (val); |
free_if_alloc (val); |
|
|
|
|
val = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, "KeyserverConfig"); |
|
|
if (val && val[0] != ' ') |
|
|
opt->kserv_conf = m_strdup (val); |
|
|
free_if_alloc (val); |
|
685 |
|
|
686 |
for (i=0; reg_hotkeys[i].reg_entry; i++) { |
for (i=0; wpt_hotkeys[i].name; i++) { |
687 |
val = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, reg_hotkeys[i].reg_entry); |
wpt_hotkeys[i].enabled = 0; |
688 |
|
val = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, wpt_hotkeys[i].name); |
689 |
if (val && val[0] != ' ') { |
if (val && val[0] != ' ') { |
690 |
reg_hotkeys[i].key[0] = *val; |
wpt_hotkeys[i].key = *val; |
691 |
reg_hotkeys[i].enabled = 1; |
wpt_hotkeys[i].enabled = 1; |
692 |
} |
} |
|
else |
|
|
reg_hotkeys[i].enabled = 0; |
|
693 |
free_if_alloc (val); |
free_if_alloc (val); |
694 |
} |
} |
695 |
return 0; |
return 0; |
696 |
} /* get_reg_winpt_prefs */ |
} |
697 |
|
|
698 |
|
|
699 |
char* |
char* |
700 |
get_reg_entry_keyserver (const char *name) |
get_reg_entry_keyserver (const char *name) |
701 |
{ |
{ |
702 |
char * p = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG"\\Keyserver", name); |
char *p = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG"\\Keyserver", name); |
703 |
if (p && !strcmp (p, "")) { |
if (p && (!strcmp (p, "") || strlen (p) == 0)) { |
704 |
free_if_alloc (p); |
free_if_alloc (p); |
705 |
return NULL; |
return NULL; |
706 |
} |
} |
708 |
} |
} |
709 |
|
|
710 |
|
|
|
void |
|
|
get_reg_proxy_prefs (char ** host, int * port, char ** user, char ** pass) |
|
|
{ |
|
|
if (host) |
|
|
*host = get_reg_entry_keyserver ("Host"); |
|
|
if (user) |
|
|
*user = get_reg_entry_keyserver ("User"); |
|
|
if (pass) |
|
|
*pass = get_reg_entry_keyserver ("Pass"); |
|
|
if (port) { |
|
|
char * p = get_reg_entry_keyserver ("Port"); |
|
|
if (p) { |
|
|
*port = atol (p); |
|
|
free_if_alloc (p); |
|
|
} |
|
|
} |
|
|
} /* get_reg_proxy_prefs */ |
|
|
|
|
|
|
|
|
int |
|
|
set_reg_entry_keyserver( const char * name, const char * val ) |
|
|
{ |
|
|
return set_reg_entry( HKEY_CURRENT_USER, WINPT_REG"\\Keyserver", name, val ); |
|
|
} /* set_reg_entry_keyserver */ |
|
|
|
|
|
|
|
711 |
int |
int |
712 |
set_reg_proxy_prefs( const char * host, int port, const char * user, const char * pass ) |
set_reg_entry_keyserver (const char *name, const char *val) |
713 |
{ |
{ |
714 |
int rc; |
return set_reg_entry (HKEY_CURRENT_USER, WINPT_REG"\\Keyserver", name, val); |
715 |
|
} |
716 |
rc = set_reg_entry_keyserver( "Host", host? host : "" ); |
#endif |
|
if( !rc ) { |
|
|
char buf[32]; |
|
|
sprintf( buf, "%d", port ); |
|
|
rc = set_reg_entry_keyserver( "Port", buf ); |
|
|
} |
|
|
if( !rc ) |
|
|
rc = set_reg_entry_keyserver( "User", user? user : "" ); |
|
|
if( !rc ) |
|
|
rc = set_reg_entry_keyserver( "Pass", pass? pass : "" ); |
|
|
return rc; |
|
|
} /* set_reg_proxy_prefs */ |
|