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
);
57 ret
= strv_add(talloc_tos(), &val
, domain
);
59 DBG_DEBUG("strv_add failed: %s\n", strerror(ret
));
62 ret
= strv_add(NULL
, &val
, name
);
64 DBG_DEBUG("strv_add failed: %s\n", strerror(ret
));
67 ret
= strv_add(NULL
, &val
, typebuf
);
69 DBG_DEBUG("strv_add failed: %s\n", strerror(ret
));
73 dom_sid_string_buf(sid
, sidbuf
, sizeof(sidbuf
));
74 snprintf(keybuf
, sizeof(keybuf
), "SID2NAME/%s", sidbuf
);
76 data
= data_blob_const(val
, talloc_get_size(val
));
78 ok
= gencache_set_data_blob(keybuf
, data
, timeout
);
80 DBG_DEBUG("gencache_set_data_blob failed\n");
87 struct namemap_cache_find_sid_state
{
88 void (*fn
)(const char *domain
, const char *name
,
89 enum lsa_SidType type
, time_t timeout
,
95 static void namemap_cache_find_sid_parser(time_t timeout
, DATA_BLOB blob
,
98 struct namemap_cache_find_sid_state
*state
= private_data
;
99 const char *strv
= (const char *)blob
.data
;
100 size_t strv_len
= blob
.length
;
109 domain
= strv_len_next(strv
, strv_len
, NULL
);
110 if (domain
== NULL
) {
113 name
= strv_len_next(strv
, strv_len
, domain
);
117 typebuf
= strv_len_next(strv
, strv_len
, name
);
118 if (typebuf
== NULL
) {
122 type
= strtoul(typebuf
, &endptr
, 10);
123 if (*endptr
!= '\0') {
126 if ((type
== ULONG_MAX
) && (errno
== ERANGE
)) {
130 state
->fn(domain
, name
, (enum lsa_SidType
)type
, timeout
,
131 state
->private_data
);
136 bool namemap_cache_find_sid(const struct dom_sid
*sid
,
137 void (*fn
)(const char *domain
, const char *name
,
138 enum lsa_SidType type
, time_t timeout
,
142 struct namemap_cache_find_sid_state state
= {
143 .fn
= fn
, .private_data
= private_data
145 char sidbuf
[DOM_SID_STR_BUFLEN
];
146 char keybuf
[DOM_SID_STR_BUFLEN
+10];
149 dom_sid_string_buf(sid
, sidbuf
, sizeof(sidbuf
));
150 snprintf(keybuf
, sizeof(keybuf
), "SID2NAME/%s", sidbuf
);
152 ok
= gencache_parse(keybuf
, namemap_cache_find_sid_parser
, &state
);
154 DBG_DEBUG("gencache_parse(%s) failed\n", keybuf
);
159 DBG_DEBUG("Could not parse %s, deleting\n", keybuf
);
160 gencache_del(keybuf
);
167 bool namemap_cache_set_name2sid(const char *domain
, const char *name
,
168 const struct dom_sid
*sid
,
169 enum lsa_SidType type
,
173 char sidbuf
[DOM_SID_STR_BUFLEN
];
181 if (domain
== NULL
) {
187 if (type
== SID_NAME_UNKNOWN
) {
190 dom_sid_string_buf(sid
, sidbuf
, sizeof(sidbuf
));
193 snprintf(typebuf
, sizeof(typebuf
), "%d", (int)type
);
195 key
= talloc_asprintf(talloc_tos(), "NAME2SID/%s\\%s", domain
, name
);
197 DBG_DEBUG("talloc_asprintf failed\n");
200 key_upper
= strupper_talloc(key
, key
);
201 if (key_upper
== NULL
) {
202 DBG_DEBUG("strupper_talloc failed\n");
206 ret
= strv_add(key
, &val
, sidbuf
);
208 DBG_DEBUG("strv_add failed: %s\n", strerror(ret
));
211 ret
= strv_add(NULL
, &val
, typebuf
);
213 DBG_DEBUG("strv_add failed: %s\n", strerror(ret
));
217 data
= data_blob_const(val
, talloc_get_size(val
));
219 ok
= gencache_set_data_blob(key_upper
, data
, timeout
);
221 DBG_DEBUG("gencache_set_data_blob failed\n");
228 struct namemap_cache_find_name_state
{
229 void (*fn
)(const struct dom_sid
*sid
,
230 enum lsa_SidType type
, time_t timeout
,
236 static void namemap_cache_find_name_parser(time_t timeout
, DATA_BLOB blob
,
239 struct namemap_cache_find_name_state
*state
= private_data
;
240 const char *strv
= (const char *)blob
.data
;
241 size_t strv_len
= blob
.length
;
243 const char *sid_endptr
;
252 sidbuf
= strv_len_next(strv
, strv_len
, NULL
);
253 if (sidbuf
== NULL
) {
256 typebuf
= strv_len_next(strv
, strv_len
, sidbuf
);
257 if (typebuf
== NULL
) {
261 ok
= dom_sid_parse_endp(sidbuf
, &sid
, &sid_endptr
);
265 if (*sid_endptr
!= '\0') {
269 type
= strtoul(typebuf
, &endptr
, 10);
270 if (*endptr
!= '\0') {
273 if ((type
== ULONG_MAX
) && (errno
== ERANGE
)) {
277 state
->fn(&sid
, (enum lsa_SidType
)type
, timeout
, state
->private_data
);
282 bool namemap_cache_find_name(const char *domain
, const char *name
,
283 void (*fn
)(const struct dom_sid
*sid
,
284 enum lsa_SidType type
, time_t timeout
,
288 struct namemap_cache_find_name_state state
= {
289 .fn
= fn
, .private_data
= private_data
296 key
= talloc_asprintf(talloc_tos(), "NAME2SID/%s\\%s", domain
, name
);
298 DBG_DEBUG("talloc_asprintf failed\n");
301 key_upper
= strupper_talloc(key
, key
);
302 if (key_upper
== NULL
) {
303 DBG_DEBUG("strupper_talloc failed\n");
307 ok
= gencache_parse(key_upper
, namemap_cache_find_name_parser
, &state
);
309 DBG_DEBUG("gencache_parse(%s) failed\n", key_upper
);
314 DBG_DEBUG("Could not parse %s, deleting\n", key_upper
);