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 2 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, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "include/includes.h"
25 #include "lib/messaging/irpc.h"
26 #include "librpc/gen_ndr/ndr_irpc.h"
27 #include "librpc/gen_ndr/ndr_samr.h"
28 #include "libcli/composite/composite.h"
29 #include "libcli/libcli.h"
30 #include "libcli/resolve/resolve.h"
32 struct finddcs_state {
33 struct composite_context *ctx;
34 struct messaging_context *msg_ctx;
36 const char *domain_name;
37 struct dom_sid *domain_sid;
39 struct nbtd_getdcname r;
40 struct nbt_name_status node_status;
43 struct nbt_dc_name *dcs;
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);
52 * Setup and send off the a normal name resolution for the target name.
54 * The domain_sid parameter is optional, and is used in the subsequent getdc request.
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
61 struct composite_context *finddcs_send(TALLOC_CTX *mem_ctx,
62 const char *domain_name,
64 struct dom_sid *domain_sid,
66 struct event_context *event_ctx,
67 struct messaging_context *msg_ctx)
69 struct composite_context *result, *ctx;
70 struct finddcs_state *state;
73 result = talloc(mem_ctx, struct composite_context);
74 if (result == NULL) goto failed;
75 result->state = COMPOSITE_STATE_IN_PROGRESS;
76 result->async.fn = NULL;
77 result->event_ctx = event_ctx;
79 state = talloc(result, struct finddcs_state);
80 if (state == NULL) goto failed;
82 result->private_data = state;
84 state->domain_name = talloc_strdup(state, domain_name);
85 if (state->domain_name == NULL) goto failed;
86 state->domain_sid = domain_sid;
87 if (domain_sid != NULL) {
88 if (talloc_reference(state, domain_sid) == NULL) {
92 state->msg_ctx = msg_ctx;
94 make_nbt_name(&name, state->domain_name, name_type);
95 ctx = resolve_name_send(&name, result->event_ctx,
98 if (ctx == NULL) goto failed;
99 ctx->async.fn = finddcs_name_resolved;
100 ctx->async.private_data = state;
109 /* Having got an name query answer, fire off a GetDC request, so we
110 * can find the target's all-important name. (Kerberos and some
111 * netlogon operations are quite picky about names)
113 * The name is a courtasy, if we don't find it, don't compleatly fail.
115 * However, if the nbt server is down, fall back to a node status
118 static void finddcs_name_resolved(struct composite_context *ctx)
120 struct finddcs_state *state =
121 talloc_get_type(ctx->async.private_data, struct finddcs_state);
122 struct irpc_request *ireq;
123 uint32_t *nbt_servers;
126 state->ctx->status = resolve_name_recv(ctx, state, &address);
127 if (!composite_is_ok(state->ctx)) return;
130 state->dcs = talloc_array(state, struct nbt_dc_name, state->num_dcs);
131 if (composite_nomem(state->dcs, state->ctx)) return;
133 state->dcs[0].address = talloc_steal(state->dcs, address);
135 /* Try and find the nbt server. Fallback to a node status
136 * request if we can't make this happen The nbt server just
137 * might not be running, or we may not have a messaging
138 * context (not root etc) */
139 if (!state->msg_ctx) {
140 fallback_node_status(state);
144 nbt_servers = irpc_servers_byname(state->msg_ctx, "nbt_server");
145 if ((nbt_servers == NULL) || (nbt_servers[0] == 0)) {
146 fallback_node_status(state);
150 state->r.in.domainname = state->domain_name;
151 state->r.in.ip_address = state->dcs[0].address;
152 state->r.in.my_computername = lp_netbios_name();
153 state->r.in.my_accountname = talloc_asprintf(state, "%s$",
155 if (composite_nomem(state->r.in.my_accountname, state->ctx)) return;
156 state->r.in.account_control = ACB_WSTRUST;
157 state->r.in.domain_sid = state->domain_sid;
159 ireq = irpc_call_send(state->msg_ctx, nbt_servers[0],
160 &dcerpc_table_irpc, DCERPC_NBTD_GETDCNAME,
163 fallback_node_status(state);
167 composite_continue_irpc(state->ctx, ireq, finddcs_getdc_replied, state);
170 /* Called when the GetDC request returns */
171 static void finddcs_getdc_replied(struct irpc_request *ireq)
173 struct finddcs_state *state =
174 talloc_get_type(ireq->async.private, struct finddcs_state);
176 state->ctx->status = irpc_call_recv(ireq);
177 if (!composite_is_ok(state->ctx)) return;
179 state->dcs[0].name = talloc_steal(state->dcs, state->r.out.dcname);
180 composite_done(state->ctx);
183 /* The GetDC request might not be availible (such as occours when the
184 * NBT server is down). Fallback to a node status. It is the best
186 static void fallback_node_status(struct finddcs_state *state)
188 struct nbt_name_socket *nbtsock;
189 struct nbt_name_request *name_req;
191 state->node_status.in.name.name = "*";
192 state->node_status.in.name.type = NBT_NAME_CLIENT;
193 state->node_status.in.name.scope = NULL;
194 state->node_status.in.dest_addr = state->dcs[0].address;
195 state->node_status.in.timeout = 1;
196 state->node_status.in.retries = 2;
198 nbtsock = nbt_name_socket_init(state, state->ctx->event_ctx);
199 if (composite_nomem(nbtsock, state->ctx)) return;
201 name_req = nbt_name_status_send(nbtsock, &state->node_status);
202 if (composite_nomem(name_req, state->ctx)) return;
204 composite_continue_nbt(state->ctx,
206 fallback_node_status_replied,
210 /* We have a node status reply (or perhaps a timeout) */
211 static void fallback_node_status_replied(struct nbt_name_request *name_req)
214 struct finddcs_state *state = talloc_get_type(name_req->async.private, struct finddcs_state);
215 state->ctx->status = nbt_name_status_recv(name_req, state, &state->node_status);
216 if (!composite_is_ok(state->ctx)) return;
218 for (i=0; i < state->node_status.out.status.num_names; i++) {
220 if (state->node_status.out.status.names[i].type == NBT_NAME_SERVER) {
221 char *name = talloc_strndup(state->dcs, state->node_status.out.status.names[0].name, 15);
222 /* Strip space padding */
224 j = MIN(strlen(name), 15);
225 for (; j > 0 && name[j - 1] == ' '; j--) {
229 state->dcs[0].name = name;
230 composite_done(state->ctx);
234 composite_error(state->ctx, NT_STATUS_NO_LOGON_SERVERS);
237 NTSTATUS finddcs_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
238 int *num_dcs, struct nbt_dc_name **dcs)
240 NTSTATUS status = composite_wait(c);
241 if (NT_STATUS_IS_OK(status)) {
242 struct finddcs_state *state =
243 talloc_get_type(c->private_data, struct finddcs_state);
244 *num_dcs = state->num_dcs;
245 *dcs = talloc_steal(mem_ctx, state->dcs);
251 NTSTATUS finddcs(TALLOC_CTX *mem_ctx,
252 const char *domain_name, int name_type,
253 struct dom_sid *domain_sid,
254 const char **methods,
255 struct event_context *event_ctx,
256 struct messaging_context *msg_ctx,
257 int *num_dcs, struct nbt_dc_name **dcs)
259 struct composite_context *c = finddcs_send(mem_ctx,
260 domain_name, name_type,
263 return finddcs_recv(c, mem_ctx, num_dcs, dcs);