summaryrefslogtreecommitdiffstats
path: root/src/sss_client/nss_mc_sid.c
blob: 52e684da52574aea9163557b981024c383e19dc0 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 * System Security Services Daemon. NSS client interface
 *
 * Copyright (C) 2022 Red Hat
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/* SID database NSS interface using mmap cache */

#include <stddef.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

#include "nss_mc.h"
#include "util/mmap_cache.h"
#include "idmap/sss_nss_idmap.h"

#if HAVE_PTHREAD
static pthread_mutex_t sid_mc_ctx_mutex = PTHREAD_MUTEX_INITIALIZER;
static struct sss_cli_mc_ctx sid_mc_ctx = SSS_CLI_MC_CTX_INITIALIZER(&sid_mc_ctx_mutex);
#else
static struct sss_cli_mc_ctx sid_mc_ctx = SSS_CLI_MC_CTX_INITIALIZER;
#endif

static errno_t mc_get_sid_by_typed_id(uint32_t id, enum sss_id_type object_type,
                                      char **sid, uint32_t *type,
                                      uint32_t *populated_by)
{
    int ret;
    char key[16];
    int key_len;
    uint32_t hash;
    uint32_t slot;
    struct sss_mc_rec *rec = NULL;
    const struct sss_mc_sid_data *data = NULL;

    key_len = snprintf(key, sizeof(key), "%d-%ld", object_type, (long)id);
    if (key_len > (sizeof(key) - 1)) {
        return EINVAL;
    }

    ret = sss_nss_mc_get_ctx("sid", &sid_mc_ctx);
    if (ret) {
        return ret;
    }

    hash = sss_nss_mc_hash(&sid_mc_ctx, key, key_len + 1);
    slot = sid_mc_ctx.hash_table[hash];

    while (MC_SLOT_WITHIN_BOUNDS(slot, sid_mc_ctx.dt_size)) {
        free(rec); /* free record from previous iteration */
        rec = NULL;

        ret = sss_nss_mc_get_record(&sid_mc_ctx, slot, &rec);
        if (ret) {
            goto done;
        }
        if (hash != rec->hash2) {
            ret = EINVAL;
            goto done;
        }

        data = (struct sss_mc_sid_data *)rec->data;
        if (id == data->id) {
            if (rec->expire < time(NULL)) {
                ret = EINVAL;
                goto done;
            }
            *type = data->type;
            if (populated_by) {
                *populated_by = data->populated_by;
            }
            *sid = strdup(data->sid);
            if (!*sid) {
                ret = ENOMEM;
            }
            goto done;
        }

        slot = sss_nss_mc_next_slot_with_hash(rec, hash);
    }

    ret = ENOENT;

done:
    free(rec);
    __sync_sub_and_fetch(&sid_mc_ctx.active_threads, 1);
    return ret;
}

errno_t sss_nss_mc_get_sid_by_uid(uint32_t id, char **sid, uint32_t *type)
{
    return mc_get_sid_by_typed_id(id, SSS_ID_TYPE_UID, sid, type, NULL);
}

errno_t sss_nss_mc_get_sid_by_gid(uint32_t id, char **sid, uint32_t *type)
{
    return mc_get_sid_by_typed_id(id, SSS_ID_TYPE_GID, sid, type, NULL);
}

errno_t sss_nss_mc_get_sid_by_id(uint32_t id, char **sid, uint32_t *type)
{
    errno_t ret;
    uint32_t populated_by;

    /* MC should behave the same way sssd_nss does.
     * If user object exists sssd_nss would always return this user object.
     */
    ret = sss_nss_mc_get_sid_by_uid(id, sid, type);
    if (ret != ENOENT) {
        return ret; /* found or fatal error */
    }

    /* This is where things get tricky.
     * Consider a case of manually created user private group:
     * since MC could be primed via explicit by-gid() lookup,
     * missing user object doesn't mean sssd_nss wouldn't return
     * it, hence only return group object if cache was primed via
     * by-id() lookup.
     */
    ret = mc_get_sid_by_typed_id(id, SSS_ID_TYPE_GID, sid, type, &populated_by);
    if ((ret == 0) && (populated_by == 1)) {
        /* Cache was primed via explicit by-gid() lookup - request should go to sssd_nss */
        free(*sid);
        ret = ENOENT;
    }

    return ret;
}

errno_t sss_nss_mc_get_id_by_sid(const char *sid, uint32_t *id, uint32_t *type)
{
    int ret;
    int key_len;
    uint32_t hash;
    uint32_t slot;
    struct sss_mc_rec *rec = NULL;
    const struct sss_mc_sid_data *data = NULL;

    key_len = strlen(sid) + 1;

    ret = sss_nss_mc_get_ctx("sid", &sid_mc_ctx);
    if (ret) {
        return ret;
    }

    hash = sss_nss_mc_hash(&sid_mc_ctx, sid, key_len);
    slot = sid_mc_ctx.hash_table[hash];

    while (MC_SLOT_WITHIN_BOUNDS(slot, sid_mc_ctx.dt_size)) {
        free(rec); /* free record from previous iteration */
        rec = NULL;

        ret = sss_nss_mc_get_record(&sid_mc_ctx, slot, &rec);
        if (ret) {
            goto done;
        }
        if (hash != rec->hash1) {
            ret = EINVAL;
            goto done;
        }

        data = (struct sss_mc_sid_data *)rec->data;
        if (strcmp(sid, data->sid) == 0) {
            if (rec->expire < time(NULL)) {
                ret = EINVAL;
                goto done;
            }
            *type = data->type;
            *id = data->id;
            goto done; /* ret == 0 */
        }

        slot = sss_nss_mc_next_slot_with_hash(rec, hash);
    }

    ret = ENOENT;

done:
    free(rec);
    __sync_sub_and_fetch(&sid_mc_ctx.active_threads, 1);
    return ret;
}