summaryrefslogtreecommitdiff
path: root/source3/nsswitch/idmap_cache.c
blob: 897dd9c4f5b5e8d6159c7245aa817bf28080bff2 (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/* 
   Unix SMB/CIFS implementation.
   ID Mapping Cache

   based on gencache

   Copyright (C) Simo Sorce		2006
   Copyright (C) Rafal Szczesniak	2002

   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 2 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, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.*/

#include "includes.h"

#define TIMEOUT_LEN 12
#define IDMAP_CACHE_DATA_FMT	"%12u/%s"
#define IDMAP_READ_CACHE_DATA_FMT_TEMPLATE "%%12u/%%%us"

struct idmap_cache_ctx {
	TDB_CONTEXT *tdb;
};

static int idmap_cache_destructor(struct idmap_cache_ctx *cache)
{
	int ret = 0;

	if (cache && cache->tdb) {
		ret = tdb_close(cache->tdb);
		cache->tdb = NULL;
	}

	return ret;
}

struct idmap_cache_ctx *idmap_cache_init(TALLOC_CTX *memctx)
{
	struct idmap_cache_ctx *cache;
	char* cache_fname = NULL;

	cache = talloc(memctx, struct idmap_cache_ctx);
	if ( ! cache) {
		DEBUG(0, ("Out of memory!\n"));
		return NULL;
	}

	cache_fname = lock_path("idmap_cache.tdb");

	DEBUG(10, ("Opening cache file at %s\n", cache_fname));

	cache->tdb = tdb_open_log(cache_fname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);

	if (!cache->tdb) {
		DEBUG(5, ("Attempt to open %s has failed.\n", cache_fname));
		return NULL;
	}

	talloc_set_destructor(cache, idmap_cache_destructor);

	return cache;
}

void idmap_cache_shutdown(struct idmap_cache_ctx *cache)
{
	talloc_free(cache);
}

NTSTATUS idmap_cache_build_sidkey(TALLOC_CTX *ctx, char **sidkey, const struct id_map *id)
{
	*sidkey = talloc_asprintf(ctx, "IDMAP/SID/%s", sid_string_static(id->sid));
	if ( ! *sidkey) {
		DEBUG(1, ("failed to build sidkey, OOM?\n"));
		return NT_STATUS_NO_MEMORY;
	}

	return NT_STATUS_OK;
}

NTSTATUS idmap_cache_build_idkey(TALLOC_CTX *ctx, char **idkey, const struct id_map *id)
{
	*idkey = talloc_asprintf(ctx, "IDMAP/%s/%lu",
				(id->xid.type==ID_TYPE_UID)?"UID":"GID",
				(unsigned long)id->xid.id);
	if ( ! *idkey) {
		DEBUG(1, ("failed to build idkey, OOM?\n"));
		return NT_STATUS_NO_MEMORY;
	}

	return NT_STATUS_OK;
}

NTSTATUS idmap_cache_set(struct idmap_cache_ctx *cache, const struct id_map *id)
{
	NTSTATUS ret;
	time_t timeout = time(NULL) + lp_idmap_expire_time();
	TDB_DATA keybuf, databuf;
	char *sidkey;
	char *idkey;
	char *valstr;

	ret = idmap_cache_build_sidkey(cache, &sidkey, id);
	if (!NT_STATUS_IS_OK(ret)) return ret;

	/* use sidkey as the local memory ctx */
	ret = idmap_cache_build_idkey(sidkey, &idkey, id);
	if (!NT_STATUS_IS_OK(ret)) {
		goto done;
	}

	/* save SID -> ID */

	/* use sidkey as the local memory ctx */
	valstr = talloc_asprintf(sidkey, IDMAP_CACHE_DATA_FMT, (int)timeout, idkey);
	if (!valstr) {
		DEBUG(0, ("Out of memory!\n"));
		ret = NT_STATUS_NO_MEMORY;
		goto done;
	}

	keybuf.dptr = sidkey;
	keybuf.dsize = strlen(sidkey)+1;
	databuf.dptr = valstr;
	databuf.dsize = strlen(valstr)+1;
	DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout ="
	           " %s (%d seconds %s)\n", keybuf.dptr, valstr , ctime(&timeout),
		   (int)(timeout - time(NULL)), 
		   timeout > time(NULL) ? "ahead" : "in the past"));

	if (tdb_store(cache->tdb, keybuf, databuf, TDB_REPLACE) != 0) {
		DEBUG(3, ("Failed to store cache entry!\n"));
		ret = NT_STATUS_UNSUCCESSFUL;
		goto done;
	}

	/* save ID -> SID */

	/* use sidkey as the local memory ctx */
	valstr = talloc_asprintf(sidkey, IDMAP_CACHE_DATA_FMT, (int)timeout, sidkey);
	if (!valstr) {
		DEBUG(0, ("Out of memory!\n"));
		ret = NT_STATUS_NO_MEMORY;
		goto done;
	}

	keybuf.dptr = idkey;
	keybuf.dsize = strlen(idkey)+1;
	databuf.dptr = valstr;
	databuf.dsize = strlen(valstr)+1;
	DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout ="
	           " %s (%d seconds %s)\n", keybuf.dptr, valstr, ctime(&timeout),
		   (int)(timeout - time(NULL)), 
		   timeout > time(NULL) ? "ahead" : "in the past"));

	if (tdb_store(cache->tdb, keybuf, databuf, TDB_REPLACE) != 0) {
		DEBUG(3, ("Failed to store cache entry!\n"));
		ret = NT_STATUS_UNSUCCESSFUL;
		goto done;
	}

	ret = NT_STATUS_OK;

