~ubuntu-branches/ubuntu/oneiric/gnupg2/oneiric-updates

« back to all changes in this revision

Viewing changes to common/homedir.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Viehmann
  • Date: 2008-10-04 10:25:53 UTC
  • mfrom: (5.1.15 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081004102553-fv62pp8dsitxli47
Tags: 2.0.9-3.1
* Non-maintainer upload.
* agent/gpg-agent.c: Deinit the threading library before exec'ing
  the command to run in --daemon mode. And because that still doesn't
  restore the sigprocmask, do that manually. Closes: #499569

Show diffs side-by-side

added added

removed removed

Lines of Context:
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.
3
3
 *
4
4
 * This file is part of GnuPG.
5
5
 *
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.
10
10
 *
11
11
 * GnuPG is distributed in the hope that it will be useful,
14
14
 * GNU General Public License for more details.
15
15
 *
16
16
 * You should have received a copy of the GNU General Public License
17
 
 * along with this program; if not, write to the Free Software
18
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 
17
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19
18
 */
20
19
 
21
20
#include <config.h>
41
40
#include "util.h"
42
41
#include "sysutils.h"
43
42
 
44
 
/* Set up the default home directory.  The usual --homedir option
45
 
   should be parsed later. */
 
43
 
 
44
/* This is a helper function to load a Windows function from either of
 
45
   one DLLs. */
 
46
#ifdef HAVE_W32_SYSTEM
 
47
static HRESULT
 
48
w32_shgetfolderpath (HWND a, int b, HANDLE c, DWORD d, LPSTR e)
 
49
{
 
50
  static int initialized;
 
51
  static HRESULT (WINAPI * func)(HWND,int,HANDLE,DWORD,LPSTR);
 
52
 
 
53
  if (!initialized)
 
54
    {
 
55
      static char *dllnames[] = { "shell32.dll", "shfolder.dll", NULL };
 
56
      void *handle;
 
57
      int i;
 
58
 
 
59
      initialized = 1;
 
60
 
 
61
      for (i=0, handle = NULL; !handle && dllnames[i]; i++)
 
62
        {
 
63
          handle = dlopen (dllnames[i], RTLD_LAZY);
 
64
          if (handle)
 
65
            {
 
66
              func = dlsym (handle, "SHGetFolderPathA");
 
67
              if (!func)
 
68
                {
 
69
                  dlclose (handle);
 
70
                  handle = NULL;
 
71
                }
 
72
            }
 
73
        }
 
74
    }
 
75
 
 
76
  if (func)
 
77
    return func (a,b,c,d,e);
 
78
  else
 
79
    return -1;
 
80
}
 
81
#endif /*HAVE_W32_SYSTEM*/
 
82
 
 
83
 
 
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
 
87
   default_homedir(). */
46
88
const char *
47
 
default_homedir (void)
 
89
standard_homedir (void)
48
90
{
49
 
  const char *dir;
 
91
#ifdef HAVE_W32_SYSTEM
 
92
  static const char *dir;
50
93
 
51
 
  dir = getenv("GNUPGHOME");
52
 
#ifdef HAVE_W32_SYSTEM
53
 
  if (!dir || !*dir)
54
 
    dir = read_w32_registry_string (NULL, "Software\\GNU\\GnuPG", "HomeDir");
55
 
  if (!dir || !*dir)
 
94
  if (!dir)
56
95
    {
57
96
      char path[MAX_PATH];
58
97
      
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, 
67
 
                          NULL, 0, path) >= 0) 
 
105
      if (w32_shgetfolderpath (NULL, CSIDL_APPDATA|CSIDL_FLAG_CREATE, 
 
106
                               NULL, 0, path) >= 0) 
68
107
        {
69
108
          char *tmp = xmalloc (strlen (path) + 6 +1);
70
109
          strcpy (stpcpy (tmp, path), "\\gnupg");
71
110
          dir = tmp;
72
111
          
73
 
          /* Try to create the directory if it does not yet
74
 
             exists.  */
 
112
          /* Try to create the directory if it does not yet exists.  */
75
113
          if (access (dir, F_OK))
76
114
            CreateDirectory (dir, NULL);
77
115
        }
 
116
      else
 
117
        dir = GNUPG_DEFAULT_HOMEDIR;
 
118
    }
 
119
  return dir;
 
120
#else/*!HAVE_W32_SYSTEM*/
 
121
  return GNUPG_DEFAULT_HOMEDIR;
 
122
#endif /*!HAVE_W32_SYSTEM*/
 
123
}
 
124
 
 
125
/* Set up the default home directory.  The usual --homedir option
 
126
   should be parsed later. */
 
127
const char *
 
