2 Unix SMB/CIFS implementation.
4 Copyright (C) Volker Lendecke 2005
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 a composite API for finding a DC and its name
25 #include "libcli/raw/libcliraw.h"
26 #include "libcli/composite/composite.h"
27 #include "winbind/wb_async_helpers.h"
29 #include "librpc/gen_ndr/nbt.h"
30 #include "librpc/gen_ndr/samr.h"
31 #include "lib/messaging/irpc.h"
32 #include "librpc/gen_ndr/irpc.h"
33 #include "librpc/gen_ndr/ndr_irpc.h"
35 struct finddcs_state {
36 struct wb_finddcs *io;
37 struct composite_context *creq;
39 struct nbtd_getdcname *r;
40 struct irpc_request *ireq;
43 static void finddcs_getdc(struct irpc_request *req)
45 struct composite_context *c = talloc_get_type(req->async.private,
46 struct composite_context);
47 struct finddcs_state *state =
48 talloc_get_type(c->private, struct finddcs_state);
50 c->status = irpc_call_recv(req);
51 if (!NT_STATUS_IS_OK(c->status)) {
55 state->io->out.dcs[0].name = talloc_steal(state->io->out.dcs,
56 state->r->out.dcname);
58 c->status = NT_STATUS_OK;
59 c->state = SMBCLI_REQUEST_DONE;
62 if (!NT_STATUS_IS_OK(c->status)) {
63 c->state = SMBCLI_REQUEST_ERROR;
66 if (c->state >= SMBCLI_REQUEST_DONE &&
73 called when name resolution is finished
75 static void finddcs_resolve(struct composite_context *res_ctx)
77 struct composite_context *c = talloc_get_type(res_ctx->async.private,
78 struct composite_context);
79 struct finddcs_state *state =
80 talloc_get_type(c->private, struct finddcs_state);
81 uint32_t *nbt_servers;
83 state->io->out.num_dcs = 1;
84 state->io->out.dcs = talloc_array(state, struct nbt_dc_name,
85 state->io->out.num_dcs);
86 if (state->io->out.dcs == NULL) {
87 c->status = NT_STATUS_NO_MEMORY;
91 c->status = resolve_name_recv(res_ctx, state->io->out.dcs,
92 &state->io->out.dcs[0].address);
93 if (!NT_STATUS_IS_OK(c->status)) {
97 nbt_servers = irpc_servers_byname(state->io->in.msg_ctx, "nbt_server");
98 if ((nbt_servers == NULL) || (nbt_servers[0] == 0)) {
99 c->status = NT_STATUS_NO_LOGON_SERVERS;
103 state->r = talloc(state, struct nbtd_getdcname);
104 if (state->r == NULL) {
105 c->status = NT_STATUS_NO_MEMORY;
109 state->r->in.domainname = talloc_strdup(state->r, lp_workgroup());
110 state->r->in.ip_address = state->io->out.dcs[0].address;
111 state->r->in.my_computername = lp_netbios_name();
112 state->r->in.my_accountname = talloc_asprintf(state->r, "%s$",
114 state->r->in.account_control = ACB_WSTRUST;
115 state->r->in.domain_sid = secrets_get_domain_sid(state->r,
118 if ((state->r->in.domainname == NULL) ||
119 (state->r->in.my_accountname == NULL)) {
120 DEBUG(0, ("talloc failed\n"));
121 c->status = NT_STATUS_NO_MEMORY;
124 if (state->r->in.domain_sid == NULL) {
125 c->status = NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
129 state->ireq = irpc_call_send(state->io->in.msg_ctx, nbt_servers[0],
130 &dcerpc_table_irpc, DCERPC_NBTD_GETDCNAME,
133 if (state->ireq == NULL) {
134 c->status = NT_STATUS_NO_MEMORY;
138 c->status = NT_STATUS_OK;
139 state->ireq->async.fn = finddcs_getdc;
140 state->ireq->async.private = c;
143 if (!NT_STATUS_IS_OK(c->status)) {
144 c->state = SMBCLI_REQUEST_ERROR;
147 if (c->state >= SMBCLI_REQUEST_DONE &&
153 struct composite_context *wb_finddcs_send(struct wb_finddcs *io,
154 struct event_context *event_ctx)
156 struct composite_context *c;
157 struct finddcs_state *state;
158 struct nbt_name name;
160 c = talloc_zero(NULL, struct composite_context);
161 if (c == NULL) goto failed;
162 c->state = SMBCLI_REQUEST_SEND;
163 c->event_ctx = event_ctx;
165 state = talloc(c, struct finddcs_state);
166 if (state == NULL) goto failed;
170 make_nbt_name(&name, io->in.domain, 0x1c);
171 state->creq = resolve_name_send(&name, c->event_ctx,
172 lp_name_resolve_order());
174 if (state->creq == NULL) goto failed;
175 state->creq->async.private = c;
176 state->creq->async.fn = finddcs_resolve;
185 NTSTATUS wb_finddcs_recv(struct composite_context *c, TALLOC_CTX *mem_ctx)
189 status = composite_wait(c);
191 if (NT_STATUS_IS_OK(status)) {
192 struct finddcs_state *state =
193 talloc_get_type(c->private, struct finddcs_state);
194 talloc_steal(mem_ctx, state->io->out.dcs);
201 NTSTATUS wb_finddcs(struct wb_finddcs *io, TALLOC_CTX *mem_ctx,
202 struct event_context *ev)
204 struct composite_context *c = wb_finddcs_send(io, ev);
205 return wb_finddcs_recv(c, mem_ctx);