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 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);
218 static dbus_bool_t database_locked = FALSE;
219 static DBusUserDatabase *system_db = NULL;
220 static DBusString process_username;
221 static DBusString process_homedir;
224 shutdown_system_db (void *data)
226 if (system_db != NULL)
227 _dbus_user_database_unref (system_db);
229 _dbus_string_free (&process_username);
230 _dbus_string_free (&process_homedir);
234 init_system_db (void)
236 _dbus_assert (database_locked);
238 if (system_db == NULL)
240 DBusError error = DBUS_ERROR_INIT;
241 const DBusUserInfo *info;
243 system_db = _dbus_user_database_new ();
244 if (system_db == NULL)
247 if (!_dbus_user_database_get_uid (system_db,
252 _dbus_user_database_unref (system_db);
255 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
257 dbus_error_free (&error);
262 /* This really should not happen. */
263 _dbus_warn ("Could not get password database information for UID of current process: %s",
265 dbus_error_free (&error);
270 if (!_dbus_string_init (&process_username))
272 _dbus_user_database_unref (system_db);
277 if (!_dbus_string_init (&process_homedir))
279 _dbus_string_free (&process_username);
280 _dbus_user_database_unref (system_db);
285 if (!_dbus_string_append (&process_username,
287 !_dbus_string_append (&process_homedir,
289 !_dbus_register_shutdown_func (shutdown_system_db, NULL))
291 _dbus_string_free (&process_username);
292 _dbus_string_free (&process_homedir);
293 _dbus_user_database_unref (system_db);
303 * Locks global system user database.
306 _dbus_user_database_lock_system (void)
308 if (_DBUS_LOCK (system_users))
310 database_locked = TRUE;
320 * Unlocks global system user database.
323 _dbus_user_database_unlock_system (void)
325 database_locked = FALSE;
326 _DBUS_UNLOCK (system_users);
330 * Gets the system global user database;
331 * must be called with lock held (_dbus_user_database_lock_system()).
333 * @returns the database or #NULL if no memory
336 _dbus_user_database_get_system (void)
338 _dbus_assert (database_locked);
346 * Flushes the system global user database;
349 _dbus_user_database_flush_system (void)
351 if (!_dbus_user_database_lock_system ())
353 /* nothing to flush */
357 if (system_db != NULL)
358 _dbus_user_database_flush (system_db);
360 _dbus_user_database_unlock_system ();
364 * Gets username of user owning current process. The returned string
365 * is valid until dbus_shutdown() is called.
367 * @param username place to store pointer to username
368 * @returns #FALSE if no memory
371 _dbus_username_from_current_process (const DBusString **username)
373 if (!_dbus_user_database_lock_system ())
376 if (!init_system_db ())
378 _dbus_user_database_unlock_system ();
381 *username = &process_username;
382 _dbus_user_database_unlock_system ();
388 * Gets homedir of user owning current process. The returned string
389 * is valid until dbus_shutdown() is called.
391 * @param homedir place to store pointer to homedir
392 * @returns #FALSE if no memory
395 _dbus_homedir_from_current_process (const DBusString **homedir)
397 if (!_dbus_user_database_lock_system ())
400 if (!init_system_db ())
402 _dbus_user_database_unlock_system ();
405 *homedir = &process_homedir;
406 _dbus_user_database_unlock_system ();
412 * Gets the home directory for the given user.
414 * @param username the username
415 * @param homedir string to append home directory to
416 * @returns #TRUE if user existed and we appended their homedir
419 _dbus_homedir_from_username (const DBusString *username,
422 DBusUserDatabase *db;
423 const DBusUserInfo *info;
425 /* FIXME: this can't distinguish ENOMEM from other errors */
426 if (!_dbus_user_database_lock_system ())
429 db = _dbus_user_database_get_system ();
432 _dbus_user_database_unlock_system ();
436 if (!_dbus_user_database_get_username (db, username,
439 _dbus_user_database_unlock_system ();
443 if (!_dbus_string_append (homedir, info->homedir))
445 _dbus_user_database_unlock_system ();
449 _dbus_user_database_unlock_system ();
454 * Gets the home directory for the given user.
457 * @param homedir string to append home directory to
458 * @returns #TRUE if user existed and we appended their homedir
461 _dbus_homedir_from_uid (dbus_uid_t uid,
464 DBusUserDatabase *db;
465 const DBusUserInfo *info;
467 /* FIXME: this can't distinguish ENOMEM from other errors */
468 if (!_dbus_user_database_lock_system ())
471 db = _dbus_user_database_get_system ();
474 _dbus_user_database_unlock_system ();
478 if (!_dbus_user_database_get_uid (db, uid,
481 _dbus_user_database_unlock_system ();
485 if (!_dbus_string_append (homedir, info->homedir))
487 _dbus_user_database_unlock_system ();
491 _dbus_user_database_unlock_system ();
496 * Adds the credentials corresponding to the given username.
498 * Used among other purposes to parses a desired identity provided
499 * from a client in the auth protocol. On UNIX this means parsing a
500 * UID, on Windows probably parsing an SID string.
502 * @todo this is broken because it treats OOM and parse error
503 * the same way. Needs a #DBusError.
505 * @param credentials credentials to fill in
506 * @param username the username
507 * @returns #TRUE if the username existed and we got some credentials
510 _dbus_credentials_add_from_user (DBusCredentials *credentials,
511 const DBusString *username)
513 DBusUserDatabase *db;
514 const DBusUserInfo *info;
516 /* FIXME: this can't distinguish ENOMEM from other errors */
517 if (!_dbus_user_database_lock_system ())
520 db = _dbus_user_database_get_system ();
523 _dbus_user_database_unlock_system ();
527 if (!_dbus_user_database_get_username (db, username,
530 _dbus_user_database_unlock_system ();
534 if (!_dbus_credentials_add_unix_uid(credentials, info->uid))
536 _dbus_user_database_unlock_system ();
540 _dbus_user_database_unlock_system ();
545 * Creates a new user database object used to look up and
546 * cache user information.
547 * @returns new database, or #NULL on out of memory
550 _dbus_user_database_new (void)
552 DBusUserDatabase *db;
554 db = dbus_new0 (DBusUserDatabase, 1);
560 db->users = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
561 NULL, (DBusFreeFunction) _dbus_user_info_free_allocated);
563 if (db->users == NULL)
566 db->groups = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
567 NULL, (DBusFreeFunction) _dbus_group_info_free_allocated);
569 if (db->groups == NULL)
572 db->users_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
574 if (db->users_by_name == NULL)
577 db->groups_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
579 if (db->groups_by_name == NULL)
585 _dbus_user_database_unref (db);
590 * Flush all information out of the user database.
593 _dbus_user_database_flush (DBusUserDatabase *db)
595 _dbus_hash_table_remove_all(db->users_by_name);
596 _dbus_hash_table_remove_all(db->groups_by_name);
597 _dbus_hash_table_remove_all(db->users);
598 _dbus_hash_table_remove_all(db->groups);
601 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
603 * Increments refcount of user database.
604 * @param db the database
605 * @returns the database
608 _dbus_user_database_ref (DBusUserDatabase *db)
610 _dbus_assert (db->refcount > 0);
616 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */
619 * Decrements refcount of user database.
620 * @param db the database
623 _dbus_user_database_unref (DBusUserDatabase *db)
625 _dbus_assert (db->refcount > 0);
628 if (db->refcount == 0)
631 _dbus_hash_table_unref (db->users);
634 _dbus_hash_table_unref (db->groups);
636 if (db->users_by_name)
637 _dbus_hash_table_unref (db->users_by_name);
639 if (db->groups_by_name)
640 _dbus_hash_table_unref (db->groups_by_name);
647 * Gets the user information for the given UID,
648 * returned user info should not be freed.
650 * @param db user database
651 * @param uid the user ID
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_uid (DBusUserDatabase *db,
659 const DBusUserInfo **info,
662 *info = _dbus_user_database_lookup (db, uid, NULL, error);
663 return *info != NULL;
667 * Gets the user information for the given username.
669 * @param db user database
670 * @param username the user name
671 * @param info return location for const ref to user info
672 * @param error error location
673 * @returns #FALSE if error is set
676 _dbus_user_database_get_username (DBusUserDatabase *db,
677 const DBusString *username,
678 const DBusUserInfo **info,
681 *info = _dbus_user_database_lookup (db, DBUS_UID_UNSET, username, error);
682 return *info != NULL;
687 /* Tests in dbus-userdb-util.c */