1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-userdb.c User database abstraction
4 * Copyright (C) 2003 Red Hat, Inc.
6 * Licensed under the Academic Free License version 1.2
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 #include "dbus-userdb.h"
24 #include "dbus-hash.h"
25 #include "dbus-test.h"
26 #include "dbus-internals.h"
29 typedef struct DBusUserEntry DBusUserEntry;
35 dbus_gid_t *group_ids;
39 struct DBusUserDatabase
47 free_user_entry (void *data)
49 DBusUserEntry *entry = data;
51 if (entry == NULL) /* hash table will pass NULL */
54 dbus_free (entry->group_ids);
60 _dbus_user_database_lookup (DBusUserDatabase *db,
66 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
68 entry = _dbus_hash_table_lookup_ulong (db->users, uid);
73 entry = dbus_new0 (DBusUserEntry, 1);
76 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
80 if (!_dbus_get_groups (uid, &entry->group_ids, &entry->n_group_ids, error))
82 _DBUS_ASSERT_ERROR_IS_SET (error);
83 free_user_entry (entry);
87 if (!_dbus_hash_table_insert_ulong (db->users, entry->uid, entry))
89 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
90 free_user_entry (entry);
99 * @addtogroup DBusInternalsUtils
104 * Creates a new user database object used to look up and
105 * cache user information.
106 * @returns new database, or #NULL on out of memory
109 _dbus_user_database_new (void)
111 DBusUserDatabase *db;
113 db = dbus_new0 (DBusUserDatabase, 1);
119 db->users = _dbus_hash_table_new (DBUS_HASH_ULONG,
120 NULL, free_user_entry);
122 if (db->users == NULL)
128 _dbus_user_database_unref (db);
133 * Increments refcount of user database.
134 * @param db the database
137 _dbus_user_database_ref (DBusUserDatabase *db)
139 _dbus_assert (db->refcount > 0);
145 * Decrements refcount of user database.
146 * @param db the database
149 _dbus_user_database_unref (DBusUserDatabase *db)
151 _dbus_assert (db->refcount > 0);
154 if (db->refcount == 0)
157 _dbus_hash_table_unref (db->users);
164 * Gets all groups for a particular user. Returns #FALSE
165 * if no memory, or user isn't known, but always initializes
166 * group_ids to a NULL array. Sets error to the reason
167 * for returning #FALSE.
169 * @param db the user database object
170 * @param uid the user ID
171 * @param group_ids return location for array of group IDs
172 * @param n_group_ids return location for length of returned array
173 * @param error return location for error
174 * @returns #TRUE on success
177 _dbus_user_database_get_groups (DBusUserDatabase *db,
179 dbus_gid_t **group_ids,
183 DBusUserEntry *entry;
185 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
190 entry = _dbus_user_database_lookup (db, uid, error);
193 _DBUS_ASSERT_ERROR_IS_SET (error);
197 if (entry->n_group_ids > 0)
199 *group_ids = dbus_new (dbus_gid_t, entry->n_group_ids);
200 if (*group_ids == NULL)
202 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
206 *n_group_ids = entry->n_group_ids;
208 memcpy (*group_ids, entry->group_ids, entry->n_group_ids * sizeof (dbus_gid_t));
216 #ifdef DBUS_BUILD_TESTS
218 * Unit test for dbus-userdb.c.
220 * @returns #TRUE on success.
223 _dbus_userdb_test (const char *test_data_dir)
228 #endif /* DBUS_BUILD_TESTS */