Coverage report for dbus/dbus-userdb-util.c.gcov

        -:    0:Source:dbus-userdb-util.c
        -:    0:Graph:.libs/dbus-userdb-util.gcno
        -:    0:Data:.libs/dbus-userdb-util.gcda
        -:    0:Runs:10879
        -:    0:Programs:3
        -:    1:/* -*- mode: C; c-file-style: "gnu" -*- */
        -:    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., 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-test.h"
        -:   26:#include "dbus-internals.h"
        -:   27:#include "dbus-protocol.h"
        -:   28:#include <string.h>
        -:   29:
        -:   30:/**
        -:   31: * @addtogroup DBusInternalsUtils
        -:   32: * @{
        -:   33: */
        -:   34:
        -:   35:/**
        -:   36: * Checks to see if the UID sent in is the console user
        -:   37: *
        -:   38: * @param uid UID of person to check 
        -:   39: * @param error return location for errors
        -:   40: * @returns #TRUE if the UID is the same as the console user and there are no errors
        -:   41: */
        -:   42:dbus_bool_t
        -:   43:_dbus_is_console_user (dbus_uid_t uid,
        -:   44:		       DBusError *error)
function _dbus_is_console_user called 6117 returned 100% blocks executed 64%
     6117:   45:{
        -:   46:
        -:   47:  DBusUserDatabase *db;
        -:   48:  const DBusUserInfo *info;
     6117:   49:  dbus_bool_t result = FALSE; 
        -:   50:
     6117:   51:  _dbus_user_database_lock_system ();
call    0 returned 100%
        -:   52:
     6117:   53:  db = _dbus_user_database_get_system ();
call    0 returned 100%
     6117:   54:  if (db == NULL)
branch  0 taken 0% (fallthrough)
branch  1 taken 100%
        -:   55:    {
    #####:   56:      dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get system database.");
call    0 never executed
    #####:   57:      _dbus_user_database_unlock_system ();
call    0 never executed
    #####:   58:      return FALSE;
        -:   59:    }
        -:   60:
     6117:   61:  info = _dbus_user_database_lookup (db, uid, NULL, error);
call    0 returned 100%
        -:   62:
     6117:   63:  if (info == NULL)
branch  0 taken 0% (fallthrough)
branch  1 taken 100%
        -:   64:    {
    #####:   65:      _dbus_user_database_unlock_system ();
call    0 never executed
    #####:   66:       return FALSE;
        -:   67:    }
        -:   68:
     6117:   69:  result = _dbus_user_at_console (info->username, error);
call    0 returned 100%
        -:   70:
     6117:   71:  _dbus_user_database_unlock_system ();
call    0 returned 100%
        -:   72:
     6117:   73:  return result;
        -:   74:}
        -:   75:
        -:   76:
        -:   77:/**
        -:   78: * Gets the credentials corresponding to the given UID.
        -:   79: *
        -:   80: * @param uid the UID
        -:   81: * @param credentials credentials to fill in
        -:   82: * @returns #TRUE if the UID existed and we got some credentials
        -:   83: */
        -:   84:dbus_bool_t
        -:   85:_dbus_credentials_from_uid (dbus_uid_t        uid,
        -:   86:                            DBusCredentials  *credentials)
function _dbus_credentials_from_uid called 0 returned 0% blocks executed 0%
    #####:   87:{
        -:   88:  DBusUserDatabase *db;
        -:   89:  const DBusUserInfo *info;
    #####:   90:  _dbus_user_database_lock_system ();
call    0 never executed
        -:   91:
    #####:   92:  db = _dbus_user_database_get_system ();
call    0 never executed
    #####:   93:  if (db == NULL)
branch  0 never executed
branch  1 never executed
        -:   94:    {
    #####:   95:      _dbus_user_database_unlock_system ();
call    0 never executed
    #####:   96:      return FALSE;
        -:   97:    }
        -:   98:
    #####:   99:  if (!_dbus_user_database_get_uid (db, uid,
