D-Bus  1.12.24
dbus-sysdeps-util-unix.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-sysdeps-util-unix.c Would be in dbus-sysdeps-unix.c, but not used in libdbus
3  *
4  * Copyright (C) 2002, 2003, 2004, 2005 Red Hat, Inc.
5  * Copyright (C) 2003 CodeFactory AB
6  *
7  * Licensed under the Academic Free License version 2.1
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 #include <config.h>
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"
36 
37 #include <sys/types.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <signal.h>
41 #include <unistd.h>
42 #include <stdio.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <sys/stat.h>
46 #ifdef HAVE_SYS_RESOURCE_H
47 #include <sys/resource.h>
48 #endif
49 #include <grp.h>
50 #include <sys/socket.h>
51 #include <dirent.h>
52 #include <sys/un.h>
53 
54 #ifdef HAVE_SYS_SYSLIMITS_H
55 #include <sys/syslimits.h>
56 #endif
57 
58 #ifdef HAVE_SYSTEMD
59 #include <systemd/sd-daemon.h>
60 #endif
61 
62 #ifdef HAVE_PDPLINUX
63 #include <sys/capability.h>
64 #endif
65 
66 #ifndef O_BINARY
67 #define O_BINARY 0
68 #endif
69 
87  DBusPipe *print_pid_pipe,
88  DBusError *error,
89  dbus_bool_t keep_umask)
90 {
91  const char *s;
92  pid_t child_pid;
93  DBusEnsureStandardFdsFlags flags;
94 
95  _dbus_verbose ("Becoming a daemon...\n");
96 
97  _dbus_verbose ("chdir to /\n");
98  if (chdir ("/") < 0)
99  {
101  "Could not chdir() to root directory");
102  return FALSE;
103  }
104 
105  _dbus_verbose ("forking...\n");
106  switch ((child_pid = fork ()))
107  {
108  case -1:
109  _dbus_verbose ("fork failed\n");
110  dbus_set_error (error, _dbus_error_from_errno (errno),
111  "Failed to fork daemon: %s", _dbus_strerror (errno));
112  return FALSE;
113  break;
114 
115  case 0:
116  _dbus_verbose ("in child, closing std file descriptors\n");
117 
118  flags = DBUS_FORCE_STDIN_NULL | DBUS_FORCE_STDOUT_NULL;
119  s = _dbus_getenv ("DBUS_DEBUG_OUTPUT");
120 
121  if (s == NULL || *s == '\0')
122  flags |= DBUS_FORCE_STDERR_NULL;
123  else
124  _dbus_verbose ("keeping stderr open due to DBUS_DEBUG_OUTPUT\n");
125 
126  if (!_dbus_ensure_standard_fds (flags, &s))
127  {
128  _dbus_warn ("%s: %s", s, _dbus_strerror (errno));
129  _exit (1);
130  }
131 
132  if (!keep_umask)
133  {
134  /* Get a predictable umask */
135  _dbus_verbose ("setting umask\n");
136  umask (022);
137  }
138 
139  _dbus_verbose ("calling setsid()\n");
140  if (setsid () == -1)
141  _dbus_assert_not_reached ("setsid() failed");
142 
143  break;
144 
145  default:
146  if (!_dbus_write_pid_to_file_and_pipe (pidfile, print_pid_pipe,
147  child_pid, error))
148  {
149  _dbus_verbose ("pid file or pipe write failed: %s\n",
150  error->message);
151  kill (child_pid, SIGTERM);
152  return FALSE;
153  }
154 
155  _dbus_verbose ("parent exiting\n");
156  _exit (0);
157  break;
158  }
159 
160  return TRUE;
161 }
162 
163 
172 static dbus_bool_t
173 _dbus_write_pid_file (const DBusString *filename,
174  unsigned long pid,
175  DBusError *error)
176 {
177  const char *cfilename;
178  int fd;
179  FILE *f;
180 
181  cfilename = _dbus_string_get_const_data (filename);
182 
183  fd = open (cfilename, O_WRONLY|O_CREAT|O_EXCL|O_BINARY, 0644);
184 
185  if (fd < 0)
186  {
187  dbus_set_error (error, _dbus_error_from_errno (errno),
188  "Failed to open \"%s\": %s", cfilename,
189  _dbus_strerror (errno));
190  return FALSE;
191  }
192 
193  if ((f = fdopen (fd, "w")) == NULL)
194  {
195  dbus_set_error (error, _dbus_error_from_errno (errno),
196  "Failed to fdopen fd %d: %s", fd, _dbus_strerror (errno));
197  _dbus_close (fd, NULL);
198  return FALSE;
199  }
200 
201  if (fprintf (f, "%lu\n", pid) < 0)
202  {
203  dbus_set_error (error, _dbus_error_from_errno (errno),
204  "Failed to write to \"%s\": %s", cfilename,
205  _dbus_strerror (errno));
206 
207  fclose (f);
208  return FALSE;
209  }
210 
211  if (fclose (f) == EOF)
212  {
213  dbus_set_error (error, _dbus_error_from_errno (errno),
214  "Failed to close \"%s\": %s", cfilename,
215  _dbus_strerror (errno));
216  return FALSE;
217  }
218 
219  return TRUE;
220 }
221 
235  DBusPipe *print_pid_pipe,
236  dbus_pid_t pid_to_write,
237  DBusError *error)
238 {
239  if (pidfile)
240  {
241  _dbus_verbose ("writing pid file %s\n", _dbus_string_get_const_data (pidfile));
242  if (!_dbus_write_pid_file (pidfile,
243  pid_to_write,
244  error))
245  {
246  _dbus_verbose ("pid file write failed\n");
247  _DBUS_ASSERT_ERROR_IS_SET(error);
248  return FALSE;
249  }
250  }
251  else
252  {
253  _dbus_verbose ("No pid file requested\n");
254  }
255 
256  if (print_pid_pipe != NULL && _dbus_pipe_is_valid (print_pid_pipe))
257  {
258  DBusString pid;
259  int bytes;
260 
261  _dbus_verbose ("writing our pid to pipe %d\n",
262  print_pid_pipe->fd);
263 
264  if (!_dbus_string_init (&pid))
265  {
266  _DBUS_SET_OOM (error);
267  return FALSE;
268  }
269 
270  if (!_dbus_string_append_int (&pid, pid_to_write) ||
271  !_dbus_string_append (&pid, "\n"))
272  {
273  _dbus_string_free (&pid);
274  _DBUS_SET_OOM (error);
275  return FALSE;
276  }
277 
278  bytes = _dbus_string_get_length (&pid);
279  if (_dbus_pipe_write (print_pid_pipe, &pid, 0, bytes, error) != bytes)
280  {
281  /* _dbus_pipe_write sets error only on failure, not short write */
282  if (error != NULL && !dbus_error_is_set(error))
283  {
285  "Printing message bus PID: did not write enough bytes\n");
286  }
287  _dbus_string_free (&pid);
288  return FALSE;
289  }
290 
291  _dbus_string_free (&pid);
292  }
293  else
294  {
295  _dbus_verbose ("No pid pipe to write to\n");
296  }
297 
298  return TRUE;
299 }
300 
308 _dbus_verify_daemon_user (const char *user)
309 {
310  DBusString u;
311 
312  _dbus_string_init_const (&u, user);
313 
315 }
316 
317 
318 /* The HAVE_LIBAUDIT case lives in selinux.c */
319 #ifndef HAVE_LIBAUDIT
320 
328 _dbus_change_to_daemon_user (const char *user,
329  DBusError *error)
330 {
331  dbus_uid_t uid;
332  dbus_gid_t gid;
333  DBusString u;
334 
335  _dbus_string_init_const (&u, user);
336 
337  if (!_dbus_get_user_id_and_primary_group (&u, &uid, &gid))
338  {
340  "User '%s' does not appear to exist?",
341  user);
342  return FALSE;
343  }
344 
345  /* setgroups() only works if we are a privileged process,
346  * so we don't return error on failure; the only possible
347  * failure is that we don't have perms to do it.
348  *
349  * not sure this is right, maybe if setuid()
350  * is going to work then setgroups() should also work.
351  */
352  if (setgroups (0, NULL) < 0)
353  _dbus_warn ("Failed to drop supplementary groups: %s",
354  _dbus_strerror (errno));
355 
356  /* Set GID first, or the setuid may remove our permission
357  * to change the GID
358  */
359  if (setgid (gid) < 0)
360  {
361  dbus_set_error (error, _dbus_error_from_errno (errno),
362  "Failed to set GID to %lu: %s", gid,
363  _dbus_strerror (errno));
364  return FALSE;
365  }
366 
367  if (setuid (uid) < 0)
368  {
369  dbus_set_error (error, _dbus_error_from_errno (errno),
370  "Failed to set UID to %lu: %s", uid,
371  _dbus_strerror (errno));
372  return FALSE;
373  }
374 
375 #ifdef HAVE_PDPLINUX
376  if (uid == 0){
377  /* Add special capabilities to read /proc for process name detection
378  * when Not enabled HAVE_LIBAUDIT and started by root.
379  */
380  cap_t capsproc=NULL;
381  cap_value_t cap_val=0;
382  capsproc=cap_get_proc();
383 
384  // Set all caps from CAP_INHERITABLE but needed only CAP_SYS_ADMIN CAP_DAC_OVERRIDE CAP_SYS_PTRACE
385  // Set all CAP_INHERITABLE caps (which setted by systemd daemon) to CAP_EFFECTIVE
386 
387  while (cap_val<=CAP_LAST_CAP){
388  cap_flag_value_t val;
389 
390  if (0==cap_get_flag(capsproc,
391  cap_val,
392  CAP_INHERITABLE,
393  &val)){
394  cap_set_flag(capsproc, CAP_EFFECTIVE, 1, &cap_val, val);
395  }
396 
397  cap_val++;
398  }
399 
400  cap_set_proc(capsproc);
401 
402  if (capsproc) cap_free(capsproc);
403  }
404 #endif
405 
406  return TRUE;
407 }
408 #endif /* !HAVE_LIBAUDIT */
409 
410 #ifdef HAVE_SETRLIMIT
411 
412 /* We assume that if we have setrlimit, we also have getrlimit and
413  * struct rlimit.
414  */
415 
416 struct DBusRLimit {
417  struct rlimit lim;
418 };
419 
420 DBusRLimit *
421 _dbus_rlimit_save_fd_limit (DBusError *error)
422 {
423  DBusRLimit *self;
424 
425  self = dbus_new0 (DBusRLimit, 1);
426 
427  if (self == NULL)
428  {
429  _DBUS_SET_OOM (error);
430  return NULL;
431  }
432 
433  if (getrlimit (RLIMIT_NOFILE, &self->lim) < 0)
434  {
435  dbus_set_error (error, _dbus_error_from_errno (errno),
436  "Failed to get fd limit: %s", _dbus_strerror (errno));
437  dbus_free (self);
438  return NULL;
439  }
440 
441  return self;
442 }
443 
444 /* Enough fds that we shouldn't run out, even if several uids work
445  * together to carry out a denial-of-service attack. This happens to be
446  * the same number that systemd < 234 would normally use. */
447 #define ENOUGH_FDS 65536
448 
450 _dbus_rlimit_raise_fd_limit (DBusError *error)
451 {
452  struct rlimit old, lim;
453 
454  if (getrlimit (RLIMIT_NOFILE, &lim) < 0)
455  {
456  dbus_set_error (error, _dbus_error_from_errno (errno),
457  "Failed to get fd limit: %s", _dbus_strerror (errno));
458  return FALSE;
459  }
460 
461  old = lim;
462 
463  if (getuid () == 0)
464  {
465  /* We are privileged, so raise the soft limit to at least
466  * ENOUGH_FDS, and the hard limit to at least the desired soft
467  * limit. This assumes we can exercise CAP_SYS_RESOURCE on Linux,
468  * or other OSs' equivalents. */
469  if (lim.rlim_cur != RLIM_INFINITY &&
470  lim.rlim_cur < ENOUGH_FDS)
471  lim.rlim_cur = ENOUGH_FDS;
472 
473  if (lim.rlim_max != RLIM_INFINITY &&
474  lim.rlim_max < lim.rlim_cur)
475  lim.rlim_max = lim.rlim_cur;
476  }
477 
478  /* Raise the soft limit to match the hard limit, which we can do even
479  * if we are unprivileged. In particular, systemd >= 240 will normally
480  * set rlim_cur to 1024 and rlim_max to 512*1024, recent Debian
481  * versions end up setting rlim_cur to 1024 and rlim_max to 1024*1024,
482  * and older and non-systemd Linux systems would typically set rlim_cur
483  * to 1024 and rlim_max to 4096. */
484  if (lim.rlim_max == RLIM_INFINITY || lim.rlim_cur < lim.rlim_max)
485  {
486 #if defined(__APPLE__) && defined(__MACH__)
487  /* macOS 10.5 and above no longer allows RLIM_INFINITY for rlim_cur */
488  lim.rlim_cur = MIN (OPEN_MAX, lim.rlim_max);
489 #else
490  lim.rlim_cur = lim.rlim_max;
491 #endif
492  }
493 
494  /* Early-return if there is nothing to do. */
495  if (lim.rlim_max == old.rlim_max &&
496  lim.rlim_cur == old.rlim_cur)
497  return TRUE;
498 
499  if (setrlimit (RLIMIT_NOFILE, &lim) < 0)
500  {
501  dbus_set_error (error, _dbus_error_from_errno (errno),
502  "Failed to set fd limit to %lu: %s",
503  (unsigned long) lim.rlim_cur,
504  _dbus_strerror (errno));
505  return FALSE;
506  }
507 
508  return TRUE;
509 }
510 
512 _dbus_rlimit_restore_fd_limit (DBusRLimit *saved,
513  DBusError *error)
514 {
515  if (setrlimit (RLIMIT_NOFILE, &saved->lim) < 0)
516  {
517  dbus_set_error (error, _dbus_error_from_errno (errno),
518  "Failed to restore old fd limit: %s",
519  _dbus_strerror (errno));
520  return FALSE;
521  }
522 
523  return TRUE;
524 }
525 
526 #else /* !HAVE_SETRLIMIT */
527 
528 static void
529 fd_limit_not_supported (DBusError *error)
530 {
532  "cannot change fd limit on this platform");
533 }
534 
535 DBusRLimit *
536 _dbus_rlimit_save_fd_limit (DBusError *error)
537 {
538  fd_limit_not_supported (error);
539  return NULL;
540 }
541 
543 _dbus_rlimit_raise_fd_limit (DBusError *error)
544 {
545  fd_limit_not_supported (error);
546  return FALSE;
547 }
548 
550 _dbus_rlimit_restore_fd_limit (DBusRLimit *saved,
551  DBusError *error)
552 {
553  fd_limit_not_supported (error);
554  return FALSE;
555 }
556 
557 #endif
558 
559 void
560 _dbus_rlimit_free (DBusRLimit *lim)
561 {
562  dbus_free (lim);
563 }
564 
570 void
572  DBusSignalHandler handler)
573 {
574  struct sigaction act;
575  sigset_t empty_mask;
576 
577  sigemptyset (&empty_mask);
578  act.sa_handler = handler;
579  act.sa_mask = empty_mask;
580  act.sa_flags = 0;
581  sigaction (sig, &act, NULL);
582 }
583 
590 _dbus_file_exists (const char *file)
591 {
592  return (access (file, F_OK) == 0);
593 }
594 
602 _dbus_user_at_console (const char *username,
603  DBusError *error)
604 {
605 #ifdef DBUS_CONSOLE_AUTH_DIR
606  DBusString u, f;
607  dbus_bool_t result;
608 
609  result = FALSE;
610  if (!_dbus_string_init (&f))
611  {
612  _DBUS_SET_OOM (error);
613  return FALSE;
614  }
615 
616  if (!_dbus_string_append (&f, DBUS_CONSOLE_AUTH_DIR))
617  {
618  _DBUS_SET_OOM (error);
619  goto out;
620  }
621 
622  _dbus_string_init_const (&u, username);
623 
624  if (!_dbus_concat_dir_and_file (&f, &u))
625  {
626  _DBUS_SET_OOM (error);
627  goto out;
628  }
629 
630  result = _dbus_file_exists (_dbus_string_get_const_data (&f));
631 
632  out:
633  _dbus_string_free (&f);
634 
635  return result;
636 #else
637  return FALSE;
638 #endif
639 }
640 
641 
650 {
651  if (_dbus_string_get_length (filename) > 0)
652  return _dbus_string_get_byte (filename, 0) == '/';
653  else
654  return FALSE;
655 }
656 
666 _dbus_stat (const DBusString *filename,
667  DBusStat *statbuf,
668  DBusError *error)
669 {
670  const char *filename_c;
671  struct stat sb;
672 
673  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
674 
675  filename_c = _dbus_string_get_const_data (filename);
676 
677  if (stat (filename_c, &sb) < 0)
678  {
679  dbus_set_error (error, _dbus_error_from_errno (errno),
680  "%s", _dbus_strerror (errno));
681  return FALSE;
682  }
683 
684  statbuf->mode = sb.st_mode;
685  statbuf->nlink = sb.st_nlink;
686  statbuf->uid = sb.st_uid;
687  statbuf->gid = sb.st_gid;
688  statbuf->size = sb.st_size;
689  statbuf->atime = sb.st_atime;
690  statbuf->mtime = sb.st_mtime;
691  statbuf->ctime = sb.st_ctime;
692 
693  return TRUE;
694 }
695 
696 
701 {
702  DIR *d;
704 };
705 
715  DBusError *error)
716 {
717  DIR *d;
718  DBusDirIter *iter;
719  const char *filename_c;
720 
721  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
722 
723  filename_c = _dbus_string_get_const_data (filename);
724 
725  d = opendir (filename_c);
726  if (d == NULL)
727  {
728  dbus_set_error (error, _dbus_error_from_errno (errno),
729  "Failed to read directory \"%s\": %s",
730  filename_c,
731  _dbus_strerror (errno));
732  return NULL;
733  }
734  iter = dbus_new0 (DBusDirIter, 1);
735  if (iter == NULL)
736  {
737  closedir (d);
739  "Could not allocate memory for directory iterator");
740  return NULL;
741  }
742 
743  iter->d = d;
744 
745  return iter;
746 }
747 
763  DBusString *filename,
764  DBusError *error)
765 {
766  struct dirent *ent;
767  int err;
768 
769  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
770 
771  again:
772  errno = 0;
773  ent = readdir (iter->d);
774 
775  if (!ent)
776  {
777  err = errno;
778 
779  if (err != 0)
780  dbus_set_error (error,
782  "%s", _dbus_strerror (err));
783 
784  return FALSE;
785  }
786  else if (ent->d_name[0] == '.' &&
787  (ent->d_name[1] == '\0' ||
788  (ent->d_name[1] == '.' && ent->d_name[2] == '\0')))
789  goto again;
790  else
791  {
792  _dbus_string_set_length (filename, 0);
793  if (!_dbus_string_append (filename, ent->d_name))
794  {
796  "No memory to read directory entry");
797  return FALSE;
798  }
799  else
800  {
801  return TRUE;
802  }
803  }
804 }
805 
809 void
811 {
812  closedir (iter->d);
813  dbus_free (iter);
814 }
815 
816 static dbus_bool_t
817 fill_user_info_from_group (struct group *g,
818  DBusGroupInfo *info,
819  DBusError *error)
820 {
821  _dbus_assert (g->gr_name != NULL);
822 
823  info->gid = g->gr_gid;
824  info->groupname = _dbus_strdup (g->gr_name);
825 
826  /* info->members = dbus_strdupv (g->gr_mem) */
827 
828  if (info->groupname == NULL)
829  {
831  return FALSE;
832  }
833 
834  return TRUE;
835 }
836 
837 static dbus_bool_t
838 fill_group_info (DBusGroupInfo *info,
839  dbus_gid_t gid,
840  const DBusString *groupname,
841  DBusError *error)
842 {
843  const char *group_c_str;
844 
845  _dbus_assert (groupname != NULL || gid != DBUS_GID_UNSET);
846  _dbus_assert (groupname == NULL || gid == DBUS_GID_UNSET);
847 
848  if (groupname)
849  group_c_str = _dbus_string_get_const_data (groupname);
850  else
851  group_c_str = NULL;
852 
853  /* For now assuming that the getgrnam() and getgrgid() flavors
854  * always correspond to the pwnam flavors, if not we have
855  * to add more configure checks.
856  */
857 
858 #if defined (HAVE_POSIX_GETPWNAM_R) || defined (HAVE_NONPOSIX_GETPWNAM_R)
859  {
860  struct group *g;
861  int result;
862  size_t buflen;
863  char *buf;
864  struct group g_str;
865  dbus_bool_t b;
866 
867  /* retrieve maximum needed size for buf */
868  buflen = sysconf (_SC_GETGR_R_SIZE_MAX);
869 
870  /* sysconf actually returns a long, but everything else expects size_t,
871  * so just recast here.
872  * https://bugs.freedesktop.org/show_bug.cgi?id=17061
873  */
874  if ((long) buflen <= 0)
875  buflen = 1024;
876 
877  result = -1;
878  while (1)
879  {
880  buf = dbus_malloc (buflen);
881  if (buf == NULL)
882  {
884  return FALSE;
885  }
886 
887  g = NULL;
888 #ifdef HAVE_POSIX_GETPWNAM_R
889  if (group_c_str)
890  result = getgrnam_r (group_c_str, &g_str, buf, buflen,
891  &g);
892  else
893  result = getgrgid_r (gid, &g_str, buf, buflen,
894  &g);
895 #else
896  g = getgrnam_r (group_c_str, &g_str, buf, buflen);
897  result = 0;
898 #endif /* !HAVE_POSIX_GETPWNAM_R */
899  /* Try a bigger buffer if ERANGE was returned:
900  https://bugs.freedesktop.org/show_bug.cgi?id=16727
901  */
902  if (result == ERANGE && buflen < 512 * 1024)
903  {
904  dbus_free (buf);
905  buflen *= 2;
906  }
907  else
908  {
909  break;
910  }
911  }
912 
913  if (result == 0 && g == &g_str)
914  {
915  b = fill_user_info_from_group (g, info, error);
916  dbus_free (buf);
917  return b;
918  }
919  else
920  {
921  dbus_set_error (error, _dbus_error_from_errno (errno),
922  "Group %s unknown or failed to look it up\n",
923  group_c_str ? group_c_str : "???");
924  dbus_free (buf);
925  return FALSE;
926  }
927  }
928 #else /* ! HAVE_GETPWNAM_R */
929  {
930  /* I guess we're screwed on thread safety here */
931  struct group *g;
932 
933  g = getgrnam (group_c_str);
934 
935  if (g != NULL)
936  {
937  return fill_user_info_from_group (g, info, error);
938  }
939  else
940  {
941  dbus_set_error (error, _dbus_error_from_errno (errno),
942  "Group %s unknown or failed to look it up\n",
943  group_c_str ? group_c_str : "???");
944  return FALSE;
945  }
946  }
947 #endif /* ! HAVE_GETPWNAM_R */
948 }
949 
961  const DBusString *groupname,
962  DBusError *error)
963 {
964  return fill_group_info (info, DBUS_GID_UNSET,
965  groupname, error);
966 
967 }
968 
980  dbus_gid_t gid,
981  DBusError *error)
982 {
983  return fill_group_info (info, gid, NULL, error);
984 }
985 
996  dbus_uid_t *uid_p)
997 {
998  return _dbus_get_user_id (username, uid_p);
999 
1000 }
1001 
1012  dbus_gid_t *gid_p)
1013 {
1014  return _dbus_get_group_id (groupname, gid_p);
1015 }
1016 
1029  dbus_gid_t **group_ids,
1030  int *n_group_ids)
1031 {
1032  return _dbus_groups_from_uid (uid, group_ids, n_group_ids);
1033 }
1034 
1046  DBusError *error)
1047 {
1048  return _dbus_is_console_user (uid, error);
1049 
1050 }
1051 
1061 {
1062  return uid == _dbus_geteuid ();
1063 }
1064 
1073 _dbus_windows_user_is_process_owner (const char *windows_sid)
1074 {
1075  return FALSE;
1076 }
1077  /* End of DBusInternalsUtils functions */
1079 
1093  DBusString *dirname)
1094 {
1095  int sep;
1096 
1097  _dbus_assert (filename != dirname);
1098  _dbus_assert (filename != NULL);
1099  _dbus_assert (dirname != NULL);
1100 
1101  /* Ignore any separators on the end */
1102  sep = _dbus_string_get_length (filename);
1103  if (sep == 0)
1104  return _dbus_string_append (dirname, "."); /* empty string passed in */
1105 
1106  while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
1107  --sep;
1108 
1109  _dbus_assert (sep >= 0);
1110 
1111  if (sep == 0)
1112  return _dbus_string_append (dirname, "/");
1113 
1114  /* Now find the previous separator */
1115  _dbus_string_find_byte_backward (filename, sep, '/', &sep);
1116  if (sep < 0)
1117  return _dbus_string_append (dirname, ".");
1118 
1119  /* skip multiple separators */
1120  while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
1121  --sep;
1122 
1123  _dbus_assert (sep >= 0);
1124 
1125  if (sep == 0 &&
1126  _dbus_string_get_byte (filename, 0) == '/')
1127  return _dbus_string_append (dirname, "/");
1128  else
1129  return _dbus_string_copy_len (filename, 0, sep - 0,
1130  dirname, _dbus_string_get_length (dirname));
1131 } /* DBusString stuff */
1133 
1134 static void
1135 string_squash_nonprintable (DBusString *str)
1136 {
1137  unsigned char *buf;
1138  int i, len;
1139 
1140  buf = _dbus_string_get_udata (str);
1141  len = _dbus_string_get_length (str);
1142 
1143  for (i = 0; i < len; i++)
1144  {
1145  unsigned char c = (unsigned char) buf[i];
1146  if (c == '\0')
1147  buf[i] = ' ';
1148  else if (c < 0x20 || c > 127)
1149  buf[i] = '?';
1150  }
1151 }
1152 
1167 dbus_bool_t
1168 _dbus_command_for_pid (unsigned long pid,
1169  DBusString *str,
1170  int max_len,
1171  DBusError *error)
1172 {
1173  /* This is all Linux-specific for now */
1174  DBusString path;
1175  DBusString cmdline;
1176  int fd;
1177 
1178  if (!_dbus_string_init (&path))
1179  {
1180  _DBUS_SET_OOM (error);
1181  return FALSE;
1182  }
1183 
1184  if (!_dbus_string_init (&cmdline))
1185  {
1186  _DBUS_SET_OOM (error);
1187  _dbus_string_free (&path);
1188  return FALSE;
1189  }
1190 
1191  if (!_dbus_string_append_printf (&path, "/proc/%ld/cmdline", pid))
1192  goto oom;
1193 
1194  fd = open (_dbus_string_get_const_data (&path), O_RDONLY);
1195  if (fd < 0)
1196  {
1197  dbus_set_error (error,
1198  _dbus_error_from_errno (errno),
1199  "Failed to open \"%s\": %s",
1200  _dbus_string_get_const_data (&path),
1201  _dbus_strerror (errno));
1202  goto fail;
1203  }
1204 
1205  if (!_dbus_read (fd, &cmdline, max_len))
1206  {
1207  dbus_set_error (error,
1208  _dbus_error_from_errno (errno),
1209  "Failed to read from \"%s\": %s",
1210  _dbus_string_get_const_data (&path),
1211  _dbus_strerror (errno));
1212  _dbus_close (fd, NULL);
1213  goto fail;
1214  }
1215 
1216  if (!_dbus_close (fd, error))
1217  goto fail;
1218 
1219  string_squash_nonprintable (&cmdline);
1220 
1221  if (!_dbus_string_copy (&cmdline, 0, str, _dbus_string_get_length (str)))
1222  goto oom;
1223 
1224  _dbus_string_free (&cmdline);
1225  _dbus_string_free (&path);
1226  return TRUE;
1227 oom:
1228  _DBUS_SET_OOM (error);
1229 fail:
1230  _dbus_string_free (&cmdline);
1231  _dbus_string_free (&path);
1232  return FALSE;
1233 }
1234 
1245 {
1246  return TRUE;
1247 }
1248 
1249 static dbus_bool_t
1250 ensure_owned_directory (const char *label,
1251  const DBusString *string,
1252  dbus_bool_t create,
1253  DBusError *error)
1254 {
1255  const char *dir = _dbus_string_get_const_data (string);
1256  struct stat buf;
1257 
1258  if (create && !_dbus_ensure_directory (string, error))
1259  return FALSE;
1260 
1261  /*
1262  * The stat()-based checks in this function are to protect against
1263  * mistakes, not malice. We are working in a directory that is meant
1264  * to be trusted; but if a user has used `su` or similar to escalate
1265  * their privileges without correctly clearing the environment, the
1266  * XDG_RUNTIME_DIR in the environment might still be the user's
1267  * and not root's. We don't want to write root-owned files into that
1268  * directory, so just warn and don't provide support for transient
1269  * services in that case.
1270  *
1271  * In particular, we use stat() and not lstat() so that if we later
1272  * decide to use a different directory name for transient services,
1273  * we can drop in a compatibility symlink without breaking older
1274  * libdbus.
1275  */
1276 
1277  if (stat (dir, &buf) != 0)
1278  {
1279  int saved_errno = errno;
1280 
1281  dbus_set_error (error, _dbus_error_from_errno (saved_errno),
1282  "%s \"%s\" not available: %s", label, dir,
1283  _dbus_strerror (saved_errno));
1284  return FALSE;
1285  }
1286 
1287  if (!S_ISDIR (buf.st_mode))
1288  {
1289  dbus_set_error (error, DBUS_ERROR_FAILED, "%s \"%s\" is not a directory",
1290  label, dir);
1291  return FALSE;
1292  }
1293 
1294  if (buf.st_uid != geteuid ())
1295  {
1297  "%s \"%s\" is owned by uid %ld, not our uid %ld",
1298  label, dir, (long) buf.st_uid, (long) geteuid ());
1299  return FALSE;
1300  }
1301 
1302  /* This is just because we have the stat() results already, so we might
1303  * as well check opportunistically. */
1304  if ((S_IWOTH | S_IWGRP) & buf.st_mode)
1305  {
1307  "%s \"%s\" can be written by others (mode 0%o)",
1308  label, dir, buf.st_mode);
1309  return FALSE;
1310  }
1311 
1312  return TRUE;
1313 }
1314 
1315 #define DBUS_UNIX_STANDARD_SESSION_SERVICEDIR "/dbus-1/services"
1316 #define DBUS_UNIX_STANDARD_SYSTEM_SERVICEDIR "/dbus-1/system-services"
1317 
1327  DBusError *error)
1328 {
1329  const char *xdg_runtime_dir;
1330  DBusString services;
1331  DBusString dbus1;
1332  DBusString xrd;
1333  dbus_bool_t ret = FALSE;
1334  char *data = NULL;
1335 
1336  if (!_dbus_string_init (&dbus1))
1337  {
1338  _DBUS_SET_OOM (error);
1339  return FALSE;
1340  }
1341 
1342  if (!_dbus_string_init (&services))
1343  {
1344  _dbus_string_free (&dbus1);
1345  _DBUS_SET_OOM (error);
1346  return FALSE;
1347  }
1348 
1349  if (!_dbus_string_init (&xrd))
1350  {
1351  _dbus_string_free (&dbus1);
1352  _dbus_string_free (&services);
1353  _DBUS_SET_OOM (error);
1354  return FALSE;
1355  }
1356 
1357  xdg_runtime_dir = _dbus_getenv ("XDG_RUNTIME_DIR");
1358 
1359  /* Not an error, we just can't have transient session services */
1360  if (xdg_runtime_dir == NULL)
1361  {
1362  _dbus_verbose ("XDG_RUNTIME_DIR is unset: transient session services "
1363  "not available here\n");
1364  ret = TRUE;
1365  goto out;
1366  }
1367 
1368  if (!_dbus_string_append (&xrd, xdg_runtime_dir) ||
1369  !_dbus_string_append_printf (&dbus1, "%s/dbus-1",
1370  xdg_runtime_dir) ||
1371  !_dbus_string_append_printf (&services, "%s/dbus-1/services",
1372  xdg_runtime_dir))
1373  {
1374  _DBUS_SET_OOM (error);
1375  goto out;
1376  }
1377 
1378  if (!ensure_owned_directory ("XDG_RUNTIME_DIR", &xrd, FALSE, error) ||
1379  !ensure_owned_directory ("XDG_RUNTIME_DIR subdirectory", &dbus1, TRUE,
1380  error) ||
1381  !ensure_owned_directory ("XDG_RUNTIME_DIR subdirectory", &services,
1382  TRUE, error))
1383  goto out;
1384 
1385  if (!_dbus_string_steal_data (&services, &data) ||
1386  !_dbus_list_append (dirs, data))
1387  {
1388  _DBUS_SET_OOM (error);
1389  goto out;
1390  }
1391 
1392  _dbus_verbose ("Transient service directory is %s\n", data);
1393  /* Ownership was transferred to @dirs */
1394  data = NULL;
1395  ret = TRUE;
1396 
1397 out:
1398  _dbus_string_free (&dbus1);
1399  _dbus_string_free (&services);
1400  _dbus_string_free (&xrd);
1401  dbus_free (data);
1402  return ret;
1403 }
1404 
1424 {
1425  const char *xdg_data_home;
1426  const char *xdg_data_dirs;
1427  DBusString servicedir_path;
1428 
1429  if (!_dbus_string_init (&servicedir_path))
1430  return FALSE;
1431 
1432  xdg_data_home = _dbus_getenv ("XDG_DATA_HOME");
1433  xdg_data_dirs = _dbus_getenv ("XDG_DATA_DIRS");
1434 
1435  if (xdg_data_home != NULL)
1436  {
1437  if (!_dbus_string_append (&servicedir_path, xdg_data_home))
1438  goto oom;
1439  }
1440  else
1441  {
1442  const DBusString *homedir;
1443  DBusString local_share;
1444 
1445  if (!_dbus_homedir_from_current_process (&homedir))
1446  goto oom;
1447 
1448  if (!_dbus_string_append (&servicedir_path, _dbus_string_get_const_data (homedir)))
1449  goto oom;
1450 
1451  _dbus_string_init_const (&local_share, "/.local/share");
1452  if (!_dbus_concat_dir_and_file (&servicedir_path, &local_share))
1453  goto oom;
1454  }
1455 
1456  if (!_dbus_string_append (&servicedir_path, ":"))
1457  goto oom;
1458 
1459  if (xdg_data_dirs != NULL)
1460  {
1461  if (!_dbus_string_append (&servicedir_path, xdg_data_dirs))
1462  goto oom;
1463 
1464  if (!_dbus_string_append (&servicedir_path, ":"))
1465  goto oom;
1466  }
1467  else
1468  {
1469  if (!_dbus_string_append (&servicedir_path, "/usr/local/share:/usr/share:"))
1470  goto oom;
1471  }
1472 
1473  /*
1474  * add configured datadir to defaults
1475  * this may be the same as an xdg dir
1476  * however the config parser should take
1477  * care of duplicates
1478  */
1479  if (!_dbus_string_append (&servicedir_path, DBUS_DATADIR))
1480  goto oom;
1481 
1482  if (!_dbus_split_paths_and_append (&servicedir_path,
1483  DBUS_UNIX_STANDARD_SESSION_SERVICEDIR,
1484  dirs))
1485  goto oom;
1486 
1487  _dbus_string_free (&servicedir_path);
1488  return TRUE;
1489 
1490  oom:
1491  _dbus_string_free (&servicedir_path);
1492  return FALSE;
1493 }
1494 
1495 
1516 {
1517  /*
1518  * DBUS_DATADIR may be the same as one of the standard directories. However,
1519  * the config parser should take care of the duplicates.
1520  *
1521  * Also, append /lib as counterpart of /usr/share on the root
1522  * directory (the root directory does not know /share), in order to
1523  * facilitate early boot system bus activation where /usr might not
1524  * be available.
1525  */
1526  static const char standard_search_path[] =
1527  "/usr/local/share:"
1528  "/usr/share:"
1529  DBUS_DATADIR ":"
1530  "/lib";
1531  DBusString servicedir_path;
1532 
1533  _dbus_string_init_const (&servicedir_path, standard_search_path);
1534 
1535  return _dbus_split_paths_and_append (&servicedir_path,
1536  DBUS_UNIX_STANDARD_SYSTEM_SERVICEDIR,
1537  dirs);
1538 }
1539 
1550 {
1551  _dbus_assert (_dbus_string_get_length (str) == 0);
1552 
1553  return _dbus_string_append (str, DBUS_SYSTEM_CONFIG_FILE);
1554 }
1555 
1564 {
1565  _dbus_assert (_dbus_string_get_length (str) == 0);
1566 
1567  return _dbus_string_append (str, DBUS_SESSION_CONFIG_FILE);
1568 }
1569 
1570 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
1571 
1572 /*
1573  * Set uid to a machine-readable authentication identity (numeric Unix
1574  * uid or ConvertSidToStringSid-style Windows SID) that is likely to exist,
1575  * and differs from the identity of the current process.
1576  *
1577  * @param uid Populated with a machine-readable authentication identity
1578  * on success
1579  * @returns #FALSE if no memory
1580  */
1582 _dbus_test_append_different_uid (DBusString *uid)
1583 {
1584  if (geteuid () == 0)
1585  return _dbus_string_append (uid, "65534");
1586  else
1587  return _dbus_string_append (uid, "0");
1588 }
1589 
1590 /*
1591  * Set uid to a human-readable authentication identity (login name)
1592  * that is likely to exist, and differs from the identity of the current
1593  * process. This function currently only exists on Unix platforms.
1594  *
1595  * @param uid Populated with a machine-readable authentication identity
1596  * on success
1597  * @returns #FALSE if no memory
1598  */
1600 _dbus_test_append_different_username (DBusString *username)
1601 {
1602  if (geteuid () == 0)
1603  return _dbus_string_append (username, "nobody");
1604  else
1605  return _dbus_string_append (username, "root");
1606 }
1607 
1608 #endif
1609 
1624 _dbus_reset_oom_score_adj (const char **error_str_p)
1625 {
1626 #ifdef __linux__
1627  int fd = -1;
1628  dbus_bool_t ret = FALSE;
1629  int saved_errno = 0;
1630  const char *error_str = NULL;
1631 
1632 #ifdef O_CLOEXEC
1633  fd = open ("/proc/self/oom_score_adj", O_RDWR | O_CLOEXEC);
1634 #endif
1635 
1636  if (fd < 0)
1637  {
1638  fd = open ("/proc/self/oom_score_adj", O_RDWR);
1640  }
1641 
1642  if (fd >= 0)
1643  {
1644  ssize_t read_result = -1;
1645  /* It doesn't actually matter whether we read the whole file,
1646  * as long as we get the presence or absence of the minus sign */
1647  char first_char = '\0';
1648 
1649  read_result = read (fd, &first_char, 1);
1650 
1651  if (read_result < 0)
1652  {
1653  /* This probably can't actually happen in practice: if we can
1654  * open it, then we can hopefully read from it */
1655  ret = FALSE;
1656  error_str = "failed to read from /proc/self/oom_score_adj";
1657  saved_errno = errno;
1658  goto out;
1659  }
1660 
1661  /* If we are running with protection from the OOM killer
1662  * (typical for the system dbus-daemon under systemd), then
1663  * oom_score_adj will be negative. Drop that protection,
1664  * returning to oom_score_adj = 0.
1665  *
1666  * Conversely, if we are running with increased susceptibility
1667  * to the OOM killer (as user sessions typically do in
1668  * systemd >= 250), oom_score_adj will be strictly positive,
1669  * and we are not allowed to decrease it to 0 without privileges.
1670  *
1671  * If it's exactly 0 (typical for non-systemd systems, and
1672  * user processes on older systemd) then there's no need to
1673  * alter it.
1674  *
1675  * We shouldn't get an empty result, but if we do, assume it
1676  * means zero and don't try to change it. */
1677  if (read_result == 0 || first_char != '-')
1678  {
1679  /* Nothing needs to be done: the OOM score adjustment is
1680  * non-negative */
1681  ret = TRUE;
1682  goto out;
1683  }
1684 
1685  if (pwrite (fd, "0", sizeof (char), 0) < 0)
1686  {
1687  ret = FALSE;
1688  error_str = "writing oom_score_adj error";
1689  saved_errno = errno;
1690  goto out;
1691  }
1692 
1693  /* Success */
1694  ret = TRUE;
1695  }
1696  else if (errno == ENOENT)
1697  {
1698  /* If /proc/self/oom_score_adj doesn't exist, assume the kernel
1699  * doesn't support this feature and ignore it. */
1700  ret = TRUE;
1701  }
1702  else
1703  {
1704  ret = FALSE;
1705  error_str = "open(/proc/self/oom_score_adj)";
1706  saved_errno = errno;
1707  goto out;
1708  }
1709 
1710 out:
1711  if (fd >= 0)
1712  _dbus_close (fd, NULL);
1713 
1714  if (error_str_p != NULL)
1715  *error_str_p = error_str;
1716 
1717  errno = saved_errno;
1718  return ret;
1719 #else
1720  /* nothing to do on this platform */
1721  return TRUE;
1722 #endif
1723 }
_dbus_get_session_config_file
dbus_bool_t _dbus_get_session_config_file(DBusString *str)
Get the absolute path of the session.conf file.
Definition: dbus-sysdeps-util-unix.c:1563
_dbus_ensure_standard_fds
dbus_bool_t _dbus_ensure_standard_fds(DBusEnsureStandardFdsFlags flags, const char **error_str_p)
Ensure that the standard file descriptors stdin, stdout and stderr are open, by opening /dev/null if ...
Definition: dbus-sysdeps-unix.c:159
_dbus_concat_dir_and_file
dbus_bool_t _dbus_concat_dir_and_file(DBusString *dir, const DBusString *next_component)
Appends the given filename to the given directory.
Definition: dbus-sysdeps-unix.c:3287
_dbus_set_up_transient_session_servicedirs
dbus_bool_t _dbus_set_up_transient_session_servicedirs(DBusList **dirs, DBusError *error)
Returns the standard directories for a session bus to look for transient service activation files.
Definition: dbus-sysdeps-util-unix.c:1326
_dbus_get_system_config_file
dbus_bool_t _dbus_get_system_config_file(DBusString *str)
Get the absolute path of the system.conf file (there is no system bus on Windows so this can just ret...
Definition: dbus-sysdeps-util-unix.c:1549
_dbus_group_info_fill_gid
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.
Definition: dbus-sysdeps-util-unix.c:979
_dbus_string_free
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init().
Definition: dbus-string.c:259
DBusStat::uid
dbus_uid_t uid
User owning file.
Definition: dbus-sysdeps.h:537
_dbus_get_standard_system_servicedirs
dbus_bool_t _dbus_get_standard_system_servicedirs(DBusList **dirs)
Returns the standard directories for a system bus to look for service activation files.
Definition: dbus-sysdeps-util-unix.c:1515
_dbus_stat
dbus_bool_t _dbus_stat(const DBusString *filename, DBusStat *statbuf, DBusError *error)
stat() wrapper.
Definition: dbus-sysdeps-util-unix.c:666
_dbus_string_append_int
DBUS_PRIVATE_EXPORT dbus_bool_t _dbus_string_append_int(DBusString *str, long value)
Appends an integer to a DBusString.
Definition: dbus-sysdeps.c:356
_dbus_error_from_errno
const char * _dbus_error_from_errno(int error_number)
Converts a UNIX errno, or Windows errno or WinSock error value into a DBusError name.
Definition: dbus-sysdeps.c:592
_dbus_replace_install_prefix
dbus_bool_t _dbus_replace_install_prefix(DBusString *path)
Replace the DBUS_PREFIX in the given path, in-place, by the current D-Bus installation directory.
Definition: dbus-sysdeps-util-unix.c:1244
_dbus_directory_close
void _dbus_directory_close(DBusDirIter *iter)
Closes a directory iteration.
Definition: dbus-sysdeps-util-unix.c:810
_dbus_string_find_byte_backward
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.
Definition: dbus-string-util.c:90
DBusStat
Portable struct with stat() results.
Definition: dbus-sysdeps.h:534
_dbus_get_user_id_and_primary_group
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.
Definition: dbus-userdb-util.c:208
DBusGroupInfo::gid
dbus_gid_t gid
GID.
Definition: dbus-sysdeps-unix.h:123
_dbus_reset_oom_score_adj
dbus_bool_t _dbus_reset_oom_score_adj(const char **error_str_p)
If the current process has been protected from the Linux OOM killer (the oom_score_adj process parame...
Definition: dbus-sysdeps-util-unix.c:1624
_dbus_string_copy
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...
Definition: dbus-string.c:1283
DBusPipe
Definition: dbus-pipe.h:41
_dbus_group_info_fill
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.
Definition: dbus-sysdeps-util-unix.c:960
DBusStat::ctime
unsigned long ctime
Creation time.
Definition: dbus-sysdeps.h:542
DBusGroupInfo
Information about a UNIX group.
Definition: dbus-sysdeps-unix.h:121
_dbus_path_is_absolute
dbus_bool_t _dbus_path_is_absolute(const DBusString *filename)
Checks whether the filename is an absolute path.
Definition: dbus-sysdeps-util-unix.c:649
dbus_gid_t
unsigned long dbus_gid_t
A group ID.
Definition: dbus-sysdeps.h:136
_dbus_string_init
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:175
_dbus_groups_from_uid
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.
Definition: dbus-userdb-util.c:412
_dbus_unix_user_is_process_owner
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.
Definition: dbus-sysdeps-util-unix.c:1060
_dbus_string_get_dirname
dbus_bool_t _dbus_string_get_dirname(const DBusString *filename, DBusString *dirname)
Get the directory name from a complete filename.
Definition: dbus-sysdeps-util-unix.c:1092
TRUE
#define TRUE
Expands to "1".
_dbus_list_append
dbus_bool_t _dbus_list_append(DBusList **list, void *data)
Appends a value to the list.
Definition: dbus-list.c:270
_dbus_user_at_console
dbus_bool_t _dbus_user_at_console(const char *username, DBusError *error)
Checks if user is at the console.
Definition: dbus-sysdeps-util-unix.c:602
DBUS_ERROR_FAILED
#define DBUS_ERROR_FAILED
A generic error; "something went wrong" - see the error message for more.
Definition: dbus-protocol.h:368
_dbus_change_to_daemon_user
dbus_bool_t _dbus_change_to_daemon_user(const char *user, DBusError *error)
Changes the user and group the bus is running as.
Definition: dbus-sysdeps-util-unix.c:328
DBusStat::atime
unsigned long atime
Access time.
Definition: dbus-sysdeps.h:540
_dbus_read
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.
Definition: dbus-sysdeps-unix.c:735
dbus_free
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:702
_dbus_verify_daemon_user
dbus_bool_t _dbus_verify_daemon_user(const char *user)
Verify that after the fork we can successfully change to this user.
Definition: dbus-sysdeps-util-unix.c:308
dbus_malloc
void * dbus_malloc(size_t bytes)
Allocates the given number of bytes, as with standard malloc().
Definition: dbus-memory.c:462
DBusDirIter::d
DIR * d
The DIR* from opendir()
Definition: dbus-sysdeps-util-unix.c:702
DBusString
Definition: dbus-string.h:43
DBusStat::mode
unsigned long mode
File mode.
Definition: dbus-sysdeps.h:535
_dbus_string_append_printf
dbus_bool_t _dbus_string_append_printf(DBusString *str, const char *format,...)
Appends a printf-style formatted string to the DBusString.
Definition: dbus-string.c:1114
_dbus_get_group_id
dbus_bool_t _dbus_get_group_id(const DBusString *groupname, dbus_gid_t *gid)
Gets group ID given groupname.
Definition: dbus-userdb-util.c:169
_dbus_unix_user_is_at_console
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.
Definition: dbus-sysdeps-util-unix.c:1045
dbus_pid_t
unsigned long dbus_pid_t
A process ID.
Definition: dbus-sysdeps.h:132
_dbus_split_paths_and_append
dbus_bool_t _dbus_split_paths_and_append(DBusString *dirs, const char *suffix, DBusList **dir_list)
Split paths into a list of char strings.
Definition: dbus-sysdeps.c:228
dbus_uid_t
unsigned long dbus_uid_t
A user ID.
Definition: dbus-sysdeps.h:134
FALSE
#define FALSE
Expands to "0".
DBUS_ERROR_NO_MEMORY
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
Definition: dbus-protocol.h:370
DBusStat::nlink
unsigned long nlink
Number of hard links.
Definition: dbus-sysdeps.h:536
_dbus_parse_unix_group_from_config
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.
Definition: dbus-sysdeps-util-unix.c:1011
_dbus_getenv
const char * _dbus_getenv(const char *varname)
Wrapper for getenv().
Definition: dbus-sysdeps.c:187
_dbus_is_console_user
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.
Definition: dbus-userdb-util.c:58
_dbus_directory_get_next_file
dbus_bool_t _dbus_directory_get_next_file(DBusDirIter *iter, DBusString *filename, DBusError *error)
Get next file in the directory.
Definition: dbus-sysdeps-util-unix.c:762
_dbus_fd_set_close_on_exec
void _dbus_fd_set_close_on_exec(int fd)
Sets the file descriptor to be close on exec.
Definition: dbus-sysdeps-unix.c:3459
_dbus_file_exists
dbus_bool_t _dbus_file_exists(const char *file)
Checks if a file exists.
Definition: dbus-sysdeps-util-unix.c:590
dbus_error_is_set
dbus_bool_t dbus_error_is_set(const DBusError *error)
Checks whether an error occurred (the error is set).
Definition: dbus-errors.c:329
_dbus_assert_not_reached
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
Definition: dbus-internals.h:163
_dbus_string_set_length
dbus_bool_t _dbus_string_set_length(DBusString *str, int length)
Sets the length of a string.
Definition: dbus-string.c:802
DBusStat::mtime
unsigned long mtime
Modify time.
Definition: dbus-sysdeps.h:541
DBusStat::gid
dbus_gid_t gid
Group owning file.
Definition: dbus-sysdeps.h:538
_dbus_command_for_pid
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.
Definition: dbus-sysdeps-util-unix.c:1168
_dbus_assert
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
Definition: dbus-internals.h:152
_dbus_close
dbus_bool_t _dbus_close(int fd, DBusError *error)
Closes a file descriptor.
Definition: dbus-sysdeps-unix.c:3481
_dbus_get_standard_session_servicedirs
dbus_bool_t _dbus_get_standard_session_servicedirs(DBusList **dirs)
Returns the standard directories for a session bus to look for service activation files.
Definition: dbus-sysdeps-util-unix.c:1423
_dbus_warn
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
Definition: dbus-internals.c:263
_dbus_geteuid
dbus_uid_t _dbus_geteuid(void)
Gets our effective UID.
Definition: dbus-sysdeps-unix.c:2948
_dbus_strdup
char * _dbus_strdup(const char *str)
Duplicates a string.
Definition: dbus-internals.c:644
_dbus_set_signal_handler
void _dbus_set_signal_handler(int sig, DBusSignalHandler handler)
Installs a UNIX signal handler.
Definition: dbus-sysdeps-util-unix.c:571
_dbus_unix_groups_from_uid
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.
Definition: dbus-sysdeps-util-unix.c:1028
DBusSignalHandler
void(* DBusSignalHandler)(int sig)
A UNIX signal handler.
Definition: dbus-sysdeps-unix.h:171
DBusError
Object representing an exception.
Definition: dbus-errors.h:49
DBusList
A node in a linked list.
Definition: dbus-list.h:35
_dbus_directory_open
DBusDirIter * _dbus_directory_open(const DBusString *filename, DBusError *error)
Open a directory to iterate over.
Definition: dbus-sysdeps-util-unix.c:714
_dbus_ensure_directory
dbus_bool_t _dbus_ensure_directory(const DBusString *filename, DBusError *error)
Creates a directory; succeeds if the directory is created or already existed.
Definition: dbus-sysdeps-unix.c:3224
_dbus_become_daemon
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.
Definition: dbus-sysdeps-util-unix.c:86
dbus_set_error
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
Definition: dbus-errors.c:354
_dbus_string_steal_data
dbus_bool_t _dbus_string_steal_data(DBusString *str, char **data_return)
Like _dbus_string_get_data(), but removes the gotten data from the original string.
Definition: dbus-string.c:641
_dbus_parse_unix_user_from_config
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.
Definition: dbus-sysdeps-util-unix.c:995
DBusError::message
const char * message
public error message field
Definition: dbus-errors.h:51
DBusDirIter
Internals of directory iterator.
Definition: dbus-sysdeps-util-unix.c:701
DBUS_GID_UNSET
#define DBUS_GID_UNSET
an invalid GID used to represent an uninitialized dbus_gid_t field
Definition: dbus-sysdeps.h:143
_dbus_get_user_id
dbus_bool_t _dbus_get_user_id(const DBusString *username, dbus_uid_t *uid)
Gets user ID given username.
Definition: dbus-userdb-util.c:155
_dbus_write_pid_to_file_and_pipe
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).
Definition: dbus-sysdeps-util-unix.c:234
_dbus_string_init_const
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
Definition: dbus-string.c:190
_dbus_string_copy_len
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.
Definition: dbus-string.c:1375
dbus_new0
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition: dbus-memory.h:58
DBUS_ERROR_NOT_SUPPORTED
#define DBUS_ERROR_NOT_SUPPORTED
Requested operation isn't supported (like ENOSYS on UNIX).
Definition: dbus-protocol.h:382
_dbus_homedir_from_current_process
dbus_bool_t _dbus_homedir_from_current_process(const DBusString **homedir)
Gets homedir of user owning current process.
Definition: dbus-userdb.c:447
_dbus_string_append
dbus_bool_t _dbus_string_append(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString.
Definition: dbus-string.c:935
DBusGroupInfo::groupname
char * groupname
Group name.
Definition: dbus-sysdeps-unix.h:124
_dbus_windows_user_is_process_owner
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.
Definition: dbus-sysdeps-util-unix.c:1073
DBusStat::size
unsigned long size
Size of file.
Definition: dbus-sysdeps.h:539
dbus_bool_t
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
NULL
#define NULL
A null pointer, defined appropriately for C or C++.