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"
32 * @addtogroup DBusInternalsUtils
37 * Frees the given #DBusUserInfo's members with _dbus_user_info_free()
38 * and also calls dbus_free() on the block itself
40 * @param info the info
43 _dbus_user_info_free_allocated (DBusUserInfo *info)
45 if (info == NULL) /* hash table will pass NULL */
48 _dbus_user_info_free (info);
53 * Frees the given #DBusGroupInfo's members with _dbus_group_info_free()
54 * and also calls dbus_free() on the block itself
56 * @param info the info
59 _dbus_group_info_free_allocated (DBusGroupInfo *info)
61 if (info == NULL) /* hash table will pass NULL */
64 _dbus_group_info_free (info);
69 * Checks if a given string is actually a number
70 * and converts it if it is
72 * @param str the string to check
73 * @param num the memory location of the unsigned long to fill in
74 * @returns TRUE if str is a number and num is filled in
77 _dbus_is_a_number (const DBusString *str,
82 if (_dbus_string_parse_uint (str, 0, num, &end) &&
83 end == _dbus_string_get_length (str))
90 * Looks up a uid or username in the user database. Only one of name
91 * or UID can be provided. There are wrapper functions for this that
92 * are better to use, this one does no locking or anything on the
93 * database and otherwise sort of sucks.
95 * @param db the database
96 * @param uid the user ID or #DBUS_UID_UNSET
97 * @param username username or #NULL
98 * @param error error to fill in
99 * @returns the entry in the database
102 _dbus_user_database_lookup (DBusUserDatabase *db,
104 const DBusString *username,
109 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
110 _dbus_assert (uid != DBUS_UID_UNSET || username != NULL);
112 /* See if the username is really a number */
113 if (uid == DBUS_UID_UNSET)
117 if (_dbus_is_a_number (username, &n))
121 if (uid != DBUS_UID_UNSET)
122 info = _dbus_hash_table_lookup_ulong (db->users, uid);
124 info = _dbus_hash_table_lookup_string (db->users_by_name, _dbus_string_get_const_data (username));
128 _dbus_verbose ("Using cache for UID "DBUS_UID_FORMAT" information\n",
134 if (uid != DBUS_UID_UNSET)
135 _dbus_verbose ("No cache for UID "DBUS_UID_FORMAT"\n",
138 _dbus_verbose ("No cache for user \"%s\"\n",
139 _dbus_string_get_const_data (username));
141 info = dbus_new0 (DBusUserInfo, 1);
144 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
148 if (uid != DBUS_UID_UNSET)
150 if (!_dbus_user_info_fill_uid (info, uid, error))
152 _DBUS_ASSERT_ERROR_IS_SET (error);
153 _dbus_user_info_free_allocated (info);
159 if (!_dbus_user_info_fill (info, username, error))
161 _DBUS_ASSERT_ERROR_IS_SET (error);
162 _dbus_user_info_free_allocated (info);
167 /* be sure we don't use these after here */
168 uid = DBUS_UID_UNSET;
171 /* insert into hash */
172 if (!_dbus_hash_table_insert_ulong (db->users, info->uid, info))
174 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
175 _dbus_user_info_free_allocated (info);
179 if (!_dbus_hash_table_insert_string (db->users_by_name,
183 _dbus_hash_table_remove_ulong (db->users, info->uid);
184 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
192 _DBUS_DEFINE_GLOBAL_LOCK(system_users);
193 static dbus_bool_t database_locked = FALSE;
194 static DBusUserDatabase *system_db = NULL;
195 static DBusString process_username;
196 static DBusString process_homedir;
199 shutdown_system_db (void *data)
201 _dbus_user_database_unref (system_db);
203 _dbus_string_free (&process_username);
204 _dbus_string_free (&process_homedir);
208 init_system_db (void)
210 _dbus_assert (database_locked);
212 if (system_db == NULL)
215 const DBusUserInfo *info;
217 system_db = _dbus_user_database_new ();
218 if (system_db == NULL)
221 dbus_error_init (&error);
223 if (!_dbus_user_database_get_uid (system_db,
228 _dbus_user_database_unref (system_db);
231 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
233 dbus_error_free (&error);
238 /* This really should not happen. */
239 _dbus_warn ("Could not get password database information for UID of current process: %s\n",
241 dbus_error_free (&error);
246 if (!_dbus_string_init (&process_username))
248 _dbus_user_database_unref (system_db);
253 if (!_dbus_string_init (&process_homedir))
255 _dbus_string_free (&process_username);
256 _dbus_user_database_unref (system_db);
261 if (!_dbus_string_append (&process_username,
263 !_dbus_string_append (&process_homedir,
265 !_dbus_register_shutdown_func (shutdown_system_db, NULL))
267 _dbus_string_free (&process_username);
268 _dbus_string_free (&process_homedir);
269 _dbus_user_database_unref (system_db);
279 * Locks global system user database.
282 _dbus_user_database_lock_system (void)
284 _DBUS_LOCK (system_users);
285 database_locked = TRUE;
289 * Unlocks global system user database.
292 _dbus_user_database_unlock_system (void)
294 database_locked = FALSE;
295 _DBUS_UNLOCK (system_users);
299 * Gets the system global user database;
300 * must be called with lock held (_dbus_user_database_lock_system()).
302 * @returns the database or #NULL if no memory
305 _dbus_user_database_get_system (void)
307 _dbus_assert (database_locked);
315 * Flushes the system global user database;
318 _dbus_user_database_flush_system (void)
320 _dbus_user_database_lock_system ();
322 _dbus_user_database_flush (system_db);
324 _dbus_user_database_unlock_system ();
328 * Gets username of user owning current process. The returned string
329 * is valid until dbus_shutdown() is called.
331 * @param username place to store pointer to username
332 * @returns #FALSE if no memory
335 _dbus_username_from_current_process (const DBusString **username)
337 _dbus_user_database_lock_system ();
338 if (!init_system_db ())
340 _dbus_user_database_unlock_system ();
343 *username = &process_username;
344 _dbus_user_database_unlock_system ();
350 * Gets homedir of user owning current process. The returned string
351 * is valid until dbus_shutdown() is called.
353 * @param homedir place to store pointer to homedir
354 * @returns #FALSE if no memory
357 _dbus_homedir_from_current_process (const DBusString **homedir)
359 _dbus_user_database_lock_system ();
360 if (!init_system_db ())
362 _dbus_user_database_unlock_system ();
365 *homedir = &process_homedir;
366 _dbus_user_database_unlock_system ();
372 * Gets the home directory for the given user.
374 * @param username the username
375 * @param homedir string to append home directory to
376 * @returns #TRUE if user existed and we appended their homedir
379 _dbus_homedir_from_username (const DBusString *username,
382 DBusUserDatabase *db;
383 const DBusUserInfo *info;
384 _dbus_user_database_lock_system ();
386 db = _dbus_user_database_get_system ();
389 _dbus_user_database_unlock_system ();
393 if (!_dbus_user_database_get_username (db, username,
396 _dbus_user_database_unlock_system ();
400 if (!_dbus_string_append (homedir, info->homedir))
402 _dbus_user_database_unlock_system ();
406 _dbus_user_database_unlock_system ();
411 * Gets the credentials corresponding to the given username.
413 * @param username the username
414 * @param credentials credentials to fill in
415 * @returns #TRUE if the username existed and we got some credentials
418 _dbus_credentials_from_username (const DBusString *username,
419 DBusCredentials *credentials)
421 DBusUserDatabase *db;
422 const DBusUserInfo *info;
423 _dbus_user_database_lock_system ();
425 db = _dbus_user_database_get_system ();
428 _dbus_user_database_unlock_system ();
432 if (!_dbus_user_database_get_username (db, username,
435 _dbus_user_database_unlock_system ();
439 credentials->pid = DBUS_PID_UNSET;
440 credentials->uid = info->uid;
441 credentials->gid = info->primary_gid;
443 _dbus_user_database_unlock_system ();
448 * Creates a new user database object used to look up and
449 * cache user information.
450 * @returns new database, or #NULL on out of memory
453 _dbus_user_database_new (void)
455 DBusUserDatabase *db;
457 db = dbus_new0 (DBusUserDatabase, 1);
463 db->users = _dbus_hash_table_new (DBUS_HASH_ULONG,
464 NULL, (DBusFreeFunction) _dbus_user_info_free_allocated);
466 if (db->users == NULL)
469 db->groups = _dbus_hash_table_new (DBUS_HASH_ULONG,
470 NULL, (DBusFreeFunction) _dbus_group_info_free_allocated);
472 if (db->groups == NULL)
475 db->users_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
477 if (db->users_by_name == NULL)
480 db->groups_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
482 if (db->groups_by_name == NULL)
488 _dbus_user_database_unref (db);
493 * Flush all information out of the user database.
496 _dbus_user_database_flush (DBusUserDatabase *db)
498 _dbus_hash_table_remove_all(db->users_by_name);
499 _dbus_hash_table_remove_all(db->groups_by_name);
500 _dbus_hash_table_remove_all(db->users);
501 _dbus_hash_table_remove_all(db->groups);
504 #ifdef DBUS_BUILD_TESTS
506 * Increments refcount of user database.
507 * @param db the database
508 * @returns the database
511 _dbus_user_database_ref (DBusUserDatabase *db)
513 _dbus_assert (db->refcount > 0);
519 #endif /* DBUS_BUILD_TESTS */
522 * Decrements refcount of user database.
523 * @param db the database
526 _dbus_user_database_unref (DBusUserDatabase *db)
528 _dbus_assert (db->refcount > 0);
531 if (db->refcount == 0)
534 _dbus_hash_table_unref (db->users);
537 _dbus_hash_table_unref (db->groups);
539 if (db->users_by_name)
540 _dbus_hash_table_unref (db->users_by_name);
542 if (db->groups_by_name)
543 _dbus_hash_table_unref (db->groups_by_name);
550 * Gets the user information for the given UID,
551 * returned user info should not be freed.
553 * @param db user database
554 * @param uid the user ID
555 * @param info return location for const ref to user info
556 * @param error error location
557 * @returns #FALSE if error is set
560 _dbus_user_database_get_uid (DBusUserDatabase *db,
562 const DBusUserInfo **info,
565 *info = _dbus_user_database_lookup (db, uid, NULL, error);
566 return *info != NULL;
570 * Gets the user information for the given username.
572 * @param db user database
573 * @param username the user name
574 * @param info return location for const ref to user info
575 * @param error error location
576 * @returns #FALSE if error is set
579 _dbus_user_database_get_username (DBusUserDatabase *db,
580 const DBusString *username,
581 const DBusUserInfo **info,
584 *info = _dbus_user_database_lookup (db, DBUS_UID_UNSET, username, error);
585 return *info != NULL;
590 /* Tests in dbus-userdb-util.c */