s4:ldap.py Improve testsuite for primaryGroupToken behaviour
[Samba/ekacnet.git] / source4 / winbind / wb_dom_info_trusted.c
blob5730acda40b8805a6bd862cac3fb9458acd54df2
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"
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 state = talloc(result, struct trusted_dom_info_state);
59 if (state == NULL) goto failed;
60 state->ctx = result;
61 result->private_data = state;
63 state->info = talloc_zero(state, struct wb_dom_info);
64 if (state->info == NULL) goto failed;
66 state->service = service;
68 state->info->sid = dom_sid_dup(state->info, sid);
69 if (state->info->sid == NULL) goto failed;
71 state->info->name = talloc_strdup(state->info, domain_name);
72 if (state->info->name == NULL) goto failed;
74 ctx = wb_sid2domain_send(state, service, service->primary_sid);
75 if (ctx == NULL) goto failed;
77 ctx->async.fn = trusted_dom_info_recv_domain;
78 ctx->async.private_data = state;
79 return result;
81 failed:
82 talloc_free(result);
83 return NULL;
86 static void trusted_dom_info_recv_domain(struct composite_context *ctx)
88 struct trusted_dom_info_state *state =
89 talloc_get_type(ctx->async.private_data,
90 struct trusted_dom_info_state);
91 struct rpc_request *req;
93 state->ctx->status = wb_sid2domain_recv(ctx, &state->my_domain);
94 if (!composite_is_ok(state->ctx)) return;
96 state->d.in.server_unc =
97 talloc_asprintf(state, "\\\\%s",
98 dcerpc_server_name(state->my_domain->netlogon_pipe));
99 if (composite_nomem(state->d.in.server_unc,
100 state->ctx)) return;
102 state->d.in.domain_name = state->info->name;
103 state->d.in.domain_guid = NULL;
104 state->d.in.site_guid = NULL;
105 state->d.in.flags = DS_RETURN_DNS_NAME;
106 state->d.out.info = talloc(state, struct netr_DsRGetDCNameInfo *);
107 if (composite_nomem(state->d.out.info, state->ctx)) return;
109 req = dcerpc_netr_DsRGetDCName_send(state->my_domain->netlogon_pipe,
110 state, &state->d);
111 composite_continue_rpc(state->ctx, req, trusted_dom_info_recv_dsr,
112 state);
116 * dcerpc_netr_DsRGetDCName has replied
119 static void trusted_dom_info_recv_dsr(struct rpc_request *req)
121 struct trusted_dom_info_state *state =
122 talloc_get_type(req->async.private_data,
123 struct trusted_dom_info_state);
125 state->ctx->status = dcerpc_ndr_request_recv(req);
126 if (!NT_STATUS_IS_OK(state->ctx->status)) {
127 DEBUG(9, ("dcerpc_ndr_request_recv returned %s\n",
128 nt_errstr(state->ctx->status)));
129 goto fallback;
132 state->ctx->status =
133 werror_to_ntstatus(state->d.out.result);
134 if (!NT_STATUS_IS_OK(state->ctx->status)) {
135 DEBUG(9, ("dsrgetdcname returned %s\n",
136 nt_errstr(state->ctx->status)));
137 goto fallback;
140 /* Hey, that was easy! */
141 state->info->num_dcs = 1;
142 state->info->dcs = talloc(state->info, struct nbt_dc_name);
143 state->info->dcs[0].name = talloc_steal(state->info,
144 (*state->d.out.info)->dc_unc);
145 if (*state->info->dcs[0].name == '\\') state->info->dcs[0].name++;
146 if (*state->info->dcs[0].name == '\\') state->info->dcs[0].name++;
148 state->info->dcs[0].address = talloc_steal(state->info,
149 (*state->d.out.info)->dc_address);
150 if (*state->info->dcs[0].address == '\\') state->info->dcs[0].address++;
151 if (*state->info->dcs[0].address == '\\') state->info->dcs[0].address++;
153 state->info->dns_name = talloc_steal(state->info,
154 (*state->d.out.info)->domain_name);
156 composite_done(state->ctx);
157 return;
159 fallback:
161 state->g.in.logon_server = talloc_asprintf(
162 state, "\\\\%s",
163 dcerpc_server_name(state->my_domain->netlogon_pipe));
164 state->g.in.domainname = state->info->name;
165 state->g.out.dcname = talloc(state, const char *);
167 req = dcerpc_netr_GetAnyDCName_send(state->my_domain->netlogon_pipe,
168 state, &state->g);
169 if (composite_nomem(req, state->ctx)) return;
171 composite_continue_rpc(state->ctx, req, trusted_dom_info_recv_dcname,
172 state);
175 static void trusted_dom_info_recv_dcname(struct rpc_request *req)
177 struct trusted_dom_info_state *state =
178 talloc_get_type(req->async.private_data,
179 struct trusted_dom_info_state);
180 struct composite_context *ctx;
181 struct nbt_name name;
183 state->ctx->status = dcerpc_ndr_request_recv(req);
184 if (!composite_is_ok(state->ctx)) return;
185 state->ctx->status = werror_to_ntstatus(state->g.out.result);
186 if (!composite_is_ok(state->ctx)) return;
188 /* Hey, that was easy! */
189 state->info->num_dcs = 1;
190 state->info->dcs = talloc(state->info, struct nbt_dc_name);
191 state->info->dcs[0].name = talloc_steal(state->info,
192 *(state->g.out.dcname));
193 if (*state->info->dcs[0].name == '\\') state->info->dcs[0].name++;
194 if (*state->info->dcs[0].name == '\\') state->info->dcs[0].name++;
196 make_nbt_name(&name, state->info->dcs[0].name, 0x20);
197 ctx = resolve_name_send(lp_resolve_context(state->service->task->lp_ctx), state,
198 &name, state->service->task->event_ctx);
200 composite_continue(state->ctx, ctx, trusted_dom_info_recv_dcaddr,
201 state);
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->dcs[0].address);
212 if (!composite_is_ok(state->ctx)) return;
214 composite_done(state->ctx);
217 NTSTATUS wb_trusted_dom_info_recv(struct composite_context *ctx,
218 TALLOC_CTX *mem_ctx,
219 struct wb_dom_info **result)
221 NTSTATUS status = composite_wait(ctx);
222 if (NT_STATUS_IS_OK(status)) {
223 struct trusted_dom_info_state *state =
224 talloc_get_type(ctx->private_data,
225 struct trusted_dom_info_state);
226 *result = talloc_steal(mem_ctx, state->info);
228 talloc_free(ctx);
229 return status;
232 NTSTATUS wb_trusted_dom_info(TALLOC_CTX *mem_ctx,
233 struct wbsrv_service *service,
234 const char *domain_name,
235 const struct dom_sid *sid,
236 struct wb_dom_info **result)
238 struct composite_context *ctx =
239 wb_trusted_dom_info_send(mem_ctx, service, domain_name, sid);
240 return wb_trusted_dom_info_recv(ctx, mem_ctx, result);