~ubuntu-branches/ubuntu/vivid/gnupg2/vivid-proposed

« back to all changes in this revision

Viewing changes to common/homedir.c

  • Committer: Package Import Robot
  • Author(s): Dmitrijs Ledkovs
  • Date: 2013-11-01 22:15:05 UTC
  • mfrom: (14.1.8 sid)
  • Revision ID: package-import@ubuntu.com-20131101221505-i1zpbl1qd1a3gsvx
Tags: 2.0.22-1ubuntu1
* Merge from Debian, remaining changes:
  - Drop sh prefix from openpgp test environment as it leads to exec
  invocations of sh /bin/bash leading to syntax errors from sh.  Fixes
  FTBFS detected in Ubuntu saucy archive rebuild.
  - Add udev rules to give gpg access to some smartcard readers;
    Debian #543217.
  - debian/gnupg2.udev: udev rules to set ACLs on SCM smartcard readers.
  - Add upstart user job for gpg-agent.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* homedir.c - Setup the home directory.
2
 
 *      Copyright (C) 2004, 2006, 2007 Free Software Foundation, Inc.
 
2
 * Copyright (C) 2004, 2006, 2007 Free Software Foundation, Inc.
 
3
 * Copyright (C) 2013 Werner Koch
3
4
 *
4
5
 * This file is part of GnuPG.
5
6
 *
47
48
#include "sysutils.h"
48
49
 
49
50
 
 
51
#ifdef HAVE_W32_SYSTEM
 
52
/* A flag used to indicate that a control file for gpgconf has been
 
53
   detected.  Under Windows the presence of this file indicates a
 
54
   portable installations and triggers several changes:
 
55
 
 
56
   - The GNUGHOME directory is fixed relative to installation
 
57
     directory.  All other means to set the home directory are
 
58
     ignored.
 
59
 
 
60
   - All registry variables are ignored.
 
61
 
 
62
   This flag is not used on Unix systems.
 
63
 */
 
64
static int w32_portable_app;
 
65
 
 
66
/* This flag is true if this process' binary has been installed under
 
67
   bin and not in the root directory. */
 
68
static int w32_bin_is_bin;
 
69
 
 
70
/* Just a little prototype.  */
 
71
static const char *w32_rootdir (void);
 
72
 
 
73
#endif /*HAVE_W32_SYSTEM*/
 
74
 
 
75
 
 
76
 
 
77
 
50
78
/* This is a helper function to load a Windows function from either of
51
79
   one DLLs. */
52
80
#ifdef HAVE_W32_SYSTEM
99
127
 
100
128
  if (!dir)
101
129
    {
102
 
      char path[MAX_PATH];
 
130
      const char *rdir;
103
131
 
104
 
      /* It might be better to use LOCAL_APPDATA because this is
105
 
         defined as "non roaming" and thus more likely to be kept
106
 
         locally.  For private keys this is desired.  However, given
107
 
         that many users copy private keys anyway forth and back,
108
 
         using a system roaming services might be better than to let
109
 
         them do it manually.  A security conscious user will anyway
110
 
         use the registry entry to have better control.  */
111
 
      if (w32_shgetfolderpath (NULL, CSIDL_APPDATA|CSIDL_FLAG_CREATE,
112
 
                               NULL, 0, path) >= 0)
 
132
      rdir = w32_rootdir ();
 
133
      if (w32_portable_app)
113
134
        {
114
 
          char *tmp = xmalloc (strlen (path) + 6 +1);
115
 
          strcpy (stpcpy (tmp, path), "\\gnupg");
116
 
          dir = tmp;
117
 
 
118
 
          /* Try to create the directory if it does not yet exists.  */
119
 
          if (access (dir, F_OK))
120
 
            CreateDirectory (dir, NULL);
 
135
          dir = xstrconcat (rdir, DIRSEP_S "home", NULL);
121
136
        }
122
137
      else
123
 
        dir = GNUPG_DEFAULT_HOMEDIR;
 
138
        {
 
139
          char path[MAX_PATH];
 
140
 
 
141
          /* It might be better to use LOCAL_APPDATA because this is
 
142
             defined as "non roaming" and thus more likely to be kept
 
143
             locally.  For private keys this is desired.  However,
 
144
             given that many users copy private keys anyway forth and
 
145
             back, using a system roaming services might be better
 
146
             than to let them do it manually.  A security conscious
 
147
             user will anyway use the registry entry to have better
 
148
             control.  */
 
149
          if (w32_shgetfolderpath (NULL, CSIDL_APPDATA|CSIDL_FLAG_CREATE,
 
150
                                   NULL, 0, path) >= 0)
 
151
            {
 
152
              char *tmp = xmalloc (strlen (path) + 6 +1);
 
153
              strcpy (stpcpy (tmp, path), "\\gnupg");
 
154
              dir = tmp;
 
155
 
 
156
              /* Try to create the directory if it does not yet exists.  */
 
157
              if (access (dir, F_OK))
 
158
                CreateDirectory (dir, NULL);
 
159
            }
 
160
          else
 
161
            dir = GNUPG_DEFAULT_HOMEDIR;
 
162
        }
124
163
    }
