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

   Do a netr_DsrUpdateReadOnlyServerDnsRecords to a remote DC

   Copyright (C) Andrew Bartlett 2010
   Copyright (C) Andrew Tridgell 2010

   based heavily on wb_sam_logon.c which is copyright:

   Copyright (C) Volker Lendecke 2005
   Copyright (C) Andrew Bartlett 2005
   Copyright (C) Stefan Metzmacher 2006

   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 "auth/credentials/credentials.h"
#include "libcli/auth/libcli_auth.h"
#include "librpc/gen_ndr/ndr_netlogon_c.h"
#include "librpc/gen_ndr/winbind.h"

struct wb_update_rodc_dns_state {
	struct tevent_context *ev;

	struct winbind_DsrUpdateReadOnlyServerDnsRecords *req;

	struct wbsrv_domain *domain;
	struct tevent_queue_entry *queue_entry;
        struct netlogon_creds_CredentialState *creds_state;
        struct netr_Authenticator auth1, auth2;

	TALLOC_CTX *r_mem_ctx;
        struct netr_DsrUpdateReadOnlyServerDnsRecords r;
};

static void wb_update_rodc_dns_recv_domain(struct composite_context *csubreq);
static void wb_sam_logon_queue_trigger(struct tevent_req *req, void *priv);
static void wb_update_rodc_dns_recv_response(struct tevent_req *subreq);

/*
    Find the connection to the DC (or find an existing connection)
*/
struct tevent_req *wb_update_rodc_dns_send(TALLOC_CTX *mem_ctx,
					   struct tevent_context *ev,
					   struct wbsrv_service *service,
					   struct winbind_DsrUpdateReadOnlyServerDnsRecords *_req)
{
	struct tevent_req *req;
	struct wb_update_rodc_dns_state *state;
	struct composite_context *csubreq;

	req = tevent_req_create(mem_ctx, &state,
				struct wb_update_rodc_dns_state);
	if (req == NULL) {
		return NULL;
	}
	state->ev = ev;
	state->req = _req;

	csubreq = wb_sid2domain_send(state, service, service->primary_sid);
	if (tevent_req_nomem(csubreq, req)) {
		return tevent_req_post(req, ev);
	}
	csubreq->async.fn = wb_update_rodc_dns_recv_domain;
	csubreq->async.private_data = req;

	return req;
}

/*
    Having finished making the connection to the DC
    Send of a DsrUpdateReadOnlyServerDnsRecords request to authenticate a user.
*/
static void wb_update_rodc_dns_recv_domain(struct composite_context *csubreq)
{
	struct tevent_req *req =
		talloc_get_type_abort(csubreq->async.private_data,
		struct tevent_req);
	struct wb_update_rodc_dns_state *state =
		tevent_req_data(req,
		struct wb_update_rodc_dns_state);
	NTSTATUS status;
	struct tevent_queue_entry *e;

	status = wb_sid2domain_recv(csubreq, &state->domain);
	if (tevent_req_nterror(req, status)) {
		return;
	}

	/*
	 * Because of the netlogon_creds behavior we have to
	 * queue the netr_LogonSamLogon() calls
	 */
	e = tevent_queue_add_entry(state->domain->netlogon_queue,
				   state->ev,
				   req,
				   wb_sam_logon_queue_trigger,
				   NULL);
	state->queue_entry = e;
}

static void wb_sam_logon_queue_trigger(struct tevent_req *req, void *priv)
{
	struct wb_update_rodc_dns_state *state =
		tevent_req_data(req,
		struct wb_update_rodc_dns_state);
	struct wbsrv_domain *domain = state->domain;
	struct tevent_req *subreq;

	state->creds_state = cli_credentials_get_netlogon_creds(domain->libnet_ctx->cred);
	netlogon_creds_client_authenticator(state->creds_state, &state->auth1);

	state->r.in.server_name = talloc_asprintf(state, "\\\\%s",
			      dcerpc_server_name(domain->netlogon_pipe));
	if (tevent_req_nomem(state->r.in.server_name, req)) {
		return;
	}

	state->r.in.computer_name = cli_credentials_get_workstation(domain->libnet_ctx->cred);
	state->r.in.credential = &state->auth1;
	state->r.out.return_authenticator = &state->auth2;
	state->r.in.site_name = state->req->in.site_name;
	state->r.in.dns_ttl = state->req->in.dns_ttl;
	state->r.in.dns_names = state->req->in.dns_names;
	state->r.out.dns_names = state->req->in.dns_names;

	/*
	 * use a new talloc context for the DsrUpdateReadOnlyServerDnsRecords call
	 * because then we can just to a talloc_steal on this context
	 * in the final _recv() function to give the caller all the content of
	 * the s->r.out.dns_names
	 */
	state->r_mem_ctx = talloc_new(state);
	if (tevent_req_nomem(state->r_mem_ctx, req)) {
		return;
	}

	subreq = dcerpc_netr_DsrUpdateReadOnlyServerDnsRecords_r_send(state,
						state->ev,
						domain->netlogon_pipe->binding_handle,
						&state->r);
	if (tevent_req_nomem(subreq, req)) {
		return;
	}
	tevent_req_set_callback(subreq, wb_update_rodc_dns_recv_response, req);
}

/*
   NTLM Authentication

   Check the DsrUpdateReadOnlyServerDnsRecords reply and decrypt the session keys
*/
static void wb_update_rodc_dns_recv_response(struct tevent_req *subreq)
{
	struct tevent_req *req =
		tevent_req_callback_data(subreq,
		struct tevent_req);
	struct wb_update_rodc_dns_state *state =
		tevent_req_data(req,
		struct wb_update_rodc_dns_state);
	NTSTATUS status;
	bool ok;

	status = dcerpc_netr_DsrUpdateReadOnlyServerDnsRecords_r_recv(subreq,
								state->r_mem_ctx);
	TALLOC_FREE(subreq);
	if (tevent_req_nterror(req, status)) {
		return;
	}

	if (tevent_req_nterror(req, state->r.out.result)) {
		return;
	}

	if (state->r.out.return_authenticator == NULL) {
		tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
		return;
	}

	ok = netlogon_creds_client_check(state->creds_state,
				&state->r.out.return_authenticator->cred);
	if (!ok) {
		DEBUG(0, ("Credentials check failed!\n"));
		tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
		return;
	}

	/*
	 * we do not need the netlogon_creds lock anymore
	 */
	TALLOC_FREE(state->queue_entry);

	tevent_req_done(req);
}

NTSTATUS wb_update_rodc_dns_recv(struct tevent_req *req,
			TALLOC_CTX *mem_ctx,
			struct winbind_DsrUpdateReadOnlyServerDnsRecords *_req)
{
	struct wb_update_rodc_dns_state *state =
		tevent_req_data(req,
		struct wb_update_rodc_dns_state);
	NTSTATUS status;

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

	talloc_steal(mem_ctx, state->r_mem_ctx);
	_req->out.dns_names = state->r.out.dns_names;

	tevent_req_received(req);
	return NT_STATUS_OK;
}