LCOV - code coverage report
Current view: top level - responder/pam - pam_helpers.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 35 50 70.0 %
Date: 2016-06-29 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :     SSSD
       3             : 
       4             :     Authors:
       5             :         Stephen Gallagher <sgallagh@redhat.com>
       6             : 
       7             :     Copyright (C) 2011 Red Hat
       8             : 
       9             :     This program is free software; you can redistribute it and/or modify
      10             :     it under the terms of the GNU General Public License as published by
      11             :     the Free Software Foundation; either version 3 of the License, or
      12             :     (at your option) any later version.
      13             : 
      14             :     This program is distributed in the hope that it will be useful,
      15             :     but WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17             :     GNU General Public License for more details.
      18             : 
      19             :     You should have received a copy of the GNU General Public License
      20             :     along with this program.  If not, see <http://www.gnu.org/licenses/>.
      21             : */
      22             : 
      23             : 
      24             : #include "src/responder/pam/pam_helpers.h"
      25             : 
      26             : struct pam_initgr_table_ctx {
      27             :     hash_table_t *id_table;
      28             :     char *name;
      29             : };
      30             : 
      31             : static void pam_initgr_cache_remove(struct tevent_context *ev,
      32             :                                     struct tevent_timer *te,
      33             :                                     struct timeval tv,
      34             :                                     void *pvt);
      35             : 
      36          76 : errno_t pam_initgr_cache_set(struct tevent_context *ev,
      37             :                              hash_table_t *id_table,
      38             :                              char *name,
      39             :                              long timeout)
      40             : {
      41             :     errno_t ret;
      42             :     hash_key_t key;
      43             :     hash_value_t val;
      44             :     int hret;
      45             :     struct tevent_timer *te;
      46             :     struct timeval tv;
      47             :     struct pam_initgr_table_ctx *table_ctx;
      48             : 
      49          76 :     table_ctx = talloc_zero(id_table, struct pam_initgr_table_ctx);
      50          76 :     if (!table_ctx) return ENOMEM;
      51             : 
      52          76 :     table_ctx->id_table = id_table;
      53          76 :     table_ctx->name = talloc_strdup(table_ctx, name);
      54          76 :     if (!table_ctx->name) {
      55           0 :         ret = ENOMEM;
      56           0 :         goto done;
      57             :     }
      58             : 
      59          76 :     key.type = HASH_KEY_STRING;
      60          76 :     key.str = name;
      61             : 
      62             :     /* The value isn't relevant, since we're using
      63             :      * a timer to remove the entry.
      64             :      */
      65          76 :     val.type = HASH_VALUE_UNDEF;
      66             : 
      67          76 :     hret = hash_enter(id_table, &key, &val);
      68          76 :     if (hret != HASH_SUCCESS) {
      69           0 :         DEBUG(SSSDBG_MINOR_FAILURE,
      70             :               "Could not update initgr cache for [%s]: [%s]\n",
      71             :                name, hash_error_string(hret));
      72           0 :         ret = EIO;
      73           0 :         goto done;
      74             :     } else {
      75          76 :         DEBUG(SSSDBG_TRACE_INTERNAL,
      76             :               "[%s] added to PAM initgroup cache\n",
      77             :                name);
      78             :     }
      79             : 
      80             :     /* Create a timer event to remove the entry from the cache */
      81          76 :     tv = tevent_timeval_current_ofs(timeout, 0);
      82          76 :     te = tevent_add_timer(ev, table_ctx, tv,
      83             :                           pam_initgr_cache_remove,
      84             :                           table_ctx);
      85          76 :     if (!te) {
      86           0 :         ret = ENOMEM;
      87           0 :         goto done;
      88             :     }
      89             : 
      90          76 :     ret = EOK;
      91             : 
      92             : done:
      93          76 :     if (ret != EOK) {
      94           0 :         talloc_free(table_ctx);
      95             :     }
      96          76 :     return ret;
      97             : }
      98             : 
      99          18 : static void pam_initgr_cache_remove(struct tevent_context *ev,
     100             :                                     struct tevent_timer *te,
     101             :                                     struct timeval tv,
     102             :                                     void *pvt)
     103             : {
     104             :     int hret;
     105             :     hash_key_t key;
     106             : 
     107          18 :     struct pam_initgr_table_ctx *table_ctx =
     108             :             talloc_get_type(pvt, struct pam_initgr_table_ctx);
     109             : 
     110          18 :     key.type = HASH_KEY_STRING;
     111          18 :     key.str = table_ctx->name;
     112             : 
     113          18 :     hret = hash_delete(table_ctx->id_table, &key);
     114          18 :     if (hret != HASH_SUCCESS
     115           0 :             && hret != HASH_ERROR_KEY_NOT_FOUND) {
     116           0 :         DEBUG(SSSDBG_MINOR_FAILURE,
     117             :               "Could not clear [%s] from initgr cache: [%s]\n",
     118             :                table_ctx->name,
     119             :                hash_error_string(hret));
     120             :     } else {
     121          18 :         DEBUG(SSSDBG_TRACE_INTERNAL,
     122             :               "[%s] removed from PAM initgroup cache\n",
     123             :                table_ctx->name);
     124             :     }
     125             : 
     126          18 :     talloc_free(table_ctx);
     127          18 : }
     128             : 
     129          30 : errno_t pam_initgr_check_timeout(hash_table_t *id_table,
     130             :                                  char *name)
     131             : {
     132             :     hash_key_t key;
     133             :     hash_value_t val;
     134             :     int hret;
     135             : 
     136          30 :     key.type = HASH_KEY_STRING;
     137          30 :     key.str = name;
     138             : 
     139          30 :     hret = hash_lookup(id_table, &key, &val);
     140          30 :     if (hret != HASH_SUCCESS
     141           0 :             && hret != HASH_ERROR_KEY_NOT_FOUND) {
     142           0 :             DEBUG(SSSDBG_TRACE_ALL, "Error searching user [%s] in PAM cache.\n",
     143             :                                     name);
     144           0 :         return EIO;
     145          30 :     } else if (hret == HASH_ERROR_KEY_NOT_FOUND) {
     146           0 :         DEBUG(SSSDBG_TRACE_ALL, "User [%s] not found in PAM cache.\n", name);
     147           0 :         return ENOENT;
     148             :     }
     149             : 
     150             :     /* If there's a value here, then the cache
     151             :      * entry is still valid.
     152             :      */
     153          30 :     DEBUG(SSSDBG_TRACE_INTERNAL, "User [%s] found in PAM cache.\n", name);
     154          30 :     return EOK;
     155             : }
     156             : 

Generated by: LCOV version 1.10