Coverage Report

Created: 2026-10-17 07:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gnupg/common/homedir.c
Line
Count
Source
1
/* homedir.c - Setup the home directory.
2
 * Copyright (C) 2004, 2006, 2007, 2010 Free Software Foundation, Inc.
3
 * Copyright (C) 2013, 2016 Werner Koch
4
 * Copyright (C) 2021, 2024 g10 Code GmbH
5
 *
6
 * This file is part of GnuPG.
7
 *
8
 * This file is free software; you can redistribute it and/or modify
9
 * it under the terms of either
10
 *
11
 *   - the GNU Lesser General Public License as published by the Free
12
 *     Software Foundation; either version 3 of the License, or (at
13
 *     your option) any later version.
14
 *
15
 * or
16
 *
17
 *   - the GNU General Public License as published by the Free
18
 *     Software Foundation; either version 2 of the License, or (at
19
 *     your option) any later version.
20
 *
21
 * or both in parallel, as here.
22
 *
23
 * This file is distributed in the hope that it will be useful,
24
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26
 * GNU General Public License for more details.
27
 *
28
 * You should have received a copy of the GNU General Public License
29
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
30
 * SPDX-License-Identifier: (LGPL-3.0-or-later OR GPL-2.0-or-later)
31
 */
32
33
#include <config.h>
34
#include <stdlib.h>
35
#include <errno.h>
36
#include <fcntl.h>
37
#include <unistd.h>
38
39
#ifdef HAVE_W32_SYSTEM
40
#include <winsock2.h>   /* Due to the stupid mingw64 requirement to
41
                           include this header before windows.h which
42
                           is often implicitly included.  */
43
#include <shlobj.h>
44
#ifndef CSIDL_APPDATA
45
#define CSIDL_APPDATA 0x001a
46
#endif
47
#ifndef CSIDL_LOCAL_APPDATA
48
#define CSIDL_LOCAL_APPDATA 0x001c
49
#endif
50
#ifndef CSIDL_COMMON_APPDATA
51
#define CSIDL_COMMON_APPDATA 0x0023
52
#endif
53
#ifndef CSIDL_FLAG_CREATE
54
#define CSIDL_FLAG_CREATE 0x8000
55
#endif
56
#endif /*HAVE_W32_SYSTEM*/
57
58
#ifdef HAVE_STAT
59
#include <sys/stat.h> /* for stat() */
60
#endif
61
62
#ifdef __APPLE__
63
#include <libproc.h>
64
#endif /*__APPLE__*/
65
66
#include "util.h"
67
#include "sysutils.h"
68
#include "i18n.h"
69
#include "zb32.h"
70
71
/* The name of the symbolic link to the file from which the process
72
 * text was read.  */
73
#if __linux__
74
1
# define MYPROC_SELF_EXE "/proc/self/exe"
75
#elif defined(__NetBSD__)
76
# define MYPROC_SELF_EXE "/proc/curproc/exe"
77
#elif defined(__illumos__) || defined(__sun)
78
# define MYPROC_SELF_EXE "/proc/self/path/a.out"
79
#else /* Assume other BSDs */
80
# define MYPROC_SELF_EXE "/proc/curproc/file"
81
#endif
82
83
84
/* Mode flags for unix_rootdir.  */
85
enum wantdir_values {
86
  WANTDIR_ROOT = 0,
87
  WANTDIR_SYSCONF,
88
  WANTDIR_SOCKET
89
};
90
91
92
/* The GnuPG homedir.  This is only accessed by the functions
93
 * gnupg_homedir and gnupg_set_homedir.  Malloced.  */
94
static char *the_gnupg_homedir;
95
96
/* Flag indicating that home directory is not the default one.  */
97
static byte non_default_homedir;
98
99
100
/* An object to store information taken from a gpgconf.ctl file.  This
101
 * is parsed early at startup time and never changed later.  */
102
static struct
103
{
104
  unsigned int checked:1; /* True if we have checked for a gpgconf.ctl.  */
105
  unsigned int found:1;   /* True if a gpgconf.ctl was found.            */
106
  unsigned int empty:1;   /* The file is empty except for comments.      */
107
  unsigned int valid:1;   /* The entries in gpgconf.ctl are valid.       */
108
  unsigned int portable:1;/* Windows portable installation.              */
109
  char *gnupg;            /* The "gnupg" directory part.  */
110
  char *rootdir;          /* rootdir or NULL        */
111
  char *sysconfdir;       /* sysconfdir or NULL     */
112
  char *socketdir;        /* socketdir or NULL      */
113
} gpgconf_ctl;
114
115
116
#ifdef HAVE_W32_SYSTEM
117
/* A flag used to indicate that a control file for gpgconf has been
118
 * detected.  Under Windows the presence of this file indicates a
119
 * portable installations and triggers several changes:
120
 *
121
 * - The GNUGHOME directory is fixed relative to installation
122
 *   directory.  All other means to set the home directory are ignored.
123
 *
124
 * - All registry variables will be ignored.
125
 *
126
 * This flag is not used on Unix systems.
127
 */
128
static byte w32_portable_app;
129
#endif /*HAVE_W32_SYSTEM*/
130
131
#ifdef HAVE_W32_SYSTEM
132
/* This flag is true if this process's binary has been installed under
133
   bin and not in the root directory as often used before GnuPG 2.1. */
134
static byte w32_bin_is_bin;
135
#endif /*HAVE_W32_SYSTEM*/
136
137
138
#ifdef HAVE_W32_SYSTEM
139
static const char *w32_rootdir (void);
140
#endif
141
142
/* Return the name of the gnupg dir.  This is usually "gnupg".  */
143
static const char *
144
my_gnupg_dirname (void)
145
1
{
146
1
  if (gpgconf_ctl.valid && gpgconf_ctl.gnupg)
147
0
    return gpgconf_ctl.gnupg;
148
1
  return "gnupg";
149
1
}
150
151
/* Return the hardwired home directory which is not anymore so
152
 * hardwired because it may now be modified using the gpgconf.ctl
153
 * "gnupg" keyword.  */
154
static const char *
155
my_fixed_default_homedir (void)
156
1
{
157
1
  if (gpgconf_ctl.valid && gpgconf_ctl.gnupg)
158
0
    {
159
0
      static char *name;
160
0
      char *p;
161
162
0
      if (!name)
163
0
        {
164
0
          name = xmalloc (strlen (GNUPG_DEFAULT_HOMEDIR)
165
0
                          + strlen (gpgconf_ctl.gnupg) + 1);
166
0
          strcpy (name, GNUPG_DEFAULT_HOMEDIR);
167
0
          p = strrchr (name, '/');
168
0
          if (p)
169
0
            p++;
170
0
          else
171
0
            p = name;
172
0
          if (*p == '.')
173
0
            p++; /* Keep a leading dot.  */
174
0
          strcpy (p, gpgconf_ctl.gnupg);
175
0
          gpgrt_annotate_leaked_object (name);
176
0
        }
177
0
      return name;
178
0
    }
179
1
  return GNUPG_DEFAULT_HOMEDIR;
180
1
}
181
182
183
184
/* Under Windows we need to modify the standard registry key with the
185
 * "gnupg" keyword from a gpgconf.ctl.  */
186
#ifdef HAVE_W32_SYSTEM
187
const char *
188
gnupg_registry_dir (void)
189
{
190
  if (gpgconf_ctl.valid && gpgconf_ctl.gnupg)
191
    {
192
      static char *name;
193
      char *p;
194
195
      if (!name)
196
        {
197
          name = xmalloc (strlen (GNUPG_REGISTRY_DIR)
198
                          + strlen (gpgconf_ctl.gnupg) + 1);
199
          strcpy (name, GNUPG_REGISTRY_DIR);
200
          p = strrchr (name, '\\');
201
          if (p)
202
            p++;
203
          else
204
            p = name;
205
          strcpy (p, gpgconf_ctl.gnupg);
206
          if (!strncmp (p, "gnupg", 5))
207
            {
208
              /* Registry keys are case-insensitive and we use a
209
               * capitalized version of gnupg by default.  So, if the
210
               * new value starts with "gnupg" we apply the usual
211
               * capitalization for this first part.  */
212
              p[0] = 'G';
213
              p[3] = 'P';
214
              p[4] = 'G';
215
            }
216
          gpgrt_annotate_leaked_object (name);
217
        }
218
      return name;
219
    }
220
  return GNUPG_REGISTRY_DIR;
221
}
222
#endif /*HAVE_W32_SYSTEM*/
223
224
225
/* This is a helper function to load and call a Windows function from
226
 * either of one DLLs.  On success an UTF-8 file name is returned.
227
 * ERRNO is _not_ set on error.  */
228
#ifdef HAVE_W32_SYSTEM
229
static char *
230
w32_shgetfolderpath (HWND a, int b, HANDLE c, DWORD d)
231
{
232
  static int initialized;
233
  static HRESULT (WINAPI * func)(HWND,int,HANDLE,DWORD,LPWSTR);
234
  wchar_t wfname[MAX_PATH];
235
236
  if (!initialized)
237
    {
238
      static char *dllnames[] = { "shell32.dll", "shfolder.dll", NULL };
239
      void *handle;
240
      int i;
241
242
      initialized = 1;
243
244
      for (i=0, handle = NULL; !handle && dllnames[i]; i++)
245
        {
246
          handle = dlopen (dllnames[i], RTLD_LAZY);
247
          if (handle)
248
            {
249
              func = dlsym (handle, "SHGetFolderPathW");
250
              if (!func)
251
                {
252
                  dlclose (handle);
253
                  handle = NULL;
254
                }
255
            }
256
        }
257
    }
258
259
  if (func && func (a,b,c,d,wfname) >= 0)
260
    return wchar_to_utf8 (wfname);
261
  else
262
    return NULL;
263
}
264
#endif /*HAVE_W32_SYSTEM*/
265
266
/* Given the directory name DNAME try to create a common.conf and
267
 * enable the keyboxd.  This should only be called for the standard
268
 * home directory and only if that directory has just been created.  */
