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 dbus_bool_t result = FALSE;
418 _dbus_user_database_lock_system ();
420 db = _dbus_user_database_get_system ();
423 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get system database.");
424 _dbus_user_database_unlock_system ();
428 info = _dbus_user_database_lookup (db, uid, NULL, error);
432 _dbus_user_database_unlock_system ();
436 result = _dbus_user_at_console (info->username, error);
438 _dbus_user_database_unlock_system ();
444 * Gets group ID given groupname
446 * @param groupname the groupname
447 * @param gid return location for GID
448 * @returns #TRUE if group name existed and we got the GID
451 _dbus_get_group_id (const DBusString *groupname,
454 DBusUserDatabase *db;
455 const DBusGroupInfo *info;
456 _dbus_user_database_lock_system ();
458 db = _dbus_user_database_get_system ();
461 _dbus_user_database_unlock_system ();
465 if (!_dbus_user_database_get_groupname (db, groupname,
468 _dbus_user_database_unlock_system ();
474 _dbus_user_database_unlock_system ();
479 * Gets the home directory for the given user.
481 * @param username the username
482 * @param homedir string to append home directory to
483 * @returns #TRUE if user existed and we appended their homedir
486 _dbus_homedir_from_username (const DBusString *username,
489 DBusUserDatabase *db;
490 const DBusUserInfo *info;
491 _dbus_user_database_lock_system ();
493 db = _dbus_user_database_get_system ();
496 _dbus_user_database_unlock_system ();
500 if (!_dbus_user_database_get_username (db, username,
503 _dbus_user_database_unlock_system ();
507 if (!_dbus_string_append (homedir, info->homedir))
509 _dbus_user_database_unlock_system ();
513 _dbus_user_database_unlock_system ();
518 * Gets a UID from a UID string.
520 * @param uid_str the UID in string form
521 * @param uid UID to fill in
522 * @returns #TRUE if successfully filled in UID
525 _dbus_uid_from_string (const DBusString *uid_str,
531 if (_dbus_string_get_length (uid_str) == 0)
533 _dbus_verbose ("UID string was zero length\n");
539 if (!_dbus_string_parse_int (uid_str, 0, &val,
542 _dbus_verbose ("could not parse string as a UID\n");
546 if (end != _dbus_string_get_length (uid_str))
548 _dbus_verbose ("string contained trailing stuff after UID\n");
558 * Gets the credentials corresponding to the given username.
560 * @param username the username
561 * @param credentials credentials to fill in
562 * @returns #TRUE if the username existed and we got some credentials
565 _dbus_credentials_from_username (const DBusString *username,
566 DBusCredentials *credentials)
568 DBusUserDatabase *db;
569 const DBusUserInfo *info;
570 _dbus_user_database_lock_system ();
572 db = _dbus_user_database_get_system ();
575 _dbus_user_database_unlock_system ();
579 if (!_dbus_user_database_get_username (db, username,
582 _dbus_user_database_unlock_system ();
586 credentials->pid = DBUS_PID_UNSET;
587 credentials->uid = info->uid;
588 credentials->gid = info->primary_gid;
590 _dbus_user_database_unlock_system ();
595 * Gets the credentials corresponding to the given UID.
598 * @param credentials credentials to fill in
599 * @returns #TRUE if the UID existed and we got some credentials
602 _dbus_credentials_from_uid (dbus_uid_t uid,
603 DBusCredentials *credentials)
605 DBusUserDatabase *db;
606 const DBusUserInfo *info;
607 _dbus_user_database_lock_system ();
609 db = _dbus_user_database_get_system ();
612 _dbus_user_database_unlock_system ();
616 if (!_dbus_user_database_get_uid (db, uid,
619 _dbus_user_database_unlock_system ();
623 _dbus_assert (info->uid == uid);
625 credentials->pid = DBUS_PID_UNSET;
626 credentials->uid = info->uid;
627 credentials->gid = info->primary_gid;
629 _dbus_user_database_unlock_system ();
634 * Creates a new user database object used to look up and
635 * cache user information.
636 * @returns new database, or #NULL on out of memory
639 _dbus_user_database_new (void)
641 DBusUserDatabase *db;
643 db = dbus_new0 (DBusUserDatabase, 1);
649 db->users = _dbus_hash_table_new (DBUS_HASH_ULONG,
650 NULL, free_user_info);
652 if (db->users == NULL)
655 db->groups = _dbus_hash_table_new (DBUS_HASH_ULONG,
656 NULL, free_group_info);
658 if (db->groups == NULL)
661 db->users_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
663 if (db->users_by_name == NULL)
666 db->groups_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
668 if (db->groups_by_name == NULL)
674 _dbus_user_database_unref (db);
679 * Increments refcount of user database.
680 * @param db the database
681 * @returns the database
684 _dbus_user_database_ref (DBusUserDatabase *db)
686 _dbus_assert (db->refcount > 0);
694 * Decrements refcount of user database.
695 * @param db the database
698 _dbus_user_database_unref (DBusUserDatabase *db)
700 _dbus_assert (db->refcount > 0);
703 if (db->refcount == 0)
706 _dbus_hash_table_unref (db->users);
709 _dbus_hash_table_unref (db->groups);
711 if (db->users_by_name)
712 _dbus_hash_table_unref (db->users_by_name);
714 if (db->groups_by_name)
715 _dbus_hash_table_unref (db->groups_by_name);
722 * Gets all groups for a particular user. Returns #FALSE
723 * if no memory, or user isn't known, but always initializes
724 * group_ids to a NULL array. Sets error to the reason
725 * for returning #FALSE.
727 * @param db the user database object
728 * @param uid the user ID
729 * @param group_ids return location for array of group IDs
730 * @param n_group_ids return location for length of returned array
731 * @param error return location for error
732 * @returns #TRUE on success
735 _dbus_user_database_get_groups (DBusUserDatabase *db,
737 dbus_gid_t **group_ids,
743 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
748 info = _dbus_user_database_lookup (db, uid, NULL, error);
751 _DBUS_ASSERT_ERROR_IS_SET (error);
755 if (info->n_group_ids > 0)
757 *group_ids = dbus_new (dbus_gid_t, info->n_group_ids);
758 if (*group_ids == NULL)
760 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
764 *n_group_ids = info->n_group_ids;
766 memcpy (*group_ids, info->group_ids, info->n_group_ids * sizeof (dbus_gid_t));
773 * Gets the user information for the given UID,
774 * returned user info should not be freed.
776 * @param db user database
777 * @param uid the user ID
778 * @param info return location for const ref to user info
779 * @param error error location
780 * @returns #FALSE if error is set
783 _dbus_user_database_get_uid (DBusUserDatabase *db,
785 const DBusUserInfo **info,
788 *info = _dbus_user_database_lookup (db, uid, NULL, error);
789 return *info != NULL;
793 * Gets the user information for the given GID,
794 * returned group info should not be freed.
796 * @param db user database
797 * @param gid the group ID
798 * @param info return location for const ref to group info
799 * @param error error location
800 * @returns #FALSE if error is set
803 _dbus_user_database_get_gid (DBusUserDatabase *db,
805 const DBusGroupInfo **info,
808 *info = _dbus_user_database_lookup_group (db, gid, NULL, error);
809 return *info != NULL;
813 * Gets the user information for the given username.
815 * @param db user database
816 * @param username the user name
817 * @param info return location for const ref to user info
818 * @param error error location
819 * @returns #FALSE if error is set
822 _dbus_user_database_get_username (DBusUserDatabase *db,
823 const DBusString *username,
824 const DBusUserInfo **info,
827 *info = _dbus_user_database_lookup (db, DBUS_UID_UNSET, username, error);
828 return *info != NULL;
832 * Gets the user information for the given group name,
833 * returned group info should not be freed.
835 * @param db user database
836 * @param groupname the group name
837 * @param info return location for const ref to group info
838 * @param error error location
839 * @returns #FALSE if error is set
842 _dbus_user_database_get_groupname (DBusUserDatabase *db,
843 const DBusString *groupname,
844 const DBusGroupInfo **info,
847 *info = _dbus_user_database_lookup_group (db, DBUS_GID_UNSET, groupname, error);
848 return *info != NULL;
853 #ifdef DBUS_BUILD_TESTS
857 * Unit test for dbus-userdb.c.
859 * @returns #TRUE on success.
862 _dbus_userdb_test (const char *test_data_dir)
864 const DBusString *username;
865 const DBusString *homedir;
867 if (!_dbus_username_from_current_process (&username))
868 _dbus_assert_not_reached ("didn't get username");
870 if (!_dbus_homedir_from_current_process (&homedir))
871 _dbus_assert_not_reached ("didn't get homedir");
873 printf (" Current user: %s homedir: %s\n",
874 _dbus_string_get_const_data (username),
875 _dbus_string_get_const_data (homedir));
879 #endif /* DBUS_BUILD_TESTS */