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