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