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 "libcli/resolve/resolve.h"
26 #include "libcli/security/security.h"
27 #include "winbind/wb_server.h"
28 #include "smbd/service_task.h"
29 #include "librpc/gen_ndr/ndr_netlogon_c.h"
31 struct trusted_dom_info_state {
32 struct composite_context *ctx;
33 struct wbsrv_service *service;
34 struct wbsrv_domain *my_domain;
36 struct netr_DsRGetDCName d;
37 struct netr_GetAnyDCName g;
39 struct wb_dom_info *info;
42 static void trusted_dom_info_recv_domain(struct composite_context *ctx);
43 static void trusted_dom_info_recv_dsr(struct rpc_request *req);
44 static void trusted_dom_info_recv_dcname(struct rpc_request *req);
45 static void trusted_dom_info_recv_dcaddr(struct composite_context *ctx);
47 struct composite_context *wb_trusted_dom_info_send(TALLOC_CTX *mem_ctx,
48 struct wbsrv_service *service,
49 const char *domain_name,
50 const struct dom_sid *sid)
52 struct composite_context *result, *ctx;
53 struct trusted_dom_info_state *state;
55 result = composite_create(mem_ctx, service->task->event_ctx);
56 if (result == NULL) goto failed;
58 composite_error(result, NT_STATUS_FOOBAR);
64 state = talloc(result, struct trusted_dom_info_state);
65 if (state == NULL) goto failed;
67 result->private_data = state;
69 state->info = talloc_zero(state, struct wb_dom_info);
70 if (state->info == NULL) goto failed;
72 state->service = service;
74 state->info->sid = dom_sid_dup(state->info, sid);
75 if (state->info->sid == NULL) goto failed;
77 state->info->name = talloc_strdup(state->info, domain_name);
78 if (state->info->name == NULL) goto failed;
80 ctx = wb_sid2domain_send(state, service, service->primary_sid);
81 if (ctx == NULL) goto failed;
83 ctx->async.fn = trusted_dom_info_recv_domain;
84 ctx->async.private_data = state;
92 static void trusted_dom_info_recv_domain(struct composite_context *ctx)
94 struct trusted_dom_info_state *state =
95 talloc_get_type(ctx->async.private_data,
96 struct trusted_dom_info_state);
97 struct rpc_request *req;
99 state->ctx->status = wb_sid2domain_recv(ctx, &state->my_domain);
100 if (!composite_is_ok(state->ctx)) return;
102 state->d.in.server_unc =
103 talloc_asprintf(state, "\\\\%s",
104 state->my_domain->info->dc_name);
105 if (composite_nomem(state->d.in.server_unc,
108 state->d.in.domain_name = state->info->name;
109 state->d.in.domain_guid = NULL;
110 state->d.in.site_guid = NULL;
111 state->d.in.flags = 0x40000000;
113 req = dcerpc_netr_DsRGetDCName_send(state->my_domain->netlogon_pipe,
115 composite_continue_rpc(state->ctx, req, trusted_dom_info_recv_dsr,
120 * dcerpc_netr_DsRGetDCName has replied
123 static void trusted_dom_info_recv_dsr(struct rpc_request *req)
125 struct trusted_dom_info_state *state =
126 talloc_get_type(req->async.private_data,
127 struct trusted_dom_info_state);
129 state->ctx->status = dcerpc_ndr_request_recv(req);
130 if (!NT_STATUS_IS_OK(state->ctx->status)) {
131 DEBUG(9, ("dcerpc_ndr_request_recv returned %s\n",
132 nt_errstr(state->ctx->status)));
137 werror_to_ntstatus(state->d.out.result);
138 if (!NT_STATUS_IS_OK(state->ctx->status)) {
139 DEBUG(9, ("dsrgetdcname returned %s\n",
140 nt_errstr(state->ctx->status)));
144 /* Hey, that was easy! */
146 state->info->dc_name = talloc_steal(state->info,
147 state->d.out.info->dc_unc);
148 if (*state->info->dc_name == '\\') state->info->dc_name++;
149 if (*state->info->dc_name == '\\') state->info->dc_name++;
151 state->info->dc_address = talloc_steal(state->info,
152 state->d.out.info->dc_address);
153 if (*state->info->dc_address == '\\') state->info->dc_address++;
154 if (*state->info->dc_address == '\\') state->info->dc_address++;
156 state->info->dns_name = talloc_steal(state->info,
157 state->d.out.info->domain_name);
159 composite_done(state->ctx);
164 state->g.in.logon_server = talloc_asprintf(
166 dcerpc_server_name(state->my_domain->netlogon_pipe));
167 state->g.in.domainname = state->info->name;
169 req = dcerpc_netr_GetAnyDCName_send(state->my_domain->netlogon_pipe,
171 if (composite_nomem(req, state->ctx)) return;
173 composite_continue_rpc(state->ctx, req, trusted_dom_info_recv_dcname,
177 static void trusted_dom_info_recv_dcname(struct rpc_request *req)
179 struct trusted_dom_info_state *state =
180 talloc_get_type(req->async.private_data,
181 struct trusted_dom_info_state);
182 struct composite_context *ctx;
183 struct nbt_name name;
185 state->ctx->status = dcerpc_ndr_request_recv(req);
186 if (!composite_is_ok(state->ctx)) return;
187 state->ctx->status = werror_to_ntstatus(state->g.out.result);
188 if (!composite_is_ok(state->ctx)) return;
190 state->info->dc_name = talloc_steal(state->info,
191 state->g.out.dcname);
193 if (*state->info->dc_name == '\\') state->info->dc_name++;
194 if (*state->info->dc_name == '\\') state->info->dc_name++;
196 make_nbt_name(&name, state->info->dc_name, 0x20);
197 ctx = resolve_name_send(&name, state->service->task->event_ctx,
198 lp_name_resolve_order());
200 composite_continue(state->ctx, ctx, trusted_dom_info_recv_dcaddr,
204 static void trusted_dom_info_recv_dcaddr(struct composite_context *ctx)
206 struct trusted_dom_info_state *state =
207 talloc_get_type(ctx->async.private_data,
208 struct trusted_dom_info_state);
210 state->ctx->status = resolve_name_recv(ctx, state->info,
211 &state->info->dc_address);
212 if (!composite_is_ok(state->ctx)) return;
214 composite_done(state->ctx);
218 NTSTATUS wb_trusted_dom_info_recv(struct composite_context *ctx,
220 struct wb_dom_info **result)
222 NTSTATUS status = composite_wait(ctx);
223 if (NT_STATUS_IS_OK(status)) {
224 struct trusted_dom_info_state *state =
225 talloc_get_type(ctx->private_data,
226 struct trusted_dom_info_state);
227 *result = talloc_steal(mem_ctx, state->info);
233 NTSTATUS wb_trusted_dom_info(TALLOC_CTX *mem_ctx,
234 struct wbsrv_service *service,
235 const char *domain_name,
236 const struct dom_sid *sid,
237 struct wb_dom_info **result)
239 struct composite_context *ctx =
240 wb_trusted_dom_info_send(mem_ctx, service, domain_name, sid);
241 return wb_trusted_dom_info_recv(ctx, mem_ctx, result);