summaryrefslogtreecommitdiff
path: root/source3/winbindd/winbindd_irpc.c
blob: c6c786c8be162fd31b300658ac496c269ff65249 (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
/*
   Unix SMB/CIFS implementation.
   async implementation of commands submitted over IRPC
   Copyright (C) Volker Lendecke 2009
   Copyright (C) Guenther Deschner 2009
   Copyright (C) Andrew Bartlett 2014
   Copyright (C) Andrew Tridgell 2009

   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 "librpc/gen_ndr/ndr_winbind_c.h"
#include "source4/lib/messaging/irpc.h"
#include "librpc/gen_ndr/ndr_winbind.h"

struct wb_irpc_forward_state {
	struct irpc_message *msg;
	struct winbind_DsrUpdateReadOnlyServerDnsRecords *req;

	const char *opname;
	struct dcesrv_call_state *dce_call;
};

/*
  called when the forwarded rpc request is finished
 */
static void wb_irpc_forward_callback(struct tevent_req *subreq)
{
	struct wb_irpc_forward_state *st =
		tevent_req_callback_data(subreq,
		struct wb_irpc_forward_state);
	const char *opname = st->opname;
	NTSTATUS status;

	status = dcerpc_binding_handle_call_recv(subreq);
	TALLOC_FREE(subreq);
	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(0,("RPC callback failed for %s - %s\n",
			 opname, nt_errstr(status)));
		irpc_send_reply(st->msg, status);
		return;
	}

	irpc_send_reply(st->msg, status);
}



/**
 * Forward a RPC call using IRPC to another task
 */

