D-Bus 1.14.10
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
45static DBusUserInfo *
46_dbus_user_info_ref (DBusUserInfo *info)
47{
48 _dbus_assert (info->refcount > 0);
49 _dbus_assert (info->refcount < SIZE_MAX);
50 info->refcount++;
51 return info;
52}
53
61void
63{
64 if (info == NULL) /* hash table will pass NULL */
65 return;
66
67 _dbus_assert (info->refcount > 0);
68 _dbus_assert (info->refcount < SIZE_MAX);
69
70 if (--info->refcount > 0)
71 return;
72
74 dbus_free (info);
75}
76
84void
86{
87 if (info == NULL) /* hash table will pass NULL */
88 return;
89
90 _dbus_assert (info->refcount > 0);
91 _dbus_assert (info->refcount < SIZE_MAX);
92
93 if (--info->refcount > 0)
94 return;
95
97 dbus_free (info);
98}
99
105void
107{
108 dbus_free (info->group_ids);
109 dbus_free (info->username);
110 dbus_free (info->homedir);
111}
112
118void
120{
121 dbus_free (info->groupname);
122}
123
134 unsigned long *num)
135{
136 int end;
137
138 if (_dbus_string_parse_uint (str, 0, num, &end) &&
139 end == _dbus_string_get_length (str))
140 return TRUE;
141 else
142 return FALSE;
143}
144
157const DBusUserInfo *
158_dbus_user_database_lookup (DBusUserDatabase *db,
159 dbus_uid_t uid,
160 const DBusString *username,
161 DBusError *error)
162{
163 DBusUserInfo *info;
164
165 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
166 _dbus_assert (uid != DBUS_UID_UNSET || username != NULL);
167
168 /* See if the username is really a number */
169 if (uid == DBUS_UID_UNSET)
170 {
171 unsigned long n;
172
173 if (_dbus_is_a_number (username, &n))
174 uid = n;
175 }
176
177 if (uid != DBUS_UID_UNSET)
178 info = _dbus_hash_table_lookup_uintptr (db->users, uid);
179 else
180 info = _dbus_hash_table_lookup_string (db->users_by_name, _dbus_string_get_const_data (username));
181
182 if (info)
183 {
184 _dbus_verbose ("Using cache for UID "DBUS_UID_FORMAT" information\n",
185 info->uid);
186 return info;
187 }
188 else
189 {
190 if (uid != DBUS_UID_UNSET)
191 _dbus_verbose ("No cache for UID "DBUS_UID_FORMAT"\n",
192 uid);
193 else
194 _dbus_verbose ("No cache for user \"%s\"\n",
195 _dbus_string_get_const_data (username));
196
197 info = dbus_new0 (DBusUserInfo, 1);
198 if (info == NULL)
199 {
201 return NULL;
202 }
203 info->refcount = 1;
204
205 if (uid != DBUS_UID_UNSET)
206 {
207 if (!_dbus_user_info_fill_uid (info, uid, error))
208 {
209 _DBUS_ASSERT_ERROR_IS_SET (error);
211 return NULL;
212 }
213 }
214 else
215 {
216 if (!_dbus_user_info_fill (info, username, error))
217 {
218 _DBUS_ASSERT_ERROR_IS_SET (error);
220 return NULL;
221 }
222 }
223
224 /* be sure we don't use these after here */
225 uid = DBUS_UID_UNSET;
226 username = NULL;
227
228 /* insert into hash */
229 if (_dbus_hash_table_insert_uintptr (db->users, info->uid, info))
230 {
231 _dbus_user_info_ref (info);
232 }
233 else
234 {
237 return NULL;
238 }
239
240 if (_dbus_hash_table_insert_string (db->users_by_name,
241 info->username,
242 info))
243 {
244 _dbus_user_info_ref (info);
245 }
246 else
247 {
248 _dbus_hash_table_remove_uintptr (db->users, info->uid);
251 return NULL;
252 }
253
255
256 /* Return a borrowed pointer to the DBusUserInfo owned by the
257 * hash tables */
258 return info;
259 }
260}
261
262/* Protected by _DBUS_LOCK_system_users */
263static dbus_bool_t database_locked = FALSE;
264static DBusUserDatabase *system_db = NULL;
265static DBusString process_username;
266static DBusString process_homedir;
267
268static void
269shutdown_system_db (void *data)
270{
271 if (system_db != NULL)
272 _dbus_user_database_unref (system_db);
273 system_db = NULL;
274 _dbus_string_free (&process_username);
275 _dbus_string_free (&process_homedir);
276}
277
278static dbus_bool_t
279init_system_db (void)
280{
281 _dbus_assert (database_locked);
282
283 if (system_db == NULL)
284 {
286 const DBusUserInfo *info;
287
288 system_db = _dbus_user_database_new ();
289 if (system_db == NULL)
290 return FALSE;
291
292 if (!_dbus_user_database_get_uid (system_db,
293 _dbus_getuid (),
294 &info,
295 &error))
296 {
297 _dbus_user_database_unref (system_db);
298 system_db = NULL;
299
301 {
302 dbus_error_free (&error);
303 return FALSE;
304 }
305 else
306 {
307 /* This really should not happen. */
308 _dbus_warn ("Could not get password database information for UID of current process: %s",
309 error.message);
310 dbus_error_free (&error);
311 return FALSE;
312 }
313 }
314
315 if (!_dbus_string_init (&process_username))
316 {
317 _dbus_user_database_unref (system_db);
318 system_db = NULL;
319 return FALSE;
320 }
321
322 if (!_dbus_string_init (&process_homedir))
323 {
324 _dbus_string_free (&process_username);
325 _dbus_user_database_unref (system_db);
326 system_db = NULL;
327 return FALSE;
328 }
329
330 if (!_dbus_string_append (&process_username,
331 info->username) ||
332 !_dbus_string_append (&process_homedir,
333 info->homedir) ||
334 !_dbus_register_shutdown_func (shutdown_system_db, NULL))
335 {
336 _dbus_string_free (&process_username);
337 _dbus_string_free (&process_homedir);
338 _dbus_user_database_unref (system_db);
339 system_db = NULL;
340 return FALSE;
341 }
342 }
343
344 return TRUE;
345}
346
352{
353 if (_DBUS_LOCK (system_users))
354 {
355 database_locked = TRUE;
356 return TRUE;
357 }
358 else
359 {
360 return FALSE;
361 }
362}
363
367void
369{
370 database_locked = FALSE;
371 _DBUS_UNLOCK (system_users);
372}
373
380DBusUserDatabase*
382{
383 _dbus_assert (database_locked);
384
385 init_system_db ();
386
387 return system_db;
388}
389
393void
395{
397 {
398 /* nothing to flush */
399 return;
400 }
401
402 if (system_db != NULL)
403 _dbus_user_database_flush (system_db);
404
406}
407
417{
419 return FALSE;
420
421 if (!init_system_db ())
422 {
424 return FALSE;
425 }
426 *username = &process_username;
428
429 return TRUE;
430}
431
441{
443 return FALSE;
444
445 if (!init_system_db ())
446 {
448 return FALSE;
449 }
450 *homedir = &process_homedir;
452
453 return TRUE;
454}
455
465 DBusString *homedir)
466{
467 DBusUserDatabase *db;
468 const DBusUserInfo *info;
469
470 if (uid == _dbus_getuid () && uid == _dbus_geteuid ())
471 {
472 const char *from_environment;
473
474 from_environment = _dbus_getenv ("HOME");
475
476 if (from_environment != NULL)
477 return _dbus_string_append (homedir, from_environment);
478 }
479
480 /* FIXME: this can't distinguish ENOMEM from other errors */
482 return FALSE;
483
485 if (db == NULL)
486 {
488 return FALSE;
489 }
490
491 if (!_dbus_user_database_get_uid (db, uid,
492 &info, NULL))
493 {
495 return FALSE;
496 }
497
498 if (!_dbus_string_append (homedir, info->homedir))
499 {
501 return FALSE;
502 }
503
505 return TRUE;
506}
507
524 const DBusString *username,
525 DBusCredentialsAddFlags flags,
526 DBusError *error)
527{
528 DBusUserDatabase *db;
529 const DBusUserInfo *info;
530 unsigned long uid = DBUS_UID_UNSET;
531
532 /* Fast-path for the common case: if the "username" is all-numeric,
533 * then it's a Unix uid. This is true regardless of whether that uid
534 * exists in NSS or /etc/passwd or equivalent. */
535 if (_dbus_is_a_number (username, &uid))
536 {
537 _DBUS_STATIC_ASSERT (sizeof (uid) == sizeof (dbus_uid_t));
538
539 if (_dbus_credentials_add_unix_uid (credentials, uid))
540 {
541 return TRUE;
542 }
543 else
544 {
545 _DBUS_SET_OOM (error);
546 return FALSE;
547 }
548 }
549
550 /* If we aren't allowed to look in NSS or /etc/passwd, fail now. */
551 if (!(flags & DBUS_CREDENTIALS_ADD_FLAGS_USER_DATABASE))
552 {
554 "Expected a numeric Unix uid");
555 return FALSE;
556 }
557
559 {
560 _DBUS_SET_OOM (error);
561 return FALSE;
562 }
563
565 if (db == NULL)
566 {
568 _DBUS_SET_OOM (error);
569 return FALSE;
570 }
571
572 if (!_dbus_user_database_get_username (db, username,
573 &info, error))
574 {
576 return FALSE;
577 }
578
579 if (!_dbus_credentials_add_unix_uid(credentials, info->uid))
580 {
582 _DBUS_SET_OOM (error);
583 return FALSE;
584 }
585
587 return TRUE;
588}
589
595DBusUserDatabase*
597{
598 DBusUserDatabase *db;
599
600 db = dbus_new0 (DBusUserDatabase, 1);
601 if (db == NULL)
602 return NULL;
603
604 db->refcount = 1;
605
608
609 if (db->users == NULL)
610 goto failed;
611
614
615 if (db->groups == NULL)
616 goto failed;
617
618 db->users_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
620 if (db->users_by_name == NULL)
621 goto failed;
622
623 db->groups_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
625 if (db->groups_by_name == NULL)
626 goto failed;
627
628 return db;
629
630 failed:
632 return NULL;
633}
634
638void
639_dbus_user_database_flush (DBusUserDatabase *db)
640{
641 _dbus_hash_table_remove_all(db->users_by_name);
642 _dbus_hash_table_remove_all(db->groups_by_name);
644 _dbus_hash_table_remove_all(db->groups);
645}
646
647#ifdef DBUS_ENABLE_EMBEDDED_TESTS
653DBusUserDatabase *
654_dbus_user_database_ref (DBusUserDatabase *db)
655{
656 _dbus_assert (db->refcount > 0);
657
658 db->refcount += 1;
659
660 return db;
661}
662#endif /* DBUS_ENABLE_EMBEDDED_TESTS */
663
668void
669_dbus_user_database_unref (DBusUserDatabase *db)
670{
671 _dbus_assert (db->refcount > 0);
672
673 db->refcount -= 1;
674 if (db->refcount == 0)
675 {
676 if (db->users)
677 _dbus_hash_table_unref (db->users);
678
679 if (db->groups)
680 _dbus_hash_table_unref (db->groups);
681
682 if (db->users_by_name)
683 _dbus_hash_table_unref (db->users_by_name);
684
685 if (db->groups_by_name)
686 _dbus_hash_table_unref (db->groups_by_name);
687
688 dbus_free (db);
689 }
690}
691
703_dbus_user_database_get_uid (DBusUserDatabase *db,
704 dbus_uid_t uid,
705 const DBusUserInfo **info,
706 DBusError *error)
707{
708 *info = _dbus_user_database_lookup (db, uid, NULL, error);
709 return *info != NULL;
710}
711
723 const DBusString *username,
724 const DBusUserInfo **info,
725 DBusError *error)
726{
727 *info = _dbus_user_database_lookup (db, DBUS_UID_UNSET, username, error);
728 return *info != NULL;
729}
730
733/* Tests in dbus-userdb-util.c */
dbus_bool_t _dbus_credentials_add_unix_uid(DBusCredentials *credentials, dbus_uid_t uid)
Add a UNIX user ID to the credentials.
#define DBUS_ERROR_INIT
Expands to a suitable initializer for a DBusError on the stack.
Definition: dbus-errors.h:62
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
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_error_free(DBusError *error)
Frees an error that'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_remove_uintptr(DBusHashTable *table, uintptr_t key)
Removes the hash entry for the given key.
Definition: dbus-hash.c:1242
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
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
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
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
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
void _dbus_hash_table_remove_all(DBusHashTable *table)
Removed all entries from a hash table.
Definition: dbus-hash.c:424
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
@ DBUS_HASH_UINTPTR
Hash keys are integer capable to hold a pointer.
Definition: dbus-hash.h:71
@ DBUS_HASH_STRING
Hash keys are strings.
Definition: dbus-hash.h:69
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
dbus_bool_t _dbus_user_database_lock_system(void)
Locks global system user database.
Definition: dbus-userdb.c:351
dbus_bool_t _dbus_homedir_from_current_process(const DBusString **homedir)
Gets homedir of user owning current process.
Definition: dbus-userdb.c:440
#define _DBUS_UNLOCK(name)
Unlocks a global lock.
DBusUserDatabase * _dbus_user_database_new(void)
Creates a new user database object used to look up and cache user information.
Definition: dbus-userdb.c:596
#define _DBUS_LOCK(name)
Locks a global lock, initializing it first if necessary.
void _dbus_user_database_unlock_system(void)
Unlocks global system user database.
Definition: dbus-userdb.c:368
void _dbus_user_database_unref(DBusUserDatabase *db)
Decrements refcount of user database.
Definition: dbus-userdb.c:669
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:523
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:703
void _dbus_user_database_flush_system(void)
Flushes the system global user database;.
Definition: dbus-userdb.c:394
void _dbus_group_info_unref(DBusGroupInfo *info)
Decrements the reference count.
Definition: dbus-userdb.c:85
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:158
void _dbus_user_info_unref(DBusUserInfo *info)
Decrements the reference count.
Definition: dbus-userdb.c:62
dbus_bool_t _dbus_username_from_current_process(const DBusString **username)
Gets username of user owning current process.
Definition: dbus-userdb.c:416
void _dbus_user_info_free(DBusUserInfo *info)
Frees the members of info (but not info itself)
Definition: dbus-userdb.c:106
void _dbus_user_database_flush(DBusUserDatabase *db)
Flush all information out of the user database.
Definition: dbus-userdb.c:639
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:464
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
void _dbus_group_info_free(DBusGroupInfo *info)
Frees the members of info (but not info itself).
Definition: dbus-userdb.c:119
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:722
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:133
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:381
#define NULL
A null pointer, defined appropriately for C or C++.
#define TRUE
Expands to "1".
#define FALSE
Expands to "0".
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:801
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:692
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition: dbus-memory.h:58
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
#define DBUS_ERROR_INVALID_ARGS
Invalid arguments passed to a method call.
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:966
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:182
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:278
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_bool_t _dbus_user_info_fill(DBusUserInfo *info, const DBusString *username, DBusError *error)
Gets user info for the given username.
dbus_uid_t _dbus_geteuid(void)
Gets our effective 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.
unsigned long dbus_uid_t
A user ID.
Definition: dbus-sysdeps.h:137
#define DBUS_UID_UNSET
an invalid UID used to represent an uninitialized dbus_uid_t field
Definition: dbus-sysdeps.h:144
const char * _dbus_getenv(const char *varname)
Wrapper for getenv().
Definition: dbus-sysdeps.c:195
dbus_uid_t _dbus_getuid(void)
Gets our UID.
#define DBUS_UID_FORMAT
an appropriate printf format for dbus_uid_t
Definition: dbus-sysdeps.h:151
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
Object representing an exception.
Definition: dbus-errors.h:49
const char * message
public error message field
Definition: dbus-errors.h:51
Information about a UNIX group.
char * groupname
Group name.
size_t refcount
Reference count.
Information about a UNIX user.
dbus_uid_t uid
UID.
char * homedir
Home directory.
dbus_gid_t * group_ids
Groups IDs, including above primary group.
size_t refcount
Reference count.
char * username
Username.