LCOV - code coverage report
Current view: top level - common - homedir.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 8 59 13.6 %
Date: 2015-11-05 17:10:59 Functions: 3 14 21.4 %

          Line data    Source code
       1             : /* homedir.c - Setup the home directory.
       2             :  * Copyright (C) 2004, 2006, 2007, 2010 Free Software Foundation, Inc.
       3             :  * Copyright (C) 2013 Werner Koch
       4             :  *
       5             :  * This file is part of GnuPG.
       6             :  *
       7             :  * This file is free software; you can redistribute it and/or modify
       8             :  * it under the terms of either
       9             :  *
      10             :  *   - the GNU Lesser General Public License as published by the Free
      11             :  *     Software Foundation; either version 3 of the License, or (at
      12             :  *     your option) any later version.
      13             :  *
      14             :  * or
      15             :  *
      16             :  *   - the GNU General Public License as published by the Free
      17             :  *     Software Foundation; either version 2 of the License, or (at
      18             :  *     your option) any later version.
      19             :  *
      20             :  * or both in parallel, as here.
      21             :  *
      22             :  * This file is distributed in the hope that it will be useful,
      23             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      24             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      25             :  * GNU General Public License for more details.
      26             :  *
      27             :  * You should have received a copy of the GNU General Public License
      28             :  * along with this program; if not, see <http://www.gnu.org/licenses/>.
      29             :  */
      30             : 
      31             : #include <config.h>
      32             : #include <stdlib.h>
      33             : #include <errno.h>
      34             : #include <fcntl.h>
      35             : #include <unistd.h>
      36             : 
      37             : #ifdef HAVE_W32_SYSTEM
      38             : #include <winsock2.h>   /* Due to the stupid mingw64 requirement to
      39             :                            include this header before windows.h which
      40             :                            is often implicitly included.  */
      41             : #include <shlobj.h>
      42             : #ifndef CSIDL_APPDATA
      43             : #define CSIDL_APPDATA 0x001a
      44             : #endif
      45             : #ifndef CSIDL_LOCAL_APPDATA
      46             : #define CSIDL_LOCAL_APPDATA 0x001c
      47             : #endif
      48             : #ifndef CSIDL_COMMON_APPDATA
      49             : #define CSIDL_COMMON_APPDATA 0x0023
      50             : #endif
      51             : #ifndef CSIDL_FLAG_CREATE
      52             : #define CSIDL_FLAG_CREATE 0x8000
      53             : #endif
      54             : #endif /*HAVE_W32_SYSTEM*/
      55             : 
      56             : 
      57             : 
      58             : #include "util.h"
      59             : #include "sysutils.h"
      60             : 
      61             : #ifdef HAVE_W32_SYSTEM
      62             : /* A flag used to indicate that a control file for gpgconf has been
      63             :    detected.  Under Windows the presence of this file indicates a
      64             :    portable installations and triggers several changes:
      65             : 
      66             :    - The GNUGHOME directory is fixed relative to installation
      67             :      directory.  All other means to set the home directory are ignore.
      68             : 
      69             :    - All registry variables will be ignored.
      70             : 
      71             :    This flag is not used on Unix systems.
      72             :  */
      73             : static int w32_portable_app;
      74             : #endif /*HAVE_W32_SYSTEM*/
      75             : 
      76             : #ifdef HAVE_W32_SYSTEM
      77             : /* This flag is true if this process' binary has been installed under
      78             :    bin and not in the root directory as often used before GnuPG 2.1. */
      79             : static int w32_bin_is_bin;
      80             : #endif /*HAVE_W32_SYSTEM*/
      81             : 
      82             : 
      83             : #ifdef HAVE_W32_SYSTEM
      84             : static const char *w32_rootdir (void);
      85             : #endif
      86             : 
      87             : 
      88             : 
      89             : #ifdef HAVE_W32_SYSTEM
      90             : static void
      91             : w32_try_mkdir (const char *dir)
      92             : {
      93             : #ifdef HAVE_W32CE_SYSTEM
      94             :   wchar_t *wdir = utf8_to_wchar (dir);
      95             :   if (wdir)
      96             :     {
      97             :       CreateDirectory (wdir, NULL);
      98             :       xfree (wdir);
      99             :     }
     100             : #else
     101             :   CreateDirectory (dir, NULL);
     102             : #endif
     103             : }
     104             : #endif
     105             : 
     106             : 
     107             : /* This is a helper function to load a Windows function from either of
     108             :    one DLLs. */
     109             : #ifdef HAVE_W32_SYSTEM
     110             : static HRESULT
     111             : w32_shgetfolderpath (HWND a, int b, HANDLE c, DWORD d, LPSTR e)
     112             : {
     113             :   static int initialized;
     114             :   static HRESULT (WINAPI * func)(HWND,int,HANDLE,DWORD,LPSTR);
     115             : 
     116             :   if (!initialized)
     117             :     {
     118             :       static char *dllnames[] = { "shell32.dll", "shfolder.dll", NULL };
     119             :       void *handle;
     120             :       int i;
     121             : 
     122             :       initialized = 1;
     123             : 
     124             :       for (i=0, handle = NULL; !handle && dllnames[i]; i++)
     125             :         {
     126             :           handle = dlopen (dllnames[i], RTLD_LAZY);
     127             :           if (handle)
     128             :             {
     129             :               func = dlsym (handle, "SHGetFolderPathA");
     130             :               if (!func)
     131             :                 {
     132             :                   dlclose (handle);
     133             :                   handle = NULL;
     134             :                 }
     135             :             }
     136             :         }
     137             :     }
     138             : 
     139             :   if (func)
     140             :     return func (a,b,c,d,e);
     141             :   else
     142             :     return -1;
     143             : }
     144             : #endif /*HAVE_W32_SYSTEM*/
     145             : 
     146             : 
     147             : /* Get the standard home directory.  In general this function should
     148             :    not be used as it does not consider a registry value (under W32) or
     149             :    the GNUPGHOME environment variable.  It is better to use
     150             :    default_homedir(). */
     151             : const char *
     152           1 : standard_homedir (void)
     153             : {
     154             : #ifdef HAVE_W32_SYSTEM
     155             :   static const char *dir;
     156             : 
     157             :   if (!dir)
     158             :     {
     159             :       const char *rdir;
     160             : 
     161             :       rdir = w32_rootdir ();
     162             :       if (w32_portable_app)
     163             :         {
     164             :           dir = xstrconcat (rdir, DIRSEP_S "home", NULL);
     165             :         }
     166             :       else
     167             :         {
     168             :           char path[MAX_PATH];
     169             : 
     170             :           /* It might be better to use LOCAL_APPDATA because this is
     171             :              defined as "non roaming" and thus more likely to be kept
     172             :              locally.  For private keys this is desired.  However,
     173             :              given that many users copy private keys anyway forth and
     174             :              back, using a system roaming services might be better
     175             :              than to let them do it manually.  A security conscious
     176             :              user will anyway use the registry entry to have better
     177             :              control.  */
     178             :           if (w32_shgetfolderpath (NULL, CSIDL_APPDATA|CSIDL_FLAG_CREATE,
     179             :                                    NULL, 0, path) >= 0)
     180             :             {
     181             :               char *tmp = xmalloc (strlen (path) + 6 +1);
     182             :               strcpy (stpcpy (tmp, path), "\\gnupg");
     183             :               dir = tmp;
     184             : 
     185             :               /* Try to create the directory if it does not yet exists.  */
     186             :               if (access (dir, F_OK))
     187             :                 w32_try_mkdir (dir);
     188             :             }
     189             :           else
     190             :             dir = GNUPG_DEFAULT_HOMEDIR;
     191             :         }
     192             :     }
     193             :   return dir;
     194             : #else/*!HAVE_W32_SYSTEM*/
     195           1 :   return GNUPG_DEFAULT_HOMEDIR;
     196             : #endif /*!HAVE_W32_SYSTEM*/
     197             : }
     198             : 
     199             : /* Set up the default home directory.  The usual --homedir option
     200             :    should be parsed later. */
     201             : const char *
     202        1349 : default_homedir (void)
     203             : {
     204             :   const char *dir;
     205             : 
     206             : #ifdef HAVE_W32_SYSTEM
     207             :   /* For a portable application we only use the standard homedir.  */
     208             :   w32_rootdir ();
     209             :   if (w32_portable_app)
     210             :     return standard_homedir ();
     211             : #endif /*HAVE_W32_SYSTEM*/
     212             : 
     213        1349 :   dir = getenv ("GNUPGHOME");
     214             : #ifdef HAVE_W32_SYSTEM
     215             :   if (!dir || !*dir)
     216             :     {
     217             :       static const char *saved_dir;
     218             : 
     219             :       if (!saved_dir)
     220             :         {
     221             :           if (!dir || !*dir)
     222             :             {
     223             :               char *tmp;
     224             : 
     225             :               tmp = read_w32_registry_string (NULL,
     226             :                                               GNUPG_REGISTRY_DIR,
     227             :                                               "HomeDir");
     228             :               if (tmp && !*tmp)
     229             :                 {
     230             :                   xfree (tmp);
     231             :                   tmp = NULL;
     232             :                 }
     233             :               if (tmp)
     234             :                 saved_dir = tmp;
     235             :             }
     236             : 
     237             :           if (!saved_dir)
     238             :             saved_dir = standard_homedir ();
     239             :         }
     240             :       dir = saved_dir;
     241             :     }
     242             : #endif /*HAVE_W32_SYSTEM*/
     243        1349 :   if (!dir || !*dir)
     244           0 :     dir = GNUPG_DEFAULT_HOMEDIR;
     245             : 
     246        1349 :   return dir;
     247             : }
     248             : 
     249             : 
     250             : #ifdef HAVE_W32_SYSTEM
     251             : /* Check whether gpgconf is installed and if so read the gpgconf.ctl
     252             :    file. */
     253             : static void
     254             : check_portable_app (const char *dir)
     255             : {
     256             :   char *fname;
     257             : 
     258             :   fname = xstrconcat (dir, DIRSEP_S "gpgconf.exe", NULL);
     259             :   if (access (fname, F_OK))
     260             :     log_error ("required binary '%s' is not installed\n", fname);
     261             :   else
     262             :     {
     263             :       strcpy (fname + strlen (fname) - 3, "ctl");
     264             :       if (!access (fname, F_OK))
     265             :         {
     266             :           /* gpgconf.ctl file found.  Record this fact.  */
     267             :           w32_portable_app = 1;
     268             :           {
     269             :             unsigned int flags;
     270             :             log_get_prefix (&flags);
     271             :             log_set_prefix (NULL, (flags | GPGRT_LOG_NO_REGISTRY));
     272             :           }
     273             :           /* FIXME: We should read the file to detect special flags
     274             :              and print a warning if we don't understand them  */
     275             :         }
     276             :     }
     277             :   xfree (fname);
     278             : }
     279             : 
     280             : 
     281             : /* Determine the root directory of the gnupg installation on Windows.  */
     282             : static const char *
     283             : w32_rootdir (void)
     284             : {
     285             :   static int got_dir;
     286             :   static char dir[MAX_PATH+5];
     287             : 
     288             :   if (!got_dir)
     289             :     {
     290             :       char *p;
     291             :       int rc;
     292             :       wchar_t wdir [MAX_PATH+5];
     293             : 
     294             :       rc = GetModuleFileNameW (NULL, wdir, MAX_PATH);
     295             :       if (rc && WideCharToMultiByte (CP_UTF8, 0, wdir, -1, dir, MAX_PATH-4,
     296             :                                      NULL, NULL) < 0)
     297             :         rc = 0;
     298             :       if (!rc)
     299             :         {
     300             :           log_debug ("GetModuleFileName failed: %s\n", w32_strerror (-1));
     301             :           *dir = 0;
     302             :         }
     303             :       got_dir = 1;
     304             :       p = strrchr (dir, DIRSEP_C);
     305             :       if (p)
     306             :         {
     307             :           *p = 0;
     308             : 
     309             :           check_portable_app (dir);
     310             : 
     311             :           /* If we are installed below "bin" we strip that and use
     312             :              the top directory instead.  */
     313             :           p = strrchr (dir, DIRSEP_C);
     314             :           if (p && !strcmp (p+1, "bin"))
     315             :             {
     316             :               *p = 0;
     317             :               w32_bin_is_bin = 1;
     318             :             }
     319             :         }
     320             :       if (!p)
     321             :         {
     322             :           log_debug ("bad filename '%s' returned for this process\n", dir);
     323             :           *dir = 0;
     324             :         }
     325             :     }
     326             : 
     327             :   if (*dir)
     328             :     return dir;
     329             :   /* Fallback to the hardwired value. */
     330             :   return GNUPG_LIBEXECDIR;
     331             : }
     332             : 
     333             : static const char *
     334             : w32_commondir (void)
     335             : {
     336             :   static char *dir;
     337             : 
     338             :   if (!dir)
     339             :     {
     340             :       const char *rdir;
     341             :       char path[MAX_PATH];
     342             : 
     343             :       /* Make sure that w32_rootdir has been called so that we are
     344             :          able to check the portable application flag.  The common dir
     345             :          is the identical to the rootdir.  In that case there is also
     346             :          no need to strdup its value.  */
     347             :       rdir = w32_rootdir ();
     348             :       if (w32_portable_app)
     349             :         return rdir;
     350             : 
     351             :       if (w32_shgetfolderpath (NULL, CSIDL_COMMON_APPDATA,
     352             :                                NULL, 0, path) >= 0)
     353             :         {
     354             :           char *tmp = xmalloc (strlen (path) + 4 +1);
     355             :           strcpy (stpcpy (tmp, path), "\\GNU");
     356             :           dir = tmp;
     357             :           /* No auto create of the directory.  Either the installer or
     358             :              the admin has to create these directories.  */
     359             :         }
     360             :       else
     361             :         {
     362             :           /* Ooops: Not defined - probably an old Windows version.
     363             :              Use the installation directory instead.  */
     364             :           dir = xstrdup (rdir);
     365             :         }
     366             :     }
     367             : 
     368             :   return dir;
     369             : }
     370             : #endif /*HAVE_W32_SYSTEM*/
     371             : 
     372             : 
     373             : 
     374             : 
     375             : /* Return the name of the sysconfdir.  This is a static string.  This
     376             :    function is required because under Windows we can't simply compile
     377             :    it in.  */
     378             : const char *
     379           0 : gnupg_sysconfdir (void)
     380             : {
     381             : #ifdef HAVE_W32_SYSTEM
     382             :   static char *name;
     383             : 
     384             :   if (!name)
     385             :     {
     386             :       const char *s1, *s2;
     387             :       s1 = w32_commondir ();
     388             :       s2 = DIRSEP_S "etc" DIRSEP_S "gnupg";
     389             :       name = xmalloc (strlen (s1) + strlen (s2) + 1);
     390             :       strcpy (stpcpy (name, s1), s2);
     391             :     }
     392             :   return name;
     393             : #else /*!HAVE_W32_SYSTEM*/
     394           0 :   return GNUPG_SYSCONFDIR;
     395             : #endif /*!HAVE_W32_SYSTEM*/
     396             : }
     397             : 
     398             : 
     399             : const char *
     400           0 : gnupg_bindir (void)
     401             : {
     402             : #if defined (HAVE_W32CE_SYSTEM)
     403             :   static char *name;
     404             : 
     405             :   if (!name)
     406             :     name = xstrconcat (w32_rootdir (), DIRSEP_S "bin", NULL);
     407             :   return name;
     408             : #elif defined(HAVE_W32_SYSTEM)
     409             :   const char *rdir;
     410             : 
     411             :   rdir = w32_rootdir ();
     412             :   if (w32_bin_is_bin)
     413             :     {
     414             :       static char *name;
     415             : 
     416             :       if (!name)
     417             :         name = xstrconcat (rdir, DIRSEP_S "bin", NULL);
     418             :       return name;
     419             :     }
     420             :   else
     421             :     return rdir;
     422             : #else /*!HAVE_W32_SYSTEM*/
     423           0 :   return GNUPG_BINDIR;
     424             : #endif /*!HAVE_W32_SYSTEM*/
     425             : }
     426             : 
     427             : 
     428             : /* Return the name of the libexec directory.  The name is allocated in
     429             :    a static area on the first use.  This function won't fail. */
     430             : const char *
     431           0 : gnupg_libexecdir (void)
     432             : {
     433             : #ifdef HAVE_W32_SYSTEM
     434             :   return gnupg_bindir ();
     435             : #else /*!HAVE_W32_SYSTEM*/
     436           0 :   return GNUPG_LIBEXECDIR;
     437             : #endif /*!HAVE_W32_SYSTEM*/
     438             : }
     439             : 
     440             : const char *
     441           0 : gnupg_libdir (void)
     442             : {
     443             : #ifdef HAVE_W32_SYSTEM
     444             :   static char *name;
     445             : 
     446             :   if (!name)
     447             :     name = xstrconcat (w32_rootdir (), DIRSEP_S "lib" DIRSEP_S "gnupg", NULL);
     448             :   return name;
     449             : #else /*!HAVE_W32_SYSTEM*/
     450           0 :   return GNUPG_LIBDIR;
     451             : #endif /*!HAVE_W32_SYSTEM*/
     452             : }
     453             : 
     454             : const char *
     455           1 : gnupg_datadir (void)
     456             : {
     457             : #ifdef HAVE_W32_SYSTEM
     458             :   static char *name;
     459             : 
     460             :   if (!name)
     461             :     name = xstrconcat (w32_rootdir (), DIRSEP_S "share" DIRSEP_S "gnupg", NULL);
     462             :   return name;
     463             : #else /*!HAVE_W32_SYSTEM*/
     464           1 :   return GNUPG_DATADIR;
     465             : #endif /*!HAVE_W32_SYSTEM*/
     466             : }
     467             : 
     468             : 
     469             : const char *
     470           0 : gnupg_localedir (void)
     471             : {
     472             : #ifdef HAVE_W32_SYSTEM
     473             :   static char *name;
     474             : 
     475             :   if (!name)
     476             :     name = xstrconcat (w32_rootdir (), DIRSEP_S "share" DIRSEP_S "locale",
     477             :                        NULL);
     478             :   return name;
     479             : #else /*!HAVE_W32_SYSTEM*/
     480           0 :   return LOCALEDIR;
     481             : #endif /*!HAVE_W32_SYSTEM*/
     482             : }
     483             : 
     484             : 
     485             : /* Return the name of the cache directory.  The name is allocated in a
     486             :    static area on the first use.  Windows only: If the directory does
     487             :    not exist it is created.  */
     488             : const char *
     489           0 : gnupg_cachedir (void)
     490             : {
     491             : #ifdef HAVE_W32_SYSTEM
     492             :   static const char *dir;
     493             : 
     494             :   if (!dir)
     495             :     {
     496             :       const char *rdir;
     497             : 
     498             :       rdir = w32_rootdir ();
     499             :       if (w32_portable_app)
     500             :         {
     501             :           dir = xstrconcat (rdir,
     502             :                             DIRSEP_S, "var",
     503             :                             DIRSEP_S, "cache",
     504             :                             DIRSEP_S, "gnupg", NULL);
     505             :         }
     506             :       else
     507             :         {
     508             :           char path[MAX_PATH];
     509             :           const char *s1[] = { "GNU", "cache", "gnupg", NULL };
     510             :           int s1_len;
     511             :           const char **comp;
     512             : 
     513             :           s1_len = 0;
     514             :           for (comp = s1; *comp; comp++)
     515             :             s1_len += 1 + strlen (*comp);
     516             : 
     517             :           if (w32_shgetfolderpath (NULL, CSIDL_LOCAL_APPDATA|CSIDL_FLAG_CREATE,
     518             :                                    NULL, 0, path) >= 0)
     519             :             {
     520             :               char *tmp = xmalloc (strlen (path) + s1_len + 1);
     521             :               char *p;
     522             : 
     523             :               p = stpcpy (tmp, path);
     524             :               for (comp = s1; *comp; comp++)
     525             :                 {
     526             :                   p = stpcpy (p, "\\");
     527             :                   p = stpcpy (p, *comp);
     528             : 
     529             :                   if (access (tmp, F_OK))
     530             :                     w32_try_mkdir (tmp);
     531             :                 }
     532             : 
     533             :               dir = tmp;
     534             :             }
     535             :           else
     536             :             {
     537             :               dir = "c:\\temp\\cache\\gnupg";
     538             : #ifdef HAVE_W32CE_SYSTEM
     539             :               dir += 2;
     540             :               w32_try_mkdir ("\\temp\\cache");
     541             :               w32_try_mkdir ("\\temp\\cache\\gnupg");
     542             : #endif
     543             :             }
     544             :         }
     545             :     }
     546             :   return dir;
     547             : #else /*!HAVE_W32_SYSTEM*/
     548           0 :   return GNUPG_LOCALSTATEDIR "/cache/" PACKAGE_NAME;
     549             : #endif /*!HAVE_W32_SYSTEM*/
     550             : }
     551             : 
     552             : 
     553             : /* Return the system socket name used by DirMngr.  */
     554             : const char *
     555           0 : dirmngr_sys_socket_name (void)
     556             : {
     557             : #ifdef HAVE_W32_SYSTEM
     558             :   static char *name;
     559             : 
     560             :   if (!name)
     561             :     {
     562             :       char *p;
     563             : # ifdef HAVE_W32CE_SYSTEM
     564             :       const char *s1, *s2;
     565             : 
     566             :       s1 = default_homedir ();
     567             : # else
     568             :       char s1buf[MAX_PATH];
     569             :       const char *s1, *s2;
     570             : 
     571             :       s1 = default_homedir ();
     572             :       if (!w32_portable_app)
     573             :         {
     574             :           /* We need something akin CSIDL_COMMON_PROGRAMS, but local
     575             :              (non-roaming).  This is because the file needs to be on
     576             :              the local machine and makes only sense on that machine.
     577             :              CSIDL_WINDOWS seems to be the only location which
     578             :              guarantees that. */
     579             :           if (w32_shgetfolderpath (NULL, CSIDL_WINDOWS, NULL, 0, s1buf) < 0)
     580             :             strcpy (s1buf, "C:\\WINDOWS");
     581             :           s1 = s1buf;
     582             :         }
     583             : # endif
     584             :       s2 = DIRSEP_S DIRMNGR_SOCK_NAME;
     585             :       name = xmalloc (strlen (s1) + strlen (s2) + 1);
     586             :       strcpy (stpcpy (name, s1), s2);
     587             :       for (p=name; *p; p++)
     588             :         if (*p == '/')
     589             :           *p = '\\';
     590             :     }
     591             :   return name;
     592             : #else /*!HAVE_W32_SYSTEM*/
     593           0 :   return GNUPG_LOCALSTATEDIR "/run/" PACKAGE_NAME "/"DIRMNGR_SOCK_NAME;
     594             : #endif /*!HAVE_W32_SYSTEM*/
     595             : }
     596             : 
     597             : 
     598             : /* Return the user socket name used by DirMngr.  If a user specific
     599             :    dirmngr installation is not supported, NULL is returned.  */
     600             : const char *
     601           0 : dirmngr_user_socket_name (void)
     602             : {
     603             :   static char *name;
     604             : 
     605           0 :   if (!name)
     606           0 :     name = make_absfilename (default_homedir (), DIRMNGR_SOCK_NAME, NULL);
     607           0 :   return name;
     608             : }
     609             : 
     610             : 
     611             : /* Return the default pinentry name.  If RESET is true the internal
     612             :    cache is first flushed.  */
     613             : static const char *
     614           0 : get_default_pinentry_name (int reset)
     615             : {
     616             :   static struct {
     617             :     const char *(*rfnc)(void);
     618             :     const char *name;
     619             :   } names[] = {
     620             :     /* The first entry is what we return in case we found no
     621             :        other pinentry.  */
     622             :     { gnupg_bindir, DIRSEP_S "pinentry" EXEEXT_S },
     623             : #ifdef HAVE_W32_SYSTEM
     624             :     /* Try Gpg4win directory (with bin and without.) */
     625             :     { w32_rootdir, "\\..\\Gpg4win\\bin\\pinentry.exe" },
     626             :     { w32_rootdir, "\\..\\Gpg4win\\pinentry.exe" },
     627             :     /* Try old Gpgwin directory.  */
     628             :     { w32_rootdir, "\\..\\GNU\\GnuPG\\pinentry.exe" },
     629             :     /* Try a Pinentry from the common GNU dir.  */
     630             :     { w32_rootdir, "\\..\\GNU\\bin\\pinentry.exe" },
     631             : #endif
     632             :     /* Last chance is a pinentry-basic (which comes with the
     633             :        GnuPG 2.1 Windows installer).  */
     634             :     { gnupg_bindir, DIRSEP_S "pinentry-basic" EXEEXT_S }
     635             :   };
     636             :   static char *name;
     637             : 
     638           0 :   if (reset)
     639             :     {
     640           0 :       xfree (name);
     641           0 :       name = NULL;
     642             :     }
     643             : 
     644           0 :   if (!name)
     645             :     {
     646             :       int i;
     647             : 
     648           0 :       for (i=0; i < DIM(names); i++)
     649             :         {
     650             :           char *name2;
     651             : 
     652           0 :           name2 = xstrconcat (names[i].rfnc (), names[i].name, NULL);
     653           0 :           if (!access (name2, F_OK))
     654             :             {
     655             :               /* Use that pinentry.  */
     656           0 :               xfree (name);
     657           0 :               name = name2;
     658           0 :               break;
     659             :             }
     660           0 :           if (!i) /* Store the first as fallback return.  */
     661           0 :             name = name2;
     662             :           else
     663           0 :             xfree (name2);
     664             :         }
     665             :     }
     666             : 
     667           0 :   return name;
     668             : }
     669             : 
     670             : 
     671             : /* Return the file name of a helper tool.  WHICH is one of the
     672             :    GNUPG_MODULE_NAME_foo constants.  */
     673             : const char *
     674           0 : gnupg_module_name (int which)
     675             : {
     676             : #define X(a,b) do {                                                     \
     677             :     static char *name;                                                  \
     678             :     if (!name)                                                          \
     679             :       name = xstrconcat (gnupg_ ## a (), DIRSEP_S b EXEEXT_S, NULL);    \
     680             :     return name;                                                        \
     681             :   } while (0)
     682             : 
     683           0 :   switch (which)
     684             :     {
     685             :     case GNUPG_MODULE_NAME_AGENT:
     686             : #ifdef GNUPG_DEFAULT_AGENT
     687             :       return GNUPG_DEFAULT_AGENT;
     688             : #else
     689           0 :       X(bindir, "gpg-agent");
     690             : #endif
     691             : 
     692             :     case GNUPG_MODULE_NAME_PINENTRY:
     693             : #ifdef GNUPG_DEFAULT_PINENTRY
     694             :       return GNUPG_DEFAULT_PINENTRY;  /* (Set by a configure option) */
     695             : #else
     696           0 :       return get_default_pinentry_name (0);
     697             : #endif
     698             : 
     699             :     case GNUPG_MODULE_NAME_SCDAEMON:
     700             : #ifdef GNUPG_DEFAULT_SCDAEMON
     701             :       return GNUPG_DEFAULT_SCDAEMON;
     702             : #else
     703           0 :       X(libexecdir, "scdaemon");
     704             : #endif
     705             : 
     706             :     case GNUPG_MODULE_NAME_DIRMNGR:
     707             : #ifdef GNUPG_DEFAULT_DIRMNGR
     708             :       return GNUPG_DEFAULT_DIRMNGR;
     709             : #else
     710           0 :       X(bindir, DIRMNGR_NAME);
     711             : #endif
     712             : 
     713             :     case GNUPG_MODULE_NAME_PROTECT_TOOL:
     714             : #ifdef GNUPG_DEFAULT_PROTECT_TOOL
     715             :       return GNUPG_DEFAULT_PROTECT_TOOL;
     716             : #else
     717           0 :       X(libexecdir, "gpg-protect-tool");
     718             : #endif
     719             : 
     720             :     case GNUPG_MODULE_NAME_DIRMNGR_LDAP:
     721             : #ifdef GNUPG_DEFAULT_DIRMNGR_LDAP
     722             :       return GNUPG_DEFAULT_DIRMNGR_LDAP;
     723             : #else
     724           0 :       X(libexecdir, "dirmngr_ldap");
     725             : #endif
     726             : 
     727             :     case GNUPG_MODULE_NAME_CHECK_PATTERN:
     728           0 :       X(libexecdir, "gpg-check-pattern");
     729             : 
     730             :     case GNUPG_MODULE_NAME_GPGSM:
     731           0 :       X(bindir, "gpgsm");
     732             : 
     733             :     case GNUPG_MODULE_NAME_GPG:
     734           0 :       X(bindir, NAME_OF_INSTALLED_GPG);
     735             : 
     736             :     case GNUPG_MODULE_NAME_CONNECT_AGENT:
     737           0 :       X(bindir, "gpg-connect-agent");
     738             : 
     739             :     case GNUPG_MODULE_NAME_GPGCONF:
     740           0 :       X(bindir, "gpgconf");
     741             : 
     742             :     default:
     743           0 :       BUG ();
     744             :     }
     745             : #undef X
     746             : }
     747             : 
     748             : 
     749             : /* Flush some of the cached module names.  This is for example used by
     750             :    gpg-agent to allow configuring a different pinentry.  */
     751             : void
     752           0 : gnupg_module_name_flush_some (void)
     753             : {
     754           0 :   (void)get_default_pinentry_name (1);
     755           0 : }

Generated by: LCOV version 1.11