2 Unix SMB/CIFS implementation.
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/>.*/
21 #include "idmap_cache.h"
22 #include "../libcli/security/security.h"
23 #include "../librpc/gen_ndr/idmap.h"
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
,
46 key
= talloc_asprintf(talloc_tos(), "IDMAP/SID2XID/%s",
47 sid_to_fstring(sidstr
, sid
));
51 ret
= gencache_get(key
, &value
, &timeout
);
57 DEBUG(10, ("Parsing value for key [%s]: value=[%s]\n", key
, value
));
59 tmp_id
.id
= strtol(value
, &endptr
, 10);
60 DEBUG(10, ("Parsing value for key [%s]: id=[%llu], endptr=[%s]\n",
61 key
, (unsigned long long)tmp_id
.id
, endptr
));
63 ret
= (*endptr
== ':');
67 tmp_id
.type
= ID_TYPE_UID
;
71 tmp_id
.type
= ID_TYPE_GID
;
75 tmp_id
.type
= ID_TYPE_BOTH
;
81 DEBUG(0, ("FAILED to parse value for key [%s] "
82 "(id=[%llu], endptr=[%s]): "
83 "no type character after colon\n",
84 key
, (unsigned long long)tmp_id
.id
, endptr
));
89 DEBUG(0, ("FAILED to parse value for key [%s] "
90 "(id=[%llu], endptr=[%s]): "
91 "illegal type character '%c'\n",
92 key
, (unsigned long long)tmp_id
.id
, endptr
,
96 if (endptr
[2] != '\0') {
99 DEBUG(0, ("FAILED to parse value for key [%s] "
100 "(id=[%llu], endptr=[%s]): "
101 "more than 1 type character after colon\n",
102 key
, (unsigned long long)tmp_id
.id
, endptr
));
107 *expired
= (timeout
<= time(NULL
));
109 DEBUG(0, ("FAILED to parse value for key [%s] (value=[%s]): "
110 "colon missing after id=[%llu]\n",
111 key
, value
, (unsigned long long)tmp_id
.id
));
119 * Find a sid2uid mapping
120 * @param[in] sid the sid to map
121 * @param[out] puid where to put the result
122 * @param[out] expired is the cache entry expired?
123 * @retval Was anything in the cache at all?
125 * If *puid == -1 this was a negative mapping.
128 bool idmap_cache_find_sid2uid(const struct dom_sid
*sid
, uid_t
*puid
,
133 ret
= idmap_cache_find_sid2unixid(sid
, &id
, expired
);
138 if (id
.type
== ID_TYPE_BOTH
|| id
.type
== ID_TYPE_UID
) {
147 * Find a sid2gid mapping
148 * @param[in] sid the sid to map
149 * @param[out] pgid where to put the result
150 * @param[out] expired is the cache entry expired?
151 * @retval Was anything in the cache at all?
153 * If *pgid == -1 this was a negative mapping.
156 bool idmap_cache_find_sid2gid(const struct dom_sid
*sid
, gid_t
*pgid
,
161 ret
= idmap_cache_find_sid2unixid(sid
, &id
, expired
);
166 if (id
.type
== ID_TYPE_BOTH
|| id
.type
== ID_TYPE_GID
) {
175 * Find a uid2sid mapping
176 * @param[in] uid the uid to map
177 * @param[out] sid where to put the result
178 * @param[out] expired is the cache entry expired?
179 * @retval Was anything in the cache at all?
181 * If "is_null_sid(sid)", this was a negative mapping.
184 bool idmap_cache_find_uid2sid(uid_t uid
, struct dom_sid
*sid
, bool *expired
)
191 key
= talloc_asprintf(talloc_tos(), "IDMAP/UID2SID/%d", (int)uid
);
195 ret
= gencache_get(key
, &value
, &timeout
);
201 if (value
[0] != '-') {
202 ret
= string_to_sid(sid
, value
);
206 *expired
= (timeout
<= time(NULL
));
212 * Find a gid2sid mapping
213 * @param[in] gid the gid to map
214 * @param[out] sid where to put the result
215 * @param[out] expired is the cache entry expired?
216 * @retval Was anything in the cache at all?
218 * If "is_null_sid(sid)", this was a negative mapping.
221 bool idmap_cache_find_gid2sid(gid_t gid
, struct dom_sid
*sid
, bool *expired
)
228 key
= talloc_asprintf(talloc_tos(), "IDMAP/GID2SID/%d", (int)gid
);
232 ret
= gencache_get(key
, &value
, &timeout
);
238 if (value
[0] != '-') {
239 ret
= string_to_sid(sid
, value
);
243 *expired
= (timeout
<= time(NULL
));
249 * Store a mapping in the idmap cache
250 * @param[in] sid the sid to map
251 * @param[in] gid the gid to map
253 * If both parameters are valid values, then a positive mapping in both
254 * directions is stored. If "is_null_sid(sid)" is true, then this will be a
255 * negative mapping of gid, we want to cache that for this gid we could not
256 * find anything. Likewise if "gid==-1", then we want to cache that we did not
257 * find a mapping for the sid passed here.
260 void idmap_cache_set_sid2unixid(const struct dom_sid
*sid
, struct unixid
*unix_id
)
262 time_t now
= time(NULL
);
264 fstring sidstr
, key
, value
;
266 if (!is_null_sid(sid
)) {
267 fstr_sprintf(key
, "IDMAP/SID2XID/%s",
268 sid_to_fstring(sidstr
, sid
));
269 switch (unix_id
->type
) {
271 fstr_sprintf(value
, "%d:U", (int)unix_id
->id
);
274 fstr_sprintf(value
, "%d:G", (int)unix_id
->id
);
277 fstr_sprintf(value
, "%d:B", (int)unix_id
->id
);
282 timeout
= (unix_id
->id
== -1)
283 ? lp_idmap_negative_cache_time()
284 : lp_idmap_cache_time();
285 gencache_set(key
, value
, now
+ timeout
);
287 if (unix_id
->id
!= -1) {
288 if (is_null_sid(sid
)) {
289 /* negative gid mapping */
291 timeout
= lp_idmap_negative_cache_time();
294 sid_to_fstring(value
, sid
);
295 timeout
= lp_idmap_cache_time();
297 switch (unix_id
->type
) {
299 fstr_sprintf(key
, "IDMAP/UID2SID/%d", (int)unix_id
->id
);
300 gencache_set(key
, value
, now
+ timeout
);
301 fstr_sprintf(key
, "IDMAP/GID2SID/%d", (int)unix_id
->id
);
302 gencache_set(key
, value
, now
+ timeout
);
306 fstr_sprintf(key
, "IDMAP/UID2SID/%d", (int)unix_id
->id
);
310 fstr_sprintf(key
, "IDMAP/GID2SID/%d", (int)unix_id
->id
);
316 gencache_set(key
, value
, now
+ timeout
);
321 * Store a mapping in the idmap cache
322 * @param[in] sid the sid to map
323 * @param[in] uid the uid to map
325 * If both parameters are valid values, then a positive mapping in both
326 * directions is stored. If "is_null_sid(sid)" is true, then this will be a
327 * negative mapping of uid, we want to cache that for this uid we could not
328 * find anything. Likewise if "uid==-1", then we want to cache that we did not
329 * find a mapping for the sid passed here.
332 void idmap_cache_set_sid2uid(const struct dom_sid
*sid
, uid_t uid
)
335 id
.type
= ID_TYPE_UID
;
341 /* If we were asked to invalidate this SID -> UID
342 * mapping, it was because we found out that this was
343 * not a UID at all. Do not overwrite a valid GID or
345 if (idmap_cache_find_sid2gid(sid
, &tmp_gid
, &expired
)) {
352 idmap_cache_set_sid2unixid(sid
, &id
);
357 * Store a mapping in the idmap cache
358 * @param[in] sid the sid to map
359 * @param[in] gid the gid to map
361 * If both parameters are valid values, then a positive mapping in both
362 * directions is stored. If "is_null_sid(sid)" is true, then this will be a
363 * negative mapping of gid, we want to cache that for this gid we could not
364 * find anything. Likewise if "gid==-1", then we want to cache that we did not
365 * find a mapping for the sid passed here.
368 void idmap_cache_set_sid2gid(const struct dom_sid
*sid
, gid_t gid
)
371 id
.type
= ID_TYPE_GID
;
377 /* If we were asked to invalidate this SID -> GID
378 * mapping, it was because we found out that this was
379 * not a GID at all. Do not overwrite a valid UID or
381 if (idmap_cache_find_sid2uid(sid
, &tmp_uid
, &expired
)) {
388 idmap_cache_set_sid2unixid(sid
, &id
);
392 static char* key_xid2sid_str(TALLOC_CTX
* mem_ctx
, char t
, const char* id
) {
393 return talloc_asprintf(mem_ctx
, "IDMAP/%cID2SID/%s", t
, id
);
396 static char* key_xid2sid(TALLOC_CTX
* mem_ctx
, char t
, int id
) {
398 snprintf(str
, sizeof(str
), "%d", id
);
399 return key_xid2sid_str(mem_ctx
, t
, str
);
402 static char* key_sid2xid_str(TALLOC_CTX
* mem_ctx
, const char* id
) {
403 return talloc_asprintf(mem_ctx
, "IDMAP/SID2XID/%s", id
);
406 static bool idmap_cache_del_xid(char t
, int xid
)
408 TALLOC_CTX
* mem_ctx
= talloc_stackframe();
409 const char* key
= key_xid2sid(mem_ctx
, t
, xid
);
410 char* sid_str
= NULL
;
414 if (!gencache_get(key
, &sid_str
, &timeout
)) {
415 DEBUG(3, ("no entry: %s\n", key
));
420 if (sid_str
[0] != '-') {
421 const char* sid_key
= key_sid2xid_str(mem_ctx
, sid_str
);
422 if (!gencache_del(sid_key
)) {
423 DEBUG(2, ("failed to delete: %s\n", sid_key
));
426 DEBUG(5, ("delete: %s\n", sid_key
));
431 if (!gencache_del(key
)) {
432 DEBUG(1, ("failed to delete: %s\n", key
));
435 DEBUG(5, ("delete: %s\n", key
));
439 talloc_free(mem_ctx
);
443 bool idmap_cache_del_uid(uid_t uid
) {
444 return idmap_cache_del_xid('U', uid
);
447 bool idmap_cache_del_gid(gid_t gid
) {
448 return idmap_cache_del_xid('G', gid
);
451 bool idmap_cache_del_sid(const struct dom_sid
*sid
)
453 TALLOC_CTX
* mem_ctx
= talloc_stackframe();
459 if (!idmap_cache_find_sid2unixid(sid
, &id
, &expired
)) {
467 idmap_cache_del_xid('U', id
.id
);
468 idmap_cache_del_xid('G', id
.id
);
471 idmap_cache_del_xid('U', id
.id
);
474 idmap_cache_del_xid('G', id
.id
);
481 sid_key
= key_sid2xid_str(mem_ctx
, dom_sid_string(mem_ctx
, sid
));
482 if (sid_key
== NULL
) {
485 /* If the mapping was symmetric, then this should fail */
486 gencache_del(sid_key
);
488 talloc_free(mem_ctx
);