r11528: Separate finding dcs from initializing a domain. Makes it easier to possibly
[jelmer/samba4-debian.git] / source / 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/smb_composite/smb_composite.h"
26 #include "winbind/wb_async_helpers.h"
27 #include "winbind/wb_server.h"
28 #include "smbd/service_stream.h"
29 #include "smbd/service_task.h"
30 #include "librpc/gen_ndr/nbt.h"
31 #include "librpc/gen_ndr/ndr_netlogon.h"
32
33 struct trusted_dom_info_state {
34         struct composite_context *ctx;
35         struct wbsrv_service *service;
36         struct wbsrv_domain *my_domain;
37
38         struct netr_DsRGetDCName d;
39         struct netr_GetAnyDCName g;
40
41         struct wb_dom_info *info;
42 };
43
44 static void trusted_dom_info_recv_domain(struct composite_context *ctx);
45 static void trusted_dom_info_recv_dsr(struct rpc_request *req);
46 static void trusted_dom_info_recv_dcname(struct rpc_request *req);
47 static void trusted_dom_info_recv_dcaddr(struct composite_context *ctx);
48
49 struct composite_context *wb_trusted_dom_info_send(TALLOC_CTX *mem_ctx,
50                                                    struct wbsrv_service *service,
51                                                    const char *domain_name,
52                                                    const struct dom_sid *sid)
53 {
54         struct composite_context *result, *ctx;
55         struct trusted_dom_info_state *state;
56
57         result = talloc(mem_ctx, struct composite_context);
58         if (result == NULL) goto failed;
59         result->state = COMPOSITE_STATE_IN_PROGRESS;
60         result->async.fn = NULL;
61         result->event_ctx = service->task->event_ctx;
62
63         state = talloc(result, struct trusted_dom_info_state);
64         if (state == NULL) goto failed;
65         state->ctx = result;
66         result->private_data = state;
67
68         state->info = talloc_zero(state, struct wb_dom_info);
69         if (state->info == NULL) goto failed;
70
71         state->service = service;
72
73         state->info->sid = dom_sid_dup(state->info, sid);
74         if (state->info->sid == NULL) goto failed;
75
76         state->info->name = talloc_strdup(state->info, domain_name);
77         if (state->info->name == NULL) goto failed;
78
79         ctx = wb_sid2domain_send(state, service, service->primary_sid);
80         if (ctx == NULL) goto failed;
81
82         ctx->async.fn = trusted_dom_info_recv_domain;
83         ctx->async.private_data = state;
84         return result;
85
86  failed:
87         talloc_free(result);
88         return NULL;
89 }
90
91 static void trusted_dom_info_recv_domain(struct composite_context *ctx)
92 {
93         struct trusted_dom_info_state *state =
94                 talloc_get_type(ctx->async.private_data,
95                                 struct trusted_dom_info_state);
96         struct rpc_request *req;
97
98         state->ctx->status = wb_sid2domain_recv(ctx, &state->my_domain);
99         if (!composite_is_ok(state->ctx)) return;
100
101         state->d.in.server_unc =
102                 talloc_asprintf(state, "\\\\%s",
103                                 state->my_domain->info->dc_name);
104         if (composite_nomem(state->d.in.server_unc,
105                             state->ctx)) return;
106
107         state->d.in.domain_name = state->info->name;
108         state->d.in.domain_guid = NULL;
109         state->d.in.site_guid = NULL;
110         state->d.in.flags = 0x40000000;
111
112         req = dcerpc_netr_DsRGetDCName_send(state->my_domain->netlogon_pipe,
113                                             state, &state->d);
114         composite_continue_rpc(state->ctx, req, trusted_dom_info_recv_dsr,
115                                state);
116 }
117
118 /*
119  * dcerpc_netr_DsRGetDCName has replied
120  */
121
122 static void trusted_dom_info_recv_dsr(struct rpc_request *req)
123 {
124         struct trusted_dom_info_state *state =
125                 talloc_get_type(req->async.private,
126                                 struct trusted_dom_info_state);
127
128         state->ctx->status = dcerpc_ndr_request_recv(req);
129         if (!NT_STATUS_IS_OK(state->ctx->status)) {
130                 DEBUG(9, ("dcerpc_ndr_request_recv returned %s\n",
131                           nt_errstr(state->ctx->status)));
132                 goto fallback;
133         }
134
135         state->ctx->status =
136                 werror_to_ntstatus(state->d.out.result);
137         if (!NT_STATUS_IS_OK(state->ctx->status)) {
138                 DEBUG(9, ("dsrgetdcname returned %s\n",
139                           nt_errstr(state->ctx->status)));
140                 goto fallback;
141         }
142
143         /* Hey, that was easy! */
144
145         state->info->dc_name = talloc_steal(state->info,
146                                             state->d.out.info->dc_unc);
147         if (*state->info->dc_name == '\\') state->info->dc_name++;
148         if (*state->info->dc_name == '\\') state->info->dc_name++;
149
150         state->info->dc_address = talloc_steal(state->info,
151                                                state->d.out.info->dc_address);
152         if (*state->info->dc_address == '\\') state->info->dc_address++;
153         if (*state->info->dc_address == '\\') state->info->dc_address++;
154
155         state->info->dns_name = talloc_steal(state->info,
156                                              state->d.out.info->domain_name);
157
158         composite_done(state->ctx);
159         return;
160
161  fallback:
162
163         state->g.in.logon_server = talloc_asprintf(
164                 state, "\\\\%s",
165                 dcerpc_server_name(state->my_domain->netlogon_pipe));
166         state->g.in.domainname = state->info->name;
167
168         req = dcerpc_netr_GetAnyDCName_send(state->my_domain->netlogon_pipe,
169                                             state, &state->g);
170         if (composite_nomem(req, state->ctx)) return;
171
172         composite_continue_rpc(state->ctx, req, trusted_dom_info_recv_dcname,
173                                state);
174 }
175
176 static void trusted_dom_info_recv_dcname(struct rpc_request *req)
177 {
178         struct trusted_dom_info_state *state =
179                 talloc_get_type(req->async.private,
180                                 struct trusted_dom_info_state);
181         struct composite_context *ctx;
182         struct nbt_name name;
183
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;
188
189         state->info->dc_name = talloc_steal(state->info,
190                                             state->g.out.dcname);
191
192         if (*state->info->dc_name == '\\') state->info->dc_name++;
193         if (*state->info->dc_name == '\\') state->info->dc_name++;
194         
195         make_nbt_name(&name, state->info->dc_name, 0x20);
196         ctx = resolve_name_send(&name, state->service->task->event_ctx,
197                                 lp_name_resolve_order());
198
199         composite_continue(state->ctx, ctx, trusted_dom_info_recv_dcaddr,
200                            state);
201 }
202
203 static void trusted_dom_info_recv_dcaddr(struct composite_context *ctx)
204 {
205         struct trusted_dom_info_state *state =
206                 talloc_get_type(ctx->async.private_data,
207                                 struct trusted_dom_info_state);
208
209         state->ctx->status = resolve_name_recv(ctx, state->info,
210                                                &state->info->dc_address);
211         if (!composite_is_ok(state->ctx)) return;
212
213         composite_done(state->ctx);
214 }
215
216 NTSTATUS wb_trusted_dom_info_recv(struct composite_context *ctx,
217                                   TALLOC_CTX *mem_ctx,
218                                   struct wb_dom_info **result)
219 {
220         NTSTATUS status = composite_wait(ctx);
221         if (NT_STATUS_IS_OK(status)) {
222                 struct trusted_dom_info_state *state =
223                         talloc_get_type(ctx->private_data,
224                                         struct trusted_dom_info_state);
225                 *result = talloc_steal(mem_ctx, state->info);
226         }
227         talloc_free(ctx);
228         return status;
229 }
230
231 NTSTATUS wb_trusted_dom_info(TALLOC_CTX *mem_ctx,
232                              struct wbsrv_service *service,
233                              const char *domain_name,
234                              const struct dom_sid *sid,
235                              struct wb_dom_info **result)
236 {
237         struct composite_context *ctx =
238                 wb_trusted_dom_info_send(mem_ctx, service, domain_name, sid);
239         return wb_trusted_dom_info_recv(ctx, mem_ctx, result);
240 }