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