128
default_homedir (void)
 
129
{
 
130
  const char *dir;
 
131
 
 
132
  dir = getenv ("GNUPGHOME");
 
133
#ifdef HAVE_W32_SYSTEM
 
134
  if (!dir || !*dir)
 
135
    {
 
136
      static const char *saved_dir;
 
137
      
 
138
      if (!saved_dir)
 
139
        {
 
140
          if (!dir || !*dir)
 
141
            {
 
142
              char *tmp;
 
143
 
 
144
              tmp = read_w32_registry_string (NULL, "Software\\GNU\\GnuPG",
 
145
                                              "HomeDir");
 
146
              if (tmp && !*tmp)
 
147
                {
 
148
                  xfree (tmp);
 
149
                  tmp = NULL;
 
150
                }
 
151
              if (tmp)
 
152
                saved_dir = tmp;
 
153
            }
 
154
          
 
155
          if (!saved_dir)
 
156
            saved_dir = standard_homedir ();
 
157
        }
 
158
      dir = saved_dir;
78
159
    }
79
160
#endif /*HAVE_W32_SYSTEM*/
80
161
  if (!dir || !*dir)
82
163
 
83
164
  return dir;
84
165
}
 
166
 
 
167
 
 
168
#ifdef HAVE_W32_SYSTEM
 
169
static const char *
 
170
w32_rootdir (void)
 
171
{
 
172
  static int got_dir;
 
173
  static char dir[MAX_PATH+5];
 
174
 
 
175
  if (!got_dir)
 
176
    {
 
177
      char *p;
 
178
 
 
179
      if ( !GetModuleFileName ( NULL, dir, MAX_PATH) )
 
180
        {
 
181
          log_debug ("GetModuleFileName failed: %s\n", w32_strerror (0));
 
182
          *dir = 0;
 
183
        }
 
184
      got_dir = 1;
 
185
      p = strrchr (dir, DIRSEP_C);
 
186
      if (p)
 
187
        *p = 0;
 
188
      else
 
189
        {
 
190
          log_debug ("bad filename `%s' returned for this process\n", dir);
 
191
          *dir = 0; 
 
192
        }
 
193
    }
 
194
 
 
195
  if (*dir)
 
196
    return dir;
 
197
  /* Fallback to the hardwired value. */
 
198
  return GNUPG_LIBEXECDIR;
 
199
}
 
200
#endif /*HAVE_W32_SYSTEM*/
 
201
 
 
202
 
 
203
 
 
204
 
 
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
 
207
   it in.  */
 
208
const char *
 
209
gnupg_sysconfdir (void)
 
210
{
 
211
#ifdef HAVE_W32_SYSTEM
 
212
  static char *name;
 
213
 
 
214
  if (!name)
 
215
    {
 
216
      const char *s1, *s2;
 
217
      s1 = w32_rootdir ();
 
218
      s2 = DIRSEP_S "etc" DIRSEP_S "gnupg";
 
219
      name = xmalloc (strlen (s1) + strlen (s2) + 1);
 
220
      strcpy (stpcpy (name, s1), s2);
 
221
    }
 
222
  return name;
 
223
#else /*!HAVE_W32_SYSTEM*/
 
224
  return GNUPG_SYSCONFDIR;
 
225
#endif /*!HAVE_W32_SYSTEM*/
 
226
}
 
227
 
 
228
 
 
229
const char *
 
230
gnupg_bindir (void)
 
231
{
 
232
#ifdef HAVE_W32_SYSTEM
 
233
  return w32_rootdir ();
 
234
#else /*!HAVE_W32_SYSTEM*/
 
235
  return GNUPG_BINDIR;
 
236
#endif /*!HAVE_W32_SYSTEM*/
 
237
}
 
238
 
 
239
 
 
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. */
 
242
const char *
 
243
gnupg_libexecdir (void)
 
244
{
 
245
#ifdef HAVE_W32_SYSTEM
 
246
  return w32_rootdir ();
 
247
#else /*!HAVE_W32_SYSTEM*/
 
248
  return GNUPG_LIBEXECDIR;
 
249
#endif /*!HAVE_W32_SYSTEM*/
 
250
}
 
251
 
 
252
const char *
 
253
gnupg_libdir (void)
 
254
{
 
255
#ifdef HAVE_W32_SYSTEM
 
256
  static char *name;
 
257
 
 
258
  if (!name)
 
259
    {
 
260
      const char *s1, *s2;
 
261
      s1 = w32_rootdir ();
 
262
      s2 = DIRSEP_S "lib" DIRSEP_S "gnupg";
 
263
      name = xmalloc (strlen (s1) + strlen (s2) + 1);
 
264
      strcpy (stpcpy (name, s1), s2);
 
265
    }
 
266
  return name;
 
267
#else /*!HAVE_W32_SYSTEM*/
 
268
  return GNUPG_LIBDIR;
 
269
#endif /*!HAVE_W32_SYSTEM*/
 
270
}
 