done:
	talloc_free(sidkey);
	return ret;
}

NTSTATUS idmap_cache_del(struct idmap_cache_ctx *cache, const struct id_map *id)
{
	NTSTATUS ret;
	TDB_DATA keybuf;
	char *sidkey = NULL;
	char *idkey = NULL;

	ret = idmap_cache_build_sidkey(cache, &sidkey, id);
	if (!NT_STATUS_IS_OK(ret)) return ret;

	ret = idmap_cache_build_idkey(cache, &idkey, id);
	if (!NT_STATUS_IS_OK(ret)) {
		goto done;
	}

	/* delete SID */

	keybuf.dptr = sidkey;
	keybuf.dsize = strlen(sidkey)+1;
	DEBUG(10, ("Deleting cache entry (key = %s)\n", keybuf.dptr));

	if (tdb_delete(cache->tdb, keybuf) != 0) {
		DEBUG(3, ("Failed to delete cache entry!\n"));
	}

	/* delete ID */

	keybuf.dptr = idkey;
	keybuf.dsize = strlen(idkey)+1;
	DEBUG(10, ("Deleting cache entry (key = %s)\n", keybuf.dptr));

	if (tdb_delete(cache->tdb, keybuf) != 0) {
		DEBUG(3, ("Failed to delete cache entry!\n"));
	}

done:
	talloc_free(sidkey);
	talloc_free(idkey);
	return ret;
}

NTSTATUS idmap_cache_set_negative_sid(struct idmap_cache_ctx *cache, const struct id_map *id)
{
	NTSTATUS ret;
	time_t timeout = time(NULL) + lp_idmap_negative_time();
	TDB_DATA keybuf, databuf;
	char *sidkey;
	char *valstr;

	ret = idmap_cache_build_sidkey(cache, &sidkey, id);
	if (!NT_STATUS_IS_OK(ret)) return ret;

	/* use sidkey as the local memory ctx */
	valstr = talloc_asprintf(sidkey, IDMAP_CACHE_DATA_FMT, (int)timeout, "IDMAP/NEGATIVE");
	if (!valstr) {
		DEBUG(0, ("Out of memory!\n"));
		ret = NT_STATUS_NO_MEMORY;
		goto done;
	}

	keybuf.dptr = sidkey;
	keybuf.dsize = strlen(sidkey)+1;
	databuf.dptr = valstr;
	databuf.dsize = strlen(valstr)+1;
	DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout ="
	           " %s (%d seconds %s)\n", keybuf.dptr, valstr, ctime(&timeout),
		   (int)(timeout - time(NULL)), 
		   timeout > time(NULL) ? "ahead" : "in the past"));

	if (tdb_store(cache->tdb, keybuf, databuf, TDB_REPLACE) != 0) {
		DEBUG(3, ("Failed to store cache entry!\n"));
		ret = NT_STATUS_UNSUCCESSFUL;
		goto done;
	}

done:
	talloc_free(sidkey);
	return ret;
}

