summaryrefslogtreecommitdiff
path: root/source3/lib/namemap_cache.c
blob: 7075b7c5b1e36a58a90ce254b555fedf6e7b9a7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
 * Unix SMB/CIFS implementation.
 * Utils for caching sid2name and name2sid
 * Copyright (C) Volker Lendecke 2017
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "replace.h"
#include "namemap_cache.h"
#include "source3/lib/gencache.h"
#include "lib/util/debug.h"
#include "lib/util/strv.h"
#include "lib/util/util.h"
#include "lib/util/talloc_stack.h"
#include "lib/util/charset/charset.h"
#include "libcli/security/dom_sid.h"

bool namemap_cache_set_sid2name(const struct dom_sid *sid,
				const char *domain, const char *name,
				enum lsa_SidType type, time_t timeout)
{
	char typebuf[16];
	struct dom_sid_buf sidbuf;
	char keybuf[sizeof(sidbuf.buf)+10];
	char *val = NULL;
	DATA_BLOB data;
	int ret;
	bool ok = false;

	if ((sid == NULL) || is_null_sid(sid)) {
		return true;
	}
	if (domain == NULL) {
		domain = "";
	}
	if (name == NULL) {
		name = "";
	}
	if (type == SID_NAME_UNKNOWN) {
		domain = "";
		name = "";
	}

	snprintf(typebuf, sizeof(typebuf), "%d", (int)type);

	ret = strv_add(talloc_tos(), &val, domain);
	if (ret != 0) {
		DBG_DEBUG("strv_add failed: %s\n", strerror(ret));
		goto fail;
	}
	ret = strv_add(NULL, &val, name);
	if (ret != 0) {
		DBG_DEBUG("strv_add failed: %s\n", strerror(ret));
		goto fail;
	}
	ret = strv_add(NULL, &val, typebuf);
	if (ret != 0) {
		DBG_DEBUG("strv_add failed: %s\n", strerror(ret));
		goto fail;
	}

	dom_sid_str_buf(sid, &sidbuf);
	snprintf(keybuf, sizeof(keybuf), "SID2NAME/%s", sidbuf.buf);

	data = data_blob_const(val, talloc_get_size(val));

	ok = gencache_set_data_blob(keybuf, data, timeout);
	if (!ok) {
		DBG_DEBUG("gencache_set_data_blob failed\n");
	}
fail:
	TALLOC_FREE(val);
	return ok;
}

struct namemap_cache_find_sid_state {
	void (*fn)(const char *domain,
		   const char *name,
		   enum lsa_SidType type,
		   bool expired,
		   void *private_data);
	void *private_data;
	bool ok;
};

static void namemap_cache_find_sid_parser(
	const struct gencache_timeout *timeout,
	DATA_BLOB blob,
	void *private_data)
{
	struct namemap_cache_find_sid_state *state = private_data;
	const char *strv = (const char *)blob.data;
	size_t strv_len = blob.length;
	const char *domain;
	const char *name;
	const char *typebuf;
	int error = 0;
	unsigned long type;

	state->ok = false;

	domain = strv_len_next(strv, strv_len, NULL);
	if (domain == NULL) {
		return;
	}
	name = strv_len_next(strv, strv_len, domain);
	if (name == NULL) {
		return;
	}
	typebuf = strv_len_next(strv, strv_len, name);
	if (typebuf == NULL) {
		return;
	}

	type = smb_strtoul(typebuf, NULL, 10, &error, SMB_STR_FULL_STR_CONV);
	if (error != 0) {
		return;
	}

	state->fn(domain,
		  name,
		  (enum lsa_SidType)type,
		  gencache_timeout_expired(timeout),
		  state->private_data);

	state->ok = true;
}

bool namemap_cache_find_sid(const struct dom_sid *sid,
			    void (*fn)(const char *domain,
				       const char *name,
				       enum lsa_SidType type,
				       bool expired,
				       void *private_data),
			    void *private_data)
{
	struct namemap_cache_find_sid_state state = {
		.fn = fn, .private_data = private_data
	};
	struct dom_sid_buf sidbuf;
	char keybuf[sizeof(sidbuf.buf)+10];
	bool ok;

	dom_sid_str_buf(sid, &sidbuf);
	snprintf(keybuf, sizeof(keybuf), "SID2NAME/%s", sidbuf.buf);

	ok = gencache_parse(keybuf, namemap_cache_find_sid_parser, &state);
	if (!ok) {
		DBG_DEBUG("gencache_parse(%s) failed\n", keybuf);
		return false;
	}

	if (!state.ok) {
		DBG_DEBUG("Could not parse %s, deleting\n", keybuf);
		gencache_del(keybuf);
		return false;
	}

	return true;
}

