Imported Upstream version 1.14.0
[platform/upstream/gpgme.git] / src / posix-util.c
1 /* posix-util.c - Utility functions for Posix
2  * Copyright (C) 2001 Werner Koch (dd9jn)
3  * Copyright (C) 2001, 2002, 2004 g10 Code GmbH
4  *
5  * This file is part of GPGME.
6  *
7  * GPGME is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * GPGME is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this program; if not, see <https://gnu.org/licenses/>.
19  * SPDX-License-Identifier: LGPL-2.1-or-later
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <assert.h>
29
30 #include "util.h"
31 #include "sys-util.h"
32 #include "debug.h"
33
34 /* These variables store the malloced name of alternative default
35    binaries.  The are set only once by gpgme_set_global_flag.  */
36 static char *default_gpg_name;
37 static char *default_gpgconf_name;
38
39 /* Set the default name for the gpg binary.  This function may only be
40    called by gpgme_set_global_flag.  Returns 0 on success.  Leading
41    directories are removed from NAME.  */
42 int
43 _gpgme_set_default_gpg_name (const char *name)
44 {
45   const char *s;
46
47   s = strrchr (name, '/');
48   if (s)
49     name = s + 1;
50
51   if (!default_gpg_name)
52     default_gpg_name = strdup (name);
53   return !default_gpg_name;
54 }
55
56 /* Set the default name for the gpgconf binary.  This function may
57    only be called by gpgme_set_global_flag.  Returns 0 on success.
58    Leading directories are removed from NAME.  */
59 int
60 _gpgme_set_default_gpgconf_name (const char *name)
61 {
62   const char *s;
63
64   s = strrchr (name, '/');
65   if (s)
66     name = s + 1;
67
68   if (!default_gpgconf_name)
69     default_gpgconf_name = strdup (name);
70   return !default_gpgconf_name;
71 }
72
73
74 /* Dummy function - see w32-util.c for the actual code.  */
75 int
76 _gpgme_set_override_inst_dir (const char *dir)
77 {
78   (void)dir;
79   return 0;
80 }
81
82 /* Find an executable program in the colon seperated paths. */
83 static char *
84 walk_path_str (const char *path_str, const char *pgm)
85 {
86   const char *path, *s;
87   char *fname, *p;
88
89   fname = malloc (strlen (path_str) + 1 + strlen (pgm) + 1);
90   if (!fname)
91     return NULL;
92
93   path = path_str;
94   for (;;)
95     {
96       for (s=path, p=fname; *s && *s != ':'; s++, p++)
97         *p = *s;
98       if (p != fname && p[-1] != '/')
99         *p++ = '/';
100       strcpy (p, pgm);
101       if (!access (fname, X_OK))
102         return fname;
103       if (!*s)
104         break;
105       path = s + 1;
106     }
107
108   free (fname);
109   return NULL;
110 }
111
112 /* Find an executable program PGM. */
113 static char *
114 find_executable (const char *pgm)
115 {
116   const char *orig_path;
117   char *ret;
118
119 #ifdef FIXED_SEARCH_PATH
120   orig_path = FIXED_SEARCH_PATH;
121 #else
122   orig_path = getenv ("PATH");
123   if (!orig_path)
124     orig_path = "/bin:/usr/bin";
125 #endif
126   ret = walk_path_str (orig_path, pgm);
127
128   if (!ret)
129     {
130       _gpgme_debug (NULL, DEBUG_ENGINE, -1, NULL, NULL, NULL,
131                     "gpgme-walk_path: '%s' not found in '%s'",
132                     pgm, orig_path);
133     }
134 #ifdef __APPLE__
135   /* On apple, especially when started through gpgme-json via
136      the browser interface we should look into some additional
137      fallback paths. */
138   const char *additional_path = "/usr/local/bin:/usr/local/MacGPG2/bin";
139   if (!ret)
140     {
141       ret = walk_path_str (additional_path, pgm);
142     }
143   if (!ret)
144     {
145       _gpgme_debug (NULL, DEBUG_ENGINE, -1, NULL, NULL, NULL,
146                     "gpgme-walk_path: '%s' not found in '%s'",
147                     pgm, additional_path);
148     }
149 #endif
150
151   return ret;
152 }
153
154
155 /* Return the full file name of the GPG binary.  This function is used
156    if gpgconf was not found and thus it can be assumed that gpg2 is
157    not installed.  This function is only called by get_gpgconf_item
158    and may not be called concurrently.  */
159 char *
160 _gpgme_get_gpg_path (void)
161 {
162   return find_executable (default_gpg_name? default_gpg_name : "gpg");
163 }
164
165
166 /* This function is only called by get_gpgconf_item and may not be
167    called concurrently.  */
168 char *
169 _gpgme_get_gpgconf_path (void)
170 {
171   return find_executable (default_gpgconf_name? default_gpgconf_name : "gpgconf");
172 }
173
174 /* See w32-util.c */
175 int
176 _gpgme_get_conf_int (const char *key, int *value)
177 {
178   (void)key;
179   (void)value;
180   return 0;
181 }
182
183 void
184 _gpgme_allow_set_foreground_window (pid_t pid)
185 {
186   (void)pid;
187   /* Not needed.  */
188 }
189
190 /* See w32-util.c */
191 int
192 _gpgme_access (const char *path, int mode)
193 {
194   return access (path, mode);
195 }