summaryrefslogtreecommitdiffstats
path: root/source3/winbindd/winbindd_idmap.c
blob: e6d2c284cf201129c4be764a386a7389ff1a65a9 (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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
   Unix SMB/CIFS implementation.

   Async helpers for blocking functions

   Copyright (C) Volker Lendecke 2005
   Copyright (C) Gerald Carter 2006
   Copyright (C) Simo Sorce 2007

   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 "includes.h"
#include "winbindd.h"
#include "../libcli/security/security.h"
#include "passdb/lookup_sid.h"
#include "lib/global_contexts.h"

#undef DBGC_CLASS
#define DBGC_CLASS DBGC_WINBIND

static struct winbindd_child static_idmap_child;

/*
 * Map idmap ranges to domain names, taken from smb.conf. This is
 * stored in the parent winbind and used to assemble xids2sids/sids2xids calls
 * into per-idmap-domain chunks.
 */
static struct wb_parent_idmap_config static_parent_idmap_config;

struct winbindd_child *idmap_child(void)
{
	return &static_idmap_child;
}

bool is_idmap_child(const struct winbindd_child *child)
{
	if (child == &static_idmap_child) {
		return true;
	}

	return false;
}

pid_t idmap_child_pid(void)
{
	return static_idmap_child.pid;
}

struct dcerpc_binding_handle *idmap_child_handle(void)
{
	/*
	 * The caller needs to use wb_parent_idmap_setup_send/recv
	 * before talking to the idmap child!
	 */
	SMB_ASSERT(static_parent_idmap_config.num_doms > 0);
	return static_idmap_child.binding_handle;
}

static void init_idmap_child_done(struct tevent_req *subreq);

void init_idmap_child(void)
{
	struct tevent_req *subreq = NULL;

	subreq = wb_parent_idmap_setup_send(global_event_context(),
					    global_event_context());
	if (subreq == NULL) {
		/*
		 * This is only an optimization, so we're free to
		 * to ignore errors
		 */
		DBG_ERR("wb_parent_idmap_setup_send() failed\n");
		return;
	}
	tevent_req_set_callback(subreq, init_idmap_child_done, NULL);
	DBG_DEBUG("wb_parent_idmap_setup_send() started\n");
}

static void init_idmap_child_done(struct tevent_req *subreq)
{
	const struct wb_parent_idmap_config *cfg = NULL;
	NTSTATUS status;

	status = wb_parent_idmap_setup_recv(subreq, &cfg);
	TALLOC_FREE(subreq);
	if (!NT_STATUS_IS_OK(status)) {
		/*
		 * This is only an optimization, so we're free to
		 * to ignore errors
		 */
		DBG_ERR("wb_parent_idmap_setup_recv() failed %s\n",
			nt_errstr(status));
		return;
	}

	DBG_DEBUG("wb_parent_idmap_setup_recv() finished\n");
}

struct wb_parent_idmap_setup_state {
	struct tevent_context *ev;
	struct wb_parent_idmap_config *cfg;
	size_t dom_idx;
};

static void wb_parent_idmap_setup_cleanup(struct tevent_req *req,
					  enum tevent_req_state req_state)
{
	struct wb_parent_idmap_setup_state *state =
		tevent_req_data(req,
		struct wb_parent_idmap_setup_state);

	if (req_state == TEVENT_REQ_DONE) {
		state->cfg = NULL;
		return;
	}

	if (state->cfg == NULL) {
		return;
	}

	state->cfg->num_doms = 0;
	TALLOC_FREE(state->cfg->doms);
	state->cfg = NULL;
}

static void wb_parent_idmap_setup_queue_wait_done(struct tevent_req *subreq);
static bool wb_parent_idmap_setup_scan_config(const char *domname,
					      void *private_data);
static void wb_parent_idmap_setup_lookupname_next(struct tevent_req *req);
static void wb_parent_idmap_setup_lookupname_done(struct tevent_req *subreq);

struct tevent_req *wb_parent_idmap_setup_send(TALLOC_CTX *mem_ctx,
					      struct tevent_context *ev)
{
	struct tevent_req *req = NULL;
	struct wb_parent_idmap_setup_state *state = NULL;
	struct tevent_req *subreq = NULL;

	req = tevent_req_create(mem_ctx, &state,
				struct wb_parent_idmap_setup_state);
	if (req == NULL) {
		return NULL;
	}
	*state = (struct wb_parent_idmap_setup_state) {
		.ev = ev,
		.cfg = &static_parent_idmap_config,
		.dom_idx = 0,
	};

	if (state->cfg->queue == NULL) {
		state->cfg->queue = tevent_queue_create(NULL,
						"wb_parent_idmap_config_queue");
		if (tevent_req_nomem(state->cfg->queue, req)) {
			return tevent_req_post(req, ev);
		}
	}

	subreq = tevent_queue_wait_send(state, state->ev, state->cfg->queue);
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq,
				wb_parent_idmap_setup_queue_wait_done,
				req);

	return req;
}