bool namemap_cache_set_name2sid(const char *domain, const char *name,
				const struct dom_sid *sid,
				enum lsa_SidType type,
				time_t timeout)
{
	char typebuf[16];
	struct dom_sid_buf sidbuf = {{0}};
	char *key;
	char *key_upper;
	char *val = NULL;
	DATA_BLOB data;
	int ret;
	bool ok = false;

	if (domain == NULL) {
		domain = "";
	}
	if (name == NULL) {
		name = "";
	}
	if (type != SID_NAME_UNKNOWN) {
		dom_sid_str_buf(sid, &sidbuf);
	}

	snprintf(typebuf, sizeof(typebuf), "%d", (int)type);

	key = talloc_asprintf(talloc_tos(), "NAME2SID/%s\\%s", domain, name);
	if (key == NULL) {
		DBG_DEBUG("talloc_asprintf failed\n");
		goto fail;
	}
	key_upper = strupper_talloc(key, key);
	if (key_upper == NULL) {
		DBG_DEBUG("strupper_talloc failed\n");
		goto fail;
	}

	ret = strv_add(key, &val, sidbuf.buf);
	if (ret != 0) {
		DBG_DEBUG("strv_add failed: %s\n", strerror(ret));
		goto fail;
	}
	ret = strv_add(NULL, &val, typebuf);
	if (ret != 0) {
		DBG_DEBUG("strv_add failed: %s\n", strerror(ret));
		goto fail;
	}

	data = data_blob_const(val, talloc_get_size(val));

	ok = gencache_set_data_blob(key_upper, data, timeout);
	if (!ok) {
		DBG_DEBUG("gencache_set_data_blob failed\n");
	}
fail:
	TALLOC_FREE(key);
	return ok;
}

struct namemap_cache_find_name_state {
	void (*fn)(const struct dom_sid *sid,
		   enum lsa_SidType type,
		   bool expired,
		   void *private_data);
	void *private_data;
	bool ok;
};

static void namemap_cache_find_name_parser(
	const struct gencache_timeout *timeout,
	DATA_BLOB blob,
	void *private_data)
{
	struct namemap_cache_find_name_state *state = private_data;
	const char *strv = (const char *)blob.data;
	size_t strv_len = blob.length;
	const char *sidbuf;
	const char *sid_endptr;
	const char *typebuf;
	int error = 0;
	struct dom_sid sid;
	unsigned long type;
	bool ok;

	state->ok = false;

	sidbuf = strv_len_next(strv, strv_len, NULL);
	if (sidbuf == NULL) {
		return;
	}
	typebuf = strv_len_next(strv, strv_len, sidbuf);
	if (typebuf == NULL) {
		return;
	}

	ok = dom_sid_parse_endp(sidbuf, &sid, &sid_endptr);
	if (!ok) {
		return;
	}
	if (*sid_endptr != '\0') {
		return;
	}

	type = smb_strtoul(typebuf, NULL, 10, &error, SMB_STR_FULL_STR_CONV);
	if (error != 0) {
		return;
	}

	state->fn(&sid,
		  (enum lsa_SidType)type,
		  gencache_timeout_expired(timeout),
		  state->private_data);

	state->ok = true;
}

bool namemap_cache_find_name(const char *domain,
			     const char *name,
			     void (*fn)(const struct dom_sid *sid,
					enum lsa_SidType type,
					bool expired,
					void *private_data),
			     void *private_data)
{
	struct namemap_cache_find_name_state state = {
		.fn = fn, .private_data = private_data
	};
	char *key;
	char *key_upper;
	bool ret = false;
	bool ok;

	key = talloc_asprintf(talloc_tos(), "NAME2SID/%s\\%s", domain, name);
	if (key == NULL) {
		DBG_DEBUG("talloc_asprintf failed\n");
		return false;
	}
	key_upper = strupper_talloc(key, key);
	if (key_upper == NULL) {
		DBG_DEBUG("strupper_talloc failed\n");
		goto fail;
	}

	ok = gencache_parse(key_upper, namemap_cache_find_name_parser, &state);
	if (!ok) {
		DBG_DEBUG("gencache_parse(%s) failed\n", key_upper);
		goto fail;
	}

	if (!state.ok) {
		DBG_DEBUG("Could not parse %s, deleting\n", key_upper);
		goto fail;
	}

	ret = true;
fail:
	TALLOC_FREE(key);
	return ret;
}