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 (borrowed, do not free)
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 if (uid != DBUS_UID_UNSET)
148 info = _dbus_hash_table_lookup_uintptr (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",
160 if (uid != DBUS_UID_UNSET)
161 _dbus_verbose ("No cache for UID "DBUS_UID_FORMAT"\n",
164 _dbus_verbose ("No cache for user \"%s\"\n",
165 _dbus_string_get_const_data (username));
167 info = dbus_new0 (DBusUserInfo, 1);
170 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
174 if (uid != DBUS_UID_UNSET)
176 if (!_dbus_user_info_fill_uid (info, uid, error))
178 _DBUS_ASSERT_ERROR_IS_SET (error);
179 _dbus_user_info_free_allocated (info);
185 if (!_dbus_user_info_fill (info, username, error))
187 _DBUS_ASSERT_ERROR_IS_SET (error);
188 _dbus_user_info_free_allocated (info);
193 /* be sure we don't use these after here */
194 uid = DBUS_UID_UNSET;
197 /* insert into hash */
198 if (!_dbus_hash_table_insert_uintptr (db->users, info->uid, info))
200 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
201 _dbus_user_info_free_allocated (info);
205 if (!_dbus_hash_table_insert_string (db->users_by_name,
209 _dbus_hash_table_remove_uintptr (db->users, info->uid);
210 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
214 /* Return a borrowed pointer to the DBusUserInfo owned by the
220 static dbus_bool_t database_locked = FALSE;
221 static DBusUserDatabase *system_db = NULL;
222 static DBusString process_username;
223 static DBusString process_homedir;
226 shutdown_system_db (void *data)
228 if (system_db != NULL)
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",
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 if (_DBUS_LOCK (system_users))
312 database_locked = TRUE;
322 * Unlocks global system user database.
325 _dbus_user_database_unlock_system (void)
327 database_locked = FALSE;
328 _DBUS_UNLOCK (system_users);
332 * Gets the system global user database;
333 * must be called with lock held (_dbus_user_database_lock_system()).
335 * @returns the database or #NULL if no memory
338 _dbus_user_database_get_system (void)
340 _dbus_assert (database_locked);
348 * Flushes the system global user database;
351 _dbus_user_database_flush_system (void)
353 if (!_dbus_user_database_lock_system ())
355 /* nothing to flush */
359 if (system_db != NULL)
360 _dbus_user_database_flush (system_db);
362 _dbus_user_database_unlock_system ();
366 * Gets username of user owning current process. The returned string
367 * is valid until dbus_shutdown() is called.
369 * @param username place to store pointer to username
370 * @returns #FALSE if no memory
373 _dbus_username_from_current_process (const DBusString **username)
375 if (!_dbus_user_database_lock_system ())
378 if (!init_system_db ())
380 _dbus_user_database_unlock_system ();
383 *username = &process_username;
384 _dbus_user_database_unlock_system ();
390 * Gets homedir of user owning current process. The returned string
391 * is valid until dbus_shutdown() is called.
393 * @param homedir place to store pointer to homedir
394 * @returns #FALSE if no memory
397 _dbus_homedir_from_current_process (const DBusString **homedir)
399 if (!_dbus_user_database_lock_system ())
402 if (!init_system_db ())
404 _dbus_user_database_unlock_system ();
407 *homedir = &process_homedir;
408 _dbus_user_database_unlock_system ();
414 * Gets the home directory for the given user.
416 * @param username the username
417 * @param homedir string to append home directory to
418 * @returns #TRUE if user existed and we appended their homedir
421 _dbus_homedir_from_username (const DBusString *username,
424 DBusUserDatabase *db;
425 const DBusUserInfo *info;
427 /* FIXME: this can't distinguish ENOMEM from other errors */
428 if (!_dbus_user_database_lock_system ())
431 db = _dbus_user_database_get_system ();
434 _dbus_user_database_unlock_system ();
438 if (!_dbus_user_database_get_username (db, username,
441 _dbus_user_database_unlock_system ();
445 if (!_dbus_string_append (homedir, info->homedir))
447 _dbus_user_database_unlock_system ();
451 _dbus_user_database_unlock_system ();
456 * Gets the home directory for the given user.
459 * @param homedir string to append home directory to
460 * @returns #TRUE if user existed and we appended their homedir
463 _dbus_homedir_from_uid (dbus_uid_t uid,
466 DBusUserDatabase *db;
467 const DBusUserInfo *info;
469 if (uid == _dbus_getuid () && uid == _dbus_geteuid ())
471 const char *from_environment;
473 from_environment = _dbus_getenv ("HOME");
475 if (from_environment != NULL)
476 return _dbus_string_append (homedir, from_environment);
479 /* FIXME: this can't distinguish ENOMEM from other errors */
480 if (!_dbus_user_database_lock_system ())
483 db = _dbus_user_database_get_system ();
486 _dbus_user_database_unlock_system ();
490 if (!_dbus_user_database_get_uid (db, uid,
493 _dbus_user_database_unlock_system ();
497 if (!_dbus_string_append (homedir, info->homedir))
499 _dbus_user_database_unlock_system ();
503 _dbus_user_database_unlock_system ();
508 * Adds the credentials corresponding to the given username.
510 * Used among other purposes to parses a desired identity provided
511 * from a client in the auth protocol. On UNIX this means parsing a
512 * UID, on Windows probably parsing an SID string.
514 * @todo this is broken because it treats OOM and parse error
515 * the same way. Needs a #DBusError.
517 * @param credentials credentials to fill in
518 * @param username the username
519 * @returns #TRUE if the username existed and we got some credentials
522 _dbus_credentials_add_from_user (DBusCredentials *credentials,
523 const DBusString *username)
525 DBusUserDatabase *db;
526 const DBusUserInfo *info;
528 /* FIXME: this can't distinguish ENOMEM from other errors */
529 if (!_dbus_user_database_lock_system ())
532 db = _dbus_user_database_get_system ();
535 _dbus_user_database_unlock_system ();
539 if (!_dbus_user_database_get_username (db, username,
542 _dbus_user_database_unlock_system ();
546 if (!_dbus_credentials_add_unix_uid(credentials, info->uid))
548 _dbus_user_database_unlock_system ();
552 _dbus_user_database_unlock_system ();
557 * Creates a new user database object used to look up and
558 * cache user information.
559 * @returns new database, or #NULL on out of memory
562 _dbus_user_database_new (void)
564 DBusUserDatabase *db;
566 db = dbus_new0 (DBusUserDatabase, 1);
572 db->users = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
573 NULL, (DBusFreeFunction) _dbus_user_info_free_allocated);
575 if (db->users == NULL)
578 db->groups = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
579 NULL, (DBusFreeFunction) _dbus_group_info_free_allocated);
581 if (db->groups == NULL)
584 db->users_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
586 if (db->users_by_name == NULL)
589 db->groups_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
591 if (db->groups_by_name == NULL)
597 _dbus_user_database_unref (db);
602 * Flush all information out of the user database.
605 _dbus_user_database_flush (DBusUserDatabase *db)
607 _dbus_hash_table_remove_all(db->users_by_name);
608 _dbus_hash_table_remove_all(db->groups_by_name);
609 _dbus_hash_table_remove_all(db->users);
610 _dbus_hash_table_remove_all(db->groups);
613 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
615 * Increments refcount of user database.
616 * @param db the database
617 * @returns the database
620 _dbus_user_database_ref (DBusUserDatabase *db)
622 _dbus_assert (db->refcount > 0);
628 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */
631 * Decrements refcount of user database.
632 * @param db the database
635 _dbus_user_database_unref (DBusUserDatabase *db)
637 _dbus_assert (db->refcount > 0);
640 if (db->refcount == 0)
643 _dbus_hash_table_unref (db->users);
646 _dbus_hash_table_unref (db->groups);
648 if (db->users_by_name)
649 _dbus_hash_table_unref (db->users_by_name);
651 if (db->groups_by_name)
652 _dbus_hash_table_unref (db->groups_by_name);
659 * Gets the user information for the given UID,
660 * returned user info should not be freed.
662 * @param db user database
663 * @param uid the user ID
664 * @param info return location for const ref to user info
665 * @param error error location
666 * @returns #FALSE if error is set
669 _dbus_user_database_get_uid (DBusUserDatabase *db,
671 const DBusUserInfo **info,
674 *info = _dbus_user_database_lookup (db, uid, NULL, error);
675 return *info != NULL;
679 * Gets the user information for the given username.
681 * @param db user database
682 * @param username the user name
683 * @param info return location for const ref to user info
684 * @param error error location
685 * @returns #FALSE if error is set
688 _dbus_user_database_get_username (DBusUserDatabase *db,
689 const DBusString *username,
690 const DBusUserInfo **info,
693 *info = _dbus_user_database_lookup (db, DBUS_UID_UNSET, username, error);
694 return *info != NULL;
699 /* Tests in dbus-userdb-util.c */