Use variable for ndr_netlogon.o path.
[gd/samba/.git] / source4 / libcli / finddcs.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 "lib/messaging/irpc.h"
25 #include "librpc/gen_ndr/ndr_irpc.h"
26 #include "librpc/gen_ndr/samr.h"
27 #include "libcli/composite/composite.h"
28 #include "libcli/libcli.h"
29 #include "libcli/resolve/resolve.h"
30 #include "libcli/finddcs.h"
31 #include "param/param.h"
32
33 struct finddcs_state {
34         struct composite_context *ctx;
35         struct messaging_context *msg_ctx;
36
37         const char *my_netbios_name;
38         const char *domain_name;
39         struct dom_sid *domain_sid;
40
41         struct nbtd_getdcname r;
42         struct nbt_name_status node_status;
43
44         struct smb_iconv_convenience *iconv_convenience;
45
46         int num_dcs;
47         struct nbt_dc_name *dcs;
48         uint16_t nbt_port;
49 };
50
51 static void finddcs_name_resolved(struct composite_context *ctx);
52 static void finddcs_getdc_replied(struct irpc_request *ireq);
53 static void fallback_node_status(struct finddcs_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 composite_context *finddcs_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 smb_iconv_convenience *iconv_convenience,
73                                        struct resolve_context *resolve_ctx,
74                                        struct event_context *event_ctx,
75                                        struct messaging_context *msg_ctx)
76 {
77         struct composite_context *c, *creq;
78         struct finddcs_state *state;
79         struct nbt_name name;
80
81         c = composite_create(mem_ctx, event_ctx);
82         if (c == NULL) return NULL;
83
84         state = talloc(c, struct finddcs_state);
85         if (composite_nomem(state, c)) return c;
86         c->private_data = state;
87
88         state->ctx = c;
89
90         state->nbt_port = nbt_port;
91         state->my_netbios_name = talloc_strdup(state, my_netbios_name);
92         state->domain_name = talloc_strdup(state, domain_name);
93         state->iconv_convenience = iconv_convenience;
94         if (composite_nomem(state->domain_name, c)) return c;
95
96         if (domain_sid) {
97                 state->domain_sid = talloc_reference(state, domain_sid);
98                 if (composite_nomem(state->domain_sid, c)) return c;
99         } else {
100                 state->domain_sid = NULL;
101         }
102
103         state->msg_ctx = msg_ctx;
104
105         make_nbt_name(&name, state->domain_name, name_type);
106         creq = resolve_name_send(resolve_ctx, &name, event_ctx);
107         composite_continue(c, creq, finddcs_name_resolved, state);
108         return c;
109 }
110
111 /* Having got an name query answer, fire off a GetDC request, so we
112  * can find the target's all-important name.  (Kerberos and some
113  * netlogon operations are quite picky about names) 
114  *
115  * The name is a courtesy, if we don't find it, don't completely fail.
116  *
117  * However, if the nbt server is down, fall back to a node status
118  * request
119  */
120 static void finddcs_name_resolved(struct composite_context *ctx)
121 {
122         struct finddcs_state *state =
123                 talloc_get_type(ctx->async.private_data, struct finddcs_state);
124         struct irpc_request *ireq;
125         struct server_id *nbt_servers;
126         const char *address;
127
128         state->ctx->status = resolve_name_recv(ctx, state, &address);
129         if (!composite_is_ok(state->ctx)) return;
130
131         /* TODO: This should try and find all the DCs, and give the
132          * caller them in the order they responded */
133
134         state->num_dcs = 1;
135         state->dcs = talloc_array(state, struct nbt_dc_name, state->num_dcs);
136         if (composite_nomem(state->dcs, state->ctx)) return;
137
138         state->dcs[0].address = talloc_steal(state->dcs, address);
139
140         /* Try and find the nbt server.  Fallback to a node status
141          * request if we can't make this happen The nbt server just
142          * might not be running, or we may not have a messaging
143          * context (not root etc) */
144         if (!state->msg_ctx) {
145                 fallback_node_status(state);
146                 return;
147         }
148
149         nbt_servers = irpc_servers_byname(state->msg_ctx, state, "nbt_server");
150         if ((nbt_servers == NULL) || (nbt_servers[0].id == 0)) {
151                 fallback_node_status(state);
152                 return;
153         }
154
155         state->r.in.domainname = state->domain_name;
156         state->r.in.ip_address = state->dcs[0].address;
157         state->r.in.my_computername = state->my_netbios_name;
158         state->r.in.my_accountname = talloc_asprintf(state, "%s$", state->my_netbios_name);
159         if (composite_nomem(state->r.in.my_accountname, state->ctx)) return;
160         state->r.in.account_control = ACB_WSTRUST;
161         state->r.in.domain_sid = state->domain_sid;
162
163         ireq = irpc_call_send(state->msg_ctx, nbt_servers[0],
164                               &ndr_table_irpc, NDR_NBTD_GETDCNAME,
165                               &state->r, state);
166         if (!ireq) {
167                 fallback_node_status(state);
168                 return;
169         }
170
171         composite_continue_irpc(state->ctx, ireq, finddcs_getdc_replied, state);
172 }
173
174 /* Called when the GetDC request returns */
175 static void finddcs_getdc_replied(struct irpc_request *ireq)
176 {
177         struct finddcs_state *state =
178                 talloc_get_type(ireq->async.private, struct finddcs_state);
179
180         state->ctx->status = irpc_call_recv(ireq);
181         if (!composite_is_ok(state->ctx)) return;
182
183         state->dcs[0].name = talloc_steal(state->dcs, state->r.out.dcname);
184         composite_done(state->ctx);
185 }
186
187 /* The GetDC request might not be available (such as occours when the
188  * NBT server is down).  Fallback to a node status.  It is the best
189  * hope we have... */
190 static void fallback_node_status(struct finddcs_state *state) 
191 {
192         struct nbt_name_socket *nbtsock;
193         struct nbt_name_request *name_req;
194
195         state->node_status.in.name.name = "*";
196         state->node_status.in.name.type = NBT_NAME_CLIENT;
197         state->node_status.in.name.scope = NULL;
198         state->node_status.in.dest_addr = state->dcs[0].address;
199         state->node_status.in.dest_port = state->nbt_port;
200         state->node_status.in.timeout = 1;
201         state->node_status.in.retries = 2;
202
203         nbtsock = nbt_name_socket_init(state, state->ctx->event_ctx, 
204                                        state->iconv_convenience);
205         if (composite_nomem(nbtsock, state->ctx)) return;
206         
207         name_req = nbt_name_status_send(nbtsock, &state->node_status);
208         if (composite_nomem(name_req, state->ctx)) return;
209
210         composite_continue_nbt(state->ctx, 
211                                name_req, 
212                                fallback_node_status_replied,
213                                state);
214 }
215
216 /* We have a node status reply (or perhaps a timeout) */
217 static void fallback_node_status_replied(struct nbt_name_request *name_req) 
218 {
219         int i;
220         struct finddcs_state *state = talloc_get_type(name_req->async.private, struct finddcs_state);
221         state->ctx->status = nbt_name_status_recv(name_req, state, &state->node_status);
222         if (!composite_is_ok(state->ctx)) return;
223
224         for (i=0; i < state->node_status.out.status.num_names; i++) {
225                 int j;
226                 if (state->node_status.out.status.names[i].type == NBT_NAME_SERVER) {
227                         char *name = talloc_strndup(state->dcs, state->node_status.out.status.names[0].name, 15);
228                         /* Strip space padding */
229                         if (name) {
230                                 j = MIN(strlen(name), 15);
231                                 for (; j > 0 && name[j - 1] == ' '; j--) {
232                                         name[j - 1] = '\0';
233                                 }
234                         }
235                         state->dcs[0].name = name;
236                         composite_done(state->ctx);
237                         return;
238                 }
239         }
240         composite_error(state->ctx, NT_STATUS_NO_LOGON_SERVERS);
241 }
242
243 NTSTATUS finddcs_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
244                       int *num_dcs, struct nbt_dc_name **dcs)
245 {
246         NTSTATUS status = composite_wait(c);
247         if (NT_STATUS_IS_OK(status)) {
248                 struct finddcs_state *state =
249                         talloc_get_type(c->private_data, struct finddcs_state);
250                 *num_dcs = state->num_dcs;
251                 *dcs = talloc_steal(mem_ctx, state->dcs);
252         }
253         talloc_free(c);
254         return status;
255 }
256
257 NTSTATUS finddcs(TALLOC_CTX *mem_ctx,
258                  const char *my_netbios_name,
259                  uint16_t nbt_port,
260                  const char *domain_name, int name_type, 
261                  struct dom_sid *domain_sid,
262                  struct smb_iconv_convenience *iconv_convenience,
263                  struct resolve_context *resolve_ctx,
264                  struct event_context *event_ctx,
265                  struct messaging_context *msg_ctx,
266                  int *num_dcs, struct nbt_dc_name **dcs)
267 {
268         struct composite_context *c = finddcs_send(mem_ctx,
269                                                    my_netbios_name,
270                                                    nbt_port,
271                                                    domain_name, name_type,
272                                                    domain_sid, 
273                                                    iconv_convenience,
274                                                    resolve_ctx,
275                                                    event_ctx, msg_ctx);
276         return finddcs_recv(c, mem_ctx, num_dcs, dcs);
277 }