s3:quota: clean up and consolidate the xfs quota checks and defines
[kai/samba.git] / source3 / lib / idmap_cache.c
1 /*
2    Unix SMB/CIFS implementation.
3    ID Mapping Cache
4
5    Copyright (C) Volker Lendecke        2008
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
19
20 #include "includes.h"
21 #include "idmap_cache.h"
22 #include "../libcli/security/security.h"
23 #include "../librpc/gen_ndr/idmap.h"
24
25 /**
26  * Find a sid2xid mapping
27  * @param[in] sid               the sid to map
28  * @param[out] id               where to put the result
29  * @param[out] expired          is the cache entry expired?
30  * @retval Was anything in the cache at all?
31  *
32  * If id->id == -1 this was a negative mapping.
33  */
34
35 bool idmap_cache_find_sid2unixid(const struct dom_sid *sid, struct unixid *id,
36                                  bool *expired)
37 {
38         fstring sidstr;
39         char *key;
40         char *value = NULL;
41         char *endptr;
42         time_t timeout;
43         bool ret;
44         struct unixid tmp_id;
45
46         key = talloc_asprintf(talloc_tos(), "IDMAP/SID2XID/%s",
47                               sid_to_fstring(sidstr, sid));
48         if (key == NULL) {
49                 return false;
50         }
51         ret = gencache_get(key, &value, &timeout);
52         if (!ret) {
53                 goto done;
54         }
55
56         DEBUG(10, ("Parsing value for key [%s]: value=[%s]\n", key, value));
57
58         if (value[0] == '\0') {
59                 DEBUG(0, ("Failed to parse value for key [%s]: "
60                           "value is empty\n", key));
61                 ret = false;
62                 goto done;
63         }
64
65         tmp_id.id = strtol(value, &endptr, 10);
66
67         if ((value == endptr) && (tmp_id.id == 0)) {
68                 DEBUG(0, ("Failed to parse value for key [%s]: value[%s] does "
69                           "not start with a number\n", key, value));
70                 ret = false;
71                 goto done;
72         }
73
74         DEBUG(10, ("Parsing value for key [%s]: id=[%llu], endptr=[%s]\n",
75                    key, (unsigned long long)tmp_id.id, endptr));
76
77         ret = (*endptr == ':');
78         if (ret) {
79                 switch (endptr[1]) {
80                 case 'U':
81                         tmp_id.type = ID_TYPE_UID;
82                         break;
83
84                 case 'G':
85                         tmp_id.type = ID_TYPE_GID;
86                         break;
87
88                 case 'B':
89                         tmp_id.type = ID_TYPE_BOTH;
90                         break;
91
92                 case 'N':
93                         tmp_id.type = ID_TYPE_NOT_SPECIFIED;
94                         break;
95
96                 case '\0':
97                         DEBUG(0, ("FAILED to parse value for key [%s] "
98                                   "(id=[%llu], endptr=[%s]): "
99                                   "no type character after colon\n",
100                                   key, (unsigned long long)tmp_id.id, endptr));
101                         ret = false;
102                         goto done;
103                 default:
104                         DEBUG(0, ("FAILED to parse value for key [%s] "
105                                   "(id=[%llu], endptr=[%s]): "
106                                   "illegal type character '%c'\n",
107                                   key, (unsigned long long)tmp_id.id, endptr,
108                                   endptr[1]));
109                         ret = false;
110                         goto done;
111                 }
112                 if (endptr[2] != '\0') {
113                         DEBUG(0, ("FAILED to parse value for key [%s] "
114                                   "(id=[%llu], endptr=[%s]): "
115                                   "more than 1 type character after colon\n",
116                                   key, (unsigned long long)tmp_id.id, endptr));
117                         ret = false;
118                         goto done;
119                 }
120
121                 *id = tmp_id;
122                 *expired = (timeout <= time(NULL));
123         } else {
124                 DEBUG(0, ("FAILED to parse value for key [%s] (value=[%s]): "
125                           "colon missing after id=[%llu]\n",
126                           key, value, (unsigned long long)tmp_id.id));
127         }
128
129 done:
130         TALLOC_FREE(key);
131         SAFE_FREE(value);
132         return ret;
133 }
134
135 /**
136  * Find a sid2uid mapping
137  * @param[in] sid               the sid to map
138  * @param[out] puid             where to put the result
139  * @param[out] expired          is the cache entry expired?
140  * @retval Was anything in the cache at all?
141  *
142  * If *puid == -1 this was a negative mapping.
143  */
144
145 bool idmap_cache_find_sid2uid(const struct dom_sid *sid, uid_t *puid,
146                               bool *expired)
147 {
148         bool ret;
149         struct unixid id;
150         ret = idmap_cache_find_sid2unixid(sid, &id, expired);
151         if (!ret) {
152                 return false;
153         }
154
155         if (id.type == ID_TYPE_BOTH || id.type == ID_TYPE_UID) {
156                 *puid = id.id;
157         } else {
158                 *puid = -1;
159         }
160         return true;
161 }
162
163 /**
164  * Find a sid2gid mapping
165  * @param[in] sid               the sid to map
166  * @param[out] pgid             where to put the result
167  * @param[out] expired          is the cache entry expired?
168  * @retval Was anything in the cache at all?
169  *
170  * If *pgid == -1 this was a negative mapping.
171  */
172
173 bool idmap_cache_find_sid2gid(const struct dom_sid *sid, gid_t *pgid,
174                               bool *expired)
175 {
176         bool ret;
177         struct unixid id;
178         ret = idmap_cache_find_sid2unixid(sid, &id, expired);
179         if (!ret) {
180                 return false;
181         }
182
183         if (id.type == ID_TYPE_BOTH || id.type == ID_TYPE_GID) {
184                 *pgid = id.id;
185         } else {
186                 *pgid = -1;
187         }
188         return true;
189 }
190
191 /**
192  * Find a uid2sid mapping
193  * @param[in] uid               the uid to map
194  * @param[out] sid              where to put the result
195  * @param[out] expired          is the cache entry expired?
196  * @retval Was anything in the cache at all?
197  *
198  * If "is_null_sid(sid)", this was a negative mapping.
199  */
200
201 bool idmap_cache_find_uid2sid(uid_t uid, struct dom_sid *sid, bool *expired)
202 {
203         char *key;
204         char *value;
205         time_t timeout;
206         bool ret = true;
207
208         key = talloc_asprintf(talloc_tos(), "IDMAP/UID2SID/%d", (int)uid);
209         if (key == NULL) {
210                 return false;
211         }
212         ret = gencache_get(key, &value, &timeout);
213         TALLOC_FREE(key);
214         if (!ret) {
215                 return false;
216         }
217         ZERO_STRUCTP(sid);
218         if (value[0] != '-') {
219                 ret = string_to_sid(sid, value);
220         }
221         SAFE_FREE(value);
222         if (ret) {
223                 *expired = (timeout <= time(NULL));
224         }
225         return ret;
226 }
227
228 /**
229  * Find a gid2sid mapping
230  * @param[in] gid               the gid to map
231  * @param[out] sid              where to put the result
232  * @param[out] expired          is the cache entry expired?
233  * @retval Was anything in the cache at all?
234  *
235  * If "is_null_sid(sid)", this was a negative mapping.
236  */
237
238 bool idmap_cache_find_gid2sid(gid_t gid, struct dom_sid *sid, bool *expired)
239 {
240         char *key;
241         char *value;
242         time_t timeout;
243         bool ret = true;
244
245         key = talloc_asprintf(talloc_tos(), "IDMAP/GID2SID/%d", (int)gid);
246         if (key == NULL) {
247                 return false;
248         }
249         ret = gencache_get(key, &value, &timeout);
250         TALLOC_FREE(key);
251         if (!ret) {
252                 return false;
253         }
254         ZERO_STRUCTP(sid);
255         if (value[0] != '-') {
256                 ret = string_to_sid(sid, value);
257         }
258         SAFE_FREE(value);
259         if (ret) {
260                 *expired = (timeout <= time(NULL));
261         }
262         return ret;
263 }
264
265 /**
266  * Store a mapping in the idmap cache
267  * @param[in] sid               the sid to map
268  * @param[in] gid               the gid to map
269  *
270  * If both parameters are valid values, then a positive mapping in both
271  * directions is stored. If "is_null_sid(sid)" is true, then this will be a
272  * negative mapping of gid, we want to cache that for this gid we could not
273  * find anything. Likewise if "gid==-1", then we want to cache that we did not
274  * find a mapping for the sid passed here.
275  */
276
277 void idmap_cache_set_sid2unixid(const struct dom_sid *sid, struct unixid *unix_id)
278 {
279         time_t now = time(NULL);
280         time_t timeout;
281         fstring sidstr, key, value;
282
283         if (!is_null_sid(sid)) {
284                 fstr_sprintf(key, "IDMAP/SID2XID/%s",
285                              sid_to_fstring(sidstr, sid));
286                 switch (unix_id->type) {
287                 case ID_TYPE_UID:
288                         fstr_sprintf(value, "%d:U", (int)unix_id->id);
289                         break;
290                 case ID_TYPE_GID:
291                         fstr_sprintf(value, "%d:G", (int)unix_id->id);
292                         break;
293                 case ID_TYPE_BOTH:
294                         fstr_sprintf(value, "%d:B", (int)unix_id->id);
295                         break;
296                 case ID_TYPE_NOT_SPECIFIED:
297                         fstr_sprintf(value, "%d:N", (int)unix_id->id);
298                         break;
299                 default:
300                         return;
301                 }
302                 timeout = (unix_id->id == -1)
303                         ? lp_idmap_negative_cache_time()
304                         : lp_idmap_cache_time();
305                 gencache_set(key, value, now + timeout);
306         }
307         if (unix_id->id != -1) {
308                 if (is_null_sid(sid)) {
309                         /* negative gid mapping */
310                         fstrcpy(value, "-");
311                         timeout = lp_idmap_negative_cache_time();
312                 }
313                 else {
314                         sid_to_fstring(value, sid);
315                         timeout = lp_idmap_cache_time();
316                 }
317                 switch (unix_id->type) {
318                 case ID_TYPE_BOTH:
319                         fstr_sprintf(key, "IDMAP/UID2SID/%d", (int)unix_id->id);
320                         gencache_set(key, value, now + timeout);
321                         fstr_sprintf(key, "IDMAP/GID2SID/%d", (int)unix_id->id);
322                         gencache_set(key, value, now + timeout);
323                         return;
324
325                 case ID_TYPE_UID:
326                         fstr_sprintf(key, "IDMAP/UID2SID/%d", (int)unix_id->id);
327                         break;
328
329                 case ID_TYPE_GID:
330                         fstr_sprintf(key, "IDMAP/GID2SID/%d", (int)unix_id->id);
331                         break;
332
333                 default:
334                         return;
335                 }
336                 gencache_set(key, value, now + timeout);
337         }
338 }
339
340 /**
341  * Store a mapping in the idmap cache
342  * @param[in] sid               the sid to map
343  * @param[in] uid               the uid to map
344  *
345  * If both parameters are valid values, then a positive mapping in both
346  * directions is stored. If "is_null_sid(sid)" is true, then this will be a
347  * negative mapping of uid, we want to cache that for this uid we could not
348  * find anything. Likewise if "uid==-1", then we want to cache that we did not
349  * find a mapping for the sid passed here.
350  */
351
352 void idmap_cache_set_sid2uid(const struct dom_sid *sid, uid_t uid)
353 {
354         struct unixid id;
355         id.type = ID_TYPE_UID;
356         id.id = uid;
357
358         if (uid == -1) {
359                 uid_t tmp_gid;
360                 bool expired;
361                 /* If we were asked to invalidate this SID -> UID
362                  * mapping, it was because we found out that this was
363                  * not a UID at all.  Do not overwrite a valid GID or
364                  * BOTH mapping */
365                 if (idmap_cache_find_sid2gid(sid, &tmp_gid, &expired)) {
366                         if (!expired) {
367                                 return;
368                         }
369                 }
370         }
371
372         idmap_cache_set_sid2unixid(sid, &id);
373         return;
374 }
375
376 /**
377  * Store a mapping in the idmap cache
378  * @param[in] sid               the sid to map
379  * @param[in] gid               the gid to map
380  *
381  * If both parameters are valid values, then a positive mapping in both
382  * directions is stored. If "is_null_sid(sid)" is true, then this will be a
383  * negative mapping of gid, we want to cache that for this gid we could not
384  * find anything. Likewise if "gid==-1", then we want to cache that we did not
385  * find a mapping for the sid passed here.
386  */
387
388 void idmap_cache_set_sid2gid(const struct dom_sid *sid, gid_t gid)
389 {
390         struct unixid id;
391         id.type = ID_TYPE_GID;
392         id.id = gid;
393
394         if (gid == -1) {
395                 uid_t tmp_uid;
396                 bool expired;
397                 /* If we were asked to invalidate this SID -> GID
398                  * mapping, it was because we found out that this was
399                  * not a GID at all.  Do not overwrite a valid UID or
400                  * BOTH mapping */
401                 if (idmap_cache_find_sid2uid(sid, &tmp_uid, &expired)) {
402                         if (!expired) {
403                                 return;
404                         }
405                 }
406         }
407
408         idmap_cache_set_sid2unixid(sid, &id);
409         return;
410 }
411
412 static char* key_xid2sid_str(TALLOC_CTX* mem_ctx, char t, const char* id) {
413         return talloc_asprintf(mem_ctx, "IDMAP/%cID2SID/%s", t, id);
414 }
415
416 static char* key_xid2sid(TALLOC_CTX* mem_ctx, char t, int id) {
417         char str[32];
418         snprintf(str, sizeof(str), "%d", id);
419         return key_xid2sid_str(mem_ctx, t, str);
420 }
421
422 static char* key_sid2xid_str(TALLOC_CTX* mem_ctx, const char* id) {
423         return talloc_asprintf(mem_ctx, "IDMAP/SID2XID/%s", id);
424 }
425
426 static bool idmap_cache_del_xid(char t, int xid)
427 {
428         TALLOC_CTX* mem_ctx = talloc_stackframe();
429         const char* key = key_xid2sid(mem_ctx, t, xid);
430         char* sid_str = NULL;
431         time_t timeout;
432         bool ret = true;
433
434         if (!gencache_get(key, &sid_str, &timeout)) {
435                 DEBUG(3, ("no entry: %s\n", key));
436                 ret = false;
437                 goto done;
438         }
439
440         if (sid_str[0] != '-') {
441                 const char* sid_key = key_sid2xid_str(mem_ctx, sid_str);
442                 if (!gencache_del(sid_key)) {
443                         DEBUG(2, ("failed to delete: %s\n", sid_key));
444                         ret = false;
445                 } else {
446                         DEBUG(5, ("delete: %s\n", sid_key));
447                 }
448
449         }
450
451         if (!gencache_del(key)) {
452                 DEBUG(1, ("failed to delete: %s\n", key));
453                 ret = false;
454         } else {
455                 DEBUG(5, ("delete: %s\n", key));
456         }
457
458 done:
459         talloc_free(mem_ctx);
460         return ret;
461 }
462
463 bool idmap_cache_del_uid(uid_t uid) {
464         return idmap_cache_del_xid('U', uid);
465 }
466
467 bool idmap_cache_del_gid(gid_t gid) {
468         return idmap_cache_del_xid('G', gid);
469 }
470
471 bool idmap_cache_del_sid(const struct dom_sid *sid)
472 {
473         TALLOC_CTX* mem_ctx = talloc_stackframe();
474         bool ret = true;
475         bool expired;
476         struct unixid id;
477         const char *sid_key;
478
479         if (!idmap_cache_find_sid2unixid(sid, &id, &expired)) {
480                 ret = false;
481                 goto done;
482         }
483
484         if (id.id != -1) {
485                 switch (id.type) {
486                 case ID_TYPE_BOTH:
487                         idmap_cache_del_xid('U', id.id);
488                         idmap_cache_del_xid('G', id.id);
489                         break;
490                 case ID_TYPE_UID:
491                         idmap_cache_del_xid('U', id.id);
492                         break;
493                 case ID_TYPE_GID:
494                         idmap_cache_del_xid('G', id.id);
495                         break;
496                 default:
497                         break;
498                 }
499         }
500
501         sid_key = key_sid2xid_str(mem_ctx, dom_sid_string(mem_ctx, sid));
502         if (sid_key == NULL) {
503                 return false;
504         }
505         /* If the mapping was symmetric, then this should fail */
506         gencache_del(sid_key);
507 done:
508         talloc_free(mem_ctx);
509         return ret;
510 }