Blame src/dirinfo.c

Packit d7e8d0
/* dirinfo.c - Get directory information
Packit d7e8d0
 * Copyright (C) 2009, 2013 g10 Code GmbH
Packit d7e8d0
 *
Packit d7e8d0
 * This file is part of GPGME.
Packit d7e8d0
 *
Packit d7e8d0
 * GPGME is free software; you can redistribute it and/or modify it
Packit d7e8d0
 * under the terms of the GNU Lesser General Public License as
Packit d7e8d0
 * published by the Free Software Foundation; either version 2.1 of
Packit d7e8d0
 * the License, or (at your option) any later version.
Packit d7e8d0
 *
Packit d7e8d0
 * GPGME is distributed in the hope that it will be useful, but
Packit d7e8d0
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit d7e8d0
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit d7e8d0
 * Lesser General Public License for more details.
Packit d7e8d0
 *
Packit d7e8d0
 * You should have received a copy of the GNU Lesser General Public
Packit Service 30b792
 * License along with this program; if not, see <https://gnu.org/licenses/>.
Packit Service 30b792
 * SPDX-License-Identifier: LGPL-2.1-or-later
Packit d7e8d0
 */
Packit d7e8d0
Packit d7e8d0
#if HAVE_CONFIG_H
Packit d7e8d0
#include <config.h>
Packit d7e8d0
#endif
Packit d7e8d0
Packit d7e8d0
#include <stdlib.h>
Packit d7e8d0
#include <string.h>
Packit d7e8d0
Packit d7e8d0
#include "gpgme.h"
Packit d7e8d0
#include "util.h"
Packit d7e8d0
#include "priv-io.h"
Packit d7e8d0
#include "debug.h"
Packit d7e8d0
#include "sema.h"
Packit d7e8d0
#include "sys-util.h"
Packit d7e8d0
Packit d7e8d0
DEFINE_STATIC_LOCK (dirinfo_lock);
Packit d7e8d0
Packit d7e8d0
/* Constants used internally to select the data.  */
Packit d7e8d0
enum
Packit d7e8d0
  {
Packit d7e8d0
    WANT_HOMEDIR,
Packit d7e8d0
    WANT_SYSCONFDIR,
Packit d7e8d0
    WANT_BINDIR,
Packit d7e8d0
    WANT_LIBEXECDIR,
Packit d7e8d0
    WANT_LIBDIR,
Packit d7e8d0
    WANT_DATADIR,
Packit d7e8d0
    WANT_LOCALEDIR,
Packit d7e8d0
    WANT_AGENT_SOCKET,
Packit d7e8d0
    WANT_AGENT_SSH_SOCKET,
Packit d7e8d0
    WANT_DIRMNGR_SOCKET,
Packit d7e8d0
    WANT_UISRV_SOCKET,
Packit d7e8d0
    WANT_GPGCONF_NAME,
Packit d7e8d0
    WANT_GPG_NAME,
Packit d7e8d0
    WANT_GPGSM_NAME,
Packit d7e8d0
    WANT_G13_NAME,
Packit d7e8d0
    WANT_GPG_WKS_CLIENT_NAME,
Packit d7e8d0
    WANT_GPG_ONE_MODE
Packit d7e8d0
  };
Packit d7e8d0
Packit d7e8d0
/* Values retrieved via gpgconf and cached here.  */
Packit d7e8d0
static struct {
Packit d7e8d0
  int  valid;         /* Cached information is valid.  */
Packit d7e8d0
  int  disable_gpgconf;
Packit d7e8d0
  char *homedir;
Packit d7e8d0
  char *sysconfdir;
Packit d7e8d0
  char *bindir;
Packit d7e8d0
  char *libexecdir;
Packit d7e8d0
  char *libdir;
Packit d7e8d0
  char *datadir;
Packit d7e8d0
  char *localedir;
Packit d7e8d0
  char *agent_socket;
Packit d7e8d0
  char *agent_ssh_socket;
Packit d7e8d0
  char *dirmngr_socket;
Packit d7e8d0
  char *uisrv_socket;
Packit d7e8d0
  char *gpgconf_name;
Packit d7e8d0
  char *gpg_name;
Packit d7e8d0
  char *gpgsm_name;
Packit d7e8d0
  char *g13_name;
Packit d7e8d0
  char *gpg_wks_client_name;
Packit d7e8d0
  int  gpg_one_mode;  /* System is in gpg1 mode.  */
Packit d7e8d0
} dirinfo;
Packit d7e8d0
Packit d7e8d0
Packit d7e8d0