call    0 never executed
branch  1 never executed
branch  2 never executed
        -:  100:                                    &info, NULL))
        -:  101:    {
    #####:  102:      _dbus_user_database_unlock_system ();
call    0 never executed
    #####:  103:      return FALSE;
        -:  104:    }
        -:  105:
    #####:  106:  _dbus_assert (info->uid == uid);
call    0 never executed
        -:  107:  
    #####:  108:  credentials->pid = DBUS_PID_UNSET;
    #####:  109:  credentials->uid = info->uid;
    #####:  110:  credentials->gid = info->primary_gid;
        -:  111:  
    #####:  112:  _dbus_user_database_unlock_system ();
call    0 never executed
    #####:  113:  return TRUE;
        -:  114:}
        -:  115:
        -:  116:
        -:  117:/**
        -:  118: * Gets user ID given username
        -:  119: *
        -:  120: * @param username the username
        -:  121: * @param uid return location for UID
        -:  122: * @returns #TRUE if username existed and we got the UID
        -:  123: */
        -:  124:dbus_bool_t
        -:  125:_dbus_get_user_id (const DBusString  *username,
        -:  126:                   dbus_uid_t        *uid)
function _dbus_get_user_id called 5 returned 100% blocks executed 71%
        5:  127:{
        -:  128:  DBusCredentials creds;
        -:  129:
        5:  130:  if (!_dbus_credentials_from_username (username, &creds))
call    0 returned 100%
branch  1 taken 0% (fallthrough)
branch  2 taken 100%
    #####:  131:    return FALSE;
        -:  132:
        5:  133:  if (creds.uid == DBUS_UID_UNSET)
branch  0 taken 0% (fallthrough)
branch  1 taken 100%
    #####:  134:    return FALSE;
        -:  135:
        5:  136:  *uid = creds.uid;
        -:  137:
        5:  138:  return TRUE;
        -:  139:}
        -:  140:
        -:  141:/**
        -:  142: * Gets group ID given groupname
        -:  143: *
        -:  144: * @param groupname the groupname
        -:  145: * @param gid return location for GID
        -:  146: * @returns #TRUE if group name existed and we got the GID
        -:  147: */
        -:  148:dbus_bool_t
        -:  149:_dbus_get_group_id (const DBusString  *groupname,
        -:  150:                    dbus_gid_t        *gid)
function _dbus_get_group_id called 0 returned 0% blocks executed 0%
    #####:  151:{
        -:  152:  DBusUserDatabase *db;
        -:  153:  const DBusGroupInfo *info;
    #####:  154:  _dbus_user_database_lock_system ();
call    0 never executed
        -:  155:
    #####:  156:  db = _dbus_user_database_get_system ();
call    0 never executed
    #####:  157:  if (db == NULL)
branch  0 never executed
branch  1 never executed
        -:  158:    {
    #####:  159:      _dbus_user_database_unlock_system ();
call    0 never executed
    #####:  160:      return FALSE;
        -:  161:    }
        -:  162:
    #####:  163:  if (!_dbus_user_database_get_groupname (db, groupname,
call    0 never executed
branch  1 never executed
branch  2 never executed
        -:  164:                                          &info, NULL))
        -:  165:    {
    #####:  166:      _dbus_user_database_unlock_system ();
call    0 never executed
    #####:  167:      return FALSE;
        -:  168:    }
        -:  169:
    #####:  170:  *gid = info->gid;
        -:  171:  
    #####:  172:  _dbus_user_database_unlock_system ();
