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))
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",
251 if (gid != DBUS_GID_UNSET)
252 _dbus_verbose ("No cache for GID "DBUS_GID_FORMAT"\n",
255 _dbus_verbose ("No cache for groupname \"%s\"\n",
256 _dbus_string_get_const_data (groupname));
258 info = dbus_new0 (DBusGroupInfo, 1);
261 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
265 if (gid != DBUS_GID_UNSET)
267 if (!_dbus_group_info_fill_gid (info, gid, error))
269 _DBUS_ASSERT_ERROR_IS_SET (error);
270 _dbus_group_info_free_allocated (info);
276 if (!_dbus_group_info_fill (info, groupname, error))
278 _DBUS_ASSERT_ERROR_IS_SET (error);
279 _dbus_group_info_free_allocated (info);
284 /* don't use these past here */
285 gid = DBUS_GID_UNSET;
288 if (!_dbus_hash_table_insert_ulong (db->groups, info->gid, info))
290 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
291 _dbus_group_info_free_allocated (info);
296 if (!_dbus_hash_table_insert_string (db->groups_by_name,
300 _dbus_hash_table_remove_ulong (db->groups, info->gid);
301 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
311 * Gets the user information for the given group name,
312 * returned group info should not be freed.
314 * @param db user database
315 * @param groupname the group name
316 * @param info return location for const ref to group info
317 * @param error error location
318 * @returns #FALSE if error is set
321 _dbus_user_database_get_groupname (DBusUserDatabase *db,
322 const DBusString *groupname,
323 const DBusGroupInfo **info,
326 *info = _dbus_user_database_lookup_group (db, DBUS_GID_UNSET, groupname, error);
327 return *info != NULL;
331 * Gets the user information for the given GID,
332 * returned group info should not be freed.
334 * @param db user database
335 * @param gid the group ID
336 * @param info return location for const ref to group info
337 * @param error error location
338 * @returns #FALSE if error is set
341 _dbus_user_database_get_gid (DBusUserDatabase *db,
343 const DBusGroupInfo **info,
346 *info = _dbus_user_database_lookup_group (db, gid, NULL, error);
347 return *info != NULL;
352 * Gets all groups corresponding to the given UID. Returns #FALSE
353 * if no memory, or user isn't known, but always initializes
354 * group_ids to a NULL array.
357 * @param group_ids return location for array of group IDs
358 * @param n_group_ids return location for length of returned array
359 * @returns #TRUE if the UID existed and we got some credentials
362 _dbus_groups_from_uid (dbus_uid_t uid,
363 dbus_gid_t **group_ids,
366 DBusUserDatabase *db;
367 const DBusUserInfo *info;
371 _dbus_user_database_lock_system ();
373 db = _dbus_user_database_get_system ();
376 _dbus_user_database_unlock_system ();
380 if (!_dbus_user_database_get_uid (db, uid,
383 _dbus_user_database_unlock_system ();
387 _dbus_assert (info->uid == uid);
389 if (info->n_group_ids > 0)
391 *group_ids = dbus_new (dbus_gid_t, info->n_group_ids);
392 if (*group_ids == NULL)
394 _dbus_user_database_unlock_system ();
398 *n_group_ids = info->n_group_ids;
400 memcpy (*group_ids, info->group_ids, info->n_group_ids * sizeof (dbus_gid_t));
403 _dbus_user_database_unlock_system ();
408 #ifdef DBUS_BUILD_TESTS
412 * Unit test for dbus-userdb.c.
414 * @returns #TRUE on success.
417 _dbus_userdb_test (const char *test_data_dir)
419 const DBusString *username;
420 const DBusString *homedir;
422 unsigned long *group_ids;
425 if (!_dbus_username_from_current_process (&username))
426 _dbus_assert_not_reached ("didn't get username");
428 if (!_dbus_homedir_from_current_process (&homedir))
429 _dbus_assert_not_reached ("didn't get homedir");
431 if (!_dbus_get_user_id (username, &uid))
432 _dbus_assert_not_reached ("didn't get uid");
435 if (!_dbus_groups_from_uid (uid, &group_ids, &n_group_ids))
436 _dbus_assert_not_reached ("didn't get groups");
438 printf (" Current user: %s homedir: %s gids:",
439 _dbus_string_get_const_data (username),
440 _dbus_string_get_const_data (homedir));
442 for (i=0; i<n_group_ids; i++)
443 printf(" %ld", group_ids[i]);
447 dbus_free (group_ids);
451 #endif /* DBUS_BUILD_TESTS */