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., 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)
242 DBusError error = DBUS_ERROR_INIT;
243 const DBusUserInfo *info;
245 system_db = _dbus_user_database_new ();
246 if (system_db == NULL)
249 if (!_dbus_user_database_get_uid (system_db,
254 _dbus_user_database_unref (system_db);
257 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
259 dbus_error_free (&error);
264 /* This really should not happen. */
265 _dbus_warn ("Could not get password database information for UID of current process: %s\n",
267 dbus_error_free (&error);
272 if (!_dbus_string_init (&process_username))
274 _dbus_user_database_unref (system_db);
279 if (!_dbus_string_init (&process_homedir))
281 _dbus_string_free (&process_username);
282 _dbus_user_database_unref (system_db);
287 if (!_dbus_string_append (&process_username,
289 !_dbus_string_append (&process_homedir,
291 !_dbus_register_shutdown_func (shutdown_system_db, NULL))
293 _dbus_string_free (&process_username);
294 _dbus_string_free (&process_homedir);
295 _dbus_user_database_unref (system_db);
305 * Locks global system user database.
308 _dbus_user_database_lock_system (void)
310 _DBUS_LOCK (system_users);
311 database_locked = TRUE;
315 * Unlocks global system user database.
318 _dbus_user_database_unlock_system (void)
320 database_locked = FALSE;
321 _DBUS_UNLOCK (system_users);
325 * Gets the system global user database;
326 * must be called with lock held (_dbus_user_database_lock_system()).
328 * @returns the database or #NULL if no memory
331 _dbus_user_database_get_system (void)
333 _dbus_assert (database_locked);
341 * Flushes the system global user database;
344 _dbus_user_database_flush_system (void)
346 _dbus_user_database_lock_system ();
348 _dbus_user_database_flush (system_db);
350 _dbus_user_database_unlock_system ();
354 * Gets username of user owning current process. The returned string
355 * is valid until dbus_shutdown() is called.
357 * @param username place to store pointer to username
358 * @returns #FALSE if no memory
361 _dbus_username_from_current_process (const DBusString **username)
363 _dbus_user_database_lock_system ();
364 if (!init_system_db ())
366 _dbus_user_database_unlock_system ();
369 *username = &process_username;
370 _dbus_user_database_unlock_system ();
376 * Gets homedir of user owning current process. The returned string
377 * is valid until dbus_shutdown() is called.
379 * @param homedir place to store pointer to homedir
380 * @returns #FALSE if no memory
383 _dbus_homedir_from_current_process (const DBusString **homedir)
385 _dbus_user_database_lock_system ();
386 if (!init_system_db ())
388 _dbus_user_database_unlock_system ();
391 *homedir = &process_homedir;
392 _dbus_user_database_unlock_system ();
398 * Gets the home directory for the given user.
400 * @param username the username
401 * @param homedir string to append home directory to
402 * @returns #TRUE if user existed and we appended their homedir
405 _dbus_homedir_from_username (const DBusString *username,
408 DBusUserDatabase *db;
409 const DBusUserInfo *info;
410 _dbus_user_database_lock_system ();
412 db = _dbus_user_database_get_system ();
415 _dbus_user_database_unlock_system ();
419 if (!_dbus_user_database_get_username (db, username,
422 _dbus_user_database_unlock_system ();
426 if (!_dbus_string_append (homedir, info->homedir))
428 _dbus_user_database_unlock_system ();
432 _dbus_user_database_unlock_system ();
437 * Gets the home directory for the given user.
440 * @param homedir string to append home directory to
441 * @returns #TRUE if user existed and we appended their homedir
444 _dbus_homedir_from_uid (dbus_uid_t uid,
447 DBusUserDatabase *db;
448 const DBusUserInfo *info;
449 _dbus_user_database_lock_system ();
451 db = _dbus_user_database_get_system ();
454 _dbus_user_database_unlock_system ();
458 if (!_dbus_user_database_get_uid (db, uid,
461 _dbus_user_database_unlock_system ();
465 if (!_dbus_string_append (homedir, info->homedir))
467 _dbus_user_database_unlock_system ();
471 _dbus_user_database_unlock_system ();
476 * Adds the credentials corresponding to the given username.
478 * Used among other purposes to parses a desired identity provided
479 * from a client in the auth protocol. On UNIX this means parsing a
480 * UID, on Windows probably parsing an SID string.
482 * @todo this is broken because it treats OOM and parse error
483 * the same way. Needs a #DBusError.
485 * @param credentials credentials to fill in
486 * @param username the username
487 * @returns #TRUE if the username existed and we got some credentials
490 _dbus_credentials_add_from_user (DBusCredentials *credentials,
491 const DBusString *username)
493 DBusUserDatabase *db;
494 const DBusUserInfo *info;
496 _dbus_user_database_lock_system ();
498 db = _dbus_user_database_get_system ();
501 _dbus_user_database_unlock_system ();
505 if (!_dbus_user_database_get_username (db, username,
508 _dbus_user_database_unlock_system ();
512 if (!_dbus_credentials_add_unix_uid(credentials, info->uid))
514 _dbus_user_database_unlock_system ();
518 _dbus_user_database_unlock_system ();
523 * Creates a new user database object used to look up and
524 * cache user information.
525 * @returns new database, or #NULL on out of memory
528 _dbus_user_database_new (void)
530 DBusUserDatabase *db;
532 db = dbus_new0 (DBusUserDatabase, 1);
538 db->users = _dbus_hash_table_new (DBUS_HASH_ULONG,
539 NULL, (DBusFreeFunction) _dbus_user_info_free_allocated);
541 if (db->users == NULL)
544 db->groups = _dbus_hash_table_new (DBUS_HASH_ULONG,
545 NULL, (DBusFreeFunction) _dbus_group_info_free_allocated);
547 if (db->groups == NULL)
550 db->users_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
552 if (db->users_by_name == NULL)
555 db->groups_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
557 if (db->groups_by_name == NULL)
563 _dbus_user_database_unref (db);
568 * Flush all information out of the user database.
571 _dbus_user_database_flush (DBusUserDatabase *db)
573 _dbus_hash_table_remove_all(db->users_by_name);
574 _dbus_hash_table_remove_all(db->groups_by_name);
575 _dbus_hash_table_remove_all(db->users);
576 _dbus_hash_table_remove_all(db->groups);
579 #ifdef DBUS_BUILD_TESTS
581 * Increments refcount of user database.
582 * @param db the database
583 * @returns the database
586 _dbus_user_database_ref (DBusUserDatabase *db)
588 _dbus_assert (db->refcount > 0);
594 #endif /* DBUS_BUILD_TESTS */
597 * Decrements refcount of user database.
598 * @param db the database
601 _dbus_user_database_unref (DBusUserDatabase *db)
603 _dbus_assert (db->refcount > 0);
606 if (db->refcount == 0)
609 _dbus_hash_table_unref (db->users);
612 _dbus_hash_table_unref (db->groups);
614 if (db->users_by_name)
615 _dbus_hash_table_unref (db->users_by_name);
617 if (db->groups_by_name)
618 _dbus_hash_table_unref (db->groups_by_name);
625 * Gets the user information for the given UID,
626 * returned user info should not be freed.
628 * @param db user database
629 * @param uid the user ID
630 * @param info return location for const ref to user info
631 * @param error error location
632 * @returns #FALSE if error is set
635 _dbus_user_database_get_uid (DBusUserDatabase *db,
637 const DBusUserInfo **info,
640 *info = _dbus_user_database_lookup (db, uid, NULL, error);
641 return *info != NULL;
645 * Gets the user information for the given username.
647 * @param db user database
648 * @param username the user name
649 * @param info return location for const ref to user info
650 * @param error error location
651 * @returns #FALSE if error is set
654 _dbus_user_database_get_username (DBusUserDatabase *db,
655 const DBusString *username,
656 const DBusUserInfo **info,
659 *info = _dbus_user_database_lookup (db, DBUS_UID_UNSET, username, error);
660 return *info != NULL;
665 /* Tests in dbus-userdb-util.c */