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/smb_composite/smb_composite.h"
26 #include "winbind/wb_async_helpers.h"
27 #include "winbind/wb_server.h"
28 #include "smbd/service_stream.h"
29 #include "smbd/service_task.h"
30 #include "librpc/gen_ndr/nbt.h"
31 #include "librpc/gen_ndr/ndr_netlogon.h"
33 struct trusted_dom_info_state {
34 struct composite_context *ctx;
35 struct wbsrv_service *service;
36 struct wbsrv_domain *my_domain;
38 struct netr_DsRGetDCName d;
39 struct netr_GetAnyDCName g;
41 struct wb_dom_info *info;
44 static void trusted_dom_info_recv_domain(struct composite_context *ctx);
45 static void trusted_dom_info_recv_dsr(struct rpc_request *req);
46 static void trusted_dom_info_recv_dcname(struct rpc_request *req);
47 static void trusted_dom_info_recv_dcaddr(struct composite_context *ctx);
49 struct composite_context *wb_trusted_dom_info_send(TALLOC_CTX *mem_ctx,
50 struct wbsrv_service *service,
51 const char *domain_name,
52 const struct dom_sid *sid)
54 struct composite_context *result, *ctx;
55 struct trusted_dom_info_state *state;
57 result = talloc(mem_ctx, struct composite_context);
58 if (result == NULL) goto failed;
59 result->state = COMPOSITE_STATE_IN_PROGRESS;
60 result->async.fn = NULL;
61 result->event_ctx = service->task->event_ctx;
63 state = talloc(result, struct trusted_dom_info_state);
64 if (state == NULL) goto failed;
66 result->private_data = state;
68 state->info = talloc_zero(state, struct wb_dom_info);
69 if (state->info == NULL) goto failed;
71 state->service = service;
73 state->info->sid = dom_sid_dup(state->info, sid);
74 if (state->info->sid == NULL) goto failed;
76 state->info->name = talloc_strdup(state->info, domain_name);
77 if (state->info->name == NULL) goto failed;
79 ctx = wb_sid2domain_send(state, service, service->primary_sid);
80 if (ctx == NULL) goto failed;
82 ctx->async.fn = trusted_dom_info_recv_domain;
83 ctx->async.private_data = state;
91 static void trusted_dom_info_recv_domain(struct composite_context *ctx)
93 struct trusted_dom_info_state *state =
94 talloc_get_type(ctx->async.private_data,
95 struct trusted_dom_info_state);
96 struct rpc_request *req;
98 state->ctx->status = wb_sid2domain_recv(ctx, &state->my_domain);
99 if (!composite_is_ok(state->ctx)) return;
101 state->d.in.server_unc =
102 talloc_asprintf(state, "\\\\%s",
103 state->my_domain->info->dc_name);
104 if (composite_nomem(state->d.in.server_unc,
107 state->d.in.domain_name = state->info->name;
108 state->d.in.domain_guid = NULL;
109 state->d.in.site_guid = NULL;
110 state->d.in.flags = 0x40000000;
112 req = dcerpc_netr_DsRGetDCName_send(state->my_domain->netlogon_pipe,
114 composite_continue_rpc(state->ctx, req, trusted_dom_info_recv_dsr,
119 * dcerpc_netr_DsRGetDCName has replied
122 static void trusted_dom_info_recv_dsr(struct rpc_request *req)
124 struct trusted_dom_info_state *state =
125 talloc_get_type(req->async.private,
126 struct trusted_dom_info_state);
128 state->ctx->status = dcerpc_ndr_request_recv(req);
129 if (!NT_STATUS_IS_OK(state->ctx->status)) {
130 DEBUG(9, ("dcerpc_ndr_request_recv returned %s\n",
131 nt_errstr(state->ctx->status)));
136 werror_to_ntstatus(state->d.out.result);
137 if (!NT_STATUS_IS_OK(state->ctx->status)) {
138 DEBUG(9, ("dsrgetdcname returned %s\n",
139 nt_errstr(state->ctx->status)));
143 /* Hey, that was easy! */
145 state->info->dc_name = talloc_steal(state->info,
146 state->d.out.info->dc_unc);
147 if (*state->info->dc_name == '\\') state->info->dc_name++;
148 if (*state->info->dc_name == '\\') state->info->dc_name++;
150 state->info->dc_address = talloc_steal(state->info,
151 state->d.out.info->dc_address);
152 if (*state->info->dc_address == '\\') state->info->dc_address++;
153 if (*state->info->dc_address == '\\') state->info->dc_address++;
155 state->info->dns_name = talloc_steal(state->info,
156 state->d.out.info->domain_name);
158 composite_done(state->ctx);
163 state->g.in.logon_server = talloc_asprintf(
165 dcerpc_server_name(state->my_domain->netlogon_pipe));
166 state->g.in.domainname = state->info->name;
168 req = dcerpc_netr_GetAnyDCName_send(state->my_domain->netlogon_pipe,
170 if (composite_nomem(req, state->ctx)) return;
172 composite_continue_rpc(state->ctx, req, trusted_dom_info_recv_dcname,
176 static void trusted_dom_info_recv_dcname(struct rpc_request *req)
178 struct trusted_dom_info_state *state =
179 talloc_get_type(req->async.private,
180 struct trusted_dom_info_state);
181 struct composite_context *ctx;
182 struct nbt_name name;
184 state->ctx->status = dcerpc_ndr_request_recv(req);
185 if (!composite_is_ok(state->ctx)) return;
186 state->ctx->status = werror_to_ntstatus(state->g.out.result);
187 if (!composite_is_ok(state->ctx)) return;
189 state->info->dc_name = talloc_steal(state->info,
190 state->g.out.dcname);
192 if (*state->info->dc_name == '\\') state->info->dc_name++;
193 if (*state->info->dc_name == '\\') state->info->dc_name++;
195 make_nbt_name(&name, state->info->dc_name, 0x20);
196 ctx = resolve_name_send(&name, state->service->task->event_ctx,
197 lp_name_resolve_order());
199 composite_continue(state->ctx, ctx, trusted_dom_info_recv_dcaddr,
203 static void trusted_dom_info_recv_dcaddr(struct composite_context *ctx)
205 struct trusted_dom_info_state *state =
206 talloc_get_type(ctx->async.private_data,
207 struct trusted_dom_info_state);
209 state->ctx->status = resolve_name_recv(ctx, state->info,
210 &state->info->dc_address);
211 if (!composite_is_ok(state->ctx)) return;
213 composite_done(state->ctx);
216 NTSTATUS wb_trusted_dom_info_recv(struct composite_context *ctx,
218 struct wb_dom_info **result)
220 NTSTATUS status = composite_wait(ctx);
221 if (NT_STATUS_IS_OK(status)) {
222 struct trusted_dom_info_state *state =
223 talloc_get_type(ctx->private_data,
224 struct trusted_dom_info_state);
225 *result = talloc_steal(mem_ctx, state->info);
231 NTSTATUS wb_trusted_dom_info(TALLOC_CTX *mem_ctx,
232 struct wbsrv_service *service,
233 const char *domain_name,
234 const struct dom_sid *sid,
235 struct wb_dom_info **result)
237 struct composite_context *ctx =
238 wb_trusted_dom_info_send(mem_ctx, service, domain_name, sid);
239 return wb_trusted_dom_info_recv(ctx, mem_ctx, result);