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