00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "dbus-sysdeps.h"
00025 #include "dbus-sysdeps-unix.h"
00026 #include "dbus-internals.h"
00027 #include "dbus-protocol.h"
00028 #include "dbus-string.h"
00029 #define DBUS_USERDB_INCLUDES_PRIVATE 1
00030 #include "dbus-userdb.h"
00031 #include "dbus-test.h"
00032
00033 #include <sys/types.h>
00034 #include <stdlib.h>
00035 #include <string.h>
00036 #include <signal.h>
00037 #include <unistd.h>
00038 #include <stdio.h>
00039 #include <errno.h>
00040 #include <fcntl.h>
00041 #include <sys/stat.h>
00042 #include <grp.h>
00043 #include <sys/socket.h>
00044 #include <dirent.h>
00045 #include <sys/un.h>
00046 #ifdef HAVE_LIBAUDIT
00047 #include <sys/prctl.h>
00048 #include <sys/capability.h>
00049 #include <libaudit.h>
00050 #endif
00051 #include <syslog.h>
00052
00053 #ifdef HAVE_SYS_SYSLIMITS_H
00054 #include <sys/syslimits.h>
00055 #endif
00056
00057 #ifndef O_BINARY
00058 #define O_BINARY 0
00059 #endif
00060
00075 dbus_bool_t
00076 _dbus_become_daemon (const DBusString *pidfile,
00077 DBusPipe *print_pid_pipe,
00078 DBusError *error)
00079 {
00080 const char *s;
00081 pid_t child_pid;
00082 int dev_null_fd;
00083
00084 _dbus_verbose ("Becoming a daemon...\n");
00085
00086 _dbus_verbose ("chdir to /\n");
00087 if (chdir ("/") < 0)
00088 {
00089 dbus_set_error (error, DBUS_ERROR_FAILED,
00090 "Could not chdir() to root directory");
00091 return FALSE;
00092 }
00093
00094 _dbus_verbose ("forking...\n");
00095 switch ((child_pid = fork ()))
00096 {
00097 case -1:
00098 _dbus_verbose ("fork failed\n");
00099 dbus_set_error (error, _dbus_error_from_errno (errno),
00100 "Failed to fork daemon: %s", _dbus_strerror (errno));
00101 return FALSE;
00102 break;
00103
00104 case 0:
00105 _dbus_verbose ("in child, closing std file descriptors\n");
00106
00107
00108
00109
00110
00111
00112 dev_null_fd = open ("/dev/null", O_RDWR);
00113 if (dev_null_fd >= 0)
00114 {
00115 dup2 (dev_null_fd, 0);
00116 dup2 (dev_null_fd, 1);
00117
00118 s = _dbus_getenv ("DBUS_DEBUG_OUTPUT");
00119 if (s == NULL || *s == '\0')
00120 dup2 (dev_null_fd, 2);
00121 else
00122 _dbus_verbose ("keeping stderr open due to DBUS_DEBUG_OUTPUT\n");
00123 }
00124
00125
00126 _dbus_verbose ("setting umask\n");
00127 umask (022);
00128
00129 _dbus_verbose ("calling setsid()\n");
00130 if (setsid () == -1)
00131 _dbus_assert_not_reached ("setsid() failed");
00132
00133 break;
00134
00135 default:
00136 if (!_dbus_write_pid_to_file_and_pipe (pidfile, print_pid_pipe,
00137 child_pid, error))
00138 {
00139 _dbus_verbose ("pid file or pipe write failed: %s\n",
00140 error->message);
00141 kill (child_pid, SIGTERM);
00142 return FALSE;
00143 }
00144
00145 _dbus_verbose ("parent exiting\n");
00146 _exit (0);
00147 break;
00148 }
00149
00150 return TRUE;
00151 }
00152
00153
00162 static dbus_bool_t
00163 _dbus_write_pid_file (const DBusString *filename,
00164 unsigned long pid,
00165 DBusError *error)
00166 {
00167 const char *cfilename;
00168 int fd;
00169 FILE *f;
00170
00171 cfilename = _dbus_string_get_const_data (filename);
00172
00173 fd = open (cfilename, O_WRONLY|O_CREAT|O_EXCL|O_BINARY, 0644);
00174
00175 if (fd < 0)
00176 {
00177 dbus_set_error (error, _dbus_error_from_errno (errno),
00178 "Failed to open \"%s\": %s", cfilename,
00179 _dbus_strerror (errno));
00180 return FALSE;
00181 }
00182
00183 if ((f = fdopen (fd, "w")) == NULL)
00184 {
00185 dbus_set_error (error, _dbus_error_from_errno (errno),
00186 "Failed to fdopen fd %d: %s", fd, _dbus_strerror (errno));
00187 _dbus_close (fd, NULL);
00188 return FALSE;
00189 }
00190
00191 if (fprintf (f, "%lu\n", pid) < 0)
00192 {
00193 dbus_set_error (error, _dbus_error_from_errno (errno),
00194 "Failed to write to \"%s\": %s", cfilename,
00195 _dbus_strerror (errno));
00196
00197 fclose (f);
00198 return FALSE;
00199 }
00200
00201 if (fclose (f) == EOF)
00202 {
00203 dbus_set_error (error, _dbus_error_from_errno (errno),
00204 "Failed to close \"%s\": %s", cfilename,
00205 _dbus_strerror (errno));
00206 return FALSE;
00207 }
00208
00209 return TRUE;
00210 }
00211
00223 dbus_bool_t
00224 _dbus_write_pid_to_file_and_pipe (const DBusString *pidfile,
00225 DBusPipe *print_pid_pipe,
00226 dbus_pid_t pid_to_write,
00227 DBusError *error)
00228 {
00229 if (pidfile)
00230 {
00231 _dbus_verbose ("writing pid file %s\n", _dbus_string_get_const_data (pidfile));
00232 if (!_dbus_write_pid_file (pidfile,
00233 pid_to_write,
00234 error))
00235 {
00236 _dbus_verbose ("pid file write failed\n");
00237 _DBUS_ASSERT_ERROR_IS_SET(error);
00238 return FALSE;
00239 }
00240 }
00241 else
00242 {
00243 _dbus_verbose ("No pid file requested\n");
00244 }
00245
00246 if (print_pid_pipe != NULL && _dbus_pipe_is_valid (print_pid_pipe))
00247 {
00248 DBusString pid;
00249 int bytes;
00250
00251 _dbus_verbose ("writing our pid to pipe %d\n", print_pid_pipe->fd_or_handle);
00252
00253 if (!_dbus_string_init (&pid))
00254 {
00255 _DBUS_SET_OOM (error);
00256 return FALSE;
00257 }
00258
00259 if (!_dbus_string_append_int (&pid, pid_to_write) ||
00260 !_dbus_string_append (&pid, "\n"))
00261 {
00262 _dbus_string_free (&pid);
00263 _DBUS_SET_OOM (error);
00264 return FALSE;
00265 }
00266
00267 bytes = _dbus_string_get_length (&pid);
00268 if (_dbus_pipe_write (print_pid_pipe, &pid, 0, bytes, error) != bytes)
00269 {
00270
00271 if (error != NULL && !dbus_error_is_set(error))
00272 {
00273 dbus_set_error (error, DBUS_ERROR_FAILED,
00274 "Printing message bus PID: did not write enough bytes\n");
00275 }
00276 _dbus_string_free (&pid);
00277 return FALSE;
00278 }
00279
00280 _dbus_string_free (&pid);
00281 }
00282 else
00283 {
00284 _dbus_verbose ("No pid pipe to write to\n");
00285 }
00286
00287 return TRUE;
00288 }
00289
00296 dbus_bool_t
00297 _dbus_verify_daemon_user (const char *user)
00298 {
00299 DBusString u;
00300
00301 _dbus_string_init_const (&u, user);
00302
00303 return _dbus_get_user_id_and_primary_group (&u, NULL, NULL);
00304 }
00305
00313 dbus_bool_t
00314 _dbus_change_to_daemon_user (const char *user,
00315 DBusError *error)
00316 {
00317 dbus_uid_t uid;
00318 dbus_gid_t gid;
00319 DBusString u;
00320 #ifdef HAVE_LIBAUDIT
00321 dbus_bool_t we_were_root;
00322 cap_t new_caps;
00323 #endif
00324
00325 _dbus_string_init_const (&u, user);
00326
00327 if (!_dbus_get_user_id_and_primary_group (&u, &uid, &gid))
00328 {
00329 dbus_set_error (error, DBUS_ERROR_FAILED,
00330 "User '%s' does not appear to exist?",
00331 user);
00332 return FALSE;
00333 }
00334
00335 #ifdef HAVE_LIBAUDIT
00336 we_were_root = _dbus_geteuid () == 0;
00337 new_caps = NULL;
00338
00339
00340
00341
00342 if (we_were_root)
00343 {
00344 cap_value_t new_cap_list[] = { CAP_AUDIT_WRITE };
00345 cap_value_t tmp_cap_list[] = { CAP_AUDIT_WRITE, CAP_SETUID, CAP_SETGID };
00346 cap_t tmp_caps = cap_init();
00347
00348 if (!tmp_caps || !(new_caps = cap_init ()))
00349 {
00350 dbus_set_error (error, DBUS_ERROR_FAILED,
00351 "Failed to initialize drop of capabilities: %s\n",
00352 _dbus_strerror (errno));
00353
00354 if (tmp_caps)
00355 cap_free (tmp_caps);
00356
00357 return FALSE;
00358 }
00359
00360
00361 cap_set_flag (new_caps, CAP_PERMITTED, 1, new_cap_list, CAP_SET);
00362 cap_set_flag (new_caps, CAP_EFFECTIVE, 1, new_cap_list, CAP_SET);
00363 cap_set_flag (tmp_caps, CAP_PERMITTED, 3, tmp_cap_list, CAP_SET);
00364 cap_set_flag (tmp_caps, CAP_EFFECTIVE, 3, tmp_cap_list, CAP_SET);
00365
00366 if (prctl (PR_SET_KEEPCAPS, 1, 0, 0, 0) == -1)
00367 {
00368 dbus_set_error (error, _dbus_error_from_errno (errno),
00369 "Failed to set keep-capabilities: %s\n",
00370 _dbus_strerror (errno));
00371 cap_free (tmp_caps);
00372 goto fail;
00373 }
00374
00375 if (cap_set_proc (tmp_caps) == -1)
00376 {
00377 dbus_set_error (error, DBUS_ERROR_FAILED,
00378 "Failed to drop capabilities: %s\n",
00379 _dbus_strerror (errno));
00380 cap_free (tmp_caps);
00381 goto fail;
00382 }
00383 cap_free (tmp_caps);
00384 }
00385 #endif
00386
00387
00388
00389
00390
00391
00392
00393
00394 if (setgroups (0, NULL) < 0)
00395 _dbus_warn ("Failed to drop supplementary groups: %s\n",
00396 _dbus_strerror (errno));
00397
00398
00399
00400
00401 if (setgid (gid) < 0)
00402 {
00403 dbus_set_error (error, _dbus_error_from_errno (errno),
00404 "Failed to set GID to %lu: %s", gid,
00405 _dbus_strerror (errno));
00406 goto fail;
00407 }
00408
00409 if (setuid (uid) < 0)
00410 {
00411 dbus_set_error (error, _dbus_error_from_errno (errno),
00412 "Failed to set UID to %lu: %s", uid,
00413 _dbus_strerror (errno));
00414 goto fail;
00415 }
00416
00417 #ifdef HAVE_LIBAUDIT
00418 if (we_were_root)
00419 {
00420 if (cap_set_proc (new_caps))
00421 {
00422 dbus_set_error (error, DBUS_ERROR_FAILED,
00423 "Failed to drop capabilities: %s\n",
00424 _dbus_strerror (errno));
00425 goto fail;
00426 }
00427 cap_free (new_caps);
00428
00429
00430 if (prctl (PR_SET_KEEPCAPS, 0, 0, 0, 0) == -1)
00431 {
00432 dbus_set_error (error, _dbus_error_from_errno (errno),
00433 "Failed to unset keep-capabilities: %s\n",
00434 _dbus_strerror (errno));
00435 return FALSE;
00436 }
00437 }
00438 #endif
00439
00440 return TRUE;
00441
00442 fail:
00443 #ifdef HAVE_LIBAUDIT
00444 if (!we_were_root)
00445 {
00446
00447 prctl (PR_SET_KEEPCAPS, 0, 0, 0, 0);
00448 cap_free (new_caps);
00449 }
00450 #endif
00451
00452 return FALSE;
00453 }
00454
00455 void
00456 _dbus_init_system_log (void)
00457 {
00458 openlog ("dbus", LOG_PID, LOG_DAEMON);
00459 }
00460
00468 void
00469 _dbus_log_info (const char *msg, va_list args)
00470 {
00471 vsyslog (LOG_DAEMON|LOG_NOTICE, msg, args);
00472 }
00473
00481 void
00482 _dbus_log_security (const char *msg, va_list args)
00483 {
00484 vsyslog (LOG_AUTH|LOG_NOTICE, msg, args);
00485 }
00486
00492 void
00493 _dbus_set_signal_handler (int sig,
00494 DBusSignalHandler handler)
00495 {
00496 struct sigaction act;
00497 sigset_t empty_mask;
00498
00499 sigemptyset (&empty_mask);
00500 act.sa_handler = handler;
00501 act.sa_mask = empty_mask;
00502 act.sa_flags = 0;
00503 sigaction (sig, &act, NULL);
00504 }
00505
00506
00514 dbus_bool_t
00515 _dbus_delete_directory (const DBusString *filename,
00516 DBusError *error)
00517 {
00518 const char *filename_c;
00519
00520 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00521
00522 filename_c = _dbus_string_get_const_data (filename);
00523
00524 if (rmdir (filename_c) != 0)
00525 {
00526 dbus_set_error (error, DBUS_ERROR_FAILED,
00527 "Failed to remove directory %s: %s\n",
00528 filename_c, _dbus_strerror (errno));
00529 return FALSE;
00530 }
00531
00532 return TRUE;
00533 }
00534
00540 dbus_bool_t
00541 _dbus_file_exists (const char *file)
00542 {
00543 return (access (file, F_OK) == 0);
00544 }
00545
00552 dbus_bool_t
00553 _dbus_user_at_console (const char *username,
00554 DBusError *error)
00555 {
00556
00557 DBusString f;
00558 dbus_bool_t result;
00559
00560 result = FALSE;
00561 if (!_dbus_string_init (&f))
00562 {
00563 _DBUS_SET_OOM (error);
00564 return FALSE;
00565 }
00566
00567 if (!_dbus_string_append (&f, DBUS_CONSOLE_AUTH_DIR))
00568 {
00569 _DBUS_SET_OOM (error);
00570 goto out;
00571 }
00572
00573
00574 if (!_dbus_string_append (&f, username))
00575 {
00576 _DBUS_SET_OOM (error);
00577 goto out;
00578 }
00579
00580 result = _dbus_file_exists (_dbus_string_get_const_data (&f));
00581
00582 out:
00583 _dbus_string_free (&f);
00584
00585 return result;
00586 }
00587
00588
00595 dbus_bool_t
00596 _dbus_path_is_absolute (const DBusString *filename)
00597 {
00598 if (_dbus_string_get_length (filename) > 0)
00599 return _dbus_string_get_byte (filename, 0) == '/';
00600 else
00601 return FALSE;
00602 }
00603
00612 dbus_bool_t
00613 _dbus_stat (const DBusString *filename,
00614 DBusStat *statbuf,
00615 DBusError *error)
00616 {
00617 const char *filename_c;
00618 struct stat sb;
00619
00620 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00621
00622 filename_c = _dbus_string_get_const_data (filename);
00623
00624 if (stat (filename_c, &sb) < 0)
00625 {
00626 dbus_set_error (error, _dbus_error_from_errno (errno),
00627 "%s", _dbus_strerror (errno));
00628 return FALSE;
00629 }
00630
00631 statbuf->mode = sb.st_mode;
00632 statbuf->nlink = sb.st_nlink;
00633 statbuf->uid = sb.st_uid;
00634 statbuf->gid = sb.st_gid;
00635 statbuf->size = sb.st_size;
00636 statbuf->atime = sb.st_atime;
00637 statbuf->mtime = sb.st_mtime;
00638 statbuf->ctime = sb.st_ctime;
00639
00640 return TRUE;
00641 }
00642
00643
00647 struct DBusDirIter
00648 {
00649 DIR *d;
00651 };
00652
00660 DBusDirIter*
00661 _dbus_directory_open (const DBusString *filename,
00662 DBusError *error)
00663 {
00664 DIR *d;
00665 DBusDirIter *iter;
00666 const char *filename_c;
00667
00668 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00669
00670 filename_c = _dbus_string_get_const_data (filename);
00671
00672 d = opendir (filename_c);
00673 if (d == NULL)
00674 {
00675 dbus_set_error (error, _dbus_error_from_errno (errno),
00676 "Failed to read directory \"%s\": %s",
00677 filename_c,
00678 _dbus_strerror (errno));
00679 return NULL;
00680 }
00681 iter = dbus_new0 (DBusDirIter, 1);
00682 if (iter == NULL)
00683 {
00684 closedir (d);
00685 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
00686 "Could not allocate memory for directory iterator");
00687 return NULL;
00688 }
00689
00690 iter->d = d;
00691
00692 return iter;
00693 }
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703 static dbus_bool_t
00704 dirent_buf_size(DIR * dirp, size_t *size)
00705 {
00706 long name_max;
00707 # if defined(HAVE_FPATHCONF) && defined(_PC_NAME_MAX)
00708 # if defined(HAVE_DIRFD)
00709 name_max = fpathconf(dirfd(dirp), _PC_NAME_MAX);
00710 # elif defined(HAVE_DDFD)
00711 name_max = fpathconf(dirp->dd_fd, _PC_NAME_MAX);
00712 # else
00713 name_max = fpathconf(dirp->__dd_fd, _PC_NAME_MAX);
00714 # endif
00715 if (name_max == -1)
00716 # if defined(NAME_MAX)
00717 name_max = NAME_MAX;
00718 # else
00719 return FALSE;
00720 # endif
00721 # elif defined(MAXNAMELEN)
00722 name_max = MAXNAMELEN;
00723 # else
00724 # if defined(NAME_MAX)
00725 name_max = NAME_MAX;
00726 # else
00727 # error "buffer size for readdir_r cannot be determined"
00728 # endif
00729 # endif
00730 if (size)
00731 *size = (size_t)offsetof(struct dirent, d_name) + name_max + 1;
00732 else
00733 return FALSE;
00734
00735 return TRUE;
00736 }
00737
00748 dbus_bool_t
00749 _dbus_directory_get_next_file (DBusDirIter *iter,
00750 DBusString *filename,
00751 DBusError *error)
00752 {
00753 struct dirent *d, *ent;
00754 size_t buf_size;
00755 int err;
00756
00757 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00758
00759 if (!dirent_buf_size (iter->d, &buf_size))
00760 {
00761 dbus_set_error (error, DBUS_ERROR_FAILED,
00762 "Can't calculate buffer size when reading directory");
00763 return FALSE;
00764 }
00765
00766 d = (struct dirent *)dbus_malloc (buf_size);
00767 if (!d)
00768 {
00769 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
00770 "No memory to read directory entry");
00771 return FALSE;
00772 }
00773
00774 again:
00775 err = readdir_r (iter->d, d, &ent);
00776 if (err || !ent)
00777 {
00778 if (err != 0)
00779 dbus_set_error (error,
00780 _dbus_error_from_errno (err),
00781 "%s", _dbus_strerror (err));
00782
00783 dbus_free (d);
00784 return FALSE;
00785 }
00786 else if (ent->d_name[0] == '.' &&
00787 (ent->d_name[1] == '\0' ||
00788 (ent->d_name[1] == '.' && ent->d_name[2] == '\0')))
00789 goto again;
00790 else
00791 {
00792 _dbus_string_set_length (filename, 0);
00793 if (!_dbus_string_append (filename, ent->d_name))
00794 {
00795 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
00796 "No memory to read directory entry");
00797 dbus_free (d);
00798 return FALSE;
00799 }
00800 else
00801 {
00802 dbus_free (d);
00803 return TRUE;
00804 }
00805 }
00806 }
00807
00811 void
00812 _dbus_directory_close (DBusDirIter *iter)
00813 {
00814 closedir (iter->d);
00815 dbus_free (iter);
00816 }
00817
00818 static dbus_bool_t
00819 fill_user_info_from_group (struct group *g,
00820 DBusGroupInfo *info,
00821 DBusError *error)
00822 {
00823 _dbus_assert (g->gr_name != NULL);
00824
00825 info->gid = g->gr_gid;
00826 info->groupname = _dbus_strdup (g->gr_name);
00827
00828
00829
00830 if (info->groupname == NULL)
00831 {
00832 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
00833 return FALSE;
00834 }
00835
00836 return TRUE;
00837 }
00838
00839 static dbus_bool_t
00840 fill_group_info (DBusGroupInfo *info,
00841 dbus_gid_t gid,
00842 const DBusString *groupname,
00843 DBusError *error)
00844 {
00845 const char *group_c_str;
00846
00847 _dbus_assert (groupname != NULL || gid != DBUS_GID_UNSET);
00848 _dbus_assert (groupname == NULL || gid == DBUS_GID_UNSET);
00849
00850 if (groupname)
00851 group_c_str = _dbus_string_get_const_data (groupname);
00852 else
00853 group_c_str = NULL;
00854
00855
00856
00857
00858
00859
00860 #if defined (HAVE_POSIX_GETPWNAM_R) || defined (HAVE_NONPOSIX_GETPWNAM_R)
00861 {
00862 struct group *g;
00863 int result;
00864 size_t buflen;
00865 char *buf;
00866 struct group g_str;
00867 dbus_bool_t b;
00868
00869
00870 buflen = sysconf (_SC_GETGR_R_SIZE_MAX);
00871
00872 if (buflen <= 0)
00873 buflen = 1024;
00874
00875 result = -1;
00876 while (1)
00877 {
00878 buf = dbus_malloc (buflen);
00879 if (buf == NULL)
00880 {
00881 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
00882 return FALSE;
00883 }
00884
00885 g = NULL;
00886 #ifdef HAVE_POSIX_GETPWNAM_R
00887 if (group_c_str)
00888 result = getgrnam_r (group_c_str, &g_str, buf, buflen,
00889 &g);
00890 else
00891 result = getgrgid_r (gid, &g_str, buf, buflen,
00892 &g);
00893 #else
00894 g = getgrnam_r (group_c_str, &g_str, buf, buflen);
00895 result = 0;
00896 #endif
00897
00898
00899
00900 if (result == ERANGE && buflen < 512 * 1024)
00901 {
00902 dbus_free (buf);
00903 buflen *= 2;
00904 }
00905 else
00906 {
00907 break;
00908 }
00909 }
00910
00911 if (result == 0 && g == &g_str)
00912 {
00913 b = fill_user_info_from_group (g, info, error);
00914 dbus_free (buf);
00915 return b;
00916 }
00917 else
00918 {
00919 dbus_set_error (error, _dbus_error_from_errno (errno),
00920 "Group %s unknown or failed to look it up\n",
00921 group_c_str ? group_c_str : "???");
00922 dbus_free (buf);
00923 return FALSE;
00924 }
00925 }
00926 #else
00927 {
00928
00929 struct group *g;
00930
00931 g = getgrnam (group_c_str);
00932
00933 if (g != NULL)
00934 {
00935 return fill_user_info_from_group (g, info, error);
00936 }
00937 else
00938 {
00939 dbus_set_error (error, _dbus_error_from_errno (errno),
00940 "Group %s unknown or failed to look it up\n",
00941 group_c_str ? group_c_str : "???");
00942 return FALSE;
00943 }
00944 }
00945 #endif
00946 }
00947
00957 dbus_bool_t
00958 _dbus_group_info_fill (DBusGroupInfo *info,
00959 const DBusString *groupname,
00960 DBusError *error)
00961 {
00962 return fill_group_info (info, DBUS_GID_UNSET,
00963 groupname, error);
00964
00965 }
00966
00976 dbus_bool_t
00977 _dbus_group_info_fill_gid (DBusGroupInfo *info,
00978 dbus_gid_t gid,
00979 DBusError *error)
00980 {
00981 return fill_group_info (info, gid, NULL, error);
00982 }
00983
00992 dbus_bool_t
00993 _dbus_parse_unix_user_from_config (const DBusString *username,
00994 dbus_uid_t *uid_p)
00995 {
00996 return _dbus_get_user_id (username, uid_p);
00997
00998 }
00999
01008 dbus_bool_t
01009 _dbus_parse_unix_group_from_config (const DBusString *groupname,
01010 dbus_gid_t *gid_p)
01011 {
01012 return _dbus_get_group_id (groupname, gid_p);
01013 }
01014
01025 dbus_bool_t
01026 _dbus_unix_groups_from_uid (dbus_uid_t uid,
01027 dbus_gid_t **group_ids,
01028 int *n_group_ids)
01029 {
01030 return _dbus_groups_from_uid (uid, group_ids, n_group_ids);
01031 }
01032
01042 dbus_bool_t
01043 _dbus_unix_user_is_at_console (dbus_uid_t uid,
01044 DBusError *error)
01045 {
01046 return _dbus_is_console_user (uid, error);
01047
01048 }
01049
01057 dbus_bool_t
01058 _dbus_unix_user_is_process_owner (dbus_uid_t uid)
01059 {
01060 return uid == _dbus_geteuid ();
01061 }
01062
01070 dbus_bool_t
01071 _dbus_windows_user_is_process_owner (const char *windows_sid)
01072 {
01073 return FALSE;
01074 }
01075
01077
01089 dbus_bool_t
01090 _dbus_string_get_dirname (const DBusString *filename,
01091 DBusString *dirname)
01092 {
01093 int sep;
01094
01095 _dbus_assert (filename != dirname);
01096 _dbus_assert (filename != NULL);
01097 _dbus_assert (dirname != NULL);
01098
01099
01100 sep = _dbus_string_get_length (filename);
01101 if (sep == 0)
01102 return _dbus_string_append (dirname, ".");
01103
01104 while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
01105 --sep;
01106
01107 _dbus_assert (sep >= 0);
01108
01109 if (sep == 0)
01110 return _dbus_string_append (dirname, "/");
01111
01112
01113 _dbus_string_find_byte_backward (filename, sep, '/', &sep);
01114 if (sep < 0)
01115 return _dbus_string_append (dirname, ".");
01116
01117
01118 while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
01119 --sep;
01120
01121 _dbus_assert (sep >= 0);
01122
01123 if (sep == 0 &&
01124 _dbus_string_get_byte (filename, 0) == '/')
01125 return _dbus_string_append (dirname, "/");
01126 else
01127 return _dbus_string_copy_len (filename, 0, sep - 0,
01128 dirname, _dbus_string_get_length (dirname));
01129 }
01131
01132 static void
01133 string_squash_nonprintable (DBusString *str)
01134 {
01135 char *buf;
01136 int i, len;
01137
01138 buf = _dbus_string_get_data (str);
01139 len = _dbus_string_get_length (str);
01140
01141 for (i = 0; i < len; i++)
01142 if (buf[i] == '\0')
01143 buf[i] = ' ';
01144 else if (buf[i] < 0x20 || buf[i] > 127)
01145 buf[i] = '?';
01146 }
01147
01162 dbus_bool_t
01163 _dbus_command_for_pid (unsigned long pid,
01164 DBusString *str,
01165 int max_len,
01166 DBusError *error)
01167 {
01168
01169 DBusString path;
01170 DBusString cmdline;
01171 int fd;
01172
01173 if (!_dbus_string_init (&path))
01174 {
01175 _DBUS_SET_OOM (error);
01176 return FALSE;
01177 }
01178
01179 if (!_dbus_string_init (&cmdline))
01180 {
01181 _DBUS_SET_OOM (error);
01182 _dbus_string_free (&path);
01183 return FALSE;
01184 }
01185
01186 if (!_dbus_string_append_printf (&path, "/proc/%ld/cmdline", pid))
01187 goto oom;
01188
01189 fd = open (_dbus_string_get_const_data (&path), O_RDONLY);
01190 if (fd < 0)
01191 {
01192 dbus_set_error (error,
01193 _dbus_error_from_errno (errno),
01194 "Failed to open \"%s\": %s",
01195 _dbus_string_get_const_data (&path),
01196 _dbus_strerror (errno));
01197 goto fail;
01198 }
01199
01200 if (!_dbus_read (fd, &cmdline, max_len))
01201 {
01202 dbus_set_error (error,
01203 _dbus_error_from_errno (errno),
01204 "Failed to read from \"%s\": %s",
01205 _dbus_string_get_const_data (&path),
01206 _dbus_strerror (errno));
01207 goto fail;
01208 }
01209
01210 if (!_dbus_close (fd, error))
01211 goto fail;
01212
01213 string_squash_nonprintable (&cmdline);
01214
01215 if (!_dbus_string_copy (&cmdline, 0, str, _dbus_string_get_length (str)))
01216 goto oom;
01217
01218 _dbus_string_free (&cmdline);
01219 _dbus_string_free (&path);
01220 return TRUE;
01221 oom:
01222 _DBUS_SET_OOM (error);
01223 fail:
01224 _dbus_string_free (&cmdline);
01225 _dbus_string_free (&path);
01226 return FALSE;
01227 }