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