269
static void
270
create_common_conf (const char *dname)
271
0
{
272
#ifdef BUILD_WITH_KEYBOXD
273
  estream_t fp;
274
  char *fcommon;
275
276
  fcommon = make_filename (dname, "common.conf", NULL);
277
  fp = es_fopen (fcommon, "wx,mode=-rw-r");
278
  if (!fp)
279
    {
280
      log_info (_("error creating '%s': %s\n"), fcommon,
281
                gpg_strerror (gpg_error_from_syserror ()));
282
    }
283
  else
284
    {
285
      if (es_fputs ("use-keyboxd\n", fp) == EOF)
286
        {
287
          log_info (_("error writing to '%s': %s\n"), fcommon,
288
                    gpg_strerror (es_ferror (fp)
289
                                  ? gpg_error_from_syserror ()
290
                                  : gpg_error (GPG_ERR_EOF)));
291
          es_fclose (fp);
292
        }
293
      else if (es_fclose (fp))
294
        {
295
          log_info (_("error closing '%s': %s\n"), fcommon,
296
                    gpg_strerror (gpg_error_from_syserror ()));
297
        }
298
    }
299
#else /* BUILD_WITH_KEYBOXD */
300
0
  (void)dname;
301
0
#endif /* !BUILD_WITH_KEYBOXD */
302
0
}
303
304
305
/* Check whether DIR is the default homedir.  */
306
static int
307
is_gnupg_default_homedir (const char *dir)
308
1
{
309
1
  int result;
310
1
  char *a = make_absfilename (dir, NULL);
311
1
  char *b = make_absfilename (standard_homedir (), NULL);
312
1
  result = !compare_filenames (a, b);
313
1
  xfree (b);
314
1
  xfree (a);
315
1
  return result;
316
1
}
317
318
319
/* Helper to remove trailing slashes from NEWDIR.  Return a new
320
 * allocated string if that has been done or NULL if there are no
321
 * slashes to remove.  Also inserts a missing slash after a Windows
322
 * drive letter.  */
323
static char *
324
copy_dir_with_fixup (const char *newdir)
325
1
{
326
1
  char *result = NULL;
327
1
  char *p;
328
#ifdef HAVE_W32_SYSTEM
329
  char *p0;
330
  const char *s;
331
#endif
332
333
1
  if (!*newdir)
334
0
    return NULL;
335
336
#ifdef HAVE_W32_SYSTEM
337
  if (newdir[0] && newdir[1] == ':'
338
      && !(newdir[2] == '/' || newdir[2] == '\\'))
339
    {
340
      /* Drive letter with missing leading slash.  */
341
      p = result = xmalloc (strlen (newdir) + 1 + 1);
342
      *p++ = newdir[0];
343
      *p++ = newdir[1];
344
      *p++ = '\\';
345
      strcpy (p, newdir+2);
346
347
      /* Remove trailing slashes.  */
348
      p = result + strlen (result) - 1;
349
      while (p > result+2 && (*p == '/' || *p == '\\'))
350
        *p-- = 0;
351
    }
352
  else if (newdir[strlen (newdir)-1] == '/'
353
           || newdir[strlen (newdir)-1] == '\\' )
354
    {
355
      result = xstrdup (newdir);
356
      p = result + strlen (result) - 1;
357
      while (p > result
358
             && (*p == '/' || *p == '\\')
359
             && (p-1 > result && p[-1] != ':')) /* We keep "c:/". */
360
        *p-- = 0;
361
    }
362
363
  /* Hack to mitigate badly doubled backslashes.  */
364
  s = result? result : newdir;
365
  if (s[0] == '\\' && s[1] == '\\' && s[2] != '\\')
366
    {
367
      /* UNC (\\Servername\file) or Long UNC (\\?\Servername\file)
368
       * Does not seem to be double quoted.  */
369
    }
370
  else if (strstr (s, "\\\\"))
371
    {
372
      /* Double quotes detected.  Fold them into one because that is
373
       * what what Windows does.  This way we get a unique hash
374
       * regardless of the number of doubled backslashes. */
375
      if (!result)
376
        result = xstrdup (newdir);
377
      for (p0=p=result; *p; p++)
378
        {
379
          *p0++ = *p;
380
          while (*p == '\\' && p[1] == '\\')
381
            p++;
382
        }
383
      *p0 = 0;
384
    }
385
386
#else /*!HAVE_W32_SYSTEM*/
387
388
1
  if (newdir[strlen (newdir)-1] == '/')
389
1
    {
390
1
      result = xstrdup (newdir);
391
1
      p = result + strlen (result) - 1;
392
2
      while (p > result && *p == '/')
393
1
        *p-- = 0;
394
1
    }
395
396
1
#endif /*!HAVE_W32_SYSTEM*/
397
398
1
  return result;
399
1
}
400
401
402
/* Get the standard home directory.  In general this function should
403
   not be used as it does not consider a registry value (under W32) or
404
   the GNUPGHOME environment variable.  It is better to use
405
   gnupg_homedir(). */
406
const char *
407
standard_homedir (void)
408
1
{
409
#ifdef HAVE_W32_SYSTEM
410
  static const char *dir;
411
412
  if (!dir)
413
    {
414
      const char *rdir;
415
416
      rdir = w32_rootdir ();
417
      if (w32_portable_app)
418
        {
419
          dir = xstrconcat (rdir, DIRSEP_S "home", NULL);
420
          gpgrt_annotate_leaked_object (dir);
421
        }
422
      else
423
        {
424
          char *path;
425
426
          path = w32_shgetfolderpath (NULL, CSIDL_APPDATA|CSIDL_FLAG_CREATE,
427
                                      NULL, 0);
428
          if (path)
429
            {
430
              dir = xstrconcat (path, "\\", my_gnupg_dirname (), NULL);
431
              xfree (path);
432
              gpgrt_annotate_leaked_object (dir);
433
434
              /* Try to create the directory if it does not yet exists.  */
435
              if (gnupg_access (dir, F_OK))
436
                if (!gnupg_mkdir (dir, "-rwx"))
437
                  create_common_conf (dir);
438
439
            }
440
          else
441
            dir = my_fixed_default_homedir ();
442
        }
443
    }
444
  return dir;
445
#else/*!HAVE_W32_SYSTEM*/
446
1
  return my_fixed_default_homedir ();
447
1
#endif /*!HAVE_W32_SYSTEM*/
448
1
}
449
450
/* Set up the default home directory.  The usual --homedir option
451
   should be parsed later. */
452
static const char *
453
default_homedir (void)
454
0
{
455
0
  const char *dir;
456
457
#ifdef HAVE_W32_SYSTEM
458
  /* For a portable application we only use the standard homedir.  */
459
  w32_rootdir ();
460
  if (w32_portable_app)
461
    return standard_homedir ();
462
#endif /*HAVE_W32_SYSTEM*/
463
464
0
  dir = getenv ("GNUPGHOME");
465
#ifdef HAVE_W32_SYSTEM
466
  if (!dir || !*dir)
467
    {
468
      static const char *saved_dir;
469
470
      if (!saved_dir)
471
        {
472
          if (!dir || !*dir)
473
            {
474
              char *tmp, *p;
475
476
              /* This is deprecated; gpgconf --list-dirs prints a
477
               * warning if the homedir has been taken from the
478
               * registry.  */
479
              tmp = read_w32_registry_string (NULL,
480
                                              gnupg_registry_dir (),
481
                                              "HomeDir");
482
              if (tmp && !*tmp)
483
                {
484
                  xfree (tmp);
485
                  tmp = NULL;
486
                }
487
              if (tmp)
488
                {
489
                  /* Strip trailing backslashes.  */
490
                  p = tmp + strlen (tmp) - 1;
491
                  while (p > tmp && *p == '\\')
492
                    *p-- = 0;
493
                  saved_dir = tmp;
494
                }
495
            }
496
497
          if (!saved_dir)
498
            saved_dir = standard_homedir ();
499
        }
500
      dir = saved_dir;
501
    }
502
#endif /*HAVE_W32_SYSTEM*/
503
504
0
  if (!dir || !*dir)
505
0
    dir = my_fixed_default_homedir ();
506
0
  else
507
0
    {
508
0
      char *p;
509
510
0
      p = copy_dir_with_fixup (dir);
511
0
      if (p)
512
0
        {
513
          /* A new buffer has been allocated with proper semantics.
514
           * Assign this to DIR.  If DIR is passed again to
515
           * copy_dir_with_fixup there will be no need for a fix up
516
           * and the function returns NULL.  Thus we leak only once.
517
           * Setting the homedir is usually a one-off task but might
518
           * be called a second time.  We also ignore such extra leaks
519
           * because we don't know who still references the former
520
           * string.  */
521
0
          gpgrt_annotate_leaked_object (p);
522
0
          dir = p;
523
0
        }
524
525
0
      if (!is_gnupg_default_homedir (dir))
526
0
        non_default_homedir = 1;
527
0
    }
528
529
0
  return dir;
530
0
}
531
532
533
/* Return true if S can be inteprtated as true.  This is uised for
534
 * keywords in gpgconf.ctl.  Spaces must have been trimmed.  */
