* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
- * License along with this program; if not, see <http://www.gnu.org/licenses/>.
+ * License along with this program; if not, see <https://www.gnu.org/licenses/>.
**/
#ifdef HAVE_CONFIG_H
# define F_OK 0
#endif
+/* The Registry key used by GNUPG. */
+#ifdef _WIN64
+# define GNUPG_REGKEY_2 "Software\\Wow6432Node\\GNU\\GnuPG"
+#else
+# define GNUPG_REGKEY_2 "Software\\GNU\\GnuPG"
+#endif
+#ifdef _WIN64
+# define GNUPG_REGKEY_3 "Software\\Wow6432Node\\GnuPG"
+#else
+# define GNUPG_REGKEY_3 "Software\\GnuPG"
+#endif
DEFINE_STATIC_LOCK (get_path_lock);
file name of the DLL or executable which contains the gpgme code. */
static HMODULE my_hmodule;
+/* These variables store the malloced name of alternative default
+ binaries. The are set only once by gpgme_set_global_flag. */
+static char *default_gpg_name;
+static char *default_gpgconf_name;
+/* If this variable is not NULL the value is assumed to be the
+ installation directory. The variable may only be set once by
+ gpgme_set_global_flag and accessed by _gpgme_get_inst_dir. */
+static char *override_inst_dir;
#ifdef HAVE_ALLOW_SET_FOREGROUND_WINDOW
dlopen (const char * name, int flag)
{
void * hd = LoadLibrary (name);
+
+ (void)flag;
return hd;
}
}
+/* Replace all forward slashes by backslashes. */
+static void
+replace_slashes (char *string)
+{
+ for (; *string; string++)
+ if (*string == '/')
+ *string = '\\';
+}
+
+
+/* Get the base name of NAME. Returns a pointer into NAME right after
+ the last slash or backslash or to NAME if no slash or backslash
+ exists. */
+static const char *
+get_basename (const char *name)
+{
+ const char *mark, *s;
+
+ for (mark=NULL, s=name; *s; s++)
+ if (*s == '/' || *s == '\\')
+ mark = s;
+
+ return mark? mark+1 : name;
+}
+
+
void
_gpgme_allow_set_foreground_window (pid_t pid)
{
{
static char *inst_dir;
+ if (override_inst_dir)
+ return override_inst_dir;
+
LOCK (get_path_lock);
if (!inst_dir)
{
{
char *result;
- result = malloc (strlen (dir) + 1 + strlen (name) + 1);
+ result = _gpgme_strconcat (dir, "\\", name, NULL);
if (!result)
return NULL;
- strcpy (stpcpy (stpcpy (result, dir), "\\"), name);
if (access (result, F_OK))
{
free (result);
static char *
-find_program_in_inst_dir (const char *inst_dir, const char *name)
+find_program_at_standard_place (const char *name)
{
- char *result;
- char *dir;
-
- /* If an installation directory has been passed, this overrides a
- location given bu the registry. The idea here is that we prefer
- a a program installed alongside with gpgme. We don't want the
- registry to override this to have a better isolation of an gpgme
- aware applications for other effects. Note that the "Install
- Directory" registry item has been used for ages in Gpg4win and
- earlier GnuPG windows installers. It is technically not anymore
- required. */
- if (inst_dir)
+ char path[MAX_PATH];
+ char *result = NULL;
+
+ /* See http://wiki.tcl.tk/17492 for details on compatibility.
+
+ We First try the generic place and then fallback to the x86
+ (i.e. 32 bit) place. This will prefer a 64 bit of the program
+ over a 32 bit version on 64 bit Windows if installed. */
+ if (SHGetSpecialFolderPathA (NULL, path, CSIDL_PROGRAM_FILES, 0))
{
- result = find_program_in_dir (inst_dir, name);
- if (result)
- return result;
+ result = _gpgme_strconcat (path, "\\", name, NULL);
+ if (result && access (result, F_OK))
+ {
+ free (result);
+ result = NULL;
+ }
}
-
- dir = read_w32_registry_string ("HKEY_LOCAL_MACHINE",
- "Software\\GNU\\GnuPG",
- "Install Directory");
- if (dir)
+ if (!result
+ && SHGetSpecialFolderPathA (NULL, path, CSIDL_PROGRAM_FILESX86, 0))
{
- result = find_program_in_dir (dir, name);
- free (dir);
- return result;
+ result = _gpgme_strconcat (path, "\\", name, NULL);
+ if (result && access (result, F_OK))
+ {
+ free (result);
+ result = NULL;
+ }
}
- return NULL;
+ return result;
}
-static char *
-find_program_at_standard_place (const char *name)
+/* Set the default name for the gpg binary. This function may only be
+ called by gpgme_set_global_flag. Returns 0 on success. */
+int
+_gpgme_set_default_gpg_name (const char *name)
{
- char path[MAX_PATH];
- char *result = NULL;
+ if (!default_gpg_name)
+ {
+ default_gpg_name = _gpgme_strconcat (name, ".exe", NULL);
+ if (default_gpg_name)
+ replace_slashes (default_gpg_name);
+ }
+ return !default_gpg_name;
+}
- /* See http://wiki.tcl.tk/17492 for details on compatibility. */
- if (SHGetSpecialFolderPathA (NULL, path, CSIDL_PROGRAM_FILES, 0))
+/* Set the default name for the gpgconf binary. This function may only be
+ called by gpgme_set_global_flag. Returns 0 on success. */
+int
+_gpgme_set_default_gpgconf_name (const char *name)
+{
+ if (!default_gpgconf_name)
{
- result = malloc (strlen (path) + 1 + strlen (name) + 1);
- if (result)
+ default_gpgconf_name = _gpgme_strconcat (name, ".exe", NULL);
+ if (default_gpgconf_name)
+ replace_slashes (default_gpgconf_name);
+ }
+ return !default_gpgconf_name;
+}
+
+
+/* Set the override installation directory. This function may only be
+ called by gpgme_set_global_flag. Returns 0 on success. */
+int
+_gpgme_set_override_inst_dir (const char *dir)
+{
+ if (!override_inst_dir)
+ {
+ override_inst_dir = strdup (dir);
+ if (override_inst_dir)
{
- strcpy (stpcpy (stpcpy (result, path), "\\"), name);
- if (access (result, F_OK))
- {
- free (result);
- result = NULL;
- }
+ replace_slashes (override_inst_dir);
+ /* Remove a trailing slash. */
+ if (*override_inst_dir
+ && override_inst_dir[strlen (override_inst_dir)-1] == '\\')
+ override_inst_dir[strlen (override_inst_dir)-1] = 0;
}
}
- return result;
+ return !override_inst_dir;
}
-const char *
+/* Return the full file name of the GPG binary. This function is used
+ iff gpgconf was not found and thus it can be assumed that gpg2 is
+ not installed. This function is only called by get_gpgconf_item
+ and may not be called concurrently. */
+char *
_gpgme_get_gpg_path (void)
{
- static char *gpg_program;
- const char *inst_dir;
+ char *gpg = NULL;
+ const char *name, *inst_dir;
+
+ name = default_gpg_name? get_basename (default_gpg_name) : "gpg.exe";
+ /* 1. Try to find gpg.exe in the installation directory of gpgme. */
inst_dir = _gpgme_get_inst_dir ();
- LOCK (get_path_lock);
- if (!gpg_program)
- gpg_program = find_program_in_inst_dir (inst_dir, "gpg.exe");
- if (!gpg_program)
- gpg_program = find_program_at_standard_place ("GNU\\GnuPG\\gpg.exe");
- UNLOCK (get_path_lock);
- return gpg_program;
-}
+ if (inst_dir)
+ {
+ gpg = find_program_in_dir (inst_dir, name);
+ }
+ /* 2. Try to find gpg.exe using that ancient registry key. */
+ if (!gpg)
+ {
+ char *dir;
-const char *
-_gpgme_get_gpgsm_path (void)
-{
- static char *gpgsm_program;
- const char *inst_dir;
+ dir = read_w32_registry_string ("HKEY_LOCAL_MACHINE",
+ GNUPG_REGKEY_2,
+ "Install Directory");
+ if (dir)
+ {
+ gpg = find_program_in_dir (dir, name);
+ free (dir);
+ }
+ }
- inst_dir = _gpgme_get_inst_dir ();
- LOCK (get_path_lock);
- if (!gpgsm_program)
- gpgsm_program = find_program_in_inst_dir (inst_dir, "gpgsm.exe");
- if (!gpgsm_program)
- gpgsm_program = find_program_at_standard_place ("GNU\\GnuPG\\gpgsm.exe");
- UNLOCK (get_path_lock);
- return gpgsm_program;
+ /* 3. Try to find gpg.exe below CSIDL_PROGRAM_FILES. */
+ if (!gpg)
+ {
+ name = default_gpg_name? default_gpg_name : "GNU\\GnuPG\\gpg.exe";
+ gpg = find_program_at_standard_place (name);
+ }
+
+ /* 4. Print a debug message if not found. */
+ if (!gpg)
+ _gpgme_debug (DEBUG_ENGINE, "_gpgme_get_gpg_path: '%s' not found", name);
+
+ return gpg;
}
-const char *
+/* This function is only called by get_gpgconf_item and may not be
+ called concurrently. */
+char *
_gpgme_get_gpgconf_path (void)
{
- static char *gpgconf_program;
- const char *inst_dir;
+ char *gpgconf = NULL;
+ const char *inst_dir, *name;
+
+ name = default_gpgconf_name? get_basename(default_gpgconf_name):"gpgconf.exe";
+ /* 1. Try to find gpgconf.exe in the installation directory of gpgme. */
inst_dir = _gpgme_get_inst_dir ();
- LOCK (get_path_lock);
- if (!gpgconf_program)
- gpgconf_program = find_program_in_inst_dir (inst_dir, "gpgconf.exe");
- if (!gpgconf_program)
- gpgconf_program
- = find_program_at_standard_place ("GNU\\GnuPG\\gpgconf.exe");
- UNLOCK (get_path_lock);
- return gpgconf_program;
-}
+ if (inst_dir)
+ {
+ gpgconf = find_program_in_dir (inst_dir, name);
+ }
+
+ /* 2. Try to find gpgconf.exe from GnuPG >= 2.1 below CSIDL_PROGRAM_FILES. */
+ if (!gpgconf)
+ {
+ const char *name2 = (default_gpgconf_name ? default_gpgconf_name
+ /**/ : "GnuPG\\bin\\gpgconf.exe");
+ gpgconf = find_program_at_standard_place (name2);
+ }
+ /* 3. Try to find gpgconf.exe using the Windows registry. */
+ if (!gpgconf)
+ {
+ char *dir;
-const char *
-_gpgme_get_g13_path (void)
-{
- static char *g13_program;
- const char *inst_dir;
+ dir = read_w32_registry_string (NULL,
+ GNUPG_REGKEY_2,
+ "Install Directory");
+ if (!dir)
+ {
+ char *tmp = read_w32_registry_string (NULL,
+ GNUPG_REGKEY_3,
+ "Install Directory");
+ if (tmp)
+ {
+ dir = _gpgme_strconcat (tmp, "\\bin", NULL);
+ free (tmp);
+ if (!dir)
+ return NULL;
+ }
+ }
+ if (dir)
+ {
+ gpgconf = find_program_in_dir (dir, name);
+ free (dir);
+ }
+ }
- inst_dir = _gpgme_get_inst_dir ();
- LOCK (get_path_lock);
- if (!g13_program)
- g13_program = find_program_in_inst_dir (inst_dir, "g13.exe");
- if (!g13_program)
- g13_program = find_program_at_standard_place ("GNU\\GnuPG\\g13.exe");
- UNLOCK (get_path_lock);
- return g13_program;
+ /* 4. Try to find gpgconf.exe from Gpg4win below CSIDL_PROGRAM_FILES. */
+ if (!gpgconf)
+ {
+ gpgconf = find_program_at_standard_place ("GNU\\GnuPG\\gpgconf.exe");
+ }
+
+ /* 5. Try to find gpgconf.exe relative to us. */
+ if (!gpgconf && inst_dir)
+ {
+ char *dir = _gpgme_strconcat (inst_dir, "\\..\\..\\GnuPG\\bin");
+ gpgconf = find_program_in_dir (dir, name);
+ free (dir);
+ }
+
+ /* 5. Print a debug message if not found. */
+ if (!gpgconf)
+ _gpgme_debug (DEBUG_ENGINE, "_gpgme_get_gpgconf_path: '%s' not found",name);
+
+ return gpgconf;
}
inst_dir = _gpgme_get_inst_dir ();
LOCK (get_path_lock);
if (!w32spawn_program)
- w32spawn_program = find_program_in_inst_dir (inst_dir,"gpgme-w32spawn.exe");
- if (!w32spawn_program)
- w32spawn_program
- = find_program_at_standard_place ("GNU\\GnuPG\\gpgme-w32spawn.exe");
+ w32spawn_program = find_program_in_dir (inst_dir, "gpgme-w32spawn.exe");
UNLOCK (get_path_lock);
return w32spawn_program;
}
does not exist at the time of the call to mkstemp. TMPL is
overwritten with the result. */
static int
-mkstemp (char *tmpl)
+my_mkstemp (char *tmpl)
{
int len;
char *XXXXXX;
}
}
- tmpname = malloc (strlen (tmp) + 13 + 1);
+ tmpname = _gpgme_strconcat (tmp, "\\gpgme-XXXXXX", NULL);
if (!tmpname)
return -1;
- strcpy (stpcpy (tmpname, tmp), "\\gpgme-XXXXXX");
- *fd = mkstemp (tmpname);
- if (fd < 0)
- return -1;
+ *fd = my_mkstemp (tmpname);
+ if (*fd < 0)
+ {
+ free (tmpname);
+ return -1;
+ }
*name = tmpname;
return 0;