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"
24 #include "lib/gencache.h"
25 #include "lib/util/string_wrappers.h"
28 * Find a sid2xid mapping
29 * @param[in] sid the sid to map
30 * @param[out] id where to put the result
31 * @param[out] expired is the cache entry expired?
32 * @retval Was anything in the cache at all?
34 * If id->id == -1 this was a negative mapping.
37 bool idmap_cache_find_sid2unixid(const struct dom_sid
*sid
, struct unixid
*id
,
40 struct dom_sid_buf sidstr
;
48 key
= talloc_asprintf(talloc_tos(), "IDMAP/SID2XID/%s",
49 dom_sid_str_buf(sid
, &sidstr
));
53 ret
= gencache_get(key
, talloc_tos(), &value
, &timeout
);
58 DEBUG(10, ("Parsing value for key [%s]: value=[%s]\n", key
, value
));
60 if (value
[0] == '\0') {
61 DEBUG(0, ("Failed to parse value for key [%s]: "
62 "value is empty\n", key
));
67 tmp_id
.id
= strtol(value
, &endptr
, 10);
69 if ((value
== endptr
) && (tmp_id
.id
== 0)) {
70 DEBUG(0, ("Failed to parse value for key [%s]: value[%s] does "
71 "not start with a number\n", key
, value
));
76 DEBUG(10, ("Parsing value for key [%s]: id=[%llu], endptr=[%s]\n",
77 key
, (unsigned long long)tmp_id
.id
, endptr
));
79 ret
= (*endptr
== ':');
83 tmp_id
.type
= ID_TYPE_UID
;
87 tmp_id
.type
= ID_TYPE_GID
;
91 tmp_id
.type
= ID_TYPE_BOTH
;
95 tmp_id
.type
= ID_TYPE_NOT_SPECIFIED
;
99 DEBUG(0, ("FAILED to parse value for key [%s] "
100 "(id=[%llu], endptr=[%s]): "
101 "no type character after colon\n",
102 key
, (unsigned long long)tmp_id
.id
, endptr
));
106 DEBUG(0, ("FAILED to parse value for key [%s] "
107 "(id=[%llu], endptr=[%s]): "
108 "illegal type character '%c'\n",
109 key
, (unsigned long long)tmp_id
.id
, endptr
,
114 if (endptr
[2] != '\0') {
115 DEBUG(0, ("FAILED to parse value for key [%s] "
116 "(id=[%llu], endptr=[%s]): "
117 "more than 1 type character after colon\n",
118 key
, (unsigned long long)tmp_id
.id
, endptr
));
124 *expired
= (timeout
<= time(NULL
));
126 DEBUG(0, ("FAILED to parse value for key [%s] (value=[%s]): "
127 "colon missing after id=[%llu]\n",
128 key
, value
, (unsigned long long)tmp_id
.id
));
138 * Find a sid2uid mapping
139 * @param[in] sid the sid to map
140 * @param[out] puid where to put the result
141 * @param[out] expired is the cache entry expired?
142 * @retval Was anything in the cache at all?
144 * If *puid == -1 this was a negative mapping.
147 bool idmap_cache_find_sid2uid(const struct dom_sid
*sid
, uid_t
*puid
,
152 ret
= idmap_cache_find_sid2unixid(sid
, &id
, expired
);
157 if (id
.type
== ID_TYPE_BOTH
|| id
.type
== ID_TYPE_UID
) {
166 * Find a sid2gid mapping
167 * @param[in] sid the sid to map
168 * @param[out] pgid where to put the result
169 * @param[out] expired is the cache entry expired?
170 * @retval Was anything in the cache at all?
172 * If *pgid == -1 this was a negative mapping.
175 bool idmap_cache_find_sid2gid(const struct dom_sid
*sid
, gid_t
*pgid
,
180 ret
= idmap_cache_find_sid2unixid(sid
, &id
, expired
);
185 if (id
.type
== ID_TYPE_BOTH
|| id
.type
== ID_TYPE_GID
) {
193 struct idmap_cache_xid2sid_state
{
199 static void idmap_cache_xid2sid_parser(const struct gencache_timeout
*timeout
,
203 struct idmap_cache_xid2sid_state
*state
=
204 (struct idmap_cache_xid2sid_state
*)private_data
;
207 if ((blob
.length
== 0) || (blob
.data
[blob
.length
-1] != 0)) {
209 * Not a string, can't be a valid mapping
215 value
= (char *)blob
.data
;
217 if ((value
[0] == '-') && (value
[1] == '\0')) {
219 * Return NULL SID, see comment to uid2sid
221 *state
->sid
= (struct dom_sid
) {0};
224 state
->ret
= string_to_sid(state
->sid
, value
);
227 *state
->expired
= gencache_timeout_expired(timeout
);
232 * Find a xid2sid mapping
233 * @param[in] id the unix id to map
234 * @param[out] sid where to put the result
235 * @param[out] expired is the cache entry expired?
236 * @retval Was anything in the cache at all?
238 * If "is_null_sid(sid)", this was a negative mapping.
240 bool idmap_cache_find_xid2sid(
241 const struct unixid
*id
, struct dom_sid
*sid
, bool *expired
)
243 struct idmap_cache_xid2sid_state state
= {
244 .sid
= sid
, .expired
= expired
260 fstr_sprintf(key
, "IDMAP/%cID2SID/%d", c
, (int)id
->id
);
262 gencache_parse(key
, idmap_cache_xid2sid_parser
, &state
);
268 * Store a mapping in the idmap cache
269 * @param[in] sid the sid to map
270 * @param[in] unix_id the unix_id to map
272 * If both parameters are valid values, then a positive mapping in both
273 * directions is stored. If "is_null_sid(sid)" is true, then this will be a
274 * negative mapping of xid, we want to cache that for this xid we could not
275 * find anything. Likewise if "xid==-1", then we want to cache that we did not
276 * find a mapping for the sid passed here.
279 void idmap_cache_set_sid2unixid(const struct dom_sid
*sid
, struct unixid
*unix_id
)
281 time_t now
= time(NULL
);
285 if (!is_null_sid(sid
)) {
286 struct dom_sid_buf sidstr
;
287 fstr_sprintf(key
, "IDMAP/SID2XID/%s",
288 dom_sid_str_buf(sid
, &sidstr
));
289 switch (unix_id
->type
) {
291 fstr_sprintf(value
, "%d:U", (int)unix_id
->id
);
294 fstr_sprintf(value
, "%d:G", (int)unix_id
->id
);
297 fstr_sprintf(value
, "%d:B", (int)unix_id
->id
);
299 case ID_TYPE_NOT_SPECIFIED
:
300 fstr_sprintf(value
, "%d:N", (int)unix_id
->id
);
305 timeout
= (unix_id
->id
== -1)
306 ? lp_idmap_negative_cache_time()
307 : lp_idmap_cache_time();
308 gencache_set(key
, value
, now
+ timeout
);
310 if (unix_id
->id
!= -1) {
311 if (is_null_sid(sid
)) {
312 /* negative xid mapping */
314 timeout
= lp_idmap_negative_cache_time();
317 sid_to_fstring(value
, sid
);
318 timeout
= lp_idmap_cache_time();
320 switch (unix_id
->type
) {
322 fstr_sprintf(key
, "IDMAP/UID2SID/%d", (int)unix_id
->id
);
323 gencache_set(key
, value
, now
+ timeout
);
324 fstr_sprintf(key
, "IDMAP/GID2SID/%d", (int)unix_id
->id
);
325 gencache_set(key
, value
, now
+ timeout
);
329 fstr_sprintf(key
, "IDMAP/UID2SID/%d", (int)unix_id
->id
);
333 fstr_sprintf(key
, "IDMAP/GID2SID/%d", (int)unix_id
->id
);
339 gencache_set(key
, value
, now
+ timeout
);
343 static char* key_xid2sid_str(TALLOC_CTX
* mem_ctx
, char t
, const char* id
) {
344 return talloc_asprintf(mem_ctx
, "IDMAP/%cID2SID/%s", t
, id
);
347 static char* key_xid2sid(TALLOC_CTX
* mem_ctx
, char t
, int id
) {
349 snprintf(str
, sizeof(str
), "%d", id
);
350 return key_xid2sid_str(mem_ctx
, t
, str
);
353 static char* key_sid2xid_str(TALLOC_CTX
* mem_ctx
, const char* id
) {
354 return talloc_asprintf(mem_ctx
, "IDMAP/SID2XID/%s", id
);
357 static bool idmap_cache_del_xid(char t
, int xid
)
359 TALLOC_CTX
* mem_ctx
= talloc_stackframe();
360 const char* key
= key_xid2sid(mem_ctx
, t
, xid
);
361 char* sid_str
= NULL
;
365 if (!gencache_get(key
, mem_ctx
, &sid_str
, &timeout
)) {
366 DEBUG(3, ("no entry: %s\n", key
));
371 if (sid_str
[0] != '-') {
372 const char* sid_key
= key_sid2xid_str(mem_ctx
, sid_str
);
373 if (!gencache_del(sid_key
)) {
374 DEBUG(2, ("failed to delete: %s\n", sid_key
));
377 DEBUG(5, ("delete: %s\n", sid_key
));
382 if (!gencache_del(key
)) {
383 DEBUG(1, ("failed to delete: %s\n", key
));
386 DEBUG(5, ("delete: %s\n", key
));
390 talloc_free(mem_ctx
);
394 bool idmap_cache_del_uid(uid_t uid
) {
395 return idmap_cache_del_xid('U', uid
);
398 bool idmap_cache_del_gid(gid_t gid
) {
399 return idmap_cache_del_xid('G', gid
);
402 bool idmap_cache_del_sid(const struct dom_sid
*sid
)
404 TALLOC_CTX
* mem_ctx
= talloc_stackframe();
408 struct dom_sid_buf sidbuf
;
411 if (!idmap_cache_find_sid2unixid(sid
, &id
, &expired
)) {
419 idmap_cache_del_xid('U', id
.id
);
420 idmap_cache_del_xid('G', id
.id
);
423 idmap_cache_del_xid('U', id
.id
);
426 idmap_cache_del_xid('G', id
.id
);
433 sid_key
= key_sid2xid_str(mem_ctx
, dom_sid_str_buf(sid
, &sidbuf
));
434 if (sid_key
== NULL
) {
437 /* If the mapping was symmetric, then this should fail */
438 gencache_del(sid_key
);
440 talloc_free(mem_ctx
);