s4-netlogon: implement dcesrv_netr_DsRAddressToSitenamesExW
[Samba/aatanasov.git] / source4 / winbind / wb_dom_info_trusted.c
blobc3bc754f69585e472dfee00ca3a199bafca53c45
1 /*
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 3 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, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "libcli/composite/composite.h"
24 #include "libcli/resolve/resolve.h"
25 #include "libcli/security/security.h"
26 #include "winbind/wb_server.h"
27 #include "smbd/service_task.h"
28 #include "librpc/gen_ndr/ndr_netlogon_c.h"
29 #include "libcli/libcli.h"
30 #include "param/param.h"
32 struct trusted_dom_info_state {
33 struct composite_context *ctx;
34 struct wbsrv_service *service;
35 struct wbsrv_domain *my_domain;
37 struct netr_DsRGetDCName d;
38 struct netr_GetAnyDCName g;
40 struct wb_dom_info *info;
43 static void trusted_dom_info_recv_domain(struct composite_context *ctx);
44 static void trusted_dom_info_recv_dsr(struct rpc_request *req);
45 static void trusted_dom_info_recv_dcname(struct rpc_request *req);
46 static void trusted_dom_info_recv_dcaddr(struct composite_context *ctx);
48 struct composite_context *wb_trusted_dom_info_send(TALLOC_CTX *mem_ctx,
49 struct wbsrv_service *service,
50 const char *domain_name,
51 const struct dom_sid *sid)
53 struct composite_context *result, *ctx;
54 struct trusted_dom_info_state *state;
56 result = composite_create(mem_ctx, service->task->event_ctx);
57 if (result == NULL) goto failed;
59 state = talloc(result, struct trusted_dom_info_state);
60 if (state == NULL) goto failed;
61 state->ctx = result;
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;
80 return result;
82 failed:
83 talloc_free(result);
84 return NULL;
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 dcerpc_server_name(state->my_domain->netlogon_pipe));
100 if (composite_nomem(state->d.in.server_unc,
101 state->ctx)) return;
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 = DS_RETURN_DNS_NAME;
107 state->d.out.info = talloc(state, struct netr_DsRGetDCNameInfo *);
108 if (composite_nomem(state->d.out.info, state->ctx)) return;
110 req = dcerpc_netr_DsRGetDCName_send(state->my_domain->netlogon_pipe,
111 state, &state->d);
112 composite_continue_rpc(state->ctx, req, trusted_dom_info_recv_dsr,
113 state);
117 * dcerpc_netr_DsRGetDCName has replied
120 static void trusted_dom_info_recv_dsr(struct rpc_request *req)
122 struct trusted_dom_info_state *state =
123 talloc_get_type(req->async.private_data,
124 struct trusted_dom_info_state);
126 state->ctx->status = dcerpc_ndr_request_recv(req);
127 if (!NT_STATUS_IS_OK(state->ctx->status)) {
128 DEBUG(9, ("dcerpc_ndr_request_recv returned %s\n",
129 nt_errstr(state->ctx->status)));
130 goto fallback;
133 state->ctx->status =
134 werror_to_ntstatus(state->d.out.result);
135 if (!NT_STATUS_IS_OK(state->ctx->status)) {
136 DEBUG(9, ("dsrgetdcname returned %s\n",
137 nt_errstr(state->ctx->status)));
138 goto fallback;
141 /* Hey, that was easy! */
142 state->info->num_dcs = 1;
143 state->info->dcs = talloc(state->info, struct nbt_dc_name);
144 state->info->dcs[0].name = talloc_steal(state->info,
145 (*state->d.out.info)->dc_unc);
146 if (*state->info->dcs[0].name == '\\') state->info->dcs[0].name++;
147 if (*state->info->dcs[0].name == '\\') state->info->dcs[0].name++;
149 state->info->dcs[0].address = talloc_steal(state->info,
150 (*state->d.out.info)->dc_address);
151 if (*state->info->dcs[0].address == '\\') state->info->dcs[0].address++;
152 if (*state->info->dcs[0].address == '\\') state->info->dcs[0].address++;
154 state->info->dns_name = talloc_steal(state->info,
155 (*state->d.out.info)->domain_name);
157 composite_done(state->ctx);
158 return;
160 fallback:
162 state->g.in.logon_server = talloc_asprintf(
163 state, "\\\\%s",
164 dcerpc_server_name(state->my_domain->netlogon_pipe));
165 state->g.in.domainname = state->info->name;
166 state->g.out.dcname = talloc(state, const char *);
168 req = dcerpc_netr_GetAnyDCName_send(state->my_domain->netlogon_pipe,
169 state, &state->g);
170 if (composite_nomem(req, state->ctx)) return;
172 composite_continue_rpc(state->ctx, req, trusted_dom_info_recv_dcname,
173 state);
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_data,
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 /* Hey, that was easy! */
190 state->info->num_dcs = 1;
191 state->info->dcs = talloc(state->info, struct nbt_dc_name);
192 state->info->dcs[0].name = talloc_steal(state->info,
193 *(state->g.out.dcname));
194 if (*state->info->dcs[0].name == '\\') state->info->dcs[0].name++;
195 if (*state->info->dcs[0].name == '\\') state->info->dcs[0].name++;
197 make_nbt_name(&name, state->info->dcs[0].name, 0x20);
198 ctx = resolve_name_send(lp_resolve_context(state->service->task->lp_ctx), state,
199 &name, state->service->task->event_ctx);
201 composite_continue(state->ctx, ctx, trusted_dom_info_recv_dcaddr,
202 state);
205 static void trusted_dom_info_recv_dcaddr(struct composite_context *ctx)
207 struct trusted_dom_info_state *state =
208 talloc_get_type(ctx->async.private_data,
209 struct trusted_dom_info_state);
211 state->ctx->status = resolve_name_recv(ctx, state->info,
212 &state->info->dcs[0].address);
213 if (!composite_is_ok(state->ctx)) return;
215 composite_done(state->ctx);
218 NTSTATUS wb_trusted_dom_info_recv(struct composite_context *ctx,
219 TALLOC_CTX *mem_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);
229 talloc_free(ctx);
230 return status;
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);