1
1
/* homedir.c - Setup the home directory.
2
* Copyright (C) 2004 Free Software Foundation, Inc.
2
* Copyright (C) 2004, 2006, 2007 Free Software Foundation, Inc.
4
4
* This file is part of GnuPG.
6
6
* GnuPG is free software; you can redistribute it and/or modify
7
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
8
* the Free Software Foundation; either version 3 of the License, or
9
9
* (at your option) any later version.
11
11
* GnuPG is distributed in the hope that it will be useful,
42
41
#include "sysutils.h"
44
/* Set up the default home directory. The usual --homedir option
45
should be parsed later. */
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
47
default_homedir (void)
89
standard_homedir (void)
91
#ifdef HAVE_W32_SYSTEM
92
static const char *dir;
51
dir = getenv("GNUPGHOME");
52
#ifdef HAVE_W32_SYSTEM
54
dir = read_w32_registry_string (NULL, "Software\\GNU\\GnuPG", "HomeDir");
57
96
char path[MAX_PATH];
59
/* fixme: It might be better to use LOCAL_APPDATA because this
60
is defined as "non roaming" and thus more likely to be kept
98
/* It might be better to use LOCAL_APPDATA because this is
99
defined as "non roaming" and thus more likely to be kept
61
100
locally. For private keys this is desired. However, given
62
101
that many users copy private keys anyway forth and back,
63
using a system roaming serives might be better than to let
102
using a system roaming services might be better than to let
64
103
them do it manually. A security conscious user will anyway
65
104
use the registry entry to have better control. */
66
if (SHGetFolderPath(NULL, CSIDL_APPDATA|CSIDL_FLAG_CREATE,
105
if (w32_shgetfolderpath (NULL, CSIDL_APPDATA|CSIDL_FLAG_CREATE,
69
108
char *tmp = xmalloc (strlen (path) + 6 +1);
70
109
strcpy (stpcpy (tmp, path), "\\gnupg");
73
/* Try to create the directory if it does not yet
112
/* Try to create the directory if it does not yet exists. */
75
113
if (access (dir, F_OK))
76
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 ();
79
160
#endif /*HAVE_W32_SYSTEM*/
80
161
if (!dir || !*dir)
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 default socket name used by DirMngr. */
295
dirmngr_socket_name (void)
297
#ifdef HAVE_W32_SYSTEM
305
/* We need something akin CSIDL_COMMON_PROGRAMS, but local
307
if (w32_shgetfolderpath (NULL, CSIDL_WINDOWS, NULL, 0, s1) < 0)
308
strcpy (s1, "C:\\WINDOWS");
309
s2 = DIRSEP_S "S.dirmngr";
310
name = xmalloc (strlen (s1) + strlen (s2) + 1);
311
strcpy (stpcpy (name, s1), s2);
314
#else /*!HAVE_W32_SYSTEM*/
315
return "/var/run/dirmngr/socket";
316
#endif /*!HAVE_W32_SYSTEM*/
321
/* Return the file name of a helper tool. WHICH is one of the
322
GNUPG_MODULE_NAME_foo constants. */
324
gnupg_module_name (int which)
328
#define X(a,b) do { \
332
s = gnupg_ ## a (); \
333
s2 = DIRSEP_S b EXEEXT_S; \
334
name = xmalloc (strlen (s) + strlen (s2) + 1); \
335
strcpy (stpcpy (name, s), s2); \
342
case GNUPG_MODULE_NAME_AGENT:
343
#ifdef GNUPG_DEFAULT_AGENT
344
return GNUPG_DEFAULT_AGENT;
346
X(bindir, "gpg-agent");
349
case GNUPG_MODULE_NAME_PINENTRY:
350
#ifdef GNUPG_DEFAULT_PINENTRY
351
return GNUPG_DEFAULT_PINENTRY;
353
X(bindir, "pinentry");
356
case GNUPG_MODULE_NAME_SCDAEMON:
357
#ifdef GNUPG_DEFAULT_SCDAEMON
358
return GNUPG_DEFAULT_SCDAEMON;
360
X(bindir, "scdaemon");
363
case GNUPG_MODULE_NAME_DIRMNGR:
364
#ifdef GNUPG_DEFAULT_DIRMNGR
365
return GNUPG_DEFAULT_DIRMNGR;
367
X(bindir, "dirmngr");
370
case GNUPG_MODULE_NAME_PROTECT_TOOL:
371
#ifdef GNUPG_DEFAULT_PROTECT_TOOL
372
return GNUPG_DEFAULT_PROTECT_TOOL;
374
X(libexecdir, "gpg-protect-tool");
377
case GNUPG_MODULE_NAME_CHECK_PATTERN:
378
X(libexecdir, "gpg-check-pattern");
380
case GNUPG_MODULE_NAME_GPGSM:
383
case GNUPG_MODULE_NAME_GPG: