D-Bus  1.12.24
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 
150 const DBusUserInfo *
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 a borrowed pointer to the DBusUserInfo owned by the
250  * hash tables */
251  return info;
252  }
253 }
254 
255 static dbus_bool_t database_locked = FALSE;
256 static DBusUserDatabase *system_db = NULL;
257 static DBusString process_username;
258 static DBusString process_homedir;
259 
260 static void
261 shutdown_system_db (void *data)
262 {
263  if (system_db != NULL)
264  _dbus_user_database_unref (system_db);
265  system_db = NULL;
266  _dbus_string_free (&process_username);
267  _dbus_string_free (&process_homedir);
268 }
269 
270 static dbus_bool_t
271 init_system_db (void)
272 {
273  _dbus_assert (database_locked);
274 
275  if (system_db == NULL)
276  {
277  DBusError error = DBUS_ERROR_INIT;
278  const DBusUserInfo *info;
279 
280  system_db = _dbus_user_database_new ();
281  if (system_db == NULL)
282  return FALSE;
283 
284  if (!_dbus_user_database_get_uid (system_db,
285  _dbus_getuid (),
286  &info,
287  &error))
288  {
289  _dbus_user_database_unref (system_db);
290  system_db = NULL;
291 
293  {
294  dbus_error_free (&error);
295  return FALSE;
296  }
297  else
298  {
299  /* This really should not happen. */
300  _dbus_warn ("Could not get password database information for UID of current process: %s",
301  error.message);
302  dbus_error_free (&error);
303  return FALSE;
304  }
305  }
306 
307  if (!_dbus_string_init (&process_username))
308  {
309  _dbus_user_database_unref (system_db);
310  system_db = NULL;
311  return FALSE;
312  }
313 
314  if (!_dbus_string_init (&process_homedir))
315  {
316  _dbus_string_free (&process_username);
317  _dbus_user_database_unref (system_db);
318  system_db = NULL;
319  return FALSE;
320  }
321 
322  if (!_dbus_string_append (&process_username,
323  info->username) ||
324  !_dbus_string_append (&process_homedir,
325  info->homedir) ||
326  !_dbus_register_shutdown_func (shutdown_system_db, NULL))
327  {
328  _dbus_string_free (&process_username);
329  _dbus_string_free (&process_homedir);
330  _dbus_user_database_unref (system_db);
331  system_db = NULL;
332  return FALSE;
333  }
334  }
335 
336  return TRUE;
337 }
338 
344 {
345  if (_DBUS_LOCK (system_users))
346  {
347  database_locked = TRUE;
348  return TRUE;
349  }
350  else
351  {
352  return FALSE;
353  }
354 }
355 
359 void
361 {
362  database_locked = FALSE;
363  _DBUS_UNLOCK (system_users);
364 }
365 
372 DBusUserDatabase*
374 {
375  _dbus_assert (database_locked);
376 
377  init_system_db ();
378 
379  return system_db;
380 }
381 
385 void
387 {
389  {
390  /* nothing to flush */
391  return;
392  }
393 
394  if (system_db != NULL)
395  _dbus_user_database_flush (system_db);
396 
398 }
399 
409 {
411  return FALSE;
412 
413  if (!init_system_db ())
414  {
416  return FALSE;
417  }
418  *username = &process_username;
420 
421  return TRUE;
422 }
423 
433 {
435  return FALSE;
436 
437  if (!init_system_db ())
438  {
440  return FALSE;
441  }
442  *homedir = &process_homedir;
444 
445  return TRUE;
446 }
447 
457  DBusString *homedir)
458 {
459  DBusUserDatabase *db;
460  const DBusUserInfo *info;
461 
462  /* FIXME: this can't distinguish ENOMEM from other errors */
464  return FALSE;
465 
467  if (db == NULL)
468  {
470  return FALSE;
471  }
472 
473  if (!_dbus_user_database_get_username (db, username,
474  &info, NULL))
475  {
477  return FALSE;
478  }
479 
480  if (!_dbus_string_append (homedir, info->homedir))
481  {
483  return FALSE;
484  }
485 
487  return TRUE;
488 }
489 
499  DBusString *homedir)
500 {
501  DBusUserDatabase *db;
502  const DBusUserInfo *info;
503 
504  if (uid == _dbus_getuid () && uid == _dbus_geteuid ())
505  {
506  const char *from_environment;
507 
508  from_environment = _dbus_getenv ("HOME");
509 
510  if (from_environment != NULL)
511  return _dbus_string_append (homedir, from_environment);
512  }
513 
514  /* FIXME: this can't distinguish ENOMEM from other errors */
516  return FALSE;
517 
519  if (db == NULL)
520  {
522  return FALSE;
523  }
524 
525  if (!_dbus_user_database_get_uid (db, uid,
526  &info, NULL))
527  {
529  return FALSE;
530  }
531 
532  if (!_dbus_string_append (homedir, info->homedir))
533  {
535  return FALSE;
536  }
537 
539  return TRUE;
540 }
541 
558  const DBusString *username)
559 {
560  DBusUserDatabase *db;
561  const DBusUserInfo *info;
562 
563  /* FIXME: this can't distinguish ENOMEM from other errors */
565  return FALSE;
566 
568  if (db == NULL)
569  {
571  return FALSE;
572  }
573 
574  if (!_dbus_user_database_get_username (db, username,
575  &info, NULL))
576  {
578  return FALSE;
579  }
580 
581  if (!_dbus_credentials_add_unix_uid(credentials, info->uid))
582  {
584  return FALSE;
585  }
586 
588  return TRUE;
589 }
590 
596 DBusUserDatabase*
598 {
599  DBusUserDatabase *db;
600 
601  db = dbus_new0 (DBusUserDatabase, 1);
602  if (db == NULL)
603  return NULL;
604 
605  db->refcount = 1;
606 
609 
610  if (db->users == NULL)
611  goto failed;
612 
615 
616  if (db->groups == NULL)
617  goto failed;
618 
619  db->users_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
620  NULL, (DBusFreeFunction) _dbus_user_info_unref);
621  if (db->users_by_name == NULL)
622  goto failed;
623 
624  db->groups_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
625  NULL, (DBusFreeFunction) _dbus_group_info_unref);
626  if (db->groups_by_name == NULL)
627  goto failed;
628 
629  return db;
630 
631  failed:
633  return NULL;
634 }
635 
639 void
640 _dbus_user_database_flush (DBusUserDatabase *db)
641 {
642  _dbus_hash_table_remove_all(db->users_by_name);
643  _dbus_hash_table_remove_all(db->groups_by_name);
644  _dbus_hash_table_remove_all(db->users);
645  _dbus_hash_table_remove_all(db->groups);
646 }
647 
648 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
649 
654 DBusUserDatabase *
655 _dbus_user_database_ref (DBusUserDatabase *db)
656 {
657  _dbus_assert (db->refcount > 0);
658 
659  db->refcount += 1;
660 
661  return db;
662 }
663 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */
664 
669 void
670 _dbus_user_database_unref (DBusUserDatabase *db)
671 {
672  _dbus_assert (db->refcount > 0);
673 
674  db->refcount -= 1;
675  if (db->refcount == 0)
676  {
677  if (db->users)
678  _dbus_hash_table_unref (db->users);
679 
680  if (db->groups)
681  _dbus_hash_table_unref (db->groups);
682 
683  if (db->users_by_name)
684  _dbus_hash_table_unref (db->users_by_name);
685 
686  if (db->groups_by_name)
687  _dbus_hash_table_unref (db->groups_by_name);
688 
689  dbus_free (db);
690  }
691 }
692 
704 _dbus_user_database_get_uid (DBusUserDatabase *db,
705  dbus_uid_t uid,
706  const DBusUserInfo **info,
707  DBusError *error)
708 {
709  *info = _dbus_user_database_lookup (db, uid, NULL, error);
710  return *info != NULL;
711 }
712 
723 _dbus_user_database_get_username (DBusUserDatabase *db,
724  const DBusString *username,
725  const DBusUserInfo **info,
726  DBusError *error)
727 {
728  *info = _dbus_user_database_lookup (db, DBUS_UID_UNSET, username, error);
729  return *info != NULL;
730 }
731 
734 /* 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:63
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:343
#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:386
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:456
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:640
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
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:141
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:373
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:498
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:58
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:557
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:597
#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: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:432
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:723
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:670
unsigned long dbus_uid_t
A user ID.
Definition: dbus-sysdeps.h:134
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:408
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.
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