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 #define DBUS_USERDB_INCLUDES_PRIVATE 1
24 #include "dbus-userdb.h"
25 #include "dbus-hash.h"
26 #include "dbus-test.h"
27 #include "dbus-internals.h"
28 #include "dbus-protocol.h"
29 #include "dbus-credentials.h"
33 * @addtogroup DBusInternalsUtils
38 * Frees the given #DBusUserInfo's members with _dbus_user_info_free()
39 * and also calls dbus_free() on the block itself
41 * @param info the info
44 _dbus_user_info_free_allocated (DBusUserInfo *info)
46 if (info == NULL) /* hash table will pass NULL */
49 _dbus_user_info_free (info);
54 * Frees the given #DBusGroupInfo's members with _dbus_group_info_free()
55 * and also calls dbus_free() on the block itself
57 * @param info the info
60 _dbus_group_info_free_allocated (DBusGroupInfo *info)
62 if (info == NULL) /* hash table will pass NULL */
65 _dbus_group_info_free (info);
70 * Frees the members of info
71 * (but not info itself)
72 * @param info the user info struct
75 _dbus_user_info_free (DBusUserInfo *info)
77 dbus_free (info->group_ids);
78 dbus_free (info->username);
79 dbus_free (info->homedir);
83 * Frees the members of info (but not info itself).
85 * @param info the group info
88 _dbus_group_info_free (DBusGroupInfo *info)
90 dbus_free (info->groupname);
94 * Checks if a given string is actually a number
95 * and converts it if it is
97 * @param str the string to check
98 * @param num the memory location of the unsigned long to fill in
99 * @returns TRUE if str is a number and num is filled in
102 _dbus_is_a_number (const DBusString *str,
107 if (_dbus_string_parse_uint (str, 0, num, &end) &&
108 end == _dbus_string_get_length (str))
115 * Looks up a uid or username in the user database. Only one of name
116 * or UID can be provided. There are wrapper functions for this that
117 * are better to use, this one does no locking or anything on the
118 * database and otherwise sort of sucks.
120 * @param db the database
121 * @param uid the user ID or #DBUS_UID_UNSET
122 * @param username username or #NULL
123 * @param error error to fill in
124 * @returns the entry in the database
127 _dbus_user_database_lookup (DBusUserDatabase *db,
129 const DBusString *username,
134 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
135 _dbus_assert (uid != DBUS_UID_UNSET || username != NULL);
137 /* See if the username is really a number */
138 if (uid == DBUS_UID_UNSET)
142 if (_dbus_is_a_number (username, &n))
146 #ifdef DBUS_ENABLE_USER_CACHE
147 if (uid != DBUS_UID_UNSET)
148 info = _dbus_hash_table_lookup_ulong (db->users, uid);
150 info = _dbus_hash_table_lookup_string (db->users_by_name, _dbus_string_get_const_data (username));
154 _dbus_verbose ("Using cache for UID "DBUS_UID_FORMAT" information\n",
163 if (uid != DBUS_UID_UNSET)
164 _dbus_verbose ("No cache for UID "DBUS_UID_FORMAT"\n",
167 _dbus_verbose ("No cache for user \"%s\"\n",
168 _dbus_string_get_const_data (username));
170 info = dbus_new0 (DBusUserInfo, 1);
173 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
177 if (uid != DBUS_UID_UNSET)
179 if (!_dbus_user_info_fill_uid (info, uid, error))
181 _DBUS_ASSERT_ERROR_IS_SET (error);
182 _dbus_user_info_free_allocated (info);
188 if (!_dbus_user_info_fill (info, username, error))
190 _DBUS_ASSERT_ERROR_IS_SET (error);
191 _dbus_user_info_free_allocated (info);
196 /* be sure we don't use these after here */
197 uid = DBUS_UID_UNSET;
200 /* insert into hash */
201 if (!_dbus_hash_table_insert_ulong (db->users, info->uid, info))
203 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
204 _dbus_user_info_free_allocated (info);
208 if (!_dbus_hash_table_insert_string (db->users_by_name,
212 _dbus_hash_table_remove_ulong (db->users, info->uid);
213 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
221 static dbus_bool_t database_locked = FALSE;
222 static DBusUserDatabase *system_db = NULL;
223 static DBusString process_username;
224 static DBusString process_homedir;
227 shutdown_system_db (void *data)
229 _dbus_user_database_unref (system_db);
231 _dbus_string_free (&process_username);
232 _dbus_string_free (&process_homedir);
236 init_system_db (void)
238 _dbus_assert (database_locked);
240 if (system_db == NULL)
243 const DBusUserInfo *info;
245 system_db = _dbus_user_database_new ();
246 if (system_db == NULL)
249 dbus_error_init (&error);
251 if (!_dbus_user_database_get_uid (system_db,
256 _dbus_user_database_unref (system_db);
259 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
261 dbus_error_free (&error);
266 /* This really should not happen. */
267 _dbus_warn ("Could not get password database information for UID of current process: %s\n",
269 dbus_error_free (&error);
274 if (!_dbus_string_init (&process_username))
276 _dbus_user_database_unref (system_db);
281 if (!_dbus_string_init (&process_homedir))
283 _dbus_string_free (&process_username);
284 _dbus_user_database_unref (system_db);
289 if (!_dbus_string_append (&process_username,
291 !_dbus_string_append (&process_homedir,
293 !_dbus_register_shutdown_func (shutdown_system_db, NULL))
295 _dbus_string_free (&process_username);
296 _dbus_string_free (&process_homedir);
297 _dbus_user_database_unref (system_db);
307 * Locks global system user database.
310 _dbus_user_database_lock_system (void)
312 _DBUS_LOCK (system_users);
313 database_locked = TRUE;
317 * Unlocks global system user database.
320 _dbus_user_database_unlock_system (void)
322 database_locked = FALSE;
323 _DBUS_UNLOCK (system_users);
327 * Gets the system global user database;
328 * must be called with lock held (_dbus_user_database_lock_system()).
330 * @returns the database or #NULL if no memory
333 _dbus_user_database_get_system (void)
335 _dbus_assert (database_locked);
343 * Flushes the system global user database;
346 _dbus_user_database_flush_system (void)
348 _dbus_user_database_lock_system ();
350 _dbus_user_database_flush (system_db);
352 _dbus_user_database_unlock_system ();
356 * Gets username of user owning current process. The returned string
357 * is valid until dbus_shutdown() is called.
359 * @param username place to store pointer to username
360 * @returns #FALSE if no memory
363 _dbus_username_from_current_process (const DBusString **username)
365 _dbus_user_database_lock_system ();
366 if (!init_system_db ())
368 _dbus_user_database_unlock_system ();
371 *username = &process_username;
372 _dbus_user_database_unlock_system ();
378 * Gets homedir of user owning current process. The returned string
379 * is valid until dbus_shutdown() is called.
381 * @param homedir place to store pointer to homedir
382 * @returns #FALSE if no memory
385 _dbus_homedir_from_current_process (const DBusString **homedir)
387 _dbus_user_database_lock_system ();
388 if (!init_system_db ())
390 _dbus_user_database_unlock_system ();
393 *homedir = &process_homedir;
394 _dbus_user_database_unlock_system ();
400 * Gets the home directory for the given user.
402 * @param username the username
403 * @param homedir string to append home directory to
404 * @returns #TRUE if user existed and we appended their homedir
407 _dbus_homedir_from_username (const DBusString *username,
410 DBusUserDatabase *db;
411 const DBusUserInfo *info;
412 _dbus_user_database_lock_system ();
414 db = _dbus_user_database_get_system ();
417 _dbus_user_database_unlock_system ();
421 if (!_dbus_user_database_get_username (db, username,
424 _dbus_user_database_unlock_system ();
428 if (!_dbus_string_append (homedir, info->homedir))
430 _dbus_user_database_unlock_system ();
434 _dbus_user_database_unlock_system ();
439 * Gets the home directory for the given user.
442 * @param homedir string to append home directory to
443 * @returns #TRUE if user existed and we appended their homedir
446 _dbus_homedir_from_uid (dbus_uid_t uid,
449 DBusUserDatabase *db;
450 const DBusUserInfo *info;
451 _dbus_user_database_lock_system ();
453 db = _dbus_user_database_get_system ();
456 _dbus_user_database_unlock_system ();
460 if (!_dbus_user_database_get_uid (db, uid,
463 _dbus_user_database_unlock_system ();
467 if (!_dbus_string_append (homedir, info->homedir))
469 _dbus_user_database_unlock_system ();
473 _dbus_user_database_unlock_system ();
478 * Adds the credentials corresponding to the given username.
480 * @param credentials credentials to fill in
481 * @param username the username
482 * @returns #TRUE if the username existed and we got some credentials
485 _dbus_credentials_add_from_username (DBusCredentials *credentials,
486 const DBusString *username)
488 DBusUserDatabase *db;
489 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_credentials_add_unix_uid(credentials, info->uid))
509 _dbus_user_database_unlock_system ();
513 _dbus_user_database_unlock_system ();
518 * Creates a new user database object used to look up and
519 * cache user information.
520 * @returns new database, or #NULL on out of memory
523 _dbus_user_database_new (void)
525 DBusUserDatabase *db;
527 db = dbus_new0 (DBusUserDatabase, 1);
533 db->users = _dbus_hash_table_new (DBUS_HASH_ULONG,
534 NULL, (DBusFreeFunction) _dbus_user_info_free_allocated);
536 if (db->users == NULL)
539 db->groups = _dbus_hash_table_new (DBUS_HASH_ULONG,
540 NULL, (DBusFreeFunction) _dbus_group_info_free_allocated);
542 if (db->groups == NULL)
545 db->users_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
547 if (db->users_by_name == NULL)
550 db->groups_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
552 if (db->groups_by_name == NULL)
558 _dbus_user_database_unref (db);
563 * Flush all information out of the user database.
566 _dbus_user_database_flush (DBusUserDatabase *db)
568 _dbus_hash_table_remove_all(db->users_by_name);
569 _dbus_hash_table_remove_all(db->groups_by_name);
570 _dbus_hash_table_remove_all(db->users);
571 _dbus_hash_table_remove_all(db->groups);
574 #ifdef DBUS_BUILD_TESTS
576 * Increments refcount of user database.
577 * @param db the database
578 * @returns the database
581 _dbus_user_database_ref (DBusUserDatabase *db)
583 _dbus_assert (db->refcount > 0);
589 #endif /* DBUS_BUILD_TESTS */
592 * Decrements refcount of user database.
593 * @param db the database
596 _dbus_user_database_unref (DBusUserDatabase *db)
598 _dbus_assert (db->refcount > 0);
601 if (db->refcount == 0)
604 _dbus_hash_table_unref (db->users);
607 _dbus_hash_table_unref (db->groups);
609 if (db->users_by_name)
610 _dbus_hash_table_unref (db->users_by_name);
612 if (db->groups_by_name)
613 _dbus_hash_table_unref (db->groups_by_name);
620 * Gets the user information for the given UID,
621 * returned user info should not be freed.
623 * @param db user database
624 * @param uid the user ID
625 * @param info return location for const ref to user info
626 * @param error error location
627 * @returns #FALSE if error is set
630 _dbus_user_database_get_uid (DBusUserDatabase *db,
632 const DBusUserInfo **info,
635 *info = _dbus_user_database_lookup (db, uid, NULL, error);
636 return *info != NULL;
640 * Gets the user information for the given username.
642 * @param db user database
643 * @param username the user name
644 * @param info return location for const ref to user info
645 * @param error error location
646 * @returns #FALSE if error is set
649 _dbus_user_database_get_username (DBusUserDatabase *db,
650 const DBusString *username,
651 const DBusUserInfo **info,
654 *info = _dbus_user_database_lookup (db, DBUS_UID_UNSET, username, error);
655 return *info != NULL;
660 /* Tests in dbus-userdb-util.c */