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 _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 if (system_db != NULL)
351 _dbus_user_database_flush (system_db);
353 _dbus_user_database_unlock_system ();
357 * Gets username of user owning current process. The returned string
358 * is valid until dbus_shutdown() is called.
360 * @param username place to store pointer to username
361 * @returns #FALSE if no memory
364 _dbus_username_from_current_process (const DBusString **username)
366 _dbus_user_database_lock_system ();
367 if (!init_system_db ())
369 _dbus_user_database_unlock_system ();
372 *username = &process_username;
373 _dbus_user_database_unlock_system ();
379 * Gets homedir of user owning current process. The returned string
380 * is valid until dbus_shutdown() is called.
382 * @param homedir place to store pointer to homedir
383 * @returns #FALSE if no memory
386 _dbus_homedir_from_current_process (const DBusString **homedir)
388 _dbus_user_database_lock_system ();
389 if (!init_system_db ())
391 _dbus_user_database_unlock_system ();
394 *homedir = &process_homedir;
395 _dbus_user_database_unlock_system ();
401 * Gets the home directory for the given user.
403 * @param username the username
404 * @param homedir string to append home directory to
405 * @returns #TRUE if user existed and we appended their homedir
408 _dbus_homedir_from_username (const DBusString *username,
411 DBusUserDatabase *db;
412 const DBusUserInfo *info;
413 _dbus_user_database_lock_system ();
415 db = _dbus_user_database_get_system ();
418 _dbus_user_database_unlock_system ();
422 if (!_dbus_user_database_get_username (db, username,
425 _dbus_user_database_unlock_system ();
429 if (!_dbus_string_append (homedir, info->homedir))
431 _dbus_user_database_unlock_system ();
435 _dbus_user_database_unlock_system ();
440 * Gets the home directory for the given user.
443 * @param homedir string to append home directory to
444 * @returns #TRUE if user existed and we appended their homedir
447 _dbus_homedir_from_uid (dbus_uid_t uid,
450 DBusUserDatabase *db;
451 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_uid (db, uid,
464 _dbus_user_database_unlock_system ();
468 if (!_dbus_string_append (homedir, info->homedir))
470 _dbus_user_database_unlock_system ();
474 _dbus_user_database_unlock_system ();
479 * Adds the credentials corresponding to the given username.
481 * Used among other purposes to parses a desired identity provided
482 * from a client in the auth protocol. On UNIX this means parsing a
483 * UID, on Windows probably parsing an SID string.
485 * @todo this is broken because it treats OOM and parse error
486 * the same way. Needs a #DBusError.
488 * @param credentials credentials to fill in
489 * @param username the username
490 * @returns #TRUE if the username existed and we got some credentials
493 _dbus_credentials_add_from_user (DBusCredentials *credentials,
494 const DBusString *username)
496 DBusUserDatabase *db;
497 const DBusUserInfo *info;
499 _dbus_user_database_lock_system ();
501 db = _dbus_user_database_get_system ();
504 _dbus_user_database_unlock_system ();
508 if (!_dbus_user_database_get_username (db, username,
511 _dbus_user_database_unlock_system ();
515 if (!_dbus_credentials_add_unix_uid(credentials, info->uid))
517 _dbus_user_database_unlock_system ();
521 _dbus_user_database_unlock_system ();
526 * Creates a new user database object used to look up and
527 * cache user information.
528 * @returns new database, or #NULL on out of memory
531 _dbus_user_database_new (void)
533 DBusUserDatabase *db;
535 db = dbus_new0 (DBusUserDatabase, 1);
541 db->users = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
542 NULL, (DBusFreeFunction) _dbus_user_info_free_allocated);
544 if (db->users == NULL)
547 db->groups = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
548 NULL, (DBusFreeFunction) _dbus_group_info_free_allocated);
550 if (db->groups == NULL)
553 db->users_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
555 if (db->users_by_name == NULL)
558 db->groups_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
560 if (db->groups_by_name == NULL)
566 _dbus_user_database_unref (db);
571 * Flush all information out of the user database.
574 _dbus_user_database_flush (DBusUserDatabase *db)
576 _dbus_hash_table_remove_all(db->users_by_name);
577 _dbus_hash_table_remove_all(db->groups_by_name);
578 _dbus_hash_table_remove_all(db->users);
579 _dbus_hash_table_remove_all(db->groups);
582 #ifdef DBUS_BUILD_TESTS
584 * Increments refcount of user database.
585 * @param db the database
586 * @returns the database
589 _dbus_user_database_ref (DBusUserDatabase *db)
591 _dbus_assert (db->refcount > 0);
597 #endif /* DBUS_BUILD_TESTS */
600 * Decrements refcount of user database.
601 * @param db the database
604 _dbus_user_database_unref (DBusUserDatabase *db)
606 _dbus_assert (db->refcount > 0);
609 if (db->refcount == 0)
612 _dbus_hash_table_unref (db->users);
615 _dbus_hash_table_unref (db->groups);
617 if (db->users_by_name)
618 _dbus_hash_table_unref (db->users_by_name);
620 if (db->groups_by_name)
621 _dbus_hash_table_unref (db->groups_by_name);
628 * Gets the user information for the given UID,
629 * returned user info should not be freed.
631 * @param db user database
632 * @param uid the user ID
633 * @param info return location for const ref to user info
634 * @param error error location
635 * @returns #FALSE if error is set
638 _dbus_user_database_get_uid (DBusUserDatabase *db,
640 const DBusUserInfo **info,
643 *info = _dbus_user_database_lookup (db, uid, NULL, error);
644 return *info != NULL;
648 * Gets the user information for the given username.
650 * @param db user database
651 * @param username the user name
652 * @param info return location for const ref to user info
653 * @param error error location
654 * @returns #FALSE if error is set
657 _dbus_user_database_get_username (DBusUserDatabase *db,
658 const DBusString *username,
659 const DBusUserInfo **info,
662 *info = _dbus_user_database_lookup (db, DBUS_UID_UNSET, username, error);
663 return *info != NULL;
668 /* Tests in dbus-userdb-util.c */