|
D-Bus
1.8.20
|
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 00053 #ifdef HAVE_SYSLOG_H 00054 #include <syslog.h> 00055 #endif 00056 00057 #ifdef __sun 00058 #include <pwd.h> 00059 #endif 00060 00061 #ifdef HAVE_SYS_SYSLIMITS_H 00062 #include <sys/syslimits.h> 00063 #endif 00064 00065 #include "sd-daemon.h" 00066 00067 #ifndef O_BINARY 00068 #define O_BINARY 0 00069 #endif 00070 00086 dbus_bool_t 00087 _dbus_become_daemon (const DBusString *pidfile, 00088 DBusPipe *print_pid_pipe, 00089 DBusError *error, 00090 dbus_bool_t keep_umask) 00091 { 00092 const char *s; 00093 pid_t child_pid; 00094 int dev_null_fd; 00095 00096 _dbus_verbose ("Becoming a daemon...\n"); 00097 00098 _dbus_verbose ("chdir to /\n"); 00099 if (chdir ("/") < 0) 00100 { 00101 dbus_set_error (error, DBUS_ERROR_FAILED, 00102 "Could not chdir() to root directory"); 00103 return FALSE; 00104 } 00105 00106 _dbus_verbose ("forking...\n"); 00107 switch ((child_pid = fork ())) 00108 { 00109 case -1: 00110 _dbus_verbose ("fork failed\n"); 00111 dbus_set_error (error, _dbus_error_from_errno (errno), 00112 "Failed to fork daemon: %s", _dbus_strerror (errno)); 00113 return FALSE; 00114 break; 00115 00116 case 0: 00117 _dbus_verbose ("in child, closing std file descriptors\n"); 00118 00119 /* silently ignore failures here, if someone 00120 * doesn't have /dev/null we may as well try 00121 * to continue anyhow 00122 */ 00123 00124 dev_null_fd = open ("/dev/null", O_RDWR); 00125 if (dev_null_fd >= 0) 00126 { 00127 dup2 (dev_null_fd, 0); 00128 dup2 (dev_null_fd, 1); 00129 00130 s = _dbus_getenv ("DBUS_DEBUG_OUTPUT"); 00131 if (s == NULL || *s == '\0') 00132 dup2 (dev_null_fd, 2); 00133 else 00134 _dbus_verbose ("keeping stderr open due to DBUS_DEBUG_OUTPUT\n"); 00135 close (dev_null_fd); 00136 } 00137 00138 if (!keep_umask) 00139 { 00140 /* Get a predictable umask */ 00141 _dbus_verbose ("setting umask\n"); 00142 umask (022); 00143 } 00144 00145 _dbus_verbose ("calling setsid()\n"); 00146 if (setsid () == -1) 00147 _dbus_assert_not_reached ("setsid() failed"); 00148 00149 break; 00150 00151 default: 00152 if (!_dbus_write_pid_to_file_and_pipe (pidfile, print_pid_pipe, 00153 child_pid, error)) 00154 { 00155 _dbus_verbose ("pid file or pipe write failed: %s\n", 00156 error->message); 00157 kill (child_pid, SIGTERM); 00158 return FALSE; 00159 } 00160 00161 _dbus_verbose ("parent exiting\n"); 00162 _exit (0); 00163 break; 00164 } 00165 00166 return TRUE; 00167 } 00168 00169 00178 static dbus_bool_t 00179 _dbus_write_pid_file (const DBusString *filename, 00180 unsigned long pid, 00181 DBusError *error) 00182 { 00183 const char *cfilename; 00184 int fd; 00185 FILE *f; 00186 00187 cfilename = _dbus_string_get_const_data (filename); 00188 00189 fd = open (cfilename, O_WRONLY|O_CREAT|O_EXCL|O_BINARY, 0644); 00190 00191 if (fd < 0) 00192 { 00193 dbus_set_error (error, _dbus_error_from_errno (errno), 00194 "Failed to open \"%s\": %s", cfilename, 00195 _dbus_strerror (errno)); 00196 return FALSE; 00197 } 00198 00199 if ((f = fdopen (fd, "w")) == NULL) 00200 { 00201 dbus_set_error (error, _dbus_error_from_errno (errno), 00202 "Failed to fdopen fd %d: %s", fd, _dbus_strerror (errno)); 00203 _dbus_close (fd, NULL); 00204 return FALSE; 00205 } 00206 00207 if (fprintf (f, "%lu\n", pid) < 0) 00208 { 00209 dbus_set_error (error, _dbus_error_from_errno (errno), 00210 "Failed to write to \"%s\": %s", cfilename, 00211 _dbus_strerror (errno)); 00212 00213 fclose (f); 00214 return FALSE; 00215 } 00216 00217 if (fclose (f) == EOF) 00218 { 00219 dbus_set_error (error, _dbus_error_from_errno (errno), 00220 "Failed to close \"%s\": %s", cfilename, 00221 _dbus_strerror (errno)); 00222 return FALSE; 00223 } 00224 00225 return TRUE; 00226 } 00227 00239 dbus_bool_t 00240 _dbus_write_pid_to_file_and_pipe (const DBusString *pidfile, 00241 DBusPipe *print_pid_pipe, 00242 dbus_pid_t pid_to_write, 00243 DBusError *error) 00244 { 00245 if (pidfile) 00246 { 00247 _dbus_verbose ("writing pid file %s\n", _dbus_string_get_const_data (pidfile)); 00248 if (!_dbus_write_pid_file (pidfile, 00249 pid_to_write, 00250 error)) 00251 { 00252 _dbus_verbose ("pid file write failed\n"); 00253 _DBUS_ASSERT_ERROR_IS_SET(error); 00254 return FALSE; 00255 } 00256 } 00257 else 00258 { 00259 _dbus_verbose ("No pid file requested\n"); 00260 } 00261 00262 if (print_pid_pipe != NULL && _dbus_pipe_is_valid (print_pid_pipe)) 00263 { 00264 DBusString pid; 00265 int bytes; 00266 00267 _dbus_verbose ("writing our pid to pipe %d\n", 00268 print_pid_pipe->fd); 00269 00270 if (!_dbus_string_init (&pid)) 00271 { 00272 _DBUS_SET_OOM (error); 00273 return FALSE; 00274 } 00275 00276 if (!_dbus_string_append_int (&pid, pid_to_write) || 00277 !_dbus_string_append (&pid, "\n")) 00278 { 00279 _dbus_string_free (&pid); 00280 _DBUS_SET_OOM (error); 00281 return FALSE; 00282 } 00283 00284 bytes = _dbus_string_get_length (&pid); 00285 if (_dbus_pipe_write (print_pid_pipe, &pid, 0, bytes, error) != bytes) 00286 { 00287 /* _dbus_pipe_write sets error only on failure, not short write */ 00288 if (error != NULL && !dbus_error_is_set(error)) 00289 { 00290 dbus_set_error (error, DBUS_ERROR_FAILED, 00291 "Printing message bus PID: did not write enough bytes\n"); 00292 } 00293 _dbus_string_free (&pid); 00294 return FALSE; 00295 } 00296 00297 _dbus_string_free (&pid); 00298 } 00299 else 00300 { 00301 _dbus_verbose ("No pid pipe to write to\n"); 00302 } 00303 00304 return TRUE; 00305 } 00306 00313 dbus_bool_t 00314 _dbus_verify_daemon_user (const char *user) 00315 { 00316 DBusString u; 00317 00318 _dbus_string_init_const (&u, user); 00319 00320 return _dbus_get_user_id_and_primary_group (&u, NULL, NULL); 00321 } 00322 00323 00324 /* The HAVE_LIBAUDIT case lives in selinux.c */ 00325 #ifndef HAVE_LIBAUDIT 00326 00333 dbus_bool_t 00334 _dbus_change_to_daemon_user (const char *user, 00335 DBusError *error) 00336 { 00337 dbus_uid_t uid; 00338 dbus_gid_t gid; 00339 DBusString u; 00340 00341 _dbus_string_init_const (&u, user); 00342 00343 if (!_dbus_get_user_id_and_primary_group (&u, &uid, &gid)) 00344 { 00345 dbus_set_error (error, DBUS_ERROR_FAILED, 00346 "User '%s' does not appear to exist?", 00347 user); 00348 return FALSE; 00349 } 00350 00351 /* setgroups() only works if we are a privileged process, 00352 * so we don't return error on failure; the only possible 00353 * failure is that we don't have perms to do it. 00354 * 00355 * not sure this is right, maybe if setuid() 00356 * is going to work then setgroups() should also work. 00357 */ 00358 if (setgroups (0, NULL) < 0) 00359 _dbus_warn ("Failed to drop supplementary groups: %s\n", 00360 _dbus_strerror (errno)); 00361 00362 /* Set GID first, or the setuid may remove our permission 00363 * to change the GID 00364 */ 00365 if (setgid (gid) < 0) 00366 { 00367 dbus_set_error (error, _dbus_error_from_errno (errno), 00368 "Failed to set GID to %lu: %s", gid, 00369 _dbus_strerror (errno)); 00370 return FALSE; 00371 } 00372 00373 if (setuid (uid) < 0) 00374 { 00375 dbus_set_error (error, _dbus_error_from_errno (errno), 00376 "Failed to set UID to %lu: %s", uid, 00377 _dbus_strerror (errno)); 00378 return FALSE; 00379 } 00380 00381 return TRUE; 00382 } 00383 #endif /* !HAVE_LIBAUDIT */ 00384 00385 #ifdef HAVE_SETRLIMIT 00386 00387 /* We assume that if we have setrlimit, we also have getrlimit and 00388 * struct rlimit. 00389 */ 00390 00391 struct DBusRLimit { 00392 struct rlimit lim; 00393 }; 00394 00395 DBusRLimit * 00396 _dbus_rlimit_save_fd_limit (DBusError *error) 00397 { 00398 DBusRLimit *self; 00399 00400 self = dbus_new0 (DBusRLimit, 1); 00401 00402 if (self == NULL) 00403 { 00404 _DBUS_SET_OOM (error); 00405 return NULL; 00406 } 00407 00408 if (getrlimit (RLIMIT_NOFILE, &self->lim) < 0) 00409 { 00410 dbus_set_error (error, _dbus_error_from_errno (errno), 00411 "Failed to get fd limit: %s", _dbus_strerror (errno)); 00412 dbus_free (self); 00413 return NULL; 00414 } 00415 00416 return self; 00417 } 00418 00419 dbus_bool_t 00420 _dbus_rlimit_raise_fd_limit_if_privileged (unsigned int desired, 00421 DBusError *error) 00422 { 00423 struct rlimit lim; 00424 00425 /* No point to doing this practically speaking 00426 * if we're not uid 0. We expect the system 00427 * bus to use this before we change UID, and 00428 * the session bus takes the Linux default, 00429 * currently 1024 for cur and 4096 for max. 00430 */ 00431 if (getuid () != 0) 00432 { 00433 /* not an error, we're probably the session bus */ 00434 return TRUE; 00435 } 00436 00437 if (getrlimit (RLIMIT_NOFILE, &lim) < 0) 00438 { 00439 dbus_set_error (error, _dbus_error_from_errno (errno), 00440 "Failed to get fd limit: %s", _dbus_strerror (errno)); 00441 return FALSE; 00442 } 00443 00444 if (lim.rlim_cur == RLIM_INFINITY || lim.rlim_cur >= desired) 00445 { 00446 /* not an error, everything is fine */ 00447 return TRUE; 00448 } 00449 00450 /* Ignore "maximum limit", assume we have the "superuser" 00451 * privileges. On Linux this is CAP_SYS_RESOURCE. 00452 */ 00453 lim.rlim_cur = lim.rlim_max = desired; 00454 00455 if (setrlimit (RLIMIT_NOFILE, &lim) < 0) 00456 { 00457 dbus_set_error (error, _dbus_error_from_errno (errno), 00458 "Failed to set fd limit to %u: %s", 00459 desired, _dbus_strerror (errno)); 00460 return FALSE; 00461 } 00462 00463 return TRUE; 00464 } 00465 00466 dbus_bool_t 00467 _dbus_rlimit_restore_fd_limit (DBusRLimit *saved, 00468 DBusError *error) 00469 { 00470 if (setrlimit (RLIMIT_NOFILE, &saved->lim) < 0) 00471 { 00472 dbus_set_error (error, _dbus_error_from_errno (errno), 00473 "Failed to restore old fd limit: %s", 00474 _dbus_strerror (errno)); 00475 return FALSE; 00476 } 00477 00478 return TRUE; 00479 } 00480 00481 #else /* !HAVE_SETRLIMIT */ 00482 00483 static void 00484 fd_limit_not_supported (DBusError *error) 00485 { 00486 dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED, 00487 "cannot change fd limit on this platform"); 00488 } 00489 00490 DBusRLimit * 00491 _dbus_rlimit_save_fd_limit (DBusError *error) 00492 { 00493 fd_limit_not_supported (error); 00494 return NULL; 00495 } 00496 00497 dbus_bool_t 00498 _dbus_rlimit_raise_fd_limit_if_privileged (unsigned int desired, 00499 DBusError *error) 00500 { 00501 fd_limit_not_supported (error); 00502 return FALSE; 00503 } 00504 00505 dbus_bool_t 00506 _dbus_rlimit_restore_fd_limit (DBusRLimit *saved, 00507 DBusError *error) 00508 { 00509 fd_limit_not_supported (error); 00510 return FALSE; 00511 } 00512 00513 #endif 00514 00515 void 00516 _dbus_rlimit_free (DBusRLimit *lim) 00517 { 00518 dbus_free (lim); 00519 } 00520 00521 void 00522 _dbus_init_system_log (dbus_bool_t is_daemon) 00523 { 00524 #ifdef HAVE_SYSLOG_H 00525 int logopts = LOG_PID; 00526 00527 #if LOG_PERROR 00528 #ifdef HAVE_SYSTEMD 00529 if (!is_daemon || sd_booted () <= 0) 00530 #endif 00531 logopts |= LOG_PERROR; 00532 #endif 00533 00534 openlog ("dbus", logopts, LOG_DAEMON); 00535 #endif 00536 } 00537 00544 void 00545 _dbus_system_log (DBusSystemLogSeverity severity, const char *msg, ...) 00546 { 00547 va_list args; 00548 00549 va_start (args, msg); 00550 00551 _dbus_system_logv (severity, msg, args); 00552 00553 va_end (args); 00554 } 00555 00566 void 00567 _dbus_system_logv (DBusSystemLogSeverity severity, const char *msg, va_list args) 00568 { 00569 va_list tmp; 00570 #ifdef HAVE_SYSLOG_H 00571 int flags; 00572 switch (severity) 00573 { 00574 case DBUS_SYSTEM_LOG_INFO: 00575 flags = LOG_DAEMON | LOG_NOTICE; 00576 break; 00577 case DBUS_SYSTEM_LOG_SECURITY: 00578 flags = LOG_AUTH | LOG_NOTICE; 00579 break; 00580 case DBUS_SYSTEM_LOG_FATAL: 00581 flags = LOG_DAEMON|LOG_CRIT; 00582 break; 00583 default: 00584 return; 00585 } 00586 00587 DBUS_VA_COPY (tmp, args); 00588 vsyslog (flags, msg, tmp); 00589 va_end (tmp); 00590 #endif 00591 00592 #if !defined(HAVE_SYSLOG_H) || !HAVE_DECL_LOG_PERROR 00593 { 00594 /* vsyslog() won't write to stderr, so we'd better do it */ 00595 DBUS_VA_COPY (tmp, args); 00596 fprintf (stderr, "dbus[" DBUS_PID_FORMAT "]: ", _dbus_getpid ()); 00597 vfprintf (stderr, msg, tmp); 00598 fputc ('\n', stderr); 00599 va_end (tmp); 00600 } 00601 #endif 00602 00603 if (severity == DBUS_SYSTEM_LOG_FATAL) 00604 exit (1); 00605 } 00606 00612 void 00613 _dbus_set_signal_handler (int sig, 00614 DBusSignalHandler handler) 00615 { 00616 struct sigaction act; 00617 sigset_t empty_mask; 00618 00619 sigemptyset (&empty_mask); 00620 act.sa_handler = handler; 00621 act.sa_mask = empty_mask; 00622 act.sa_flags = 0; 00623 sigaction (sig, &act, NULL); 00624 } 00625 00631 dbus_bool_t 00632 _dbus_file_exists (const char *file) 00633 { 00634 return (access (file, F_OK) == 0); 00635 } 00636 00643 dbus_bool_t 00644 _dbus_user_at_console (const char *username, 00645 DBusError *error) 00646 { 00647 00648 DBusString u, f; 00649 dbus_bool_t result; 00650 #ifdef __sun 00651 struct passwd *passwd_entry; 00652 #endif 00653 00654 result = FALSE; 00655 00656 #ifdef __sun 00657 passwd_entry = getpwnam (username); 00658 if (passwd_entry != NULL) 00659 { 00660 struct stat st; 00661 uid_t uid; 00662 00663 uid = passwd_entry->pw_uid; 00664 00665 if (stat ("/dev/vt/console_user", &st) == 0 && st.st_uid == uid) 00666 { 00667 /* 00668 * Owner is allowed to take over. Before we have real 00669 * ownership in HAL, assume it's the console owner. 00670 */ 00671 result = TRUE; 00672 } 00673 } 00674 #else 00675 if (!_dbus_string_init (&f)) 00676 { 00677 _DBUS_SET_OOM (error); 00678 return FALSE; 00679 } 00680 00681 if (!_dbus_string_append (&f, DBUS_CONSOLE_AUTH_DIR)) 00682 { 00683 _DBUS_SET_OOM (error); 00684 goto out; 00685 } 00686 00687 _dbus_string_init_const (&u, username); 00688 00689 if (!_dbus_concat_dir_and_file (&f, &u)) 00690 { 00691 _DBUS_SET_OOM (error); 00692 goto out; 00693 } 00694 00695 result = _dbus_file_exists (_dbus_string_get_const_data (&f)); 00696 00697 out: 00698 _dbus_string_free (&f); 00699 #endif 00700 00701 return result; 00702 } 00703 00704 00711 dbus_bool_t 00712 _dbus_path_is_absolute (const DBusString *filename) 00713 { 00714 if (_dbus_string_get_length (filename) > 0) 00715 return _dbus_string_get_byte (filename, 0) == '/'; 00716 else 00717 return FALSE; 00718 } 00719 00728 dbus_bool_t 00729 _dbus_stat (const DBusString *filename, 00730 DBusStat *statbuf, 00731 DBusError *error) 00732 { 00733 const char *filename_c; 00734 struct stat sb; 00735 00736 _DBUS_ASSERT_ERROR_IS_CLEAR (error); 00737 00738 filename_c = _dbus_string_get_const_data (filename); 00739 00740 if (stat (filename_c, &sb) < 0) 00741 { 00742 dbus_set_error (error, _dbus_error_from_errno (errno), 00743 "%s", _dbus_strerror (errno)); 00744 return FALSE; 00745 } 00746 00747 statbuf->mode = sb.st_mode; 00748 statbuf->nlink = sb.st_nlink; 00749 statbuf->uid = sb.st_uid; 00750 statbuf->gid = sb.st_gid; 00751 statbuf->size = sb.st_size; 00752 statbuf->atime = sb.st_atime; 00753 statbuf->mtime = sb.st_mtime; 00754 statbuf->ctime = sb.st_ctime; 00755 00756 return TRUE; 00757 } 00758 00759 00763 struct DBusDirIter 00764 { 00765 DIR *d; 00767 }; 00768 00776 DBusDirIter* 00777 _dbus_directory_open (const DBusString *filename, 00778 DBusError *error) 00779 { 00780 DIR *d; 00781 DBusDirIter *iter; 00782 const char *filename_c; 00783 00784 _DBUS_ASSERT_ERROR_IS_CLEAR (error); 00785 00786 filename_c = _dbus_string_get_const_data (filename); 00787 00788 d = opendir (filename_c); 00789 if (d == NULL) 00790 { 00791 dbus_set_error (error, _dbus_error_from_errno (errno), 00792 "Failed to read directory \"%s\": %s", 00793 filename_c, 00794 _dbus_strerror (errno)); 00795 return NULL; 00796 } 00797 iter = dbus_new0 (DBusDirIter, 1); 00798 if (iter == NULL) 00799 { 00800 closedir (d); 00801 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, 00802 "Could not allocate memory for directory iterator"); 00803 return NULL; 00804 } 00805 00806 iter->d = d; 00807 00808 return iter; 00809 } 00810 00824 dbus_bool_t 00825 _dbus_directory_get_next_file (DBusDirIter *iter, 00826 DBusString *filename, 00827 DBusError *error) 00828 { 00829 struct dirent *ent; 00830 int err; 00831 00832 _DBUS_ASSERT_ERROR_IS_CLEAR (error); 00833 00834 again: 00835 errno = 0; 00836 ent = readdir (iter->d); 00837 00838 if (!ent) 00839 { 00840 err = errno; 00841 00842 if (err != 0) 00843 dbus_set_error (error, 00844 _dbus_error_from_errno (err), 00845 "%s", _dbus_strerror (err)); 00846 00847 return FALSE; 00848 } 00849 else if (ent->d_name[0] == '.' && 00850 (ent->d_name[1] == '\0' || 00851 (ent->d_name[1] == '.' && ent->d_name[2] == '\0'))) 00852 goto again; 00853 else 00854 { 00855 _dbus_string_set_length (filename, 0); 00856 if (!_dbus_string_append (filename, ent->d_name)) 00857 { 00858 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, 00859 "No memory to read directory entry"); 00860 return FALSE; 00861 } 00862 else 00863 { 00864 return TRUE; 00865 } 00866 } 00867 } 00868 00872 void 00873 _dbus_directory_close (DBusDirIter *iter) 00874 { 00875 closedir (iter->d); 00876 dbus_free (iter); 00877 } 00878 00879 static dbus_bool_t 00880 fill_user_info_from_group (struct group *g, 00881 DBusGroupInfo *info, 00882 DBusError *error) 00883 { 00884 _dbus_assert (g->gr_name != NULL); 00885 00886 info->gid = g->gr_gid; 00887 info->groupname = _dbus_strdup (g->gr_name); 00888 00889 /* info->members = dbus_strdupv (g->gr_mem) */ 00890 00891 if (info->groupname == NULL) 00892 { 00893 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); 00894 return FALSE; 00895 } 00896 00897 return TRUE; 00898 } 00899 00900 static dbus_bool_t 00901 fill_group_info (DBusGroupInfo *info, 00902 dbus_gid_t gid, 00903 const DBusString *groupname, 00904 DBusError *error) 00905 { 00906 const char *group_c_str; 00907 00908 _dbus_assert (groupname != NULL || gid != DBUS_GID_UNSET); 00909 _dbus_assert (groupname == NULL || gid == DBUS_GID_UNSET); 00910 00911 if (groupname) 00912 group_c_str = _dbus_string_get_const_data (groupname); 00913 else 00914 group_c_str = NULL; 00915 00916 /* For now assuming that the getgrnam() and getgrgid() flavors 00917 * always correspond to the pwnam flavors, if not we have 00918 * to add more configure checks. 00919 */ 00920 00921 #if defined (HAVE_POSIX_GETPWNAM_R) || defined (HAVE_NONPOSIX_GETPWNAM_R) 00922 { 00923 struct group *g; 00924 int result; 00925 size_t buflen; 00926 char *buf; 00927 struct group g_str; 00928 dbus_bool_t b; 00929 00930 /* retrieve maximum needed size for buf */ 00931 buflen = sysconf (_SC_GETGR_R_SIZE_MAX); 00932 00933 /* sysconf actually returns a long, but everything else expects size_t, 00934 * so just recast here. 00935 * https://bugs.freedesktop.org/show_bug.cgi?id=17061 00936 */ 00937 if ((long) buflen <= 0) 00938 buflen = 1024; 00939 00940 result = -1; 00941 while (1) 00942 { 00943 buf = dbus_malloc (buflen); 00944 if (buf == NULL) 00945 { 00946 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); 00947 return FALSE; 00948 } 00949 00950 g = NULL; 00951 #ifdef HAVE_POSIX_GETPWNAM_R 00952 if (group_c_str) 00953 result = getgrnam_r (group_c_str, &g_str, buf, buflen, 00954 &g); 00955 else 00956 result = getgrgid_r (gid, &g_str, buf, buflen, 00957 &g); 00958 #else 00959 g = getgrnam_r (group_c_str, &g_str, buf, buflen); 00960 result = 0; 00961 #endif /* !HAVE_POSIX_GETPWNAM_R */ 00962 /* Try a bigger buffer if ERANGE was returned: 00963 https://bugs.freedesktop.org/show_bug.cgi?id=16727 00964 */ 00965 if (result == ERANGE && buflen < 512 * 1024) 00966 { 00967 dbus_free (buf); 00968 buflen *= 2; 00969 } 00970 else 00971 { 00972 break; 00973 } 00974 } 00975 00976 if (result == 0 && g == &g_str) 00977 { 00978 b = fill_user_info_from_group (g, info, error); 00979 dbus_free (buf); 00980 return b; 00981 } 00982 else 00983 { 00984 dbus_set_error (error, _dbus_error_from_errno (errno), 00985 "Group %s unknown or failed to look it up\n", 00986 group_c_str ? group_c_str : "???"); 00987 dbus_free (buf); 00988 return FALSE; 00989 } 00990 } 00991 #else /* ! HAVE_GETPWNAM_R */ 00992 { 00993 /* I guess we're screwed on thread safety here */ 00994 struct group *g; 00995 00996 g = getgrnam (group_c_str); 00997 00998 if (g != NULL) 00999 { 01000 return fill_user_info_from_group (g, info, error); 01001 } 01002 else 01003 { 01004 dbus_set_error (error, _dbus_error_from_errno (errno), 01005 "Group %s unknown or failed to look it up\n", 01006 group_c_str ? group_c_str : "???"); 01007 return FALSE; 01008 } 01009 } 01010 #endif /* ! HAVE_GETPWNAM_R */ 01011 } 01012 01022 dbus_bool_t 01023 _dbus_group_info_fill (DBusGroupInfo *info, 01024 const DBusString *groupname, 01025 DBusError *error) 01026 { 01027 return fill_group_info (info, DBUS_GID_UNSET, 01028 groupname, error); 01029 01030 } 01031 01041 dbus_bool_t 01042 _dbus_group_info_fill_gid (DBusGroupInfo *info, 01043 dbus_gid_t gid, 01044 DBusError *error) 01045 { 01046 return fill_group_info (info, gid, NULL, error); 01047 } 01048 01057 dbus_bool_t 01058 _dbus_parse_unix_user_from_config (const DBusString *username, 01059 dbus_uid_t *uid_p) 01060 { 01061 return _dbus_get_user_id (username, uid_p); 01062 01063 } 01064 01073 dbus_bool_t 01074 _dbus_parse_unix_group_from_config (const DBusString *groupname, 01075 dbus_gid_t *gid_p) 01076 { 01077 return _dbus_get_group_id (groupname, gid_p); 01078 } 01079 01090 dbus_bool_t 01091 _dbus_unix_groups_from_uid (dbus_uid_t uid, 01092 dbus_gid_t **group_ids, 01093 int *n_group_ids) 01094 { 01095 return _dbus_groups_from_uid (uid, group_ids, n_group_ids); 01096 } 01097 01107 dbus_bool_t 01108 _dbus_unix_user_is_at_console (dbus_uid_t uid, 01109 DBusError *error) 01110 { 01111 return _dbus_is_console_user (uid, error); 01112 01113 } 01114 01122 dbus_bool_t 01123 _dbus_unix_user_is_process_owner (dbus_uid_t uid) 01124 { 01125 return uid == _dbus_geteuid (); 01126 } 01127 01135 dbus_bool_t 01136 _dbus_windows_user_is_process_owner (const char *windows_sid) 01137 { 01138 return FALSE; 01139 } 01140 /* End of DBusInternalsUtils functions */ 01142 01154 dbus_bool_t 01155 _dbus_string_get_dirname (const DBusString *filename, 01156 DBusString *dirname) 01157 { 01158 int sep; 01159 01160 _dbus_assert (filename != dirname); 01161 _dbus_assert (filename != NULL); 01162 _dbus_assert (dirname != NULL); 01163 01164 /* Ignore any separators on the end */ 01165 sep = _dbus_string_get_length (filename); 01166 if (sep == 0) 01167 return _dbus_string_append (dirname, "."); /* empty string passed in */ 01168 01169 while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/') 01170 --sep; 01171 01172 _dbus_assert (sep >= 0); 01173 01174 if (sep == 0) 01175 return _dbus_string_append (dirname, "/"); 01176 01177 /* Now find the previous separator */ 01178 _dbus_string_find_byte_backward (filename, sep, '/', &sep); 01179 if (sep < 0) 01180 return _dbus_string_append (dirname, "."); 01181 01182 /* skip multiple separators */ 01183 while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/') 01184 --sep; 01185 01186 _dbus_assert (sep >= 0); 01187 01188 if (sep == 0 && 01189 _dbus_string_get_byte (filename, 0) == '/') 01190 return _dbus_string_append (dirname, "/"); 01191 else 01192 return _dbus_string_copy_len (filename, 0, sep - 0, 01193 dirname, _dbus_string_get_length (dirname)); 01194 } /* DBusString stuff */ 01196 01197 static void 01198 string_squash_nonprintable (DBusString *str) 01199 { 01200 unsigned char *buf; 01201 int i, len; 01202 01203 buf = _dbus_string_get_data (str); 01204 len = _dbus_string_get_length (str); 01205 01206 for (i = 0; i < len; i++) 01207 { 01208 unsigned char c = (unsigned char) buf[i]; 01209 if (c == '\0') 01210 buf[i] = ' '; 01211 else if (c < 0x20 || c > 127) 01212 buf[i] = '?'; 01213 } 01214 } 01215 01230 dbus_bool_t 01231 _dbus_command_for_pid (unsigned long pid, 01232 DBusString *str, 01233 int max_len, 01234 DBusError *error) 01235 { 01236 /* This is all Linux-specific for now */ 01237 DBusString path; 01238 DBusString cmdline; 01239 int fd; 01240 01241 if (!_dbus_string_init (&path)) 01242 { 01243 _DBUS_SET_OOM (error); 01244 return FALSE; 01245 } 01246 01247 if (!_dbus_string_init (&cmdline)) 01248 { 01249 _DBUS_SET_OOM (error); 01250 _dbus_string_free (&path); 01251 return FALSE; 01252 } 01253 01254 if (!_dbus_string_append_printf (&path, "/proc/%ld/cmdline", pid)) 01255 goto oom; 01256 01257 fd = open (_dbus_string_get_const_data (&path), O_RDONLY); 01258 if (fd < 0) 01259 { 01260 dbus_set_error (error, 01261 _dbus_error_from_errno (errno), 01262 "Failed to open \"%s\": %s", 01263 _dbus_string_get_const_data (&path), 01264 _dbus_strerror (errno)); 01265 goto fail; 01266 } 01267 01268 if (!_dbus_read (fd, &cmdline, max_len)) 01269 { 01270 dbus_set_error (error, 01271 _dbus_error_from_errno (errno), 01272 "Failed to read from \"%s\": %s", 01273 _dbus_string_get_const_data (&path), 01274 _dbus_strerror (errno)); 01275 _dbus_close (fd, NULL); 01276 goto fail; 01277 } 01278 01279 if (!_dbus_close (fd, error)) 01280 goto fail; 01281 01282 string_squash_nonprintable (&cmdline); 01283 01284 if (!_dbus_string_copy (&cmdline, 0, str, _dbus_string_get_length (str))) 01285 goto oom; 01286 01287 _dbus_string_free (&cmdline); 01288 _dbus_string_free (&path); 01289 return TRUE; 01290 oom: 01291 _DBUS_SET_OOM (error); 01292 fail: 01293 _dbus_string_free (&cmdline); 01294 _dbus_string_free (&path); 01295 return FALSE; 01296 } 01297 01298 /* 01299 * replaces the term DBUS_PREFIX in configure_time_path by the 01300 * current dbus installation directory. On unix this function is a noop 01301 * 01302 * @param configure_time_path 01303 * @return real path 01304 */ 01305 const char * 01306 _dbus_replace_install_prefix (const char *configure_time_path) 01307 { 01308 return configure_time_path; 01309 } 01310 01311 #define DBUS_UNIX_STANDARD_SESSION_SERVICEDIR "/dbus-1/services" 01312 #define DBUS_UNIX_STANDARD_SYSTEM_SERVICEDIR "/dbus-1/system-services" 01313 01331 dbus_bool_t 01332 _dbus_get_standard_session_servicedirs (DBusList **dirs) 01333 { 01334 const char *xdg_data_home; 01335 const char *xdg_data_dirs; 01336 DBusString servicedir_path; 01337 01338 if (!_dbus_string_init (&servicedir_path)) 01339 return FALSE; 01340 01341 xdg_data_home = _dbus_getenv ("XDG_DATA_HOME"); 01342 xdg_data_dirs = _dbus_getenv ("XDG_DATA_DIRS"); 01343 01344 if (xdg_data_home != NULL) 01345 { 01346 if (!_dbus_string_append (&servicedir_path, xdg_data_home)) 01347 goto oom; 01348 } 01349 else 01350 { 01351 const DBusString *homedir; 01352 DBusString local_share; 01353 01354 if (!_dbus_homedir_from_current_process (&homedir)) 01355 goto oom; 01356 01357 if (!_dbus_string_append (&servicedir_path, _dbus_string_get_const_data (homedir))) 01358 goto oom; 01359 01360 _dbus_string_init_const (&local_share, "/.local/share"); 01361 if (!_dbus_concat_dir_and_file (&servicedir_path, &local_share)) 01362 goto oom; 01363 } 01364 01365 if (!_dbus_string_append (&servicedir_path, ":")) 01366 goto oom; 01367 01368 if (xdg_data_dirs != NULL) 01369 { 01370 if (!_dbus_string_append (&servicedir_path, xdg_data_dirs)) 01371 goto oom; 01372 01373 if (!_dbus_string_append (&servicedir_path, ":")) 01374 goto oom; 01375 } 01376 else 01377 { 01378 if (!_dbus_string_append (&servicedir_path, "/usr/local/share:/usr/share:")) 01379 goto oom; 01380 } 01381 01382 /* 01383 * add configured datadir to defaults 01384 * this may be the same as an xdg dir 01385 * however the config parser should take 01386 * care of duplicates 01387 */ 01388 if (!_dbus_string_append (&servicedir_path, DBUS_DATADIR)) 01389 goto oom; 01390 01391 if (!_dbus_split_paths_and_append (&servicedir_path, 01392 DBUS_UNIX_STANDARD_SESSION_SERVICEDIR, 01393 dirs)) 01394 goto oom; 01395 01396 _dbus_string_free (&servicedir_path); 01397 return TRUE; 01398 01399 oom: 01400 _dbus_string_free (&servicedir_path); 01401 return FALSE; 01402 } 01403 01404 01423 dbus_bool_t 01424 _dbus_get_standard_system_servicedirs (DBusList **dirs) 01425 { 01426 /* 01427 * DBUS_DATADIR may be the same as one of the standard directories. However, 01428 * the config parser should take care of the duplicates. 01429 * 01430 * Also, append /lib as counterpart of /usr/share on the root 01431 * directory (the root directory does not know /share), in order to 01432 * facilitate early boot system bus activation where /usr might not 01433 * be available. 01434 */ 01435 static const char standard_search_path[] = 01436 "/usr/local/share:" 01437 "/usr/share:" 01438 DBUS_DATADIR ":" 01439 "/lib"; 01440 DBusString servicedir_path; 01441 01442 _dbus_string_init_const (&servicedir_path, standard_search_path); 01443 01444 return _dbus_split_paths_and_append (&servicedir_path, 01445 DBUS_UNIX_STANDARD_SYSTEM_SERVICEDIR, 01446 dirs); 01447 } 01448 01457 dbus_bool_t 01458 _dbus_append_system_config_file (DBusString *str) 01459 { 01460 return _dbus_string_append (str, DBUS_SYSTEM_CONFIG_FILE); 01461 } 01462 01469 dbus_bool_t 01470 _dbus_append_session_config_file (DBusString *str) 01471 { 01472 return _dbus_string_append (str, DBUS_SESSION_CONFIG_FILE); 01473 }
1.7.6.1