static void wb_parent_idmap_setup_queue_wait_done(struct tevent_req *subreq)
{
	struct tevent_req *req =
		tevent_req_callback_data(subreq,
		struct tevent_req);
	struct wb_parent_idmap_setup_state *state =
		tevent_req_data(req,
		struct wb_parent_idmap_setup_state);
	bool ok;

	/*
	 * Note we don't call TALLOC_FREE(subreq) here in order to block the
	 * queue until tevent_req_received() in wb_parent_idmap_setup_recv()
	 * will destroy it implicitly.
	 */
	ok = tevent_queue_wait_recv(subreq);
	if (!ok) {
		DBG_ERR("tevent_queue_wait_recv() failed\n");
		tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
		return;
	}

	if (state->cfg->num_doms != 0) {
		/*
		 * If we're not the first one we're done.
		 */
		tevent_req_done(req);
		return;
	}

	/*
	 * From this point we start changing state->cfg,
	 * which is &static_parent_idmap_config,
	 * so we better setup a cleanup function
	 * to undo the changes on failure.
	 */
	tevent_req_set_cleanup_fn(req, wb_parent_idmap_setup_cleanup);

	/*
	 * Put the passdb idmap domain first. We always need to try
	 * there first.
	 */
	state->cfg->doms = talloc_zero_array(NULL,
					     struct wb_parent_idmap_config_dom,
					     1);
	if (tevent_req_nomem(state->cfg->doms, req)) {
		return;
	}
	state->cfg->doms[0].low_id = 0;
	state->cfg->doms[0].high_id = UINT_MAX;
	state->cfg->doms[0].name = talloc_strdup(state->cfg->doms,
						 get_global_sam_name());
	if (tevent_req_nomem(state->cfg->doms[0].name, req)) {
		return;
	}
	state->cfg->num_doms += 1;

	lp_scan_idmap_domains(wb_parent_idmap_setup_scan_config, req);
	if (!tevent_req_is_in_progress(req)) {
		return;
	}

	wb_parent_idmap_setup_lookupname_next(req);
}

static bool wb_parent_idmap_setup_scan_config(const char *domname,
					      void *private_data)
{
	struct tevent_req *req =
		talloc_get_type_abort(private_data,
		struct tevent_req);
	struct wb_parent_idmap_setup_state *state =
		tevent_req_data(req,
		struct wb_parent_idmap_setup_state);
	struct wb_parent_idmap_config_dom *map = NULL;
	size_t i;
	const char *range;
	unsigned low_id, high_id;
	int ret;

	range = idmap_config_const_string(domname, "range", NULL);
	if (range == NULL) {
		DBG_DEBUG("No range for domain %s found\n", domname);
		return false;
	}

	ret = sscanf(range, "%u - %u", &low_id, &high_id);
	if (ret != 2) {
		DBG_DEBUG("Invalid range spec \"%s\" for domain %s\n",
			  range, domname);
		return false;
	}

	if (low_id > high_id) {
		DBG_DEBUG("Invalid range %u - %u for domain %s\n",
			  low_id, high_id, domname);
		return false;
	}

	for (i=0; i<state->cfg->num_doms; i++) {
		if (strequal(domname, state->cfg->doms[i].name)) {
			map = &state->cfg->doms[i];
			break;
		}
	}

	if (map == NULL) {
		struct wb_parent_idmap_config_dom *tmp;
		char *name;

		name = talloc_strdup(state, domname);
		if (name == NULL) {
			DBG_ERR("talloc failed\n");
			return false;
		}

		tmp = talloc_realloc(
			NULL, state->cfg->doms, struct wb_parent_idmap_config_dom,
			state->cfg->num_doms+1);
		if (tmp == NULL) {
			DBG_ERR("talloc failed\n");
			return false;
		}
		state->cfg->doms = tmp;

		map = &state->cfg->doms[state->cfg->num_doms];
		state->cfg->num_doms += 1;
		ZERO_STRUCTP(map);
		map->name = talloc_move(state->cfg->doms, &name);
	}

	map->low_id = low_id;
	map->high_id = high_id;

	return false;
}

