D-Bus  1.11.6
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_SYS_SYSLIMITS_H
54 #include <sys/syslimits.h>
55 #endif
56 
57 #ifdef HAVE_SYSTEMD
58 #include <systemd/sd-daemon.h>
59 #endif
60 
61 #ifndef O_BINARY
62 #define O_BINARY 0
63 #endif
64 
82  DBusPipe *print_pid_pipe,
83  DBusError *error,
84  dbus_bool_t keep_umask)
85 {
86  const char *s;
87  pid_t child_pid;
88  DBusEnsureStandardFdsFlags flags;
89 
90  _dbus_verbose ("Becoming a daemon...\n");
91 
92  _dbus_verbose ("chdir to /\n");
93  if (chdir ("/") < 0)
94  {
96  "Could not chdir() to root directory");
97  return FALSE;
98  }
99 
100  _dbus_verbose ("forking...\n");
101  switch ((child_pid = fork ()))
102  {
103  case -1:
104  _dbus_verbose ("fork failed\n");
105  dbus_set_error (error, _dbus_error_from_errno (errno),
106  "Failed to fork daemon: %s", _dbus_strerror (errno));
107  return FALSE;
108  break;
109 
110  case 0:
111  _dbus_verbose ("in child, closing std file descriptors\n");
112 
113  flags = DBUS_FORCE_STDIN_NULL | DBUS_FORCE_STDOUT_NULL;
114  s = _dbus_getenv ("DBUS_DEBUG_OUTPUT");
115 
116  if (s == NULL || *s == '\0')
117  flags |= DBUS_FORCE_STDERR_NULL;
118  else
119  _dbus_verbose ("keeping stderr open due to DBUS_DEBUG_OUTPUT\n");
120 
121  if (!_dbus_ensure_standard_fds (flags, &s))
122  {
123  _dbus_warn ("%s: %s", s, _dbus_strerror (errno));
124  _exit (1);
125  }
126 
127  if (!keep_umask)
128  {
129  /* Get a predictable umask */
130  _dbus_verbose ("setting umask\n");
131  umask (022);
132  }
133 
134  _dbus_verbose ("calling setsid()\n");
135  if (setsid () == -1)
136  _dbus_assert_not_reached ("setsid() failed");
137 
138  break;
139 
140  default:
141  if (!_dbus_write_pid_to_file_and_pipe (pidfile, print_pid_pipe,
142  child_pid, error))
143  {
144  _dbus_verbose ("pid file or pipe write failed: %s\n",
145  error->message);
146  kill (child_pid, SIGTERM);
147  return FALSE;
148  }
149 
150  _dbus_verbose ("parent exiting\n");
151  _exit (0);
152  break;
153  }
154 
155  return TRUE;
156 }
157 
158 
167 static dbus_bool_t
168 _dbus_write_pid_file (const DBusString *filename,
169  unsigned long pid,
170  DBusError *error)
171 {
172  const char *cfilename;
173  int fd;
174  FILE *f;
175 
176  cfilename = _dbus_string_get_const_data (filename);
177 
178  fd = open (cfilename, O_WRONLY|O_CREAT|O_EXCL|O_BINARY, 0644);
179 
180  if (fd < 0)
181  {
182  dbus_set_error (error, _dbus_error_from_errno (errno),
183  "Failed to open \"%s\": %s", cfilename,
184  _dbus_strerror (errno));
185  return FALSE;
186  }
187 
188  if ((f = fdopen (fd, "w")) == NULL)
189  {
190  dbus_set_error (error, _dbus_error_from_errno (errno),
191  "Failed to fdopen fd %d: %s", fd, _dbus_strerror (errno));
192  _dbus_close (fd, NULL);
193  return FALSE;
194  }
195 
196  if (fprintf (f, "%lu\n", pid) < 0)
197  {
198  dbus_set_error (error, _dbus_error_from_errno (errno),
199  "Failed to write to \"%s\": %s", cfilename,
200  _dbus_strerror (errno));
201 
202  fclose (f);
203  return FALSE;
204  }
205 
206  if (fclose (f) == EOF)
207  {
208  dbus_set_error (error, _dbus_error_from_errno (errno),
209  "Failed to close \"%s\": %s", cfilename,
210  _dbus_strerror (errno));
211  return FALSE;
212  }
213 
214  return TRUE;
215 }
216 
230  DBusPipe *print_pid_pipe,
231  dbus_pid_t pid_to_write,
232  DBusError *error)
233 {
234  if (pidfile)
235  {
236  _dbus_verbose ("writing pid file %s\n", _dbus_string_get_const_data (pidfile));
237  if (!_dbus_write_pid_file (pidfile,
238  pid_to_write,
239  error))
240  {
241  _dbus_verbose ("pid file write failed\n");
242  _DBUS_ASSERT_ERROR_IS_SET(error);
243  return FALSE;
244  }
245  }
246  else
247  {
248  _dbus_verbose ("No pid file requested\n");
249  }
250 
251  if (print_pid_pipe != NULL && _dbus_pipe_is_valid (print_pid_pipe))
252  {
253  DBusString pid;
254  int bytes;
255 
256  _dbus_verbose ("writing our pid to pipe %d\n",
257  print_pid_pipe->fd);
258 
259  if (!_dbus_string_init (&pid))
260  {
261  _DBUS_SET_OOM (error);
262  return FALSE;
263  }
264 
265  if (!_dbus_string_append_int (&pid, pid_to_write) ||
266  !_dbus_string_append (&pid, "\n"))
267  {
268  _dbus_string_free (&pid);
269  _DBUS_SET_OOM (error);
270  return FALSE;
271  }
272 
273  bytes = _dbus_string_get_length (&pid);
274  if (_dbus_pipe_write (print_pid_pipe, &pid, 0, bytes, error) != bytes)
275  {
276  /* _dbus_pipe_write sets error only on failure, not short write */
277  if (error != NULL && !dbus_error_is_set(error))
278  {
280  "Printing message bus PID: did not write enough bytes\n");
281  }
282  _dbus_string_free (&pid);
283  return FALSE;
284  }
285 
286  _dbus_string_free (&pid);
287  }
288  else
289  {
290  _dbus_verbose ("No pid pipe to write to\n");
291  }
292 
293  return TRUE;
294 }
295 
303 _dbus_verify_daemon_user (const char *user)
304 {
305  DBusString u;
306 
307  _dbus_string_init_const (&u, user);
308 
310 }
311 
312 
313 /* The HAVE_LIBAUDIT case lives in selinux.c */
314 #ifndef HAVE_LIBAUDIT
315 
323 _dbus_change_to_daemon_user (const char *user,
324  DBusError *error)
325 {
326  dbus_uid_t uid;
327  dbus_gid_t gid;
328  DBusString u;
329 
330  _dbus_string_init_const (&u, user);
331 
332  if (!_dbus_get_user_id_and_primary_group (&u, &uid, &gid))
333  {
335  "User '%s' does not appear to exist?",
336  user);
337  return FALSE;
338  }
339 
340  /* setgroups() only works if we are a privileged process,
341  * so we don't return error on failure; the only possible
342  * failure is that we don't have perms to do it.
343  *
344  * not sure this is right, maybe if setuid()
345  * is going to work then setgroups() should also work.
346  */
347  if (setgroups (0, NULL) < 0)
348  _dbus_warn ("Failed to drop supplementary groups: %s",
349  _dbus_strerror (errno));
350 
351  /* Set GID first, or the setuid may remove our permission
352  * to change the GID
353  */
354  if (setgid (gid) < 0)
355  {
356  dbus_set_error (error, _dbus_error_from_errno (errno),
357  "Failed to set GID to %lu: %s", gid,
358  _dbus_strerror (errno));
359  return FALSE;
360  }
361 
362  if (setuid (uid) < 0)
363  {
364  dbus_set_error (error, _dbus_error_from_errno (errno),
365  "Failed to set UID to %lu: %s", uid,
366  _dbus_strerror (errno));
367  return FALSE;
368  }
369 
370  return TRUE;
371 }
372 #endif /* !HAVE_LIBAUDIT */
373 
374 #ifdef HAVE_SETRLIMIT
375 
376 /* We assume that if we have setrlimit, we also have getrlimit and
377  * struct rlimit.
378  */
379 
380 struct DBusRLimit {
381  struct rlimit lim;
382 };
383 
384 DBusRLimit *
385 _dbus_rlimit_save_fd_limit (DBusError *error)
386 {
387  DBusRLimit *self;
388 
389  self = dbus_new0 (DBusRLimit, 1);
390 
391  if (self == NULL)
392  {
393  _DBUS_SET_OOM (error);
394  return NULL;
395  }
396 
397  if (getrlimit (RLIMIT_NOFILE, &self->lim) < 0)
398  {
399  dbus_set_error (error, _dbus_error_from_errno (errno),
400  "Failed to get fd limit: %s", _dbus_strerror (errno));
401  dbus_free (self);
402  return NULL;
403  }
404 
405  return self;
406 }
407 
409 _dbus_rlimit_raise_fd_limit_if_privileged (unsigned int desired,
410  DBusError *error)
411 {
412  struct rlimit lim;
413 
414  /* No point to doing this practically speaking
415  * if we're not uid 0. We expect the system
416  * bus to use this before we change UID, and
417  * the session bus takes the Linux default,
418  * currently 1024 for cur and 4096 for max.
419  */
420  if (getuid () != 0)
421  {
422  /* not an error, we're probably the session bus */
423  return TRUE;
424  }
425 
426  if (getrlimit (RLIMIT_NOFILE, &lim) < 0)
427  {
428  dbus_set_error (error, _dbus_error_from_errno (errno),
429  "Failed to get fd limit: %s", _dbus_strerror (errno));
430  return FALSE;
431  }
432 
433  if (lim.rlim_cur == RLIM_INFINITY || lim.rlim_cur >= desired)
434  {
435  /* not an error, everything is fine */
436  return TRUE;
437  }
438 
439  /* Ignore "maximum limit", assume we have the "superuser"
440  * privileges. On Linux this is CAP_SYS_RESOURCE.
441  */
442  lim.rlim_cur = lim.rlim_max = desired;
443 
444  if (setrlimit (RLIMIT_NOFILE, &lim) < 0)
445  {
446  dbus_set_error (error, _dbus_error_from_errno (errno),
447  "Failed to set fd limit to %u: %s",
448  desired, _dbus_strerror (errno));
449  return FALSE;
450  }
451 
452  return TRUE;
453 }
454 
456 _dbus_rlimit_restore_fd_limit (DBusRLimit *saved,
457  DBusError *error)
458 {
459  if (setrlimit (RLIMIT_NOFILE, &saved->lim) < 0)
460  {
461  dbus_set_error (error, _dbus_error_from_errno (errno),
462  "Failed to restore old fd limit: %s",
463  _dbus_strerror (errno));
464  return FALSE;
465  }
466 
467  return TRUE;
468 }
469 
470 #else /* !HAVE_SETRLIMIT */
471 
472 static void
473 fd_limit_not_supported (DBusError *error)
474 {
476  "cannot change fd limit on this platform");
477 }
478 
479 DBusRLimit *
480 _dbus_rlimit_save_fd_limit (DBusError *error)
481 {
482  fd_limit_not_supported (error);
483  return NULL;
484 }
485 
487 _dbus_rlimit_raise_fd_limit_if_privileged (unsigned int desired,
488  DBusError *error)
489 {
490  fd_limit_not_supported (error);
491  return FALSE;
492 }
493 
495 _dbus_rlimit_restore_fd_limit (DBusRLimit *saved,
496  DBusError *error)
497 {
498  fd_limit_not_supported (error);
499  return FALSE;
500 }
501 
502 #endif
503 
504 void
505 _dbus_rlimit_free (DBusRLimit *lim)
506 {
507  dbus_free (lim);
508 }
509 
515 void
517  DBusSignalHandler handler)
518 {
519  struct sigaction act;
520  sigset_t empty_mask;
521 
522  sigemptyset (&empty_mask);
523  act.sa_handler = handler;
524  act.sa_mask = empty_mask;
525  act.sa_flags = 0;
526  sigaction (sig, &act, NULL);
527 }
528 
535 _dbus_file_exists (const char *file)
536 {
537  return (access (file, F_OK) == 0);
538 }
539 
547 _dbus_user_at_console (const char *username,
548  DBusError *error)
549 {
550 
551  DBusString u, f;
552  dbus_bool_t result;
553 
554  result = FALSE;
555  if (!_dbus_string_init (&f))
556  {
557  _DBUS_SET_OOM (error);
558  return FALSE;
559  }
560 
561  if (!_dbus_string_append (&f, DBUS_CONSOLE_AUTH_DIR))
562  {
563  _DBUS_SET_OOM (error);
564  goto out;
565  }
566 
567  _dbus_string_init_const (&u, username);
568 
569  if (!_dbus_concat_dir_and_file (&f, &u))
570  {
571  _DBUS_SET_OOM (error);
572  goto out;
573  }
574 
575  result = _dbus_file_exists (_dbus_string_get_const_data (&f));
576 
577  out:
578  _dbus_string_free (&f);
579 
580  return result;
581 }
582 
583 
592 {
593  if (_dbus_string_get_length (filename) > 0)
594  return _dbus_string_get_byte (filename, 0) == '/';
595  else
596  return FALSE;
597 }
598 
608 _dbus_stat (const DBusString *filename,
609  DBusStat *statbuf,
610  DBusError *error)
611 {
612  const char *filename_c;
613  struct stat sb;
614 
615  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
616 
617  filename_c = _dbus_string_get_const_data (filename);
618 
619  if (stat (filename_c, &sb) < 0)
620  {
621  dbus_set_error (error, _dbus_error_from_errno (errno),
622  "%s", _dbus_strerror (errno));
623  return FALSE;
624  }
625 
626  statbuf->mode = sb.st_mode;
627  statbuf->nlink = sb.st_nlink;
628  statbuf->uid = sb.st_uid;
629  statbuf->gid = sb.st_gid;
630  statbuf->size = sb.st_size;
631  statbuf->atime = sb.st_atime;
632  statbuf->mtime = sb.st_mtime;
633  statbuf->ctime = sb.st_ctime;
634 
635  return TRUE;
636 }
637 
638 
643 {
644  DIR *d;
646 };
647 
657  DBusError *error)
658 {
659  DIR *d;
660  DBusDirIter *iter;
661  const char *filename_c;
662 
663  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
664 
665  filename_c = _dbus_string_get_const_data (filename);
666 
667  d = opendir (filename_c);
668  if (d == NULL)
669  {
670  dbus_set_error (error, _dbus_error_from_errno (errno),
671  "Failed to read directory \"%s\": %s",
672  filename_c,
673  _dbus_strerror (errno));
674  return NULL;
675  }
676  iter = dbus_new0 (DBusDirIter, 1);
677  if (iter == NULL)
678  {
679  closedir (d);
681  "Could not allocate memory for directory iterator");
682  return NULL;
683  }
684 
685  iter->d = d;
686 
687  return iter;
688 }
689 
705  DBusString *filename,
706  DBusError *error)
707 {
708  struct dirent *ent;
709  int err;
710 
711  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
712 
713  again:
714  errno = 0;
715  ent = readdir (iter->d);
716 
717  if (!ent)
718  {
719  err = errno;
720 
721  if (err != 0)
722  dbus_set_error (error,
724  "%s", _dbus_strerror (err));
725 
726  return FALSE;
727  }
728  else if (ent->d_name[0] == '.' &&
729  (ent->d_name[1] == '\0' ||
730  (ent->d_name[1] == '.' && ent->d_name[2] == '\0')))
731  goto again;
732  else
733  {
734  _dbus_string_set_length (filename, 0);
735  if (!_dbus_string_append (filename, ent->d_name))
736  {
738  "No memory to read directory entry");
739  return FALSE;
740  }
741  else
742  {
743  return TRUE;
744  }
745  }
746 }
747 
751 void
753 {
754  closedir (iter->d);
755  dbus_free (iter);
756 }
757 
758 static dbus_bool_t
759 fill_user_info_from_group (struct group *g,
760  DBusGroupInfo *info,
761  DBusError *error)
762 {
763  _dbus_assert (g->gr_name != NULL);
764 
765  info->gid = g->gr_gid;
766  info->groupname = _dbus_strdup (g->gr_name);
767 
768  /* info->members = dbus_strdupv (g->gr_mem) */
769 
770  if (info->groupname == NULL)
771  {
773  return FALSE;
774  }
775 
776  return TRUE;
777 }
778 
779 static dbus_bool_t
780 fill_group_info (DBusGroupInfo *info,
781  dbus_gid_t gid,
782  const DBusString *groupname,
783  DBusError *error)
784 {
785  const char *group_c_str;
786 
787  _dbus_assert (groupname != NULL || gid != DBUS_GID_UNSET);
788  _dbus_assert (groupname == NULL || gid == DBUS_GID_UNSET);
789 
790  if (groupname)
791  group_c_str = _dbus_string_get_const_data (groupname);
792  else
793  group_c_str = NULL;
794 
795  /* For now assuming that the getgrnam() and getgrgid() flavors
796  * always correspond to the pwnam flavors, if not we have
797  * to add more configure checks.
798  */
799 
800 #if defined (HAVE_POSIX_GETPWNAM_R) || defined (HAVE_NONPOSIX_GETPWNAM_R)
801  {
802  struct group *g;
803  int result;
804  size_t buflen;
805  char *buf;
806  struct group g_str;
807  dbus_bool_t b;
808 
809  /* retrieve maximum needed size for buf */
810  buflen = sysconf (_SC_GETGR_R_SIZE_MAX);
811 
812  /* sysconf actually returns a long, but everything else expects size_t,
813  * so just recast here.
814  * https://bugs.freedesktop.org/show_bug.cgi?id=17061
815  */
816  if ((long) buflen <= 0)
817  buflen = 1024;
818 
819  result = -1;
820  while (1)
821  {
822  buf = dbus_malloc (buflen);
823  if (buf == NULL)
824  {
826  return FALSE;
827  }
828 
829  g = NULL;
830 #ifdef HAVE_POSIX_GETPWNAM_R
831  if (group_c_str)
832  result = getgrnam_r (group_c_str, &g_str, buf, buflen,
833  &g);
834  else
835  result = getgrgid_r (gid, &g_str, buf, buflen,
836  &g);
837 #else
838  g = getgrnam_r (group_c_str, &g_str, buf, buflen);
839  result = 0;
840 #endif /* !HAVE_POSIX_GETPWNAM_R */
841  /* Try a bigger buffer if ERANGE was returned:
842  https://bugs.freedesktop.org/show_bug.cgi?id=16727
843  */
844  if (result == ERANGE && buflen < 512 * 1024)
845  {
846  dbus_free (buf);
847  buflen *= 2;
848  }
849  else
850  {
851  break;
852  }
853  }
854 
855  if (result == 0 && g == &g_str)
856  {
857  b = fill_user_info_from_group (g, info, error);
858  dbus_free (buf);
859  return b;
860  }
861  else
862  {
863  dbus_set_error (error, _dbus_error_from_errno (errno),
864  "Group %s unknown or failed to look it up\n",
865  group_c_str ? group_c_str : "???");
866  dbus_free (buf);
867  return FALSE;
868  }
869  }
870 #else /* ! HAVE_GETPWNAM_R */
871  {
872  /* I guess we're screwed on thread safety here */
873  struct group *g;
874 
875  g = getgrnam (group_c_str);
876 
877  if (g != NULL)
878  {
879  return fill_user_info_from_group (g, info, error);
880  }
881  else
882  {
883  dbus_set_error (error, _dbus_error_from_errno (errno),
884  "Group %s unknown or failed to look it up\n",
885  group_c_str ? group_c_str : "???");
886  return FALSE;
887  }
888  }
889 #endif /* ! HAVE_GETPWNAM_R */
890 }
891 
903  const DBusString *groupname,
904  DBusError *error)
905 {
906  return fill_group_info (info, DBUS_GID_UNSET,
907  groupname, error);
908 
909 }
910 
922  dbus_gid_t gid,
923  DBusError *error)
924 {
925  return fill_group_info (info, gid, NULL, error);
926 }
927 
938  dbus_uid_t *uid_p)
939 {
940  return _dbus_get_user_id (username, uid_p);
941 
942 }
943 
954  dbus_gid_t *gid_p)
955 {
956  return _dbus_get_group_id (groupname, gid_p);
957 }
958 
971  dbus_gid_t **group_ids,
972  int *n_group_ids)
973 {
974  return _dbus_groups_from_uid (uid, group_ids, n_group_ids);
975 }
976 
988  DBusError *error)
989 {
990  return _dbus_is_console_user (uid, error);
991 
992 }
993 
1003 {
1004  return uid == _dbus_geteuid ();
1005 }
1006 
1015 _dbus_windows_user_is_process_owner (const char *windows_sid)
1016 {
1017  return FALSE;
1018 }
1019  /* End of DBusInternalsUtils functions */
1021 
1035  DBusString *dirname)
1036 {
1037  int sep;
1038 
1039  _dbus_assert (filename != dirname);
1040  _dbus_assert (filename != NULL);
1041  _dbus_assert (dirname != NULL);
1042 
1043  /* Ignore any separators on the end */
1044  sep = _dbus_string_get_length (filename);
1045  if (sep == 0)
1046  return _dbus_string_append (dirname, "."); /* empty string passed in */
1047 
1048  while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
1049  --sep;
1050 
1051  _dbus_assert (sep >= 0);
1052 
1053  if (sep == 0)
1054  return _dbus_string_append (dirname, "/");
1055 
1056  /* Now find the previous separator */
1057  _dbus_string_find_byte_backward (filename, sep, '/', &sep);
1058  if (sep < 0)
1059  return _dbus_string_append (dirname, ".");
1060 
1061  /* skip multiple separators */
1062  while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
1063  --sep;
1064 
1065  _dbus_assert (sep >= 0);
1066 
1067  if (sep == 0 &&
1068  _dbus_string_get_byte (filename, 0) == '/')
1069  return _dbus_string_append (dirname, "/");
1070  else
1071  return _dbus_string_copy_len (filename, 0, sep - 0,
1072  dirname, _dbus_string_get_length (dirname));
1073 } /* DBusString stuff */
1075 
1076 static void
1077 string_squash_nonprintable (DBusString *str)
1078 {
1079  unsigned char *buf;
1080  int i, len;
1081 
1082  buf = _dbus_string_get_udata (str);
1083  len = _dbus_string_get_length (str);
1084 
1085  for (i = 0; i < len; i++)
1086  {
1087  unsigned char c = (unsigned char) buf[i];
1088  if (c == '\0')
1089  buf[i] = ' ';
1090  else if (c < 0x20 || c > 127)
1091  buf[i] = '?';
1092  }
1093 }
1094 
1109 dbus_bool_t
1110 _dbus_command_for_pid (unsigned long pid,
1111  DBusString *str,
1112  int max_len,
1113  DBusError *error)
1114 {
1115  /* This is all Linux-specific for now */
1116  DBusString path;
1117  DBusString cmdline;
1118  int fd;
1119 
1120  if (!_dbus_string_init (&path))
1121  {
1122  _DBUS_SET_OOM (error);
1123  return FALSE;
1124  }
1125 
1126  if (!_dbus_string_init (&cmdline))
1127  {
1128  _DBUS_SET_OOM (error);
1129  _dbus_string_free (&path);
1130  return FALSE;
1131  }
1132 
1133  if (!_dbus_string_append_printf (&path, "/proc/%ld/cmdline", pid))
1134  goto oom;
1135 
1136  fd = open (_dbus_string_get_const_data (&path), O_RDONLY);
1137  if (fd < 0)
1138  {
1139  dbus_set_error (error,
1140  _dbus_error_from_errno (errno),
1141  "Failed to open \"%s\": %s",
1142  _dbus_string_get_const_data (&path),
1143  _dbus_strerror (errno));
1144  goto fail;
1145  }
1146 
1147  if (!_dbus_read (fd, &cmdline, max_len))
1148  {
1149  dbus_set_error (error,
1150  _dbus_error_from_errno (errno),
1151  "Failed to read from \"%s\": %s",
1152  _dbus_string_get_const_data (&path),
1153  _dbus_strerror (errno));
1154  _dbus_close (fd, NULL);
1155  goto fail;
1156  }
1157 
1158  if (!_dbus_close (fd, error))
1159  goto fail;
1160 
1161  string_squash_nonprintable (&cmdline);
1162 
1163  if (!_dbus_string_copy (&cmdline, 0, str, _dbus_string_get_length (str)))
1164  goto oom;
1165 
1166  _dbus_string_free (&cmdline);
1167  _dbus_string_free (&path);
1168  return TRUE;
1169 oom:
1170  _DBUS_SET_OOM (error);
1171 fail:
1172  _dbus_string_free (&cmdline);
1173  _dbus_string_free (&path);
1174  return FALSE;
1175 }
1176 
1187 {
1188  return TRUE;
1189 }
1190 
1191 #define DBUS_UNIX_STANDARD_SESSION_SERVICEDIR "/dbus-1/services"
1192 #define DBUS_UNIX_STANDARD_SYSTEM_SERVICEDIR "/dbus-1/system-services"
1193 
1213 {
1214  const char *xdg_data_home;
1215  const char *xdg_data_dirs;
1216  DBusString servicedir_path;
1217 
1218  if (!_dbus_string_init (&servicedir_path))
1219  return FALSE;
1220 
1221  xdg_data_home = _dbus_getenv ("XDG_DATA_HOME");
1222  xdg_data_dirs = _dbus_getenv ("XDG_DATA_DIRS");
1223 
1224  if (xdg_data_home != NULL)
1225  {
1226  if (!_dbus_string_append (&servicedir_path, xdg_data_home))
1227  goto oom;
1228  }
1229  else
1230  {
1231  const DBusString *homedir;
1232  DBusString local_share;
1233 
1234  if (!_dbus_homedir_from_current_process (&homedir))
1235  goto oom;
1236 
1237  if (!_dbus_string_append (&servicedir_path, _dbus_string_get_const_data (homedir)))
1238  goto oom;
1239 
1240  _dbus_string_init_const (&local_share, "/.local/share");
1241  if (!_dbus_concat_dir_and_file (&servicedir_path, &local_share))
1242  goto oom;
1243  }
1244 
1245  if (!_dbus_string_append (&servicedir_path, ":"))
1246  goto oom;
1247 
1248  if (xdg_data_dirs != NULL)
1249  {
1250  if (!_dbus_string_append (&servicedir_path, xdg_data_dirs))
1251  goto oom;
1252 
1253  if (!_dbus_string_append (&servicedir_path, ":"))
1254  goto oom;
1255  }
1256  else
1257  {
1258  if (!_dbus_string_append (&servicedir_path, "/usr/local/share:/usr/share:"))
1259  goto oom;
1260  }
1261 
1262  /*
1263  * add configured datadir to defaults
1264  * this may be the same as an xdg dir
1265  * however the config parser should take
1266  * care of duplicates
1267  */
1268  if (!_dbus_string_append (&servicedir_path, DBUS_DATADIR))
1269  goto oom;
1270 
1271  if (!_dbus_split_paths_and_append (&servicedir_path,
1272  DBUS_UNIX_STANDARD_SESSION_SERVICEDIR,
1273  dirs))
1274  goto oom;
1275 
1276  _dbus_string_free (&servicedir_path);
1277  return TRUE;
1278 
1279  oom:
1280  _dbus_string_free (&servicedir_path);
1281  return FALSE;
1282 }
1283 
1284 
1305 {
1306  /*
1307  * DBUS_DATADIR may be the same as one of the standard directories. However,
1308  * the config parser should take care of the duplicates.
1309  *
1310  * Also, append /lib as counterpart of /usr/share on the root
1311  * directory (the root directory does not know /share), in order to
1312  * facilitate early boot system bus activation where /usr might not
1313  * be available.
1314  */
1315  static const char standard_search_path[] =
1316  "/usr/local/share:"
1317  "/usr/share:"
1318  DBUS_DATADIR ":"
1319  "/lib";
1320  DBusString servicedir_path;
1321 
1322  _dbus_string_init_const (&servicedir_path, standard_search_path);
1323 
1324  return _dbus_split_paths_and_append (&servicedir_path,
1325  DBUS_UNIX_STANDARD_SYSTEM_SERVICEDIR,
1326  dirs);
1327 }
1328 
1339 {
1340  _dbus_assert (_dbus_string_get_length (str) == 0);
1341 
1342  return _dbus_string_append (str, DBUS_SYSTEM_CONFIG_FILE);
1343 }
1344 
1353 {
1354  _dbus_assert (_dbus_string_get_length (str) == 0);
1355 
1356  return _dbus_string_append (str, DBUS_SESSION_CONFIG_FILE);
1357 }
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_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.
Portable struct with stat() results.
Definition: dbus-sysdeps.h:506
dbus_bool_t _dbus_ensure_standard_fds(DBusEnsureStandardFdsFlags flags, const char **error_str_p)
Ensure that the standard file descriptors stdin, stdout and stderr are open, by opening /dev/null if ...
#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.
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:513
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_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_get_system_config_file(DBusString *str)
Get the absolute path of the system.conf file (there is no system bus on Windows so this can just ret...
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.
void(* DBusSignalHandler)(int sig)
A UNIX signal handler.
Definition: dbus-sysdeps.h:550
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:508
unsigned long dbus_pid_t
A process ID.
Definition: dbus-sysdeps.h:106
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:511
#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_get_session_config_file(DBusString *str)
Get the absolute path of the session.conf file.
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:515
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:117
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:509
#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:510
#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_replace_install_prefix(DBusString *path)
Replace the DBUS_PREFIX in the given path, in-place, by the current D-Bus installation directory...
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:514
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:110
unsigned long size
Size of file.
Definition: dbus-sysdeps.h:512
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.
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:108
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