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 _dbus_user_info_ref (DBusUserInfo *info)
41 _dbus_assert (info->refcount > 0);
42 _dbus_assert (info->refcount < SIZE_MAX);
48 * Decrements the reference count. If it reaches 0,
49 * frees the given #DBusUserInfo's members with _dbus_user_info_free()
50 * and also calls dbus_free() on the block itself
52 * @param info the info
55 _dbus_user_info_unref (DBusUserInfo *info)
57 if (info == NULL) /* hash table will pass NULL */
60 _dbus_assert (info->refcount > 0);
61 _dbus_assert (info->refcount < SIZE_MAX);
63 if (--info->refcount > 0)
66 _dbus_user_info_free (info);
71 * Decrements the reference count. If it reaches 0,
72 * frees the given #DBusGroupInfo's members with _dbus_group_info_free()
73 * and also calls dbus_free() on the block itself
75 * @param info the info
78 _dbus_group_info_unref (DBusGroupInfo *info)
80 if (info == NULL) /* hash table will pass NULL */
83 _dbus_assert (info->refcount > 0);
84 _dbus_assert (info->refcount < SIZE_MAX);
86 if (--info->refcount > 0)
89 _dbus_group_info_free (info);
94 * Frees the members of info
95 * (but not info itself)
96 * @param info the user info struct
99 _dbus_user_info_free (DBusUserInfo *info)
101 dbus_free (info->group_ids);
102 dbus_free (info->username);
103 dbus_free (info->homedir);
107 * Frees the members of info (but not info itself).
109 * @param info the group info
112 _dbus_group_info_free (DBusGroupInfo *info)
114 dbus_free (info->groupname);
118 * Checks if a given string is actually a number
119 * and converts it if it is
121 * @param str the string to check
122 * @param num the memory location of the unsigned long to fill in
123 * @returns TRUE if str is a number and num is filled in
126 _dbus_is_a_number (const DBusString *str,
131 if (_dbus_string_parse_uint (str, 0, num, &end) &&
132 end == _dbus_string_get_length (str))
139 * Looks up a uid or username in the user database. Only one of name
140 * or UID can be provided. There are wrapper functions for this that
141 * are better to use, this one does no locking or anything on the
142 * database and otherwise sort of sucks.
144 * @param db the database
145 * @param uid the user ID or #DBUS_UID_UNSET
146 * @param username username or #NULL
147 * @param error error to fill in
148 * @returns the entry in the database (borrowed, do not free)
151 _dbus_user_database_lookup (DBusUserDatabase *db,
153 const DBusString *username,
158 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
159 _dbus_assert (uid != DBUS_UID_UNSET || username != NULL);
161 /* See if the username is really a number */
162 if (uid == DBUS_UID_UNSET)
166 if (_dbus_is_a_number (username, &n))
170 if (uid != DBUS_UID_UNSET)
171 info = _dbus_hash_table_lookup_uintptr (db->users, uid);
173 info = _dbus_hash_table_lookup_string (db->users_by_name, _dbus_string_get_const_data (username));
177 _dbus_verbose ("Using cache for UID "DBUS_UID_FORMAT" information\n",
183 if (uid != DBUS_UID_UNSET)
184 _dbus_verbose ("No cache for UID "DBUS_UID_FORMAT"\n",
187 _dbus_verbose ("No cache for user \"%s\"\n",
188 _dbus_string_get_const_data (username));
190 info = dbus_new0 (DBusUserInfo, 1);
193 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
198 if (uid != DBUS_UID_UNSET)
200 if (!_dbus_user_info_fill_uid (info, uid, error))
202 _DBUS_ASSERT_ERROR_IS_SET (error);
203 _dbus_user_info_unref (info);
209 if (!_dbus_user_info_fill (info, username, error))
211 _DBUS_ASSERT_ERROR_IS_SET (error);
212 _dbus_user_info_unref (info);
217 /* be sure we don't use these after here */
218 uid = DBUS_UID_UNSET;
221 /* insert into hash */
222 if (_dbus_hash_table_insert_uintptr (db->users, info->uid, info))
224 _dbus_user_info_ref (info);
228 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
229 _dbus_user_info_unref (info);
233 if (_dbus_hash_table_insert_string (db->users_by_name,
237 _dbus_user_info_ref (info);
241 _dbus_hash_table_remove_uintptr (db->users, info->uid);
242 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
243 _dbus_user_info_unref (info);
247 _dbus_user_info_unref (info);
249 /* Return a borrowed pointer to the DBusUserInfo owned by the
255 static dbus_bool_t database_locked = FALSE;
256 static DBusUserDatabase *system_db = NULL;
257 static DBusString process_username;
258 static DBusString process_homedir;
261 shutdown_system_db (void *data)
263 if (system_db != NULL)
264 _dbus_user_database_unref (system_db);
266 _dbus_string_free (&process_username);
267 _dbus_string_free (&process_homedir);
271 init_system_db (void)
273 _dbus_assert (database_locked);
275 if (system_db == NULL)
277 DBusError error = DBUS_ERROR_INIT;
278 const DBusUserInfo *info;
280 system_db = _dbus_user_database_new ();
281 if (system_db == NULL)
284 if (!_dbus_user_database_get_uid (system_db,
289 _dbus_user_database_unref (system_db);
292 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
294 dbus_error_free (&error);
299 /* This really should not happen. */
300 _dbus_warn ("Could not get password database information for UID of current process: %s",
302 dbus_error_free (&error);
307 if (!_dbus_string_init (&process_username))
309 _dbus_user_database_unref (system_db);
314 if (!_dbus_string_init (&process_homedir))
316 _dbus_string_free (&process_username);
317 _dbus_user_database_unref (system_db);
322 if (!_dbus_string_append (&process_username,
324 !_dbus_string_append (&process_homedir,
326 !_dbus_register_shutdown_func (shutdown_system_db, NULL))
328 _dbus_string_free (&process_username);
329 _dbus_string_free (&process_homedir);
330 _dbus_user_database_unref (system_db);
340 * Locks global system user database.
343 _dbus_user_database_lock_system (void)
345 if (_DBUS_LOCK (system_users))
347 database_locked = TRUE;
357 * Unlocks global system user database.
360 _dbus_user_database_unlock_system (void)
362 database_locked = FALSE;
363 _DBUS_UNLOCK (system_users);
367 * Gets the system global user database;
368 * must be called with lock held (_dbus_user_database_lock_system()).
370 * @returns the database or #NULL if no memory
373 _dbus_user_database_get_system (void)
375 _dbus_assert (database_locked);
383 * Flushes the system global user database;
386 _dbus_user_database_flush_system (void)
388 if (!_dbus_user_database_lock_system ())
390 /* nothing to flush */
394 if (system_db != NULL)
395 _dbus_user_database_flush (system_db);
397 _dbus_user_database_unlock_system ();
401 * Gets username of user owning current process. The returned string
402 * is valid until dbus_shutdown() is called.
404 * @param username place to store pointer to username
405 * @returns #FALSE if no memory
408 _dbus_username_from_current_process (const DBusString **username)
410 if (!_dbus_user_database_lock_system ())
413 if (!init_system_db ())
415 _dbus_user_database_unlock_system ();
418 *username = &process_username;
419 _dbus_user_database_unlock_system ();
425 * Gets homedir of user owning current process. The returned string
426 * is valid until dbus_shutdown() is called.
428 * @param homedir place to store pointer to homedir
429 * @returns #FALSE if no memory
432 _dbus_homedir_from_current_process (const DBusString **homedir)
434 if (!_dbus_user_database_lock_system ())
437 if (!init_system_db ())
439 _dbus_user_database_unlock_system ();
442 *homedir = &process_homedir;
443 _dbus_user_database_unlock_system ();
449 * Gets the home directory for the given user.
451 * @param username the username
452 * @param homedir string to append home directory to
453 * @returns #TRUE if user existed and we appended their homedir
456 _dbus_homedir_from_username (const DBusString *username,
459 DBusUserDatabase *db;
460 const DBusUserInfo *info;
462 /* FIXME: this can't distinguish ENOMEM from other errors */
463 if (!_dbus_user_database_lock_system ())
466 db = _dbus_user_database_get_system ();
469 _dbus_user_database_unlock_system ();
473 if (!_dbus_user_database_get_username (db, username,
476 _dbus_user_database_unlock_system ();
480 if (!_dbus_string_append (homedir, info->homedir))
482 _dbus_user_database_unlock_system ();
486 _dbus_user_database_unlock_system ();
491 * Gets the home directory for the given user.
494 * @param homedir string to append home directory to
495 * @returns #TRUE if user existed and we appended their homedir
498 _dbus_homedir_from_uid (dbus_uid_t uid,
501 DBusUserDatabase *db;
502 const DBusUserInfo *info;
504 if (uid == _dbus_getuid () && uid == _dbus_geteuid ())
506 const char *from_environment;
508 from_environment = _dbus_getenv ("HOME");
510 if (from_environment != NULL)
511 return _dbus_string_append (homedir, from_environment);
514 /* FIXME: this can't distinguish ENOMEM from other errors */
515 if (!_dbus_user_database_lock_system ())
518 db = _dbus_user_database_get_system ();
521 _dbus_user_database_unlock_system ();
525 if (!_dbus_user_database_get_uid (db, uid,
528 _dbus_user_database_unlock_system ();
532 if (!_dbus_string_append (homedir, info->homedir))
534 _dbus_user_database_unlock_system ();
538 _dbus_user_database_unlock_system ();
543 * Adds the credentials corresponding to the given username.
545 * Used among other purposes to parses a desired identity provided
546 * from a client in the auth protocol. On UNIX this means parsing a
547 * UID, on Windows probably parsing an SID string.
549 * @todo this is broken because it treats OOM and parse error
550 * the same way. Needs a #DBusError.
552 * @param credentials credentials to fill in
553 * @param username the username
554 * @returns #TRUE if the username existed and we got some credentials
557 _dbus_credentials_add_from_user (DBusCredentials *credentials,
558 const DBusString *username)
560 DBusUserDatabase *db;
561 const DBusUserInfo *info;
563 /* FIXME: this can't distinguish ENOMEM from other errors */
564 if (!_dbus_user_database_lock_system ())
567 db = _dbus_user_database_get_system ();
570 _dbus_user_database_unlock_system ();
574 if (!_dbus_user_database_get_username (db, username,
577 _dbus_user_database_unlock_system ();
581 if (!_dbus_credentials_add_unix_uid(credentials, info->uid))
583 _dbus_user_database_unlock_system ();
587 _dbus_user_database_unlock_system ();
592 * Creates a new user database object used to look up and
593 * cache user information.
594 * @returns new database, or #NULL on out of memory
597 _dbus_user_database_new (void)
599 DBusUserDatabase *db;
601 db = dbus_new0 (DBusUserDatabase, 1);
607 db->users = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
608 NULL, (DBusFreeFunction) _dbus_user_info_unref);
610 if (db->users == NULL)
613 db->groups = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
614 NULL, (DBusFreeFunction) _dbus_group_info_unref);
616 if (db->groups == NULL)
619 db->users_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
620 NULL, (DBusFreeFunction) _dbus_user_info_unref);
621 if (db->users_by_name == NULL)
624 db->groups_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
625 NULL, (DBusFreeFunction) _dbus_group_info_unref);
626 if (db->groups_by_name == NULL)
632 _dbus_user_database_unref (db);
637 * Flush all information out of the user database.
640 _dbus_user_database_flush (DBusUserDatabase *db)
642 _dbus_hash_table_remove_all(db->users_by_name);
643 _dbus_hash_table_remove_all(db->groups_by_name);
644 _dbus_hash_table_remove_all(db->users);
645 _dbus_hash_table_remove_all(db->groups);
648 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
650 * Increments refcount of user database.
651 * @param db the database
652 * @returns the database
655 _dbus_user_database_ref (DBusUserDatabase *db)
657 _dbus_assert (db->refcount > 0);
663 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */
666 * Decrements refcount of user database.
667 * @param db the database
670 _dbus_user_database_unref (DBusUserDatabase *db)
672 _dbus_assert (db->refcount > 0);
675 if (db->refcount == 0)
678 _dbus_hash_table_unref (db->users);
681 _dbus_hash_table_unref (db->groups);
683 if (db->users_by_name)
684 _dbus_hash_table_unref (db->users_by_name);
686 if (db->groups_by_name)
687 _dbus_hash_table_unref (db->groups_by_name);
694 * Gets the user information for the given UID,
695 * returned user info should not be freed.
697 * @param db user database
698 * @param uid the user ID
699 * @param info return location for const ref to user info
700 * @param error error location
701 * @returns #FALSE if error is set
704 _dbus_user_database_get_uid (DBusUserDatabase *db,
706 const DBusUserInfo **info,
709 *info = _dbus_user_database_lookup (db, uid, NULL, error);
710 return *info != NULL;
714 * Gets the user information for the given username.
716 * @param db user database
717 * @param username the user name
718 * @param info return location for const ref to user info
719 * @param error error location
720 * @returns #FALSE if error is set
723 _dbus_user_database_get_username (DBusUserDatabase *db,
724 const DBusString *username,
725 const DBusUserInfo **info,
728 *info = _dbus_user_database_lookup (db, DBUS_UID_UNSET, username, error);
729 return *info != NULL;
734 /* Tests in dbus-userdb-util.c */