D-Bus 1.4.1
|
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_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 default: 00475 return; 00476 } 00477 00478 vsyslog (flags, msg, args); 00479 00480 if (severity == DBUS_SYSTEM_LOG_FATAL) 00481 exit (1); 00482 } 00483 00489 void 00490 _dbus_set_signal_handler (int sig, 00491 DBusSignalHandler handler) 00492 { 00493 struct sigaction act; 00494 sigset_t empty_mask; 00495 00496 sigemptyset (&empty_mask); 00497 act.sa_handler = handler; 00498 act.sa_mask = empty_mask; 00499 act.sa_flags = 0; 00500 sigaction (sig, &act, NULL); 00501 } 00502 00508 dbus_bool_t 00509 _dbus_file_exists (const char *file) 00510 { 00511 return (access (file, F_OK) == 0); 00512 } 00513 00520 dbus_bool_t 00521 _dbus_user_at_console (const char *username, 00522 DBusError *error) 00523 { 00524 00525 DBusString f; 00526 dbus_bool_t result; 00527 00528 result = FALSE; 00529 if (!_dbus_string_init (&f)) 00530 { 00531 _DBUS_SET_OOM (error); 00532 return FALSE; 00533 } 00534 00535 if (!_dbus_string_append (&f, DBUS_CONSOLE_AUTH_DIR)) 00536 { 00537 _DBUS_SET_OOM (error); 00538 goto out; 00539 } 00540 00541 00542 if (!_dbus_string_append (&f, username)) 00543 { 00544 _DBUS_SET_OOM (error); 00545 goto out; 00546 } 00547 00548 result = _dbus_file_exists (_dbus_string_get_const_data (&f)); 00549 00550 out: 00551 _dbus_string_free (&f); 00552 00553 return result; 00554 } 00555 00556 00563 dbus_bool_t 00564 _dbus_path_is_absolute (const DBusString *filename) 00565 { 00566 if (_dbus_string_get_length (filename) > 0) 00567 return _dbus_string_get_byte (filename, 0) == '/'; 00568 else 00569 return FALSE; 00570 } 00571 00580 dbus_bool_t 00581 _dbus_stat (const DBusString *filename, 00582 DBusStat *statbuf, 00583 DBusError *error) 00584 { 00585 const char *filename_c; 00586 struct stat sb; 00587 00588 _DBUS_ASSERT_ERROR_IS_CLEAR (error); 00589 00590 filename_c = _dbus_string_get_const_data (filename); 00591 00592 if (stat (filename_c, &sb) < 0) 00593 { 00594 dbus_set_error (error, _dbus_error_from_errno (errno), 00595 "%s", _dbus_strerror (errno)); 00596 return FALSE; 00597 } 00598 00599 statbuf->mode = sb.st_mode; 00600 statbuf->nlink = sb.st_nlink; 00601 statbuf->uid = sb.st_uid; 00602 statbuf->gid = sb.st_gid; 00603 statbuf->size = sb.st_size; 00604 statbuf->atime = sb.st_atime; 00605 statbuf->mtime = sb.st_mtime; 00606 statbuf->ctime = sb.st_ctime; 00607 00608 return TRUE; 00609 } 00610 00611 00615 struct DBusDirIter 00616 { 00617 DIR *d; 00619 }; 00620 00628 DBusDirIter* 00629 _dbus_directory_open (const DBusString *filename, 00630 DBusError *error) 00631 { 00632 DIR *d; 00633 DBusDirIter *iter; 00634 const char *filename_c; 00635 00636 _DBUS_ASSERT_ERROR_IS_CLEAR (error); 00637 00638 filename_c = _dbus_string_get_const_data (filename); 00639 00640 d = opendir (filename_c); 00641 if (d == NULL) 00642 { 00643 dbus_set_error (error, _dbus_error_from_errno (errno), 00644 "Failed to read directory \"%s\": %s", 00645 filename_c, 00646 _dbus_strerror (errno)); 00647 return NULL; 00648 } 00649 iter = dbus_new0 (DBusDirIter, 1); 00650 if (iter == NULL) 00651 { 00652 closedir (d); 00653 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, 00654 "Could not allocate memory for directory iterator"); 00655 return NULL; 00656 } 00657 00658 iter->d = d; 00659 00660 return iter; 00661 } 00662 00663 /* Calculate the required buffer size (in bytes) for directory 00664 * entries read from the given directory handle. Return -1 if this 00665 * this cannot be done. 00666 * 00667 * If you use autoconf, include fpathconf and dirfd in your 00668 * AC_CHECK_FUNCS list. Otherwise use some other method to detect 00669 * and use them where available. 00670 */ 00671 static dbus_bool_t 00672 dirent_buf_size(DIR * dirp, size_t *size) 00673 { 00674 long name_max; 00675 # if defined(HAVE_FPATHCONF) && defined(_PC_NAME_MAX) 00676 # if defined(HAVE_DIRFD) 00677 name_max = fpathconf(dirfd(dirp), _PC_NAME_MAX); 00678 # elif defined(HAVE_DDFD) 00679 name_max = fpathconf(dirp->dd_fd, _PC_NAME_MAX); 00680 # else 00681 name_max = fpathconf(dirp->__dd_fd, _PC_NAME_MAX); 00682 # endif /* HAVE_DIRFD */ 00683 if (name_max == -1) 00684 # if defined(NAME_MAX) 00685 name_max = NAME_MAX; 00686 # else 00687 return FALSE; 00688 # endif 00689 # elif defined(MAXNAMELEN) 00690 name_max = MAXNAMELEN; 00691 # else 00692 # if defined(NAME_MAX) 00693 name_max = NAME_MAX; 00694 # else 00695 # error "buffer size for readdir_r cannot be determined" 00696 # endif 00697 # endif 00698 if (size) 00699 *size = (size_t)offsetof(struct dirent, d_name) + name_max + 1; 00700 else 00701 return FALSE; 00702 00703 return TRUE; 00704 } 00705 00716 dbus_bool_t 00717 _dbus_directory_get_next_file (DBusDirIter *iter, 00718 DBusString *filename, 00719 DBusError *error) 00720 { 00721 struct dirent *d, *ent; 00722 size_t buf_size; 00723 int err; 00724 00725 _DBUS_ASSERT_ERROR_IS_CLEAR (error); 00726 00727 if (!dirent_buf_size (iter->d, &buf_size)) 00728 { 00729 dbus_set_error (error, DBUS_ERROR_FAILED, 00730 "Can't calculate buffer size when reading directory"); 00731 return FALSE; 00732 } 00733 00734 d = (struct dirent *)dbus_malloc (buf_size); 00735 if (!d) 00736 { 00737 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, 00738 "No memory to read directory entry"); 00739 return FALSE; 00740 } 00741 00742 again: 00743 err = readdir_r (iter->d, d, &ent); 00744 if (err || !ent) 00745 { 00746 if (err != 0) 00747 dbus_set_error (error, 00748 _dbus_error_from_errno (err), 00749 "%s", _dbus_strerror (err)); 00750 00751 dbus_free (d); 00752 return FALSE; 00753 } 00754 else if (ent->d_name[0] == '.' && 00755 (ent->d_name[1] == '\0' || 00756 (ent->d_name[1] == '.' && ent->d_name[2] == '\0'))) 00757 goto again; 00758 else 00759 { 00760 _dbus_string_set_length (filename, 0); 00761 if (!_dbus_string_append (filename, ent->d_name)) 00762 { 00763 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, 00764 "No memory to read directory entry"); 00765 dbus_free (d); 00766 return FALSE; 00767 } 00768 else 00769 { 00770 dbus_free (d); 00771 return TRUE; 00772 } 00773 } 00774 } 00775 00779 void 00780 _dbus_directory_close (DBusDirIter *iter) 00781 { 00782 closedir (iter->d); 00783 dbus_free (iter); 00784 } 00785 00786 static dbus_bool_t 00787 fill_user_info_from_group (struct group *g, 00788 DBusGroupInfo *info, 00789 DBusError *error) 00790 { 00791 _dbus_assert (g->gr_name != NULL); 00792 00793 info->gid = g->gr_gid; 00794 info->groupname = _dbus_strdup (g->gr_name); 00795 00796 /* info->members = dbus_strdupv (g->gr_mem) */ 00797 00798 if (info->groupname == NULL) 00799 { 00800 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); 00801 return FALSE; 00802 } 00803 00804 return TRUE; 00805 } 00806 00807 static dbus_bool_t 00808 fill_group_info (DBusGroupInfo *info, 00809 dbus_gid_t gid, 00810 const DBusString *groupname, 00811 DBusError *error) 00812 { 00813 const char *group_c_str; 00814 00815 _dbus_assert (groupname != NULL || gid != DBUS_GID_UNSET); 00816 _dbus_assert (groupname == NULL || gid == DBUS_GID_UNSET); 00817 00818 if (groupname) 00819 group_c_str = _dbus_string_get_const_data (groupname); 00820 else 00821 group_c_str = NULL; 00822 00823 /* For now assuming that the getgrnam() and getgrgid() flavors 00824 * always correspond to the pwnam flavors, if not we have 00825 * to add more configure checks. 00826 */ 00827 00828 #if defined (HAVE_POSIX_GETPWNAM_R) || defined (HAVE_NONPOSIX_GETPWNAM_R) 00829 { 00830 struct group *g; 00831 int result; 00832 size_t buflen; 00833 char *buf; 00834 struct group g_str; 00835 dbus_bool_t b; 00836 00837 /* retrieve maximum needed size for buf */ 00838 buflen = sysconf (_SC_GETGR_R_SIZE_MAX); 00839 00840 /* sysconf actually returns a long, but everything else expects size_t, 00841 * so just recast here. 00842 * https://bugs.freedesktop.org/show_bug.cgi?id=17061 00843 */ 00844 if ((long) buflen <= 0) 00845 buflen = 1024; 00846 00847 result = -1; 00848 while (1) 00849 { 00850 buf = dbus_malloc (buflen); 00851 if (buf == NULL) 00852 { 00853 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); 00854 return FALSE; 00855 } 00856 00857 g = NULL; 00858 #ifdef HAVE_POSIX_GETPWNAM_R 00859 if (group_c_str) 00860 result = getgrnam_r (group_c_str, &g_str, buf, buflen, 00861 &g); 00862 else 00863 result = getgrgid_r (gid, &g_str, buf, buflen, 00864 &g); 00865 #else 00866 g = getgrnam_r (group_c_str, &g_str, buf, buflen); 00867 result = 0; 00868 #endif /* !HAVE_POSIX_GETPWNAM_R */ 00869 /* Try a bigger buffer if ERANGE was returned: 00870 https://bugs.freedesktop.org/show_bug.cgi?id=16727 00871 */ 00872 if (result == ERANGE && buflen < 512 * 1024) 00873 { 00874 dbus_free (buf); 00875 buflen *= 2; 00876 } 00877 else 00878 { 00879 break; 00880 } 00881 } 00882 00883 if (result == 0 && g == &g_str) 00884 { 00885 b = fill_user_info_from_group (g, info, error); 00886 dbus_free (buf); 00887 return b; 00888 } 00889 else 00890 { 00891 dbus_set_error (error, _dbus_error_from_errno (errno), 00892 "Group %s unknown or failed to look it up\n", 00893 group_c_str ? group_c_str : "???"); 00894 dbus_free (buf); 00895 return FALSE; 00896 } 00897 } 00898 #else /* ! HAVE_GETPWNAM_R */ 00899 { 00900 /* I guess we're screwed on thread safety here */ 00901 struct group *g; 00902 00903 g = getgrnam (group_c_str); 00904 00905 if (g != NULL) 00906 { 00907 return fill_user_info_from_group (g, info, error); 00908 } 00909 else 00910 { 00911 dbus_set_error (error, _dbus_error_from_errno (errno), 00912 "Group %s unknown or failed to look it up\n", 00913 group_c_str ? group_c_str : "???"); 00914 return FALSE; 00915 } 00916 } 00917 #endif /* ! HAVE_GETPWNAM_R */ 00918 } 00919 00929 dbus_bool_t 00930 _dbus_group_info_fill (DBusGroupInfo *info, 00931 const DBusString *groupname, 00932 DBusError *error) 00933 { 00934 return fill_group_info (info, DBUS_GID_UNSET, 00935 groupname, error); 00936 00937 } 00938 00948 dbus_bool_t 00949 _dbus_group_info_fill_gid (DBusGroupInfo *info, 00950 dbus_gid_t gid, 00951 DBusError *error) 00952 { 00953 return fill_group_info (info, gid, NULL, error); 00954 } 00955 00964 dbus_bool_t 00965 _dbus_parse_unix_user_from_config (const DBusString *username, 00966 dbus_uid_t *uid_p) 00967 { 00968 return _dbus_get_user_id (username, uid_p); 00969 00970 } 00971 00980 dbus_bool_t 00981 _dbus_parse_unix_group_from_config (const DBusString *groupname, 00982 dbus_gid_t *gid_p) 00983 { 00984 return _dbus_get_group_id (groupname, gid_p); 00985 } 00986 00997 dbus_bool_t 00998 _dbus_unix_groups_from_uid (dbus_uid_t uid, 00999 dbus_gid_t **group_ids, 01000 int *n_group_ids) 01001 { 01002 return _dbus_groups_from_uid (uid, group_ids, n_group_ids); 01003 } 01004 01014 dbus_bool_t 01015 _dbus_unix_user_is_at_console (dbus_uid_t uid, 01016 DBusError *error) 01017 { 01018 return _dbus_is_console_user (uid, error); 01019 01020 } 01021 01029 dbus_bool_t 01030 _dbus_unix_user_is_process_owner (dbus_uid_t uid) 01031 { 01032 return uid == _dbus_geteuid (); 01033 } 01034 01042 dbus_bool_t 01043 _dbus_windows_user_is_process_owner (const char *windows_sid) 01044 { 01045 return FALSE; 01046 } 01047 /* End of DBusInternalsUtils functions */ 01049 01061 dbus_bool_t 01062 _dbus_string_get_dirname (const DBusString *filename, 01063 DBusString *dirname) 01064 { 01065 int sep; 01066 01067 _dbus_assert (filename != dirname); 01068 _dbus_assert (filename != NULL); 01069 _dbus_assert (dirname != NULL); 01070 01071 /* Ignore any separators on the end */ 01072 sep = _dbus_string_get_length (filename); 01073 if (sep == 0) 01074 return _dbus_string_append (dirname, "."); /* empty string passed in */ 01075 01076 while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/') 01077 --sep; 01078 01079 _dbus_assert (sep >= 0); 01080 01081 if (sep == 0) 01082 return _dbus_string_append (dirname, "/"); 01083 01084 /* Now find the previous separator */ 01085 _dbus_string_find_byte_backward (filename, sep, '/', &sep); 01086 if (sep < 0) 01087 return _dbus_string_append (dirname, "."); 01088 01089 /* skip multiple separators */ 01090 while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/') 01091 --sep; 01092 01093 _dbus_assert (sep >= 0); 01094 01095 if (sep == 0 && 01096 _dbus_string_get_byte (filename, 0) == '/') 01097 return _dbus_string_append (dirname, "/"); 01098 else 01099 return _dbus_string_copy_len (filename, 0, sep - 0, 01100 dirname, _dbus_string_get_length (dirname)); 01101 } /* DBusString stuff */ 01103 01104 static void 01105 string_squash_nonprintable (DBusString *str) 01106 { 01107 unsigned char *buf; 01108 int i, len; 01109 01110 buf = _dbus_string_get_data (str); 01111 len = _dbus_string_get_length (str); 01112 01113 for (i = 0; i < len; i++) 01114 { 01115 unsigned char c = (unsigned char) buf[i]; 01116 if (c == '\0') 01117 c = ' '; 01118 else if (c < 0x20 || c > 127) 01119 c = '?'; 01120 } 01121 } 01122 01137 dbus_bool_t 01138 _dbus_command_for_pid (unsigned long pid, 01139 DBusString *str, 01140 int max_len, 01141 DBusError *error) 01142 { 01143 /* This is all Linux-specific for now */ 01144 DBusString path; 01145 DBusString cmdline; 01146 int fd; 01147 01148 if (!_dbus_string_init (&path)) 01149 { 01150 _DBUS_SET_OOM (error); 01151 return FALSE; 01152 } 01153 01154 if (!_dbus_string_init (&cmdline)) 01155 { 01156 _DBUS_SET_OOM (error); 01157 _dbus_string_free (&path); 01158 return FALSE; 01159 } 01160 01161 if (!_dbus_string_append_printf (&path, "/proc/%ld/cmdline", pid)) 01162 goto oom; 01163 01164 fd = open (_dbus_string_get_const_data (&path), O_RDONLY); 01165 if (fd < 0) 01166 { 01167 dbus_set_error (error, 01168 _dbus_error_from_errno (errno), 01169 "Failed to open \"%s\": %s", 01170 _dbus_string_get_const_data (&path), 01171 _dbus_strerror (errno)); 01172 goto fail; 01173 } 01174 01175 if (!_dbus_read (fd, &cmdline, max_len)) 01176 { 01177 dbus_set_error (error, 01178 _dbus_error_from_errno (errno), 01179 "Failed to read from \"%s\": %s", 01180 _dbus_string_get_const_data (&path), 01181 _dbus_strerror (errno)); 01182 goto fail; 01183 } 01184 01185 if (!_dbus_close (fd, error)) 01186 goto fail; 01187 01188 string_squash_nonprintable (&cmdline); 01189 01190 if (!_dbus_string_copy (&cmdline, 0, str, _dbus_string_get_length (str))) 01191 goto oom; 01192 01193 _dbus_string_free (&cmdline); 01194 _dbus_string_free (&path); 01195 return TRUE; 01196 oom: 01197 _DBUS_SET_OOM (error); 01198 fail: 01199 _dbus_string_free (&cmdline); 01200 _dbus_string_free (&path); 01201 return FALSE; 01202 }