D-Bus  1.13.7
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  /* FIXME: this can't distinguish ENOMEM from other errors */
434  return FALSE;
435 
437  if (db == NULL)
438  {
440  return FALSE;
441  }
442 
443  if (!_dbus_user_database_get_username (db, username,
444  &info, NULL))
445  {
447  return FALSE;
448  }
449 
450  if (!_dbus_string_append (homedir, info->homedir))
451  {
453  return FALSE;
454  }
455 
457  return TRUE;
458 }
459 
469  DBusString *homedir)
470 {
471  DBusUserDatabase *db;
472  const DBusUserInfo *info;
473 
474  if (uid == _dbus_getuid () && uid == _dbus_geteuid ())
475  {
476  const char *from_environment;
477 
478  from_environment = _dbus_getenv ("HOME");
479 
480  if (from_environment != NULL)
481  return _dbus_string_append (homedir, from_environment);
482  }
483 
484  /* FIXME: this can't distinguish ENOMEM from other errors */
486  return FALSE;
487 
489  if (db == NULL)
490  {
492  return FALSE;
493  }
494 
495  if (!_dbus_user_database_get_uid (db, uid,
496  &info, NULL))
497  {
499  return FALSE;
500  }
501 
502  if (!_dbus_string_append (homedir, info->homedir))
503  {
505  return FALSE;
506  }
507 
509  return TRUE;
510 }
511 
528  const DBusString *username,
529  DBusCredentialsAddFlags flags,
530  DBusError *error)
531 {
532  DBusUserDatabase *db;
533  const DBusUserInfo *info;
534  unsigned long uid = DBUS_UID_UNSET;
535 
536  /* Fast-path for the common case: if the "username" is all-numeric,
537  * then it's a Unix uid. This is true regardless of whether that uid
538  * exists in NSS or /etc/passwd or equivalent. */
539  if (_dbus_is_a_number (username, &uid))
540  {
541  _DBUS_STATIC_ASSERT (sizeof (uid) == sizeof (dbus_uid_t));
542 
543  if (_dbus_credentials_add_unix_uid (credentials, uid))
544  {
545  return TRUE;
546  }
547  else
548  {
549  _DBUS_SET_OOM (error);
550  return FALSE;
551  }
552  }
553 
554  /* If we aren't allowed to look in NSS or /etc/passwd, fail now. */
555  if (!(flags & DBUS_CREDENTIALS_ADD_FLAGS_USER_DATABASE))
556  {
558  "Expected a numeric Unix uid");
559  return FALSE;
560  }
561 
563  {
564  _DBUS_SET_OOM (error);
565  return FALSE;
566  }
567 
569  if (db == NULL)
570  {
572  _DBUS_SET_OOM (error);
573  return FALSE;
574  }
575 
576  if (!_dbus_user_database_get_username (db, username,
577  &info, error))
578  {
580  return FALSE;
581  }
582 
583  if (!_dbus_credentials_add_unix_uid(credentials, info->uid))
584  {
586  _DBUS_SET_OOM (error);
587  return FALSE;
588  }
589 
591  return TRUE;
592 }
593 
599 DBusUserDatabase*
601 {
602  DBusUserDatabase *db;
603 
604  db = dbus_new0 (DBusUserDatabase, 1);
605  if (db == NULL)
606  return NULL;
607 
608  db->refcount = 1;
609 
612 
613  if (db->users == NULL)
614  goto failed;
615 
618 
619  if (db->groups == NULL)
620  goto failed;
621 
622  db->users_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
623  NULL, NULL);
624  if (db->users_by_name == NULL)
625  goto failed;
626 
627  db->groups_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
628  NULL, NULL);
629  if (db->groups_by_name == NULL)
630  goto failed;
631 
632  return db;
633 
634  failed:
636  return NULL;
637 }
638 
642 void
643 _dbus_user_database_flush (DBusUserDatabase *db)
644 {
645  _dbus_hash_table_remove_all(db->users_by_name);
646  _dbus_hash_table_remove_all(db->groups_by_name);
647  _dbus_hash_table_remove_all(db->users);
648  _dbus_hash_table_remove_all(db->groups);
649 }
650 
651 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
652 
657 DBusUserDatabase *
658 _dbus_user_database_ref (DBusUserDatabase *db)
659 {
660  _dbus_assert (db->refcount > 0);
661 
662  db->refcount += 1;
663 
664  return db;
665 }
666 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */
667 
672 void
673 _dbus_user_database_unref (DBusUserDatabase *db)
674 {
675  _dbus_assert (db->refcount > 0);
676 
677  db->refcount -= 1;
678  if (db->refcount == 0)
679  {
680  if (db->users)
681  _dbus_hash_table_unref (db->users);
682 
683  if (db->groups)
684  _dbus_hash_table_unref (db->groups);
685 
686  if (db->users_by_name)
687  _dbus_hash_table_unref (db->users_by_name);
688 
689  if (db->groups_by_name)
690  _dbus_hash_table_unref (db->groups_by_name);
691 
692  dbus_free (db);
693  }
694 }
695 
707 _dbus_user_database_get_uid (DBusUserDatabase *db,
708  dbus_uid_t uid,
709  const DBusUserInfo **info,
710  DBusError *error)
711 {
712  *info = _dbus_user_database_lookup (db, uid, NULL, error);
713  return *info != NULL;
714 }
715 
726 _dbus_user_database_get_username (DBusUserDatabase *db,
727  const DBusString *username,
728  const DBusUserInfo **info,
729  DBusError *error)
730 {
731  *info = _dbus_user_database_lookup (db, DBUS_UID_UNSET, username, error);
732  return *info != NULL;
733 }
734 
737 /* 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:952
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:1110
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_homedir_from_username(const DBusString *username, DBusString *homedir)
Gets the home directory for the given user.
Definition: dbus-userdb.c:426
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:1300
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:362
void _dbus_user_database_flush(DBusUserDatabase *db)
Flush all information out of the user database.
Definition: dbus-userdb.c:643
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:707
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: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:419
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:468
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:1225
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:600
#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:264
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:527
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:1060
#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:726
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:673
#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:1190
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:286
Information about a UNIX user.