1 | /* |
---|
2 | Unix SMB/CIFS implementation. |
---|
3 | ID Mapping Cache |
---|
4 | |
---|
5 | based on gencache |
---|
6 | |
---|
7 | Copyright (C) Simo Sorce 2006 |
---|
8 | Copyright (C) Rafal Szczesniak 2002 |
---|
9 | |
---|
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. |
---|
14 | |
---|
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. |
---|
19 | |
---|
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.*/ |
---|
23 | |
---|
24 | #include "includes.h" |
---|
25 | #include "winbindd.h" |
---|
26 | |
---|
27 | #define TIMEOUT_LEN 12 |
---|
28 | #define IDMAP_CACHE_DATA_FMT "%12u/%s" |
---|
29 | #define IDMAP_READ_CACHE_DATA_FMT_TEMPLATE "%%12u/%%%us" |
---|
30 | |
---|
31 | struct idmap_cache_ctx { |
---|
32 | TDB_CONTEXT *tdb; |
---|
33 | }; |
---|
34 | |
---|
35 | static int idmap_cache_destructor(struct idmap_cache_ctx *cache) |
---|
36 | { |
---|
37 | int ret = 0; |
---|
38 | |
---|
39 | if (cache && cache->tdb) { |
---|
40 | ret = tdb_close(cache->tdb); |
---|
41 | cache->tdb = NULL; |
---|
42 | } |
---|
43 | |
---|
44 | return ret; |
---|
45 | } |
---|
46 | |
---|
47 | struct idmap_cache_ctx *idmap_cache_init(TALLOC_CTX *memctx) |
---|
48 | { |
---|
49 | struct idmap_cache_ctx *cache; |
---|
50 | char* cache_fname = NULL; |
---|
51 | |
---|
52 | cache = talloc(memctx, struct idmap_cache_ctx); |
---|
53 | if ( ! cache) { |
---|
54 | DEBUG(0, ("Out of memory!\n")); |
---|
55 | return NULL; |
---|
56 | } |
---|
57 | |
---|
58 | cache_fname = lock_path("idmap_cache.tdb"); |
---|
59 | |
---|
60 | DEBUG(10, ("Opening cache file at %s\n", cache_fname)); |
---|
61 | |
---|
62 | cache->tdb = tdb_open_log(cache_fname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600); |
---|
63 | |
---|
64 | if (!cache->tdb) { |
---|
65 | DEBUG(5, ("Attempt to open %s has failed.\n", cache_fname)); |
---|
66 | return NULL; |
---|
67 | } |
---|
68 | |
---|
69 | talloc_set_destructor(cache, idmap_cache_destructor); |
---|
70 | |
---|
71 | return cache; |
---|
72 | } |
---|
73 | |
---|
74 | void idmap_cache_shutdown(struct idmap_cache_ctx *cache) |
---|
75 | { |
---|
76 | talloc_free(cache); |
---|
77 | } |
---|
78 | |
---|
79 | NTSTATUS idmap_cache_build_sidkey(TALLOC_CTX *ctx, char **sidkey, const struct id_map *id) |
---|
80 | { |
---|
81 | *sidkey = talloc_asprintf(ctx, "IDMAP/SID/%s", sid_string_static(id->sid)); |
---|
82 | if ( ! *sidkey) { |
---|
83 | DEBUG(1, ("failed to build sidkey, OOM?\n")); |
---|
84 | return NT_STATUS_NO_MEMORY; |
---|
85 | } |
---|
86 | |
---|
87 | return NT_STATUS_OK; |
---|
88 | } |
---|
89 | |
---|
90 | NTSTATUS idmap_cache_build_idkey(TALLOC_CTX *ctx, char **idkey, const struct id_map *id) |
---|
91 | { |
---|
92 | *idkey = talloc_asprintf(ctx, "IDMAP/%s/%lu", |
---|
93 | (id->xid.type==ID_TYPE_UID)?"UID":"GID", |
---|
94 | (unsigned long)id->xid.id); |
---|
95 | if ( ! *idkey) { |
---|
96 | DEBUG(1, ("failed to build idkey, OOM?\n")); |
---|
97 | return NT_STATUS_NO_MEMORY; |
---|
98 | } |
---|
99 | |
---|
100 | return NT_STATUS_OK; |
---|
101 | } |
---|
102 | |
---|
103 | NTSTATUS idmap_cache_set(struct idmap_cache_ctx *cache, const struct id_map *id) |
---|
104 | { |
---|
105 | NTSTATUS ret; |
---|
106 | time_t timeout = time(NULL) + lp_idmap_cache_time(); |
---|
107 | TDB_DATA keybuf, databuf; |
---|
108 | char *sidkey; |
---|
109 | char *idkey; |
---|
110 | char *valstr; |
---|
111 | |
---|
112 | ret = idmap_cache_build_sidkey(cache, &sidkey, id); |
---|
113 | if (!NT_STATUS_IS_OK(ret)) return ret; |
---|
114 | |
---|
115 | /* use sidkey as the local memory ctx */ |
---|
116 | ret = idmap_cache_build_idkey(sidkey, &idkey, id); |
---|
117 | if (!NT_STATUS_IS_OK(ret)) { |
---|
118 | goto done; |
---|
119 | } |
---|
120 | |
---|
121 | /* save SID -> ID */ |
---|
122 | |
---|
123 | /* use sidkey as the local memory ctx */ |
---|
124 | valstr = talloc_asprintf(sidkey, IDMAP_CACHE_DATA_FMT, (int)timeout, idkey); |
---|
125 | if (!valstr) { |
---|
126 | DEBUG(0, ("Out of memory!\n")); |
---|
127 | ret = NT_STATUS_NO_MEMORY; |
---|
128 | goto done; |
---|
129 | } |
---|
130 | |
---|
131 | keybuf.dptr = sidkey; |
---|
132 | keybuf.dsize = strlen(sidkey)+1; |
---|
133 | databuf.dptr = valstr; |
---|
134 | databuf.dsize = strlen(valstr)+1; |
---|
135 | DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout =" |
---|
136 | " %s (%d seconds %s)\n", keybuf.dptr, valstr , ctime(&timeout), |
---|
137 | (int)(timeout - time(NULL)), |
---|
138 | timeout > time(NULL) ? "ahead" : "in the past")); |
---|
139 | |
---|
140 | if (tdb_store(cache->tdb, keybuf, databuf, TDB_REPLACE) != 0) { |
---|
141 | DEBUG(3, ("Failed to store cache entry!\n")); |
---|
142 | ret = NT_STATUS_UNSUCCESSFUL; |
---|
143 | goto done; |
---|
144 | } |
---|
145 | |
---|
146 | /* save ID -> SID */ |
---|
147 | |
---|
148 | /* use sidkey as the local memory ctx */ |
---|
149 | valstr = talloc_asprintf(sidkey, IDMAP_CACHE_DATA_FMT, (int)timeout, sidkey); |
---|
150 | if (!valstr) { |
---|
151 | DEBUG(0, ("Out of memory!\n")); |
---|
152 | ret = NT_STATUS_NO_MEMORY; |
---|
153 | goto done; |
---|
154 | } |
---|
155 | |
---|
156 | keybuf.dptr = idkey; |
---|
157 | keybuf.dsize = strlen(idkey)+1; |
---|
158 | databuf.dptr = valstr; |
---|
159 | databuf.dsize = strlen(valstr)+1; |
---|
160 | DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout =" |
---|
161 | " %s (%d seconds %s)\n", keybuf.dptr, valstr, ctime(&timeout), |
---|
162 | (int)(timeout - time(NULL)), |
---|
163 | timeout > time(NULL) ? "ahead" : "in the past")); |
---|
164 | |
---|
165 | if (tdb_store(cache->tdb, keybuf, databuf, TDB_REPLACE) != 0) { |
---|
166 | DEBUG(3, ("Failed to store cache entry!\n")); |
---|
167 | ret = NT_STATUS_UNSUCCESSFUL; |
---|
168 | goto done; |
---|
169 | } |
---|
170 | |
---|
171 | ret = NT_STATUS_OK; |
---|
172 | |
---|
173 | done: |
---|
174 | talloc_free(sidkey); |
---|
175 | return ret; |
---|
176 | } |
---|
177 | |
---|
178 | NTSTATUS idmap_cache_del(struct idmap_cache_ctx *cache, const struct id_map *id) |
---|
179 | { |
---|
180 | NTSTATUS ret; |
---|
181 | TDB_DATA keybuf; |
---|
182 | char *sidkey = NULL; |
---|
183 | char *idkey = NULL; |
---|
184 | |
---|
185 | ret = idmap_cache_build_sidkey(cache, &sidkey, id); |
---|
186 | if (!NT_STATUS_IS_OK(ret)) return ret; |
---|
187 | |
---|
188 | ret = idmap_cache_build_idkey(cache, &idkey, id); |
---|
189 | if (!NT_STATUS_IS_OK(ret)) { |
---|
190 | goto done; |
---|
191 | } |
---|
192 | |
---|
193 | /* delete SID */ |
---|
194 | |
---|
195 | keybuf.dptr = sidkey; |
---|
196 | keybuf.dsize = strlen(sidkey)+1; |
---|
197 | DEBUG(10, ("Deleting cache entry (key = %s)\n", keybuf.dptr)); |
---|
198 | |
---|
199 | if (tdb_delete(cache->tdb, keybuf) != 0) { |
---|
200 | DEBUG(3, ("Failed to delete cache entry!\n")); |
---|
201 | } |
---|
202 | |
---|
203 | /* delete ID */ |
---|
204 | |
---|
205 | keybuf.dptr = idkey; |
---|
206 | keybuf.dsize = strlen(idkey)+1; |
---|
207 | DEBUG(10, ("Deleting cache entry (key = %s)\n", keybuf.dptr)); |
---|
208 | |
---|
209 | if (tdb_delete(cache->tdb, keybuf) != 0) { |
---|
210 | DEBUG(3, ("Failed to delete cache entry!\n")); |
---|
211 | } |
---|
212 | |
---|
213 | done: |
---|
214 | talloc_free(sidkey); |
---|
215 | talloc_free(idkey); |
---|
216 | return ret; |
---|
217 | } |
---|
218 | |
---|
219 | NTSTATUS idmap_cache_set_negative_sid(struct idmap_cache_ctx *cache, const struct id_map *id) |
---|
220 | { |
---|
221 | NTSTATUS ret; |
---|
222 | time_t timeout = time(NULL) + lp_idmap_negative_cache_time(); |
---|
223 | TDB_DATA keybuf, databuf; |
---|
224 | char *sidkey; |
---|
225 | char *valstr; |
---|
226 | |
---|
227 | ret = idmap_cache_build_sidkey(cache, &sidkey, id); |
---|
228 | if (!NT_STATUS_IS_OK(ret)) return ret; |
---|
229 | |
---|
230 | /* use sidkey as the local memory ctx */ |
---|
231 | valstr = talloc_asprintf(sidkey, IDMAP_CACHE_DATA_FMT, (int)timeout, "IDMAP/NEGATIVE"); |
---|
232 | if (!valstr) { |
---|
233 | DEBUG(0, ("Out of memory!\n")); |
---|
234 | ret = NT_STATUS_NO_MEMORY; |
---|
235 | goto done; |
---|
236 | } |
---|
237 | |
---|
238 | keybuf.dptr = sidkey; |
---|
239 | keybuf.dsize = strlen(sidkey)+1; |
---|
240 | databuf.dptr = valstr; |
---|
241 | databuf.dsize = strlen(valstr)+1; |
---|
242 | DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout =" |
---|
243 | " %s (%d seconds %s)\n", keybuf.dptr, valstr, ctime(&timeout), |
---|
244 | (int)(timeout - time(NULL)), |
---|
245 | timeout > time(NULL) ? "ahead" : "in the past")); |
---|
246 | |
---|
247 | if (tdb_store(cache->tdb, keybuf, databuf, TDB_REPLACE) != 0) { |
---|
248 | DEBUG(3, ("Failed to store cache entry!\n")); |
---|
249 | ret = NT_STATUS_UNSUCCESSFUL; |
---|
250 | goto done; |
---|
251 | } |
---|
252 | |
---|
253 | done: |
---|
254 | talloc_free(sidkey); |
---|
255 | return ret; |
---|
256 | } |
---|
257 | |
---|
258 | NTSTATUS idmap_cache_set_negative_id(struct idmap_cache_ctx *cache, const struct id_map *id) |
---|
259 | { |
---|
260 | NTSTATUS ret; |
---|
261 | time_t timeout = time(NULL) + lp_idmap_negative_cache_time(); |
---|
262 | TDB_DATA keybuf, databuf; |
---|
263 | char *idkey; |
---|
264 | char *valstr; |
---|
265 | |
---|
266 | ret = idmap_cache_build_idkey(cache, &idkey, id); |
---|
267 | if (!NT_STATUS_IS_OK(ret)) return ret; |
---|
268 | |
---|
269 | /* use idkey as the local memory ctx */ |
---|
270 | valstr = talloc_asprintf(idkey, IDMAP_CACHE_DATA_FMT, (int)timeout, "IDMAP/NEGATIVE"); |
---|
271 | if (!valstr) { |
---|
272 | DEBUG(0, ("Out of memory!\n")); |
---|
273 | ret = NT_STATUS_NO_MEMORY; |
---|
274 | goto done; |
---|
275 | } |
---|
276 | |
---|
277 | keybuf.dptr = idkey; |
---|
278 | keybuf.dsize = strlen(idkey)+1; |
---|
279 | databuf.dptr = valstr; |
---|
280 | databuf.dsize = strlen(valstr)+1; |
---|
281 | DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout =" |
---|
282 | " %s (%d seconds %s)\n", keybuf.dptr, valstr, ctime(&timeout), |
---|
283 | (int)(timeout - time(NULL)), |
---|
284 | timeout > time(NULL) ? "ahead" : "in the past")); |
---|
285 | |
---|
286 | if (tdb_store(cache->tdb, keybuf, databuf, TDB_REPLACE) != 0) { |
---|
287 | DEBUG(3, ("Failed to store cache entry!\n")); |
---|
288 | ret = NT_STATUS_UNSUCCESSFUL; |
---|
289 | goto done; |
---|
290 | } |
---|
291 | |
---|
292 | done: |
---|
293 | talloc_free(idkey); |
---|
294 | return ret; |
---|
295 | } |
---|
296 | |
---|
297 | NTSTATUS idmap_cache_fill_map(struct id_map *id, const char *value) |
---|
298 | { |
---|
299 | char *rem; |
---|
300 | |
---|
301 | /* see if it is a sid */ |
---|
302 | if ( ! strncmp("IDMAP/SID/", value, 10)) { |
---|
303 | |
---|
304 | if ( ! string_to_sid(id->sid, &value[10])) { |
---|
305 | goto failed; |
---|
306 | } |
---|
307 | |
---|
308 | id->status = ID_MAPPED; |
---|
309 | |
---|
310 | return NT_STATUS_OK; |
---|
311 | } |
---|
312 | |
---|
313 | /* not a SID see if it is an UID or a GID */ |
---|
314 | if ( ! strncmp("IDMAP/UID/", value, 10)) { |
---|
315 | |
---|
316 | /* a uid */ |
---|
317 | id->xid.type = ID_TYPE_UID; |
---|
318 | |
---|
319 | } else if ( ! strncmp("IDMAP/GID/", value, 10)) { |
---|
320 | |
---|
321 | /* a gid */ |
---|
322 | id->xid.type = ID_TYPE_GID; |
---|
323 | |
---|
324 | } else { |
---|
325 | |
---|
326 | /* a completely bogus value bail out */ |
---|
327 | goto failed; |
---|
328 | } |
---|
329 | |
---|
330 | id->xid.id = strtol(&value[10], &rem, 0); |
---|
331 | if (*rem != '\0') { |
---|
332 | goto failed; |
---|
333 | } |
---|
334 | |
---|
335 | id->status = ID_MAPPED; |
---|
336 | |
---|
337 | return NT_STATUS_OK; |
---|
338 | |
---|
339 | failed: |
---|
340 | DEBUG(1, ("invalid value: %s\n", value)); |
---|
341 | id->status = ID_UNKNOWN; |
---|
342 | return NT_STATUS_INTERNAL_DB_CORRUPTION; |
---|
343 | } |
---|
344 | |
---|
345 | BOOL idmap_cache_is_negative(const char *val) |
---|
346 | { |
---|
347 | if ( ! strcmp("IDMAP/NEGATIVE", val)) { |
---|
348 | return True; |
---|
349 | } |
---|
350 | return False; |
---|
351 | } |
---|
352 | |
---|
353 | /* search the cahce for the SID an return a mapping if found * |
---|
354 | * |
---|
355 | * 3 cases are possible |
---|
356 | * |
---|
357 | * 1 map found |
---|
358 | * in this case id->status = ID_MAPPED and NT_STATUS_OK is returned |
---|
359 | * 2 map not found |
---|
360 | * in this case id->status = ID_UNKNOWN and NT_STATUS_NONE_MAPPED is returned |
---|
361 | * 3 negative cache found |
---|
362 | * in this case id->status = ID_UNMAPPED and NT_STATUS_OK is returned |
---|
363 | * |
---|
364 | * As a special case if the cache is expired NT_STATUS_SYNCHRONIZATION_REQUIRED |
---|
365 | * is returned instead of NT_STATUS_OK. In this case revalidation of the cache |
---|
366 | * is needed. |
---|
367 | */ |
---|
368 | |
---|
369 | NTSTATUS idmap_cache_map_sid(struct idmap_cache_ctx *cache, struct id_map *id) |
---|
370 | { |
---|
371 | NTSTATUS ret; |
---|
372 | TDB_DATA keybuf, databuf; |
---|
373 | time_t t; |
---|
374 | char *sidkey; |
---|
375 | char *endptr; |
---|
376 | |
---|
377 | /* make sure it is marked as not mapped by default */ |
---|
378 | id->status = ID_UNKNOWN; |
---|
379 | |
---|
380 | ret = idmap_cache_build_sidkey(cache, &sidkey, id); |
---|
381 | if (!NT_STATUS_IS_OK(ret)) return ret; |
---|
382 | |
---|
383 | keybuf.dptr = sidkey; |
---|
384 | keybuf.dsize = strlen(sidkey)+1; |
---|
385 | |
---|
386 | databuf = tdb_fetch(cache->tdb, keybuf); |
---|
387 | |
---|
388 | if (databuf.dptr == NULL) { |
---|
389 | DEBUG(10, ("Cache entry with key = %s couldn't be found\n", sidkey)); |
---|
390 | return NT_STATUS_NONE_MAPPED; |
---|
391 | } |
---|
392 | |
---|
393 | t = strtol(databuf.dptr, &endptr, 10); |
---|
394 | |
---|
395 | if ((endptr == NULL) || (*endptr != '/')) { |
---|
396 | DEBUG(2, ("Invalid gencache data format: %s\n", databuf.dptr)); |
---|
397 | /* remove the entry */ |
---|
398 | tdb_delete(cache->tdb, keybuf); |
---|
399 | ret = NT_STATUS_NONE_MAPPED; |
---|
400 | goto done; |
---|
401 | } |
---|
402 | |
---|
403 | /* check it is not negative */ |
---|
404 | if (strcmp("IDMAP/NEGATIVE", endptr+1) != 0) { |
---|
405 | |
---|
406 | DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, " |
---|
407 | "timeout = %s", t > time(NULL) ? "valid" : |
---|
408 | "expired", sidkey, endptr+1, ctime(&t))); |
---|
409 | |
---|
410 | /* this call if successful will also mark the entry as mapped */ |
---|
411 | ret = idmap_cache_fill_map(id, endptr+1); |
---|
412 | if ( ! NT_STATUS_IS_OK(ret)) { |
---|
413 | /* if not valid form delete the entry */ |
---|
414 | tdb_delete(cache->tdb, keybuf); |
---|
415 | ret = NT_STATUS_NONE_MAPPED; |
---|
416 | goto done; |
---|
417 | } |
---|
418 | |
---|
419 | /* here ret == NT_STATUS_OK and id->status = ID_MAPPED */ |
---|
420 | |
---|
421 | if (t <= time(NULL)) { |
---|
422 | /* If we've been told to be offline - stay in |
---|
423 | that state... */ |
---|
424 | if (lp_winbind_offline_logon() && |
---|
425 | get_global_winbindd_state_offline()) |
---|
426 | { |
---|
427 | DEBUG(10,("idmap_cache_map_sid: winbindd is " |
---|
428 | "globally offline.\n")); |
---|
429 | } else { |
---|
430 | /* We're expired, set an error code |
---|
431 | for upper layer */ |
---|
432 | ret = NT_STATUS_SYNCHRONIZATION_REQUIRED; |
---|
433 | } |
---|
434 | } |
---|
435 | } else { |
---|
436 | if (t <= time(NULL)) { |
---|
437 | /* If we've been told to be offline - stay in |
---|
438 | that state... */ |
---|
439 | if (lp_winbind_offline_logon() && |
---|
440 | get_global_winbindd_state_offline()) |
---|
441 | { |
---|
442 | DEBUG(10,("idmap_cache_map_sid: winbindd is " |
---|
443 | "globally offline.\n")); |
---|
444 | } else { |
---|
445 | /* We're expired, delete the entry and return |
---|
446 | not mapped */ |
---|
447 | tdb_delete(cache->tdb, keybuf); |
---|
448 | ret = NT_STATUS_NONE_MAPPED; |
---|
449 | } |
---|
450 | } else { |
---|
451 | /* this is not mapped as it was a negative cache hit */ |
---|
452 | id->status = ID_UNMAPPED; |
---|
453 | ret = NT_STATUS_OK; |
---|
454 | } |
---|
455 | } |
---|
456 | |
---|
457 | done: |
---|
458 | SAFE_FREE(databuf.dptr); |
---|
459 | talloc_free(sidkey); |
---|
460 | return ret; |
---|
461 | } |
---|
462 | |
---|
463 | /* search the cahce for the ID an return a mapping if found * |
---|
464 | * |
---|
465 | * 3 cases are possible |
---|
466 | * |
---|
467 | * 1 map found |
---|
468 | * in this case id->status = ID_MAPPED and NT_STATUS_OK is returned |
---|
469 | * 2 map not found |
---|
470 | * in this case id->status = ID_UNKNOWN and NT_STATUS_NONE_MAPPED is returned |
---|
471 | * 3 negative cache found |
---|
472 | * in this case id->status = ID_UNMAPPED and NT_STATUS_OK is returned |
---|
473 | * |
---|
474 | * As a special case if the cache is expired NT_STATUS_SYNCHRONIZATION_REQUIRED |
---|
475 | * is returned instead of NT_STATUS_OK. In this case revalidation of the cache |
---|
476 | * is needed. |
---|
477 | */ |
---|
478 | |
---|
479 | NTSTATUS idmap_cache_map_id(struct idmap_cache_ctx *cache, struct id_map *id) |
---|
480 | { |
---|
481 | NTSTATUS ret; |
---|
482 | TDB_DATA keybuf, databuf; |
---|
483 | time_t t; |
---|
484 | char *idkey; |
---|
485 | char *endptr; |
---|
486 | |
---|
487 | /* make sure it is marked as not mapped by default */ |
---|
488 | id->status = ID_UNKNOWN; |
---|
489 | |
---|
490 | ret = idmap_cache_build_idkey(cache, &idkey, id); |
---|
491 | if (!NT_STATUS_IS_OK(ret)) return ret; |
---|
492 | |
---|
493 | keybuf.dptr = idkey; |
---|
494 | keybuf.dsize = strlen(idkey)+1; |
---|
495 | |
---|
496 | databuf = tdb_fetch(cache->tdb, keybuf); |
---|
497 | |
---|
498 | if (databuf.dptr == NULL) { |
---|
499 | DEBUG(10, ("Cache entry with key = %s couldn't be found\n", idkey)); |
---|
500 | return NT_STATUS_NONE_MAPPED; |
---|
501 | } |
---|
502 | |
---|
503 | t = strtol(databuf.dptr, &endptr, 10); |
---|
504 | |
---|
505 | if ((endptr == NULL) || (*endptr != '/')) { |
---|
506 | DEBUG(2, ("Invalid gencache data format: %s\n", databuf.dptr)); |
---|
507 | /* remove the entry */ |
---|
508 | tdb_delete(cache->tdb, keybuf); |
---|
509 | ret = NT_STATUS_NONE_MAPPED; |
---|
510 | goto done; |
---|
511 | } |
---|
512 | |
---|
513 | /* check it is not negative */ |
---|
514 | if (strcmp("IDMAP/NEGATIVE", endptr+1) != 0) { |
---|
515 | |
---|
516 | DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, " |
---|
517 | "timeout = %s", t > time(NULL) ? "valid" : |
---|
518 | "expired", idkey, endptr+1, ctime(&t))); |
---|
519 | |
---|
520 | /* this call if successful will also mark the entry as mapped */ |
---|
521 | ret = idmap_cache_fill_map(id, endptr+1); |
---|
522 | if ( ! NT_STATUS_IS_OK(ret)) { |
---|
523 | /* if not valid form delete the entry */ |
---|
524 | tdb_delete(cache->tdb, keybuf); |
---|
525 | ret = NT_STATUS_NONE_MAPPED; |
---|
526 | goto done; |
---|
527 | } |
---|
528 | |
---|
529 | /* here ret == NT_STATUS_OK and id->mapped = True */ |
---|
530 | |
---|
531 | if (t <= time(NULL)) { |
---|
532 | /* If we've been told to be offline - stay in |
---|
533 | that state... */ |
---|
534 | if (lp_winbind_offline_logon() && |
---|
535 | get_global_winbindd_state_offline()) |
---|
536 | { |
---|
537 | DEBUG(10,("idmap_cache_map_sid: winbindd is " |
---|
538 | "globally offline.\n")); |
---|
539 | } else { |
---|
540 | /* We're expired, set an error code |
---|
541 | for upper layer */ |
---|
542 | ret = NT_STATUS_SYNCHRONIZATION_REQUIRED; |
---|
543 | } |
---|
544 | } |
---|
545 | } else { |
---|
546 | if (t <= time(NULL)) { |
---|
547 | /* If we've been told to be offline - stay in |
---|
548 | that state... */ |
---|
549 | if (lp_winbind_offline_logon() && |
---|
550 | get_global_winbindd_state_offline()) |
---|
551 | { |
---|
552 | DEBUG(10,("idmap_cache_map_sid: winbindd is " |
---|
553 | "globally offline.\n")); |
---|
554 | } else { |
---|
555 | /* We're expired, delete the entry and |
---|
556 | return not mapped */ |
---|
557 | tdb_delete(cache->tdb, keybuf); |
---|
558 | ret = NT_STATUS_NONE_MAPPED; |
---|
559 | } |
---|
560 | } else { |
---|
561 | /* this is not mapped is it was a negative cache hit */ |
---|
562 | id->status = ID_UNMAPPED; |
---|
563 | ret = NT_STATUS_OK; |
---|
564 | } |
---|
565 | } |
---|
566 | done: |
---|
567 | SAFE_FREE(databuf.dptr); |
---|
568 | talloc_free(idkey); |
---|
569 | return ret; |
---|
570 | } |
---|
571 | |
---|