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