2 * Unix SMB/CIFS implementation.
3 * Utils for caching sid2name and name2sid
4 * Copyright (C) Volker Lendecke 2017
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "namemap_cache.h"
22 #include "source3/lib/gencache.h"
23 #include "lib/util/debug.h"
24 #include "lib/util/strv.h"
25 #include "lib/util/util.h"
26 #include "lib/util/talloc_stack.h"
27 #include "lib/util/charset/charset.h"
28 #include "libcli/security/dom_sid.h"
29 #include "lib/util/smb_strtox.h"
31 bool namemap_cache_set_sid2name(const struct dom_sid
*sid
,
32 const char *domain
, const char *name
,
33 enum lsa_SidType type
, time_t timeout
)
36 struct dom_sid_buf sidbuf
;
37 char keybuf
[sizeof(sidbuf
.buf
)+10];
43 if ((sid
== NULL
) || is_null_sid(sid
)) {
52 if (type
== SID_NAME_UNKNOWN
) {
57 snprintf(typebuf
, sizeof(typebuf
), "%d", (int)type
);
59 ret
= strv_add(talloc_tos(), &val
, domain
);
61 DBG_DEBUG("strv_add failed: %s\n", strerror(ret
));
64 ret
= strv_add(NULL
, &val
, name
);
66 DBG_DEBUG("strv_add failed: %s\n", strerror(ret
));
69 ret
= strv_add(NULL
, &val
, typebuf
);
71 DBG_DEBUG("strv_add failed: %s\n", strerror(ret
));
75 dom_sid_str_buf(sid
, &sidbuf
);
76 snprintf(keybuf
, sizeof(keybuf
), "SID2NAME/%s", sidbuf
.buf
);
78 data
= data_blob_const(val
, talloc_get_size(val
));
80 ok
= gencache_set_data_blob(keybuf
, data
, timeout
);
82 DBG_DEBUG("gencache_set_data_blob failed\n");
89 struct namemap_cache_find_sid_state
{
90 void (*fn
)(const char *domain
,
92 enum lsa_SidType type
,
99 static void namemap_cache_find_sid_parser(
100 const struct gencache_timeout
*timeout
,
104 struct namemap_cache_find_sid_state
*state
= private_data
;
105 const char *strv
= (const char *)blob
.data
;
106 size_t strv_len
= blob
.length
;
115 domain
= strv_len_next(strv
, strv_len
, NULL
);
116 if (domain
== NULL
) {
119 name
= strv_len_next(strv
, strv_len
, domain
);
123 typebuf
= strv_len_next(strv
, strv_len
, name
);
124 if (typebuf
== NULL
) {
128 type
= smb_strtoul(typebuf
, NULL
, 10, &error
, SMB_STR_FULL_STR_CONV
);
135 (enum lsa_SidType
)type
,
136 gencache_timeout_expired(timeout
),
137 state
->private_data
);
142 bool namemap_cache_find_sid(const struct dom_sid
*sid
,
143 void (*fn
)(const char *domain
,
145 enum lsa_SidType type
,
150 struct namemap_cache_find_sid_state state
= {
151 .fn
= fn
, .private_data
= private_data
153 struct dom_sid_buf sidbuf
;
154 char keybuf
[sizeof(sidbuf
.buf
)+10];
157 dom_sid_str_buf(sid
, &sidbuf
);
158 snprintf(keybuf
, sizeof(keybuf
), "SID2NAME/%s", sidbuf
.buf
);
160 ok
= gencache_parse(keybuf
, namemap_cache_find_sid_parser
, &state
);
162 DBG_DEBUG("gencache_parse(%s) failed\n", keybuf
);
167 DBG_DEBUG("Could not parse %s, deleting\n", keybuf
);
168 gencache_del(keybuf
);
175 bool namemap_cache_set_name2sid(const char *domain
, const char *name
,
176 const struct dom_sid
*sid
,
177 enum lsa_SidType type
,
181 struct dom_sid_buf sidbuf
= {{0}};
189 if (domain
== NULL
) {
195 if (type
!= SID_NAME_UNKNOWN
) {
196 dom_sid_str_buf(sid
, &sidbuf
);
199 snprintf(typebuf
, sizeof(typebuf
), "%d", (int)type
);
201 key
= talloc_asprintf(talloc_tos(), "NAME2SID/%s\\%s", domain
, name
);
203 DBG_DEBUG("talloc_asprintf failed\n");
206 key_upper
= strupper_talloc(key
, key
);
207 if (key_upper
== NULL
) {
208 DBG_DEBUG("strupper_talloc failed\n");
212 ret
= strv_add(key
, &val
, sidbuf
.buf
);
214 DBG_DEBUG("strv_add failed: %s\n", strerror(ret
));
217 ret
= strv_add(NULL
, &val
, typebuf
);
219 DBG_DEBUG("strv_add failed: %s\n", strerror(ret
));
223 data
= data_blob_const(val
, talloc_get_size(val
));
225 ok
= gencache_set_data_blob(key_upper
, data
, timeout
);
227 DBG_DEBUG("gencache_set_data_blob failed\n");
234 struct namemap_cache_find_name_state
{
235 void (*fn
)(const struct dom_sid
*sid
,
236 enum lsa_SidType type
,
243 static void namemap_cache_find_name_parser(
244 const struct gencache_timeout
*timeout
,
248 struct namemap_cache_find_name_state
*state
= private_data
;
249 const char *strv
= (const char *)blob
.data
;
250 size_t strv_len
= blob
.length
;
252 const char *sid_endptr
;
261 sidbuf
= strv_len_next(strv
, strv_len
, NULL
);
262 if (sidbuf
== NULL
) {
265 typebuf
= strv_len_next(strv
, strv_len
, sidbuf
);
266 if (typebuf
== NULL
) {
270 ok
= dom_sid_parse_endp(sidbuf
, &sid
, &sid_endptr
);
274 if (*sid_endptr
!= '\0') {
278 type
= smb_strtoul(typebuf
, NULL
, 10, &error
, SMB_STR_FULL_STR_CONV
);
284 (enum lsa_SidType
)type
,
285 gencache_timeout_expired(timeout
),
286 state
->private_data
);
291 bool namemap_cache_find_name(const char *domain
,
293 void (*fn
)(const struct dom_sid
*sid
,
294 enum lsa_SidType type
,
299 struct namemap_cache_find_name_state state
= {
300 .fn
= fn
, .private_data
= private_data
307 key
= talloc_asprintf(talloc_tos(), "NAME2SID/%s\\%s", domain
, name
);
309 DBG_DEBUG("talloc_asprintf failed\n");
312 key_upper
= strupper_talloc(key
, key
);
313 if (key_upper
== NULL
) {
314 DBG_DEBUG("strupper_talloc failed\n");
318 ok
= gencache_parse(key_upper
, namemap_cache_find_name_parser
, &state
);
320 DBG_DEBUG("gencache_parse(%s) failed\n", key_upper
);
325 DBG_DEBUG("Could not parse %s, deleting\n", key_upper
);