s3: Fix Coverity ID 242704 Dereference before null check
[Samba/gebeck_regimport.git] / source3 / lib / idmap_cache.c
blob3669e93e58facdb59715d587f9940b9bc099726d
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;
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, &value, &timeout);
52 if (!ret) {
53 TALLOC_FREE(key);
54 return false;
56 tmp_id.id = strtol(value, &endptr, 10);
57 DEBUG(10, ("Parsing result of %s, endptr=%s, id=%llu\n",
58 key, endptr, (unsigned long long)tmp_id.id));
60 ret = (*endptr == ':');
61 if (ret) {
62 switch (endptr[1]) {
63 case 'U':
64 tmp_id.type = ID_TYPE_UID;
65 break;
67 case 'G':
68 tmp_id.type = ID_TYPE_GID;
69 break;
71 case 'B':
72 tmp_id.type = ID_TYPE_BOTH;
73 break;
75 case '\0':
76 default:
77 TALLOC_FREE(key);
78 SAFE_FREE(value);
79 DEBUG(0, ("FAILED Parsing result of %s, endptr=%s, id=%llu\n", key, endptr, (unsigned long long)tmp_id.id));
80 return false;
82 if (endptr[2] != '\0') {
83 TALLOC_FREE(key);
84 SAFE_FREE(value);
85 DEBUG(0, ("FAILED (2) Parsing result of %s, endptr=%s, id=%llu\n", key, endptr, (unsigned long long)tmp_id.id));
86 return false;
89 *id = tmp_id;
90 *expired = (timeout <= time(NULL));
91 } else {
92 DEBUG(0, ("FAILED (3) Parsing result of %s, value=%s\n", key, value));
94 TALLOC_FREE(key);
95 SAFE_FREE(value);
96 return ret;
99 /**
100 * Find a sid2uid mapping
101 * @param[in] sid the sid to map
102 * @param[out] puid where to put the result
103 * @param[out] expired is the cache entry expired?
104 * @retval Was anything in the cache at all?
106 * If *puid == -1 this was a negative mapping.
109 bool idmap_cache_find_sid2uid(const struct dom_sid *sid, uid_t *puid,
110 bool *expired)
112 bool ret;
113 struct unixid id;
114 ret = idmap_cache_find_sid2unixid(sid, &id, expired);
115 if (!ret) {
116 return false;
119 if (id.type == ID_TYPE_BOTH || id.type == ID_TYPE_UID) {
120 *puid = id.id;
121 } else {
122 *puid = -1;
124 return true;
128 * Find a sid2gid mapping
129 * @param[in] sid the sid to map
130 * @param[out] pgid where to put the result
131 * @param[out] expired is the cache entry expired?
132 * @retval Was anything in the cache at all?
134 * If *pgid == -1 this was a negative mapping.
137 bool idmap_cache_find_sid2gid(const struct dom_sid *sid, gid_t *pgid,
138 bool *expired)
140 bool ret;
141 struct unixid id;
142 ret = idmap_cache_find_sid2unixid(sid, &id, expired);
143 if (!ret) {
144 return false;
147 if (id.type == ID_TYPE_BOTH || id.type == ID_TYPE_GID) {
148 *pgid = id.id;
149 } else {
150 *pgid = -1;
152 return true;
156 * Find a uid2sid mapping
157 * @param[in] uid the uid to map
158 * @param[out] sid where to put the result
159 * @param[out] expired is the cache entry expired?
160 * @retval Was anything in the cache at all?
162 * If "is_null_sid(sid)", this was a negative mapping.
165 bool idmap_cache_find_uid2sid(uid_t uid, struct dom_sid *sid, bool *expired)
167 char *key;
168 char *value;
169 time_t timeout;
170 bool ret = true;
172 key = talloc_asprintf(talloc_tos(), "IDMAP/UID2SID/%d", (int)uid);
173 if (key == NULL) {
174 return false;
176 ret = gencache_get(key, &value, &timeout);
177 TALLOC_FREE(key);
178 if (!ret) {
179 return false;
181 ZERO_STRUCTP(sid);
182 if (value[0] != '-') {
183 ret = string_to_sid(sid, value);
185 SAFE_FREE(value);
186 if (ret) {
187 *expired = (timeout <= time(NULL));
189 return ret;
193 * Find a gid2sid mapping
194 * @param[in] gid the gid to map
195 * @param[out] sid where to put the result
196 * @param[out] expired is the cache entry expired?
197 * @retval Was anything in the cache at all?
199 * If "is_null_sid(sid)", this was a negative mapping.
202 bool idmap_cache_find_gid2sid(gid_t gid, struct dom_sid *sid, bool *expired)
204 char *key;
205 char *value;
206 time_t timeout;
207 bool ret = true;
209 key = talloc_asprintf(talloc_tos(), "IDMAP/GID2SID/%d", (int)gid);
210 if (key == NULL) {
211 return false;
213 ret = gencache_get(key, &value, &timeout);
214 TALLOC_FREE(key);
215 if (!ret) {
216 return false;
218 ZERO_STRUCTP(sid);
219 if (value[0] != '-') {
220 ret = string_to_sid(sid, value);
222 SAFE_FREE(value);
223 if (ret) {
224 *expired = (timeout <= time(NULL));
226 return ret;
230 * Store a mapping in the idmap cache
231 * @param[in] sid the sid to map
232 * @param[in] gid the gid to map
234 * If both parameters are valid values, then a positive mapping in both
235 * directions is stored. If "is_null_sid(sid)" is true, then this will be a
236 * negative mapping of gid, we want to cache that for this gid we could not
237 * find anything. Likewise if "gid==-1", then we want to cache that we did not
238 * find a mapping for the sid passed here.
241 void idmap_cache_set_sid2unixid(const struct dom_sid *sid, struct unixid *unix_id)
243 time_t now = time(NULL);
244 time_t timeout;
245 fstring sidstr, key, value;
247 if (!is_null_sid(sid)) {
248 fstr_sprintf(key, "IDMAP/SID2XID/%s",
249 sid_to_fstring(sidstr, sid));
250 switch (unix_id->type) {
251 case ID_TYPE_UID:
252 fstr_sprintf(value, "%d:U", (int)unix_id->id);
253 break;
254 case ID_TYPE_GID:
255 fstr_sprintf(value, "%d:G", (int)unix_id->id);
256 break;
257 case ID_TYPE_BOTH:
258 fstr_sprintf(value, "%d:B", (int)unix_id->id);
259 break;
260 default:
261 return;
263 timeout = (unix_id->id == -1)
264 ? lp_idmap_negative_cache_time()
265 : lp_idmap_cache_time();
266 gencache_set(key, value, now + timeout);
268 if (unix_id->id != -1) {
269 if (is_null_sid(sid)) {
270 /* negative gid mapping */
271 fstrcpy(value, "-");
272 timeout = lp_idmap_negative_cache_time();
274 else {
275 sid_to_fstring(value, sid);
276 timeout = lp_idmap_cache_time();
278 switch (unix_id->type) {
279 case ID_TYPE_BOTH:
280 fstr_sprintf(key, "IDMAP/UID2SID/%d", (int)unix_id->id);
281 gencache_set(key, value, now + timeout);
282 fstr_sprintf(key, "IDMAP/GID2SID/%d", (int)unix_id->id);
283 gencache_set(key, value, now + timeout);
284 return;
286 case ID_TYPE_UID:
287 fstr_sprintf(key, "IDMAP/UID2SID/%d", (int)unix_id->id);
288 break;
290 case ID_TYPE_GID:
291 fstr_sprintf(key, "IDMAP/GID2SID/%d", (int)unix_id->id);
292 break;
294 default:
295 return;
297 gencache_set(key, value, now + timeout);
302 * Store a mapping in the idmap cache
303 * @param[in] sid the sid to map
304 * @param[in] uid the uid to map
306 * If both parameters are valid values, then a positive mapping in both
307 * directions is stored. If "is_null_sid(sid)" is true, then this will be a
308 * negative mapping of uid, we want to cache that for this uid we could not
309 * find anything. Likewise if "uid==-1", then we want to cache that we did not
310 * find a mapping for the sid passed here.
313 void idmap_cache_set_sid2uid(const struct dom_sid *sid, uid_t uid)
315 struct unixid id;
316 id.type = ID_TYPE_UID;
317 id.id = uid;
319 if (uid == -1) {
320 uid_t tmp_gid;
321 bool expired;
322 /* If we were asked to invalidate this SID -> UID
323 * mapping, it was because we found out that this was
324 * not a UID at all. Do not overwrite a valid GID or
325 * BOTH mapping */
326 if (idmap_cache_find_sid2gid(sid, &tmp_gid, &expired)) {
327 if (!expired) {
328 return;
333 idmap_cache_set_sid2unixid(sid, &id);
334 return;
338 * Store a mapping in the idmap cache
339 * @param[in] sid the sid to map
340 * @param[in] gid the gid to map
342 * If both parameters are valid values, then a positive mapping in both
343 * directions is stored. If "is_null_sid(sid)" is true, then this will be a
344 * negative mapping of gid, we want to cache that for this gid we could not
345 * find anything. Likewise if "gid==-1", then we want to cache that we did not
346 * find a mapping for the sid passed here.
349 void idmap_cache_set_sid2gid(const struct dom_sid *sid, gid_t gid)
351 struct unixid id;
352 id.type = ID_TYPE_GID;
353 id.id = gid;
355 if (gid == -1) {
356 uid_t tmp_uid;
357 bool expired;
358 /* If we were asked to invalidate this SID -> GID
359 * mapping, it was because we found out that this was
360 * not a GID at all. Do not overwrite a valid UID or
361 * BOTH mapping */
362 if (idmap_cache_find_sid2uid(sid, &tmp_uid, &expired)) {
363 if (!expired) {
364 return;
369 idmap_cache_set_sid2unixid(sid, &id);
370 return;
373 static char* key_xid2sid_str(TALLOC_CTX* mem_ctx, char t, const char* id) {
374 return talloc_asprintf(mem_ctx, "IDMAP/%cID2SID/%s", t, id);
377 static char* key_xid2sid(TALLOC_CTX* mem_ctx, char t, int id) {
378 char str[32];
379 snprintf(str, sizeof(str), "%d", id);
380 return key_xid2sid_str(mem_ctx, t, str);
383 static char* key_sid2xid_str(TALLOC_CTX* mem_ctx, const char* id) {
384 return talloc_asprintf(mem_ctx, "IDMAP/SID2XID/%s", id);
387 static bool idmap_cache_del_xid(char t, int xid)
389 TALLOC_CTX* mem_ctx = talloc_stackframe();
390 const char* key = key_xid2sid(mem_ctx, t, xid);
391 char* sid_str = NULL;
392 time_t timeout;
393 bool ret = true;
395 if (!gencache_get(key, &sid_str, &timeout)) {
396 DEBUG(3, ("no entry: %s\n", key));
397 ret = false;
398 goto done;
401 if (sid_str[0] != '-') {
402 const char* sid_key = key_sid2xid_str(mem_ctx, sid_str);
403 if (!gencache_del(sid_key)) {
404 DEBUG(2, ("failed to delete: %s\n", sid_key));
405 ret = false;
406 } else {
407 DEBUG(5, ("delete: %s\n", sid_key));
412 if (!gencache_del(key)) {
413 DEBUG(1, ("failed to delete: %s\n", key));
414 ret = false;
415 } else {
416 DEBUG(5, ("delete: %s\n", key));
419 done:
420 talloc_free(mem_ctx);
421 return ret;
424 bool idmap_cache_del_uid(uid_t uid) {
425 return idmap_cache_del_xid('U', uid);
428 bool idmap_cache_del_gid(gid_t gid) {
429 return idmap_cache_del_xid('G', gid);
432 bool idmap_cache_del_sid(const struct dom_sid *sid)
434 TALLOC_CTX* mem_ctx = talloc_stackframe();
435 bool ret = true;
436 bool expired;
437 struct unixid id;
438 const char *sid_key;
440 if (!idmap_cache_find_sid2unixid(sid, &id, &expired)) {
441 ret = false;
442 goto done;
445 if (id.id != -1) {
446 switch (id.type) {
447 case ID_TYPE_BOTH:
448 idmap_cache_del_xid('U', id.id);
449 idmap_cache_del_xid('G', id.id);
450 break;
451 case ID_TYPE_UID:
452 idmap_cache_del_xid('U', id.id);
453 break;
454 case ID_TYPE_GID:
455 idmap_cache_del_xid('G', id.id);
456 break;
457 default:
458 break;
462 sid_key = key_sid2xid_str(mem_ctx, dom_sid_string(mem_ctx, sid));
463 if (sid_key == NULL) {
464 return false;
466 /* If the mapping was symmetric, then this should fail */
467 gencache_del(sid_key);
468 done:
469 talloc_free(mem_ctx);
470 return ret;