s4-idmap: Add mapping using uidNumber and gidNumber like idmap_ad
[samba.git] / source4 / winbind / wb_update_rodc_dns.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Do a netr_DsrUpdateReadOnlyServerDnsRecords to a remote DC
5
6    Copyright (C) Andrew Bartlett 2010
7    Copyright (C) Andrew Tridgell 2010
8
9    based heavily on wb_sam_logon.c which is copyright:
10
11    Copyright (C) Volker Lendecke 2005
12    Copyright (C) Andrew Bartlett 2005
13    Copyright (C) Stefan Metzmacher 2006
14
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.
19
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.
24
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/>.
27 */
28
29 #include "includes.h"
30 #include "libcli/composite/composite.h"
31 #include "winbind/wb_server.h"
32 #include "smbd/service_task.h"
33 #include "auth/credentials/credentials.h"
34 #include "libcli/auth/libcli_auth.h"
35 #include "librpc/gen_ndr/ndr_netlogon_c.h"
36 #include "librpc/gen_ndr/winbind.h"
37
38 struct wb_update_rodc_dns_state {
39         struct composite_context *ctx;
40
41         struct winbind_DsrUpdateReadOnlyServerDnsRecords *req;
42
43         struct netlogon_creds_CredentialState *creds_state;
44         struct netr_Authenticator auth1, auth2;
45
46         TALLOC_CTX *r_mem_ctx;
47         struct netr_DsrUpdateReadOnlyServerDnsRecords r;
48 };
49
50 static void wb_update_rodc_dns_recv_domain(struct composite_context *ctx);
51 static void wb_update_rodc_dns_recv_response(struct tevent_req *subreq);
52
53 /*
54     Find the connection to the DC (or find an existing connection)
55 */
56 struct composite_context *wb_update_rodc_dns_send(TALLOC_CTX *mem_ctx,
57                                             struct wbsrv_service *service,
58                                             struct winbind_DsrUpdateReadOnlyServerDnsRecords *req)
59 {
60         struct composite_context *c, *creq;
61         struct wb_update_rodc_dns_state *s;
62
63         c = composite_create(mem_ctx, service->task->event_ctx);
64         if (!c) return NULL;
65
66         s = talloc_zero(c, struct wb_update_rodc_dns_state);
67         if (composite_nomem(s, c)) return c;
68         s->ctx = c;
69         s->req = req;
70
71         c->private_data = s;
72
73         creq = wb_sid2domain_send(s, service, service->primary_sid);
74         composite_continue(c, creq, wb_update_rodc_dns_recv_domain, s);
75         return c;
76 }
77
78 /*
79     Having finished making the connection to the DC
80     Send of a DsrUpdateReadOnlyServerDnsRecords request to authenticate a user.
81 */
82 static void wb_update_rodc_dns_recv_domain(struct composite_context *creq)
83 {
84         struct wb_update_rodc_dns_state *s = talloc_get_type(creq->async.private_data,
85                                        struct wb_update_rodc_dns_state);
86         struct wbsrv_domain *domain;
87         struct tevent_req *subreq;
88
89         s->ctx->status = wb_sid2domain_recv(creq, &domain);
90         if (!composite_is_ok(s->ctx)) return;
91
92         s->creds_state = cli_credentials_get_netlogon_creds(domain->libnet_ctx->cred);
93         netlogon_creds_client_authenticator(s->creds_state, &s->auth1);
94
95         s->r.in.server_name = talloc_asprintf(s, "\\\\%s",
96                               dcerpc_server_name(domain->netlogon_pipe));
97         if (composite_nomem(s->r.in.server_name, s->ctx)) return;
98
99         s->r.in.computer_name = cli_credentials_get_workstation(domain->libnet_ctx->cred);
100         s->r.in.credential = &s->auth1;
101         s->r.out.return_authenticator = &s->auth2;
102         s->r.in.site_name = s->req->in.site_name;
103         s->r.in.dns_ttl = s->req->in.dns_ttl;
104         s->r.in.dns_names = s->req->in.dns_names;
105         s->r.out.dns_names = s->req->in.dns_names;
106
107         /*
108          * use a new talloc context for the DsrUpdateReadOnlyServerDnsRecords call
109          * because then we can just to a talloc_steal on this context
110          * in the final _recv() function to give the caller all the content of
111          * the s->r.out.dns_names
112          */
113         s->r_mem_ctx = talloc_new(s);
114         if (composite_nomem(s->r_mem_ctx, s->ctx)) return;
115
116         subreq = dcerpc_netr_DsrUpdateReadOnlyServerDnsRecords_r_send(s,
117                                                   s->ctx->event_ctx,
118                                                   domain->netlogon_pipe->binding_handle,
119                                                   &s->r);
120         if (composite_nomem(subreq, s->ctx)) return;
121         tevent_req_set_callback(subreq, wb_update_rodc_dns_recv_response, s);
122 }
123
124 /*
125    NTLM Authentication
126
127    Check the DsrUpdateReadOnlyServerDnsRecords reply and decrypt the session keys
128 */
129 static void wb_update_rodc_dns_recv_response(struct tevent_req *subreq)
130 {
131         struct wb_update_rodc_dns_state *s = tevent_req_callback_data(subreq,
132                                        struct wb_update_rodc_dns_state);
133
134         s->ctx->status = dcerpc_netr_DsrUpdateReadOnlyServerDnsRecords_r_recv(subreq, s->r_mem_ctx);
135         TALLOC_FREE(subreq);
136         if (!composite_is_ok(s->ctx)) return;
137
138         s->ctx->status = s->r.out.result;
139         if (!composite_is_ok(s->ctx)) return;
140
141         if ((s->r.out.return_authenticator == NULL) ||
142             (!netlogon_creds_client_check(s->creds_state,
143                                           &s->r.out.return_authenticator->cred))) {
144                 DEBUG(0, ("Credentials check failed!\n"));
145                 composite_error(s->ctx, NT_STATUS_ACCESS_DENIED);
146                 return;
147         }
148
149         composite_done(s->ctx);
150 }
151
152 NTSTATUS wb_update_rodc_dns_recv(struct composite_context *c,
153                            TALLOC_CTX *mem_ctx,
154                            struct winbind_DsrUpdateReadOnlyServerDnsRecords *req)
155 {
156         struct wb_update_rodc_dns_state *s = talloc_get_type(c->private_data,
157                                        struct wb_update_rodc_dns_state);
158         NTSTATUS status = composite_wait(c);
159
160         if (NT_STATUS_IS_OK(status)) {
161                 talloc_steal(mem_ctx, s->r_mem_ctx);
162                 req->out.dns_names      = s->r.out.dns_names;
163         }
164
165         talloc_free(s);
166         return status;
167 }