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.0
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 */
44 free_user_info (void *data)
46 DBusUserInfo *info = data;
48 if (info == NULL) /* hash table will pass NULL */
51 _dbus_user_info_free (info);
56 free_group_info (void *data)
58 DBusGroupInfo *info = data;
60 if (info == NULL) /* hash table will pass NULL */
63 _dbus_group_info_free (info);
68 _dbus_user_database_lookup (DBusUserDatabase *db,
70 const DBusString *username,
75 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
76 _dbus_assert (uid != DBUS_UID_UNSET || username != NULL);
78 if (uid != DBUS_UID_UNSET)
79 info = _dbus_hash_table_lookup_ulong (db->users, uid);
81 info = _dbus_hash_table_lookup_string (db->users_by_name, _dbus_string_get_const_data (username));
85 _dbus_verbose ("Using cache for UID "DBUS_UID_FORMAT" information\n",
91 _dbus_verbose ("No cache for UID "DBUS_UID_FORMAT"\n",
94 info = dbus_new0 (DBusUserInfo, 1);
97 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
101 if (uid != DBUS_UID_UNSET)
103 if (!_dbus_user_info_fill_uid (info, uid, error))
105 _DBUS_ASSERT_ERROR_IS_SET (error);
106 free_user_info (info);
112 if (!_dbus_user_info_fill (info, username, error))
114 _DBUS_ASSERT_ERROR_IS_SET (error);
115 free_user_info (info);
120 /* be sure we don't use these after here */
121 uid = DBUS_UID_UNSET;
124 /* insert into hash */
125 if (!_dbus_hash_table_insert_ulong (db->users, info->uid, info))
127 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
128 free_user_info (info);
132 if (!_dbus_hash_table_insert_string (db->users_by_name,
136 _dbus_hash_table_remove_ulong (db->users, info->uid);
137 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
145 static DBusGroupInfo*
146 _dbus_user_database_lookup_group (DBusUserDatabase *db,
148 const DBusString *groupname,
153 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
155 if (gid != DBUS_GID_UNSET)
156 info = _dbus_hash_table_lookup_ulong (db->groups, gid);
158 info = _dbus_hash_table_lookup_string (db->groups_by_name,
159 _dbus_string_get_const_data (groupname));
162 _dbus_verbose ("Using cache for GID "DBUS_GID_FORMAT" information\n",
168 _dbus_verbose ("No cache for GID "DBUS_GID_FORMAT"\n",
171 info = dbus_new0 (DBusGroupInfo, 1);
174 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
178 if (!_dbus_group_info_fill_gid (info, gid, error))
180 _DBUS_ASSERT_ERROR_IS_SET (error);
181 free_group_info (info);
185 if (!_dbus_hash_table_insert_ulong (db->groups, info->gid, info))
187 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
188 free_group_info (info);
193 if (!_dbus_hash_table_insert_string (db->groups_by_name,
197 _dbus_hash_table_remove_ulong (db->groups, info->gid);
198 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
206 _DBUS_DEFINE_GLOBAL_LOCK(system_users);
207 static dbus_bool_t database_locked = FALSE;
208 static DBusUserDatabase *system_db = NULL;
209 static DBusString process_username;
210 static DBusString process_homedir;
213 shutdown_system_db (void *data)
215 _dbus_user_database_unref (system_db);
217 _dbus_string_free (&process_username);
218 _dbus_string_free (&process_homedir);
222 init_system_db (void)
224 _dbus_assert (database_locked);
226 if (system_db == NULL)
229 const DBusUserInfo *info;
231 system_db = _dbus_user_database_new ();
232 if (system_db == NULL)
235 dbus_error_init (&error);
237 if (!_dbus_user_database_get_uid (system_db,
242 _dbus_user_database_unref (system_db);
245 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
247 dbus_error_free (&error);
252 /* This really should not happen. */
253 _dbus_warn ("Could not get password database information for UID of current process: %s\n",
255 dbus_error_free (&error);
260 if (!_dbus_string_init (&process_username))
262 _dbus_user_database_unref (system_db);
267 if (!_dbus_string_init (&process_homedir))
269 _dbus_string_free (&process_username);
270 _dbus_user_database_unref (system_db);
275 if (!_dbus_string_append (&process_username,
277 !_dbus_string_append (&process_homedir,
279 !_dbus_register_shutdown_func (shutdown_system_db, NULL))
281 _dbus_string_free (&process_username);
282 _dbus_string_free (&process_homedir);
283 _dbus_user_database_unref (system_db);
293 * @addtogroup DBusInternalsUtils
298 * Locks global system user database.
301 _dbus_user_database_lock_system (void)
303 _DBUS_LOCK (system_users);
304 database_locked = TRUE;
308 * Unlocks global system user database.
311 _dbus_user_database_unlock_system (void)
313 database_locked = FALSE;
314 _DBUS_UNLOCK (system_users);
318 * Gets the system global user database;
319 * must be called with lock held (_dbus_user_database_lock_system()).
321 * @returns the database or #NULL if no memory
324 _dbus_user_database_get_system (void)
326 _dbus_assert (database_locked);
334 * Gets username of user owning current process. The returned string
335 * is valid until dbus_shutdown() is called.
337 * @param username place to store pointer to username
338 * @returns #FALSE if no memory
341 _dbus_username_from_current_process (const DBusString **username)
343 _dbus_user_database_lock_system ();
344 if (!init_system_db ())
346 _dbus_user_database_unlock_system ();
349 *username = &process_username;
350 _dbus_user_database_unlock_system ();
356 * Gets homedir of user owning current process. The returned string
357 * is valid until dbus_shutdown() is called.
359 * @param homedir place to store pointer to homedir
360 * @returns #FALSE if no memory
363 _dbus_homedir_from_current_process (const DBusString **homedir)
365 _dbus_user_database_lock_system ();
366 if (!init_system_db ())
368 _dbus_user_database_unlock_system ();
371 *homedir = &process_homedir;
372 _dbus_user_database_unlock_system ();
378 * Gets user ID given username
380 * @param username the username
381 * @param uid return location for UID
382 * @returns #TRUE if username existed and we got the UID
385 _dbus_get_user_id (const DBusString *username,
388 DBusCredentials creds;
390 if (!_dbus_credentials_from_username (username, &creds))
393 if (creds.uid == DBUS_UID_UNSET)
402 * Gets group ID given groupname
404 * @param groupname the groupname
405 * @param gid return location for GID
406 * @returns #TRUE if group name existed and we got the GID
409 _dbus_get_group_id (const DBusString *groupname,
412 DBusUserDatabase *db;
413 const DBusGroupInfo *info;
414 _dbus_user_database_lock_system ();
416 db = _dbus_user_database_get_system ();
419 _dbus_user_database_unlock_system ();
423 if (!_dbus_user_database_get_groupname (db, groupname,
426 _dbus_user_database_unlock_system ();
432 _dbus_user_database_unlock_system ();
437 * Gets the home directory for the given user.
439 * @param username the username
440 * @param homedir string to append home directory to
441 * @returns #TRUE if user existed and we appended their homedir
444 _dbus_homedir_from_username (const DBusString *username,
447 DBusUserDatabase *db;
448 const DBusUserInfo *info;
449 _dbus_user_database_lock_system ();
451 db = _dbus_user_database_get_system ();
454 _dbus_user_database_unlock_system ();
458 if (!_dbus_user_database_get_username (db, username,
461 _dbus_user_database_unlock_system ();
465 if (!_dbus_string_append (homedir, info->homedir))
467 _dbus_user_database_unlock_system ();
471 _dbus_user_database_unlock_system ();
476 * Gets a UID from a UID string.
478 * @param uid_str the UID in string form
479 * @param uid UID to fill in
480 * @returns #TRUE if successfully filled in UID
483 _dbus_uid_from_string (const DBusString *uid_str,
489 if (_dbus_string_get_length (uid_str) == 0)
491 _dbus_verbose ("UID string was zero length\n");
497 if (!_dbus_string_parse_int (uid_str, 0, &val,
500 _dbus_verbose ("could not parse string as a UID\n");
504 if (end != _dbus_string_get_length (uid_str))
506 _dbus_verbose ("string contained trailing stuff after UID\n");
516 * Gets the credentials corresponding to the given username.
518 * @param username the username
519 * @param credentials credentials to fill in
520 * @returns #TRUE if the username existed and we got some credentials
523 _dbus_credentials_from_username (const DBusString *username,
524 DBusCredentials *credentials)
526 DBusUserDatabase *db;
527 const DBusUserInfo *info;
528 _dbus_user_database_lock_system ();
530 db = _dbus_user_database_get_system ();
533 _dbus_user_database_unlock_system ();
537 if (!_dbus_user_database_get_username (db, username,
540 _dbus_user_database_unlock_system ();
544 credentials->pid = DBUS_PID_UNSET;
545 credentials->uid = info->uid;
546 credentials->gid = info->primary_gid;
548 _dbus_user_database_unlock_system ();
553 * Gets the credentials corresponding to the given UID.
556 * @param credentials credentials to fill in
557 * @returns #TRUE if the UID existed and we got some credentials
560 _dbus_credentials_from_uid (dbus_uid_t uid,
561 DBusCredentials *credentials)
563 DBusUserDatabase *db;
564 const DBusUserInfo *info;
565 _dbus_user_database_lock_system ();
567 db = _dbus_user_database_get_system ();
570 _dbus_user_database_unlock_system ();
574 if (!_dbus_user_database_get_uid (db, uid,
577 _dbus_user_database_unlock_system ();
581 _dbus_assert (info->uid == uid);
583 credentials->pid = DBUS_PID_UNSET;
584 credentials->uid = info->uid;
585 credentials->gid = info->primary_gid;
587 _dbus_user_database_unlock_system ();
592 * Creates a new user database object used to look up and
593 * cache user information.
594 * @returns new database, or #NULL on out of memory
597 _dbus_user_database_new (void)
599 DBusUserDatabase *db;
601 db = dbus_new0 (DBusUserDatabase, 1);
607 db->users = _dbus_hash_table_new (DBUS_HASH_ULONG,
608 NULL, free_user_info);
610 if (db->users == NULL)
613 db->groups = _dbus_hash_table_new (DBUS_HASH_ULONG,
614 NULL, free_group_info);
616 if (db->groups == NULL)
619 db->users_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
621 if (db->users_by_name == NULL)
624 db->groups_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
626 if (db->groups_by_name == NULL)
632 _dbus_user_database_unref (db);
637 * Increments refcount of user database.
638 * @param db the database
639 * @returns the database
642 _dbus_user_database_ref (DBusUserDatabase *db)
644 _dbus_assert (db->refcount > 0);
652 * Decrements refcount of user database.
653 * @param db the database
656 _dbus_user_database_unref (DBusUserDatabase *db)
658 _dbus_assert (db->refcount > 0);
661 if (db->refcount == 0)
664 _dbus_hash_table_unref (db->users);
667 _dbus_hash_table_unref (db->groups);
669 if (db->users_by_name)
670 _dbus_hash_table_unref (db->users_by_name);
672 if (db->groups_by_name)
673 _dbus_hash_table_unref (db->groups_by_name);
680 * Gets all groups for a particular user. Returns #FALSE
681 * if no memory, or user isn't known, but always initializes
682 * group_ids to a NULL array. Sets error to the reason
683 * for returning #FALSE.
685 * @param db the user database object
686 * @param uid the user ID
687 * @param group_ids return location for array of group IDs
688 * @param n_group_ids return location for length of returned array
689 * @param error return location for error
690 * @returns #TRUE on success
693 _dbus_user_database_get_groups (DBusUserDatabase *db,
695 dbus_gid_t **group_ids,
701 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
706 info = _dbus_user_database_lookup (db, uid, NULL, error);
709 _DBUS_ASSERT_ERROR_IS_SET (error);
713 if (info->n_group_ids > 0)
715 *group_ids = dbus_new (dbus_gid_t, info->n_group_ids);
716 if (*group_ids == NULL)
718 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
722 *n_group_ids = info->n_group_ids;
724 memcpy (*group_ids, info->group_ids, info->n_group_ids * sizeof (dbus_gid_t));
731 * Gets the user information for the given UID,
732 * returned user info should not be freed.
734 * @param db user database
735 * @param uid the user ID
736 * @param info return location for const ref to user info
737 * @param error error location
738 * @returns #FALSE if error is set
741 _dbus_user_database_get_uid (DBusUserDatabase *db,
743 const DBusUserInfo **info,
746 *info = _dbus_user_database_lookup (db, uid, NULL, error);
747 return *info != NULL;
751 * Gets the user information for the given GID,
752 * returned group info should not be freed.
754 * @param db user database
755 * @param gid the group ID
756 * @param info return location for const ref to group info
757 * @param error error location
758 * @returns #FALSE if error is set
761 _dbus_user_database_get_gid (DBusUserDatabase *db,
763 const DBusGroupInfo **info,
766 *info = _dbus_user_database_lookup_group (db, gid, NULL, error);
767 return *info != NULL;
771 * Gets the user information for the given username.
773 * @param db user database
774 * @param username the user name
775 * @param info return location for const ref to user info
776 * @param error error location
777 * @returns #FALSE if error is set
780 _dbus_user_database_get_username (DBusUserDatabase *db,
781 const DBusString *username,
782 const DBusUserInfo **info,
785 *info = _dbus_user_database_lookup (db, DBUS_UID_UNSET, username, error);
786 return *info != NULL;
790 * Gets the user information for the given group name,
791 * returned group info should not be freed.
793 * @param db user database
794 * @param groupname the group name
795 * @param info return location for const ref to group info
796 * @param error error location
797 * @returns #FALSE if error is set
800 _dbus_user_database_get_groupname (DBusUserDatabase *db,
801 const DBusString *groupname,
802 const DBusGroupInfo **info,
805 *info = _dbus_user_database_lookup_group (db, DBUS_GID_UNSET, groupname, error);
806 return *info != NULL;
811 #ifdef DBUS_BUILD_TESTS
815 * Unit test for dbus-userdb.c.
817 * @returns #TRUE on success.
820 _dbus_userdb_test (const char *test_data_dir)
822 const DBusString *username;
823 const DBusString *homedir;
825 if (!_dbus_username_from_current_process (&username))
826 _dbus_assert_not_reached ("didn't get username");
828 if (!_dbus_homedir_from_current_process (&homedir))
829 _dbus_assert_not_reached ("didn't get homedir");
831 printf (" Current user: %s homedir: %s\n",
832 _dbus_string_get_const_data (username),
833 _dbus_string_get_const_data (homedir));
837 #endif /* DBUS_BUILD_TESTS */