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 "lib/messaging/irpc.h"
25 #include "librpc/gen_ndr/ndr_irpc.h"
26 #include "librpc/gen_ndr/samr.h"
27 #include "libcli/composite/composite.h"
28 #include "libcli/libcli.h"
29 #include "libcli/resolve/resolve.h"
30 #include "libcli/finddcs.h"
32 struct finddcs_state
{
33 struct composite_context
*ctx
;
34 struct messaging_context
*msg_ctx
;
36 const char *my_netbios_name
;
37 const char *domain_name
;
38 struct dom_sid
*domain_sid
;
40 struct nbtd_getdcname r
;
41 struct nbt_name_status node_status
;
44 struct nbt_dc_name
*dcs
;
48 static void finddcs_name_resolved(struct composite_context
*ctx
);
49 static void finddcs_getdc_replied(struct irpc_request
*ireq
);
50 static void fallback_node_status(struct finddcs_state
*state
);
51 static void fallback_node_status_replied(struct nbt_name_request
*name_req
);
54 * Setup and send off the a normal name resolution for the target name.
56 * The domain_sid parameter is optional, and is used in the subsequent getdc request.
58 * This will try a GetDC request, but this may not work. It will try
59 * a node status as a fallback, then return no name (but still include
63 struct composite_context
*finddcs_send(TALLOC_CTX
*mem_ctx
,
64 const char *my_netbios_name
,
66 const char *domain_name
,
68 struct dom_sid
*domain_sid
,
69 struct resolve_context
*resolve_ctx
,
70 struct tevent_context
*event_ctx
,
71 struct messaging_context
*msg_ctx
)
73 struct composite_context
*c
, *creq
;
74 struct finddcs_state
*state
;
77 c
= composite_create(mem_ctx
, event_ctx
);
78 if (c
== NULL
) return NULL
;
80 state
= talloc(c
, struct finddcs_state
);
81 if (composite_nomem(state
, c
)) return c
;
82 c
->private_data
= state
;
86 state
->nbt_port
= nbt_port
;
87 state
->my_netbios_name
= talloc_strdup(state
, my_netbios_name
);
88 state
->domain_name
= talloc_strdup(state
, domain_name
);
89 if (composite_nomem(state
->domain_name
, c
)) return c
;
92 state
->domain_sid
= talloc_reference(state
, domain_sid
);
93 if (composite_nomem(state
->domain_sid
, c
)) return c
;
95 state
->domain_sid
= NULL
;
98 state
->msg_ctx
= msg_ctx
;
100 make_nbt_name(&name
, state
->domain_name
, name_type
);
101 creq
= resolve_name_send(resolve_ctx
, state
, &name
, event_ctx
);
102 composite_continue(c
, creq
, finddcs_name_resolved
, state
);
106 /* Having got an name query answer, fire off a GetDC request, so we
107 * can find the target's all-important name. (Kerberos and some
108 * netlogon operations are quite picky about names)
110 * The name is a courtesy, if we don't find it, don't completely fail.
112 * However, if the nbt server is down, fall back to a node status
115 static void finddcs_name_resolved(struct composite_context
*ctx
)
117 struct finddcs_state
*state
=
118 talloc_get_type(ctx
->async
.private_data
, struct finddcs_state
);
119 struct irpc_request
*ireq
;
120 struct server_id
*nbt_servers
;
123 state
->ctx
->status
= resolve_name_recv(ctx
, state
, &address
);
124 if (!composite_is_ok(state
->ctx
)) return;
126 /* TODO: This should try and find all the DCs, and give the
127 * caller them in the order they responded */
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
, state
, "nbt_server");
145 if ((nbt_servers
== NULL
) || (nbt_servers
[0].id
== 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
= state
->my_netbios_name
;
153 state
->r
.in
.my_accountname
= talloc_asprintf(state
, "%s$", state
->my_netbios_name
);
154 if (composite_nomem(state
->r
.in
.my_accountname
, state
->ctx
)) return;
155 state
->r
.in
.account_control
= ACB_WSTRUST
;
156 state
->r
.in
.domain_sid
= state
->domain_sid
;
157 if (state
->r
.in
.domain_sid
== NULL
) {
158 state
->r
.in
.domain_sid
= talloc_zero(state
, struct dom_sid
);
161 ireq
= irpc_call_send(state
->msg_ctx
, nbt_servers
[0],
162 &ndr_table_irpc
, NDR_NBTD_GETDCNAME
,
165 fallback_node_status(state
);
169 composite_continue_irpc(state
->ctx
, ireq
, finddcs_getdc_replied
, state
);
172 /* Called when the GetDC request returns */
173 static void finddcs_getdc_replied(struct irpc_request
*ireq
)
175 struct finddcs_state
*state
=
176 talloc_get_type(ireq
->async
.private_data
, struct finddcs_state
);
178 state
->ctx
->status
= irpc_call_recv(ireq
);
179 if (!composite_is_ok(state
->ctx
)) return;
181 state
->dcs
[0].name
= talloc_steal(state
->dcs
, state
->r
.out
.dcname
);
182 composite_done(state
->ctx
);
185 /* The GetDC request might not be available (such as occours when the
186 * NBT server is down). Fallback to a node status. It is the best
188 static void fallback_node_status(struct finddcs_state
*state
)
190 struct nbt_name_socket
*nbtsock
;
191 struct nbt_name_request
*name_req
;
193 state
->node_status
.in
.name
.name
= "*";
194 state
->node_status
.in
.name
.type
= NBT_NAME_CLIENT
;
195 state
->node_status
.in
.name
.scope
= NULL
;
196 state
->node_status
.in
.dest_addr
= state
->dcs
[0].address
;
197 state
->node_status
.in
.dest_port
= state
->nbt_port
;
198 state
->node_status
.in
.timeout
= 1;
199 state
->node_status
.in
.retries
= 2;
201 nbtsock
= nbt_name_socket_init(state
, state
->ctx
->event_ctx
);
202 if (composite_nomem(nbtsock
, state
->ctx
)) return;
204 name_req
= nbt_name_status_send(nbtsock
, &state
->node_status
);
205 if (composite_nomem(name_req
, state
->ctx
)) return;
207 composite_continue_nbt(state
->ctx
,
209 fallback_node_status_replied
,
213 /* We have a node status reply (or perhaps a timeout) */
214 static void fallback_node_status_replied(struct nbt_name_request
*name_req
)
217 struct finddcs_state
*state
= talloc_get_type(name_req
->async
.private_data
, struct finddcs_state
);
218 state
->ctx
->status
= nbt_name_status_recv(name_req
, state
, &state
->node_status
);
219 if (!composite_is_ok(state
->ctx
)) return;
221 for (i
=0; i
< state
->node_status
.out
.status
.num_names
; i
++) {
223 if (state
->node_status
.out
.status
.names
[i
].type
== NBT_NAME_SERVER
) {
224 char *name
= talloc_strndup(state
->dcs
, state
->node_status
.out
.status
.names
[0].name
, 15);
225 /* Strip space padding */
227 j
= MIN(strlen(name
), 15);
228 for (; j
> 0 && name
[j
- 1] == ' '; j
--) {
232 state
->dcs
[0].name
= name
;
233 composite_done(state
->ctx
);
237 composite_error(state
->ctx
, NT_STATUS_NO_LOGON_SERVERS
);
240 NTSTATUS
finddcs_recv(struct composite_context
*c
, TALLOC_CTX
*mem_ctx
,
241 int *num_dcs
, struct nbt_dc_name
**dcs
)
243 NTSTATUS status
= composite_wait(c
);
244 if (NT_STATUS_IS_OK(status
)) {
245 struct finddcs_state
*state
=
246 talloc_get_type(c
->private_data
, struct finddcs_state
);
247 *num_dcs
= state
->num_dcs
;
248 *dcs
= talloc_steal(mem_ctx
, state
->dcs
);
254 NTSTATUS
finddcs(TALLOC_CTX
*mem_ctx
,
255 const char *my_netbios_name
,
257 const char *domain_name
, int name_type
,
258 struct dom_sid
*domain_sid
,
259 struct resolve_context
*resolve_ctx
,
260 struct tevent_context
*event_ctx
,
261 struct messaging_context
*msg_ctx
,
262 int *num_dcs
, struct nbt_dc_name
**dcs
)
264 struct composite_context
*c
= finddcs_send(mem_ctx
,
267 domain_name
, name_type
,
271 return finddcs_recv(c
, mem_ctx
, num_dcs
, dcs
);