1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-userdb-util.c Would be in dbus-userdb.c, but not used in libdbus
4 * Copyright (C) 2003, 2004, 2005 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-test.h"
26 #include "dbus-internals.h"
27 #include "dbus-protocol.h"
31 * @addtogroup DBusInternalsUtils
37 * Checks to see if the UID sent in is the console user
39 * @param uid UID of person to check
40 * @param error return location for errors
41 * @returns #TRUE if the UID is the same as the console user and there are no errors
44 _dbus_is_console_user (dbus_uid_t uid,
49 const DBusUserInfo *info;
50 dbus_bool_t result = FALSE;
52 _dbus_user_database_lock_system ();
54 db = _dbus_user_database_get_system ();
57 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get system database.");
58 _dbus_user_database_unlock_system ();
62 info = _dbus_user_database_lookup (db, uid, NULL, error);
66 _dbus_user_database_unlock_system ();
70 result = _dbus_user_at_console (info->username, error);
72 _dbus_user_database_unlock_system ();
79 * Gets the credentials corresponding to the given UID.
82 * @param credentials credentials to fill in
83 * @returns #TRUE if the UID existed and we got some credentials
86 _dbus_credentials_from_uid (dbus_uid_t uid,
87 DBusCredentials *credentials)
90 const DBusUserInfo *info;
91 _dbus_user_database_lock_system ();
93 db = _dbus_user_database_get_system ();
96 _dbus_user_database_unlock_system ();
100 if (!_dbus_user_database_get_uid (db, uid,
103 _dbus_user_database_unlock_system ();
107 _dbus_assert (info->uid == uid);
109 credentials->pid = DBUS_PID_UNSET;
110 credentials->uid = info->uid;
111 credentials->gid = info->primary_gid;
113 _dbus_user_database_unlock_system ();
119 * Gets user ID given username
121 * @param username the username
122 * @param uid return location for UID
123 * @returns #TRUE if username existed and we got the UID
126 _dbus_get_user_id (const DBusString *username,
129 DBusCredentials creds;
131 if (!_dbus_credentials_from_username (username, &creds))
134 if (creds.uid == DBUS_UID_UNSET)
143 * Gets group ID given groupname
145 * @param groupname the groupname
146 * @param gid return location for GID
147 * @returns #TRUE if group name existed and we got the GID
150 _dbus_get_group_id (const DBusString *groupname,
153 DBusUserDatabase *db;
154 const DBusGroupInfo *info;
155 _dbus_user_database_lock_system ();
157 db = _dbus_user_database_get_system ();
160 _dbus_user_database_unlock_system ();
164 if (!_dbus_user_database_get_groupname (db, groupname,
167 _dbus_user_database_unlock_system ();
173 _dbus_user_database_unlock_system ();
178 * Looks up a gid or group name in the user database. Only one of
179 * name or GID can be provided. There are wrapper functions for this
180 * that are better to use, this one does no locking or anything on the
181 * database and otherwise sort of sucks.
183 * @param db the database
184 * @param gid the group ID or #DBUS_GID_UNSET
185 * @param groupname group name or #NULL
186 * @param error error to fill in
187 * @returns the entry in the database
190 _dbus_user_database_lookup_group (DBusUserDatabase *db,
192 const DBusString *groupname,
197 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
199 if (gid != DBUS_GID_UNSET)
200 info = _dbus_hash_table_lookup_ulong (db->groups, gid);
202 info = _dbus_hash_table_lookup_string (db->groups_by_name,
203 _dbus_string_get_const_data (groupname));
206 _dbus_verbose ("Using cache for GID "DBUS_GID_FORMAT" information\n",
212 _dbus_verbose ("No cache for GID "DBUS_GID_FORMAT"\n",
215 info = dbus_new0 (DBusGroupInfo, 1);
218 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
222 if (!_dbus_group_info_fill_gid (info, gid, error))
224 _DBUS_ASSERT_ERROR_IS_SET (error);
225 _dbus_group_info_free_allocated (info);
229 if (!_dbus_hash_table_insert_ulong (db->groups, info->gid, info))
231 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
232 _dbus_group_info_free_allocated (info);
237 if (!_dbus_hash_table_insert_string (db->groups_by_name,
241 _dbus_hash_table_remove_ulong (db->groups, info->gid);
242 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
252 * Gets the user information for the given group name,
253 * returned group info should not be freed.
255 * @param db user database
256 * @param groupname the group name
257 * @param info return location for const ref to group info
258 * @param error error location
259 * @returns #FALSE if error is set
262 _dbus_user_database_get_groupname (DBusUserDatabase *db,
263 const DBusString *groupname,
264 const DBusGroupInfo **info,
267 *info = _dbus_user_database_lookup_group (db, DBUS_GID_UNSET, groupname, error);
268 return *info != NULL;
272 * Gets the user information for the given GID,
273 * returned group info should not be freed.
275 * @param db user database
276 * @param gid the group ID
277 * @param info return location for const ref to group info
278 * @param error error location
279 * @returns #FALSE if error is set
282 _dbus_user_database_get_gid (DBusUserDatabase *db,
284 const DBusGroupInfo **info,
287 *info = _dbus_user_database_lookup_group (db, gid, NULL, error);
288 return *info != NULL;
293 * Gets all groups for a particular user. Returns #FALSE
294 * if no memory, or user isn't known, but always initializes
295 * group_ids to a NULL array. Sets error to the reason
296 * for returning #FALSE.
298 * @param db the user database object
299 * @param uid the user ID
300 * @param group_ids return location for array of group IDs
301 * @param n_group_ids return location for length of returned array
302 * @param error return location for error
303 * @returns #TRUE on success
306 _dbus_user_database_get_groups (DBusUserDatabase *db,
308 dbus_gid_t **group_ids,
314 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
319 info = _dbus_user_database_lookup (db, uid, NULL, error);
322 _DBUS_ASSERT_ERROR_IS_SET (error);
326 if (info->n_group_ids > 0)
328 *group_ids = dbus_new (dbus_gid_t, info->n_group_ids);
329 if (*group_ids == NULL)
331 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
335 *n_group_ids = info->n_group_ids;
337 memcpy (*group_ids, info->group_ids, info->n_group_ids * sizeof (dbus_gid_t));
345 #ifdef DBUS_BUILD_TESTS
349 * Unit test for dbus-userdb.c.
351 * @returns #TRUE on success.
354 _dbus_userdb_test (const char *test_data_dir)
356 const DBusString *username;
357 const DBusString *homedir;
359 if (!_dbus_username_from_current_process (&username))
360 _dbus_assert_not_reached ("didn't get username");
362 if (!_dbus_homedir_from_current_process (&homedir))
363 _dbus_assert_not_reached ("didn't get homedir");
365 printf (" Current user: %s homedir: %s\n",
366 _dbus_string_get_const_data (username),
367 _dbus_string_get_const_data (homedir));
371 #endif /* DBUS_BUILD_TESTS */