D-Bus  1.12.2
dbus-userdb.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-userdb.c User database abstraction
3  *
4  * Copyright (C) 2003, 2004 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-hash.h"
27 #include "dbus-test.h"
28 #include "dbus-internals.h"
29 #include "dbus-protocol.h"
30 #include "dbus-credentials.h"
31 #include <string.h>
32 
38 static DBusUserInfo *
39 _dbus_user_info_ref (DBusUserInfo *info)
40 {
41  _dbus_assert (info->refcount > 0);
42  _dbus_assert (info->refcount < SIZE_MAX);
43  info->refcount++;
44  return info;
45 }
46 
54 void
56 {
57  if (info == NULL) /* hash table will pass NULL */
58  return;
59 
60  _dbus_assert (info->refcount > 0);
61  _dbus_assert (info->refcount < SIZE_MAX);
62 
63  if (--info->refcount > 0)
64  return;
65 
66  _dbus_user_info_free (info);
67  dbus_free (info);
68 }
69 
77 void
79 {
80  if (info == NULL) /* hash table will pass NULL */
81  return;
82 
83  _dbus_assert (info->refcount > 0);
84  _dbus_assert (info->refcount < SIZE_MAX);
85 
86  if (--info->refcount > 0)
87  return;
88 
89  _dbus_group_info_free (info);
90  dbus_free (info);
91 }
92 
98 void
100 {
101  dbus_free (info->group_ids);
102  dbus_free (info->username);
103  dbus_free (info->homedir);
104 }
105 
111 void
113 {
114  dbus_free (info->groupname);
115 }
116 
127  unsigned long *num)
128 {
129  int end;
130 
131  if (_dbus_string_parse_uint (str, 0, num, &end) &&
132  end == _dbus_string_get_length (str))
133  return TRUE;
134  else
135  return FALSE;
136 }
137 
151 _dbus_user_database_lookup (DBusUserDatabase *db,
152  dbus_uid_t uid,
153  const DBusString *username,
154  DBusError *error)
155 {
156  DBusUserInfo *info;
157 
158  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
159  _dbus_assert (uid != DBUS_UID_UNSET || username != NULL);
160 
161  /* See if the username is really a number */
162  if (uid == DBUS_UID_UNSET)
163  {
164  unsigned long n;
165 
166  if (_dbus_is_a_number (username, &n))
167  uid = n;
168  }
169 
170  if (uid != DBUS_UID_UNSET)
171  info = _dbus_hash_table_lookup_uintptr (db->users, uid);
172  else
173  info = _dbus_hash_table_lookup_string (db->users_by_name, _dbus_string_get_const_data (username));
174 
175  if (info)
176  {
177  _dbus_verbose ("Using cache for UID "DBUS_UID_FORMAT" information\n",
178  info->uid);
179  return info;
180  }
181  else
182  {
183  if (uid != DBUS_UID_UNSET)
184  _dbus_verbose ("No cache for UID "DBUS_UID_FORMAT"\n",
185  uid);
186  else
187  _dbus_verbose ("No cache for user \"%s\"\n",
188  _dbus_string_get_const_data (username));
189 
190  info = dbus_new0 (DBusUserInfo, 1);
191  if (info == NULL)
192  {
194  return NULL;
195  }
196  info->refcount = 1;
197 
198  if (uid != DBUS_UID_UNSET)
199  {
200  if (!_dbus_user_info_fill_uid (info, uid, error))
201  {
202  _DBUS_ASSERT_ERROR_IS_SET (error);
203  _dbus_user_info_unref (info);
204  return NULL;
205  }
206  }
207  else
208  {
209  if (!_dbus_user_info_fill (info, username, error))
210  {
211  _DBUS_ASSERT_ERROR_IS_SET (error);
212  _dbus_user_info_unref (info);
213  return NULL;
214  }
215  }
216 
217  /* be sure we don't use these after here */
218  uid = DBUS_UID_UNSET;
219  username = NULL;
220 
221  /* insert into hash */
222  if (_dbus_hash_table_insert_uintptr (db->users, info->uid, info))
223  {
224  _dbus_user_info_ref (info);
225  }
226  else
227  {
229  _dbus_user_info_unref (info);
230  return NULL;
231  }
232 
233  if (_dbus_hash_table_insert_string (db->users_by_name,
234  info->username,
235  info))
236  {
237  _dbus_user_info_ref (info);
238  }
239  else
240  {
241  _dbus_hash_table_remove_uintptr (db->users, info->uid);
243  _dbus_user_info_unref (info);
244  return NULL;
245  }
246 
247  _dbus_user_info_unref (info);
248 
249  return info;
250  }
251 }
252 
253 static dbus_bool_t database_locked = FALSE;
254 static DBusUserDatabase *system_db = NULL;
255 static DBusString process_username;
256 static DBusString process_homedir;
257 
258 static void
259 shutdown_system_db (void *data)
260 {
261  if (system_db != NULL)
262  _dbus_user_database_unref (system_db);
263  system_db = NULL;
264  _dbus_string_free (&process_username);
265  _dbus_string_free (&process_homedir);
266 }
267 
268 static dbus_bool_t
269 init_system_db (void)
270 {
271  _dbus_assert (database_locked);
272 
273  if (system_db == NULL)
274  {
275  DBusError error = DBUS_ERROR_INIT;
276  const DBusUserInfo *info;
277 
278  system_db = _dbus_user_database_new ();
279  if (system_db == NULL)
280  return FALSE;
281 
282  if (!_dbus_user_database_get_uid (system_db,
283  _dbus_getuid (),
284  &info,
285  &error))
286  {
287  _dbus_user_database_unref (system_db);
288  system_db = NULL;
289 
291  {
292  dbus_error_free (&error);
293  return FALSE;
294  }
295  else
296  {
297  /* This really should not happen. */
298  _dbus_warn ("Could not get password database information for UID of current process: %s",
299  error.message);
300  dbus_error_free (&error);
301  return FALSE;
302  }
303  }
304 
305  if (!_dbus_string_init (&process_username))
306  {
307  _dbus_user_database_unref (system_db);
308  system_db = NULL;
309  return FALSE;
310  }
311 
312  if (!_dbus_string_init (&process_homedir))
313  {
314  _dbus_string_free (&process_username);
315  _dbus_user_database_unref (system_db);
316  system_db = NULL;
317  return FALSE;
318  }
319 
320  if (!_dbus_string_append (&process_username,
321  info->username) ||
322  !_dbus_string_append (&process_homedir,
323  info->homedir) ||
324  !_dbus_register_shutdown_func (shutdown_system_db, NULL))
325  {
326  _dbus_string_free (&process_username);
327  _dbus_string_free (&process_homedir);
328  _dbus_user_database_unref (system_db);
329  system_db = NULL;
330  return FALSE;
331  }
332  }
333 
334  return TRUE;
335 }
336 
342 {
343  if (_DBUS_LOCK (system_users))
344  {
345  database_locked = TRUE;
346  return TRUE;
347  }
348  else
349  {
350  return FALSE;
351  }
352 }
353 
357 void
359 {
360  database_locked = FALSE;
361  _DBUS_UNLOCK (system_users);
362 }
363 
370 DBusUserDatabase*
372 {
373  _dbus_assert (database_locked);
374 
375  init_system_db ();
376 
377  return system_db;
378 }
379 
383 void
385 {
387  {
388  /* nothing to flush */
389  return;
390  }
391 
392  if (system_db != NULL)
393  _dbus_user_database_flush (system_db);
394 
396 }
397 
407 {
409  return FALSE;
410 
411  if (!init_system_db ())
412  {
414  return FALSE;
415  }
416  *username = &process_username;
418 
419  return TRUE;
420 }
421 
431 {
433  return FALSE;
434 
435  if (!init_system_db ())
436  {
438  return FALSE;
439  }
440  *homedir = &process_homedir;
442 
443  return TRUE;
444 }
445 
455  DBusString *homedir)
456 {
457  DBusUserDatabase *db;
458  const DBusUserInfo *info;
459 
460  /* FIXME: this can't distinguish ENOMEM from other errors */
462  return FALSE;
463 
465  if (db == NULL)
466  {
468  return FALSE;
469  }
470 
471  if (!_dbus_user_database_get_username (db, username,
472  &info, NULL))
473  {
475  return FALSE;
476  }
477 
478  if (!_dbus_string_append (homedir, info->homedir))
479  {
481  return FALSE;
482  }
483 
485  return TRUE;
486 }
487 
497  DBusString *homedir)
498 {
499  DBusUserDatabase *db;
500  const DBusUserInfo *info;
501 
502  if (uid == _dbus_getuid () && uid == _dbus_geteuid ())
503  {
504  const char *from_environment;
505 
506  from_environment = _dbus_getenv ("HOME");
507 
508  if (from_environment != NULL)
509  return _dbus_string_append (homedir, from_environment);
510  }
511 
512  /* FIXME: this can't distinguish ENOMEM from other errors */
514  return FALSE;
515 
517  if (db == NULL)
518  {
520  return FALSE;
521  }
522 
523  if (!_dbus_user_database_get_uid (db, uid,
524  &info, NULL))
525  {
527  return FALSE;
528  }
529 
530  if (!_dbus_string_append (homedir, info->homedir))
531  {
533  return FALSE;
534  }
535 
537  return TRUE;
538 }
539 
556  const DBusString *username)
557 {
558  DBusUserDatabase *db;
559  const DBusUserInfo *info;
560 
561  /* FIXME: this can't distinguish ENOMEM from other errors */
563  return FALSE;
564 
566  if (db == NULL)
567  {
569  return FALSE;
570  }
571 
572  if (!_dbus_user_database_get_username (db, username,
573  &info, NULL))
574  {
576  return FALSE;
577  }
578 
579  if (!_dbus_credentials_add_unix_uid(credentials, info->uid))
580  {
582  return FALSE;
583  }
584 
586  return TRUE;
587 }
588 
594 DBusUserDatabase*
596 {
597  DBusUserDatabase *db;
598 
599  db = dbus_new0 (DBusUserDatabase, 1);
600  if (db == NULL)
601  return NULL;
602 
603  db->refcount = 1;
604 
607 
608  if (db->users == NULL)
609  goto failed;
610 
613 
614  if (db->groups == NULL)
615  goto failed;
616 
617  db->users_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
618  NULL, (DBusFreeFunction) _dbus_user_info_unref);
619  if (db->users_by_name == NULL)
620  goto failed;
621 
622  db->groups_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
623  NULL, (DBusFreeFunction) _dbus_group_info_unref);
624  if (db->groups_by_name == NULL)
625  goto failed;
626 
627  return db;
628 
629  failed:
631  return NULL;
632 }
633 
637 void
638 _dbus_user_database_flush (DBusUserDatabase *db)
639 {
640  _dbus_hash_table_remove_all(db->users_by_name);
641  _dbus_hash_table_remove_all(db->groups_by_name);
642  _dbus_hash_table_remove_all(db->users);
643  _dbus_hash_table_remove_all(db->groups);
644 }
645 
646 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
647 
652 DBusUserDatabase *
653 _dbus_user_database_ref (DBusUserDatabase *db)
654 {
655  _dbus_assert (db->refcount > 0);
656 
657  db->refcount += 1;
658 
659  return db;
660 }
661 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */
662 
667 void
668 _dbus_user_database_unref (DBusUserDatabase *db)
669 {
670  _dbus_assert (db->refcount > 0);
671 
672  db->refcount -= 1;
673  if (db->refcount == 0)
674  {
675  if (db->users)
676  _dbus_hash_table_unref (db->users);
677 
678  if (db->groups)
679  _dbus_hash_table_unref (db->groups);
680 
681  if (db->users_by_name)
682  _dbus_hash_table_unref (db->users_by_name);
683 
684  if (db->groups_by_name)
685  _dbus_hash_table_unref (db->groups_by_name);
686 
687  dbus_free (db);
688  }
689 }
690 
702 _dbus_user_database_get_uid (DBusUserDatabase *db,
703  dbus_uid_t uid,
704  const DBusUserInfo **info,
705  DBusError *error)
706 {
707  *info = _dbus_user_database_lookup (db, uid, NULL, error);
708  return *info != NULL;
709 }
710 
721 _dbus_user_database_get_username (DBusUserDatabase *db,
722  const DBusString *username,
723  const DBusUserInfo **info,
724  DBusError *error)
725 {
726  *info = _dbus_user_database_lookup (db, DBUS_UID_UNSET, username, error);
727  return *info != NULL;
728 }
729 
732 /* Tests in dbus-userdb-util.c */
dbus_bool_t dbus_error_has_name(const DBusError *error, const char *name)
Checks whether the error is set and has the given name.
Definition: dbus-errors.c:302
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(* DBusFreeFunction)(void *memory)
The type of a function which frees a block of memory.
Definition: dbus-memory.h:64
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
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:702
void _dbus_user_info_free(DBusUserInfo *info)
Frees the members of info (but not info itself)
Definition: dbus-userdb.c:99
dbus_bool_t _dbus_user_database_lock_system(void)
Locks global system user database.
Definition: dbus-userdb.c:341
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
#define DBUS_ERROR_INIT
Expands to a suitable initializer for a DBusError on the stack.
Definition: dbus-errors.h:62
void _dbus_user_database_flush_system(void)
Flushes the system global user database;.
Definition: dbus-userdb.c:384
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_bool_t _dbus_homedir_from_username(const DBusString *username, DBusString *homedir)
Gets the home directory for the given user.
Definition: dbus-userdb.c:454
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
void _dbus_hash_table_unref(DBusHashTable *table)
Decrements the reference count for a hash table, freeing the hash table if the count reaches zero...
Definition: dbus-hash.c:361
void _dbus_user_database_flush(DBusUserDatabase *db)
Flush all information out of the user database.
Definition: dbus-userdb.c:638
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:702
void _dbus_user_database_unlock_system(void)
Unlocks global system user database.
Definition: dbus-userdb.c:358
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:175
Hash keys are strings.
Definition: dbus-hash.h:69
Hash keys are integer capable to hold a pointer.
Definition: dbus-hash.h:71
void _dbus_hash_table_remove_all(DBusHashTable *table)
Removed all entries from a hash table.
Definition: dbus-hash.c:418
char * groupname
Group name.
#define DBUS_UID_UNSET
an invalid UID used to represent an uninitialized dbus_uid_t field
Definition: dbus-sysdeps.h:115
dbus_bool_t _dbus_user_info_fill(DBusUserInfo *info, const DBusString *username, DBusError *error)
Gets user info for the given username.
void _dbus_group_info_free(DBusGroupInfo *info)
Frees the members of info (but not info itself).
Definition: dbus-userdb.c:112
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:371
dbus_bool_t _dbus_homedir_from_uid(dbus_uid_t uid, DBusString *homedir)
Gets the home directory for the given user.
Definition: dbus-userdb.c:496
dbus_gid_t * group_ids
Groups IDs, including above primary group.
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:59
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
dbus_bool_t _dbus_credentials_add_from_user(DBusCredentials *credentials, const DBusString *username)
Adds the credentials corresponding to the given username.
Definition: dbus-userdb.c:555
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.
void _dbus_user_info_unref(DBusUserInfo *info)
Decrements the reference count.
Definition: dbus-userdb.c:55
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
DBusUserDatabase * _dbus_user_database_new(void)
Creates a new user database object used to look up and cache user information.
Definition: dbus-userdb.c:595
#define _DBUS_UNLOCK(name)
Unlocks a global lock.
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init().
Definition: dbus-string.c:259
dbus_uid_t _dbus_geteuid(void)
Gets our effective UID.
#define TRUE
Expands to "1".
#define DBUS_UID_FORMAT
an appropriate printf format for dbus_uid_t
Definition: dbus-sysdeps.h:122
char * homedir
Home directory.
dbus_bool_t _dbus_homedir_from_current_process(const DBusString **homedir)
Gets homedir of user owning current process.
Definition: dbus-userdb.c:430
Information about a UNIX group.
size_t refcount
Reference count.
DBUS_PRIVATE_EXPORT dbus_bool_t _dbus_string_parse_uint(const DBusString *str, int start, unsigned long *value_return, int *end_return)
Parses an unsigned integer contained in a DBusString.
Definition: dbus-sysdeps.c:476
dbus_uid_t _dbus_getuid(void)
Gets our UID.
dbus_bool_t _dbus_user_info_fill_uid(DBusUserInfo *info, dbus_uid_t uid, DBusError *error)
Gets user info for the given user ID.
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
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
#define FALSE
Expands to "0".
DBusCredentials * credentials
Credentials of other end read from the socket.
DBUS_PRIVATE_EXPORT dbus_bool_t _dbus_register_shutdown_func(DBusShutdownFunction function, void *data)
Register a cleanup function to be called exactly once the next time dbus_shutdown() is called...
Definition: dbus-memory.c:811
#define _DBUS_LOCK(name)
Locks a global lock, initializing it first if necessary.
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:721
dbus_bool_t _dbus_credentials_add_unix_uid(DBusCredentials *credentials, dbus_uid_t uid)
Add a UNIX user ID to the credentials.
const char * _dbus_getenv(const char *varname)
Wrapper for getenv().
Definition: dbus-sysdeps.c:187
void _dbus_user_database_unref(DBusUserDatabase *db)
Decrements refcount of user database.
Definition: dbus-userdb.c:668
unsigned long dbus_uid_t
A user ID.
Definition: dbus-sysdeps.h:108
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
size_t refcount
Reference count.
dbus_bool_t _dbus_username_from_current_process(const DBusString **username)
Gets username of user owning current process.
Definition: dbus-userdb.c:406
DBusHashTable * _dbus_hash_table_new(DBusHashType type, DBusFreeFunction key_free_function, DBusFreeFunction value_free_function)
Constructs a new hash table.
Definition: dbus-hash.c:285
Information about a UNIX user.