summaryrefslogtreecommitdiff
path: root/source4/winbind/wb_sid2domain.c
blob: 172a6d0a09aeec2da6f37993cb21e5801dde88e1 (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
/* 
   Unix SMB/CIFS implementation.

   Find and init a domain struct for a SID

   Copyright (C) Volker Lendecke 2005
   
   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 <tevent.h>
#include "../lib/util/tevent_ntstatus.h"
#include "libcli/composite/composite.h"
#include "winbind/wb_server.h"
#include "smbd/service_task.h"
#include "libcli/security/security.h"
#include "../lib/util/dlinklist.h"
#include "param/param.h"

static struct wbsrv_domain *find_domain_from_sid(struct wbsrv_service *service,
						 const struct dom_sid *sid)
{
	struct wbsrv_domain *domain;

	for (domain = service->domains; domain!=NULL; domain = domain->next) {
		if (dom_sid_equal(domain->info->sid, sid)) {
			break;
		}
		if (dom_sid_in_domain(domain->info->sid, sid)) {
			break;
		}
	}
	return domain;
}

struct wb_sid2domain_state {
	struct wbsrv_service *service;
	struct dom_sid sid;

	struct wbsrv_domain *domain;
};

static void wb_sid2domain_recv_dom_info(struct composite_context *ctx);
static void wb_sid2domain_recv_name(struct composite_context *ctx);
static void wb_sid2domain_recv_trusted_dom_info(struct composite_context *ctx);
static void wb_sid2domain_recv_init(struct composite_context *ctx);

static struct tevent_req *_wb_sid2domain_send(TALLOC_CTX *mem_ctx,
					      struct tevent_context *ev,
					      struct wbsrv_service *service,
					      const struct dom_sid *sid)
{
	struct tevent_req *req;
	struct wb_sid2domain_state *state;
	struct composite_context *ctx;

	DEBUG(5, ("wb_sid2domain_send called\n"));

	req = tevent_req_create(mem_ctx, &state,
				struct wb_sid2domain_state);
	if (req == NULL) {
		return NULL;
	}

	state->service = service;
	state->sid = *sid;

	state->domain = find_domain_from_sid(service, sid);
	if (state->domain != NULL) {
		tevent_req_done(req);
		return tevent_req_post(req, ev);
	}

	if (dom_sid_equal(service->primary_sid, sid) ||
	    dom_sid_in_domain(service->primary_sid, sid)) {
		ctx = wb_get_dom_info_send(state, service,
					   lpcfg_workgroup(service->task->lp_ctx),
					   lpcfg_realm(service->task->lp_ctx),
					   service->primary_sid);
		if (tevent_req_nomem(ctx, req)) {
			return tevent_req_post(req, ev);
		}
		ctx->async.fn = wb_sid2domain_recv_dom_info;
		ctx->async.private_data = req;

		return req;
	}

	if (dom_sid_equal(&global_sid_Builtin, sid) ||
	    dom_sid_in_domain(&global_sid_Builtin, sid)) {
		ctx = wb_get_dom_info_send(state, service,
					   "BUILTIN", NULL,
					   &global_sid_Builtin);
		if (tevent_req_nomem(ctx, req)) {
			return tevent_req_post(req, ev);
		}
		ctx->async.fn = wb_sid2domain_recv_dom_info;
		ctx->async.private_data = req;

		return req;
	}

	ctx = wb_cmd_lookupsid_send(state, service, &state->sid);
	if (tevent_req_nomem(ctx, req)) {
		return tevent_req_post(req, ev);
	}
	ctx->async.fn = wb_sid2domain_recv_name;
	ctx->async.private_data = req;

	return req;
}

static void wb_sid2domain_recv_dom_info(struct composite_context *ctx)
{
	struct tevent_req *req =
		talloc_get_type_abort(ctx->async.private_data,
		struct tevent_req);
	struct wb_sid2domain_state *state =
		tevent_req_data(req,
		struct wb_sid2domain_state);
	struct wb_dom_info *info;
	NTSTATUS status;

	status = wb_get_dom_info_recv(ctx, state, &info);
	if (tevent_req_nterror(req, status)) {
		return;
	}

	ctx = wb_init_domain_send(state, state->service, info);
	if (tevent_req_nomem(ctx, req)) {
		return;
	}
	ctx->async.fn = wb_sid2domain_recv_init;
	ctx->async.private_data = req;
}

static void wb_sid2domain_recv_name(struct composite_context *ctx)
{
	struct tevent_req *req =
		talloc_get_type_abort(ctx->async.private_data,
		struct tevent_req);
	struct wb_sid2domain_state *state =
		tevent_req_data(req,
		struct wb_sid2domain_state);
	struct wb_sid_object *name;
	NTSTATUS status;

	status = wb_cmd_lookupsid_recv(ctx, state, &name);
	if (tevent_req_nterror(req, status)) {
		return;
	}

	if (name->type == SID_NAME_UNKNOWN) {
		tevent_req_nterror(req, NT_STATUS_NO_SUCH_DOMAIN);
		return;
	}

	if (name->type != SID_NAME_DOMAIN) {
		state->sid.num_auths -= 1;
	}

	ctx = wb_trusted_dom_info_send(state, state->service, name->domain,
				       &state->sid);
	if (tevent_req_nomem(ctx, req)) {
		return;
	}
	ctx->async.fn = wb_sid2domain_recv_trusted_dom_info;
	ctx->async.private_data = req;
}

static void wb_sid2domain_recv_trusted_dom_info(struct composite_context *ctx)
{
	struct tevent_req *req =
		talloc_get_type_abort(ctx->async.private_data,
		struct tevent_req);
	struct wb_sid2domain_state *state =
		tevent_req_data(req,
		struct wb_sid2domain_state);
	struct wb_dom_info *info;
	NTSTATUS status;

	status = wb_trusted_dom_info_recv(ctx, state, &info);
	if (tevent_req_nterror(req, status)) {
		return;
	}

	ctx = wb_init_domain_send(state, state->service, info);
	if (tevent_req_nomem(ctx, req)) {
		return;
	}
	ctx->async.fn = wb_sid2domain_recv_init;
	ctx->async.private_data = req;
}

static void wb_sid2domain_recv_init(struct composite_context *ctx)
{
	struct tevent_req *req =
		talloc_get_type_abort(ctx->async.private_data,
		struct tevent_req);
	struct wb_sid2domain_state *state =
		tevent_req_data(req,
		struct wb_sid2domain_state);
	struct wbsrv_domain *existing;
	NTSTATUS status;

	status = wb_init_domain_recv(ctx, state, &state->domain);
	if (tevent_req_nterror(req, status)) {
		DEBUG(10, ("Could not init domain\n"));
		return;
	}

	existing = find_domain_from_sid(state->service, &state->sid);
	if (existing != NULL) {
		DEBUG(5, ("Initialized domain twice, dropping second one\n"));
		talloc_free(state->domain);
		state->domain = existing;
	} else {
		talloc_steal(state->service, state->domain);
		DLIST_ADD(state->service->domains, state->domain);
	}

	tevent_req_done(req);
}

static NTSTATUS _wb_sid2domain_recv(struct tevent_req *req,
				    struct wbsrv_domain **result)
{
	struct wb_sid2domain_state *state =
		tevent_req_data(req,
		struct wb_sid2domain_state);
	NTSTATUS status;

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

	*result = state->domain;
	tevent_req_received(req);
	return NT_STATUS_OK;
}

struct sid2domain_state {
	struct composite_context *ctx;
	struct wbsrv_domain *domain;
};

static void sid2domain_recv_domain(struct tevent_req *subreq);

struct composite_context *wb_sid2domain_send(TALLOC_CTX *mem_ctx,
					     struct wbsrv_service *service,
					     const struct dom_sid *sid)
{
	struct composite_context *result;
	struct sid2domain_state *state;
	struct tevent_req *subreq;

	DEBUG(5, ("wb_sid2domain_send called\n"));
	result = composite_create(mem_ctx, service->task->event_ctx);
	if (result == NULL) goto failed;

	state = talloc(result, struct sid2domain_state);
	if (state == NULL) goto failed;
	state->ctx = result;
	result->private_data = state;

	subreq = _wb_sid2domain_send(state,
				     result->event_ctx,
				     service, sid);
	if (subreq == NULL) goto failed;
	tevent_req_set_callback(subreq, sid2domain_recv_domain, state);

	return result;

 failed:
	talloc_free(result);
	return NULL;

}

static void sid2domain_recv_domain(struct tevent_req *subreq)
{
	struct sid2domain_state *state =
		tevent_req_callback_data(subreq,
				struct sid2domain_state);

	state->ctx->status = _wb_sid2domain_recv(subreq, &state->domain);
	TALLOC_FREE(subreq);
	if (!composite_is_ok(state->ctx)) return;

	composite_done(state->ctx);
}

NTSTATUS wb_sid2domain_recv(struct composite_context *ctx,
			    struct wbsrv_domain **result)
{
	NTSTATUS status = composite_wait(ctx);
	if (NT_STATUS_IS_OK(status)) {
		struct sid2domain_state *state =
			talloc_get_type(ctx->private_data,
					struct sid2domain_state);
		*result = state->domain;
	}
	talloc_free(ctx);
	return status;
}

NTSTATUS wb_sid2domain(TALLOC_CTX *mem_ctx, struct wbsrv_service *service,
		       const struct dom_sid *sid,
		       struct wbsrv_domain **result)
{
	struct composite_context *c = wb_sid2domain_send(mem_ctx, service,
							 sid);
	return wb_sid2domain_recv(c, result);
}