D-Bus  1.10.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-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"
35 
36 #include <sys/types.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <signal.h>
40 #include <unistd.h>
41 #include <stdio.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <sys/stat.h>
45 #ifdef HAVE_SYS_RESOURCE_H
46 #include <sys/resource.h>
47 #endif
48 #include <grp.h>
49 #include <sys/socket.h>
50 #include <dirent.h>
51 #include <sys/un.h>
52 
53 #ifdef HAVE_SYSLOG_H
54 #include <syslog.h>
55 #endif
56 
57 #ifdef HAVE_SYS_SYSLIMITS_H
58 #include <sys/syslimits.h>
59 #endif
60 
61 #ifdef HAVE_SYSTEMD
62 #include <systemd/sd-daemon.h>
63 #endif
64 
65 #ifndef O_BINARY
66 #define O_BINARY 0
67 #endif
68 
87  const char *working_dir,
88  DBusPipe *print_pid_pipe,
89  DBusError *error,
90  dbus_bool_t keep_umask)
91 {
92  const char *s;
93  pid_t child_pid;
94  int dev_null_fd;
95 
96  _dbus_verbose ("Becoming a daemon...\n");
97 
98  _dbus_verbose ("chdir to %s\n", working_dir);
99  if (chdir (working_dir) < 0)
100  {
102  "Could not chdir() to working directory (%s)", working_dir);
103  return FALSE;
104  }
105 
106  _dbus_verbose ("forking...\n");
107  switch ((child_pid = fork ()))
108  {
109  case -1:
110  _dbus_verbose ("fork failed\n");
111  dbus_set_error (error, _dbus_error_from_errno (errno),
112  "Failed to fork daemon: %s", _dbus_strerror (errno));
113  return FALSE;
114  break;
115 
116  case 0:
117  _dbus_verbose ("in child, closing std file descriptors\n");
118 
119  /* silently ignore failures here, if someone
120  * doesn't have /dev/null we may as well try
121  * to continue anyhow
122  */
123 
124  dev_null_fd = open ("/dev/null", O_RDWR);
125  if (dev_null_fd >= 0)
126  {
127  dup2 (dev_null_fd, 0);
128  dup2 (dev_null_fd, 1);
129 
130  s = _dbus_getenv ("DBUS_DEBUG_OUTPUT");
131  if (s == NULL || *s == '\0')
132  dup2 (dev_null_fd, 2);
133  else
134  _dbus_verbose ("keeping stderr open due to DBUS_DEBUG_OUTPUT\n");
135  close (dev_null_fd);
136  }
137 
138  if (!keep_umask)
139  {
140  /* Get a predictable umask */
141  _dbus_verbose ("setting umask\n");
142  umask (022);
143  }
144 
145  _dbus_verbose ("calling setsid()\n");
146  if (setsid () == -1)
147  _dbus_assert_not_reached ("setsid() failed");
148 
149  break;
150 
151  default:
152  if (!_dbus_write_pid_to_file_and_pipe (pidfile, print_pid_pipe,
153  child_pid, error))
154  {
155  _dbus_verbose ("pid file or pipe write failed: %s\n",
156  error->message);
157  kill (child_pid, SIGTERM);
158  return FALSE;
159  }
160 
161  _dbus_verbose ("parent exiting\n");
162  _exit (0);
163  break;
164  }
165 
166  return TRUE;
167 }
168 
169 
178 static dbus_bool_t
179 _dbus_write_pid_file (const DBusString *filename,
180  unsigned long pid,
181  DBusError *error)
182 {
183  const char *cfilename;
184  int fd;
185  FILE *f;
186 
187  cfilename = _dbus_string_get_const_data (filename);
188 
189  fd = open (cfilename, O_WRONLY|O_CREAT|O_EXCL|O_BINARY, 0644);
190 
191  if (fd < 0)
192  {
193  dbus_set_error (error, _dbus_error_from_errno (errno),
194  "Failed to open \"%s\": %s", cfilename,
195  _dbus_strerror (errno));
196  return FALSE;
197  }
198 
199  if ((f = fdopen (fd, "w")) == NULL)
200  {
201  dbus_set_error (error, _dbus_error_from_errno (errno),
202  "Failed to fdopen fd %d: %s", fd, _dbus_strerror (errno));
203  _dbus_close (fd, NULL);
204  return FALSE;
205  }
206 
207  if (fprintf (f, "%lu\n", pid) < 0)
208  {
209  dbus_set_error (error, _dbus_error_from_errno (errno),
210  "Failed to write to \"%s\": %s", cfilename,
211  _dbus_strerror (errno));
212 
213  fclose (f);
214  return FALSE;
215  }
216 
217  if (fclose (f) == EOF)
218  {
219  dbus_set_error (error, _dbus_error_from_errno (errno),
220  "Failed to close \"%s\": %s", cfilename,
221  _dbus_strerror (errno));
222  return FALSE;
223  }
224 
225  return TRUE;
226 }
227 
241  DBusPipe *print_pid_pipe,
242  dbus_pid_t pid_to_write,
243  DBusError *error)
244 {
245  if (pidfile)
246  {
247  _dbus_verbose ("writing pid file %s\n", _dbus_string_get_const_data (pidfile));
248  if (!_dbus_write_pid_file (pidfile,
249  pid_to_write,
250  error))
251  {
252  _dbus_verbose ("pid file write failed\n");
253  _DBUS_ASSERT_ERROR_IS_SET(error);
254  return FALSE;
255  }
256  }
257  else
258  {
259  _dbus_verbose ("No pid file requested\n");
260  }
261 
262  if (print_pid_pipe != NULL && _dbus_pipe_is_valid (print_pid_pipe))
263  {
264  DBusString pid;
265  int bytes;
266 
267  _dbus_verbose ("writing our pid to pipe %d\n",
268  print_pid_pipe->fd);
269 
270  if (!_dbus_string_init (&pid))
271  {
272  _DBUS_SET_OOM (error);
273  return FALSE;
274  }
275 
276  if (!_dbus_string_append_int (&pid, pid_to_write) ||
277  !_dbus_string_append (&pid, "\n"))
278  {
279  _dbus_string_free (&pid);
280  _DBUS_SET_OOM (error);
281  return FALSE;
282  }
283 
284  bytes = _dbus_string_get_length (&pid);
285  if (_dbus_pipe_write (print_pid_pipe, &pid, 0, bytes, error) != bytes)
286  {
287  /* _dbus_pipe_write sets error only on failure, not short write */
288  if (error != NULL && !dbus_error_is_set(error))
289  {
291  "Printing message bus PID: did not write enough bytes\n");
292  }
293  _dbus_string_free (&pid);
294  return FALSE;
295  }
296 
297  _dbus_string_free (&pid);
298  }
299  else
300  {
301  _dbus_verbose ("No pid pipe to write to\n");
302  }
303 
304  return TRUE;
305 }
306 
314 _dbus_verify_daemon_user (const char *user)
315 {
316  DBusString u;
317 
318  _dbus_string_init_const (&u, user);
319 
321 }
322 
323 
324 /* The HAVE_LIBAUDIT case lives in selinux.c */
325 #ifndef HAVE_LIBAUDIT
326 
334 _dbus_change_to_daemon_user (const char *user,
335  DBusError *error)
336 {
337  dbus_uid_t uid;
338  dbus_gid_t gid;
339  DBusString u;
340 
341  _dbus_string_init_const (&u, user);
342 
343  if (!_dbus_get_user_id_and_primary_group (&u, &uid, &gid))
344  {
346  "User '%s' does not appear to exist?",
347  user);
348  return FALSE;
349  }
350 
351  /* setgroups() only works if we are a privileged process,
352  * so we don't return error on failure; the only possible
353  * failure is that we don't have perms to do it.
354  *
355  * not sure this is right, maybe if setuid()
356  * is going to work then setgroups() should also work.
357  */
358  if (setgroups (0, NULL) < 0)
359  _dbus_warn ("Failed to drop supplementary groups: %s\n",
360  _dbus_strerror (errno));
361 
362  /* Set GID first, or the setuid may remove our permission
363  * to change the GID
364  */
365  if (setgid (gid) < 0)
366  {
367  dbus_set_error (error, _dbus_error_from_errno (errno),
368  "Failed to set GID to %lu: %s", gid,
369  _dbus_strerror (errno));
370  return FALSE;
371  }
372 
373  if (setuid (uid) < 0)
374  {
375  dbus_set_error (error, _dbus_error_from_errno (errno),
376  "Failed to set UID to %lu: %s", uid,
377  _dbus_strerror (errno));
378  return FALSE;
379  }
380 
381  return TRUE;
382 }
383 #endif /* !HAVE_LIBAUDIT */
384 
385 #ifdef HAVE_SETRLIMIT
386 
387 /* We assume that if we have setrlimit, we also have getrlimit and
388  * struct rlimit.
389  */
390 
391 struct DBusRLimit {
392  struct rlimit lim;
393 };
394 
395 DBusRLimit *
396 _dbus_rlimit_save_fd_limit (DBusError *error)
397 {
398  DBusRLimit *self;
399 
400  self = dbus_new0 (DBusRLimit, 1);
401 
402  if (self == NULL)
403  {
404  _DBUS_SET_OOM (error);
405  return NULL;
406  }
407 
408  if (getrlimit (RLIMIT_NOFILE, &self->lim) < 0)
409  {
410  dbus_set_error (error, _dbus_error_from_errno (errno),
411  "Failed to get fd limit: %s", _dbus_strerror (errno));
412  dbus_free (self);
413  return NULL;
414  }
415 
416  return self;
417 }
418 
420 _dbus_rlimit_raise_fd_limit_if_privileged (unsigned int desired,
421  DBusError *error)
422 {
423  struct rlimit lim;
424 
425  /* No point to doing this practically speaking
426  * if we're not uid 0. We expect the system
427  * bus to use this before we change UID, and
428  * the session bus takes the Linux default,
429  * currently 1024 for cur and 4096 for max.
430  */
431  if (getuid () != 0)
432  {
433  /* not an error, we're probably the session bus */
434  return TRUE;
435  }
436 
437  if (getrlimit (RLIMIT_NOFILE, &lim) < 0)
438  {
439  dbus_set_error (error, _dbus_error_from_errno (errno),
440  "Failed to get fd limit: %s", _dbus_strerror (errno));
441  return FALSE;
442  }
443 
444  if (lim.rlim_cur == RLIM_INFINITY || lim.rlim_cur >= desired)
445  {
446  /* not an error, everything is fine */
447  return TRUE;
448  }
449 
450  /* Ignore "maximum limit", assume we have the "superuser"
451  * privileges. On Linux this is CAP_SYS_RESOURCE.
452  */
453  lim.rlim_cur = lim.rlim_max = desired;
454 
455  if (setrlimit (RLIMIT_NOFILE, &lim) < 0)
456  {
457  dbus_set_error (error, _dbus_error_from_errno (errno),
458  "Failed to set fd limit to %u: %s",
459  desired, _dbus_strerror (errno));
460  return FALSE;
461  }
462 
463  return TRUE;
464 }
465 
467 _dbus_rlimit_restore_fd_limit (DBusRLimit *saved,
468  DBusError *error)
469 {
470  if (setrlimit (RLIMIT_NOFILE, &saved->lim) < 0)
471  {
472  dbus_set_error (error, _dbus_error_from_errno (errno),
473  "Failed to restore old fd limit: %s",
474  _dbus_strerror (errno));
475  return FALSE;
476  }
477 
478  return TRUE;
479 }
480 
481 #else /* !HAVE_SETRLIMIT */
482 
483 static void
484 fd_limit_not_supported (DBusError *error)
485 {
487  "cannot change fd limit on this platform");
488 }
489 
490 DBusRLimit *
491 _dbus_rlimit_save_fd_limit (DBusError *error)
492 {
493  fd_limit_not_supported (error);
494  return NULL;
495 }
496 
498 _dbus_rlimit_raise_fd_limit_if_privileged (unsigned int desired,
499  DBusError *error)
500 {
501  fd_limit_not_supported (error);
502  return FALSE;
503 }
504 
506 _dbus_rlimit_restore_fd_limit (DBusRLimit *saved,
507  DBusError *error)
508 {
509  fd_limit_not_supported (error);
510  return FALSE;
511 }
512 
513 #endif
514 
515 void
516 _dbus_rlimit_free (DBusRLimit *lim)
517 {
518  dbus_free (lim);
519 }
520 
521 void
522 _dbus_init_system_log (dbus_bool_t is_daemon)
523 {
524 #ifdef HAVE_SYSLOG_H
525  int logopts = LOG_PID;
526 
527 #if HAVE_DECL_LOG_PERROR
528 #ifdef HAVE_SYSTEMD
529  if (!is_daemon || sd_booted () <= 0)
530 #endif
531  logopts |= LOG_PERROR;
532 #endif
533 
534  openlog ("dbus", logopts, LOG_DAEMON);
535 #endif
536 }
537 
544 void
545 _dbus_system_log (DBusSystemLogSeverity severity, const char *msg, ...)
546 {
547  va_list args;
548 
549  va_start (args, msg);
550 
551  _dbus_system_logv (severity, msg, args);
552 
553  va_end (args);
554 }
555 
566 void
567 _dbus_system_logv (DBusSystemLogSeverity severity, const char *msg, va_list args)
568 {
569  va_list tmp;
570 #ifdef HAVE_SYSLOG_H
571  int flags;
572  switch (severity)
573  {
574  case DBUS_SYSTEM_LOG_INFO:
575  flags = LOG_DAEMON | LOG_NOTICE;
576  break;
577  case DBUS_SYSTEM_LOG_WARNING:
578  flags = LOG_DAEMON | LOG_WARNING;
579  break;
580  case DBUS_SYSTEM_LOG_SECURITY:
581  flags = LOG_AUTH | LOG_NOTICE;
582  break;
583  case DBUS_SYSTEM_LOG_FATAL:
584  flags = LOG_DAEMON|LOG_CRIT;
585  break;
586  default:
587  return;
588  }
589 
590  DBUS_VA_COPY (tmp, args);
591  vsyslog (flags, msg, tmp);
592  va_end (tmp);
593 #endif
594 
595 #if !defined(HAVE_SYSLOG_H) || !HAVE_DECL_LOG_PERROR
596  {
597  /* vsyslog() won't write to stderr, so we'd better do it */
598  DBUS_VA_COPY (tmp, args);
599  fprintf (stderr, "dbus[" DBUS_PID_FORMAT "]: ", _dbus_getpid ());
600  vfprintf (stderr, msg, tmp);
601  fputc ('\n', stderr);
602  va_end (tmp);
603  }
604 #endif
605 
606  if (severity == DBUS_SYSTEM_LOG_FATAL)
607  exit (1);
608 }
609 
615 void
617  DBusSignalHandler handler)
618 {
619  struct sigaction act;
620  sigset_t empty_mask;
621 
622  sigemptyset (&empty_mask);
623  act.sa_handler = handler;
624  act.sa_mask = empty_mask;
625  act.sa_flags = 0;
626  sigaction (sig, &act, NULL);
627 }
628 
635 _dbus_file_exists (const char *file)
636 {
637  return (access (file, F_OK) == 0);
638 }
639 
647 _dbus_user_at_console (const char *username,
648  DBusError *error)
649 {
650 
651  DBusString u, f;
652  dbus_bool_t result;
653 
654  result = FALSE;
655  if (!_dbus_string_init (&f))
656  {
657  _DBUS_SET_OOM (error);
658  return FALSE;
659  }
660 
661  if (!_dbus_string_append (&f, DBUS_CONSOLE_AUTH_DIR))
662  {
663  _DBUS_SET_OOM (error);
664  goto out;
665  }
666 
667  _dbus_string_init_const (&u, username);
668 
669  if (!_dbus_concat_dir_and_file (&f, &u))
670  {
671  _DBUS_SET_OOM (error);
672  goto out;
673  }
674 
675  result = _dbus_file_exists (_dbus_string_get_const_data (&f));
676 
677  out:
678  _dbus_string_free (&f);
679 
680  return result;
681 }
682 
683 
692 {
693  if (_dbus_string_get_length (filename) > 0)
694  return _dbus_string_get_byte (filename, 0) == '/';
695  else
696  return FALSE;
697 }
698 
708 _dbus_stat (const DBusString *filename,
709  DBusStat *statbuf,
710  DBusError *error)
711 {
712  const char *filename_c;
713  struct stat sb;
714 
715  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
716 
717  filename_c = _dbus_string_get_const_data (filename);
718 
719  if (stat (filename_c, &sb) < 0)
720  {
721  dbus_set_error (error, _dbus_error_from_errno (errno),
722  "%s", _dbus_strerror (errno));
723  return FALSE;
724  }
725 
726  statbuf->mode = sb.st_mode;
727  statbuf->nlink = sb.st_nlink;
728  statbuf->uid = sb.st_uid;
729  statbuf->gid = sb.st_gid;
730  statbuf->size = sb.st_size;
731  statbuf->atime = sb.st_atime;
732  statbuf->mtime = sb.st_mtime;
733  statbuf->ctime = sb.st_ctime;
734 
735  return TRUE;
736 }
737 
738 
743 {
744  DIR *d;
746 };
747 
757  DBusError *error)
758 {
759  DIR *d;
760  DBusDirIter *iter;
761  const char *filename_c;
762 
763  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
764 
765  filename_c = _dbus_string_get_const_data (filename);
766 
767  d = opendir (filename_c);
768  if (d == NULL)
769  {
770  dbus_set_error (error, _dbus_error_from_errno (errno),
771  "Failed to read directory \"%s\": %s",
772  filename_c,
773  _dbus_strerror (errno));
774  return NULL;
775  }
776  iter = dbus_new0 (DBusDirIter, 1);
777  if (iter == NULL)
778  {
779  closedir (d);
781  "Could not allocate memory for directory iterator");
782  return NULL;
783  }
784 
785  iter->d = d;
786 
787  return iter;
788 }
789 
805  DBusString *filename,
806  DBusError *error)
807 {
808  struct dirent *ent;
809  int err;
810 
811  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
812 
813  again:
814  errno = 0;
815  ent = readdir (iter->d);
816 
817  if (!ent)
818  {
819  err = errno;
820 
821  if (err != 0)
822  dbus_set_error (error,
824  "%s", _dbus_strerror (err));
825 
826  return FALSE;
827  }
828  else if (ent->d_name[0] == '.' &&
829  (ent->d_name[1] == '\0' ||
830  (ent->d_name[1] == '.' && ent->d_name[2] == '\0')))
831  goto again;
832  else
833  {
834  _dbus_string_set_length (filename, 0);
835  if (!_dbus_string_append (filename, ent->d_name))
836  {
838  "No memory to read directory entry");
839  return FALSE;
840  }
841  else
842  {
843  return TRUE;
844  }
845  }
846 }
847 
851 void
853 {
854  closedir (iter->d);
855  dbus_free (iter);
856 }
857 
858 static dbus_bool_t
859 fill_user_info_from_group (struct group *g,
860  DBusGroupInfo *info,
861  DBusError *error)
862 {
863  _dbus_assert (g->gr_name != NULL);
864 
865  info->gid = g->gr_gid;
866  info->groupname = _dbus_strdup (g->gr_name);
867 
868  /* info->members = dbus_strdupv (g->gr_mem) */
869 
870  if (info->groupname == NULL)
871  {
873  return FALSE;
874  }
875 
876  return TRUE;
877 }
878 
879 static dbus_bool_t
880 fill_group_info (DBusGroupInfo *info,
881  dbus_gid_t gid,
882  const DBusString *groupname,
883  DBusError *error)
884 {
885  const char *group_c_str;
886 
887  _dbus_assert (groupname != NULL || gid != DBUS_GID_UNSET);
888  _dbus_assert (groupname == NULL || gid == DBUS_GID_UNSET);
889 
890  if (groupname)
891  group_c_str = _dbus_string_get_const_data (groupname);
892  else
893  group_c_str = NULL;
894 
895  /* For now assuming that the getgrnam() and getgrgid() flavors
896  * always correspond to the pwnam flavors, if not we have
897  * to add more configure checks.
898  */
899 
900 #if defined (HAVE_POSIX_GETPWNAM_R) || defined (HAVE_NONPOSIX_GETPWNAM_R)
901  {
902  struct group *g;
903  int result;
904  size_t buflen;
905  char *buf;
906  struct group g_str;
907  dbus_bool_t b;
908 
909  /* retrieve maximum needed size for buf */
910  buflen = sysconf (_SC_GETGR_R_SIZE_MAX);
911 
912  /* sysconf actually returns a long, but everything else expects size_t,
913  * so just recast here.
914  * https://bugs.freedesktop.org/show_bug.cgi?id=17061
915  */
916  if ((long) buflen <= 0)
917  buflen = 1024;
918 
919  result = -1;
920  while (1)
921  {
922  buf = dbus_malloc (buflen);
923  if (buf == NULL)
924  {
926  return FALSE;
927  }
928 
929  g = NULL;
930 #ifdef HAVE_POSIX_GETPWNAM_R
931  if (group_c_str)
932  result = getgrnam_r (group_c_str, &g_str, buf, buflen,
933  &g);
934  else
935  result = getgrgid_r (gid, &g_str, buf, buflen,
936  &g);
937 #else
938  g = getgrnam_r (group_c_str, &g_str, buf, buflen);
939  result = 0;
940 #endif /* !HAVE_POSIX_GETPWNAM_R */
941  /* Try a bigger buffer if ERANGE was returned:
942  https://bugs.freedesktop.org/show_bug.cgi?id=16727
943  */
944  if (result == ERANGE && buflen < 512 * 1024)
945  {
946  dbus_free (buf);
947  buflen *= 2;
948  }
949  else
950  {
951  break;
952  }
953  }
954 
955  if (result == 0 && g == &g_str)
956  {
957  b = fill_user_info_from_group (g, info, error);
958  dbus_free (buf);
959  return b;
960  }
961  else
962  {
963  dbus_set_error (error, _dbus_error_from_errno (errno),
964  "Group %s unknown or failed to look it up\n",
965  group_c_str ? group_c_str : "???");
966  dbus_free (buf);
967  return FALSE;
968  }
969  }
970 #else /* ! HAVE_GETPWNAM_R */
971  {
972  /* I guess we're screwed on thread safety here */
973  struct group *g;
974 
975  g = getgrnam (group_c_str);
976 
977  if (g != NULL)
978  {
979  return fill_user_info_from_group (g, info, error);
980  }
981  else
982  {
983  dbus_set_error (error, _dbus_error_from_errno (errno),
984  "Group %s unknown or failed to look it up\n",
985  group_c_str ? group_c_str : "???");
986  return FALSE;
987  }
988  }
989 #endif /* ! HAVE_GETPWNAM_R */
990 }
991 
1003  const DBusString *groupname,
1004  DBusError *error)
1005 {
1006  return fill_group_info (info, DBUS_GID_UNSET,
1007  groupname, error);
1008 
1009 }
1010 
1022  dbus_gid_t gid,
1023  DBusError *error)
1024 {
1025  return fill_group_info (info, gid, NULL, error);
1026 }
1027 
1038  dbus_uid_t *uid_p)
1039 {
1040  return _dbus_get_user_id (username, uid_p);
1041 
1042 }
1043 
1054  dbus_gid_t *gid_p)
1055 {
1056  return _dbus_get_group_id (groupname, gid_p);
1057 }
1058 
1071  dbus_gid_t **group_ids,
1072  int *n_group_ids)
1073 {
1074  return _dbus_groups_from_uid (uid, group_ids, n_group_ids);
1075 }
1076 
1088  DBusError *error)
1089 {
1090  return _dbus_is_console_user (uid, error);
1091 
1092 }
1093 
1103 {
1104  return uid == _dbus_geteuid ();
1105 }
1106 
1115 _dbus_windows_user_is_process_owner (const char *windows_sid)
1116 {
1117  return FALSE;
1118 }
1119  /* End of DBusInternalsUtils functions */
1121 
1135  DBusString *dirname)
1136 {
1137  int sep;
1138 
1139  _dbus_assert (filename != dirname);
1140  _dbus_assert (filename != NULL);
1141  _dbus_assert (dirname != NULL);
1142 
1143  /* Ignore any separators on the end */
1144  sep = _dbus_string_get_length (filename);
1145  if (sep == 0)
1146  return _dbus_string_append (dirname, "."); /* empty string passed in */
1147 
1148  while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
1149  --sep;
1150 
1151  _dbus_assert (sep >= 0);
1152 
1153  if (sep == 0)
1154  return _dbus_string_append (dirname, "/");
1155 
1156  /* Now find the previous separator */
1157  _dbus_string_find_byte_backward (filename, sep, '/', &sep);
1158  if (sep < 0)
1159  return _dbus_string_append (dirname, ".");
1160 
1161  /* skip multiple separators */
1162  while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
1163  --sep;
1164 
1165  _dbus_assert (sep >= 0);
1166 
1167  if (sep == 0 &&
1168  _dbus_string_get_byte (filename, 0) == '/')
1169  return _dbus_string_append (dirname, "/");
1170  else
1171  return _dbus_string_copy_len (filename, 0, sep - 0,
1172  dirname, _dbus_string_get_length (dirname));
1173 } /* DBusString stuff */
1175 
1176 static void
1177 string_squash_nonprintable (DBusString *str)
1178 {
1179  unsigned char *buf;
1180  int i, len;
1181 
1182  buf = _dbus_string_get_data (str);
1183  len = _dbus_string_get_length (str);
1184 
1185  for (i = 0; i < len; i++)
1186  {
1187  unsigned char c = (unsigned char) buf[i];
1188  if (c == '\0')
1189  buf[i] = ' ';
1190  else if (c < 0x20 || c > 127)
1191  buf[i] = '?';
1192  }
1193 }
1194 
1209 dbus_bool_t
1210 _dbus_command_for_pid (unsigned long pid,
1211  DBusString *str,
1212  int max_len,
1213  DBusError *error)
1214 {
1215  /* This is all Linux-specific for now */
1216  DBusString path;
1217  DBusString cmdline;
1218  int fd;
1219 
1220  if (!_dbus_string_init (&path))
1221  {
1222  _DBUS_SET_OOM (error);
1223  return FALSE;
1224  }
1225 
1226  if (!_dbus_string_init (&cmdline))
1227  {
1228  _DBUS_SET_OOM (error);
1229  _dbus_string_free (&path);
1230  return FALSE;
1231  }
1232 
1233  if (!_dbus_string_append_printf (&path, "/proc/%ld/cmdline", pid))
1234  goto oom;
1235 
1236  fd = open (_dbus_string_get_const_data (&path), O_RDONLY);
1237  if (fd < 0)
1238  {
1239  dbus_set_error (error,
1240  _dbus_error_from_errno (errno),
1241  "Failed to open \"%s\": %s",
1242  _dbus_string_get_const_data (&path),
1243  _dbus_strerror (errno));
1244  goto fail;
1245  }
1246 
1247  if (!_dbus_read (fd, &cmdline, max_len))
1248  {
1249  dbus_set_error (error,
1250  _dbus_error_from_errno (errno),
1251  "Failed to read from \"%s\": %s",
1252  _dbus_string_get_const_data (&path),
1253  _dbus_strerror (errno));
1254  _dbus_close (fd, NULL);
1255  goto fail;
1256  }
1257 
1258  if (!_dbus_close (fd, error))
1259  goto fail;
1260 
1261  string_squash_nonprintable (&cmdline);
1262 
1263  if (!_dbus_string_copy (&cmdline, 0, str, _dbus_string_get_length (str)))
1264  goto oom;
1265 
1266  _dbus_string_free (&cmdline);
1267  _dbus_string_free (&path);
1268  return TRUE;
1269 oom:
1270  _DBUS_SET_OOM (error);
1271 fail:
1272  _dbus_string_free (&cmdline);
1273  _dbus_string_free (&path);
1274  return FALSE;
1275 }
1276 
1277 /*
1278  * replaces the term DBUS_PREFIX in configure_time_path by the
1279  * current dbus installation directory. On unix this function is a noop
1280  *
1281  * @param configure_time_path
1282  * @return real path
1283  */
1284 const char *
1285 _dbus_replace_install_prefix (const char *configure_time_path)
1286 {
1287  return configure_time_path;
1288 }
1289 
1290 #define DBUS_UNIX_STANDARD_SESSION_SERVICEDIR "/dbus-1/services"
1291 #define DBUS_UNIX_STANDARD_SYSTEM_SERVICEDIR "/dbus-1/system-services"
1292 
1312 {
1313  const char *xdg_data_home;
1314  const char *xdg_data_dirs;
1315  DBusString servicedir_path;
1316 
1317  if (!_dbus_string_init (&servicedir_path))
1318  return FALSE;
1319 
1320  xdg_data_home = _dbus_getenv ("XDG_DATA_HOME");
1321  xdg_data_dirs = _dbus_getenv ("XDG_DATA_DIRS");
1322 
1323  if (xdg_data_home != NULL)
1324  {
1325  if (!_dbus_string_append (&servicedir_path, xdg_data_home))
1326  goto oom;
1327  }
1328  else
1329  {
1330  const DBusString *homedir;
1331  DBusString local_share;
1332 
1333  if (!_dbus_homedir_from_current_process (&homedir))
1334  goto oom;
1335 
1336  if (!_dbus_string_append (&servicedir_path, _dbus_string_get_const_data (homedir)))
1337  goto oom;
1338 
1339  _dbus_string_init_const (&local_share, "/.local/share");
1340  if (!_dbus_concat_dir_and_file (&servicedir_path, &local_share))
1341  goto oom;
1342  }
1343 
1344  if (!_dbus_string_append (&servicedir_path, ":"))
1345  goto oom;
1346 
1347  if (xdg_data_dirs != NULL)
1348  {
1349  if (!_dbus_string_append (&servicedir_path, xdg_data_dirs))
1350  goto oom;
1351 
1352  if (!_dbus_string_append (&servicedir_path, ":"))
1353  goto oom;
1354  }
1355  else
1356  {
1357  if (!_dbus_string_append (&servicedir_path, "/usr/local/share:/usr/share:"))
1358  goto oom;
1359  }
1360 
1361  /*
1362  * add configured datadir to defaults
1363  * this may be the same as an xdg dir
1364  * however the config parser should take
1365  * care of duplicates
1366  */
1367  if (!_dbus_string_append (&servicedir_path, DBUS_DATADIR))
1368  goto oom;
1369 
1370  if (!_dbus_split_paths_and_append (&servicedir_path,
1371  DBUS_UNIX_STANDARD_SESSION_SERVICEDIR,
1372  dirs))
1373  goto oom;
1374 
1375  _dbus_string_free (&servicedir_path);
1376  return TRUE;
1377 
1378  oom:
1379  _dbus_string_free (&servicedir_path);
1380  return FALSE;
1381 }
1382 
1383 
1404 {
1405  /*
1406  * DBUS_DATADIR may be the same as one of the standard directories. However,
1407  * the config parser should take care of the duplicates.
1408  *
1409  * Also, append /lib as counterpart of /usr/share on the root
1410  * directory (the root directory does not know /share), in order to
1411  * facilitate early boot system bus activation where /usr might not
1412  * be available.
1413  */
1414  static const char standard_search_path[] =
1415  "/usr/local/share:"
1416  "/usr/share:"
1417  DBUS_DATADIR ":"
1418  "/lib";
1419  DBusString servicedir_path;
1420 
1421  _dbus_string_init_const (&servicedir_path, standard_search_path);
1422 
1423  return _dbus_split_paths_and_append (&servicedir_path,
1424  DBUS_UNIX_STANDARD_SYSTEM_SERVICEDIR,
1425  dirs);
1426 }
1427 
1438 {
1439  return _dbus_string_append (str, DBUS_SYSTEM_CONFIG_FILE);
1440 }
1441 
1450 {
1451  return _dbus_string_append (str, DBUS_SESSION_CONFIG_FILE);
1452 }
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
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:226
const char * message
public error message field
Definition: dbus-errors.h:51
#define NULL
A null pointer, defined appropriately for C or C++.
dbus_bool_t _dbus_append_system_config_file(DBusString *str)
Append the absolute path of the system.conf file (there is no system bus on Windows so this can just ...
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().
Definition: dbus-memory.c:701
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.
Definition: dbus-sysdeps.h:504
#define DBUS_ERROR_NOT_SUPPORTED
Requested operation isn&#39;t supported (like ENOSYS on UNIX).
DBUS_PRIVATE_EXPORT dbus_bool_t _dbus_string_append_int(DBusString *str, long value)
Appends an integer to a DBusString.
Definition: dbus-sysdeps.c:354
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
Definition: dbus-sysdeps.h:119
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.
Definition: dbus-sysdeps.h:511
dbus_bool_t _dbus_get_standard_session_servicedirs(DBusList **dirs)
Returns the standard directories for a session bus to look for service activation files...
dbus_pid_t _dbus_getpid(void)
Gets our process ID.
dbus_bool_t _dbus_concat_dir_and_file(DBusString *dir, const DBusString *next_component)
Appends the given filename to the given directory.
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.
Definition: dbus-string.c:175
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&#39;s copied to the d...
Definition: dbus-string.c:1283
char * groupname
Group name.
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:590
Internals of directory iterator.
unsigned long mode
File mode.
Definition: dbus-sysdeps.h:506
unsigned long dbus_pid_t
A process ID.
Definition: dbus-sysdeps.h:105
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.
DIR * d
The DIR* from opendir()
void * dbus_malloc(size_t bytes)
Allocates the given number of bytes, as with standard malloc().
Definition: dbus-memory.c:461
dbus_gid_t gid
Group owning file.
Definition: dbus-sysdeps.h:509
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition: dbus-memory.h:59
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
Definition: dbus-string.c:190
dbus_bool_t _dbus_become_daemon(const DBusString *pidfile, const char *working_dir, DBusPipe *print_pid_pipe, DBusError *error, dbus_bool_t keep_umask)
Does the chdir, fork, setsid, etc.
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...
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_string_append_printf(DBusString *str, const char *format,...)
Appends a printf-style formatted string to the DBusString.
Definition: dbus-string.c:1114
void(* DBusSignalHandler)(int sig)
A UNIX signal handler.
Definition: dbus-sysdeps.h:548
dbus_bool_t _dbus_get_group_id(const DBusString *groupname, dbus_gid_t *gid)
Gets group ID given groupname.
Object representing an exception.
Definition: dbus-errors.h:48
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_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.
Definition: dbus-sysdeps.h:513
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init().
Definition: dbus-string.c:259
#define DBUS_GID_UNSET
an invalid GID used to represent an uninitialized dbus_gid_t field
Definition: dbus-sysdeps.h:116
dbus_uid_t _dbus_geteuid(void)
Gets our effective UID.
#define TRUE
Expands to &quot;1&quot;.
unsigned long nlink
Number of hard links.
Definition: dbus-sysdeps.h:507
#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.
Definition: dbus-sysdeps.h:508
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; &quot;something went wrong&quot; - 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.
dbus_bool_t _dbus_homedir_from_current_process(const DBusString **homedir)
Gets homedir of user owning current process.
Definition: dbus-userdb.c:395
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.
A node in a linked list.
Definition: dbus-list.h:34
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.
dbus_bool_t _dbus_close(int fd, DBusError *error)
Closes a file descriptor.
#define FALSE
Expands to &quot;0&quot;.
unsigned long mtime
Modify time.
Definition: dbus-sysdeps.h:512
dbus_bool_t _dbus_string_set_length(DBusString *str, int length)
Sets the length of a string.
Definition: dbus-string.c:802
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_gid_t gid
GID.
unsigned long dbus_gid_t
A group ID.
Definition: dbus-sysdeps.h:109
unsigned long size
Size of file.
Definition: dbus-sysdeps.h:510
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.
dbus_bool_t _dbus_append_session_config_file(DBusString *str)
Append the absolute path of the session.conf file.
const char * _dbus_getenv(const char *varname)
Wrapper for getenv().
Definition: dbus-sysdeps.c:185
unsigned long dbus_uid_t
A user ID.
Definition: dbus-sysdeps.h:107
dbus_bool_t _dbus_get_standard_system_servicedirs(DBusList **dirs)
Returns the standard directories for a system bus to look for service activation files.
dbus_bool_t dbus_error_is_set(const DBusError *error)
Checks whether an error occurred (the error is set).
Definition: dbus-errors.c:329