libsmb: Pull up wire_flags calculation from open_internal
[garming/samba-autobuild/.git] / source3 / lib / namemap_cache.c
1 /*
2  * Unix SMB/CIFS implementation.
3  * Utils for caching sid2name and name2sid
4  * Copyright (C) Volker Lendecke 2017
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
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"
28
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)
32 {
33         char typebuf[16];
34         struct dom_sid_buf sidbuf;
35         char keybuf[sizeof(sidbuf.buf)+10];
36         char *val = NULL;
37         DATA_BLOB data;
38         int ret;
39         bool ok = false;
40
41         if ((sid == NULL) || is_null_sid(sid)) {
42                 return true;
43         }
44         if (domain == NULL) {
45                 domain = "";
46         }
47         if (name == NULL) {
48                 name = "";
49         }
50         if (type == SID_NAME_UNKNOWN) {
51                 domain = "";
52                 name = "";
53         }
54
55         snprintf(typebuf, sizeof(typebuf), "%d", (int)type);
56
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;
61         }
62         ret = strv_add(NULL, &val, name);
63         if (ret != 0) {
64                 DBG_DEBUG("strv_add failed: %s\n", strerror(ret));
65                 goto fail;
66         }
67         ret = strv_add(NULL, &val, typebuf);
68         if (ret != 0) {
69                 DBG_DEBUG("strv_add failed: %s\n", strerror(ret));
70                 goto fail;
71         }
72
73         dom_sid_str_buf(sid, &sidbuf);
74         snprintf(keybuf, sizeof(keybuf), "SID2NAME/%s", sidbuf.buf);
75
76         data = data_blob_const(val, talloc_get_size(val));
77
78         ok = gencache_set_data_blob(keybuf, data, timeout);
79         if (!ok) {
80                 DBG_DEBUG("gencache_set_data_blob failed\n");
81         }
82 fail:
83         TALLOC_FREE(val);
84         return ok;
85 }
86
87 struct namemap_cache_find_sid_state {
88         void (*fn)(const char *domain,
89                    const char *name,
90                    enum lsa_SidType type,
91                    bool expired,
92                    void *private_data);
93         void *private_data;
94         bool ok;
95 };
96
97 static void namemap_cache_find_sid_parser(
98         const struct gencache_timeout *timeout,
99         DATA_BLOB blob,
100         void *private_data)
101 {
102         struct namemap_cache_find_sid_state *state = private_data;
103         const char *strv = (const char *)blob.data;
104         size_t strv_len = blob.length;
105         const char *domain;
106         const char *name;
107         const char *typebuf;
108         char *endptr;
109         unsigned long type;
110
111         state->ok = false;
112
113         domain = strv_len_next(strv, strv_len, NULL);
114         if (domain == NULL) {
115                 return;
116         }
117         name = strv_len_next(strv, strv_len, domain);
118         if (name == NULL) {
119                 return;
120         }
121         typebuf = strv_len_next(strv, strv_len, name);
122         if (typebuf == NULL) {
123                 return;
124         }
125
126         type = strtoul(typebuf, &endptr, 10);
127         if (*endptr != '\0') {
128                 return;
129         }
130         if ((type == ULONG_MAX) && (errno == ERANGE)) {
131                 return;
132         }
133
134         state->fn(domain,
135                   name,
136                   (enum lsa_SidType)type,
137                   gencache_timeout_expired(timeout),
138                   state->private_data);
139
140         state->ok = true;
141 }
142
143 bool namemap_cache_find_sid(const struct dom_sid *sid,
144                             void (*fn)(const char *domain,
145                                        const char *name,
146                                        enum lsa_SidType type,
147                                        bool expired,
148                                        void *private_data),
149                             void *private_data)
150 {
151         struct namemap_cache_find_sid_state state = {
152                 .fn = fn, .private_data = private_data
153         };
154         struct dom_sid_buf sidbuf;
155         char keybuf[sizeof(sidbuf.buf)+10];
156         bool ok;
157
158         dom_sid_str_buf(sid, &sidbuf);
159         snprintf(keybuf, sizeof(keybuf), "SID2NAME/%s", sidbuf.buf);
160
161         ok = gencache_parse(keybuf, namemap_cache_find_sid_parser, &state);
162         if (!ok) {
163                 DBG_DEBUG("gencache_parse(%s) failed\n", keybuf);
164                 return false;
165         }
166
167         if (!state.ok) {
168                 DBG_DEBUG("Could not parse %s, deleting\n", keybuf);
169                 gencache_del(keybuf);
170                 return false;
171         }
172
173         return true;
174 }
175
176 bool namemap_cache_set_name2sid(const char *domain, const char *name,
177                                 const struct dom_sid *sid,
178                                 enum lsa_SidType type,
179                                 time_t timeout)
180 {
181         char typebuf[16];
182         struct dom_sid_buf sidbuf = {{0}};
183         char *key;
184         char *key_upper;
185         char *val = NULL;
186         DATA_BLOB data;
187         int ret;
188         bool ok = false;
189
190         if (domain == NULL) {
191                 domain = "";
192         }
193         if (name == NULL) {
194                 name = "";
195         }
196         if (type != SID_NAME_UNKNOWN) {
197                 dom_sid_str_buf(sid, &sidbuf);
198         }
199
200         snprintf(typebuf, sizeof(typebuf), "%d", (int)type);
201
202         key = talloc_asprintf(talloc_tos(), "NAME2SID/%s\\%s", domain, name);
203         if (key == NULL) {
204                 DBG_DEBUG("talloc_asprintf failed\n");
205                 goto fail;
206         }
207         key_upper = strupper_talloc(key, key);
208         if (key_upper == NULL) {
209                 DBG_DEBUG("strupper_talloc failed\n");
210                 goto fail;
211         }
212
213         ret = strv_add(key, &val, sidbuf.buf);
214         if (ret != 0) {
215                 DBG_DEBUG("strv_add failed: %s\n", strerror(ret));
216                 goto fail;
217         }
218         ret = strv_add(NULL, &val, typebuf);
219         if (ret != 0) {
220                 DBG_DEBUG("strv_add failed: %s\n", strerror(ret));
221                 goto fail;
222         }
223
224         data = data_blob_const(val, talloc_get_size(val));
225
226         ok = gencache_set_data_blob(key_upper, data, timeout);
227         if (!ok) {
228                 DBG_DEBUG("gencache_set_data_blob failed\n");
229         }
230 fail:
231         TALLOC_FREE(key);
232         return ok;
233 }
234
235 struct namemap_cache_find_name_state {
236         void (*fn)(const struct dom_sid *sid,
237                    enum lsa_SidType type,
238                    bool expired,
239                    void *private_data);
240         void *private_data;
241         bool ok;
242 };
243
244 static void namemap_cache_find_name_parser(
245         const struct gencache_timeout *timeout,
246         DATA_BLOB blob,
247         void *private_data)
248 {
249         struct namemap_cache_find_name_state *state = private_data;
250         const char *strv = (const char *)blob.data;
251         size_t strv_len = blob.length;
252         const char *sidbuf;
253         const char *sid_endptr;
254         const char *typebuf;
255         char *endptr;
256         struct dom_sid sid;
257         unsigned long type;
258         bool ok;
259
260         state->ok = false;
261
262         sidbuf = strv_len_next(strv, strv_len, NULL);
263         if (sidbuf == NULL) {
264                 return;
265         }
266         typebuf = strv_len_next(strv, strv_len, sidbuf);
267         if (typebuf == NULL) {
268                 return;
269         }
270
271         ok = dom_sid_parse_endp(sidbuf, &sid, &sid_endptr);
272         if (!ok) {
273                 return;
274         }
275         if (*sid_endptr != '\0') {
276                 return;
277         }
278
279         type = strtoul(typebuf, &endptr, 10);
280         if (*endptr != '\0') {
281                 return;
282         }
283         if ((type == ULONG_MAX) && (errno == ERANGE)) {
284                 return;
285         }
286
287         state->fn(&sid,
288                   (enum lsa_SidType)type,
289                   gencache_timeout_expired(timeout),
290                   state->private_data);
291
292         state->ok = true;
293 }
294
295 bool namemap_cache_find_name(const char *domain,
296                              const char *name,
297                              void (*fn)(const struct dom_sid *sid,
298                                         enum lsa_SidType type,
299                                         bool expired,
300                                         void *private_data),
301                              void *private_data)
302 {
303         struct namemap_cache_find_name_state state = {
304                 .fn = fn, .private_data = private_data
305         };
306         char *key;
307         char *key_upper;
308         bool ret = false;
309         bool ok;
310
311         key = talloc_asprintf(talloc_tos(), "NAME2SID/%s\\%s", domain, name);
312         if (key == NULL) {
313                 DBG_DEBUG("talloc_asprintf failed\n");
314                 return false;
315         }
316         key_upper = strupper_talloc(key, key);
317         if (key_upper == NULL) {
318                 DBG_DEBUG("strupper_talloc failed\n");
319                 goto fail;
320         }
321
322         ok = gencache_parse(key_upper, namemap_cache_find_name_parser, &state);
323         if (!ok) {
324                 DBG_DEBUG("gencache_parse(%s) failed\n", key_upper);
325                 goto fail;
326         }
327
328         if (!state.ok) {
329                 DBG_DEBUG("Could not parse %s, deleting\n", key_upper);
330                 goto fail;
331         }
332
333         ret = true;
334 fail:
335         TALLOC_FREE(key);
336         return ret;
337 }