2005-01-16 Havoc Pennington <hp@redhat.com>
[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_allocated (info);
65   dbus_free (info);
66 }
67
68 /**
69  * Looks up a uid or username in the user database.  Only one of name
70  * or UID can be provided. There are wrapper functions for this that
71  * are better to use, this one does no locking or anything on the
72  * database and otherwise sort of sucks.
73  *
74  * @param db the database
75  * @param uid the user ID or #DBUS_UID_UNSET
76  * @param username username or #NULL 
77  * @param error error to fill in
78  * @returns the entry in the database
79  */
80 DBusUserInfo*
81 _dbus_user_database_lookup (DBusUserDatabase *db,
82                             dbus_uid_t        uid,
83                             const DBusString *username,
84                             DBusError        *error)
85 {
86   DBusUserInfo *info;
87
88   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
89   _dbus_assert (uid != DBUS_UID_UNSET || username != NULL);
90   
91   if (uid != DBUS_UID_UNSET)
92     info = _dbus_hash_table_lookup_ulong (db->users, uid);
93   else
94     info = _dbus_hash_table_lookup_string (db->users_by_name, _dbus_string_get_const_data (username));
95   
96   if (info)
97     {
98       _dbus_verbose ("Using cache for UID "DBUS_UID_FORMAT" information\n",
99                      uid);
100       return info;
101     }
102   else
103     {
104       _dbus_verbose ("No cache for UID "DBUS_UID_FORMAT"\n",
105                      uid);
106       
107       info = dbus_new0 (DBusUserInfo, 1);
108       if (info == NULL)
109         {
110           dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
111           return NULL;
112         }
113
114       if (uid != DBUS_UID_UNSET)
115         {
116           if (!_dbus_user_info_fill_uid (info, uid, error))
117             {
118               _DBUS_ASSERT_ERROR_IS_SET (error);
119               _dbus_user_info_free_allocated (info);
120               return NULL;
121             }
122         }
123       else
124         {
125           if (!_dbus_user_info_fill (info, username, error))
126             {
127               _DBUS_ASSERT_ERROR_IS_SET (error);
128               _dbus_user_info_free_allocated (info);
129               return NULL;
130             }
131         }
132
133       /* be sure we don't use these after here */
134       uid = DBUS_UID_UNSET;
135       username = NULL;
136
137       /* insert into hash */
138       if (!_dbus_hash_table_insert_ulong (db->users, info->uid, info))
139         {
140           dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
141           _dbus_user_info_free_allocated (info);
142           return NULL;
143         }
144
145       if (!_dbus_hash_table_insert_string (db->users_by_name,
146                                            info->username,
147                                            info))
148         {
149           _dbus_hash_table_remove_ulong (db->users, info->uid);
150           dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
151           return NULL;
152         }
153       
154       return info;
155     }
156 }
157
158 _DBUS_DEFINE_GLOBAL_LOCK(system_users);
159 static dbus_bool_t database_locked = FALSE;
160 static DBusUserDatabase *system_db = NULL;
161 static DBusString process_username;
162 static DBusString process_homedir;
163       
164 static void
165 shutdown_system_db (void *data)
166 {
167   _dbus_user_database_unref (system_db);
168   system_db = NULL;
169   _dbus_string_free (&process_username);
170   _dbus_string_free (&process_homedir);
171 }
172
173 static dbus_bool_t
174 init_system_db (void)
175 {
176   _dbus_assert (database_locked);
177     
178   if (system_db == NULL)
179     {
180       DBusError error;
181       const DBusUserInfo *info;
182       
183       system_db = _dbus_user_database_new ();
184       if (system_db == NULL)
185         return FALSE;
186
187       dbus_error_init (&error);
188
189       if (!_dbus_user_database_get_uid (system_db,
190                                         _dbus_getuid (),
191                                         &info,
192                                         &error))
193         {
194           _dbus_user_database_unref (system_db);
195           system_db = NULL;
196           
197           if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
198             {
199               dbus_error_free (&error);
200               return FALSE;
201             }
202           else
203             {
204               /* This really should not happen. */
205               _dbus_warn ("Could not get password database information for UID of current process: %s\n",
206                           error.message);
207               dbus_error_free (&error);
208               return FALSE;
209             }
210         }
211
212       if (!_dbus_string_init (&process_username))
213         {
214           _dbus_user_database_unref (system_db);
215           system_db = NULL;
216           return FALSE;
217         }
218
219       if (!_dbus_string_init (&process_homedir))
220         {
221           _dbus_string_free (&process_username);
222           _dbus_user_database_unref (system_db);
223           system_db = NULL;
224           return FALSE;
225         }
226
227       if (!_dbus_string_append (&process_username,
228                                 info->username) ||
229           !_dbus_string_append (&process_homedir,
230                                 info->homedir) ||
231           !_dbus_register_shutdown_func (shutdown_system_db, NULL))
232         {
233           _dbus_string_free (&process_username);
234           _dbus_string_free (&process_homedir);
235           _dbus_user_database_unref (system_db);
236           system_db = NULL;
237           return FALSE;
238         }
239     }
240
241   return TRUE;
242 }
243
244 /**
245  * Locks global system user database.
246  */
247 void
248 _dbus_user_database_lock_system (void)
249 {
250   _DBUS_LOCK (system_users);
251   database_locked = TRUE;
252 }
253
254 /**
255  * Unlocks global system user database.
256  */
257 void
258 _dbus_user_database_unlock_system (void)
259 {
260   database_locked = FALSE;
261   _DBUS_UNLOCK (system_users);
262 }
263
264 /**
265  * Gets the system global user database;
266  * must be called with lock held (_dbus_user_database_lock_system()).
267  *
268  * @returns the database or #NULL if no memory
269  */
270 DBusUserDatabase*
271 _dbus_user_database_get_system (void)
272 {
273   _dbus_assert (database_locked);
274
275   init_system_db ();
276   
277   return system_db;
278 }
279
280 /**
281  * Gets username of user owning current process.  The returned string
282  * is valid until dbus_shutdown() is called.
283  *
284  * @param username place to store pointer to username
285  * @returns #FALSE if no memory
286  */
287 dbus_bool_t
288 _dbus_username_from_current_process (const DBusString **username)
289 {
290   _dbus_user_database_lock_system ();
291   if (!init_system_db ())
292     {
293       _dbus_user_database_unlock_system ();
294       return FALSE;
295     }
296   *username = &process_username;
297   _dbus_user_database_unlock_system ();  
298
299   return TRUE;
300 }
301
302 /**
303  * Gets homedir of user owning current process.  The returned string
304  * is valid until dbus_shutdown() is called.
305  *
306  * @param homedir place to store pointer to homedir
307  * @returns #FALSE if no memory
308  */
309 dbus_bool_t
310 _dbus_homedir_from_current_process (const DBusString  **homedir)
311 {
312   _dbus_user_database_lock_system ();
313   if (!init_system_db ())
314     {
315       _dbus_user_database_unlock_system ();
316       return FALSE;
317     }
318   *homedir = &process_homedir;
319   _dbus_user_database_unlock_system ();
320
321   return TRUE;
322 }
323
324 /**
325  * Gets the home directory for the given user.
326  *
327  * @param username the username
328  * @param homedir string to append home directory to
329  * @returns #TRUE if user existed and we appended their homedir
330  */
331 dbus_bool_t
332 _dbus_homedir_from_username (const DBusString *username,
333                              DBusString       *homedir)
334 {
335   DBusUserDatabase *db;
336   const DBusUserInfo *info;
337   _dbus_user_database_lock_system ();
338
339   db = _dbus_user_database_get_system ();
340   if (db == NULL)
341     {
342       _dbus_user_database_unlock_system ();
343       return FALSE;
344     }
345
346   if (!_dbus_user_database_get_username (db, username,
347                                          &info, NULL))
348     {
349       _dbus_user_database_unlock_system ();
350       return FALSE;
351     }
352
353   if (!_dbus_string_append (homedir, info->homedir))
354     {
355       _dbus_user_database_unlock_system ();
356       return FALSE;
357     }
358   
359   _dbus_user_database_unlock_system ();
360   return TRUE;
361 }
362
363 /**
364  * Gets the credentials corresponding to the given username.
365  *
366  * @param username the username
367  * @param credentials credentials to fill in
368  * @returns #TRUE if the username existed and we got some credentials
369  */
370 dbus_bool_t
371 _dbus_credentials_from_username (const DBusString *username,
372                                  DBusCredentials  *credentials)
373 {
374   DBusUserDatabase *db;
375   const DBusUserInfo *info;
376   _dbus_user_database_lock_system ();
377
378   db = _dbus_user_database_get_system ();
379   if (db == NULL)
380     {
381       _dbus_user_database_unlock_system ();
382       return FALSE;
383     }
384
385   if (!_dbus_user_database_get_username (db, username,
386                                          &info, NULL))
387     {
388       _dbus_user_database_unlock_system ();
389       return FALSE;
390     }
391
392   credentials->pid = DBUS_PID_UNSET;
393   credentials->uid = info->uid;
394   credentials->gid = info->primary_gid;
395   
396   _dbus_user_database_unlock_system ();
397   return TRUE;
398 }
399
400 /**
401  * Creates a new user database object used to look up and
402  * cache user information.
403  * @returns new database, or #NULL on out of memory
404  */
405 DBusUserDatabase*
406 _dbus_user_database_new (void)
407 {
408   DBusUserDatabase *db;
409   
410   db = dbus_new0 (DBusUserDatabase, 1);
411   if (db == NULL)
412     return NULL;
413
414   db->refcount = 1;
415
416   db->users = _dbus_hash_table_new (DBUS_HASH_ULONG,
417                                     NULL, (DBusFreeFunction) _dbus_user_info_free_allocated);
418   
419   if (db->users == NULL)
420     goto failed;
421
422   db->groups = _dbus_hash_table_new (DBUS_HASH_ULONG,
423                                      NULL, (DBusFreeFunction) _dbus_group_info_free_allocated);
424   
425   if (db->groups == NULL)
426     goto failed;
427
428   db->users_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
429                                             NULL, NULL);
430   if (db->users_by_name == NULL)
431     goto failed;
432   
433   db->groups_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
434                                              NULL, NULL);
435   if (db->groups_by_name == NULL)
436     goto failed;
437   
438   return db;
439   
440  failed:
441   _dbus_user_database_unref (db);
442   return NULL;
443 }
444
445 /**
446  * Increments refcount of user database.
447  * @param db the database
448  * @returns the database
449  */
450 DBusUserDatabase *
451 _dbus_user_database_ref (DBusUserDatabase  *db)
452 {
453   _dbus_assert (db->refcount > 0);
454
455   db->refcount += 1;
456
457   return db;
458 }
459
460 /**
461  * Decrements refcount of user database.
462  * @param db the database
463  */
464 void
465 _dbus_user_database_unref (DBusUserDatabase  *db)
466 {
467   _dbus_assert (db->refcount > 0);
468
469   db->refcount -= 1;
470   if (db->refcount == 0)
471     {
472       if (db->users)
473         _dbus_hash_table_unref (db->users);
474
475       if (db->groups)
476         _dbus_hash_table_unref (db->groups);
477
478       if (db->users_by_name)
479         _dbus_hash_table_unref (db->users_by_name);
480
481       if (db->groups_by_name)
482         _dbus_hash_table_unref (db->groups_by_name);
483       
484       dbus_free (db);
485     }
486 }
487
488 /**
489  * Gets the user information for the given UID,
490  * returned user info should not be freed. 
491  *
492  * @param db user database
493  * @param uid the user ID
494  * @param info return location for const ref to user info
495  * @param error error location
496  * @returns #FALSE if error is set
497  */
498 dbus_bool_t
499 _dbus_user_database_get_uid (DBusUserDatabase    *db,
500                              dbus_uid_t           uid,
501                              const DBusUserInfo **info,
502                              DBusError           *error)
503 {
504   *info = _dbus_user_database_lookup (db, uid, NULL, error);
505   return *info != NULL;
506 }
507
508 /**
509  * Gets the user information for the given username.
510  *
511  * @param db user database
512  * @param username the user name
513  * @param info return location for const ref to user info
514  * @param error error location
515  * @returns #FALSE if error is set
516  */
517 dbus_bool_t
518 _dbus_user_database_get_username  (DBusUserDatabase     *db,
519                                    const DBusString     *username,
520                                    const DBusUserInfo  **info,
521                                    DBusError            *error)
522 {
523   *info = _dbus_user_database_lookup (db, DBUS_UID_UNSET, username, error);
524   return *info != NULL;
525 }
526
527 /** @} */
528
529 /* Tests in dbus-userdb-util.c */