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 /* TPTD: this should be cache-safe, we've locked the DB and
90 _dbus_user_at_console doesn't pass it on. */
91 info = _dbus_user_database_lookup (db, uid, NULL, error);
95 _dbus_user_database_unlock_system ();
99 result = _dbus_user_at_console (info->username, error);
101 _dbus_user_database_unlock_system ();
108 * Gets the credentials corresponding to the given UID.
111 * @param credentials credentials to fill in
112 * @returns #TRUE if the UID existed and we got some credentials
115 _dbus_credentials_from_uid (dbus_uid_t uid,
116 DBusCredentials *credentials)
118 DBusUserDatabase *db;
119 const DBusUserInfo *info;
120 _dbus_user_database_lock_system ();
122 db = _dbus_user_database_get_system ();
125 _dbus_user_database_unlock_system ();
129 if (!_dbus_user_database_get_uid (db, uid,
132 _dbus_user_database_unlock_system ();
136 _dbus_assert (info->uid == uid);
138 credentials->pid = DBUS_PID_UNSET;
139 credentials->uid = info->uid;
140 credentials->gid = info->primary_gid;
142 _dbus_user_database_unlock_system ();
148 * Gets user ID given username
150 * @param username the username
151 * @param uid return location for UID
152 * @returns #TRUE if username existed and we got the UID
155 _dbus_get_user_id (const DBusString *username,
158 DBusCredentials creds;
160 if (!_dbus_credentials_from_username (username, &creds))
163 if (creds.uid == DBUS_UID_UNSET)
172 * Gets group ID given groupname
174 * @param groupname the groupname
175 * @param gid return location for GID
176 * @returns #TRUE if group name existed and we got the GID
179 _dbus_get_group_id (const DBusString *groupname,
182 DBusUserDatabase *db;
183 const DBusGroupInfo *info;
184 _dbus_user_database_lock_system ();
186 db = _dbus_user_database_get_system ();
189 _dbus_user_database_unlock_system ();
193 if (!_dbus_user_database_get_groupname (db, groupname,
196 _dbus_user_database_unlock_system ();
202 _dbus_user_database_unlock_system ();
207 * Looks up a gid or group name in the user database. Only one of
208 * name or GID can be provided. There are wrapper functions for this
209 * that are better to use, this one does no locking or anything on the
210 * database and otherwise sort of sucks.
212 * @param db the database
213 * @param gid the group ID or #DBUS_GID_UNSET
214 * @param groupname group name or #NULL
215 * @param error error to fill in
216 * @returns the entry in the database
219 _dbus_user_database_lookup_group (DBusUserDatabase *db,
221 const DBusString *groupname,
226 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
228 /* See if the group is really a number */
229 if (gid == DBUS_UID_UNSET)
233 if (_dbus_is_a_number (groupname, &n))
237 #ifdef DBUS_ENABLE_USER_CACHE
238 if (gid != DBUS_GID_UNSET)
239 info = _dbus_hash_table_lookup_ulong (db->groups, gid);
241 info = _dbus_hash_table_lookup_string (db->groups_by_name,
242 _dbus_string_get_const_data (groupname));
245 _dbus_verbose ("Using cache for GID "DBUS_GID_FORMAT" information\n",
254 if (gid != DBUS_GID_UNSET)
255 _dbus_verbose ("No cache for GID "DBUS_GID_FORMAT"\n",
258 _dbus_verbose ("No cache for groupname \"%s\"\n",
259 _dbus_string_get_const_data (groupname));
261 info = dbus_new0 (DBusGroupInfo, 1);
264 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
268 if (gid != DBUS_GID_UNSET)
270 if (!_dbus_group_info_fill_gid (info, gid, error))
272 _DBUS_ASSERT_ERROR_IS_SET (error);
273 _dbus_group_info_free_allocated (info);
279 if (!_dbus_group_info_fill (info, groupname, error))
281 _DBUS_ASSERT_ERROR_IS_SET (error);
282 _dbus_group_info_free_allocated (info);
287 /* don't use these past here */
288 gid = DBUS_GID_UNSET;
291 if (!_dbus_hash_table_insert_ulong (db->groups, info->gid, info))
293 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
294 _dbus_group_info_free_allocated (info);
299 if (!_dbus_hash_table_insert_string (db->groups_by_name,
303 _dbus_hash_table_remove_ulong (db->groups, info->gid);
304 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
314 * Gets the user information for the given group name,
315 * returned group info should not be freed.
317 * @param db user database
318 * @param groupname the group name
319 * @param info return location for const ref to group info
320 * @param error error location
321 * @returns #FALSE if error is set
324 _dbus_user_database_get_groupname (DBusUserDatabase *db,
325 const DBusString *groupname,
326 const DBusGroupInfo **info,
329 *info = _dbus_user_database_lookup_group (db, DBUS_GID_UNSET, groupname, error);
330 return *info != NULL;
334 * Gets the user information for the given GID,
335 * returned group info should not be freed.
337 * @param db user database
338 * @param gid the group ID
339 * @param info return location for const ref to group info
340 * @param error error location
341 * @returns #FALSE if error is set
344 _dbus_user_database_get_gid (DBusUserDatabase *db,
346 const DBusGroupInfo **info,
349 *info = _dbus_user_database_lookup_group (db, gid, NULL, error);
350 return *info != NULL;
355 * Gets all groups corresponding to the given UID. Returns #FALSE
356 * if no memory, or user isn't known, but always initializes
357 * group_ids to a NULL array.
360 * @param group_ids return location for array of group IDs
361 * @param n_group_ids return location for length of returned array
362 * @returns #TRUE if the UID existed and we got some credentials
365 _dbus_groups_from_uid (dbus_uid_t uid,
366 dbus_gid_t **group_ids,
369 DBusUserDatabase *db;
370 const DBusUserInfo *info;
374 _dbus_user_database_lock_system ();
376 db = _dbus_user_database_get_system ();
379 _dbus_user_database_unlock_system ();
383 if (!_dbus_user_database_get_uid (db, uid,
386 _dbus_user_database_unlock_system ();
390 _dbus_assert (info->uid == uid);
392 if (info->n_group_ids > 0)
394 *group_ids = dbus_new (dbus_gid_t, info->n_group_ids);
395 if (*group_ids == NULL)
397 _dbus_user_database_unlock_system ();
401 *n_group_ids = info->n_group_ids;
403 memcpy (*group_ids, info->group_ids, info->n_group_ids * sizeof (dbus_gid_t));
406 _dbus_user_database_unlock_system ();
411 #ifdef DBUS_BUILD_TESTS
415 * Unit test for dbus-userdb.c.
417 * @returns #TRUE on success.
420 _dbus_userdb_test (const char *test_data_dir)
422 const DBusString *username;
423 const DBusString *homedir;
425 unsigned long *group_ids;
428 if (!_dbus_username_from_current_process (&username))
429 _dbus_assert_not_reached ("didn't get username");
431 if (!_dbus_homedir_from_current_process (&homedir))
432 _dbus_assert_not_reached ("didn't get homedir");
434 if (!_dbus_get_user_id (username, &uid))
435 _dbus_assert_not_reached ("didn't get uid");
438 if (!_dbus_groups_from_uid (uid, &group_ids, &n_group_ids))
439 _dbus_assert_not_reached ("didn't get groups");
441 printf (" Current user: %s homedir: %s gids:",
442 _dbus_string_get_const_data (username),
443 _dbus_string_get_const_data (homedir));
445 for (i=0; i<n_group_ids; i++)
446 printf(" %ld", group_ids[i]);
450 dbus_free (group_ids);
454 #endif /* DBUS_BUILD_TESTS */