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/finddc.h"
32 #include "lib/util/tevent_ntstatus.h"
34 struct finddcs_nbt_state
{
35 struct tevent_context
*ev
;
36 struct tevent_req
*req
;
37 struct messaging_context
*msg_ctx
;
39 const char *my_netbios_name
;
40 const char *domain_name
;
41 struct dom_sid
*domain_sid
;
43 struct nbtd_getdcname r
;
44 struct nbt_name_status node_status
;
47 struct nbt_dc_name
*dcs
;
51 static void finddcs_nbt_name_resolved(struct composite_context
*ctx
);
52 static void finddcs_nbt_getdc_replied(struct tevent_req
*subreq
);
53 static void fallback_node_status(struct finddcs_nbt_state
*state
);
54 static void fallback_node_status_replied(struct nbt_name_request
*name_req
);
57 * Setup and send off the a normal name resolution for the target name.
59 * The domain_sid parameter is optional, and is used in the subsequent getdc request.
61 * This will try a GetDC request, but this may not work. It will try
62 * a node status as a fallback, then return no name (but still include
66 struct tevent_req
*finddcs_nbt_send(TALLOC_CTX
*mem_ctx
,
67 const char *my_netbios_name
,
69 const char *domain_name
,
71 struct dom_sid
*domain_sid
,
72 struct resolve_context
*resolve_ctx
,
73 struct tevent_context
*event_ctx
,
74 struct messaging_context
*msg_ctx
)
76 struct finddcs_nbt_state
*state
;
78 struct tevent_req
*req
;
79 struct composite_context
*creq
;
81 req
= tevent_req_create(mem_ctx
, &state
, struct finddcs_nbt_state
);
87 state
->ev
= event_ctx
;
88 state
->nbt_port
= nbt_port
;
89 state
->my_netbios_name
= talloc_strdup(state
, my_netbios_name
);
90 if (tevent_req_nomem(state
->my_netbios_name
, req
)) {
91 return tevent_req_post(req
, event_ctx
);
93 state
->domain_name
= talloc_strdup(state
, domain_name
);
94 if (tevent_req_nomem(state
->domain_name
, req
)) {
95 return tevent_req_post(req
, event_ctx
);
99 state
->domain_sid
= talloc_reference(state
, domain_sid
);
100 if (tevent_req_nomem(state
->domain_sid
, req
)) {
101 return tevent_req_post(req
, event_ctx
);
104 state
->domain_sid
= NULL
;
107 state
->msg_ctx
= msg_ctx
;
109 make_nbt_name(&name
, state
->domain_name
, name_type
);
110 creq
= resolve_name_send(resolve_ctx
, state
, &name
, event_ctx
);
111 if (tevent_req_nomem(creq
, req
)) {
112 return tevent_req_post(req
, event_ctx
);
114 creq
->async
.fn
= finddcs_nbt_name_resolved
;
115 creq
->async
.private_data
= state
;
120 /* Having got an name query answer, fire off a GetDC request, so we
121 * can find the target's all-important name. (Kerberos and some
122 * netlogon operations are quite picky about names)
124 * The name is a courtesy, if we don't find it, don't completely fail.
126 * However, if the nbt server is down, fall back to a node status
129 static void finddcs_nbt_name_resolved(struct composite_context
*ctx
)
131 struct finddcs_nbt_state
*state
=
132 talloc_get_type(ctx
->async
.private_data
, struct finddcs_nbt_state
);
133 struct tevent_req
*subreq
;
134 struct dcerpc_binding_handle
*irpc_handle
;
138 status
= resolve_name_recv(ctx
, state
, &address
);
139 if (tevent_req_nterror(state
->req
, status
)) {
143 /* TODO: This should try and find all the DCs, and give the
144 * caller them in the order they responded */
147 state
->dcs
= talloc_array(state
, struct nbt_dc_name
, state
->num_dcs
);
148 if (tevent_req_nomem(state
->dcs
, state
->req
)) {
152 state
->dcs
[0].address
= talloc_steal(state
->dcs
, address
);
154 /* Try and find the nbt server. Fallback to a node status
155 * request if we can't make this happen The nbt server just
156 * might not be running, or we may not have a messaging
157 * context (not root etc) */
158 if (!state
->msg_ctx
) {
159 fallback_node_status(state
);
163 irpc_handle
= irpc_binding_handle_by_name(state
, state
->msg_ctx
,
164 "nbt_server", &ndr_table_irpc
);
165 if (irpc_handle
== NULL
) {
166 fallback_node_status(state
);
170 state
->r
.in
.domainname
= state
->domain_name
;
171 state
->r
.in
.ip_address
= state
->dcs
[0].address
;
172 state
->r
.in
.my_computername
= state
->my_netbios_name
;
173 state
->r
.in
.my_accountname
= talloc_asprintf(state
, "%s$", state
->my_netbios_name
);
174 if (tevent_req_nomem(state
->r
.in
.my_accountname
, state
->req
)) {
177 state
->r
.in
.account_control
= ACB_WSTRUST
;
178 state
->r
.in
.domain_sid
= state
->domain_sid
;
179 if (state
->r
.in
.domain_sid
== NULL
) {
180 state
->r
.in
.domain_sid
= talloc_zero(state
, struct dom_sid
);
183 subreq
= dcerpc_nbtd_getdcname_r_send(state
, state
->ev
,
184 irpc_handle
, &state
->r
);
185 if (tevent_req_nomem(subreq
, state
->req
)) {
188 tevent_req_set_callback(subreq
, finddcs_nbt_getdc_replied
, state
);
191 /* Called when the GetDC request returns */
192 static void finddcs_nbt_getdc_replied(struct tevent_req
*subreq
)
194 struct finddcs_nbt_state
*state
=
195 tevent_req_callback_data(subreq
,
196 struct finddcs_nbt_state
);
199 status
= dcerpc_nbtd_getdcname_r_recv(subreq
, state
);
201 if (!NT_STATUS_IS_OK(status
)) {
202 fallback_node_status(state
);
206 state
->dcs
[0].name
= talloc_steal(state
->dcs
, state
->r
.out
.dcname
);
207 tevent_req_done(state
->req
);
210 /* The GetDC request might not be available (such as occours when the
211 * NBT server is down). Fallback to a node status. It is the best
213 static void fallback_node_status(struct finddcs_nbt_state
*state
)
215 struct nbt_name_socket
*nbtsock
;
216 struct nbt_name_request
*name_req
;
218 state
->node_status
.in
.name
.name
= "*";
219 state
->node_status
.in
.name
.type
= NBT_NAME_CLIENT
;
220 state
->node_status
.in
.name
.scope
= NULL
;
221 state
->node_status
.in
.dest_addr
= state
->dcs
[0].address
;
222 state
->node_status
.in
.dest_port
= state
->nbt_port
;
223 state
->node_status
.in
.timeout
= 1;
224 state
->node_status
.in
.retries
= 2;
226 nbtsock
= nbt_name_socket_init(state
, state
->ev
);
227 if (tevent_req_nomem(nbtsock
, state
->req
)) {
231 name_req
= nbt_name_status_send(nbtsock
, &state
->node_status
);
232 if (tevent_req_nomem(name_req
, state
->req
)) {
236 name_req
->async
.fn
= fallback_node_status_replied
;
237 name_req
->async
.private_data
= state
;
240 /* We have a node status reply (or perhaps a timeout) */
241 static void fallback_node_status_replied(struct nbt_name_request
*name_req
)
244 struct finddcs_nbt_state
*state
= talloc_get_type(name_req
->async
.private_data
, struct finddcs_nbt_state
);
247 status
= nbt_name_status_recv(name_req
, state
, &state
->node_status
);
248 if (tevent_req_nterror(state
->req
, status
)) {
252 for (i
=0; i
< state
->node_status
.out
.status
.num_names
; i
++) {
254 if (state
->node_status
.out
.status
.names
[i
].type
== NBT_NAME_SERVER
) {
255 char *name
= talloc_strndup(state
->dcs
, state
->node_status
.out
.status
.names
[0].name
, 15);
256 /* Strip space padding */
258 j
= MIN(strlen(name
), 15);
259 for (; j
> 0 && name
[j
- 1] == ' '; j
--) {
263 state
->dcs
[0].name
= name
;
264 tevent_req_done(state
->req
);
268 tevent_req_nterror(state
->req
, NT_STATUS_NO_LOGON_SERVERS
);
271 NTSTATUS
finddcs_nbt_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
272 int *num_dcs
, struct nbt_dc_name
**dcs
)
274 struct finddcs_nbt_state
*state
= tevent_req_data(req
, struct finddcs_nbt_state
);
278 ok
= tevent_req_poll(req
, state
->ev
);
281 return NT_STATUS_INTERNAL_ERROR
;
283 status
= tevent_req_simple_recv_ntstatus(req
);
284 if (NT_STATUS_IS_OK(status
)) {
285 *num_dcs
= state
->num_dcs
;
286 *dcs
= talloc_steal(mem_ctx
, state
->dcs
);
292 NTSTATUS
finddcs_nbt(TALLOC_CTX
*mem_ctx
,
293 const char *my_netbios_name
,
295 const char *domain_name
, int name_type
,
296 struct dom_sid
*domain_sid
,
297 struct resolve_context
*resolve_ctx
,
298 struct tevent_context
*event_ctx
,
299 struct messaging_context
*msg_ctx
,
300 int *num_dcs
, struct nbt_dc_name
**dcs
)
302 struct tevent_req
*req
= finddcs_nbt_send(mem_ctx
,
305 domain_name
, name_type
,
309 return finddcs_nbt_recv(req
, mem_ctx
, num_dcs
, dcs
);