r25026: Move param/param.h out of includes.h
[bbaumbach/samba-autobuild/.git] / source4 / winbind / wb_dom_info_trusted.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Get a struct wb_dom_info for a trusted domain, relying on "our" DC.
5
6    Copyright (C) Volker Lendecke 2005
7    
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.
12    
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.
17    
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/>.
20 */
21
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"
31
32 struct trusted_dom_info_state {
33         struct composite_context *ctx;
34         struct wbsrv_service *service;
35         struct wbsrv_domain *my_domain;
36
37         struct netr_DsRGetDCName d;
38         struct netr_GetAnyDCName g;
39
40         struct wb_dom_info *info;
41 };
42
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);
47
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)
52 {
53         struct composite_context *result, *ctx;
54         struct trusted_dom_info_state *state;
55
56         result = composite_create(mem_ctx, service->task->event_ctx);
57         if (result == NULL) goto failed;
58
59         state = talloc(result, struct trusted_dom_info_state);
60         if (state == NULL) goto failed;
61         state->ctx = result;
62         result->private_data = state;
63
64         state->info = talloc_zero(state, struct wb_dom_info);
65         if (state->info == NULL) goto failed;
66
67         state->service = service;
68
69         state->info->sid = dom_sid_dup(state->info, sid);
70         if (state->info->sid == NULL) goto failed;
71
72         state->info->name = talloc_strdup(state->info, domain_name);
73         if (state->info->name == NULL) goto failed;
74
75         ctx = wb_sid2domain_send(state, service, service->primary_sid);
76         if (ctx == NULL) goto failed;
77
78         ctx->async.fn = trusted_dom_info_recv_domain;
79         ctx->async.private_data = state;
80         return result;
81
82  failed:
83         talloc_free(result);
84         return NULL;
85 }
86
87 static void trusted_dom_info_recv_domain(struct composite_context *ctx)
88 {
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;
93
94         state->ctx->status = wb_sid2domain_recv(ctx, &state->my_domain);
95         if (!composite_is_ok(state->ctx)) return;
96
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;
102
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
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);
112 }
113
114 /*
115  * dcerpc_netr_DsRGetDCName has replied
116  */
117
118 static void trusted_dom_info_recv_dsr(struct rpc_request *req)
119 {
120         struct trusted_dom_info_state *state =
121                 talloc_get_type(req->async.private_data,
122                                 struct trusted_dom_info_state);
123
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;
129         }
130
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;
137         }
138
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++;
146
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++;
151
152         state->info->dns_name = talloc_steal(state->info,
153                                              state->d.out.info->domain_name);
154
155         composite_done(state->ctx);
156         return;
157
158  fallback:
159
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;
164
165         req = dcerpc_netr_GetAnyDCName_send(state->my_domain->netlogon_pipe,
166                                             state, &state->g);
167         if (composite_nomem(req, state->ctx)) return;
168
169         composite_continue_rpc(state->ctx, req, trusted_dom_info_recv_dcname,
170                                state);
171 }
172
173 static void trusted_dom_info_recv_dcname(struct rpc_request *req)
174 {
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;
180
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;
185
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++;
193         
194         make_nbt_name(&name, state->info->dcs[0].name, 0x20);
195         ctx = resolve_name_send(&name, state->service->task->event_ctx,
196                                 lp_name_resolve_order());
197
198         composite_continue(state->ctx, ctx, trusted_dom_info_recv_dcaddr,
199                            state);
200 }
201
202 static void trusted_dom_info_recv_dcaddr(struct composite_context *ctx)
203 {
204         struct trusted_dom_info_state *state =
205                 talloc_get_type(ctx->async.private_data,
206                                 struct trusted_dom_info_state);
207
208         state->ctx->status = resolve_name_recv(ctx, state->info,
209                                                &state->info->dcs[0].address);
210         if (!composite_is_ok(state->ctx)) return;
211
212         composite_done(state->ctx);
213 }
214
215 NTSTATUS wb_trusted_dom_info_recv(struct composite_context *ctx,
216                                   TALLOC_CTX *mem_ctx,
217                                   struct wb_dom_info **result)
218 {
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);
225         }
226         talloc_free(ctx);
227         return status;
228 }
229
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)
235 {
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);
239 }