1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-userdb-util.c Would be in dbus-userdb.c, but not used in libdbus
4 * Copyright (C) 2003, 2004, 2005 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #define DBUS_USERDB_INCLUDES_PRIVATE 1
25 #include "dbus-userdb.h"
26 #include "dbus-test.h"
27 #include "dbus-internals.h"
28 #include "dbus-protocol.h"
32 #include <systemd/sd-daemon.h>
33 #include <systemd/sd-login.h>
37 * @addtogroup DBusInternalsUtils
42 * Checks to see if the UID sent in is the console user
44 * @param uid UID of person to check
45 * @param error return location for errors
46 * @returns #TRUE if the UID is the same as the console user and there are no errors
49 _dbus_is_console_user (dbus_uid_t uid,
54 const DBusUserInfo *info;
55 dbus_bool_t result = FALSE;
62 /* Check whether this user is logged in on at least one physical
64 r = sd_uid_get_seats (uid, 0, NULL);
67 dbus_set_error (error, _dbus_error_from_errno (-r),
68 "Failed to determine seats of user \"" DBUS_UID_FORMAT "\": %s",
78 #ifdef HAVE_CONSOLE_OWNER_FILE
83 if (!_dbus_string_init (&f))
85 _DBUS_SET_OOM (error);
89 if (!_dbus_string_append(&f, DBUS_CONSOLE_OWNER_FILE))
91 _dbus_string_free(&f);
92 _DBUS_SET_OOM (error);
96 if (_dbus_stat(&f, &st, NULL) && (st.uid == uid))
98 _dbus_string_free(&f);
102 _dbus_string_free(&f);
104 #endif /* HAVE_CONSOLE_OWNER_FILE */
106 _dbus_user_database_lock_system ();
108 db = _dbus_user_database_get_system ();
111 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get system database.");
112 _dbus_user_database_unlock_system ();
116 /* TPTD: this should be cache-safe, we've locked the DB and
117 _dbus_user_at_console doesn't pass it on. */
118 info = _dbus_user_database_lookup (db, uid, NULL, error);
122 _dbus_user_database_unlock_system ();
126 result = _dbus_user_at_console (info->username, error);
128 _dbus_user_database_unlock_system ();
134 * Gets user ID given username
136 * @param username the username
137 * @param uid return location for UID
138 * @returns #TRUE if username existed and we got the UID
141 _dbus_get_user_id (const DBusString *username,
144 return _dbus_get_user_id_and_primary_group (username, uid, NULL);
148 * Gets group ID given groupname
150 * @param groupname the groupname
151 * @param gid return location for GID
152 * @returns #TRUE if group name existed and we got the GID
155 _dbus_get_group_id (const DBusString *groupname,
158 DBusUserDatabase *db;
159 const DBusGroupInfo *info;
160 _dbus_user_database_lock_system ();
162 db = _dbus_user_database_get_system ();
165 _dbus_user_database_unlock_system ();
169 if (!_dbus_user_database_get_groupname (db, groupname,
172 _dbus_user_database_unlock_system ();
178 _dbus_user_database_unlock_system ();
183 * Gets user ID and primary group given username
185 * @param username the username
186 * @param uid_p return location for UID
187 * @param gid_p return location for GID
188 * @returns #TRUE if username existed and we got the UID and GID
191 _dbus_get_user_id_and_primary_group (const DBusString *username,
195 DBusUserDatabase *db;
196 const DBusUserInfo *info;
197 _dbus_user_database_lock_system ();
199 db = _dbus_user_database_get_system ();
202 _dbus_user_database_unlock_system ();
206 if (!_dbus_user_database_get_username (db, username,
209 _dbus_user_database_unlock_system ();
216 *gid_p = info->primary_gid;
218 _dbus_user_database_unlock_system ();
223 * Looks up a gid or group name in the user database. Only one of
224 * name or GID can be provided. There are wrapper functions for this
225 * that are better to use, this one does no locking or anything on the
226 * database and otherwise sort of sucks.
228 * @param db the database
229 * @param gid the group ID or #DBUS_GID_UNSET
230 * @param groupname group name or #NULL
231 * @param error error to fill in
232 * @returns the entry in the database
235 _dbus_user_database_lookup_group (DBusUserDatabase *db,
237 const DBusString *groupname,
242 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
244 /* See if the group is really a number */
245 if (gid == DBUS_UID_UNSET)
249 if (_dbus_is_a_number (groupname, &n))
253 #ifdef DBUS_ENABLE_USERDB_CACHE
254 if (gid != DBUS_GID_UNSET)
255 info = _dbus_hash_table_lookup_uintptr (db->groups, gid);
257 info = _dbus_hash_table_lookup_string (db->groups_by_name,
258 _dbus_string_get_const_data (groupname));
261 _dbus_verbose ("Using cache for GID "DBUS_GID_FORMAT" information\n",
270 if (gid != DBUS_GID_UNSET)
271 _dbus_verbose ("No cache for GID "DBUS_GID_FORMAT"\n",
274 _dbus_verbose ("No cache for groupname \"%s\"\n",
275 _dbus_string_get_const_data (groupname));
277 info = dbus_new0 (DBusGroupInfo, 1);
280 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
284 if (gid != DBUS_GID_UNSET)
286 if (!_dbus_group_info_fill_gid (info, gid, error))
288 _DBUS_ASSERT_ERROR_IS_SET (error);
289 _dbus_group_info_free_allocated (info);
295 if (!_dbus_group_info_fill (info, groupname, error))
297 _DBUS_ASSERT_ERROR_IS_SET (error);
298 _dbus_group_info_free_allocated (info);
303 /* don't use these past here */
304 gid = DBUS_GID_UNSET;
307 if (!_dbus_hash_table_insert_uintptr (db->groups, info->gid, info))
309 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
310 _dbus_group_info_free_allocated (info);
315 if (!_dbus_hash_table_insert_string (db->groups_by_name,
319 _dbus_hash_table_remove_uintptr (db->groups, info->gid);
320 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
330 * Gets the user information for the given group name,
331 * returned group info should not be freed.
333 * @param db user database
334 * @param groupname the group name
335 * @param info return location for const ref to group info
336 * @param error error location
337 * @returns #FALSE if error is set
340 _dbus_user_database_get_groupname (DBusUserDatabase *db,
341 const DBusString *groupname,
342 const DBusGroupInfo **info,
345 *info = _dbus_user_database_lookup_group (db, DBUS_GID_UNSET, groupname, error);
346 return *info != NULL;
350 * Gets the user information for the given GID,
351 * returned group info should not be freed.
353 * @param db user database
354 * @param gid the group ID
355 * @param info return location for const ref to group info
356 * @param error error location
357 * @returns #FALSE if error is set
360 _dbus_user_database_get_gid (DBusUserDatabase *db,
362 const DBusGroupInfo **info,
365 *info = _dbus_user_database_lookup_group (db, gid, NULL, error);
366 return *info != NULL;
371 * Gets all groups corresponding to the given UID. Returns #FALSE
372 * if no memory, or user isn't known, but always initializes
373 * group_ids to a NULL array.
376 * @param group_ids return location for array of group IDs
377 * @param n_group_ids return location for length of returned array
378 * @returns #TRUE if the UID existed and we got some credentials
381 _dbus_groups_from_uid (dbus_uid_t uid,
382 dbus_gid_t **group_ids,
385 DBusUserDatabase *db;
386 const DBusUserInfo *info;
390 _dbus_user_database_lock_system ();
392 db = _dbus_user_database_get_system ();
395 _dbus_user_database_unlock_system ();
399 if (!_dbus_user_database_get_uid (db, uid,
402 _dbus_user_database_unlock_system ();
406 _dbus_assert (info->uid == uid);
408 if (info->n_group_ids > 0)
410 *group_ids = dbus_new (dbus_gid_t, info->n_group_ids);
411 if (*group_ids == NULL)
413 _dbus_user_database_unlock_system ();
417 *n_group_ids = info->n_group_ids;
419 memcpy (*group_ids, info->group_ids, info->n_group_ids * sizeof (dbus_gid_t));
422 _dbus_user_database_unlock_system ();
427 #ifdef DBUS_BUILD_TESTS
431 * Unit test for dbus-userdb.c.
433 * @returns #TRUE on success.
436 _dbus_userdb_test (const char *test_data_dir)
438 const DBusString *username;
439 const DBusString *homedir;
441 unsigned long *group_ids;
445 if (!_dbus_username_from_current_process (&username))
446 _dbus_assert_not_reached ("didn't get username");
448 if (!_dbus_homedir_from_current_process (&homedir))
449 _dbus_assert_not_reached ("didn't get homedir");
451 if (!_dbus_get_user_id (username, &uid))
452 _dbus_assert_not_reached ("didn't get uid");
454 if (!_dbus_groups_from_uid (uid, &group_ids, &n_group_ids))
455 _dbus_assert_not_reached ("didn't get groups");
457 printf (" Current user: %s homedir: %s gids:",
458 _dbus_string_get_const_data (username),
459 _dbus_string_get_const_data (homedir));
461 for (i=0; i<n_group_ids; i++)
462 printf(" %ld", group_ids[i]);
466 dbus_error_init (&error);
467 printf ("Is Console user: %i\n",
468 _dbus_is_console_user (uid, &error));
469 printf ("Invocation was OK: %s\n", error.message ? error.message : "yes");
470 dbus_error_free (&error);
471 printf ("Is Console user 4711: %i\n",
472 _dbus_is_console_user (4711, &error));
473 printf ("Invocation was OK: %s\n", error.message ? error.message : "yes");
474 dbus_error_free (&error);
476 dbus_free (group_ids);
480 #endif /* DBUS_BUILD_TESTS */