summaryrefslogtreecommitdiffstats
path: root/source/winbind/wb_sid2domain.c
blob: af3add42fe2c32056134e6617cbb5a1c641dc097 (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
/* 
   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 2 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, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "libcli/composite/composite.h"
#include "winbind/wb_server.h"
#include "smbd/service_stream.h"
#include "smbd/service_task.h"
#include "lib/events/events.h"
#include "librpc/gen_ndr/nbt.h"
#include "librpc/gen_ndr/ndr_netlogon.h"
#include "winbind/wb_async_helpers.h"
#include "include/dlinklist.h"

static const char *sam_name(void)
{
	if (lp_server_role() == ROLE_STANDALONE) {
		return lp_netbios_name();
	}
	return lp_workgroup();
}

static struct wbsrv_domain *find_primary_domain(struct wbsrv_service *service)
{
	const char *my_domain_name = sam_name();
	struct wbsrv_domain *domain;

	for (domain = service->domains; domain!=NULL; domain = domain->next) {
		if (strcasecmp(domain->name, my_domain_name) == 0) {
			break;
		}
	}
	return domain;
}

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->sid, sid)) {
			break;
		}
		if (dom_sid_in_domain(domain->sid, sid)) {
			break;
		}
	}
	return domain;
}

struct sid2domain_state {
	struct composite_context *ctx;
	struct wbsrv_service *service;
	const struct dom_sid *sid;

	const char *domain_name;
	const char *dc_name;
	struct dom_sid *domain_sid;

	struct wbsrv_domain *my_domain;
	struct wbsrv_domain *result;
};

static void sid2domain_recv_name(struct composite_context *ctx);
static void sid2domain_recv_dcname(struct composite_context *ctx);
static void sid2domain_recv_init(struct composite_context *ctx);

struct composite_context *wb_sid2domain_send(struct wbsrv_service *service,
					     const struct dom_sid *sid)
{
	struct composite_context *result, *ctx;
	struct sid2domain_state *state;

	result = talloc_zero(NULL, struct composite_context);
	if (result == NULL) goto failed;
	result->state = COMPOSITE_STATE_IN_PROGRESS;
	result->async.fn = NULL;
	result->event_ctx = service->task->event_ctx;

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

	state->service = service;
	state->sid = dom_sid_dup(state, sid);
	if (state->sid == NULL) goto failed;

	state->result = find_domain_from_sid(service, sid);
	if (state->result != NULL) {
		result->status = NT_STATUS_OK;
		composite_trigger_done(result);
		return result;
	}

	state->my_domain = find_primary_domain(service);
	if (state->my_domain == NULL) {
		result->status = NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
		composite_trigger_error(result);
		return result;
	}

	ctx = wb_cmd_lookupsid_send(service, state->my_domain, state->sid);
	if (ctx == NULL) goto failed;
	ctx->async.fn = sid2domain_recv_name;
	ctx->async.private_data = state;
	return result;

 failed:
	talloc_free(result);
	return NULL;
}

static void sid2domain_recv_name(struct composite_context *ctx)
{
	struct sid2domain_state *state =
		talloc_get_type(ctx->async.private_data,
				struct sid2domain_state);
	struct wb_sid_object *name;

	state->ctx->status = wb_cmd_lookupsid_recv(ctx, state, &name);
	if (!composite_is_ok(state->ctx)) return;

	if (name->type == SID_NAME_UNKNOWN) {
		composite_error(state->ctx, NT_STATUS_NO_SUCH_DOMAIN);
		return;
	}

	state->domain_name = name->domain;
	state->domain_sid = dom_sid_dup(state, state->sid);
	if (name->type != SID_NAME_DOMAIN) {
		state->domain_sid->num_auths -= 1;
	}
	
	ctx = wb_cmd_getdcname_send(state->service, state->my_domain,
				    state->domain_name);
	composite_continue(state->ctx, ctx, sid2domain_recv_dcname, state);
}

static void sid2domain_recv_dcname(struct composite_context *ctx)
{
	struct sid2domain_state *state =
		talloc_get_type(ctx->async.private_data,
				struct sid2domain_state);

	state->ctx->status = wb_cmd_getdcname_recv(ctx, state,
						   &state->dc_name);
	if (!composite_is_ok(state->ctx)) return;

	state->result = talloc_zero(state, struct wbsrv_domain);
	if (composite_nomem(state->result, state->ctx)) return;

	state->result->name = talloc_steal(state->result, state->domain_name);
	state->result->sid = talloc_steal(state->result, state->domain_sid);
	state->result->dcname = talloc_steal(state->result, state->dc_name);

	state->result->schannel_creds = cli_credentials_init(state->result);
	if (composite_nomem(state->result->schannel_creds, state->ctx)) return;
	cli_credentials_set_conf(state->result->schannel_creds);
	cli_credentials_set_anonymous(state->result->schannel_creds);

	talloc_steal(state->service, state->result);
	DLIST_ADD(state->service->domains, state->result);

	ctx = wb_init_domain_send(state->result,
				  state->service->task->event_ctx,
				  state->service->task->msg_ctx);
	composite_continue(state->ctx, ctx, sid2domain_recv_init, state);
}

static void sid2domain_recv_init(struct composite_context *ctx)
{
	struct sid2domain_state *state =
		talloc_get_type(ctx->async.private_data,
				struct sid2domain_state);

	state->ctx->status = wb_init_domain_recv(ctx);
	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->result;
	}
	talloc_free(ctx);
	return status;
}

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