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"
25 #include "lib/messaging/irpc.h"
26 #include "librpc/gen_ndr/ndr_irpc_c.h"
27 #include "libcli/composite/composite.h"
28 #include "libcli/libcli.h"
29 #include "libcli/resolve/resolve.h"
30 #include "lib/util/tevent_ntstatus.h"
32 struct finddcs_nbt_state {
33 struct tevent_context *ev;
34 struct tevent_req *req;
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;
45 struct nbt_dc_name *dcs;
49 static void finddcs_nbt_name_resolved(struct composite_context *ctx);
50 static void finddcs_nbt_getdc_replied(struct tevent_req *subreq);
51 static void fallback_node_status(struct finddcs_nbt_state *state);
52 static void fallback_node_status_replied(struct nbt_name_request *name_req);
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
64 struct tevent_req *finddcs_nbt_send(TALLOC_CTX *mem_ctx,
65 const char *my_netbios_name,
67 const char *domain_name,
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 finddcs_nbt_state *state;
76 struct tevent_req *req;
77 struct composite_context *creq;
79 req = tevent_req_create(mem_ctx, &state, struct finddcs_nbt_state);
85 state->ev = event_ctx;
86 state->nbt_port = nbt_port;
87 state->my_netbios_name = talloc_strdup(state, my_netbios_name);
88 if (tevent_req_nomem(state->my_netbios_name, req)) {
89 return tevent_req_post(req, event_ctx);
91 state->domain_name = talloc_strdup(state, domain_name);
92 if (tevent_req_nomem(state->domain_name, req)) {
93 return tevent_req_post(req, event_ctx);
97 state->domain_sid = talloc_reference(state, domain_sid);
98 if (tevent_req_nomem(state->domain_sid, req)) {
99 return tevent_req_post(req, event_ctx);
102 state->domain_sid = NULL;
105 state->msg_ctx = msg_ctx;
107 make_nbt_name(&name, state->domain_name, name_type);
108 creq = resolve_name_send(resolve_ctx, state, &name, event_ctx);
109 if (tevent_req_nomem(creq, req)) {
110 return tevent_req_post(req, event_ctx);
112 creq->async.fn = finddcs_nbt_name_resolved;
113 creq->async.private_data = state;
118 /* Having got an name query answer, fire off a GetDC request, so we
119 * can find the target's all-important name. (Kerberos and some
120 * netlogon operations are quite picky about names)
122 * The name is a courtesy, if we don't find it, don't completely fail.
124 * However, if the nbt server is down, fall back to a node status
127 static void finddcs_nbt_name_resolved(struct composite_context *ctx)
129 struct finddcs_nbt_state *state =
130 talloc_get_type(ctx->async.private_data, struct finddcs_nbt_state);
131 struct tevent_req *subreq;
132 struct dcerpc_binding_handle *irpc_handle;
136 status = resolve_name_recv(ctx, state, &address);
137 if (tevent_req_nterror(state->req, status)) {
141 /* TODO: This should try and find all the DCs, and give the
142 * caller them in the order they responded */
145 state->dcs = talloc_array(state, struct nbt_dc_name, state->num_dcs);
146 if (tevent_req_nomem(state->dcs, state->req)) {
150 state->dcs[0].address = talloc_steal(state->dcs, address);
152 /* Try and find the nbt server. Fallback to a node status
153 * request if we can't make this happen The nbt server just
154 * might not be running, or we may not have a messaging
155 * context (not root etc) */
156 if (!state->msg_ctx) {
157 fallback_node_status(state);
161 irpc_handle = irpc_binding_handle_by_name(state, state->msg_ctx,
162 "nbt_server", &ndr_table_irpc);
163 if (irpc_handle == NULL) {
164 fallback_node_status(state);
168 state->r.in.domainname = state->domain_name;
169 state->r.in.ip_address = state->dcs[0].address;
170 state->r.in.my_computername = state->my_netbios_name;
171 state->r.in.my_accountname = talloc_asprintf(state, "%s$", state->my_netbios_name);
172 if (tevent_req_nomem(state->r.in.my_accountname, state->req)) {
175 state->r.in.account_control = ACB_WSTRUST;
176 state->r.in.domain_sid = state->domain_sid;
177 if (state->r.in.domain_sid == NULL) {
178 state->r.in.domain_sid = talloc_zero(state, struct dom_sid);
181 subreq = dcerpc_nbtd_getdcname_r_send(state, state->ev,
182 irpc_handle, &state->r);
183 if (tevent_req_nomem(subreq, state->req)) {
186 tevent_req_set_callback(subreq, finddcs_nbt_getdc_replied, state);
189 /* Called when the GetDC request returns */
190 static void finddcs_nbt_getdc_replied(struct tevent_req *subreq)
192 struct finddcs_nbt_state *state =
193 tevent_req_callback_data(subreq,
194 struct finddcs_nbt_state);
197 status = dcerpc_nbtd_getdcname_r_recv(subreq, state);
199 if (!NT_STATUS_IS_OK(status)) {
200 fallback_node_status(state);
204 state->dcs[0].name = talloc_steal(state->dcs, state->r.out.dcname);
205 tevent_req_done(state->req);
208 /* The GetDC request might not be available (such as occours when the
209 * NBT server is down). Fallback to a node status. It is the best
211 static void fallback_node_status(struct finddcs_nbt_state *state)
213 struct nbt_name_socket *nbtsock;
214 struct nbt_name_request *name_req;
216 state->node_status.in.name.name = "*";
217 state->node_status.in.name.type = NBT_NAME_CLIENT;
218 state->node_status.in.name.scope = NULL;
219 state->node_status.in.dest_addr = state->dcs[0].address;
220 state->node_status.in.dest_port = state->nbt_port;
221 state->node_status.in.timeout = 1;
222 state->node_status.in.retries = 2;
224 nbtsock = nbt_name_socket_init(state, state->ev);
225 if (tevent_req_nomem(nbtsock, state->req)) {
229 name_req = nbt_name_status_send(nbtsock, &state->node_status);
230 if (tevent_req_nomem(name_req, state->req)) {
234 name_req->async.fn = fallback_node_status_replied;
235 name_req->async.private_data = state;
238 /* We have a node status reply (or perhaps a timeout) */
239 static void fallback_node_status_replied(struct nbt_name_request *name_req)
242 struct finddcs_nbt_state *state = talloc_get_type(name_req->async.private_data, struct finddcs_nbt_state);
245 status = nbt_name_status_recv(name_req, state, &state->node_status);
246 if (tevent_req_nterror(state->req, status)) {
250 for (i=0; i < state->node_status.out.status.num_names; i++) {
252 if (state->node_status.out.status.names[i].type == NBT_NAME_SERVER) {
253 char *name = talloc_strndup(state->dcs, state->node_status.out.status.names[0].name, 15);
254 /* Strip space padding */
256 j = MIN(strlen(name), 15);
257 for (; j > 0 && name[j - 1] == ' '; j--) {
261 state->dcs[0].name = name;
262 tevent_req_done(state->req);
266 tevent_req_nterror(state->req, NT_STATUS_NO_LOGON_SERVERS);
269 NTSTATUS finddcs_nbt_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
270 int *num_dcs, struct nbt_dc_name **dcs)
272 struct finddcs_nbt_state *state = tevent_req_data(req, struct finddcs_nbt_state);
276 ok = tevent_req_poll(req, state->ev);
279 return NT_STATUS_INTERNAL_ERROR;
281 status = tevent_req_simple_recv_ntstatus(req);
282 if (NT_STATUS_IS_OK(status)) {
283 *num_dcs = state->num_dcs;
284 *dcs = talloc_steal(mem_ctx, state->dcs);
290 NTSTATUS finddcs_nbt(TALLOC_CTX *mem_ctx,
291 const char *my_netbios_name,
293 const char *domain_name, int name_type,
294 struct dom_sid *domain_sid,
295 struct resolve_context *resolve_ctx,
296 struct tevent_context *event_ctx,
297 struct messaging_context *msg_ctx,
298 int *num_dcs, struct nbt_dc_name **dcs)
300 struct tevent_req *req = finddcs_nbt_send(mem_ctx,
303 domain_name, name_type,
307 return finddcs_nbt_recv(req, mem_ctx, num_dcs, dcs);