D-Bus  1.13.12
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 
33 /* It isn't obvious from its name, but this file is part of the Unix
34  * system-dependent part of libdbus. Windows has a parallel
35  * implementation of some of it in dbus-sysdeps-win.c. */
36 #if defined(DBUS_WIN) || !defined(DBUS_UNIX)
37 #error "This file only makes sense on Unix OSs"
38 #endif
39 
51 void
53 {
54  if (info == NULL) /* hash table will pass NULL */
55  return;
56 
57  _dbus_user_info_free (info);
58  dbus_free (info);
59 }
60 
67 void
69 {
70  if (info == NULL) /* hash table will pass NULL */
71  return;
72 
73  _dbus_group_info_free (info);
74  dbus_free (info);
75 }
76 
82 void
84 {
85  dbus_free (info->group_ids);
86  dbus_free (info->username);
87  dbus_free (info->homedir);
88 }
89 
95 void
97 {
98  dbus_free (info->groupname);
99 }
100 
111  unsigned long *num)
112 {
113  int end;
114 
115  if (_dbus_string_parse_uint (str, 0, num, &end) &&
116  end == _dbus_string_get_length (str))
117  return TRUE;
118  else
119  return FALSE;
120 }
121 
135 _dbus_user_database_lookup (DBusUserDatabase *db,
136  dbus_uid_t uid,
137  const DBusString *username,
138  DBusError *error)
139 {
140  DBusUserInfo *info;
141 
142  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
143  _dbus_assert (uid != DBUS_UID_UNSET || username != NULL);
144 
145  /* See if the username is really a number */
146  if (uid == DBUS_UID_UNSET)
147  {
148  unsigned long n;
149 
150  if (_dbus_is_a_number (username, &n))
151  uid = n;
152  }
153 
154  if (uid != DBUS_UID_UNSET)
155  info = _dbus_hash_table_lookup_uintptr (db->users, uid);
156  else
157  info = _dbus_hash_table_lookup_string (db->users_by_name, _dbus_string_get_const_data (username));
158 
159  if (info)
160  {
161  _dbus_verbose ("Using cache for UID "DBUS_UID_FORMAT" information\n",
162  info->uid);
163  return info;
164  }
165  else
166  {
167  if (uid != DBUS_UID_UNSET)
168  _dbus_verbose ("No cache for UID "DBUS_UID_FORMAT"\n",
169  uid);
170  else
171  _dbus_verbose ("No cache for user \"%s\"\n",
172  _dbus_string_get_const_data (username));
173 
174  info = dbus_new0 (DBusUserInfo, 1);
175  if (info == NULL)
176  {
178  return NULL;
179  }
180 
181  if (uid != DBUS_UID_UNSET)
182  {
183  if (!_dbus_user_info_fill_uid (info, uid, error))
184  {
185  _DBUS_ASSERT_ERROR_IS_SET (error);
187  return NULL;
188  }
189  }
190  else
191  {
192  if (!_dbus_user_info_fill (info, username, error))
193  {
194  _DBUS_ASSERT_ERROR_IS_SET (error);
196  return NULL;
197  }
198  }
199 
200  /* be sure we don't use these after here */
201  uid = DBUS_UID_UNSET;
202  username = NULL;
203 
204  /* insert into hash */
205  if (!_dbus_hash_table_insert_uintptr (db->users, info->uid, info))
206  {
209  return NULL;
210  }
211 
212  if (!_dbus_hash_table_insert_string (db->users_by_name,
213  info->username,
214  info))
215  {
216  _dbus_hash_table_remove_uintptr (db->users, info->uid);
218  return NULL;
219  }
220 
221  return info;
222  }
223 }
224 
225 static dbus_bool_t database_locked = FALSE;
226 static DBusUserDatabase *system_db = NULL;
227 static DBusString process_username;
228 static DBusString process_homedir;
229 
230 static void
231 shutdown_system_db (void *data)
232 {
233  if (system_db != NULL)
234  _dbus_user_database_unref (system_db);
235  system_db = NULL;
236  _dbus_string_free (&process_username);
237  _dbus_string_free (&process_homedir);
238 }
239 
240 static dbus_bool_t
241 init_system_db (void)
242 {
243  _dbus_assert (database_locked);
244 
245  if (system_db == NULL)
246  {
247  DBusError error = DBUS_ERROR_INIT;
248  const DBusUserInfo *info;
249 
250  system_db = _dbus_user_database_new ();
251  if (system_db == NULL)
252  return FALSE;
253 
254  if (!_dbus_user_database_get_uid (system_db,
255  _dbus_getuid (),
256  &info,
257  &error))
258  {
259  _dbus_user_database_unref (system_db);
260  system_db = NULL;
261 
263  {
264  dbus_error_free (&error);
265  return FALSE;
266  }
267  else
268  {
269  /* This really should not happen. */
270  _dbus_warn ("Could not get password database information for UID of current process: %s",
271  error.message);
272  dbus_error_free (&error);
273  return FALSE;
274  }
275  }
276 
277  if (!_dbus_string_init (&process_username))
278  {
279  _dbus_user_database_unref (system_db);
280  system_db = NULL;
281  return FALSE;
282  }
283 
284  if (!_dbus_string_init (&process_homedir))
285  {
286  _dbus_string_free (&process_username);
287  _dbus_user_database_unref (system_db);
288  system_db = NULL;
289  return FALSE;
290  }
291 
292  if (!_dbus_string_append (&process_username,
293  info->username) ||
294  !_dbus_string_append (&process_homedir,
295  info->homedir) ||
296  !_dbus_register_shutdown_func (shutdown_system_db, NULL))
297  {
298  _dbus_string_free (&process_username);
299  _dbus_string_free (&process_homedir);
300  _dbus_user_database_unref (system_db);
301  system_db = NULL;
302  return FALSE;
303  }
304  }
305 
306  return TRUE;
307 }
308 
314 {
315  if (_DBUS_LOCK (system_users))
316  {
317  database_locked = TRUE;
318  return TRUE;
319  }
320  else
321  {
322  return FALSE;
323  }
324 }
325 
329 void
331 {
332  database_locked = FALSE;
333  _DBUS_UNLOCK (system_users);
334 }
335 
342 DBusUserDatabase*
344 {
345  _dbus_assert (database_locked);
346 
347  init_system_db ();
348 
349  return system_db;
350 }
351 
355 void
357 {
359  {
360  /* nothing to flush */
361  return;
362  }
363 
364  if (system_db != NULL)
365  _dbus_user_database_flush (system_db);
366 
368 }
369 
379 {
381  return FALSE;
382 
383  if (!init_system_db ())
384  {
386  return FALSE;
387  }
388  *username = &process_username;
390 
391  return TRUE;
392 }
393 
403 {
405  return FALSE;
406 
407  if (!init_system_db ())
408  {
410  return FALSE;
411  }
412  *homedir = &process_homedir;
414 
415  return TRUE;
416 }
417 
427  DBusString *homedir)
428 {
429  DBusUserDatabase *db;
430  const DBusUserInfo *info;
431 
432  if (uid == _dbus_getuid () && uid == _dbus_geteuid ())
433  {
434  const char *from_environment;
435 
436  from_environment = _dbus_getenv ("HOME");
437 
438  if (from_environment != NULL)
439  return _dbus_string_append (homedir, from_environment);
440  }
441 
442  /* FIXME: this can't distinguish ENOMEM from other errors */
444  return FALSE;
445 
447  if (db == NULL)
448  {
450  return FALSE;
451  }
452 
453  if (!_dbus_user_database_get_uid (db, uid,
454  &info, NULL))
455  {
457  return FALSE;
458  }
459 
460  if (!_dbus_string_append (homedir, info->homedir))
461  {
463  return FALSE;
464  }
465 
467  return TRUE;
468 }
469 
486  const DBusString *username,
487  DBusCredentialsAddFlags flags,
488  DBusError *error)
489 {
490  DBusUserDatabase *db;
491  const DBusUserInfo *info;
492  unsigned long uid = DBUS_UID_UNSET;
493 
494  /* Fast-path for the common case: if the "username" is all-numeric,
495  * then it's a Unix uid. This is true regardless of whether that uid
496  * exists in NSS or /etc/passwd or equivalent. */
497  if (_dbus_is_a_number (username, &uid))
498  {
499  _DBUS_STATIC_ASSERT (sizeof (uid) == sizeof (dbus_uid_t));
500 
501  if (_dbus_credentials_add_unix_uid (credentials, uid))
502  {
503  return TRUE;
504  }
505  else
506  {
507  _DBUS_SET_OOM (error);
508  return FALSE;
509  }
510  }
511 
512  /* If we aren't allowed to look in NSS or /etc/passwd, fail now. */
513  if (!(flags & DBUS_CREDENTIALS_ADD_FLAGS_USER_DATABASE))
514  {
516  "Expected a numeric Unix uid");
517  return FALSE;
518  }
519 
521  {
522  _DBUS_SET_OOM (error);
523  return FALSE;
524  }
525 
527  if (db == NULL)
528  {
530  _DBUS_SET_OOM (error);
531  return FALSE;
532  }
533 
534  if (!_dbus_user_database_get_username (db, username,
535  &info, error))
536  {
538  return FALSE;
539  }
540 
541  if (!_dbus_credentials_add_unix_uid(credentials, info->uid))
542  {
544  _DBUS_SET_OOM (error);
545  return FALSE;
546  }
547 
549  return TRUE;
550 }
551 
557 DBusUserDatabase*
559 {
560  DBusUserDatabase *db;
561 
562  db = dbus_new0 (DBusUserDatabase, 1);
563  if (db == NULL)
564  return NULL;
565 
566  db->refcount = 1;
567 
570 
571  if (db->users == NULL)
572  goto failed;
573 
576 
577  if (db->groups == NULL)
578  goto failed;
579 
580  db->users_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
581  NULL, NULL);
582  if (db->users_by_name == NULL)
583  goto failed;
584 
585  db->groups_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
586  NULL, NULL);
587  if (db->groups_by_name == NULL)
588  goto failed;
589 
590  return db;
591 
592  failed:
594  return NULL;
595 }
596 
600 void
601 _dbus_user_database_flush (DBusUserDatabase *db)
602 {
603  _dbus_hash_table_remove_all(db->users_by_name);
604  _dbus_hash_table_remove_all(db->groups_by_name);
605  _dbus_hash_table_remove_all(db->users);
606  _dbus_hash_table_remove_all(db->groups);
607 }
608 
609 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
610 
615 DBusUserDatabase *
616 _dbus_user_database_ref (DBusUserDatabase *db)
617 {
618  _dbus_assert (db->refcount > 0);
619 
620  db->refcount += 1;
621 
622  return db;
623 }
624 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */
625 
630 void
631 _dbus_user_database_unref (DBusUserDatabase *db)
632 {
633  _dbus_assert (db->refcount > 0);
634 
635  db->refcount -= 1;
636  if (db->refcount == 0)
637  {
638  if (db->users)
639  _dbus_hash_table_unref (db->users);
640 
641  if (db->groups)
642  _dbus_hash_table_unref (db->groups);
643 
644  if (db->users_by_name)
645  _dbus_hash_table_unref (db->users_by_name);
646 
647  if (db->groups_by_name)
648  _dbus_hash_table_unref (db->groups_by_name);
649 
650  dbus_free (db);
651  }
652 }
653 
665 _dbus_user_database_get_uid (DBusUserDatabase *db,
666  dbus_uid_t uid,
667  const DBusUserInfo **info,
668  DBusError *error)
669 {
670  *info = _dbus_user_database_lookup (db, uid, NULL, error);
671  return *info != NULL;
672 }
673 
684 _dbus_user_database_get_username (DBusUserDatabase *db,
685  const DBusString *username,
686  const DBusUserInfo **info,
687  DBusError *error)
688 {
689  *info = _dbus_user_database_lookup (db, DBUS_UID_UNSET, username, error);
690  return *info != NULL;
691 }
692 
695 /* 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:959
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
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:63
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
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:703
void _dbus_user_info_free(DBusUserInfo *info)
Frees the members of info (but not info itself)
Definition: dbus-userdb.c:83
dbus_bool_t _dbus_user_database_lock_system(void)
Locks global system user database.
Definition: dbus-userdb.c:313
#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:356
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_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
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:367
void _dbus_user_database_flush(DBusUserDatabase *db)
Flush all information out of the user database.
Definition: dbus-userdb.c:601
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:665
void _dbus_user_database_unlock_system(void)
Unlocks global system user database.
Definition: dbus-userdb.c:330
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:182
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:424
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
void _dbus_user_info_free_allocated(DBusUserInfo *info)
Frees the given DBusUserInfo&#39;s members with _dbus_user_info_free() and also calls dbus_free() on the ...
Definition: dbus-userdb.c:52
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:96
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:343
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:426
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:110
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition: dbus-memory.h:58
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:68
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_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_uid_t uid
UID.
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:558
#define _DBUS_UNLOCK(name)
Unlocks a global lock.
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
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:148
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:402
Information about a UNIX group.
dbus_bool_t _dbus_credentials_add_from_user(DBusCredentials *credentials, const DBusString *username, DBusCredentialsAddFlags flags, DBusError *error)
Adds the credentials corresponding to the given username.
Definition: dbus-userdb.c:485
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:483
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:1112
#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:812
#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:684
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:195
void _dbus_user_database_unref(DBusUserDatabase *db)
Decrements refcount of user database.
Definition: dbus-userdb.c:631
#define DBUS_ERROR_INVALID_ARGS
Invalid arguments passed to a method call.
unsigned long dbus_uid_t
A user ID.
Definition: dbus-sysdeps.h:134
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_username_from_current_process(const DBusString **username)
Gets username of user owning current process.
Definition: dbus-userdb.c:378
DBusHashTable * _dbus_hash_table_new(DBusHashType type, DBusFreeFunction key_free_function, DBusFreeFunction value_free_function)
Constructs a new hash table.
Definition: dbus-hash.c:291
Information about a UNIX user.