* reverted global rename of function _dbus_username_from_current_process.
[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 "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_USER_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;
243       const DBusUserInfo *info;
244       
245       system_db = _dbus_user_database_new ();
246       if (system_db == NULL)
247         return FALSE;
248
249       dbus_error_init (&error);
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 void
310 _dbus_user_database_lock_system (void)
311 {
312   _DBUS_LOCK (system_users);
313   database_locked = TRUE;
314 }
315
316 /**
317  * Unlocks global system user database.
318  */
319 void
320 _dbus_user_database_unlock_system (void)
321 {
322   database_locked = FALSE;
323   _DBUS_UNLOCK (system_users);
324 }
325
326 /**
327  * Gets the system global user database;
328  * must be called with lock held (_dbus_user_database_lock_system()).
329  *
330  * @returns the database or #NULL if no memory
331  */
332 DBusUserDatabase*
333 _dbus_user_database_get_system (void)
334 {
335   _dbus_assert (database_locked);
336
337   init_system_db ();
338   
339   return system_db;
340 }
341
342 /**
343  * Flushes the system global user database;
344  */
345 void
346 _dbus_user_database_flush_system (void)
347 {
348   _dbus_user_database_lock_system ();
349    
350   _dbus_user_database_flush (system_db);
351
352   _dbus_user_database_unlock_system ();
353 }
354
355 /**
356  * Gets username of user owning current process.  The returned string
357  * is valid until dbus_shutdown() is called.
358  *
359  * @param username place to store pointer to username
360  * @returns #FALSE if no memory
361  */
362 dbus_bool_t
363 _dbus_username_from_current_process (const DBusString **username)
364 {
365   _dbus_user_database_lock_system ();
366   if (!init_system_db ())
367     {
368       _dbus_user_database_unlock_system ();
369       return FALSE;
370     }
371   *username = &process_username;
372   _dbus_user_database_unlock_system ();  
373
374   return TRUE;
375 }
376
377 /**
378  * Gets homedir of user owning current process.  The returned string
379  * is valid until dbus_shutdown() is called.
380  *
381  * @param homedir place to store pointer to homedir
382  * @returns #FALSE if no memory
383  */
384 dbus_bool_t
385 _dbus_homedir_from_current_process (const DBusString  **homedir)
386 {
387   _dbus_user_database_lock_system ();
388   if (!init_system_db ())
389     {
390       _dbus_user_database_unlock_system ();
391       return FALSE;
392     }
393   *homedir = &process_homedir;
394   _dbus_user_database_unlock_system ();
395
396   return TRUE;
397 }
398
399 /**
400  * Gets the home directory for the given user.
401  *
402  * @param username the username
403  * @param homedir string to append home directory to
404  * @returns #TRUE if user existed and we appended their homedir
405  */
406 dbus_bool_t
407 _dbus_homedir_from_username (const DBusString *username,
408                              DBusString       *homedir)
409 {
410   DBusUserDatabase *db;
411   const DBusUserInfo *info;
412   _dbus_user_database_lock_system ();
413
414   db = _dbus_user_database_get_system ();
415   if (db == NULL)
416     {
417       _dbus_user_database_unlock_system ();
418       return FALSE;
419     }
420
421   if (!_dbus_user_database_get_username (db, username,
422                                          &info, NULL))
423     {
424       _dbus_user_database_unlock_system ();
425       return FALSE;
426     }
427
428   if (!_dbus_string_append (homedir, info->homedir))
429     {
430       _dbus_user_database_unlock_system ();
431       return FALSE;
432     }
433   
434   _dbus_user_database_unlock_system ();
435   return TRUE;
436 }
437
438 /**
439  * Adds the credentials corresponding to the given username.
440  *
441  * @param credentials credentials to fill in 
442  * @param username the username
443  * @returns #TRUE if the username existed and we got some credentials
444  */
445 dbus_bool_t
446 _dbus_credentials_add_from_username (DBusCredentials  *credentials,
447                                      const DBusString *username)
448 {
449   DBusUserDatabase *db;
450   const DBusUserInfo *info;
451
452   _dbus_user_database_lock_system ();
453
454   db = _dbus_user_database_get_system ();
455   if (db == NULL)
456     {
457       _dbus_user_database_unlock_system ();
458       return FALSE;
459     }
460
461   if (!_dbus_user_database_get_username (db, username,
462                                          &info, NULL))
463     {
464       _dbus_user_database_unlock_system ();
465       return FALSE;
466     }
467
468   if (!_dbus_credentials_add_unix_uid(credentials, info->uid))
469     {
470       _dbus_user_database_unlock_system ();
471       return FALSE;
472     }
473   
474   _dbus_user_database_unlock_system ();
475   return TRUE;
476 }
477
478 /**
479  * Creates a new user database object used to look up and
480  * cache user information.
481  * @returns new database, or #NULL on out of memory
482  */
483 DBusUserDatabase*
484 _dbus_user_database_new (void)
485 {
486   DBusUserDatabase *db;
487   
488   db = dbus_new0 (DBusUserDatabase, 1);
489   if (db == NULL)
490     return NULL;
491
492   db->refcount = 1;
493
494   db->users = _dbus_hash_table_new (DBUS_HASH_ULONG,
495                                     NULL, (DBusFreeFunction) _dbus_user_info_free_allocated);
496   
497   if (db->users == NULL)
498     goto failed;
499
500   db->groups = _dbus_hash_table_new (DBUS_HASH_ULONG,
501                                      NULL, (DBusFreeFunction) _dbus_group_info_free_allocated);
502   
503   if (db->groups == NULL)
504     goto failed;
505
506   db->users_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
507                                             NULL, NULL);
508   if (db->users_by_name == NULL)
509     goto failed;
510   
511   db->groups_by_name = _dbus_hash_table_new (DBUS_HASH_STRING,
512                                              NULL, NULL);
513   if (db->groups_by_name == NULL)
514     goto failed;
515   
516   return db;
517   
518  failed:
519   _dbus_user_database_unref (db);
520   return NULL;
521 }
522
523 /**
524  * Flush all information out of the user database. 
525  */
526 void
527 _dbus_user_database_flush (DBusUserDatabase *db) 
528 {
529   _dbus_hash_table_remove_all(db->users_by_name);
530   _dbus_hash_table_remove_all(db->groups_by_name);
531   _dbus_hash_table_remove_all(db->users);
532   _dbus_hash_table_remove_all(db->groups);
533 }
534
535 #ifdef DBUS_BUILD_TESTS
536 /**
537  * Increments refcount of user database.
538  * @param db the database
539  * @returns the database
540  */
541 DBusUserDatabase *
542 _dbus_user_database_ref (DBusUserDatabase  *db)
543 {
544   _dbus_assert (db->refcount > 0);
545
546   db->refcount += 1;
547
548   return db;
549 }
550 #endif /* DBUS_BUILD_TESTS */
551
552 /**
553  * Decrements refcount of user database.
554  * @param db the database
555  */
556 void
557 _dbus_user_database_unref (DBusUserDatabase  *db)
558 {
559   _dbus_assert (db->refcount > 0);
560
561   db->refcount -= 1;
562   if (db->refcount == 0)
563     {
564       if (db->users)
565         _dbus_hash_table_unref (db->users);
566
567       if (db->groups)
568         _dbus_hash_table_unref (db->groups);
569
570       if (db->users_by_name)
571         _dbus_hash_table_unref (db->users_by_name);
572
573       if (db->groups_by_name)
574         _dbus_hash_table_unref (db->groups_by_name);
575       
576       dbus_free (db);
577     }
578 }
579
580 /**
581  * Gets the user information for the given UID,
582  * returned user info should not be freed. 
583  *
584  * @param db user database
585  * @param uid the user ID
586  * @param info return location for const ref to user info
587  * @param error error location
588  * @returns #FALSE if error is set
589  */
590 dbus_bool_t
591 _dbus_user_database_get_uid (DBusUserDatabase    *db,
592                              dbus_uid_t           uid,
593                              const DBusUserInfo **info,
594                              DBusError           *error)
595 {
596   *info = _dbus_user_database_lookup (db, uid, NULL, error);
597   return *info != NULL;
598 }
599
600 /**
601  * Gets the user information for the given username.
602  *
603  * @param db user database
604  * @param username the user name
605  * @param info return location for const ref to user info
606  * @param error error location
607  * @returns #FALSE if error is set
608  */
609 dbus_bool_t
610 _dbus_user_database_get_username  (DBusUserDatabase     *db,
611                                    const DBusString     *username,
612                                    const DBusUserInfo  **info,
613                                    DBusError            *error)
614 {
615   *info = _dbus_user_database_lookup (db, DBUS_UID_UNSET, username, error);
616   return *info != NULL;
617 }
618
619 /** @} */
620
621 /* Tests in dbus-userdb-util.c */