Packit d7e8d0
/* Helper function to be used only by gpgme_set_global_flag.  */
Packit d7e8d0
void
Packit d7e8d0
_gpgme_dirinfo_disable_gpgconf (void)
Packit d7e8d0
{
Packit d7e8d0
  dirinfo.disable_gpgconf = 1;
Packit d7e8d0
}
Packit d7e8d0
Packit d7e8d0
Packit d7e8d0
/* Return the length of the directory part including the trailing
Packit d7e8d0
 * slash of NAME.  */
Packit d7e8d0
static size_t
Packit d7e8d0
dirname_len (const char *name)
Packit d7e8d0
{
Packit d7e8d0
  return _gpgme_get_basename (name) - name;
Packit d7e8d0
}
Packit d7e8d0
Packit d7e8d0
Packit d7e8d0
/* Parse the output of "gpgconf --list-dirs".  This function expects
Packit d7e8d0
   that DIRINFO_LOCK is held by the caller.  If COMPONENTS is set, the
Packit d7e8d0
   output of --list-components is expected. */
Packit d7e8d0
static void
Packit d7e8d0
parse_output (char *line, int components)
Packit d7e8d0
{
Packit d7e8d0
  char *value, *p;
Packit d7e8d0
  size_t n;
Packit d7e8d0
Packit d7e8d0
  value = strchr (line, ':');
Packit d7e8d0
  if (!value)
Packit d7e8d0
    return;
Packit d7e8d0
  *value++ = 0;
Packit d7e8d0
  if (components)
Packit d7e8d0
    {
Packit d7e8d0
      /* Skip the second field.  */
Packit d7e8d0
      value = strchr (value, ':');
Packit d7e8d0
      if (!value)
Packit d7e8d0
        return;
Packit d7e8d0
      *value++ = 0;
Packit d7e8d0
    }
Packit d7e8d0
  p = strchr (value, ':');
Packit d7e8d0
  if (p)
Packit d7e8d0
    *p = 0;
Packit d7e8d0
  if (_gpgme_decode_percent_string (value, &value, strlen (value)+1, 0))
Packit d7e8d0
    return;
Packit d7e8d0
  if (!*value)
Packit d7e8d0
    return;
Packit d7e8d0
Packit d7e8d0
  if (components)
Packit d7e8d0
    {
Packit d7e8d0
      if (!strcmp (line, "gpg") && !dirinfo.gpg_name)
Packit d7e8d0
        dirinfo.gpg_name = strdup (value);
Packit d7e8d0
      else if (!strcmp (line, "gpgsm") && !dirinfo.gpgsm_name)
Packit d7e8d0
        dirinfo.gpgsm_name = strdup (value);
Packit d7e8d0
      else if (!strcmp (line, "g13") && !dirinfo.g13_name)
Packit d7e8d0
        dirinfo.g13_name = strdup (value);
Packit d7e8d0
    }
Packit d7e8d0
  else
Packit d7e8d0
    {
Packit d7e8d0
      if (!strcmp (line, "homedir") && !dirinfo.homedir)
Packit d7e8d0
        dirinfo.homedir = strdup (value);
Packit d7e8d0
      else if (!strcmp (line, "sysconfdir") && !dirinfo.sysconfdir)
Packit d7e8d0
        dirinfo.sysconfdir = strdup (value);
Packit d7e8d0
      else if (!strcmp (line, "bindir") && !dirinfo.bindir)
Packit d7e8d0
        dirinfo.bindir = strdup (value);
Packit d7e8d0
      else if (!strcmp (line, "libexecdir") && !dirinfo.libexecdir)
Packit d7e8d0
        dirinfo.libexecdir = strdup (value);
Packit d7e8d0
      else if (!strcmp (line, "libdir") && !dirinfo.libdir)
Packit d7e8d0
        dirinfo.libdir = strdup (value);
Packit d7e8d0
      else if (!strcmp (line, "datadir") && !dirinfo.datadir)
Packit d7e8d0
        dirinfo.datadir = strdup (value);
Packit d7e8d0
      else if (!strcmp (line, "localedir") && !dirinfo.localedir)
Packit d7e8d0
        dirinfo.localedir = strdup (value);
Packit d7e8d0
      else if (!strcmp (line, "agent-socket") && !dirinfo.agent_socket)
Packit d7e8d0
        {
Packit d7e8d0
          const char name[] = "S.uiserver";
Packit d7e8d0
          char *buffer;
Packit d7e8d0
Packit d7e8d0
          dirinfo.agent_socket = strdup (value);
Packit d7e8d0
          if (dirinfo.agent_socket)
Packit d7e8d0
            {
Packit d7e8d0
              n = dirname_len (dirinfo.agent_socket);
Packit d7e8d0
              buffer = malloc (n + strlen (name) + 1);
Packit d7e8d0
              if (buffer)
Packit d7e8d0
                {
Packit d7e8d0
                  strncpy (buffer, dirinfo.agent_socket, n);
Packit d7e8d0
                  strcpy (buffer + n, name);
Packit d7e8d0
                  dirinfo.uisrv_socket = buffer;
Packit d7e8d0
                }
Packit d7e8d0
            }
Packit d7e8d0
        }
Packit d7e8d0
      else if (!strcmp (line, "dirmngr-socket") && !dirinfo.dirmngr_socket)
Packit d7e8d0
        dirinfo.dirmngr_socket = strdup (value);
Packit d7e8d0
      else if (!strcmp (line, "agent-ssh-socket") && !dirinfo.agent_ssh_socket)
Packit d7e8d0
        dirinfo.agent_ssh_socket = strdup (value);
Packit d7e8d0
    }
Packit d7e8d0
}
Packit d7e8d0
Packit d7e8d0
Packit d7e8d0
/* Read the directory information from gpgconf.  This function expects
Packit d7e8d0
   that DIRINFO_LOCK is held by the caller.  PGNAME is the name of the
Packit d7e8d0
   gpgconf binary. If COMPONENTS is set, not the directories bit the
Packit d7e8d0
   name of the componeNts are read. */
