diff options
author | Lennart Poettering <lennart@poettering.net> | 2009-08-07 00:25:08 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2009-10-17 00:28:29 +0200 |
commit | dc3f32f00d7c3091d7d44fe1739a8c38ea3fb34f (patch) | |
tree | f4f873446abf31e15f30550b817c71946453db15 /dbus/dbus-userdb-util.c | |
parent | dc11af9d1b7f029e6a58fd3354438032877efce4 (diff) |
check ConsoleKit database for detecting if user is on console
In addtion to Solaris style /dev/console permission checking and
pam_console style /var/run/console file existance checking add support
for checking console status via the ConsoleKit database.
This adds very basic support and will read the console database on every
single read. These needs optimization.
Diffstat (limited to 'dbus/dbus-userdb-util.c')
-rw-r--r-- | dbus/dbus-userdb-util.c | 98 |
1 files changed, 77 insertions, 21 deletions
diff --git a/dbus/dbus-userdb-util.c b/dbus/dbus-userdb-util.c index f75d1bc0..8361fcf3 100644 --- a/dbus/dbus-userdb-util.c +++ b/dbus/dbus-userdb-util.c @@ -1,10 +1,10 @@ /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ /* dbus-userdb-util.c Would be in dbus-userdb.c, but not used in libdbus - * + * * Copyright (C) 2003, 2004, 2005 Red Hat, Inc. * * Licensed under the Academic Free License version 2.1 - * + * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or @@ -14,7 +14,7 @@ * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA @@ -25,7 +25,10 @@ #include "dbus-test.h" #include "dbus-internals.h" #include "dbus-protocol.h" +#include "dbus-desktop-file.h" #include <string.h> +#include <errno.h> +#include <stdlib.h> /** * @addtogroup DBusInternalsUtils @@ -35,7 +38,7 @@ /** * Checks to see if the UID sent in is the console user * - * @param uid UID of person to check + * @param uid UID of person to check * @param error return location for errors * @returns #TRUE if the UID is the same as the console user and there are no errors */ @@ -44,11 +47,48 @@ _dbus_is_console_user (dbus_uid_t uid, DBusError *error) { - DBusUserDatabase *db; - const DBusUserInfo *info; - dbus_bool_t result = FALSE; +#if defined(HAVE_CONSOLEKIT_DATABASE) + + DBusDesktopFile *f; + const char *section; + dbus_bool_t found = FALSE; + DBusString fn; + unsigned i = 0; + + _dbus_string_init_const(&fn, DBUS_CONSOLEKIT_DATABASE); + + f = _dbus_desktop_file_load(&fn, error); + if (!f) + return FALSE; + + while ((section = _dbus_desktop_file_get_section(f, i++))) + { + const char *val; + char *e = NULL; + unsigned long console_uid; + + if (strncmp(section, "Session ", 8)) + continue; + + if (!_dbus_desktop_file_get_raw(f, section, "uid", &val)) + continue; + + errno = 0; + console_uid = strtol(val, &e, 10); + if (errno != 0 || !e || *e != 0) + continue; + + if (console_uid == uid) + { + found = TRUE; + break; + } + } + + _dbus_desktop_file_free(f); + return found; -#ifdef HAVE_CONSOLE_OWNER_FILE +#elif defined(HAVE_CONSOLE_OWNER_FILE) DBusString f; DBusStat st; @@ -74,7 +114,13 @@ _dbus_is_console_user (dbus_uid_t uid, _dbus_string_free(&f); -#endif /* HAVE_CONSOLE_OWNER_FILE */ + return FALSE; + +#elif defined(HAVE_CONSOLE_AUTH_DIR) + + DBusUserDatabase *db; + const DBusUserInfo *info; + dbus_bool_t result = FALSE; _dbus_user_database_lock_system (); @@ -101,6 +147,9 @@ _dbus_is_console_user (dbus_uid_t uid, _dbus_user_database_unlock_system (); return result; +#else + return FALSE; +#endif } /** @@ -147,7 +196,7 @@ _dbus_get_group_id (const DBusString *groupname, } *gid = info->gid; - + _dbus_user_database_unlock_system (); return TRUE; } @@ -187,7 +236,7 @@ _dbus_get_user_id_and_primary_group (const DBusString *username, *uid_p = info->uid; if (gid_p) *gid_p = info->primary_gid; - + _dbus_user_database_unlock_system (); return TRUE; } @@ -200,7 +249,7 @@ _dbus_get_user_id_and_primary_group (const DBusString *username, * * @param db the database * @param gid the group ID or #DBUS_GID_UNSET - * @param groupname group name or #NULL + * @param groupname group name or #NULL * @param error error to fill in * @returns the entry in the database */ @@ -246,7 +295,7 @@ _dbus_user_database_lookup_group (DBusUserDatabase *db, else _dbus_verbose ("No cache for groupname \"%s\"\n", _dbus_string_get_const_data (groupname)); - + info = dbus_new0 (DBusGroupInfo, 1); if (info == NULL) { @@ -293,7 +342,7 @@ _dbus_user_database_lookup_group (DBusUserDatabase *db, dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); return NULL; } - + return info; } } @@ -301,7 +350,7 @@ _dbus_user_database_lookup_group (DBusUserDatabase *db, /** * Gets the user information for the given group name, - * returned group info should not be freed. + * returned group info should not be freed. * * @param db user database * @param groupname the group name @@ -321,7 +370,7 @@ _dbus_user_database_get_groupname (DBusUserDatabase *db, /** * Gets the user information for the given GID, - * returned group info should not be freed. + * returned group info should not be freed. * * @param db user database * @param gid the group ID @@ -343,7 +392,7 @@ _dbus_user_database_get_gid (DBusUserDatabase *db, /** * Gets all groups corresponding to the given UID. Returns #FALSE * if no memory, or user isn't known, but always initializes - * group_ids to a NULL array. + * group_ids to a NULL array. * * @param uid the UID * @param group_ids return location for array of group IDs @@ -377,7 +426,7 @@ _dbus_groups_from_uid (dbus_uid_t uid, } _dbus_assert (info->uid == uid); - + if (info->n_group_ids > 0) { *group_ids = dbus_new (dbus_gid_t, info->n_group_ids); @@ -402,7 +451,7 @@ _dbus_groups_from_uid (dbus_uid_t uid, /** * Unit test for dbus-userdb.c. - * + * * @returns #TRUE on success. */ dbus_bool_t @@ -413,12 +462,13 @@ _dbus_userdb_test (const char *test_data_dir) dbus_uid_t uid; unsigned long *group_ids; int n_group_ids, i; + DBusError error; if (!_dbus_username_from_current_process (&username)) _dbus_assert_not_reached ("didn't get username"); if (!_dbus_homedir_from_current_process (&homedir)) - _dbus_assert_not_reached ("didn't get homedir"); + _dbus_assert_not_reached ("didn't get homedir"); if (!_dbus_get_user_id (username, &uid)) _dbus_assert_not_reached ("didn't get uid"); @@ -434,7 +484,13 @@ _dbus_userdb_test (const char *test_data_dir) printf(" %ld", group_ids[i]); printf ("\n"); - + + dbus_error_init(&error); + printf ("Is Console user: %i\n", + _dbus_is_console_user(uid, &error)); + printf ("Invocation was OK: %s\n", error.message ? error.message : "yes"); + dbus_error_free(&error); + dbus_free (group_ids); return TRUE; |