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
25 #define DBUS_USERDB_INCLUDES_PRIVATE 1
26 #include "dbus-userdb.h"
27 #include "dbus-test.h"
28 #include "dbus-internals.h"
29 #include "dbus-protocol.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;
58 /* check if we have logind */
59 if (access ("/run/systemd/seats/", F_OK) >= 0)
63 /* Check whether this user is logged in on at least one physical
65 r = sd_uid_get_seats (uid, 0, NULL);
68 dbus_set_error (error, _dbus_error_from_errno (-r),
69 "Failed to determine seats of user \"" DBUS_UID_FORMAT "\": %s",
79 #ifdef HAVE_CONSOLE_OWNER_FILE
84 if (!_dbus_string_init (&f))
86 _DBUS_SET_OOM (error);
90 if (!_dbus_string_append(&f, DBUS_CONSOLE_OWNER_FILE))
92 _dbus_string_free(&f);
93 _DBUS_SET_OOM (error);
97 if (_dbus_stat(&f, &st, NULL) && (st.uid == uid))
99 _dbus_string_free(&f);
103 _dbus_string_free(&f);
105 #endif /* HAVE_CONSOLE_OWNER_FILE */
107 _dbus_user_database_lock_system ();
109 db = _dbus_user_database_get_system ();
112 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get system database.");
113 _dbus_user_database_unlock_system ();
117 /* TPTD: this should be cache-safe, we've locked the DB and
118 _dbus_user_at_console doesn't pass it on. */
119 info = _dbus_user_database_lookup (db, uid, NULL, error);
123 _dbus_user_database_unlock_system ();
127 result = _dbus_user_at_console (info->username, error);
129 _dbus_user_database_unlock_system ();
135 * Gets user ID given username
137 * @param username the username
138 * @param uid return location for UID
139 * @returns #TRUE if username existed and we got the UID
142 _dbus_get_user_id (const DBusString *username,
145 return _dbus_get_user_id_and_primary_group (username, uid, NULL);
149 * Gets group ID given groupname
151 * @param groupname the groupname
152 * @param gid return location for GID
153 * @returns #TRUE if group name existed and we got the GID
156 _dbus_get_group_id (const DBusString *groupname,
159 DBusUserDatabase *db;
160 const DBusGroupInfo *info;
161 _dbus_user_database_lock_system ();
163 db = _dbus_user_database_get_system ();
166 _dbus_user_database_unlock_system ();
170 if (!_dbus_user_database_get_groupname (db, groupname,
173 _dbus_user_database_unlock_system ();
179 _dbus_user_database_unlock_system ();
184 * Gets user ID and primary group given username
186 * @param username the username
187 * @param uid_p return location for UID
188 * @param gid_p return location for GID
189 * @returns #TRUE if username existed and we got the UID and GID
192 _dbus_get_user_id_and_primary_group (const DBusString *username,
196 DBusUserDatabase *db;
197 const DBusUserInfo *info;
198 _dbus_user_database_lock_system ();
200 db = _dbus_user_database_get_system ();
203 _dbus_user_database_unlock_system ();
207 if (!_dbus_user_database_get_username (db, username,
210 _dbus_user_database_unlock_system ();
217 *gid_p = info->primary_gid;
219 _dbus_user_database_unlock_system ();
224 * Looks up a gid or group name in the user database. Only one of
225 * name or GID can be provided. There are wrapper functions for this
226 * that are better to use, this one does no locking or anything on the
227 * database and otherwise sort of sucks.
229 * @param db the database
230 * @param gid the group ID or #DBUS_GID_UNSET
231 * @param groupname group name or #NULL
232 * @param error error to fill in
233 * @returns the entry in the database
236 _dbus_user_database_lookup_group (DBusUserDatabase *db,
238 const DBusString *groupname,
243 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
245 /* See if the group is really a number */
246 if (gid == DBUS_UID_UNSET)
250 if (_dbus_is_a_number (groupname, &n))
254 #ifdef DBUS_ENABLE_USERDB_CACHE
255 if (gid != DBUS_GID_UNSET)
256 info = _dbus_hash_table_lookup_uintptr (db->groups, gid);
258 info = _dbus_hash_table_lookup_string (db->groups_by_name,
259 _dbus_string_get_const_data (groupname));
262 _dbus_verbose ("Using cache for GID "DBUS_GID_FORMAT" information\n",
271 if (gid != DBUS_GID_UNSET)
272 _dbus_verbose ("No cache for GID "DBUS_GID_FORMAT"\n",
275 _dbus_verbose ("No cache for groupname \"%s\"\n",
276 _dbus_string_get_const_data (groupname));
278 info = dbus_new0 (DBusGroupInfo, 1);
281 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
285 if (gid != DBUS_GID_UNSET)
287 if (!_dbus_group_info_fill_gid (info, gid, error))
289 _DBUS_ASSERT_ERROR_IS_SET (error);
290 _dbus_group_info_free_allocated (info);
296 if (!_dbus_group_info_fill (info, groupname, error))
298 _DBUS_ASSERT_ERROR_IS_SET (error);
299 _dbus_group_info_free_allocated (info);
304 /* don't use these past here */
305 gid = DBUS_GID_UNSET;
308 if (!_dbus_hash_table_insert_uintptr (db->groups, info->gid, info))
310 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
311 _dbus_group_info_free_allocated (info);
316 if (!_dbus_hash_table_insert_string (db->groups_by_name,
320 _dbus_hash_table_remove_uintptr (db->groups, info->gid);
321 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
331 * Gets the user information for the given group name,
332 * returned group info should not be freed.
334 * @param db user database
335 * @param groupname the group name
336 * @param info return location for const ref to group info
337 * @param error error location
338 * @returns #FALSE if error is set
341 _dbus_user_database_get_groupname (DBusUserDatabase *db,
342 const DBusString *groupname,
343 const DBusGroupInfo **info,
346 *info = _dbus_user_database_lookup_group (db, DBUS_GID_UNSET, groupname, error);
347 return *info != NULL;
351 * Gets the user information for the given GID,
352 * returned group info should not be freed.
354 * @param db user database
355 * @param gid the group ID
356 * @param info return location for const ref to group info
357 * @param error error location
358 * @returns #FALSE if error is set
361 _dbus_user_database_get_gid (DBusUserDatabase *db,
363 const DBusGroupInfo **info,
366 *info = _dbus_user_database_lookup_group (db, gid, NULL, error);
367 return *info != NULL;
372 * Gets all groups corresponding to the given UID. Returns #FALSE
373 * if no memory, or user isn't known, but always initializes
374 * group_ids to a NULL array.
377 * @param group_ids return location for array of group IDs
378 * @param n_group_ids return location for length of returned array
379 * @returns #TRUE if the UID existed and we got some credentials
382 _dbus_groups_from_uid (dbus_uid_t uid,
383 dbus_gid_t **group_ids,
386 DBusUserDatabase *db;
387 const DBusUserInfo *info;
391 _dbus_user_database_lock_system ();
393 db = _dbus_user_database_get_system ();
396 _dbus_user_database_unlock_system ();
400 if (!_dbus_user_database_get_uid (db, uid,
403 _dbus_user_database_unlock_system ();
407 _dbus_assert (info->uid == uid);
409 if (info->n_group_ids > 0)
411 *group_ids = dbus_new (dbus_gid_t, info->n_group_ids);
412 if (*group_ids == NULL)
414 _dbus_user_database_unlock_system ();
418 *n_group_ids = info->n_group_ids;
420 memcpy (*group_ids, info->group_ids, info->n_group_ids * sizeof (dbus_gid_t));
423 _dbus_user_database_unlock_system ();
428 #ifdef DBUS_BUILD_TESTS
432 * Unit test for dbus-userdb.c.
434 * @returns #TRUE on success.
437 _dbus_userdb_test (const char *test_data_dir)
439 const DBusString *username;
440 const DBusString *homedir;
442 unsigned long *group_ids;
446 if (!_dbus_username_from_current_process (&username))
447 _dbus_assert_not_reached ("didn't get username");
449 if (!_dbus_homedir_from_current_process (&homedir))
450 _dbus_assert_not_reached ("didn't get homedir");
452 if (!_dbus_get_user_id (username, &uid))
453 _dbus_assert_not_reached ("didn't get uid");
455 if (!_dbus_groups_from_uid (uid, &group_ids, &n_group_ids))
456 _dbus_assert_not_reached ("didn't get groups");
458 printf (" Current user: %s homedir: %s gids:",
459 _dbus_string_get_const_data (username),
460 _dbus_string_get_const_data (homedir));
462 for (i=0; i<n_group_ids; i++)
463 printf(" %ld", group_ids[i]);
467 dbus_error_init (&error);
468 printf ("Is Console user: %i\n",
469 _dbus_is_console_user (uid, &error));
470 printf ("Invocation was OK: %s\n", error.message ? error.message : "yes");
471 dbus_error_free (&error);
472 printf ("Is Console user 4711: %i\n",
473 _dbus_is_console_user (4711, &error));
474 printf ("Invocation was OK: %s\n", error.message ? error.message : "yes");
475 dbus_error_free (&error);
477 dbus_free (group_ids);
481 #endif /* DBUS_BUILD_TESTS */