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 if (!_dbus_user_database_lock_system ())
109 _DBUS_SET_OOM (error);
113 db = _dbus_user_database_get_system ();
116 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get system database.");
117 _dbus_user_database_unlock_system ();
121 /* TPTD: this should be cache-safe, we've locked the DB and
122 _dbus_user_at_console doesn't pass it on. */
123 info = _dbus_user_database_lookup (db, uid, NULL, error);
127 _dbus_user_database_unlock_system ();
131 result = _dbus_user_at_console (info->username, error);
133 _dbus_user_database_unlock_system ();
139 * Gets user ID given username
141 * @param username the username
142 * @param uid return location for UID
143 * @returns #TRUE if username existed and we got the UID
146 _dbus_get_user_id (const DBusString *username,
149 return _dbus_get_user_id_and_primary_group (username, uid, NULL);
153 * Gets group ID given groupname
155 * @param groupname the groupname
156 * @param gid return location for GID
157 * @returns #TRUE if group name existed and we got the GID
160 _dbus_get_group_id (const DBusString *groupname,
163 DBusUserDatabase *db;
164 const DBusGroupInfo *info;
166 /* FIXME: this can't distinguish ENOMEM from other errors */
167 if (!_dbus_user_database_lock_system ())
170 db = _dbus_user_database_get_system ();
173 _dbus_user_database_unlock_system ();
177 if (!_dbus_user_database_get_groupname (db, groupname,
180 _dbus_user_database_unlock_system ();
186 _dbus_user_database_unlock_system ();
191 * Gets user ID and primary group given username
193 * @param username the username
194 * @param uid_p return location for UID
195 * @param gid_p return location for GID
196 * @returns #TRUE if username existed and we got the UID and GID
199 _dbus_get_user_id_and_primary_group (const DBusString *username,
203 DBusUserDatabase *db;
204 const DBusUserInfo *info;
206 /* FIXME: this can't distinguish ENOMEM from other errors */
207 if (!_dbus_user_database_lock_system ())
210 db = _dbus_user_database_get_system ();
213 _dbus_user_database_unlock_system ();
217 if (!_dbus_user_database_get_username (db, username,
220 _dbus_user_database_unlock_system ();
227 *gid_p = info->primary_gid;
229 _dbus_user_database_unlock_system ();
234 * Looks up a gid or group name in the user database. Only one of
235 * name or GID can be provided. There are wrapper functions for this
236 * that are better to use, this one does no locking or anything on the
237 * database and otherwise sort of sucks.
239 * @param db the database
240 * @param gid the group ID or #DBUS_GID_UNSET
241 * @param groupname group name or #NULL
242 * @param error error to fill in
243 * @returns the entry in the database
246 _dbus_user_database_lookup_group (DBusUserDatabase *db,
248 const DBusString *groupname,
253 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
255 /* See if the group is really a number */
256 if (gid == DBUS_UID_UNSET)
260 if (_dbus_is_a_number (groupname, &n))
264 if (gid != DBUS_GID_UNSET)
265 info = _dbus_hash_table_lookup_uintptr (db->groups, gid);
267 info = _dbus_hash_table_lookup_string (db->groups_by_name,
268 _dbus_string_get_const_data (groupname));
271 _dbus_verbose ("Using cache for GID "DBUS_GID_FORMAT" information\n",
277 if (gid != DBUS_GID_UNSET)
278 _dbus_verbose ("No cache for GID "DBUS_GID_FORMAT"\n",
281 _dbus_verbose ("No cache for groupname \"%s\"\n",
282 _dbus_string_get_const_data (groupname));
284 info = dbus_new0 (DBusGroupInfo, 1);
287 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
291 if (gid != DBUS_GID_UNSET)
293 if (!_dbus_group_info_fill_gid (info, gid, error))
295 _DBUS_ASSERT_ERROR_IS_SET (error);
296 _dbus_group_info_free_allocated (info);
302 if (!_dbus_group_info_fill (info, groupname, error))
304 _DBUS_ASSERT_ERROR_IS_SET (error);
305 _dbus_group_info_free_allocated (info);
310 /* don't use these past here */
311 gid = DBUS_GID_UNSET;
314 if (!_dbus_hash_table_insert_uintptr (db->groups, info->gid, info))
316 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
317 _dbus_group_info_free_allocated (info);
322 if (!_dbus_hash_table_insert_string (db->groups_by_name,
326 _dbus_hash_table_remove_uintptr (db->groups, info->gid);
327 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
337 * Gets the user information for the given group name,
338 * returned group info should not be freed.
340 * @param db user database
341 * @param groupname the group name
342 * @param info return location for const ref to group info
343 * @param error error location
344 * @returns #FALSE if error is set
347 _dbus_user_database_get_groupname (DBusUserDatabase *db,
348 const DBusString *groupname,
349 const DBusGroupInfo **info,
352 *info = _dbus_user_database_lookup_group (db, DBUS_GID_UNSET, groupname, error);
353 return *info != NULL;
357 * Gets the user information for the given GID,
358 * returned group info should not be freed.
360 * @param db user database
361 * @param gid the group ID
362 * @param info return location for const ref to group info
363 * @param error error location
364 * @returns #FALSE if error is set
367 _dbus_user_database_get_gid (DBusUserDatabase *db,
369 const DBusGroupInfo **info,
372 *info = _dbus_user_database_lookup_group (db, gid, NULL, error);
373 return *info != NULL;
378 * Gets all groups corresponding to the given UID. Returns #FALSE
379 * if no memory, or user isn't known, but always initializes
380 * group_ids to a NULL array.
383 * @param group_ids return location for array of group IDs
384 * @param n_group_ids return location for length of returned array
385 * @returns #TRUE if the UID existed and we got some credentials
388 _dbus_groups_from_uid (dbus_uid_t uid,
389 dbus_gid_t **group_ids,
392 DBusUserDatabase *db;
393 const DBusUserInfo *info;
397 /* FIXME: this can't distinguish ENOMEM from other errors */
398 if (!_dbus_user_database_lock_system ())
401 db = _dbus_user_database_get_system ();
404 _dbus_user_database_unlock_system ();
408 if (!_dbus_user_database_get_uid (db, uid,
411 _dbus_user_database_unlock_system ();
415 _dbus_assert (info->uid == uid);
417 if (info->n_group_ids > 0)
419 *group_ids = dbus_new (dbus_gid_t, info->n_group_ids);
420 if (*group_ids == NULL)
422 _dbus_user_database_unlock_system ();
426 *n_group_ids = info->n_group_ids;
428 memcpy (*group_ids, info->group_ids, info->n_group_ids * sizeof (dbus_gid_t));
431 _dbus_user_database_unlock_system ();
436 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
440 * Unit test for dbus-userdb.c.
442 * @returns #TRUE on success.
445 _dbus_userdb_test (const char *test_data_dir)
447 const DBusString *username;
448 const DBusString *homedir;
450 unsigned long *group_ids;
454 if (!_dbus_username_from_current_process (&username))
455 _dbus_assert_not_reached ("didn't get username");
457 if (!_dbus_homedir_from_current_process (&homedir))
458 _dbus_assert_not_reached ("didn't get homedir");
460 if (!_dbus_get_user_id (username, &uid))
461 _dbus_assert_not_reached ("didn't get uid");
463 if (!_dbus_groups_from_uid (uid, &group_ids, &n_group_ids))
464 _dbus_assert_not_reached ("didn't get groups");
466 printf (" Current user: %s homedir: %s gids:",
467 _dbus_string_get_const_data (username),
468 _dbus_string_get_const_data (homedir));
470 for (i=0; i<n_group_ids; i++)
471 printf(" %ld", group_ids[i]);
475 dbus_error_init (&error);
476 printf ("Is Console user: %i\n",
477 _dbus_is_console_user (uid, &error));
478 printf ("Invocation was OK: %s\n", error.message ? error.message : "yes");
479 dbus_error_free (&error);
480 printf ("Is Console user 4711: %i\n",
481 _dbus_is_console_user (4711, &error));
482 printf ("Invocation was OK: %s\n", error.message ? error.message : "yes");
483 dbus_error_free (&error);
485 dbus_free (group_ids);
489 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */