D-Bus  1.6.18
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 #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  int dev_null_fd;
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  /* silently ignore failures here, if someone
114  * doesn't have /dev/null we may as well try
115  * to continue anyhow
116  */
117 
118  dev_null_fd = open ("/dev/null", O_RDWR);
119  if (dev_null_fd >= 0)
120  {
121  dup2 (dev_null_fd, 0);
122  dup2 (dev_null_fd, 1);
123 
124  s = _dbus_getenv ("DBUS_DEBUG_OUTPUT");
125  if (s == NULL || *s == '\0')
126  dup2 (dev_null_fd, 2);
127  else
128  _dbus_verbose ("keeping stderr open due to DBUS_DEBUG_OUTPUT\n");
129  close (dev_null_fd);
130  }
131 
132  if (!keep_umask)
133  {
134  /* Get a predictable umask */
135  _dbus_verbose ("setting umask\n");
136  umask (022);
137  }
138 
139  _dbus_verbose ("calling setsid()\n");
140  if (setsid () == -1)
141  _dbus_assert_not_reached ("setsid() failed");
142 
143  break;
144 
145  default:
146  if (!_dbus_write_pid_to_file_and_pipe (pidfile, print_pid_pipe,
147  child_pid, error))
148  {
149  _dbus_verbose ("pid file or pipe write failed: %s\n",
150  error->message);
151  kill (child_pid, SIGTERM);
152  return FALSE;
153  }
154 
155  _dbus_verbose ("parent exiting\n");
156  _exit (0);
157  break;
158  }
159 
160  return TRUE;
161 }
162 
163 
172 static dbus_bool_t
173 _dbus_write_pid_file (const DBusString *filename,
174  unsigned long pid,
175  DBusError *error)
176 {
177  const char *cfilename;
178  int fd;
179  FILE *f;
180 
181  cfilename = _dbus_string_get_const_data (filename);
182 
183  fd = open (cfilename, O_WRONLY|O_CREAT|O_EXCL|O_BINARY, 0644);
184 
185  if (fd < 0)
186  {
187  dbus_set_error (error, _dbus_error_from_errno (errno),
188  "Failed to open \"%s\": %s", cfilename,
189  _dbus_strerror (errno));
190  return FALSE;
191  }
192 
193  if ((f = fdopen (fd, "w")) == NULL)
194  {
195  dbus_set_error (error, _dbus_error_from_errno (errno),
196  "Failed to fdopen fd %d: %s", fd, _dbus_strerror (errno));
197  _dbus_close (fd, NULL);
198  return FALSE;
199  }
200 
201  if (fprintf (f, "%lu\n", pid) < 0)
202  {
203  dbus_set_error (error, _dbus_error_from_errno (errno),
204  "Failed to write to \"%s\": %s", cfilename,
205  _dbus_strerror (errno));
206 
207  fclose (f);
208  return FALSE;
209  }
210 
211  if (fclose (f) == EOF)
212  {
213  dbus_set_error (error, _dbus_error_from_errno (errno),
214  "Failed to close \"%s\": %s", cfilename,
215  _dbus_strerror (errno));
216  return FALSE;
217  }
218 
219  return TRUE;
220 }
221 
235  DBusPipe *print_pid_pipe,
236  dbus_pid_t pid_to_write,
237  DBusError *error)
238 {
239  if (pidfile)
240  {
241  _dbus_verbose ("writing pid file %s\n", _dbus_string_get_const_data (pidfile));
242  if (!_dbus_write_pid_file (pidfile,
243  pid_to_write,
244  error))
245  {
246  _dbus_verbose ("pid file write failed\n");
247  _DBUS_ASSERT_ERROR_IS_SET(error);
248  return FALSE;
249  }
250  }
251  else
252  {
253  _dbus_verbose ("No pid file requested\n");
254  }
255 
256  if (print_pid_pipe != NULL && _dbus_pipe_is_valid (print_pid_pipe))
257  {
258  DBusString pid;
259  int bytes;
260 
261  _dbus_verbose ("writing our pid to pipe %d\n",
262  print_pid_pipe->fd);
263 
264  if (!_dbus_string_init (&pid))
265  {
266  _DBUS_SET_OOM (error);
267  return FALSE;
268  }
269 
270  if (!_dbus_string_append_int (&pid, pid_to_write) ||
271  !_dbus_string_append (&pid, "\n"))
272  {
273  _dbus_string_free (&pid);
274  _DBUS_SET_OOM (error);
275  return FALSE;
276  }
277 
278  bytes = _dbus_string_get_length (&pid);
279  if (_dbus_pipe_write (print_pid_pipe, &pid, 0, bytes, error) != bytes)
280  {
281  /* _dbus_pipe_write sets error only on failure, not short write */
282  if (error != NULL && !dbus_error_is_set(error))
283  {
285  "Printing message bus PID: did not write enough bytes\n");
286  }
287  _dbus_string_free (&pid);
288  return FALSE;
289  }
290 
291  _dbus_string_free (&pid);
292  }
293  else
294  {
295  _dbus_verbose ("No pid pipe to write to\n");
296  }
297 
298  return TRUE;
299 }
300 
308 _dbus_verify_daemon_user (const char *user)
309 {
310  DBusString u;
311 
312  _dbus_string_init_const (&u, user);
313 
315 }
316 
317 
318 /* The HAVE_LIBAUDIT case lives in selinux.c */
319 #ifndef HAVE_LIBAUDIT
320 
328 _dbus_change_to_daemon_user (const char *user,
329  DBusError *error)
330 {
331  dbus_uid_t uid;
332  dbus_gid_t gid;
333  DBusString u;
334 
335  _dbus_string_init_const (&u, user);
336 
337  if (!_dbus_get_user_id_and_primary_group (&u, &uid, &gid))
338  {
340  "User '%s' does not appear to exist?",
341  user);
342  return FALSE;
343  }
344 
345  /* setgroups() only works if we are a privileged process,
346  * so we don't return error on failure; the only possible
347  * failure is that we don't have perms to do it.
348  *
349  * not sure this is right, maybe if setuid()
350  * is going to work then setgroups() should also work.
351  */
352  if (setgroups (0, NULL) < 0)
353  _dbus_warn ("Failed to drop supplementary groups: %s\n",
354  _dbus_strerror (errno));
355 
356  /* Set GID first, or the setuid may remove our permission
357  * to change the GID
358  */
359  if (setgid (gid) < 0)
360  {
361  dbus_set_error (error, _dbus_error_from_errno (errno),
362  "Failed to set GID to %lu: %s", gid,
363  _dbus_strerror (errno));
364  return FALSE;
365  }
366 
367  if (setuid (uid) < 0)
368  {
369  dbus_set_error (error, _dbus_error_from_errno (errno),
370  "Failed to set UID to %lu: %s", uid,
371  _dbus_strerror (errno));
372  return FALSE;
373  }
374 
375  return TRUE;
376 }
377 #endif /* !HAVE_LIBAUDIT */
378 
379 #ifdef HAVE_SETRLIMIT
380 
381 /* We assume that if we have setrlimit, we also have getrlimit and
382  * struct rlimit.
383  */
384 
385 struct DBusRLimit {
386  struct rlimit lim;
387 };
388 
389 DBusRLimit *
390 _dbus_rlimit_save_fd_limit (DBusError *error)
391 {
392  DBusRLimit *self;
393 
394  self = dbus_new0 (DBusRLimit, 1);
395 
396  if (self == NULL)
397  {
398  _DBUS_SET_OOM (error);
399  return NULL;
400  }
401 
402  if (getrlimit (RLIMIT_NOFILE, &self->lim) < 0)
403  {
404  dbus_set_error (error, _dbus_error_from_errno (errno),
405  "Failed to get fd limit: %s", _dbus_strerror (errno));
406  dbus_free (self);
407  return NULL;
408  }
409 
410  return self;
411 }
412 
414 _dbus_rlimit_raise_fd_limit_if_privileged (unsigned int desired,
415  DBusError *error)
416 {
417  struct rlimit lim;
418 
419  /* No point to doing this practically speaking
420  * if we're not uid 0. We expect the system
421  * bus to use this before we change UID, and
422  * the session bus takes the Linux default,
423  * currently 1024 for cur and 4096 for max.
424  */
425  if (getuid () != 0)
426  {
427  /* not an error, we're probably the session bus */
428  return TRUE;
429  }
430 
431  if (getrlimit (RLIMIT_NOFILE, &lim) < 0)
432  {
433  dbus_set_error (error, _dbus_error_from_errno (errno),
434  "Failed to get fd limit: %s", _dbus_strerror (errno));
435  return FALSE;
436  }
437 
438  if (lim.rlim_cur == RLIM_INFINITY || lim.rlim_cur >= desired)
439  {
440  /* not an error, everything is fine */
441  return TRUE;
442  }
443 
444  /* Ignore "maximum limit", assume we have the "superuser"
445  * privileges. On Linux this is CAP_SYS_RESOURCE.
446  */
447  lim.rlim_cur = lim.rlim_max = desired;
448 
449  if (setrlimit (RLIMIT_NOFILE, &lim) < 0)
450  {
451  dbus_set_error (error, _dbus_error_from_errno (errno),
452  "Failed to set fd limit to %u: %s",
453  desired, _dbus_strerror (errno));
454  return FALSE;
455  }
456 
457  return TRUE;
458 }
459 
461 _dbus_rlimit_restore_fd_limit (DBusRLimit *saved,
462  DBusError *error)
463 {
464  if (setrlimit (RLIMIT_NOFILE, &saved->lim) < 0)
465  {
466  dbus_set_error (error, _dbus_error_from_errno (errno),
467  "Failed to restore old fd limit: %s",
468  _dbus_strerror (errno));
469  return FALSE;
470  }
471 
472  return TRUE;
473 }
474 
475 #else /* !HAVE_SETRLIMIT */
476 
477 static void
478 fd_limit_not_supported (DBusError *error)
479 {
481  "cannot change fd limit on this platform");
482 }
483 
484 DBusRLimit *
485 _dbus_rlimit_save_fd_limit (DBusError *error)
486 {
487  fd_limit_not_supported (error);
488  return NULL;
489 }
490 
492 _dbus_rlimit_raise_fd_limit_if_privileged (unsigned int desired,
493  DBusError *error)
494 {
495  fd_limit_not_supported (error);
496  return FALSE;
497 }
498 
500 _dbus_rlimit_restore_fd_limit (DBusRLimit *saved,
501  DBusError *error)
502 {
503  fd_limit_not_supported (error);
504  return FALSE;
505 }
506 
507 #endif
508 
509 void
510 _dbus_rlimit_free (DBusRLimit *lim)
511 {
512  dbus_free (lim);
513 }
514 
515 void
516 _dbus_init_system_log (dbus_bool_t is_daemon)
517 {
518 #ifdef HAVE_SYSLOG_H
519  int logopts = LOG_PID;
520 
521 #ifdef HAVE_DECL_LOG_PERROR
522 #ifdef HAVE_SYSTEMD
523  if (!is_daemon || sd_booted () <= 0)
524 #endif
525  logopts |= LOG_PERROR;
526 #endif
527 
528  openlog ("dbus", logopts, LOG_DAEMON);
529 #endif
530 }
531 
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 #ifdef HAVE_SYSLOG_H
566  int flags;
567  switch (severity)
568  {
569  case DBUS_SYSTEM_LOG_INFO:
570  flags = LOG_DAEMON | LOG_NOTICE;
571  break;
572  case DBUS_SYSTEM_LOG_SECURITY:
573  flags = LOG_AUTH | LOG_NOTICE;
574  break;
575  case DBUS_SYSTEM_LOG_FATAL:
576  flags = LOG_DAEMON|LOG_CRIT;
577  break;
578  default:
579  return;
580  }
581 
582  vsyslog (flags, msg, args);
583 #endif
584 
585 #if !defined(HAVE_SYSLOG_H) || !HAVE_DECL_LOG_PERROR
586  {
587  /* vsyslog() won't write to stderr, so we'd better do it */
588  va_list tmp;
589 
590  DBUS_VA_COPY (tmp, args);
591  fprintf (stderr, "dbus[" DBUS_PID_FORMAT "]: ", _dbus_getpid ());
592  vfprintf (stderr, msg, tmp);
593  fputc ('\n', stderr);
594  va_end (tmp);
595  }
596 #endif
597 
598  if (severity == DBUS_SYSTEM_LOG_FATAL)
599  exit (1);
600 }
601 
607 void
609  DBusSignalHandler handler)
610 {
611  struct sigaction act;
612  sigset_t empty_mask;
613 
614  sigemptyset (&empty_mask);
615  act.sa_handler = handler;
616  act.sa_mask = empty_mask;
617  act.sa_flags = 0;
618  sigaction (sig, &act, NULL);
619 }
620 
627 _dbus_file_exists (const char *file)
628 {
629  return (access (file, F_OK) == 0);
630 }
631 
639 _dbus_user_at_console (const char *username,
640  DBusError *error)
641 {
642 
643  DBusString u, f;
644  dbus_bool_t result;
645 
646  result = FALSE;
647  if (!_dbus_string_init (&f))
648  {
649  _DBUS_SET_OOM (error);
650  return FALSE;
651  }
652 
653  if (!_dbus_string_append (&f, DBUS_CONSOLE_AUTH_DIR))
654  {
655  _DBUS_SET_OOM (error);
656  goto out;
657  }
658 
659  _dbus_string_init_const (&u, username);
660 
661  if (!_dbus_concat_dir_and_file (&f, &u))
662  {
663  _DBUS_SET_OOM (error);
664  goto out;
665  }
666 
668 
669  out:
670  _dbus_string_free (&f);
671 
672  return result;
673 }
674 
675 
684 {
685  if (_dbus_string_get_length (filename) > 0)
686  return _dbus_string_get_byte (filename, 0) == '/';
687  else
688  return FALSE;
689 }
690 
700 _dbus_stat (const DBusString *filename,
701  DBusStat *statbuf,
702  DBusError *error)
703 {
704  const char *filename_c;
705  struct stat sb;
706 
707  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
708 
709  filename_c = _dbus_string_get_const_data (filename);
710 
711  if (stat (filename_c, &sb) < 0)
712  {
713  dbus_set_error (error, _dbus_error_from_errno (errno),
714  "%s", _dbus_strerror (errno));
715  return FALSE;
716  }
717 
718  statbuf->mode = sb.st_mode;
719  statbuf->nlink = sb.st_nlink;
720  statbuf->uid = sb.st_uid;
721  statbuf->gid = sb.st_gid;
722  statbuf->size = sb.st_size;
723  statbuf->atime = sb.st_atime;
724  statbuf->mtime = sb.st_mtime;
725  statbuf->ctime = sb.st_ctime;
726 
727  return TRUE;
728 }
729 
730 
735 {
736  DIR *d;
738 };
739 
749  DBusError *error)
750 {
751  DIR *d;
752  DBusDirIter *iter;
753  const char *filename_c;
754 
755  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
756 
757  filename_c = _dbus_string_get_const_data (filename);
758 
759  d = opendir (filename_c);
760  if (d == NULL)
761  {
762  dbus_set_error (error, _dbus_error_from_errno (errno),
763  "Failed to read directory \"%s\": %s",
764  filename_c,
765  _dbus_strerror (errno));
766  return NULL;
767  }
768  iter = dbus_new0 (DBusDirIter, 1);
769  if (iter == NULL)
770  {
771  closedir (d);
773  "Could not allocate memory for directory iterator");
774  return NULL;
775  }
776 
777  iter->d = d;
778 
779  return iter;
780 }
781 
797  DBusString *filename,
798  DBusError *error)
799 {
800  struct dirent *ent;
801  int err;
802 
803  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
804 
805  again:
806  errno = 0;
807  ent = readdir (iter->d);
808 
809  if (!ent)
810  {
811  err = errno;
812 
813  if (err != 0)
814  dbus_set_error (error,
816  "%s", _dbus_strerror (err));
817 
818  return FALSE;
819  }
820  else if (ent->d_name[0] == '.' &&
821  (ent->d_name[1] == '\0' ||
822  (ent->d_name[1] == '.' && ent->d_name[2] == '\0')))
823  goto again;
824  else
825  {
826  _dbus_string_set_length (filename, 0);
827  if (!_dbus_string_append (filename, ent->d_name))
828  {
830  "No memory to read directory entry");
831  return FALSE;
832  }
833  else
834  {
835  return TRUE;
836  }
837  }
838 }
839 
843 void
845 {
846  closedir (iter->d);
847  dbus_free (iter);
848 }
849 
850 static dbus_bool_t
851 fill_user_info_from_group (struct group *g,
852  DBusGroupInfo *info,
853  DBusError *error)
854 {
855  _dbus_assert (g->gr_name != NULL);
856 
857  info->gid = g->gr_gid;
858  info->groupname = _dbus_strdup (g->gr_name);
859 
860  /* info->members = dbus_strdupv (g->gr_mem) */
861 
862  if (info->groupname == NULL)
863  {
865  return FALSE;
866  }
867 
868  return TRUE;
869 }
870 
871 static dbus_bool_t
872 fill_group_info (DBusGroupInfo *info,
873  dbus_gid_t gid,
874  const DBusString *groupname,
875  DBusError *error)
876 {
877  const char *group_c_str;
878 
879  _dbus_assert (groupname != NULL || gid != DBUS_GID_UNSET);
880  _dbus_assert (groupname == NULL || gid == DBUS_GID_UNSET);
881 
882  if (groupname)
883  group_c_str = _dbus_string_get_const_data (groupname);
884  else
885  group_c_str = NULL;
886 
887  /* For now assuming that the getgrnam() and getgrgid() flavors
888  * always correspond to the pwnam flavors, if not we have
889  * to add more configure checks.
890  */
891 
892 #if defined (HAVE_POSIX_GETPWNAM_R) || defined (HAVE_NONPOSIX_GETPWNAM_R)
893  {
894  struct group *g;
895  int result;
896  size_t buflen;
897  char *buf;
898  struct group g_str;
899  dbus_bool_t b;
900 
901  /* retrieve maximum needed size for buf */
902  buflen = sysconf (_SC_GETGR_R_SIZE_MAX);
903 
904  /* sysconf actually returns a long, but everything else expects size_t,
905  * so just recast here.
906  * https://bugs.freedesktop.org/show_bug.cgi?id=17061
907  */
908  if ((long) buflen <= 0)
909  buflen = 1024;
910 
911  result = -1;
912  while (1)
913  {
914  buf = dbus_malloc (buflen);
915  if (buf == NULL)
916  {
918  return FALSE;
919  }
920 
921  g = NULL;
922 #ifdef HAVE_POSIX_GETPWNAM_R
923  if (group_c_str)
924  result = getgrnam_r (group_c_str, &g_str, buf, buflen,
925  &g);
926  else
927  result = getgrgid_r (gid, &g_str, buf, buflen,
928  &g);
929 #else
930  g = getgrnam_r (group_c_str, &g_str, buf, buflen);
931  result = 0;
932 #endif /* !HAVE_POSIX_GETPWNAM_R */
933  /* Try a bigger buffer if ERANGE was returned:
934  https://bugs.freedesktop.org/show_bug.cgi?id=16727
935  */
936  if (result == ERANGE && buflen < 512 * 1024)
937  {
938  dbus_free (buf);
939  buflen *= 2;
940  }
941  else
942  {
943  break;
944  }
945  }
946 
947  if (result == 0 && g == &g_str)
948  {
949  b = fill_user_info_from_group (g, info, error);
950  dbus_free (buf);
951  return b;
952  }
953  else
954  {
955  dbus_set_error (error, _dbus_error_from_errno (errno),
956  "Group %s unknown or failed to look it up\n",
957  group_c_str ? group_c_str : "???");
958  dbus_free (buf);
959  return FALSE;
960  }
961  }
962 #else /* ! HAVE_GETPWNAM_R */
963  {
964  /* I guess we're screwed on thread safety here */
965  struct group *g;
966 
967  g = getgrnam (group_c_str);
968 
969  if (g != NULL)
970  {
971  return fill_user_info_from_group (g, info, error);
972  }
973  else
974  {
975  dbus_set_error (error, _dbus_error_from_errno (errno),
976  "Group %s unknown or failed to look it up\n",
977  group_c_str ? group_c_str : "???");
978  return FALSE;
979  }
980  }
981 #endif /* ! HAVE_GETPWNAM_R */
982 }
983 
995  const DBusString *groupname,
996  DBusError *error)
997 {
998  return fill_group_info (info, DBUS_GID_UNSET,
999  groupname, error);
1000 
1001 }
1002 
1014  dbus_gid_t gid,
1015  DBusError *error)
1016 {
1017  return fill_group_info (info, gid, NULL, error);
1018 }
1019 
1030  dbus_uid_t *uid_p)
1031 {
1032  return _dbus_get_user_id (username, uid_p);
1033 
1034 }
1035 
1046  dbus_gid_t *gid_p)
1047 {
1048  return _dbus_get_group_id (groupname, gid_p);
1049 }
1050 
1063  dbus_gid_t **group_ids,
1064  int *n_group_ids)
1065 {
1066  return _dbus_groups_from_uid (uid, group_ids, n_group_ids);
1067 }
1068 
1080  DBusError *error)
1081 {
1082  return _dbus_is_console_user (uid, error);
1083 
1084 }
1085 
1095 {
1096  return uid == _dbus_geteuid ();
1097 }
1098 
1107 _dbus_windows_user_is_process_owner (const char *windows_sid)
1108 {
1109  return FALSE;
1110 }
1111  /* End of DBusInternalsUtils functions */
1113 
1127  DBusString *dirname)
1128 {
1129  int sep;
1130 
1131  _dbus_assert (filename != dirname);
1132  _dbus_assert (filename != NULL);
1133  _dbus_assert (dirname != NULL);
1134 
1135  /* Ignore any separators on the end */
1136  sep = _dbus_string_get_length (filename);
1137  if (sep == 0)
1138  return _dbus_string_append (dirname, "."); /* empty string passed in */
1139 
1140  while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
1141  --sep;
1142 
1143  _dbus_assert (sep >= 0);
1144 
1145  if (sep == 0)
1146  return _dbus_string_append (dirname, "/");
1147 
1148  /* Now find the previous separator */
1149  _dbus_string_find_byte_backward (filename, sep, '/', &sep);
1150  if (sep < 0)
1151  return _dbus_string_append (dirname, ".");
1152 
1153  /* skip multiple separators */
1154  while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
1155  --sep;
1156 
1157  _dbus_assert (sep >= 0);
1158 
1159  if (sep == 0 &&
1160  _dbus_string_get_byte (filename, 0) == '/')
1161  return _dbus_string_append (dirname, "/");
1162  else
1163  return _dbus_string_copy_len (filename, 0, sep - 0,
1164  dirname, _dbus_string_get_length (dirname));
1165 } /* DBusString stuff */
1167 
1168 static void
1169 string_squash_nonprintable (DBusString *str)
1170 {
1171  unsigned char *buf;
1172  int i, len;
1173 
1174  buf = _dbus_string_get_data (str);
1175  len = _dbus_string_get_length (str);
1176 
1177  for (i = 0; i < len; i++)
1178  {
1179  unsigned char c = (unsigned char) buf[i];
1180  if (c == '\0')
1181  buf[i] = ' ';
1182  else if (c < 0x20 || c > 127)
1183  buf[i] = '?';
1184  }
1185 }
1186 
1201 dbus_bool_t
1202 _dbus_command_for_pid (unsigned long pid,
1203  DBusString *str,
1204  int max_len,
1205  DBusError *error)
1206 {
1207  /* This is all Linux-specific for now */
1208  DBusString path;
1209  DBusString cmdline;
1210  int fd;
1211 
1212  if (!_dbus_string_init (&path))
1213  {
1214  _DBUS_SET_OOM (error);
1215  return FALSE;
1216  }
1217 
1218  if (!_dbus_string_init (&cmdline))
1219  {
1220  _DBUS_SET_OOM (error);
1221  _dbus_string_free (&path);
1222  return FALSE;
1223  }
1224 
1225  if (!_dbus_string_append_printf (&path, "/proc/%ld/cmdline", pid))
1226  goto oom;
1227 
1228  fd = open (_dbus_string_get_const_data (&path), O_RDONLY);
1229  if (fd < 0)
1230  {
1231  dbus_set_error (error,
1232  _dbus_error_from_errno (errno),
1233  "Failed to open \"%s\": %s",
1235  _dbus_strerror (errno));
1236  goto fail;
1237  }
1238 
1239  if (!_dbus_read (fd, &cmdline, max_len))
1240  {
1241  dbus_set_error (error,
1242  _dbus_error_from_errno (errno),
1243  "Failed to read from \"%s\": %s",
1245  _dbus_strerror (errno));
1246  _dbus_close (fd, NULL);
1247  goto fail;
1248  }
1249 
1250  if (!_dbus_close (fd, error))
1251  goto fail;
1252 
1253  string_squash_nonprintable (&cmdline);
1254 
1255  if (!_dbus_string_copy (&cmdline, 0, str, _dbus_string_get_length (str)))
1256  goto oom;
1257 
1258  _dbus_string_free (&cmdline);
1259  _dbus_string_free (&path);
1260  return TRUE;
1261 oom:
1262  _DBUS_SET_OOM (error);
1263 fail:
1264  _dbus_string_free (&cmdline);
1265  _dbus_string_free (&path);
1266  return FALSE;
1267 }
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:921
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_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:700
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:391
#define DBUS_ERROR_NOT_SUPPORTED
Requested operation isn&#39;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:352
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:112
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:398
unsigned char _dbus_string_get_byte(const DBusString *str, int start)
Gets the byte at the given position.
Definition: dbus-string.c:548
dbus_bool_t _dbus_file_exists(const char *file)
File interface.
DBusDirIter * _dbus_directory_open(const DBusString *filename, DBusError *error)
Open a directory to iterate over.
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:175
dbus_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&#39;s copied to the d...
Definition: dbus-string.c:1288
char * groupname
Group name.
const char * _dbus_getenv(const char *varname)
Wrapper for getenv().
Definition: dbus-sysdeps.c:183
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:612
Internals of directory iterator.
unsigned long mode
File mode.
Definition: dbus-sysdeps.h:393
unsigned long dbus_pid_t
A process ID.
Definition: dbus-sysdeps.h:98
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:460
dbus_gid_t gid
Group owning file.
Definition: dbus-sysdeps.h:396
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
char * _dbus_string_get_data(DBusString *str)
Gets the raw character buffer from the string.
Definition: dbus-string.c:437
DIR * d
The DIR* from opendir()
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
dbus_bool_t _dbus_group_info_fill_gid(DBusGroupInfo *info, dbus_gid_t gid, DBusError *error)
Initializes the given DBusGroupInfo struct with information about the given group ID...
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:1119
int _dbus_string_get_length(const DBusString *str)
Gets the length of a string (not including nul termination).
Definition: dbus-string.c:725
void(* DBusSignalHandler)(int sig)
A UNIX signal handler.
Definition: dbus-sysdeps.h:433
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:400
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:109
#define TRUE
Expands to &quot;1&quot;.
unsigned long nlink
Number of hard links.
Definition: dbus-sysdeps.h:394
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
dbus_bool_t _dbus_write_pid_to_file_and_pipe(const DBusString *pidfile, DBusPipe *print_pid_pipe, dbus_pid_t pid_to_write, DBusError *error)
Writes the given pid_to_write to a pidfile (if non-NULL) and/or to a pipe (if non-NULL).
dbus_bool_t _dbus_group_info_fill(DBusGroupInfo *info, const DBusString *groupname, DBusError *error)
Initializes the given DBusGroupInfo struct with information about the given group name...
dbus_uid_t uid
User owning file.
Definition: dbus-sysdeps.h:395
void _dbus_system_logv(DBusSystemLogSeverity severity, const char *msg, va_list args)
Log a message to the system log file (e.g.
#define DBUS_ERROR_FAILED
A generic error; &quot;something went wrong&quot; - see the error message for more.
dbus_bool_t _dbus_verify_daemon_user(const char *user)
Verify that after the fork we can successfully change to this user.
dbus_bool_t _dbus_string_find_byte_backward(const DBusString *str, int start, unsigned char byte, int *found)
Find the given byte scanning backward from the given start.
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.
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 &quot;0&quot;.
unsigned long mtime
Modify time.
Definition: dbus-sysdeps.h:399
dbus_bool_t _dbus_string_set_length(DBusString *str, int length)
Sets the length of a string.
Definition: dbus-string.c:788
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:1380
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:102
unsigned long size
Size of file.
Definition: dbus-sysdeps.h:397
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.
unsigned long dbus_uid_t
A user ID.
Definition: dbus-sysdeps.h:100
const char * _dbus_string_get_const_data(const DBusString *str)
Gets the raw character buffer from a const string.
Definition: dbus-string.c:454
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