1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
|
/* -*- mode: C; c-file-style: "gnu" -*- */
/* dbus-userdb.c User database abstraction
*
* Copyright (C) 2003, 2004 Red Hat, Inc.
*
* Licensed under the Academic Free License version 2.1
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#define DBUS_USERDB_INCLUDES_PRIVATE 1
#include "dbus-userdb.h"
#include "dbus-hash.h"
#include "dbus-test.h"
#include "dbus-internals.h"
#include "dbus-protocol.h"
#include "dbus-credentials.h"
#include <string.h>
/**
* @addtogroup DBusInternalsUtils
* @{
*/
/**
* Frees the given #DBusUserInfo's members with _dbus_user_info_free()
* and also calls dbus_free() on the block itself
*
* @param info the info
*/
void
_dbus_user_info_free_allocated (DBusUserInfo *info)
{
if (info == NULL) /* hash table will pass NULL */
return;
_dbus_user_info_free (info);
dbus_free (info);
}
/**
* Frees the given #DBusGroupInfo's members with _dbus_group_info_free()
* and also calls dbus_free() on the block itself
*
* @param info the info
*/
void
_dbus_group_info_free_allocated (DBusGroupInfo *info)
{
if (info == NULL) /* hash table will pass NULL */
return;
_dbus_group_info_free (info);
dbus_free (info);
}
/**
* Frees the members of info
* (but not info itself)
* @param info the user info struct
*/
void
_dbus_user_info_free (DBusUserInfo *info)
{
dbus_free (info->group_ids);
dbus_free (info->username);
dbus_free (info->homedir);
}
/**
* Frees the members of info (but not info itself).
*
* @param info the group info
*/
void
_dbus_group_info_free (DBusGroupInfo *info)
{
dbus_free (info->groupname);
}
/**
* Checks if a given string is actually a number
* and converts it if it is
*
* @param str the string to check
* @param num the memory location of the unsigned long to fill in
* @returns TRUE if str is a number and num is filled in
*/
dbus_bool_t
_dbus_is_a_number (const DBusString *str,
unsigned long *num)
{
int end;
if (_dbus_string_parse_uint (str, 0, num, &end) &&
end == _dbus_string_get_length (str))
return TRUE;
else
return FALSE;
}
/**
* Looks up a uid or username in the user database. Only one of name
* or UID can be provided. There are wrapper functions for this that
* are better to use, this one does no locking or anything on the
* database and otherwise sort of sucks.
*
* @param db the database
* @param uid the user ID or #DBUS_UID_UNSET
* @param username username or #NULL
* @param error error to fill in
* @returns the entry in the database
*/
DBusUserInfo*
_dbus_user_database_lookup (DBusUserDatabase *db,
dbus_uid_t uid,
const DBusString *username,
DBusError *error)
{
DBusUserInfo *info;
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
_dbus_assert (uid != DBUS_UID_UNSET || username != NULL);
/* See if the username is really a number */
if (uid == DBUS_UID_UNSET)
{
unsigned long n;
if (_dbus_is_a_number (username, &n))
uid = n;
}
#ifdef DBUS_ENABLE_USER_CACHE
if (uid != DBUS_UID_UNSET)
info = _dbus_hash_table_lookup_ulong (db->users, uid);
else
info = _dbus_hash_table_lookup_string (db->users_by_name, _dbus_string_get_const_data (username));
if (info)
{
_dbus_verbose ("Using cache for UID "DBUS_UID_FORMAT" information\n",
info->uid);
return info;
}
else
#else
if (1)
#endif
{
if (uid != DBUS_UID_UNSET)
_dbus_verbose ("No cache for UID "DBUS_UID_FORMAT"\n",
uid);
else
_dbus_verbose ("No cache for user \"%s\"\n",
_dbus_string_get_const_data (username));
info = dbus_new0 (DBusUserInfo, 1);
if (info == NULL)
{
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
return NULL;
}
if (uid != DBUS_UID_UNSET)
{
if (!_dbus_user_info_fill_uid (info, uid, error))
{
_DBUS_ASSERT_ERROR_IS_SET (error);
_dbus_user_info_free_allocated (info);
return NULL;
}
}
else
{
if (!_dbus_user_info_fill (info, username, error))
{
_DBUS_ASSERT_ERROR_IS_SET (error);
_dbus_user_info_free_allocated (info);
return NULL;
}
}
/* be sure we don't use these after here */
uid = DBUS_UID_UNSET;
username = NULL;
/* insert into hash */
if (!_dbus_hash_table_insert_ulong (db->users, info->uid, info))
{
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
_dbus_user_info_free_allocated (info);
return NULL;
}
if (!_dbus_hash_table_insert_string (db->users_by_name,
info->username,
info))
{
_dbus_hash_table_remove_ulong (db->users, info->uid);
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
return NULL;
}
return info;
}
}
static dbus_bool_t database_locked = FALSE;
static DBusUserDatabase *system_db = NULL;
static DBusString process_username;
static DBusString process_homedir;
static void
shutdown_system_db (void *data)
{
_dbus_user_database_unref (system_db);
system_db = NULL;
_dbus_string_free (&process_username);
_dbus_string_free (&process_homedir);
}
static dbus_bool_t
init_system_db (void)
{
_dbus_assert (database_locked);
if (system_db == NULL)
{
DBusError error;
const DBusUserInfo *info;
system_db = _dbus_user_database_new ();
if (system_db == NULL)
return FALSE;
dbus_error_init (&error);
if (!_dbus_user_database_get_uid (system_db,
_dbus_getuid (),
&info,
&error))
{
_dbus_user_database_unref (system_db);
system_db = NULL;
if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
{
dbus_error_free (&error);
return FALSE;
}
else
{
/* This really should not happen. */
_dbus_warn ("Could not get password database information for UID of current process: %s\n",
error.message);
dbus_error_free (&error);
return FALSE;
}
}
if (!_dbus_string_init (&process_username))
{
_dbus_user_database_unref (system_db);
system_db = NULL;
return FALSE;
}
if (!_dbus_string_init (&process_homedir))
{
_dbus_string_free (&process_username);
_dbus_user_database_unref (system_db);
system_db = NULL;
return FALSE;
}
if (!_dbus_string_append (&process_username,
info->username) ||
!_dbus_string_append (&process_homedir,
info->homedir) ||
!_dbus_register_shutdown_func (shutdown_system_db, NULL))
{
_dbus_string_free (&process_username);
_dbus_string_free (&process_homedir);
_dbus_user_database_unref (system_db);
system_db = NULL;
return FALSE;
}
}
return TRUE;
}
/**
* Locks global system user database.
*/
void
_dbus_user_database_lock_system (void)
{
_DBUS_LOCK (system_users);
database_locked = TRUE;
}
/**
* Unlocks global system user database.
*/
void
_dbus_user_database_unlock_system (void)
{
database_locked = FALSE;
_DBUS_UNLOCK (system_users);
}
/**
* Gets the system global user database;
* must be called with lock held (_dbus_user_database_lock_system()).
*
* @returns the database or #NULL if no memory
*/
DBusUserDatabase*
_dbus_user_database_get_system (void)
{
_dbus_assert (database_locked);
init_system_db ();
return system_db;
}
/**
* Flushes the system global user database;
*/
void
_dbus_user_database_flush_system (void)
{
_dbus_user_database_lock_system ();
_dbus_user_database_flush (system_db);
_dbus_user_database_unlock_system ();
}
/**
* Gets username of user owning current process. The returned string
* is valid until dbus_shutdown() is called.
*
* @param username place to store pointer to username
* @returns #FALSE if no memory
*/
dbus_bool_t
_dbus_username_from_current_process (const DBusString **username)
{
_dbus_user_database_lock_system ();
if (!init_system_db ())
{
_dbus_user_database_unlock_system ();
return FALSE;
}
*username = &process_username;
_dbus_user_database_unlock_system ();
return TRUE;
}
/**
* Gets homedir of user owning current process. The returned string
* is valid until dbus_shutdown() is called.
*
* @param homedir place to store pointer to homedir
* @returns #FALSE if no memory
*/
dbus_bool_t
_dbus_homedir_from_current_process (const DBusString **homedir)
{
_dbus_user_database_lock_system ();
if (!init_system_db ())
{
_dbus_user_database_unlock_system ();
return FALSE;
}
*homedir = &process_homedir;
_dbus_user_database_unlock_system ();
return TRUE;
}
/**
* Gets the home directory for the given user.
*
* @param username the username
* @param homedir string to append home directory to
* @returns #TRUE if user existed and we appended their homedir
*/
dbus_bool_t
_dbus_homedir_from_username (const DBusString *username,
DBusString *homedir)
{
DBusUserDatabase *db;
const DBusUserInfo *info;
_dbus_user_database_lock_system ();
db = _dbus_user_database_get_system ();
if (db == NULL)
{
_dbus_user_database_unlock_system ();
return FALSE;
}
if (!_dbus_user_database_get_username (db, username,
&info, NULL))
{
_dbus_user_database_unlock_system ();
return FALSE;
}
if (!_dbus_string_append (homedir, info->homedir))
{
_dbus_user_database_unlock_system ();
return FALSE;
}
_dbus_user_database_unlock_system ();
return TRUE;
}
/**
* Gets the home directory for the given user.
*
* @param uid the uid
* @param homedir string to append home directory to
* @returns #TRUE if user existed and we appended their homedir
*/
dbus_bool_t
_dbus_homedir_from_uid (dbus_uid_t uid,
DBusString *homedir)
{
DBusUserDatabase *db;
const DBusUserInfo *info;
_dbus_user_database_lock_system ();
db = _dbus_user_database_get_system ();
if (db == NULL)
{
_dbus_user_database_unlock_system ();
return FALSE;
}
if (!_dbus_user_database_get_uid (db, uid,
&info, NULL))
{
_dbus_user_database_unlock_system ();
return FALSE;
}
if (!_dbus_string_append (homedir, info->homedir))
{
_dbus_user_database_unlock_system ();
return FALSE;
}
_dbus_user_database_unlock_system ();
return TRUE;
}
/**
* Adds the credentials corresponding to the given username.
*
* Used among other purposes to parses a desired identity provided
* from a client in the auth protocol. On UNIX this means parsing a
* UID, on Windows probably parsing an SID string.
*
* @todo this is broken because it treats OOM and parse error
* the same way. Needs a #DBusError.
*
* @param credentials credentials to fill in
* @param username the username
* @returns #TRUE if the username existed and we got some credentials
*/
dbus_bool_t
_dbus_credentials_add_from_user (DBusCredentials *credentials,
const DBusString *username)
{
DBusUserDatabase *db;
const DBusUserInfo *info;
_dbus_user_database_lock_system ();
db = _dbus_user_database_get_system ();
if (db == NULL)
{
_dbus_user_database_unlock_system ();
return FALSE;
}
if (!_dbus_user_database_get_username (db, username,
&info, NULL))
{
_dbus_user_database_unlock_system ();
return FALSE;
}
if (!_dbus_credentials_add_unix_uid(credentials, info->uid))
{
_dbus_user_database_unlock_system ();
return FALSE;
}
_dbus_user_database_unlock_system ();
return TRUE;
}
/**
* Creates a new user database object used to look up and
* cache user information.
* @returns new database, or #NULL on out of memory
*/
DBusUserDatabase*
_dbus_user_database_new (void)
{
DBusUserDatabase *db;
db = dbus_new0 (DBusUserDatabase, 1);
if (db == NULL)
return NULL;
db->refcount = 1;
db->users = _dbus_hash_table_new (DBUS_HASH_ULONG,
NULL, (DBusFreeFunction) _dbus_user_info_free_allocated);
if (db->users == NULL)
goto failed;
db->groups = _dbus_hash_table_new (DBUS_HASH_ULONG,
NULL, (DBusFreeFunction) _dbus_group_info_free_allocated);
if (db->groups == NULL)
goto failed;
db->users_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
NULL, NULL);
if (db->users_by_name == NULL)
goto failed;
db->groups_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
NULL, NULL);
if (db->groups_by_name == NULL)
goto failed;
return db;
failed:
_dbus_user_database_unref (db);
return NULL;
}
/**
* Flush all information out of the user database.
*/
void
_dbus_user_database_flush (DBusUserDatabase *db)
{
_dbus_hash_table_remove_all(db->users_by_name);
_dbus_hash_table_remove_all(db->groups_by_name);
_dbus_hash_table_remove_all(db->users);
_dbus_hash_table_remove_all(db->groups);
}
#ifdef DBUS_BUILD_TESTS
/**
* Increments refcount of user database.
* @param db the database
* @returns the database
*/
DBusUserDatabase *
_dbus_user_database_ref (DBusUserDatabase *db)
{
_dbus_assert (db->refcount > 0);
db->refcount += 1;
return db;
}
#endif /* DBUS_BUILD_TESTS */
/**
* Decrements refcount of user database.
* @param db the database
*/
void
_dbus_user_database_unref (DBusUserDatabase *db)
{
_dbus_assert (db->refcount > 0);
db->refcount -= 1;
if (db->refcount == 0)
{
if (db->users)
_dbus_hash_table_unref (db->users);
if (db->groups)
_dbus_hash_table_unref (db->groups);
if (db->users_by_name)
_dbus_hash_table_unref (db->users_by_name);
if (db->groups_by_name)
_dbus_hash_table_unref (db->groups_by_name);
dbus_free (db);
}
}
/**
* Gets the user information for the given UID,
* returned user info should not be freed.
*
* @param db user database
* @param uid the user ID
* @param info return location for const ref to user info
* @param error error location
* @returns #FALSE if error is set
*/
dbus_bool_t
_dbus_user_database_get_uid (DBusUserDatabase *db,
dbus_uid_t uid,
const DBusUserInfo **info,
DBusError *error)
{
*info = _dbus_user_database_lookup (db, uid, NULL, error);
return *info != NULL;
}
/**
* Gets the user information for the given username.
*
* @param db user database
* @param username the user name
* @param info return location for const ref to user info
* @param error error location
* @returns #FALSE if error is set
*/
dbus_bool_t
_dbus_user_database_get_username (DBusUserDatabase *db,
const DBusString *username,
const DBusUserInfo **info,
DBusError *error)
{
*info = _dbus_user_database_lookup (db, DBUS_UID_UNSET, username, error);
return *info != NULL;
}
/** @} */
/* Tests in dbus-userdb-util.c */
|