s4:libcli: split out smb_raw_negotiate_fill_transport()
[Samba.git] / source3 / lib / namemap_cache.c
blob38f7a2ddc88044989626c8d9094c2334a16b68be
1 /*
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/>.
20 #include "replace.h"
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)
33 char typebuf[16];
34 char sidbuf[DOM_SID_STR_BUFLEN];
35 char keybuf[DOM_SID_STR_BUFLEN+10];
36 char *val = NULL;
37 DATA_BLOB data;
38 int ret;
39 bool ok = false;
41 if ((sid == NULL) || is_null_sid(sid)) {
42 return true;
44 if (domain == NULL) {
45 domain = "";
47 if (name == NULL) {
48 name = "";
50 if (type == SID_NAME_UNKNOWN) {
51 domain = "";
52 name = "";
55 snprintf(typebuf, sizeof(typebuf), "%d", (int)type);
57 ret = strv_add(talloc_tos(), &val, domain);
58 if (ret != 0) {
59 DBG_DEBUG("strv_add failed: %s\n", strerror(ret));
60 goto fail;
62 ret = strv_add(NULL, &val, name);
63 if (ret != 0) {
64 DBG_DEBUG("strv_add failed: %s\n", strerror(ret));
65 goto fail;
67 ret = strv_add(NULL, &val, typebuf);
68 if (ret != 0) {
69 DBG_DEBUG("strv_add failed: %s\n", strerror(ret));
70 goto fail;
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);
79 if (!ok) {
80 DBG_DEBUG("gencache_set_data_blob failed\n");
82 fail:
83 TALLOC_FREE(val);
84 return ok;
87 struct namemap_cache_find_sid_state {
88 void (*fn)(const char *domain, const char *name,
89 enum lsa_SidType type, time_t timeout,
90 void *private_data);
91 void *private_data;
92 bool ok;
95 static void namemap_cache_find_sid_parser(time_t timeout, DATA_BLOB blob,
96 void *private_data)
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;
101 const char *domain;
102 const char *name;
103 const char *typebuf;
104 char *endptr;
105 unsigned long type;
107 state->ok = false;
109 domain = strv_len_next(strv, strv_len, NULL);
110 if (domain == NULL) {
111 return;
113 name = strv_len_next(strv, strv_len, domain);
114 if (name == NULL) {
115 return;
117 typebuf = strv_len_next(strv, strv_len, name);
118 if (typebuf == NULL) {
119 return;
122 type = strtoul(typebuf, &endptr, 10);
123 if (*endptr != '\0') {
124 return;
126 if ((type == ULONG_MAX) && (errno == ERANGE)) {
127 return;
130 state->fn(domain, name, (enum lsa_SidType)type, timeout,
131 state->private_data);
133 state->ok = true;
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,
139 void *private_data),
140 void *private_data)
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];
147 bool ok;
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);
153 if (!ok) {
154 DBG_DEBUG("gencache_parse(%s) failed\n", keybuf);
155 return false;
158 if (!state.ok) {
159 DBG_DEBUG("Could not parse %s, deleting\n", keybuf);
160 gencache_del(keybuf);
161 return false;
164 return true;
167 bool namemap_cache_set_name2sid(const char *domain, const char *name,
168 const struct dom_sid *sid,
169 enum lsa_SidType type,
170 time_t timeout)
172 char typebuf[16];
173 char sidbuf[DOM_SID_STR_BUFLEN];
174 char *key;
175 char *key_upper;
176 char *val = NULL;
177 DATA_BLOB data;
178 int ret;
179 bool ok = false;
181 if (domain == NULL) {
182 domain = "";
184 if (name == NULL) {
185 name = "";
187 if (type == SID_NAME_UNKNOWN) {
188 sidbuf[0] = '\0';
189 } else {
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);
196 if (key == NULL) {
197 DBG_DEBUG("talloc_asprintf failed\n");
198 goto fail;
200 key_upper = strupper_talloc(key, key);
201 if (key_upper == NULL) {
202 DBG_DEBUG("strupper_talloc failed\n");
203 goto fail;
206 ret = strv_add(key, &val, sidbuf);
207 if (ret != 0) {
208 DBG_DEBUG("strv_add failed: %s\n", strerror(ret));
209 goto fail;
211 ret = strv_add(NULL, &val, typebuf);
212 if (ret != 0) {
213 DBG_DEBUG("strv_add failed: %s\n", strerror(ret));
214 goto fail;
217 data = data_blob_const(val, talloc_get_size(val));
219 ok = gencache_set_data_blob(key_upper, data, timeout);
220 if (!ok) {
221 DBG_DEBUG("gencache_set_data_blob failed\n");
223 fail:
224 TALLOC_FREE(key);
225 return ok;
228 struct namemap_cache_find_name_state {
229 void (*fn)(const struct dom_sid *sid,
230 enum lsa_SidType type, time_t timeout,
231 void *private_data);
232 void *private_data;
233 bool ok;
236 static void namemap_cache_find_name_parser(time_t timeout, DATA_BLOB blob,
237 void *private_data)
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;
242 const char *sidbuf;
243 const char *sid_endptr;
244 const char *typebuf;
245 char *endptr;
246 struct dom_sid sid;
247 unsigned long type;
248 bool ok;
250 state->ok = false;
252 sidbuf = strv_len_next(strv, strv_len, NULL);
253 if (sidbuf == NULL) {
254 return;
256 typebuf = strv_len_next(strv, strv_len, sidbuf);
257 if (typebuf == NULL) {
258 return;
261 ok = dom_sid_parse_endp(sidbuf, &sid, &sid_endptr);
262 if (!ok) {
263 return;
265 if (*sid_endptr != '\0') {
266 return;
269 type = strtoul(typebuf, &endptr, 10);
270 if (*endptr != '\0') {
271 return;
273 if ((type == ULONG_MAX) && (errno == ERANGE)) {
274 return;
277 state->fn(&sid, (enum lsa_SidType)type, timeout, state->private_data);
279 state->ok = true;
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,
285 void *private_data),
286 void *private_data)
288 struct namemap_cache_find_name_state state = {
289 .fn = fn, .private_data = private_data
291 char *key;
292 char *key_upper;
293 bool ret = false;
294 bool ok;
296 key = talloc_asprintf(talloc_tos(), "NAME2SID/%s\\%s", domain, name);
297 if (key == NULL) {
298 DBG_DEBUG("talloc_asprintf failed\n");
299 return false;
301 key_upper = strupper_talloc(key, key);
302 if (key_upper == NULL) {
303 DBG_DEBUG("strupper_talloc failed\n");
304 goto fail;
307 ok = gencache_parse(key_upper, namemap_cache_find_name_parser, &state);
308 if (!ok) {
309 DBG_DEBUG("gencache_parse(%s) failed\n", key_upper);
310 goto fail;
313 if (!state.ok) {
314 DBG_DEBUG("Could not parse %s, deleting\n", key_upper);
315 goto fail;
318 ret = true;
319 fail:
320 TALLOC_FREE(key);
321 return ret;