1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-userdb.c User database abstraction
4 * Copyright (C) 2003 Red Hat, Inc.
6 * Licensed under the Academic Free License version 1.2
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"
30 * Internals of DBusUserDatabase
32 struct DBusUserDatabase
34 int refcount; /**< Reference count */
36 DBusHashTable *users; /**< Users in the database by UID */
37 DBusHashTable *groups; /**< Groups in the database by GID */
38 DBusHashTable *users_by_name; /**< Users in the database by name */
39 DBusHashTable *groups_by_name; /**< Groups in the database by name */
43 free_user_info (void *data)
45 DBusUserInfo *info = data;
47 if (info == NULL) /* hash table will pass NULL */
50 _dbus_user_info_free (info);
55 free_group_info (void *data)
57 DBusGroupInfo *info = data;
59 if (info == NULL) /* hash table will pass NULL */
62 _dbus_group_info_free (info);
67 _dbus_user_database_lookup (DBusUserDatabase *db,
69 const DBusString *username,
74 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
75 _dbus_assert (uid != DBUS_UID_UNSET || username != NULL);
77 if (uid != DBUS_UID_UNSET)
78 info = _dbus_hash_table_lookup_ulong (db->users, uid);
80 info = _dbus_hash_table_lookup_string (db->users_by_name, _dbus_string_get_const_data (username));
84 _dbus_verbose ("Using cache for UID "DBUS_UID_FORMAT" information\n",
90 _dbus_verbose ("No cache for UID "DBUS_UID_FORMAT"\n",
93 info = dbus_new0 (DBusUserInfo, 1);
96 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
100 if (uid != DBUS_UID_UNSET)
102 if (!_dbus_user_info_fill_uid (info, uid, error))
104 _DBUS_ASSERT_ERROR_IS_SET (error);
105 free_user_info (info);
111 if (!_dbus_user_info_fill (info, username, error))
113 _DBUS_ASSERT_ERROR_IS_SET (error);
114 free_user_info (info);
119 /* be sure we don't use these after here */
120 uid = DBUS_UID_UNSET;
123 /* insert into hash */
124 if (!_dbus_hash_table_insert_ulong (db->users, info->uid, info))
126 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
127 free_user_info (info);
131 if (!_dbus_hash_table_insert_string (db->users_by_name,
135 _dbus_hash_table_remove_ulong (db->users, info->uid);
136 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
144 static DBusGroupInfo*
145 _dbus_user_database_lookup_group (DBusUserDatabase *db,
147 const DBusString *groupname,
152 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
154 if (gid != DBUS_GID_UNSET)
155 info = _dbus_hash_table_lookup_ulong (db->groups, gid);
157 info = _dbus_hash_table_lookup_string (db->groups_by_name,
158 _dbus_string_get_const_data (groupname));
161 _dbus_verbose ("Using cache for GID "DBUS_GID_FORMAT" information\n",
167 _dbus_verbose ("No cache for GID "DBUS_GID_FORMAT"\n",
170 info = dbus_new0 (DBusGroupInfo, 1);
173 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
177 if (!_dbus_group_info_fill_gid (info, gid, error))
179 _DBUS_ASSERT_ERROR_IS_SET (error);
180 free_group_info (info);
184 if (!_dbus_hash_table_insert_ulong (db->groups, info->gid, info))
186 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
187 free_group_info (info);
192 if (!_dbus_hash_table_insert_string (db->groups_by_name,
196 _dbus_hash_table_remove_ulong (db->groups, info->gid);
197 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
205 _DBUS_DEFINE_GLOBAL_LOCK(system_users);
206 static dbus_bool_t database_locked = FALSE;
207 static DBusUserDatabase *system_db = NULL;
208 static DBusString process_username;
209 static DBusString process_homedir;
212 shutdown_system_db (void *data)
214 _dbus_user_database_unref (system_db);
216 _dbus_string_free (&process_username);
217 _dbus_string_free (&process_homedir);
221 init_system_db (void)
223 _dbus_assert (database_locked);
225 if (system_db == NULL)
228 const DBusUserInfo *info;
230 system_db = _dbus_user_database_new ();
231 if (system_db == NULL)
234 dbus_error_init (&error);
236 if (!_dbus_user_database_get_uid (system_db,
241 _dbus_user_database_unref (system_db);
244 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
246 dbus_error_free (&error);
251 /* This really should not happen. */
252 _dbus_warn ("Could not get password database information for UID of current process: %s\n",
254 dbus_error_free (&error);
259 if (!_dbus_string_init (&process_username))
261 _dbus_user_database_unref (system_db);
266 if (!_dbus_string_init (&process_homedir))
268 _dbus_string_free (&process_username);
269 _dbus_user_database_unref (system_db);
274 if (!_dbus_string_append (&process_username,
276 !_dbus_string_append (&process_homedir,
278 !_dbus_register_shutdown_func (shutdown_system_db, NULL))
280 _dbus_string_free (&process_username);
281 _dbus_string_free (&process_homedir);
282 _dbus_user_database_unref (system_db);
292 * @addtogroup DBusInternalsUtils
297 * Locks global system user database.
300 _dbus_user_database_lock_system (void)
302 _DBUS_LOCK (system_users);
303 database_locked = TRUE;
307 * Unlocks global system user database.
310 _dbus_user_database_unlock_system (void)
312 database_locked = FALSE;
313 _DBUS_UNLOCK (system_users);
317 * Gets the system global user database;
318 * must be called with lock held (_dbus_user_database_lock_system()).
320 * @returns the database or #NULL if no memory
323 _dbus_user_database_get_system (void)
325 _dbus_assert (database_locked);
333 * Gets username of user owning current process. The returned string
334 * is valid until dbus_shutdown() is called.
336 * @param username place to store pointer to username
337 * @returns #FALSE if no memory
340 _dbus_username_from_current_process (const DBusString **username)
342 _dbus_user_database_lock_system ();
343 if (!init_system_db ())
345 _dbus_user_database_unlock_system ();
348 *username = &process_username;
349 _dbus_user_database_unlock_system ();
355 * Gets homedir of user owning current process. The returned string
356 * is valid until dbus_shutdown() is called.
358 * @param homedir place to store pointer to homedir
359 * @returns #FALSE if no memory
362 _dbus_homedir_from_current_process (const DBusString **homedir)
364 _dbus_user_database_lock_system ();
365 if (!init_system_db ())
367 _dbus_user_database_unlock_system ();
370 *homedir = &process_homedir;
371 _dbus_user_database_unlock_system ();
377 * Gets user ID given username
379 * @param username the username
380 * @param uid return location for UID
381 * @returns #TRUE if username existed and we got the UID
384 _dbus_get_user_id (const DBusString *username,
387 DBusCredentials creds;
389 if (!_dbus_credentials_from_username (username, &creds))
392 if (creds.uid == DBUS_UID_UNSET)
401 * Gets group ID given groupname
403 * @param groupname the groupname
404 * @param gid return location for GID
405 * @returns #TRUE if group name existed and we got the GID
408 _dbus_get_group_id (const DBusString *groupname,
411 DBusUserDatabase *db;
412 const DBusGroupInfo *info;
413 _dbus_user_database_lock_system ();
415 db = _dbus_user_database_get_system ();
418 _dbus_user_database_unlock_system ();
422 if (!_dbus_user_database_get_groupname (db, groupname,
425 _dbus_user_database_unlock_system ();
431 _dbus_user_database_unlock_system ();
436 * Gets the home directory for the given user.
438 * @param username the username
439 * @param homedir string to append home directory to
440 * @returns #TRUE if user existed and we appended their homedir
443 _dbus_homedir_from_username (const DBusString *username,
446 DBusUserDatabase *db;
447 const DBusUserInfo *info;
448 _dbus_user_database_lock_system ();
450 db = _dbus_user_database_get_system ();
453 _dbus_user_database_unlock_system ();
457 if (!_dbus_user_database_get_username (db, username,
460 _dbus_user_database_unlock_system ();
464 if (!_dbus_string_append (homedir, info->homedir))
466 _dbus_user_database_unlock_system ();
470 _dbus_user_database_unlock_system ();
475 * Gets a UID from a UID string.
477 * @param uid_str the UID in string form
478 * @param uid UID to fill in
479 * @returns #TRUE if successfully filled in UID
482 _dbus_uid_from_string (const DBusString *uid_str,
488 if (_dbus_string_get_length (uid_str) == 0)
490 _dbus_verbose ("UID string was zero length\n");
496 if (!_dbus_string_parse_int (uid_str, 0, &val,
499 _dbus_verbose ("could not parse string as a UID\n");
503 if (end != _dbus_string_get_length (uid_str))
505 _dbus_verbose ("string contained trailing stuff after UID\n");
515 * Gets the credentials corresponding to the given username.
517 * @param username the username
518 * @param credentials credentials to fill in
519 * @returns #TRUE if the username existed and we got some credentials
522 _dbus_credentials_from_username (const DBusString *username,
523 DBusCredentials *credentials)
525 DBusUserDatabase *db;
526 const DBusUserInfo *info;
527 _dbus_user_database_lock_system ();
529 db = _dbus_user_database_get_system ();
532 _dbus_user_database_unlock_system ();
536 if (!_dbus_user_database_get_username (db, username,
539 _dbus_user_database_unlock_system ();
543 credentials->pid = DBUS_PID_UNSET;
544 credentials->uid = info->uid;
545 credentials->gid = info->primary_gid;
547 _dbus_user_database_unlock_system ();
552 * Gets the credentials corresponding to the given UID.
555 * @param credentials credentials to fill in
556 * @returns #TRUE if the UID existed and we got some credentials
559 _dbus_credentials_from_uid (dbus_uid_t uid,
560 DBusCredentials *credentials)
562 DBusUserDatabase *db;
563 const DBusUserInfo *info;
564 _dbus_user_database_lock_system ();
566 db = _dbus_user_database_get_system ();
569 _dbus_user_database_unlock_system ();
573 if (!_dbus_user_database_get_uid (db, uid,
576 _dbus_user_database_unlock_system ();
580 _dbus_assert (info->uid == uid);
582 credentials->pid = DBUS_PID_UNSET;
583 credentials->uid = info->uid;
584 credentials->gid = info->primary_gid;
586 _dbus_user_database_unlock_system ();
591 * Creates a new user database object used to look up and
592 * cache user information.
593 * @returns new database, or #NULL on out of memory
596 _dbus_user_database_new (void)
598 DBusUserDatabase *db;
600 db = dbus_new0 (DBusUserDatabase, 1);
606 db->users = _dbus_hash_table_new (DBUS_HASH_ULONG,
607 NULL, free_user_info);
609 if (db->users == NULL)
612 db->groups = _dbus_hash_table_new (DBUS_HASH_ULONG,
613 NULL, free_group_info);
615 if (db->groups == NULL)
618 db->users_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
620 if (db->users_by_name == NULL)
623 db->groups_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
625 if (db->groups_by_name == NULL)
631 _dbus_user_database_unref (db);
636 * Increments refcount of user database.
637 * @param db the database
640 _dbus_user_database_ref (DBusUserDatabase *db)
642 _dbus_assert (db->refcount > 0);
648 * Decrements refcount of user database.
649 * @param db the database
652 _dbus_user_database_unref (DBusUserDatabase *db)
654 _dbus_assert (db->refcount > 0);
657 if (db->refcount == 0)
660 _dbus_hash_table_unref (db->users);
663 _dbus_hash_table_unref (db->groups);
665 if (db->users_by_name)
666 _dbus_hash_table_unref (db->users_by_name);
668 if (db->groups_by_name)
669 _dbus_hash_table_unref (db->groups_by_name);
676 * Gets all groups for a particular user. Returns #FALSE
677 * if no memory, or user isn't known, but always initializes
678 * group_ids to a NULL array. Sets error to the reason
679 * for returning #FALSE.
681 * @param db the user database object
682 * @param uid the user ID
683 * @param group_ids return location for array of group IDs
684 * @param n_group_ids return location for length of returned array
685 * @param error return location for error
686 * @returns #TRUE on success
689 _dbus_user_database_get_groups (DBusUserDatabase *db,
691 dbus_gid_t **group_ids,
697 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
702 info = _dbus_user_database_lookup (db, uid, NULL, error);
705 _DBUS_ASSERT_ERROR_IS_SET (error);
709 if (info->n_group_ids > 0)
711 *group_ids = dbus_new (dbus_gid_t, info->n_group_ids);
712 if (*group_ids == NULL)
714 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
718 *n_group_ids = info->n_group_ids;
720 memcpy (*group_ids, info->group_ids, info->n_group_ids * sizeof (dbus_gid_t));
727 * Gets the user information for the given UID,
728 * returned user info should not be freed.
730 * @param db user database
731 * @param uid the user ID
732 * @param info return location for const ref to user info
733 * @param error error location
734 * @returns #FALSE if error is set
737 _dbus_user_database_get_uid (DBusUserDatabase *db,
739 const DBusUserInfo **info,
742 *info = _dbus_user_database_lookup (db, uid, NULL, error);
743 return *info != NULL;
747 * Gets the user information for the given GID,
748 * returned group info should not be freed.
750 * @param db user database
751 * @param gid the group ID
752 * @param info return location for const ref to group info
753 * @param error error location
754 * @returns #FALSE if error is set
757 _dbus_user_database_get_gid (DBusUserDatabase *db,
759 const DBusGroupInfo **info,
762 *info = _dbus_user_database_lookup_group (db, gid, NULL, error);
763 return *info != NULL;
767 * Gets the user information for the given username.
769 * @param db user database
770 * @param username the user name
771 * @param info return location for const ref to user info
772 * @param error error location
773 * @returns #FALSE if error is set
776 _dbus_user_database_get_username (DBusUserDatabase *db,
777 const DBusString *username,
778 const DBusUserInfo **info,
781 *info = _dbus_user_database_lookup (db, DBUS_UID_UNSET, username, error);
782 return *info != NULL;
786 * Gets the user information for the given group name,
787 * returned group info should not be freed.
789 * @param db user database
790 * @param groupname the group name
791 * @param info return location for const ref to group info
792 * @param error error location
793 * @returns #FALSE if error is set
796 _dbus_user_database_get_groupname (DBusUserDatabase *db,
797 const DBusString *groupname,
798 const DBusGroupInfo **info,
801 *info = _dbus_user_database_lookup_group (db, DBUS_GID_UNSET, groupname, error);
802 return *info != NULL;
807 #ifdef DBUS_BUILD_TESTS
811 * Unit test for dbus-userdb.c.
813 * @returns #TRUE on success.
816 _dbus_userdb_test (const char *test_data_dir)
818 const DBusString *username;
819 const DBusString *homedir;
821 if (!_dbus_username_from_current_process (&username))
822 _dbus_assert_not_reached ("didn't get username");
824 if (!_dbus_homedir_from_current_process (&homedir))
825 _dbus_assert_not_reached ("didn't get homedir");
827 printf (" Current user: %s homedir: %s\n",
828 _dbus_string_get_const_data (username),
829 _dbus_string_get_const_data (homedir));
833 #endif /* DBUS_BUILD_TESTS */