r23368: Make "winbind:rpc only" a full blown parameter. Thanks to Karolin for
[sfrench/samba-autobuild/.git] / source / 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 #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 databuf;
108         char *sidkey;
109         char *idkey;
110         char *valstr;
111
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) )
115         {
116                 return NT_STATUS_OK;
117         }
118         if ( (id->xid.type == ID_TYPE_GID) && 
119              sid_check_is_in_unix_groups(id->sid) )
120         {
121                 return NT_STATUS_OK;
122         }
123         
124
125         ret = idmap_cache_build_sidkey(cache, &sidkey, id);
126         if (!NT_STATUS_IS_OK(ret)) return ret;
127
128         /* use sidkey as the local memory ctx */
129         ret = idmap_cache_build_idkey(sidkey, &idkey, id);
130         if (!NT_STATUS_IS_OK(ret)) {
131                 goto done;
132         }
133
134         /* save SID -> ID */
135
136         /* use sidkey as the local memory ctx */
137         valstr = talloc_asprintf(sidkey, IDMAP_CACHE_DATA_FMT, (int)timeout, idkey);
138         if (!valstr) {
139                 DEBUG(0, ("Out of memory!\n"));
140                 ret = NT_STATUS_NO_MEMORY;
141                 goto done;
142         }
143
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"));
149
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;
153                 goto done;
154         }
155
156         /* save ID -> SID */
157
158         /* use sidkey as the local memory ctx */
159         valstr = talloc_asprintf(sidkey, IDMAP_CACHE_DATA_FMT, (int)timeout, sidkey);
160         if (!valstr) {
161                 DEBUG(0, ("Out of memory!\n"));
162                 ret = NT_STATUS_NO_MEMORY;
163                 goto done;
164         }
165
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"));
171
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;
175                 goto done;
176         }
177
178         ret = NT_STATUS_OK;
179
180 done:
181         talloc_free(sidkey);
182         return ret;
183 }
184
185 NTSTATUS idmap_cache_set_negative_sid(struct idmap_cache_ctx *cache, const struct id_map *id)
186 {
187         NTSTATUS ret;
188         time_t timeout = time(NULL) + lp_idmap_negative_cache_time();
189         TDB_DATA databuf;
190         char *sidkey;
191         char *valstr;
192
193         ret = idmap_cache_build_sidkey(cache, &sidkey, id);
194         if (!NT_STATUS_IS_OK(ret)) return ret;
195
196         /* use sidkey as the local memory ctx */
197         valstr = talloc_asprintf(sidkey, IDMAP_CACHE_DATA_FMT, (int)timeout, "IDMAP/NEGATIVE");
198         if (!valstr) {
199                 DEBUG(0, ("Out of memory!\n"));
200                 ret = NT_STATUS_NO_MEMORY;
201                 goto done;
202         }
203
204         databuf = string_term_tdb_data(valstr);
205         DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout ="
206                    " %s (%d seconds %s)\n", sidkey, valstr, ctime(&timeout),
207                    (int)(timeout - time(NULL)), 
208                    timeout > time(NULL) ? "ahead" : "in the past"));
209
210         if (tdb_store_bystring(cache->tdb, sidkey, databuf, TDB_REPLACE) != 0) {
211                 DEBUG(3, ("Failed to store cache entry!\n"));
212                 ret = NT_STATUS_UNSUCCESSFUL;
213                 goto done;
214         }
215
216 done:
217         talloc_free(sidkey);
218         return ret;
219 }
220
221 NTSTATUS idmap_cache_set_negative_id(struct idmap_cache_ctx *cache, const struct id_map *id)
222 {
223         NTSTATUS ret;
224         time_t timeout = time(NULL) + lp_idmap_negative_cache_time();
225         TDB_DATA databuf;
226         char *idkey;
227         char *valstr;
228
229         ret = idmap_cache_build_idkey(cache, &idkey, id);
230         if (!NT_STATUS_IS_OK(ret)) return ret;
231
232         /* use idkey as the local memory ctx */
233         valstr = talloc_asprintf(idkey, IDMAP_CACHE_DATA_FMT, (int)timeout, "IDMAP/NEGATIVE");
234         if (!valstr) {
235                 DEBUG(0, ("Out of memory!\n"));
236                 ret = NT_STATUS_NO_MEMORY;
237                 goto done;
238         }
239
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", idkey, valstr, ctime(&timeout),
243                    (int)(timeout - time(NULL)), 
244                    timeout > time(NULL) ? "ahead" : "in the past"));
245
246         if (tdb_store_bystring(cache->tdb, idkey, 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(idkey);
254         return ret;
255 }
256
257 NTSTATUS idmap_cache_fill_map(struct id_map *id, const char *value)
258 {
259         char *rem;
260
261         /* see if it is a sid */
262         if ( ! strncmp("IDMAP/SID/", value, 10)) {
263                 
264                 if ( ! string_to_sid(id->sid, &value[10])) {
265                         goto failed;
266                 }
267                 
268                 id->status = ID_MAPPED;
269
270                 return NT_STATUS_OK;
271         }
272
273         /* not a SID see if it is an UID or a GID */
274         if ( ! strncmp("IDMAP/UID/", value, 10)) {
275                 
276                 /* a uid */
277                 id->xid.type = ID_TYPE_UID;
278                 
279         } else if ( ! strncmp("IDMAP/GID/", value, 10)) {
280                 
281                 /* a gid */
282                 id->xid.type = ID_TYPE_GID;
283                 
284         } else {
285                 
286                 /* a completely bogus value bail out */
287                 goto failed;
288         }
289         
290         id->xid.id = strtol(&value[10], &rem, 0);
291         if (*rem != '\0') {
292                 goto failed;
293         }
294
295         id->status = ID_MAPPED;
296
297         return NT_STATUS_OK;
298
299 failed:
300         DEBUG(1, ("invalid value: %s\n", value));
301         id->status = ID_UNKNOWN;
302         return NT_STATUS_INTERNAL_DB_CORRUPTION;
303 }
304
305 BOOL idmap_cache_is_negative(const char *val)
306 {
307         if ( ! strcmp("IDMAP/NEGATIVE", val)) {
308                 return True;
309         }
310         return False;
311 }
312
313 /* search the cahce for the SID an return a mapping if found *
314  *
315  * 4 cases are possible
316  *
317  * 1 map found
318  *      in this case id->status = ID_MAPPED and NT_STATUS_OK is returned
319  * 2 map not found
320  *      in this case id->status = ID_UNKNOWN and NT_STATUS_NONE_MAPPED is returned
321  * 3 negative cache found
322  *      in this case id->status = ID_UNMAPPED and NT_STATUS_OK is returned
323  * 4 map found but timer expired
324  *      in this case id->status = ID_EXPIRED and NT_STATUS_SYNCHRONIZATION_REQUIRED
325  *      is returned. In this case revalidation of the cache is needed.
326  */
327
328 NTSTATUS idmap_cache_map_sid(struct idmap_cache_ctx *cache, struct id_map *id)
329 {
330         NTSTATUS ret;
331         TDB_DATA databuf;
332         time_t t;
333         char *sidkey;
334         char *endptr;
335         struct winbindd_domain *our_domain = find_our_domain(); 
336         time_t now = time(NULL);        
337
338         /* make sure it is marked as not mapped by default */
339         id->status = ID_UNKNOWN;
340         
341         ret = idmap_cache_build_sidkey(cache, &sidkey, id);
342         if (!NT_STATUS_IS_OK(ret)) return ret;
343
344         databuf = tdb_fetch_bystring(cache->tdb, sidkey);
345
346         if (databuf.dptr == NULL) {
347                 DEBUG(10, ("Cache entry with key = %s couldn't be found\n", sidkey));
348                 return NT_STATUS_NONE_MAPPED;
349         }
350
351         t = strtol((const char *)databuf.dptr, &endptr, 10);
352
353         if ((endptr == NULL) || (*endptr != '/')) {
354                 DEBUG(2, ("Invalid gencache data format: %s\n", (const char *)databuf.dptr));
355                 /* remove the entry */
356                 tdb_delete_bystring(cache->tdb, sidkey);
357                 ret = NT_STATUS_NONE_MAPPED;
358                 goto done;
359         }
360
361         /* check it is not negative */
362         if (strcmp("IDMAP/NEGATIVE", endptr+1) != 0) {
363
364                 DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, "
365                            "timeout = %s", t > now ? "valid" :
366                            "expired", sidkey, endptr+1, ctime(&t)));
367
368                 /* this call if successful will also mark the entry as mapped */
369                 ret = idmap_cache_fill_map(id, endptr+1);
370                 if ( ! NT_STATUS_IS_OK(ret)) {
371                         /* if not valid form delete the entry */
372                         tdb_delete_bystring(cache->tdb, sidkey);
373                         ret = NT_STATUS_NONE_MAPPED;
374                         goto done;
375                 }
376
377                 /* here ret == NT_STATUS_OK and id->status = ID_MAPPED */
378
379                 if (t <= now) {
380                         /* If we've been told to be offline - stay in 
381                            that state... */
382                         if ( IS_DOMAIN_OFFLINE(our_domain) ) {
383                                 DEBUG(10,("idmap_cache_map_sid: idmap is offline\n"));
384                                 goto done;                              
385                         }
386                                 
387                         /* We're expired, set an error code
388                            for upper layer */
389                         ret = NT_STATUS_SYNCHRONIZATION_REQUIRED;
390                 }
391
392                 goto done;              
393         }
394
395         /* Was a negative cache hit */
396
397         /* Ignore the negative cache when offline */
398
399         if ( IS_DOMAIN_OFFLINE(our_domain) ) {
400                 DEBUG(10,("idmap_cache_map_sid: idmap is offline\n"));
401                 goto done;
402         }
403
404
405         /* Check for valid or expired cache hits */
406                 if (t <= now) {
407                 /* We're expired. Return not mapped */
408                         ret = NT_STATUS_NONE_MAPPED;
409                 } else {
410                         /* this is not mapped as it was a negative cache hit */
411                         id->status = ID_UNMAPPED;
412                         ret = NT_STATUS_OK;
413                 }
414         
415 done:
416         SAFE_FREE(databuf.dptr);
417         talloc_free(sidkey);
418         return ret;
419 }
420
421 /* search the cahce for the ID an return a mapping if found *
422  *
423  * 4 cases are possible
424  *
425  * 1 map found
426  *      in this case id->status = ID_MAPPED and NT_STATUS_OK is returned
427  * 2 map not found
428  *      in this case id->status = ID_UNKNOWN and NT_STATUS_NONE_MAPPED is returned
429  * 3 negative cache found
430  *      in this case id->status = ID_UNMAPPED and NT_STATUS_OK is returned
431  * 4 map found but timer expired
432  *      in this case id->status = ID_EXPIRED and NT_STATUS_SYNCHRONIZATION_REQUIRED
433  *      is returned. In this case revalidation of the cache is needed.
434  */
435
436 NTSTATUS idmap_cache_map_id(struct idmap_cache_ctx *cache, struct id_map *id)
437 {
438         NTSTATUS ret;
439         TDB_DATA databuf;
440         time_t t;
441         char *idkey;
442         char *endptr;
443         struct winbindd_domain *our_domain = find_our_domain(); 
444         time_t now = time(NULL);        
445
446         /* make sure it is marked as unknown by default */
447         id->status = ID_UNKNOWN;
448         
449         ret = idmap_cache_build_idkey(cache, &idkey, id);
450         if (!NT_STATUS_IS_OK(ret)) return ret;
451
452         databuf = tdb_fetch_bystring(cache->tdb, idkey);
453
454         if (databuf.dptr == NULL) {
455                 DEBUG(10, ("Cache entry with key = %s couldn't be found\n", idkey));
456                 return NT_STATUS_NONE_MAPPED;
457         }
458
459         t = strtol((const char *)databuf.dptr, &endptr, 10);
460
461         if ((endptr == NULL) || (*endptr != '/')) {
462                 DEBUG(2, ("Invalid gencache data format: %s\n", (const char *)databuf.dptr));
463                 /* remove the entry */
464                 tdb_delete_bystring(cache->tdb, idkey);
465                 ret = NT_STATUS_NONE_MAPPED;
466                 goto done;
467         }
468
469         /* check it is not negative */
470         if (strcmp("IDMAP/NEGATIVE", endptr+1) != 0) {
471                 
472                 DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, "
473                            "timeout = %s", t > now ? "valid" :
474                            "expired", idkey, endptr+1, ctime(&t)));
475
476                 /* this call if successful will also mark the entry as mapped */
477                 ret = idmap_cache_fill_map(id, endptr+1);
478                 if ( ! NT_STATUS_IS_OK(ret)) {
479                         /* if not valid form delete the entry */
480                         tdb_delete_bystring(cache->tdb, idkey);
481                         ret = NT_STATUS_NONE_MAPPED;
482                         goto done;
483                 }
484
485                 /* here ret == NT_STATUS_OK and id->mapped = ID_MAPPED */
486
487                 if (t <= now) {
488                         /* If we've been told to be offline - stay in
489                            that state... */
490                         if ( IS_DOMAIN_OFFLINE(our_domain) ) {
491                                 DEBUG(10,("idmap_cache_map_sid: idmap is offline\n"));
492                                 goto done;
493                         }
494
495                         /* We're expired, set an error code
496                            for upper layer */
497                         ret = NT_STATUS_SYNCHRONIZATION_REQUIRED;
498                 }
499
500                 goto done;
501         }
502         
503         /* Was a negative cache hit */
504
505         /* Ignore the negative cache when offline */
506
507         if ( IS_DOMAIN_OFFLINE(our_domain) ) {
508                 DEBUG(10,("idmap_cache_map_sid: idmap is offline\n"));
509                 ret = NT_STATUS_NONE_MAPPED;
510                 
511                 goto done;
512         }
513
514         /* Process the negative cache hit */
515
516                 if (t <= now) {
517                 /* We're expired.  Return not mapped */
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