D-Bus 1.14.8
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
287#ifdef HAVE_PDPLINUX
288 // Patch mem
289 const DBusUserInfo *info=NULL;
290#else
291 const DBusUserInfo *info;
292#endif
293
294 system_db = _dbus_user_database_new ();
295 if (system_db == NULL)
296 return FALSE;
297
298 if (!_dbus_user_database_get_uid (system_db,
299 _dbus_getuid (),
300 &info,
301 &error))
302 {
303 _dbus_user_database_unref (system_db);
304 system_db = NULL;
305
307 {
308 dbus_error_free (&error);
309 return FALSE;
310 }
311 else
312 {
313 /* This really should not happen. */
314 _dbus_warn ("Could not get password database information for UID of current process: %s",
315 error.message);
316 dbus_error_free (&error);
317 return FALSE;
318 }
319 }
320
321 if (!_dbus_string_init (&process_username))
322 {
323 _dbus_user_database_unref (system_db);
324 system_db = NULL;
325 return FALSE;
326 }
327
328 if (!_dbus_string_init (&process_homedir))
329 {
330 _dbus_string_free (&process_username);
331 _dbus_user_database_unref (system_db);
332 system_db = NULL;
333 return FALSE;
334 }
335
336 if (!_dbus_string_append (&process_username,
337 info->username) ||
338 !_dbus_string_append (&process_homedir,
339 info->homedir) ||
340 !_dbus_register_shutdown_func (shutdown_system_db, NULL))
341 {
342 _dbus_string_free (&process_username);
343 _dbus_string_free (&process_homedir);
344 _dbus_user_database_unref (system_db);
345 system_db = NULL;
346 return FALSE;
347 }
348#ifdef HAVE_PDPLINUX
349 //_dbus_verbose ("Added mem free\n");
350 if (info){
351 //_dbus_verbose ("start free\n");
352 //_dbus_user_info_free_allocated(info);
353 //_dbus_verbose ("end free\n");
354 }
355
356#endif
357 }
358
359 return TRUE;
360}
361
367{
368 if (_DBUS_LOCK (system_users))
369 {
370 database_locked = TRUE;
371 return TRUE;
372 }
373 else
374 {
375 return FALSE;
376 }
377}
378
382void
384{
385 database_locked = FALSE;
386 _DBUS_UNLOCK (system_users);
387}
388
395DBusUserDatabase*
397{
398 _dbus_assert (database_locked);
399
400 init_system_db ();
401
402 return system_db;
403}
404
408void
410{
412 {
413 /* nothing to flush */
414 return;
415 }
416
417 if (system_db != NULL)
418 _dbus_user_database_flush (system_db);
419
421}
422
432{
434 return FALSE;
435
436 if (!init_system_db ())
437 {
439 return FALSE;
440 }
441 *username = &process_username;
443
444 return TRUE;
445}
446
456{
458 return FALSE;
459
460 if (!init_system_db ())
461 {
463 return FALSE;
464 }
465 *homedir = &process_homedir;
467
468 return TRUE;
469}
470
480 DBusString *homedir)
481{
482 DBusUserDatabase *db;
483 const DBusUserInfo *info;
484
485 if (uid == _dbus_getuid () && uid == _dbus_geteuid ())
486 {
487 const char *from_environment;
488
489 from_environment = _dbus_getenv ("HOME");
490
491 if (from_environment != NULL)
492 return _dbus_string_append (homedir, from_environment);
493 }
494
495 /* FIXME: this can't distinguish ENOMEM from other errors */
497 return FALSE;
498
500 if (db == NULL)
501 {
503 return FALSE;
504 }
505
506 if (!_dbus_user_database_get_uid (db, uid,
507 &info, NULL))
508 {
510 return FALSE;
511 }
512
513 if (!_dbus_string_append (homedir, info->homedir))
514 {
516 return FALSE;
517 }
518
520 return TRUE;
521}
522
539 const DBusString *username,
540 DBusCredentialsAddFlags flags,
541 DBusError *error)
542{
543 DBusUserDatabase *db;
544 const DBusUserInfo *info;
545 unsigned long uid = DBUS_UID_UNSET;
546
547 /* Fast-path for the common case: if the "username" is all-numeric,
548 * then it's a Unix uid. This is true regardless of whether that uid
549 * exists in NSS or /etc/passwd or equivalent. */
550 if (_dbus_is_a_number (username, &uid))
551 {
552 _DBUS_STATIC_ASSERT (sizeof (uid) == sizeof (dbus_uid_t));
553
554 if (_dbus_credentials_add_unix_uid (credentials, uid))
555 {
556 return TRUE;
557 }
558 else
559 {
560 _DBUS_SET_OOM (error);
561 return FALSE;
562 }
563 }
564
565 /* If we aren't allowed to look in NSS or /etc/passwd, fail now. */
566 if (!(flags & DBUS_CREDENTIALS_ADD_FLAGS_USER_DATABASE))
567 {
569 "Expected a numeric Unix uid");
570 return FALSE;
571 }
572
574 {
575 _DBUS_SET_OOM (error);
576 return FALSE;
577 }
578
580 if (db == NULL)
581 {
583 _DBUS_SET_OOM (error);
584 return FALSE;
585 }
586
587 if (!_dbus_user_database_get_username (db, username,
588 &info, error))
589 {
591 return FALSE;
592 }
593
594 if (!_dbus_credentials_add_unix_uid(credentials, info->uid))
595 {
597 _DBUS_SET_OOM (error);
598 return FALSE;
599 }
600
602 return TRUE;
603}
604
610DBusUserDatabase*
612{
613 DBusUserDatabase *db;
614
615 db = dbus_new0 (DBusUserDatabase, 1);
616 if (db == NULL)
617 return NULL;
618
619 db->refcount = 1;
620
623
624 if (db->users == NULL)
625 goto failed;
626
629
630 if (db->groups == NULL)
631 goto failed;
632
633 db->users_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
635 if (db->users_by_name == NULL)
636 goto failed;
637
638 db->groups_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
640 if (db->groups_by_name == NULL)
641 goto failed;
642
643 return db;
644
645 failed:
647 return NULL;
648}
649
653void
654_dbus_user_database_flush (DBusUserDatabase *db)
655{
656 _dbus_hash_table_remove_all(db->users_by_name);
657 _dbus_hash_table_remove_all(db->groups_by_name);
659 _dbus_hash_table_remove_all(db->groups);
660}
661
662#ifdef DBUS_ENABLE_EMBEDDED_TESTS
668DBusUserDatabase *
669_dbus_user_database_ref (DBusUserDatabase *db)
670{
671 _dbus_assert (db->refcount > 0);
672
673 db->refcount += 1;
674
675 return db;
676}
677#endif /* DBUS_ENABLE_EMBEDDED_TESTS */
678
683void
684_dbus_user_database_unref (DBusUserDatabase *db)
685{
686 _dbus_assert (db->refcount > 0);
687
688 db->refcount -= 1;
689 if (db->refcount == 0)
690 {
691 if (db->users)
692 _dbus_hash_table_unref (db->users);
693
694 if (db->groups)
695 _dbus_hash_table_unref (db->groups);
696
697 if (db->users_by_name)
698 _dbus_hash_table_unref (db->users_by_name);
699
700 if (db->groups_by_name)
701 _dbus_hash_table_unref (db->groups_by_name);
702
703 dbus_free (db);
704 }
705}
706
718_dbus_user_database_get_uid (DBusUserDatabase *db,
719 dbus_uid_t uid,
720 const DBusUserInfo **info,
721 DBusError *error)
722{
723 *info = _dbus_user_database_lookup (db, uid, NULL, error);
724 return *info != NULL;
725}
726
738 const DBusString *username,
739 const DBusUserInfo **info,
740 DBusError *error)
741{
742 *info = _dbus_user_database_lookup (db, DBUS_UID_UNSET, username, error);
743 return *info != NULL;
744}
745
748/* 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:366
dbus_bool_t _dbus_homedir_from_current_process(const DBusString **homedir)
Gets homedir of user owning current process.
Definition: dbus-userdb.c:455
#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:611
#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:383
void _dbus_user_database_unref(DBusUserDatabase *db)
Decrements refcount of user database.
Definition: dbus-userdb.c:684
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:538
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:718
void _dbus_user_database_flush_system(void)
Flushes the system global user database;.
Definition: dbus-userdb.c:409
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:431
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:654
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:479
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:737
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:396
#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:157
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.