s3: use monotonic clock for aio timeout
[Samba/id10ts.git] / source4 / libcli / finddcs.c
blob104ad17e0017340e04f2ef108afef5ef3c336bdd
1 /*
2 Unix SMB/CIFS implementation.
4 a composite API for finding a DC and its name
6 Copyright (C) Volker Lendecke 2005
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006
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.
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.
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/>.
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 "librpc/gen_ndr/samr.h"
28 #include "libcli/composite/composite.h"
29 #include "libcli/libcli.h"
30 #include "libcli/resolve/resolve.h"
31 #include "libcli/finddcs.h"
33 struct finddcs_state {
34 struct composite_context *ctx;
35 struct messaging_context *msg_ctx;
37 const char *my_netbios_name;
38 const char *domain_name;
39 struct dom_sid *domain_sid;
41 struct nbtd_getdcname r;
42 struct nbt_name_status node_status;
44 int num_dcs;
45 struct nbt_dc_name *dcs;
46 uint16_t nbt_port;
49 static void finddcs_name_resolved(struct composite_context *ctx);
50 static void finddcs_getdc_replied(struct tevent_req *subreq);
51 static void fallback_node_status(struct finddcs_state *state);
52 static void fallback_node_status_replied(struct nbt_name_request *name_req);
54 /*
55 * Setup and send off the a normal name resolution for the target name.
57 * The domain_sid parameter is optional, and is used in the subsequent getdc request.
59 * This will try a GetDC request, but this may not work. It will try
60 * a node status as a fallback, then return no name (but still include
61 * the IP)
64 struct composite_context *finddcs_send(TALLOC_CTX *mem_ctx,
65 const char *my_netbios_name,
66 uint16_t nbt_port,
67 const char *domain_name,
68 int name_type,
69 struct dom_sid *domain_sid,
70 struct resolve_context *resolve_ctx,
71 struct tevent_context *event_ctx,
72 struct messaging_context *msg_ctx)
74 struct composite_context *c, *creq;
75 struct finddcs_state *state;
76 struct nbt_name name;
78 c = composite_create(mem_ctx, event_ctx);
79 if (c == NULL) return NULL;
81 state = talloc(c, struct finddcs_state);
82 if (composite_nomem(state, c)) return c;
83 c->private_data = state;
85 state->ctx = c;
87 state->nbt_port = nbt_port;
88 state->my_netbios_name = talloc_strdup(state, my_netbios_name);
89 state->domain_name = talloc_strdup(state, domain_name);
90 if (composite_nomem(state->domain_name, c)) return c;
92 if (domain_sid) {
93 state->domain_sid = talloc_reference(state, domain_sid);
94 if (composite_nomem(state->domain_sid, c)) return c;
95 } else {
96 state->domain_sid = NULL;
99 state->msg_ctx = msg_ctx;
101 make_nbt_name(&name, state->domain_name, name_type);
102 creq = resolve_name_send(resolve_ctx, state, &name, event_ctx);
103 composite_continue(c, creq, finddcs_name_resolved, state);
104 return c;
107 /* Having got an name query answer, fire off a GetDC request, so we
108 * can find the target's all-important name. (Kerberos and some
109 * netlogon operations are quite picky about names)
111 * The name is a courtesy, if we don't find it, don't completely fail.
113 * However, if the nbt server is down, fall back to a node status
114 * request
116 static void finddcs_name_resolved(struct composite_context *ctx)
118 struct finddcs_state *state =
119 talloc_get_type(ctx->async.private_data, struct finddcs_state);
120 struct tevent_req *subreq;
121 struct dcerpc_binding_handle *irpc_handle;
122 const char *address;
124 state->ctx->status = resolve_name_recv(ctx, state, &address);
125 if (!composite_is_ok(state->ctx)) return;
127 /* TODO: This should try and find all the DCs, and give the
128 * caller them in the order they responded */
130 state->num_dcs = 1;
131 state->dcs = talloc_array(state, struct nbt_dc_name, state->num_dcs);
132 if (composite_nomem(state->dcs, state->ctx)) return;
134 state->dcs[0].address = talloc_steal(state->dcs, address);
136 /* Try and find the nbt server. Fallback to a node status
137 * request if we can't make this happen The nbt server just
138 * might not be running, or we may not have a messaging
139 * context (not root etc) */
140 if (!state->msg_ctx) {
141 fallback_node_status(state);
142 return;
145 irpc_handle = irpc_binding_handle_by_name(state, state->msg_ctx,
146 "nbt_server", &ndr_table_irpc);
147 if (irpc_handle == NULL) {
148 fallback_node_status(state);
149 return;
152 state->r.in.domainname = state->domain_name;
153 state->r.in.ip_address = state->dcs[0].address;
154 state->r.in.my_computername = state->my_netbios_name;
155 state->r.in.my_accountname = talloc_asprintf(state, "%s$", state->my_netbios_name);
156 if (composite_nomem(state->r.in.my_accountname, state->ctx)) return;
157 state->r.in.account_control = ACB_WSTRUST;
158 state->r.in.domain_sid = state->domain_sid;
159 if (state->r.in.domain_sid == NULL) {
160 state->r.in.domain_sid = talloc_zero(state, struct dom_sid);
163 subreq = dcerpc_nbtd_getdcname_r_send(state, state->ctx->event_ctx,
164 irpc_handle, &state->r);
165 if (composite_nomem(subreq, state->ctx)) return;
166 tevent_req_set_callback(subreq, finddcs_getdc_replied, state);
169 /* Called when the GetDC request returns */
170 static void finddcs_getdc_replied(struct tevent_req *subreq)
172 struct finddcs_state *state =
173 tevent_req_callback_data(subreq,
174 struct finddcs_state);
175 NTSTATUS status;
177 status = dcerpc_nbtd_getdcname_r_recv(subreq, state);
178 TALLOC_FREE(subreq);
179 if (!NT_STATUS_IS_OK(status)) {
180 fallback_node_status(state);
183 state->dcs[0].name = talloc_steal(state->dcs, state->r.out.dcname);
184 composite_done(state->ctx);
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)
192 struct nbt_name_socket *nbtsock;
193 struct nbt_name_request *name_req;
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;
203 nbtsock = nbt_name_socket_init(state, state->ctx->event_ctx);
204 if (composite_nomem(nbtsock, state->ctx)) return;
206 name_req = nbt_name_status_send(nbtsock, &state->node_status);
207 if (composite_nomem(name_req, state->ctx)) return;
209 composite_continue_nbt(state->ctx,
210 name_req,
211 fallback_node_status_replied,
212 state);
215 /* We have a node status reply (or perhaps a timeout) */
216 static void fallback_node_status_replied(struct nbt_name_request *name_req)
218 int i;
219 struct finddcs_state *state = talloc_get_type(name_req->async.private_data, struct finddcs_state);
220 state->ctx->status = nbt_name_status_recv(name_req, state, &state->node_status);
221 if (!composite_is_ok(state->ctx)) return;
223 for (i=0; i < state->node_status.out.status.num_names; i++) {
224 int j;
225 if (state->node_status.out.status.names[i].type == NBT_NAME_SERVER) {
226 char *name = talloc_strndup(state->dcs, state->node_status.out.status.names[0].name, 15);
227 /* Strip space padding */
228 if (name) {
229 j = MIN(strlen(name), 15);
230 for (; j > 0 && name[j - 1] == ' '; j--) {
231 name[j - 1] = '\0';
234 state->dcs[0].name = name;
235 composite_done(state->ctx);
236 return;
239 composite_error(state->ctx, NT_STATUS_NO_LOGON_SERVERS);
242 NTSTATUS finddcs_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
243 int *num_dcs, struct nbt_dc_name **dcs)
245 NTSTATUS status = composite_wait(c);
246 if (NT_STATUS_IS_OK(status)) {
247 struct finddcs_state *state =
248 talloc_get_type(c->private_data, struct finddcs_state);
249 *num_dcs = state->num_dcs;
250 *dcs = talloc_steal(mem_ctx, state->dcs);
252 talloc_free(c);
253 return status;
256 NTSTATUS finddcs(TALLOC_CTX *mem_ctx,
257 const char *my_netbios_name,
258 uint16_t nbt_port,
259 const char *domain_name, int name_type,
260 struct dom_sid *domain_sid,
261 struct resolve_context *resolve_ctx,
262 struct tevent_context *event_ctx,
263 struct messaging_context *msg_ctx,
264 int *num_dcs, struct nbt_dc_name **dcs)
266 struct composite_context *c = finddcs_send(mem_ctx,
267 my_netbios_name,
268 nbt_port,
269 domain_name, name_type,
270 domain_sid,
271 resolve_ctx,
272 event_ctx, msg_ctx);
273 return finddcs_recv(c, mem_ctx, num_dcs, dcs);