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