1 /* w32-util.c - Utility functions for the W32 API
2 * Copyright (C) 1999 Free Software Foundation, Inc
3 * Copyright (C) 2001 Werner Koch (dd9jn)
4 * Copyright (C) 2001, 2002, 2003, 2004, 2007, 2013 g10 Code GmbH
6 * This file is part of GPGME.
8 * GPGME is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as
10 * published by the Free Software Foundation; either version 2.1 of
11 * the License, or (at your option) any later version.
13 * GPGME is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this program; if not, see <https://gnu.org/licenses/>.
20 * SPDX-License-Identifier: LGPL-2.1-or-later
32 #ifdef HAVE_SYS_TIME_H
33 # include <sys/time.h>
35 #ifdef HAVE_SYS_TYPES_H
36 # include <sys/types.h>
38 #ifdef HAVE_SYS_STAT_H
39 # include <sys/stat.h>
47 #if __MINGW64_VERSION_MAJOR >= 2
48 # define _WIN32_IE 0x0501 /* Required by mingw64 toolkit. */
50 # define _WIN32_IE 0x0400 /* Required for SHGetSpecialFolderPathA. */
53 /* We need to include the windows stuff here prior to shlobj.h so that
54 we get the right winsock version. This is usually done in util.h
55 but that header also redefines some Windows functions which we need
56 to avoid unless having included shlobj.h. */
69 #define HAVE_ALLOW_SET_FOREGROUND_WINDOW 1
74 /* The Registry key used by GNUPG. */
76 # define GNUPG_REGKEY_2 "Software\\Wow6432Node\\GNU\\GnuPG"
78 # define GNUPG_REGKEY_2 "Software\\GNU\\GnuPG"
81 # define GNUPG_REGKEY_3 "Software\\Wow6432Node\\GnuPG"
83 # define GNUPG_REGKEY_3 "Software\\GnuPG"
86 DEFINE_STATIC_LOCK (get_path_lock);
88 /* The module handle of this DLL. If we are linked statically,
89 dllmain does not exists and thus the value of my_hmodule will be
90 NULL. The effect is that a GetModuleFileName always returns the
91 file name of the DLL or executable which contains the gpgme code. */
92 static HMODULE my_hmodule;
94 /* These variables store the malloced name of alternative default
95 binaries. The are set only once by gpgme_set_global_flag. */
96 static char *default_gpg_name;
97 static char *default_gpgconf_name;
98 /* If this variable is not NULL the value is assumed to be the
99 installation directory. The variable may only be set once by
100 gpgme_set_global_flag and accessed by _gpgme_get_inst_dir. */
101 static char *override_inst_dir;
105 static GPG_ERR_INLINE void *
106 dlopen (const char * name, int flag)
108 void * hd = LoadLibrary (name);
114 static GPG_ERR_INLINE void *
115 dlsym (void * hd, const char * sym)
119 void * fnc = GetProcAddress (hd, sym);
127 static GPG_ERR_INLINE int
139 /* Return a malloced string encoded in UTF-8 from the wide char input
140 string STRING. Caller must free this value. Returns NULL and sets
141 ERRNO on failure. Calling this function with STRING set to NULL is
144 wchar_to_utf8 (const wchar_t *string)
149 n = WideCharToMultiByte (CP_UTF8, 0, string, -1, NULL, 0, NULL, NULL);
152 gpg_err_set_errno (EINVAL);
156 result = malloc (n+1);
160 n = WideCharToMultiByte (CP_UTF8, 0, string, -1, result, n, NULL, NULL);
164 gpg_err_set_errno (EINVAL);
171 /* Return a malloced wide char string from an UTF-8 encoded input
172 string STRING. Caller must free this value. On failure returns
173 NULL; caller may use GetLastError to get the actual error number.
174 Calling this function with STRING set to NULL is not defined. */
176 utf8_to_wchar (const char *string)
182 n = MultiByteToWideChar (CP_UTF8, 0, string, -1, NULL, 0);
186 result = (wchar_t *) malloc ((n+1) * sizeof *result);
190 n = MultiByteToWideChar (CP_UTF8, 0, string, -1, result, n);
200 /* Same as utf8_to_wchar but calling it with NULL returns
201 NULL. So a return value of NULL only indicates failure
202 if STRING is not set to NULL. */
204 utf8_to_wchar0 (const char *string)
209 return utf8_to_wchar (string);
213 /* Replace all forward slashes by backslashes. */
215 replace_slashes (char *string)
217 for (; *string; string++)
223 /* Get the base name of NAME. Returns a pointer into NAME right after
224 the last slash or backslash or to NAME if no slash or backslash
227 get_basename (const char *name)
229 const char *mark, *s;
231 for (mark=NULL, s=name; *s; s++)
232 if (*s == '/' || *s == '\\')
235 return mark? mark+1 : name;
240 _gpgme_allow_set_foreground_window (pid_t pid)
242 #ifdef HAVE_ALLOW_SET_FOREGROUND_WINDOW
243 static int initialized;
244 static BOOL (WINAPI * func)(DWORD);
249 /* Available since W2000; thus we dynload it. */
251 handle = dlopen ("user32.dll", RTLD_LAZY);
254 func = dlsym (handle, "AllowSetForegroundWindow");
263 if (!pid || pid == (pid_t)(-1))
265 TRACE (DEBUG_ENGINE, "gpgme:AllowSetForegroundWindow", NULL,
266 "no action for pid %d", (int)pid);
271 TRACE (DEBUG_ENGINE, "gpgme:AllowSetForegroundWindow", NULL,
272 "called for pid %d; result=%d", (int)pid, rc);
277 TRACE (DEBUG_ENGINE, "gpgme:AllowSetForegroundWindow", NULL,
278 "function not available");
280 #endif /* HAVE_ALLOW_SET_FOREGROUND_WINDOW */
284 /* Wrapper around CancelSynchronousIo which is only available since
287 _gpgme_w32_cancel_synchronous_io (HANDLE thread)
289 static int initialized;
290 static BOOL (WINAPI * func)(DWORD);
295 /* Available since Vista; thus we dynload it. */
297 handle = dlopen ("kernel32.dll", RTLD_LAZY);
300 func = dlsym (handle, "CancelSynchronousIo");
311 if (!func ((DWORD)thread) && GetLastError() != ERROR_NOT_FOUND)
313 TRACE (DEBUG_ENGINE, "gpgme:CancelSynchronousIo", NULL,
314 "called for thread %p: ec=%u",
315 thread, (unsigned int)GetLastError ());
320 TRACE (DEBUG_ENGINE, "gpgme:CancelSynchronousIo", NULL,
321 "function not available");
326 /* Return a string from the W32 Registry or NULL in case of error.
327 Caller must release the return value. A NULL for root is an alias
328 for HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE in turn. */
330 read_w32_registry_string (const char *root, const char *dir, const char *name)
332 HKEY root_key, key_handle;
333 DWORD n1, nbytes, type;
337 root_key = HKEY_CURRENT_USER;
338 else if (!strcmp( root, "HKEY_CLASSES_ROOT"))
339 root_key = HKEY_CLASSES_ROOT;
340 else if (!strcmp( root, "HKEY_CURRENT_USER"))
341 root_key = HKEY_CURRENT_USER;
342 else if (!strcmp( root, "HKEY_LOCAL_MACHINE"))
343 root_key = HKEY_LOCAL_MACHINE;
344 else if (!strcmp( root, "HKEY_USERS"))
345 root_key = HKEY_USERS;
346 else if (!strcmp( root, "HKEY_PERFORMANCE_DATA"))
347 root_key = HKEY_PERFORMANCE_DATA;
348 else if (!strcmp( root, "HKEY_CURRENT_CONFIG"))
349 root_key = HKEY_CURRENT_CONFIG;
353 if (RegOpenKeyExA (root_key, dir, 0, KEY_READ, &key_handle))
356 return NULL; /* no need for a RegClose, so return direct */
357 /* It seems to be common practise to fall back to HKLM. */
358 if (RegOpenKeyExA (HKEY_LOCAL_MACHINE, dir, 0, KEY_READ, &key_handle))
359 return NULL; /* still no need for a RegClose, so return direct */
363 if (RegQueryValueExA (key_handle, name, 0, NULL, NULL, &nbytes))
367 /* Try to fallback to HKLM also vor a missing value. */
368 RegCloseKey (key_handle);
369 if (RegOpenKeyExA (HKEY_LOCAL_MACHINE, dir, 0, KEY_READ, &key_handle))
370 return NULL; /* Nope. */
371 if (RegQueryValueExA (key_handle, name, 0, NULL, NULL, &nbytes))
375 result = malloc (n1);
378 if (RegQueryValueExA (key_handle, name, 0, &type, (LPBYTE) result, &n1))
384 result[nbytes] = 0; /* Make sure it is really a string. */
387 RegCloseKey (key_handle);
392 /* Return the name of the directory with the gpgme DLL or the EXE (if
393 statically linked). May return NULL on severe errors. */
395 _gpgme_get_inst_dir (void)
397 static char *inst_dir;
399 if (override_inst_dir)
400 return override_inst_dir;
402 LOCK (get_path_lock);
407 moddir = malloc ((MAX_PATH+5) * sizeof *moddir);
410 if (!GetModuleFileNameW (my_hmodule, moddir, MAX_PATH))
413 gpg_err_set_errno (ENOENT);
416 inst_dir = wchar_to_utf8 (moddir);
419 char *p = strrchr (inst_dir, '\\');
427 UNLOCK (get_path_lock);
433 find_program_in_dir (const char *dir, const char *name)
437 result = _gpgme_strconcat (dir, "\\", name, NULL);
441 if (_gpgme_access (result, F_OK))
452 find_program_at_standard_place (const char *name)
454 wchar_t path[MAX_PATH];
457 /* See https://wiki.tcl-lang.org/page/Getting+Windows+%22special+folders%22+with+Ffidl for details on compatibility.
459 We First try the generic place and then fallback to the x86
460 (i.e. 32 bit) place. This will prefer a 64 bit of the program
461 over a 32 bit version on 64 bit Windows if installed. */
462 if (SHGetSpecialFolderPathW (NULL, path, CSIDL_PROGRAM_FILES, 0))
464 char *utf8_path = wchar_to_utf8 (path);
465 result = _gpgme_strconcat (utf8_path, "\\", name, NULL);
467 if (result && _gpgme_access (result, F_OK))
474 && SHGetSpecialFolderPathW (NULL, path, CSIDL_PROGRAM_FILESX86, 0))
476 char *utf8_path = wchar_to_utf8 (path);
477 result = _gpgme_strconcat (utf8_path, "\\", name, NULL);
479 if (result && _gpgme_access (result, F_OK))
489 /* Set the default name for the gpg binary. This function may only be
490 called by gpgme_set_global_flag. Returns 0 on success. */
492 _gpgme_set_default_gpg_name (const char *name)
494 if (!default_gpg_name)
496 default_gpg_name = _gpgme_strconcat (name, ".exe", NULL);
497 if (default_gpg_name)
498 replace_slashes (default_gpg_name);
500 return !default_gpg_name;
503 /* Set the default name for the gpgconf binary. This function may only be
504 called by gpgme_set_global_flag. Returns 0 on success. */
506 _gpgme_set_default_gpgconf_name (const char *name)
508 if (!default_gpgconf_name)
510 default_gpgconf_name = _gpgme_strconcat (name, ".exe", NULL);
511 if (default_gpgconf_name)
512 replace_slashes (default_gpgconf_name);
514 return !default_gpgconf_name;
518 /* Set the override installation directory. This function may only be
519 called by gpgme_set_global_flag. Returns 0 on success. */
521 _gpgme_set_override_inst_dir (const char *dir)
523 if (!override_inst_dir)
525 override_inst_dir = strdup (dir);
526 if (override_inst_dir)
528 replace_slashes (override_inst_dir);
529 /* Remove a trailing slash. */
530 if (*override_inst_dir
531 && override_inst_dir[strlen (override_inst_dir)-1] == '\\')
532 override_inst_dir[strlen (override_inst_dir)-1] = 0;
535 return !override_inst_dir;
539 /* Return the full file name of the GPG binary. This function is used
540 iff gpgconf was not found and thus it can be assumed that gpg2 is
541 not installed. This function is only called by get_gpgconf_item
542 and may not be called concurrently. */
544 _gpgme_get_gpg_path (void)
547 const char *name, *inst_dir;
549 name = default_gpg_name? get_basename (default_gpg_name) : "gpg.exe";
551 /* 1. Try to find gpg.exe in the installation directory of gpgme. */
552 inst_dir = _gpgme_get_inst_dir ();
555 gpg = find_program_in_dir (inst_dir, name);
558 /* 2. Try to find gpg.exe using that ancient registry key. */
563 dir = read_w32_registry_string ("HKEY_LOCAL_MACHINE",
565 "Install Directory");
568 gpg = find_program_in_dir (dir, name);
573 /* 3. Try to find gpg.exe below CSIDL_PROGRAM_FILES. */
576 name = default_gpg_name? default_gpg_name : "GNU\\GnuPG\\gpg.exe";
577 gpg = find_program_at_standard_place (name);
580 /* 4. Print a debug message if not found. */
582 _gpgme_debug (NULL, DEBUG_ENGINE, -1, NULL, NULL, NULL,
583 "_gpgme_get_gpg_path: '%s' not found", name);
589 /* This function is only called by get_gpgconf_item and may not be
590 called concurrently. */
592 _gpgme_get_gpgconf_path (void)
594 char *gpgconf = NULL;
595 const char *inst_dir, *name;
597 name = default_gpgconf_name? get_basename(default_gpgconf_name):"gpgconf.exe";
599 /* 1. Try to find gpgconf.exe in the installation directory of gpgme. */
600 inst_dir = _gpgme_get_inst_dir ();
603 gpgconf = find_program_in_dir (inst_dir, name);
606 /* 2. Try to find gpgconf.exe from GnuPG >= 2.1 below CSIDL_PROGRAM_FILES. */
609 const char *name2 = (default_gpgconf_name ? default_gpgconf_name
610 /**/ : "GnuPG\\bin\\gpgconf.exe");
611 gpgconf = find_program_at_standard_place (name2);
614 /* 3. Try to find gpgconf.exe using the Windows registry. */
619 dir = read_w32_registry_string (NULL,
621 "Install Directory");
624 char *tmp = read_w32_registry_string (NULL,
626 "Install Directory");
629 dir = _gpgme_strconcat (tmp, "\\bin", NULL);
637 gpgconf = find_program_in_dir (dir, name);
642 /* 4. Try to find gpgconf.exe from Gpg4win below CSIDL_PROGRAM_FILES. */
645 gpgconf = find_program_at_standard_place ("GNU\\GnuPG\\gpgconf.exe");
648 /* 5. Try to find gpgconf.exe relative to us. */
649 if (!gpgconf && inst_dir)
651 char *dir = _gpgme_strconcat (inst_dir, "\\..\\..\\GnuPG\\bin", NULL);
652 gpgconf = find_program_in_dir (dir, name);
656 /* 5. Print a debug message if not found. */
658 _gpgme_debug (NULL, DEBUG_ENGINE, -1, NULL, NULL, NULL,
659 "_gpgme_get_gpgconf_path: '%s' not found",name);
666 _gpgme_get_w32spawn_path (void)
668 static char *w32spawn_program;
669 const char *inst_dir;
671 inst_dir = _gpgme_get_inst_dir ();
672 LOCK (get_path_lock);
673 if (!w32spawn_program)
674 w32spawn_program = find_program_in_dir (inst_dir, "gpgme-w32spawn.exe");
675 UNLOCK (get_path_lock);
676 return w32spawn_program;
680 /* Return an integer value from gpgme specific configuration
681 entries. VALUE receives that value; function returns true if a value
682 has been configured and false if not. */
684 _gpgme_get_conf_int (const char *key, int *value)
686 char *tmp = read_w32_registry_string (NULL, "Software\\GNU\\gpgme", key);
696 /* mkstemp extracted from libc/sysdeps/posix/tempname.c. Copyright
697 (C) 1991-1999, 2000, 2001, 2006 Free Software Foundation, Inc.
699 The GNU C Library is free software; you can redistribute it and/or
700 modify it under the terms of the GNU Lesser General Public
701 License as published by the Free Software Foundation; either
702 version 2.1 of the License, or (at your option) any later version. */
704 static const char letters[] =
705 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
707 /* Generate a temporary file name based on TMPL. TMPL must match the
708 rules for mk[s]temp (i.e. end in "XXXXXX"). The name constructed
709 does not exist at the time of the call to mkstemp. TMPL is
710 overwritten with the result. */
712 my_mkstemp (char *tmpl)
716 static uint64_t value;
717 uint64_t random_time_bits;
720 int save_errno = errno;
722 /* A lower bound on the number of temporary files to attempt to
723 generate. The maximum total number of temporary file names that
724 can exist for a given template is 62**6. It should never be
725 necessary to try all these combinations. Instead if a reasonable
726 number of names is tried (we define reasonable as 62**3) fail to
727 give the system administrator the chance to remove the problems. */
728 #define ATTEMPTS_MIN (62 * 62 * 62)
730 /* The number of times to attempt to generate a temporary file. To
731 conform to POSIX, this must be no smaller than TMP_MAX. */
732 #if ATTEMPTS_MIN < TMP_MAX
733 unsigned int attempts = TMP_MAX;
735 unsigned int attempts = ATTEMPTS_MIN;
739 if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
741 gpg_err_set_errno (EINVAL);
745 /* This is where the Xs start. */
746 XXXXXX = &tmpl[len - 6];
748 /* Get some more or less random data. */
752 GetSystemTimeAsFileTime (&ft);
753 random_time_bits = (((uint64_t)ft.dwHighDateTime << 32)
754 | (uint64_t)ft.dwLowDateTime);
756 value += random_time_bits ^ ath_self ();
758 for (count = 0; count < attempts; value += 7777, ++count)
762 /* Fill in the random bits. */
763 XXXXXX[0] = letters[v % 62];
765 XXXXXX[1] = letters[v % 62];
767 XXXXXX[2] = letters[v % 62];
769 XXXXXX[3] = letters[v % 62];
771 XXXXXX[4] = letters[v % 62];
773 XXXXXX[5] = letters[v % 62];
775 fd = open (tmpl, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
778 gpg_err_set_errno (save_errno);
781 else if (errno != EEXIST)
785 /* We got out of the loop because we ran out of combinations to try. */
786 gpg_err_set_errno (EEXIST);
792 _gpgme_mkstemp (int *fd, char **name)
794 char tmp[MAX_PATH + 2];
801 err = GetTempPathA (MAX_PATH + 1, tmp);
802 if (err == 0 || err > MAX_PATH + 1)
803 strcpy (tmp,"c:\\windows\\temp");
806 int len = strlen(tmp);
808 /* GetTempPath may return with \ on the end */
809 while(len > 0 && tmp[len - 1] == '\\')
816 tmpname = _gpgme_strconcat (tmp, "\\gpgme-XXXXXX", NULL);
819 *fd = my_mkstemp (tmpname);
831 /* Like access but using windows _waccess */
833 _gpgme_access (const char *path, int mode)
835 wchar_t *u16 = utf8_to_wchar0 (path);
836 int r = _waccess (u16, mode);
843 /* Like CreateProcessA but mapping the arguments to wchar API */
844 int _gpgme_create_process_utf8 (const char *application_name_utf8,
845 char *command_line_utf8,
846 LPSECURITY_ATTRIBUTES lpProcessAttributes,
847 LPSECURITY_ATTRIBUTES lpThreadAttributes,
848 BOOL bInheritHandles,
849 DWORD dwCreationFlags,
851 char *working_directory_utf8,
853 LPPROCESS_INFORMATION lpProcessInformation)
856 wchar_t *application_name = utf8_to_wchar0 (application_name_utf8);
857 wchar_t *command_line = utf8_to_wchar0 (command_line_utf8);
858 wchar_t *working_directory = utf8_to_wchar0 (working_directory_utf8);
861 memset (&siw, 0, sizeof siw);
864 siw.cb = sizeof (siw);
865 siw.dwFlags = si->dwFlags;
866 siw.wShowWindow = si->wShowWindow;
867 siw.hStdInput = si->hStdInput;
868 siw.hStdOutput = si->hStdOutput;
869 siw.hStdError = si->hStdError;
872 siw.dwXSize = si->dwXSize;
873 siw.dwYSize = si->dwYSize;
874 siw.dwXCountChars = si->dwXCountChars;
875 siw.dwYCountChars = si->dwYCountChars;
876 siw.dwFillAttribute = si->dwFillAttribute;
877 siw.lpDesktop = utf8_to_wchar0 (si->lpDesktop);
878 siw.lpTitle = utf8_to_wchar0 (si->lpTitle);
881 ret = CreateProcessW (application_name,
890 lpProcessInformation);
892 free (siw.lpDesktop);
893 free (application_name);
895 free (working_directory);
899 /* Entry point called by the DLL loader. */
902 DllMain (HINSTANCE hinst, DWORD reason, LPVOID reserved)
906 if (reason == DLL_PROCESS_ATTACH)
911 #endif /*DLL_EXPORT*/