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 "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
;
45 struct nbt_dc_name
*dcs
;
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
);
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 composite_context
*finddcs_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 composite_context
*c
, *creq
;
75 struct finddcs_state
*state
;
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
;
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
;
93 state
->domain_sid
= talloc_reference(state
, domain_sid
);
94 if (composite_nomem(state
->domain_sid
, c
)) return c
;
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
);
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
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
;
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 */
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
);
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
);
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
);
177 status
= dcerpc_nbtd_getdcname_r_recv(subreq
, state
);
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
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
,
211 fallback_node_status_replied
,
215 /* We have a node status reply (or perhaps a timeout) */
216 static void fallback_node_status_replied(struct nbt_name_request
*name_req
)
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
++) {
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 */
229 j
= MIN(strlen(name
), 15);
230 for (; j
> 0 && name
[j
- 1] == ' '; j
--) {
234 state
->dcs
[0].name
= name
;
235 composite_done(state
->ctx
);
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
);
256 NTSTATUS
finddcs(TALLOC_CTX
*mem_ctx
,
257 const char *my_netbios_name
,
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
,
269 domain_name
, name_type
,
273 return finddcs_recv(c
, mem_ctx
, num_dcs
, dcs
);