s4-join: factorize code, add info
[Samba/gbeck.git] / source4 / winbind / wb_update_rodc_dns.c
blobbd29d7865c143f8514896b868cd4fb1f55faa2b7
1 /*
2 Unix SMB/CIFS implementation.
4 Do a netr_DsrUpdateReadOnlyServerDnsRecords to a remote DC
6 Copyright (C) Andrew Bartlett 2010
7 Copyright (C) Andrew Tridgell 2010
9 based heavily on wb_sam_logon.c which is copyright:
11 Copyright (C) Volker Lendecke 2005
12 Copyright (C) Andrew Bartlett 2005
13 Copyright (C) Stefan Metzmacher 2006
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 3 of the License, or
18 (at your option) any later version.
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 #include "includes.h"
30 #include <tevent.h>
31 #include "../lib/util/tevent_ntstatus.h"
32 #include "libcli/composite/composite.h"
33 #include "winbind/wb_server.h"
34 #include "smbd/service_task.h"
35 #include "auth/credentials/credentials.h"
36 #include "libcli/auth/libcli_auth.h"
37 #include "librpc/gen_ndr/ndr_netlogon_c.h"
38 #include "librpc/gen_ndr/winbind.h"
40 struct wb_update_rodc_dns_state {
41 struct tevent_context *ev;
43 struct winbind_DsrUpdateReadOnlyServerDnsRecords *req;
45 struct wbsrv_domain *domain;
46 struct tevent_queue_entry *queue_entry;
47 struct netlogon_creds_CredentialState *creds_state;
48 struct netr_Authenticator auth1, auth2;
50 TALLOC_CTX *r_mem_ctx;
51 struct netr_DsrUpdateReadOnlyServerDnsRecords r;
54 static void wb_update_rodc_dns_recv_domain(struct composite_context *csubreq);
55 static void wb_sam_logon_queue_trigger(struct tevent_req *req, void *priv);
56 static void wb_update_rodc_dns_recv_response(struct tevent_req *subreq);
59 Find the connection to the DC (or find an existing connection)
61 struct tevent_req *wb_update_rodc_dns_send(TALLOC_CTX *mem_ctx,
62 struct tevent_context *ev,
63 struct wbsrv_service *service,
64 struct winbind_DsrUpdateReadOnlyServerDnsRecords *_req)
66 struct tevent_req *req;
67 struct wb_update_rodc_dns_state *state;
68 struct composite_context *csubreq;
70 req = tevent_req_create(mem_ctx, &state,
71 struct wb_update_rodc_dns_state);
72 if (req == NULL) {
73 return NULL;
75 state->ev = ev;
76 state->req = _req;
78 csubreq = wb_sid2domain_send(state, service, service->primary_sid);
79 if (tevent_req_nomem(csubreq, req)) {
80 return tevent_req_post(req, ev);
82 csubreq->async.fn = wb_update_rodc_dns_recv_domain;
83 csubreq->async.private_data = req;
85 return req;
89 Having finished making the connection to the DC
90 Send of a DsrUpdateReadOnlyServerDnsRecords request to authenticate a user.
92 static void wb_update_rodc_dns_recv_domain(struct composite_context *csubreq)
94 struct tevent_req *req =
95 talloc_get_type_abort(csubreq->async.private_data,
96 struct tevent_req);
97 struct wb_update_rodc_dns_state *state =
98 tevent_req_data(req,
99 struct wb_update_rodc_dns_state);
100 NTSTATUS status;
101 struct tevent_queue_entry *e;
103 status = wb_sid2domain_recv(csubreq, &state->domain);
104 if (tevent_req_nterror(req, status)) {
105 return;
109 * Because of the netlogon_creds behavior we have to
110 * queue the netr_LogonSamLogon() calls
112 e = tevent_queue_add_entry(state->domain->netlogon_queue,
113 state->ev,
114 req,
115 wb_sam_logon_queue_trigger,
116 NULL);
117 state->queue_entry = e;
120 static void wb_sam_logon_queue_trigger(struct tevent_req *req, void *priv)
122 struct wb_update_rodc_dns_state *state =
123 tevent_req_data(req,
124 struct wb_update_rodc_dns_state);
125 struct wbsrv_domain *domain = state->domain;
126 struct tevent_req *subreq;
128 state->creds_state = cli_credentials_get_netlogon_creds(domain->libnet_ctx->cred);
129 netlogon_creds_client_authenticator(state->creds_state, &state->auth1);
131 state->r.in.server_name = talloc_asprintf(state, "\\\\%s",
132 dcerpc_server_name(domain->netlogon_pipe));
133 if (tevent_req_nomem(state->r.in.server_name, req)) {
134 return;
137 state->r.in.computer_name = cli_credentials_get_workstation(domain->libnet_ctx->cred);
138 state->r.in.credential = &state->auth1;
139 state->r.out.return_authenticator = &state->auth2;
140 state->r.in.site_name = state->req->in.site_name;
141 state->r.in.dns_ttl = state->req->in.dns_ttl;
142 state->r.in.dns_names = state->req->in.dns_names;
143 state->r.out.dns_names = state->req->in.dns_names;
146 * use a new talloc context for the DsrUpdateReadOnlyServerDnsRecords call
147 * because then we can just to a talloc_steal on this context
148 * in the final _recv() function to give the caller all the content of
149 * the s->r.out.dns_names
151 state->r_mem_ctx = talloc_new(state);
152 if (tevent_req_nomem(state->r_mem_ctx, req)) {
153 return;
156 subreq = dcerpc_netr_DsrUpdateReadOnlyServerDnsRecords_r_send(state,
157 state->ev,
158 domain->netlogon_pipe->binding_handle,
159 &state->r);
160 if (tevent_req_nomem(subreq, req)) {
161 return;
163 tevent_req_set_callback(subreq, wb_update_rodc_dns_recv_response, req);
167 NTLM Authentication
169 Check the DsrUpdateReadOnlyServerDnsRecords reply and decrypt the session keys
171 static void wb_update_rodc_dns_recv_response(struct tevent_req *subreq)
173 struct tevent_req *req =
174 tevent_req_callback_data(subreq,
175 struct tevent_req);
176 struct wb_update_rodc_dns_state *state =
177 tevent_req_data(req,
178 struct wb_update_rodc_dns_state);
179 NTSTATUS status;
180 bool ok;
182 status = dcerpc_netr_DsrUpdateReadOnlyServerDnsRecords_r_recv(subreq,
183 state->r_mem_ctx);
184 TALLOC_FREE(subreq);
185 if (tevent_req_nterror(req, status)) {
186 return;
189 if (tevent_req_nterror(req, state->r.out.result)) {
190 return;
193 if (state->r.out.return_authenticator == NULL) {
194 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
195 return;
198 ok = netlogon_creds_client_check(state->creds_state,
199 &state->r.out.return_authenticator->cred);
200 if (!ok) {
201 DEBUG(0, ("Credentials check failed!\n"));
202 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
203 return;
207 * we do not need the netlogon_creds lock anymore
209 TALLOC_FREE(state->queue_entry);
211 tevent_req_done(req);
214 NTSTATUS wb_update_rodc_dns_recv(struct tevent_req *req,
215 TALLOC_CTX *mem_ctx,
216 struct winbind_DsrUpdateReadOnlyServerDnsRecords *_req)
218 struct wb_update_rodc_dns_state *state =
219 tevent_req_data(req,
220 struct wb_update_rodc_dns_state);
221 NTSTATUS status;
223 if (tevent_req_is_nterror(req, &status)) {
224 tevent_req_received(req);
225 return status;
228 talloc_steal(mem_ctx, state->r_mem_ctx);
229 _req->out.dns_names = state->r.out.dns_names;
231 tevent_req_received(req);
232 return NT_STATUS_OK;