static void wb_parent_idmap_setup_lookupname_next(struct tevent_req *req)
{
	struct wb_parent_idmap_setup_state *state =
		tevent_req_data(req,
		struct wb_parent_idmap_setup_state);
	struct wb_parent_idmap_config_dom *dom =
		&state->cfg->doms[state->dom_idx];
	struct tevent_req *subreq = NULL;

 next_domain:
	if (state->dom_idx == state->cfg->num_doms) {
		/*
		 * We're done, so start the idmap child
		 */
		setup_child(NULL, &static_idmap_child, "log.winbindd", "idmap");
		tevent_req_done(req);
		return;
	}

	if (strequal(dom->name, "*")) {
		state->dom_idx++;
		goto next_domain;
	}

	subreq = wb_lookupname_send(state,
				    state->ev,
				    dom->name,
				    dom->name,
				    "",
				    LOOKUP_NAME_NO_NSS);
	if (tevent_req_nomem(subreq, req)) {
		return;
	}
	tevent_req_set_callback(subreq,
				wb_parent_idmap_setup_lookupname_done,
				req);
}

static void wb_parent_idmap_setup_lookupname_done(struct tevent_req *subreq)
{
	struct tevent_req *req =
		tevent_req_callback_data(subreq,
		struct tevent_req);
	struct wb_parent_idmap_setup_state *state =
		tevent_req_data(req,
		struct wb_parent_idmap_setup_state);
	struct wb_parent_idmap_config_dom *dom =
		&state->cfg->doms[state->dom_idx];
	enum lsa_SidType type;
	NTSTATUS status;

	status = wb_lookupname_recv(subreq, &dom->sid, &type);
	TALLOC_FREE(subreq);
	if (!NT_STATUS_IS_OK(status)) {
		DBG_ERR("Lookup domain name '%s' failed '%s'\n",
			dom->name,
			nt_errstr(status));

		state->dom_idx++;
		wb_parent_idmap_setup_lookupname_next(req);
		return;
	}

	if (type != SID_NAME_DOMAIN) {
		struct dom_sid_buf buf;

		DBG_ERR("SID %s for idmap domain name '%s' "
			"not a domain SID\n",
			dom_sid_str_buf(&dom->sid, &buf),
			dom->name);

		ZERO_STRUCT(dom->sid);
	}

	state->dom_idx++;
	wb_parent_idmap_setup_lookupname_next(req);

	return;
}

NTSTATUS wb_parent_idmap_setup_recv(struct tevent_req *req,
				    const struct wb_parent_idmap_config **_cfg)
{
	const struct wb_parent_idmap_config *cfg = &static_parent_idmap_config;
	NTSTATUS status;

	*_cfg = NULL;

	if (tevent_req_is_nterror(req, &status)) {
		tevent_req_received(req);
		return status;
	}

	/*
	 * Note state->cfg is already set to NULL by
	 * wb_parent_idmap_setup_cleanup()
	 */
	*_cfg = cfg;
	tevent_req_received(req);
	return NT_STATUS_OK;
}