Main Page | Modules | Data Structures | Directories | File List | Data Fields | Related Pages

dbus-userdb-util.c

00001 /* -*- mode: C; c-file-style: "gnu" -*- */
00002 /* dbus-userdb-util.c Would be in dbus-userdb.c, but not used in libdbus
00003  * 
00004  * Copyright (C) 2003, 2004, 2005  Red Hat, Inc.
00005  *
00006  * Licensed under the Academic Free License version 2.1
00007  * 
00008  * This program is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  * 
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  */
00023 #define DBUS_USERDB_INCLUDES_PRIVATE 1
00024 #include "dbus-userdb.h"
00025 #include "dbus-test.h"
00026 #include "dbus-internals.h"
00027 #include "dbus-protocol.h"
00028 #include <string.h>
00029 
00042 dbus_bool_t
00043 _dbus_is_console_user (dbus_uid_t uid,
00044                        DBusError *error)
00045 {
00046 
00047   DBusUserDatabase *db;
00048   const DBusUserInfo *info;
00049   dbus_bool_t result = FALSE; 
00050 
00051   _dbus_user_database_lock_system ();
00052 
00053   db = _dbus_user_database_get_system ();
00054   if (db == NULL)
00055     {
00056       dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get system database.");
00057       _dbus_user_database_unlock_system ();
00058       return FALSE;
00059     }
00060 
00061   info = _dbus_user_database_lookup (db, uid, NULL, error);
00062 
00063   if (info == NULL)
00064     {
00065       _dbus_user_database_unlock_system ();
00066        return FALSE;
00067     }
00068 
00069   result = _dbus_user_at_console (info->username, error);
00070 
00071   _dbus_user_database_unlock_system ();
00072 
00073   return result;
00074 }
00075 
00076 
00084 dbus_bool_t
00085 _dbus_credentials_from_uid (dbus_uid_t        uid,
00086                             DBusCredentials  *credentials)
00087 {
00088   DBusUserDatabase *db;
00089   const DBusUserInfo *info;
00090   _dbus_user_database_lock_system ();
00091 
00092   db = _dbus_user_database_get_system ();
00093   if (db == NULL)
00094     {
00095       _dbus_user_database_unlock_system ();
00096       return FALSE;
00097     }
00098 
00099   if (!_dbus_user_database_get_uid (db, uid,
00100                                     &info, NULL))
00101     {
00102       _dbus_user_database_unlock_system ();
00103       return FALSE;
00104     }
00105 
00106   _dbus_assert (info->uid == uid);
00107   
00108   credentials->pid = DBUS_PID_UNSET;
00109   credentials->uid = info->uid;
00110   credentials->gid = info->primary_gid;
00111   
00112   _dbus_user_database_unlock_system ();
00113   return TRUE;
00114 }
00115 
00116 
00124 dbus_bool_t
00125 _dbus_get_user_id (const DBusString  *username,
00126                    dbus_uid_t        *uid)
00127 {
00128   DBusCredentials creds;
00129 
00130   if (!_dbus_credentials_from_username (username, &creds))
00131     return FALSE;
00132 
00133   if (creds.uid == DBUS_UID_UNSET)
00134     return FALSE;
00135 
00136   *uid = creds.uid;
00137 
00138   return TRUE;
00139 }
00140 
00148 dbus_bool_t
00149 _dbus_get_group_id (const DBusString  *groupname,
00150                     dbus_gid_t        *gid)
00151 {
00152   DBusUserDatabase *db;
00153   const DBusGroupInfo *info;
00154   _dbus_user_database_lock_system ();
00155 
00156   db = _dbus_user_database_get_system ();
00157   if (db == NULL)
00158     {
00159       _dbus_user_database_unlock_system ();
00160       return FALSE;
00161     }
00162 
00163   if (!_dbus_user_database_get_groupname (db, groupname,
00164                                           &info, NULL))
00165     {
00166       _dbus_user_database_unlock_system ();
00167       return FALSE;
00168     }
00169 
00170   *gid = info->gid;
00171   
00172   _dbus_user_database_unlock_system ();
00173   return TRUE;
00174 }
00175 
00188 DBusGroupInfo*
00189 _dbus_user_database_lookup_group (DBusUserDatabase *db,
00190                                   dbus_gid_t        gid,
00191                                   const DBusString *groupname,
00192                                   DBusError        *error)
00193 {
00194   DBusGroupInfo *info;
00195 
00196   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00197 
00198    /* See if the group is really a number */
00199    if (gid == DBUS_UID_UNSET)
00200     {
00201       unsigned long n;
00202 
00203       if (_dbus_is_a_number (groupname, &n))
00204         gid = n;
00205     }
00206 
00207 
00208   if (gid != DBUS_GID_UNSET)
00209     info = _dbus_hash_table_lookup_ulong (db->groups, gid);
00210   else
00211     info = _dbus_hash_table_lookup_string (db->groups_by_name,
00212                                            _dbus_string_get_const_data (groupname));
00213   if (info)
00214     {
00215       _dbus_verbose ("Using cache for GID "DBUS_GID_FORMAT" information\n",
00216                      info->gid);
00217       return info;
00218     }
00219   else
00220     {
00221       if (gid != DBUS_GID_UNSET)
00222         _dbus_verbose ("No cache for GID "DBUS_GID_FORMAT"\n",
00223                        gid);
00224       else
00225         _dbus_verbose ("No cache for groupname \"%s\"\n",
00226                        _dbus_string_get_const_data (groupname));
00227       
00228       info = dbus_new0 (DBusGroupInfo, 1);
00229       if (info == NULL)
00230         {
00231           dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
00232           return NULL;
00233         }
00234 
00235       if (gid != DBUS_GID_UNSET)
00236         {
00237           if (!_dbus_group_info_fill_gid (info, gid, error))
00238             {
00239               _DBUS_ASSERT_ERROR_IS_SET (error);
00240               _dbus_group_info_free_allocated (info);
00241               return NULL;
00242             }
00243         }
00244       else
00245         {
00246           if (!_dbus_group_info_fill (info, groupname, error))
00247             {
00248               _DBUS_ASSERT_ERROR_IS_SET (error);
00249               _dbus_group_info_free_allocated (info);
00250               return NULL;
00251             }
00252         }
00253 
00254       /* don't use these past here */
00255       gid = DBUS_GID_UNSET;
00256       groupname = NULL;
00257 
00258       if (!_dbus_hash_table_insert_ulong (db->groups, info->gid, info))
00259         {
00260           dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
00261           _dbus_group_info_free_allocated (info);
00262           return NULL;
00263         }
00264 
00265 
00266       if (!_dbus_hash_table_insert_string (db->groups_by_name,
00267                                            info->groupname,
00268                                            info))
00269         {
00270           _dbus_hash_table_remove_ulong (db->groups, info->gid);
00271           dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
00272           return NULL;
00273         }
00274       
00275       return info;
00276     }
00277 }
00278 
00279 
00290 dbus_bool_t
00291 _dbus_user_database_get_groupname (DBusUserDatabase     *db,
00292                                    const DBusString     *groupname,
00293                                    const DBusGroupInfo **info,
00294                                    DBusError            *error)
00295 {
00296   *info = _dbus_user_database_lookup_group (db, DBUS_GID_UNSET, groupname, error);
00297   return *info != NULL;
00298 }
00299 
00310 dbus_bool_t
00311 _dbus_user_database_get_gid (DBusUserDatabase     *db,
00312                              dbus_gid_t            gid,
00313                              const DBusGroupInfo **info,
00314                              DBusError            *error)
00315 {
00316   *info = _dbus_user_database_lookup_group (db, gid, NULL, error);
00317   return *info != NULL;
00318 }
00319 
00320 
00334 dbus_bool_t
00335 _dbus_user_database_get_groups (DBusUserDatabase  *db,
00336                                 dbus_uid_t         uid,
00337                                 dbus_gid_t       **group_ids,
00338                                 int               *n_group_ids,
00339                                 DBusError         *error)
00340 {
00341   DBusUserInfo *info;
00342   
00343   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00344 
00345   *group_ids = NULL;
00346   *n_group_ids = 0;
00347   
00348   info = _dbus_user_database_lookup (db, uid, NULL, error);
00349   if (info == NULL)
00350     {
00351       _DBUS_ASSERT_ERROR_IS_SET (error);
00352       return FALSE;
00353     }
00354 
00355   if (info->n_group_ids > 0)
00356     {
00357       *group_ids = dbus_new (dbus_gid_t, info->n_group_ids);
00358       if (*group_ids == NULL)
00359         {
00360           dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
00361           return FALSE;
00362         }
00363 
00364       *n_group_ids = info->n_group_ids;
00365 
00366       memcpy (*group_ids, info->group_ids, info->n_group_ids * sizeof (dbus_gid_t));
00367     }
00368 
00369   return TRUE;
00370 }
00371 
00374 #ifdef DBUS_BUILD_TESTS
00375 #include <stdio.h>
00376 
00382 dbus_bool_t
00383 _dbus_userdb_test (const char *test_data_dir)
00384 {
00385   const DBusString *username;
00386   const DBusString *homedir;
00387 
00388   if (!_dbus_username_from_current_process (&username))
00389     _dbus_assert_not_reached ("didn't get username");
00390 
00391   if (!_dbus_homedir_from_current_process (&homedir))
00392     _dbus_assert_not_reached ("didn't get homedir");  
00393 
00394   printf ("    Current user: %s homedir: %s\n",
00395           _dbus_string_get_const_data (username),
00396           _dbus_string_get_const_data (homedir));
00397   
00398   return TRUE;
00399 }
00400 #endif /* DBUS_BUILD_TESTS */

Generated on Tue Aug 30 16:35:52 2005 for D-BUS by  doxygen 1.4.3