D-Bus  1.5.8
dbus-sysdeps-util-unix.c
00001 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
00002 /* dbus-sysdeps-util-unix.c Would be in dbus-sysdeps-unix.c, but not used in libdbus
00003  * 
00004  * Copyright (C) 2002, 2003, 2004, 2005  Red Hat, Inc.
00005  * Copyright (C) 2003 CodeFactory AB
00006  *
00007  * Licensed under the Academic Free License version 2.1
00008  * 
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  * 
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00022  *
00023  */
00024 
00025 #include <config.h>
00026 #include "dbus-sysdeps.h"
00027 #include "dbus-sysdeps-unix.h"
00028 #include "dbus-internals.h"
00029 #include "dbus-pipe.h"
00030 #include "dbus-protocol.h"
00031 #include "dbus-string.h"
00032 #define DBUS_USERDB_INCLUDES_PRIVATE 1
00033 #include "dbus-userdb.h"
00034 #include "dbus-test.h"
00035 
00036 #include <sys/types.h>
00037 #include <stdlib.h>
00038 #include <string.h>
00039 #include <signal.h>
00040 #include <unistd.h>
00041 #include <stdio.h>
00042 #include <errno.h>
00043 #include <fcntl.h>
00044 #include <sys/stat.h>
00045 #ifdef HAVE_SYS_RESOURCE_H
00046 #include <sys/resource.h>
00047 #endif
00048 #include <grp.h>
00049 #include <sys/socket.h>
00050 #include <dirent.h>
00051 #include <sys/un.h>
00052 #include <syslog.h>
00053 
00054 #ifdef HAVE_SYS_SYSLIMITS_H
00055 #include <sys/syslimits.h>
00056 #endif
00057 
00058 #ifndef O_BINARY
00059 #define O_BINARY 0
00060 #endif
00061 
00077 dbus_bool_t
00078 _dbus_become_daemon (const DBusString *pidfile,
00079                      DBusPipe         *print_pid_pipe,
00080                      DBusError        *error,
00081                      dbus_bool_t       keep_umask)
00082 {
00083   const char *s;
00084   pid_t child_pid;
00085   int dev_null_fd;
00086 
00087   _dbus_verbose ("Becoming a daemon...\n");
00088 
00089   _dbus_verbose ("chdir to /\n");
00090   if (chdir ("/") < 0)
00091     {
00092       dbus_set_error (error, DBUS_ERROR_FAILED,
00093                       "Could not chdir() to root directory");
00094       return FALSE;
00095     }
00096 
00097   _dbus_verbose ("forking...\n");
00098   switch ((child_pid = fork ()))
00099     {
00100     case -1:
00101       _dbus_verbose ("fork failed\n");
00102       dbus_set_error (error, _dbus_error_from_errno (errno),
00103                       "Failed to fork daemon: %s", _dbus_strerror (errno));
00104       return FALSE;
00105       break;
00106 
00107     case 0:
00108       _dbus_verbose ("in child, closing std file descriptors\n");
00109 
00110       /* silently ignore failures here, if someone
00111        * doesn't have /dev/null we may as well try
00112        * to continue anyhow
00113        */
00114       
00115       dev_null_fd = open ("/dev/null", O_RDWR);
00116       if (dev_null_fd >= 0)
00117         {
00118           dup2 (dev_null_fd, 0);
00119           dup2 (dev_null_fd, 1);
00120           
00121           s = _dbus_getenv ("DBUS_DEBUG_OUTPUT");
00122           if (s == NULL || *s == '\0')
00123             dup2 (dev_null_fd, 2);
00124           else
00125             _dbus_verbose ("keeping stderr open due to DBUS_DEBUG_OUTPUT\n");
00126         }
00127 
00128       if (!keep_umask)
00129         {
00130           /* Get a predictable umask */
00131           _dbus_verbose ("setting umask\n");
00132           umask (022);
00133         }
00134 
00135       _dbus_verbose ("calling setsid()\n");
00136       if (setsid () == -1)
00137         _dbus_assert_not_reached ("setsid() failed");
00138       
00139       break;
00140 
00141     default:
00142       if (!_dbus_write_pid_to_file_and_pipe (pidfile, print_pid_pipe,
00143                                              child_pid, error))
00144         {
00145           _dbus_verbose ("pid file or pipe write failed: %s\n",
00146                          error->message);
00147           kill (child_pid, SIGTERM);
00148           return FALSE;
00149         }
00150 
00151       _dbus_verbose ("parent exiting\n");
00152       _exit (0);
00153       break;
00154     }
00155   
00156   return TRUE;
00157 }
00158 
00159 
00168 static dbus_bool_t
00169 _dbus_write_pid_file (const DBusString *filename,
00170                       unsigned long     pid,
00171                       DBusError        *error)
00172 {
00173   const char *cfilename;
00174   int fd;
00175   FILE *f;
00176 
00177   cfilename = _dbus_string_get_const_data (filename);
00178   
00179   fd = open (cfilename, O_WRONLY|O_CREAT|O_EXCL|O_BINARY, 0644);
00180   
00181   if (fd < 0)
00182     {
00183       dbus_set_error (error, _dbus_error_from_errno (errno),
00184                       "Failed to open \"%s\": %s", cfilename,
00185                       _dbus_strerror (errno));
00186       return FALSE;
00187     }
00188 
00189   if ((f = fdopen (fd, "w")) == NULL)
00190     {
00191       dbus_set_error (error, _dbus_error_from_errno (errno),
00192                       "Failed to fdopen fd %d: %s", fd, _dbus_strerror (errno));
00193       _dbus_close (fd, NULL);
00194       return FALSE;
00195     }
00196   
00197   if (fprintf (f, "%lu\n", pid) < 0)
00198     {
00199       dbus_set_error (error, _dbus_error_from_errno (errno),
00200                       "Failed to write to \"%s\": %s", cfilename,
00201                       _dbus_strerror (errno));
00202       
00203       fclose (f);
00204       return FALSE;
00205     }
00206 
00207   if (fclose (f) == EOF)
00208     {
00209       dbus_set_error (error, _dbus_error_from_errno (errno),
00210                       "Failed to close \"%s\": %s", cfilename,
00211                       _dbus_strerror (errno));
00212       return FALSE;
00213     }
00214   
00215   return TRUE;
00216 }
00217 
00229 dbus_bool_t
00230 _dbus_write_pid_to_file_and_pipe (const DBusString *pidfile,
00231                                   DBusPipe         *print_pid_pipe,
00232                                   dbus_pid_t        pid_to_write,
00233                                   DBusError        *error)
00234 {
00235   if (pidfile)
00236     {
00237       _dbus_verbose ("writing pid file %s\n", _dbus_string_get_const_data (pidfile));
00238       if (!_dbus_write_pid_file (pidfile,
00239                                  pid_to_write,
00240                                  error))
00241         {
00242           _dbus_verbose ("pid file write failed\n");
00243           _DBUS_ASSERT_ERROR_IS_SET(error);
00244           return FALSE;
00245         }
00246     }
00247   else
00248     {
00249       _dbus_verbose ("No pid file requested\n");
00250     }
00251 
00252   if (print_pid_pipe != NULL && _dbus_pipe_is_valid (print_pid_pipe))
00253     {
00254       DBusString pid;
00255       int bytes;
00256 
00257       _dbus_verbose ("writing our pid to pipe %"PRIuPTR"\n",
00258                      print_pid_pipe->fd_or_handle);
00259       
00260       if (!_dbus_string_init (&pid))
00261         {
00262           _DBUS_SET_OOM (error);
00263           return FALSE;
00264         }
00265           
00266       if (!_dbus_string_append_int (&pid, pid_to_write) ||
00267           !_dbus_string_append (&pid, "\n"))
00268         {
00269           _dbus_string_free (&pid);
00270           _DBUS_SET_OOM (error);
00271           return FALSE;
00272         }
00273           
00274       bytes = _dbus_string_get_length (&pid);
00275       if (_dbus_pipe_write (print_pid_pipe, &pid, 0, bytes, error) != bytes)
00276         {
00277           /* _dbus_pipe_write sets error only on failure, not short write */
00278           if (error != NULL && !dbus_error_is_set(error))
00279             {
00280               dbus_set_error (error, DBUS_ERROR_FAILED,
00281                               "Printing message bus PID: did not write enough bytes\n");
00282             }
00283           _dbus_string_free (&pid);
00284           return FALSE;
00285         }
00286           
00287       _dbus_string_free (&pid);
00288     }
00289   else
00290     {
00291       _dbus_verbose ("No pid pipe to write to\n");
00292     }
00293 
00294   return TRUE;
00295 }
00296 
00303 dbus_bool_t
00304 _dbus_verify_daemon_user (const char *user)
00305 {
00306   DBusString u;
00307 
00308   _dbus_string_init_const (&u, user);
00309 
00310   return _dbus_get_user_id_and_primary_group (&u, NULL, NULL);
00311 }
00312 
00313 
00314 /* The HAVE_LIBAUDIT case lives in selinux.c */
00315 #ifndef HAVE_LIBAUDIT
00316 
00323 dbus_bool_t
00324 _dbus_change_to_daemon_user  (const char    *user,
00325                               DBusError     *error)
00326 {
00327   dbus_uid_t uid;
00328   dbus_gid_t gid;
00329   DBusString u;
00330 
00331   _dbus_string_init_const (&u, user);
00332 
00333   if (!_dbus_get_user_id_and_primary_group (&u, &uid, &gid))
00334     {
00335       dbus_set_error (error, DBUS_ERROR_FAILED,
00336                       "User '%s' does not appear to exist?",
00337                       user);
00338       return FALSE;
00339     }
00340 
00341   /* setgroups() only works if we are a privileged process,
00342    * so we don't return error on failure; the only possible
00343    * failure is that we don't have perms to do it.
00344    *
00345    * not sure this is right, maybe if setuid()
00346    * is going to work then setgroups() should also work.
00347    */
00348   if (setgroups (0, NULL) < 0)
00349     _dbus_warn ("Failed to drop supplementary groups: %s\n",
00350                 _dbus_strerror (errno));
00351 
00352   /* Set GID first, or the setuid may remove our permission
00353    * to change the GID
00354    */
00355   if (setgid (gid) < 0)
00356     {
00357       dbus_set_error (error, _dbus_error_from_errno (errno),
00358                       "Failed to set GID to %lu: %s", gid,
00359                       _dbus_strerror (errno));
00360       return FALSE;
00361     }
00362 
00363   if (setuid (uid) < 0)
00364     {
00365       dbus_set_error (error, _dbus_error_from_errno (errno),
00366                       "Failed to set UID to %lu: %s", uid,
00367                       _dbus_strerror (errno));
00368       return FALSE;
00369     }
00370 
00371   return TRUE;
00372 }
00373 #endif /* !HAVE_LIBAUDIT */
00374 
00375 
00386 void
00387 _dbus_request_file_descriptor_limit (unsigned int limit)
00388 {
00389 #ifdef HAVE_SETRLIMIT
00390   struct rlimit lim;
00391   struct rlimit target_lim;
00392 
00393   /* No point to doing this practically speaking
00394    * if we're not uid 0.  We expect the system
00395    * bus to use this before we change UID, and
00396    * the session bus takes the Linux default
00397    * of 1024 for both cur and max.
00398    */
00399   if (getuid () != 0)
00400     return;
00401 
00402   if (getrlimit (RLIMIT_NOFILE, &lim) < 0)
00403     return;
00404 
00405   if (lim.rlim_cur >= limit)
00406     return;
00407 
00408   /* Ignore "maximum limit", assume we have the "superuser"
00409    * privileges.  On Linux this is CAP_SYS_RESOURCE.
00410    */
00411   target_lim.rlim_cur = target_lim.rlim_max = limit;
00412   /* Also ignore errors; if we fail, we will at least work
00413    * up to whatever limit we had, which seems better than
00414    * just outright aborting.
00415    *
00416    * However, in the future we should probably log this so OS builders
00417    * have a chance to notice any misconfiguration like dbus-daemon
00418    * being started without CAP_SYS_RESOURCE.
00419    */
00420   setrlimit (RLIMIT_NOFILE, &target_lim);
00421 #endif
00422 }
00423 
00424 void
00425 _dbus_init_system_log (void)
00426 {
00427 #ifdef HAVE_DECL_LOG_PERROR
00428   openlog ("dbus", LOG_PID | LOG_PERROR, LOG_DAEMON);
00429 #else
00430   openlog ("dbus", LOG_PID, LOG_DAEMON);
00431 #endif
00432 }
00433 
00442 void
00443 _dbus_system_log (DBusSystemLogSeverity severity, const char *msg, ...)
00444 {
00445   va_list args;
00446 
00447   va_start (args, msg);
00448 
00449   _dbus_system_logv (severity, msg, args);
00450 
00451   va_end (args);
00452 }
00453 
00464 void
00465 _dbus_system_logv (DBusSystemLogSeverity severity, const char *msg, va_list args)
00466 {
00467   int flags;
00468   switch (severity)
00469     {
00470       case DBUS_SYSTEM_LOG_INFO:
00471         flags =  LOG_DAEMON | LOG_NOTICE;
00472         break;
00473       case DBUS_SYSTEM_LOG_SECURITY:
00474         flags = LOG_AUTH | LOG_NOTICE;
00475         break;
00476       case DBUS_SYSTEM_LOG_FATAL:
00477         flags = LOG_DAEMON|LOG_CRIT;
00478         break;
00479       default:
00480         return;
00481     }
00482 
00483 #ifndef HAVE_DECL_LOG_PERROR
00484     {
00485       /* vsyslog() won't write to stderr, so we'd better do it */
00486       va_list tmp;
00487 
00488       DBUS_VA_COPY (tmp, args);
00489       fprintf (stderr, "dbus[" DBUS_PID_FORMAT "]: ", _dbus_getpid ());
00490       vfprintf (stderr, msg, tmp);
00491       fputc ('\n', stderr);
00492       va_end (tmp);
00493     }
00494 #endif
00495 
00496   vsyslog (flags, msg, args);
00497 
00498   if (severity == DBUS_SYSTEM_LOG_FATAL)
00499     exit (1);
00500 }
00501 
00507 void
00508 _dbus_set_signal_handler (int               sig,
00509                           DBusSignalHandler handler)
00510 {
00511   struct sigaction act;
00512   sigset_t empty_mask;
00513   
00514   sigemptyset (&empty_mask);
00515   act.sa_handler = handler;
00516   act.sa_mask    = empty_mask;
00517   act.sa_flags   = 0;
00518   sigaction (sig,  &act, NULL);
00519 }
00520 
00526 dbus_bool_t 
00527 _dbus_file_exists (const char *file)
00528 {
00529   return (access (file, F_OK) == 0);
00530 }
00531 
00538 dbus_bool_t 
00539 _dbus_user_at_console (const char *username,
00540                        DBusError  *error)
00541 {
00542 
00543   DBusString f;
00544   dbus_bool_t result;
00545 
00546   result = FALSE;
00547   if (!_dbus_string_init (&f))
00548     {
00549       _DBUS_SET_OOM (error);
00550       return FALSE;
00551     }
00552 
00553   if (!_dbus_string_append (&f, DBUS_CONSOLE_AUTH_DIR))
00554     {
00555       _DBUS_SET_OOM (error);
00556       goto out;
00557     }
00558 
00559 
00560   if (!_dbus_string_append (&f, username))
00561     {
00562       _DBUS_SET_OOM (error);
00563       goto out;
00564     }
00565 
00566   result = _dbus_file_exists (_dbus_string_get_const_data (&f));
00567 
00568  out:
00569   _dbus_string_free (&f);
00570 
00571   return result;
00572 }
00573 
00574 
00581 dbus_bool_t
00582 _dbus_path_is_absolute (const DBusString *filename)
00583 {
00584   if (_dbus_string_get_length (filename) > 0)
00585     return _dbus_string_get_byte (filename, 0) == '/';
00586   else
00587     return FALSE;
00588 }
00589 
00598 dbus_bool_t
00599 _dbus_stat (const DBusString *filename,
00600             DBusStat         *statbuf,
00601             DBusError        *error)
00602 {
00603   const char *filename_c;
00604   struct stat sb;
00605 
00606   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00607   
00608   filename_c = _dbus_string_get_const_data (filename);
00609 
00610   if (stat (filename_c, &sb) < 0)
00611     {
00612       dbus_set_error (error, _dbus_error_from_errno (errno),
00613                       "%s", _dbus_strerror (errno));
00614       return FALSE;
00615     }
00616 
00617   statbuf->mode = sb.st_mode;
00618   statbuf->nlink = sb.st_nlink;
00619   statbuf->uid = sb.st_uid;
00620   statbuf->gid = sb.st_gid;
00621   statbuf->size = sb.st_size;
00622   statbuf->atime = sb.st_atime;
00623   statbuf->mtime = sb.st_mtime;
00624   statbuf->ctime = sb.st_ctime;
00625 
00626   return TRUE;
00627 }
00628 
00629 
00633 struct DBusDirIter
00634 {
00635   DIR *d; 
00637 };
00638 
00646 DBusDirIter*
00647 _dbus_directory_open (const DBusString *filename,
00648                       DBusError        *error)
00649 {
00650   DIR *d;
00651   DBusDirIter *iter;
00652   const char *filename_c;
00653 
00654   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00655   
00656   filename_c = _dbus_string_get_const_data (filename);
00657 
00658   d = opendir (filename_c);
00659   if (d == NULL)
00660     {
00661       dbus_set_error (error, _dbus_error_from_errno (errno),
00662                       "Failed to read directory \"%s\": %s",
00663                       filename_c,
00664                       _dbus_strerror (errno));
00665       return NULL;
00666     }
00667   iter = dbus_new0 (DBusDirIter, 1);
00668   if (iter == NULL)
00669     {
00670       closedir (d);
00671       dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
00672                       "Could not allocate memory for directory iterator");
00673       return NULL;
00674     }
00675 
00676   iter->d = d;
00677 
00678   return iter;
00679 }
00680 
00694 dbus_bool_t
00695 _dbus_directory_get_next_file (DBusDirIter      *iter,
00696                                DBusString       *filename,
00697                                DBusError        *error)
00698 {
00699   struct dirent *ent;
00700   int err;
00701 
00702   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00703 
00704  again:
00705   errno = 0;
00706   ent = readdir (iter->d);
00707 
00708   if (!ent)
00709     {
00710       err = errno;
00711 
00712       if (err != 0)
00713         dbus_set_error (error,
00714                         _dbus_error_from_errno (err),
00715                         "%s", _dbus_strerror (err));
00716 
00717       return FALSE;
00718     }
00719   else if (ent->d_name[0] == '.' &&
00720            (ent->d_name[1] == '\0' ||
00721             (ent->d_name[1] == '.' && ent->d_name[2] == '\0')))
00722     goto again;
00723   else
00724     {
00725       _dbus_string_set_length (filename, 0);
00726       if (!_dbus_string_append (filename, ent->d_name))
00727         {
00728           dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
00729                           "No memory to read directory entry");
00730           return FALSE;
00731         }
00732       else
00733         {
00734           return TRUE;
00735         }
00736     }
00737 }
00738 
00742 void
00743 _dbus_directory_close (DBusDirIter *iter)
00744 {
00745   closedir (iter->d);
00746   dbus_free (iter);
00747 }
00748 
00749 static dbus_bool_t
00750 fill_user_info_from_group (struct group  *g,
00751                            DBusGroupInfo *info,
00752                            DBusError     *error)
00753 {
00754   _dbus_assert (g->gr_name != NULL);
00755   
00756   info->gid = g->gr_gid;
00757   info->groupname = _dbus_strdup (g->gr_name);
00758 
00759   /* info->members = dbus_strdupv (g->gr_mem) */
00760   
00761   if (info->groupname == NULL)
00762     {
00763       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
00764       return FALSE;
00765     }
00766 
00767   return TRUE;
00768 }
00769 
00770 static dbus_bool_t
00771 fill_group_info (DBusGroupInfo    *info,
00772                  dbus_gid_t        gid,
00773                  const DBusString *groupname,
00774                  DBusError        *error)
00775 {
00776   const char *group_c_str;
00777 
00778   _dbus_assert (groupname != NULL || gid != DBUS_GID_UNSET);
00779   _dbus_assert (groupname == NULL || gid == DBUS_GID_UNSET);
00780 
00781   if (groupname)
00782     group_c_str = _dbus_string_get_const_data (groupname);
00783   else
00784     group_c_str = NULL;
00785   
00786   /* For now assuming that the getgrnam() and getgrgid() flavors
00787    * always correspond to the pwnam flavors, if not we have
00788    * to add more configure checks.
00789    */
00790   
00791 #if defined (HAVE_POSIX_GETPWNAM_R) || defined (HAVE_NONPOSIX_GETPWNAM_R)
00792   {
00793     struct group *g;
00794     int result;
00795     size_t buflen;
00796     char *buf;
00797     struct group g_str;
00798     dbus_bool_t b;
00799 
00800     /* retrieve maximum needed size for buf */
00801     buflen = sysconf (_SC_GETGR_R_SIZE_MAX);
00802 
00803     /* sysconf actually returns a long, but everything else expects size_t,
00804      * so just recast here.
00805      * https://bugs.freedesktop.org/show_bug.cgi?id=17061
00806      */
00807     if ((long) buflen <= 0)
00808       buflen = 1024;
00809 
00810     result = -1;
00811     while (1)
00812       {
00813         buf = dbus_malloc (buflen);
00814         if (buf == NULL)
00815           {
00816             dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
00817             return FALSE;
00818           }
00819 
00820         g = NULL;
00821 #ifdef HAVE_POSIX_GETPWNAM_R
00822         if (group_c_str)
00823           result = getgrnam_r (group_c_str, &g_str, buf, buflen,
00824                                &g);
00825         else
00826           result = getgrgid_r (gid, &g_str, buf, buflen,
00827                                &g);
00828 #else
00829         g = getgrnam_r (group_c_str, &g_str, buf, buflen);
00830         result = 0;
00831 #endif /* !HAVE_POSIX_GETPWNAM_R */
00832         /* Try a bigger buffer if ERANGE was returned:
00833            https://bugs.freedesktop.org/show_bug.cgi?id=16727
00834         */
00835         if (result == ERANGE && buflen < 512 * 1024)
00836           {
00837             dbus_free (buf);
00838             buflen *= 2;
00839           }
00840         else
00841           {
00842             break;
00843           }
00844       }
00845 
00846     if (result == 0 && g == &g_str)
00847       {
00848         b = fill_user_info_from_group (g, info, error);
00849         dbus_free (buf);
00850         return b;
00851       }
00852     else
00853       {
00854         dbus_set_error (error, _dbus_error_from_errno (errno),
00855                         "Group %s unknown or failed to look it up\n",
00856                         group_c_str ? group_c_str : "???");
00857         dbus_free (buf);
00858         return FALSE;
00859       }
00860   }
00861 #else /* ! HAVE_GETPWNAM_R */
00862   {
00863     /* I guess we're screwed on thread safety here */
00864     struct group *g;
00865 
00866     g = getgrnam (group_c_str);
00867 
00868     if (g != NULL)
00869       {
00870         return fill_user_info_from_group (g, info, error);
00871       }
00872     else
00873       {
00874         dbus_set_error (error, _dbus_error_from_errno (errno),
00875                         "Group %s unknown or failed to look it up\n",
00876                         group_c_str ? group_c_str : "???");
00877         return FALSE;
00878       }
00879   }
00880 #endif  /* ! HAVE_GETPWNAM_R */
00881 }
00882 
00892 dbus_bool_t
00893 _dbus_group_info_fill (DBusGroupInfo    *info,
00894                        const DBusString *groupname,
00895                        DBusError        *error)
00896 {
00897   return fill_group_info (info, DBUS_GID_UNSET,
00898                           groupname, error);
00899 
00900 }
00901 
00911 dbus_bool_t
00912 _dbus_group_info_fill_gid (DBusGroupInfo *info,
00913                            dbus_gid_t     gid,
00914                            DBusError     *error)
00915 {
00916   return fill_group_info (info, gid, NULL, error);
00917 }
00918 
00927 dbus_bool_t
00928 _dbus_parse_unix_user_from_config (const DBusString  *username,
00929                                    dbus_uid_t        *uid_p)
00930 {
00931   return _dbus_get_user_id (username, uid_p);
00932 
00933 }
00934 
00943 dbus_bool_t
00944 _dbus_parse_unix_group_from_config (const DBusString  *groupname,
00945                                     dbus_gid_t        *gid_p)
00946 {
00947   return _dbus_get_group_id (groupname, gid_p);
00948 }
00949 
00960 dbus_bool_t
00961 _dbus_unix_groups_from_uid (dbus_uid_t            uid,
00962                             dbus_gid_t          **group_ids,
00963                             int                  *n_group_ids)
00964 {
00965   return _dbus_groups_from_uid (uid, group_ids, n_group_ids);
00966 }
00967 
00977 dbus_bool_t
00978 _dbus_unix_user_is_at_console (dbus_uid_t         uid,
00979                                DBusError         *error)
00980 {
00981   return _dbus_is_console_user (uid, error);
00982 
00983 }
00984 
00992 dbus_bool_t
00993 _dbus_unix_user_is_process_owner (dbus_uid_t uid)
00994 {
00995   return uid == _dbus_geteuid ();
00996 }
00997 
01005 dbus_bool_t
01006 _dbus_windows_user_is_process_owner (const char *windows_sid)
01007 {
01008   return FALSE;
01009 }
01010  /* End of DBusInternalsUtils functions */
01012 
01024 dbus_bool_t
01025 _dbus_string_get_dirname  (const DBusString *filename,
01026                            DBusString       *dirname)
01027 {
01028   int sep;
01029   
01030   _dbus_assert (filename != dirname);
01031   _dbus_assert (filename != NULL);
01032   _dbus_assert (dirname != NULL);
01033 
01034   /* Ignore any separators on the end */
01035   sep = _dbus_string_get_length (filename);
01036   if (sep == 0)
01037     return _dbus_string_append (dirname, "."); /* empty string passed in */
01038     
01039   while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
01040     --sep;
01041 
01042   _dbus_assert (sep >= 0);
01043   
01044   if (sep == 0)
01045     return _dbus_string_append (dirname, "/");
01046   
01047   /* Now find the previous separator */
01048   _dbus_string_find_byte_backward (filename, sep, '/', &sep);
01049   if (sep < 0)
01050     return _dbus_string_append (dirname, ".");
01051   
01052   /* skip multiple separators */
01053   while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
01054     --sep;
01055 
01056   _dbus_assert (sep >= 0);
01057   
01058   if (sep == 0 &&
01059       _dbus_string_get_byte (filename, 0) == '/')
01060     return _dbus_string_append (dirname, "/");
01061   else
01062     return _dbus_string_copy_len (filename, 0, sep - 0,
01063                                   dirname, _dbus_string_get_length (dirname));
01064 } /* DBusString stuff */
01066 
01067 static void
01068 string_squash_nonprintable (DBusString *str)
01069 {
01070   unsigned char *buf;
01071   int i, len; 
01072   
01073   buf = _dbus_string_get_data (str);
01074   len = _dbus_string_get_length (str);
01075   
01076   for (i = 0; i < len; i++)
01077     {
01078       unsigned char c = (unsigned char) buf[i];
01079       if (c == '\0')
01080         buf[i] = ' ';
01081       else if (c < 0x20 || c > 127)
01082         buf[i] = '?';
01083     }
01084 }
01085 
01100 dbus_bool_t 
01101 _dbus_command_for_pid (unsigned long  pid,
01102                        DBusString    *str,
01103                        int            max_len,
01104                        DBusError     *error)
01105 {
01106   /* This is all Linux-specific for now */
01107   DBusString path;
01108   DBusString cmdline;
01109   int fd;
01110   
01111   if (!_dbus_string_init (&path)) 
01112     {
01113       _DBUS_SET_OOM (error);
01114       return FALSE;
01115     }
01116   
01117   if (!_dbus_string_init (&cmdline))
01118     {
01119       _DBUS_SET_OOM (error);
01120       _dbus_string_free (&path);
01121       return FALSE;
01122     }
01123   
01124   if (!_dbus_string_append_printf (&path, "/proc/%ld/cmdline", pid))
01125     goto oom;
01126   
01127   fd = open (_dbus_string_get_const_data (&path), O_RDONLY);
01128   if (fd < 0) 
01129     {
01130       dbus_set_error (error,
01131                       _dbus_error_from_errno (errno),
01132                       "Failed to open \"%s\": %s",
01133                       _dbus_string_get_const_data (&path),
01134                       _dbus_strerror (errno));
01135       goto fail;
01136     }
01137   
01138   if (!_dbus_read (fd, &cmdline, max_len))
01139     {
01140       dbus_set_error (error,
01141                       _dbus_error_from_errno (errno),
01142                       "Failed to read from \"%s\": %s",
01143                       _dbus_string_get_const_data (&path),
01144                       _dbus_strerror (errno));      
01145       goto fail;
01146     }
01147   
01148   if (!_dbus_close (fd, error))
01149     goto fail;
01150   
01151   string_squash_nonprintable (&cmdline);  
01152 
01153   if (!_dbus_string_copy (&cmdline, 0, str, _dbus_string_get_length (str)))
01154     goto oom;
01155 
01156   _dbus_string_free (&cmdline);  
01157   _dbus_string_free (&path);
01158   return TRUE;
01159 oom:
01160   _DBUS_SET_OOM (error);
01161 fail:
01162   _dbus_string_free (&cmdline);
01163   _dbus_string_free (&path);
01164   return FALSE;
01165 }