62 |
|
|
63 |
|
|
64 |
/* Return the application data folder of the current user. */ |
/* Return the application data folder of the current user. */ |
65 |
static char* |
char* |
66 |
multi_gnupg_path (int strict) |
multi_gnupg_path (int strict) |
67 |
{ |
{ |
68 |
static char buf[256+64]; |
static char buf[256+64]; |
84 |
} |
} |
85 |
|
|
86 |
|
|
87 |
/* Return the full path of the GnuPG application. First the registry is scanned |
/* Return the full path to the GPG home directory. First the 'HomeDir' entry |
88 |
for the entry 'HomeDir'. If it wasn't set, the default dir C:\GNUPG is used. |
from the registry is used. Then the default $APPDATA\gnupg path. */ |
|
*/ |
|
89 |
char* |
char* |
90 |
get_gnupg_path (void) |
get_gnupg_path (void) |
91 |
{ |
{ |
92 |
char *p = NULL; |
char *path; |
93 |
char *path = NULL; |
|
94 |
|
path = get_reg_entry_gpg (GPG_REG_HOME); |
95 |
p = get_reg_entry_gpg (GPG_REG_HOME); |
if (path) { |
96 |
if (p) { |
if (dir_exist_check (path) == 0) |
97 |
path = m_strdup (p); |
return path; |
98 |
free_if_alloc (p); |
free_if_alloc (path); |
|
return path; |
|
99 |
} |
} |
100 |
else |
path = multi_gnupg_path (1); |
101 |
return multi_gnupg_path (1); |
return path; |
|
return m_strdup ("c:\\gnupg"); |
|
102 |
} |
} |
103 |
|
|
104 |
|
|
290 |
} /* get_gnupg_default_key */ |
} /* get_gnupg_default_key */ |
291 |
|
|
292 |
|
|
|
char* get_reg_entry_gpg4win (const char *path); |
|
|
|
|
293 |
/* Check if GPG4WIN is available and if so, use the |
/* Check if GPG4WIN is available and if so, use the |
294 |
install path to figure out where the gpg.exe is. */ |
install path to figure out where the gpg.exe is. */ |
295 |
char* |
char* |
296 |
check_for_gpg4win (void) |
check_for_gpg4win (void) |
297 |
{ |
{ |
298 |
return get_reg_entry_gpg4win ("\\gpg.exe"); |
return get_reg_entry_gpg4win ("gpg.exe"); |
299 |
} |
} |
300 |
|
|
301 |
|
|
337 |
tmp[i] = 0; buf++; |
tmp[i] = 0; buf++; |
338 |
*minor = atol (tmp); |
*minor = atol (tmp); |
339 |
i=0; |
i=0; |
340 |
while (buf && isdigit( *buf ) && i < 8) |
while (buf && isdigit (*buf) && i < 8) |
341 |
tmp[i++] = *buf++; |
tmp[i++] = *buf++; |
342 |
tmp[i] = 0; |
tmp[i] = 0; |
343 |
*patch = atol (tmp); |
*patch = atol (tmp); |
353 |
{ |
{ |
354 |
gpgme_ctx_t ctx; |
gpgme_ctx_t ctx; |
355 |
gpgme_engine_info_t inf; |
gpgme_engine_info_t inf; |
356 |
char * eng = NULL; |
char *eng = NULL; |
357 |
int major=0, minor=0, patch=0; |
int major=0, minor=0, patch=0; |
358 |
int rc; |
int rc = 1; |
359 |
|
|
360 |
gpgme_new (&ctx); |
gpgme_new (&ctx); |
361 |
inf = gpgme_ctx_get_engine_info (ctx); |
inf = gpgme_ctx_get_engine_info (ctx); |
363 |
gpgme_release (ctx); |
gpgme_release (ctx); |
364 |
return -1; |
return -1; |
365 |
} |
} |
366 |
|
|
367 |
/* We need to exec GPG again to find out if IDEA is available. */ |
/* We need to exec GPG again to find out if IDEA is available. */ |
368 |
if (gpg_get_version (&eng)) |
if (gpg_get_version (&eng)) |
369 |
return -1; |
return -1; |
370 |
if (strstr (eng, "IDEA")) |
if (strstr (eng, "IDEA")) |
371 |
idea_available = 1; |
idea_available = 1; |
372 |
free (eng); |
free (eng); |
373 |
rc = parse_version_nr( inf->version, &major, &minor, &patch ); |
rc = parse_version_nr (inf->version, &major, &minor, &patch); |
374 |
if( rc ) { |
if (rc) { |
375 |
gpgme_release (ctx); |
gpgme_release (ctx); |
376 |
return rc; |
return rc; |
377 |
} |
} |
378 |
/* FIXME: This check is wrong! */ |
|
379 |
if (major < *r_major || minor < *r_minor) |
if (major > *r_major) |
|
rc = 1; |
|
|
else { |
|
|
if (patch < *r_patch) |
|
|
rc = 1; |
|
380 |
rc = 0; |
rc = 0; |
381 |
} |
else if (major == *r_major && minor > *r_minor) |
382 |
|
rc = 0; |
383 |
|
else if (major == *r_major && minor == *r_minor && |
384 |
|
patch >= *r_patch) |
385 |
|
rc = 0; |
386 |
|
|
387 |
*r_major = major; |
*r_major = major; |
388 |
*r_minor = minor; |
*r_minor = minor; |
389 |
*r_patch = patch; |
*r_patch = patch; |
666 |
if (ctx->last_access.dwHighDateTime != ctx->access.dwHighDateTime && |
if (ctx->last_access.dwHighDateTime != ctx->access.dwHighDateTime && |
667 |
ctx->last_access.dwLowDateTime != ctx->access.dwLowDateTime) |
ctx->last_access.dwLowDateTime != ctx->access.dwLowDateTime) |
668 |
ctx->modified = 1; |
ctx->modified = 1; |
669 |
|
|
670 |
|
/* XXX: find a better way. without it, winpt --keymanager loads |
671 |
|
the key cache twice. */ |
672 |
|
if (ctx->last_access.dwLowDateTime == 0) |
673 |
|
ctx->modified = 0; |
674 |
|
|
675 |
ctx->last_access.dwLowDateTime = ctx->access.dwLowDateTime; |
ctx->last_access.dwLowDateTime = ctx->access.dwLowDateTime; |
676 |
ctx->last_access.dwHighDateTime = ctx->access.dwHighDateTime; |
ctx->last_access.dwHighDateTime = ctx->access.dwHighDateTime; |
777 |
|
|
778 |
/* XXX: does not work with write-protected floppies */ |
/* XXX: does not work with write-protected floppies */ |
779 |
static int |
static int |
780 |
my_access (const char * fname) |
my_access (const char *fname) |
781 |
{ |
{ |
782 |
HANDLE hd; |
HANDLE hd; |
783 |
hd = CreateFile (fname, GENERIC_WRITE, FILE_SHARE_WRITE, |
hd = CreateFile (fname, GENERIC_WRITE, FILE_SHARE_WRITE, |
789 |
} |
} |
790 |
|
|
791 |
|
|
792 |
|
/* Check the file permissions of the public keyring. |
793 |
|
If @showmsg is 1 output a message in case of errors. |
794 |
|
Return value: 1 if read-only attribute |
795 |
|
2 if file is opened by another process exclusively. */ |
796 |
int |
int |
797 |
gpg_check_permissions (int showmsg) |
gpg_check_permissions (int showmsg) |
798 |
{ |
{ |
799 |
char * p, * name = NULL; |
char *p = NULL; |
800 |
|
char *name = NULL; |
801 |
int failed = 0, ans=0, attrs=0; |
int failed = 0, ans=0, attrs=0; |
802 |
|
|
803 |
p = get_gnupg_path (); |
p = get_gnupg_path (); |
804 |
check_keyring (&p); |
if (check_keyring (&p) && p) { |
|
if (p) { |
|
805 |
name = make_filename (p, "pubring", "gpg"); |
name = make_filename (p, "pubring", "gpg"); |
|
free_if_alloc (p); |
|
806 |
if ((attrs=GetFileAttributes (name)) & FILE_ATTRIBUTE_READONLY) { |
if ((attrs=GetFileAttributes (name)) & FILE_ATTRIBUTE_READONLY) { |
807 |
ans = msg_box (NULL, |
ans = msg_box (NULL, |
808 |
_("The selected keyring has the read-only file\n" |
_("The selected keyring has the read-only file\n" |
818 |
} |
} |
819 |
} |
} |
820 |
else if (ans == IDNO) { |
else if (ans == IDNO) { |
821 |
/* |
/* All commands with write access will be disabled. */ |
|
msg_box (NULL, _("All commands with write access to the keyring\n" |
|
|
"will be disabled."), _("GPG Information"), MB_INFO); |
|
|
*/ |
|
822 |
failed = 1; |
failed = 1; |
823 |
} |
} |
824 |
} |
} |
834 |
failed = 2; |
failed = 2; |
835 |
} |
} |
836 |
} |
} |
837 |
|
free_if_alloc (p); |
838 |
free_if_alloc (name); |
free_if_alloc (name); |
839 |
return failed; |
return failed; |
840 |
} /* gpg_check_permissions */ |
} |
841 |
|
|
842 |
|
|
843 |
/* Check the GPG home dir. If all methods failed, try to |
/* Check the GPG home dir. First try to read the 'HomeDir' registry entry, |
844 |
create the default folder. */ |
then check for $APPDATA\gnupg. Create the dir if it does not exists. */ |
845 |
static int |
int |
846 |
check_homedir (void) |
gnupg_check_homedir (void) |
847 |
{ |
{ |
848 |
char *homedir = NULL; |
char *homedir = NULL; |
849 |
int yes = 0, set_reg=0; |
int val = 0; |
850 |
int rc = 0; |
int rc = 0; |
851 |
|
|
852 |
homedir = get_reg_entry_gpg (GPG_REG_HOME); |
homedir = get_reg_entry_gpg (GPG_REG_HOME); |
|
if (!homedir) { |
|
|
set_reg = 1; |
|
|
homedir = multi_gnupg_path (0); |
|
|
} |
|
853 |
if (!homedir) |
if (!homedir) |
854 |
homedir = m_strdup ("c:\\gnupg"); |
homedir = multi_gnupg_path (0); |
855 |
if (homedir) { |
if (homedir) { |
856 |
if (GetFileAttributes (homedir) == 0xFFFFFFFF) { |
if (GetFileAttributes (homedir) == 0xFFFFFFFF) { |
857 |
yes = log_box (_("Preferences"), MB_YESNO, |
val = log_box (_("Preferences"), MB_YESNO, |
858 |
_("%s does not exit.\n" |
_("%s does not exit.\n" |
859 |
"Do you want to create this directory?"), homedir); |
"Do you want to create this directory?"), homedir); |
860 |
if (yes == IDYES) { |
if (val == IDYES) { |
861 |
if (!CreateDirectory (homedir, NULL)) |
if (!CreateDirectory (homedir, NULL)) |
862 |
rc = WPTERR_DIR_CREAT; |
rc = WPTERR_DIR_CREAT; |
863 |
} |
} |
864 |
else |
else |
865 |
rc = WPTERR_DIR_OPEN; |
rc = WPTERR_DIR_OPEN; |
866 |
} |
} |
|
if (set_reg) |
|
|
set_reg_entry_gpg (GPG_REG_HOME, homedir); |
|
867 |
free_if_alloc (homedir); |
free_if_alloc (homedir); |
868 |
} |
} |
869 |
return rc; |
return rc; |
871 |
|
|
872 |
|
|
873 |
int |
int |
|
gnupg_check_homedir (void) |
|
|
{ |
|
|
char *homedir = NULL; |
|
|
char *prog = NULL; |
|
|
int rc = 0; |
|
|
|
|
|
rc = check_homedir (); |
|
|
if (rc) |
|
|
return rc; |
|
|
if ((homedir = get_reg_entry_gpg (GPG_REG_HOME)) && |
|
|
!(prog = get_reg_entry_gpg (GPG_REG_EXE ))) { |
|
|
prog = make_filename (homedir, "gpg", "exe"); |
|
|
if (file_exist_check (prog) == 0) { |
|
|
rc = set_reg_entry_gpg (GPG_REG_EXE, prog); |
|
|
if (rc) |
|
|
goto fail; |
|
|
} |
|
|
free_if_alloc (homedir); |
|
|
free_if_alloc (prog); |
|
|
return rc; |
|
|
} |
|
|
if ((prog = get_reg_entry_gpg (GPG_REG_EXE)) |
|
|
&& file_exist_check (prog)) { |
|
|
free_if_alloc (prog); |
|
|
homedir = get_reg_entry_gpg (GPG_REG_HOME); |
|
|
if (!homedir) { |
|
|
rc = WPTERR_GENERAL; |
|
|
goto fail; |
|
|
} |
|
|
prog = make_filename (homedir, "gpg", "exe"); |
|
|
if (file_exist_check (prog) == 0) { |
|
|
rc = set_reg_entry_gpg (GPG_REG_EXE, prog); |
|
|
if (rc) |
|
|
goto fail; |
|
|
free_if_alloc (prog); |
|
|
return rc; |
|
|
} |
|
|
} |
|
|
|
|
|
/* Change the return code if homedir doesn't exist or if the program |
|
|
doesn't exist. Note that exist_checks return 0 to suggest existance. */ |
|
|
if ((!homedir || dir_exist_check (homedir))) |
|
|
rc = WPTERR_GENERAL; |
|
|
|
|
|
fail: |
|
|
free_if_alloc (homedir); |
|
|
free_if_alloc (prog); |
|
|
return rc; |
|
|
} /* gnupg_check_homedir */ |
|
|
|
|
|
|
|
|
int |
|
874 |
gnupg_copy_keyrings (void) |
gnupg_copy_keyrings (void) |
875 |
{ |
{ |
876 |
const char * pring, * sring; |
const char * pring, * sring; |
883 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
884 |
hwnd = GetDesktopWindow (); |
hwnd = GetDesktopWindow (); |
885 |
|
|
886 |
pring = get_filename_dlg (hwnd, FILE_OPEN, _("Please choose your public keyring"), |
pring = get_fileopen_dlg (hwnd, _("Please choose your public keyring"), |
887 |
_("GPG Keyrings (*.gpg)\0*.gpg\0\0"),NULL); |
_("GPG Keyrings (*.gpg)\0*.gpg\0\0"),NULL); |
888 |
if (!pring) { |
if (!pring) { |
889 |
msg_box (hwnd, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR); |
msg_box (hwnd, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR); |
903 |
} |
} |
904 |
free_if_alloc (file); |
free_if_alloc (file); |
905 |
|
|
906 |
sring = get_filename_dlg (hwnd, FILE_OPEN, _("Please choose your secret keyring"), |
sring = get_fileopen_dlg (hwnd, _("Please choose your secret keyring"), |
907 |
_("GPG Keyrings (*.gpg)\0*.gpg\0\0"), NULL); |
_("GPG Keyrings (*.gpg)\0*.gpg\0\0"), NULL); |
908 |
if (!sring) { |
if (!sring) { |
909 |
msg_box( NULL, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR ); |
msg_box( NULL, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR ); |
927 |
} /* gnupg_import_keyrings */ |
} /* gnupg_import_keyrings */ |
928 |
|
|
929 |
|
|
930 |
|
/* Backup the gpg.conf file. */ |
931 |
void |
void |
932 |
gnupg_backup_options (void) |
gnupg_backup_options (void) |
933 |
{ |
{ |
935 |
char bak[512]; |
char bak[512]; |
936 |
|
|
937 |
cfgfile = get_gnupg_cfgfile (); |
cfgfile = get_gnupg_cfgfile (); |
938 |
if (cfgfile == NULL) |
if (!cfgfile) |
939 |
return; |
return; |
940 |
_snprintf (bak, DIM (bak)-1, "%s.bak", cfgfile); |
_snprintf (bak, DIM (bak)-1, "%s.bak", cfgfile); |
941 |
CopyFile (cfgfile, bak, FALSE); |
CopyFile (cfgfile, bak, FALSE); |
942 |
free_if_alloc (cfgfile); |
free_if_alloc (cfgfile); |
943 |
} /* gnupg_backup_options */ |
} |
|
|
|
944 |
|
|
945 |
|
|
946 |
static int |
static int |
968 |
} /* backup_one_file */ |
} /* backup_one_file */ |
969 |
|
|
970 |
|
|
971 |
|
/* Figure out first public keyring which is not empty. |
972 |
|
Return value: 1 on success. */ |
973 |
static int |
static int |
974 |
check_keyring (char ** r_path) |
check_keyring (char **r_path) |
975 |
{ |
{ |
976 |
char * p; |
char *p; |
977 |
char * opt, * name; |
char *opt; |
978 |
|
char *name; |
979 |
|
|
980 |
if (!*r_path) |
if (!*r_path) |
981 |
return 0; |
return 0; |
982 |
p = make_filename (*r_path, "pubring", "gpg"); |
p = make_filename (*r_path, "pubring", "gpg"); |
983 |
if (!p || get_file_size (p) > 0) |
if (!p || get_file_size (p) <= 0) |
984 |
return 0; |
return 0; |
985 |
|
|
986 |
opt = get_gnupg_cfgfile (); |
opt = get_gnupg_cfgfile (); |
992 |
if (!name) |
if (!name) |
993 |
return 0; |
return 0; |
994 |
p = strrchr (name, '\\'); |
p = strrchr (name, '\\'); |
995 |
if (!p) |
if (!p) { |
|
{ |
|
996 |
free_if_alloc (name); |
free_if_alloc (name); |
997 |
return 0; |
return 0; |
998 |
} |
} |
1005 |
} |
} |
1006 |
|
|
1007 |
|
|
1008 |
|
/* Return a temp name based on the day of the week. */ |
1009 |
static char* |
static char* |
1010 |
get_backup_name (const char *templ) |
get_backup_name (const char *templ) |
1011 |
{ |
{ |
1012 |
struct tm *tm; |
struct tm *tm; |
1013 |
char *p; |
char *p; |
1014 |
|
time_t t; |
1015 |
|
|
1016 |
time_t t = time (NULL); |
t = time (NULL); |
1017 |
tm = localtime (&t); |
tm = localtime (&t); |
1018 |
p = new char [strlen (templ) + 8 + 1]; |
p = new char [strlen (templ) + 8 + 1]; |
1019 |
if (!p) |
if (!p) |
1023 |
} |
} |
1024 |
|
|
1025 |
|
|
1026 |
|
/* Make backups of all keyrings. The public key ring is |
1027 |
|
rotated like this pubring-%d.gpg. */ |
1028 |
void |
void |
1029 |
gnupg_backup_keyrings (void) |
gnupg_backup_keyrings (void) |
1030 |
{ |
{ |
1035 |
if (!reg_prefs.auto_backup) |
if (!reg_prefs.auto_backup) |
1036 |
return; |
return; |
1037 |
bakmode = reg_prefs.backup.mode; |
bakmode = reg_prefs.backup.mode; |
1038 |
srcpath = get_gnupg_path (); |
srcpath = get_gnupg_path (); |
1039 |
check_keyring (&srcpath); |
check_keyring (&srcpath); |
1040 |
if (bakmode == 1) { |
if (bakmode == 1) { |
1041 |
dstpath = get_gnupg_path (); |
dstpath = multi_gnupg_path (1); |
1042 |
check_keyring (&dstpath); |
check_keyring (&dstpath); |
1043 |
} |
} |
1044 |
else if (bakmode == 2) { |
else if (bakmode == 2) { |
1045 |
char * tmpfile; |
char *tmpfile; |
1046 |
FILE * fp; |
FILE *fp; |
1047 |
|
|
1048 |
dstpath = m_strdup (reg_prefs.backup.path); |
dstpath = m_strdup (reg_prefs.backup.path); |
1049 |
if (!dstpath) |
if (!dstpath) |
1074 |
free_if_alloc (name); |
free_if_alloc (name); |
1075 |
free_if_alloc (srcpath); |
free_if_alloc (srcpath); |
1076 |
free_if_alloc (dstpath); |
free_if_alloc (dstpath); |
1077 |
} /* gnupg_backup_keyrings */ |
} |
1078 |
|
|
1079 |
|
|
1080 |
/* Display GPG error from file if possible. */ |
/* Display GPG error from file if possible. */ |
1092 |
return; |
return; |
1093 |
fp = fopen( tmpath, "rb" ); |
fp = fopen( tmpath, "rb" ); |
1094 |
if (!fp) { |
if (!fp) { |
1095 |
msg_box( NULL, _("No GPG error description available."), _("GPG Error"), MB_INFO ); |
msg_box (NULL, _("No GPG error description available."), |
1096 |
|
_("GPG Error"), MB_INFO); |
1097 |
return; |
return; |
1098 |
} |
} |
1099 |
errstr = new char[size+1]; |
errstr = new char[size+1]; |