1 /* homedir.c - Setup the home directory.
2 * Copyright (C) 2004, 2006, 2007 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
25 #ifdef HAVE_W32_SYSTEM
28 #define CSIDL_APPDATA 0x001a
30 #ifndef CSIDL_LOCAL_APPDATA
31 #define CSIDL_LOCAL_APPDATA 0x001c
33 #ifndef CSIDL_COMMON_APPDATA
34 #define CSIDL_COMMON_APPDATA 0x0023
36 #ifndef CSIDL_FLAG_CREATE
37 #define CSIDL_FLAG_CREATE 0x8000
39 #endif /*HAVE_W32_SYSTEM*/
47 /* This is a helper function to load a Windows function from either of
49 #ifdef HAVE_W32_SYSTEM
51 w32_shgetfolderpath (HWND a
, int b
, HANDLE c
, DWORD d
, LPSTR e
)
53 static int initialized
;
54 static HRESULT (WINAPI
* func
)(HWND
,int,HANDLE
,DWORD
,LPSTR
);
58 static char *dllnames
[] = { "shell32.dll", "shfolder.dll", NULL
};
64 for (i
=0, handle
= NULL
; !handle
&& dllnames
[i
]; i
++)
66 handle
= dlopen (dllnames
[i
], RTLD_LAZY
);
69 func
= dlsym (handle
, "SHGetFolderPathA");
80 return func (a
,b
,c
,d
,e
);
84 #endif /*HAVE_W32_SYSTEM*/
87 /* Get the standard home directory. In general this function should
88 not be used as it does not consider a registry value (under W32) or
89 the GNUPGHOME environment variable. It is better to use
92 standard_homedir (void)
94 #ifdef HAVE_W32_SYSTEM
95 static const char *dir
;
101 /* It might be better to use LOCAL_APPDATA because this is
102 defined as "non roaming" and thus more likely to be kept
103 locally. For private keys this is desired. However, given
104 that many users copy private keys anyway forth and back,
105 using a system roaming services might be better than to let
106 them do it manually. A security conscious user will anyway
107 use the registry entry to have better control. */
108 if (w32_shgetfolderpath (NULL
, CSIDL_APPDATA
|CSIDL_FLAG_CREATE
,
111 char *tmp
= xmalloc (strlen (path
) + 6 +1);
112 strcpy (stpcpy (tmp
, path
), "\\gnupg");
115 /* Try to create the directory if it does not yet exists. */
116 if (access (dir
, F_OK
))
117 CreateDirectory (dir
, NULL
);
120 dir
= GNUPG_DEFAULT_HOMEDIR
;
123 #else/*!HAVE_W32_SYSTEM*/
124 return GNUPG_DEFAULT_HOMEDIR
;
125 #endif /*!HAVE_W32_SYSTEM*/
128 /* Set up the default home directory. The usual --homedir option
129 should be parsed later. */
131 default_homedir (void)
135 dir
= getenv ("GNUPGHOME");
136 #ifdef HAVE_W32_SYSTEM
139 static const char *saved_dir
;
147 tmp
= read_w32_registry_string (NULL
, "Software\\GNU\\GnuPG",
159 saved_dir
= standard_homedir ();
163 #endif /*HAVE_W32_SYSTEM*/
165 dir
= GNUPG_DEFAULT_HOMEDIR
;
171 #ifdef HAVE_W32_SYSTEM
176 static char dir
[MAX_PATH
+5];
182 if ( !GetModuleFileName ( NULL
, dir
, MAX_PATH
) )
184 log_debug ("GetModuleFileName failed: %s\n", w32_strerror (0));
188 p
= strrchr (dir
, DIRSEP_C
);
193 log_debug ("bad filename `%s' returned for this process\n", dir
);
200 /* Fallback to the hardwired value. */
201 return GNUPG_LIBEXECDIR
;
213 if (w32_shgetfolderpath (NULL
, CSIDL_COMMON_APPDATA
,
216 char *tmp
= xmalloc (strlen (path
) + 4 +1);
217 strcpy (stpcpy (tmp
, path
), "\\GNU");
219 /* No auto create of the directory. Either the installer or
220 the admin has to create these directories. */
224 /* Ooops: Not defined - probably an old Windows version.
225 Use the installation directory instead. */
226 dir
= xstrdup (w32_rootdir ());
232 #endif /*HAVE_W32_SYSTEM*/
237 /* Return the name of the sysconfdir. This is a static string. This
238 function is required because under Windows we can't simply compile
241 gnupg_sysconfdir (void)
243 #ifdef HAVE_W32_SYSTEM
249 s1
= w32_commondir ();
250 s2
= DIRSEP_S
"etc" DIRSEP_S
"gnupg";
251 name
= xmalloc (strlen (s1
) + strlen (s2
) + 1);
252 strcpy (stpcpy (name
, s1
), s2
);
255 #else /*!HAVE_W32_SYSTEM*/
256 return GNUPG_SYSCONFDIR
;
257 #endif /*!HAVE_W32_SYSTEM*/
264 #ifdef HAVE_W32_SYSTEM
265 return w32_rootdir ();
266 #else /*!HAVE_W32_SYSTEM*/
268 #endif /*!HAVE_W32_SYSTEM*/
272 /* Return the name of the libexec directory. The name is allocated in
273 a static area on the first use. This function won't fail. */
275 gnupg_libexecdir (void)
277 #ifdef HAVE_W32_SYSTEM
278 return w32_rootdir ();
279 #else /*!HAVE_W32_SYSTEM*/
280 return GNUPG_LIBEXECDIR
;
281 #endif /*!HAVE_W32_SYSTEM*/
287 #ifdef HAVE_W32_SYSTEM
294 s2
= DIRSEP_S
"lib" DIRSEP_S
"gnupg";
295 name
= xmalloc (strlen (s1
) + strlen (s2
) + 1);
296 strcpy (stpcpy (name
, s1
), s2
);
299 #else /*!HAVE_W32_SYSTEM*/
301 #endif /*!HAVE_W32_SYSTEM*/
307 #ifdef HAVE_W32_SYSTEM
314 s2
= DIRSEP_S
"share" DIRSEP_S
"gnupg";
315 name
= xmalloc (strlen (s1
) + strlen (s2
) + 1);
316 strcpy (stpcpy (name
, s1
), s2
);
319 #else /*!HAVE_W32_SYSTEM*/
320 return GNUPG_DATADIR
;
321 #endif /*!HAVE_W32_SYSTEM*/
326 gnupg_localedir (void)
328 #ifdef HAVE_W32_SYSTEM
335 s2
= DIRSEP_S
"share" DIRSEP_S
"locale";
336 name
= xmalloc (strlen (s1
) + strlen (s2
) + 1);
337 strcpy (stpcpy (name
, s1
), s2
);
340 #else /*!HAVE_W32_SYSTEM*/
342 #endif /*!HAVE_W32_SYSTEM*/
346 /* Return the default socket name used by DirMngr. */
348 dirmngr_socket_name (void)
350 #ifdef HAVE_W32_SYSTEM
358 /* We need something akin CSIDL_COMMON_PROGRAMS, but local
360 if (w32_shgetfolderpath (NULL
, CSIDL_WINDOWS
, NULL
, 0, s1
) < 0)
361 strcpy (s1
, "C:\\WINDOWS");
362 s2
= DIRSEP_S
"S.dirmngr";
363 name
= xmalloc (strlen (s1
) + strlen (s2
) + 1);
364 strcpy (stpcpy (name
, s1
), s2
);
367 #else /*!HAVE_W32_SYSTEM*/
368 return "/var/run/dirmngr/socket";
369 #endif /*!HAVE_W32_SYSTEM*/
374 /* Return the file name of a helper tool. WHICH is one of the
375 GNUPG_MODULE_NAME_foo constants. */
377 gnupg_module_name (int which
)
381 #define X(a,b) do { \
385 s = gnupg_ ## a (); \
386 s2 = DIRSEP_S b EXEEXT_S; \
387 name = xmalloc (strlen (s) + strlen (s2) + 1); \
388 strcpy (stpcpy (name, s), s2); \
395 case GNUPG_MODULE_NAME_AGENT
:
396 #ifdef GNUPG_DEFAULT_AGENT
397 return GNUPG_DEFAULT_AGENT
;
399 X(bindir
, "gpg-agent");
402 case GNUPG_MODULE_NAME_PINENTRY
:
403 #ifdef GNUPG_DEFAULT_PINENTRY
404 return GNUPG_DEFAULT_PINENTRY
;
406 X(bindir
, "pinentry");
409 case GNUPG_MODULE_NAME_SCDAEMON
:
410 #ifdef GNUPG_DEFAULT_SCDAEMON
411 return GNUPG_DEFAULT_SCDAEMON
;
413 X(bindir
, "scdaemon");
416 case GNUPG_MODULE_NAME_DIRMNGR
:
417 #ifdef GNUPG_DEFAULT_DIRMNGR
418 return GNUPG_DEFAULT_DIRMNGR
;
420 X(bindir
, "dirmngr");
423 case GNUPG_MODULE_NAME_PROTECT_TOOL
:
424 #ifdef GNUPG_DEFAULT_PROTECT_TOOL
425 return GNUPG_DEFAULT_PROTECT_TOOL
;
427 X(libexecdir
, "gpg-protect-tool");
430 case GNUPG_MODULE_NAME_CHECK_PATTERN
:
431 X(libexecdir
, "gpg-check-pattern");
433 case GNUPG_MODULE_NAME_GPGSM
:
436 case GNUPG_MODULE_NAME_GPG
:
439 case GNUPG_MODULE_NAME_CONNECT_AGENT
:
440 X(bindir
, "gpg-connect-agent");
442 case GNUPG_MODULE_NAME_GPGCONF
:
443 X(bindir
, "gpgconf");