1 /* -*- mode: C; c-file-style: "gnu" -*- */
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"
32 * @addtogroup DBusInternalsUtils
37 * Frees the given #DBusUserInfo's members with _dbus_user_info_free()
38 * and also calls dbus_free() on the block itself
40 * @param info the info
43 _dbus_user_info_free_allocated (DBusUserInfo *info)
45 if (info == NULL) /* hash table will pass NULL */
48 _dbus_user_info_free (info);
53 * Frees the given #DBusGroupInfo's members with _dbus_group_info_free()
54 * and also calls dbus_free() on the block itself
56 * @param info the info
59 _dbus_group_info_free_allocated (DBusGroupInfo *info)
61 if (info == NULL) /* hash table will pass NULL */
64 _dbus_group_info_free_allocated (info);
69 * Looks up a uid or username in the user database. Only one of name
70 * or UID can be provided. There are wrapper functions for this that
71 * are better to use, this one does no locking or anything on the
72 * database and otherwise sort of sucks.
74 * @param db the database
75 * @param uid the user ID or #DBUS_UID_UNSET
76 * @param username username or #NULL
77 * @param error error to fill in
78 * @returns the entry in the database
81 _dbus_user_database_lookup (DBusUserDatabase *db,
83 const DBusString *username,
88 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
89 _dbus_assert (uid != DBUS_UID_UNSET || username != NULL);
91 if (uid != DBUS_UID_UNSET)
92 info = _dbus_hash_table_lookup_ulong (db->users, uid);
94 info = _dbus_hash_table_lookup_string (db->users_by_name, _dbus_string_get_const_data (username));
98 _dbus_verbose ("Using cache for UID "DBUS_UID_FORMAT" information\n",
104 if (uid != DBUS_UID_UNSET)
105 _dbus_verbose ("No cache for UID "DBUS_UID_FORMAT"\n",
108 _dbus_verbose ("No cache for user \"%s\"\n",
109 _dbus_string_get_const_data (username));
111 info = dbus_new0 (DBusUserInfo, 1);
114 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
118 if (uid != DBUS_UID_UNSET)
120 if (!_dbus_user_info_fill_uid (info, uid, error))
122 _DBUS_ASSERT_ERROR_IS_SET (error);
123 _dbus_user_info_free_allocated (info);
129 if (!_dbus_user_info_fill (info, username, error))
131 _DBUS_ASSERT_ERROR_IS_SET (error);
132 _dbus_user_info_free_allocated (info);
137 /* be sure we don't use these after here */
138 uid = DBUS_UID_UNSET;
141 /* insert into hash */
142 if (!_dbus_hash_table_insert_ulong (db->users, info->uid, info))
144 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
145 _dbus_user_info_free_allocated (info);
149 if (!_dbus_hash_table_insert_string (db->users_by_name,
153 _dbus_hash_table_remove_ulong (db->users, info->uid);
154 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
162 _DBUS_DEFINE_GLOBAL_LOCK(system_users);
163 static dbus_bool_t database_locked = FALSE;
164 static DBusUserDatabase *system_db = NULL;
165 static DBusString process_username;
166 static DBusString process_homedir;
169 shutdown_system_db (void *data)
171 _dbus_user_database_unref (system_db);
173 _dbus_string_free (&process_username);
174 _dbus_string_free (&process_homedir);
178 init_system_db (void)
180 _dbus_assert (database_locked);
182 if (system_db == NULL)
185 const DBusUserInfo *info;
187 system_db = _dbus_user_database_new ();
188 if (system_db == NULL)
191 dbus_error_init (&error);
193 if (!_dbus_user_database_get_uid (system_db,
198 _dbus_user_database_unref (system_db);
201 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
203 dbus_error_free (&error);
208 /* This really should not happen. */
209 _dbus_warn ("Could not get password database information for UID of current process: %s\n",
211 dbus_error_free (&error);
216 if (!_dbus_string_init (&process_username))
218 _dbus_user_database_unref (system_db);
223 if (!_dbus_string_init (&process_homedir))
225 _dbus_string_free (&process_username);
226 _dbus_user_database_unref (system_db);
231 if (!_dbus_string_append (&process_username,
233 !_dbus_string_append (&process_homedir,
235 !_dbus_register_shutdown_func (shutdown_system_db, NULL))
237 _dbus_string_free (&process_username);
238 _dbus_string_free (&process_homedir);
239 _dbus_user_database_unref (system_db);
249 * Locks global system user database.
252 _dbus_user_database_lock_system (void)
254 _DBUS_LOCK (system_users);
255 database_locked = TRUE;
259 * Unlocks global system user database.
262 _dbus_user_database_unlock_system (void)
264 database_locked = FALSE;
265 _DBUS_UNLOCK (system_users);
269 * Gets the system global user database;
270 * must be called with lock held (_dbus_user_database_lock_system()).
272 * @returns the database or #NULL if no memory
275 _dbus_user_database_get_system (void)
277 _dbus_assert (database_locked);
285 * Gets username of user owning current process. The returned string
286 * is valid until dbus_shutdown() is called.
288 * @param username place to store pointer to username
289 * @returns #FALSE if no memory
292 _dbus_username_from_current_process (const DBusString **username)
294 _dbus_user_database_lock_system ();
295 if (!init_system_db ())
297 _dbus_user_database_unlock_system ();
300 *username = &process_username;
301 _dbus_user_database_unlock_system ();
307 * Gets homedir of user owning current process. The returned string
308 * is valid until dbus_shutdown() is called.
310 * @param homedir place to store pointer to homedir
311 * @returns #FALSE if no memory
314 _dbus_homedir_from_current_process (const DBusString **homedir)
316 _dbus_user_database_lock_system ();
317 if (!init_system_db ())
319 _dbus_user_database_unlock_system ();
322 *homedir = &process_homedir;
323 _dbus_user_database_unlock_system ();
329 * Gets the home directory for the given user.
331 * @param username the username
332 * @param homedir string to append home directory to
333 * @returns #TRUE if user existed and we appended their homedir
336 _dbus_homedir_from_username (const DBusString *username,
339 DBusUserDatabase *db;
340 const DBusUserInfo *info;
341 _dbus_user_database_lock_system ();
343 db = _dbus_user_database_get_system ();
346 _dbus_user_database_unlock_system ();
350 if (!_dbus_user_database_get_username (db, username,
353 _dbus_user_database_unlock_system ();
357 if (!_dbus_string_append (homedir, info->homedir))
359 _dbus_user_database_unlock_system ();
363 _dbus_user_database_unlock_system ();
368 * Gets the credentials corresponding to the given username.
370 * @param username the username
371 * @param credentials credentials to fill in
372 * @returns #TRUE if the username existed and we got some credentials
375 _dbus_credentials_from_username (const DBusString *username,
376 DBusCredentials *credentials)
378 DBusUserDatabase *db;
379 const DBusUserInfo *info;
380 _dbus_user_database_lock_system ();
382 db = _dbus_user_database_get_system ();
385 _dbus_user_database_unlock_system ();
389 if (!_dbus_user_database_get_username (db, username,
392 _dbus_user_database_unlock_system ();
396 credentials->pid = DBUS_PID_UNSET;
397 credentials->uid = info->uid;
398 credentials->gid = info->primary_gid;
400 _dbus_user_database_unlock_system ();
405 * Creates a new user database object used to look up and
406 * cache user information.
407 * @returns new database, or #NULL on out of memory
410 _dbus_user_database_new (void)
412 DBusUserDatabase *db;
414 db = dbus_new0 (DBusUserDatabase, 1);
420 db->users = _dbus_hash_table_new (DBUS_HASH_ULONG,
421 NULL, (DBusFreeFunction) _dbus_user_info_free_allocated);
423 if (db->users == NULL)
426 db->groups = _dbus_hash_table_new (DBUS_HASH_ULONG,
427 NULL, (DBusFreeFunction) _dbus_group_info_free_allocated);
429 if (db->groups == NULL)
432 db->users_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
434 if (db->users_by_name == NULL)
437 db->groups_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
439 if (db->groups_by_name == NULL)
445 _dbus_user_database_unref (db);
449 #ifdef DBUS_BUILD_TESTS
451 * Increments refcount of user database.
452 * @param db the database
453 * @returns the database
456 _dbus_user_database_ref (DBusUserDatabase *db)
458 _dbus_assert (db->refcount > 0);
464 #endif /* DBUS_BUILD_TESTS */
467 * Decrements refcount of user database.
468 * @param db the database
471 _dbus_user_database_unref (DBusUserDatabase *db)
473 _dbus_assert (db->refcount > 0);
476 if (db->refcount == 0)
479 _dbus_hash_table_unref (db->users);
482 _dbus_hash_table_unref (db->groups);
484 if (db->users_by_name)
485 _dbus_hash_table_unref (db->users_by_name);
487 if (db->groups_by_name)
488 _dbus_hash_table_unref (db->groups_by_name);
495 * Gets the user information for the given UID,
496 * returned user info should not be freed.
498 * @param db user database
499 * @param uid the user ID
500 * @param info return location for const ref to user info
501 * @param error error location
502 * @returns #FALSE if error is set
505 _dbus_user_database_get_uid (DBusUserDatabase *db,
507 const DBusUserInfo **info,
510 *info = _dbus_user_database_lookup (db, uid, NULL, error);
511 return *info != NULL;
515 * Gets the user information for the given username.
517 * @param db user database
518 * @param username the user name
519 * @param info return location for const ref to user info
520 * @param error error location
521 * @returns #FALSE if error is set
524 _dbus_user_database_get_username (DBusUserDatabase *db,
525 const DBusString *username,
526 const DBusUserInfo **info,
529 *info = _dbus_user_database_lookup (db, DBUS_UID_UNSET, username, error);
530 return *info != NULL;
535 /* Tests in dbus-userdb-util.c */