2 This file is part of GNUnet.
3 Copyright (C) 2006-2018 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file src/util/os_installation.c
23 * @brief get paths used by the program
25 * @author Christian Fuchs
26 * @author Christian Grothoff
27 * @author Matthias Wachs
28 * @author Heikki Lindholm
35 #include <unistr.h> /* for u16_to_u8 */
38 #include "gnunet_util_lib.h"
40 #include <mach-o/ldsyms.h>
41 #include <mach-o/dyld.h>
45 #define LOG(kind, ...) \
46 GNUNET_log_from(kind, "util-os-installation", __VA_ARGS__)
48 #define LOG_STRERROR_FILE(kind, syscall, filename) \
49 GNUNET_log_from_strerror_file(kind, \
50 "util-os-installation", \
56 * Default project data used for installation path detection
59 static const struct GNUNET_OS_ProjectData default_pd = {
60 .libname = "libgnunetutil",
61 .project_dirname = "gnunet",
62 .binary_name = "gnunet-arm",
63 .version = PACKAGE_VERSION " " VCS_VERSION,
64 .env_varname = "GNUNET_PREFIX",
65 .base_config_varname = "GNUNET_BASE_CONFIG",
66 .bug_email = "gnunet-developers@gnu.org",
67 .homepage = "http://www.gnu.org/s/gnunet/",
68 .config_file = "gnunet.conf",
69 .user_config_file = "~/.config/gnunet.conf",
71 .gettext_domain = PACKAGE,
76 * Which project data do we currently use for installation
77 * path detection? Never NULL.
79 static const struct GNUNET_OS_ProjectData *current_pd = &default_pd;
82 * Wether or not gettext has been initialized for the library.
83 * Note that the gettext initialization done within
84 * GNUNET_PROGRAM_run2 is for the specific application.
86 static int gettextinit = 0;
89 * Return default project data used by 'libgnunetutil' for GNUnet.
91 const struct GNUNET_OS_ProjectData *
92 GNUNET_OS_project_data_default(void)
99 * @return current project data.
101 const struct GNUNET_OS_ProjectData *
102 GNUNET_OS_project_data_get()
104 if (0 == gettextinit)
106 char *path = GNUNET_OS_installation_get_path(GNUNET_OS_IPK_LOCALEDIR);
108 bindtextdomain(PACKAGE, path);
117 * Setup OS subsystem with project data.
119 * @param pd project data used to determine paths
122 GNUNET_OS_init(const struct GNUNET_OS_ProjectData *pd)
124 if (0 == gettextinit)
126 char *path = GNUNET_OS_installation_get_path(GNUNET_OS_IPK_LOCALEDIR);
128 bindtextdomain(PACKAGE, path);
132 GNUNET_assert(NULL != pd);
139 * Try to determine path by reading /proc/PID/exe
141 * @return NULL on error
144 get_path_from_proc_maps()
152 GNUNET_snprintf(fn, sizeof(fn), "/proc/%u/maps", getpid());
153 if (NULL == (f = fopen(fn, "r")))
155 while (NULL != fgets(line, sizeof(line), f))
157 if ((1 == sscanf(line,
158 "%*x-%*x %*c%*c%*c%*c %*x %*2x:%*2x %*u%*[ ]%1023s",
160 (NULL != (lgu = strstr(dir, current_pd->libname))))
164 return GNUNET_strdup(dir);
173 * Try to determine path by reading /proc/PID/exe
175 * @return NULL on error
178 get_path_from_proc_exe()
185 GNUNET_snprintf(fn, sizeof(fn), "/proc/%u/exe", getpid());
186 size = readlink(fn, lnk, sizeof(lnk) - 1);
189 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_ERROR, "readlink", fn);
192 GNUNET_assert(((size_t)size) < sizeof(lnk));
194 while ((lnk[size] != '/') && (size > 0))
196 GNUNET_asprintf(&lep, "/%s/libexec/", current_pd->project_dirname);
197 /* test for being in lib/gnunet/libexec/ or lib/MULTIARCH/gnunet/libexec */
198 if ((((size_t)size) > strlen(lep)) &&
199 (0 == strcmp(lep, &lnk[size - strlen(lep)])))
200 size -= strlen(lep) - 1;
202 if ((size < 4) || (lnk[size - 4] != '/'))
204 /* not installed in "/bin/" -- binary path probably useless */
208 return GNUNET_strdup(lnk);
215 * Signature of the '_NSGetExecutablePath" function.
217 * @param buf where to write the path
218 * @param number of bytes available in @a buf
219 * @return 0 on success, otherwise desired number of bytes is stored in 'bufsize'
221 typedef int (*MyNSGetExecutablePathProto) (char *buf, size_t *bufsize);
225 * Try to obtain the path of our executable using '_NSGetExecutablePath'.
227 * @return NULL on error
230 get_path_from_NSGetExecutablePath()
232 static char zero = '\0';
235 MyNSGetExecutablePathProto func;
239 (func = (MyNSGetExecutablePathProto)dlsym(RTLD_DEFAULT,
240 "_NSGetExecutablePath")))
244 /* get the path len, including the trailing \0 */
245 (void)func(path, &len);
248 path = GNUNET_malloc(len);
249 if (0 != func(path, &len))
255 while ((path[len] != '/') && (len > 0))
263 * Try to obtain the path of our executable using '_dyld_image' API.
265 * @return NULL on error
268 get_path_from_dyld_image()
276 c = _dyld_image_count();
277 for (i = 0; i < c; i++)
279 if (((const void *)_dyld_get_image_header(i)) !=
280 ((const void *)&_mh_dylib_header))
282 path = _dyld_get_image_name(i);
283 if ((NULL == path) || (0 == strlen(path)))
285 p = GNUNET_strdup(path);
287 while ((s > p) && ('/' != *s))
299 * Return the actual path to a file found in the current
300 * PATH environment variable.
302 * @param binary the name of the file to find
303 * @return path to binary, NULL if not found
306 get_path_from_PATH(const char *binary)
314 if (NULL == (p = getenv("PATH")))
317 path = GNUNET_strdup(p); /* because we write on it */
319 buf = GNUNET_malloc(strlen(path) + strlen(binary) + 1 + 1);
321 while (NULL != (end = strchr(pos, PATH_SEPARATOR)))
324 sprintf(buf, "%s/%s", pos, binary);
325 if (GNUNET_DISK_file_test(buf) == GNUNET_YES)
327 pos = GNUNET_strdup(pos);
334 sprintf(buf, "%s/%s", pos, binary);
335 if (GNUNET_YES == GNUNET_DISK_file_test(buf))
337 pos = GNUNET_strdup(pos);
349 * Try to obtain the installation path using the "GNUNET_PREFIX" environment
352 * @return NULL on error (environment variable not set)
355 get_path_from_GNUNET_PREFIX()
359 if ((NULL != current_pd->env_varname) &&
360 (NULL != (p = getenv(current_pd->env_varname))))
361 return GNUNET_strdup(p);
362 if ((NULL != current_pd->env_varname_alt) &&
363 (NULL != (p = getenv(current_pd->env_varname_alt))))
364 return GNUNET_strdup(p);
370 * @brief get the path to GNUnet bin/ or lib/, prefering the lib/ path
373 * @return a pointer to the executable path, or NULL on error
380 if (NULL != (ret = get_path_from_GNUNET_PREFIX()))
383 if (NULL != (ret = get_path_from_proc_maps()))
385 /* try path *first*, before /proc/exe, as /proc/exe can be wrong */
386 if ((NULL != current_pd->binary_name) &&
387 (NULL != (ret = get_path_from_PATH(current_pd->binary_name))))
389 if (NULL != (ret = get_path_from_proc_exe()))
393 if (NULL != (ret = get_path_from_dyld_image()))
395 if (NULL != (ret = get_path_from_NSGetExecutablePath()))
398 if ((NULL != current_pd->binary_name) &&
399 (NULL != (ret = get_path_from_PATH(current_pd->binary_name))))
401 /* other attempts here */
402 LOG(GNUNET_ERROR_TYPE_ERROR,
404 "Could not determine installation path for %s. Set `%s' environment variable.\n"),
405 current_pd->project_dirname,
406 current_pd->env_varname);
412 * @brief get the path to current app's bin/
413 * @return a pointer to the executable path, or NULL on error
421 if (NULL != (ret = get_path_from_proc_exe()))
425 if (NULL != (ret = get_path_from_NSGetExecutablePath()))
428 /* other attempts here */
434 * @brief get the path to a specific GNUnet installation directory or,
435 * with #GNUNET_OS_IPK_SELF_PREFIX, the current running apps installation directory
436 * @return a pointer to the dir path (to be freed by the caller)
439 GNUNET_OS_installation_get_path(enum GNUNET_OS_InstallationPathKind dirkind)
443 char *execpath = NULL;
449 /* if wanted, try to get the current app's bin/ */
450 if (dirkind == GNUNET_OS_IPK_SELF_PREFIX)
451 execpath = os_get_exec_path();
453 /* try to get GNUnet's bin/ or lib/, or if previous was unsuccessful some
454 * guess for the current app */
455 if (NULL == execpath)
456 execpath = os_get_gnunet_path();
458 if (NULL == execpath)
461 n = strlen(execpath);
464 /* should never happen, but better safe than sorry */
465 GNUNET_free(execpath);
468 /* remove filename itself */
469 while ((n > 1) && (DIR_SEPARATOR == execpath[n - 1]))
470 execpath[--n] = '\0';
473 if ((n > 6) && ((0 == strcasecmp(&execpath[n - 6], "/lib32")) ||
474 (0 == strcasecmp(&execpath[n - 6], "/lib64"))))
476 if ((GNUNET_OS_IPK_LIBDIR != dirkind) &&
477 (GNUNET_OS_IPK_LIBEXECDIR != dirkind))
479 /* strip '/lib32' or '/lib64' */
480 execpath[n - 6] = '\0';
486 else if ((n > 4) && ((0 == strcasecmp(&execpath[n - 4], "/bin")) ||
487 (0 == strcasecmp(&execpath[n - 4], "/lib"))))
489 /* strip '/bin' or '/lib' */
490 execpath[n - 4] = '\0';
494 if (NULL != (libdir = strstr(execpath, "/lib/")))
496 /* test for multi-arch path of the form "PREFIX/lib/MULTIARCH/";
497 here we need to re-add 'multiarch' to lib and libexec paths later! */
498 multiarch = &libdir[5];
499 if (NULL == strchr(multiarch, '/'))
501 '\0'; /* Debian multiarch format, cut of from 'execpath' but preserve in multicarch */
504 NULL; /* maybe not, multiarch still has a '/', which is not OK */
506 /* in case this was a directory named foo-bin, remove "foo-" */
507 while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR))
508 execpath[--n] = '\0';
511 case GNUNET_OS_IPK_PREFIX:
512 case GNUNET_OS_IPK_SELF_PREFIX:
513 dirname = GNUNET_strdup(DIR_SEPARATOR_STR);
516 case GNUNET_OS_IPK_BINDIR:
517 dirname = GNUNET_strdup(DIR_SEPARATOR_STR "bin" DIR_SEPARATOR_STR);
520 case GNUNET_OS_IPK_LIBDIR:
523 GNUNET_asprintf(&tmp,
526 DIR_SEPARATOR_STR "lib",
527 (NULL != multiarch) ? DIR_SEPARATOR_STR : "",
528 (NULL != multiarch) ? multiarch : "",
530 current_pd->project_dirname,
532 if (GNUNET_YES == GNUNET_DISK_directory_test(tmp, GNUNET_YES))
534 GNUNET_free(execpath);
540 if (4 == sizeof(void *))
542 GNUNET_asprintf(&dirname,
543 DIR_SEPARATOR_STR "lib32" DIR_SEPARATOR_STR
544 "%s" DIR_SEPARATOR_STR,
545 current_pd->project_dirname);
546 GNUNET_asprintf(&tmp, "%s%s", execpath, dirname);
548 if (8 == sizeof(void *))
550 GNUNET_asprintf(&dirname,
551 DIR_SEPARATOR_STR "lib64" DIR_SEPARATOR_STR
552 "%s" DIR_SEPARATOR_STR,
553 current_pd->project_dirname);
554 GNUNET_asprintf(&tmp, "%s%s", execpath, dirname);
558 (GNUNET_YES == GNUNET_DISK_directory_test(tmp, GNUNET_YES)))
560 GNUNET_free(execpath);
561 GNUNET_free_non_null(dirname);
565 GNUNET_free_non_null(dirname);
567 GNUNET_asprintf(&dirname,
568 DIR_SEPARATOR_STR "%s" DIR_SEPARATOR_STR,
569 current_pd->project_dirname);
572 case GNUNET_OS_IPK_DATADIR:
573 GNUNET_asprintf(&dirname,
574 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR
575 "%s" DIR_SEPARATOR_STR,
576 current_pd->project_dirname);
579 case GNUNET_OS_IPK_LOCALEDIR:
580 dirname = GNUNET_strdup(DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR
581 "locale" DIR_SEPARATOR_STR);
584 case GNUNET_OS_IPK_ICONDIR:
585 dirname = GNUNET_strdup(DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR
586 "icons" DIR_SEPARATOR_STR);
589 case GNUNET_OS_IPK_DOCDIR:
590 GNUNET_asprintf(&dirname,
591 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR
592 "doc" DIR_SEPARATOR_STR
593 "%s" DIR_SEPARATOR_STR,
594 current_pd->project_dirname);
597 case GNUNET_OS_IPK_LIBEXECDIR:
600 GNUNET_asprintf(&dirname,
601 DIR_SEPARATOR_STR "%s" DIR_SEPARATOR_STR
602 "libexec" DIR_SEPARATOR_STR,
603 current_pd->project_dirname);
604 GNUNET_asprintf(&tmp,
607 DIR_SEPARATOR_STR "lib" DIR_SEPARATOR_STR,
608 (NULL != multiarch) ? multiarch : "",
610 if (GNUNET_YES == GNUNET_DISK_directory_test(tmp, GNUNET_YES))
612 GNUNET_free(execpath);
613 GNUNET_free(dirname);
619 if (4 == sizeof(void *))
621 GNUNET_asprintf(&dirname,
622 DIR_SEPARATOR_STR "lib32" DIR_SEPARATOR_STR
623 "%s" DIR_SEPARATOR_STR
624 "libexec" DIR_SEPARATOR_STR,
625 current_pd->project_dirname);
626 GNUNET_asprintf(&tmp, "%s%s", execpath, dirname);
628 if (8 == sizeof(void *))
630 GNUNET_asprintf(&dirname,
631 DIR_SEPARATOR_STR "lib64" DIR_SEPARATOR_STR
632 "%s" DIR_SEPARATOR_STR
633 "libexec" DIR_SEPARATOR_STR,
634 current_pd->project_dirname);
635 GNUNET_asprintf(&tmp, "%s%s", execpath, dirname);
638 (GNUNET_YES == GNUNET_DISK_directory_test(tmp, GNUNET_YES)))
640 GNUNET_free(execpath);
641 GNUNET_free_non_null(dirname);
645 GNUNET_free_non_null(dirname);
647 GNUNET_asprintf(&dirname,
648 DIR_SEPARATOR_STR "%s" DIR_SEPARATOR_STR
649 "libexec" DIR_SEPARATOR_STR,
650 current_pd->project_dirname);
654 GNUNET_free(execpath);
657 GNUNET_asprintf(&tmp, "%s%s", execpath, dirname);
658 GNUNET_free(dirname);
659 GNUNET_free(execpath);
665 * Given the name of a gnunet-helper, gnunet-service or gnunet-daemon
666 * binary, try to prefix it with the libexec/-directory to get the
669 * @param progname name of the binary
670 * @return full path to the binary, if possible, otherwise copy of 'progname'
673 GNUNET_OS_get_libexec_binary_path(const char *progname)
679 if ((DIR_SEPARATOR == progname[0]) ||
681 GNUNET_STRINGS_path_is_absolute(progname, GNUNET_NO, NULL, NULL)))
682 return GNUNET_strdup(progname);
686 libexecdir = GNUNET_OS_installation_get_path(GNUNET_OS_IPK_LIBEXECDIR);
687 if (NULL == libexecdir)
688 return GNUNET_strdup(progname);
689 GNUNET_asprintf(&binary, "%s%s", libexecdir, progname);
696 * Given the name of a helper, service or daemon binary construct the full
697 * path to the binary using the SUID_BINARY_PATH in the PATHS section of the
698 * configuration. If that option is not present, fall back to
699 * GNUNET_OS_get_libexec_binary_path. If @a progname is an absolute path, a
700 * copy of this path is returned.
702 * @param cfg configuration to inspect
703 * @param progname name of the binary
704 * @return full path to the binary, if possible, a copy of @a progname
708 GNUNET_OS_get_suid_binary_path(const struct GNUNET_CONFIGURATION_Handle *cfg,
709 const char *progname)
717 GNUNET_STRINGS_path_is_absolute(progname, GNUNET_NO, NULL, NULL))
719 return GNUNET_strdup(progname);
724 GNUNET_CONFIGURATION_get_value_string(cfg,
728 if ((NULL == path) || (0 == strlen(path)))
729 return GNUNET_OS_get_libexec_binary_path(progname);
730 path_len = strlen(path);
731 GNUNET_asprintf(&binary,
734 (path[path_len - 1] == DIR_SEPARATOR) ? ""
743 * Check whether an executable exists and possibly if the suid bit is
744 * set on the file. Attempts to find the file using the current PATH
745 * environment variable as a search path.
747 * @param binary the name of the file to check.
748 * W32: must not have an .exe suffix.
749 * @param check_suid input true if the binary should be checked for SUID (*nix)
750 * W32: checks if the program has sufficient privileges by executing this
751 * binary with the -d flag. -d omits a programs main loop and only
752 * executes all privileged operations in an binary.
753 * @param params parameters used for w32 privilege checking (can be NULL for != w32 )
754 * @return #GNUNET_YES if the file is SUID (*nix) or can be executed with current privileges (W32),
755 * #GNUNET_NO if not SUID (but binary exists),
756 * #GNUNET_SYSERR on error (no such binary or not executable)
759 GNUNET_OS_check_helper_binary(const char *binary,
768 GNUNET_STRINGS_path_is_absolute(binary, GNUNET_NO, NULL, NULL)) ||
769 (0 == strncmp(binary, "./", 2)))
771 p = GNUNET_strdup(binary);
775 p = get_path_from_PATH(binary);
778 GNUNET_asprintf(&pf, "%s/%s", p, binary);
786 LOG(GNUNET_ERROR_TYPE_INFO,
787 _("Could not find binary `%s' in PATH!\n"),
789 return GNUNET_SYSERR;
791 if (0 != access(p, X_OK))
793 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "access", p);
795 return GNUNET_SYSERR;
800 /* as we run as root, we don't insist on SUID */
805 if (0 != stat(p, &statbuf))
807 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "stat", p);
809 return GNUNET_SYSERR;
814 if ((0 != (statbuf.st_mode & S_ISUID)) && (0 == statbuf.st_uid))
819 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
820 _("Binary `%s' exists, but is not SUID\n"),
822 /* binary exists, but not SUID */
829 /* end of os_installation.c */