D-Bus 1.14.10
dbus-userdb-util.c
1/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2/* dbus-userdb-util.c Would be in dbus-userdb.c, but not used in libdbus
3 *
4 * Copyright (C) 2003, 2004, 2005 Red Hat, Inc.
5 *
6 * Licensed under the Academic Free License version 2.1
7 *
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.
12 *
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.
17 *
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 */
23#include <config.h>
24#include <unistd.h>
25#define DBUS_USERDB_INCLUDES_PRIVATE 1
26#include "dbus-userdb.h"
27#include "dbus-test.h"
28#include "dbus-internals.h"
29#include "dbus-protocol.h"
30#include <dbus/dbus-test-tap.h>
31#include <string.h>
32
33/* It isn't obvious from its name, but this file is part of the Unix
34 * system-dependent part of libdbus. */
35#if defined(DBUS_WIN) || !defined(DBUS_UNIX)
36#error "This file only makes sense on Unix OSs"
37#endif
38
39#ifdef HAVE_SYSTEMD
40#include <systemd/sd-login.h>
41#endif
42
48static DBusGroupInfo *
49_dbus_group_info_ref (DBusGroupInfo *info)
50{
51 _dbus_assert (info->refcount > 0);
52 _dbus_assert (info->refcount < SIZE_MAX);
53 info->refcount++;
54 return info;
55}
56
66 DBusError *error)
67{
68
69 DBusUserDatabase *db;
70 const DBusUserInfo *info;
71 dbus_bool_t result = FALSE;
72
73#ifdef HAVE_SYSTEMD
74 /* check if we have logind */
75 if (access ("/run/systemd/seats/", F_OK) >= 0)
76 {
77 int r;
78
79 /* Check whether this user is logged in on at least one physical
80 seat */
81 r = sd_uid_get_seats (uid, 0, NULL);
82 if (r < 0)
83 {
85 "Failed to determine seats of user \"" DBUS_UID_FORMAT "\": %s",
86 uid,
87 _dbus_strerror (-r));
88 return FALSE;
89 }
90
91 return (r > 0);
92 }
93#endif
94
95#ifdef HAVE_CONSOLE_OWNER_FILE
96
97 DBusString f;
98 DBusStat st;
99
100 if (!_dbus_string_init (&f))
101 {
102 _DBUS_SET_OOM (error);
103 return FALSE;
104 }
105
106 if (!_dbus_string_append(&f, DBUS_CONSOLE_OWNER_FILE))
107 {
109 _DBUS_SET_OOM (error);
110 return FALSE;
111 }
112
113 if (_dbus_stat(&f, &st, NULL) && (st.uid == uid))
114 {
116 return TRUE;
117 }
118
120
121#endif /* HAVE_CONSOLE_OWNER_FILE */
122
124 {
125 _DBUS_SET_OOM (error);
126 return FALSE;
127 }
128
130 if (db == NULL)
131 {
132 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get system database.");
134 return FALSE;
135 }
136
137 /* TPTD: this should be cache-safe, we've locked the DB and
138 _dbus_user_at_console doesn't pass it on. */
139 info = _dbus_user_database_lookup (db, uid, NULL, error);
140
141 if (info == NULL)
142 {
144 return FALSE;
145 }
146
147 result = _dbus_user_at_console (info->username, error);
148
150
151 return result;
152}
153
163 dbus_uid_t *uid)
164{
165 return _dbus_get_user_id_and_primary_group (username, uid, NULL);
166}
167
177 dbus_gid_t *gid)
178{
179 DBusUserDatabase *db;
180 const DBusGroupInfo *info;
181
182 /* FIXME: this can't distinguish ENOMEM from other errors */
184 return FALSE;
185
187 if (db == NULL)
188 {
190 return FALSE;
191 }
192
194 NULL);
195
196 if (info == NULL)
197 {
199 return FALSE;
200 }
201
202 *gid = info->gid;
203
205 return TRUE;
206}
207
218 dbus_uid_t *uid_p,
219 dbus_gid_t *gid_p)
220{
221 DBusUserDatabase *db;
222 const DBusUserInfo *info;
223
224 /* FIXME: this can't distinguish ENOMEM from other errors */
226 return FALSE;
227
229 if (db == NULL)
230 {
232 return FALSE;
233 }
234
235 if (!_dbus_user_database_get_username (db, username,
236 &info, NULL))
237 {
239 return FALSE;
240 }
241
242 if (uid_p)
243 *uid_p = info->uid;
244 if (gid_p)
245 *gid_p = info->primary_gid;
246
248 return TRUE;
249}
250
263const DBusGroupInfo *
265 dbus_gid_t gid,
266 const DBusString *groupname,
267 DBusError *error)
268{
269 DBusGroupInfo *info;
270
271 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
272
273 /* See if the group is really a number */
274 if (gid == DBUS_UID_UNSET)
275 {
276 unsigned long n;
277
278 if (_dbus_is_a_number (groupname, &n))
279 gid = n;
280 }
281
282 if (gid != DBUS_GID_UNSET)
283 info = _dbus_hash_table_lookup_uintptr (db->groups, gid);
284 else
285 info = _dbus_hash_table_lookup_string (db->groups_by_name,
286 _dbus_string_get_const_data (groupname));
287 if (info)
288 {
289 _dbus_verbose ("Using cache for GID "DBUS_GID_FORMAT" information\n",
290 info->gid);
291 return info;
292 }
293 else
294 {
295 if (gid != DBUS_GID_UNSET)
296 _dbus_verbose ("No cache for GID "DBUS_GID_FORMAT"\n",
297 gid);
298 else
299 _dbus_verbose ("No cache for groupname \"%s\"\n",
300 _dbus_string_get_const_data (groupname));
301
302 info = dbus_new0 (DBusGroupInfo, 1);
303 if (info == NULL)
304 {
306 return NULL;
307 }
308 info->refcount = 1;
309
310 if (gid != DBUS_GID_UNSET)
311 {
312 if (!_dbus_group_info_fill_gid (info, gid, error))
313 {
314 _DBUS_ASSERT_ERROR_IS_SET (error);
316 return NULL;
317 }
318 }
319 else
320 {
321 if (!_dbus_group_info_fill (info, groupname, error))
322 {
323 _DBUS_ASSERT_ERROR_IS_SET (error);
325 return NULL;
326 }
327 }
328
329 /* don't use these past here */
330 gid = DBUS_GID_UNSET;
331 groupname = NULL;
332
333 if (_dbus_hash_table_insert_uintptr (db->groups, info->gid, info))
334 {
335 _dbus_group_info_ref (info);
336 }
337 else
338 {
341 return NULL;
342 }
343
344
345 if (_dbus_hash_table_insert_string (db->groups_by_name,
346 info->groupname,
347 info))
348 {
349 _dbus_group_info_ref (info);
350 }
351 else
352 {
353 _dbus_hash_table_remove_uintptr (db->groups, info->gid);
356 return NULL;
357 }
358
359 /* Release the original reference */
361
362 /* Return a borrowed reference to the DBusGroupInfo owned by the
363 * two hash tables */
364 return info;
365 }
366}
367
381 dbus_gid_t **group_ids,
382 int *n_group_ids,
383 DBusError *error)
384{
385 DBusUserDatabase *db;
386 const DBusUserInfo *info;
387 *group_ids = NULL;
388 *n_group_ids = 0;
389
391 {
392 _DBUS_SET_OOM (error);
393 return FALSE;
394 }
395
397 if (db == NULL)
398 {
399 _DBUS_SET_OOM (error);
401 return FALSE;
402 }
403
404 if (!_dbus_user_database_get_uid (db, uid, &info, error))
405 {
407 return FALSE;
408 }
409
410 _dbus_assert (info->uid == uid);
411
412 if (info->n_group_ids > 0)
413 {
414 *group_ids = dbus_new (dbus_gid_t, info->n_group_ids);
415 if (*group_ids == NULL)
416 {
417 _DBUS_SET_OOM (error);
419 return FALSE;
420 }
421
422 *n_group_ids = info->n_group_ids;
423
424 memcpy (*group_ids, info->group_ids, info->n_group_ids * sizeof (dbus_gid_t));
425 }
426
428 return TRUE;
429}
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
Definition: dbus-errors.c:354
dbus_bool_t _dbus_hash_table_remove_uintptr(DBusHashTable *table, uintptr_t key)
Removes the hash entry for the given key.
Definition: dbus-hash.c:1242
dbus_bool_t _dbus_hash_table_insert_string(DBusHashTable *table, char *key, void *value)
Creates a hash entry with the given key and value.
Definition: dbus-hash.c:1277
void * _dbus_hash_table_lookup_uintptr(DBusHashTable *table, uintptr_t key)
Looks up the value for a given integer in a hash table of type DBUS_HASH_UINTPTR.
Definition: dbus-hash.c:1162
void * _dbus_hash_table_lookup_string(DBusHashTable *table, const char *key)
Looks up the value for a given string in a hash table of type DBUS_HASH_STRING.
Definition: dbus-hash.c:1112
dbus_bool_t _dbus_hash_table_insert_uintptr(DBusHashTable *table, uintptr_t key, void *value)
Creates a hash entry with the given key and value.
Definition: dbus-hash.c:1352
dbus_bool_t _dbus_stat(const DBusString *filename, DBusStat *statbuf, DBusError *error)
stat() wrapper.
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
dbus_bool_t _dbus_user_database_lock_system(void)
Locks global system user database.
Definition: dbus-userdb.c:351
dbus_bool_t _dbus_user_at_console(const char *username, DBusError *error)
Checks if user is at the console.
const char * _dbus_error_from_errno(int error_number)
Converts a UNIX errno, or Windows errno or WinSock error value into a DBusError name.
Definition: dbus-sysdeps.c:599
void _dbus_user_database_unlock_system(void)
Unlocks global system user database.
Definition: dbus-userdb.c:368
dbus_bool_t _dbus_user_database_get_uid(DBusUserDatabase *db, dbus_uid_t uid, const DBusUserInfo **info, DBusError *error)
Gets the user information for the given UID, returned user info should not be freed.
Definition: dbus-userdb.c:703
dbus_bool_t _dbus_get_group_id(const DBusString *groupname, dbus_gid_t *gid)
Gets group ID given groupname.
void _dbus_group_info_unref(DBusGroupInfo *info)
Decrements the reference count.
Definition: dbus-userdb.c:85
const DBusUserInfo * _dbus_user_database_lookup(DBusUserDatabase *db, dbus_uid_t uid, const DBusString *username, DBusError *error)
Looks up a uid or username in the user database.
Definition: dbus-userdb.c:158
dbus_bool_t _dbus_is_console_user(dbus_uid_t uid, DBusError *error)
Checks to see if the UID sent in is the console user.
const DBusGroupInfo * _dbus_user_database_lookup_group(DBusUserDatabase *db, dbus_gid_t gid, const DBusString *groupname, DBusError *error)
Looks up a gid or group name in the user database.
dbus_bool_t _dbus_groups_from_uid(dbus_uid_t uid, dbus_gid_t **group_ids, int *n_group_ids, DBusError *error)
Gets all groups corresponding to the given UID.
dbus_bool_t _dbus_get_user_id_and_primary_group(const DBusString *username, dbus_uid_t *uid_p, dbus_gid_t *gid_p)
Gets user ID and primary group given username.
dbus_bool_t _dbus_user_database_get_username(DBusUserDatabase *db, const DBusString *username, const DBusUserInfo **info, DBusError *error)
Gets the user information for the given username.
Definition: dbus-userdb.c:722
dbus_bool_t _dbus_is_a_number(const DBusString *str, unsigned long *num)
Checks if a given string is actually a number and converts it if it is.
Definition: dbus-userdb.c:133
dbus_bool_t _dbus_get_user_id(const DBusString *username, dbus_uid_t *uid)
Gets user ID given username.
DBusUserDatabase * _dbus_user_database_get_system(void)
Gets the system global user database; must be called with lock held (_dbus_user_database_lock_system(...
Definition: dbus-userdb.c:381
#define NULL
A null pointer, defined appropriately for C or C++.
#define TRUE
Expands to "1".
#define FALSE
Expands to "0".
#define dbus_new(type, count)
Safe macro for using dbus_malloc().
Definition: dbus-memory.h:57
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition: dbus-memory.h:58
#define DBUS_ERROR_FAILED
A generic error; "something went wrong" - see the error message for more.
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
dbus_bool_t _dbus_string_append(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString.
Definition: dbus-string.c:966
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:182
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init(), and fills it with the same contents as #_DBUS_STRING_I...
Definition: dbus-string.c:278
dbus_bool_t _dbus_group_info_fill(DBusGroupInfo *info, const DBusString *groupname, DBusError *error)
Initializes the given DBusGroupInfo struct with information about the given group name.
dbus_bool_t _dbus_group_info_fill_gid(DBusGroupInfo *info, dbus_gid_t gid, DBusError *error)
Initializes the given DBusGroupInfo struct with information about the given group ID.
unsigned long dbus_uid_t
A user ID.
Definition: dbus-sysdeps.h:137
unsigned long dbus_gid_t
A group ID.
Definition: dbus-sysdeps.h:139
#define DBUS_UID_UNSET
an invalid UID used to represent an uninitialized dbus_uid_t field
Definition: dbus-sysdeps.h:144
#define DBUS_GID_UNSET
an invalid GID used to represent an uninitialized dbus_gid_t field
Definition: dbus-sysdeps.h:146
#define DBUS_GID_FORMAT
an appropriate printf format for dbus_gid_t
Definition: dbus-sysdeps.h:153
#define DBUS_UID_FORMAT
an appropriate printf format for dbus_uid_t
Definition: dbus-sysdeps.h:151
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
Object representing an exception.
Definition: dbus-errors.h:49
Information about a UNIX group.
dbus_gid_t gid
GID.
char * groupname
Group name.
size_t refcount
Reference count.
Portable struct with stat() results.
Definition: dbus-sysdeps.h:551
dbus_uid_t uid
User owning file.
Definition: dbus-sysdeps.h:554
Information about a UNIX user.
int n_group_ids
Size of group IDs array.
dbus_uid_t uid
UID.
dbus_gid_t * group_ids
Groups IDs, including above primary group.
char * username
Username.
dbus_gid_t primary_gid
GID.