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"
29 #include "dbus-credentials.h"
33 * @addtogroup DBusInternalsUtils
38 * Frees the given #DBusUserInfo's members with _dbus_user_info_free()
39 * and also calls dbus_free() on the block itself
41 * @param info the info
44 _dbus_user_info_free_allocated (DBusUserInfo *info)
46 if (info == NULL) /* hash table will pass NULL */
49 _dbus_user_info_free (info);
54 * Frees the given #DBusGroupInfo's members with _dbus_group_info_free()
55 * and also calls dbus_free() on the block itself
57 * @param info the info
60 _dbus_group_info_free_allocated (DBusGroupInfo *info)
62 if (info == NULL) /* hash table will pass NULL */
65 _dbus_group_info_free (info);
70 * Frees the members of info
71 * (but not info itself)
72 * @param info the user info struct
75 _dbus_user_info_free (DBusUserInfo *info)
77 dbus_free (info->group_ids);
78 dbus_free (info->username);
79 dbus_free (info->homedir);
83 * Frees the members of info (but not info itself).
85 * @param info the group info
88 _dbus_group_info_free (DBusGroupInfo *info)
90 dbus_free (info->groupname);
94 * Checks if a given string is actually a number
95 * and converts it if it is
97 * @param str the string to check
98 * @param num the memory location of the unsigned long to fill in
99 * @returns TRUE if str is a number and num is filled in
102 _dbus_is_a_number (const DBusString *str,
107 if (_dbus_string_parse_uint (str, 0, num, &end) &&
108 end == _dbus_string_get_length (str))
115 * Looks up a uid or username in the user database. Only one of name
116 * or UID can be provided. There are wrapper functions for this that
117 * are better to use, this one does no locking or anything on the
118 * database and otherwise sort of sucks.
120 * @param db the database
121 * @param uid the user ID or #DBUS_UID_UNSET
122 * @param username username or #NULL
123 * @param error error to fill in
124 * @returns the entry in the database
127 _dbus_user_database_lookup (DBusUserDatabase *db,
129 const DBusString *username,
134 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
135 _dbus_assert (uid != DBUS_UID_UNSET || username != NULL);
137 /* See if the username is really a number */
138 if (uid == DBUS_UID_UNSET)
142 if (_dbus_is_a_number (username, &n))
146 #ifdef DBUS_ENABLE_USER_CACHE
147 if (uid != DBUS_UID_UNSET)
148 info = _dbus_hash_table_lookup_ulong (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",
163 if (uid != DBUS_UID_UNSET)
164 _dbus_verbose ("No cache for UID "DBUS_UID_FORMAT"\n",
167 _dbus_verbose ("No cache for user \"%s\"\n",
168 _dbus_string_get_const_data (username));
170 info = dbus_new0 (DBusUserInfo, 1);
173 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
177 if (uid != DBUS_UID_UNSET)
179 if (!_dbus_user_info_fill_uid (info, uid, error))
181 _DBUS_ASSERT_ERROR_IS_SET (error);
182 _dbus_user_info_free_allocated (info);
188 if (!_dbus_user_info_fill (info, username, error))
190 _DBUS_ASSERT_ERROR_IS_SET (error);
191 _dbus_user_info_free_allocated (info);
196 /* be sure we don't use these after here */
197 uid = DBUS_UID_UNSET;
200 /* insert into hash */
201 if (!_dbus_hash_table_insert_ulong (db->users, info->uid, info))
203 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
204 _dbus_user_info_free_allocated (info);
208 if (!_dbus_hash_table_insert_string (db->users_by_name,
212 _dbus_hash_table_remove_ulong (db->users, info->uid);
213 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
221 _DBUS_DEFINE_GLOBAL_LOCK(system_users);
222 static dbus_bool_t database_locked = FALSE;
223 static DBusUserDatabase *system_db = NULL;
224 static DBusString process_username;
225 static DBusString process_homedir;
228 shutdown_system_db (void *data)
230 _dbus_user_database_unref (system_db);
232 _dbus_string_free (&process_username);
233 _dbus_string_free (&process_homedir);
237 init_system_db (void)
239 _dbus_assert (database_locked);
241 if (system_db == NULL)
244 const DBusUserInfo *info;
246 system_db = _dbus_user_database_new ();
247 if (system_db == NULL)
250 dbus_error_init (&error);
252 if (!_dbus_user_database_get_uid (system_db,
257 _dbus_user_database_unref (system_db);
260 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
262 dbus_error_free (&error);
267 /* This really should not happen. */
268 _dbus_warn ("Could not get password database information for UID of current process: %s\n",
270 dbus_error_free (&error);
275 if (!_dbus_string_init (&process_username))
277 _dbus_user_database_unref (system_db);
282 if (!_dbus_string_init (&process_homedir))
284 _dbus_string_free (&process_username);
285 _dbus_user_database_unref (system_db);
290 if (!_dbus_string_append (&process_username,
292 !_dbus_string_append (&process_homedir,
294 !_dbus_register_shutdown_func (shutdown_system_db, NULL))
296 _dbus_string_free (&process_username);
297 _dbus_string_free (&process_homedir);
298 _dbus_user_database_unref (system_db);
308 * Locks global system user database.
311 _dbus_user_database_lock_system (void)
313 _DBUS_LOCK (system_users);
314 database_locked = TRUE;
318 * Unlocks global system user database.
321 _dbus_user_database_unlock_system (void)
323 database_locked = FALSE;
324 _DBUS_UNLOCK (system_users);
328 * Gets the system global user database;
329 * must be called with lock held (_dbus_user_database_lock_system()).
331 * @returns the database or #NULL if no memory
334 _dbus_user_database_get_system (void)
336 _dbus_assert (database_locked);
344 * Flushes the system global user database;
347 _dbus_user_database_flush_system (void)
349 _dbus_user_database_lock_system ();
351 _dbus_user_database_flush (system_db);
353 _dbus_user_database_unlock_system ();
357 * Gets username of user owning current process. The returned string
358 * is valid until dbus_shutdown() is called.
360 * @param username place to store pointer to username
361 * @returns #FALSE if no memory
364 _dbus_append_desired_identity (const DBusString **username)
366 _dbus_user_database_lock_system ();
367 if (!init_system_db ())
369 _dbus_user_database_unlock_system ();
372 *username = &process_username;
373 _dbus_user_database_unlock_system ();
379 * Gets homedir of user owning current process. The returned string
380 * is valid until dbus_shutdown() is called.
382 * @param homedir place to store pointer to homedir
383 * @returns #FALSE if no memory
386 _dbus_homedir_from_current_process (const DBusString **homedir)
388 _dbus_user_database_lock_system ();
389 if (!init_system_db ())
391 _dbus_user_database_unlock_system ();
394 *homedir = &process_homedir;
395 _dbus_user_database_unlock_system ();
401 * Gets the home directory for the given user.
403 * @param username the username
404 * @param homedir string to append home directory to
405 * @returns #TRUE if user existed and we appended their homedir
408 _dbus_homedir_from_username (const DBusString *username,
411 DBusUserDatabase *db;
412 const DBusUserInfo *info;
413 _dbus_user_database_lock_system ();
415 db = _dbus_user_database_get_system ();
418 _dbus_user_database_unlock_system ();
422 if (!_dbus_user_database_get_username (db, username,
425 _dbus_user_database_unlock_system ();
429 if (!_dbus_string_append (homedir, info->homedir))
431 _dbus_user_database_unlock_system ();
435 _dbus_user_database_unlock_system ();
440 * Adds the credentials corresponding to the given username.
442 * @param credentials credentials to fill in
443 * @param username the username
444 * @returns #TRUE if the username existed and we got some credentials
447 _dbus_credentials_add_from_username (DBusCredentials *credentials,
448 const DBusString *username)
450 DBusUserDatabase *db;
451 const DBusUserInfo *info;
453 _dbus_user_database_lock_system ();
455 db = _dbus_user_database_get_system ();
458 _dbus_user_database_unlock_system ();
462 if (!_dbus_user_database_get_username (db, username,
465 _dbus_user_database_unlock_system ();
469 if (!_dbus_credentials_add_unix_uid(credentials, info->uid))
471 _dbus_user_database_unlock_system ();
475 _dbus_user_database_unlock_system ();
480 * Creates a new user database object used to look up and
481 * cache user information.
482 * @returns new database, or #NULL on out of memory
485 _dbus_user_database_new (void)
487 DBusUserDatabase *db;
489 db = dbus_new0 (DBusUserDatabase, 1);
495 db->users = _dbus_hash_table_new (DBUS_HASH_ULONG,
496 NULL, (DBusFreeFunction) _dbus_user_info_free_allocated);
498 if (db->users == NULL)
501 db->groups = _dbus_hash_table_new (DBUS_HASH_ULONG,
502 NULL, (DBusFreeFunction) _dbus_group_info_free_allocated);
504 if (db->groups == NULL)
507 db->users_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
509 if (db->users_by_name == NULL)
512 db->groups_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
514 if (db->groups_by_name == NULL)
520 _dbus_user_database_unref (db);
525 * Flush all information out of the user database.
528 _dbus_user_database_flush (DBusUserDatabase *db)
530 _dbus_hash_table_remove_all(db->users_by_name);
531 _dbus_hash_table_remove_all(db->groups_by_name);
532 _dbus_hash_table_remove_all(db->users);
533 _dbus_hash_table_remove_all(db->groups);
536 #ifdef DBUS_BUILD_TESTS
538 * Increments refcount of user database.
539 * @param db the database
540 * @returns the database
543 _dbus_user_database_ref (DBusUserDatabase *db)
545 _dbus_assert (db->refcount > 0);
551 #endif /* DBUS_BUILD_TESTS */
554 * Decrements refcount of user database.
555 * @param db the database
558 _dbus_user_database_unref (DBusUserDatabase *db)
560 _dbus_assert (db->refcount > 0);
563 if (db->refcount == 0)
566 _dbus_hash_table_unref (db->users);
569 _dbus_hash_table_unref (db->groups);
571 if (db->users_by_name)
572 _dbus_hash_table_unref (db->users_by_name);
574 if (db->groups_by_name)
575 _dbus_hash_table_unref (db->groups_by_name);
582 * Gets the user information for the given UID,
583 * returned user info should not be freed.
585 * @param db user database
586 * @param uid the user ID
587 * @param info return location for const ref to user info
588 * @param error error location
589 * @returns #FALSE if error is set
592 _dbus_user_database_get_uid (DBusUserDatabase *db,
594 const DBusUserInfo **info,
597 *info = _dbus_user_database_lookup (db, uid, NULL, error);
598 return *info != NULL;
602 * Gets the user information for the given username.
604 * @param db user database
605 * @param username the user name
606 * @param info return location for const ref to user info
607 * @param error error location
608 * @returns #FALSE if error is set
611 _dbus_user_database_get_username (DBusUserDatabase *db,
612 const DBusString *username,
613 const DBusUserInfo **info,
616 *info = _dbus_user_database_lookup (db, DBUS_UID_UNSET, username, error);
617 return *info != NULL;
622 /* Tests in dbus-userdb-util.c */