Packit d7e8d0
static void
Packit d7e8d0
read_gpgconf_dirs (const char *pgmname, int components)
Packit d7e8d0
{
Packit d7e8d0
  char linebuf[1024] = {0};
Packit d7e8d0
  int linelen = 0;
Packit d7e8d0
  char * argv[3];
Packit d7e8d0
  int rp[2];
Packit d7e8d0
  struct spawn_fd_item_s cfd[] = { {-1, 1 /* STDOUT_FILENO */, -1, 0},
Packit d7e8d0
				   {-1, -1} };
Packit d7e8d0
  int status;
Packit d7e8d0
  int nread;
Packit d7e8d0
  char *mark = NULL;
Packit d7e8d0
Packit d7e8d0
  argv[0] = (char *)pgmname;
Packit d7e8d0
  argv[1] = (char*)(components? "--list-components" : "--list-dirs");
Packit d7e8d0
  argv[2] = NULL;
Packit d7e8d0
Packit d7e8d0
  if (_gpgme_io_pipe (rp, 1) < 0)
Packit d7e8d0
    return;
Packit d7e8d0
Packit d7e8d0
  cfd[0].fd = rp[1];
Packit d7e8d0
Packit d7e8d0
  status = _gpgme_io_spawn (pgmname, argv, IOSPAWN_FLAG_DETACHED,
Packit d7e8d0
                            cfd, NULL, NULL, NULL);
Packit d7e8d0
  if (status < 0)
Packit d7e8d0
    {
Packit d7e8d0
      _gpgme_io_close (rp[0]);
Packit d7e8d0
      _gpgme_io_close (rp[1]);
Packit d7e8d0
      return;
Packit d7e8d0
    }
Packit d7e8d0
Packit d7e8d0
  do
Packit d7e8d0
    {
Packit d7e8d0
      nread = _gpgme_io_read (rp[0],
Packit d7e8d0
                              linebuf + linelen,
Packit d7e8d0
                              sizeof linebuf - linelen - 1);
Packit d7e8d0
      if (nread > 0)
Packit d7e8d0
	{
Packit d7e8d0
          char *line;
Packit d7e8d0
          const char *lastmark = NULL;
Packit d7e8d0
          size_t nused;
Packit d7e8d0
Packit d7e8d0
	  linelen += nread;
Packit d7e8d0
	  linebuf[linelen] = '\0';
Packit d7e8d0
Packit d7e8d0
	  for (line=linebuf; (mark = strchr (line, '\n')); line = mark+1 )
Packit d7e8d0
	    {
Packit d7e8d0
              lastmark = mark;
Packit d7e8d0
	      if (mark > line && mark[-1] == '\r')
Packit d7e8d0
		mark[-1] = '\0';
Packit d7e8d0
              else
Packit d7e8d0
                mark[0] = '\0';
Packit d7e8d0
Packit d7e8d0
              parse_output (line, components);
Packit d7e8d0
	    }
Packit d7e8d0
Packit d7e8d0
          nused = lastmark? (lastmark + 1 - linebuf) : 0;
Packit d7e8d0
          memmove (linebuf, linebuf + nused, linelen - nused);
Packit d7e8d0
          linelen -= nused;
Packit d7e8d0
	}
Packit d7e8d0
    }
Packit d7e8d0
  while (nread > 0 && linelen < sizeof linebuf - 1);
Packit d7e8d0
Packit d7e8d0
  _gpgme_io_close (rp[0]);
Packit d7e8d0
}
Packit d7e8d0
Packit d7e8d0
Packit d7e8d0
static const char *
Packit d7e8d0
get_gpgconf_item (int what)
Packit d7e8d0
{
Packit d7e8d0
  const char *result = NULL;
Packit d7e8d0
Packit d7e8d0
  LOCK (dirinfo_lock);
Packit d7e8d0
  if (!dirinfo.valid)
Packit d7e8d0
    {
Packit d7e8d0
      char *pgmname;
Packit d7e8d0
Packit d7e8d0
      pgmname = dirinfo.disable_gpgconf? NULL : _gpgme_get_gpgconf_path ();
Packit Service 30b792
      if (pgmname && _gpgme_access (pgmname, F_OK))
Packit d7e8d0
        {
Packit Service 30b792
          _gpgme_debug (NULL, DEBUG_INIT, -1, NULL, NULL, NULL,
Packit d7e8d0
                        "gpgme-dinfo: gpgconf='%s' [not installed]\n", pgmname);
Packit d7e8d0
          free (pgmname);
Packit d7e8d0
          pgmname = NULL; /* Not available.  */
Packit d7e8d0
        }
Packit d7e8d0
      else
Packit Service 30b792
        _gpgme_debug (NULL, DEBUG_INIT, -1, NULL, NULL, NULL,
Packit Service 30b792
                      "gpgme-dinfo: gpgconf='%s'\n",
Packit d7e8d0
                      pgmname? pgmname : "[null]");
Packit d7e8d0
      if (!pgmname)
Packit d7e8d0
        {
Packit d7e8d0
          /* Probably gpgconf is not installed.  Assume we are using
Packit d7e8d0
             GnuPG-1.  */
Packit d7e8d0
          dirinfo.gpg_one_mode = 1;
Packit d7e8d0
          pgmname = _gpgme_get_gpg_path ();
Packit d7e8d0
          if (pgmname)
Packit d7e8d0
            dirinfo.gpg_name = pgmname;
Packit d7e8d0
        }
Packit d7e8d0
      else
Packit d7e8d0
        {
Packit d7e8d0
          dirinfo.gpg_one_mode = 0;
Packit d7e8d0
          read_gpgconf_dirs (pgmname, 0);
Packit d7e8d0
          read_gpgconf_dirs (pgmname, 1);
Packit d7e8d0
          dirinfo.gpgconf_name = pgmname;
Packit d7e8d0
        }
Packit d7e8d0
      /* Even if the reading of the directories failed (e.g. due to an
Packit d7e8d0
         too old version gpgconf or no gpgconf at all), we need to
Packit d7e8d0
         mark the entries as valid so that we won't try over and over
Packit d7e8d0
         to read them.  Note further that we are not able to change
Packit d7e8d0
         the read values later because they are practically statically
Packit d7e8d0
         allocated.  */
Packit d7e8d0
      dirinfo.valid = 1;
Packit d7e8d0
      if (dirinfo.gpg_name)
Packit Service 30b792
        _gpgme_debug (NULL, DEBUG_INIT, -1, NULL, NULL, NULL,
Packit Service 30b792
                      "gpgme-dinfo:     gpg='%s'\n",
Packit d7e8d0
                      dirinfo.gpg_name);
Packit d7e8d0
      if (dirinfo.g13_name)
Packit Service 30b792
        _gpgme_debug (NULL, DEBUG_INIT, -1, NULL, NULL, NULL,
Packit Service 30b792
                      "gpgme-dinfo:     g13='%s'\n",
Packit d7e8d0
                      dirinfo.g13_name);
Packit d7e8d0
      if (dirinfo.gpgsm_name)
Packit Service 30b792
        _gpgme_debug (NULL, DEBUG_INIT, -1, NULL, NULL, NULL,
Packit Service 30b792
                      "gpgme-dinfo:   gpgsm='%s'\n",
Packit d7e8d0
                      dirinfo.gpgsm_name);
Packit d7e8d0
      if (dirinfo.homedir)
Packit Service 30b792
        _gpgme_debug (NULL, DEBUG_INIT, -1, NULL, NULL, NULL,
Packit Service 30b792
                      "gpgme-dinfo: homedir='%s'\n",
Packit d7e8d0
                      dirinfo.homedir);
Packit d7e8d0
      if (dirinfo.agent_socket)
Packit Service 30b792
        _gpgme_debug (NULL,DEBUG_INIT, -1, NULL, NULL, NULL,
Packit Service 30b792
                      "gpgme-dinfo:   agent='%s'\n",
Packit d7e8d0
                      dirinfo.agent_socket);
Packit d7e8d0
      if (dirinfo.agent_ssh_socket)
Packit Service 30b792
        _gpgme_debug (NULL, DEBUG_INIT, -1, NULL, NULL, NULL,
Packit Service 30b792
                      "gpgme-dinfo:     ssh='%s'\n",
Packit d7e8d0
                      dirinfo.agent_ssh_socket);
Packit d7e8d0
      if (dirinfo.dirmngr_socket)
Packit Service 30b792
        _gpgme_debug (NULL, DEBUG_INIT, -1, NULL, NULL, NULL,
Packit Service 30b792
                      "gpgme-dinfo: dirmngr='%s'\n",
Packit d7e8d0
                      dirinfo.dirmngr_socket);
Packit d7e8d0
      if (dirinfo.uisrv_socket)
Packit Service 30b792
        _gpgme_debug (NULL, DEBUG_INIT, -1, NULL, NULL, NULL,
Packit Service 30b792
                      "gpgme-dinfo:   uisrv='%s'\n",
Packit d7e8d0
                      dirinfo.uisrv_socket);
Packit d7e8d0
    }
Packit d7e8d0
  switch (what)
Packit d7e8d0
    {
Packit d7e8d0
    case WANT_HOMEDIR:    result = dirinfo.homedir; break;
Packit d7e8d0
    case WANT_SYSCONFDIR: result = dirinfo.sysconfdir; break;
Packit d7e8d0
    case WANT_BINDIR:     result = dirinfo.bindir; break;
Packit d7e8d0
    case WANT_LIBEXECDIR: result = dirinfo.libexecdir; break;
Packit d7e8d0
    case WANT_LIBDIR:     result = dirinfo.libdir; break;
Packit d7e8d0
    case WANT_DATADIR:    result = dirinfo.datadir; break;
Packit d7e8d0
    case WANT_LOCALEDIR:  result = dirinfo.localedir; break;
Packit d7e8d0
    case WANT_AGENT_SOCKET: result = dirinfo.agent_socket; break;
Packit d7e8d0
    case WANT_AGENT_SSH_SOCKET: result = dirinfo.agent_ssh_socket; break;
Packit d7e8d0
    case WANT_DIRMNGR_SOCKET: result = dirinfo.dirmngr_socket; break;
Packit d7e8d0
    case WANT_GPGCONF_NAME: result = dirinfo.gpgconf_name; break;
Packit d7e8d0
    case WANT_GPG_NAME:   result = dirinfo.gpg_name; break;
Packit d7e8d0
    case WANT_GPGSM_NAME: result = dirinfo.gpgsm_name; break;
Packit d7e8d0
    case WANT_G13_NAME:   result = dirinfo.g13_name; break;
Packit d7e8d0
    case WANT_UISRV_SOCKET:  result = dirinfo.uisrv_socket; break;
Packit d7e8d0
    case WANT_GPG_ONE_MODE: result = dirinfo.gpg_one_mode? "1":NULL; break;
Packit d7e8d0
    case WANT_GPG_WKS_CLIENT_NAME:
Packit d7e8d0
      if (!dirinfo.gpg_wks_client_name && dirinfo.libexecdir)
Packit d7e8d0
        dirinfo.gpg_wks_client_name = _gpgme_strconcat (dirinfo.libexecdir,
Packit d7e8d0
                                                        "/",
Packit d7e8d0
                                                        "gpg-wks-client",
Packit d7e8d0
                                                        NULL);
Packit d7e8d0
      result = dirinfo.gpg_wks_client_name;
Packit d7e8d0
      break;
Packit d7e8d0
    }
Packit d7e8d0
  UNLOCK (dirinfo_lock);
Packit d7e8d0
  return result;
Packit d7e8d0
}
Packit d7e8d0
Packit d7e8d0
Packit d7e8d0
/* Return the default home directory.   Returns NULL if not known.  */
Packit d7e8d0
const char *
Packit d7e8d0
_gpgme_get_default_homedir (void)
Packit d7e8d0
{
Packit d7e8d0
  return get_gpgconf_item (WANT_HOMEDIR);
Packit d7e8d0
}
Packit d7e8d0
Packit d7e8d0
/* Return the default gpg-agent socket name.  Returns NULL if not known.  */
Packit d7e8d0
const char *
Packit d7e8d0
_gpgme_get_default_agent_socket (void)
Packit d7e8d0
{
Packit d7e8d0
  return get_gpgconf_item (WANT_AGENT_SOCKET);
Packit d7e8d0
}
Packit d7e8d0
Packit d7e8d0
/* Return the default gpg file name.  Returns NULL if not known.  */
Packit d7e8d0
const char *
Packit d7e8d0
_gpgme_get_default_gpg_name (void)
Packit d7e8d0
{
Packit d7e8d0
  return get_gpgconf_item (WANT_GPG_NAME);
Packit d7e8d0
}
Packit d7e8d0
Packit d7e8d0
/* Return the default gpgsm file name.  Returns NULL if not known.  */
Packit d7e8d0
const char *
Packit d7e8d0
_gpgme_get_default_gpgsm_name (void)
Packit d7e8d0
{
Packit d7e8d0
  return get_gpgconf_item (WANT_GPGSM_NAME);
Packit d7e8d0
}
Packit d7e8d0
Packit d7e8d0
/* Return the default g13 file name.  Returns NULL if not known.  */
Packit d7e8d0
const char *
Packit d7e8d0
_gpgme_get_default_g13_name (void)
Packit d7e8d0
{
Packit d7e8d0
  return get_gpgconf_item (WANT_G13_NAME);
Packit d7e8d0
}
Packit d7e8d0
Packit d7e8d0
/* Return the default gpgconf file name.  Returns NULL if not known.  */
Packit d7e8d0
const char *
Packit d7e8d0
_gpgme_get_default_gpgconf_name (void)
Packit d7e8d0
{
Packit d7e8d0
  return get_gpgconf_item (WANT_GPGCONF_NAME);
Packit d7e8d0
}
Packit d7e8d0
Packit d7e8d0
/* Return the default UI-server socket name.  Returns NULL if not
Packit d7e8d0
   known.  */
