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