1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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_USERDB_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 if (system_db != NULL)
230 _dbus_user_database_unref (system_db);
232 _dbus_string_free (&process_username);
233 _dbus_string_free (&process_homedir);
237 init_system_db (void)
239 _dbus_assert (database_locked);
241 if (system_db == NULL)
243 DBusError error = DBUS_ERROR_INIT;
244 const DBusUserInfo *info;
246 system_db = _dbus_user_database_new ();
247 if (system_db == NULL)
250 if (!_dbus_user_database_get_uid (system_db,
255 _dbus_user_database_unref (system_db);
258 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
260 dbus_error_free (&error);
265 /* This really should not happen. */
266 _dbus_warn ("Could not get password database information for UID of current process: %s\n",
268 dbus_error_free (&error);
273 if (!_dbus_string_init (&process_username))
275 _dbus_user_database_unref (system_db);
280 if (!_dbus_string_init (&process_homedir))
282 _dbus_string_free (&process_username);
283 _dbus_user_database_unref (system_db);
288 if (!_dbus_string_append (&process_username,
290 !_dbus_string_append (&process_homedir,
292 !_dbus_register_shutdown_func (shutdown_system_db, NULL))
294 _dbus_string_free (&process_username);
295 _dbus_string_free (&process_homedir);
296 _dbus_user_database_unref (system_db);
306 * Locks global system user database.
309 _dbus_user_database_lock_system (void)
311 _DBUS_LOCK (system_users);
312 database_locked = TRUE;
316 * Unlocks global system user database.
319 _dbus_user_database_unlock_system (void)
321 database_locked = FALSE;
322 _DBUS_UNLOCK (system_users);
326 * Gets the system global user database;
327 * must be called with lock held (_dbus_user_database_lock_system()).
329 * @returns the database or #NULL if no memory
332 _dbus_user_database_get_system (void)
334 _dbus_assert (database_locked);
342 * Flushes the system global user database;
345 _dbus_user_database_flush_system (void)
347 _dbus_user_database_lock_system ();
349 if (system_db != NULL)
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 * Used among other purposes to parses a desired identity provided
481 * from a client in the auth protocol. On UNIX this means parsing a
482 * UID, on Windows probably parsing an SID string.
484 * @todo this is broken because it treats OOM and parse error
485 * the same way. Needs a #DBusError.
487 * @param credentials credentials to fill in
488 * @param username the username
489 * @returns #TRUE if the username existed and we got some credentials
492 _dbus_credentials_add_from_user (DBusCredentials *credentials,
493 const DBusString *username)
495 DBusUserDatabase *db;
496 const DBusUserInfo *info;
498 _dbus_user_database_lock_system ();
500 db = _dbus_user_database_get_system ();
503 _dbus_user_database_unlock_system ();
507 if (!_dbus_user_database_get_username (db, username,
510 _dbus_user_database_unlock_system ();
514 if (!_dbus_credentials_add_unix_uid(credentials, info->uid))
516 _dbus_user_database_unlock_system ();
520 _dbus_user_database_unlock_system ();
525 * Creates a new user database object used to look up and
526 * cache user information.
527 * @returns new database, or #NULL on out of memory
530 _dbus_user_database_new (void)
532 DBusUserDatabase *db;
534 db = dbus_new0 (DBusUserDatabase, 1);
540 db->users = _dbus_hash_table_new (DBUS_HASH_ULONG,
541 NULL, (DBusFreeFunction) _dbus_user_info_free_allocated);
543 if (db->users == NULL)
546 db->groups = _dbus_hash_table_new (DBUS_HASH_ULONG,
547 NULL, (DBusFreeFunction) _dbus_group_info_free_allocated);
549 if (db->groups == NULL)
552 db->users_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
554 if (db->users_by_name == NULL)
557 db->groups_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
559 if (db->groups_by_name == NULL)
565 _dbus_user_database_unref (db);
570 * Flush all information out of the user database.
573 _dbus_user_database_flush (DBusUserDatabase *db)
575 _dbus_hash_table_remove_all(db->users_by_name);
576 _dbus_hash_table_remove_all(db->groups_by_name);
577 _dbus_hash_table_remove_all(db->users);
578 _dbus_hash_table_remove_all(db->groups);
581 #ifdef DBUS_BUILD_TESTS
583 * Increments refcount of user database.
584 * @param db the database
585 * @returns the database
588 _dbus_user_database_ref (DBusUserDatabase *db)
590 _dbus_assert (db->refcount > 0);
596 #endif /* DBUS_BUILD_TESTS */
599 * Decrements refcount of user database.
600 * @param db the database
603 _dbus_user_database_unref (DBusUserDatabase *db)
605 _dbus_assert (db->refcount > 0);
608 if (db->refcount == 0)
611 _dbus_hash_table_unref (db->users);
614 _dbus_hash_table_unref (db->groups);
616 if (db->users_by_name)
617 _dbus_hash_table_unref (db->users_by_name);
619 if (db->groups_by_name)
620 _dbus_hash_table_unref (db->groups_by_name);
627 * Gets the user information for the given UID,
628 * returned user info should not be freed.
630 * @param db user database
631 * @param uid the user ID
632 * @param info return location for const ref to user info
633 * @param error error location
634 * @returns #FALSE if error is set
637 _dbus_user_database_get_uid (DBusUserDatabase *db,
639 const DBusUserInfo **info,
642 *info = _dbus_user_database_lookup (db, uid, NULL, error);
643 return *info != NULL;
647 * Gets the user information for the given username.
649 * @param db user database
650 * @param username the user name
651 * @param info return location for const ref to user info
652 * @param error error location
653 * @returns #FALSE if error is set
656 _dbus_user_database_get_username (DBusUserDatabase *db,
657 const DBusString *username,
658 const DBusUserInfo **info,
661 *info = _dbus_user_database_lookup (db, DBUS_UID_UNSET, username, error);
662 return *info != NULL;
667 /* Tests in dbus-userdb-util.c */