s3:lib: Fix probably a copy&paste error in namemap_cache_set_sid2name()
[samba.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         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;
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_string_buf(sid, sidbuf, sizeof(sidbuf));
74         snprintf(keybuf, sizeof(keybuf), "SID2NAME/%s", sidbuf);
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, const char *name,
89                    enum lsa_SidType type, time_t timeout,
90                    void *private_data);
91         void *private_data;
92         bool ok;
93 };
94
95 static void namemap_cache_find_sid_parser(time_t timeout, DATA_BLOB blob,
96                                           void *private_data)
97 {
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;
106
107         state->ok = false;
108
109         domain = strv_len_next(strv, strv_len, NULL);
110         if (domain == NULL) {
111                 return;
112         }
113         name = strv_len_next(strv, strv_len, domain);
114         if (name == NULL) {
115                 return;
116         }
117         typebuf = strv_len_next(strv, strv_len, name);
118         if (typebuf == NULL) {
119                 return;
120         }
121
122         type = strtoul(typebuf, &endptr, 10);
123         if (*endptr != '\0') {
124                 return;
125         }
126         if ((type == ULONG_MAX) && (errno == ERANGE)) {
127                 return;
128         }
129
130         state->fn(domain, name, (enum lsa_SidType)type, timeout,
131                   state->private_data);
132
133         state->ok = true;
134 }
135
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)
141 {
142         struct namemap_cache_find_sid_state state = {
143                 .fn = fn, .private_data = private_data
144         };
145         char sidbuf[DOM_SID_STR_BUFLEN];
146         char keybuf[DOM_SID_STR_BUFLEN+10];
147         bool ok;
148
149         dom_sid_string_buf(sid, sidbuf, sizeof(sidbuf));
150         snprintf(keybuf, sizeof(keybuf), "SID2NAME/%s", sidbuf);
151
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;
156         }
157
158         if (!state.ok) {
159                 DBG_DEBUG("Could not parse %s, deleting\n", keybuf);
160                 gencache_del(keybuf);
161                 return false;
162         }
163
164         return true;
165 }
166
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)
171 {
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;
180
181         if (domain == NULL) {
182                 domain = "";
183         }
184         if (name == NULL) {
185                 name = "";
186         }
187         if (type == SID_NAME_UNKNOWN) {
188                 sidbuf[0] = '\0';
189         } else {
190                 dom_sid_string_buf(sid, sidbuf, sizeof(sidbuf));
191         }
192
193         snprintf(typebuf, sizeof(typebuf), "%d", (int)type);
194
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;
199         }
200         key_upper = strupper_talloc(key, key);
201         if (key_upper == NULL) {
202                 DBG_DEBUG("strupper_talloc failed\n");
203                 goto fail;
204         }
205
206         ret = strv_add(key, &val, sidbuf);
207         if (ret != 0) {
208                 DBG_DEBUG("strv_add failed: %s\n", strerror(ret));
209                 goto fail;
210         }
211         ret = strv_add(NULL, &val, typebuf);
212         if (ret != 0) {
213                 DBG_DEBUG("strv_add failed: %s\n", strerror(ret));
214                 goto fail;
215         }
216
217         data = data_blob_const(val, talloc_get_size(val));
218
219         ok = gencache_set_data_blob(key_upper, data, timeout);
220         if (!ok) {
221                 DBG_DEBUG("gencache_set_data_blob failed\n");
222         }
223 fail:
224         TALLOC_FREE(key);
225         return ok;
226 }
227
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;
234 };
235
236 static void namemap_cache_find_name_parser(time_t timeout, DATA_BLOB blob,
237                                            void *private_data)
238 {
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;
249
250         state->ok = false;
251
252         sidbuf = strv_len_next(strv, strv_len, NULL);
253         if (sidbuf == NULL) {
254                 return;
255         }
256         typebuf = strv_len_next(strv, strv_len, sidbuf);
257         if (typebuf == NULL) {
258                 return;
259         }
260
261         ok = dom_sid_parse_endp(sidbuf, &sid, &sid_endptr);
262         if (!ok) {
263                 return;
264         }
265         if (*sid_endptr != '\0') {
266                 return;
267         }
268
269         type = strtoul(typebuf, &endptr, 10);
270         if (*endptr != '\0') {
271                 return;
272         }
273         if ((type == ULONG_MAX) && (errno == ERANGE)) {
274                 return;
275         }
276
277         state->fn(&sid, (enum lsa_SidType)type, timeout, state->private_data);
278
279         state->ok = true;
280 }
281
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)
287 {
288         struct namemap_cache_find_name_state state = {
289                 .fn = fn, .private_data = private_data
290         };
291         char *key;
292         char *key_upper;
293         bool ret = false;
294         bool ok;
295
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;
300         }
301         key_upper = strupper_talloc(key, key);
302         if (key_upper == NULL) {
303                 DBG_DEBUG("strupper_talloc failed\n");
304                 goto fail;
305         }
306
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;
311         }
312
313         if (!state.ok) {
314                 DBG_DEBUG("Could not parse %s, deleting\n", key_upper);
315                 goto fail;
316         }
317
318         ret = true;
319 fail:
320         TALLOC_FREE(key);
321         return ret;
322 }