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