535
static int
536
string_is_true (const char *s)
537
0
{
538
0
  return (atoi (s)
539
0
          || !ascii_strcasecmp (s, "yes")
540
0
          || !ascii_strcasecmp (s, "true")
541
0
          || !ascii_strcasecmp (s, "fact"));
542
0
}
543
544
/* This function is used to parse the gpgconf.ctl file and set the
545
 * information ito the gpgconf_ctl structure.  This is called once
546
 * with the full filename of gpgconf.ctl.  There are two callers: One
547
 * used on Windows and one on Unix.  No error return but diagnostics
548
 * are printed.  */
549
static void
550
parse_gpgconf_ctl (const char *fname)
551
1
{
552
1
  gpg_error_t err;
553
1
  char *p;
554
1
  char *line;
555
1
  size_t linelen;
556
1
  ssize_t length;
557
1
  estream_t fp;
558
1
  const char *name;
559
1
  int anyitem = 0;
560
1
  int ignoreall = 0;
561
1
  char *gnupgval = NULL;
562
1
  char *rootdir = NULL;
563
1
  char *sysconfdir = NULL;
564
1
  char *socketdir = NULL;
565
566
1
  if (gpgconf_ctl.checked)
567
0
    return;  /* Just in case this is called a second time.  */
568
1
  gpgconf_ctl.checked = 1;
569
1
  gpgconf_ctl.found = 0;
570
1
  gpgconf_ctl.valid = 0;
571
1
  gpgconf_ctl.empty = 0;
572
573
1
  if (gnupg_access (fname, F_OK))
574
1
    return; /* No gpgconf.ctl file.  */
575
576
  /* log_info ("detected '%s'\n", buffer); */
577
0
  fp = es_fopen (fname, "r");
578
0
  if (!fp)
579
0
    {
580
0
      err = gpg_error_from_syserror ();
581
0
      log_info ("error opening '%s': %s\n", fname, gpg_strerror (err));
582
0
      return;
583
0
    }
584
0
  gpgconf_ctl.found = 1;
585
586
0
  line = NULL;
587
0
  linelen = 0;
588
0
  while ((length = es_read_line (fp, &line, &linelen, NULL)) > 0)
589
0
    {
590
0
      static const char *names[] =
591
0
        {
592
0
          "gnupg",
593
0
          "rootdir",
594
0
          "sysconfdir",
595
0
          "socketdir",
596
0
          "portable",
597
0
          ".enable"
598
0
        };
599
0
      int i;
600
0
      size_t n;
601
602
      /* Strip NL and CR, if present.  */
603
0
      while (length > 0
604
0
             && (line[length - 1] == '\n' || line[length - 1] == '\r'))
605
0
        line[--length] = 0;
606
0
      trim_spaces (line);
607
0
      if (*line == '#' || !*line)
608
0
        continue;
609
0
      anyitem = 1;
610
611
      /* Find the keyword.  */
612
0
      name = NULL;
613
0
      p = NULL;
614
0
      for (i=0; i < DIM (names); i++)
615
0
        {
616
0
          n = strlen (names[i]);
617
0
          if (!strncmp (line, names[i], n))
618
0
            {
619
0
              while (line[n] == ' ' || line[n] == '\t')
620
0
                n++;
621
0
              if (line[n] == '=')
622
0
                {
623
0
                  name = names[i];
624
0
                  p = line + n + 1;
625
0
                  break;
626
0
                }
627
0
            }
628
0
        }
629
0
      if (!name)
630
0
        continue;  /* Keyword not known.  */
631
632
0
      trim_spaces (p);
633
0
      p = substitute_envvars (p);
634
0
      if (!p)
635
0
        {
636
0
          err = gpg_error_from_syserror ();
637
0
          log_info ("error getting %s from gpgconf.ctl: %s\n",
638
0
                    name, gpg_strerror (err));
639
0
        }
640
0
      else if (!strcmp (name, ".enable"))
641
0
        {
642
0
          if (string_is_true (p))
643
0
            ;              /* Yes, this file shall be used.    */
644
0
          else
645
0
            ignoreall = 1; /* No, this file shall be ignored.  */
646
0
          xfree (p);
647
0
        }
648
0
      else if (!strcmp (name, "gnupg"))
649
0
        {
650
0
          xfree (gnupgval);
651
0
          gnupgval = p;
652
0
        }
653
0
      else if (!strcmp (name, "sysconfdir"))
654
0
        {
655
0
          xfree (sysconfdir);
656
0
          sysconfdir = p;
657
0
        }
658
0
      else if (!strcmp (name, "socketdir"))
659
0
        {
660
0
          xfree (socketdir);
661
0
          socketdir = p;
662
0
        }
663
0
      else if (!strcmp (name, "rootdir"))
664
0
        {
665
0
          xfree (rootdir);
666
0
          rootdir = p;
667
0
        }
668
0
      else if (!strcmp (name, "portable"))
669
0
        {
670
0
          gpgconf_ctl.portable = string_is_true (p);
671
0
          xfree (p);
672
0
        }
673
0
      else /* Unknown keyword.  */
674
0
        xfree (p);
675
0
    }
676
0
  if (es_ferror (fp))
677
0
    {
678
0
      err = gpg_error_from_syserror ();
679
0
      log_info ("error reading '%s': %s\n", fname, gpg_strerror (err));
680
0
      ignoreall = 1;  /* Force all entries to invalid.  */
681
0
    }
682
0
  es_fclose (fp);
683
0
  xfree (line);
684
685
0
  if (ignoreall)
686
0
    ; /* Forced error.  Note that .found is still set.  */
687
0
  else if (gnupgval && (!*gnupgval || strpbrk (gnupgval, "/\\")))
688
0
    {
689
      /* We don't allow a slash or backslash in the value because our
690
       * code assumes this is a single directory name.  */
691
0
      log_info ("invalid %s '%s' specified in gpgconf.ctl\n",
692
0
                "gnupg", gnupgval);
693
0
    }
694
0
  else if (rootdir && (!*rootdir || *rootdir != '/'))
695
0
    {
696
0
      log_info ("invalid %s '%s' specified in gpgconf.ctl\n",
697
0
                "rootdir", rootdir);
698
0
    }
699
0
  else if (sysconfdir && (!*sysconfdir || *sysconfdir != '/'))
700
0
    {
701
0
      log_info ("invalid %s '%s' specified in gpgconf.ctl\n",
702
0
                "sysconfdir", sysconfdir);
703
0
    }
704
0
  else if (socketdir && (!*socketdir || *socketdir != '/'))
705
0
    {
706
0
      log_info ("invalid %s '%s' specified in gpgconf.ctl\n",
707
0
                "socketdir", socketdir);
708
0
    }
709
0
  else
710
0
    {
711
0
      if (gnupgval)
712
0
        {
713
0
          gpgconf_ctl.gnupg = gnupgval;
714
0
          gpgrt_annotate_leaked_object (gpgconf_ctl.gnupg);
715
          /* log_info ("want gnupg '%s'\n", dir); */
716
0
        }
717
0
      if (rootdir)
718
0
        {
719
0
          while (*rootdir && rootdir[strlen (rootdir)-1] == '/')
720
0
            rootdir[strlen (rootdir)-1] = 0;
721
0
          gpgconf_ctl.rootdir = rootdir;
722
0
          gpgrt_annotate_leaked_object (gpgconf_ctl.rootdir);
723
          /* log_info ("want rootdir '%s'\n", dir); */
724
0
        }
725
0
      if (sysconfdir)
726
0
        {
727
0
          while (*sysconfdir && sysconfdir[strlen (sysconfdir)-1] == '/')
728
0
            sysconfdir[strlen (sysconfdir)-1] = 0;
729
0
          gpgconf_ctl.sysconfdir = sysconfdir;
730
0
          gpgrt_annotate_leaked_object (gpgconf_ctl.sysconfdir);
731
          /* log_info ("want sysconfdir '%s'\n", sdir); */
732
0
        }
733
0
      if (socketdir)
734
0
        {
735
0
          while (*socketdir && socketdir[strlen (socketdir)-1] == '/')
736
0
            socketdir[strlen (socketdir)-1] = 0;
737
0
          gpgconf_ctl.socketdir = socketdir;
738
0
          gpgrt_annotate_leaked_object (gpgconf_ctl.socketdir);
739
          /* log_info ("want socketdir '%s'\n", s2dir); */
740
0
        }
741
0
      gpgconf_ctl.valid = 1;
742
0
    }
743
744
0
  gpgconf_ctl.empty = !anyitem;
745
0
  if (!gpgconf_ctl.valid)
746
0
    {
747
      /* Error reading some entries - clear them all.  */
748
0
      xfree (gnupgval);
749
0
      xfree (rootdir);
750
0
      xfree (sysconfdir);
751
0
      xfree (socketdir);
752
0
      gpgconf_ctl.gnupg = NULL;
753
0
      gpgconf_ctl.rootdir = NULL;
754
0
      gpgconf_ctl.sysconfdir = NULL;
755
0
      gpgconf_ctl.socketdir = NULL;
756
0
    }
757
0
}
758
759
760
761
#ifdef HAVE_W32_SYSTEM
762
/* Check whether gpgconf is installed and if so read the gpgconf.ctl
763
   file. */
