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
24 #define DBUS_USERDB_INCLUDES_PRIVATE 1
25 #include "dbus-userdb.h"
26 #include "dbus-hash.h"
27 #include "dbus-test.h"
28 #include "dbus-internals.h"
29 #include "dbus-protocol.h"
30 #include "dbus-credentials.h"
34 * @addtogroup DBusInternalsUtils
39 * Frees the given #DBusUserInfo's members with _dbus_user_info_free()
40 * and also calls dbus_free() on the block itself
42 * @param info the info
45 _dbus_user_info_free_allocated (DBusUserInfo *info)
47 if (info == NULL) /* hash table will pass NULL */
50 _dbus_user_info_free (info);
55 * Frees the given #DBusGroupInfo's members with _dbus_group_info_free()
56 * and also calls dbus_free() on the block itself
58 * @param info the info
61 _dbus_group_info_free_allocated (DBusGroupInfo *info)
63 if (info == NULL) /* hash table will pass NULL */
66 _dbus_group_info_free (info);
71 * Frees the members of info
72 * (but not info itself)
73 * @param info the user info struct
76 _dbus_user_info_free (DBusUserInfo *info)
78 dbus_free (info->group_ids);
79 dbus_free (info->username);
80 dbus_free (info->homedir);
84 * Frees the members of info (but not info itself).
86 * @param info the group info
89 _dbus_group_info_free (DBusGroupInfo *info)
91 dbus_free (info->groupname);
95 * Checks if a given string is actually a number
96 * and converts it if it is
98 * @param str the string to check
99 * @param num the memory location of the unsigned long to fill in
100 * @returns TRUE if str is a number and num is filled in
103 _dbus_is_a_number (const DBusString *str,
108 if (_dbus_string_parse_uint (str, 0, num, &end) &&
109 end == _dbus_string_get_length (str))
116 * Looks up a uid or username in the user database. Only one of name
117 * or UID can be provided. There are wrapper functions for this that
118 * are better to use, this one does no locking or anything on the
119 * database and otherwise sort of sucks.
121 * @param db the database
122 * @param uid the user ID or #DBUS_UID_UNSET
123 * @param username username or #NULL
124 * @param error error to fill in
125 * @returns the entry in the database
128 _dbus_user_database_lookup (DBusUserDatabase *db,
130 const DBusString *username,
135 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
136 _dbus_assert (uid != DBUS_UID_UNSET || username != NULL);
138 /* See if the username is really a number */
139 if (uid == DBUS_UID_UNSET)
143 if (_dbus_is_a_number (username, &n))
147 #ifdef DBUS_ENABLE_USERDB_CACHE
148 if (uid != DBUS_UID_UNSET)
149 info = _dbus_hash_table_lookup_uintptr (db->users, uid);
151 info = _dbus_hash_table_lookup_string (db->users_by_name, _dbus_string_get_const_data (username));
155 _dbus_verbose ("Using cache for UID "DBUS_UID_FORMAT" information\n",
164 if (uid != DBUS_UID_UNSET)
165 _dbus_verbose ("No cache for UID "DBUS_UID_FORMAT"\n",
168 _dbus_verbose ("No cache for user \"%s\"\n",
169 _dbus_string_get_const_data (username));
171 info = dbus_new0 (DBusUserInfo, 1);
174 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
178 if (uid != DBUS_UID_UNSET)
180 if (!_dbus_user_info_fill_uid (info, uid, error))
182 _DBUS_ASSERT_ERROR_IS_SET (error);
183 _dbus_user_info_free_allocated (info);
189 if (!_dbus_user_info_fill (info, username, error))
191 _DBUS_ASSERT_ERROR_IS_SET (error);
192 _dbus_user_info_free_allocated (info);
197 /* be sure we don't use these after here */
198 uid = DBUS_UID_UNSET;
201 /* insert into hash */
202 if (!_dbus_hash_table_insert_uintptr (db->users, info->uid, info))
204 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
205 _dbus_user_info_free_allocated (info);
209 if (!_dbus_hash_table_insert_string (db->users_by_name,
213 _dbus_hash_table_remove_uintptr (db->users, info->uid);
214 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
222 static dbus_bool_t database_locked = FALSE;
223 static DBusUserDatabase *system_db = NULL;
224 static DBusString process_username;
225 static DBusString process_homedir;
228 shutdown_system_db (void *data)
230 if (system_db != NULL)
231 _dbus_user_database_unref (system_db);
233 _dbus_string_free (&process_username);
234 _dbus_string_free (&process_homedir);
238 init_system_db (void)
240 _dbus_assert (database_locked);
242 if (system_db == NULL)
244 DBusError error = DBUS_ERROR_INIT;
245 const DBusUserInfo *info;
247 system_db = _dbus_user_database_new ();
248 if (system_db == NULL)
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 if (_DBUS_LOCK (system_users))
314 database_locked = TRUE;
324 * Unlocks global system user database.
327 _dbus_user_database_unlock_system (void)
329 database_locked = FALSE;
330 _DBUS_UNLOCK (system_users);
334 * Gets the system global user database;
335 * must be called with lock held (_dbus_user_database_lock_system()).
337 * @returns the database or #NULL if no memory
340 _dbus_user_database_get_system (void)
342 _dbus_assert (database_locked);
350 * Flushes the system global user database;
353 _dbus_user_database_flush_system (void)
355 if (!_dbus_user_database_lock_system ())
357 /* nothing to flush */
361 if (system_db != NULL)
362 _dbus_user_database_flush (system_db);
364 _dbus_user_database_unlock_system ();
368 * Gets username of user owning current process. The returned string
369 * is valid until dbus_shutdown() is called.
371 * @param username place to store pointer to username
372 * @returns #FALSE if no memory
375 _dbus_username_from_current_process (const DBusString **username)
377 if (!_dbus_user_database_lock_system ())
380 if (!init_system_db ())
382 _dbus_user_database_unlock_system ();
385 *username = &process_username;
386 _dbus_user_database_unlock_system ();
392 * Gets homedir of user owning current process. The returned string
393 * is valid until dbus_shutdown() is called.
395 * @param homedir place to store pointer to homedir
396 * @returns #FALSE if no memory
399 _dbus_homedir_from_current_process (const DBusString **homedir)
401 if (!_dbus_user_database_lock_system ())
404 if (!init_system_db ())
406 _dbus_user_database_unlock_system ();
409 *homedir = &process_homedir;
410 _dbus_user_database_unlock_system ();
416 * Gets the home directory for the given user.
418 * @param username the username
419 * @param homedir string to append home directory to
420 * @returns #TRUE if user existed and we appended their homedir
423 _dbus_homedir_from_username (const DBusString *username,
426 DBusUserDatabase *db;
427 const DBusUserInfo *info;
429 /* FIXME: this can't distinguish ENOMEM from other errors */
430 if (!_dbus_user_database_lock_system ())
433 db = _dbus_user_database_get_system ();
436 _dbus_user_database_unlock_system ();
440 if (!_dbus_user_database_get_username (db, username,
443 _dbus_user_database_unlock_system ();
447 if (!_dbus_string_append (homedir, info->homedir))
449 _dbus_user_database_unlock_system ();
453 _dbus_user_database_unlock_system ();
458 * Gets the home directory for the given user.
461 * @param homedir string to append home directory to
462 * @returns #TRUE if user existed and we appended their homedir
465 _dbus_homedir_from_uid (dbus_uid_t uid,
468 DBusUserDatabase *db;
469 const DBusUserInfo *info;
471 /* FIXME: this can't distinguish ENOMEM from other errors */
472 if (!_dbus_user_database_lock_system ())
475 db = _dbus_user_database_get_system ();
478 _dbus_user_database_unlock_system ();
482 if (!_dbus_user_database_get_uid (db, uid,
485 _dbus_user_database_unlock_system ();
489 if (!_dbus_string_append (homedir, info->homedir))
491 _dbus_user_database_unlock_system ();
495 _dbus_user_database_unlock_system ();
500 * Adds the credentials corresponding to the given username.
502 * Used among other purposes to parses a desired identity provided
503 * from a client in the auth protocol. On UNIX this means parsing a
504 * UID, on Windows probably parsing an SID string.
506 * @todo this is broken because it treats OOM and parse error
507 * the same way. Needs a #DBusError.
509 * @param credentials credentials to fill in
510 * @param username the username
511 * @returns #TRUE if the username existed and we got some credentials
514 _dbus_credentials_add_from_user (DBusCredentials *credentials,
515 const DBusString *username)
517 DBusUserDatabase *db;
518 const DBusUserInfo *info;
520 /* FIXME: this can't distinguish ENOMEM from other errors */
521 if (!_dbus_user_database_lock_system ())
524 db = _dbus_user_database_get_system ();
527 _dbus_user_database_unlock_system ();
531 if (!_dbus_user_database_get_username (db, username,
534 _dbus_user_database_unlock_system ();
538 if (!_dbus_credentials_add_unix_uid(credentials, info->uid))
540 _dbus_user_database_unlock_system ();
544 _dbus_user_database_unlock_system ();
549 * Creates a new user database object used to look up and
550 * cache user information.
551 * @returns new database, or #NULL on out of memory
554 _dbus_user_database_new (void)
556 DBusUserDatabase *db;
558 db = dbus_new0 (DBusUserDatabase, 1);
564 db->users = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
565 NULL, (DBusFreeFunction) _dbus_user_info_free_allocated);
567 if (db->users == NULL)
570 db->groups = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
571 NULL, (DBusFreeFunction) _dbus_group_info_free_allocated);
573 if (db->groups == NULL)
576 db->users_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
578 if (db->users_by_name == NULL)
581 db->groups_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
583 if (db->groups_by_name == NULL)
589 _dbus_user_database_unref (db);
594 * Flush all information out of the user database.
597 _dbus_user_database_flush (DBusUserDatabase *db)
599 _dbus_hash_table_remove_all(db->users_by_name);
600 _dbus_hash_table_remove_all(db->groups_by_name);
601 _dbus_hash_table_remove_all(db->users);
602 _dbus_hash_table_remove_all(db->groups);
605 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
607 * Increments refcount of user database.
608 * @param db the database
609 * @returns the database
612 _dbus_user_database_ref (DBusUserDatabase *db)
614 _dbus_assert (db->refcount > 0);
620 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */
623 * Decrements refcount of user database.
624 * @param db the database
627 _dbus_user_database_unref (DBusUserDatabase *db)
629 _dbus_assert (db->refcount > 0);
632 if (db->refcount == 0)
635 _dbus_hash_table_unref (db->users);
638 _dbus_hash_table_unref (db->groups);
640 if (db->users_by_name)
641 _dbus_hash_table_unref (db->users_by_name);
643 if (db->groups_by_name)
644 _dbus_hash_table_unref (db->groups_by_name);
651 * Gets the user information for the given UID,
652 * returned user info should not be freed.
654 * @param db user database
655 * @param uid the user ID
656 * @param info return location for const ref to user info
657 * @param error error location
658 * @returns #FALSE if error is set
661 _dbus_user_database_get_uid (DBusUserDatabase *db,
663 const DBusUserInfo **info,
666 *info = _dbus_user_database_lookup (db, uid, NULL, error);
667 return *info != NULL;
671 * Gets the user information for the given username.
673 * @param db user database
674 * @param username the user name
675 * @param info return location for const ref to user info
676 * @param error error location
677 * @returns #FALSE if error is set
680 _dbus_user_database_get_username (DBusUserDatabase *db,
681 const DBusString *username,
682 const DBusUserInfo **info,
685 *info = _dbus_user_database_lookup (db, DBUS_UID_UNSET, username, error);
686 return *info != NULL;
691 /* Tests in dbus-userdb-util.c */