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 #ifdef HAVE_CONSOLE_OWNER_FILE
56 if (!_dbus_string_init (&f))
58 _DBUS_SET_OOM (error);
62 if (!_dbus_string_append(&f, DBUS_CONSOLE_OWNER_FILE))
64 _dbus_string_free(&f);
65 _DBUS_SET_OOM (error);
69 if (_dbus_stat(&f, &st, NULL) && (st.uid == uid))
71 _dbus_string_free(&f);
75 _dbus_string_free(&f);
77 #endif /* HAVE_CONSOLE_OWNER_FILE */
79 _dbus_user_database_lock_system ();
81 db = _dbus_user_database_get_system ();
84 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get system database.");
85 _dbus_user_database_unlock_system ();
89 info = _dbus_user_database_lookup (db, uid, NULL, error);
93 _dbus_user_database_unlock_system ();
97 result = _dbus_user_at_console (info->username, error);
99 _dbus_user_database_unlock_system ();
106 * Gets the credentials corresponding to the given UID.
109 * @param credentials credentials to fill in
110 * @returns #TRUE if the UID existed and we got some credentials
113 _dbus_credentials_from_uid (dbus_uid_t uid,
114 DBusCredentials *credentials)
116 DBusUserDatabase *db;
117 const DBusUserInfo *info;
118 _dbus_user_database_lock_system ();
120 db = _dbus_user_database_get_system ();
123 _dbus_user_database_unlock_system ();
127 if (!_dbus_user_database_get_uid (db, uid,
130 _dbus_user_database_unlock_system ();
134 _dbus_assert (info->uid == uid);
136 credentials->pid = DBUS_PID_UNSET;
137 credentials->uid = info->uid;
138 credentials->gid = info->primary_gid;
140 _dbus_user_database_unlock_system ();
146 * Gets user ID given username
148 * @param username the username
149 * @param uid return location for UID
150 * @returns #TRUE if username existed and we got the UID
153 _dbus_get_user_id (const DBusString *username,
156 DBusCredentials creds;
158 if (!_dbus_credentials_from_username (username, &creds))
161 if (creds.uid == DBUS_UID_UNSET)
170 * Gets group ID given groupname
172 * @param groupname the groupname
173 * @param gid return location for GID
174 * @returns #TRUE if group name existed and we got the GID
177 _dbus_get_group_id (const DBusString *groupname,
180 DBusUserDatabase *db;
181 const DBusGroupInfo *info;
182 _dbus_user_database_lock_system ();
184 db = _dbus_user_database_get_system ();
187 _dbus_user_database_unlock_system ();
191 if (!_dbus_user_database_get_groupname (db, groupname,
194 _dbus_user_database_unlock_system ();
200 _dbus_user_database_unlock_system ();
205 * Looks up a gid or group name in the user database. Only one of
206 * name or GID can be provided. There are wrapper functions for this
207 * that are better to use, this one does no locking or anything on the
208 * database and otherwise sort of sucks.
210 * @param db the database
211 * @param gid the group ID or #DBUS_GID_UNSET
212 * @param groupname group name or #NULL
213 * @param error error to fill in
214 * @returns the entry in the database
217 _dbus_user_database_lookup_group (DBusUserDatabase *db,
219 const DBusString *groupname,
224 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
226 /* See if the group is really a number */
227 if (gid == DBUS_UID_UNSET)
231 if (_dbus_is_a_number (groupname, &n))
236 if (gid != DBUS_GID_UNSET)
237 info = _dbus_hash_table_lookup_ulong (db->groups, gid);
239 info = _dbus_hash_table_lookup_string (db->groups_by_name,
240 _dbus_string_get_const_data (groupname));
243 _dbus_verbose ("Using cache for GID "DBUS_GID_FORMAT" information\n",
249 if (gid != DBUS_GID_UNSET)
250 _dbus_verbose ("No cache for GID "DBUS_GID_FORMAT"\n",
253 _dbus_verbose ("No cache for groupname \"%s\"\n",
254 _dbus_string_get_const_data (groupname));
256 info = dbus_new0 (DBusGroupInfo, 1);
259 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
263 if (gid != DBUS_GID_UNSET)
265 if (!_dbus_group_info_fill_gid (info, gid, error))
267 _DBUS_ASSERT_ERROR_IS_SET (error);
268 _dbus_group_info_free_allocated (info);
274 if (!_dbus_group_info_fill (info, groupname, error))
276 _DBUS_ASSERT_ERROR_IS_SET (error);
277 _dbus_group_info_free_allocated (info);
282 /* don't use these past here */
283 gid = DBUS_GID_UNSET;
286 if (!_dbus_hash_table_insert_ulong (db->groups, info->gid, info))
288 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
289 _dbus_group_info_free_allocated (info);
294 if (!_dbus_hash_table_insert_string (db->groups_by_name,
298 _dbus_hash_table_remove_ulong (db->groups, info->gid);
299 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
309 * Gets the user information for the given group name,
310 * returned group info should not be freed.
312 * @param db user database
313 * @param groupname the group name
314 * @param info return location for const ref to group info
315 * @param error error location
316 * @returns #FALSE if error is set
319 _dbus_user_database_get_groupname (DBusUserDatabase *db,
320 const DBusString *groupname,
321 const DBusGroupInfo **info,
324 *info = _dbus_user_database_lookup_group (db, DBUS_GID_UNSET, groupname, error);
325 return *info != NULL;
329 * Gets the user information for the given GID,
330 * returned group info should not be freed.
332 * @param db user database
333 * @param gid the group ID
334 * @param info return location for const ref to group info
335 * @param error error location
336 * @returns #FALSE if error is set
339 _dbus_user_database_get_gid (DBusUserDatabase *db,
341 const DBusGroupInfo **info,
344 *info = _dbus_user_database_lookup_group (db, gid, NULL, error);
345 return *info != NULL;
350 * Gets all groups for a particular user. Returns #FALSE
351 * if no memory, or user isn't known, but always initializes
352 * group_ids to a NULL array. Sets error to the reason
353 * for returning #FALSE.
355 * @param db the user database object
356 * @param uid the user ID
357 * @param group_ids return location for array of group IDs
358 * @param n_group_ids return location for length of returned array
359 * @param error return location for error
360 * @returns #TRUE on success
363 _dbus_user_database_get_groups (DBusUserDatabase *db,
365 dbus_gid_t **group_ids,
371 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
376 info = _dbus_user_database_lookup (db, uid, NULL, error);
379 _DBUS_ASSERT_ERROR_IS_SET (error);
383 if (info->n_group_ids > 0)
385 *group_ids = dbus_new (dbus_gid_t, info->n_group_ids);
386 if (*group_ids == NULL)
388 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
392 *n_group_ids = info->n_group_ids;
394 memcpy (*group_ids, info->group_ids, info->n_group_ids * sizeof (dbus_gid_t));
402 #ifdef DBUS_BUILD_TESTS
406 * Unit test for dbus-userdb.c.
408 * @returns #TRUE on success.
411 _dbus_userdb_test (const char *test_data_dir)
413 const DBusString *username;
414 const DBusString *homedir;
416 if (!_dbus_username_from_current_process (&username))
417 _dbus_assert_not_reached ("didn't get username");
419 if (!_dbus_homedir_from_current_process (&homedir))
420 _dbus_assert_not_reached ("didn't get homedir");
422 printf (" Current user: %s homedir: %s\n",
423 _dbus_string_get_const_data (username),
424 _dbus_string_get_const_data (homedir));
428 #endif /* DBUS_BUILD_TESTS */