764
static void
765
check_portable_app (const char *dir)
766
{
767
  char *fname;
768
769
  fname = xstrconcat (dir, DIRSEP_S "gpgconf.exe", NULL);
770
  if (!gnupg_access (fname, F_OK))
771
    {
772
      strcpy (fname + strlen (fname) - 3, "ctl");
773
      parse_gpgconf_ctl (fname);
774
      if ((gpgconf_ctl.found && gpgconf_ctl.empty)
775
          || (gpgconf_ctl.valid && gpgconf_ctl.portable))
776
        {
777
          unsigned int flags;
778
779
          /* Classic gpgconf.ctl file found.  This is a portable
780
           * application.  Note that if there are any items in that
781
           * file we don't consider this a portable application unless
782
           * the (later added) ".portable" keyword has also been
783
           * seen.  */
784
          w32_portable_app = 1;
785
          log_get_prefix (&flags);
786
          log_set_prefix (NULL, (flags | GPGRT_LOG_NO_REGISTRY));
787
        }
788
    }
789
  xfree (fname);
790
}
791
#endif /*HAVE_W32_SYSTEM*/
792
793
794
#ifdef HAVE_W32_SYSTEM
795
/* Return the filename of this process.  Caller must release the
796
 * returned buffer.  On error an empty string is returned.  */
797
static char *
798
w32_myproc_self (void)
799
{
800
  int rc;
801
  wchar_t wdir [MAX_PATH+5];
802
  char *dir;
803
804
  dir = xmalloc (MAX_PATH+5);
805
  rc = GetModuleFileNameW (NULL, wdir, MAX_PATH);
806
  if (rc && WideCharToMultiByte (CP_UTF8, 0, wdir, -1, dir, MAX_PATH-4,
807
                                 NULL, NULL) < 0)
808
    rc = 0;
809
  if (!rc)
810
    {
811
      log_debug ("GetModuleFileName failed: %s\n", w32_strerror (-1));
812
      *dir = 0;
813
    }
814
  return dir;
815
}
816
817
818
/* Determine the root directory of the gnupg installation on Windows.  */
819
static const char *
820
w32_rootdir (void)
821
{
822
  static int got_dir;
823
  static char *dir;
824
825
  if (!got_dir)
826
    {
827
      char *p;
828
829
      dir = w32_myproc_self ();
830
      gpgrt_annotate_leaked_object (dir);
831
      got_dir = 1;
832
      p = strrchr (dir, DIRSEP_C);
833
      if (p)
834
        {
835
          *p = 0;
836
837
          check_portable_app (dir);
838
839
          /* If we are installed below "bin" we strip that and use
840
             the top directory instead.  */
841
          p = strrchr (dir, DIRSEP_C);
842
          if (p && !strcmp (p+1, "bin"))
843
            {
844
              *p = 0;
845
              w32_bin_is_bin = 1;
846
            }
847
        }
848
      if (!p)
849
        {
850
          log_debug ("bad filename '%s' returned for this process\n", dir);
851
          *dir = 0;
852
        }
853
    }
854
855
  if (*dir)
856
    return dir;
857
  /* Fallback to the hardwired value. */
858
  return GNUPG_LIBEXECDIR;
859
}
860
#endif /*HAVE_W32_SYSTEM*/
861
862
863
#ifndef HAVE_W32_SYSTEM /* Unix */
864
/* Return the filename of this process.  If WITH_FALLBACK is set the
865
 * GNUPG_BUILD_ROOT envvar is considered in the error case.  Caller
866
 * must release the returned buffer.  On error an empty string is
867
 * returned.  */
868
static char *
869
unix_myproc_self (int with_fallback)
870
1
{
871
1
  char *buffer;
872
1
  size_t bufsize = 256-1;
873
1
  int nread;
874
1
  gpg_error_t err;
875
1
  const char *name;
876
877
1
  for (;;)
878
1
    {
879
1
      buffer = xmalloc (bufsize+1);
880
1
      nread = readlink (MYPROC_SELF_EXE, buffer, bufsize);
881
1
      if (nread < 0)
882
0
        {
883
0
          err = gpg_error_from_syserror ();
884
0
          buffer[0] = 0;
885
0
          if (with_fallback
886
0
              && (name = getenv ("GNUPG_BUILD_ROOT")) && *name == '/')
887
0
            {
888
              /* Try a fallback for systems w/o a supported /proc
889
               * file system if we are running a regression test.  */
890
0
              log_info ("error reading symlink '%s': %s\n",
891
0
                        MYPROC_SELF_EXE, gpg_strerror (err));
892
0
              xfree  (buffer);
893
0
              buffer = xstrconcat (name, "/bin/gpgconf", NULL);
894
0
              log_info ("trying fallback '%s'\n", buffer);
895
0
            }
896
0
          break;
897
0
        }
898
1
      else if (nread < bufsize)
899
1
        {
900
1
          buffer[nread] = 0;
901
1
          break;  /* Got it.  */
902
1
        }
903
0
      else if (bufsize >= 4095)
904
0
        {
905
0
          buffer[0] = 0;
906
0
          log_info ("error reading symlink '%s': %s\n",
907
0
                    MYPROC_SELF_EXE, "value too large");
908
0
          break;
909
0
        }
910
0
      xfree (buffer);
911
0
      bufsize += 256;
912
0
    }
913
1
  return buffer;
914
1
}
915
916
#ifdef __APPLE__
917
/* macOS has no /proc file system but an undocumented system call but
918
 * with availabale source code.  */
919
static char *
920
macos_myproc_self (int with_fallback)
921
{
922
  char *buffer;
923
  size_t bufsize = PROC_PIDPATHINFO_SIZE;
924
  int nread;
925
  gpg_error_t err;
926
  const char *name;
927
928
  for (;;)
929
    {
930
      buffer = xmalloc (bufsize+1);
931
      nread = proc_pidpath (getpid (), buffer, bufsize);
932
      if (nread > 0)
933
        break;  /* success */
934
      err = gpg_error_from_syserror ();
935
      buffer[0] = 0;  /* An empty return string indicates an error.  */
936
      if (errno == EOVERFLOW || bufsize >= 4095)
937
        {
938
          if (with_fallback
939
              && (name = getenv ("GNUPG_BUILD_ROOT")) && *name == '/')
940
            {
941
              log_info ("error from proc_pidpath: %s"
942
                        " - trying Unix method\n", gpg_strerror (err));
943
              xfree  (buffer);
944
              return unix_myproc_self (with_fallback);
945
            }
946
          break;
947
        }
948
      xfree (buffer);
949
      bufsize += 256;
950
    }
951
  return buffer;
952
}
953
#endif /*__APPLE__*/
954
955
956
/* Determine the root directory of the gnupg installation on Unix.
957
 * The standard case is that this function returns NULL so that the
958
 * root directory as configured at build time is used.  However, it
959
 * may return a static string with a different root directory, similar
960
 * to what we do on Windows.  That second mode is triggered by the
961
 * existence of a file gpgconf.ctl installed side-by-side to gpgconf.
962
 * This file is parsed for keywords describing the actually to be used
963
 * root directory.  There is no solid standard on Unix to locate the
964
 * binary used to create the process, thus we support this currently
965
 * only on Linux and BSD where we can look this info up using the proc
966
 * file system.  If WANT_SYSCONFDIR is true the optional sysconfdir
967
 * entry is returned.  */
968
static const char *
969
unix_rootdir (enum wantdir_values wantdir)
970
1
{
971
1
  if (!gpgconf_ctl.checked)
972
1
    {
973
1
      char *p;
974
1
      char *buffer;
975
976
#ifdef __APPLE__
977
      buffer = macos_myproc_self (1/* with_fallback */);
978
#else
979
1
      buffer = unix_myproc_self (1/* with_fallback */);
980
1
#endif
981
1
      if (!*buffer)
982
0
        {
983
0
          xfree (buffer);
984
0
          gpgconf_ctl.checked = 1;
985
0
          return NULL;  /* Error - assume no gpgconf.ctl.  */
986
0
        }
987
988
1
      p = strrchr (buffer, '/');
989
1
      if (!p)
990
0
        {
991
0
          xfree (buffer);
992
0
          gpgconf_ctl.checked = 1;
993
0
          return NULL;  /* Erroneous /proc - assume no gpgconf.ctl.  */
994
0
        }
995
1
      *p = 0;  /* BUFFER has the directory.  */
996
1
      if (!(p = strrchr (buffer, '/')))
997
0
        {
998
          /* Installed in the root which is not a good idea.  Assume
999
           * no gpgconf.ctl.  */
1000
0
          xfree (buffer);
1001
0
          gpgconf_ctl.checked = 1;
1002
0
          return NULL;
1003
0
        }
1004
1005
      /* Strip one part and expect the file below a bin dir.  */
1006
1
      *p = 0;
1007
1
      p = xstrconcat (buffer, "/bin/gpgconf.ctl", NULL);
1008
1
      xfree (buffer);
1009
1
      buffer = p;
1010
1
      parse_gpgconf_ctl (buffer);
1011
1
      xfree (buffer);
1012
1
    }
1013
1014
1
  if (!gpgconf_ctl.valid)
1015
1
    return NULL; /* No valid entries in gpgconf.ctl  */
1016
1017
0
  switch (wantdir)
1018
0
    {
1019
0
    case WANTDIR_ROOT:    return gpgconf_ctl.rootdir;
1020
0
    case WANTDIR_SYSCONF: return gpgconf_ctl.sysconfdir;
1021
0
    case WANTDIR_SOCKET:  return gpgconf_ctl.socketdir;
1022
0
    }
1023
1024
0
  return NULL; /* Not reached.  */
1025
0
}
1026
#endif /* Unix */
1027
1028
1029
#ifdef HAVE_W32_SYSTEM
1030
static const char *
1031
w32_commondir (void)
1032
{
1033
  static char *dir;
1034
1035
  if (!dir)
1036
    {
1037
      const char *rdir;
1038
      char *path;
1039
1040
      /* Make sure that w32_rootdir has been called so that we are
1041
         able to check the portable application flag.  The common dir
1042
         is the identical to the rootdir.  In that case there is also
1043
         no need to strdup its value.  */
1044
      rdir = w32_rootdir ();
1045
      if (w32_portable_app)
1046
        return rdir;
1047
1048
      path = w32_shgetfolderpath (NULL, CSIDL_COMMON_APPDATA, NULL, 0);
1049
      if (path)
1050
        {
1051
          dir = xstrconcat (path, "\\GNU", NULL);
1052
          /* No auto create of the directory.  Either the installer or
1053
           * the admin has to create these directories.  */
1054
        }
1055
      else
1056
        {
1057
          /* Folder not found or defined - probably an old Windows
1058
           * version.  Use the installation directory instead.  */
1059
          dir = xstrdup (rdir);
1060
        }
1061
      gpgrt_annotate_leaked_object (dir);
1062
    }
1063
1064
  return dir;
1065
}
1066
1067
1068
/*
1069
 * On Windows, isatty returns TRUE when it's NUL device.
1070
 * We need additional check.
1071
 */