125
164
  return dir;
126
165
#else/*!HAVE_W32_SYSTEM*/
135
174
{
136
175
  const char *dir;
137
176
 
 
177
#ifdef HAVE_W32_SYSTEM
 
178
  /* For a portable application we only use the standard homedir.  */
 
179
  w32_rootdir ();
 
180
  if (w32_portable_app)
 
181
    return standard_homedir ();
 
182
#endif /*HAVE_W32_SYSTEM*/
 
183
 
138
184
  dir = getenv ("GNUPGHOME");
139
185
#ifdef HAVE_W32_SYSTEM
140
186
  if (!dir || !*dir)
172
218
 
173
219
 
174
220
#ifdef HAVE_W32_SYSTEM
 
221
/* Check whether gpgconf is installed and if so read the gpgconf.ctl
 
222
   file. */
 
223
static void
 
224
check_portable_app (const char *dir)
 
225
{
 
226
  char *fname;
 
227
 
 
228
  fname = xstrconcat (dir, DIRSEP_S "gpgconf.exe", NULL);
 
229
  if (access (fname, F_OK))
 
230
    log_error ("required binary '%s' is not installed\n", fname);
 
231
  else
 
232
    {
 
233
      strcpy (fname + strlen (fname) - 3, "ctl");
 
234
      if (!access (fname, F_OK))
 
235
        {
 
236
          /* gpgconf.ctl file found.  Record this fact.  */
 
237
          w32_portable_app = 1;
 
238
 
 
239
          /* FIXME: We should read the file to detect special flags
 
240
             and print a warning if we don't understand them.  */
 
241
        }
 
242
    }
 
243
  xfree (fname);
 
244
}
 
245
 
175
246
static const char *
176
247
w32_rootdir (void)
177
248
{
190
261
      got_dir = 1;
191
262
      p = strrchr (dir, DIRSEP_C);
192
263
      if (p)
193
 
        *p = 0;
194
 
      else
 
264
        {
 
265
          *p = 0;
 
266
 
 
267
          check_portable_app (dir);
 
268
 
 
269
          /* If we are installed below "bin" we strip that and use
 
270
             the top directory instead.  */
 
271
          p = strrchr (dir, DIRSEP_C);
 
272
 
 
273
          if (p && !strcmp (p+1, "bin"))
 
274
            {
 
275
              *p = 0;
 
276
              w32_bin_is_bin = 1;
 
277
            }
 
278
        }
 
279
      if (!p)
195
280
        {
196
281
          log_debug ("bad filename `%s' returned for this process\n", dir);
197
282
          *dir = 0;
211
296
 
212
297
  if (!dir)
213
298
    {
 
299
      const char *rdir;
214
300
      char path[MAX_PATH];
215
301
 
 
302
      /* Make sure that w32_rootdir has been called so that we are
 
303
         able to check the portable application flag.  The common dir
 
304
         is identical to the rootdir.  In that case there is also no
 
305
         need to strdup its value.  */
 
306
      rdir = w32_rootdir ();
 
307
      if (w32_portable_app)
 
308
        return rdir;
 
309
 
216
310
      if (w32_shgetfolderpath (NULL, CSIDL_COMMON_APPDATA,
217
311
                               NULL, 0, path) >= 0)
218
312
        {
226
320
        {
227
321
          /* Ooops: Not defined - probably an old Windows version.
228
322
             Use the installation directory instead.  */
229
 
          dir = xstrdup (w32_rootdir ());
 
323
          dir = xstrdup (rdir);
230
324
        }
231
325
    }
232
326
 
235
329
#endif /*HAVE_W32_SYSTEM*/
236
330
 
237
331
 
238
 
 
239
 
 
240
332
/* Return the name of the sysconfdir.  This is a static string.  This
241
333
   function is required because under Windows we can't simply compile
242
334
   it in.  */
265
357
gnupg_bindir (void)
266
358
{
267
359
#ifdef HAVE_W32_SYSTEM
268
 
  return w32_rootdir ();
 
360
  const char *rdir;
 
361
 
 
362
  rdir = w32_rootdir ();
 
363
  if (w32_bin_is_bin)
 
364
    {
 
365
      static char *name;
 
366
 
 
367
      if (!name)
 
368
        name = xstrconcat (rdir, DIRSEP_S "bin", NULL);
 
369
      return name;
 
370
    }
 
371
  else
 
372
    return rdir;
269
373
#else /*!HAVE_W32_SYSTEM*/
270
374
  return GNUPG_BINDIR;
271
375
#endif /*!HAVE_W32_SYSTEM*/
278
382
gnupg_libexecdir (void)
279
383
{
280
384
#ifdef HAVE_W32_SYSTEM
281
 
  return w32_rootdir ();
 
385
  return gnupg_bindir ();
282
386
#else /*!HAVE_W32_SYSTEM*/
283
387
  return GNUPG_LIBEXECDIR;
284
388
#endif /*!HAVE_W32_SYSTEM*/