2 Unix SMB/CIFS implementation.
7 Copyright (C) Simo Sorce 2006
8 Copyright (C) Rafal Szczesniak 2002
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.*/
27 #define TIMEOUT_LEN 12
28 #define IDMAP_CACHE_DATA_FMT "%12u/%s"
29 #define IDMAP_READ_CACHE_DATA_FMT_TEMPLATE "%%12u/%%%us"
31 struct idmap_cache_ctx
{
35 static int idmap_cache_destructor(struct idmap_cache_ctx
*cache
)
39 if (cache
&& cache
->tdb
) {
40 ret
= tdb_close(cache
->tdb
);
47 struct idmap_cache_ctx
*idmap_cache_init(TALLOC_CTX
*memctx
)
49 struct idmap_cache_ctx
*cache
;
50 char* cache_fname
= NULL
;
52 cache
= talloc(memctx
, struct idmap_cache_ctx
);
54 DEBUG(0, ("Out of memory!\n"));
58 cache_fname
= lock_path("idmap_cache.tdb");
60 DEBUG(10, ("Opening cache file at %s\n", cache_fname
));
62 cache
->tdb
= tdb_open_log(cache_fname
, 0, TDB_DEFAULT
, O_RDWR
|O_CREAT
, 0600);
65 DEBUG(5, ("Attempt to open %s has failed.\n", cache_fname
));
69 talloc_set_destructor(cache
, idmap_cache_destructor
);
74 void idmap_cache_shutdown(struct idmap_cache_ctx
*cache
)
79 NTSTATUS
idmap_cache_build_sidkey(TALLOC_CTX
*ctx
, char **sidkey
, const struct id_map
*id
)
81 *sidkey
= talloc_asprintf(ctx
, "IDMAP/SID/%s", sid_string_static(id
->sid
));
83 DEBUG(1, ("failed to build sidkey, OOM?\n"));
84 return NT_STATUS_NO_MEMORY
;
90 NTSTATUS
idmap_cache_build_idkey(TALLOC_CTX
*ctx
, char **idkey
, const struct id_map
*id
)
92 *idkey
= talloc_asprintf(ctx
, "IDMAP/%s/%lu",
93 (id
->xid
.type
==ID_TYPE_UID
)?"UID":"GID",
94 (unsigned long)id
->xid
.id
);
96 DEBUG(1, ("failed to build idkey, OOM?\n"));
97 return NT_STATUS_NO_MEMORY
;
103 NTSTATUS
idmap_cache_set(struct idmap_cache_ctx
*cache
, const struct id_map
*id
)
106 time_t timeout
= time(NULL
) + lp_idmap_cache_time();
107 TDB_DATA keybuf
, databuf
;
112 /* Don't cache lookups in the S-1-22-{1,2} domain */
113 if ( (id
->xid
.type
== ID_TYPE_UID
) &&
114 sid_check_is_in_unix_users(id
->sid
) )
118 if ( (id
->xid
.type
== ID_TYPE_GID
) &&
119 sid_check_is_in_unix_groups(id
->sid
) )
125 ret
= idmap_cache_build_sidkey(cache
, &sidkey
, id
);
126 if (!NT_STATUS_IS_OK(ret
)) return ret
;
128 /* use sidkey as the local memory ctx */
129 ret
= idmap_cache_build_idkey(sidkey
, &idkey
, id
);
130 if (!NT_STATUS_IS_OK(ret
)) {
136 /* use sidkey as the local memory ctx */
137 valstr
= talloc_asprintf(sidkey
, IDMAP_CACHE_DATA_FMT
, (int)timeout
, idkey
);
139 DEBUG(0, ("Out of memory!\n"));
140 ret
= NT_STATUS_NO_MEMORY
;
144 keybuf
.dptr
= sidkey
;
145 keybuf
.dsize
= strlen(sidkey
)+1;
146 databuf
.dptr
= valstr
;
147 databuf
.dsize
= strlen(valstr
)+1;
148 DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout ="
149 " %s (%d seconds %s)\n", keybuf
.dptr
, valstr
, ctime(&timeout
),
150 (int)(timeout
- time(NULL
)),
151 timeout
> time(NULL
) ? "ahead" : "in the past"));
153 if (tdb_store(cache
->tdb
, keybuf
, databuf
, TDB_REPLACE
) != 0) {
154 DEBUG(3, ("Failed to store cache entry!\n"));
155 ret
= NT_STATUS_UNSUCCESSFUL
;
161 /* use sidkey as the local memory ctx */
162 valstr
= talloc_asprintf(sidkey
, IDMAP_CACHE_DATA_FMT
, (int)timeout
, sidkey
);
164 DEBUG(0, ("Out of memory!\n"));
165 ret
= NT_STATUS_NO_MEMORY
;
170 keybuf
.dsize
= strlen(idkey
)+1;
171 databuf
.dptr
= valstr
;
172 databuf
.dsize
= strlen(valstr
)+1;
173 DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout ="
174 " %s (%d seconds %s)\n", keybuf
.dptr
, valstr
, ctime(&timeout
),
175 (int)(timeout
- time(NULL
)),
176 timeout
> time(NULL
) ? "ahead" : "in the past"));
178 if (tdb_store(cache
->tdb
, keybuf
, databuf
, TDB_REPLACE
) != 0) {
179 DEBUG(3, ("Failed to store cache entry!\n"));
180 ret
= NT_STATUS_UNSUCCESSFUL
;
191 NTSTATUS
idmap_cache_del(struct idmap_cache_ctx
*cache
, const struct id_map
*id
)
198 ret
= idmap_cache_build_sidkey(cache
, &sidkey
, id
);
199 if (!NT_STATUS_IS_OK(ret
)) return ret
;
201 ret
= idmap_cache_build_idkey(cache
, &idkey
, id
);
202 if (!NT_STATUS_IS_OK(ret
)) {
208 keybuf
.dptr
= sidkey
;
209 keybuf
.dsize
= strlen(sidkey
)+1;
210 DEBUG(10, ("Deleting cache entry (key = %s)\n", keybuf
.dptr
));
212 if (tdb_delete(cache
->tdb
, keybuf
) != 0) {
213 DEBUG(3, ("Failed to delete cache entry!\n"));
219 keybuf
.dsize
= strlen(idkey
)+1;
220 DEBUG(10, ("Deleting cache entry (key = %s)\n", keybuf
.dptr
));
222 if (tdb_delete(cache
->tdb
, keybuf
) != 0) {
223 DEBUG(3, ("Failed to delete cache entry!\n"));
232 NTSTATUS
idmap_cache_set_negative_sid(struct idmap_cache_ctx
*cache
, const struct id_map
*id
)
235 time_t timeout
= time(NULL
) + lp_idmap_negative_cache_time();
236 TDB_DATA keybuf
, databuf
;
240 ret
= idmap_cache_build_sidkey(cache
, &sidkey
, id
);
241 if (!NT_STATUS_IS_OK(ret
)) return ret
;
243 /* use sidkey as the local memory ctx */
244 valstr
= talloc_asprintf(sidkey
, IDMAP_CACHE_DATA_FMT
, (int)timeout
, "IDMAP/NEGATIVE");
246 DEBUG(0, ("Out of memory!\n"));
247 ret
= NT_STATUS_NO_MEMORY
;
251 keybuf
.dptr
= sidkey
;
252 keybuf
.dsize
= strlen(sidkey
)+1;
253 databuf
.dptr
= valstr
;
254 databuf
.dsize
= strlen(valstr
)+1;
255 DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout ="
256 " %s (%d seconds %s)\n", keybuf
.dptr
, valstr
, ctime(&timeout
),
257 (int)(timeout
- time(NULL
)),
258 timeout
> time(NULL
) ? "ahead" : "in the past"));
260 if (tdb_store(cache
->tdb
, keybuf
, databuf
, TDB_REPLACE
) != 0) {
261 DEBUG(3, ("Failed to store cache entry!\n"));
262 ret
= NT_STATUS_UNSUCCESSFUL
;
271 NTSTATUS
idmap_cache_set_negative_id(struct idmap_cache_ctx
*cache
, const struct id_map
*id
)
274 time_t timeout
= time(NULL
) + lp_idmap_negative_cache_time();
275 TDB_DATA keybuf
, databuf
;
279 ret
= idmap_cache_build_idkey(cache
, &idkey
, id
);
280 if (!NT_STATUS_IS_OK(ret
)) return ret
;
282 /* use idkey as the local memory ctx */
283 valstr
= talloc_asprintf(idkey
, IDMAP_CACHE_DATA_FMT
, (int)timeout
, "IDMAP/NEGATIVE");
285 DEBUG(0, ("Out of memory!\n"));
286 ret
= NT_STATUS_NO_MEMORY
;
291 keybuf
.dsize
= strlen(idkey
)+1;
292 databuf
.dptr
= valstr
;
293 databuf
.dsize
= strlen(valstr
)+1;
294 DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout ="
295 " %s (%d seconds %s)\n", keybuf
.dptr
, valstr
, ctime(&timeout
),
296 (int)(timeout
- time(NULL
)),
297 timeout
> time(NULL
) ? "ahead" : "in the past"));
299 if (tdb_store(cache
->tdb
, keybuf
, databuf
, TDB_REPLACE
) != 0) {
300 DEBUG(3, ("Failed to store cache entry!\n"));
301 ret
= NT_STATUS_UNSUCCESSFUL
;
310 NTSTATUS
idmap_cache_fill_map(struct id_map
*id
, const char *value
)
314 /* see if it is a sid */
315 if ( ! strncmp("IDMAP/SID/", value
, 10)) {
317 if ( ! string_to_sid(id
->sid
, &value
[10])) {
321 id
->status
= ID_MAPPED
;
326 /* not a SID see if it is an UID or a GID */
327 if ( ! strncmp("IDMAP/UID/", value
, 10)) {
330 id
->xid
.type
= ID_TYPE_UID
;
332 } else if ( ! strncmp("IDMAP/GID/", value
, 10)) {
335 id
->xid
.type
= ID_TYPE_GID
;
339 /* a completely bogus value bail out */
343 id
->xid
.id
= strtol(&value
[10], &rem
, 0);
348 id
->status
= ID_MAPPED
;
353 DEBUG(1, ("invalid value: %s\n", value
));
354 id
->status
= ID_UNKNOWN
;
355 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
358 BOOL
idmap_cache_is_negative(const char *val
)
360 if ( ! strcmp("IDMAP/NEGATIVE", val
)) {
366 /* search the cahce for the SID an return a mapping if found *
368 * 4 cases are possible
371 * in this case id->status = ID_MAPPED and NT_STATUS_OK is returned
373 * in this case id->status = ID_UNKNOWN and NT_STATUS_NONE_MAPPED is returned
374 * 3 negative cache found
375 * in this case id->status = ID_UNMAPPED and NT_STATUS_OK is returned
376 * 4 map found but timer expired
377 * in this case id->status = ID_EXPIRED and NT_STATUS_SYNCHRONIZATION_REQUIRED
378 * is returned. In this case revalidation of the cache is needed.
381 NTSTATUS
idmap_cache_map_sid(struct idmap_cache_ctx
*cache
, struct id_map
*id
)
384 TDB_DATA keybuf
, databuf
;
389 /* make sure it is marked as unknown by default */
390 id
->status
= ID_UNKNOWN
;
392 ret
= idmap_cache_build_sidkey(cache
, &sidkey
, id
);
393 if (!NT_STATUS_IS_OK(ret
)) return ret
;
395 keybuf
.dptr
= sidkey
;
396 keybuf
.dsize
= strlen(sidkey
)+1;
398 databuf
= tdb_fetch(cache
->tdb
, keybuf
);
400 if (databuf
.dptr
== NULL
) {
401 DEBUG(10, ("Cache entry with key = %s couldn't be found\n", sidkey
));
402 ret
= NT_STATUS_NONE_MAPPED
;
406 t
= strtol(databuf
.dptr
, &endptr
, 10);
408 if ((endptr
== NULL
) || (*endptr
!= '/')) {
409 DEBUG(2, ("Invalid gencache data format: %s\n", databuf
.dptr
));
410 /* remove the entry */
411 tdb_delete(cache
->tdb
, keybuf
);
412 ret
= NT_STATUS_NONE_MAPPED
;
418 /* check it is not negative */
419 if (strcmp("IDMAP/NEGATIVE", endptr
+1) != 0) {
421 DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, "
422 "timeout = %s", t
> now
? "valid" :
423 "expired", sidkey
, endptr
+1, ctime(&t
)));
425 /* this call if successful will also mark the entry as mapped */
426 ret
= idmap_cache_fill_map(id
, endptr
+1);
427 if ( ! NT_STATUS_IS_OK(ret
)) {
428 /* if not valid form delete the entry */
429 tdb_delete(cache
->tdb
, keybuf
);
430 ret
= NT_STATUS_NONE_MAPPED
;
434 /* here ret == NT_STATUS_OK and id->status = ID_MAPPED */
438 /* we have it, but it is expired */
439 id
->status
= ID_EXPIRED
;
441 /* We're expired, set an error code
443 ret
= NT_STATUS_SYNCHRONIZATION_REQUIRED
;
447 /* We're expired, delete the NEGATIVE entry and return
449 tdb_delete(cache
->tdb
, keybuf
);
450 ret
= NT_STATUS_NONE_MAPPED
;
452 /* this is not mapped as it was a negative cache hit */
453 id
->status
= ID_UNMAPPED
;
459 SAFE_FREE(databuf
.dptr
);
464 /* search the cahce for the ID an return a mapping if found *
466 * 3 cases are possible
469 * in this case id->status = ID_MAPPED and NT_STATUS_OK is returned
471 * in this case id->status = ID_UNKNOWN and NT_STATUS_NONE_MAPPED is returned
472 * 3 negative cache found
473 * in this case id->status = ID_UNMAPPED and NT_STATUS_OK is returned
474 * 4 map found but timer expired
475 * in this case id->status = ID_EXPIRED and NT_STATUS_SYNCHRONIZATION_REQUIRED
476 * is returned. In this case revalidation of the cache is needed.
479 NTSTATUS
idmap_cache_map_id(struct idmap_cache_ctx
*cache
, struct id_map
*id
)
482 TDB_DATA keybuf
, databuf
;
487 /* make sure it is marked as unknown by default */
488 id
->status
= ID_UNKNOWN
;
490 ret
= idmap_cache_build_idkey(cache
, &idkey
, id
);
491 if (!NT_STATUS_IS_OK(ret
)) return ret
;
494 keybuf
.dsize
= strlen(idkey
)+1;
496 databuf
= tdb_fetch(cache
->tdb
, keybuf
);
498 if (databuf
.dptr
== NULL
) {
499 DEBUG(10, ("Cache entry with key = %s couldn't be found\n", idkey
));
500 ret
= NT_STATUS_NONE_MAPPED
;
504 t
= strtol(databuf
.dptr
, &endptr
, 10);
506 if ((endptr
== NULL
) || (*endptr
!= '/')) {
507 DEBUG(2, ("Invalid gencache data format: %s\n", databuf
.dptr
));
508 /* remove the entry */
509 tdb_delete(cache
->tdb
, keybuf
);
510 ret
= NT_STATUS_NONE_MAPPED
;
516 /* check it is not negative */
517 if (strcmp("IDMAP/NEGATIVE", endptr
+1) != 0) {
519 DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, "
520 "timeout = %s", t
> now
? "valid" :
521 "expired", idkey
, endptr
+1, ctime(&t
)));
523 /* this call if successful will also mark the entry as mapped */
524 ret
= idmap_cache_fill_map(id
, endptr
+1);
525 if ( ! NT_STATUS_IS_OK(ret
)) {
526 /* if not valid form delete the entry */
527 tdb_delete(cache
->tdb
, keybuf
);
528 ret
= NT_STATUS_NONE_MAPPED
;
532 /* here ret == NT_STATUS_OK and id->mapped = ID_MAPPED */
536 /* we have it, but it is expired */
537 id
->status
= ID_EXPIRED
;
539 /* We're expired, set an error code
541 ret
= NT_STATUS_SYNCHRONIZATION_REQUIRED
;
545 /* We're expired, delete the NEGATIVE entry and return
547 tdb_delete(cache
->tdb
, keybuf
);
548 ret
= NT_STATUS_NONE_MAPPED
;
550 /* this is not mapped as it was a negative cache hit */
551 id
->status
= ID_UNMAPPED
;
556 SAFE_FREE(databuf
.dptr
);