26 #include "dbus-sysdeps.h"
27 #include "dbus-sysdeps-unix.h"
28 #include "dbus-internals.h"
29 #include "dbus-pipe.h"
30 #include "dbus-protocol.h"
31 #include "dbus-string.h"
32 #define DBUS_USERDB_INCLUDES_PRIVATE 1
33 #include "dbus-userdb.h"
34 #include "dbus-test.h"
36 #include <sys/types.h>
45 #ifdef HAVE_SYS_RESOURCE_H
46 #include <sys/resource.h>
49 #include <sys/socket.h>
54 #ifdef HAVE_SYS_SYSLIMITS_H
55 #include <sys/syslimits.h>
87 _dbus_verbose (
"Becoming a daemon...\n");
89 _dbus_verbose (
"chdir to /\n");
93 "Could not chdir() to root directory");
97 _dbus_verbose (
"forking...\n");
98 switch ((child_pid = fork ()))
101 _dbus_verbose (
"fork failed\n");
103 "Failed to fork daemon: %s", _dbus_strerror (errno));
108 _dbus_verbose (
"in child, closing std file descriptors\n");
115 dev_null_fd = open (
"/dev/null", O_RDWR);
116 if (dev_null_fd >= 0)
118 dup2 (dev_null_fd, 0);
119 dup2 (dev_null_fd, 1);
122 if (s ==
NULL || *s ==
'\0')
123 dup2 (dev_null_fd, 2);
125 _dbus_verbose (
"keeping stderr open due to DBUS_DEBUG_OUTPUT\n");
132 _dbus_verbose (
"setting umask\n");
136 _dbus_verbose (
"calling setsid()\n");
146 _dbus_verbose (
"pid file or pipe write failed: %s\n",
148 kill (child_pid, SIGTERM);
152 _dbus_verbose (
"parent exiting\n");
170 _dbus_write_pid_file (
const DBusString *filename,
174 const char *cfilename;
180 fd = open (cfilename, O_WRONLY|O_CREAT|O_EXCL|O_BINARY, 0644);
185 "Failed to open \"%s\": %s", cfilename,
186 _dbus_strerror (errno));
190 if ((f = fdopen (fd,
"w")) ==
NULL)
193 "Failed to fdopen fd %d: %s", fd, _dbus_strerror (errno));
198 if (fprintf (f,
"%lu\n", pid) < 0)
201 "Failed to write to \"%s\": %s", cfilename,
202 _dbus_strerror (errno));
208 if (fclose (f) == EOF)
211 "Failed to close \"%s\": %s", cfilename,
212 _dbus_strerror (errno));
239 if (!_dbus_write_pid_file (pidfile,
243 _dbus_verbose (
"pid file write failed\n");
244 _DBUS_ASSERT_ERROR_IS_SET(error);
250 _dbus_verbose (
"No pid file requested\n");
253 if (print_pid_pipe !=
NULL && _dbus_pipe_is_valid (print_pid_pipe))
258 _dbus_verbose (
"writing our pid to pipe %d\n",
263 _DBUS_SET_OOM (error);
271 _DBUS_SET_OOM (error);
276 if (_dbus_pipe_write (print_pid_pipe, &pid, 0, bytes, error) != bytes)
282 "Printing message bus PID: did not write enough bytes\n");
292 _dbus_verbose (
"No pid pipe to write to\n");
316 #ifndef HAVE_LIBAUDIT
337 "User '%s' does not appear to exist?",
349 if (setgroups (0,
NULL) < 0)
350 _dbus_warn (
"Failed to drop supplementary groups: %s\n",
351 _dbus_strerror (errno));
356 if (setgid (gid) < 0)
359 "Failed to set GID to %lu: %s", gid,
360 _dbus_strerror (errno));
364 if (setuid (uid) < 0)
367 "Failed to set UID to %lu: %s", uid,
368 _dbus_strerror (errno));
390 #ifdef HAVE_SETRLIMIT
392 struct rlimit target_lim;
403 if (getrlimit (RLIMIT_NOFILE, &lim) < 0)
406 if (lim.rlim_cur >= limit)
412 target_lim.rlim_cur = target_lim.rlim_max = limit;
421 setrlimit (RLIMIT_NOFILE, &target_lim);
426 _dbus_init_system_log (
void)
428 #if HAVE_DECL_LOG_PERROR
429 openlog (
"dbus", LOG_PID | LOG_PERROR, LOG_DAEMON);
431 openlog (
"dbus", LOG_PID, LOG_DAEMON);
448 va_start (args, msg);
471 case DBUS_SYSTEM_LOG_INFO:
472 flags = LOG_DAEMON | LOG_NOTICE;
474 case DBUS_SYSTEM_LOG_SECURITY:
475 flags = LOG_AUTH | LOG_NOTICE;
477 case DBUS_SYSTEM_LOG_FATAL:
478 flags = LOG_DAEMON|LOG_CRIT;
484 #ifndef HAVE_DECL_LOG_PERROR
489 DBUS_VA_COPY (tmp, args);
491 vfprintf (stderr, msg, tmp);
492 fputc (
'\n', stderr);
497 vsyslog (flags, msg, args);
499 if (severity == DBUS_SYSTEM_LOG_FATAL)
512 struct sigaction act;
515 sigemptyset (&empty_mask);
516 act.sa_handler = handler;
517 act.sa_mask = empty_mask;
519 sigaction (sig, &act,
NULL);
530 return (access (file, F_OK) == 0);
550 _DBUS_SET_OOM (error);
556 _DBUS_SET_OOM (error);
564 _DBUS_SET_OOM (error);
605 const char *filename_c;
608 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
612 if (stat (filename_c, &sb) < 0)
615 "%s", _dbus_strerror (errno));
619 statbuf->
mode = sb.st_mode;
620 statbuf->
nlink = sb.st_nlink;
621 statbuf->
uid = sb.st_uid;
622 statbuf->
gid = sb.st_gid;
623 statbuf->
size = sb.st_size;
624 statbuf->
atime = sb.st_atime;
625 statbuf->
mtime = sb.st_mtime;
626 statbuf->
ctime = sb.st_ctime;
654 const char *filename_c;
656 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
660 d = opendir (filename_c);
664 "Failed to read directory \"%s\": %s",
666 _dbus_strerror (errno));
674 "Could not allocate memory for directory iterator");
704 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
708 ent = readdir (iter->
d);
717 "%s", _dbus_strerror (err));
721 else if (ent->d_name[0] ==
'.' &&
722 (ent->d_name[1] ==
'\0' ||
723 (ent->d_name[1] ==
'.' && ent->d_name[2] ==
'\0')))
731 "No memory to read directory entry");
752 fill_user_info_from_group (
struct group *g,
758 info->
gid = g->gr_gid;
778 const char *group_c_str;
793 #if defined (HAVE_POSIX_GETPWNAM_R) || defined (HAVE_NONPOSIX_GETPWNAM_R)
803 buflen = sysconf (_SC_GETGR_R_SIZE_MAX);
809 if ((
long) buflen <= 0)
823 #ifdef HAVE_POSIX_GETPWNAM_R
825 result = getgrnam_r (group_c_str, &g_str, buf, buflen,
828 result = getgrgid_r (gid, &g_str, buf, buflen,
831 g = getgrnam_r (group_c_str, &g_str, buf, buflen);
837 if (result == ERANGE && buflen < 512 * 1024)
848 if (result == 0 && g == &g_str)
850 b = fill_user_info_from_group (g, info, error);
857 "Group %s unknown or failed to look it up\n",
858 group_c_str ? group_c_str :
"???");
868 g = getgrnam (group_c_str);
872 return fill_user_info_from_group (g, info, error);
877 "Group %s unknown or failed to look it up\n",
878 group_c_str ? group_c_str :
"???");
918 return fill_group_info (info, gid,
NULL, error);
1078 for (i = 0; i < len; i++)
1080 unsigned char c = (
unsigned char) buf[i];
1083 else if (c < 0x20 || c > 127)
1115 _DBUS_SET_OOM (error);
1121 _DBUS_SET_OOM (error);
1134 "Failed to open \"%s\": %s",
1136 _dbus_strerror (errno));
1144 "Failed to read from \"%s\": %s",
1146 _dbus_strerror (errno));
1153 string_squash_nonprintable (&cmdline);
1162 _DBUS_SET_OOM (error);
dbus_bool_t _dbus_string_append(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString.
const char * message
public error message field
#define NULL
A null pointer, defined appropriately for C or C++.
dbus_bool_t _dbus_become_daemon(const DBusString *pidfile, DBusPipe *print_pid_pipe, DBusError *error, dbus_bool_t keep_umask)
Does the chdir, fork, setsid, etc.
dbus_bool_t _dbus_unix_user_is_at_console(dbus_uid_t uid, DBusError *error)
Checks to see if the UNIX user ID is at the console.
dbus_bool_t _dbus_string_get_dirname(const DBusString *filename, DBusString *dirname)
Get the directory name from a complete filename.
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
dbus_bool_t _dbus_path_is_absolute(const DBusString *filename)
Checks whether the filename is an absolute path.
void _dbus_system_log(DBusSystemLogSeverity severity, const char *msg,...)
Log a message to the system log file (e.g.
Portable struct with stat() results.
dbus_bool_t _dbus_string_append_int(DBusString *str, long value)
Appends an integer to a DBusString.
dbus_bool_t _dbus_groups_from_uid(dbus_uid_t uid, dbus_gid_t **group_ids, int *n_group_ids)
Gets all groups corresponding to the given UID.
#define DBUS_PID_FORMAT
an appropriate printf format for dbus_pid_t
dbus_bool_t _dbus_parse_unix_group_from_config(const DBusString *groupname, dbus_gid_t *gid_p)
Parse a UNIX group from the bus config file.
void _dbus_directory_close(DBusDirIter *iter)
Closes a directory iteration.
dbus_bool_t _dbus_is_console_user(dbus_uid_t uid, DBusError *error)
Checks to see if the UID sent in is the console user.
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
dbus_bool_t _dbus_directory_get_next_file(DBusDirIter *iter, DBusString *filename, DBusError *error)
Get next file in the directory.
unsigned long atime
Access time.
unsigned char _dbus_string_get_byte(const DBusString *str, int start)
Gets the byte at the given position.
dbus_bool_t _dbus_file_exists(const char *file)
File interface.
DBusDirIter * _dbus_directory_open(const DBusString *filename, DBusError *error)
Open a directory to iterate over.
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
dbus_pid_t _dbus_getpid(void)
Gets our process ID.
dbus_bool_t _dbus_command_for_pid(unsigned long pid, DBusString *str, int max_len, DBusError *error)
Get a printable string describing the command used to execute the process with pid.
dbus_bool_t _dbus_string_copy(const DBusString *source, int start, DBusString *dest, int insert_at)
Like _dbus_string_move(), but does not delete the section of the source string that's copied to the d...
char * groupname
Group name.
const char * _dbus_getenv(const char *varname)
Wrapper for getenv().
const char * _dbus_error_from_errno(int error_number)
Converts a UNIX errno, or Windows errno or WinSock error value into a DBusError name.
Internals of directory iterator.
unsigned long mode
File mode.
unsigned long dbus_pid_t
A process ID.
dbus_bool_t _dbus_get_user_id_and_primary_group(const DBusString *username, dbus_uid_t *uid_p, dbus_gid_t *gid_p)
Gets user ID and primary group given username.
dbus_bool_t _dbus_change_to_daemon_user(const char *user, DBusError *error)
Changes the user and group the bus is running as.
void * dbus_malloc(size_t bytes)
Allocates the given number of bytes, as with standard malloc().
dbus_gid_t gid
Group owning file.
dbus_bool_t _dbus_concat_dir_and_file(DBusString *dir, const DBusString *next_component)
Appends the given filename to the given directory.
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
char * _dbus_string_get_data(DBusString *str)
Gets the raw character buffer from the string.
DIR * d
The DIR* from opendir()
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
dbus_bool_t _dbus_group_info_fill_gid(DBusGroupInfo *info, dbus_gid_t gid, DBusError *error)
Initializes the given DBusGroupInfo struct with information about the given group ID...
dbus_bool_t _dbus_string_append_printf(DBusString *str, const char *format,...)
Appends a printf-style formatted string to the DBusString.
int _dbus_string_get_length(const DBusString *str)
Gets the length of a string (not including nul termination).
void(* DBusSignalHandler)(int sig)
A UNIX signal handler.
dbus_bool_t _dbus_get_group_id(const DBusString *groupname, dbus_gid_t *gid)
Gets group ID given groupname.
Object representing an exception.
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
dbus_bool_t _dbus_unix_groups_from_uid(dbus_uid_t uid, dbus_gid_t **group_ids, int *n_group_ids)
Gets all groups corresponding to the given UNIX user ID.
unsigned long ctime
Creation time.
dbus_bool_t _dbus_close(int fd, DBusError *error)
Closes a file descriptor.
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init().
#define DBUS_GID_UNSET
an invalid GID used to represent an uninitialized dbus_gid_t field
#define TRUE
Expands to "1".
unsigned long nlink
Number of hard links.
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
dbus_bool_t _dbus_write_pid_to_file_and_pipe(const DBusString *pidfile, DBusPipe *print_pid_pipe, dbus_pid_t pid_to_write, DBusError *error)
Writes the given pid_to_write to a pidfile (if non-NULL) and/or to a pipe (if non-NULL).
dbus_bool_t _dbus_group_info_fill(DBusGroupInfo *info, const DBusString *groupname, DBusError *error)
Initializes the given DBusGroupInfo struct with information about the given group name...
dbus_uid_t uid
User owning file.
void _dbus_system_logv(DBusSystemLogSeverity severity, const char *msg, va_list args)
Log a message to the system log file (e.g.
#define DBUS_ERROR_FAILED
A generic error; "something went wrong" - see the error message for more.
dbus_bool_t _dbus_verify_daemon_user(const char *user)
Verify that after the fork we can successfully change to this user.
dbus_bool_t _dbus_string_find_byte_backward(const DBusString *str, int start, unsigned char byte, int *found)
Find the given byte scanning backward from the given start.
Information about a UNIX group.
dbus_bool_t _dbus_stat(const DBusString *filename, DBusStat *statbuf, DBusError *error)
stat() wrapper.
dbus_bool_t _dbus_get_user_id(const DBusString *username, dbus_uid_t *uid)
Gets user ID given username.
void _dbus_set_signal_handler(int sig, DBusSignalHandler handler)
Installs a UNIX signal handler.
dbus_bool_t _dbus_unix_user_is_process_owner(dbus_uid_t uid)
Checks to see if the UNIX user ID matches the UID of the process.
dbus_bool_t _dbus_user_at_console(const char *username, DBusError *error)
Checks if user is at the console.
dbus_bool_t _dbus_windows_user_is_process_owner(const char *windows_sid)
Checks to see if the Windows user SID matches the owner of the process.
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
#define FALSE
Expands to "0".
unsigned long mtime
Modify time.
dbus_bool_t _dbus_string_set_length(DBusString *str, int length)
Sets the length of a string.
dbus_bool_t _dbus_string_copy_len(const DBusString *source, int start, int len, DBusString *dest, int insert_at)
Like _dbus_string_copy(), but can copy a segment from the middle of the source string.
dbus_uid_t _dbus_geteuid(void)
Gets our effective UID.
unsigned long dbus_gid_t
A group ID.
unsigned long size
Size of file.
dbus_bool_t _dbus_parse_unix_user_from_config(const DBusString *username, dbus_uid_t *uid_p)
Parse a UNIX user from the bus config file.
char * _dbus_strdup(const char *str)
Duplicates a string.
unsigned long dbus_uid_t
A user ID.
const char * _dbus_string_get_const_data(const DBusString *str)
Gets the raw character buffer from a const string.
void _dbus_request_file_descriptor_limit(unsigned int limit)
Attempt to ensure that the current process can open at least file descriptors.
int _dbus_read(int fd, DBusString *buffer, int count)
Thin wrapper around the read() system call that appends the data it reads to the DBusString buffer...
dbus_bool_t dbus_error_is_set(const DBusError *error)
Checks whether an error occurred (the error is set).