1072
int
1073
gnupg_isatty (int fd)
1074
{
1075
  HANDLE h;
1076
  DWORD mode;
1077
1078
  if (!isatty (fd))
1079
    return 0;
1080
1081
  h = (HANDLE)_get_osfhandle (fd);
1082
  if (h == INVALID_HANDLE_VALUE)
1083
    return 0;
1084
1085
  if (!GetConsoleMode (h, &mode))
1086
    return 0;
1087
1088
  return 1;
1089
}
1090
#endif /*HAVE_W32_SYSTEM*/
1091
1092
1093
/* Change the homedir.  Some care must be taken to set this early
1094
 * enough because previous calls to gnupg_homedir may else return a
1095
 * different string.  */
1096
void
1097
gnupg_set_homedir (const char *newdir)
1098
1
{
1099
1
  char *tmp = NULL;
1100
1101
1
  if (!newdir || !*newdir)
1102
0
    newdir = default_homedir ();
1103
1
  else
1104
1
    {
1105
1
      tmp = copy_dir_with_fixup (newdir);
1106
1
      if (tmp)
1107
1
        newdir = tmp;
1108
1109
1
      if (!is_gnupg_default_homedir (newdir))
1110
1
        non_default_homedir = 1;
1111
1
    }
1112
1
  xfree (the_gnupg_homedir);
1113
1
  the_gnupg_homedir = make_absfilename (newdir, NULL);;
1114
1
  xfree (tmp);
1115
  /* Fixme: Should we use
1116
   *   gpgrt_annotate_leaked_object(the_gnupg_homedir)
1117
   * despite that we may free and allocate a new one in some
1118
   * cases?  */
1119
1
}
1120
1121
1122
/* Create the homedir directory only if the supplied directory name is
1123
 * the same as the default one.  This way we avoid to create arbitrary
1124
 * directories when a non-default home directory is used.  To cope
1125
 * with HOME, we do compare only the suffix if we see that the default
1126
 * homedir does start with a tilde.  If the mkdir fails the function
1127
 * terminates the process.  If QUIET is set not diagnostic is printed
1128
 * on homedir creation.  */
1129
void
1130
gnupg_maybe_make_homedir (const char *fname, int quiet)
1131
0
{
1132
0
  const char *defhome = standard_homedir ();
1133
1134
0
  if (
1135
#ifdef HAVE_W32_SYSTEM
1136
      ( !compare_filenames (fname, defhome) )
1137
#else
1138
0
      ( *defhome == '~'
1139
0
        && (strlen(fname) >= strlen (defhome+1)
1140
0
            && !strcmp(fname+strlen(fname)-strlen(defhome+1), defhome+1 ) ))
1141
0
      || (*defhome != '~'  && !compare_filenames( fname, defhome ) )
1142
0
#endif
1143
0
      )
1144
0
    {
1145
0
      if (gnupg_mkdir (fname, "-rwx"))
1146
0
        log_fatal ( _("can't create directory '%s': %s\n"),
1147
0
                    fname, strerror(errno) );
1148
0
      else
1149
0
        {
1150
0
          if (!quiet )
1151
0
            log_info ( _("directory '%s' created\n"), fname );
1152
0
          create_common_conf (fname);
1153
0
        }
1154
0
    }
1155
0
}
1156
1157
1158
/* Return the file name of this process.  On error NULL is returns.
1159
 * The caller must free the return value.  */
1160
char *
1161
gnupg_myproc_self (void)
1162
0
{
1163
0
  char *result;
1164
#ifdef HAVE_W32_SYSTEM
1165
  result = w32_myproc_self ();
1166
#elif defined(__APPLE__)
1167
  result = macos_myproc_self (0);
1168
#else
1169
0
  result = unix_myproc_self (0);
1170
0
#endif
1171
0
  if (!*result)
1172
0
    {
1173
0
      xfree (result);
1174
0
      result= NULL;
1175
0
    }
1176
0
 return result;
1177
0
}
1178
1179
1180
1181
/* Return the homedir.  The returned string is valid until another
1182
 * gnupg-set-homedir call.  This is always an absolute directory name.
1183
 * The function replaces the former global var opt.homedir.  */
1184
const char *
1185
gnupg_homedir (void)
1186
4
{
1187
  /* If a homedir has not been set, set it to the default.  */
1188
4
  if (!the_gnupg_homedir)
1189
0
    the_gnupg_homedir = make_absfilename (default_homedir (), NULL);
1190
4
  return the_gnupg_homedir;
1191
4
}
1192
1193
1194
/* Return whether the home dir is the default one.  */
1195
int
1196
gnupg_default_homedir_p (void)
1197
0
{
1198
0
  return !non_default_homedir;
1199
0
}
1200
1201
1202
/* Return the directory name used by daemons for their current working
1203
 * directory.  */
1204
const char *
1205
gnupg_daemon_rootdir (void)
1206
0
{
1207
#ifdef HAVE_W32_SYSTEM
1208
  static char *name;
1209
1210
  if (!name)
1211
    {
1212
      char path[MAX_PATH];
1213
      size_t n;
1214
1215
      n = GetSystemDirectoryA (path, sizeof path);
1216
      if (!n || n >= sizeof path)
1217
        name = xstrdup ("/"); /* Error - use the current top dir instead.  */
1218
      else
1219
        name = xstrdup (path);
1220
      gpgrt_annotate_leaked_object (name);
1221
    }
1222
1223
  return name;
1224
1225
#else /*!HAVE_W32_SYSTEM*/
1226
0
  return "/";
1227
0
#endif /*!HAVE_W32_SYSTEM*/
1228
0
}
1229
1230
1231
/* Helper for gnupg-socketdir.  This is a global function, so that
1232
 * gpgconf can use it for its --create-socketdir command.  If
1233
 * SKIP_CHECKS is set permission checks etc. are not done.  The
1234
 * function always returns a malloced directory name and stores these
1235
 * bit flags at R_INFO:
1236
 *
1237
 *   1 := Internal error, stat failed, out of core, etc.
1238
 *   2 := No /run/user directory.
1239
 *   4 := Directory not owned by the user, not a directory
1240
 *        or wrong permissions.
1241
 *   8 := Same as 4 but for the subdir.
1242
 *  16 := mkdir failed
1243
 *  32 := Non default homedir; checking subdir.
1244
 *  64 := Subdir does not exist.
1245
 * 128 := Using homedir as fallback.
1246
 */
