/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 | | char *p; |
801 | | int rc; |
802 | | wchar_t wdir [MAX_PATH+5]; |
803 | | char *dir; |
804 | | |
805 | | dir = xmalloc (MAX_PATH+5); |
806 | | rc = GetModuleFileNameW (NULL, wdir, MAX_PATH); |
807 | | if (rc && WideCharToMultiByte (CP_UTF8, 0, wdir, -1, dir, MAX_PATH-4, |
808 | | NULL, NULL) < 0) |
809 | | rc = 0; |
810 | | if (!rc) |
811 | | { |
812 | | log_debug ("GetModuleFileName failed: %s\n", w32_strerror (-1)); |
813 | | *dir = 0; |
814 | | } |
815 | | return dir; |
816 | | } |
817 | | |
818 | | |
819 | | /* Determine the root directory of the gnupg installation on Windows. */ |
820 | | static const char * |
821 | | w32_rootdir (void) |
822 | | { |
823 | | static int got_dir; |
824 | | static char *dir; |
825 | | |
826 | | if (!got_dir) |
827 | | { |
828 | | char *p; |
829 | | |
830 | | dir = w32_myproc_self (); |
831 | | gpgrt_annotate_leaked_object (dir); |
832 | | got_dir = 1; |
833 | | p = strrchr (dir, DIRSEP_C); |
834 | | if (p) |
835 | | { |
836 | | *p = 0; |
837 | | |
838 | | check_portable_app (dir); |
839 | | |
840 | | /* If we are installed below "bin" we strip that and use |
841 | | the top directory instead. */ |
842 | | p = strrchr (dir, DIRSEP_C); |
843 | | if (p && !strcmp (p+1, "bin")) |
844 | | { |
845 | | *p = 0; |
846 | | w32_bin_is_bin = 1; |
847 | | } |
848 | | } |
849 | | if (!p) |
850 | | { |
851 | | log_debug ("bad filename '%s' returned for this process\n", dir); |
852 | | *dir = 0; |
853 | | } |
854 | | } |
855 | | |
856 | | if (*dir) |
857 | | return dir; |
858 | | /* Fallback to the hardwired value. */ |
859 | | return GNUPG_LIBEXECDIR; |
860 | | } |
861 | | #endif /*HAVE_W32_SYSTEM*/ |
862 | | |
863 | | |
864 | | #ifndef HAVE_W32_SYSTEM /* Unix */ |
865 | | /* Return the filename of this process. If WITH_FALLBACK is set the |
866 | | * GNUPG_BUILD_ROOT envvar is considered in the error case. Caller |
867 | | * must release the returned buffer. On error an empty string is |
868 | | * returned. */ |
869 | | static char * |
870 | | unix_myproc_self (int with_fallback) |
871 | 1 | { |
872 | 1 | char *buffer; |
873 | 1 | size_t bufsize = 256-1; |
874 | 1 | int nread; |
875 | 1 | gpg_error_t err; |
876 | 1 | const char *name; |
877 | | |
878 | 1 | for (;;) |
879 | 1 | { |
880 | 1 | buffer = xmalloc (bufsize+1); |
881 | 1 | nread = readlink (MYPROC_SELF_EXE, buffer, bufsize); |
882 | 1 | if (nread < 0) |
883 | 0 | { |
884 | 0 | err = gpg_error_from_syserror (); |
885 | 0 | buffer[0] = 0; |
886 | 0 | if (with_fallback |
887 | 0 | && (name = getenv ("GNUPG_BUILD_ROOT")) && *name == '/') |
888 | 0 | { |
889 | | /* Try a fallback for systems w/o a supported /proc |
890 | | * file system if we are running a regression test. */ |
891 | 0 | log_info ("error reading symlink '%s': %s\n", |
892 | 0 | MYPROC_SELF_EXE, gpg_strerror (err)); |
893 | 0 | xfree (buffer); |
894 | 0 | buffer = xstrconcat (name, "/bin/gpgconf", NULL); |
895 | 0 | log_info ("trying fallback '%s'\n", buffer); |
896 | 0 | } |
897 | 0 | break; |
898 | 0 | } |
899 | 1 | else if (nread < bufsize) |
900 | 1 | { |
901 | 1 | buffer[nread] = 0; |
902 | 1 | break; /* Got it. */ |
903 | 1 | } |
904 | 0 | else if (bufsize >= 4095) |
905 | 0 | { |
906 | 0 | buffer[0] = 0; |
907 | 0 | log_info ("error reading symlink '%s': %s\n", |
908 | 0 | MYPROC_SELF_EXE, "value too large"); |
909 | 0 | break; |
910 | 0 | } |
911 | 0 | xfree (buffer); |
912 | 0 | bufsize += 256; |
913 | 0 | } |
914 | 1 | return buffer; |
915 | 1 | } |
916 | | |
917 | | #ifdef __APPLE__ |
918 | | /* macOS has no /proc file system but an undocumented system call but |
919 | | * with availabale source code. */ |
920 | | static char * |
921 | | macos_myproc_self (int with_fallback) |
922 | | { |
923 | | char *buffer; |
924 | | size_t bufsize = PROC_PIDPATHINFO_SIZE; |
925 | | int nread; |
926 | | gpg_error_t err; |
927 | | const char *name; |
928 | | |
929 | | for (;;) |
930 | | { |
931 | | buffer = xmalloc (bufsize+1); |
932 | | nread = proc_pidpath (getpid (), buffer, bufsize); |
933 | | if (nread > 0) |
934 | | break; /* success */ |
935 | | err = gpg_error_from_syserror (); |
936 | | buffer[0] = 0; /* An empty return string indicates an error. */ |
937 | | if (errno == EOVERFLOW || bufsize >= 4095) |
938 | | { |
939 | | if (with_fallback |
940 | | && (name = getenv ("GNUPG_BUILD_ROOT")) && *name == '/') |
941 | | { |
942 | | log_info ("error from proc_pidpath: %s" |
943 | | " - trying Unix method\n", gpg_strerror (err)); |
944 | | xfree (buffer); |
945 | | return unix_myproc_self (with_fallback); |
946 | | } |
947 | | break; |
948 | | } |
949 | | xfree (buffer); |
950 | | bufsize += 256; |
951 | | } |
952 | | return buffer; |
953 | | } |
954 | | #endif /*__APPLE__*/ |
955 | | |
956 | | |
957 | | /* Determine the root directory of the gnupg installation on Unix. |
958 | | * The standard case is that this function returns NULL so that the |
959 | | * root directory as configured at build time is used. However, it |
960 | | * may return a static string with a different root directory, similar |
961 | | * to what we do on Windows. That second mode is triggered by the |
962 | | * existence of a file gpgconf.ctl installed side-by-side to gpgconf. |
963 | | * This file is parsed for keywords describing the actually to be used |
964 | | * root directory. There is no solid standard on Unix to locate the |
965 | | * binary used to create the process, thus we support this currently |
966 | | * only on Linux and BSD where we can look this info up using the proc |
967 | | * file system. If WANT_SYSCONFDIR is true the optional sysconfdir |
968 | | * entry is returned. */ |
969 | | static const char * |
970 | | unix_rootdir (enum wantdir_values wantdir) |
971 | 1 | { |
972 | 1 | if (!gpgconf_ctl.checked) |
973 | 1 | { |
974 | 1 | char *p; |
975 | 1 | char *buffer; |
976 | | |
977 | | #ifdef __APPLE__ |
978 | | buffer = macos_myproc_self (1/* with_fallback */); |
979 | | #else |
980 | 1 | buffer = unix_myproc_self (1/* with_fallback */); |
981 | 1 | #endif |
982 | 1 | if (!*buffer) |
983 | 0 | { |
984 | 0 | xfree (buffer); |
985 | 0 | gpgconf_ctl.checked = 1; |
986 | 0 | return NULL; /* Error - assume no gpgconf.ctl. */ |
987 | 0 | } |
988 | | |
989 | 1 | p = strrchr (buffer, '/'); |
990 | 1 | if (!p) |
991 | 0 | { |
992 | 0 | xfree (buffer); |
993 | 0 | gpgconf_ctl.checked = 1; |
994 | 0 | return NULL; /* Erroneous /proc - assume no gpgconf.ctl. */ |
995 | 0 | } |
996 | 1 | *p = 0; /* BUFFER has the directory. */ |
997 | 1 | if (!(p = strrchr (buffer, '/'))) |
998 | 0 | { |
999 | | /* Installed in the root which is not a good idea. Assume |
1000 | | * no gpgconf.ctl. */ |
1001 | 0 | xfree (buffer); |
1002 | 0 | gpgconf_ctl.checked = 1; |
1003 | 0 | return NULL; |
1004 | 0 | } |
1005 | | |
1006 | | /* Strip one part and expect the file below a bin dir. */ |
1007 | 1 | *p = 0; |
1008 | 1 | p = xstrconcat (buffer, "/bin/gpgconf.ctl", NULL); |
1009 | 1 | xfree (buffer); |
1010 | 1 | buffer = p; |
1011 | 1 | parse_gpgconf_ctl (buffer); |
1012 | 1 | xfree (buffer); |
1013 | 1 | } |
1014 | | |
1015 | 1 | if (!gpgconf_ctl.valid) |
1016 | 1 | return NULL; /* No valid entries in gpgconf.ctl */ |
1017 | | |
1018 | 0 | switch (wantdir) |
1019 | 0 | { |
1020 | 0 | case WANTDIR_ROOT: return gpgconf_ctl.rootdir; |
1021 | 0 | case WANTDIR_SYSCONF: return gpgconf_ctl.sysconfdir; |
1022 | 0 | case WANTDIR_SOCKET: return gpgconf_ctl.socketdir; |
1023 | 0 | } |
1024 | | |
1025 | 0 | return NULL; /* Not reached. */ |
1026 | 0 | } |
1027 | | #endif /* Unix */ |
1028 | | |
1029 | | |
1030 | | #ifdef HAVE_W32_SYSTEM |
1031 | | static const char * |
1032 | | w32_commondir (void) |
1033 | | { |
1034 | | static char *dir; |
1035 | | |
1036 | | if (!dir) |
1037 | | { |
1038 | | const char *rdir; |
1039 | | char *path; |
1040 | | |
1041 | | /* Make sure that w32_rootdir has been called so that we are |
1042 | | able to check the portable application flag. The common dir |
1043 | | is the identical to the rootdir. In that case there is also |
1044 | | no need to strdup its value. */ |
1045 | | rdir = w32_rootdir (); |
1046 | | if (w32_portable_app) |
1047 | | return rdir; |
1048 | | |
1049 | | path = w32_shgetfolderpath (NULL, CSIDL_COMMON_APPDATA, NULL, 0); |
1050 | | if (path) |
1051 | | { |
1052 | | dir = xstrconcat (path, "\\GNU", NULL); |
1053 | | /* No auto create of the directory. Either the installer or |
1054 | | * the admin has to create these directories. */ |
1055 | | } |
1056 | | else |
1057 | | { |
1058 | | /* Folder not found or defined - probably an old Windows |
1059 | | * version. Use the installation directory instead. */ |
1060 | | dir = xstrdup (rdir); |
1061 | | } |
1062 | | gpgrt_annotate_leaked_object (dir); |
1063 | | } |
1064 | | |
1065 | | return dir; |
1066 | | } |
1067 | | |
1068 | | |
1069 | | /* |
1070 | | * On Windows, isatty returns TRUE when it's NUL device. |
1071 | | * We need additional check. |
1072 | | */ |
1073 | | int |
1074 | | gnupg_isatty (int fd) |
1075 | | { |
1076 | | HANDLE h; |
1077 | | DWORD mode; |
1078 | | |
1079 | | if (!isatty (fd)) |
1080 | | return 0; |
1081 | | |
1082 | | h = (HANDLE)_get_osfhandle (fd); |
1083 | | if (h == INVALID_HANDLE_VALUE) |
1084 | | return 0; |
1085 | | |
1086 | | if (!GetConsoleMode (h, &mode)) |
1087 | | return 0; |
1088 | | |
1089 | | return 1; |
1090 | | } |
1091 | | #endif /*HAVE_W32_SYSTEM*/ |
1092 | | |
1093 | | |
1094 | | /* Change the homedir. Some care must be taken to set this early |
1095 | | * enough because previous calls to gnupg_homedir may else return a |
1096 | | * different string. */ |
1097 | | void |
1098 | | gnupg_set_homedir (const char *newdir) |
1099 | 1 | { |
1100 | 1 | char *tmp = NULL; |
1101 | | |
1102 | 1 | if (!newdir || !*newdir) |
1103 | 0 | newdir = default_homedir (); |
1104 | 1 | else |
1105 | 1 | { |
1106 | 1 | tmp = copy_dir_with_fixup (newdir); |
1107 | 1 | if (tmp) |
1108 | 1 | newdir = tmp; |
1109 | | |
1110 | 1 | if (!is_gnupg_default_homedir (newdir)) |
1111 | 1 | non_default_homedir = 1; |
1112 | 1 | } |
1113 | 1 | xfree (the_gnupg_homedir); |
1114 | 1 | the_gnupg_homedir = make_absfilename (newdir, NULL);; |
1115 | 1 | xfree (tmp); |
1116 | | /* Fixme: Should we use |
1117 | | * gpgrt_annotate_leaked_object(the_gnupg_homedir) |
1118 | | * despite that we may free and allocate a new one in some |
1119 | | * cases? */ |
1120 | 1 | } |
1121 | | |
1122 | | |
1123 | | /* Create the homedir directory only if the supplied directory name is |
1124 | | * the same as the default one. This way we avoid to create arbitrary |
1125 | | * directories when a non-default home directory is used. To cope |
1126 | | * with HOME, we do compare only the suffix if we see that the default |
1127 | | * homedir does start with a tilde. If the mkdir fails the function |
1128 | | * terminates the process. If QUIET is set not diagnostic is printed |
1129 | | * on homedir creation. */ |
1130 | | void |
1131 | | gnupg_maybe_make_homedir (const char *fname, int quiet) |
1132 | 0 | { |
1133 | 0 | const char *defhome = standard_homedir (); |
1134 | |
|
1135 | 0 | if ( |
1136 | | #ifdef HAVE_W32_SYSTEM |
1137 | | ( !compare_filenames (fname, defhome) ) |
1138 | | #else |
1139 | 0 | ( *defhome == '~' |
1140 | 0 | && (strlen(fname) >= strlen (defhome+1) |
1141 | 0 | && !strcmp(fname+strlen(fname)-strlen(defhome+1), defhome+1 ) )) |
1142 | 0 | || (*defhome != '~' && !compare_filenames( fname, defhome ) ) |
1143 | 0 | #endif |
1144 | 0 | ) |
1145 | 0 | { |
1146 | 0 | if (gnupg_mkdir (fname, "-rwx")) |
1147 | 0 | log_fatal ( _("can't create directory '%s': %s\n"), |
1148 | 0 | fname, strerror(errno) ); |
1149 | 0 | else |
1150 | 0 | { |
1151 | 0 | if (!quiet ) |
1152 | 0 | log_info ( _("directory '%s' created\n"), fname ); |
1153 | 0 | create_common_conf (fname); |
1154 | 0 | } |
1155 | 0 | } |
1156 | 0 | } |
1157 | | |
1158 | | |
1159 | | /* Return the file name of this process. On error NULL is returns. |
1160 | | * The caller must free the return value. */ |
1161 | | char * |
1162 | | gnupg_myproc_self (void) |
1163 | 0 | { |
1164 | 0 | char *result; |
1165 | | #ifdef HAVE_W32_SYSTEM |
1166 | | result = w32_myproc_self (); |
1167 | | #elif defined(__APPLE__) |
1168 | | result = macos_myproc_self (0); |
1169 | | #else |
1170 | 0 | result = unix_myproc_self (0); |
1171 | 0 | #endif |
1172 | 0 | if (!*result) |
1173 | 0 | { |
1174 | 0 | xfree (result); |
1175 | 0 | result= NULL; |
1176 | 0 | } |
1177 | 0 | return result; |
1178 | 0 | } |
1179 | | |
1180 | | |
1181 | | |
1182 | | /* Return the homedir. The returned string is valid until another |
1183 | | * gnupg-set-homedir call. This is always an absolute directory name. |
1184 | | * The function replaces the former global var opt.homedir. */ |
1185 | | const char * |
1186 | | gnupg_homedir (void) |
1187 | 4 | { |
1188 | | /* If a homedir has not been set, set it to the default. */ |
1189 | 4 | if (!the_gnupg_homedir) |
1190 | 0 | the_gnupg_homedir = make_absfilename (default_homedir (), NULL); |
1191 | 4 | return the_gnupg_homedir; |
1192 | 4 | } |
1193 | | |
1194 | | |
1195 | | /* Return whether the home dir is the default one. */ |
1196 | | int |
1197 | | gnupg_default_homedir_p (void) |
1198 | 0 | { |
1199 | 0 | return !non_default_homedir; |
1200 | 0 | } |
1201 | | |
1202 | | |
1203 | | /* Return the directory name used by daemons for their current working |
1204 | | * directory. */ |
1205 | | const char * |
1206 | | gnupg_daemon_rootdir (void) |
1207 | 0 | { |
1208 | | #ifdef HAVE_W32_SYSTEM |
1209 | | static char *name; |
1210 | | |
1211 | | if (!name) |
1212 | | { |
1213 | | char path[MAX_PATH]; |
1214 | | size_t n; |
1215 | | |
1216 | | n = GetSystemDirectoryA (path, sizeof path); |
1217 | | if (!n || n >= sizeof path) |
1218 | | name = xstrdup ("/"); /* Error - use the current top dir instead. */ |
1219 | | else |
1220 | | name = xstrdup (path); |
1221 | | gpgrt_annotate_leaked_object (name); |
1222 | | } |
1223 | | |
1224 | | return name; |
1225 | | |
1226 | | #else /*!HAVE_W32_SYSTEM*/ |
1227 | 0 | return "/"; |
1228 | 0 | #endif /*!HAVE_W32_SYSTEM*/ |
1229 | 0 | } |
1230 | | |
1231 | | |
1232 | | /* Helper for gnupg-socketdir. This is a global function, so that |
1233 | | * gpgconf can use it for its --create-socketdir command. If |
1234 | | * SKIP_CHECKS is set permission checks etc. are not done. The |
1235 | | * function always returns a malloced directory name and stores these |
1236 | | * bit flags at R_INFO: |
1237 | | * |
1238 | | * 1 := Internal error, stat failed, out of core, etc. |
1239 | | * 2 := No /run/user directory. |
1240 | | * 4 := Directory not owned by the user, not a directory |
1241 | | * or wrong permissions. |
1242 | | * 8 := Same as 4 but for the subdir. |
1243 | | * 16 := mkdir failed |
1244 | | * 32 := Non default homedir; checking subdir. |
1245 | | * 64 := Subdir does not exist. |
1246 | | * 128 := Using homedir as fallback. |
1247 | | */ |
1248 | | char * |
1249 | | _gnupg_socketdir_internal (int skip_checks, unsigned *r_info) |
1250 | 1 | { |
1251 | | #if defined(HAVE_W32_SYSTEM) |
1252 | | char *name; |
1253 | | gpg_err_code_t ec; |
1254 | | |
1255 | | (void)skip_checks; |
1256 | | |
1257 | | *r_info = 0; |
1258 | | |
1259 | | /* First make sure that non_default_homedir and w32_portable_app can |
1260 | | * be set. */ |
1261 | | gnupg_homedir (); |
1262 | | |
1263 | | if (w32_portable_app) |
1264 | | { |
1265 | | name = xstrconcat (w32_rootdir (), DIRSEP_S, my_gnupg_dirname (), NULL); |
1266 | | } |
1267 | | else |
1268 | | { |
1269 | | char *path; |
1270 | | |
1271 | | path = w32_shgetfolderpath (NULL, |
1272 | | CSIDL_LOCAL_APPDATA|CSIDL_FLAG_CREATE, |
1273 | | NULL, 0); |
1274 | | if (path) |
1275 | | { |
1276 | | name = xstrconcat (path, "\\", my_gnupg_dirname (), NULL); |
1277 | | xfree (path); |
1278 | | if (gnupg_access (name, F_OK)) |
1279 | | gnupg_mkdir (name, "-rwx"); |
1280 | | } |
1281 | | else |
1282 | | { |
1283 | | name = xstrdup (gnupg_homedir ()); |
1284 | | } |
1285 | | } |
1286 | | |
1287 | | /* If a non default homedir is used, we check whether an |
1288 | | * corresponding sub directory below the socket dir is available |
1289 | | * and use that. We hash the non default homedir to keep the new |
1290 | | * subdir short enough. */ |
1291 | | if (non_default_homedir) |
1292 | | { |
1293 | | char sha1buf[20]; |
1294 | | struct stat sb; |
1295 | | char *suffix; |
1296 | | char *p; |
1297 | | |
1298 | | *r_info |= 32; /* Testing subdir. */ |
1299 | | |
1300 | | /* Canonicalize the name to avoid problems with mixed case |
1301 | | * names. Note that we use only 10 bytes of the hash because on |
1302 | | * Windows the account name is also part of the name. */ |
1303 | | suffix = ascii_strlwr (xstrdup (gnupg_homedir ())); |
1304 | | for (p=suffix; *p; p++) |
1305 | | if ( *p == '\\') |
1306 | | *p = '/'; |
1307 | | gcry_md_hash_buffer (GCRY_MD_SHA1, sha1buf, suffix, strlen (suffix)); |
1308 | | xfree (suffix); |
1309 | | suffix = zb32_encode (sha1buf, 8*10); |
1310 | | if (!suffix) |
1311 | | { |
1312 | | *r_info |= 1; /* Out of core etc. */ |
1313 | | goto leave_w32; |
1314 | | } |
1315 | | p = xstrconcat (name, "\\d.", suffix, NULL); |
1316 | | xfree (suffix); |
1317 | | xfree (name); |
1318 | | name = p; |
1319 | | |
1320 | | /* Stat that directory and check constraints. |
1321 | | * The command |
1322 | | * gpgconf --remove-socketdir |
1323 | | * can be used to remove that directory. */ |
1324 | | if (gnupg_stat (name, &sb)) |
1325 | | { |
1326 | | if (errno != ENOENT) |
1327 | | *r_info |= 1; /* stat failed. */ |
1328 | | else if (!skip_checks) |
1329 | | { |
1330 | | /* Try to create the directory and check again. */ |
1331 | | ec = gnupg_mkdir (name, "-rwx"); |
1332 | | if (ec && ec != GPG_ERR_EEXIST) |
1333 | | *r_info |= 16; /* mkdir failed. */ |
1334 | | else if (gnupg_stat (name, &sb)) |
1335 | | { |
1336 | | if (errno != ENOENT) |
1337 | | *r_info |= 1; /* stat failed. */ |
1338 | | else |
1339 | | *r_info |= 64; /* Subdir does not exist. */ |
1340 | | } |
1341 | | else |
1342 | | goto leave_w32; /* Success! */ |
1343 | | } |
1344 | | else |
1345 | | *r_info |= 64; /* Subdir does not exist. */ |
1346 | | if (!skip_checks) |
1347 | | { |
1348 | | xfree (name); |
1349 | | name = NULL; |
1350 | | goto leave_w32; |
1351 | | } |
1352 | | } |
1353 | | } |
1354 | | |
1355 | | leave_w32: |
1356 | | /* If nothing works - fall back to the homedir. */ |
1357 | | if (!name) |
1358 | | { |
1359 | | *r_info |= 128; /* Fallback. */ |
1360 | | name = xstrdup (gnupg_homedir ()); |
1361 | | } |
1362 | | |
1363 | | #elif !defined(HAVE_STAT) |
1364 | | char *name; |
1365 | | |
1366 | | (void)skip_checks; |
1367 | | *r_info = 0; |
1368 | | name = xstrdup (gnupg_homedir ()); |
1369 | | |
1370 | | #else /* Unix and stat(2) available. */ |
1371 | | |
1372 | 1 | static const char * const bases[] = { |
1373 | | #ifdef USE_RUN_GNUPG_USER_SOCKET |
1374 | | "/run/gnupg", |
1375 | | #endif |
1376 | 1 | "/run", |
1377 | | #ifdef USE_RUN_GNUPG_USER_SOCKET |
1378 | | "/var/run/gnupg", |
1379 | | #endif |
1380 | 1 | "/var/run", |
1381 | 1 | NULL |
1382 | 1 | }; |
1383 | 1 | int i; |
1384 | 1 | struct stat sb; |
1385 | 1 | char prefixbuffer[256]; |
1386 | 1 | const char *prefix; |
1387 | 1 | const char *s; |
1388 | 1 | char *name = NULL; |
1389 | 1 | const char *gnupgname = my_gnupg_dirname (); |
1390 | 1 | gpg_err_code_t ec; |
1391 | | |
1392 | 1 | *r_info = 0; |
1393 | | |
1394 | | /* First make sure that non_default_homedir can be set. */ |
1395 | 1 | gnupg_homedir (); |
1396 | | |
1397 | | /* It has been suggested to first check XDG_RUNTIME_DIR envvar. |
1398 | | * However, the specs state that the lifetime of the directory MUST |
1399 | | * be bound to the user being logged in. Now GnuPG may also be run |
1400 | | * as a background process with no (desktop) user logged in. Thus |
1401 | | * we better don't do that. */ |
1402 | | |
1403 | 1 | prefix = unix_rootdir (WANTDIR_SOCKET); |
1404 | 1 | if (!prefix) |
1405 | 1 | { |
1406 | | /* gpgconf.ctl does not specify a directory. Check whether we |
1407 | | * have the usual /run/[gnupg/]user dir. */ |
1408 | 3 | for (i=0; bases[i]; i++) |
1409 | 2 | { |
1410 | 2 | snprintf (prefixbuffer, sizeof prefixbuffer, "%s/user/%u", |
1411 | 2 | bases[i], (unsigned int)getuid ()); |
1412 | 2 | prefix = prefixbuffer; |
1413 | 2 | if (!stat (prefix, &sb) && S_ISDIR(sb.st_mode)) |
1414 | 0 | break; |
1415 | 2 | } |
1416 | 1 | if (!bases[i]) |
1417 | 1 | { |
1418 | 1 | *r_info |= 2; /* No /run/user directory. */ |
1419 | 1 | goto leave; |
1420 | 1 | } |
1421 | | |
1422 | 0 | if (sb.st_uid != getuid ()) |
1423 | 0 | { |
1424 | 0 | *r_info |= 4; /* Not owned by the user. */ |
1425 | 0 | if (!skip_checks) |
1426 | 0 | goto leave; |
1427 | 0 | } |
1428 | | |
1429 | 0 | if (strlen (prefix) + strlen (gnupgname) + 2 >= sizeof prefixbuffer) |
1430 | 0 | { |
1431 | 0 | *r_info |= 1; /* Ooops: Buffer too short to append "/gnupg". */ |
1432 | 0 | goto leave; |
1433 | 0 | } |
1434 | 0 | strcat (prefixbuffer, "/"); |
1435 | 0 | strcat (prefixbuffer, gnupgname); |
1436 | 0 | } |
1437 | | |
1438 | | /* Check whether the gnupg sub directory (or the specified directory) |
1439 | | * has proper permissions. */ |
1440 | 0 | if (stat (prefix, &sb)) |
1441 | 0 | { |
1442 | 0 | if (errno != ENOENT) |
1443 | 0 | { |
1444 | 0 | *r_info |= 1; /* stat failed. */ |
1445 | 0 | goto leave; |
1446 | 0 | } |
1447 | | |
1448 | | /* Try to create the directory and check again. |
1449 | | * Here comes a possible race condition: |
1450 | | * stat(2) above failed by ENOENT, but another process does |
1451 | | * mkdir(2) before we do mkdir(2) |
1452 | | * So, an error with EEXIST should be handled. |
1453 | | */ |
1454 | 0 | ec = gnupg_mkdir (prefix, "-rwx"); |
1455 | 0 | if (ec && ec != GPG_ERR_EEXIST) |
1456 | 0 | { |
1457 | 0 | *r_info |= 16; /* mkdir failed. */ |
1458 | 0 | goto leave; |
1459 | 0 | } |
1460 | 0 | if (stat (prefix, &sb)) |
1461 | 0 | { |
1462 | 0 | *r_info |= 1; /* stat failed. */ |
1463 | 0 | goto leave; |
1464 | 0 | } |
1465 | 0 | } |
1466 | | /* Check that it is a directory, owned by the user, and only the |
1467 | | * user has permissions to use it. */ |
1468 | 0 | if (!S_ISDIR(sb.st_mode) |
1469 | 0 | || sb.st_uid != getuid () |
1470 | 0 | || (sb.st_mode & (S_IRWXG|S_IRWXO))) |
1471 | 0 | { |
1472 | 0 | *r_info |= 4; /* Bad permissions or not a directory. */ |
1473 | 0 | if (!skip_checks) |
1474 | 0 | goto leave; |
1475 | 0 | } |
1476 | | |
1477 | | /* If a non default homedir is used, we check whether an |
1478 | | * corresponding sub directory below the socket dir is available |
1479 | | * and use that. We hash the non default homedir to keep the new |
1480 | | * subdir short enough. */ |
1481 | 0 | if (non_default_homedir) |
1482 | 0 | { |
1483 | 0 | char sha1buf[20]; |
1484 | 0 | char *suffix; |
1485 | |
|
1486 | 0 | *r_info |= 32; /* Testing subdir. */ |
1487 | 0 | s = gnupg_homedir (); |
1488 | 0 | gcry_md_hash_buffer (GCRY_MD_SHA1, sha1buf, s, strlen (s)); |
1489 | 0 | suffix = zb32_encode (sha1buf, 8*15); |
1490 | 0 | if (!suffix) |
1491 | 0 | { |
1492 | 0 | *r_info |= 1; /* Out of core etc. */ |
1493 | 0 | goto leave; |
1494 | 0 | } |
1495 | 0 | name = strconcat (prefix, "/d.", suffix, NULL); |
1496 | 0 | xfree (suffix); |
1497 | 0 | if (!name) |
1498 | 0 | { |
1499 | 0 | *r_info |= 1; /* Out of core etc. */ |
1500 | 0 | goto leave; |
1501 | 0 | } |
1502 | | |
1503 | | /* Stat that directory and check constraints. |
1504 | | * The command |
1505 | | * gpgconf --remove-socketdir |
1506 | | * can be used to remove that directory. */ |
1507 | 0 | if (stat (name, &sb)) |
1508 | 0 | { |
1509 | 0 | if (errno != ENOENT) |
1510 | 0 | *r_info |= 1; /* stat failed. */ |
1511 | 0 | else if (!skip_checks) |
1512 | 0 | { |
1513 | | /* Try to create the directory and check again. */ |
1514 | 0 | ec = gnupg_mkdir (name, "-rwx"); |
1515 | 0 | if (ec && ec != GPG_ERR_EEXIST) |
1516 | 0 | *r_info |= 16; /* mkdir failed. */ |
1517 | 0 | else if (stat (prefix, &sb)) |
1518 | 0 | { |
1519 | 0 | if (errno != ENOENT) |
1520 | 0 | *r_info |= 1; /* stat failed. */ |
1521 | 0 | else |
1522 | 0 | *r_info |= 64; /* Subdir does not exist. */ |
1523 | 0 | } |
1524 | 0 | else |
1525 | 0 | goto leave; /* Success! */ |
1526 | 0 | } |
1527 | 0 | else |
1528 | 0 | *r_info |= 64; /* Subdir does not exist. */ |
1529 | 0 | if (!skip_checks) |
1530 | 0 | { |
1531 | 0 | xfree (name); |
1532 | 0 | name = NULL; |
1533 | 0 | goto leave; |
1534 | 0 | } |
1535 | 0 | } |
1536 | 0 | else if (!S_ISDIR(sb.st_mode) |
1537 | 0 | || sb.st_uid != getuid () |
1538 | 0 | || (sb.st_mode & (S_IRWXG|S_IRWXO))) |
1539 | 0 | { |
1540 | 0 | *r_info |= 8; /* Bad permissions or subdir is not a directory. */ |
1541 | 0 | if (!skip_checks) |
1542 | 0 | { |
1543 | 0 | xfree (name); |
1544 | 0 | name = NULL; |
1545 | 0 | goto leave; |
1546 | 0 | } |
1547 | 0 | } |
1548 | 0 | } |
1549 | 0 | else |
1550 | 0 | name = xstrdup (prefix); |
1551 | | |
1552 | 1 | leave: |
1553 | | /* If nothing works fall back to the homedir. */ |
1554 | 1 | if (!name) |
1555 | 1 | { |
1556 | 1 | *r_info |= 128; /* Fallback. */ |
1557 | 1 | name = xstrdup (gnupg_homedir ()); |
1558 | 1 | } |
1559 | | |
1560 | 1 | #endif /* Unix */ |
1561 | | |
1562 | 1 | return name; |
1563 | 0 | } |
1564 | | |
1565 | | |
1566 | | /* |
1567 | | * Return the name of the socket dir. That is the directory used for |
1568 | | * the IPC local sockets. This is an absolute directory name. |
1569 | | */ |
1570 | | const char * |
1571 | | gnupg_socketdir (void) |
1572 | 162k | { |
1573 | 162k | static char *name; |
1574 | | |
1575 | 162k | if (!name) |
1576 | 1 | { |
1577 | 1 | unsigned int dummy; |
1578 | 1 | name = _gnupg_socketdir_internal (0, &dummy); |
1579 | 1 | gpgrt_annotate_leaked_object (name); |
1580 | 1 | } |
1581 | | |
1582 | 162k | return name; |
1583 | 162k | } |
1584 | | |
1585 | | |
1586 | | /* Return the name of the sysconfdir. This is a static string. This |
1587 | | function is required because under Windows we can't simply compile |
1588 | | it in. */ |
1589 | | const char * |
1590 | | gnupg_sysconfdir (void) |
1591 | 0 | { |
1592 | | #ifdef HAVE_W32_SYSTEM |
1593 | | static char *name; |
1594 | | |
1595 | | if (!name) |
1596 | | { |
1597 | | name = xstrconcat (w32_commondir (), DIRSEP_S, "etc", DIRSEP_S, |
1598 | | my_gnupg_dirname (), NULL); |
1599 | | gpgrt_annotate_leaked_object (name); |
1600 | | } |
1601 | | return name; |
1602 | | #else /*!HAVE_W32_SYSTEM*/ |
1603 | 0 | const char *dir = unix_rootdir (WANTDIR_SYSCONF); |
1604 | 0 | if (dir) |
1605 | 0 | return dir; |
1606 | 0 | else |
1607 | 0 | return GNUPG_SYSCONFDIR; |
1608 | 0 | #endif /*!HAVE_W32_SYSTEM*/ |
1609 | 0 | } |
1610 | | |
1611 | | |
1612 | | const char * |
1613 | | gnupg_bindir (void) |
1614 | 0 | { |
1615 | 0 | static char *name; |
1616 | 0 | const char *rdir; |
1617 | |
|
1618 | | #if defined(HAVE_W32_SYSTEM) |
1619 | | rdir = w32_rootdir (); |
1620 | | if (w32_bin_is_bin) |
1621 | | { |
1622 | | if (!name) |
1623 | | { |
1624 | | name = xstrconcat (rdir, DIRSEP_S "bin", NULL); |
1625 | | gpgrt_annotate_leaked_object (name); |
1626 | | } |
1627 | | return name; |
1628 | | } |
1629 | | else |
1630 | | return rdir; |
1631 | | #else /*!HAVE_W32_SYSTEM*/ |
1632 | 0 | rdir = unix_rootdir (WANTDIR_ROOT); |
1633 | 0 | if (rdir) |
1634 | 0 | { |
1635 | 0 | if (!name) |
1636 | 0 | { |
1637 | 0 | name = xstrconcat (rdir, DIRSEP_S "bin", NULL); |
1638 | 0 | gpgrt_annotate_leaked_object (name); |
1639 | 0 | } |
1640 | 0 | return name; |
1641 | 0 | } |
1642 | 0 | else |
1643 | 0 | return GNUPG_BINDIR; |
1644 | 0 | #endif /*!HAVE_W32_SYSTEM*/ |
1645 | 0 | } |
1646 | | |
1647 | | |
1648 | | /* Return the name of the libexec directory. The name is allocated in |
1649 | | a static area on the first use. This function won't fail. */ |
1650 | | const char * |
1651 | | gnupg_libexecdir (void) |
1652 | 0 | { |
1653 | | #ifdef HAVE_W32_SYSTEM |
1654 | | return gnupg_bindir (); |
1655 | | #else /*!HAVE_W32_SYSTEM*/ |
1656 | 0 | static char *name; |
1657 | 0 | const char *rdir; |
1658 | |
|
1659 | 0 | rdir = unix_rootdir (WANTDIR_ROOT); |
1660 | 0 | if (rdir) |
1661 | 0 | { |
1662 | 0 | if (!name) |
1663 | 0 | { |
1664 | 0 | name = xstrconcat (rdir, DIRSEP_S "libexec", NULL); |
1665 | 0 | gpgrt_annotate_leaked_object (name); |
1666 | 0 | } |
1667 | 0 | return name; |
1668 | 0 | } |
1669 | 0 | else |
1670 | 0 | return GNUPG_LIBEXECDIR; |
1671 | 0 | #endif /*!HAVE_W32_SYSTEM*/ |
1672 | 0 | } |
1673 | | |
1674 | | const char * |
1675 | | gnupg_libdir (void) |
1676 | 0 | { |
1677 | 0 | static char *name; |
1678 | |
|
1679 | | #ifdef HAVE_W32_SYSTEM |
1680 | | if (!name) |
1681 | | { |
1682 | | name = xstrconcat (w32_rootdir (), DIRSEP_S "lib" DIRSEP_S "gnupg", NULL); |
1683 | | gpgrt_annotate_leaked_object (name); |
1684 | | } |
1685 | | return name; |
1686 | | #else /*!HAVE_W32_SYSTEM*/ |
1687 | 0 | const char *rdir; |
1688 | |
|
1689 | 0 | rdir = unix_rootdir (WANTDIR_ROOT); |
1690 | 0 | if (rdir) |
1691 | 0 | { |
1692 | 0 | if (!name) |
1693 | 0 | { |
1694 | 0 | name = xstrconcat (rdir, DIRSEP_S "lib", DIRSEP_S, "gnupg", NULL); |
1695 | 0 | gpgrt_annotate_leaked_object (name); |
1696 | 0 | } |
1697 | 0 | return name; |
1698 | 0 | } |
1699 | 0 | else |
1700 | 0 | return GNUPG_LIBDIR; |
1701 | 0 | #endif /*!HAVE_W32_SYSTEM*/ |
1702 | 0 | } |
1703 | | |
1704 | | const char * |
1705 | | gnupg_datadir (void) |
1706 | 0 | { |
1707 | 0 | static char *name; |
1708 | |
|
1709 | | #ifdef HAVE_W32_SYSTEM |
1710 | | if (!name) |
1711 | | { |
1712 | | name = xstrconcat (w32_rootdir (), DIRSEP_S "share" DIRSEP_S "gnupg", |
1713 | | NULL); |
1714 | | gpgrt_annotate_leaked_object (name); |
1715 | | } |
1716 | | return name; |
1717 | | #else /*!HAVE_W32_SYSTEM*/ |
1718 | 0 | const char *rdir; |
1719 | |
|
1720 | 0 | rdir = unix_rootdir (WANTDIR_ROOT); |
1721 | 0 | if (rdir) |
1722 | 0 | { |
1723 | 0 | if (!name) |
1724 | 0 | { |
1725 | 0 | name = xstrconcat (rdir, DIRSEP_S "share" DIRSEP_S "gnupg", NULL); |
1726 | 0 | gpgrt_annotate_leaked_object (name); |
1727 | 0 | } |
1728 | 0 | return name; |
1729 | 0 | } |
1730 | 0 | else |
1731 | 0 | return GNUPG_DATADIR; |
1732 | 0 | #endif /*!HAVE_W32_SYSTEM*/ |
1733 | 0 | } |
1734 | | |
1735 | | |
1736 | | const char * |
1737 | | gnupg_localedir (void) |
1738 | 0 | { |
1739 | 0 | static char *name; |
1740 | |
|
1741 | | #ifdef HAVE_W32_SYSTEM |
1742 | | if (!name) |
1743 | | { |
1744 | | name = xstrconcat (w32_rootdir (), DIRSEP_S "share" DIRSEP_S "locale", |
1745 | | NULL); |
1746 | | gpgrt_annotate_leaked_object (name); |
1747 | | } |
1748 | | return name; |
1749 | | #else /*!HAVE_W32_SYSTEM*/ |
1750 | 0 | const char *rdir; |
1751 | |
|
1752 | 0 | rdir = unix_rootdir (WANTDIR_ROOT); |
1753 | 0 | if (rdir) |
1754 | 0 | { |
1755 | 0 | if (!name) |
1756 | 0 | { |
1757 | 0 | name = xstrconcat (rdir, DIRSEP_S "share" DIRSEP_S "locale", NULL); |
1758 | 0 | gpgrt_annotate_leaked_object (name); |
1759 | 0 | } |
1760 | 0 | return name; |
1761 | 0 | } |
1762 | 0 | else |
1763 | 0 | return LOCALEDIR; |
1764 | 0 | #endif /*!HAVE_W32_SYSTEM*/ |
1765 | 0 | } |
1766 | | |
1767 | | |
1768 | | /* Return the standard socket name used by gpg-agent. */ |
1769 | | const char * |
1770 | | gpg_agent_socket_name (void) |
1771 | 0 | { |
1772 | 0 | static char *name; |
1773 | |
|
1774 | 0 | if (!name) |
1775 | 0 | { |
1776 | 0 | name = make_filename (gnupg_socketdir (), GPG_AGENT_SOCK_NAME, NULL); |
1777 | 0 | gpgrt_annotate_leaked_object (name); |
1778 | 0 | } |
1779 | 0 | return name; |
1780 | 0 | } |
1781 | | |
1782 | | /* Return the user socket name used by DirMngr. */ |
1783 | | const char * |
1784 | | dirmngr_socket_name (void) |
1785 | 0 | { |
1786 | 0 | static char *name; |
1787 | |
|
1788 | 0 | if (!name) |
1789 | 0 | { |
1790 | 0 | name = make_filename (gnupg_socketdir (), DIRMNGR_SOCK_NAME, NULL); |
1791 | 0 | gpgrt_annotate_leaked_object (name); |
1792 | 0 | } |
1793 | 0 | return name; |
1794 | 0 | } |
1795 | | |
1796 | | |
1797 | | /* Return the user socket name used by Keyboxd. */ |
1798 | | const char * |
1799 | | keyboxd_socket_name (void) |
1800 | 0 | { |
1801 | 0 | static char *name; |
1802 | |
|
1803 | 0 | if (!name) |
1804 | 0 | { |
1805 | 0 | name = make_filename (gnupg_socketdir (), KEYBOXD_SOCK_NAME, NULL); |
1806 | 0 | gpgrt_annotate_leaked_object (name); |
1807 | 0 | } |
1808 | 0 | return name; |
1809 | 0 | } |
1810 | | |
1811 | | |
1812 | | /* Return the default pinentry name. If RESET is true the internal |
1813 | | cache is first flushed. */ |
1814 | | static const char * |
1815 | | get_default_pinentry_name (int reset) |
1816 | 0 | { |
1817 | 0 | static struct { |
1818 | 0 | const char *(*rfnc)(void); |
1819 | 0 | const char *name; |
1820 | 0 | } names[] = { |
1821 | | /* The first entry is what we return in case we found no |
1822 | | other pinentry. */ |
1823 | 0 | { gnupg_bindir, DIRSEP_S "pinentry" EXEEXT_S }, |
1824 | | #ifdef HAVE_W32_SYSTEM |
1825 | | /* Try Gpg4win directory (with bin and without.) */ |
1826 | | { w32_rootdir, "\\..\\Gpg4win\\bin\\pinentry.exe" }, |
1827 | | { w32_rootdir, "\\..\\Gpg4win\\pinentry.exe" }, |
1828 | | /* Try a pinentry in a dir above us */ |
1829 | | { w32_rootdir, "\\..\\bin\\pinentry.exe" }, |
1830 | | /* Try old Gpgwin directory. */ |
1831 | | { w32_rootdir, "\\..\\GNU\\GnuPG\\pinentry.exe" }, |
1832 | | /* Try a Pinentry from the common GNU dir. */ |
1833 | | { w32_rootdir, "\\..\\GNU\\bin\\pinentry.exe" }, |
1834 | | #endif |
1835 | | /* Last chance is a pinentry-basic (which comes with the |
1836 | | GnuPG 2.1 Windows installer). */ |
1837 | 0 | { gnupg_bindir, DIRSEP_S "pinentry-basic" EXEEXT_S } |
1838 | 0 | }; |
1839 | 0 | static char *name; |
1840 | |
|
1841 | 0 | if (reset) |
1842 | 0 | { |
1843 | 0 | xfree (name); |
1844 | 0 | name = NULL; |
1845 | 0 | } |
1846 | |
|
1847 | 0 | if (!name) |
1848 | 0 | { |
1849 | 0 | int i; |
1850 | |
|
1851 | 0 | for (i=0; i < DIM(names); i++) |
1852 | 0 | { |
1853 | 0 | char *name2; |
1854 | |
|
1855 | 0 | name2 = xstrconcat (names[i].rfnc (), names[i].name, NULL); |
1856 | 0 | if (!gnupg_access (name2, F_OK)) |
1857 | 0 | { |
1858 | | /* Use that pinentry. */ |
1859 | 0 | xfree (name); |
1860 | 0 | name = name2; |
1861 | 0 | break; |
1862 | 0 | } |
1863 | 0 | if (!i) /* Store the first as fallback return. */ |
1864 | 0 | name = name2; |
1865 | 0 | else |
1866 | 0 | xfree (name2); |
1867 | 0 | } |
1868 | 0 | if (name) |
1869 | 0 | gpgrt_annotate_leaked_object (name); |
1870 | 0 | } |
1871 | |
|
1872 | 0 | return name; |
1873 | 0 | } |
1874 | | |
1875 | | |
1876 | | /* If set, 'gnupg_module_name' returns modules from that build |
1877 | | * directory. */ |
1878 | | static char *gnupg_build_directory; |
1879 | | |
1880 | | /* For sanity checks. */ |
1881 | | static int gnupg_module_name_called; |
1882 | | |
1883 | | |
1884 | | /* Set NEWDIR as the new build directory. This will make |
1885 | | * 'gnupg_module_name' return modules from that build directory. Must |
1886 | | * be called before any invocation of 'gnupg_module_name', and must |
1887 | | * not be called twice. It can be used by test suites to make sure |
1888 | | * the components from the build directory are used instead of |
1889 | | * potentially outdated installed ones. |
1890 | | * Fixme: It might be better to make use of the newer gpgconf.ctl feature |
1891 | | * for regression testing. |
1892 | | */ |
1893 | | void |
1894 | | gnupg_set_builddir (const char *newdir) |
1895 | 0 | { |
1896 | 0 | log_assert (! gnupg_module_name_called); |
1897 | 0 | log_assert (! gnupg_build_directory); |
1898 | 0 | gnupg_build_directory = xtrystrdup (newdir); |
1899 | 0 | } |
1900 | | |
1901 | | |
1902 | | /* If no build directory has been configured, try to set it from the |
1903 | | * environment. We only do this in development builds to avoid |
1904 | | * increasing the set of influential environment variables and hence |
1905 | | * the attack surface of production builds. */ |
1906 | | static void |
1907 | | gnupg_set_builddir_from_env (void) |
1908 | 0 | { |
1909 | | #if defined(IS_DEVELOPMENT_VERSION) || defined(ENABLE_GNUPG_BUILDDIR_ENVVAR) |
1910 | | if (gnupg_build_directory) |
1911 | | return; |
1912 | | |
1913 | | gnupg_build_directory = getenv ("GNUPG_BUILDDIR"); |
1914 | | #endif |
1915 | 0 | } |
1916 | | |
1917 | | |
1918 | | /* Return the file name of a helper tool. WHICH is one of the |
1919 | | GNUPG_MODULE_NAME_foo constants. */ |
1920 | | const char * |
1921 | | gnupg_module_name (int which) |
1922 | 0 | { |
1923 | 0 | gnupg_set_builddir_from_env (); |
1924 | 0 | gnupg_module_name_called = 1; |
1925 | |
|
1926 | 0 | #define X(a,b,c) do { \ |
1927 | 0 | static char *name; \ |
1928 | 0 | if (!name) { \ |
1929 | 0 | name = gnupg_build_directory \ |
1930 | 0 | ? xstrconcat (gnupg_build_directory, \ |
1931 | 0 | DIRSEP_S b DIRSEP_S c EXEEXT_S, NULL) \ |
1932 | 0 | : xstrconcat (gnupg_ ## a (), DIRSEP_S c EXEEXT_S, NULL); \ |
1933 | 0 | gpgrt_annotate_leaked_object (name); \ |
1934 | 0 | } \ |
1935 | 0 | return name; \ |
1936 | 0 | } while (0) |
1937 | |
|
1938 | 0 | switch (which) |
1939 | 0 | { |
1940 | 0 | case GNUPG_MODULE_NAME_AGENT: |
1941 | | #ifdef GNUPG_DEFAULT_AGENT |
1942 | | return GNUPG_DEFAULT_AGENT; |
1943 | | #else |
1944 | 0 | X(bindir, "agent", "gpg-agent"); |
1945 | 0 | #endif |
1946 | | |
1947 | 0 | case GNUPG_MODULE_NAME_PINENTRY: |
1948 | | #ifdef GNUPG_DEFAULT_PINENTRY |
1949 | | return GNUPG_DEFAULT_PINENTRY; /* (Set by a configure option) */ |
1950 | | #else |
1951 | 0 | return get_default_pinentry_name (0); |
1952 | 0 | #endif |
1953 | | |
1954 | 0 | case GNUPG_MODULE_NAME_SCDAEMON: |
1955 | | #ifdef GNUPG_DEFAULT_SCDAEMON |
1956 | | return GNUPG_DEFAULT_SCDAEMON; |
1957 | | #else |
1958 | 0 | X(libexecdir, "scd", "scdaemon"); |
1959 | 0 | #endif |
1960 | | |
1961 | 0 | case GNUPG_MODULE_NAME_TPM2DAEMON: |
1962 | | #ifdef GNUPG_DEFAULT_TPM2DAEMON |
1963 | | return GNUPG_DEFAULT_TPM2DAEMON; |
1964 | | #else |
1965 | 0 | X(libexecdir, "tpm2d", TPM2DAEMON_NAME); |
1966 | 0 | #endif |
1967 | | |
1968 | 0 | case GNUPG_MODULE_NAME_DIRMNGR: |
1969 | | #ifdef GNUPG_DEFAULT_DIRMNGR |
1970 | | return GNUPG_DEFAULT_DIRMNGR; |
1971 | | #else |
1972 | 0 | X(bindir, "dirmngr", DIRMNGR_NAME); |
1973 | 0 | #endif |
1974 | | |
1975 | 0 | case GNUPG_MODULE_NAME_KEYBOXD: |
1976 | | #ifdef GNUPG_DEFAULT_KEYBOXD |
1977 | | return GNUPG_DEFAULT_KEYBOXD; |
1978 | | #else |
1979 | 0 | X(libexecdir, "kbx", KEYBOXD_NAME); |
1980 | 0 | #endif |
1981 | | |
1982 | 0 | case GNUPG_MODULE_NAME_PROTECT_TOOL: |
1983 | | #ifdef GNUPG_DEFAULT_PROTECT_TOOL |
1984 | | return GNUPG_DEFAULT_PROTECT_TOOL; |
1985 | | #else |
1986 | 0 | X(libexecdir, "agent", "gpg-protect-tool"); |
1987 | 0 | #endif |
1988 | | |
1989 | 0 | case GNUPG_MODULE_NAME_DIRMNGR_LDAP: |
1990 | | #ifdef GNUPG_DEFAULT_DIRMNGR_LDAP |
1991 | | return GNUPG_DEFAULT_DIRMNGR_LDAP; |
1992 | | #else |
1993 | 0 | X(libexecdir, "dirmngr", "dirmngr_ldap"); |
1994 | 0 | #endif |
1995 | | |
1996 | 0 | case GNUPG_MODULE_NAME_CHECK_PATTERN: |
1997 | 0 | X(libexecdir, "tools", "gpg-check-pattern"); |
1998 | | |
1999 | 0 | case GNUPG_MODULE_NAME_GPGSM: |
2000 | 0 | X(bindir, "sm", "gpgsm"); |
2001 | | |
2002 | 0 | case GNUPG_MODULE_NAME_GPG: |
2003 | 0 | X(bindir, "g10", GPG_NAME); |
2004 | | |
2005 | 0 | case GNUPG_MODULE_NAME_GPGV: |
2006 | 0 | X(bindir, "g10", GPG_NAME "v"); |
2007 | | |
2008 | 0 | case GNUPG_MODULE_NAME_CONNECT_AGENT: |
2009 | 0 | X(bindir, "tools", "gpg-connect-agent"); |
2010 | | |
2011 | 0 | case GNUPG_MODULE_NAME_GPGCONF: |
2012 | 0 | X(bindir, "tools", "gpgconf"); |
2013 | | |
2014 | 0 | case GNUPG_MODULE_NAME_CARD: |
2015 | 0 | X(bindir, "tools", "gpg-card"); |
2016 | | |
2017 | 0 | case GNUPG_MODULE_NAME_GPGTAR: |
2018 | 0 | X(bindir, "tools", "gpgtar"); |
2019 | | |
2020 | 0 | default: |
2021 | 0 | BUG (); |
2022 | 0 | } |
2023 | 0 | #undef X |
2024 | 0 | } |
2025 | | |
2026 | | |
2027 | | /* Flush some of the cached module names. This is for example used by |
2028 | | gpg-agent to allow configuring a different pinentry. */ |
2029 | | void |
2030 | | gnupg_module_name_flush_some (void) |
2031 | 0 | { |
2032 | 0 | (void)get_default_pinentry_name (1); |
2033 | 0 | } |