idmap_cache: Use an fstring instead of talloc_asprintf
[Samba.git] / source3 / lib / idmap_cache.c
blob4564bebfa36c5adce60f11571f7551922565c976
1 /*
2 Unix SMB/CIFS implementation.
3 ID Mapping Cache
5 Copyright (C) Volker Lendecke 2008
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.
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.
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/>.*/
20 #include "includes.h"
21 #include "idmap_cache.h"
22 #include "../libcli/security/security.h"
23 #include "../librpc/gen_ndr/idmap.h"
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?
32 * If id->id == -1 this was a negative mapping.
35 bool idmap_cache_find_sid2unixid(const struct dom_sid *sid, struct unixid *id,
36 bool *expired)
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;
46 key = talloc_asprintf(talloc_tos(), "IDMAP/SID2XID/%s",
47 sid_to_fstring(sidstr, sid));
48 if (key == NULL) {
49 return false;
51 ret = gencache_get(key, talloc_tos(), &value, &timeout);
52 if (!ret) {
53 goto done;
56 DEBUG(10, ("Parsing value for key [%s]: value=[%s]\n", key, value));
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;
65 tmp_id.id = strtol(value, &endptr, 10);
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;
74 DEBUG(10, ("Parsing value for key [%s]: id=[%llu], endptr=[%s]\n",
75 key, (unsigned long long)tmp_id.id, endptr));
77 ret = (*endptr == ':');
78 if (ret) {
79 switch (endptr[1]) {
80 case 'U':
81 tmp_id.type = ID_TYPE_UID;
82 break;
84 case 'G':
85 tmp_id.type = ID_TYPE_GID;
86 break;
88 case 'B':
89 tmp_id.type = ID_TYPE_BOTH;
90 break;
92 case 'N':
93 tmp_id.type = ID_TYPE_NOT_SPECIFIED;
94 break;
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;
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;
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));
129 done:
130 TALLOC_FREE(key);
131 TALLOC_FREE(value);
132 return ret;
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?
142 * If *puid == -1 this was a negative mapping.
145 bool idmap_cache_find_sid2uid(const struct dom_sid *sid, uid_t *puid,
146 bool *expired)
148 bool ret;
149 struct unixid id;
150 ret = idmap_cache_find_sid2unixid(sid, &id, expired);
151 if (!ret) {
152 return false;
155 if (id.type == ID_TYPE_BOTH || id.type == ID_TYPE_UID) {
156 *puid = id.id;
157 } else {
158 *puid = -1;
160 return true;
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?
170 * If *pgid == -1 this was a negative mapping.
173 bool idmap_cache_find_sid2gid(const struct dom_sid *sid, gid_t *pgid,
174 bool *expired)
176 bool ret;
177 struct unixid id;
178 ret = idmap_cache_find_sid2unixid(sid, &id, expired);
179 if (!ret) {
180 return false;
183 if (id.type == ID_TYPE_BOTH || id.type == ID_TYPE_GID) {
184 *pgid = id.id;
185 } else {
186 *pgid = -1;
188 return true;
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?
198 * If "is_null_sid(sid)", this was a negative mapping.
201 bool idmap_cache_find_uid2sid(uid_t uid, struct dom_sid *sid, bool *expired)
203 fstring key;
204 char *value;
205 time_t timeout;
206 bool ret = true;
208 fstr_sprintf(key, "IDMAP/UID2SID/%d", (int)uid);
210 ret = gencache_get(key, talloc_tos(), &value, &timeout);
211 if (!ret) {
212 return false;
214 ZERO_STRUCTP(sid);
215 if (value[0] != '-') {
216 ret = string_to_sid(sid, value);
218 TALLOC_FREE(value);
219 if (ret) {
220 *expired = (timeout <= time(NULL));
222 return ret;
226 * Find a gid2sid mapping
227 * @param[in] gid the gid to map
228 * @param[out] sid where to put the result
229 * @param[out] expired is the cache entry expired?
230 * @retval Was anything in the cache at all?
232 * If "is_null_sid(sid)", this was a negative mapping.
235 bool idmap_cache_find_gid2sid(gid_t gid, struct dom_sid *sid, bool *expired)
237 fstring key;
238 char *value;
239 time_t timeout;
240 bool ret = true;
242 fstr_sprintf(key, "IDMAP/GID2SID/%d", (int)gid);
244 ret = gencache_get(key, talloc_tos(), &value, &timeout);
245 if (!ret) {
246 return false;
248 ZERO_STRUCTP(sid);
249 if (value[0] != '-') {
250 ret = string_to_sid(sid, value);
252 TALLOC_FREE(value);
253 if (ret) {
254 *expired = (timeout <= time(NULL));
256 return ret;
260 * Store a mapping in the idmap cache
261 * @param[in] sid the sid to map
262 * @param[in] gid the gid to map
264 * If both parameters are valid values, then a positive mapping in both
265 * directions is stored. If "is_null_sid(sid)" is true, then this will be a
266 * negative mapping of gid, we want to cache that for this gid we could not
267 * find anything. Likewise if "gid==-1", then we want to cache that we did not
268 * find a mapping for the sid passed here.
271 void idmap_cache_set_sid2unixid(const struct dom_sid *sid, struct unixid *unix_id)
273 time_t now = time(NULL);
274 time_t timeout;
275 fstring sidstr, key, value;
277 if (!is_null_sid(sid)) {
278 fstr_sprintf(key, "IDMAP/SID2XID/%s",
279 sid_to_fstring(sidstr, sid));
280 switch (unix_id->type) {
281 case ID_TYPE_UID:
282 fstr_sprintf(value, "%d:U", (int)unix_id->id);
283 break;
284 case ID_TYPE_GID:
285 fstr_sprintf(value, "%d:G", (int)unix_id->id);
286 break;
287 case ID_TYPE_BOTH:
288 fstr_sprintf(value, "%d:B", (int)unix_id->id);
289 break;
290 case ID_TYPE_NOT_SPECIFIED:
291 fstr_sprintf(value, "%d:N", (int)unix_id->id);
292 break;
293 default:
294 return;
296 timeout = (unix_id->id == -1)
297 ? lp_idmap_negative_cache_time()
298 : lp_idmap_cache_time();
299 gencache_set(key, value, now + timeout);
301 if (unix_id->id != -1) {
302 if (is_null_sid(sid)) {
303 /* negative gid mapping */
304 fstrcpy(value, "-");
305 timeout = lp_idmap_negative_cache_time();
307 else {
308 sid_to_fstring(value, sid);
309 timeout = lp_idmap_cache_time();
311 switch (unix_id->type) {
312 case ID_TYPE_BOTH:
313 fstr_sprintf(key, "IDMAP/UID2SID/%d", (int)unix_id->id);
314 gencache_set(key, value, now + timeout);
315 fstr_sprintf(key, "IDMAP/GID2SID/%d", (int)unix_id->id);
316 gencache_set(key, value, now + timeout);
317 return;
319 case ID_TYPE_UID:
320 fstr_sprintf(key, "IDMAP/UID2SID/%d", (int)unix_id->id);
321 break;
323 case ID_TYPE_GID:
324 fstr_sprintf(key, "IDMAP/GID2SID/%d", (int)unix_id->id);
325 break;
327 default:
328 return;
330 gencache_set(key, value, now + timeout);
335 * Store a mapping in the idmap cache
336 * @param[in] sid the sid to map
337 * @param[in] uid the uid to map
339 * If both parameters are valid values, then a positive mapping in both
340 * directions is stored. If "is_null_sid(sid)" is true, then this will be a
341 * negative mapping of uid, we want to cache that for this uid we could not
342 * find anything. Likewise if "uid==-1", then we want to cache that we did not
343 * find a mapping for the sid passed here.
346 void idmap_cache_set_sid2uid(const struct dom_sid *sid, uid_t uid)
348 struct unixid id;
349 id.type = ID_TYPE_UID;
350 id.id = uid;
352 if (uid == -1) {
353 uid_t tmp_gid;
354 bool expired;
355 /* If we were asked to invalidate this SID -> UID
356 * mapping, it was because we found out that this was
357 * not a UID at all. Do not overwrite a valid GID or
358 * BOTH mapping */
359 if (idmap_cache_find_sid2gid(sid, &tmp_gid, &expired)) {
360 if (!expired) {
361 return;
366 idmap_cache_set_sid2unixid(sid, &id);
367 return;
371 * Store a mapping in the idmap cache
372 * @param[in] sid the sid to map
373 * @param[in] gid the gid to map
375 * If both parameters are valid values, then a positive mapping in both
376 * directions is stored. If "is_null_sid(sid)" is true, then this will be a
377 * negative mapping of gid, we want to cache that for this gid we could not
378 * find anything. Likewise if "gid==-1", then we want to cache that we did not
379 * find a mapping for the sid passed here.
382 void idmap_cache_set_sid2gid(const struct dom_sid *sid, gid_t gid)
384 struct unixid id;
385 id.type = ID_TYPE_GID;
386 id.id = gid;
388 if (gid == -1) {
389 uid_t tmp_uid;
390 bool expired;
391 /* If we were asked to invalidate this SID -> GID
392 * mapping, it was because we found out that this was
393 * not a GID at all. Do not overwrite a valid UID or
394 * BOTH mapping */
395 if (idmap_cache_find_sid2uid(sid, &tmp_uid, &expired)) {
396 if (!expired) {
397 return;
402 idmap_cache_set_sid2unixid(sid, &id);
403 return;
406 static char* key_xid2sid_str(TALLOC_CTX* mem_ctx, char t, const char* id) {
407 return talloc_asprintf(mem_ctx, "IDMAP/%cID2SID/%s", t, id);
410 static char* key_xid2sid(TALLOC_CTX* mem_ctx, char t, int id) {
411 char str[32];
412 snprintf(str, sizeof(str), "%d", id);
413 return key_xid2sid_str(mem_ctx, t, str);
416 static char* key_sid2xid_str(TALLOC_CTX* mem_ctx, const char* id) {
417 return talloc_asprintf(mem_ctx, "IDMAP/SID2XID/%s", id);
420 static bool idmap_cache_del_xid(char t, int xid)
422 TALLOC_CTX* mem_ctx = talloc_stackframe();
423 const char* key = key_xid2sid(mem_ctx, t, xid);
424 char* sid_str = NULL;
425 time_t timeout;
426 bool ret = true;
428 if (!gencache_get(key, mem_ctx, &sid_str, &timeout)) {
429 DEBUG(3, ("no entry: %s\n", key));
430 ret = false;
431 goto done;
434 if (sid_str[0] != '-') {
435 const char* sid_key = key_sid2xid_str(mem_ctx, sid_str);
436 if (!gencache_del(sid_key)) {
437 DEBUG(2, ("failed to delete: %s\n", sid_key));
438 ret = false;
439 } else {
440 DEBUG(5, ("delete: %s\n", sid_key));
445 if (!gencache_del(key)) {
446 DEBUG(1, ("failed to delete: %s\n", key));
447 ret = false;
448 } else {
449 DEBUG(5, ("delete: %s\n", key));
452 done:
453 talloc_free(mem_ctx);
454 return ret;
457 bool idmap_cache_del_uid(uid_t uid) {
458 return idmap_cache_del_xid('U', uid);
461 bool idmap_cache_del_gid(gid_t gid) {
462 return idmap_cache_del_xid('G', gid);
465 bool idmap_cache_del_sid(const struct dom_sid *sid)
467 TALLOC_CTX* mem_ctx = talloc_stackframe();
468 bool ret = true;
469 bool expired;
470 struct unixid id;
471 const char *sid_key;
473 if (!idmap_cache_find_sid2unixid(sid, &id, &expired)) {
474 ret = false;
475 goto done;
478 if (id.id != -1) {
479 switch (id.type) {
480 case ID_TYPE_BOTH:
481 idmap_cache_del_xid('U', id.id);
482 idmap_cache_del_xid('G', id.id);
483 break;
484 case ID_TYPE_UID:
485 idmap_cache_del_xid('U', id.id);
486 break;
487 case ID_TYPE_GID:
488 idmap_cache_del_xid('G', id.id);
489 break;
490 default:
491 break;
495 sid_key = key_sid2xid_str(mem_ctx, dom_sid_string(mem_ctx, sid));
496 if (sid_key == NULL) {
497 return false;
499 /* If the mapping was symmetric, then this should fail */
500 gencache_del(sid_key);
501 done:
502 talloc_free(mem_ctx);
503 return ret;