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 (info);
69 * Checks if a given string is actually a number
70 * and converts it if it is
72 * @param str the string to check
73 * @param num the memory location of the unsigned long to fill in
74 * @returns TRUE if str is a number and num is filled in
77 _dbus_is_a_number (const DBusString *str,
82 if (_dbus_string_parse_uint (str, 0, num, &end) &&
83 end == _dbus_string_get_length (str))
90 * Looks up a uid or username in the user database. Only one of name
91 * or UID can be provided. There are wrapper functions for this that
92 * are better to use, this one does no locking or anything on the
93 * database and otherwise sort of sucks.
95 * @param db the database
96 * @param uid the user ID or #DBUS_UID_UNSET
97 * @param username username or #NULL
98 * @param error error to fill in
99 * @returns the entry in the database
102 _dbus_user_database_lookup (DBusUserDatabase *db,
104 const DBusString *username,
109 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
110 _dbus_assert (uid != DBUS_UID_UNSET || username != NULL);
112 /* See if the username is really a number */
113 if (uid == DBUS_UID_UNSET)
117 if (_dbus_is_a_number (username, &n))
121 #ifdef DBUS_ENABLE_USER_CACHE
122 if (uid != DBUS_UID_UNSET)
123 info = _dbus_hash_table_lookup_ulong (db->users, uid);
125 info = _dbus_hash_table_lookup_string (db->users_by_name, _dbus_string_get_const_data (username));
129 _dbus_verbose ("Using cache for UID "DBUS_UID_FORMAT" information\n",
138 if (uid != DBUS_UID_UNSET)
139 _dbus_verbose ("No cache for UID "DBUS_UID_FORMAT"\n",
142 _dbus_verbose ("No cache for user \"%s\"\n",
143 _dbus_string_get_const_data (username));
145 info = dbus_new0 (DBusUserInfo, 1);
148 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
152 if (uid != DBUS_UID_UNSET)
154 if (!_dbus_user_info_fill_uid (info, uid, error))
156 _DBUS_ASSERT_ERROR_IS_SET (error);
157 _dbus_user_info_free_allocated (info);
163 if (!_dbus_user_info_fill (info, username, error))
165 _DBUS_ASSERT_ERROR_IS_SET (error);
166 _dbus_user_info_free_allocated (info);
171 /* be sure we don't use these after here */
172 uid = DBUS_UID_UNSET;
175 /* insert into hash */
176 if (!_dbus_hash_table_insert_ulong (db->users, info->uid, info))
178 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
179 _dbus_user_info_free_allocated (info);
183 if (!_dbus_hash_table_insert_string (db->users_by_name,
187 _dbus_hash_table_remove_ulong (db->users, info->uid);
188 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
196 _DBUS_DEFINE_GLOBAL_LOCK(system_users);
197 static dbus_bool_t database_locked = FALSE;
198 static DBusUserDatabase *system_db = NULL;
199 static DBusString process_username;
200 static DBusString process_homedir;
203 shutdown_system_db (void *data)
205 _dbus_user_database_unref (system_db);
207 _dbus_string_free (&process_username);
208 _dbus_string_free (&process_homedir);
212 init_system_db (void)
214 _dbus_assert (database_locked);
216 if (system_db == NULL)
219 const DBusUserInfo *info;
221 system_db = _dbus_user_database_new ();
222 if (system_db == NULL)
225 dbus_error_init (&error);
227 if (!_dbus_user_database_get_uid (system_db,
232 _dbus_user_database_unref (system_db);
235 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
237 dbus_error_free (&error);
242 /* This really should not happen. */
243 _dbus_warn ("Could not get password database information for UID of current process: %s\n",
245 dbus_error_free (&error);
250 if (!_dbus_string_init (&process_username))
252 _dbus_user_database_unref (system_db);
257 if (!_dbus_string_init (&process_homedir))
259 _dbus_string_free (&process_username);
260 _dbus_user_database_unref (system_db);
265 if (!_dbus_string_append (&process_username,
267 !_dbus_string_append (&process_homedir,
269 !_dbus_register_shutdown_func (shutdown_system_db, NULL))
271 _dbus_string_free (&process_username);
272 _dbus_string_free (&process_homedir);
273 _dbus_user_database_unref (system_db);
283 * Locks global system user database.
286 _dbus_user_database_lock_system (void)
288 _DBUS_LOCK (system_users);
289 database_locked = TRUE;
293 * Unlocks global system user database.
296 _dbus_user_database_unlock_system (void)
298 database_locked = FALSE;
299 _DBUS_UNLOCK (system_users);
303 * Gets the system global user database;
304 * must be called with lock held (_dbus_user_database_lock_system()).
306 * @returns the database or #NULL if no memory
309 _dbus_user_database_get_system (void)
311 _dbus_assert (database_locked);
319 * Flushes the system global user database;
322 _dbus_user_database_flush_system (void)
324 _dbus_user_database_lock_system ();
326 _dbus_user_database_flush (system_db);
328 _dbus_user_database_unlock_system ();
332 * Gets username of user owning current process. The returned string
333 * is valid until dbus_shutdown() is called.
335 * @param username place to store pointer to username
336 * @returns #FALSE if no memory
339 _dbus_username_from_current_process (const DBusString **username)
341 _dbus_user_database_lock_system ();
342 if (!init_system_db ())
344 _dbus_user_database_unlock_system ();
347 *username = &process_username;
348 _dbus_user_database_unlock_system ();
354 * Gets homedir of user owning current process. The returned string
355 * is valid until dbus_shutdown() is called.
357 * @param homedir place to store pointer to homedir
358 * @returns #FALSE if no memory
361 _dbus_homedir_from_current_process (const DBusString **homedir)
363 _dbus_user_database_lock_system ();
364 if (!init_system_db ())
366 _dbus_user_database_unlock_system ();
369 *homedir = &process_homedir;
370 _dbus_user_database_unlock_system ();
376 * Gets the home directory for the given user.
378 * @param username the username
379 * @param homedir string to append home directory to
380 * @returns #TRUE if user existed and we appended their homedir
383 _dbus_homedir_from_username (const DBusString *username,
386 DBusUserDatabase *db;
387 const DBusUserInfo *info;
388 _dbus_user_database_lock_system ();
390 db = _dbus_user_database_get_system ();
393 _dbus_user_database_unlock_system ();
397 if (!_dbus_user_database_get_username (db, username,
400 _dbus_user_database_unlock_system ();
404 if (!_dbus_string_append (homedir, info->homedir))
406 _dbus_user_database_unlock_system ();
410 _dbus_user_database_unlock_system ();
415 * Gets the credentials corresponding to the given username.
417 * @param username the username
418 * @param credentials credentials to fill in
419 * @returns #TRUE if the username existed and we got some credentials
422 _dbus_credentials_from_username (const DBusString *username,
423 DBusCredentials *credentials)
425 DBusUserDatabase *db;
426 const DBusUserInfo *info;
427 _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 credentials->pid = DBUS_PID_UNSET;
444 credentials->uid = info->uid;
445 credentials->gid = info->primary_gid;
447 _dbus_user_database_unlock_system ();
452 * Creates a new user database object used to look up and
453 * cache user information.
454 * @returns new database, or #NULL on out of memory
457 _dbus_user_database_new (void)
459 DBusUserDatabase *db;
461 db = dbus_new0 (DBusUserDatabase, 1);
467 db->users = _dbus_hash_table_new (DBUS_HASH_ULONG,
468 NULL, (DBusFreeFunction) _dbus_user_info_free_allocated);
470 if (db->users == NULL)
473 db->groups = _dbus_hash_table_new (DBUS_HASH_ULONG,
474 NULL, (DBusFreeFunction) _dbus_group_info_free_allocated);
476 if (db->groups == NULL)
479 db->users_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
481 if (db->users_by_name == NULL)
484 db->groups_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
486 if (db->groups_by_name == NULL)
492 _dbus_user_database_unref (db);
497 * Flush all information out of the user database.
500 _dbus_user_database_flush (DBusUserDatabase *db)
502 _dbus_hash_table_remove_all(db->users_by_name);
503 _dbus_hash_table_remove_all(db->groups_by_name);
504 _dbus_hash_table_remove_all(db->users);
505 _dbus_hash_table_remove_all(db->groups);
508 #ifdef DBUS_BUILD_TESTS
510 * Increments refcount of user database.
511 * @param db the database
512 * @returns the database
515 _dbus_user_database_ref (DBusUserDatabase *db)
517 _dbus_assert (db->refcount > 0);
523 #endif /* DBUS_BUILD_TESTS */
526 * Decrements refcount of user database.
527 * @param db the database
530 _dbus_user_database_unref (DBusUserDatabase *db)
532 _dbus_assert (db->refcount > 0);
535 if (db->refcount == 0)
538 _dbus_hash_table_unref (db->users);
541 _dbus_hash_table_unref (db->groups);
543 if (db->users_by_name)
544 _dbus_hash_table_unref (db->users_by_name);
546 if (db->groups_by_name)
547 _dbus_hash_table_unref (db->groups_by_name);
554 * Gets the user information for the given UID,
555 * returned user info should not be freed.
557 * @param db user database
558 * @param uid the user ID
559 * @param info return location for const ref to user info
560 * @param error error location
561 * @returns #FALSE if error is set
564 _dbus_user_database_get_uid (DBusUserDatabase *db,
566 const DBusUserInfo **info,
569 *info = _dbus_user_database_lookup (db, uid, NULL, error);
570 return *info != NULL;
574 * Gets the user information for the given username.
576 * @param db user database
577 * @param username the user name
578 * @param info return location for const ref to user info
579 * @param error error location
580 * @returns #FALSE if error is set
583 _dbus_user_database_get_username (DBusUserDatabase *db,
584 const DBusString *username,
585 const DBusUserInfo **info,
588 *info = _dbus_user_database_lookup (db, DBUS_UID_UNSET, username, error);
589 return *info != NULL;
594 /* Tests in dbus-userdb-util.c */