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
36 * Checks to see if the UID sent in is the console user
38 * @param uid UID of person to check
39 * @param error return location for errors
40 * @returns #TRUE if the UID is the same as the console user and there are no errors
43 _dbus_is_console_user (dbus_uid_t uid,
48 const DBusUserInfo *info;
49 dbus_bool_t result = FALSE;
51 _dbus_user_database_lock_system ();
53 db = _dbus_user_database_get_system ();
56 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get system database.");
57 _dbus_user_database_unlock_system ();
61 info = _dbus_user_database_lookup (db, uid, NULL, error);
65 _dbus_user_database_unlock_system ();
69 result = _dbus_user_at_console (info->username, error);
71 _dbus_user_database_unlock_system ();
78 * Gets the credentials corresponding to the given UID.
81 * @param credentials credentials to fill in
82 * @returns #TRUE if the UID existed and we got some credentials
85 _dbus_credentials_from_uid (dbus_uid_t uid,
86 DBusCredentials *credentials)
89 const DBusUserInfo *info;
90 _dbus_user_database_lock_system ();
92 db = _dbus_user_database_get_system ();
95 _dbus_user_database_unlock_system ();
99 if (!_dbus_user_database_get_uid (db, uid,
102 _dbus_user_database_unlock_system ();
106 _dbus_assert (info->uid == uid);
108 credentials->pid = DBUS_PID_UNSET;
109 credentials->uid = info->uid;
110 credentials->gid = info->primary_gid;
112 _dbus_user_database_unlock_system ();
118 * Gets user ID given username
120 * @param username the username
121 * @param uid return location for UID
122 * @returns #TRUE if username existed and we got the UID
125 _dbus_get_user_id (const DBusString *username,
128 DBusCredentials creds;
130 if (!_dbus_credentials_from_username (username, &creds))
133 if (creds.uid == DBUS_UID_UNSET)
142 * Gets group ID given groupname
144 * @param groupname the groupname
145 * @param gid return location for GID
146 * @returns #TRUE if group name existed and we got the GID
149 _dbus_get_group_id (const DBusString *groupname,
152 DBusUserDatabase *db;
153 const DBusGroupInfo *info;
154 _dbus_user_database_lock_system ();
156 db = _dbus_user_database_get_system ();
159 _dbus_user_database_unlock_system ();
163 if (!_dbus_user_database_get_groupname (db, groupname,
166 _dbus_user_database_unlock_system ();
172 _dbus_user_database_unlock_system ();
177 * Looks up a gid or group name in the user database. Only one of
178 * name or GID can be provided. There are wrapper functions for this
179 * that are better to use, this one does no locking or anything on the
180 * database and otherwise sort of sucks.
182 * @param db the database
183 * @param gid the group ID or #DBUS_GID_UNSET
184 * @param groupname group name or #NULL
185 * @param error error to fill in
186 * @returns the entry in the database
189 _dbus_user_database_lookup_group (DBusUserDatabase *db,
191 const DBusString *groupname,
196 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
198 if (gid != DBUS_GID_UNSET)
199 info = _dbus_hash_table_lookup_ulong (db->groups, gid);
201 info = _dbus_hash_table_lookup_string (db->groups_by_name,
202 _dbus_string_get_const_data (groupname));
205 _dbus_verbose ("Using cache for GID "DBUS_GID_FORMAT" information\n",
211 _dbus_verbose ("No cache for GID "DBUS_GID_FORMAT"\n",
214 info = dbus_new0 (DBusGroupInfo, 1);
217 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
221 if (gid != DBUS_GID_UNSET)
223 if (!_dbus_group_info_fill_gid (info, gid, error))
225 _DBUS_ASSERT_ERROR_IS_SET (error);
226 _dbus_group_info_free_allocated (info);
232 if (!_dbus_group_info_fill (info, groupname, error))
234 _DBUS_ASSERT_ERROR_IS_SET (error);
235 _dbus_group_info_free_allocated (info);
240 /* don't use these past here */
241 gid = DBUS_GID_UNSET;
244 if (!_dbus_hash_table_insert_ulong (db->groups, info->gid, info))
246 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
247 _dbus_group_info_free_allocated (info);
252 if (!_dbus_hash_table_insert_string (db->groups_by_name,
256 _dbus_hash_table_remove_ulong (db->groups, info->gid);
257 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
267 * Gets the user information for the given group name,
268 * returned group info should not be freed.
270 * @param db user database
271 * @param groupname the group name
272 * @param info return location for const ref to group info
273 * @param error error location
274 * @returns #FALSE if error is set
277 _dbus_user_database_get_groupname (DBusUserDatabase *db,
278 const DBusString *groupname,
279 const DBusGroupInfo **info,
282 *info = _dbus_user_database_lookup_group (db, DBUS_GID_UNSET, groupname, error);
283 return *info != NULL;
287 * Gets the user information for the given GID,
288 * returned group info should not be freed.
290 * @param db user database
291 * @param gid the group ID
292 * @param info return location for const ref to group info
293 * @param error error location
294 * @returns #FALSE if error is set
297 _dbus_user_database_get_gid (DBusUserDatabase *db,
299 const DBusGroupInfo **info,
302 *info = _dbus_user_database_lookup_group (db, gid, NULL, error);
303 return *info != NULL;
308 * Gets all groups for a particular user. Returns #FALSE
309 * if no memory, or user isn't known, but always initializes
310 * group_ids to a NULL array. Sets error to the reason
311 * for returning #FALSE.
313 * @param db the user database object
314 * @param uid the user ID
315 * @param group_ids return location for array of group IDs
316 * @param n_group_ids return location for length of returned array
317 * @param error return location for error
318 * @returns #TRUE on success
321 _dbus_user_database_get_groups (DBusUserDatabase *db,
323 dbus_gid_t **group_ids,
329 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
334 info = _dbus_user_database_lookup (db, uid, NULL, error);
337 _DBUS_ASSERT_ERROR_IS_SET (error);
341 if (info->n_group_ids > 0)
343 *group_ids = dbus_new (dbus_gid_t, info->n_group_ids);
344 if (*group_ids == NULL)
346 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
350 *n_group_ids = info->n_group_ids;
352 memcpy (*group_ids, info->group_ids, info->n_group_ids * sizeof (dbus_gid_t));
360 #ifdef DBUS_BUILD_TESTS
364 * Unit test for dbus-userdb.c.
366 * @returns #TRUE on success.
369 _dbus_userdb_test (const char *test_data_dir)
371 const DBusString *username;
372 const DBusString *homedir;
374 if (!_dbus_username_from_current_process (&username))
375 _dbus_assert_not_reached ("didn't get username");
377 if (!_dbus_homedir_from_current_process (&homedir))
378 _dbus_assert_not_reached ("didn't get homedir");
380 printf (" Current user: %s homedir: %s\n",
381 _dbus_string_get_const_data (username),
382 _dbus_string_get_const_data (homedir));
386 #endif /* DBUS_BUILD_TESTS */