1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-userdb.c User database abstraction
4 * Copyright (C) 2003, 2004 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "dbus-userdb.h"
24 #include "dbus-hash.h"
25 #include "dbus-test.h"
26 #include "dbus-internals.h"
27 #include "dbus-protocol.h"
31 * Internals of DBusUserDatabase
33 struct DBusUserDatabase
35 int refcount; /**< Reference count */
37 DBusHashTable *users; /**< Users in the database by UID */
38 DBusHashTable *groups; /**< Groups in the database by GID */
39 DBusHashTable *users_by_name; /**< Users in the database by name */
40 DBusHashTable *groups_by_name; /**< Groups in the database by name */
45 free_user_info (void *data)
47 DBusUserInfo *info = data;
49 if (info == NULL) /* hash table will pass NULL */
52 _dbus_user_info_free (info);
57 free_group_info (void *data)
59 DBusGroupInfo *info = data;
61 if (info == NULL) /* hash table will pass NULL */
64 _dbus_group_info_free (info);
69 _dbus_user_database_lookup (DBusUserDatabase *db,
71 const DBusString *username,
76 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
77 _dbus_assert (uid != DBUS_UID_UNSET || username != NULL);
79 if (uid != DBUS_UID_UNSET)
80 info = _dbus_hash_table_lookup_ulong (db->users, uid);
82 info = _dbus_hash_table_lookup_string (db->users_by_name, _dbus_string_get_const_data (username));
86 _dbus_verbose ("Using cache for UID "DBUS_UID_FORMAT" information\n",
92 _dbus_verbose ("No cache for UID "DBUS_UID_FORMAT"\n",
95 info = dbus_new0 (DBusUserInfo, 1);
98 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
102 if (uid != DBUS_UID_UNSET)
104 if (!_dbus_user_info_fill_uid (info, uid, error))
106 _DBUS_ASSERT_ERROR_IS_SET (error);
107 free_user_info (info);
113 if (!_dbus_user_info_fill (info, username, error))
115 _DBUS_ASSERT_ERROR_IS_SET (error);
116 free_user_info (info);
121 /* be sure we don't use these after here */
122 uid = DBUS_UID_UNSET;
125 /* insert into hash */
126 if (!_dbus_hash_table_insert_ulong (db->users, info->uid, info))
128 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
129 free_user_info (info);
133 if (!_dbus_hash_table_insert_string (db->users_by_name,
137 _dbus_hash_table_remove_ulong (db->users, info->uid);
138 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
146 static DBusGroupInfo*
147 _dbus_user_database_lookup_group (DBusUserDatabase *db,
149 const DBusString *groupname,
154 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
156 if (gid != DBUS_GID_UNSET)
157 info = _dbus_hash_table_lookup_ulong (db->groups, gid);
159 info = _dbus_hash_table_lookup_string (db->groups_by_name,
160 _dbus_string_get_const_data (groupname));
163 _dbus_verbose ("Using cache for GID "DBUS_GID_FORMAT" information\n",
169 _dbus_verbose ("No cache for GID "DBUS_GID_FORMAT"\n",
172 info = dbus_new0 (DBusGroupInfo, 1);
175 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
179 if (!_dbus_group_info_fill_gid (info, gid, error))
181 _DBUS_ASSERT_ERROR_IS_SET (error);
182 free_group_info (info);
186 if (!_dbus_hash_table_insert_ulong (db->groups, info->gid, info))
188 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
189 free_group_info (info);
194 if (!_dbus_hash_table_insert_string (db->groups_by_name,
198 _dbus_hash_table_remove_ulong (db->groups, info->gid);
199 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
207 _DBUS_DEFINE_GLOBAL_LOCK(system_users);
208 static dbus_bool_t database_locked = FALSE;
209 static DBusUserDatabase *system_db = NULL;
210 static DBusString process_username;
211 static DBusString process_homedir;
214 shutdown_system_db (void *data)
216 _dbus_user_database_unref (system_db);
218 _dbus_string_free (&process_username);
219 _dbus_string_free (&process_homedir);
223 init_system_db (void)
225 _dbus_assert (database_locked);
227 if (system_db == NULL)
230 const DBusUserInfo *info;
232 system_db = _dbus_user_database_new ();
233 if (system_db == NULL)
236 dbus_error_init (&error);
238 if (!_dbus_user_database_get_uid (system_db,
243 _dbus_user_database_unref (system_db);
246 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
248 dbus_error_free (&error);
253 /* This really should not happen. */
254 _dbus_warn ("Could not get password database information for UID of current process: %s\n",
256 dbus_error_free (&error);
261 if (!_dbus_string_init (&process_username))
263 _dbus_user_database_unref (system_db);
268 if (!_dbus_string_init (&process_homedir))
270 _dbus_string_free (&process_username);
271 _dbus_user_database_unref (system_db);
276 if (!_dbus_string_append (&process_username,
278 !_dbus_string_append (&process_homedir,
280 !_dbus_register_shutdown_func (shutdown_system_db, NULL))
282 _dbus_string_free (&process_username);
283 _dbus_string_free (&process_homedir);
284 _dbus_user_database_unref (system_db);
294 * @addtogroup DBusInternalsUtils
299 * Locks global system user database.
302 _dbus_user_database_lock_system (void)
304 _DBUS_LOCK (system_users);
305 database_locked = TRUE;
309 * Unlocks global system user database.
312 _dbus_user_database_unlock_system (void)
314 database_locked = FALSE;
315 _DBUS_UNLOCK (system_users);
319 * Gets the system global user database;
320 * must be called with lock held (_dbus_user_database_lock_system()).
322 * @returns the database or #NULL if no memory
325 _dbus_user_database_get_system (void)
327 _dbus_assert (database_locked);
335 * Gets username of user owning current process. The returned string
336 * is valid until dbus_shutdown() is called.
338 * @param username place to store pointer to username
339 * @returns #FALSE if no memory
342 _dbus_username_from_current_process (const DBusString **username)
344 _dbus_user_database_lock_system ();
345 if (!init_system_db ())
347 _dbus_user_database_unlock_system ();
350 *username = &process_username;
351 _dbus_user_database_unlock_system ();
357 * Gets homedir of user owning current process. The returned string
358 * is valid until dbus_shutdown() is called.
360 * @param homedir place to store pointer to homedir
361 * @returns #FALSE if no memory
364 _dbus_homedir_from_current_process (const DBusString **homedir)
366 _dbus_user_database_lock_system ();
367 if (!init_system_db ())
369 _dbus_user_database_unlock_system ();
372 *homedir = &process_homedir;
373 _dbus_user_database_unlock_system ();
379 * Gets user ID given username
381 * @param username the username
382 * @param uid return location for UID
383 * @returns #TRUE if username existed and we got the UID
386 _dbus_get_user_id (const DBusString *username,
389 DBusCredentials creds;
391 if (!_dbus_credentials_from_username (username, &creds))
394 if (creds.uid == DBUS_UID_UNSET)
403 * Checks to see if the UID sent in is the console user
405 * @param uid UID of person to check
406 * @param error return location for errors
407 * @returns #TRUE if the UID is the same as the console user and there are no errors
410 _dbus_is_console_user (dbus_uid_t uid,
414 DBusUserDatabase *db;
415 const DBusUserInfo *info;
416 DBusString *console_file;
417 dbus_bool_t result = FALSE;
419 _dbus_user_database_lock_system ();
421 db = _dbus_user_database_get_system ();
424 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get system database.");
425 _dbus_user_database_unlock_system ();
429 info = _dbus_user_database_lookup (db, uid, NULL, error);
433 _dbus_user_database_unlock_system ();
437 result = _dbus_user_at_console (info->username, error);
439 _dbus_user_database_unlock_system ();
445 * Gets group ID given groupname
447 * @param groupname the groupname
448 * @param gid return location for GID
449 * @returns #TRUE if group name existed and we got the GID
452 _dbus_get_group_id (const DBusString *groupname,
455 DBusUserDatabase *db;
456 const DBusGroupInfo *info;
457 _dbus_user_database_lock_system ();
459 db = _dbus_user_database_get_system ();
462 _dbus_user_database_unlock_system ();
466 if (!_dbus_user_database_get_groupname (db, groupname,
469 _dbus_user_database_unlock_system ();
475 _dbus_user_database_unlock_system ();
480 * Gets the home directory for the given user.
482 * @param username the username
483 * @param homedir string to append home directory to
484 * @returns #TRUE if user existed and we appended their homedir
487 _dbus_homedir_from_username (const DBusString *username,
490 DBusUserDatabase *db;
491 const DBusUserInfo *info;
492 _dbus_user_database_lock_system ();
494 db = _dbus_user_database_get_system ();
497 _dbus_user_database_unlock_system ();
501 if (!_dbus_user_database_get_username (db, username,
504 _dbus_user_database_unlock_system ();
508 if (!_dbus_string_append (homedir, info->homedir))
510 _dbus_user_database_unlock_system ();
514 _dbus_user_database_unlock_system ();
519 * Gets a UID from a UID string.
521 * @param uid_str the UID in string form
522 * @param uid UID to fill in
523 * @returns #TRUE if successfully filled in UID
526 _dbus_uid_from_string (const DBusString *uid_str,
532 if (_dbus_string_get_length (uid_str) == 0)
534 _dbus_verbose ("UID string was zero length\n");
540 if (!_dbus_string_parse_int (uid_str, 0, &val,
543 _dbus_verbose ("could not parse string as a UID\n");
547 if (end != _dbus_string_get_length (uid_str))
549 _dbus_verbose ("string contained trailing stuff after UID\n");
559 * Gets the credentials corresponding to the given username.
561 * @param username the username
562 * @param credentials credentials to fill in
563 * @returns #TRUE if the username existed and we got some credentials
566 _dbus_credentials_from_username (const DBusString *username,
567 DBusCredentials *credentials)
569 DBusUserDatabase *db;
570 const DBusUserInfo *info;
571 _dbus_user_database_lock_system ();
573 db = _dbus_user_database_get_system ();
576 _dbus_user_database_unlock_system ();
580 if (!_dbus_user_database_get_username (db, username,
583 _dbus_user_database_unlock_system ();
587 credentials->pid = DBUS_PID_UNSET;
588 credentials->uid = info->uid;
589 credentials->gid = info->primary_gid;
591 _dbus_user_database_unlock_system ();
596 * Gets the credentials corresponding to the given UID.
599 * @param credentials credentials to fill in
600 * @returns #TRUE if the UID existed and we got some credentials
603 _dbus_credentials_from_uid (dbus_uid_t uid,
604 DBusCredentials *credentials)
606 DBusUserDatabase *db;
607 const DBusUserInfo *info;
608 _dbus_user_database_lock_system ();
610 db = _dbus_user_database_get_system ();
613 _dbus_user_database_unlock_system ();
617 if (!_dbus_user_database_get_uid (db, uid,
620 _dbus_user_database_unlock_system ();
624 _dbus_assert (info->uid == uid);
626 credentials->pid = DBUS_PID_UNSET;
627 credentials->uid = info->uid;
628 credentials->gid = info->primary_gid;
630 _dbus_user_database_unlock_system ();
635 * Creates a new user database object used to look up and
636 * cache user information.
637 * @returns new database, or #NULL on out of memory
640 _dbus_user_database_new (void)
642 DBusUserDatabase *db;
644 db = dbus_new0 (DBusUserDatabase, 1);
650 db->users = _dbus_hash_table_new (DBUS_HASH_ULONG,
651 NULL, free_user_info);
653 if (db->users == NULL)
656 db->groups = _dbus_hash_table_new (DBUS_HASH_ULONG,
657 NULL, free_group_info);
659 if (db->groups == NULL)
662 db->users_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
664 if (db->users_by_name == NULL)
667 db->groups_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
669 if (db->groups_by_name == NULL)
675 _dbus_user_database_unref (db);
680 * Increments refcount of user database.
681 * @param db the database
682 * @returns the database
685 _dbus_user_database_ref (DBusUserDatabase *db)
687 _dbus_assert (db->refcount > 0);
695 * Decrements refcount of user database.
696 * @param db the database
699 _dbus_user_database_unref (DBusUserDatabase *db)
701 _dbus_assert (db->refcount > 0);
704 if (db->refcount == 0)
707 _dbus_hash_table_unref (db->users);
710 _dbus_hash_table_unref (db->groups);
712 if (db->users_by_name)
713 _dbus_hash_table_unref (db->users_by_name);
715 if (db->groups_by_name)
716 _dbus_hash_table_unref (db->groups_by_name);
723 * Gets all groups for a particular user. Returns #FALSE
724 * if no memory, or user isn't known, but always initializes
725 * group_ids to a NULL array. Sets error to the reason
726 * for returning #FALSE.
728 * @param db the user database object
729 * @param uid the user ID
730 * @param group_ids return location for array of group IDs
731 * @param n_group_ids return location for length of returned array
732 * @param error return location for error
733 * @returns #TRUE on success
736 _dbus_user_database_get_groups (DBusUserDatabase *db,
738 dbus_gid_t **group_ids,
744 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
749 info = _dbus_user_database_lookup (db, uid, NULL, error);
752 _DBUS_ASSERT_ERROR_IS_SET (error);
756 if (info->n_group_ids > 0)
758 *group_ids = dbus_new (dbus_gid_t, info->n_group_ids);
759 if (*group_ids == NULL)
761 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
765 *n_group_ids = info->n_group_ids;
767 memcpy (*group_ids, info->group_ids, info->n_group_ids * sizeof (dbus_gid_t));
774 * Gets the user information for the given UID,
775 * returned user info should not be freed.
777 * @param db user database
778 * @param uid the user ID
779 * @param info return location for const ref to user info
780 * @param error error location
781 * @returns #FALSE if error is set
784 _dbus_user_database_get_uid (DBusUserDatabase *db,
786 const DBusUserInfo **info,
789 *info = _dbus_user_database_lookup (db, uid, NULL, error);
790 return *info != NULL;
794 * Gets the user information for the given GID,
795 * returned group info should not be freed.
797 * @param db user database
798 * @param gid the group ID
799 * @param info return location for const ref to group info
800 * @param error error location
801 * @returns #FALSE if error is set
804 _dbus_user_database_get_gid (DBusUserDatabase *db,
806 const DBusGroupInfo **info,
809 *info = _dbus_user_database_lookup_group (db, gid, NULL, error);
810 return *info != NULL;
814 * Gets the user information for the given username.
816 * @param db user database
817 * @param username the user name
818 * @param info return location for const ref to user info
819 * @param error error location
820 * @returns #FALSE if error is set
823 _dbus_user_database_get_username (DBusUserDatabase *db,
824 const DBusString *username,
825 const DBusUserInfo **info,
828 *info = _dbus_user_database_lookup (db, DBUS_UID_UNSET, username, error);
829 return *info != NULL;
833 * Gets the user information for the given group name,
834 * returned group info should not be freed.
836 * @param db user database
837 * @param groupname the group name
838 * @param info return location for const ref to group info
839 * @param error error location
840 * @returns #FALSE if error is set
843 _dbus_user_database_get_groupname (DBusUserDatabase *db,
844 const DBusString *groupname,
845 const DBusGroupInfo **info,
848 *info = _dbus_user_database_lookup_group (db, DBUS_GID_UNSET, groupname, error);
849 return *info != NULL;
854 #ifdef DBUS_BUILD_TESTS
858 * Unit test for dbus-userdb.c.
860 * @returns #TRUE on success.
863 _dbus_userdb_test (const char *test_data_dir)
865 const DBusString *username;
866 const DBusString *homedir;
868 if (!_dbus_username_from_current_process (&username))
869 _dbus_assert_not_reached ("didn't get username");
871 if (!_dbus_homedir_from_current_process (&homedir))
872 _dbus_assert_not_reached ("didn't get homedir");
874 printf (" Current user: %s homedir: %s\n",
875 _dbus_string_get_const_data (username),
876 _dbus_string_get_const_data (homedir));
880 #endif /* DBUS_BUILD_TESTS */