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();
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 databuf
= string_term_tdb_data(valstr
);
145 DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout ="
146 " %s (%d seconds %s)\n", sidkey
, valstr
, ctime(&timeout
),
147 (int)(timeout
- time(NULL
)),
148 timeout
> time(NULL
) ? "ahead" : "in the past"));
150 if (tdb_store_bystring(cache
->tdb
, sidkey
, databuf
, TDB_REPLACE
) != 0) {
151 DEBUG(3, ("Failed to store cache entry!\n"));
152 ret
= NT_STATUS_UNSUCCESSFUL
;
158 /* use sidkey as the local memory ctx */
159 valstr
= talloc_asprintf(sidkey
, IDMAP_CACHE_DATA_FMT
, (int)timeout
, sidkey
);
161 DEBUG(0, ("Out of memory!\n"));
162 ret
= NT_STATUS_NO_MEMORY
;
166 databuf
= string_term_tdb_data(valstr
);
167 DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout ="
168 " %s (%d seconds %s)\n", idkey
, valstr
, ctime(&timeout
),
169 (int)(timeout
- time(NULL
)),
170 timeout
> time(NULL
) ? "ahead" : "in the past"));
172 if (tdb_store_bystring(cache
->tdb
, idkey
, databuf
, TDB_REPLACE
) != 0) {
173 DEBUG(3, ("Failed to store cache entry!\n"));
174 ret
= NT_STATUS_UNSUCCESSFUL
;
185 NTSTATUS
idmap_cache_del(struct idmap_cache_ctx
*cache
, const struct id_map
*id
)
191 ret
= idmap_cache_build_sidkey(cache
, &sidkey
, id
);
192 if (!NT_STATUS_IS_OK(ret
)) return ret
;
194 ret
= idmap_cache_build_idkey(cache
, &idkey
, id
);
195 if (!NT_STATUS_IS_OK(ret
)) {
201 DEBUG(10, ("Deleting cache entry (key = %s)\n", sidkey
));
203 if (tdb_delete_bystring(cache
->tdb
, sidkey
) != 0) {
204 DEBUG(3, ("Failed to delete cache entry!\n"));
209 DEBUG(10, ("Deleting cache entry (key = %s)\n", idkey
));
211 if (tdb_delete_bystring(cache
->tdb
, idkey
) != 0) {
212 DEBUG(3, ("Failed to delete cache entry!\n"));
221 NTSTATUS
idmap_cache_set_negative_sid(struct idmap_cache_ctx
*cache
, const struct id_map
*id
)
224 time_t timeout
= time(NULL
) + lp_idmap_negative_cache_time();
229 ret
= idmap_cache_build_sidkey(cache
, &sidkey
, id
);
230 if (!NT_STATUS_IS_OK(ret
)) return ret
;
232 /* use sidkey as the local memory ctx */
233 valstr
= talloc_asprintf(sidkey
, IDMAP_CACHE_DATA_FMT
, (int)timeout
, "IDMAP/NEGATIVE");
235 DEBUG(0, ("Out of memory!\n"));
236 ret
= NT_STATUS_NO_MEMORY
;
240 databuf
= string_term_tdb_data(valstr
);
241 DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout ="
242 " %s (%d seconds %s)\n", sidkey
, valstr
, ctime(&timeout
),
243 (int)(timeout
- time(NULL
)),
244 timeout
> time(NULL
) ? "ahead" : "in the past"));
246 if (tdb_store_bystring(cache
->tdb
, sidkey
, databuf
, TDB_REPLACE
) != 0) {
247 DEBUG(3, ("Failed to store cache entry!\n"));
248 ret
= NT_STATUS_UNSUCCESSFUL
;
257 NTSTATUS
idmap_cache_set_negative_id(struct idmap_cache_ctx
*cache
, const struct id_map
*id
)
260 time_t timeout
= time(NULL
) + lp_idmap_negative_cache_time();
265 ret
= idmap_cache_build_idkey(cache
, &idkey
, id
);
266 if (!NT_STATUS_IS_OK(ret
)) return ret
;
268 /* use idkey as the local memory ctx */
269 valstr
= talloc_asprintf(idkey
, IDMAP_CACHE_DATA_FMT
, (int)timeout
, "IDMAP/NEGATIVE");
271 DEBUG(0, ("Out of memory!\n"));
272 ret
= NT_STATUS_NO_MEMORY
;
276 databuf
= string_term_tdb_data(valstr
);
277 DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout ="
278 " %s (%d seconds %s)\n", idkey
, valstr
, ctime(&timeout
),
279 (int)(timeout
- time(NULL
)),
280 timeout
> time(NULL
) ? "ahead" : "in the past"));
282 if (tdb_store_bystring(cache
->tdb
, idkey
, databuf
, TDB_REPLACE
) != 0) {
283 DEBUG(3, ("Failed to store cache entry!\n"));
284 ret
= NT_STATUS_UNSUCCESSFUL
;
293 NTSTATUS
idmap_cache_fill_map(struct id_map
*id
, const char *value
)
297 /* see if it is a sid */
298 if ( ! strncmp("IDMAP/SID/", value
, 10)) {
300 if ( ! string_to_sid(id
->sid
, &value
[10])) {
304 id
->status
= ID_MAPPED
;
309 /* not a SID see if it is an UID or a GID */
310 if ( ! strncmp("IDMAP/UID/", value
, 10)) {
313 id
->xid
.type
= ID_TYPE_UID
;
315 } else if ( ! strncmp("IDMAP/GID/", value
, 10)) {
318 id
->xid
.type
= ID_TYPE_GID
;
322 /* a completely bogus value bail out */
326 id
->xid
.id
= strtol(&value
[10], &rem
, 0);
331 id
->status
= ID_MAPPED
;
336 DEBUG(1, ("invalid value: %s\n", value
));
337 id
->status
= ID_UNKNOWN
;
338 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
341 BOOL
idmap_cache_is_negative(const char *val
)
343 if ( ! strcmp("IDMAP/NEGATIVE", val
)) {
349 /* search the cahce for the SID an return a mapping if found *
351 * 3 cases are possible
354 * in this case id->status = ID_MAPPED and NT_STATUS_OK is returned
356 * in this case id->status = ID_UNKNOWN and NT_STATUS_NONE_MAPPED is returned
357 * 3 negative cache found
358 * in this case id->status = ID_UNMAPPED and NT_STATUS_OK is returned
360 * As a special case if the cache is expired NT_STATUS_SYNCHRONIZATION_REQUIRED
361 * is returned instead of NT_STATUS_OK. In this case revalidation of the cache
365 NTSTATUS
idmap_cache_map_sid(struct idmap_cache_ctx
*cache
, struct id_map
*id
)
373 /* make sure it is marked as not mapped by default */
374 id
->status
= ID_UNKNOWN
;
376 ret
= idmap_cache_build_sidkey(cache
, &sidkey
, id
);
377 if (!NT_STATUS_IS_OK(ret
)) return ret
;
379 databuf
= tdb_fetch_bystring(cache
->tdb
, sidkey
);
381 if (databuf
.dptr
== NULL
) {
382 DEBUG(10, ("Cache entry with key = %s couldn't be found\n", sidkey
));
383 return NT_STATUS_NONE_MAPPED
;
386 t
= strtol((const char *)databuf
.dptr
, &endptr
, 10);
388 if ((endptr
== NULL
) || (*endptr
!= '/')) {
389 DEBUG(2, ("Invalid gencache data format: %s\n", (const char *)databuf
.dptr
));
390 /* remove the entry */
391 tdb_delete_bystring(cache
->tdb
, sidkey
);
392 ret
= NT_STATUS_NONE_MAPPED
;
396 /* check it is not negative */
397 if (strcmp("IDMAP/NEGATIVE", endptr
+1) != 0) {
399 DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, "
400 "timeout = %s", t
> time(NULL
) ? "valid" :
401 "expired", sidkey
, endptr
+1, ctime(&t
)));
403 /* this call if successful will also mark the entry as mapped */
404 ret
= idmap_cache_fill_map(id
, endptr
+1);
405 if ( ! NT_STATUS_IS_OK(ret
)) {
406 /* if not valid form delete the entry */
407 tdb_delete_bystring(cache
->tdb
, sidkey
);
408 ret
= NT_STATUS_NONE_MAPPED
;
412 /* here ret == NT_STATUS_OK and id->status = ID_MAPPED */
414 if (t
<= time(NULL
)) {
415 /* If we've been told to be offline - stay in
417 if (lp_winbind_offline_logon() &&
418 get_global_winbindd_state_offline())
420 DEBUG(10,("idmap_cache_map_sid: winbindd is "
421 "globally offline.\n"));
423 /* We're expired, set an error code
425 ret
= NT_STATUS_SYNCHRONIZATION_REQUIRED
;
429 if (t
<= time(NULL
)) {
430 /* If we've been told to be offline - stay in
432 if (lp_winbind_offline_logon() &&
433 get_global_winbindd_state_offline())
435 DEBUG(10,("idmap_cache_map_sid: winbindd is "
436 "globally offline.\n"));
438 /* We're expired, delete the entry and return
440 tdb_delete_bystring(cache
->tdb
, sidkey
);
441 ret
= NT_STATUS_NONE_MAPPED
;
444 /* this is not mapped as it was a negative cache hit */
445 id
->status
= ID_UNMAPPED
;
451 SAFE_FREE(databuf
.dptr
);
456 /* search the cahce for the ID an return a mapping if found *
458 * 3 cases are possible
461 * in this case id->status = ID_MAPPED and NT_STATUS_OK is returned
463 * in this case id->status = ID_UNKNOWN and NT_STATUS_NONE_MAPPED is returned
464 * 3 negative cache found
465 * in this case id->status = ID_UNMAPPED and NT_STATUS_OK is returned
467 * As a special case if the cache is expired NT_STATUS_SYNCHRONIZATION_REQUIRED
468 * is returned instead of NT_STATUS_OK. In this case revalidation of the cache
472 NTSTATUS
idmap_cache_map_id(struct idmap_cache_ctx
*cache
, struct id_map
*id
)
480 /* make sure it is marked as not mapped by default */
481 id
->status
= ID_UNKNOWN
;
483 ret
= idmap_cache_build_idkey(cache
, &idkey
, id
);
484 if (!NT_STATUS_IS_OK(ret
)) return ret
;
486 databuf
= tdb_fetch_bystring(cache
->tdb
, idkey
);
488 if (databuf
.dptr
== NULL
) {
489 DEBUG(10, ("Cache entry with key = %s couldn't be found\n", idkey
));
490 return NT_STATUS_NONE_MAPPED
;
493 t
= strtol((const char *)databuf
.dptr
, &endptr
, 10);
495 if ((endptr
== NULL
) || (*endptr
!= '/')) {
496 DEBUG(2, ("Invalid gencache data format: %s\n", (const char *)databuf
.dptr
));
497 /* remove the entry */
498 tdb_delete_bystring(cache
->tdb
, idkey
);
499 ret
= NT_STATUS_NONE_MAPPED
;
503 /* check it is not negative */
504 if (strcmp("IDMAP/NEGATIVE", endptr
+1) != 0) {
506 DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, "
507 "timeout = %s", t
> time(NULL
) ? "valid" :
508 "expired", idkey
, endptr
+1, ctime(&t
)));
510 /* this call if successful will also mark the entry as mapped */
511 ret
= idmap_cache_fill_map(id
, endptr
+1);
512 if ( ! NT_STATUS_IS_OK(ret
)) {
513 /* if not valid form delete the entry */
514 tdb_delete_bystring(cache
->tdb
, idkey
);
515 ret
= NT_STATUS_NONE_MAPPED
;
519 /* here ret == NT_STATUS_OK and id->mapped = True */
521 if (t
<= time(NULL
)) {
522 /* If we've been told to be offline - stay in
524 if (lp_winbind_offline_logon() &&
525 get_global_winbindd_state_offline())
527 DEBUG(10,("idmap_cache_map_sid: winbindd is "
528 "globally offline.\n"));
530 /* We're expired, set an error code
532 ret
= NT_STATUS_SYNCHRONIZATION_REQUIRED
;
536 if (t
<= time(NULL
)) {
537 /* If we've been told to be offline - stay in
539 if (lp_winbind_offline_logon() &&
540 get_global_winbindd_state_offline())
542 DEBUG(10,("idmap_cache_map_sid: winbindd is "
543 "globally offline.\n"));
545 /* We're expired, delete the entry and
547 tdb_delete_bystring(cache
->tdb
, idkey
);
548 ret
= NT_STATUS_NONE_MAPPED
;
551 /* this is not mapped is it was a negative cache hit */
552 id
->status
= ID_UNMAPPED
;
557 SAFE_FREE(databuf
.dptr
);