D-Bus  1.13.16
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 
57  DBusError *error)
58 {
59 
60  DBusUserDatabase *db;
61  const DBusUserInfo *info;
62  dbus_bool_t result = FALSE;
63 
64 #ifdef HAVE_SYSTEMD
65  /* check if we have logind */
66  if (access ("/run/systemd/seats/", F_OK) >= 0)
67  {
68  int r;
69 
70  /* Check whether this user is logged in on at least one physical
71  seat */
72  r = sd_uid_get_seats (uid, 0, NULL);
73  if (r < 0)
74  {
76  "Failed to determine seats of user \"" DBUS_UID_FORMAT "\": %s",
77  uid,
78  _dbus_strerror (-r));
79  return FALSE;
80  }
81 
82  return (r > 0);
83  }
84 #endif
85 
86 #ifdef HAVE_CONSOLE_OWNER_FILE
87 
88  DBusString f;
89  DBusStat st;
90 
91  if (!_dbus_string_init (&f))
92  {
93  _DBUS_SET_OOM (error);
94  return FALSE;
95  }
96 
97  if (!_dbus_string_append(&f, DBUS_CONSOLE_OWNER_FILE))
98  {
100  _DBUS_SET_OOM (error);
101  return FALSE;
102  }
103 
104  if (_dbus_stat(&f, &st, NULL) && (st.uid == uid))
105  {
106  _dbus_string_free(&f);
107  return TRUE;
108  }
109 
110  _dbus_string_free(&f);
111 
112 #endif /* HAVE_CONSOLE_OWNER_FILE */
113 
115  {
116  _DBUS_SET_OOM (error);
117  return FALSE;
118  }
119 
121  if (db == NULL)
122  {
123  dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get system database.");
125  return FALSE;
126  }
127 
128  /* TPTD: this should be cache-safe, we've locked the DB and
129  _dbus_user_at_console doesn't pass it on. */
130  info = _dbus_user_database_lookup (db, uid, NULL, error);
131 
132  if (info == NULL)
133  {
135  return FALSE;
136  }
137 
138  result = _dbus_user_at_console (info->username, error);
139 
141 
142  return result;
143 }
144 
153 _dbus_get_user_id (const DBusString *username,
154  dbus_uid_t *uid)
155 {
156  return _dbus_get_user_id_and_primary_group (username, uid, NULL);
157 }
158 
167 _dbus_get_group_id (const DBusString *groupname,
168  dbus_gid_t *gid)
169 {
170  DBusUserDatabase *db;
171  const DBusGroupInfo *info;
172 
173  /* FIXME: this can't distinguish ENOMEM from other errors */
175  return FALSE;
176 
178  if (db == NULL)
179  {
181  return FALSE;
182  }
183 
184  info = _dbus_user_database_lookup_group (db, DBUS_GID_UNSET, groupname,
185  NULL);
186 
187  if (info == NULL)
188  {
190  return FALSE;
191  }
192 
193  *gid = info->gid;
194 
196  return TRUE;
197 }
198 
209  dbus_uid_t *uid_p,
210  dbus_gid_t *gid_p)
211 {
212  DBusUserDatabase *db;
213  const DBusUserInfo *info;
214 
215  /* FIXME: this can't distinguish ENOMEM from other errors */
217  return FALSE;
218 
220  if (db == NULL)
221  {
223  return FALSE;
224  }
225 
226  if (!_dbus_user_database_get_username (db, username,
227  &info, NULL))
228  {
230  return FALSE;
231  }
232 
233  if (uid_p)
234  *uid_p = info->uid;
235  if (gid_p)
236  *gid_p = info->primary_gid;
237 
239  return TRUE;
240 }
241 
255 _dbus_user_database_lookup_group (DBusUserDatabase *db,
256  dbus_gid_t gid,
257  const DBusString *groupname,
258  DBusError *error)
259 {
260  DBusGroupInfo *info;
261 
262  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
263 
264  /* See if the group is really a number */
265  if (gid == DBUS_UID_UNSET)
266  {
267  unsigned long n;
268 
269  if (_dbus_is_a_number (groupname, &n))
270  gid = n;
271  }
272 
273  if (gid != DBUS_GID_UNSET)
274  info = _dbus_hash_table_lookup_uintptr (db->groups, gid);
275  else
276  info = _dbus_hash_table_lookup_string (db->groups_by_name,
277  _dbus_string_get_const_data (groupname));
278  if (info)
279  {
280  _dbus_verbose ("Using cache for GID "DBUS_GID_FORMAT" information\n",
281  info->gid);
282  return info;
283  }
284  else
285  {
286  if (gid != DBUS_GID_UNSET)
287  _dbus_verbose ("No cache for GID "DBUS_GID_FORMAT"\n",
288  gid);
289  else
290  _dbus_verbose ("No cache for groupname \"%s\"\n",
291  _dbus_string_get_const_data (groupname));
292 
293  info = dbus_new0 (DBusGroupInfo, 1);
294  if (info == NULL)
295  {
297  return NULL;
298  }
299 
300  if (gid != DBUS_GID_UNSET)
301  {
302  if (!_dbus_group_info_fill_gid (info, gid, error))
303  {
304  _DBUS_ASSERT_ERROR_IS_SET (error);
306  return NULL;
307  }
308  }
309  else
310  {
311  if (!_dbus_group_info_fill (info, groupname, error))
312  {
313  _DBUS_ASSERT_ERROR_IS_SET (error);
315  return NULL;
316  }
317  }
318 
319  /* don't use these past here */
320  gid = DBUS_GID_UNSET;
321  groupname = NULL;
322 
323  if (!_dbus_hash_table_insert_uintptr (db->groups, info->gid, info))
324  {
327  return NULL;
328  }
329 
330 
331  if (!_dbus_hash_table_insert_string (db->groups_by_name,
332  info->groupname,
333  info))
334  {
335  _dbus_hash_table_remove_uintptr (db->groups, info->gid);
337  return NULL;
338  }
339 
340  return info;
341  }
342 }
343 
356  dbus_gid_t **group_ids,
357  int *n_group_ids)
358 {
359  DBusUserDatabase *db;
360  const DBusUserInfo *info;
361  *group_ids = NULL;
362  *n_group_ids = 0;
363 
364  /* FIXME: this can't distinguish ENOMEM from other errors */
366  return FALSE;
367 
369  if (db == NULL)
370  {
372  return FALSE;
373  }
374 
375  if (!_dbus_user_database_get_uid (db, uid,
376  &info, NULL))
377  {
379  return FALSE;
380  }
381 
382  _dbus_assert (info->uid == uid);
383 
384  if (info->n_group_ids > 0)
385  {
386  *group_ids = dbus_new (dbus_gid_t, info->n_group_ids);
387  if (*group_ids == NULL)
388  {
390  return FALSE;
391  }
392 
393  *n_group_ids = info->n_group_ids;
394 
395  memcpy (*group_ids, info->group_ids, info->n_group_ids * sizeof (dbus_gid_t));
396  }
397 
399  return TRUE;
400 }
DBusUserInfo::group_ids
dbus_gid_t * group_ids
Groups IDs, including above primary group.
Definition: dbus-sysdeps-unix.h:110
_dbus_hash_table_insert_uintptr
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
DBusUserInfo::n_group_ids
int n_group_ids
Size of group IDs array.
Definition: dbus-sysdeps-unix.h:111
_dbus_user_database_get_uid
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:666
_dbus_user_database_unlock_system
void _dbus_user_database_unlock_system(void)
Unlocks global system user database.
Definition: dbus-userdb.c:331
_dbus_string_free
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:271
DBusStat::uid
dbus_uid_t uid
User owning file.
Definition: dbus-sysdeps.h:557
_dbus_hash_table_lookup_string
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_stat
dbus_bool_t _dbus_stat(const DBusString *filename, DBusStat *statbuf, DBusError *error)
stat() wrapper.
Definition: dbus-sysdeps-util-unix.c:635
_dbus_error_from_errno
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
DBusStat
Portable struct with stat() results.
Definition: dbus-sysdeps.h:553
_dbus_get_user_id_and_primary_group
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.
Definition: dbus-userdb-util.c:208
DBUS_UID_FORMAT
#define DBUS_UID_FORMAT
an appropriate printf format for dbus_uid_t
Definition: dbus-sysdeps.h:151
DBusGroupInfo::gid
dbus_gid_t gid
GID.
Definition: dbus-sysdeps-unix.h:121
DBusGroupInfo
Information about a UNIX group.
Definition: dbus-sysdeps-unix.h:119
DBusUserInfo::uid
dbus_uid_t uid
UID.
Definition: dbus-sysdeps-unix.h:108
dbus_gid_t
unsigned long dbus_gid_t
A group ID.
Definition: dbus-sysdeps.h:139
_dbus_string_init
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:182
_dbus_groups_from_uid
dbus_bool_t _dbus_groups_from_uid(dbus_uid_t uid, dbus_gid_t **group_ids, int *n_group_ids)
Gets all groups corresponding to the given UID.
Definition: dbus-userdb-util.c:355
TRUE
#define TRUE
_dbus_user_at_console
dbus_bool_t _dbus_user_at_console(const char *username, DBusError *error)
Checks if user is at the console.
Definition: dbus-sysdeps-util-unix.c:571
DBUS_ERROR_FAILED
#define DBUS_ERROR_FAILED
A generic error; "something went wrong" - see the error message for more.
Definition: dbus-protocol.h:359
DBusUserInfo::username
char * username
Username.
Definition: dbus-sysdeps-unix.h:112
_dbus_user_database_lock_system
dbus_bool_t _dbus_user_database_lock_system(void)
Locks global system user database.
Definition: dbus-userdb.c:314
DBusString
Definition: dbus-string.h:42
_dbus_user_database_lookup_group
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.
Definition: dbus-userdb-util.c:255
_dbus_get_group_id
dbus_bool_t _dbus_get_group_id(const DBusString *groupname, dbus_gid_t *gid)
Gets group ID given groupname.
Definition: dbus-userdb-util.c:167
_dbus_user_database_get_system
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:344
dbus_uid_t
unsigned long dbus_uid_t
A user ID.
Definition: dbus-sysdeps.h:137
DBUS_GID_FORMAT
#define DBUS_GID_FORMAT
an appropriate printf format for dbus_gid_t
Definition: dbus-sysdeps.h:153
FALSE
#define FALSE
DBUS_ERROR_NO_MEMORY
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
Definition: dbus-protocol.h:361
_dbus_is_a_number
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:110
_dbus_hash_table_lookup_uintptr
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
DBUS_UID_UNSET
#define DBUS_UID_UNSET
an invalid UID used to represent an uninitialized dbus_uid_t field
Definition: dbus-sysdeps.h:144
_dbus_group_info_fill_gid
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.
Definition: dbus-sysdeps-util-unix.c:945
_dbus_is_console_user
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.
Definition: dbus-userdb-util.c:56
_dbus_user_database_lookup
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:135
_dbus_user_database_get_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:685
dbus_new
#define dbus_new(type, count)
Definition: dbus-memory.h:57
_dbus_assert
#define _dbus_assert(condition)
Definition: dbus-internals.h:153
DBusUserInfo
Information about a UNIX user.
Definition: dbus-sysdeps-unix.h:106
DBusError
Object representing an exception.
Definition: dbus-errors.h:48
_dbus_hash_table_remove_uintptr
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_set_error
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
DBusUserInfo::primary_gid
dbus_gid_t primary_gid
GID.
Definition: dbus-sysdeps-unix.h:109
_dbus_group_info_free_allocated
void _dbus_group_info_free_allocated(DBusGroupInfo *info)
Frees the given DBusGroupInfo's members with _dbus_group_info_free() and also calls dbus_free() on th...
Definition: dbus-userdb.c:68
DBUS_GID_UNSET
#define DBUS_GID_UNSET
an invalid GID used to represent an uninitialized dbus_gid_t field
Definition: dbus-sysdeps.h:146
_dbus_get_user_id
dbus_bool_t _dbus_get_user_id(const DBusString *username, dbus_uid_t *uid)
Gets user ID given username.
Definition: dbus-userdb-util.c:153
dbus_new0
#define dbus_new0(type, count)
Definition: dbus-memory.h:58
_dbus_hash_table_insert_string
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
_dbus_group_info_fill
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.
Definition: dbus-sysdeps-util-unix.c:926
_dbus_string_append
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:959
DBusGroupInfo::groupname
char * groupname
Group name.
Definition: dbus-sysdeps-unix.h:122
dbus_bool_t
dbus_uint32_t dbus_bool_t
Definition: dbus-types.h:35
NULL
#define NULL