1247
char *
1248
_gnupg_socketdir_internal (int skip_checks, unsigned *r_info)
1249
1
{
1250
#if defined(HAVE_W32_SYSTEM)
1251
  char *name;
1252
  gpg_err_code_t ec;
1253
1254
  (void)skip_checks;
1255
1256
  *r_info = 0;
1257
1258
  /* First make sure that non_default_homedir and w32_portable_app can
1259
   * be set.  */
1260
  gnupg_homedir ();
1261
1262
  if (w32_portable_app)
1263
    {
1264
      name = xstrconcat (w32_rootdir (), DIRSEP_S, my_gnupg_dirname (), NULL);
1265
    }
1266
  else
1267
    {
1268
      char *path;
1269
1270
      path = w32_shgetfolderpath (NULL,
1271
                                  CSIDL_LOCAL_APPDATA|CSIDL_FLAG_CREATE,
1272
                                  NULL, 0);
1273
      if (path)
1274
        {
1275
          name = xstrconcat (path, "\\", my_gnupg_dirname (), NULL);
1276
          xfree (path);
1277
          if (gnupg_access (name, F_OK))
1278
            gnupg_mkdir (name, "-rwx");
1279
        }
1280
      else
1281
        {
1282
          name = xstrdup (gnupg_homedir ());
1283
        }
1284
    }
1285
1286
  /* If a non default homedir is used, we check whether an
1287
   * corresponding sub directory below the socket dir is available
1288
   * and use that.  We hash the non default homedir to keep the new
1289
   * subdir short enough.  */
1290
  if (non_default_homedir)
1291
    {
1292
      char sha1buf[20];
1293
      struct stat sb;
1294
      char *suffix;
1295
      char *p;
1296
1297
      *r_info |= 32; /* Testing subdir.  */
1298
1299
      /* Canonicalize the name to avoid problems with mixed case
1300
       * names.  Note that we use only 10 bytes of the hash because on
1301
       * Windows the account name is also part of the name.  */
1302
      suffix = ascii_strlwr (xstrdup (gnupg_homedir ()));
1303
      for (p=suffix; *p; p++)
1304
        if ( *p == '\\')
1305
          *p = '/';
1306
      gcry_md_hash_buffer (GCRY_MD_SHA1, sha1buf, suffix, strlen (suffix));
1307
      xfree (suffix);
1308
      suffix = zb32_encode (sha1buf, 8*10);
1309
      if (!suffix)
1310
        {
1311
          *r_info |= 1; /* Out of core etc. */
1312
          goto leave_w32;
1313
        }
1314
      p = xstrconcat (name, "\\d.", suffix, NULL);
1315
      xfree (suffix);
1316
      xfree (name);
1317
      name = p;
1318
1319
      /* Stat that directory and check constraints.
1320
       * The command
1321
       *    gpgconf --remove-socketdir
1322
       * can be used to remove that directory.  */
1323
      if (gnupg_stat (name, &sb))
1324
        {
1325
          if (errno != ENOENT)
1326
            *r_info |= 1; /* stat failed. */
1327
          else if (!skip_checks)
1328
            {
1329
              /* Try to create the directory and check again.  */
1330
              ec = gnupg_mkdir (name, "-rwx");
1331
              if (ec && ec != GPG_ERR_EEXIST)
1332
                *r_info |= 16; /* mkdir failed.  */
1333
              else if (gnupg_stat (name, &sb))
1334
                {
1335
                  if (errno != ENOENT)
1336
                    *r_info |= 1; /* stat failed. */
1337
                  else
1338
                    *r_info |= 64; /* Subdir does not exist.  */
1339
                }
1340
              else
1341
                goto leave_w32; /* Success!  */
1342
            }
1343
          else
1344
            *r_info |= 64; /* Subdir does not exist.  */
1345
          if (!skip_checks)
1346
            {
1347
              xfree (name);
1348
              name = NULL;
1349
              goto leave_w32;
1350
            }
1351
        }
1352
    }
1353
1354
 leave_w32:
1355
  /* If nothing works - fall back to the homedir.  */
1356
  if (!name)
1357
    {
1358
      *r_info |= 128; /* Fallback.  */
1359
      name = xstrdup (gnupg_homedir ());
1360
    }
1361
1362
#elif !defined(HAVE_STAT)
1363
  char *name;
1364
1365
  (void)skip_checks;
1366
  *r_info = 0;
1367
  name = xstrdup (gnupg_homedir ());
1368
1369
#else /* Unix and stat(2) available. */
1370
1371
1
  static const char * const bases[] = {
1372
#ifdef USE_RUN_GNUPG_USER_SOCKET
1373
    "/run/gnupg",
1374
#endif
1375
1
    "/run",
1376
#ifdef USE_RUN_GNUPG_USER_SOCKET
1377
    "/var/run/gnupg",
1378
#endif
1379
1
    "/var/run",
1380
1
    NULL
1381
1
  };
1382
1
  int i;
1383
1
  struct stat sb;
1384
1
  char prefixbuffer[256];
1385
1
  const char *prefix;
1386
1
  const char *s;
1387
1
  char *name = NULL;
1388
1
  const char *gnupgname = my_gnupg_dirname ();
1389
1
  gpg_err_code_t ec;
1390
1391
1
  *r_info = 0;
1392
1393
  /* First make sure that non_default_homedir can be set.  */
1394
1
  gnupg_homedir ();
1395
1396
  /* It has been suggested to first check XDG_RUNTIME_DIR envvar.
1397
   * However, the specs state that the lifetime of the directory MUST
1398
   * be bound to the user being logged in.  Now GnuPG may also be run
1399
   * as a background process with no (desktop) user logged in.  Thus
1400
   * we better don't do that.  */
1401
1402
1
  prefix = unix_rootdir (WANTDIR_SOCKET);
1403
1
  if (!prefix)
1404
1
    {
1405
      /* gpgconf.ctl does not specify a directory.  Check whether we
1406
       * have the usual /run/[gnupg/]user dir.  */
1407
3
      for (i=0; bases[i]; i++)
1408
2
        {
1409
2
          snprintf (prefixbuffer, sizeof prefixbuffer, "%s/user/%u",
1410
2
                    bases[i], (unsigned int)getuid ());
1411
2
          prefix = prefixbuffer;
1412
2
          if (!stat (prefix, &sb) && S_ISDIR(sb.st_mode))
1413
0
            break;
1414
2
        }
1415
1
      if (!bases[i])
1416
1
        {
1417
1
          *r_info |= 2; /* No /run/user directory.  */
1418
1
          goto leave;
1419
1
        }
1420
1421
0
      if (sb.st_uid != getuid ())
1422
0
        {
1423
0
          *r_info |= 4; /* Not owned by the user.  */
1424
0
          if (!skip_checks)
1425
0
            goto leave;
1426
0
        }
1427
1428
0
      if (strlen (prefix) + strlen (gnupgname) + 2 >= sizeof prefixbuffer)
1429
0
        {
1430
0
          *r_info |= 1; /* Ooops: Buffer too short to append "/gnupg".  */
1431
0
          goto leave;
1432
0
        }
1433
0
      strcat (prefixbuffer, "/");
1434
0
      strcat (prefixbuffer, gnupgname);
1435
0
    }
1436
1437
  /* Check whether the gnupg sub directory (or the specified directory)
1438
   * has proper permissions.  */
1439
0
  if (stat (prefix, &sb))
1440
0
    {
1441
0
      if (errno != ENOENT)
1442
0
        {
1443
0
          *r_info |= 1; /* stat failed.  */
1444
0
          goto leave;
1445
0
        }
1446
1447
      /* Try to create the directory and check again.
1448
       * Here comes a possible race condition:
1449
       *     stat(2) above failed by ENOENT, but another process does
1450
       *     mkdir(2) before we do mkdir(2)
1451
       * So, an error with EEXIST should be handled.
1452
       */
1453
0
      ec = gnupg_mkdir (prefix, "-rwx");
1454
0
      if (ec && ec != GPG_ERR_EEXIST)
1455
0
        {
1456
0
          *r_info |= 16; /* mkdir failed.  */
1457
0
          goto leave;
1458
0
        }
1459
0
      if (stat (prefix, &sb))
1460
0
        {
1461
0
          *r_info |= 1; /* stat failed.  */
1462
0
          goto leave;
1463
0
        }
1464
0
    }
1465
  /* Check that it is a directory, owned by the user, and only the
1466
   * user has permissions to use it.  */
1467
0
  if (!S_ISDIR(sb.st_mode)
1468
0
      || sb.st_uid != getuid ()
1469
0
      || (sb.st_mode & (S_IRWXG|S_IRWXO)))
1470
0
    {
1471
0
      *r_info |= 4; /* Bad permissions or not a directory. */
1472
0
      if (!skip_checks)
1473
0
        goto leave;
1474
0
    }
1475
1476
  /* If a non default homedir is used, we check whether an
1477
   * corresponding sub directory below the socket dir is available
1478
   * and use that.  We hash the non default homedir to keep the new
1479
   * subdir short enough.  */
1480
0
  if (non_default_homedir)
1481
0
    {
1482
0
      char sha1buf[20];
1483
0
      char *suffix;
1484
1485
0
      *r_info |= 32; /* Testing subdir.  */
1486
0
      s = gnupg_homedir ();
1487
0
      gcry_md_hash_buffer (GCRY_MD_SHA1, sha1buf, s, strlen (s));
1488
0
      suffix = zb32_encode (sha1buf, 8*15);
1489
0
      if (!suffix)
1490
0
        {
1491
0
          *r_info |= 1; /* Out of core etc. */
1492
0
          goto leave;
1493
0
        }
1494
0
      name = strconcat (prefix, "/d.", suffix, NULL);
1495
0
      xfree (suffix);
1496
0
      if (!name)
1497
0
        {
1498
0
          *r_info |= 1; /* Out of core etc. */
1499
0
          goto leave;
1500
0
        }
1501
1502
      /* Stat that directory and check constraints.
1503
       * The command
1504
       *    gpgconf --remove-socketdir
1505
       * can be used to remove that directory.  */
1506
0
      if (stat (name, &sb))
1507
0
        {
1508
0
          if (errno != ENOENT)
1509
0
            *r_info |= 1; /* stat failed. */
1510
0
          else if (!skip_checks)
1511
0
            {
1512
              /* Try to create the directory and check again.  */
1513
0
              ec = gnupg_mkdir (name, "-rwx");
1514
0
              if (ec && ec != GPG_ERR_EEXIST)
1515
0
                *r_info |= 16; /* mkdir failed.  */
1516
0
              else if (stat (prefix, &sb))
1517
0
                {
1518
0
                  if (errno != ENOENT)
1519
0
                    *r_info |= 1; /* stat failed. */
1520
0
                  else
1521
0
                    *r_info |= 64; /* Subdir does not exist.  */
1522
0
                }
1523
0
              else
1524
0
                goto leave; /* Success!  */
1525
0
            }
1526
0
          else
1527
0
            *r_info |= 64; /* Subdir does not exist.  */
1528
0
          if (!skip_checks)
1529
0
            {
1530
0
              xfree (name);
1531
0
              name = NULL;
1532
0
              goto leave;
1533
0
            }
1534
0
        }
1535
0
      else if (!S_ISDIR(sb.st_mode)
1536
0
               || sb.st_uid != getuid ()
1537
0
               || (sb.st_mode & (S_IRWXG|S_IRWXO)))
1538
0
        {
1539
0
          *r_info |= 8; /* Bad permissions or subdir is not a directory.  */
1540
0
          if (!skip_checks)
1541
0
            {
1542
0
              xfree (name);
1543
0
              name = NULL;
1544
0
              goto leave;
1545
0
            }
1546
0
        }
1547
0
    }
1548
0
  else
1549
0
    name = xstrdup (prefix);
1550
1551
1
 leave:
1552
  /* If nothing works fall back to the homedir.  */
1553
1
  if (!name)
1554
1
    {
1555
1
      *r_info |= 128; /* Fallback.  */
1556
1
      name = xstrdup (gnupg_homedir ());
1557
1
    }
1558
1559
1
#endif /* Unix */
1560
1561
1
  return name;
1562
0
}
1563
1564
1565
/*
1566
 * Return the name of the socket dir.  That is the directory used for
1567
 * the IPC local sockets.  This is an absolute directory name.
1568
 */