NTSTATUS idmap_cache_set_negative_id(struct idmap_cache_ctx *cache, const struct id_map *id)
{
	NTSTATUS ret;
	time_t timeout = time(NULL) + lp_idmap_negative_time();
	TDB_DATA keybuf, databuf;
	char *idkey;
	char *valstr;

	ret = idmap_cache_build_idkey(cache, &idkey, id);
	if (!NT_STATUS_IS_OK(ret)) return ret;

	/* use idkey as the local memory ctx */
	valstr = talloc_asprintf(idkey, IDMAP_CACHE_DATA_FMT, (int)timeout, "IDMAP/NEGATIVE");
	if (!valstr) {
		DEBUG(0, ("Out of memory!\n"));
		ret = NT_STATUS_NO_MEMORY;
		goto done;
	}

	keybuf.dptr = idkey;
	keybuf.dsize = strlen(idkey)+1;
	databuf.dptr = valstr;
	databuf.dsize = strlen(valstr)+1;
	DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout ="
	           " %s (%d seconds %s)\n", keybuf.dptr, valstr, ctime(&timeout),
		   (int)(timeout - time(NULL)), 
		   timeout > time(NULL) ? "ahead" : "in the past"));

	if (tdb_store(cache->tdb, keybuf, databuf, TDB_REPLACE) != 0) {
		DEBUG(3, ("Failed to store cache entry!\n"));
		ret = NT_STATUS_UNSUCCESSFUL;
		goto done;
	}

done:
	talloc_free(idkey);
	return ret;
}

NTSTATUS idmap_cache_fill_map(struct id_map *id, const char *value)
{
	char *rem;

	/* see if it is a sid */
	if ( ! strncmp("IDMAP/SID/", value, 10)) {
		
		if ( ! string_to_sid(id->sid, &value[10])) {
			goto failed;
		}
		
		id->status = ID_MAPPED;

		return NT_STATUS_OK;
	}

	/* not a SID see if it is an UID or a GID */
	if ( ! strncmp("IDMAP/UID/", value, 10)) {
		
		/* a uid */
		id->xid.type = ID_TYPE_UID;
		
	} else if ( ! strncmp("IDMAP/GID/", value, 10)) {
		
		/* a gid */
		id->xid.type = ID_TYPE_GID;
		
	} else {
		
		/* a completely bogus value bail out */
		goto failed;
	}
	
	id->xid.id = strtol(&value[10], &rem, 0);
	if (*rem != '\0') {
		goto failed;
	}

	id->status = ID_MAPPED;

	return NT_STATUS_OK;

failed:
	DEBUG(1, ("invalid value: %s\n", value));
	id->status = ID_UNKNOWN;
	return NT_STATUS_INTERNAL_DB_CORRUPTION;
}

BOOL idmap_cache_is_negative(const char *val)
{
	if ( ! strcmp("IDMAP/NEGATIVE", val)) {
		return True;
	}
	return False;
}

/* search the cahce for the SID an return a mapping if found *
 *
 * 3 cases are possible
 *
 * 1 map found
 * 	in this case id->status = ID_MAPPED and NT_STATUS_OK is returned
 * 2 map not found
 * 	in this case id->status = ID_UNKNOWN and NT_STATUS_NONE_MAPPED is returned
 * 3 negative cache found
 * 	in this case id->status = ID_UNMAPPED and NT_STATUS_OK is returned
 *
 * As a special case if the cache is expired NT_STATUS_SYNCHRONIZATION_REQUIRED
 * is returned instead of NT_STATUS_OK. In this case revalidation of the cache
 * is needed.
 */

NTSTATUS idmap_cache_map_sid(struct idmap_cache_ctx *cache, struct id_map *id)
{
	NTSTATUS ret;
	TDB_DATA keybuf, databuf;
	time_t t;
	char *sidkey;
	char *endptr;

	/* make sure it is marked as not mapped by default */
	id->status = ID_UNKNOWN;
	
	ret = idmap_cache_build_sidkey(cache, &sidkey, id);
	if (!NT_STATUS_IS_OK(ret)) return ret;

	keybuf.dptr = sidkey;
	keybuf.dsize = strlen(sidkey)+1;

	databuf = tdb_fetch(cache->tdb, keybuf);

	if (databuf.dptr == NULL) {
		DEBUG(10, ("Cache entry with key = %s couldn't be found\n", sidkey));
		return NT_STATUS_NONE_MAPPED;
	}

	t = strtol(databuf.dptr, &endptr, 10);

	if ((endptr == NULL) || (*endptr != '/')) {
		DEBUG(2, ("Invalid gencache data format: %s\n", databuf.dptr));
		/* remove the entry */
		tdb_delete(cache->tdb, keybuf);
		ret = NT_STATUS_NONE_MAPPED;
		goto done;
	}

	/* check it is not negative */
	if (strcmp("IDMAP/NEGATIVE", endptr+1) != 0) {
		
		DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, "
			   "timeout = %s", t > time(NULL) ? "valid" :
			   "expired", sidkey, endptr+1, ctime(&t)));

		/* this call if successful will also mark the entry as mapped */
		ret = idmap_cache_fill_map(id, endptr+1);
		if ( ! NT_STATUS_IS_OK(ret)) {
			/* if not valid form delete the entry */
			tdb_delete(cache->tdb, keybuf);
			ret = NT_STATUS_NONE_MAPPED;
			goto done;
		}

		/* here ret == NT_STATUS_OK and id->status = ID_MAPPED */

		if (t <= time(NULL)) {
			/* We're expired, set an error code for upper layer */
			ret = NT_STATUS_SYNCHRONIZATION_REQUIRED;
		}
	} else {
		if (t <= time(NULL)) {
			/* We're expired, delete the entry and return not mapped */
			tdb_delete(cache->tdb, keybuf);
			ret = NT_STATUS_NONE_MAPPED;
		} else {
			/* this is not mapped as it was a negative cache hit */
			id->status = ID_UNMAPPED;
			ret = NT_STATUS_OK;
		}
	}
	
