summaryrefslogtreecommitdiffstats
path: root/src/responder/pam/pam_helpers.c
blob: d2068e57c4e6fa6a3e422e000d3fa2fbf8c57660 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
    SSSD

    Authors:
        Stephen Gallagher <sgallagh@redhat.com>

    Copyright (C) 2011 Red Hat

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


#include "util/util.h"
#include "src/responder/pam/pam_helpers.h"

struct pam_initgr_table_ctx {
    hash_table_t *id_table;
    char *name;
};

static void pam_initgr_cache_remove(struct tevent_context *ev,
                                    struct tevent_timer *te,
                                    struct timeval tv,
                                    void *pvt);

errno_t pam_initgr_cache_set(struct tevent_context *ev,
                             hash_table_t *id_table,
                             char *name,
                             long timeout)
{
    errno_t ret;
    hash_key_t key;
    hash_value_t val;
    int hret;
    struct tevent_timer *te;
    struct timeval tv;
    struct pam_initgr_table_ctx *table_ctx;

    table_ctx = talloc_zero(id_table, struct pam_initgr_table_ctx);
    if (!table_ctx) return ENOMEM;

    table_ctx->id_table = id_table;
    table_ctx->name = talloc_strdup(table_ctx, name);
    if (!table_ctx->name) {
        ret = ENOMEM;
        goto done;
    }

    key.type = HASH_KEY_STRING;
    key.str = name;

    /* The value isn't relevant, since we're using
     * a timer to remove the entry.
     */
    val.type = HASH_VALUE_UNDEF;

    hret = hash_enter(id_table, &key, &val);
    if (hret != HASH_SUCCESS) {
        DEBUG(SSSDBG_MINOR_FAILURE,
              ("Could not update initgr cache for [%s]: [%s]\n",
               name, hash_error_string(hret)));
        ret = EIO;
        goto done;
    } else {
        DEBUG(SSSDBG_TRACE_INTERNAL,
              ("[%s] added to PAM initgroup cache\n",
               name));
    }

    /* Create a timer event to remove the entry from the cache */
    tv = tevent_timeval_current_ofs(timeout, 0);
    te = tevent_add_timer(ev, table_ctx, tv,
                          pam_initgr_cache_remove,
                          table_ctx);
    if (!te) {
        ret = ENOMEM;
        goto done;
    }

    ret = EOK;

done:
    if (ret != EOK) {
        talloc_free(table_ctx);
    }
    return ret;
}

static void pam_initgr_cache_remove(struct tevent_context *ev,
                                    struct tevent_timer *te,
                                    struct timeval tv,
                                    void *pvt)
{
    int hret;
    hash_key_t key;

    struct pam_initgr_table_ctx *table_ctx =
            talloc_get_type(pvt, struct pam_initgr_table_ctx);

    key.type = HASH_KEY_STRING;
    key.str = table_ctx->name;

    hret = hash_delete(table_ctx->id_table, &key);
    if (hret != HASH_SUCCESS
            && hret != HASH_ERROR_KEY_NOT_FOUND) {
        DEBUG(SSSDBG_MINOR_FAILURE,
              ("Could not clear [%s] from initgr cache: [%s]\n",
               table_ctx->name,
               hash_error_string(hret)));
    } else {
        DEBUG(SSSDBG_TRACE_INTERNAL,
              ("[%s] removed from PAM initgroup cache\n",
               table_ctx->name));
    }

    talloc_free(table_ctx);
}

errno_t pam_initgr_check_timeout(hash_table_t *id_table,
                                 char *name)
{
    hash_key_t key;
    hash_value_t val;
    int hret;

    key.type = HASH_KEY_STRING;
    key.str = name;

    hret = hash_lookup(id_table, &key, &val);
    if (hret != HASH_SUCCESS
            && hret != HASH_ERROR_KEY_NOT_FOUND) {
        return EIO;
    } else if (hret == HASH_ERROR_KEY_NOT_FOUND) {
        return ENOENT;
    }

    /* If there's a value here, then the cache
     * entry is still valid.
     */
    return EOK;
}