D-Bus  1.12.20
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 <string.h>
31 
32 #ifdef HAVE_SYSTEMD
33 #include <systemd/sd-login.h>
34 #endif
35 #if HAVE_ELOGIND
36 #include <elogind/sd-login.h>
37 #endif
38 
44 static DBusGroupInfo *
45 _dbus_group_info_ref (DBusGroupInfo *info)
46 {
47  _dbus_assert (info->refcount > 0);
48  _dbus_assert (info->refcount < SIZE_MAX);
49  info->refcount++;
50  return info;
51 }
52 
62  DBusError *error)
63 {
64 
65  DBusUserDatabase *db;
66  const DBusUserInfo *info;
67  dbus_bool_t result = FALSE;
68 
69 #if defined(HAVE_SYSTEMD) || defined(HAVE_ELOGIND)
70  /* check if we have logind */
71  if (access ("/run/systemd/seats/", F_OK) >= 0)
72  {
73  int r;
74 
75  /* Check whether this user is logged in on at least one physical
76  seat */
77  r = sd_uid_get_seats (uid, 0, NULL);
78  if (r < 0)
79  {
81  "Failed to determine seats of user \"" DBUS_UID_FORMAT "\": %s",
82  uid,
83  _dbus_strerror (-r));
84  return FALSE;
85  }
86 
87  return (r > 0);
88  }
89 #endif
90 
91 #ifdef HAVE_CONSOLE_OWNER_FILE
92 
93  DBusString f;
94  DBusStat st;
95 
96  if (!_dbus_string_init (&f))
97  {
98  _DBUS_SET_OOM (error);
99  return FALSE;
100  }
101 
102  if (!_dbus_string_append(&f, DBUS_CONSOLE_OWNER_FILE))
103  {
104  _dbus_string_free(&f);
105  _DBUS_SET_OOM (error);
106  return FALSE;
107  }
108 
109  if (_dbus_stat(&f, &st, NULL) && (st.uid == uid))
110  {
111  _dbus_string_free(&f);
112  return TRUE;
113  }
114 
115  _dbus_string_free(&f);
116 
117 #endif /* HAVE_CONSOLE_OWNER_FILE */
118 
120  {
121  _DBUS_SET_OOM (error);
122  return FALSE;
123  }
124 
126  if (db == NULL)
127  {
128  dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get system database.");
130  return FALSE;
131  }
132 
133  /* TPTD: this should be cache-safe, we've locked the DB and
134  _dbus_user_at_console doesn't pass it on. */
135  info = _dbus_user_database_lookup (db, uid, NULL, error);
136 
137  if (info == NULL)
138  {
140  return FALSE;
141  }
142 
143  result = _dbus_user_at_console (info->username, error);
144 
146 
147  return result;
148 }
149 
158 _dbus_get_user_id (const DBusString *username,
159  dbus_uid_t *uid)
160 {
161  return _dbus_get_user_id_and_primary_group (username, uid, NULL);
162 }
163 
172 _dbus_get_group_id (const DBusString *groupname,
173  dbus_gid_t *gid)
174 {
175  DBusUserDatabase *db;
176  const DBusGroupInfo *info;
177 
178  /* FIXME: this can't distinguish ENOMEM from other errors */
180  return FALSE;
181 
183  if (db == NULL)
184  {
186  return FALSE;
187  }
188 
189  if (!_dbus_user_database_get_groupname (db, groupname,
190  &info, NULL))
191  {
193  return FALSE;
194  }
195 
196  *gid = info->gid;
197 
199  return TRUE;
200 }
201 
212  dbus_uid_t *uid_p,
213  dbus_gid_t *gid_p)
214 {
215  DBusUserDatabase *db;
216  const DBusUserInfo *info;
217 
218  /* FIXME: this can't distinguish ENOMEM from other errors */
220  return FALSE;
221 
223  if (db == NULL)
224  {
226  return FALSE;
227  }
228 
229  if (!_dbus_user_database_get_username (db, username,
230  &info, NULL))
231  {
233  return FALSE;
234  }
235 
236  if (uid_p)
237  *uid_p = info->uid;
238  if (gid_p)
239  *gid_p = info->primary_gid;
240 
242  return TRUE;
243 }
244 
257 const DBusGroupInfo *
258 _dbus_user_database_lookup_group (DBusUserDatabase *db,
259  dbus_gid_t gid,
260  const DBusString *groupname,
261  DBusError *error)
262 {
263  DBusGroupInfo *info;
264 
265  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
266 
267  /* See if the group is really a number */
268  if (gid == DBUS_UID_UNSET)
269  {
270  unsigned long n;
271 
272  if (_dbus_is_a_number (groupname, &n))
273  gid = n;
274  }
275 
276  if (gid != DBUS_GID_UNSET)
277  info = _dbus_hash_table_lookup_uintptr (db->groups, gid);
278  else
279  info = _dbus_hash_table_lookup_string (db->groups_by_name,
280  _dbus_string_get_const_data (groupname));
281  if (info)
282  {
283  _dbus_verbose ("Using cache for GID "DBUS_GID_FORMAT" information\n",
284  info->gid);
285  return info;
286  }
287  else
288  {
289  if (gid != DBUS_GID_UNSET)
290  _dbus_verbose ("No cache for GID "DBUS_GID_FORMAT"\n",
291  gid);
292  else
293  _dbus_verbose ("No cache for groupname \"%s\"\n",
294  _dbus_string_get_const_data (groupname));
295 
296  info = dbus_new0 (DBusGroupInfo, 1);
297  if (info == NULL)
298  {
300  return NULL;
301  }
302  info->refcount = 1;
303 
304  if (gid != DBUS_GID_UNSET)
305  {
306  if (!_dbus_group_info_fill_gid (info, gid, error))
307  {
308  _DBUS_ASSERT_ERROR_IS_SET (error);
309  _dbus_group_info_unref (info);
310  return NULL;
311  }
312  }
313  else
314  {
315  if (!_dbus_group_info_fill (info, groupname, error))
316  {
317  _DBUS_ASSERT_ERROR_IS_SET (error);
318  _dbus_group_info_unref (info);
319  return NULL;
320  }
321  }
322 
323  /* don't use these past here */
324  gid = DBUS_GID_UNSET;
325  groupname = NULL;
326 
327  if (_dbus_hash_table_insert_uintptr (db->groups, info->gid, info))
328  {
329  _dbus_group_info_ref (info);
330  }
331  else
332  {
334  _dbus_group_info_unref (info);
335  return NULL;
336  }
337 
338 
339  if (_dbus_hash_table_insert_string (db->groups_by_name,
340  info->groupname,
341  info))
342  {
343  _dbus_group_info_ref (info);
344  }
345  else
346  {
347  _dbus_hash_table_remove_uintptr (db->groups, info->gid);
348  _dbus_group_info_unref (info);
350  return NULL;
351  }
352 
353  /* Release the original reference */
354  _dbus_group_info_unref (info);
355 
356  /* Return a borrowed reference to the DBusGroupInfo owned by the
357  * two hash tables */
358  return info;
359  }
360 }
361 
362 
374 _dbus_user_database_get_groupname (DBusUserDatabase *db,
375  const DBusString *groupname,
376  const DBusGroupInfo **info,
377  DBusError *error)
378 {
379  *info = _dbus_user_database_lookup_group (db, DBUS_GID_UNSET, groupname, error);
380  return *info != NULL;
381 }
382 
394 _dbus_user_database_get_gid (DBusUserDatabase *db,
395  dbus_gid_t gid,
396  const DBusGroupInfo **info,
397  DBusError *error)
398 {
399  *info = _dbus_user_database_lookup_group (db, gid, NULL, error);
400  return *info != NULL;
401 }
402 
403 
416  dbus_gid_t **group_ids,
417  int *n_group_ids)
418 {
419  DBusUserDatabase *db;
420  const DBusUserInfo *info;
421  *group_ids = NULL;
422  *n_group_ids = 0;
423 
424  /* FIXME: this can't distinguish ENOMEM from other errors */
426  return FALSE;
427 
429  if (db == NULL)
430  {
432  return FALSE;
433  }
434 
435  if (!_dbus_user_database_get_uid (db, uid,
436  &info, NULL))
437  {
439  return FALSE;
440  }
441 
442  _dbus_assert (info->uid == uid);
443 
444  if (info->n_group_ids > 0)
445  {
446  *group_ids = dbus_new (dbus_gid_t, info->n_group_ids);
447  if (*group_ids == NULL)
448  {
450  return FALSE;
451  }
452 
453  *n_group_ids = info->n_group_ids;
454 
455  memcpy (*group_ids, info->group_ids, info->n_group_ids * sizeof (dbus_gid_t));
456  }
457 
459  return TRUE;
460 }
463 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
464 #include <stdio.h>
465 
472 _dbus_userdb_test (const char *test_data_dir)
473 {
474  const DBusString *username;
475  const DBusString *homedir;
476  dbus_uid_t uid;
477  unsigned long *group_ids;
478  int n_group_ids, i;
479  DBusError error;
480 
481  if (!_dbus_username_from_current_process (&username))
482  _dbus_assert_not_reached ("didn't get username");
483 
484  if (!_dbus_homedir_from_current_process (&homedir))
485  _dbus_assert_not_reached ("didn't get homedir");
486 
487  if (!_dbus_get_user_id (username, &uid))
488  _dbus_assert_not_reached ("didn't get uid");
489 
490  if (!_dbus_groups_from_uid (uid, &group_ids, &n_group_ids))
491  _dbus_assert_not_reached ("didn't get groups");
492 
493  printf (" Current user: %s homedir: %s gids:",
494  _dbus_string_get_const_data (username),
495  _dbus_string_get_const_data (homedir));
496 
497  for (i=0; i<n_group_ids; i++)
498  printf(" %ld", group_ids[i]);
499 
500  printf ("\n");
501 
502  dbus_error_init (&error);
503  printf ("Is Console user: %i\n",
504  _dbus_is_console_user (uid, &error));
505  printf ("Invocation was OK: %s\n", error.message ? error.message : "yes");
506  dbus_error_free (&error);
507  printf ("Is Console user 4711: %i\n",
508  _dbus_is_console_user (4711, &error));
509  printf ("Invocation was OK: %s\n", error.message ? error.message : "yes");
510  dbus_error_free (&error);
511 
512  dbus_free (group_ids);
513 
514  return TRUE;
515 }
516 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */
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:935
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:1109
const char * message
public error message field
Definition: dbus-errors.h:51
char * username
Username.
#define NULL
A null pointer, defined appropriately for C or C++.
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:702
Portable struct with stat() results.
Definition: dbus-sysdeps.h:527
#define dbus_new(type, count)
Safe macro for using dbus_malloc().
Definition: dbus-memory.h:57
#define DBUS_GID_FORMAT
an appropriate printf format for dbus_gid_t
Definition: dbus-sysdeps.h:150
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.
dbus_bool_t _dbus_user_database_lock_system(void)
Locks global system user database.
Definition: dbus-userdb.c:343
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.
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
void dbus_error_free(DBusError *error)
Frees an error that's been set (or just initialized), then reinitializes the error as in dbus_error_i...
Definition: dbus-errors.c:211
dbus_gid_t primary_gid
GID.
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:1299
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:704
void _dbus_user_database_unlock_system(void)
Unlocks global system user database.
Definition: dbus-userdb.c:360
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:175
char * groupname
Group name.
#define DBUS_UID_UNSET
an invalid UID used to represent an uninitialized dbus_uid_t field
Definition: dbus-sysdeps.h:141
dbus_bool_t _dbus_user_database_get_groupname(DBusUserDatabase *db, const DBusString *groupname, const DBusGroupInfo **info, DBusError *error)
Gets the user information for the given group name, returned group info should not be freed...
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:592
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.
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:373
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_gid_t * group_ids
Groups IDs, including above primary group.
dbus_bool_t _dbus_user_database_get_gid(DBusUserDatabase *db, dbus_gid_t gid, const DBusGroupInfo **info, DBusError *error)
Gets the user information for the given GID, returned group info should not be freed.
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:126
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition: dbus-memory.h:58
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
int n_group_ids
Size of group IDs array.
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:1224
dbus_uid_t uid
UID.
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...
dbus_bool_t _dbus_get_group_id(const DBusString *groupname, dbus_gid_t *gid)
Gets group ID given groupname.
Object representing an exception.
Definition: dbus-errors.h:48
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
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init().
Definition: dbus-string.c:259
#define DBUS_GID_UNSET
an invalid GID used to represent an uninitialized dbus_gid_t field
Definition: dbus-sysdeps.h:143
#define TRUE
Expands to "1".
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
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_uid_t uid
User owning file.
Definition: dbus-sysdeps.h:531
#define DBUS_ERROR_FAILED
A generic error; "something went wrong" - see the error message for more.
#define DBUS_UID_FORMAT
an appropriate printf format for dbus_uid_t
Definition: dbus-sysdeps.h:148
dbus_bool_t _dbus_homedir_from_current_process(const DBusString **homedir)
Gets homedir of user owning current process.
Definition: dbus-userdb.c:432
Information about a UNIX group.
dbus_bool_t _dbus_stat(const DBusString *filename, DBusStat *statbuf, DBusError *error)
stat() wrapper.
dbus_bool_t _dbus_get_user_id(const DBusString *username, dbus_uid_t *uid)
Gets user ID given username.
size_t refcount
Reference count.
void dbus_error_init(DBusError *error)
Initializes a DBusError structure.
Definition: dbus-errors.c:188
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:1059
dbus_bool_t _dbus_user_at_console(const char *username, DBusError *error)
Checks if user is at the console.
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
#define FALSE
Expands to "0".
dbus_gid_t gid
GID.
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:723
unsigned long dbus_gid_t
A group ID.
Definition: dbus-sysdeps.h:136
unsigned long dbus_uid_t
A user ID.
Definition: dbus-sysdeps.h:134
const char * _dbus_string_get_const_data(const DBusString *str)
Gets the raw character buffer from a const string.
Definition: dbus-string.c:468
void _dbus_group_info_unref(DBusGroupInfo *info)
Decrements the reference count.
Definition: dbus-userdb.c:78
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:1189
dbus_bool_t _dbus_username_from_current_process(const DBusString **username)
Gets username of user owning current process.
Definition: dbus-userdb.c:408
Information about a UNIX user.
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:151