1569
const char *
1570
gnupg_socketdir (void)
1571
2.71k
{
1572
2.71k
  static char *name;
1573
1574
2.71k
  if (!name)
1575
1
    {
1576
1
      unsigned int dummy;
1577
1
      name = _gnupg_socketdir_internal (0, &dummy);
1578
1
      gpgrt_annotate_leaked_object (name);
1579
1
    }
1580
1581
2.71k
  return name;
1582
2.71k
}
1583
1584
1585
/* Return the name of the sysconfdir.  This is a static string.  This
1586
   function is required because under Windows we can't simply compile
1587
   it in.  */
1588
const char *
1589
gnupg_sysconfdir (void)
1590
0
{
1591
#ifdef HAVE_W32_SYSTEM
1592
  static char *name;
1593
1594
  if (!name)
1595
    {
1596
      name = xstrconcat (w32_commondir (), DIRSEP_S, "etc", DIRSEP_S,
1597
                         my_gnupg_dirname (), NULL);
1598
      gpgrt_annotate_leaked_object (name);
1599
    }
1600
  return name;
1601
#else /*!HAVE_W32_SYSTEM*/
1602
0
  const char *dir = unix_rootdir (WANTDIR_SYSCONF);
1603
0
  if (dir)
1604
0
    return dir;
1605
0
  else
1606
0
    return GNUPG_SYSCONFDIR;
1607
0
#endif /*!HAVE_W32_SYSTEM*/
1608
0
}
1609
1610
1611
const char *
1612
gnupg_bindir (void)
1613
0
{
1614
0
  static char *name;
1615
0
  const char *rdir;
1616
1617
#if defined(HAVE_W32_SYSTEM)
1618
  rdir = w32_rootdir ();
1619
  if (w32_bin_is_bin)
1620
    {
1621
      if (!name)
1622
        {
1623
          name = xstrconcat (rdir, DIRSEP_S "bin", NULL);
1624
          gpgrt_annotate_leaked_object (name);
1625
        }
1626
      return name;
1627
    }
1628
  else
1629
    return rdir;
1630
#else /*!HAVE_W32_SYSTEM*/
1631
0
  rdir = unix_rootdir (WANTDIR_ROOT);
1632
0
  if (rdir)
1633
0
    {
1634
0
      if (!name)
1635
0
        {
1636
0
          name = xstrconcat (rdir, DIRSEP_S "bin", NULL);
1637
0
          gpgrt_annotate_leaked_object (name);
1638
0
        }
1639
0
      return name;
1640
0
    }
1641
0
  else
1642
0
    return GNUPG_BINDIR;
1643
0
#endif /*!HAVE_W32_SYSTEM*/
1644
0
}
1645
1646
1647
/* Return the name of the libexec directory.  The name is allocated in
1648
   a static area on the first use.  This function won't fail. */
1649
const char *
1650
gnupg_libexecdir (void)
1651
0
{
1652
#ifdef HAVE_W32_SYSTEM
1653
  return gnupg_bindir ();
1654
#else /*!HAVE_W32_SYSTEM*/
1655
0
  static char *name;
1656
0
  const char *rdir;
1657
1658
0
  rdir = unix_rootdir (WANTDIR_ROOT);
1659
0
  if (rdir)
1660
0
    {
1661
0
      if (!name)
1662
0
        {
1663
0
          name = xstrconcat (rdir, DIRSEP_S "libexec", NULL);
1664
0
          gpgrt_annotate_leaked_object (name);
1665
0
        }
1666
0
      return name;
1667
0
    }
1668
0
  else
1669
0
    return GNUPG_LIBEXECDIR;
1670
0
#endif /*!HAVE_W32_SYSTEM*/
1671
0
}
1672
1673
const char *
1674
gnupg_libdir (void)
1675
0
{
1676
0
  static char *name;
1677
1678
#ifdef HAVE_W32_SYSTEM
1679
  if (!name)
1680
    {
1681
      name = xstrconcat (w32_rootdir (), DIRSEP_S "lib" DIRSEP_S "gnupg", NULL);
1682
      gpgrt_annotate_leaked_object (name);
1683
    }
1684
  return name;
1685
#else /*!HAVE_W32_SYSTEM*/
1686
0
  const char *rdir;
1687
1688
0
  rdir = unix_rootdir (WANTDIR_ROOT);
1689
0
  if (rdir)
1690
0
    {
1691
0
      if (!name)
1692
0
        {
1693
0
          name = xstrconcat (rdir, DIRSEP_S "lib", DIRSEP_S, "gnupg", NULL);
1694
0
          gpgrt_annotate_leaked_object (name);
1695
0
        }
1696
0
      return name;
1697
0
    }
1698
0
  else
1699
0
    return GNUPG_LIBDIR;
1700
0
#endif /*!HAVE_W32_SYSTEM*/
1701
0
}
1702
1703
const char *
1704
gnupg_datadir (void)
1705
0
{
1706
0
  static char *name;
1707
1708
#ifdef HAVE_W32_SYSTEM
1709
  if (!name)
1710
    {
1711
      name = xstrconcat (w32_rootdir (), DIRSEP_S "share" DIRSEP_S "gnupg",
1712
                         NULL);
1713
      gpgrt_annotate_leaked_object (name);
1714
    }
1715
  return name;
1716
#else /*!HAVE_W32_SYSTEM*/
1717
0
  const char *rdir;
1718
1719
0
  rdir = unix_rootdir (WANTDIR_ROOT);
1720
0
  if (rdir)
1721
0
    {
1722
0
      if (!name)
1723
0
        {
1724
0
          name = xstrconcat (rdir, DIRSEP_S "share" DIRSEP_S "gnupg", NULL);
1725
0
          gpgrt_annotate_leaked_object (name);
1726
0
        }
1727
0
      return name;
1728
0
    }
1729
0
  else
1730
0
    return GNUPG_DATADIR;
1731
0
#endif /*!HAVE_W32_SYSTEM*/
1732
0
}
1733
1734
1735
const char *
1736
gnupg_localedir (void)
1737
0
{
1738
0
  static char *name;
1739
1740
#ifdef HAVE_W32_SYSTEM
1741
  if (!name)
1742
    {
1743
      name = xstrconcat (w32_rootdir (), DIRSEP_S "share" DIRSEP_S "locale",
1744
                         NULL);
1745
      gpgrt_annotate_leaked_object (name);
1746
    }
1747
  return name;
1748
#else /*!HAVE_W32_SYSTEM*/
1749
0
  const char *rdir;
1750
1751
0
  rdir = unix_rootdir (WANTDIR_ROOT);
1752
0
  if (rdir)
1753
0
    {
1754
0
      if (!name)
1755
0
        {
1756
0
          name = xstrconcat (rdir, DIRSEP_S "share" DIRSEP_S "locale", NULL);
1757
0
          gpgrt_annotate_leaked_object (name);
1758
0
        }
1759
0
      return name;
1760
0
    }
1761
0
  else
1762
0
    return LOCALEDIR;
1763
0
#endif /*!HAVE_W32_SYSTEM*/
1764
0
}
1765
1766
1767
/* Return the standard socket name used by gpg-agent.  */
1768
const char *
1769
gpg_agent_socket_name (void)
1770
0
{
1771
0
  static char *name;
1772
1773
0
  if (!name)
1774
0
    {
1775
0
      name = make_filename (gnupg_socketdir (), GPG_AGENT_SOCK_NAME, NULL);
1776
0
      gpgrt_annotate_leaked_object (name);
1777
0
    }
1778
0
  return name;
1779
0
}
1780
1781
/* Return the user socket name used by DirMngr.  */
1782
const char *
1783
dirmngr_socket_name (void)
1784
0
{
1785
0
  static char *name;
1786
1787
0
  if (!name)
1788
0
    {
1789
0
      name = make_filename (gnupg_socketdir (), DIRMNGR_SOCK_NAME, NULL);
1790
0
      gpgrt_annotate_leaked_object (name);
1791
0
    }
1792
0
  return name;
1793
0
}
1794
1795
1796
/* Return the user socket name used by Keyboxd.  */
1797
const char *
1798
keyboxd_socket_name (void)
1799
0
{
1800
0
  static char *name;
1801
1802
0
  if (!name)
1803
0
    {
1804
0
      name = make_filename (gnupg_socketdir (), KEYBOXD_SOCK_NAME, NULL);
1805
0
      gpgrt_annotate_leaked_object (name);
1806
0
    }
1807
0
  return name;
1808
0
}
1809
1810
1811
/* Return the default pinentry name.  If RESET is true the internal
1812
   cache is first flushed.  */
