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
, talloc_tos(), &value
, &timeout
);
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
));
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
));
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
== ':');
81 tmp_id
.type
= ID_TYPE_UID
;
85 tmp_id
.type
= ID_TYPE_GID
;
89 tmp_id
.type
= ID_TYPE_BOTH
;
93 tmp_id
.type
= ID_TYPE_NOT_SPECIFIED
;
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
));
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
,
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
));
122 *expired
= (timeout
<= time(NULL
));
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
));
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
,
150 ret
= idmap_cache_find_sid2unixid(sid
, &id
, expired
);
155 if (id
.type
== ID_TYPE_BOTH
|| id
.type
== ID_TYPE_UID
) {
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
,
178 ret
= idmap_cache_find_sid2unixid(sid
, &id
, expired
);
183 if (id
.type
== ID_TYPE_BOTH
|| id
.type
== ID_TYPE_GID
) {
191 struct idmap_cache_xid2sid_state
{
197 static void idmap_cache_xid2sid_parser(time_t timeout
, DATA_BLOB blob
,
200 struct idmap_cache_xid2sid_state
*state
=
201 (struct idmap_cache_xid2sid_state
*)private_data
;
204 ZERO_STRUCTP(state
->sid
);
207 if ((blob
.length
== 0) || (blob
.data
[blob
.length
-1] != 0)) {
209 * Not a string, can't be a valid mapping
214 value
= (char *)blob
.data
;
216 if (value
[0] != '-') {
217 state
->ret
= string_to_sid(state
->sid
, value
);
220 *state
->expired
= (timeout
<= time(NULL
));
225 * Find a uid2sid mapping
226 * @param[in] uid the uid to map
227 * @param[out] sid where to put the result
228 * @param[out] expired is the cache entry expired?
229 * @retval Was anything in the cache at all?
231 * If "is_null_sid(sid)", this was a negative mapping.
234 bool idmap_cache_find_uid2sid(uid_t uid
, struct dom_sid
*sid
, bool *expired
)
237 struct idmap_cache_xid2sid_state state
;
239 fstr_sprintf(key
, "IDMAP/UID2SID/%d", (int)uid
);
242 state
.expired
= expired
;
245 gencache_parse(key
, idmap_cache_xid2sid_parser
, &state
);
250 * Find a gid2sid mapping
251 * @param[in] gid the gid to map
252 * @param[out] sid where to put the result
253 * @param[out] expired is the cache entry expired?
254 * @retval Was anything in the cache at all?
256 * If "is_null_sid(sid)", this was a negative mapping.
259 bool idmap_cache_find_gid2sid(gid_t gid
, struct dom_sid
*sid
, bool *expired
)
262 struct idmap_cache_xid2sid_state state
;
264 fstr_sprintf(key
, "IDMAP/GID2SID/%d", (int)gid
);
267 state
.expired
= expired
;
270 gencache_parse(key
, idmap_cache_xid2sid_parser
, &state
);
275 * Store a mapping in the idmap cache
276 * @param[in] sid the sid to map
277 * @param[in] gid the gid to map
279 * If both parameters are valid values, then a positive mapping in both
280 * directions is stored. If "is_null_sid(sid)" is true, then this will be a
281 * negative mapping of gid, we want to cache that for this gid we could not
282 * find anything. Likewise if "gid==-1", then we want to cache that we did not
283 * find a mapping for the sid passed here.
286 void idmap_cache_set_sid2unixid(const struct dom_sid
*sid
, struct unixid
*unix_id
)
288 time_t now
= time(NULL
);
290 fstring sidstr
, key
, value
;
292 if (!is_null_sid(sid
)) {
293 fstr_sprintf(key
, "IDMAP/SID2XID/%s",
294 sid_to_fstring(sidstr
, sid
));
295 switch (unix_id
->type
) {
297 fstr_sprintf(value
, "%d:U", (int)unix_id
->id
);
300 fstr_sprintf(value
, "%d:G", (int)unix_id
->id
);
303 fstr_sprintf(value
, "%d:B", (int)unix_id
->id
);
305 case ID_TYPE_NOT_SPECIFIED
:
306 fstr_sprintf(value
, "%d:N", (int)unix_id
->id
);
311 timeout
= (unix_id
->id
== -1)
312 ? lp_idmap_negative_cache_time()
313 : lp_idmap_cache_time();
314 gencache_set(key
, value
, now
+ timeout
);
316 if (unix_id
->id
!= -1) {
317 if (is_null_sid(sid
)) {
318 /* negative gid mapping */
320 timeout
= lp_idmap_negative_cache_time();
323 sid_to_fstring(value
, sid
);
324 timeout
= lp_idmap_cache_time();
326 switch (unix_id
->type
) {
328 fstr_sprintf(key
, "IDMAP/UID2SID/%d", (int)unix_id
->id
);
329 gencache_set(key
, value
, now
+ timeout
);
330 fstr_sprintf(key
, "IDMAP/GID2SID/%d", (int)unix_id
->id
);
331 gencache_set(key
, value
, now
+ timeout
);
335 fstr_sprintf(key
, "IDMAP/UID2SID/%d", (int)unix_id
->id
);
339 fstr_sprintf(key
, "IDMAP/GID2SID/%d", (int)unix_id
->id
);
345 gencache_set(key
, value
, now
+ timeout
);
350 * Store a mapping in the idmap cache
351 * @param[in] sid the sid to map
352 * @param[in] uid the uid to map
354 * If both parameters are valid values, then a positive mapping in both
355 * directions is stored. If "is_null_sid(sid)" is true, then this will be a
356 * negative mapping of uid, we want to cache that for this uid we could not
357 * find anything. Likewise if "uid==-1", then we want to cache that we did not
358 * find a mapping for the sid passed here.
361 void idmap_cache_set_sid2uid(const struct dom_sid
*sid
, uid_t uid
)
364 id
.type
= ID_TYPE_UID
;
370 /* If we were asked to invalidate this SID -> UID
371 * mapping, it was because we found out that this was
372 * not a UID at all. Do not overwrite a valid GID or
374 if (idmap_cache_find_sid2gid(sid
, &tmp_gid
, &expired
)) {
381 idmap_cache_set_sid2unixid(sid
, &id
);
386 * Store a mapping in the idmap cache
387 * @param[in] sid the sid to map
388 * @param[in] gid the gid to map
390 * If both parameters are valid values, then a positive mapping in both
391 * directions is stored. If "is_null_sid(sid)" is true, then this will be a
392 * negative mapping of gid, we want to cache that for this gid we could not
393 * find anything. Likewise if "gid==-1", then we want to cache that we did not
394 * find a mapping for the sid passed here.
397 void idmap_cache_set_sid2gid(const struct dom_sid
*sid
, gid_t gid
)
400 id
.type
= ID_TYPE_GID
;
406 /* If we were asked to invalidate this SID -> GID
407 * mapping, it was because we found out that this was
408 * not a GID at all. Do not overwrite a valid UID or
410 if (idmap_cache_find_sid2uid(sid
, &tmp_uid
, &expired
)) {
417 idmap_cache_set_sid2unixid(sid
, &id
);
421 static char* key_xid2sid_str(TALLOC_CTX
* mem_ctx
, char t
, const char* id
) {
422 return talloc_asprintf(mem_ctx
, "IDMAP/%cID2SID/%s", t
, id
);
425 static char* key_xid2sid(TALLOC_CTX
* mem_ctx
, char t
, int id
) {
427 snprintf(str
, sizeof(str
), "%d", id
);
428 return key_xid2sid_str(mem_ctx
, t
, str
);
431 static char* key_sid2xid_str(TALLOC_CTX
* mem_ctx
, const char* id
) {
432 return talloc_asprintf(mem_ctx
, "IDMAP/SID2XID/%s", id
);
435 static bool idmap_cache_del_xid(char t
, int xid
)
437 TALLOC_CTX
* mem_ctx
= talloc_stackframe();
438 const char* key
= key_xid2sid(mem_ctx
, t
, xid
);
439 char* sid_str
= NULL
;
443 if (!gencache_get(key
, mem_ctx
, &sid_str
, &timeout
)) {
444 DEBUG(3, ("no entry: %s\n", key
));
449 if (sid_str
[0] != '-') {
450 const char* sid_key
= key_sid2xid_str(mem_ctx
, sid_str
);
451 if (!gencache_del(sid_key
)) {
452 DEBUG(2, ("failed to delete: %s\n", sid_key
));
455 DEBUG(5, ("delete: %s\n", sid_key
));
460 if (!gencache_del(key
)) {
461 DEBUG(1, ("failed to delete: %s\n", key
));
464 DEBUG(5, ("delete: %s\n", key
));
468 talloc_free(mem_ctx
);
472 bool idmap_cache_del_uid(uid_t uid
) {
473 return idmap_cache_del_xid('U', uid
);
476 bool idmap_cache_del_gid(gid_t gid
) {
477 return idmap_cache_del_xid('G', gid
);
480 bool idmap_cache_del_sid(const struct dom_sid
*sid
)
482 TALLOC_CTX
* mem_ctx
= talloc_stackframe();
488 if (!idmap_cache_find_sid2unixid(sid
, &id
, &expired
)) {
496 idmap_cache_del_xid('U', id
.id
);
497 idmap_cache_del_xid('G', id
.id
);
500 idmap_cache_del_xid('U', id
.id
);
503 idmap_cache_del_xid('G', id
.id
);
510 sid_key
= key_sid2xid_str(mem_ctx
, dom_sid_string(mem_ctx
, sid
));
511 if (sid_key
== NULL
) {
514 /* If the mapping was symmetric, then this should fail */
515 gencache_del(sid_key
);
517 talloc_free(mem_ctx
);