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