r14470: Remove some unnecessary headers.
[samba.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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "libcli/composite/composite.h"
25 #include "libcli/resolve/resolve.h"
26 #include "libcli/security/proto.h"
27 #include "winbind/wb_server.h"
28 #include "smbd/service_task.h"
29 #include "librpc/gen_ndr/ndr_netlogon_c.h"
30
31 struct trusted_dom_info_state {
32         struct composite_context *ctx;
33         struct wbsrv_service *service;
34         struct wbsrv_domain *my_domain;
35
36         struct netr_DsRGetDCName d;
37         struct netr_GetAnyDCName g;
38
39         struct wb_dom_info *info;
40 };
41
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);
46
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)
51 {
52         struct composite_context *result, *ctx;
53         struct trusted_dom_info_state *state;
54
55         result = talloc(mem_ctx, struct composite_context);
56         if (result == NULL) goto failed;
57         result->state = COMPOSITE_STATE_IN_PROGRESS;
58         result->async.fn = NULL;
59         result->event_ctx = service->task->event_ctx;
60
61         state = talloc(result, struct trusted_dom_info_state);
62         if (state == NULL) goto failed;
63         state->ctx = result;
64         result->private_data = state;
65
66         state->info = talloc_zero(state, struct wb_dom_info);
67         if (state->info == NULL) goto failed;
68
69         state->service = service;
70
71         state->info->sid = dom_sid_dup(state->info, sid);
72         if (state->info->sid == NULL) goto failed;
73
74         state->info->name = talloc_strdup(state->info, domain_name);
75         if (state->info->name == NULL) goto failed;
76
77         ctx = wb_sid2domain_send(state, service, service->primary_sid);
78         if (ctx == NULL) goto failed;
79
80         ctx->async.fn = trusted_dom_info_recv_domain;
81         ctx->async.private_data = state;
82         return result;
83
84  failed:
85         talloc_free(result);
86         return NULL;
87 }
88
89 static void trusted_dom_info_recv_domain(struct composite_context *ctx)
90 {
91         struct trusted_dom_info_state *state =
92                 talloc_get_type(ctx->async.private_data,
93                                 struct trusted_dom_info_state);
94         struct rpc_request *req;
95
96         state->ctx->status = wb_sid2domain_recv(ctx, &state->my_domain);
97         if (!composite_is_ok(state->ctx)) return;
98
99         state->d.in.server_unc =
100                 talloc_asprintf(state, "\\\\%s",
101                                 state->my_domain->info->dc_name);
102         if (composite_nomem(state->d.in.server_unc,
103                             state->ctx)) return;
104
105         state->d.in.domain_name = state->info->name;
106         state->d.in.domain_guid = NULL;
107         state->d.in.site_guid = NULL;
108         state->d.in.flags = 0x40000000;
109
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);
114 }
115
116 /*
117  * dcerpc_netr_DsRGetDCName has replied
118  */
119
120 static void trusted_dom_info_recv_dsr(struct rpc_request *req)
121 {
122         struct trusted_dom_info_state *state =
123                 talloc_get_type(req->async.private,
124                                 struct trusted_dom_info_state);
125
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;
131         }
132
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;
139         }
140
141         /* Hey, that was easy! */
142
143         state->info->dc_name = talloc_steal(state->info,
144                                             state->d.out.info->dc_unc);
145         if (*state->info->dc_name == '\\') state->info->dc_name++;
146         if (*state->info->dc_name == '\\') state->info->dc_name++;
147
148         state->info->dc_address = talloc_steal(state->info,
149                                                state->d.out.info->dc_address);
150         if (*state->info->dc_address == '\\') state->info->dc_address++;
151         if (*state->info->dc_address == '\\') state->info->dc_address++;
152
153         state->info->dns_name = talloc_steal(state->info,
154                                              state->d.out.info->domain_name);
155
156         composite_done(state->ctx);
157         return;
158
159  fallback:
160
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
166         req = dcerpc_netr_GetAnyDCName_send(state->my_domain->netlogon_pipe,
167                                             state, &state->g);
168         if (composite_nomem(req, state->ctx)) return;
169
170         composite_continue_rpc(state->ctx, req, trusted_dom_info_recv_dcname,
171                                state);
172 }
173
174 static void trusted_dom_info_recv_dcname(struct rpc_request *req)
175 {
176         struct trusted_dom_info_state *state =
177                 talloc_get_type(req->async.private,
178                                 struct trusted_dom_info_state);
179         struct composite_context *ctx;
180         struct nbt_name name;
181
182         state->ctx->status = dcerpc_ndr_request_recv(req);
183         if (!composite_is_ok(state->ctx)) return;
184         state->ctx->status = werror_to_ntstatus(state->g.out.result);
185         if (!composite_is_ok(state->ctx)) return;
186
187         state->info->dc_name = talloc_steal(state->info,
188                                             state->g.out.dcname);
189
190         if (*state->info->dc_name == '\\') state->info->dc_name++;
191         if (*state->info->dc_name == '\\') state->info->dc_name++;
192         
193         make_nbt_name(&name, state->info->dc_name, 0x20);
194         ctx = resolve_name_send(&name, state->service->task->event_ctx,
195                                 lp_name_resolve_order());
196
197         composite_continue(state->ctx, ctx, trusted_dom_info_recv_dcaddr,
198                            state);
199 }
200
201 static void trusted_dom_info_recv_dcaddr(struct composite_context *ctx)
202 {
203         struct trusted_dom_info_state *state =
204                 talloc_get_type(ctx->async.private_data,
205                                 struct trusted_dom_info_state);
206
207         state->ctx->status = resolve_name_recv(ctx, state->info,
208                                                &state->info->dc_address);
209         if (!composite_is_ok(state->ctx)) return;
210
211         composite_done(state->ctx);
212 }
213
214 NTSTATUS wb_trusted_dom_info_recv(struct composite_context *ctx,
215                                   TALLOC_CTX *mem_ctx,
216                                   struct wb_dom_info **result)
217 {
218         NTSTATUS status = composite_wait(ctx);
219         if (NT_STATUS_IS_OK(status)) {
220                 struct trusted_dom_info_state *state =
221                         talloc_get_type(ctx->private_data,
222                                         struct trusted_dom_info_state);
223                 *result = talloc_steal(mem_ctx, state->info);
224         }
225         talloc_free(ctx);
226         return status;
227 }
228
229 NTSTATUS wb_trusted_dom_info(TALLOC_CTX *mem_ctx,
230                              struct wbsrv_service *service,
231                              const char *domain_name,
232                              const struct dom_sid *sid,
233                              struct wb_dom_info **result)
234 {
235         struct composite_context *ctx =
236                 wb_trusted_dom_info_send(mem_ctx, service, domain_name, sid);
237         return wb_trusted_dom_info_recv(ctx, mem_ctx, result);
238 }