D-Bus  1.8.16
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 #include "sd-daemon.h"
62 
63 #ifndef O_BINARY
64 #define O_BINARY 0
65 #endif
66 
84  DBusPipe *print_pid_pipe,
85  DBusError *error,
86  dbus_bool_t keep_umask)
87 {
88  const char *s;
89  pid_t child_pid;
90  int dev_null_fd;
91 
92  _dbus_verbose ("Becoming a daemon...\n");
93 
94  _dbus_verbose ("chdir to /\n");
95  if (chdir ("/") < 0)
96  {
98  "Could not chdir() to root directory");
99  return FALSE;
100  }
101 
102  _dbus_verbose ("forking...\n");
103  switch ((child_pid = fork ()))
104  {
105  case -1:
106  _dbus_verbose ("fork failed\n");
107  dbus_set_error (error, _dbus_error_from_errno (errno),
108  "Failed to fork daemon: %s", _dbus_strerror (errno));
109  return FALSE;
110  break;
111 
112  case 0:
113  _dbus_verbose ("in child, closing std file descriptors\n");
114 
115  /* silently ignore failures here, if someone
116  * doesn't have /dev/null we may as well try
117  * to continue anyhow
118  */
119 
120  dev_null_fd = open ("/dev/null", O_RDWR);
121  if (dev_null_fd >= 0)
122  {
123  dup2 (dev_null_fd, 0);
124  dup2 (dev_null_fd, 1);
125 
126  s = _dbus_getenv ("DBUS_DEBUG_OUTPUT");
127  if (s == NULL || *s == '\0')
128  dup2 (dev_null_fd, 2);
129  else
130  _dbus_verbose ("keeping stderr open due to DBUS_DEBUG_OUTPUT\n");
131  close (dev_null_fd);
132  }
133 
134  if (!keep_umask)
135  {
136  /* Get a predictable umask */
137  _dbus_verbose ("setting umask\n");
138  umask (022);
139  }
140 
141  _dbus_verbose ("calling setsid()\n");
142  if (setsid () == -1)
143  _dbus_assert_not_reached ("setsid() failed");
144 
145  break;
146 
147  default:
148  if (!_dbus_write_pid_to_file_and_pipe (pidfile, print_pid_pipe,
149  child_pid, error))
150  {
151  _dbus_verbose ("pid file or pipe write failed: %s\n",
152  error->message);
153  kill (child_pid, SIGTERM);
154  return FALSE;
155  }
156 
157  _dbus_verbose ("parent exiting\n");
158  _exit (0);
159  break;
160  }
161 
162  return TRUE;
163 }
164 
165 
174 static dbus_bool_t
175 _dbus_write_pid_file (const DBusString *filename,
176  unsigned long pid,
177  DBusError *error)
178 {
179  const char *cfilename;
180  int fd;
181  FILE *f;
182 
183  cfilename = _dbus_string_get_const_data (filename);
184 
185  fd = open (cfilename, O_WRONLY|O_CREAT|O_EXCL|O_BINARY, 0644);
186 
187  if (fd < 0)
188  {
189  dbus_set_error (error, _dbus_error_from_errno (errno),
190  "Failed to open \"%s\": %s", cfilename,
191  _dbus_strerror (errno));
192  return FALSE;
193  }
194 
195  if ((f = fdopen (fd, "w")) == NULL)
196  {
197  dbus_set_error (error, _dbus_error_from_errno (errno),
198  "Failed to fdopen fd %d: %s", fd, _dbus_strerror (errno));
199  _dbus_close (fd, NULL);
200  return FALSE;
201  }
202 
203  if (fprintf (f, "%lu\n", pid) < 0)
204  {
205  dbus_set_error (error, _dbus_error_from_errno (errno),
206  "Failed to write to \"%s\": %s", cfilename,
207  _dbus_strerror (errno));
208 
209  fclose (f);
210  return FALSE;
211  }
212 
213  if (fclose (f) == EOF)
214  {
215  dbus_set_error (error, _dbus_error_from_errno (errno),
216  "Failed to close \"%s\": %s", cfilename,
217  _dbus_strerror (errno));
218  return FALSE;
219  }
220 
221  return TRUE;
222 }
223 
237  DBusPipe *print_pid_pipe,
238  dbus_pid_t pid_to_write,
239  DBusError *error)
240 {
241  if (pidfile)
242  {
243  _dbus_verbose ("writing pid file %s\n", _dbus_string_get_const_data (pidfile));
244  if (!_dbus_write_pid_file (pidfile,
245  pid_to_write,
246  error))
247  {
248  _dbus_verbose ("pid file write failed\n");
249  _DBUS_ASSERT_ERROR_IS_SET(error);
250  return FALSE;
251  }
252  }
253  else
254  {
255  _dbus_verbose ("No pid file requested\n");
256  }
257 
258  if (print_pid_pipe != NULL && _dbus_pipe_is_valid (print_pid_pipe))
259  {
260  DBusString pid;
261  int bytes;
262 
263  _dbus_verbose ("writing our pid to pipe %d\n",
264  print_pid_pipe->fd);
265 
266  if (!_dbus_string_init (&pid))
267  {
268  _DBUS_SET_OOM (error);
269  return FALSE;
270  }
271 
272  if (!_dbus_string_append_int (&pid, pid_to_write) ||
273  !_dbus_string_append (&pid, "\n"))
274  {
275  _dbus_string_free (&pid);
276  _DBUS_SET_OOM (error);
277  return FALSE;
278  }
279 
280  bytes = _dbus_string_get_length (&pid);
281  if (_dbus_pipe_write (print_pid_pipe, &pid, 0, bytes, error) != bytes)
282  {
283  /* _dbus_pipe_write sets error only on failure, not short write */
284  if (error != NULL && !dbus_error_is_set(error))
285  {
287  "Printing message bus PID: did not write enough bytes\n");
288  }
289  _dbus_string_free (&pid);
290  return FALSE;
291  }
292 
293  _dbus_string_free (&pid);
294  }
295  else
296  {
297  _dbus_verbose ("No pid pipe to write to\n");
298  }
299 
300  return TRUE;
301 }
302 
310 _dbus_verify_daemon_user (const char *user)
311 {
312  DBusString u;
313 
314  _dbus_string_init_const (&u, user);
315 
317 }
318 
319 
320 /* The HAVE_LIBAUDIT case lives in selinux.c */
321 #ifndef HAVE_LIBAUDIT
322 
330 _dbus_change_to_daemon_user (const char *user,
331  DBusError *error)
332 {
333  dbus_uid_t uid;
334  dbus_gid_t gid;
335  DBusString u;
336 
337  _dbus_string_init_const (&u, user);
338 
339  if (!_dbus_get_user_id_and_primary_group (&u, &uid, &gid))
340  {
342  "User '%s' does not appear to exist?",
343  user);
344  return FALSE;
345  }
346 
347  /* setgroups() only works if we are a privileged process,
348  * so we don't return error on failure; the only possible
349  * failure is that we don't have perms to do it.
350  *
351  * not sure this is right, maybe if setuid()
352  * is going to work then setgroups() should also work.
353  */
354  if (setgroups (0, NULL) < 0)
355  _dbus_warn ("Failed to drop supplementary groups: %s\n",
356  _dbus_strerror (errno));
357 
358  /* Set GID first, or the setuid may remove our permission
359  * to change the GID
360  */
361  if (setgid (gid) < 0)
362  {
363  dbus_set_error (error, _dbus_error_from_errno (errno),
364  "Failed to set GID to %lu: %s", gid,
365  _dbus_strerror (errno));
366  return FALSE;
367  }
368 
369  if (setuid (uid) < 0)
370  {
371  dbus_set_error (error, _dbus_error_from_errno (errno),
372  "Failed to set UID to %lu: %s", uid,
373  _dbus_strerror (errno));
374  return FALSE;
375  }
376 
377  return TRUE;
378 }
379 #endif /* !HAVE_LIBAUDIT */
380 
381 #ifdef HAVE_SETRLIMIT
382 
383 /* We assume that if we have setrlimit, we also have getrlimit and
384  * struct rlimit.
385  */
386 
387 struct DBusRLimit {
388  struct rlimit lim;
389 };
390 
391 DBusRLimit *
392 _dbus_rlimit_save_fd_limit (DBusError *error)
393 {
394  DBusRLimit *self;
395 
396  self = dbus_new0 (DBusRLimit, 1);
397 
398  if (self == NULL)
399  {
400  _DBUS_SET_OOM (error);
401  return NULL;
402  }
403 
404  if (getrlimit (RLIMIT_NOFILE, &self->lim) < 0)
405  {
406  dbus_set_error (error, _dbus_error_from_errno (errno),
407  "Failed to get fd limit: %s", _dbus_strerror (errno));
408  dbus_free (self);
409  return NULL;
410  }
411 
412  return self;
413 }
414 
416 _dbus_rlimit_raise_fd_limit_if_privileged (unsigned int desired,
417  DBusError *error)
418 {
419  struct rlimit lim;
420 
421  /* No point to doing this practically speaking
422  * if we're not uid 0. We expect the system
423  * bus to use this before we change UID, and
424  * the session bus takes the Linux default,
425  * currently 1024 for cur and 4096 for max.
426  */
427  if (getuid () != 0)
428  {
429  /* not an error, we're probably the session bus */
430  return TRUE;
431  }
432 
433  if (getrlimit (RLIMIT_NOFILE, &lim) < 0)
434  {
435  dbus_set_error (error, _dbus_error_from_errno (errno),
436  "Failed to get fd limit: %s", _dbus_strerror (errno));
437  return FALSE;
438  }
439 
440  if (lim.rlim_cur == RLIM_INFINITY || lim.rlim_cur >= desired)
441  {
442  /* not an error, everything is fine */
443  return TRUE;
444  }
445 
446  /* Ignore "maximum limit", assume we have the "superuser"
447  * privileges. On Linux this is CAP_SYS_RESOURCE.
448  */
449  lim.rlim_cur = lim.rlim_max = desired;
450 
451  if (setrlimit (RLIMIT_NOFILE, &lim) < 0)
452  {
453  dbus_set_error (error, _dbus_error_from_errno (errno),
454  "Failed to set fd limit to %u: %s",
455  desired, _dbus_strerror (errno));
456  return FALSE;
457  }
458 
459  return TRUE;
460 }
461 
463 _dbus_rlimit_restore_fd_limit (DBusRLimit *saved,
464  DBusError *error)
465 {
466  if (setrlimit (RLIMIT_NOFILE, &saved->lim) < 0)
467  {
468  dbus_set_error (error, _dbus_error_from_errno (errno),
469  "Failed to restore old fd limit: %s",
470  _dbus_strerror (errno));
471  return FALSE;
472  }
473 
474  return TRUE;
475 }
476 
477 #else /* !HAVE_SETRLIMIT */
478 
479 static void
480 fd_limit_not_supported (DBusError *error)
481 {
483  "cannot change fd limit on this platform");
484 }
485 
486 DBusRLimit *
487 _dbus_rlimit_save_fd_limit (DBusError *error)
488 {
489  fd_limit_not_supported (error);
490  return NULL;
491 }
492 
494 _dbus_rlimit_raise_fd_limit_if_privileged (unsigned int desired,
495  DBusError *error)
496 {
497  fd_limit_not_supported (error);
498  return FALSE;
499 }
500 
502 _dbus_rlimit_restore_fd_limit (DBusRLimit *saved,
503  DBusError *error)
504 {
505  fd_limit_not_supported (error);
506  return FALSE;
507 }
508 
509 #endif
510 
511 void
512 _dbus_rlimit_free (DBusRLimit *lim)
513 {
514  dbus_free (lim);
515 }
516 
517 void
518 _dbus_init_system_log (dbus_bool_t is_daemon)
519 {
520 #ifdef HAVE_SYSLOG_H
521  int logopts = LOG_PID;
522 
523 #if HAVE_DECL_LOG_PERROR
524 #ifdef HAVE_SYSTEMD
525  if (!is_daemon || sd_booted () <= 0)
526 #endif
527  logopts |= LOG_PERROR;
528 #endif
529 
530  openlog ("dbus", logopts, LOG_DAEMON);
531 #endif
532 }
533 
540 void
541 _dbus_system_log (DBusSystemLogSeverity severity, const char *msg, ...)
542 {
543  va_list args;
544 
545  va_start (args, msg);
546 
547  _dbus_system_logv (severity, msg, args);
548 
549  va_end (args);
550 }
551 
562 void
563 _dbus_system_logv (DBusSystemLogSeverity severity, const char *msg, va_list args)
564 {
565  va_list tmp;
566 #ifdef HAVE_SYSLOG_H
567  int flags;
568  switch (severity)
569  {
570  case DBUS_SYSTEM_LOG_INFO:
571  flags = LOG_DAEMON | LOG_NOTICE;
572  break;
573  case DBUS_SYSTEM_LOG_SECURITY:
574  flags = LOG_AUTH | LOG_NOTICE;
575  break;
576  case DBUS_SYSTEM_LOG_FATAL:
577  flags = LOG_DAEMON|LOG_CRIT;
578  break;
579  default:
580  return;
581  }
582 
583  DBUS_VA_COPY (tmp, args);
584  vsyslog (flags, msg, tmp);
585  va_end (tmp);
586 #endif
587 
588 #if !defined(HAVE_SYSLOG_H) || !HAVE_DECL_LOG_PERROR
589  {
590  /* vsyslog() won't write to stderr, so we'd better do it */
591  DBUS_VA_COPY (tmp, args);
592  fprintf (stderr, "dbus[" DBUS_PID_FORMAT "]: ", _dbus_getpid ());
593  vfprintf (stderr, msg, tmp);
594  fputc ('\n', stderr);
595  va_end (tmp);
596  }
597 #endif
598 
599  if (severity == DBUS_SYSTEM_LOG_FATAL)
600  exit (1);
601 }
602 
608 void
610  DBusSignalHandler handler)
611 {
612  struct sigaction act;
613  sigset_t empty_mask;
614 
615  sigemptyset (&empty_mask);
616  act.sa_handler = handler;
617  act.sa_mask = empty_mask;
618  act.sa_flags = 0;
619  sigaction (sig, &act, NULL);
620 }
621 
628 _dbus_file_exists (const char *file)
629 {
630  return (access (file, F_OK) == 0);
631 }
632 
640 _dbus_user_at_console (const char *username,
641  DBusError *error)
642 {
643 
644  DBusString u, f;
645  dbus_bool_t result;
646 
647  result = FALSE;
648  if (!_dbus_string_init (&f))
649  {
650  _DBUS_SET_OOM (error);
651  return FALSE;
652  }
653 
654  if (!_dbus_string_append (&f, DBUS_CONSOLE_AUTH_DIR))
655  {
656  _DBUS_SET_OOM (error);
657  goto out;
658  }
659 
660  _dbus_string_init_const (&u, username);
661 
662  if (!_dbus_concat_dir_and_file (&f, &u))
663  {
664  _DBUS_SET_OOM (error);
665  goto out;
666  }
667 
668  result = _dbus_file_exists (_dbus_string_get_const_data (&f));
669 
670  out:
671  _dbus_string_free (&f);
672 
673  return result;
674 }
675 
676 
685 {
686  if (_dbus_string_get_length (filename) > 0)
687  return _dbus_string_get_byte (filename, 0) == '/';
688  else
689  return FALSE;
690 }
691 
701 _dbus_stat (const DBusString *filename,
702  DBusStat *statbuf,
703  DBusError *error)
704 {
705  const char *filename_c;
706  struct stat sb;
707 
708  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
709 
710  filename_c = _dbus_string_get_const_data (filename);
711 
712  if (stat (filename_c, &sb) < 0)
713  {
714  dbus_set_error (error, _dbus_error_from_errno (errno),
715  "%s", _dbus_strerror (errno));
716  return FALSE;
717  }
718 
719  statbuf->mode = sb.st_mode;
720  statbuf->nlink = sb.st_nlink;
721  statbuf->uid = sb.st_uid;
722  statbuf->gid = sb.st_gid;
723  statbuf->size = sb.st_size;
724  statbuf->atime = sb.st_atime;
725  statbuf->mtime = sb.st_mtime;
726  statbuf->ctime = sb.st_ctime;
727 
728  return TRUE;
729 }
730 
731 
736 {
737  DIR *d;
739 };
740 
750  DBusError *error)
751 {
752  DIR *d;
753  DBusDirIter *iter;
754  const char *filename_c;
755 
756  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
757 
758  filename_c = _dbus_string_get_const_data (filename);
759 
760  d = opendir (filename_c);
761  if (d == NULL)
762  {
763  dbus_set_error (error, _dbus_error_from_errno (errno),
764  "Failed to read directory \"%s\": %s",
765  filename_c,
766  _dbus_strerror (errno));
767  return NULL;
768  }
769  iter = dbus_new0 (DBusDirIter, 1);
770  if (iter == NULL)
771  {
772  closedir (d);
774  "Could not allocate memory for directory iterator");
775  return NULL;
776  }
777 
778  iter->d = d;
779 
780  return iter;
781 }
782 
798  DBusString *filename,
799  DBusError *error)
800 {
801  struct dirent *ent;
802  int err;
803 
804  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
805 
806  again:
807  errno = 0;
808  ent = readdir (iter->d);
809 
810  if (!ent)
811  {
812  err = errno;
813 
814  if (err != 0)
815  dbus_set_error (error,
817  "%s", _dbus_strerror (err));
818 
819  return FALSE;
820  }
821  else if (ent->d_name[0] == '.' &&
822  (ent->d_name[1] == '\0' ||
823  (ent->d_name[1] == '.' && ent->d_name[2] == '\0')))
824  goto again;
825  else
826  {
827  _dbus_string_set_length (filename, 0);
828  if (!_dbus_string_append (filename, ent->d_name))
829  {
831  "No memory to read directory entry");
832  return FALSE;
833  }
834  else
835  {
836  return TRUE;
837  }
838  }
839 }
840 
844 void
846 {
847  closedir (iter->d);
848  dbus_free (iter);
849 }
850 
851 static dbus_bool_t
852 fill_user_info_from_group (struct group *g,
853  DBusGroupInfo *info,
854  DBusError *error)
855 {
856  _dbus_assert (g->gr_name != NULL);
857 
858  info->gid = g->gr_gid;
859  info->groupname = _dbus_strdup (g->gr_name);
860 
861  /* info->members = dbus_strdupv (g->gr_mem) */
862 
863  if (info->groupname == NULL)
864  {
866  return FALSE;
867  }
868 
869  return TRUE;
870 }
871 
872 static dbus_bool_t
873 fill_group_info (DBusGroupInfo *info,
874  dbus_gid_t gid,
875  const DBusString *groupname,
876  DBusError *error)
877 {
878  const char *group_c_str;
879 
880  _dbus_assert (groupname != NULL || gid != DBUS_GID_UNSET);
881  _dbus_assert (groupname == NULL || gid == DBUS_GID_UNSET);
882 
883  if (groupname)
884  group_c_str = _dbus_string_get_const_data (groupname);
885  else
886  group_c_str = NULL;
887 
888  /* For now assuming that the getgrnam() and getgrgid() flavors
889  * always correspond to the pwnam flavors, if not we have
890  * to add more configure checks.
891  */
892 
893 #if defined (HAVE_POSIX_GETPWNAM_R) || defined (HAVE_NONPOSIX_GETPWNAM_R)
894  {
895  struct group *g;
896  int result;
897  size_t buflen;
898  char *buf;
899  struct group g_str;
900  dbus_bool_t b;
901 
902  /* retrieve maximum needed size for buf */
903  buflen = sysconf (_SC_GETGR_R_SIZE_MAX);
904 
905  /* sysconf actually returns a long, but everything else expects size_t,
906  * so just recast here.
907  * https://bugs.freedesktop.org/show_bug.cgi?id=17061
908  */
909  if ((long) buflen <= 0)
910  buflen = 1024;
911 
912  result = -1;
913  while (1)
914  {
915  buf = dbus_malloc (buflen);
916  if (buf == NULL)
917  {
919  return FALSE;
920  }
921 
922  g = NULL;
923 #ifdef HAVE_POSIX_GETPWNAM_R
924  if (group_c_str)
925  result = getgrnam_r (group_c_str, &g_str, buf, buflen,
926  &g);
927  else
928  result = getgrgid_r (gid, &g_str, buf, buflen,
929  &g);
930 #else
931  g = getgrnam_r (group_c_str, &g_str, buf, buflen);
932  result = 0;
933 #endif /* !HAVE_POSIX_GETPWNAM_R */
934  /* Try a bigger buffer if ERANGE was returned:
935  https://bugs.freedesktop.org/show_bug.cgi?id=16727
936  */
937  if (result == ERANGE && buflen < 512 * 1024)
938  {
939  dbus_free (buf);
940  buflen *= 2;
941  }
942  else
943  {
944  break;
945  }
946  }
947 
948  if (result == 0 && g == &g_str)
949  {
950  b = fill_user_info_from_group (g, info, error);
951  dbus_free (buf);
952  return b;
953  }
954  else
955  {
956  dbus_set_error (error, _dbus_error_from_errno (errno),
957  "Group %s unknown or failed to look it up\n",
958  group_c_str ? group_c_str : "???");
959  dbus_free (buf);
960  return FALSE;
961  }
962  }
963 #else /* ! HAVE_GETPWNAM_R */
964  {
965  /* I guess we're screwed on thread safety here */
966  struct group *g;
967 
968  g = getgrnam (group_c_str);
969 
970  if (g != NULL)
971  {
972  return fill_user_info_from_group (g, info, error);
973  }
974  else
975  {
976  dbus_set_error (error, _dbus_error_from_errno (errno),
977  "Group %s unknown or failed to look it up\n",
978  group_c_str ? group_c_str : "???");
979  return FALSE;
980  }
981  }
982 #endif /* ! HAVE_GETPWNAM_R */
983 }
984 
996  const DBusString *groupname,
997  DBusError *error)
998 {
999  return fill_group_info (info, DBUS_GID_UNSET,
1000  groupname, error);
1001 
1002 }
1003 
1015  dbus_gid_t gid,
1016  DBusError *error)
1017 {
1018  return fill_group_info (info, gid, NULL, error);
1019 }
1020 
1031  dbus_uid_t *uid_p)
1032 {
1033  return _dbus_get_user_id (username, uid_p);
1034 
1035 }
1036 
1047  dbus_gid_t *gid_p)
1048 {
1049  return _dbus_get_group_id (groupname, gid_p);
1050 }
1051 
1064  dbus_gid_t **group_ids,
1065  int *n_group_ids)
1066 {
1067  return _dbus_groups_from_uid (uid, group_ids, n_group_ids);
1068 }
1069 
1081  DBusError *error)
1082 {
1083  return _dbus_is_console_user (uid, error);
1084 
1085 }
1086 
1096 {
1097  return uid == _dbus_geteuid ();
1098 }
1099 
1108 _dbus_windows_user_is_process_owner (const char *windows_sid)
1109 {
1110  return FALSE;
1111 }
1112  /* End of DBusInternalsUtils functions */
1114 
1128  DBusString *dirname)
1129 {
1130  int sep;
1131 
1132  _dbus_assert (filename != dirname);
1133  _dbus_assert (filename != NULL);
1134  _dbus_assert (dirname != NULL);
1135 
1136  /* Ignore any separators on the end */
1137  sep = _dbus_string_get_length (filename);
1138  if (sep == 0)
1139  return _dbus_string_append (dirname, "."); /* empty string passed in */
1140 
1141  while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
1142  --sep;
1143 
1144  _dbus_assert (sep >= 0);
1145 
1146  if (sep == 0)
1147  return _dbus_string_append (dirname, "/");
1148 
1149  /* Now find the previous separator */
1150  _dbus_string_find_byte_backward (filename, sep, '/', &sep);
1151  if (sep < 0)
1152  return _dbus_string_append (dirname, ".");
1153 
1154  /* skip multiple separators */
1155  while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
1156  --sep;
1157 
1158  _dbus_assert (sep >= 0);
1159 
1160  if (sep == 0 &&
1161  _dbus_string_get_byte (filename, 0) == '/')
1162  return _dbus_string_append (dirname, "/");
1163  else
1164  return _dbus_string_copy_len (filename, 0, sep - 0,
1165  dirname, _dbus_string_get_length (dirname));
1166 } /* DBusString stuff */
1168 
1169 static void
1170 string_squash_nonprintable (DBusString *str)
1171 {
1172  unsigned char *buf;
1173  int i, len;
1174 
1175  buf = _dbus_string_get_data (str);
1176  len = _dbus_string_get_length (str);
1177 
1178  for (i = 0; i < len; i++)
1179  {
1180  unsigned char c = (unsigned char) buf[i];
1181  if (c == '\0')
1182  buf[i] = ' ';
1183  else if (c < 0x20 || c > 127)
1184  buf[i] = '?';
1185  }
1186 }
1187 
1202 dbus_bool_t
1203 _dbus_command_for_pid (unsigned long pid,
1204  DBusString *str,
1205  int max_len,
1206  DBusError *error)
1207 {
1208  /* This is all Linux-specific for now */
1209  DBusString path;
1210  DBusString cmdline;
1211  int fd;
1212 
1213  if (!_dbus_string_init (&path))
1214  {
1215  _DBUS_SET_OOM (error);
1216  return FALSE;
1217  }
1218 
1219  if (!_dbus_string_init (&cmdline))
1220  {
1221  _DBUS_SET_OOM (error);
1222  _dbus_string_free (&path);
1223  return FALSE;
1224  }
1225 
1226  if (!_dbus_string_append_printf (&path, "/proc/%ld/cmdline", pid))
1227  goto oom;
1228 
1229  fd = open (_dbus_string_get_const_data (&path), O_RDONLY);
1230  if (fd < 0)
1231  {
1232  dbus_set_error (error,
1233  _dbus_error_from_errno (errno),
1234  "Failed to open \"%s\": %s",
1235  _dbus_string_get_const_data (&path),
1236  _dbus_strerror (errno));
1237  goto fail;
1238  }
1239 
1240  if (!_dbus_read (fd, &cmdline, max_len))
1241  {
1242  dbus_set_error (error,
1243  _dbus_error_from_errno (errno),
1244  "Failed to read from \"%s\": %s",
1245  _dbus_string_get_const_data (&path),
1246  _dbus_strerror (errno));
1247  _dbus_close (fd, NULL);
1248  goto fail;
1249  }
1250 
1251  if (!_dbus_close (fd, error))
1252  goto fail;
1253 
1254  string_squash_nonprintable (&cmdline);
1255 
1256  if (!_dbus_string_copy (&cmdline, 0, str, _dbus_string_get_length (str)))
1257  goto oom;
1258 
1259  _dbus_string_free (&cmdline);
1260  _dbus_string_free (&path);
1261  return TRUE;
1262 oom:
1263  _DBUS_SET_OOM (error);
1264 fail:
1265  _dbus_string_free (&cmdline);
1266  _dbus_string_free (&path);
1267  return FALSE;
1268 }
1269 
1270 /*
1271  * replaces the term DBUS_PREFIX in configure_time_path by the
1272  * current dbus installation directory. On unix this function is a noop
1273  *
1274  * @param configure_time_path
1275  * @return real path
1276  */
1277 const char *
1278 _dbus_replace_install_prefix (const char *configure_time_path)
1279 {
1280  return configure_time_path;
1281 }
1282 
1283 #define DBUS_UNIX_STANDARD_SESSION_SERVICEDIR "/dbus-1/services"
1284 #define DBUS_UNIX_STANDARD_SYSTEM_SERVICEDIR "/dbus-1/system-services"
1285 
1305 {
1306  const char *xdg_data_home;
1307  const char *xdg_data_dirs;
1308  DBusString servicedir_path;
1309 
1310  if (!_dbus_string_init (&servicedir_path))
1311  return FALSE;
1312 
1313  xdg_data_home = _dbus_getenv ("XDG_DATA_HOME");
1314  xdg_data_dirs = _dbus_getenv ("XDG_DATA_DIRS");
1315 
1316  if (xdg_data_home != NULL)
1317  {
1318  if (!_dbus_string_append (&servicedir_path, xdg_data_home))
1319  goto oom;
1320  }
1321  else
1322  {
1323  const DBusString *homedir;
1324  DBusString local_share;
1325 
1326  if (!_dbus_homedir_from_current_process (&homedir))
1327  goto oom;
1328 
1329  if (!_dbus_string_append (&servicedir_path, _dbus_string_get_const_data (homedir)))
1330  goto oom;
1331 
1332  _dbus_string_init_const (&local_share, "/.local/share");
1333  if (!_dbus_concat_dir_and_file (&servicedir_path, &local_share))
1334  goto oom;
1335  }
1336 
1337  if (!_dbus_string_append (&servicedir_path, ":"))
1338  goto oom;
1339 
1340  if (xdg_data_dirs != NULL)
1341  {
1342  if (!_dbus_string_append (&servicedir_path, xdg_data_dirs))
1343  goto oom;
1344 
1345  if (!_dbus_string_append (&servicedir_path, ":"))
1346  goto oom;
1347  }
1348  else
1349  {
1350  if (!_dbus_string_append (&servicedir_path, "/usr/local/share:/usr/share:"))
1351  goto oom;
1352  }
1353 
1354  /*
1355  * add configured datadir to defaults
1356  * this may be the same as an xdg dir
1357  * however the config parser should take
1358  * care of duplicates
1359  */
1360  if (!_dbus_string_append (&servicedir_path, DBUS_DATADIR))
1361  goto oom;
1362 
1363  if (!_dbus_split_paths_and_append (&servicedir_path,
1364  DBUS_UNIX_STANDARD_SESSION_SERVICEDIR,
1365  dirs))
1366  goto oom;
1367 
1368  _dbus_string_free (&servicedir_path);
1369  return TRUE;
1370 
1371  oom:
1372  _dbus_string_free (&servicedir_path);
1373  return FALSE;
1374 }
1375 
1376 
1397 {
1398  /*
1399  * DBUS_DATADIR may be the same as one of the standard directories. However,
1400  * the config parser should take care of the duplicates.
1401  *
1402  * Also, append /lib as counterpart of /usr/share on the root
1403  * directory (the root directory does not know /share), in order to
1404  * facilitate early boot system bus activation where /usr might not
1405  * be available.
1406  */
1407  static const char standard_search_path[] =
1408  "/usr/local/share:"
1409  "/usr/share:"
1410  DBUS_DATADIR ":"
1411  "/lib";
1412  DBusString servicedir_path;
1413 
1414  _dbus_string_init_const (&servicedir_path, standard_search_path);
1415 
1416  return _dbus_split_paths_and_append (&servicedir_path,
1417  DBUS_UNIX_STANDARD_SYSTEM_SERVICEDIR,
1418  dirs);
1419 }
1420 
1431 {
1432  return _dbus_string_append (str, DBUS_SYSTEM_CONFIG_FILE);
1433 }
1434 
1443 {
1444  return _dbus_string_append (str, DBUS_SESSION_CONFIG_FILE);
1445 }
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:918
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:402
#define DBUS_ERROR_NOT_SUPPORTED
Requested operation isn't supported (like ENOSYS on UNIX).
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:110
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:409
dbus_bool_t _dbus_get_standard_session_servicedirs(DBusList **dirs)
Returns the standard directories for a session bus to look for service activation files...
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_pid_t _dbus_getpid(void)
Gets our process ID.
dbus_bool_t _dbus_command_for_pid(unsigned long pid, DBusString *str, int max_len, DBusError *error)
Get a printable string describing the command used to execute the process with pid.
dbus_bool_t _dbus_string_copy(const DBusString *source, int start, DBusString *dest, int insert_at)
Like _dbus_string_move(), but does not delete the section of the source string that's copied to the d...
Definition: dbus-string.c:1265
char * groupname
Group name.
void(* DBusSignalHandler)(int sig)
A UNIX signal handler.
Definition: dbus-sysdeps.h:444
const char * _dbus_getenv(const char *varname)
Wrapper for getenv().
Definition: dbus-sysdeps.c:185
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:614
Internals of directory iterator.
unsigned long mode
File mode.
Definition: dbus-sysdeps.h:404
unsigned long dbus_pid_t
A process ID.
Definition: dbus-sysdeps.h:96
dbus_bool_t _dbus_get_user_id_and_primary_group(const DBusString *username, dbus_uid_t *uid_p, dbus_gid_t *gid_p)
Gets user ID and primary group given username.
dbus_bool_t _dbus_change_to_daemon_user(const char *user, DBusError *error)
Changes the user and group the bus is running as.
void * dbus_malloc(size_t bytes)
Allocates the given number of bytes, as with standard malloc().
Definition: dbus-memory.c:461
dbus_gid_t gid
Group owning file.
Definition: dbus-sysdeps.h:407
dbus_bool_t _dbus_concat_dir_and_file(DBusString *dir, const DBusString *next_component)
Appends the given filename to the given directory.
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
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
DIR * d
The DIR* from opendir()
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
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:1096
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:411
dbus_bool_t _dbus_close(int fd, DBusError *error)
Closes a file descriptor.
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init().
Definition: dbus-string.c:242
#define DBUS_GID_UNSET
an invalid GID used to represent an uninitialized dbus_gid_t field
Definition: dbus-sysdeps.h:107
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:405
#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:406
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_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
dbus_bool_t _dbus_unix_user_is_process_owner(dbus_uid_t uid)
Checks to see if the UNIX user ID matches the UID of the process.
dbus_bool_t _dbus_user_at_console(const char *username, DBusError *error)
Checks if user is at the console.
dbus_bool_t _dbus_windows_user_is_process_owner(const char *windows_sid)
Checks to see if the Windows user SID matches the owner of the process.
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
#define FALSE
Expands to "0".
unsigned long mtime
Modify time.
Definition: dbus-sysdeps.h:410
dbus_bool_t _dbus_string_set_length(DBusString *str, int length)
Sets the length of a string.
Definition: dbus-string.c:785
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:1357
dbus_gid_t gid
GID.
dbus_uid_t _dbus_geteuid(void)
Gets our effective UID.
unsigned long dbus_gid_t
A group ID.
Definition: dbus-sysdeps.h:100
unsigned long size
Size of file.
Definition: dbus-sysdeps.h:408
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.
unsigned long dbus_uid_t
A user ID.
Definition: dbus-sysdeps.h:98
dbus_bool_t _dbus_get_standard_system_servicedirs(DBusList **dirs)
Returns the standard directories for a system bus to look for service activation files.
int _dbus_read(int fd, DBusString *buffer, int count)
Thin wrapper around the read() system call that appends the data it reads to the DBusString buffer...
dbus_bool_t dbus_error_is_set(const DBusError *error)
Checks whether an error occurred (the error is set).
Definition: dbus-errors.c:329