2 Unix SMB/CIFS implementation.
4 Get a struct wb_dom_info for a trusted domain, relying on "our" DC.
6 Copyright (C) Volker Lendecke 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "libcli/composite/composite.h"
25 #include "winbind/wb_server.h"
26 #include "smbd/service_task.h"
27 #include "librpc/gen_ndr/ndr_netlogon.h"
29 struct trusted_dom_info_state {
30 struct composite_context *ctx;
31 struct wbsrv_service *service;
32 struct wbsrv_domain *my_domain;
34 struct netr_DsRGetDCName d;
35 struct netr_GetAnyDCName g;
37 struct wb_dom_info *info;
40 static void trusted_dom_info_recv_domain(struct composite_context *ctx);
41 static void trusted_dom_info_recv_dsr(struct rpc_request *req);
42 static void trusted_dom_info_recv_dcname(struct rpc_request *req);
43 static void trusted_dom_info_recv_dcaddr(struct composite_context *ctx);
45 struct composite_context *wb_trusted_dom_info_send(TALLOC_CTX *mem_ctx,
46 struct wbsrv_service *service,
47 const char *domain_name,
48 const struct dom_sid *sid)
50 struct composite_context *result, *ctx;
51 struct trusted_dom_info_state *state;
53 result = talloc(mem_ctx, struct composite_context);
54 if (result == NULL) goto failed;
55 result->state = COMPOSITE_STATE_IN_PROGRESS;
56 result->async.fn = NULL;
57 result->event_ctx = service->task->event_ctx;
59 state = talloc(result, struct trusted_dom_info_state);
60 if (state == NULL) goto failed;
62 result->private_data = state;
64 state->info = talloc_zero(state, struct wb_dom_info);
65 if (state->info == NULL) goto failed;
67 state->service = service;
69 state->info->sid = dom_sid_dup(state->info, sid);
70 if (state->info->sid == NULL) goto failed;
72 state->info->name = talloc_strdup(state->info, domain_name);
73 if (state->info->name == NULL) goto failed;
75 ctx = wb_sid2domain_send(state, service, service->primary_sid);
76 if (ctx == NULL) goto failed;
78 ctx->async.fn = trusted_dom_info_recv_domain;
79 ctx->async.private_data = state;
87 static void trusted_dom_info_recv_domain(struct composite_context *ctx)
89 struct trusted_dom_info_state *state =
90 talloc_get_type(ctx->async.private_data,
91 struct trusted_dom_info_state);
92 struct rpc_request *req;
94 state->ctx->status = wb_sid2domain_recv(ctx, &state->my_domain);
95 if (!composite_is_ok(state->ctx)) return;
97 state->d.in.server_unc =
98 talloc_asprintf(state, "\\\\%s",
99 state->my_domain->info->dc_name);
100 if (composite_nomem(state->d.in.server_unc,
103 state->d.in.domain_name = state->info->name;
104 state->d.in.domain_guid = NULL;
105 state->d.in.site_guid = NULL;
106 state->d.in.flags = 0x40000000;
108 req = dcerpc_netr_DsRGetDCName_send(state->my_domain->netlogon_pipe,
110 composite_continue_rpc(state->ctx, req, trusted_dom_info_recv_dsr,
115 * dcerpc_netr_DsRGetDCName has replied
118 static void trusted_dom_info_recv_dsr(struct rpc_request *req)
120 struct trusted_dom_info_state *state =
121 talloc_get_type(req->async.private,
122 struct trusted_dom_info_state);
124 state->ctx->status = dcerpc_ndr_request_recv(req);
125 if (!NT_STATUS_IS_OK(state->ctx->status)) {
126 DEBUG(9, ("dcerpc_ndr_request_recv returned %s\n",
127 nt_errstr(state->ctx->status)));
132 werror_to_ntstatus(state->d.out.result);
133 if (!NT_STATUS_IS_OK(state->ctx->status)) {
134 DEBUG(9, ("dsrgetdcname returned %s\n",
135 nt_errstr(state->ctx->status)));
139 /* Hey, that was easy! */
141 state->info->dc_name = talloc_steal(state->info,
142 state->d.out.info->dc_unc);
143 if (*state->info->dc_name == '\\') state->info->dc_name++;
144 if (*state->info->dc_name == '\\') state->info->dc_name++;
146 state->info->dc_address = talloc_steal(state->info,
147 state->d.out.info->dc_address);
148 if (*state->info->dc_address == '\\') state->info->dc_address++;
149 if (*state->info->dc_address == '\\') state->info->dc_address++;
151 state->info->dns_name = talloc_steal(state->info,
152 state->d.out.info->domain_name);
154 composite_done(state->ctx);
159 state->g.in.logon_server = talloc_asprintf(
161 dcerpc_server_name(state->my_domain->netlogon_pipe));
162 state->g.in.domainname = state->info->name;
164 req = dcerpc_netr_GetAnyDCName_send(state->my_domain->netlogon_pipe,
166 if (composite_nomem(req, state->ctx)) return;
168 composite_continue_rpc(state->ctx, req, trusted_dom_info_recv_dcname,
172 static void trusted_dom_info_recv_dcname(struct rpc_request *req)
174 struct trusted_dom_info_state *state =
175 talloc_get_type(req->async.private,
176 struct trusted_dom_info_state);
177 struct composite_context *ctx;
178 struct nbt_name name;
180 state->ctx->status = dcerpc_ndr_request_recv(req);
181 if (!composite_is_ok(state->ctx)) return;
182 state->ctx->status = werror_to_ntstatus(state->g.out.result);
183 if (!composite_is_ok(state->ctx)) return;
185 state->info->dc_name = talloc_steal(state->info,
186 state->g.out.dcname);
188 if (*state->info->dc_name == '\\') state->info->dc_name++;
189 if (*state->info->dc_name == '\\') state->info->dc_name++;
191 make_nbt_name(&name, state->info->dc_name, 0x20);
192 ctx = resolve_name_send(&name, state->service->task->event_ctx,
193 lp_name_resolve_order());
195 composite_continue(state->ctx, ctx, trusted_dom_info_recv_dcaddr,
199 static void trusted_dom_info_recv_dcaddr(struct composite_context *ctx)
201 struct trusted_dom_info_state *state =
202 talloc_get_type(ctx->async.private_data,
203 struct trusted_dom_info_state);
205 state->ctx->status = resolve_name_recv(ctx, state->info,
206 &state->info->dc_address);
207 if (!composite_is_ok(state->ctx)) return;
209 composite_done(state->ctx);
212 NTSTATUS wb_trusted_dom_info_recv(struct composite_context *ctx,
214 struct wb_dom_info **result)
216 NTSTATUS status = composite_wait(ctx);
217 if (NT_STATUS_IS_OK(status)) {
218 struct trusted_dom_info_state *state =
219 talloc_get_type(ctx->private_data,
220 struct trusted_dom_info_state);
221 *result = talloc_steal(mem_ctx, state->info);
227 NTSTATUS wb_trusted_dom_info(TALLOC_CTX *mem_ctx,
228 struct wbsrv_service *service,
229 const char *domain_name,
230 const struct dom_sid *sid,
231 struct wb_dom_info **result)
233 struct composite_context *ctx =
234 wb_trusted_dom_info_send(mem_ctx, service, domain_name, sid);
235 return wb_trusted_dom_info_recv(ctx, mem_ctx, result);