67 |
char* |
char* |
68 |
multi_gnupg_path (int strict) |
multi_gnupg_path (int strict) |
69 |
{ |
{ |
70 |
static char buf[256+64]; |
static char buf[MAX_PATH+64]; |
71 |
BOOL ec; |
BOOL ec; |
72 |
|
|
73 |
/* MSDN: buf must be at least MAX_PATH=256 bytes */ |
/* MSDN: buf must be at least MAX_PATH=256 bytes */ |
106 |
char* |
char* |
107 |
get_gnupg_cfgfile (void) |
get_gnupg_cfgfile (void) |
108 |
{ |
{ |
109 |
char *optfile = NULL; |
char *optfile; |
110 |
char *path; |
char *path; |
|
size_t nlen; |
|
111 |
|
|
112 |
path = get_gnupg_path (); |
path = get_gnupg_path (); |
113 |
if (!path) |
if (!path) |
114 |
return NULL; |
return NULL; |
115 |
nlen = strlen (path) + 64; |
optfile = make_filename (path, GPG_CONF, NULL); |
|
optfile = new char[nlen + 1]; |
|
|
if (!optfile) |
|
|
BUG (NULL); |
|
|
_snprintf (optfile, nlen, "%s\\"GPG_CONF, path); |
|
|
|
|
116 |
free_if_alloc (path); |
free_if_alloc (path); |
117 |
|
|
118 |
return optfile; |
return optfile; |
119 |
} |
} |
120 |
|
|
121 |
|
|
122 |
|
static char* |
123 |
|
get_keyring_from_conf (const char *fname, int pub) |
124 |
|
{ |
125 |
|
config_file_t opt; |
126 |
|
conf_option_t e; |
127 |
|
char *kring = NULL; |
128 |
|
int rc; |
129 |
|
|
130 |
|
rc = parse_config (fname, &opt); |
131 |
|
if (rc) |
132 |
|
return NULL; |
133 |
|
if (pub) |
134 |
|
e = conf_find_option (opt, "keyring"); |
135 |
|
else |
136 |
|
e = conf_find_option (opt, "secret-keyring"); |
137 |
|
if (e != NULL) |
138 |
|
kring = m_strdup (e->val); |
139 |
|
release_config (opt); |
140 |
|
|
141 |
|
return kring; |
142 |
|
} |
143 |
|
|
144 |
|
|
145 |
/* Return the full path of the keyring. If @pub is 1, the public |
/* Return the full path of the keyring. If @pub is 1, the public |
146 |
keyring is return, otherwise the secret keyring. */ |
keyring is return, otherwise the secret keyring. */ |
147 |
char* |
char* |
148 |
get_gnupg_keyring (int pub, int strict) |
get_gnupg_keyring (int pub, int strict) |
149 |
{ |
{ |
150 |
char *optfile = NULL; |
char *optfile; |
151 |
char *path = NULL; |
char *path; |
152 |
char *keyring = NULL; |
char *keyring; |
153 |
|
|
154 |
path = get_gnupg_path (); |
path = get_gnupg_path (); |
155 |
if (!path) |
if (!path) |
163 |
free_if_alloc (path); |
free_if_alloc (path); |
164 |
return keyring; |
return keyring; |
165 |
} |
} |
166 |
if (file_exist_check (keyring) || pub && get_file_size (keyring) == 0) { |
if (file_exist_check (keyring) || |
167 |
|
pub && get_file_size (keyring) == 0) { |
168 |
free_if_alloc (keyring); |
free_if_alloc (keyring); |
169 |
optfile = make_filename (path, GPG_CONF, NULL); |
optfile = make_filename (path, GPG_CONF, NULL); |
170 |
keyring = get_gnupg_keyring_from_options (optfile, pub); |
keyring = get_keyring_from_conf (optfile, pub); |
171 |
|
free_if_alloc (optfile); |
172 |
} |
} |
173 |
free_if_alloc (path); |
free_if_alloc (path); |
174 |
free_if_alloc (optfile); |
|
175 |
return keyring; |
return keyring; |
176 |
} |
} |
177 |
|
|
231 |
int |
int |
232 |
gnupg_load_config (void) |
gnupg_load_config (void) |
233 |
{ |
{ |
234 |
gpg_optfile_t opt; |
config_file_t opt; |
235 |
char *conf; |
char *conf; |
236 |
|
|
237 |
conf = get_gnupg_cfgfile (); |
conf = get_gnupg_cfgfile (); |
241 |
free_if_alloc (conf); |
free_if_alloc (conf); |
242 |
return -1; |
return -1; |
243 |
} |
} |
244 |
if (find_option (opt, "ask-cert-level")) |
if (conf_find_option (opt, "ask-cert-level")) |
245 |
reg_prefs.gpg.ask_cert_level = 1; |
reg_prefs.gpg.ask_cert_level = 1; |
246 |
if (find_option (opt, "ask-cert-expire")) |
if (conf_find_option (opt, "ask-cert-expire")) |
247 |
reg_prefs.gpg.ask_cert_expire = 1; |
reg_prefs.gpg.ask_cert_expire = 1; |
248 |
release_config (opt); |
release_config (opt); |
249 |
free_if_alloc (conf); |
free_if_alloc (conf); |
257 |
{ |
{ |
258 |
size_t len = strlen (val); |
size_t len = strlen (val); |
259 |
|
|
260 |
if (len > 1 && val[len-1] == '!') { |
if (len > 1 && val[len-1] == '!') |
261 |
char *p = new char[len+1]; |
return substr (val, 0, len-1); |
|
if (!p) |
|
|
BUG (0); |
|
|
memset (p, 0, len+1); |
|
|
memcpy (p, val, len-1); |
|
|
return p; |
|
|
} |
|
262 |
return m_strdup (val); |
return m_strdup (val); |
263 |
} |
} |
264 |
|
|
265 |
|
|
266 |
|
/* Return the default key. |
267 |
|
This can be either a substring or a key ID. */ |
268 |
char* |
char* |
269 |
get_gnupg_default_key (void) |
get_gnupg_default_key (void) |
270 |
{ |
{ |
271 |
gpg_optfile_t opt = NULL; |
config_file_t opt = NULL; |
272 |
gpg_option_t e; |
conf_option_t e; |
273 |
char *keyid = NULL, *optfile = NULL; |
char *keyid = NULL, *optfile = NULL; |
274 |
int no_usable=0; |
int no_usable=0; |
275 |
|
|
280 |
free_if_alloc (optfile); |
free_if_alloc (optfile); |
281 |
return default_key_from_cache (&no_usable); |
return default_key_from_cache (&no_usable); |
282 |
} |
} |
283 |
e = find_option (opt, "default-key"); |
/* First we search for config entries which specify a default key. */ |
284 |
|
e = conf_find_option (opt, "default-key"); |
285 |
if (!e) |
if (!e) |
286 |
e = find_option (opt, "local-user"); |
e = conf_find_option (opt, "local-user"); |
287 |
if (e) |
if (e) |
288 |
keyid = extract_keyid (e->val); |
keyid = extract_keyid (e->val); |
289 |
|
|
290 |
free_if_alloc (optfile); |
free_if_alloc (optfile); |
291 |
release_config (opt); |
release_config (opt); |
292 |
|
|
293 |
|
/* If no entry in the config has been found, we get a key |
294 |
|
from the key cache. */ |
295 |
if (!keyid) |
if (!keyid) |
296 |
keyid = default_key_from_cache (&no_usable); |
keyid = default_key_from_cache (&no_usable); |
297 |
return keyid; |
return keyid; |
414 |
static int |
static int |
415 |
cfgfile_count_keyrings (const char *fname, int *r_secrings, int *r_pubrings) |
cfgfile_count_keyrings (const char *fname, int *r_secrings, int *r_pubrings) |
416 |
{ |
{ |
417 |
gpg_optfile_t opt; |
config_file_t opt; |
418 |
gpg_option_t e; |
conf_option_t e; |
419 |
|
|
420 |
*r_secrings = 0; |
*r_secrings = 0; |
421 |
*r_pubrings = 0; |
*r_pubrings = 0; |
570 |
int |
int |
571 |
set_gnupg_default_key (const char *key) |
set_gnupg_default_key (const char *key) |
572 |
{ |
{ |
573 |
gpg_optfile_t opt; |
config_file_t opt; |
574 |
gpg_option_t e; |
conf_option_t e; |
575 |
char *optfile = NULL; |
char *optfile = NULL; |
576 |
int rc = 0; |
int rc = 0; |
577 |
|
|
583 |
free_if_alloc (optfile); |
free_if_alloc (optfile); |
584 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
585 |
} |
} |
586 |
e = find_option (opt, "default-key"); |
e = conf_find_option (opt, "default-key"); |
587 |
if (e && !key) |
if (e && !key) |
588 |
e->used = 0; |
e->used = 0; |
589 |
else if (e) { |
else if (e) { |
592 |
e->used = 1; |
e->used = 1; |
593 |
} |
} |
594 |
else if (key) |
else if (key) |
595 |
add_entry (opt, ENTRY_MULTI, "default-key", key); |
conf_add_entry (opt, ENTRY_MULTI, "default-key", key); |
596 |
rc = commit_config (optfile, opt); |
rc = commit_config (optfile, opt); |
597 |
|
|
598 |
free_if_alloc (optfile); |
free_if_alloc (optfile); |
646 |
check_line (const char *buf) |
check_line (const char *buf) |
647 |
{ |
{ |
648 |
int j, len; |
int j, len; |
649 |
int rc; |
int rc = 0; |
650 |
|
|
651 |
if (*buf == '#' || *buf == '\r' || *buf == '\n') |
if (*buf == '#' || *buf == '\r' || *buf == '\n') |
652 |
return 1; |
return 1; |
|
rc = 0; |
|
653 |
for (j = 0; valid_gpg_args[j]; j++) { |
for (j = 0; valid_gpg_args[j]; j++) { |
654 |
len = strlen (valid_gpg_args[j]); |
len = strlen (valid_gpg_args[j]); |
655 |
if (!strncmp (valid_gpg_args[j], buf, len)) |
if (!strncmp (valid_gpg_args[j], buf, len)) |
694 |
HANDLE fd; |
HANDLE fd; |
695 |
|
|
696 |
fd = CreateFile (ctx->fpath_object, GENERIC_READ, FILE_SHARE_READ, |
fd = CreateFile (ctx->fpath_object, GENERIC_READ, FILE_SHARE_READ, |
697 |
NULL, OPEN_ALWAYS, 0, NULL); |
NULL, OPEN_EXISTING, 0, NULL); |
698 |
if (fd == INVALID_HANDLE_VALUE) |
if (fd == INVALID_HANDLE_VALUE) |
699 |
return WPTERR_FILE_OPEN; |
return WPTERR_FILE_OPEN; |
700 |
GetFileTime (fd, NULL, NULL, &ctx->access); |
GetFileTime (fd, NULL, NULL, &ctx->access); |
801 |
} |
} |
802 |
|
|
803 |
|
|
|
char* |
|
|
get_gnupg_keyring_from_options (const char *fname, int pub) |
|
|
{ |
|
|
gpg_optfile_t opt; |
|
|
gpg_option_t e; |
|
|
char *kring = NULL; |
|
|
int rc; |
|
|
|
|
|
rc = parse_config (fname, &opt); |
|
|
if (rc) |
|
|
return NULL; |
|
|
if (pub) |
|
|
e = find_option (opt, "keyring"); |
|
|
else |
|
|
e = find_option (opt, "secret-keyring"); |
|
|
if (e) |
|
|
kring = m_strdup (e->val); |
|
|
release_config (opt); |
|
|
|
|
|
return kring; |
|
|
} |
|
|
|
|
804 |
|
|
805 |
/* Check if the device file @fname is stored on, is write-protected. */ |
/* Check if the device file @fname is stored on, is write-protected. */ |
806 |
static int |
static int |
877 |
int val; |
int val; |
878 |
int rc = 0; |
int rc = 0; |
879 |
|
|
880 |
|
#ifdef WINPT_MOBILE |
881 |
|
return 0; |
882 |
|
#endif |
883 |
|
|
884 |
homedir = get_reg_entry_gpg (GPG_REG_HOME); |
homedir = get_reg_entry_gpg (GPG_REG_HOME); |
885 |
if (!homedir) |
if (!homedir) |
886 |
homedir = multi_gnupg_path (0); |
homedir = multi_gnupg_path (0); |
1017 |
opt = get_gnupg_cfgfile (); |
opt = get_gnupg_cfgfile (); |
1018 |
if (!opt) |
if (!opt) |
1019 |
BUG (0); |
BUG (0); |
1020 |
name = get_gnupg_keyring_from_options (opt, 1); |
name = get_keyring_from_conf (opt, 1); |
1021 |
free_if_alloc (opt); |
free_if_alloc (opt); |
1022 |
free_if_alloc (p); |
free_if_alloc (p); |
1023 |
if (!name) |
if (!name) |
1041 |
get_backup_name (const char *templ) |
get_backup_name (const char *templ) |
1042 |
{ |
{ |
1043 |
struct tm *tm; |
struct tm *tm; |
1044 |
|
const char *fmt; |
1045 |
char *p; |
char *p; |
1046 |
time_t t; |
time_t t; |
1047 |
|
|
1048 |
t = time (NULL); |
t = time (NULL); |
1049 |
tm = localtime (&t); |
tm = localtime (&t); |
1050 |
p = new char [strlen (templ) + 8 + 1]; |
fmt = "%s-%d"; |
1051 |
|
p = new char [strlen (templ) + strlen (fmt) + 8 + 1]; |
1052 |
if (!p) |
if (!p) |
1053 |
BUG (0); |
BUG (0); |
1054 |
sprintf (p, "%s-%d", templ, tm->tm_wday % 3); |
sprintf (p, fmt, templ, tm->tm_wday % 3); |
1055 |
return p; |
return p; |
1056 |
} |
} |
1057 |
|
|
1063 |
void |
void |
1064 |
gnupg_backup_keyrings (int auto_backup, int backup_mode, int include_secr) |
gnupg_backup_keyrings (int auto_backup, int backup_mode, int include_secr) |
1065 |
{ |
{ |
1066 |
char *srcpath = NULL, *dstpath = NULL; |
char *srcpath, *dstpath; |
1067 |
char *name=NULL; |
char *name; |
1068 |
int rc; |
int rc; |
1069 |
|
|
1070 |
if (!auto_backup) |
if (!auto_backup) |
1072 |
srcpath = get_gnupg_path (); |
srcpath = get_gnupg_path (); |
1073 |
check_keyring (&srcpath); |
check_keyring (&srcpath); |
1074 |
if (backup_mode == 1) { |
if (backup_mode == 1) { |
1075 |
dstpath = multi_gnupg_path (1); |
/* If the backup mode uses the home directory the source |
1076 |
|
and destination folder will be the same. */ |
1077 |
|
dstpath = get_gnupg_path (); |
1078 |
check_keyring (&dstpath); |
check_keyring (&dstpath); |
1079 |
} |
} |
1080 |
else if (backup_mode == 2) { |
else if (backup_mode == 2) { |
1094 |
DeleteFile (tmpfile); |
DeleteFile (tmpfile); |
1095 |
} |
} |
1096 |
free_if_alloc (tmpfile); |
free_if_alloc (tmpfile); |
1097 |
if (!fp || rc == IDCANCEL) |
if (!fp || rc == IDCANCEL) { |
1098 |
|
free_if_alloc (dstpath); |
1099 |
|
free_if_alloc (srcpath); |
1100 |
return; |
return; |
1101 |
|
} |
1102 |
} |
} |
1103 |
else { |
else { |
1104 |
log_box (_("Backup"), MB_ERR, _("Invalid backup mode %d"), backup_mode); |
log_box (_("Backup"), MB_ERR, _("Invalid backup mode %d"), backup_mode); |
1105 |
|
free_if_alloc (srcpath); |
1106 |
return; |
return; |
1107 |
} |
} |
1108 |
name = get_backup_name ("pubring-bak"); |
name = get_backup_name ("pubring-bak"); |