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_append_desired_identity (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 * Adds the credentials corresponding to the given username.
441 * @param credentials credentials to fill in
442 * @param username the username
443 * @returns #TRUE if the username existed and we got some credentials
446 _dbus_credentials_add_from_username (DBusCredentials *credentials,
447 const DBusString *username)
449 DBusUserDatabase *db;
450 const DBusUserInfo *info;
452 _dbus_user_database_lock_system ();
454 db = _dbus_user_database_get_system ();
457 _dbus_user_database_unlock_system ();
461 if (!_dbus_user_database_get_username (db, username,
464 _dbus_user_database_unlock_system ();
468 if (!_dbus_credentials_add_unix_uid(credentials, info->uid))
470 _dbus_user_database_unlock_system ();
474 _dbus_user_database_unlock_system ();
479 * Creates a new user database object used to look up and
480 * cache user information.
481 * @returns new database, or #NULL on out of memory
484 _dbus_user_database_new (void)
486 DBusUserDatabase *db;
488 db = dbus_new0 (DBusUserDatabase, 1);
494 db->users = _dbus_hash_table_new (DBUS_HASH_ULONG,
495 NULL, (DBusFreeFunction) _dbus_user_info_free_allocated);
497 if (db->users == NULL)
500 db->groups = _dbus_hash_table_new (DBUS_HASH_ULONG,
501 NULL, (DBusFreeFunction) _dbus_group_info_free_allocated);
503 if (db->groups == NULL)
506 db->users_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
508 if (db->users_by_name == NULL)
511 db->groups_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
513 if (db->groups_by_name == NULL)
519 _dbus_user_database_unref (db);
524 * Flush all information out of the user database.
527 _dbus_user_database_flush (DBusUserDatabase *db)
529 _dbus_hash_table_remove_all(db->users_by_name);
530 _dbus_hash_table_remove_all(db->groups_by_name);
531 _dbus_hash_table_remove_all(db->users);
532 _dbus_hash_table_remove_all(db->groups);
535 #ifdef DBUS_BUILD_TESTS
537 * Increments refcount of user database.
538 * @param db the database
539 * @returns the database
542 _dbus_user_database_ref (DBusUserDatabase *db)
544 _dbus_assert (db->refcount > 0);
550 #endif /* DBUS_BUILD_TESTS */
553 * Decrements refcount of user database.
554 * @param db the database
557 _dbus_user_database_unref (DBusUserDatabase *db)
559 _dbus_assert (db->refcount > 0);
562 if (db->refcount == 0)
565 _dbus_hash_table_unref (db->users);
568 _dbus_hash_table_unref (db->groups);
570 if (db->users_by_name)
571 _dbus_hash_table_unref (db->users_by_name);
573 if (db->groups_by_name)
574 _dbus_hash_table_unref (db->groups_by_name);
581 * Gets the user information for the given UID,
582 * returned user info should not be freed.
584 * @param db user database
585 * @param uid the user ID
586 * @param info return location for const ref to user info
587 * @param error error location
588 * @returns #FALSE if error is set
591 _dbus_user_database_get_uid (DBusUserDatabase *db,
593 const DBusUserInfo **info,
596 *info = _dbus_user_database_lookup (db, uid, NULL, error);
597 return *info != NULL;
601 * Gets the user information for the given username.
603 * @param db user database
604 * @param username the user name
605 * @param info return location for const ref to user info
606 * @param error error location
607 * @returns #FALSE if error is set
610 _dbus_user_database_get_username (DBusUserDatabase *db,
611 const DBusString *username,
612 const DBusUserInfo **info,
615 *info = _dbus_user_database_lookup (db, DBUS_UID_UNSET, username, error);
616 return *info != NULL;
621 /* Tests in dbus-userdb-util.c */