summaryrefslogtreecommitdiffstats
path: root/dbus/dbus-userdb.c
blob: 871fa5bd40b1f18261e1eb6baa8c7fec8078ce9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* -*- mode: C; c-file-style: "gnu" -*- */
/* dbus-userdb.c User database abstraction
 * 
 * Copyright (C) 2003  Red Hat, Inc.
 *
 * Licensed under the Academic Free License version 1.2
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include "dbus-userdb.h"
#include "dbus-hash.h"
#include "dbus-test.h"
#include "dbus-internals.h"
#include <string.h>

typedef struct DBusUserEntry DBusUserEntry;

struct DBusUserEntry
{
  dbus_uid_t  uid;

  dbus_gid_t *group_ids;
  int         n_group_ids;
};

struct DBusUserDatabase
{
  int refcount;

  DBusHashTable *users;
};

static void
free_user_entry (void *data)
{
  DBusUserEntry *entry = data;

  if (entry == NULL) /* hash table will pass NULL */
    return;

  dbus_free (entry->group_ids);
  
  dbus_free (entry);
}

static DBusUserEntry*
_dbus_user_database_lookup (DBusUserDatabase *db,
                            dbus_uid_t        uid,
                            DBusError        *error)
{
  DBusUserEntry *entry;

  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
  
  entry = _dbus_hash_table_lookup_ulong (db->users, uid);
  if (entry)
    return entry;
  else
    {
      entry = dbus_new0 (DBusUserEntry, 1);
      if (entry == NULL)
        {
          dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
          return NULL;
        }

      if (!_dbus_get_groups (uid, &entry->group_ids, &entry->n_group_ids, error))
        {
          _DBUS_ASSERT_ERROR_IS_SET (error);
          free_user_entry (entry);
          return NULL;
        }

      if (!_dbus_hash_table_insert_ulong (db->users, entry->uid, entry))
        {
          dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
          free_user_entry (entry);
          return NULL;
        }

      return entry;
    }
}

/**
 * @addtogroup DBusInternalsUtils
 * @{
 */

/**
 * Creates a new user database object used to look up and
 * cache user information.
 * @returns new database, or #NULL on out of memory
 */
DBusUserDatabase*
_dbus_user_database_new (void)
{
  DBusUserDatabase *db;
  
  db = dbus_new0 (DBusUserDatabase, 1);
  if (db == NULL)
    return NULL;

  db->refcount = 1;

  db->users = _dbus_hash_table_new (DBUS_HASH_ULONG,
                                    NULL, free_user_entry);

  if (db->users == NULL)
    goto failed;
  
  return db;
  
 failed:
  _dbus_user_database_unref (db);
  return NULL;
}

/**
 * Increments refcount of user database.
 * @param db the database
 */
void
_dbus_user_database_ref (DBusUserDatabase  *db)
{
  _dbus_assert (db->refcount > 0);

  db->refcount += 1;
}

/**
 * Decrements refcount of user database.
 * @param db the database
 */
void
_dbus_user_database_unref (DBusUserDatabase  *db)
{
  _dbus_assert (db->refcount > 0);

  db->refcount -= 1;
  if (db->refcount == 0)
    {
      if (db->users)
        _dbus_hash_table_unref (db->users);
      
      dbus_free (db);
    }
}

/**
 * Gets all groups for a particular user. Returns #FALSE
 * if no memory, or user isn't known, but always initializes
 * group_ids to a NULL array. Sets error to the reason
 * for returning #FALSE.
 *
 * @param db the user database object
 * @param uid the user ID
 * @param group_ids return location for array of group IDs
 * @param n_group_ids return location for length of returned array
 * @param error return location for error
 * @returns #TRUE on success
 */
dbus_bool_t
_dbus_user_database_get_groups (DBusUserDatabase  *db,
                                dbus_uid_t         uid,
                                dbus_gid_t       **group_ids,
                                int               *n_group_ids,
                                DBusError         *error)
{
  DBusUserEntry *entry;
  
  _DBUS_ASSERT_ERROR_IS_CLEAR (error);

  *group_ids = NULL;
  *n_group_ids = 0;
  
  entry = _dbus_user_database_lookup (db, uid, error);
  if (entry == NULL)
    {
      _DBUS_ASSERT_ERROR_IS_SET (error);
      return FALSE;
    }

  if (entry->n_group_ids > 0)
    {
      *group_ids = dbus_new (dbus_gid_t, entry->n_group_ids);
      if (*group_ids == NULL)
        {
          dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
          return FALSE;
        }

      *n_group_ids = entry->n_group_ids;

      memcpy (*group_ids, entry->group_ids, entry->n_group_ids * sizeof (dbus_gid_t));
    }

  return TRUE;
}

/** @} */

#ifdef DBUS_BUILD_TESTS
/**
 * Unit test for dbus-userdb.c.
 * 
 * @returns #TRUE on success.
 */
dbus_bool_t
_dbus_userdb_test (const char *test_data_dir)
{

  return TRUE;
}
#endif /* DBUS_BUILD_TESTS */