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,
73 .agpl_url = GNUNET_AGPL_URL,
77 * Which project data do we currently use for installation
78 * path detection? Never NULL.
80 static const struct GNUNET_OS_ProjectData *current_pd = &default_pd;
83 * Wether or not gettext has been initialized for the library.
84 * Note that the gettext initialization done within
85 * GNUNET_PROGRAM_run2 is for the specific application.
87 static int gettextinit = 0;
90 * Return default project data used by 'libgnunetutil' for GNUnet.
92 const struct GNUNET_OS_ProjectData *
93 GNUNET_OS_project_data_default (void)
100 * @return current project data.
102 const struct GNUNET_OS_ProjectData *
103 GNUNET_OS_project_data_get ()
105 if (0 == gettextinit)
107 char *path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LOCALEDIR);
109 bindtextdomain (PACKAGE, path);
118 * Setup OS subsystem with project data.
120 * @param pd project data used to determine paths
123 GNUNET_OS_init (const struct GNUNET_OS_ProjectData *pd)
125 if (0 == gettextinit)
127 char *path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LOCALEDIR);
129 bindtextdomain (PACKAGE, path);
133 GNUNET_assert (NULL != pd);
140 * Try to determine path by reading /proc/PID/exe
142 * @return NULL on error
145 get_path_from_proc_maps ()
153 GNUNET_snprintf (fn, sizeof(fn), "/proc/%u/maps", getpid ());
154 if (NULL == (f = fopen (fn, "r")))
156 while (NULL != fgets (line, sizeof(line), f))
158 if ((1 == sscanf (line,
159 "%*x-%*x %*c%*c%*c%*c %*x %*2x:%*2x %*u%*[ ]%1023s",
161 (NULL != (lgu = strstr (dir, current_pd->libname))))
165 return GNUNET_strdup (dir);
174 * Try to determine path by reading /proc/PID/exe
176 * @return NULL on error
179 get_path_from_proc_exe ()
186 GNUNET_snprintf (fn, sizeof(fn), "/proc/%u/exe", getpid ());
187 size = readlink (fn, lnk, sizeof(lnk) - 1);
190 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "readlink", fn);
193 GNUNET_assert (((size_t) size) < sizeof(lnk));
195 while ((lnk[size] != '/') && (size > 0))
197 GNUNET_asprintf (&lep, "/%s/libexec/", current_pd->project_dirname);
198 /* test for being in lib/gnunet/libexec/ or lib/MULTIARCH/gnunet/libexec */
199 if ((((size_t) size) > strlen (lep)) &&
200 (0 == strcmp (lep, &lnk[size - strlen (lep)])))
201 size -= strlen (lep) - 1;
203 if ((size < 4) || (lnk[size - 4] != '/'))
205 /* not installed in "/bin/" -- binary path probably useless */
209 return GNUNET_strdup (lnk);
218 * Signature of the '_NSGetExecutablePath" function.
220 * @param buf where to write the path
221 * @param number of bytes available in @a buf
222 * @return 0 on success, otherwise desired number of bytes is stored in 'bufsize'
224 typedef int (*MyNSGetExecutablePathProto) (char *buf, size_t *bufsize);
228 * Try to obtain the path of our executable using '_NSGetExecutablePath'.
230 * @return NULL on error
233 get_path_from_NSGetExecutablePath ()
235 static char zero = '\0';
238 MyNSGetExecutablePathProto func;
242 (func = (MyNSGetExecutablePathProto) dlsym (RTLD_DEFAULT,
243 "_NSGetExecutablePath")))
247 /* get the path len, including the trailing \0 */
248 (void) func (path, &len);
251 path = GNUNET_malloc (len);
252 if (0 != func (path, &len))
258 while ((path[len] != '/') && (len > 0))
266 * Try to obtain the path of our executable using '_dyld_image' API.
268 * @return NULL on error
271 get_path_from_dyld_image ()
279 c = _dyld_image_count ();
280 for (i = 0; i < c; i++)
282 if (((const void *) _dyld_get_image_header (i)) !=
283 ((const void *) &_mh_dylib_header))
285 path = _dyld_get_image_name (i);
286 if ((NULL == path) || (0 == strlen (path)))
288 p = GNUNET_strdup (path);
290 while ((s > p) && ('/' != *s))
304 * Return the actual path to a file found in the current
305 * PATH environment variable.
307 * @param binary the name of the file to find
308 * @return path to binary, NULL if not found
311 get_path_from_PATH (const char *binary)
319 if (NULL == (p = getenv ("PATH")))
322 path = GNUNET_strdup (p); /* because we write on it */
324 buf = GNUNET_malloc (strlen (path) + strlen (binary) + 1 + 1);
326 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
329 sprintf (buf, "%s/%s", pos, binary);
330 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
332 pos = GNUNET_strdup (pos);
339 sprintf (buf, "%s/%s", pos, binary);
340 if (GNUNET_YES == GNUNET_DISK_file_test (buf))
342 pos = GNUNET_strdup (pos);
354 * Try to obtain the installation path using the "GNUNET_PREFIX" environment
357 * @return NULL on error (environment variable not set)
360 get_path_from_GNUNET_PREFIX ()
364 if ((NULL != current_pd->env_varname) &&
365 (NULL != (p = getenv (current_pd->env_varname))))
366 return GNUNET_strdup (p);
367 if ((NULL != current_pd->env_varname_alt) &&
368 (NULL != (p = getenv (current_pd->env_varname_alt))))
369 return GNUNET_strdup (p);
375 * @brief get the path to GNUnet bin/ or lib/, prefering the lib/ path
378 * @return a pointer to the executable path, or NULL on error
381 os_get_gnunet_path ()
385 if (NULL != (ret = get_path_from_GNUNET_PREFIX ()))
388 if (NULL != (ret = get_path_from_proc_maps ()))
390 /* try path *first*, before /proc/exe, as /proc/exe can be wrong */
391 if ((NULL != current_pd->binary_name) &&
392 (NULL != (ret = get_path_from_PATH (current_pd->binary_name))))
394 if (NULL != (ret = get_path_from_proc_exe ()))
398 if (NULL != (ret = get_path_from_dyld_image ()))
400 if (NULL != (ret = get_path_from_NSGetExecutablePath ()))
403 if ((NULL != current_pd->binary_name) &&
404 (NULL != (ret = get_path_from_PATH (current_pd->binary_name))))
406 /* other attempts here */
407 LOG (GNUNET_ERROR_TYPE_ERROR,
409 "Could not determine installation path for %s. Set `%s' environment variable.\n"),
410 current_pd->project_dirname,
411 current_pd->env_varname);
417 * @brief get the path to current app's bin/
418 * @return a pointer to the executable path, or NULL on error
426 if (NULL != (ret = get_path_from_proc_exe ()))
430 if (NULL != (ret = get_path_from_NSGetExecutablePath ()))
433 /* other attempts here */
439 * @brief get the path to a specific GNUnet installation directory or,
440 * with #GNUNET_OS_IPK_SELF_PREFIX, the current running apps installation directory
441 * @return a pointer to the dir path (to be freed by the caller)
444 GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind)
448 char *execpath = NULL;
454 /* if wanted, try to get the current app's bin/ */
455 if (dirkind == GNUNET_OS_IPK_SELF_PREFIX)
456 execpath = os_get_exec_path ();
458 /* try to get GNUnet's bin/ or lib/, or if previous was unsuccessful some
459 * guess for the current app */
460 if (NULL == execpath)
461 execpath = os_get_gnunet_path ();
463 if (NULL == execpath)
466 n = strlen (execpath);
469 /* should never happen, but better safe than sorry */
470 GNUNET_free (execpath);
473 /* remove filename itself */
474 while ((n > 1) && (DIR_SEPARATOR == execpath[n - 1]))
475 execpath[--n] = '\0';
478 if ((n > 6) && ((0 == strcasecmp (&execpath[n - 6], "/lib32")) ||
479 (0 == strcasecmp (&execpath[n - 6], "/lib64"))))
481 if ((GNUNET_OS_IPK_LIBDIR != dirkind) &&
482 (GNUNET_OS_IPK_LIBEXECDIR != dirkind))
484 /* strip '/lib32' or '/lib64' */
485 execpath[n - 6] = '\0';
491 else if ((n > 4) && ((0 == strcasecmp (&execpath[n - 4], "/bin")) ||
492 (0 == strcasecmp (&execpath[n - 4], "/lib"))))
494 /* strip '/bin' or '/lib' */
495 execpath[n - 4] = '\0';
499 if (NULL != (libdir = strstr (execpath, "/lib/")))
501 /* test for multi-arch path of the form "PREFIX/lib/MULTIARCH/";
502 here we need to re-add 'multiarch' to lib and libexec paths later! */
503 multiarch = &libdir[5];
504 if (NULL == strchr (multiarch, '/'))
506 '\0'; /* Debian multiarch format, cut of from 'execpath' but preserve in multicarch */
509 NULL; /* maybe not, multiarch still has a '/', which is not OK */
511 /* in case this was a directory named foo-bin, remove "foo-" */
512 while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR))
513 execpath[--n] = '\0';
516 case GNUNET_OS_IPK_PREFIX:
517 case GNUNET_OS_IPK_SELF_PREFIX:
518 dirname = GNUNET_strdup (DIR_SEPARATOR_STR);
521 case GNUNET_OS_IPK_BINDIR:
522 dirname = GNUNET_strdup (DIR_SEPARATOR_STR "bin" DIR_SEPARATOR_STR);
525 case GNUNET_OS_IPK_LIBDIR:
528 GNUNET_asprintf (&tmp,
531 DIR_SEPARATOR_STR "lib",
532 (NULL != multiarch) ? DIR_SEPARATOR_STR : "",
533 (NULL != multiarch) ? multiarch : "",
535 current_pd->project_dirname,
537 if (GNUNET_YES == GNUNET_DISK_directory_test (tmp, GNUNET_YES))
539 GNUNET_free (execpath);
545 if (4 == sizeof(void *))
547 GNUNET_asprintf (&dirname,
548 DIR_SEPARATOR_STR "lib32" DIR_SEPARATOR_STR
549 "%s" DIR_SEPARATOR_STR,
550 current_pd->project_dirname);
551 GNUNET_asprintf (&tmp, "%s%s", execpath, dirname);
553 if (8 == sizeof(void *))
555 GNUNET_asprintf (&dirname,
556 DIR_SEPARATOR_STR "lib64" DIR_SEPARATOR_STR
557 "%s" DIR_SEPARATOR_STR,
558 current_pd->project_dirname);
559 GNUNET_asprintf (&tmp, "%s%s", execpath, dirname);
563 (GNUNET_YES == GNUNET_DISK_directory_test (tmp, GNUNET_YES)))
565 GNUNET_free (execpath);
566 GNUNET_free_non_null (dirname);
570 GNUNET_free_non_null (dirname);
572 GNUNET_asprintf (&dirname,
573 DIR_SEPARATOR_STR "%s" DIR_SEPARATOR_STR,
574 current_pd->project_dirname);
577 case GNUNET_OS_IPK_DATADIR:
578 GNUNET_asprintf (&dirname,
579 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR
580 "%s" DIR_SEPARATOR_STR,
581 current_pd->project_dirname);
584 case GNUNET_OS_IPK_LOCALEDIR:
585 dirname = GNUNET_strdup (DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR
586 "locale" DIR_SEPARATOR_STR);
589 case GNUNET_OS_IPK_ICONDIR:
590 dirname = GNUNET_strdup (DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR
591 "icons" DIR_SEPARATOR_STR);
594 case GNUNET_OS_IPK_DOCDIR:
595 GNUNET_asprintf (&dirname,
596 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR
597 "doc" DIR_SEPARATOR_STR
598 "%s" DIR_SEPARATOR_STR,
599 current_pd->project_dirname);
602 case GNUNET_OS_IPK_LIBEXECDIR:
605 GNUNET_asprintf (&dirname,
606 DIR_SEPARATOR_STR "%s" DIR_SEPARATOR_STR
607 "libexec" DIR_SEPARATOR_STR,
608 current_pd->project_dirname);
609 GNUNET_asprintf (&tmp,
612 DIR_SEPARATOR_STR "lib" DIR_SEPARATOR_STR,
613 (NULL != multiarch) ? multiarch : "",
615 GNUNET_free (dirname);
616 if (GNUNET_YES == GNUNET_DISK_directory_test (tmp, GNUNET_YES))
618 GNUNET_free (execpath);
624 if (4 == sizeof(void *))
626 GNUNET_asprintf (&dirname,
627 DIR_SEPARATOR_STR "lib32" DIR_SEPARATOR_STR
628 "%s" DIR_SEPARATOR_STR
629 "libexec" DIR_SEPARATOR_STR,
630 current_pd->project_dirname);
631 GNUNET_asprintf (&tmp, "%s%s", execpath, dirname);
633 if (8 == sizeof(void *))
635 GNUNET_asprintf (&dirname,
636 DIR_SEPARATOR_STR "lib64" DIR_SEPARATOR_STR
637 "%s" DIR_SEPARATOR_STR
638 "libexec" DIR_SEPARATOR_STR,
639 current_pd->project_dirname);
640 GNUNET_asprintf (&tmp, "%s%s", execpath, dirname);
643 (GNUNET_YES == GNUNET_DISK_directory_test (tmp, GNUNET_YES)))
645 GNUNET_free (execpath);
646 GNUNET_free_non_null (dirname);
650 GNUNET_free_non_null (dirname);
652 GNUNET_asprintf (&dirname,
653 DIR_SEPARATOR_STR "%s" DIR_SEPARATOR_STR
654 "libexec" DIR_SEPARATOR_STR,
655 current_pd->project_dirname);
659 GNUNET_free (execpath);
662 GNUNET_asprintf (&tmp, "%s%s", execpath, dirname);
663 GNUNET_free (dirname);
664 GNUNET_free (execpath);
670 * Given the name of a gnunet-helper, gnunet-service or gnunet-daemon
671 * binary, try to prefix it with the libexec/-directory to get the
674 * @param progname name of the binary
675 * @return full path to the binary, if possible, otherwise copy of 'progname'
678 GNUNET_OS_get_libexec_binary_path (const char *progname)
684 if ((DIR_SEPARATOR == progname[0]) ||
686 GNUNET_STRINGS_path_is_absolute (progname, GNUNET_NO, NULL, NULL)))
687 return GNUNET_strdup (progname);
691 libexecdir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBEXECDIR);
692 if (NULL == libexecdir)
693 return GNUNET_strdup (progname);
694 GNUNET_asprintf (&binary, "%s%s", libexecdir, progname);
701 * Given the name of a helper, service or daemon binary construct the full
702 * path to the binary using the SUID_BINARY_PATH in the PATHS section of the
703 * configuration. If that option is not present, fall back to
704 * GNUNET_OS_get_libexec_binary_path. If @a progname is an absolute path, a
705 * copy of this path is returned.
707 * @param cfg configuration to inspect
708 * @param progname name of the binary
709 * @return full path to the binary, if possible, a copy of @a progname
713 GNUNET_OS_get_suid_binary_path (const struct GNUNET_CONFIGURATION_Handle *cfg,
714 const char *progname)
722 GNUNET_STRINGS_path_is_absolute (progname, GNUNET_NO, NULL, NULL))
724 return GNUNET_strdup (progname);
729 GNUNET_CONFIGURATION_get_value_string (cfg,
733 if ((NULL == path) || (0 == strlen (path)))
734 return GNUNET_OS_get_libexec_binary_path (progname);
735 path_len = strlen (path);
736 GNUNET_asprintf (&binary,
739 (path[path_len - 1] == DIR_SEPARATOR) ? ""
748 * Check whether an executable exists and possibly if the suid bit is
749 * set on the file. Attempts to find the file using the current PATH
750 * environment variable as a search path.
752 * @param binary the name of the file to check.
753 * W32: must not have an .exe suffix.
754 * @param check_suid input true if the binary should be checked for SUID (*nix)
755 * W32: checks if the program has sufficient privileges by executing this
756 * binary with the -d flag. -d omits a programs main loop and only
757 * executes all privileged operations in an binary.
758 * @param params parameters used for w32 privilege checking (can be NULL for != w32 )
759 * @return #GNUNET_YES if the file is SUID (*nix) or can be executed with current privileges (W32),
760 * #GNUNET_NO if not SUID (but binary exists),
761 * #GNUNET_SYSERR on error (no such binary or not executable)
764 GNUNET_OS_check_helper_binary (const char *binary,
773 GNUNET_STRINGS_path_is_absolute (binary, GNUNET_NO, NULL, NULL)) ||
774 (0 == strncmp (binary, "./", 2)))
776 p = GNUNET_strdup (binary);
780 p = get_path_from_PATH (binary);
783 GNUNET_asprintf (&pf, "%s/%s", p, binary);
791 LOG (GNUNET_ERROR_TYPE_INFO,
792 _ ("Could not find binary `%s' in PATH!\n"),
794 return GNUNET_SYSERR;
796 if (0 != access (p, X_OK))
798 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "access", p);
800 return GNUNET_SYSERR;
805 /* as we run as root, we don't insist on SUID */
810 if (0 != stat (p, &statbuf))
812 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", p);
814 return GNUNET_SYSERR;
819 if ((0 != (statbuf.st_mode & S_ISUID)) && (0 == statbuf.st_uid))
824 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
825 _ ("Binary `%s' exists, but is not SUID\n"),
827 /* binary exists, but not SUID */
834 /* end of os_installation.c */