done:
	SAFE_FREE(databuf.dptr);
	talloc_free(sidkey);
	return ret;
}

/* search the cahce for the ID an return a mapping if found *
 *
 * 3 cases are possible
 *
 * 1 map found
 * 	in this case id->status = ID_MAPPED and NT_STATUS_OK is returned
 * 2 map not found
 * 	in this case id->status = ID_UNKNOWN and NT_STATUS_NONE_MAPPED is returned
 * 3 negative cache found
 * 	in this case id->status = ID_UNMAPPED and NT_STATUS_OK is returned
 *
 * As a special case if the cache is expired NT_STATUS_SYNCHRONIZATION_REQUIRED
 * is returned instead of NT_STATUS_OK. In this case revalidation of the cache
 * is needed.
 */

NTSTATUS idmap_cache_map_id(struct idmap_cache_ctx *cache, struct id_map *id)
{
	NTSTATUS ret;
	TDB_DATA keybuf, databuf;
	time_t t;
	char *idkey;
	char *endptr;

	/* make sure it is marked as not mapped by default */
	id->status = ID_UNKNOWN;
	
	ret = idmap_cache_build_idkey(cache, &idkey, id);
	if (!NT_STATUS_IS_OK(ret)) return ret;

	keybuf.dptr = idkey;
	keybuf.dsize = strlen(idkey)+1;

	databuf = tdb_fetch(cache->tdb, keybuf);

	if (databuf.dptr == NULL) {
		DEBUG(10, ("Cache entry with key = %s couldn't be found\n", idkey));
		return NT_STATUS_NONE_MAPPED;
	}

	t = strtol(databuf.dptr, &endptr, 10);

	if ((endptr == NULL) || (*endptr != '/')) {
		DEBUG(2, ("Invalid gencache data format: %s\n", databuf.dptr));
		/* remove the entry */
		tdb_delete(cache->tdb, keybuf);
		ret = NT_STATUS_NONE_MAPPED;
		goto done;
	}

	/* check it is not negative */
	if (strcmp("IDMAP/NEGATIVE", endptr+1) != 0) {
		
		DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, "
			   "timeout = %s", t > time(NULL) ? "valid" :
			   "expired", idkey, endptr+1, ctime(&t)));

		/* this call if successful will also mark the entry as mapped */
		ret = idmap_cache_fill_map(id, endptr+1);
		if ( ! NT_STATUS_IS_OK(ret)) {
			/* if not valid form delete the entry */
			tdb_delete(cache->tdb, keybuf);
			ret = NT_STATUS_NONE_MAPPED;
			goto done;
		}

		/* here ret == NT_STATUS_OK and id->mapped = True */

		if (t <= time(NULL)) {
			/* We're expired, set an error code for upper layer */
			ret = NT_STATUS_SYNCHRONIZATION_REQUIRED;
		}
	} else {
		if (t <= time(NULL)) {
			/* We're expired, delete the entry and return not mapped */
			tdb_delete(cache->tdb, keybuf);
			ret = NT_STATUS_NONE_MAPPED;
		} else {
			/* this is not mapped is it was a negative cache hit */
			id->status = ID_UNMAPPED;
			ret = NT_STATUS_OK;
		}
	}
done:
	SAFE_FREE(databuf.dptr);
	talloc_free(idkey);
	return ret;
}