call    0 never executed
    #####:  173:  return TRUE;
        -:  174:}
        -:  175:
        -:  176:/**
        -:  177: * Looks up a gid or group name in the user database.  Only one of
        -:  178: * name or GID can be provided. There are wrapper functions for this
        -:  179: * that are better to use, this one does no locking or anything on the
        -:  180: * database and otherwise sort of sucks.
        -:  181: *
        -:  182: * @param db the database
        -:  183: * @param gid the group ID or #DBUS_GID_UNSET
        -:  184: * @param groupname group name or #NULL 
        -:  185: * @param error error to fill in
        -:  186: * @returns the entry in the database
        -:  187: */
        -:  188:DBusGroupInfo*
        -:  189:_dbus_user_database_lookup_group (DBusUserDatabase *db,
        -:  190:                                  dbus_gid_t        gid,
        -:  191:                                  const DBusString *groupname,
        -:  192:                                  DBusError        *error)
function _dbus_user_database_lookup_group called 0 returned 0% blocks executed 0%
    #####:  193:{
        -:  194:  DBusGroupInfo *info;
        -:  195:
    #####:  196:  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
branch  0 never executed
branch  1 never executed
call    2 never executed
branch  3 never executed
branch  4 never executed
call    5 never executed
        -:  197:
        -:  198:   /* See if the group is really a number */
    #####:  199:   if (gid == DBUS_UID_UNSET)
branch  0 never executed
branch  1 never executed
        -:  200:    {
        -:  201:      unsigned long n;
        -:  202:
    #####:  203:      if (_dbus_is_a_number (groupname, &n))
call    0 never executed
branch  1 never executed
branch  2 never executed
    #####:  204:        gid = n;
        -:  205:    }
        -:  206:
        -:  207:
    #####:  208:  if (gid != DBUS_GID_UNSET)
branch  0 never executed
branch  1 never executed
    #####:  209:    info = _dbus_hash_table_lookup_ulong (db->groups, gid);
call    0 never executed
        -:  210:  else
    #####:  211:    info = _dbus_hash_table_lookup_string (db->groups_by_name,
call    0 never executed
call    1 never executed
        -:  212:                                           _dbus_string_get_const_data (groupname));
    #####:  213:  if (info)
branch  0 never executed
branch  1 never executed
        -:  214:    {
    #####:  215:      _dbus_verbose ("Using cache for GID "DBUS_GID_FORMAT" information\n",
call    0 never executed
        -:  216:                     info->gid);
    #####:  217:      return info;
        -:  218:    }
        -:  219:  else
        -:  220:    {
    #####:  221:      if (gid != DBUS_GID_UNSET)
branch  0 never executed
branch  1 never executed
    #####:  222:	_dbus_verbose ("No cache for GID "DBUS_GID_FORMAT"\n",
call    0 never executed
        -:  223:		       gid);
        -:  224:      else
    #####:  225:	_dbus_verbose ("No cache for groupname \"%s\"\n",
call    0 never executed
call    1 never executed
        -:  226:		       _dbus_string_get_const_data (groupname));
        -:  227:      
    #####:  228:      info = dbus_new0 (DBusGroupInfo, 1);
call    0 never executed
    #####:  229:      if (info == NULL)
branch  0 never executed
branch  1 never executed
        -:  230:        {
    #####:  231:          dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
call    0 never executed
    #####:  232:          return NULL;
        -:  233:        }
        -:  234:
    #####:  235:      if (gid != DBUS_GID_UNSET)
branch  0 never executed
branch  1 never executed
        -:  236:        {
    #####:  237:          if (!_dbus_group_info_fill_gid (info, gid, error))
call    0 never executed
branch  1 never executed
branch  2 never executed
        -:  238:            {
    #####:  239:              _DBUS_ASSERT_ERROR_IS_SET (error);
branch  0 never executed
branch  1 never executed
call    2 never executed
branch  3 never executed
branch  4 never executed
call    5 never executed
    #####:  240:              _dbus_group_info_free_allocated (info);
call    0 never executed
    #####:  241:              return NULL;
        -:  242:            }
        -:  243:        }
        -:  244:      else
        -:  245:        {
    #####:  246:          if (!_dbus_group_info_fill (info, groupname, error))
call    0 never executed
branch  1 never executed
branch  2 never executed
        -:  247:            {
    #####:  248:              _DBUS_ASSERT_ERROR_IS_SET (error);
branch  0 never executed
branch  1 never executed
call    2 never executed
branch  3 never executed
branch  4 never executed
call    5 never executed
    #####:  249:              _dbus_group_info_free_allocated (info);
call    0 never executed
    #####:  250:              return NULL;
        -:  251:            }
        -:  252:        }
        -:  253:
        -:  254:      /* don't use these past here */
    #####:  255:      gid = DBUS_GID_UNSET;
    #####:  256:      groupname = NULL;
        -:  257:
    #####:  258:      if (!_dbus_hash_table_insert_ulong (db->groups, info->gid, info))