271
 
 
272
const char *
 
273
gnupg_datadir (void)
 
274
{
 
275
#ifdef HAVE_W32_SYSTEM
 
276
  static char *name;
 
277
 
 
278
  if (!name)
 
279
    {
 
280
      const char *s1, *s2;
 
281
      s1 = w32_rootdir ();
 
282
      s2 = DIRSEP_S "share" DIRSEP_S "gnupg";
 
283
      name = xmalloc (strlen (s1) + strlen (s2) + 1);
 
284
      strcpy (stpcpy (name, s1), s2);
 
285
    }
 
286
  return name;
 
287
#else /*!HAVE_W32_SYSTEM*/
 
288
  return GNUPG_DATADIR;
 
289
#endif /*!HAVE_W32_SYSTEM*/
 
290
}
 
291
 
 
292
 
 
293
/* Return the default socket name used by DirMngr. */
 
294
const char *
 
295
dirmngr_socket_name (void)
 
296
{
 
297
#ifdef HAVE_W32_SYSTEM
 
298
  static char *name;
 
299
 
 
300
  if (!name)
 
301
    {
 
302
      char s1[MAX_PATH];
 
303
      const char *s2;
 
304
 
 
305
      /* We need something akin CSIDL_COMMON_PROGRAMS, but local
 
306
         (non-roaming).  */
 
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);
 
312
    }
 
313
  return name;
 
314
#else /*!HAVE_W32_SYSTEM*/
 
315
  return "/var/run/dirmngr/socket";
 
316
#endif /*!HAVE_W32_SYSTEM*/
 
317
}
 
318
 
 
319
 
 
320
 
 
321
/* Return the file name of a helper tool.  WHICH is one of the
 
322
   GNUPG_MODULE_NAME_foo constants.  */
 
323
const char *
 
324
gnupg_module_name (int which)
 
325
{
 
326
  const char *s, *s2;
 
327
 
 
328
#define X(a,b) do {                                          \
 
329
        static char *name;                                   \
 
330
        if (!name)                                           \
 
331
          {                                                  \
 
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);                   \
 
336
          }                                                  \
 
337
        return name;                                         \
 
338
      } while (0)                                                     
 
339
 
 
340
  switch (which)
 
341
    {
 
342
    case GNUPG_MODULE_NAME_AGENT:
 
343
#ifdef GNUPG_DEFAULT_AGENT
 
344
      return GNUPG_DEFAULT_AGENT;
 
345
#else 
 
346
      X(bindir, "gpg-agent");
 
347
#endif
 
348
      
 
349
    case GNUPG_MODULE_NAME_PINENTRY:
 
350
#ifdef GNUPG_DEFAULT_PINENTRY
 
351
      return GNUPG_DEFAULT_PINENTRY;
 
352
#else 
 
353
      X(bindir, "pinentry");
 
354
#endif
 
355
 
 
356
    case GNUPG_MODULE_NAME_SCDAEMON:
 
357
#ifdef GNUPG_DEFAULT_SCDAEMON
 
358
      return GNUPG_DEFAULT_SCDAEMON;
 
359
#else 
 
360
      X(bindir, "scdaemon");
 
361
#endif
 
362
 
 
363
    case GNUPG_MODULE_NAME_DIRMNGR:
 
364
#ifdef GNUPG_DEFAULT_DIRMNGR
 
365
      return GNUPG_DEFAULT_DIRMNGR;
 
366
#else 
 
367
      X(bindir, "dirmngr");
 
368
#endif
 
369
 
 
370
    case GNUPG_MODULE_NAME_PROTECT_TOOL:
 
371
#ifdef GNUPG_DEFAULT_PROTECT_TOOL
 
372
      return GNUPG_DEFAULT_PROTECT_TOOL;
 
373
#else 
 
374
      X(libexecdir, "gpg-protect-tool");
 
375
#endif
 
376
 
 
377
    case GNUPG_MODULE_NAME_CHECK_PATTERN:
 
378
      X(libexecdir, "gpg-check-pattern");
 
379
 
 
380
    case GNUPG_MODULE_NAME_GPGSM:
 
381
      X(bindir, "gpgsm");
 
382
 
 
383
    case GNUPG_MODULE_NAME_GPG:
 
384
      X(bindir, "gpg2");
 
385
 
 
386
    default: 
 
387
      BUG ();
 
388
    }
 
389
#undef X
 
390
}