static NTSTATUS wb_irpc_forward_rpc_call(struct irpc_message *msg, TALLOC_CTX *mem_ctx,
					 struct tevent_context *ev,
					 void *r, uint32_t callid,
					 const char *opname,
					 struct winbindd_domain *domain,
					 uint32_t timeout)
{
	struct wb_irpc_forward_state *st;
	struct dcerpc_binding_handle *binding_handle;
	struct tevent_req *subreq;

	st = talloc(mem_ctx, struct wb_irpc_forward_state);
	if (st == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	st->msg = msg;
	st->opname   = opname;

	binding_handle =  dom_child_handle(domain);
	if (binding_handle == NULL) {
		DEBUG(0,("%s: Failed to forward request to winbind handler for %s\n",
			 opname, domain->name));
		return NT_STATUS_UNSUCCESSFUL;
	}

	/* reset timeout for the handle */
	dcerpc_binding_handle_set_timeout(binding_handle, timeout);

	/* forward the call */
	subreq = dcerpc_binding_handle_call_send(st, ev,
						 binding_handle,
						 NULL, &ndr_table_winbind,
						 callid,
						 msg, r);
	if (subreq == NULL) {
		DEBUG(0,("%s: Failed to forward request to winbind handler for %s\n",
			 opname, domain->name));
		return NT_STATUS_UNSUCCESSFUL;
	}

	/* mark the request as replied async */
	msg->defer_reply = true;

	/* setup the callback */
	tevent_req_set_callback(subreq, wb_irpc_forward_callback, st);
	return NT_STATUS_OK;
}

static NTSTATUS wb_irpc_DsrUpdateReadOnlyServerDnsRecords(struct irpc_message *msg,
						   struct winbind_DsrUpdateReadOnlyServerDnsRecords *req)
{
	struct winbindd_domain *domain = find_our_domain();
	if (domain == NULL) {
		return NT_STATUS_NO_SUCH_DOMAIN;
	}

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

	return wb_irpc_forward_rpc_call(msg, msg,
					winbind_event_context(),
					req, NDR_WINBIND_DSRUPDATEREADONLYSERVERDNSRECORDS,
					"winbind_DsrUpdateReadOnlyServerDnsRecords",
					domain, IRPC_CALL_TIMEOUT);
}

static NTSTATUS wb_irpc_SamLogon(struct irpc_message *msg,
				 struct winbind_SamLogon *req)
{
	struct winbindd_domain *domain;
	const char *target_domain_name;
	if (req->in.logon.network == NULL) {
		return NT_STATUS_REQUEST_NOT_ACCEPTED;
	}
	target_domain_name = req->in.logon.network->identity_info.domain_name.string;

	domain = find_auth_domain(0, target_domain_name);
	if (domain == NULL) {
		req->out.result = NT_STATUS_NO_SUCH_USER;
		req->out.authoritative = 0;
		return NT_STATUS_OK;
	}

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

	return wb_irpc_forward_rpc_call(msg, msg,
					winbind_event_context(),
					req, NDR_WINBIND_SAMLOGON,
					"winbind_SamLogon",
					domain, IRPC_CALL_TIMEOUT);
}

static NTSTATUS wb_irpc_LogonControl(struct irpc_message *msg,
				     struct winbind_LogonControl *req)
{
	TALLOC_CTX *frame = talloc_stackframe();
	char *domain_name = NULL;
	struct winbindd_domain *domain = NULL;

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

	switch (req->in.function_code) {
	case NETLOGON_CONTROL_REDISCOVER:
	case NETLOGON_CONTROL_TC_QUERY:
	case NETLOGON_CONTROL_CHANGE_PASSWORD:
	case NETLOGON_CONTROL_TC_VERIFY:
		if (req->in.data->domain == NULL) {
			TALLOC_FREE(frame);
			return NT_STATUS_INVALID_PARAMETER;
		}

		domain_name = talloc_strdup(frame, req->in.data->domain);
		if (domain_name == NULL) {
			req->out.result = WERR_NOT_ENOUGH_MEMORY;
			TALLOC_FREE(frame);
			return NT_STATUS_OK;
		}

		break;
	default:
		TALLOC_FREE(frame);
		return NT_STATUS_NOT_IMPLEMENTED;
	}

	if (req->in.function_code == NETLOGON_CONTROL_REDISCOVER) {
		char *p = NULL;

		/*
		 * NETLOGON_CONTROL_REDISCOVER
		 * get's an optional \dcname appended to the domain name
		 */
		p = strchr_m(domain_name, '\\');
		if (p != NULL) {
			*p = '\0';
		}
	}

	domain = find_domain_from_name_noinit(domain_name);
	if (domain == NULL) {
		req->out.result = WERR_NO_SUCH_DOMAIN;
		TALLOC_FREE(frame);
		return NT_STATUS_OK;
	}

	TALLOC_FREE(frame);
	return wb_irpc_forward_rpc_call(msg, msg,
					winbind_event_context(),
					req, NDR_WINBIND_LOGONCONTROL,
					"winbind_LogonControl",
					domain, 45 /* timeout */);
}

static NTSTATUS wb_irpc_GetForestTrustInformation(struct irpc_message *msg,
				     struct winbind_GetForestTrustInformation *req)
{
	struct winbindd_domain *domain = NULL;

	if (req->in.trusted_domain_name == NULL) {
		req->out.result = WERR_NO_SUCH_DOMAIN;
		return NT_STATUS_OK;
	}

	domain = find_domain_from_name_noinit(req->in.trusted_domain_name);
	if (domain == NULL) {
		req->out.result = WERR_NO_SUCH_DOMAIN;
		return NT_STATUS_OK;
	}

	/*
	 * checking for domain->internal and domain->primary
	 * makes sure we only do some work when running as DC.
	 */

	if (domain->internal) {
		req->out.result = WERR_NO_SUCH_DOMAIN;
		return NT_STATUS_OK;
	}

	if (domain->primary) {
		req->out.result = WERR_NO_SUCH_DOMAIN;
		return NT_STATUS_OK;
	}

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

	return wb_irpc_forward_rpc_call(msg, msg,
					winbind_event_context(),
					req, NDR_WINBIND_GETFORESTTRUSTINFORMATION,
					"winbind_GetForestTrustInformation",
					domain, 45 /* timeout */);
}

static NTSTATUS wb_irpc_SendToSam(struct irpc_message *msg,
				  struct winbind_SendToSam *req)
{
	/* TODO make sure that it is RWDC */
	struct winbindd_domain *domain = find_our_domain();
	if (domain == NULL) {
		return NT_STATUS_NO_SUCH_DOMAIN;
	}

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

	return wb_irpc_forward_rpc_call(msg, msg,
					winbind_event_context(),
					req, NDR_WINBIND_SENDTOSAM,
					"winbind_SendToSam",
					domain, IRPC_CALL_TIMEOUT);
}

NTSTATUS wb_irpc_register(void)
{
	NTSTATUS status;

	status = IRPC_REGISTER(winbind_imessaging_context(), winbind, WINBIND_DSRUPDATEREADONLYSERVERDNSRECORDS,
			       wb_irpc_DsrUpdateReadOnlyServerDnsRecords, NULL);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}
	status = IRPC_REGISTER(winbind_imessaging_context(), winbind, WINBIND_SAMLOGON,
			       wb_irpc_SamLogon, NULL);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}
	status = IRPC_REGISTER(winbind_imessaging_context(), winbind,
			       WINBIND_LOGONCONTROL,
			       wb_irpc_LogonControl, NULL);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}
	status = IRPC_REGISTER(winbind_imessaging_context(), winbind,
			       WINBIND_GETFORESTTRUSTINFORMATION,
			       wb_irpc_GetForestTrustInformation, NULL);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}
	status = IRPC_REGISTER(winbind_imessaging_context(), winbind, WINBIND_SENDTOSAM,
			       wb_irpc_SendToSam, NULL);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	return NT_STATUS_OK;
}