1813
static const char *
1814
get_default_pinentry_name (int reset)
1815
0
{
1816
0
  static struct {
1817
0
    const char *(*rfnc)(void);
1818
0
    const char *name;
1819
0
  } names[] = {
1820
    /* The first entry is what we return in case we found no
1821
       other pinentry.  */
1822
0
    { gnupg_bindir, DIRSEP_S "pinentry" EXEEXT_S },
1823
#ifdef HAVE_W32_SYSTEM
1824
    /* Try Gpg4win directory (with bin and without.) */
1825
    { w32_rootdir, "\\..\\Gpg4win\\bin\\pinentry.exe" },
1826
    { w32_rootdir, "\\..\\Gpg4win\\pinentry.exe" },
1827
    /* Try a pinentry in a dir above us */
1828
    { w32_rootdir, "\\..\\bin\\pinentry.exe" },
1829
    /* Try old Gpgwin directory.  */
1830
    { w32_rootdir, "\\..\\GNU\\GnuPG\\pinentry.exe" },
1831
    /* Try a Pinentry from the common GNU dir.  */
1832
    { w32_rootdir, "\\..\\GNU\\bin\\pinentry.exe" },
1833
#endif
1834
    /* Last chance is a pinentry-basic (which comes with the
1835
       GnuPG 2.1 Windows installer).  */
1836
0
    { gnupg_bindir, DIRSEP_S "pinentry-basic" EXEEXT_S }
1837
0
  };
1838
0
  static char *name;
1839
1840
0
  if (reset)
1841
0
    {
1842
0
      xfree (name);
1843
0
      name = NULL;
1844
0
    }
1845
1846
0
  if (!name)
1847
0
    {
1848
0
      int i;
1849
1850
0
      for (i=0; i < DIM(names); i++)
1851
0
        {
1852
0
          char *name2;
1853
1854
0
          name2 = xstrconcat (names[i].rfnc (), names[i].name, NULL);
1855
0
          if (!gnupg_access (name2, F_OK))
1856
0
            {
1857
              /* Use that pinentry.  */
1858
0
              xfree (name);
1859
0
              name = name2;
1860
0
              break;
1861
0
            }
1862
0
          if (!i) /* Store the first as fallback return.  */
1863
0
            name = name2;
1864
0
          else
1865
0
            xfree (name2);
1866
0
        }
1867
0
      if (name)
1868
0
        gpgrt_annotate_leaked_object (name);
1869
0
    }
1870
1871
0
  return name;
1872
0
}
1873
1874
1875
/* If set, 'gnupg_module_name' returns modules from that build
1876
 * directory.  */
1877
static char *gnupg_build_directory;
1878
1879
/* For sanity checks.  */
1880
static int gnupg_module_name_called;
1881
1882
1883
/* Set NEWDIR as the new build directory.  This will make
1884
 * 'gnupg_module_name' return modules from that build directory.  Must
1885
 * be called before any invocation of 'gnupg_module_name', and must
1886
 * not be called twice.  It can be used by test suites to make sure
1887
 * the components from the build directory are used instead of
1888
 * potentially outdated installed ones.
1889
 * Fixme: It might be better to make use of the newer gpgconf.ctl feature
1890
 *        for regression testing.
1891
 */
1892
void
1893
gnupg_set_builddir (const char *newdir)
1894
0
{
1895
0
  log_assert (! gnupg_module_name_called);
1896
0
  log_assert (! gnupg_build_directory);
1897
0
  gnupg_build_directory = xtrystrdup (newdir);
1898
0
}
1899
1900
1901
/* If no build directory has been configured, try to set it from the
1902
 * environment.  We only do this in development builds to avoid
1903
 * increasing the set of influential environment variables and hence
1904
 * the attack surface of production builds.  */
1905
static void
1906
gnupg_set_builddir_from_env (void)
1907
0
{
1908
#if defined(IS_DEVELOPMENT_VERSION) || defined(ENABLE_GNUPG_BUILDDIR_ENVVAR)
1909
  if (gnupg_build_directory)
1910
    return;
1911
1912
  gnupg_build_directory = getenv ("GNUPG_BUILDDIR");
1913
#endif
1914
0
}
1915
1916
1917
/* Return the file name of a helper tool.  WHICH is one of the
1918
   GNUPG_MODULE_NAME_foo constants.  */
1919
const char *
1920
gnupg_module_name (int which)
1921
0
{
1922
0
  gnupg_set_builddir_from_env ();
1923
0
  gnupg_module_name_called = 1;
1924
1925
0
#define X(a,b,c) do {                                                   \
1926
0
    static char *name;                                                  \
1927
0
    if (!name) {                                                        \
1928
0
      name = gnupg_build_directory                                      \
1929
0
        ? xstrconcat (gnupg_build_directory,                            \
1930
0
                      DIRSEP_S b DIRSEP_S c EXEEXT_S, NULL)             \
1931
0
        : xstrconcat (gnupg_ ## a (), DIRSEP_S c EXEEXT_S, NULL);       \
1932
0
      gpgrt_annotate_leaked_object (name);                              \
1933
0
    }                                                                   \
1934
0
    return name;                                                        \
1935
0
  } while (0)
1936
1937
0
  switch (which)
1938
0
    {
1939
0
    case GNUPG_MODULE_NAME_AGENT:
1940
#ifdef GNUPG_DEFAULT_AGENT
1941
      return GNUPG_DEFAULT_AGENT;
1942
#else
1943
0
      X(bindir, "agent", "gpg-agent");
1944
0
#endif
1945
1946
0
    case GNUPG_MODULE_NAME_PINENTRY:
1947
#ifdef GNUPG_DEFAULT_PINENTRY
1948
      return GNUPG_DEFAULT_PINENTRY;  /* (Set by a configure option) */
1949
#else
1950
0
      return get_default_pinentry_name (0);
1951
0
#endif
1952
1953
0
    case GNUPG_MODULE_NAME_SCDAEMON:
1954
#ifdef GNUPG_DEFAULT_SCDAEMON
1955
      return GNUPG_DEFAULT_SCDAEMON;
1956
#else
1957
0
      X(libexecdir, "scd", "scdaemon");
1958
0
#endif
1959
1960
0
    case GNUPG_MODULE_NAME_TPM2DAEMON:
1961
#ifdef GNUPG_DEFAULT_TPM2DAEMON
1962
      return GNUPG_DEFAULT_TPM2DAEMON;
1963
#else
1964
0
      X(libexecdir, "tpm2d", TPM2DAEMON_NAME);
1965
0
#endif
1966
1967
0
    case GNUPG_MODULE_NAME_DIRMNGR:
1968
#ifdef GNUPG_DEFAULT_DIRMNGR
1969
      return GNUPG_DEFAULT_DIRMNGR;
1970
#else
1971
0
      X(bindir, "dirmngr", DIRMNGR_NAME);
1972
0
#endif
1973
1974
0
    case GNUPG_MODULE_NAME_KEYBOXD:
1975
#ifdef GNUPG_DEFAULT_KEYBOXD
1976
      return GNUPG_DEFAULT_KEYBOXD;
1977
#else
1978
0
      X(libexecdir, "kbx", KEYBOXD_NAME);
1979
0
#endif
1980
1981
0
    case GNUPG_MODULE_NAME_PROTECT_TOOL:
1982
#ifdef GNUPG_DEFAULT_PROTECT_TOOL
1983
      return GNUPG_DEFAULT_PROTECT_TOOL;
1984
#else
1985
0
      X(libexecdir, "agent", "gpg-protect-tool");
1986
0
#endif
1987
1988
0
    case GNUPG_MODULE_NAME_DIRMNGR_LDAP:
1989
#ifdef GNUPG_DEFAULT_DIRMNGR_LDAP
1990
      return GNUPG_DEFAULT_DIRMNGR_LDAP;
1991
#else
1992
0
      X(libexecdir, "dirmngr", "dirmngr_ldap");
1993
0
#endif
1994
1995
0
    case GNUPG_MODULE_NAME_CHECK_PATTERN:
1996
0
      X(libexecdir, "tools", "gpg-check-pattern");
1997
1998
0
    case GNUPG_MODULE_NAME_GPGSM:
1999
0
      X(bindir, "sm", "gpgsm");
2000
2001
0
    case GNUPG_MODULE_NAME_GPG:
2002
0
      X(bindir, "g10", GPG_NAME);
2003
2004
0
    case GNUPG_MODULE_NAME_GPGV:
2005
0
      X(bindir, "g10", GPG_NAME "v");
2006
2007
0
    case GNUPG_MODULE_NAME_CONNECT_AGENT:
2008
0
      X(bindir, "tools", "gpg-connect-agent");
2009
2010
0
    case GNUPG_MODULE_NAME_GPGCONF:
2011
0
      X(bindir, "tools", "gpgconf");
2012
2013
0
    case GNUPG_MODULE_NAME_CARD:
2014
0
      X(bindir, "tools", "gpg-card");
2015
2016
0
    case GNUPG_MODULE_NAME_GPGTAR:
2017
0
      X(bindir, "tools", "gpgtar");
2018
2019
0
    default:
2020
0
      BUG ();
2021
0
    }
2022
0
#undef X
2023
0
}
2024
2025
2026
/* Flush some of the cached module names.  This is for example used by
2027
   gpg-agent to allow configuring a different pinentry.  */
2028
void
2029
gnupg_module_name_flush_some (void)
2030
0
{
2031
0
  (void)get_default_pinentry_name (1);
2032
0
}