D-Bus  1.4.10
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   unsigned int current_limit;
00393 
00394   /* No point to doing this practically speaking
00395    * if we're not uid 0.  We expect the system
00396    * bus to use this before we change UID, and
00397    * the session bus takes the Linux default
00398    * of 1024 for both cur and max.
00399    */
00400   if (getuid () != 0)
00401     return;
00402 
00403   if (getrlimit (RLIMIT_NOFILE, &lim) < 0)
00404     return;
00405 
00406   if (lim.rlim_cur >= limit)
00407     return;
00408 
00409   /* Ignore "maximum limit", assume we have the "superuser"
00410    * privileges.  On Linux this is CAP_SYS_RESOURCE.
00411    */
00412   target_lim.rlim_cur = target_lim.rlim_max = limit;
00413   /* Also ignore errors; if we fail, we will at least work
00414    * up to whatever limit we had, which seems better than
00415    * just outright aborting.
00416    *
00417    * However, in the future we should probably log this so OS builders
00418    * have a chance to notice any misconfiguration like dbus-daemon
00419    * being started without CAP_SYS_RESOURCE.
00420    */
00421   setrlimit (RLIMIT_NOFILE, &target_lim);
00422 #endif
00423 }
00424 
00425 void 
00426 _dbus_init_system_log (void)
00427 {
00428   openlog ("dbus", LOG_PID | LOG_PERROR, LOG_DAEMON);
00429 }
00438 void
00439 _dbus_system_log (DBusSystemLogSeverity severity, const char *msg, ...)
00440 {
00441   va_list args;
00442 
00443   va_start (args, msg);
00444 
00445   _dbus_system_logv (severity, msg, args);
00446 
00447   va_end (args);
00448 }
00449 
00460 void
00461 _dbus_system_logv (DBusSystemLogSeverity severity, const char *msg, va_list args)
00462 {
00463   int flags;
00464   switch (severity)
00465     {
00466       case DBUS_SYSTEM_LOG_INFO:
00467         flags =  LOG_DAEMON | LOG_NOTICE;
00468         break;
00469       case DBUS_SYSTEM_LOG_SECURITY:
00470         flags = LOG_AUTH | LOG_NOTICE;
00471         break;
00472       case DBUS_SYSTEM_LOG_FATAL:
00473         flags = LOG_DAEMON|LOG_CRIT;
00474         break;
00475       default:
00476         return;
00477     }
00478 
00479   vsyslog (flags, msg, args);
00480 
00481   if (severity == DBUS_SYSTEM_LOG_FATAL)
00482     exit (1);
00483 }
00484 
00490 void
00491 _dbus_set_signal_handler (int               sig,
00492                           DBusSignalHandler handler)
00493 {
00494   struct sigaction act;
00495   sigset_t empty_mask;
00496   
00497   sigemptyset (&empty_mask);
00498   act.sa_handler = handler;
00499   act.sa_mask    = empty_mask;
00500   act.sa_flags   = 0;
00501   sigaction (sig,  &act, NULL);
00502 }
00503 
00509 dbus_bool_t 
00510 _dbus_file_exists (const char *file)
00511 {
00512   return (access (file, F_OK) == 0);
00513 }
00514 
00521 dbus_bool_t 
00522 _dbus_user_at_console (const char *username,
00523                        DBusError  *error)
00524 {
00525 
00526   DBusString f;
00527   dbus_bool_t result;
00528 
00529   result = FALSE;
00530   if (!_dbus_string_init (&f))
00531     {
00532       _DBUS_SET_OOM (error);
00533       return FALSE;
00534     }
00535 
00536   if (!_dbus_string_append (&f, DBUS_CONSOLE_AUTH_DIR))
00537     {
00538       _DBUS_SET_OOM (error);
00539       goto out;
00540     }
00541 
00542 
00543   if (!_dbus_string_append (&f, username))
00544     {
00545       _DBUS_SET_OOM (error);
00546       goto out;
00547     }
00548 
00549   result = _dbus_file_exists (_dbus_string_get_const_data (&f));
00550 
00551  out:
00552   _dbus_string_free (&f);
00553 
00554   return result;
00555 }
00556 
00557 
00564 dbus_bool_t
00565 _dbus_path_is_absolute (const DBusString *filename)
00566 {
00567   if (_dbus_string_get_length (filename) > 0)
00568     return _dbus_string_get_byte (filename, 0) == '/';
00569   else
00570     return FALSE;
00571 }
00572 
00581 dbus_bool_t
00582 _dbus_stat (const DBusString *filename,
00583             DBusStat         *statbuf,
00584             DBusError        *error)
00585 {
00586   const char *filename_c;
00587   struct stat sb;
00588 
00589   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00590   
00591   filename_c = _dbus_string_get_const_data (filename);
00592 
00593   if (stat (filename_c, &sb) < 0)
00594     {
00595       dbus_set_error (error, _dbus_error_from_errno (errno),
00596                       "%s", _dbus_strerror (errno));
00597       return FALSE;
00598     }
00599 
00600   statbuf->mode = sb.st_mode;
00601   statbuf->nlink = sb.st_nlink;
00602   statbuf->uid = sb.st_uid;
00603   statbuf->gid = sb.st_gid;
00604   statbuf->size = sb.st_size;
00605   statbuf->atime = sb.st_atime;
00606   statbuf->mtime = sb.st_mtime;
00607   statbuf->ctime = sb.st_ctime;
00608 
00609   return TRUE;
00610 }
00611 
00612 
00616 struct DBusDirIter
00617 {
00618   DIR *d; 
00620 };
00621 
00629 DBusDirIter*
00630 _dbus_directory_open (const DBusString *filename,
00631                       DBusError        *error)
00632 {
00633   DIR *d;
00634   DBusDirIter *iter;
00635   const char *filename_c;
00636 
00637   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00638   
00639   filename_c = _dbus_string_get_const_data (filename);
00640 
00641   d = opendir (filename_c);
00642   if (d == NULL)
00643     {
00644       dbus_set_error (error, _dbus_error_from_errno (errno),
00645                       "Failed to read directory \"%s\": %s",
00646                       filename_c,
00647                       _dbus_strerror (errno));
00648       return NULL;
00649     }
00650   iter = dbus_new0 (DBusDirIter, 1);
00651   if (iter == NULL)
00652     {
00653       closedir (d);
00654       dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
00655                       "Could not allocate memory for directory iterator");
00656       return NULL;
00657     }
00658 
00659   iter->d = d;
00660 
00661   return iter;
00662 }
00663 
00677 dbus_bool_t
00678 _dbus_directory_get_next_file (DBusDirIter      *iter,
00679                                DBusString       *filename,
00680                                DBusError        *error)
00681 {
00682   struct dirent *ent;
00683   int err;
00684 
00685   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00686 
00687  again:
00688   errno = 0;
00689   ent = readdir (iter->d);
00690 
00691   if (!ent)
00692     {
00693       err = errno;
00694 
00695       if (err != 0)
00696         dbus_set_error (error,
00697                         _dbus_error_from_errno (err),
00698                         "%s", _dbus_strerror (err));
00699 
00700       return FALSE;
00701     }
00702   else if (ent->d_name[0] == '.' &&
00703            (ent->d_name[1] == '\0' ||
00704             (ent->d_name[1] == '.' && ent->d_name[2] == '\0')))
00705     goto again;
00706   else
00707     {
00708       _dbus_string_set_length (filename, 0);
00709       if (!_dbus_string_append (filename, ent->d_name))
00710         {
00711           dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
00712                           "No memory to read directory entry");
00713           return FALSE;
00714         }
00715       else
00716         {
00717           return TRUE;
00718         }
00719     }
00720 }
00721 
00725 void
00726 _dbus_directory_close (DBusDirIter *iter)
00727 {
00728   closedir (iter->d);
00729   dbus_free (iter);
00730 }
00731 
00732 static dbus_bool_t
00733 fill_user_info_from_group (struct group  *g,
00734                            DBusGroupInfo *info,
00735                            DBusError     *error)
00736 {
00737   _dbus_assert (g->gr_name != NULL);
00738   
00739   info->gid = g->gr_gid;
00740   info->groupname = _dbus_strdup (g->gr_name);
00741 
00742   /* info->members = dbus_strdupv (g->gr_mem) */
00743   
00744   if (info->groupname == NULL)
00745     {
00746       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
00747       return FALSE;
00748     }
00749 
00750   return TRUE;
00751 }
00752 
00753 static dbus_bool_t
00754 fill_group_info (DBusGroupInfo    *info,
00755                  dbus_gid_t        gid,
00756                  const DBusString *groupname,
00757                  DBusError        *error)
00758 {
00759   const char *group_c_str;
00760 
00761   _dbus_assert (groupname != NULL || gid != DBUS_GID_UNSET);
00762   _dbus_assert (groupname == NULL || gid == DBUS_GID_UNSET);
00763 
00764   if (groupname)
00765     group_c_str = _dbus_string_get_const_data (groupname);
00766   else
00767     group_c_str = NULL;
00768   
00769   /* For now assuming that the getgrnam() and getgrgid() flavors
00770    * always correspond to the pwnam flavors, if not we have
00771    * to add more configure checks.
00772    */
00773   
00774 #if defined (HAVE_POSIX_GETPWNAM_R) || defined (HAVE_NONPOSIX_GETPWNAM_R)
00775   {
00776     struct group *g;
00777     int result;
00778     size_t buflen;
00779     char *buf;
00780     struct group g_str;
00781     dbus_bool_t b;
00782 
00783     /* retrieve maximum needed size for buf */
00784     buflen = sysconf (_SC_GETGR_R_SIZE_MAX);
00785 
00786     /* sysconf actually returns a long, but everything else expects size_t,
00787      * so just recast here.
00788      * https://bugs.freedesktop.org/show_bug.cgi?id=17061
00789      */
00790     if ((long) buflen <= 0)
00791       buflen = 1024;
00792 
00793     result = -1;
00794     while (1)
00795       {
00796         buf = dbus_malloc (buflen);
00797         if (buf == NULL)
00798           {
00799             dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
00800             return FALSE;
00801           }
00802 
00803         g = NULL;
00804 #ifdef HAVE_POSIX_GETPWNAM_R
00805         if (group_c_str)
00806           result = getgrnam_r (group_c_str, &g_str, buf, buflen,
00807                                &g);
00808         else
00809           result = getgrgid_r (gid, &g_str, buf, buflen,
00810                                &g);
00811 #else
00812         g = getgrnam_r (group_c_str, &g_str, buf, buflen);
00813         result = 0;
00814 #endif /* !HAVE_POSIX_GETPWNAM_R */
00815         /* Try a bigger buffer if ERANGE was returned:
00816            https://bugs.freedesktop.org/show_bug.cgi?id=16727
00817         */
00818         if (result == ERANGE && buflen < 512 * 1024)
00819           {
00820             dbus_free (buf);
00821             buflen *= 2;
00822           }
00823         else
00824           {
00825             break;
00826           }
00827       }
00828 
00829     if (result == 0 && g == &g_str)
00830       {
00831         b = fill_user_info_from_group (g, info, error);
00832         dbus_free (buf);
00833         return b;
00834       }
00835     else
00836       {
00837         dbus_set_error (error, _dbus_error_from_errno (errno),
00838                         "Group %s unknown or failed to look it up\n",
00839                         group_c_str ? group_c_str : "???");
00840         dbus_free (buf);
00841         return FALSE;
00842       }
00843   }
00844 #else /* ! HAVE_GETPWNAM_R */
00845   {
00846     /* I guess we're screwed on thread safety here */
00847     struct group *g;
00848 
00849     g = getgrnam (group_c_str);
00850 
00851     if (g != NULL)
00852       {
00853         return fill_user_info_from_group (g, info, error);
00854       }
00855     else
00856       {
00857         dbus_set_error (error, _dbus_error_from_errno (errno),
00858                         "Group %s unknown or failed to look it up\n",
00859                         group_c_str ? group_c_str : "???");
00860         return FALSE;
00861       }
00862   }
00863 #endif  /* ! HAVE_GETPWNAM_R */
00864 }
00865 
00875 dbus_bool_t
00876 _dbus_group_info_fill (DBusGroupInfo    *info,
00877                        const DBusString *groupname,
00878                        DBusError        *error)
00879 {
00880   return fill_group_info (info, DBUS_GID_UNSET,
00881                           groupname, error);
00882 
00883 }
00884 
00894 dbus_bool_t
00895 _dbus_group_info_fill_gid (DBusGroupInfo *info,
00896                            dbus_gid_t     gid,
00897                            DBusError     *error)
00898 {
00899   return fill_group_info (info, gid, NULL, error);
00900 }
00901 
00910 dbus_bool_t
00911 _dbus_parse_unix_user_from_config (const DBusString  *username,
00912                                    dbus_uid_t        *uid_p)
00913 {
00914   return _dbus_get_user_id (username, uid_p);
00915 
00916 }
00917 
00926 dbus_bool_t
00927 _dbus_parse_unix_group_from_config (const DBusString  *groupname,
00928                                     dbus_gid_t        *gid_p)
00929 {
00930   return _dbus_get_group_id (groupname, gid_p);
00931 }
00932 
00943 dbus_bool_t
00944 _dbus_unix_groups_from_uid (dbus_uid_t            uid,
00945                             dbus_gid_t          **group_ids,
00946                             int                  *n_group_ids)
00947 {
00948   return _dbus_groups_from_uid (uid, group_ids, n_group_ids);
00949 }
00950 
00960 dbus_bool_t
00961 _dbus_unix_user_is_at_console (dbus_uid_t         uid,
00962                                DBusError         *error)
00963 {
00964   return _dbus_is_console_user (uid, error);
00965 
00966 }
00967 
00975 dbus_bool_t
00976 _dbus_unix_user_is_process_owner (dbus_uid_t uid)
00977 {
00978   return uid == _dbus_geteuid ();
00979 }
00980 
00988 dbus_bool_t
00989 _dbus_windows_user_is_process_owner (const char *windows_sid)
00990 {
00991   return FALSE;
00992 }
00993  /* End of DBusInternalsUtils functions */
00995 
01007 dbus_bool_t
01008 _dbus_string_get_dirname  (const DBusString *filename,
01009                            DBusString       *dirname)
01010 {
01011   int sep;
01012   
01013   _dbus_assert (filename != dirname);
01014   _dbus_assert (filename != NULL);
01015   _dbus_assert (dirname != NULL);
01016 
01017   /* Ignore any separators on the end */
01018   sep = _dbus_string_get_length (filename);
01019   if (sep == 0)
01020     return _dbus_string_append (dirname, "."); /* empty string passed in */
01021     
01022   while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
01023     --sep;
01024 
01025   _dbus_assert (sep >= 0);
01026   
01027   if (sep == 0)
01028     return _dbus_string_append (dirname, "/");
01029   
01030   /* Now find the previous separator */
01031   _dbus_string_find_byte_backward (filename, sep, '/', &sep);
01032   if (sep < 0)
01033     return _dbus_string_append (dirname, ".");
01034   
01035   /* skip multiple separators */
01036   while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
01037     --sep;
01038 
01039   _dbus_assert (sep >= 0);
01040   
01041   if (sep == 0 &&
01042       _dbus_string_get_byte (filename, 0) == '/')
01043     return _dbus_string_append (dirname, "/");
01044   else
01045     return _dbus_string_copy_len (filename, 0, sep - 0,
01046                                   dirname, _dbus_string_get_length (dirname));
01047 } /* DBusString stuff */
01049 
01050 static void
01051 string_squash_nonprintable (DBusString *str)
01052 {
01053   unsigned char *buf;
01054   int i, len; 
01055   
01056   buf = _dbus_string_get_data (str);
01057   len = _dbus_string_get_length (str);
01058   
01059   for (i = 0; i < len; i++)
01060     {
01061       unsigned char c = (unsigned char) buf[i];
01062       if (c == '\0')
01063         buf[i] = ' ';
01064       else if (c < 0x20 || c > 127)
01065         buf[i] = '?';
01066     }
01067 }
01068 
01083 dbus_bool_t 
01084 _dbus_command_for_pid (unsigned long  pid,
01085                        DBusString    *str,
01086                        int            max_len,
01087                        DBusError     *error)
01088 {
01089   /* This is all Linux-specific for now */
01090   DBusString path;
01091   DBusString cmdline;
01092   int fd;
01093   
01094   if (!_dbus_string_init (&path)) 
01095     {
01096       _DBUS_SET_OOM (error);
01097       return FALSE;
01098     }
01099   
01100   if (!_dbus_string_init (&cmdline))
01101     {
01102       _DBUS_SET_OOM (error);
01103       _dbus_string_free (&path);
01104       return FALSE;
01105     }
01106   
01107   if (!_dbus_string_append_printf (&path, "/proc/%ld/cmdline", pid))
01108     goto oom;
01109   
01110   fd = open (_dbus_string_get_const_data (&path), O_RDONLY);
01111   if (fd < 0) 
01112     {
01113       dbus_set_error (error,
01114                       _dbus_error_from_errno (errno),
01115                       "Failed to open \"%s\": %s",
01116                       _dbus_string_get_const_data (&path),
01117                       _dbus_strerror (errno));
01118       goto fail;
01119     }
01120   
01121   if (!_dbus_read (fd, &cmdline, max_len))
01122     {
01123       dbus_set_error (error,
01124                       _dbus_error_from_errno (errno),
01125                       "Failed to read from \"%s\": %s",
01126                       _dbus_string_get_const_data (&path),
01127                       _dbus_strerror (errno));      
01128       goto fail;
01129     }
01130   
01131   if (!_dbus_close (fd, error))
01132     goto fail;
01133   
01134   string_squash_nonprintable (&cmdline);  
01135 
01136   if (!_dbus_string_copy (&cmdline, 0, str, _dbus_string_get_length (str)))
01137     goto oom;
01138 
01139   _dbus_string_free (&cmdline);  
01140   _dbus_string_free (&path);
01141   return TRUE;
01142 oom:
01143   _DBUS_SET_OOM (error);
01144 fail:
01145   _dbus_string_free (&cmdline);
01146   _dbus_string_free (&path);
01147   return FALSE;
01148 }