Update account expiration to use new samdb_result_account_expires() function.
[Samba/kamenim.git] / source4 / winbind / wb_dom_info_trusted.c
blob46d3bf37f65d8c37ccc31cfd9f8ef10f4315c618
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;
108 req = dcerpc_netr_DsRGetDCName_send(state->my_domain->netlogon_pipe,
109 state, &state->d);
110 composite_continue_rpc(state->ctx, req, trusted_dom_info_recv_dsr,
111 state);
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_data,
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)));
128 goto fallback;
131 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)));
136 goto fallback;
139 /* Hey, that was easy! */
140 state->info->num_dcs = 1;
141 state->info->dcs = talloc(state->info, struct nbt_dc_name);
142 state->info->dcs[0].name = talloc_steal(state->info,
143 state->d.out.info->dc_unc);
144 if (*state->info->dcs[0].name == '\\') state->info->dcs[0].name++;
145 if (*state->info->dcs[0].name == '\\') state->info->dcs[0].name++;
147 state->info->dcs[0].address = talloc_steal(state->info,
148 state->d.out.info->dc_address);
149 if (*state->info->dcs[0].address == '\\') state->info->dcs[0].address++;
150 if (*state->info->dcs[0].address == '\\') state->info->dcs[0].address++;
152 state->info->dns_name = talloc_steal(state->info,
153 state->d.out.info->domain_name);
155 composite_done(state->ctx);
156 return;
158 fallback:
160 state->g.in.logon_server = talloc_asprintf(
161 state, "\\\\%s",
162 dcerpc_server_name(state->my_domain->netlogon_pipe));
163 state->g.in.domainname = state->info->name;
165 req = dcerpc_netr_GetAnyDCName_send(state->my_domain->netlogon_pipe,
166 state, &state->g);
167 if (composite_nomem(req, state->ctx)) return;
169 composite_continue_rpc(state->ctx, req, trusted_dom_info_recv_dcname,
170 state);
173 static void trusted_dom_info_recv_dcname(struct rpc_request *req)
175 struct trusted_dom_info_state *state =
176 talloc_get_type(req->async.private_data,
177 struct trusted_dom_info_state);
178 struct composite_context *ctx;
179 struct nbt_name name;
181 state->ctx->status = dcerpc_ndr_request_recv(req);
182 if (!composite_is_ok(state->ctx)) return;
183 state->ctx->status = werror_to_ntstatus(state->g.out.result);
184 if (!composite_is_ok(state->ctx)) return;
186 /* Hey, that was easy! */
187 state->info->num_dcs = 1;
188 state->info->dcs = talloc(state->info, struct nbt_dc_name);
189 state->info->dcs[0].name = talloc_steal(state->info,
190 state->g.out.dcname);
191 if (*state->info->dcs[0].name == '\\') state->info->dcs[0].name++;
192 if (*state->info->dcs[0].name == '\\') state->info->dcs[0].name++;
194 make_nbt_name(&name, state->info->dcs[0].name, 0x20);
195 ctx = resolve_name_send(lp_resolve_context(state->service->task->lp_ctx),
196 &name, state->service->task->event_ctx);
198 composite_continue(state->ctx, ctx, trusted_dom_info_recv_dcaddr,
199 state);
202 static void trusted_dom_info_recv_dcaddr(struct composite_context *ctx)
204 struct trusted_dom_info_state *state =
205 talloc_get_type(ctx->async.private_data,
206 struct trusted_dom_info_state);
208 state->ctx->status = resolve_name_recv(ctx, state->info,
209 &state->info->dcs[0].address);
210 if (!composite_is_ok(state->ctx)) return;
212 composite_done(state->ctx);
215 NTSTATUS wb_trusted_dom_info_recv(struct composite_context *ctx,
216 TALLOC_CTX *mem_ctx,
217 struct wb_dom_info **result)
219 NTSTATUS status = composite_wait(ctx);
220 if (NT_STATUS_IS_OK(status)) {
221 struct trusted_dom_info_state *state =
222 talloc_get_type(ctx->private_data,
223 struct trusted_dom_info_state);
224 *result = talloc_steal(mem_ctx, state->info);
226 talloc_free(ctx);
227 return status;
230 NTSTATUS wb_trusted_dom_info(TALLOC_CTX *mem_ctx,
231 struct wbsrv_service *service,
232 const char *domain_name,
233 const struct dom_sid *sid,
234 struct wb_dom_info **result)
236 struct composite_context *ctx =
237 wb_trusted_dom_info_send(mem_ctx, service, domain_name, sid);
238 return wb_trusted_dom_info_recv(ctx, mem_ctx, result);