Packit d7e8d0
const char *
Packit d7e8d0
_gpgme_get_default_uisrv_socket (void)
Packit d7e8d0
{
Packit d7e8d0
  return get_gpgconf_item (WANT_UISRV_SOCKET);
Packit d7e8d0
}
Packit d7e8d0
Packit d7e8d0
/* Return true if we are in GnuPG-1 mode - ie. no gpgconf and agent
Packit d7e8d0
   being optional.  */
Packit d7e8d0
int
Packit d7e8d0
_gpgme_in_gpg_one_mode (void)
Packit d7e8d0
{
Packit d7e8d0
  return !!get_gpgconf_item (WANT_GPG_ONE_MODE);
Packit d7e8d0
}
Packit d7e8d0
Packit d7e8d0
Packit d7e8d0
Packit d7e8d0
/* Helper function to return the basename of the passed filename.  */
Packit d7e8d0
const char *
Packit d7e8d0
_gpgme_get_basename (const char *name)
Packit d7e8d0
{
Packit d7e8d0
  const char *s;
Packit d7e8d0
Packit d7e8d0
  if (!name || !*name)
Packit d7e8d0
    return name;
Packit d7e8d0
  for (s = name + strlen (name) -1; s >= name; s--)
Packit d7e8d0
    if (*s == '/'
Packit d7e8d0
#ifdef HAVE_W32_SYSTEM
Packit d7e8d0
        || *s == '\\' || *s == ':'
Packit d7e8d0
#endif
Packit d7e8d0
        )
Packit d7e8d0
      return s+1;
Packit d7e8d0
  return name;
Packit d7e8d0
}
Packit d7e8d0
Packit d7e8d0
Packit d7e8d0
/* Return default values for various directories and file names.  */
Packit d7e8d0
const char *
Packit d7e8d0
gpgme_get_dirinfo (const char *what)
Packit d7e8d0
{
Packit d7e8d0
  if (!what)
Packit d7e8d0
    return NULL;
Packit d7e8d0
  else if (!strcmp (what, "homedir"))
Packit d7e8d0
    return get_gpgconf_item (WANT_HOMEDIR);
Packit d7e8d0
  else if (!strcmp (what, "agent-socket"))
Packit d7e8d0
    return get_gpgconf_item (WANT_AGENT_SOCKET);
Packit d7e8d0
  else if (!strcmp (what, "uiserver-socket"))
Packit d7e8d0
    return get_gpgconf_item (WANT_UISRV_SOCKET);
Packit d7e8d0
  else if (!strcmp (what, "gpgconf-name"))
Packit d7e8d0
    return get_gpgconf_item (WANT_GPGCONF_NAME);
Packit d7e8d0
  else if (!strcmp (what, "gpg-name"))
Packit d7e8d0
    return get_gpgconf_item (WANT_GPG_NAME);
Packit d7e8d0
  else if (!strcmp (what, "gpgsm-name"))
Packit d7e8d0
    return get_gpgconf_item (WANT_GPGSM_NAME);
Packit d7e8d0
  else if (!strcmp (what, "g13-name"))
Packit d7e8d0
    return get_gpgconf_item (WANT_G13_NAME);
Packit d7e8d0
  else if (!strcmp (what, "gpg-wks-client-name"))
Packit d7e8d0
    return get_gpgconf_item (WANT_GPG_WKS_CLIENT_NAME);
Packit d7e8d0
  else if (!strcmp (what, "agent-ssh-socket"))
Packit d7e8d0
    return get_gpgconf_item (WANT_AGENT_SSH_SOCKET);
Packit d7e8d0
  else if (!strcmp (what, "dirmngr-socket"))
Packit d7e8d0
    return get_gpgconf_item (WANT_DIRMNGR_SOCKET);
Packit d7e8d0
  else if (!strcmp (what, "sysconfdir"))
Packit d7e8d0
    return get_gpgconf_item (WANT_SYSCONFDIR);
Packit d7e8d0
  else if (!strcmp (what, "bindir"))
Packit d7e8d0
    return get_gpgconf_item (WANT_BINDIR);
Packit d7e8d0
  else if (!strcmp (what, "libexecdir"))
Packit d7e8d0
    return get_gpgconf_item (WANT_LIBEXECDIR);
Packit d7e8d0
  else if (!strcmp (what, "libdir"))
Packit d7e8d0
    return get_gpgconf_item (WANT_LIBDIR);
Packit d7e8d0
  else if (!strcmp (what, "datadir"))
Packit d7e8d0
    return get_gpgconf_item (WANT_DATADIR);
Packit d7e8d0
  else if (!strcmp (what, "localedir"))
Packit d7e8d0
    return get_gpgconf_item (WANT_LOCALEDIR);
Packit d7e8d0
  else
Packit d7e8d0
    return NULL;
Packit d7e8d0
}