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