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_int (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 if (uid != DBUS_UID_UNSET)
122 info = _dbus_hash_table_lookup_ulong (db->users, uid);
124 info = _dbus_hash_table_lookup_string (db->users_by_name, _dbus_string_get_const_data (username));
128 _dbus_verbose ("Using cache for UID "DBUS_UID_FORMAT" information\n",
134 if (uid != DBUS_UID_UNSET)
135 _dbus_verbose ("No cache for UID "DBUS_UID_FORMAT"\n",
138 _dbus_verbose ("No cache for user \"%s\"\n",
139 _dbus_string_get_const_data (username));
141 info = dbus_new0 (DBusUserInfo, 1);
144 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
148 if (uid != DBUS_UID_UNSET)
150 if (!_dbus_user_info_fill_uid (info, uid, error))
152 _DBUS_ASSERT_ERROR_IS_SET (error);
153 _dbus_user_info_free_allocated (info);
159 if (!_dbus_user_info_fill (info, username, error))
161 _DBUS_ASSERT_ERROR_IS_SET (error);
162 _dbus_user_info_free_allocated (info);
167 /* be sure we don't use these after here */
168 uid = DBUS_UID_UNSET;
171 /* insert into hash */
172 if (!_dbus_hash_table_insert_ulong (db->users, info->uid, info))
174 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
175 _dbus_user_info_free_allocated (info);
179 if (!_dbus_hash_table_insert_string (db->users_by_name,
183 _dbus_hash_table_remove_ulong (db->users, info->uid);
184 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
192 _DBUS_DEFINE_GLOBAL_LOCK(system_users);
193 static dbus_bool_t database_locked = FALSE;
194 static DBusUserDatabase *system_db = NULL;
195 static DBusString process_username;
196 static DBusString process_homedir;
199 shutdown_system_db (void *data)
201 _dbus_user_database_unref (system_db);
203 _dbus_string_free (&process_username);
204 _dbus_string_free (&process_homedir);
208 init_system_db (void)
210 _dbus_assert (database_locked);
212 if (system_db == NULL)
215 const DBusUserInfo *info;
217 system_db = _dbus_user_database_new ();
218 if (system_db == NULL)
221 dbus_error_init (&error);
223 if (!_dbus_user_database_get_uid (system_db,
228 _dbus_user_database_unref (system_db);
231 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
233 dbus_error_free (&error);
238 /* This really should not happen. */
239 _dbus_warn ("Could not get password database information for UID of current process: %s\n",
241 dbus_error_free (&error);
246 if (!_dbus_string_init (&process_username))
248 _dbus_user_database_unref (system_db);
253 if (!_dbus_string_init (&process_homedir))
255 _dbus_string_free (&process_username);
256 _dbus_user_database_unref (system_db);
261 if (!_dbus_string_append (&process_username,
263 !_dbus_string_append (&process_homedir,
265 !_dbus_register_shutdown_func (shutdown_system_db, NULL))
267 _dbus_string_free (&process_username);
268 _dbus_string_free (&process_homedir);
269 _dbus_user_database_unref (system_db);
279 * Locks global system user database.
282 _dbus_user_database_lock_system (void)
284 _DBUS_LOCK (system_users);
285 database_locked = TRUE;
289 * Unlocks global system user database.
292 _dbus_user_database_unlock_system (void)
294 database_locked = FALSE;
295 _DBUS_UNLOCK (system_users);
299 * Gets the system global user database;
300 * must be called with lock held (_dbus_user_database_lock_system()).
302 * @returns the database or #NULL if no memory
305 _dbus_user_database_get_system (void)
307 _dbus_assert (database_locked);
315 * Gets username of user owning current process. The returned string
316 * is valid until dbus_shutdown() is called.
318 * @param username place to store pointer to username
319 * @returns #FALSE if no memory
322 _dbus_username_from_current_process (const DBusString **username)
324 _dbus_user_database_lock_system ();
325 if (!init_system_db ())
327 _dbus_user_database_unlock_system ();
330 *username = &process_username;
331 _dbus_user_database_unlock_system ();
337 * Gets homedir of user owning current process. The returned string
338 * is valid until dbus_shutdown() is called.
340 * @param homedir place to store pointer to homedir
341 * @returns #FALSE if no memory
344 _dbus_homedir_from_current_process (const DBusString **homedir)
346 _dbus_user_database_lock_system ();
347 if (!init_system_db ())
349 _dbus_user_database_unlock_system ();
352 *homedir = &process_homedir;
353 _dbus_user_database_unlock_system ();
359 * Gets the home directory for the given user.
361 * @param username the username
362 * @param homedir string to append home directory to
363 * @returns #TRUE if user existed and we appended their homedir
366 _dbus_homedir_from_username (const DBusString *username,
369 DBusUserDatabase *db;
370 const DBusUserInfo *info;
371 _dbus_user_database_lock_system ();
373 db = _dbus_user_database_get_system ();
376 _dbus_user_database_unlock_system ();
380 if (!_dbus_user_database_get_username (db, username,
383 _dbus_user_database_unlock_system ();
387 if (!_dbus_string_append (homedir, info->homedir))
389 _dbus_user_database_unlock_system ();
393 _dbus_user_database_unlock_system ();
398 * Gets the credentials corresponding to the given username.
400 * @param username the username
401 * @param credentials credentials to fill in
402 * @returns #TRUE if the username existed and we got some credentials
405 _dbus_credentials_from_username (const DBusString *username,
406 DBusCredentials *credentials)
408 DBusUserDatabase *db;
409 const DBusUserInfo *info;
410 _dbus_user_database_lock_system ();
412 db = _dbus_user_database_get_system ();
415 _dbus_user_database_unlock_system ();
419 if (!_dbus_user_database_get_username (db, username,
422 _dbus_user_database_unlock_system ();
426 credentials->pid = DBUS_PID_UNSET;
427 credentials->uid = info->uid;
428 credentials->gid = info->primary_gid;
430 _dbus_user_database_unlock_system ();
435 * Creates a new user database object used to look up and
436 * cache user information.
437 * @returns new database, or #NULL on out of memory
440 _dbus_user_database_new (void)
442 DBusUserDatabase *db;
444 db = dbus_new0 (DBusUserDatabase, 1);
450 db->users = _dbus_hash_table_new (DBUS_HASH_ULONG,
451 NULL, (DBusFreeFunction) _dbus_user_info_free_allocated);
453 if (db->users == NULL)
456 db->groups = _dbus_hash_table_new (DBUS_HASH_ULONG,
457 NULL, (DBusFreeFunction) _dbus_group_info_free_allocated);
459 if (db->groups == NULL)
462 db->users_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
464 if (db->users_by_name == NULL)
467 db->groups_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
469 if (db->groups_by_name == NULL)
475 _dbus_user_database_unref (db);
479 #ifdef DBUS_BUILD_TESTS
481 * Increments refcount of user database.
482 * @param db the database
483 * @returns the database
486 _dbus_user_database_ref (DBusUserDatabase *db)
488 _dbus_assert (db->refcount > 0);
494 #endif /* DBUS_BUILD_TESTS */
497 * Decrements refcount of user database.
498 * @param db the database
501 _dbus_user_database_unref (DBusUserDatabase *db)
503 _dbus_assert (db->refcount > 0);
506 if (db->refcount == 0)
509 _dbus_hash_table_unref (db->users);
512 _dbus_hash_table_unref (db->groups);
514 if (db->users_by_name)
515 _dbus_hash_table_unref (db->users_by_name);
517 if (db->groups_by_name)
518 _dbus_hash_table_unref (db->groups_by_name);
525 * Gets the user information for the given UID,
526 * returned user info should not be freed.
528 * @param db user database
529 * @param uid the user ID
530 * @param info return location for const ref to user info
531 * @param error error location
532 * @returns #FALSE if error is set
535 _dbus_user_database_get_uid (DBusUserDatabase *db,
537 const DBusUserInfo **info,
540 *info = _dbus_user_database_lookup (db, uid, NULL, error);
541 return *info != NULL;
545 * Gets the user information for the given username.
547 * @param db user database
548 * @param username the user name
549 * @param info return location for const ref to user info
550 * @param error error location
551 * @returns #FALSE if error is set
554 _dbus_user_database_get_username (DBusUserDatabase *db,
555 const DBusString *username,
556 const DBusUserInfo **info,
559 *info = _dbus_user_database_lookup (db, DBUS_UID_UNSET, username, error);
560 return *info != NULL;
565 /* Tests in dbus-userdb-util.c */