s4-finddcs: added finddcs_cldap()
[abartlet/samba.git/.git] / source4 / libcli / finddcs_cldap.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    a composite API for finding a DC and its name via CLDAP
5
6    Copyright (C) Andrew Tridgell 2010
7    Copyright (C) Andrew Bartlett 2010
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "include/includes.h"
24 #include <tevent.h>
25 #include "libcli/resolve/resolve.h"
26 #include "lib/messaging/messaging.h"
27 #include "libcli/libcli.h"
28 #include "libcli/cldap/cldap.h"
29 #include "libcli/finddcs.h"
30 #include "libcli/security/dom_sid.h"
31 #include "lib/util/tevent_ntstatus.h"
32 #include "libcli/composite/composite.h"
33
34 struct finddcs_cldap_state {
35         struct tevent_context *ev;
36         struct tevent_req *req;
37         const char *dns_domain_name;
38         struct dom_sid *domain_sid;
39         const char **srv_addresses;
40         uint32_t minimum_dc_flags;
41         uint32_t srv_address_index;
42         struct cldap_socket *cldap;
43         struct cldap_netlogon *netlogon;
44 };
45
46 static void finddcs_cldap_srv_resolved(struct composite_context *ctx);
47 static void finddcs_cldap_netlogon_replied(struct tevent_req *req);
48
49
50 /*
51  * find a list of DCs via DNS/CLDAP
52  *
53  */
54 struct tevent_req *finddcs_cldap_send(TALLOC_CTX *mem_ctx,
55                                       struct finddcs *io,
56                                       struct resolve_context *resolve_ctx,
57                                       struct tevent_context *event_ctx)
58 {
59         struct finddcs_cldap_state *state;
60         struct tevent_req *req;
61         const char *srv_name;
62         struct composite_context *creq;
63         struct nbt_name name;
64
65         req = tevent_req_create(mem_ctx, &state, struct finddcs_cldap_state);
66         if (req == NULL) {
67                 return NULL;
68         }
69
70         state->req = req;
71         state->ev = event_ctx;
72         state->minimum_dc_flags = io->in.minimum_dc_flags;
73         state->dns_domain_name = talloc_strdup(state, io->in.dns_domain_name);
74         if (tevent_req_nomem(state->dns_domain_name, req)) {
75                 return tevent_req_post(req, event_ctx);
76         }
77
78         if (io->in.domain_sid) {
79                 state->domain_sid = dom_sid_dup(state, io->in.domain_sid);
80                 if (tevent_req_nomem(state->domain_sid, req)) {
81                         return tevent_req_post(req, event_ctx);
82                 }
83         } else {
84                 state->domain_sid = NULL;
85         }
86
87         /* step1: lookup _ldap._tcp.* */
88         if (io->in.site_name) {
89                 srv_name = talloc_asprintf(state, "_ldap._tcp.%s._sites.%s",
90                                            io->in.site_name, io->in.dns_domain_name);
91         } else {
92                 srv_name = talloc_asprintf(state, "_ldap._tcp.%s", io->in.dns_domain_name);
93         }
94
95         make_nbt_name(&name, srv_name, 0);
96
97         creq = resolve_name_ex_send(resolve_ctx, state,
98                                     RESOLVE_NAME_FLAG_FORCE_DNS | RESOLVE_NAME_FLAG_DNS_SRV,
99                                     0, &name, event_ctx);
100         if (tevent_req_nomem(creq, req)) {
101                 return tevent_req_post(req, event_ctx);
102         }
103         creq->async.fn = finddcs_cldap_srv_resolved;
104         creq->async.private_data = state;
105
106         return req;
107 }
108
109
110 /*
111   fire off a CLDAP query to the next server
112  */
113 static void finddcs_cldap_next_server(struct finddcs_cldap_state *state)
114 {
115         struct tevent_req *subreq;
116
117         if (state->srv_addresses[state->srv_address_index] == NULL) {
118                 tevent_req_nterror(state->req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
119                 return;
120         }
121
122         state->netlogon = talloc_zero(state, struct cldap_netlogon);
123         if (tevent_req_nomem(state->netlogon, state->req)) {
124                 return;
125         }
126
127         state->netlogon->in.dest_address = state->srv_addresses[state->srv_address_index];
128         /* we should get the port from the SRV response */
129         state->netlogon->in.dest_port = 389;
130         state->netlogon->in.realm = state->dns_domain_name;
131         if (state->domain_sid) {
132                 state->netlogon->in.domain_sid = dom_sid_string(state, state->domain_sid);
133                 if (tevent_req_nomem(state->netlogon->in.domain_sid, state->req)) {
134                         return;
135                 }
136         }
137         state->netlogon->in.acct_control = -1;
138         state->netlogon->in.version =
139                 NETLOGON_NT_VERSION_5 |
140                 NETLOGON_NT_VERSION_5EX |
141                 NETLOGON_NT_VERSION_IP;
142         state->netlogon->in.map_response = true;
143
144         subreq = cldap_netlogon_send(state, state->cldap, state->netlogon);
145         if (tevent_req_nomem(subreq, state->req)) {
146                 return;
147         }
148
149         tevent_req_set_callback(subreq, finddcs_cldap_netlogon_replied, state);
150 }
151
152
153 /*
154   we have a response from a CLDAP server for a netlogon request
155  */
156 static void finddcs_cldap_netlogon_replied(struct tevent_req *subreq)
157 {
158         struct finddcs_cldap_state *state;
159         NTSTATUS status;
160
161         state = tevent_req_callback_data(subreq, struct finddcs_cldap_state);
162
163         status = cldap_netlogon_recv(subreq, state->netlogon, state->netlogon);
164         if (!NT_STATUS_IS_OK(status)) {
165                 state->srv_address_index++;
166                 finddcs_cldap_next_server(state);
167                 return;
168         }
169         if (state->minimum_dc_flags !=
170             (state->minimum_dc_flags & state->netlogon->out.netlogon.data.nt5_ex.server_type)) {
171                 /* the server didn't match the minimum requirements */
172                 DEBUG(4,(__location__ ": Skipping DC %s with server_type=0x%08x\n",
173                          state->srv_addresses[state->srv_address_index],
174                          state->netlogon->out.netlogon.data.nt5_ex.server_type));
175                 state->srv_address_index++;
176                 finddcs_cldap_next_server(state);
177                 return;
178         }
179
180         talloc_free(subreq);
181         tevent_req_done(state->req);
182 }
183
184
185 /*
186  * Having got a DNS SRV answer, fire off the first CLDAP request
187  */
188 static void finddcs_cldap_srv_resolved(struct composite_context *ctx)
189 {
190         struct finddcs_cldap_state *state =
191                 talloc_get_type(ctx->async.private_data, struct finddcs_cldap_state);
192         NTSTATUS status;
193
194         status = resolve_name_multiple_recv(ctx, state, &state->srv_addresses);
195         if (tevent_req_nterror(state->req, status)) {
196                 return;
197         }
198
199         state->srv_address_index = 0;
200
201         status = cldap_socket_init(state, state->ev, NULL, NULL, &state->cldap);
202         if (tevent_req_nterror(state->req, status)) {
203                 return;
204         }
205
206         finddcs_cldap_next_server(state);
207 }
208
209
210 NTSTATUS finddcs_cldap_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx, struct finddcs *io)
211 {
212         struct finddcs_cldap_state *state = tevent_req_data(req, struct finddcs_cldap_state);
213         bool ok;
214         NTSTATUS status;
215
216         ok = tevent_req_poll(req, state->ev);
217         if (!ok) {
218                 talloc_free(req);
219                 return NT_STATUS_INTERNAL_ERROR;
220         }
221         status = tevent_req_simple_recv_ntstatus(req);
222         if (NT_STATUS_IS_OK(status)) {
223                 talloc_steal(mem_ctx, state->netlogon);
224                 io->out.netlogon = state->netlogon->out.netlogon;
225                 io->out.address = talloc_steal(mem_ctx, state->srv_addresses[state->srv_address_index]);
226         }
227         tevent_req_received(req);
228         return status;
229 }
230
231 NTSTATUS finddcs_cldap(TALLOC_CTX *mem_ctx,
232                        struct finddcs *io,
233                        struct resolve_context *resolve_ctx,
234                        struct tevent_context *event_ctx)
235 {
236         NTSTATUS status;
237         struct tevent_req *req = finddcs_cldap_send(mem_ctx,
238                                                     io,
239                                                     resolve_ctx,
240                                                     event_ctx);
241         status = finddcs_cldap_recv(req, mem_ctx, io);
242         talloc_free(req);
243         return status;
244 }