D-Bus  1.6.12
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 #define DBUS_USERDB_INCLUDES_PRIVATE 1
25 #include "dbus-userdb.h"
26 #include "dbus-test.h"
27 #include "dbus-internals.h"
28 #include "dbus-protocol.h"
29 #include <string.h>
30 
31 #if HAVE_SYSTEMD
32 #include <systemd/sd-daemon.h>
33 #include <systemd/sd-login.h>
34 #endif
35 
50  DBusError *error)
51 {
52 
53  DBusUserDatabase *db;
54  const DBusUserInfo *info;
55  dbus_bool_t result = FALSE;
56 
57 #ifdef HAVE_SYSTEMD
58  if (sd_booted () > 0)
59  {
60  int r;
61 
62  /* Check whether this user is logged in on at least one physical
63  seat */
64  r = sd_uid_get_seats (uid, 0, NULL);
65  if (r < 0)
66  {
68  "Failed to determine seats of user \"" DBUS_UID_FORMAT "\": %s",
69  uid,
70  _dbus_strerror (-r));
71  return FALSE;
72  }
73 
74  return (r > 0);
75  }
76 #endif
77 
78 #ifdef HAVE_CONSOLE_OWNER_FILE
79 
80  DBusString f;
81  DBusStat st;
82 
83  if (!_dbus_string_init (&f))
84  {
85  _DBUS_SET_OOM (error);
86  return FALSE;
87  }
88 
89  if (!_dbus_string_append(&f, DBUS_CONSOLE_OWNER_FILE))
90  {
92  _DBUS_SET_OOM (error);
93  return FALSE;
94  }
95 
96  if (_dbus_stat(&f, &st, NULL) && (st.uid == uid))
97  {
99  return TRUE;
100  }
101 
102  _dbus_string_free(&f);
103 
104 #endif /* HAVE_CONSOLE_OWNER_FILE */
105 
107 
109  if (db == NULL)
110  {
111  dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get system database.");
113  return FALSE;
114  }
115 
116  /* TPTD: this should be cache-safe, we've locked the DB and
117  _dbus_user_at_console doesn't pass it on. */
118  info = _dbus_user_database_lookup (db, uid, NULL, error);
119 
120  if (info == NULL)
121  {
123  return FALSE;
124  }
125 
126  result = _dbus_user_at_console (info->username, error);
127 
129 
130  return result;
131 }
132 
141 _dbus_get_user_id (const DBusString *username,
142  dbus_uid_t *uid)
143 {
144  return _dbus_get_user_id_and_primary_group (username, uid, NULL);
145 }
146 
155 _dbus_get_group_id (const DBusString *groupname,
156  dbus_gid_t *gid)
157 {
158  DBusUserDatabase *db;
159  const DBusGroupInfo *info;
161 
163  if (db == NULL)
164  {
166  return FALSE;
167  }
168 
169  if (!_dbus_user_database_get_groupname (db, groupname,
170  &info, NULL))
171  {
173  return FALSE;
174  }
175 
176  *gid = info->gid;
177 
179  return TRUE;
180 }
181 
192  dbus_uid_t *uid_p,
193  dbus_gid_t *gid_p)
194 {
195  DBusUserDatabase *db;
196  const DBusUserInfo *info;
198 
200  if (db == NULL)
201  {
203  return FALSE;
204  }
205 
206  if (!_dbus_user_database_get_username (db, username,
207  &info, NULL))
208  {
210  return FALSE;
211  }
212 
213  if (uid_p)
214  *uid_p = info->uid;
215  if (gid_p)
216  *gid_p = info->primary_gid;
217 
219  return TRUE;
220 }
221 
235 _dbus_user_database_lookup_group (DBusUserDatabase *db,
236  dbus_gid_t gid,
237  const DBusString *groupname,
238  DBusError *error)
239 {
240  DBusGroupInfo *info;
241 
242  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
243 
244  /* See if the group is really a number */
245  if (gid == DBUS_UID_UNSET)
246  {
247  unsigned long n;
248 
249  if (_dbus_is_a_number (groupname, &n))
250  gid = n;
251  }
252 
253 #ifdef DBUS_ENABLE_USERDB_CACHE
254  if (gid != DBUS_GID_UNSET)
255  info = _dbus_hash_table_lookup_uintptr (db->groups, gid);
256  else
257  info = _dbus_hash_table_lookup_string (db->groups_by_name,
258  _dbus_string_get_const_data (groupname));
259  if (info)
260  {
261  _dbus_verbose ("Using cache for GID "DBUS_GID_FORMAT" information\n",
262  info->gid);
263  return info;
264  }
265  else
266 #else
267  if (1)
268 #endif
269  {
270  if (gid != DBUS_GID_UNSET)
271  _dbus_verbose ("No cache for GID "DBUS_GID_FORMAT"\n",
272  gid);
273  else
274  _dbus_verbose ("No cache for groupname \"%s\"\n",
275  _dbus_string_get_const_data (groupname));
276 
277  info = dbus_new0 (DBusGroupInfo, 1);
278  if (info == NULL)
279  {
281  return NULL;
282  }
283 
284  if (gid != DBUS_GID_UNSET)
285  {
286  if (!_dbus_group_info_fill_gid (info, gid, error))
287  {
288  _DBUS_ASSERT_ERROR_IS_SET (error);
290  return NULL;
291  }
292  }
293  else
294  {
295  if (!_dbus_group_info_fill (info, groupname, error))
296  {
297  _DBUS_ASSERT_ERROR_IS_SET (error);
299  return NULL;
300  }
301  }
302 
303  /* don't use these past here */
304  gid = DBUS_GID_UNSET;
305  groupname = NULL;
306 
307  if (!_dbus_hash_table_insert_uintptr (db->groups, info->gid, info))
308  {
311  return NULL;
312  }
313 
314 
315  if (!_dbus_hash_table_insert_string (db->groups_by_name,
316  info->groupname,
317  info))
318  {
319  _dbus_hash_table_remove_uintptr (db->groups, info->gid);
321  return NULL;
322  }
323 
324  return info;
325  }
326 }
327 
328 
340 _dbus_user_database_get_groupname (DBusUserDatabase *db,
341  const DBusString *groupname,
342  const DBusGroupInfo **info,
343  DBusError *error)
344 {
345  *info = _dbus_user_database_lookup_group (db, DBUS_GID_UNSET, groupname, error);
346  return *info != NULL;
347 }
348 
360 _dbus_user_database_get_gid (DBusUserDatabase *db,
361  dbus_gid_t gid,
362  const DBusGroupInfo **info,
363  DBusError *error)
364 {
365  *info = _dbus_user_database_lookup_group (db, gid, NULL, error);
366  return *info != NULL;
367 }
368 
369 
382  dbus_gid_t **group_ids,
383  int *n_group_ids)
384 {
385  DBusUserDatabase *db;
386  const DBusUserInfo *info;
387  *group_ids = NULL;
388  *n_group_ids = 0;
389 
391 
393  if (db == NULL)
394  {
396  return FALSE;
397  }
398 
399  if (!_dbus_user_database_get_uid (db, uid,
400  &info, NULL))
401  {
403  return FALSE;
404  }
405 
406  _dbus_assert (info->uid == uid);
407 
408  if (info->n_group_ids > 0)
409  {
410  *group_ids = dbus_new (dbus_gid_t, info->n_group_ids);
411  if (*group_ids == NULL)
412  {
414  return FALSE;
415  }
416 
417  *n_group_ids = info->n_group_ids;
418 
419  memcpy (*group_ids, info->group_ids, info->n_group_ids * sizeof (dbus_gid_t));
420  }
421 
423  return TRUE;
424 }
427 #ifdef DBUS_BUILD_TESTS
428 #include <stdio.h>
429 
436 _dbus_userdb_test (const char *test_data_dir)
437 {
438  const DBusString *username;
439  const DBusString *homedir;
440  dbus_uid_t uid;
441  unsigned long *group_ids;
442  int n_group_ids, i;
443  DBusError error;
444 
445  if (!_dbus_username_from_current_process (&username))
446  _dbus_assert_not_reached ("didn't get username");
447 
448  if (!_dbus_homedir_from_current_process (&homedir))
449  _dbus_assert_not_reached ("didn't get homedir");
450 
451  if (!_dbus_get_user_id (username, &uid))
452  _dbus_assert_not_reached ("didn't get uid");
453 
454  if (!_dbus_groups_from_uid (uid, &group_ids, &n_group_ids))
455  _dbus_assert_not_reached ("didn't get groups");
456 
457  printf (" Current user: %s homedir: %s gids:",
458  _dbus_string_get_const_data (username),
459  _dbus_string_get_const_data (homedir));
460 
461  for (i=0; i<n_group_ids; i++)
462  printf(" %ld", group_ids[i]);
463 
464  printf ("\n");
465 
466  dbus_error_init (&error);
467  printf ("Is Console user: %i\n",
468  _dbus_is_console_user (uid, &error));
469  printf ("Invocation was OK: %s\n", error.message ? error.message : "yes");
470  dbus_error_free (&error);
471  printf ("Is Console user 4711: %i\n",
472  _dbus_is_console_user (4711, &error));
473  printf ("Invocation was OK: %s\n", error.message ? error.message : "yes");
474  dbus_error_free (&error);
475 
476  dbus_free (group_ids);
477 
478  return TRUE;
479 }
480 #endif /* DBUS_BUILD_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:913
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_user_database_lock_system(void)
Locks global system user database.
Definition: dbus-userdb.c:310
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:128
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:700
Portable struct with stat() results.
Definition: dbus-sysdeps.h:390
#define dbus_new(type, count)
Safe macro for using dbus_malloc().
Definition: dbus-memory.h:58
#define DBUS_GID_FORMAT
an appropriate printf format for dbus_gid_t
Definition: dbus-sysdeps.h:116
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_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&#39;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_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:638
void _dbus_user_database_unlock_system(void)
Unlocks global system user database.
Definition: dbus-userdb.c:320
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:107
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:612
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:333
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:103
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition: dbus-memory.h:59
void _dbus_group_info_free_allocated(DBusGroupInfo *info)
Frees the given DBusGroupInfo&#39;s members with _dbus_group_info_free() and also calls dbus_free() on th...
Definition: dbus-userdb.c:61
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_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...
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:1099
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_get_group_id(const DBusString *groupname, dbus_gid_t *gid)
Gets group ID given groupname.
Object representing an exception.
Definition: dbus-errors.h:48
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:1214
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:242
#define DBUS_GID_UNSET
an invalid GID used to represent an uninitialized dbus_gid_t field
Definition: dbus-sysdeps.h:109
#define TRUE
Expands to &quot;1&quot;.
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:1289
#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:394
#define DBUS_ERROR_FAILED
A generic error; &quot;something went wrong&quot; - see the error message for more.
#define DBUS_UID_FORMAT
an appropriate printf format for dbus_uid_t
Definition: dbus-sysdeps.h:114
dbus_bool_t _dbus_homedir_from_current_process(const DBusString **homedir)
Gets homedir of user owning current process.
Definition: dbus-userdb.c:386
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.
void dbus_error_init(DBusError *error)
Initializes a DBusError structure.
Definition: dbus-errors.c:188
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 &quot;0&quot;.
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:1179
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:657
unsigned long dbus_gid_t
A group ID.
Definition: dbus-sysdeps.h:102
unsigned long dbus_uid_t
A user ID.
Definition: dbus-sysdeps.h:100
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:1049
const char * _dbus_string_get_const_data(const DBusString *str)
Gets the raw character buffer from a const string.
Definition: dbus-string.c:446
dbus_bool_t _dbus_username_from_current_process(const DBusString **username)
Gets username of user owning current process.
Definition: dbus-userdb.c:364
Information about a UNIX user.