6
Copyright (C) Simo Sorce <ssorce@redhat.com> 2008
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 3 of the License, or
11
(at your option) any later version.
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.
18
You should have received a copy of the GNU General Public License
19
along with this program. If not, see <http://www.gnu.org/licenses/>.
22
#include "util/util.h"
27
#define NC_ENTRY_PREFIX "NCE/"
28
#define NC_USER_PREFIX NC_ENTRY_PREFIX"USER"
29
#define NC_GROUP_PREFIX NC_ENTRY_PREFIX"GROUP"
30
#define NC_UID_PREFIX NC_ENTRY_PREFIX"UID"
31
#define NC_GID_PREFIX NC_ENTRY_PREFIX"GID"
34
struct tdb_context *tdb;
37
static int string_to_tdb_data(char *str, TDB_DATA *ret)
39
if (!str || !ret) return EINVAL;
41
ret->dptr = (uint8_t *)str;
42
ret->dsize = strlen(str)+1;
47
int nss_ncache_init(TALLOC_CTX *memctx, struct nss_nc_ctx **_ctx)
49
struct nss_nc_ctx *ctx;
51
ctx = talloc_zero(memctx, struct nss_nc_ctx);
52
if (!ctx) return ENOMEM;
55
/* open a memory only tdb with default hash size */
56
ctx->tdb = tdb_open("memcache", 0, TDB_INTERNAL, O_RDWR|O_CREAT, 0);
57
if (!ctx->tdb) return errno;
63
static int nss_ncache_check_str(struct nss_nc_ctx *ctx, char *str, int ttl)
67
unsigned long long int timestamp;
72
ret = string_to_tdb_data(str, &key);
73
if (ret != EOK) goto done;
75
data = tdb_fetch(ctx->tdb, key);
83
/* a negative ttl means: never expires */
89
timestamp = strtoull((const char *)data.dptr, &ep, 0);
90
if (errno != 0 || *ep != '\0') {
91
/* Malformed entry, remove it and return no entry */
97
/* a 0 timestamp means this is a permanent entry */
102
if (timestamp + ttl > time(NULL)) {
112
/* expired, remove and return no entry */
113
tdb_delete(ctx->tdb, key);
120
static int nss_ncache_set_str(struct nss_nc_ctx *ctx,
121
char *str, bool permanent)
128
ret = string_to_tdb_data(str, &key);
129
if (ret != EOK) return ret;
132
timest = talloc_strdup(ctx, "0");
134
timest = talloc_asprintf(ctx, "%llu",
135
(unsigned long long int)time(NULL));
137
if (!timest) return ENOMEM;
139
ret = string_to_tdb_data(timest, &data);
140
if (ret != EOK) goto done;
142
ret = tdb_store(ctx->tdb, key, data, TDB_REPLACE);
144
DEBUG(1, ("Negative cache failed to set entry: [%s]",
145
tdb_errorstr(ctx->tdb)));
154
int nss_ncache_check_user(struct nss_nc_ctx *ctx, int ttl,
155
const char *domain, const char *name)
160
if (!name || !*name) return EINVAL;
162
str = talloc_asprintf(ctx, "%s/%s/%s", NC_USER_PREFIX, domain, name);
163
if (!str) return ENOMEM;
165
ret = nss_ncache_check_str(ctx, str, ttl);
171
int nss_ncache_check_group(struct nss_nc_ctx *ctx, int ttl,
172
const char *domain, const char *name)
177
if (!name || !*name) return EINVAL;
179
str = talloc_asprintf(ctx, "%s/%s/%s", NC_GROUP_PREFIX, domain, name);
180
if (!str) return ENOMEM;
182
ret = nss_ncache_check_str(ctx, str, ttl);
188
int nss_ncache_check_uid(struct nss_nc_ctx *ctx, int ttl, uid_t uid)
193
str = talloc_asprintf(ctx, "%s/%u", NC_UID_PREFIX, uid);
194
if (!str) return ENOMEM;
196
ret = nss_ncache_check_str(ctx, str, ttl);
202
int nss_ncache_check_gid(struct nss_nc_ctx *ctx, int ttl, gid_t gid)
207
str = talloc_asprintf(ctx, "%s/%u", NC_GID_PREFIX, gid);
208
if (!str) return ENOMEM;
210
ret = nss_ncache_check_str(ctx, str, ttl);
216
int nss_ncache_set_user(struct nss_nc_ctx *ctx, bool permanent,
217
const char *domain, const char *name)
222
if (!name || !*name) return EINVAL;
224
str = talloc_asprintf(ctx, "%s/%s/%s", NC_USER_PREFIX, domain, name);
225
if (!str) return ENOMEM;
227
ret = nss_ncache_set_str(ctx, str, permanent);
233
int nss_ncache_set_group(struct nss_nc_ctx *ctx, bool permanent,
234
const char *domain, const char *name)
239
if (!name || !*name) return EINVAL;
241
str = talloc_asprintf(ctx, "%s/%s/%s", NC_GROUP_PREFIX, domain, name);
242
if (!str) return ENOMEM;
244
ret = nss_ncache_set_str(ctx, str, permanent);
250
int nss_ncache_set_uid(struct nss_nc_ctx *ctx, bool permanent, uid_t uid)
255
str = talloc_asprintf(ctx, "%s/%u", NC_UID_PREFIX, uid);
256
if (!str) return ENOMEM;
258
ret = nss_ncache_set_str(ctx, str, permanent);
264
int nss_ncache_set_gid(struct nss_nc_ctx *ctx, bool permanent, gid_t gid)
269
str = talloc_asprintf(ctx, "%s/%u", NC_GID_PREFIX, gid);
270
if (!str) return ENOMEM;
272
ret = nss_ncache_set_str(ctx, str, permanent);
278
static int delete_permanent(struct tdb_context *tdb,
279
TDB_DATA key, TDB_DATA data, void *state)
281
unsigned long long int timestamp;
282
bool remove_key = false;
285
if (strncmp((char *)key.dptr,
286
NC_ENTRY_PREFIX, sizeof(NC_ENTRY_PREFIX)) != 0) {
287
/* not interested in this key */
292
timestamp = strtoull((const char *)data.dptr, &ep, 0);
293
if (errno != 0 || *ep != '\0') {
294
/* Malformed entry, remove it */
299
if (timestamp == 0) {
300
/* a 0 timestamp means this is a permanent entry */
306
return tdb_delete(tdb, key);
312
int nss_ncache_reset_permament(struct nss_nc_ctx *ctx)
316
ret = tdb_traverse(ctx->tdb, delete_permanent, NULL);