call    0 never executed
branch  1 never executed
branch  2 never executed
        -:  259:        {
    #####:  260:          dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
call    0 never executed
    #####:  261:          _dbus_group_info_free_allocated (info);
call    0 never executed
    #####:  262:          return NULL;
        -:  263:        }
        -:  264:
        -:  265:
    #####:  266:      if (!_dbus_hash_table_insert_string (db->groups_by_name,
call    0 never executed
branch  1 never executed
branch  2 never executed
        -:  267:                                           info->groupname,
        -:  268:                                           info))
        -:  269:        {
    #####:  270:          _dbus_hash_table_remove_ulong (db->groups, info->gid);
call    0 never executed
    #####:  271:          dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
call    0 never executed
    #####:  272:          return NULL;
        -:  273:        }
        -:  274:      
    #####:  275:      return info;
        -:  276:    }
        -:  277:}
        -:  278:
        -:  279:
        -:  280:/**
        -:  281: * Gets the user information for the given group name,
        -:  282: * returned group info should not be freed. 
        -:  283: *
        -:  284: * @param db user database
        -:  285: * @param groupname the group name
        -:  286: * @param info return location for const ref to group info
        -:  287: * @param error error location
        -:  288: * @returns #FALSE if error is set
        -:  289: */
        -:  290:dbus_bool_t
        -:  291:_dbus_user_database_get_groupname (DBusUserDatabase     *db,
        -:  292:                                   const DBusString     *groupname,
        -:  293:                                   const DBusGroupInfo **info,
        -:  294:                                   DBusError            *error)
function _dbus_user_database_get_groupname called 0 returned 0% blocks executed 0%
    #####:  295:{
    #####:  296:  *info = _dbus_user_database_lookup_group (db, DBUS_GID_UNSET, groupname, error);
call    0 never executed
    #####:  297:  return *info != NULL;
        -:  298:}
        -:  299:
        -:  300:/**
        -:  301: * Gets the user information for the given GID,
        -:  302: * returned group info should not be freed. 
        -:  303: *
        -:  304: * @param db user database
        -:  305: * @param gid the group ID
        -:  306: * @param info return location for const ref to group info
        -:  307: * @param error error location
        -:  308: * @returns #FALSE if error is set
        -:  309: */
        -:  310:dbus_bool_t
        -:  311:_dbus_user_database_get_gid (DBusUserDatabase     *db,
        -:  312:                             dbus_gid_t            gid,
        -:  313:                             const DBusGroupInfo **info,
        -:  314:                             DBusError            *error)
function _dbus_user_database_get_gid called 0 returned 0% blocks executed 0%
    #####:  315:{
    #####:  316:  *info = _dbus_user_database_lookup_group (db, gid, NULL, error);
call    0 never executed
    #####:  317:  return *info != NULL;
        -:  318:}
        -:  319:
        -:  320:
        -:  321:/**
        -:  322: * Gets all groups for a particular user. Returns #FALSE
        -:  323: * if no memory, or user isn't known, but always initializes
        -:  324: * group_ids to a NULL array. Sets error to the reason
        -:  325: * for returning #FALSE.
        -:  326: *
        -:  327: * @param db the user database object
        -:  328: * @param uid the user ID
        -:  329: * @param group_ids return location for array of group IDs
        -:  330: * @param n_group_ids return location for length of returned array
        -:  331: * @param error return location for error
        -:  332: * @returns #TRUE on success
        -:  333: */
        -:  334:dbus_bool_t
        -:  335:_dbus_user_database_get_groups (DBusUserDatabase  *db,
        -:  336:                                dbus_uid_t         uid,
        -:  337:                                dbus_gid_t       **group_ids,
        -:  338:                                int               *n_group_ids,
        -:  339:                                DBusError         *error)
