[daemon-fix] Fixed sending daemon match rules for kdbus broadcasts
[platform/upstream/dbus.git] / dbus / dbus-userdb-util.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-userdb-util.c Would be in dbus-userdb.c, but not used in libdbus
3  * 
4  * Copyright (C) 2003, 2004, 2005  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 #include <unistd.h>
25 #define DBUS_USERDB_INCLUDES_PRIVATE 1
26 #include "dbus-userdb.h"
27 #include "dbus-test.h"
28 #include "dbus-internals.h"
29 #include "dbus-protocol.h"
30 #include <string.h>
31
32 #if HAVE_SYSTEMD
33 #include <systemd/sd-login.h>
34 #endif
35
36 /**
37  * @addtogroup DBusInternalsUtils
38  * @{
39  */
40
41 /**
42  * Checks to see if the UID sent in is the console user
43  *
44  * @param uid UID of person to check 
45  * @param error return location for errors
46  * @returns #TRUE if the UID is the same as the console user and there are no errors
47  */
48 dbus_bool_t
49 _dbus_is_console_user (dbus_uid_t uid,
50                        DBusError *error)
51 {
52
53   DBusUserDatabase *db;
54   const DBusUserInfo *info;
55   dbus_bool_t result = FALSE;
56
57 #ifdef HAVE_SYSTEMD
58   /* check if we have logind */
59   if (access ("/run/systemd/seats/", F_OK) >= 0)
60     {
61       int r;
62
63       /* Check whether this user is logged in on at least one physical
64          seat */
65       r = sd_uid_get_seats (uid, 0, NULL);
66       if (r < 0)
67         {
68           dbus_set_error (error, _dbus_error_from_errno (-r),
69                           "Failed to determine seats of user \"" DBUS_UID_FORMAT "\": %s",
70                           uid,
71                           _dbus_strerror (-r));
72           return FALSE;
73         }
74
75       return (r > 0);
76     }
77 #endif
78
79 #ifdef HAVE_CONSOLE_OWNER_FILE
80
81   DBusString f;
82   DBusStat st;
83
84   if (!_dbus_string_init (&f))
85     {
86       _DBUS_SET_OOM (error);
87       return FALSE;
88     }
89
90   if (!_dbus_string_append(&f, DBUS_CONSOLE_OWNER_FILE))
91     {
92       _dbus_string_free(&f);
93       _DBUS_SET_OOM (error);
94       return FALSE;
95     }
96
97   if (_dbus_stat(&f, &st, NULL) && (st.uid == uid))
98     {
99       _dbus_string_free(&f);
100       return TRUE;
101     }
102
103   _dbus_string_free(&f);
104
105 #endif /* HAVE_CONSOLE_OWNER_FILE */
106
107   if (!_dbus_user_database_lock_system ())
108     {
109       _DBUS_SET_OOM (error);
110       return FALSE;
111     }
112
113   db = _dbus_user_database_get_system ();
114   if (db == NULL)
115     {
116       dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get system database.");
117       _dbus_user_database_unlock_system ();
118       return FALSE;
119     }
120
121   /* TPTD: this should be cache-safe, we've locked the DB and
122     _dbus_user_at_console doesn't pass it on. */
123   info = _dbus_user_database_lookup (db, uid, NULL, error);
124
125   if (info == NULL)
126     {
127       _dbus_user_database_unlock_system ();
128        return FALSE;
129     }
130
131   result = _dbus_user_at_console (info->username, error);
132
133   _dbus_user_database_unlock_system ();
134
135   return result;
136 }
137
138 /**
139  * Gets user ID given username
140  *
141  * @param username the username
142  * @param uid return location for UID
143  * @returns #TRUE if username existed and we got the UID
144  */
145 dbus_bool_t
146 _dbus_get_user_id (const DBusString  *username,
147                    dbus_uid_t        *uid)
148 {
149   return _dbus_get_user_id_and_primary_group (username, uid, NULL);
150 }
151
152 /**
153  * Gets group ID given groupname
154  *
155  * @param groupname the groupname
156  * @param gid return location for GID
157  * @returns #TRUE if group name existed and we got the GID
158  */
159 dbus_bool_t
160 _dbus_get_group_id (const DBusString  *groupname,
161                     dbus_gid_t        *gid)
162 {
163   DBusUserDatabase *db;
164   const DBusGroupInfo *info;
165
166   /* FIXME: this can't distinguish ENOMEM from other errors */
167   if (!_dbus_user_database_lock_system ())
168     return FALSE;
169
170   db = _dbus_user_database_get_system ();
171   if (db == NULL)
172     {
173       _dbus_user_database_unlock_system ();
174       return FALSE;
175     }
176
177   if (!_dbus_user_database_get_groupname (db, groupname,
178                                           &info, NULL))
179     {
180       _dbus_user_database_unlock_system ();
181       return FALSE;
182     }
183
184   *gid = info->gid;
185   
186   _dbus_user_database_unlock_system ();
187   return TRUE;
188 }
189
190 /**
191  * Gets user ID and primary group given username
192  *
193  * @param username the username
194  * @param uid_p return location for UID
195  * @param gid_p return location for GID
196  * @returns #TRUE if username existed and we got the UID and GID
197  */
198 dbus_bool_t
199 _dbus_get_user_id_and_primary_group (const DBusString  *username,
200                                      dbus_uid_t        *uid_p,
201                                      dbus_gid_t        *gid_p)
202 {
203   DBusUserDatabase *db;
204   const DBusUserInfo *info;
205
206   /* FIXME: this can't distinguish ENOMEM from other errors */
207   if (!_dbus_user_database_lock_system ())
208     return FALSE;
209
210   db = _dbus_user_database_get_system ();
211   if (db == NULL)
212     {
213       _dbus_user_database_unlock_system ();
214       return FALSE;
215     }
216
217   if (!_dbus_user_database_get_username (db, username,
218                                          &info, NULL))
219     {
220       _dbus_user_database_unlock_system ();
221       return FALSE;
222     }
223
224   if (uid_p)
225     *uid_p = info->uid;
226   if (gid_p)
227     *gid_p = info->primary_gid;
228   
229   _dbus_user_database_unlock_system ();
230   return TRUE;
231 }
232
233 /**
234  * Looks up a gid or group name in the user database.  Only one of
235  * name or GID can be provided. There are wrapper functions for this
236  * that are better to use, this one does no locking or anything on the
237  * database and otherwise sort of sucks.
238  *
239  * @param db the database
240  * @param gid the group ID or #DBUS_GID_UNSET
241  * @param groupname group name or #NULL 
242  * @param error error to fill in
243  * @returns the entry in the database
244  */
245 DBusGroupInfo*
246 _dbus_user_database_lookup_group (DBusUserDatabase *db,
247                                   dbus_gid_t        gid,
248                                   const DBusString *groupname,
249                                   DBusError        *error)
250 {
251   DBusGroupInfo *info;
252
253   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
254
255    /* See if the group is really a number */
256    if (gid == DBUS_UID_UNSET)
257     {
258       unsigned long n;
259
260       if (_dbus_is_a_number (groupname, &n))
261         gid = n;
262     }
263
264 #ifdef DBUS_ENABLE_USERDB_CACHE
265   if (gid != DBUS_GID_UNSET)
266     info = _dbus_hash_table_lookup_uintptr (db->groups, gid);
267   else
268     info = _dbus_hash_table_lookup_string (db->groups_by_name,
269                                            _dbus_string_get_const_data (groupname));
270   if (info)
271     {
272       _dbus_verbose ("Using cache for GID "DBUS_GID_FORMAT" information\n",
273                      info->gid);
274       return info;
275     }
276   else
277 #else
278   if (1)
279 #endif
280     {
281       if (gid != DBUS_GID_UNSET)
282         _dbus_verbose ("No cache for GID "DBUS_GID_FORMAT"\n",
283                        gid);
284       else
285         _dbus_verbose ("No cache for groupname \"%s\"\n",
286                        _dbus_string_get_const_data (groupname));
287       
288       info = dbus_new0 (DBusGroupInfo, 1);
289       if (info == NULL)
290         {
291           dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
292           return NULL;
293         }
294
295       if (gid != DBUS_GID_UNSET)
296         {
297           if (!_dbus_group_info_fill_gid (info, gid, error))
298             {
299               _DBUS_ASSERT_ERROR_IS_SET (error);
300               _dbus_group_info_free_allocated (info);
301               return NULL;
302             }
303         }
304       else
305         {
306           if (!_dbus_group_info_fill (info, groupname, error))
307             {
308               _DBUS_ASSERT_ERROR_IS_SET (error);
309               _dbus_group_info_free_allocated (info);
310               return NULL;
311             }
312         }
313
314       /* don't use these past here */
315       gid = DBUS_GID_UNSET;
316       groupname = NULL;
317
318       if (!_dbus_hash_table_insert_uintptr (db->groups, info->gid, info))
319         {
320           dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
321           _dbus_group_info_free_allocated (info);
322           return NULL;
323         }
324
325
326       if (!_dbus_hash_table_insert_string (db->groups_by_name,
327                                            info->groupname,
328                                            info))
329         {
330           _dbus_hash_table_remove_uintptr (db->groups, info->gid);
331           dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
332           return NULL;
333         }
334       
335       return info;
336     }
337 }
338
339
340 /**
341  * Gets the user information for the given group name,
342  * returned group info should not be freed. 
343  *
344  * @param db user database
345  * @param groupname the group name
346  * @param info return location for const ref to group info
347  * @param error error location
348  * @returns #FALSE if error is set
349  */
350 dbus_bool_t
351 _dbus_user_database_get_groupname (DBusUserDatabase     *db,
352                                    const DBusString     *groupname,
353                                    const DBusGroupInfo **info,
354                                    DBusError            *error)
355 {
356   *info = _dbus_user_database_lookup_group (db, DBUS_GID_UNSET, groupname, error);
357   return *info != NULL;
358 }
359
360 /**
361  * Gets the user information for the given GID,
362  * returned group info should not be freed. 
363  *
364  * @param db user database
365  * @param gid the group ID
366  * @param info return location for const ref to group info
367  * @param error error location
368  * @returns #FALSE if error is set
369  */
370 dbus_bool_t
371 _dbus_user_database_get_gid (DBusUserDatabase     *db,
372                              dbus_gid_t            gid,
373                              const DBusGroupInfo **info,
374                              DBusError            *error)
375 {
376   *info = _dbus_user_database_lookup_group (db, gid, NULL, error);
377   return *info != NULL;
378 }
379
380
381 /**
382  * Gets all groups  corresponding to the given UID. Returns #FALSE
383  * if no memory, or user isn't known, but always initializes
384  * group_ids to a NULL array. 
385  *
386  * @param uid the UID
387  * @param group_ids return location for array of group IDs
388  * @param n_group_ids return location for length of returned array
389  * @returns #TRUE if the UID existed and we got some credentials
390  */
391 dbus_bool_t
392 _dbus_groups_from_uid (dbus_uid_t         uid,
393                        dbus_gid_t       **group_ids,
394                        int               *n_group_ids)
395 {
396   DBusUserDatabase *db;
397   const DBusUserInfo *info;
398   *group_ids = NULL;
399   *n_group_ids = 0;
400
401   /* FIXME: this can't distinguish ENOMEM from other errors */
402   if (!_dbus_user_database_lock_system ())
403     return FALSE;
404
405   db = _dbus_user_database_get_system ();
406   if (db == NULL)
407     {
408       _dbus_user_database_unlock_system ();
409       return FALSE;
410     }
411
412   if (!_dbus_user_database_get_uid (db, uid,
413                                     &info, NULL))
414     {
415       _dbus_user_database_unlock_system ();
416       return FALSE;
417     }
418
419   _dbus_assert (info->uid == uid);
420   
421   if (info->n_group_ids > 0)
422     {
423       *group_ids = dbus_new (dbus_gid_t, info->n_group_ids);
424       if (*group_ids == NULL)
425         {
426           _dbus_user_database_unlock_system ();
427           return FALSE;
428         }
429
430       *n_group_ids = info->n_group_ids;
431
432       memcpy (*group_ids, info->group_ids, info->n_group_ids * sizeof (dbus_gid_t));
433     }
434
435   _dbus_user_database_unlock_system ();
436   return TRUE;
437 }
438 /** @} */
439
440 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
441 #include <stdio.h>
442
443 /**
444  * Unit test for dbus-userdb.c.
445  * 
446  * @returns #TRUE on success.
447  */
448 dbus_bool_t
449 _dbus_userdb_test (const char *test_data_dir)
450 {
451   const DBusString *username;
452   const DBusString *homedir;
453   dbus_uid_t uid;
454   unsigned long *group_ids;
455   int n_group_ids, i;
456   DBusError error;
457
458   if (!_dbus_username_from_current_process (&username))
459     _dbus_assert_not_reached ("didn't get username");
460
461   if (!_dbus_homedir_from_current_process (&homedir))
462     _dbus_assert_not_reached ("didn't get homedir");  
463
464   if (!_dbus_get_user_id (username, &uid))
465     _dbus_assert_not_reached ("didn't get uid");
466
467   if (!_dbus_groups_from_uid (uid, &group_ids, &n_group_ids))
468     _dbus_assert_not_reached ("didn't get groups");
469
470   printf ("    Current user: %s homedir: %s gids:",
471           _dbus_string_get_const_data (username),
472           _dbus_string_get_const_data (homedir));
473
474   for (i=0; i<n_group_ids; i++)
475       printf(" %ld", group_ids[i]);
476
477   printf ("\n");
478
479   dbus_error_init (&error);
480   printf ("Is Console user: %i\n",
481           _dbus_is_console_user (uid, &error));
482   printf ("Invocation was OK: %s\n", error.message ? error.message : "yes");
483   dbus_error_free (&error);
484   printf ("Is Console user 4711: %i\n",
485           _dbus_is_console_user (4711, &error));
486   printf ("Invocation was OK: %s\n", error.message ? error.message : "yes");
487   dbus_error_free (&error);
488
489   dbus_free (group_ids);
490
491   return TRUE;
492 }
493 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */