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 _dbus_verbose ("No cache for UID "DBUS_UID_FORMAT"\n",
107 info = dbus_new0 (DBusUserInfo, 1);
110 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
114 if (uid != DBUS_UID_UNSET)
116 if (!_dbus_user_info_fill_uid (info, uid, error))
118 _DBUS_ASSERT_ERROR_IS_SET (error);
119 _dbus_user_info_free_allocated (info);
125 if (!_dbus_user_info_fill (info, username, error))
127 _DBUS_ASSERT_ERROR_IS_SET (error);
128 _dbus_user_info_free_allocated (info);
133 /* be sure we don't use these after here */
134 uid = DBUS_UID_UNSET;
137 /* insert into hash */
138 if (!_dbus_hash_table_insert_ulong (db->users, info->uid, info))
140 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
141 _dbus_user_info_free_allocated (info);
145 if (!_dbus_hash_table_insert_string (db->users_by_name,
149 _dbus_hash_table_remove_ulong (db->users, info->uid);
150 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
158 _DBUS_DEFINE_GLOBAL_LOCK(system_users);
159 static dbus_bool_t database_locked = FALSE;
160 static DBusUserDatabase *system_db = NULL;
161 static DBusString process_username;
162 static DBusString process_homedir;
165 shutdown_system_db (void *data)
167 _dbus_user_database_unref (system_db);
169 _dbus_string_free (&process_username);
170 _dbus_string_free (&process_homedir);
174 init_system_db (void)
176 _dbus_assert (database_locked);
178 if (system_db == NULL)
181 const DBusUserInfo *info;
183 system_db = _dbus_user_database_new ();
184 if (system_db == NULL)
187 dbus_error_init (&error);
189 if (!_dbus_user_database_get_uid (system_db,
194 _dbus_user_database_unref (system_db);
197 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
199 dbus_error_free (&error);
204 /* This really should not happen. */
205 _dbus_warn ("Could not get password database information for UID of current process: %s\n",
207 dbus_error_free (&error);
212 if (!_dbus_string_init (&process_username))
214 _dbus_user_database_unref (system_db);
219 if (!_dbus_string_init (&process_homedir))
221 _dbus_string_free (&process_username);
222 _dbus_user_database_unref (system_db);
227 if (!_dbus_string_append (&process_username,
229 !_dbus_string_append (&process_homedir,
231 !_dbus_register_shutdown_func (shutdown_system_db, NULL))
233 _dbus_string_free (&process_username);
234 _dbus_string_free (&process_homedir);
235 _dbus_user_database_unref (system_db);
245 * Locks global system user database.
248 _dbus_user_database_lock_system (void)
250 _DBUS_LOCK (system_users);
251 database_locked = TRUE;
255 * Unlocks global system user database.
258 _dbus_user_database_unlock_system (void)
260 database_locked = FALSE;
261 _DBUS_UNLOCK (system_users);
265 * Gets the system global user database;
266 * must be called with lock held (_dbus_user_database_lock_system()).
268 * @returns the database or #NULL if no memory
271 _dbus_user_database_get_system (void)
273 _dbus_assert (database_locked);
281 * Gets username of user owning current process. The returned string
282 * is valid until dbus_shutdown() is called.
284 * @param username place to store pointer to username
285 * @returns #FALSE if no memory
288 _dbus_username_from_current_process (const DBusString **username)
290 _dbus_user_database_lock_system ();
291 if (!init_system_db ())
293 _dbus_user_database_unlock_system ();
296 *username = &process_username;
297 _dbus_user_database_unlock_system ();
303 * Gets homedir of user owning current process. The returned string
304 * is valid until dbus_shutdown() is called.
306 * @param homedir place to store pointer to homedir
307 * @returns #FALSE if no memory
310 _dbus_homedir_from_current_process (const DBusString **homedir)
312 _dbus_user_database_lock_system ();
313 if (!init_system_db ())
315 _dbus_user_database_unlock_system ();
318 *homedir = &process_homedir;
319 _dbus_user_database_unlock_system ();
325 * Gets the home directory for the given user.
327 * @param username the username
328 * @param homedir string to append home directory to
329 * @returns #TRUE if user existed and we appended their homedir
332 _dbus_homedir_from_username (const DBusString *username,
335 DBusUserDatabase *db;
336 const DBusUserInfo *info;
337 _dbus_user_database_lock_system ();
339 db = _dbus_user_database_get_system ();
342 _dbus_user_database_unlock_system ();
346 if (!_dbus_user_database_get_username (db, username,
349 _dbus_user_database_unlock_system ();
353 if (!_dbus_string_append (homedir, info->homedir))
355 _dbus_user_database_unlock_system ();
359 _dbus_user_database_unlock_system ();
364 * Gets the credentials corresponding to the given username.
366 * @param username the username
367 * @param credentials credentials to fill in
368 * @returns #TRUE if the username existed and we got some credentials
371 _dbus_credentials_from_username (const DBusString *username,
372 DBusCredentials *credentials)
374 DBusUserDatabase *db;
375 const DBusUserInfo *info;
376 _dbus_user_database_lock_system ();
378 db = _dbus_user_database_get_system ();
381 _dbus_user_database_unlock_system ();
385 if (!_dbus_user_database_get_username (db, username,
388 _dbus_user_database_unlock_system ();
392 credentials->pid = DBUS_PID_UNSET;
393 credentials->uid = info->uid;
394 credentials->gid = info->primary_gid;
396 _dbus_user_database_unlock_system ();
401 * Creates a new user database object used to look up and
402 * cache user information.
403 * @returns new database, or #NULL on out of memory
406 _dbus_user_database_new (void)
408 DBusUserDatabase *db;
410 db = dbus_new0 (DBusUserDatabase, 1);
416 db->users = _dbus_hash_table_new (DBUS_HASH_ULONG,
417 NULL, (DBusFreeFunction) _dbus_user_info_free_allocated);
419 if (db->users == NULL)
422 db->groups = _dbus_hash_table_new (DBUS_HASH_ULONG,
423 NULL, (DBusFreeFunction) _dbus_group_info_free_allocated);
425 if (db->groups == NULL)
428 db->users_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
430 if (db->users_by_name == NULL)
433 db->groups_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
435 if (db->groups_by_name == NULL)
441 _dbus_user_database_unref (db);
445 #ifdef DBUS_BUILD_TESTS
447 * Increments refcount of user database.
448 * @param db the database
449 * @returns the database
452 _dbus_user_database_ref (DBusUserDatabase *db)
454 _dbus_assert (db->refcount > 0);
460 #endif /* DBUS_BUILD_TESTS */
463 * Decrements refcount of user database.
464 * @param db the database
467 _dbus_user_database_unref (DBusUserDatabase *db)
469 _dbus_assert (db->refcount > 0);
472 if (db->refcount == 0)
475 _dbus_hash_table_unref (db->users);
478 _dbus_hash_table_unref (db->groups);
480 if (db->users_by_name)
481 _dbus_hash_table_unref (db->users_by_name);
483 if (db->groups_by_name)
484 _dbus_hash_table_unref (db->groups_by_name);
491 * Gets the user information for the given UID,
492 * returned user info should not be freed.
494 * @param db user database
495 * @param uid the user ID
496 * @param info return location for const ref to user info
497 * @param error error location
498 * @returns #FALSE if error is set
501 _dbus_user_database_get_uid (DBusUserDatabase *db,
503 const DBusUserInfo **info,
506 *info = _dbus_user_database_lookup (db, uid, NULL, error);
507 return *info != NULL;
511 * Gets the user information for the given username.
513 * @param db user database
514 * @param username the user name
515 * @param info return location for const ref to user info
516 * @param error error location
517 * @returns #FALSE if error is set
520 _dbus_user_database_get_username (DBusUserDatabase *db,
521 const DBusString *username,
522 const DBusUserInfo **info,
525 *info = _dbus_user_database_lookup (db, DBUS_UID_UNSET, username, error);
526 return *info != NULL;
531 /* Tests in dbus-userdb-util.c */