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_FLAG_CREATE
34 #define CSIDL_FLAG_CREATE 0x8000
36 #endif /*HAVE_W32_SYSTEM*/
44 /* This is a helper function to load a Windows function from either of
46 #ifdef HAVE_W32_SYSTEM
48 w32_shgetfolderpath (HWND a
, int b
, HANDLE c
, DWORD d
, LPSTR e
)
50 static int initialized
;
51 static HRESULT (WINAPI
* func
)(HWND
,int,HANDLE
,DWORD
,LPSTR
);
55 static char *dllnames
[] = { "shell32.dll", "shfolder.dll", NULL
};
61 for (i
=0, handle
= NULL
; !handle
&& dllnames
[i
]; i
++)
63 handle
= dlopen (dllnames
[i
], RTLD_LAZY
);
66 func
= dlsym (handle
, "SHGetFolderPathA");
77 return func (a
,b
,c
,d
,e
);
81 #endif /*HAVE_W32_SYSTEM*/
84 /* Get the standard home directory. In general this function should
85 not be used as it does not consider a registry value (under W32) or
86 the GNUPGHOME encironment variable. It is better to use
89 standard_homedir (void)
91 #ifdef HAVE_W32_SYSTEM
92 static const char *dir
;
98 /* It might be better to use LOCAL_APPDATA because this is
99 defined as "non roaming" and thus more likely to be kept
100 locally. For private keys this is desired. However, given
101 that many users copy private keys anyway forth and back,
102 using a system roaming services might be better than to let
103 them do it manually. A security conscious user will anyway
104 use the registry entry to have better control. */
105 if (w32_shgetfolderpath (NULL
, CSIDL_APPDATA
|CSIDL_FLAG_CREATE
,
108 char *tmp
= xmalloc (strlen (path
) + 6 +1);
109 strcpy (stpcpy (tmp
, path
), "\\gnupg");
112 /* Try to create the directory if it does not yet exists. */
113 if (access (dir
, F_OK
))
114 CreateDirectory (dir
, NULL
);
117 dir
= GNUPG_DEFAULT_HOMEDIR
;
120 #else/*!HAVE_W32_SYSTEM*/
121 return GNUPG_DEFAULT_HOMEDIR
;
122 #endif /*!HAVE_W32_SYSTEM*/
125 /* Set up the default home directory. The usual --homedir option
126 should be parsed later. */
128 default_homedir (void)
132 dir
= getenv ("GNUPGHOME");
133 #ifdef HAVE_W32_SYSTEM
136 static const char *saved_dir
;
144 tmp
= read_w32_registry_string (NULL
, "Software\\GNU\\GnuPG",
156 saved_dir
= standard_homedir ();
160 #endif /*HAVE_W32_SYSTEM*/
162 dir
= GNUPG_DEFAULT_HOMEDIR
;
168 #ifdef HAVE_W32_SYSTEM
173 static char dir
[MAX_PATH
+5];
179 if ( !GetModuleFileName ( NULL
, dir
, MAX_PATH
) )
181 log_debug ("GetModuleFileName failed: %s\n", w32_strerror (0));
185 p
= strrchr (dir
, DIRSEP_C
);
190 log_debug ("bad filename `%s' returned for this process\n", dir
);
197 /* Fallback to the hardwired value. */
198 return GNUPG_LIBEXECDIR
;
200 #endif /*HAVE_W32_SYSTEM*/
205 /* Return the name of the sysconfdir. This is a static string. This
206 function is required because under Windows we can't simply compile
209 gnupg_sysconfdir (void)
211 #ifdef HAVE_W32_SYSTEM
218 s2
= DIRSEP_S
"etc" DIRSEP_S
"gnupg";
219 name
= xmalloc (strlen (s1
) + strlen (s2
) + 1);
220 strcpy (stpcpy (name
, s1
), s2
);
223 #else /*!HAVE_W32_SYSTEM*/
224 return GNUPG_SYSCONFDIR
;
225 #endif /*!HAVE_W32_SYSTEM*/
232 #ifdef HAVE_W32_SYSTEM
233 return w32_rootdir ();
234 #else /*!HAVE_W32_SYSTEM*/
236 #endif /*!HAVE_W32_SYSTEM*/
240 /* Return the name of the libexec directory. The name is allocated in
241 a static area on the first use. This function won't fail. */
243 gnupg_libexecdir (void)
245 #ifdef HAVE_W32_SYSTEM
246 return w32_rootdir ();
247 #else /*!HAVE_W32_SYSTEM*/
248 return GNUPG_LIBEXECDIR
;
249 #endif /*!HAVE_W32_SYSTEM*/
255 #ifdef HAVE_W32_SYSTEM
262 s2
= DIRSEP_S
"lib" DIRSEP_S
"gnupg";
263 name
= xmalloc (strlen (s1
) + strlen (s2
) + 1);
264 strcpy (stpcpy (name
, s1
), s2
);
267 #else /*!HAVE_W32_SYSTEM*/
269 #endif /*!HAVE_W32_SYSTEM*/
275 #ifdef HAVE_W32_SYSTEM
282 s2
= DIRSEP_S
"share" DIRSEP_S
"gnupg";
283 name
= xmalloc (strlen (s1
) + strlen (s2
) + 1);
284 strcpy (stpcpy (name
, s1
), s2
);
287 #else /*!HAVE_W32_SYSTEM*/
288 return GNUPG_DATADIR
;
289 #endif /*!HAVE_W32_SYSTEM*/
293 /* Return the file name of a helper tool. WHICH is one of the
294 GNUPG_MODULE_NAME_foo constants. */
296 gnupg_module_name (int which
)
300 #define X(a,b) do { \
304 s = gnupg_ ## a (); \
305 s2 = DIRSEP_S b EXEEXT_S; \
306 name = xmalloc (strlen (s) + strlen (s2) + 1); \
307 strcpy (stpcpy (name, s), s2); \
314 case GNUPG_MODULE_NAME_AGENT
:
315 #ifdef GNUPG_DEFAULT_AGENT
316 return GNUPG_DEFAULT_AGENT
;
318 X(bindir
, "gpg-agent");
321 case GNUPG_MODULE_NAME_PINENTRY
:
322 #ifdef GNUPG_DEFAULT_PINENTRY
323 return GNUPG_DEFAULT_PINENTRY
;
325 X(bindir
, "pinentry");
328 case GNUPG_MODULE_NAME_SCDAEMON
:
329 #ifdef GNUPG_DEFAULT_SCDAEMON
330 return GNUPG_DEFAULT_SCDAEMON
;
332 X(bindir
, "scdaemon");
335 case GNUPG_MODULE_NAME_DIRMNGR
:
336 #ifdef GNUPG_DEFAULT_DIRMNGR
337 return GNUPG_DEFAULT_DIRMNGR
;
339 X(bindir
, "dirmngr");
342 case GNUPG_MODULE_NAME_PROTECT_TOOL
:
343 #ifdef GNUPG_DEFAULT_PROTECT_TOOL
344 return GNUPG_DEFAULT_PROTECT_TOOL
;
346 X(libexecdir
, "gpg-protect-tool");