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);
217 * Signature of the '_NSGetExecutablePath" function.
219 * @param buf where to write the path
220 * @param number of bytes available in @a buf
221 * @return 0 on success, otherwise desired number of bytes is stored in 'bufsize'
223 typedef int (*MyNSGetExecutablePathProto) (char *buf, size_t *bufsize);
227 * Try to obtain the path of our executable using '_NSGetExecutablePath'.
229 * @return NULL on error
232 get_path_from_NSGetExecutablePath ()
234 static char zero = '\0';
237 MyNSGetExecutablePathProto func;
241 (func = (MyNSGetExecutablePathProto) dlsym (RTLD_DEFAULT,
242 "_NSGetExecutablePath")))
246 /* get the path len, including the trailing \0 */
247 (void) func (path, &len);
250 path = GNUNET_malloc (len);
251 if (0 != func (path, &len))
257 while ((path[len] != '/') && (len > 0))
265 * Try to obtain the path of our executable using '_dyld_image' API.
267 * @return NULL on error
270 get_path_from_dyld_image ()
278 c = _dyld_image_count ();
279 for (i = 0; i < c; i++)
281 if (((const void *) _dyld_get_image_header (i)) !=
282 ((const void *) &_mh_dylib_header))
284 path = _dyld_get_image_name (i);
285 if ((NULL == path) || (0 == strlen (path)))
287 p = GNUNET_strdup (path);
289 while ((s > p) && ('/' != *s))
303 * Return the actual path to a file found in the current
304 * PATH environment variable.
306 * @param binary the name of the file to find
307 * @return path to binary, NULL if not found
310 get_path_from_PATH (const char *binary)
318 if (NULL == (p = getenv ("PATH")))
321 path = GNUNET_strdup (p); /* because we write on it */
323 buf = GNUNET_malloc (strlen (path) + strlen (binary) + 1 + 1);
325 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
328 sprintf (buf, "%s/%s", pos, binary);
329 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
331 pos = GNUNET_strdup (pos);
338 sprintf (buf, "%s/%s", pos, binary);
339 if (GNUNET_YES == GNUNET_DISK_file_test (buf))
341 pos = GNUNET_strdup (pos);
353 * Try to obtain the installation path using the "GNUNET_PREFIX" environment
356 * @return NULL on error (environment variable not set)
359 get_path_from_GNUNET_PREFIX ()
363 if ((NULL != current_pd->env_varname) &&
364 (NULL != (p = getenv (current_pd->env_varname))))
365 return GNUNET_strdup (p);
366 if ((NULL != current_pd->env_varname_alt) &&
367 (NULL != (p = getenv (current_pd->env_varname_alt))))
368 return GNUNET_strdup (p);
374 * @brief get the path to GNUnet bin/ or lib/, prefering the lib/ path
377 * @return a pointer to the executable path, or NULL on error
380 os_get_gnunet_path ()
384 if (NULL != (ret = get_path_from_GNUNET_PREFIX ()))
387 if (NULL != (ret = get_path_from_proc_maps ()))
389 /* try path *first*, before /proc/exe, as /proc/exe can be wrong */
390 if ((NULL != current_pd->binary_name) &&
391 (NULL != (ret = get_path_from_PATH (current_pd->binary_name))))
393 if (NULL != (ret = get_path_from_proc_exe ()))
397 if (NULL != (ret = get_path_from_dyld_image ()))
399 if (NULL != (ret = get_path_from_NSGetExecutablePath ()))
402 if ((NULL != current_pd->binary_name) &&
403 (NULL != (ret = get_path_from_PATH (current_pd->binary_name))))
405 /* other attempts here */
406 LOG (GNUNET_ERROR_TYPE_ERROR,
408 "Could not determine installation path for %s. Set `%s' environment variable.\n"),
409 current_pd->project_dirname,
410 current_pd->env_varname);
416 * @brief get the path to current app's bin/
417 * @return a pointer to the executable path, or NULL on error
425 if (NULL != (ret = get_path_from_proc_exe ()))
429 if (NULL != (ret = get_path_from_NSGetExecutablePath ()))
432 /* other attempts here */
438 * @brief get the path to a specific GNUnet installation directory or,
439 * with #GNUNET_OS_IPK_SELF_PREFIX, the current running apps installation directory
440 * @return a pointer to the dir path (to be freed by the caller)
443 GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind)
447 char *execpath = NULL;
453 /* if wanted, try to get the current app's bin/ */
454 if (dirkind == GNUNET_OS_IPK_SELF_PREFIX)
455 execpath = os_get_exec_path ();
457 /* try to get GNUnet's bin/ or lib/, or if previous was unsuccessful some
458 * guess for the current app */
459 if (NULL == execpath)
460 execpath = os_get_gnunet_path ();
462 if (NULL == execpath)
465 n = strlen (execpath);
468 /* should never happen, but better safe than sorry */
469 GNUNET_free (execpath);
472 /* remove filename itself */
473 while ((n > 1) && (DIR_SEPARATOR == execpath[n - 1]))
474 execpath[--n] = '\0';
477 if ((n > 6) && ((0 == strcasecmp (&execpath[n - 6], "/lib32")) ||
478 (0 == strcasecmp (&execpath[n - 6], "/lib64"))))
480 if ((GNUNET_OS_IPK_LIBDIR != dirkind) &&
481 (GNUNET_OS_IPK_LIBEXECDIR != dirkind))
483 /* strip '/lib32' or '/lib64' */
484 execpath[n - 6] = '\0';
490 else if ((n > 4) && ((0 == strcasecmp (&execpath[n - 4], "/bin")) ||
491 (0 == strcasecmp (&execpath[n - 4], "/lib"))))
493 /* strip '/bin' or '/lib' */
494 execpath[n - 4] = '\0';
498 if (NULL != (libdir = strstr (execpath, "/lib/")))
500 /* test for multi-arch path of the form "PREFIX/lib/MULTIARCH/";
501 here we need to re-add 'multiarch' to lib and libexec paths later! */
502 multiarch = &libdir[5];
503 if (NULL == strchr (multiarch, '/'))
505 '\0'; /* Debian multiarch format, cut of from 'execpath' but preserve in multicarch */
508 NULL; /* maybe not, multiarch still has a '/', which is not OK */
510 /* in case this was a directory named foo-bin, remove "foo-" */
511 while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR))
512 execpath[--n] = '\0';
515 case GNUNET_OS_IPK_PREFIX:
516 case GNUNET_OS_IPK_SELF_PREFIX:
517 dirname = GNUNET_strdup (DIR_SEPARATOR_STR);
520 case GNUNET_OS_IPK_BINDIR:
521 dirname = GNUNET_strdup (DIR_SEPARATOR_STR "bin" DIR_SEPARATOR_STR);
524 case GNUNET_OS_IPK_LIBDIR:
527 GNUNET_asprintf (&tmp,
530 DIR_SEPARATOR_STR "lib",
531 (NULL != multiarch) ? DIR_SEPARATOR_STR : "",
532 (NULL != multiarch) ? multiarch : "",
534 current_pd->project_dirname,
536 if (GNUNET_YES == GNUNET_DISK_directory_test (tmp, GNUNET_YES))
538 GNUNET_free (execpath);
544 if (4 == sizeof(void *))
546 GNUNET_asprintf (&dirname,
547 DIR_SEPARATOR_STR "lib32" DIR_SEPARATOR_STR
548 "%s" DIR_SEPARATOR_STR,
549 current_pd->project_dirname);
550 GNUNET_asprintf (&tmp, "%s%s", execpath, dirname);
552 if (8 == sizeof(void *))
554 GNUNET_asprintf (&dirname,
555 DIR_SEPARATOR_STR "lib64" DIR_SEPARATOR_STR
556 "%s" DIR_SEPARATOR_STR,
557 current_pd->project_dirname);
558 GNUNET_asprintf (&tmp, "%s%s", execpath, dirname);
562 (GNUNET_YES == GNUNET_DISK_directory_test (tmp, GNUNET_YES)))
564 GNUNET_free (execpath);
565 GNUNET_free_non_null (dirname);
569 GNUNET_free_non_null (dirname);
571 GNUNET_asprintf (&dirname,
572 DIR_SEPARATOR_STR "%s" DIR_SEPARATOR_STR,
573 current_pd->project_dirname);
576 case GNUNET_OS_IPK_DATADIR:
577 GNUNET_asprintf (&dirname,
578 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR
579 "%s" DIR_SEPARATOR_STR,
580 current_pd->project_dirname);
583 case GNUNET_OS_IPK_LOCALEDIR:
584 dirname = GNUNET_strdup (DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR
585 "locale" DIR_SEPARATOR_STR);
588 case GNUNET_OS_IPK_ICONDIR:
589 dirname = GNUNET_strdup (DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR
590 "icons" DIR_SEPARATOR_STR);
593 case GNUNET_OS_IPK_DOCDIR:
594 GNUNET_asprintf (&dirname,
595 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR
596 "doc" DIR_SEPARATOR_STR
597 "%s" DIR_SEPARATOR_STR,
598 current_pd->project_dirname);
601 case GNUNET_OS_IPK_LIBEXECDIR:
604 GNUNET_asprintf (&dirname,
605 DIR_SEPARATOR_STR "%s" DIR_SEPARATOR_STR
606 "libexec" DIR_SEPARATOR_STR,
607 current_pd->project_dirname);
608 GNUNET_asprintf (&tmp,
611 DIR_SEPARATOR_STR "lib" DIR_SEPARATOR_STR,
612 (NULL != multiarch) ? multiarch : "",
614 GNUNET_free (dirname);
615 if (GNUNET_YES == GNUNET_DISK_directory_test (tmp, GNUNET_YES))
617 GNUNET_free (execpath);
623 if (4 == sizeof(void *))
625 GNUNET_asprintf (&dirname,
626 DIR_SEPARATOR_STR "lib32" DIR_SEPARATOR_STR
627 "%s" DIR_SEPARATOR_STR
628 "libexec" DIR_SEPARATOR_STR,
629 current_pd->project_dirname);
630 GNUNET_asprintf (&tmp, "%s%s", execpath, dirname);
632 if (8 == sizeof(void *))
634 GNUNET_asprintf (&dirname,
635 DIR_SEPARATOR_STR "lib64" DIR_SEPARATOR_STR
636 "%s" DIR_SEPARATOR_STR
637 "libexec" DIR_SEPARATOR_STR,
638 current_pd->project_dirname);
639 GNUNET_asprintf (&tmp, "%s%s", execpath, dirname);
642 (GNUNET_YES == GNUNET_DISK_directory_test (tmp, GNUNET_YES)))
644 GNUNET_free (execpath);
645 GNUNET_free_non_null (dirname);
649 GNUNET_free_non_null (dirname);
651 GNUNET_asprintf (&dirname,
652 DIR_SEPARATOR_STR "%s" DIR_SEPARATOR_STR
653 "libexec" DIR_SEPARATOR_STR,
654 current_pd->project_dirname);
658 GNUNET_free (execpath);
661 GNUNET_asprintf (&tmp, "%s%s", execpath, dirname);
662 GNUNET_free (dirname);
663 GNUNET_free (execpath);
669 * Given the name of a gnunet-helper, gnunet-service or gnunet-daemon
670 * binary, try to prefix it with the libexec/-directory to get the
673 * @param progname name of the binary
674 * @return full path to the binary, if possible, otherwise copy of 'progname'
677 GNUNET_OS_get_libexec_binary_path (const char *progname)
683 if ((DIR_SEPARATOR == progname[0]) ||
685 GNUNET_STRINGS_path_is_absolute (progname, GNUNET_NO, NULL, NULL)))
686 return GNUNET_strdup (progname);
690 libexecdir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBEXECDIR);
691 if (NULL == libexecdir)
692 return GNUNET_strdup (progname);
693 GNUNET_asprintf (&binary, "%s%s", libexecdir, progname);
700 * Given the name of a helper, service or daemon binary construct the full
701 * path to the binary using the SUID_BINARY_PATH in the PATHS section of the
702 * configuration. If that option is not present, fall back to
703 * GNUNET_OS_get_libexec_binary_path. If @a progname is an absolute path, a
704 * copy of this path is returned.
706 * @param cfg configuration to inspect
707 * @param progname name of the binary
708 * @return full path to the binary, if possible, a copy of @a progname
712 GNUNET_OS_get_suid_binary_path (const struct GNUNET_CONFIGURATION_Handle *cfg,
713 const char *progname)
721 GNUNET_STRINGS_path_is_absolute (progname, GNUNET_NO, NULL, NULL))
723 return GNUNET_strdup (progname);
728 GNUNET_CONFIGURATION_get_value_string (cfg,
732 if ((NULL == path) || (0 == strlen (path)))
733 return GNUNET_OS_get_libexec_binary_path (progname);
734 path_len = strlen (path);
735 GNUNET_asprintf (&binary,
738 (path[path_len - 1] == DIR_SEPARATOR) ? ""
747 * Check whether an executable exists and possibly if the suid bit is
748 * set on the file. Attempts to find the file using the current PATH
749 * environment variable as a search path.
751 * @param binary the name of the file to check.
752 * W32: must not have an .exe suffix.
753 * @param check_suid input true if the binary should be checked for SUID (*nix)
754 * W32: checks if the program has sufficient privileges by executing this
755 * binary with the -d flag. -d omits a programs main loop and only
756 * executes all privileged operations in an binary.
757 * @param params parameters used for w32 privilege checking (can be NULL for != w32 )
758 * @return #GNUNET_YES if the file is SUID (*nix) or can be executed with current privileges (W32),
759 * #GNUNET_NO if not SUID (but binary exists),
760 * #GNUNET_SYSERR on error (no such binary or not executable)
763 GNUNET_OS_check_helper_binary (const char *binary,
772 GNUNET_STRINGS_path_is_absolute (binary, GNUNET_NO, NULL, NULL)) ||
773 (0 == strncmp (binary, "./", 2)))
775 p = GNUNET_strdup (binary);
779 p = get_path_from_PATH (binary);
782 GNUNET_asprintf (&pf, "%s/%s", p, binary);
790 LOG (GNUNET_ERROR_TYPE_INFO,
791 _ ("Could not find binary `%s' in PATH!\n"),
793 return GNUNET_SYSERR;
795 if (0 != access (p, X_OK))
797 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "access", p);
799 return GNUNET_SYSERR;
804 /* as we run as root, we don't insist on SUID */
809 if (0 != stat (p, &statbuf))
811 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", p);
813 return GNUNET_SYSERR;
818 if ((0 != (statbuf.st_mode & S_ISUID)) && (0 == statbuf.st_uid))
823 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
824 _ ("Binary `%s' exists, but is not SUID\n"),
826 /* binary exists, but not SUID */
833 /* end of os_installation.c */