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
41 static DBusGroupInfo *
42 _dbus_group_info_ref (DBusGroupInfo *info)
44 _dbus_assert (info->refcount > 0);
45 _dbus_assert (info->refcount < SIZE_MAX);
51 * Checks to see if the UID sent in is the console user
53 * @param uid UID of person to check
54 * @param error return location for errors
55 * @returns #TRUE if the UID is the same as the console user and there are no errors
58 _dbus_is_console_user (dbus_uid_t uid,
63 const DBusUserInfo *info;
64 dbus_bool_t result = FALSE;
67 /* check if we have logind */
68 if (access ("/run/systemd/seats/", F_OK) >= 0)
72 /* Check whether this user is logged in on at least one physical
74 r = sd_uid_get_seats (uid, 0, NULL);
77 dbus_set_error (error, _dbus_error_from_errno (-r),
78 "Failed to determine seats of user \"" DBUS_UID_FORMAT "\": %s",
88 #ifdef HAVE_CONSOLE_OWNER_FILE
93 if (!_dbus_string_init (&f))
95 _DBUS_SET_OOM (error);
99 if (!_dbus_string_append(&f, DBUS_CONSOLE_OWNER_FILE))
101 _dbus_string_free(&f);
102 _DBUS_SET_OOM (error);
106 if (_dbus_stat(&f, &st, NULL) && (st.uid == uid))
108 _dbus_string_free(&f);
112 _dbus_string_free(&f);
114 #endif /* HAVE_CONSOLE_OWNER_FILE */
116 if (!_dbus_user_database_lock_system ())
118 _DBUS_SET_OOM (error);
122 db = _dbus_user_database_get_system ();
125 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get system database.");
126 _dbus_user_database_unlock_system ();
130 /* TPTD: this should be cache-safe, we've locked the DB and
131 _dbus_user_at_console doesn't pass it on. */
132 info = _dbus_user_database_lookup (db, uid, NULL, error);
136 _dbus_user_database_unlock_system ();
140 result = _dbus_user_at_console (info->username, error);
142 _dbus_user_database_unlock_system ();
148 * Gets user ID given username
150 * @param username the username
151 * @param uid return location for UID
152 * @returns #TRUE if username existed and we got the UID
155 _dbus_get_user_id (const DBusString *username,
158 return _dbus_get_user_id_and_primary_group (username, uid, NULL);
162 * Gets group ID given groupname
164 * @param groupname the groupname
165 * @param gid return location for GID
166 * @returns #TRUE if group name existed and we got the GID
169 _dbus_get_group_id (const DBusString *groupname,
172 DBusUserDatabase *db;
173 const DBusGroupInfo *info;
175 /* FIXME: this can't distinguish ENOMEM from other errors */
176 if (!_dbus_user_database_lock_system ())
179 db = _dbus_user_database_get_system ();
182 _dbus_user_database_unlock_system ();
186 if (!_dbus_user_database_get_groupname (db, groupname,
189 _dbus_user_database_unlock_system ();
195 _dbus_user_database_unlock_system ();
200 * Gets user ID and primary group given username
202 * @param username the username
203 * @param uid_p return location for UID
204 * @param gid_p return location for GID
205 * @returns #TRUE if username existed and we got the UID and GID
208 _dbus_get_user_id_and_primary_group (const DBusString *username,
212 DBusUserDatabase *db;
213 const DBusUserInfo *info;
215 /* FIXME: this can't distinguish ENOMEM from other errors */
216 if (!_dbus_user_database_lock_system ())
219 db = _dbus_user_database_get_system ();
222 _dbus_user_database_unlock_system ();
226 if (!_dbus_user_database_get_username (db, username,
229 _dbus_user_database_unlock_system ();
236 *gid_p = info->primary_gid;
238 _dbus_user_database_unlock_system ();
243 * Looks up a gid or group name in the user database. Only one of
244 * name or GID can be provided. There are wrapper functions for this
245 * that are better to use, this one does no locking or anything on the
246 * database and otherwise sort of sucks.
248 * @param db the database
249 * @param gid the group ID or #DBUS_GID_UNSET
250 * @param groupname group name or #NULL
251 * @param error error to fill in
252 * @returns the entry in the database (borrowed, do not free)
254 const DBusGroupInfo *
255 _dbus_user_database_lookup_group (DBusUserDatabase *db,
257 const DBusString *groupname,
262 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
264 /* See if the group is really a number */
265 if (gid == DBUS_UID_UNSET)
269 if (_dbus_is_a_number (groupname, &n))
273 if (gid != DBUS_GID_UNSET)
274 info = _dbus_hash_table_lookup_uintptr (db->groups, gid);
276 info = _dbus_hash_table_lookup_string (db->groups_by_name,
277 _dbus_string_get_const_data (groupname));
280 _dbus_verbose ("Using cache for GID "DBUS_GID_FORMAT" information\n",
286 if (gid != DBUS_GID_UNSET)
287 _dbus_verbose ("No cache for GID "DBUS_GID_FORMAT"\n",
290 _dbus_verbose ("No cache for groupname \"%s\"\n",
291 _dbus_string_get_const_data (groupname));
293 info = dbus_new0 (DBusGroupInfo, 1);
296 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
301 if (gid != DBUS_GID_UNSET)
303 if (!_dbus_group_info_fill_gid (info, gid, error))
305 _DBUS_ASSERT_ERROR_IS_SET (error);
306 _dbus_group_info_unref (info);
312 if (!_dbus_group_info_fill (info, groupname, error))
314 _DBUS_ASSERT_ERROR_IS_SET (error);
315 _dbus_group_info_unref (info);
320 /* don't use these past here */
321 gid = DBUS_GID_UNSET;
324 if (_dbus_hash_table_insert_uintptr (db->groups, info->gid, info))
326 _dbus_group_info_ref (info);
330 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
331 _dbus_group_info_unref (info);
336 if (_dbus_hash_table_insert_string (db->groups_by_name,
340 _dbus_group_info_ref (info);
344 _dbus_hash_table_remove_uintptr (db->groups, info->gid);
345 _dbus_group_info_unref (info);
346 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
350 /* Release the original reference */
351 _dbus_group_info_unref (info);
353 /* Return a borrowed reference to the DBusGroupInfo owned by the
361 * Gets the user information for the given group name,
362 * returned group info should not be freed.
364 * @param db user database
365 * @param groupname the group name
366 * @param info return location for const ref to group info
367 * @param error error location
368 * @returns #FALSE if error is set
371 _dbus_user_database_get_groupname (DBusUserDatabase *db,
372 const DBusString *groupname,
373 const DBusGroupInfo **info,
376 *info = _dbus_user_database_lookup_group (db, DBUS_GID_UNSET, groupname, error);
377 return *info != NULL;
381 * Gets the user information for the given GID,
382 * returned group info should not be freed.
384 * @param db user database
385 * @param gid the group ID
386 * @param info return location for const ref to group info
387 * @param error error location
388 * @returns #FALSE if error is set
391 _dbus_user_database_get_gid (DBusUserDatabase *db,
393 const DBusGroupInfo **info,
396 *info = _dbus_user_database_lookup_group (db, gid, NULL, error);
397 return *info != NULL;
402 * Gets all groups corresponding to the given UID. Returns #FALSE
403 * if no memory, or user isn't known, but always initializes
404 * group_ids to a NULL array.
407 * @param group_ids return location for array of group IDs
408 * @param n_group_ids return location for length of returned array
409 * @returns #TRUE if the UID existed and we got some credentials
412 _dbus_groups_from_uid (dbus_uid_t uid,
413 dbus_gid_t **group_ids,
416 DBusUserDatabase *db;
417 const DBusUserInfo *info;
421 /* FIXME: this can't distinguish ENOMEM from other errors */
422 if (!_dbus_user_database_lock_system ())
425 db = _dbus_user_database_get_system ();
428 _dbus_user_database_unlock_system ();
432 if (!_dbus_user_database_get_uid (db, uid,
435 _dbus_user_database_unlock_system ();
439 _dbus_assert (info->uid == uid);
441 if (info->n_group_ids > 0)
443 *group_ids = dbus_new (dbus_gid_t, info->n_group_ids);
444 if (*group_ids == NULL)
446 _dbus_user_database_unlock_system ();
450 *n_group_ids = info->n_group_ids;
452 memcpy (*group_ids, info->group_ids, info->n_group_ids * sizeof (dbus_gid_t));
455 _dbus_user_database_unlock_system ();
460 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
464 * Unit test for dbus-userdb.c.
466 * @returns #TRUE on success.
469 _dbus_userdb_test (const char *test_data_dir)
471 const DBusString *username;
472 const DBusString *homedir;
474 unsigned long *group_ids;
478 if (!_dbus_username_from_current_process (&username))
479 _dbus_assert_not_reached ("didn't get username");
481 if (!_dbus_homedir_from_current_process (&homedir))
482 _dbus_assert_not_reached ("didn't get homedir");
484 if (!_dbus_get_user_id (username, &uid))
485 _dbus_assert_not_reached ("didn't get uid");
487 if (!_dbus_groups_from_uid (uid, &group_ids, &n_group_ids))
488 _dbus_assert_not_reached ("didn't get groups");
490 printf (" Current user: %s homedir: %s gids:",
491 _dbus_string_get_const_data (username),
492 _dbus_string_get_const_data (homedir));
494 for (i=0; i<n_group_ids; i++)
495 printf(" %ld", group_ids[i]);
499 dbus_error_init (&error);
500 printf ("Is Console user: %i\n",
501 _dbus_is_console_user (uid, &error));
502 printf ("Invocation was OK: %s\n", error.message ? error.message : "yes");
503 dbus_error_free (&error);
504 printf ("Is Console user 4711: %i\n",
505 _dbus_is_console_user (4711, &error));
506 printf ("Invocation was OK: %s\n", error.message ? error.message : "yes");
507 dbus_error_free (&error);
509 dbus_free (group_ids);
513 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */