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