s4-libcli: change finddcs.h -> finddc.h
[sfrench/samba-autobuild/.git] / source4 / libcli / finddcs_nbt.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    a composite API for finding a DC and its name
5
6    Copyright (C) Volker Lendecke 2005
7    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006
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 "lib/messaging/irpc.h"
26 #include "librpc/gen_ndr/ndr_irpc_c.h"
27 #include "librpc/gen_ndr/samr.h"
28 #include "libcli/composite/composite.h"
29 #include "libcli/libcli.h"
30 #include "libcli/resolve/resolve.h"
31 #include "libcli/finddc.h"
32 #include "lib/util/tevent_ntstatus.h"
33
34 struct finddcs_nbt_state {
35         struct tevent_context *ev;
36         struct tevent_req *req;
37         struct messaging_context *msg_ctx;
38
39         const char *my_netbios_name;
40         const char *domain_name;
41         struct dom_sid *domain_sid;
42
43         struct nbtd_getdcname r;
44         struct nbt_name_status node_status;
45
46         int num_dcs;
47         struct nbt_dc_name *dcs;
48         uint16_t nbt_port;
49 };
50
51 static void finddcs_nbt_name_resolved(struct composite_context *ctx);
52 static void finddcs_nbt_getdc_replied(struct tevent_req *subreq);
53 static void fallback_node_status(struct finddcs_nbt_state *state);
54 static void fallback_node_status_replied(struct nbt_name_request *name_req);
55
56 /*
57  * Setup and send off the a normal name resolution for the target name.
58  *
59  * The domain_sid parameter is optional, and is used in the subsequent getdc request.
60  *
61  * This will try a GetDC request, but this may not work.  It will try
62  * a node status as a fallback, then return no name (but still include
63  * the IP)
64  */
65
66 struct tevent_req *finddcs_nbt_send(TALLOC_CTX *mem_ctx,
67                                 const char *my_netbios_name,
68                                 uint16_t nbt_port,
69                                 const char *domain_name,
70                                 int name_type,
71                                 struct dom_sid *domain_sid,
72                                 struct resolve_context *resolve_ctx,
73                                 struct tevent_context *event_ctx,
74                                 struct messaging_context *msg_ctx)
75 {
76         struct finddcs_nbt_state *state;
77         struct nbt_name name;
78         struct tevent_req *req;
79         struct composite_context *creq;
80
81         req = tevent_req_create(mem_ctx, &state, struct finddcs_nbt_state);
82         if (req == NULL) {
83                 return NULL;
84         }
85
86         state->req = req;
87         state->ev = event_ctx;
88         state->nbt_port = nbt_port;
89         state->my_netbios_name = talloc_strdup(state, my_netbios_name);
90         if (tevent_req_nomem(state->my_netbios_name, req)) {
91                 return tevent_req_post(req, event_ctx);
92         }
93         state->domain_name = talloc_strdup(state, domain_name);
94         if (tevent_req_nomem(state->domain_name, req)) {
95                 return tevent_req_post(req, event_ctx);
96         }
97
98         if (domain_sid) {
99                 state->domain_sid = talloc_reference(state, domain_sid);
100                 if (tevent_req_nomem(state->domain_sid, req)) {
101                         return tevent_req_post(req, event_ctx);
102                 }
103         } else {
104                 state->domain_sid = NULL;
105         }
106
107         state->msg_ctx = msg_ctx;
108
109         make_nbt_name(&name, state->domain_name, name_type);
110         creq = resolve_name_send(resolve_ctx, state, &name, event_ctx);
111         if (tevent_req_nomem(creq, req)) {
112                 return tevent_req_post(req, event_ctx);
113         }
114         creq->async.fn = finddcs_nbt_name_resolved;
115         creq->async.private_data = state;
116
117         return req;
118 }
119
120 /* Having got an name query answer, fire off a GetDC request, so we
121  * can find the target's all-important name.  (Kerberos and some
122  * netlogon operations are quite picky about names)
123  *
124  * The name is a courtesy, if we don't find it, don't completely fail.
125  *
126  * However, if the nbt server is down, fall back to a node status
127  * request
128  */
129 static void finddcs_nbt_name_resolved(struct composite_context *ctx)
130 {
131         struct finddcs_nbt_state *state =
132                 talloc_get_type(ctx->async.private_data, struct finddcs_nbt_state);
133         struct tevent_req *subreq;
134         struct dcerpc_binding_handle *irpc_handle;
135         const char *address;
136         NTSTATUS status;
137
138         status = resolve_name_recv(ctx, state, &address);
139         if (tevent_req_nterror(state->req, status)) {
140                 return;
141         }
142
143         /* TODO: This should try and find all the DCs, and give the
144          * caller them in the order they responded */
145
146         state->num_dcs = 1;
147         state->dcs = talloc_array(state, struct nbt_dc_name, state->num_dcs);
148         if (tevent_req_nomem(state->dcs, state->req)) {
149                 return;
150         }
151
152         state->dcs[0].address = talloc_steal(state->dcs, address);
153
154         /* Try and find the nbt server.  Fallback to a node status
155          * request if we can't make this happen The nbt server just
156          * might not be running, or we may not have a messaging
157          * context (not root etc) */
158         if (!state->msg_ctx) {
159                 fallback_node_status(state);
160                 return;
161         }
162
163         irpc_handle = irpc_binding_handle_by_name(state, state->msg_ctx,
164                                                   "nbt_server", &ndr_table_irpc);
165         if (irpc_handle == NULL) {
166                 fallback_node_status(state);
167                 return;
168         }
169
170         state->r.in.domainname = state->domain_name;
171         state->r.in.ip_address = state->dcs[0].address;
172         state->r.in.my_computername = state->my_netbios_name;
173         state->r.in.my_accountname = talloc_asprintf(state, "%s$", state->my_netbios_name);
174         if (tevent_req_nomem(state->r.in.my_accountname, state->req)) {
175                 return;
176         }
177         state->r.in.account_control = ACB_WSTRUST;
178         state->r.in.domain_sid = state->domain_sid;
179         if (state->r.in.domain_sid == NULL) {
180                 state->r.in.domain_sid = talloc_zero(state, struct dom_sid);
181         }
182
183         subreq = dcerpc_nbtd_getdcname_r_send(state, state->ev,
184                                               irpc_handle, &state->r);
185         if (tevent_req_nomem(subreq, state->req)) {
186                 return;
187         }
188         tevent_req_set_callback(subreq, finddcs_nbt_getdc_replied, state);
189 }
190
191 /* Called when the GetDC request returns */
192 static void finddcs_nbt_getdc_replied(struct tevent_req *subreq)
193 {
194         struct finddcs_nbt_state *state =
195                 tevent_req_callback_data(subreq,
196                 struct finddcs_nbt_state);
197         NTSTATUS status;
198
199         status = dcerpc_nbtd_getdcname_r_recv(subreq, state);
200         TALLOC_FREE(subreq);
201         if (!NT_STATUS_IS_OK(status)) {
202                 fallback_node_status(state);
203                 return;
204         }
205
206         state->dcs[0].name = talloc_steal(state->dcs, state->r.out.dcname);
207         tevent_req_done(state->req);
208 }
209
210 /* The GetDC request might not be available (such as occours when the
211  * NBT server is down).  Fallback to a node status.  It is the best
212  * hope we have... */
213 static void fallback_node_status(struct finddcs_nbt_state *state)
214 {
215         struct nbt_name_socket *nbtsock;
216         struct nbt_name_request *name_req;
217
218         state->node_status.in.name.name = "*";
219         state->node_status.in.name.type = NBT_NAME_CLIENT;
220         state->node_status.in.name.scope = NULL;
221         state->node_status.in.dest_addr = state->dcs[0].address;
222         state->node_status.in.dest_port = state->nbt_port;
223         state->node_status.in.timeout = 1;
224         state->node_status.in.retries = 2;
225
226         nbtsock = nbt_name_socket_init(state, state->ev);
227         if (tevent_req_nomem(nbtsock, state->req)) {
228                 return;
229         }
230
231         name_req = nbt_name_status_send(nbtsock, &state->node_status);
232         if (tevent_req_nomem(name_req, state->req)) {
233                 return;
234         }
235
236         name_req->async.fn = fallback_node_status_replied;
237         name_req->async.private_data = state;
238 }
239
240 /* We have a node status reply (or perhaps a timeout) */
241 static void fallback_node_status_replied(struct nbt_name_request *name_req)
242 {
243         int i;
244         struct finddcs_nbt_state *state = talloc_get_type(name_req->async.private_data, struct finddcs_nbt_state);
245         NTSTATUS status;
246
247         status = nbt_name_status_recv(name_req, state, &state->node_status);
248         if (tevent_req_nterror(state->req, status)) {
249                 return;
250         }
251
252         for (i=0; i < state->node_status.out.status.num_names; i++) {
253                 int j;
254                 if (state->node_status.out.status.names[i].type == NBT_NAME_SERVER) {
255                         char *name = talloc_strndup(state->dcs, state->node_status.out.status.names[0].name, 15);
256                         /* Strip space padding */
257                         if (name) {
258                                 j = MIN(strlen(name), 15);
259                                 for (; j > 0 && name[j - 1] == ' '; j--) {
260                                         name[j - 1] = '\0';
261                                 }
262                         }
263                         state->dcs[0].name = name;
264                         tevent_req_done(state->req);
265                         return;
266                 }
267         }
268         tevent_req_nterror(state->req, NT_STATUS_NO_LOGON_SERVERS);
269 }
270
271 NTSTATUS finddcs_nbt_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
272                       int *num_dcs, struct nbt_dc_name **dcs)
273 {
274         struct finddcs_nbt_state *state = tevent_req_data(req, struct finddcs_nbt_state);
275         bool ok;
276         NTSTATUS status;
277
278         ok = tevent_req_poll(req, state->ev);
279         if (!ok) {
280                 talloc_free(req);
281                 return NT_STATUS_INTERNAL_ERROR;
282         }
283         status = tevent_req_simple_recv_ntstatus(req);
284         if (NT_STATUS_IS_OK(status)) {
285                 *num_dcs = state->num_dcs;
286                 *dcs = talloc_steal(mem_ctx, state->dcs);
287         }
288         talloc_free(req);
289         return status;
290 }
291
292 NTSTATUS finddcs_nbt(TALLOC_CTX *mem_ctx,
293                  const char *my_netbios_name,
294                  uint16_t nbt_port,
295                  const char *domain_name, int name_type,
296                  struct dom_sid *domain_sid,
297                  struct resolve_context *resolve_ctx,
298                  struct tevent_context *event_ctx,
299                  struct messaging_context *msg_ctx,
300                  int *num_dcs, struct nbt_dc_name **dcs)
301 {
302         struct tevent_req *req = finddcs_nbt_send(mem_ctx,
303                                               my_netbios_name,
304                                               nbt_port,
305                                               domain_name, name_type,
306                                               domain_sid,
307                                               resolve_ctx,
308                                               event_ctx, msg_ctx);
309         return finddcs_nbt_recv(req, mem_ctx, num_dcs, dcs);
310 }