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/talloc_stack.h"
26 #include "lib/util/charset/charset.h"
27 #include "libcli/security/dom_sid.h"
29 bool namemap_cache_set_sid2name(const struct dom_sid
*sid
,
30 const char *domain
, const char *name
,
31 enum lsa_SidType type
, time_t timeout
)
34 char sidbuf
[DOM_SID_STR_BUFLEN
];
35 char keybuf
[DOM_SID_STR_BUFLEN
+10];
41 if ((sid
== NULL
) || is_null_sid(sid
)) {
50 if (type
== SID_NAME_UNKNOWN
) {
55 snprintf(typebuf
, sizeof(typebuf
), "%d", (int)type
);
56 snprintf(keybuf
, sizeof(keybuf
), "SID2NAME/%s", sidbuf
);
58 ret
= strv_add(talloc_tos(), &val
, domain
);
60 DBG_DEBUG("strv_add failed: %s\n", strerror(ret
));
63 ret
= strv_add(NULL
, &val
, name
);
65 DBG_DEBUG("strv_add failed: %s\n", strerror(ret
));
68 ret
= strv_add(NULL
, &val
, typebuf
);
70 DBG_DEBUG("strv_add failed: %s\n", strerror(ret
));
74 dom_sid_string_buf(sid
, sidbuf
, sizeof(sidbuf
));
75 snprintf(keybuf
, sizeof(keybuf
), "SID2NAME/%s", sidbuf
);
77 data
= data_blob_const(val
, talloc_get_size(val
));
79 ok
= gencache_set_data_blob(keybuf
, data
, timeout
);
81 DBG_DEBUG("gencache_set_data_blob failed\n");
88 struct namemap_cache_find_sid_state
{
89 void (*fn
)(const char *domain
, const char *name
,
90 enum lsa_SidType type
, time_t timeout
,
96 static void namemap_cache_find_sid_parser(time_t timeout
, DATA_BLOB blob
,
99 struct namemap_cache_find_sid_state
*state
= private_data
;
100 const char *strv
= (const char *)blob
.data
;
101 size_t strv_len
= blob
.length
;
110 domain
= strv_len_next(strv
, strv_len
, NULL
);
111 if (domain
== NULL
) {
114 name
= strv_len_next(strv
, strv_len
, domain
);
118 typebuf
= strv_len_next(strv
, strv_len
, name
);
119 if (typebuf
== NULL
) {
123 type
= strtoul(typebuf
, &endptr
, 10);
124 if (*endptr
!= '\0') {
127 if ((type
== ULONG_MAX
) && (errno
== ERANGE
)) {
131 state
->fn(domain
, name
, (enum lsa_SidType
)type
, timeout
,
132 state
->private_data
);
137 bool namemap_cache_find_sid(const struct dom_sid
*sid
,
138 void (*fn
)(const char *domain
, const char *name
,
139 enum lsa_SidType type
, time_t timeout
,
143 struct namemap_cache_find_sid_state state
= {
144 .fn
= fn
, .private_data
= private_data
146 char sidbuf
[DOM_SID_STR_BUFLEN
];
147 char keybuf
[DOM_SID_STR_BUFLEN
+10];
150 dom_sid_string_buf(sid
, sidbuf
, sizeof(sidbuf
));
151 snprintf(keybuf
, sizeof(keybuf
), "SID2NAME/%s", sidbuf
);
153 ok
= gencache_parse(keybuf
, namemap_cache_find_sid_parser
, &state
);
155 DBG_DEBUG("gencache_parse(%s) failed\n", keybuf
);
160 DBG_DEBUG("Could not parse %s, deleting\n", keybuf
);
161 gencache_del(keybuf
);
168 bool namemap_cache_set_name2sid(const char *domain
, const char *name
,
169 const struct dom_sid
*sid
,
170 enum lsa_SidType type
,
174 char sidbuf
[DOM_SID_STR_BUFLEN
];
182 if (domain
== NULL
) {
188 if (type
== SID_NAME_UNKNOWN
) {
191 dom_sid_string_buf(sid
, sidbuf
, sizeof(sidbuf
));
194 snprintf(typebuf
, sizeof(typebuf
), "%d", (int)type
);
196 key
= talloc_asprintf(talloc_tos(), "NAME2SID/%s\\%s", domain
, name
);
198 DBG_DEBUG("talloc_asprintf failed\n");
201 key_upper
= strupper_talloc(key
, key
);
202 if (key_upper
== NULL
) {
203 DBG_DEBUG("strupper_talloc failed\n");
207 ret
= strv_add(key
, &val
, sidbuf
);
209 DBG_DEBUG("strv_add failed: %s\n", strerror(ret
));
212 ret
= strv_add(NULL
, &val
, typebuf
);
214 DBG_DEBUG("strv_add failed: %s\n", strerror(ret
));
218 data
= data_blob_const(val
, talloc_get_size(val
));
220 ok
= gencache_set_data_blob(key_upper
, data
, timeout
);
222 DBG_DEBUG("gencache_set_data_blob failed\n");
229 struct namemap_cache_find_name_state
{
230 void (*fn
)(const struct dom_sid
*sid
,
231 enum lsa_SidType type
, time_t timeout
,
237 static void namemap_cache_find_name_parser(time_t timeout
, DATA_BLOB blob
,
240 struct namemap_cache_find_name_state
*state
= private_data
;
241 const char *strv
= (const char *)blob
.data
;
242 size_t strv_len
= blob
.length
;
244 const char *sid_endptr
;
253 sidbuf
= strv_len_next(strv
, strv_len
, NULL
);
254 if (sidbuf
== NULL
) {
257 typebuf
= strv_len_next(strv
, strv_len
, sidbuf
);
258 if (typebuf
== NULL
) {
262 ok
= dom_sid_parse_endp(sidbuf
, &sid
, &sid_endptr
);
266 if (*sid_endptr
!= '\0') {
270 type
= strtoul(typebuf
, &endptr
, 10);
271 if (*endptr
!= '\0') {
274 if ((type
== ULONG_MAX
) && (errno
== ERANGE
)) {
278 state
->fn(&sid
, (enum lsa_SidType
)type
, timeout
, state
->private_data
);
283 bool namemap_cache_find_name(const char *domain
, const char *name
,
284 void (*fn
)(const struct dom_sid
*sid
,
285 enum lsa_SidType type
, time_t timeout
,
289 struct namemap_cache_find_name_state state
= {
290 .fn
= fn
, .private_data
= private_data
297 key
= talloc_asprintf(talloc_tos(), "NAME2SID/%s\\%s", domain
, name
);
299 DBG_DEBUG("talloc_asprintf failed\n");
302 key_upper
= strupper_talloc(key
, key
);
303 if (key_upper
== NULL
) {
304 DBG_DEBUG("strupper_talloc failed\n");
308 ok
= gencache_parse(key_upper
, namemap_cache_find_name_parser
, &state
);
310 DBG_DEBUG("gencache_parse(%s) failed\n", key_upper
);
315 DBG_DEBUG("Could not parse %s, deleting\n", key_upper
);