function _dbus_user_database_get_groups called 6210 returned 100% blocks executed 57%
     6210:  340:{
        -:  341:  DBusUserInfo *info;
        -:  342:  
     6210:  343:  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
branch  0 taken 0% (fallthrough)
branch  1 taken 100%
call    2 never executed
branch  3 never executed
branch  4 never executed
call    5 returned 100%
        -:  344:
     6210:  345:  *group_ids = NULL;
     6210:  346:  *n_group_ids = 0;
        -:  347:  
     6210:  348:  info = _dbus_user_database_lookup (db, uid, NULL, error);
call    0 returned 100%
     6210:  349:  if (info == NULL)
branch  0 taken 0% (fallthrough)
branch  1 taken 100%
        -:  350:    {
    #####:  351:      _DBUS_ASSERT_ERROR_IS_SET (error);
branch  0 never executed
branch  1 never executed
call    2 never executed
branch  3 never executed
branch  4 never executed
call    5 never executed
    #####:  352:      return FALSE;
        -:  353:    }
        -:  354:
     6210:  355:  if (info->n_group_ids > 0)
branch  0 taken 100% (fallthrough)
branch  1 taken 0%
        -:  356:    {
     6210:  357:      *group_ids = dbus_new (dbus_gid_t, info->n_group_ids);
call    0 returned 100%
     6210:  358:      if (*group_ids == NULL)
branch  0 taken 1% (fallthrough)
branch  1 taken 99%
        -:  359:        {
        7:  360:          dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
call    0 returned 100%
        7:  361:          return FALSE;
        -:  362:        }
        -:  363:
     6203:  364:      *n_group_ids = info->n_group_ids;
        -:  365:
     6203:  366:      memcpy (*group_ids, info->group_ids, info->n_group_ids * sizeof (dbus_gid_t));
call    0 returned 100%
        -:  367:    }
        -:  368:
     6203:  369:  return TRUE;
        -:  370:}
        -:  371:
        -:  372:/** @} */
        -:  373:
        -:  374:#ifdef DBUS_BUILD_TESTS
        -:  375:#include <stdio.h>
        -:  376:
        -:  377:/**
        -:  378: * Unit test for dbus-userdb.c.
        -:  379: * 
        -:  380: * @returns #TRUE on success.
        -:  381: */
        -:  382:dbus_bool_t
        -:  383:_dbus_userdb_test (const char *test_data_dir)
function _dbus_userdb_test called 1 returned 100% blocks executed 80%
        1:  384:{
        -:  385:  const DBusString *username;
        -:  386:  const DBusString *homedir;
        -:  387:
        1:  388:  if (!_dbus_username_from_current_process (&username))
call    0 returned 100%
branch  1 taken 0% (fallthrough)
branch  2 taken 100%
    #####:  389:    _dbus_assert_not_reached ("didn't get username");
call    0 never executed
        -:  390:
        1:  391:  if (!_dbus_homedir_from_current_process (&homedir))
call    0 returned 100%
branch  1 taken 0% (fallthrough)
branch  2 taken 100%
    #####:  392:    _dbus_assert_not_reached ("didn't get homedir");  
call    0 never executed
        -:  393:
        1:  394:  printf ("    Current user: %s homedir: %s\n",
call    0 returned 100%
call    1 returned 100%
call    2 returned 100%
        -:  395:          _dbus_string_get_const_data (username),
        -:  396:          _dbus_string_get_const_data (homedir));
        -:  397:  
        1:  398:  return TRUE;
        -:  399:}
        -:  400:#endif /* DBUS_BUILD_TESTS */