26 #include "dbus-sysdeps.h"
27 #include "dbus-sysdeps-unix.h"
28 #include "dbus-internals.h"
29 #include "dbus-list.h"
30 #include "dbus-pipe.h"
31 #include "dbus-protocol.h"
32 #include "dbus-string.h"
33 #define DBUS_USERDB_INCLUDES_PRIVATE 1
34 #include "dbus-userdb.h"
35 #include "dbus-test.h"
37 #include <sys/types.h>
46 #ifdef HAVE_SYS_RESOURCE_H
47 #include <sys/resource.h>
50 #include <sys/socket.h>
54 #ifdef HAVE_SYS_SYSLIMITS_H
55 #include <sys/syslimits.h>
59 #include <systemd/sd-daemon.h>
89 DBusEnsureStandardFdsFlags flags;
91 _dbus_verbose (
"Becoming a daemon...\n");
93 _dbus_verbose (
"chdir to /\n");
97 "Could not chdir() to root directory");
101 _dbus_verbose (
"forking...\n");
102 switch ((child_pid = fork ()))
105 _dbus_verbose (
"fork failed\n");
107 "Failed to fork daemon: %s", _dbus_strerror (errno));
112 _dbus_verbose (
"in child, closing std file descriptors\n");
114 flags = DBUS_FORCE_STDIN_NULL | DBUS_FORCE_STDOUT_NULL;
117 if (s ==
NULL || *s ==
'\0')
118 flags |= DBUS_FORCE_STDERR_NULL;
120 _dbus_verbose (
"keeping stderr open due to DBUS_DEBUG_OUTPUT\n");
124 _dbus_warn (
"%s: %s", s, _dbus_strerror (errno));
131 _dbus_verbose (
"setting umask\n");
135 _dbus_verbose (
"calling setsid()\n");
145 _dbus_verbose (
"pid file or pipe write failed: %s\n",
147 kill (child_pid, SIGTERM);
151 _dbus_verbose (
"parent exiting\n");
169 _dbus_write_pid_file (
const DBusString *filename,
173 const char *cfilename;
179 fd = open (cfilename, O_WRONLY|O_CREAT|O_EXCL|O_BINARY, 0644);
184 "Failed to open \"%s\": %s", cfilename,
185 _dbus_strerror (errno));
189 if ((f = fdopen (fd,
"w")) ==
NULL)
192 "Failed to fdopen fd %d: %s", fd, _dbus_strerror (errno));
197 if (fprintf (f,
"%lu\n", pid) < 0)
200 "Failed to write to \"%s\": %s", cfilename,
201 _dbus_strerror (errno));
207 if (fclose (f) == EOF)
210 "Failed to close \"%s\": %s", cfilename,
211 _dbus_strerror (errno));
238 if (!_dbus_write_pid_file (pidfile,
242 _dbus_verbose (
"pid file write failed\n");
243 _DBUS_ASSERT_ERROR_IS_SET(error);
249 _dbus_verbose (
"No pid file requested\n");
252 if (print_pid_pipe !=
NULL && _dbus_pipe_is_valid (print_pid_pipe))
257 _dbus_verbose (
"writing our pid to pipe %d\n",
262 _DBUS_SET_OOM (error);
270 _DBUS_SET_OOM (error);
275 if (_dbus_pipe_write (print_pid_pipe, &pid, 0, bytes, error) != bytes)
281 "Printing message bus PID: did not write enough bytes\n");
291 _dbus_verbose (
"No pid pipe to write to\n");
315 #ifndef HAVE_LIBAUDIT
336 "User '%s' does not appear to exist?",
348 if (setgroups (0,
NULL) < 0)
349 _dbus_warn (
"Failed to drop supplementary groups: %s",
350 _dbus_strerror (errno));
355 if (setgid (gid) < 0)
358 "Failed to set GID to %lu: %s", gid,
359 _dbus_strerror (errno));
363 if (setuid (uid) < 0)
366 "Failed to set UID to %lu: %s", uid,
367 _dbus_strerror (errno));
375 #ifdef HAVE_SETRLIMIT
386 _dbus_rlimit_save_fd_limit (
DBusError *error)
394 _DBUS_SET_OOM (error);
398 if (getrlimit (RLIMIT_NOFILE, &self->lim) < 0)
401 "Failed to get fd limit: %s", _dbus_strerror (errno));
412 #define ENOUGH_FDS 65536
415 _dbus_rlimit_raise_fd_limit (
DBusError *error)
417 struct rlimit old, lim;
419 if (getrlimit (RLIMIT_NOFILE, &lim) < 0)
422 "Failed to get fd limit: %s", _dbus_strerror (errno));
434 if (lim.rlim_cur != RLIM_INFINITY &&
435 lim.rlim_cur < ENOUGH_FDS)
436 lim.rlim_cur = ENOUGH_FDS;
438 if (lim.rlim_max != RLIM_INFINITY &&
439 lim.rlim_max < lim.rlim_cur)
440 lim.rlim_max = lim.rlim_cur;
449 if (lim.rlim_max == RLIM_INFINITY || lim.rlim_cur < lim.rlim_max)
450 lim.rlim_cur = lim.rlim_max;
453 if (lim.rlim_max == old.rlim_max &&
454 lim.rlim_cur == old.rlim_cur)
457 if (setrlimit (RLIMIT_NOFILE, &lim) < 0)
460 "Failed to set fd limit to %lu: %s",
461 (
unsigned long) lim.rlim_cur,
462 _dbus_strerror (errno));
470 _dbus_rlimit_restore_fd_limit (DBusRLimit *saved,
473 if (setrlimit (RLIMIT_NOFILE, &saved->lim) < 0)
476 "Failed to restore old fd limit: %s",
477 _dbus_strerror (errno));
487 fd_limit_not_supported (
DBusError *error)
490 "cannot change fd limit on this platform");
494 _dbus_rlimit_save_fd_limit (
DBusError *error)
496 fd_limit_not_supported (error);
501 _dbus_rlimit_raise_fd_limit (
DBusError *error)
503 fd_limit_not_supported (error);
508 _dbus_rlimit_restore_fd_limit (DBusRLimit *saved,
511 fd_limit_not_supported (error);
518 _dbus_rlimit_free (DBusRLimit *lim)
532 struct sigaction act;
535 sigemptyset (&empty_mask);
536 act.sa_handler = handler;
537 act.sa_mask = empty_mask;
539 sigaction (sig, &act,
NULL);
550 return (access (file, F_OK) == 0);
563 #ifdef DBUS_CONSOLE_AUTH_DIR
570 _DBUS_SET_OOM (error);
576 _DBUS_SET_OOM (error);
584 _DBUS_SET_OOM (error);
628 const char *filename_c;
631 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
635 if (stat (filename_c, &sb) < 0)
638 "%s", _dbus_strerror (errno));
642 statbuf->
mode = sb.st_mode;
643 statbuf->
nlink = sb.st_nlink;
644 statbuf->
uid = sb.st_uid;
645 statbuf->
gid = sb.st_gid;
646 statbuf->
size = sb.st_size;
647 statbuf->
atime = sb.st_atime;
648 statbuf->
mtime = sb.st_mtime;
649 statbuf->
ctime = sb.st_ctime;
677 const char *filename_c;
679 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
683 d = opendir (filename_c);
687 "Failed to read directory \"%s\": %s",
689 _dbus_strerror (errno));
697 "Could not allocate memory for directory iterator");
727 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
731 ent = readdir (iter->
d);
740 "%s", _dbus_strerror (err));
744 else if (ent->d_name[0] ==
'.' &&
745 (ent->d_name[1] ==
'\0' ||
746 (ent->d_name[1] ==
'.' && ent->d_name[2] ==
'\0')))
754 "No memory to read directory entry");
775 fill_user_info_from_group (
struct group *g,
781 info->
gid = g->gr_gid;
801 const char *group_c_str;
816 #if defined (HAVE_POSIX_GETPWNAM_R) || defined (HAVE_NONPOSIX_GETPWNAM_R)
826 buflen = sysconf (_SC_GETGR_R_SIZE_MAX);
832 if ((
long) buflen <= 0)
846 #ifdef HAVE_POSIX_GETPWNAM_R
848 result = getgrnam_r (group_c_str, &g_str, buf, buflen,
851 result = getgrgid_r (gid, &g_str, buf, buflen,
854 g = getgrnam_r (group_c_str, &g_str, buf, buflen);
860 if (result == ERANGE && buflen < 512 * 1024)
871 if (result == 0 && g == &g_str)
873 b = fill_user_info_from_group (g, info, error);
880 "Group %s unknown or failed to look it up\n",
881 group_c_str ? group_c_str :
"???");
891 g = getgrnam (group_c_str);
895 return fill_user_info_from_group (g, info, error);
900 "Group %s unknown or failed to look it up\n",
901 group_c_str ? group_c_str :
"???");
941 return fill_group_info (info, gid,
NULL, error);
1098 buf = _dbus_string_get_udata (str);
1101 for (i = 0; i < len; i++)
1103 unsigned char c = (
unsigned char) buf[i];
1106 else if (c < 0x20 || c > 127)
1138 _DBUS_SET_OOM (error);
1144 _DBUS_SET_OOM (error);
1157 "Failed to open \"%s\": %s",
1159 _dbus_strerror (errno));
1167 "Failed to read from \"%s\": %s",
1169 _dbus_strerror (errno));
1177 string_squash_nonprintable (&cmdline);
1186 _DBUS_SET_OOM (error);
1208 ensure_owned_directory (
const char *label,
1235 if (stat (dir, &buf) != 0)
1237 int saved_errno = errno;
1240 "%s \"%s\" not available: %s", label, dir,
1241 _dbus_strerror (saved_errno));
1245 if (!S_ISDIR (buf.st_mode))
1252 if (buf.st_uid != geteuid ())
1255 "%s \"%s\" is owned by uid %ld, not our uid %ld",
1256 label, dir, (
long) buf.st_uid, (
long) geteuid ());
1262 if ((S_IWOTH | S_IWGRP) & buf.st_mode)
1265 "%s \"%s\" can be written by others (mode 0%o)",
1266 label, dir, buf.st_mode);
1273 #define DBUS_UNIX_STANDARD_SESSION_SERVICEDIR "/dbus-1/services"
1274 #define DBUS_UNIX_STANDARD_SYSTEM_SERVICEDIR "/dbus-1/system-services"
1287 const char *xdg_runtime_dir;
1296 _DBUS_SET_OOM (error);
1303 _DBUS_SET_OOM (error);
1311 _DBUS_SET_OOM (error);
1318 if (xdg_runtime_dir ==
NULL)
1320 _dbus_verbose (
"XDG_RUNTIME_DIR is unset: transient session services "
1321 "not available here\n");
1332 _DBUS_SET_OOM (error);
1336 if (!ensure_owned_directory (
"XDG_RUNTIME_DIR", &xrd,
FALSE, error) ||
1337 !ensure_owned_directory (
"XDG_RUNTIME_DIR subdirectory", &dbus1,
TRUE,
1339 !ensure_owned_directory (
"XDG_RUNTIME_DIR subdirectory", &services,
1346 _DBUS_SET_OOM (error);
1350 _dbus_verbose (
"Transient service directory is %s\n", data);
1383 const char *xdg_data_home;
1384 const char *xdg_data_dirs;
1393 if (xdg_data_home !=
NULL)
1417 if (xdg_data_dirs !=
NULL)
1441 DBUS_UNIX_STANDARD_SESSION_SERVICEDIR,
1484 static const char standard_search_path[] =
1494 DBUS_UNIX_STANDARD_SYSTEM_SERVICEDIR,
1528 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
1540 _dbus_test_append_different_uid (
DBusString *uid)
1542 if (geteuid () == 0)
1558 _dbus_test_append_different_username (
DBusString *username)
1560 if (geteuid () == 0)