7c2ff0232c43d147f86f856cb059ada9db56889f
[kai/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 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 "winbind/wb_server.h"
26 #include "smbd/service_task.h"
27 #include "librpc/gen_ndr/ndr_netlogon.h"
28
29 struct trusted_dom_info_state {
30         struct composite_context *ctx;
31         struct wbsrv_service *service;
32         struct wbsrv_domain *my_domain;
33
34         struct netr_DsRGetDCName d;
35         struct netr_GetAnyDCName g;
36
37         struct wb_dom_info *info;
38 };
39
40 static void trusted_dom_info_recv_domain(struct composite_context *ctx);
41 static void trusted_dom_info_recv_dsr(struct rpc_request *req);
42 static void trusted_dom_info_recv_dcname(struct rpc_request *req);
43 static void trusted_dom_info_recv_dcaddr(struct composite_context *ctx);
44
45 struct composite_context *wb_trusted_dom_info_send(TALLOC_CTX *mem_ctx,
46                                                    struct wbsrv_service *service,
47                                                    const char *domain_name,
48                                                    const struct dom_sid *sid)
49 {
50         struct composite_context *result, *ctx;
51         struct trusted_dom_info_state *state;
52
53         result = talloc(mem_ctx, struct composite_context);
54         if (result == NULL) goto failed;
55         result->state = COMPOSITE_STATE_IN_PROGRESS;
56         result->async.fn = NULL;
57         result->event_ctx = service->task->event_ctx;
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                                 state->my_domain->info->dc_name);
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 = 0x40000000;
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,
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
141         state->info->dc_name = talloc_steal(state->info,
142                                             state->d.out.info->dc_unc);
143         if (*state->info->dc_name == '\\') state->info->dc_name++;
144         if (*state->info->dc_name == '\\') state->info->dc_name++;
145
146         state->info->dc_address = talloc_steal(state->info,
147                                                state->d.out.info->dc_address);
148         if (*state->info->dc_address == '\\') state->info->dc_address++;
149         if (*state->info->dc_address == '\\') state->info->dc_address++;
150
151         state->info->dns_name = talloc_steal(state->info,
152                                              state->d.out.info->domain_name);
153
154         composite_done(state->ctx);
155         return;
156
157  fallback:
158
159         state->g.in.logon_server = talloc_asprintf(
160                 state, "\\\\%s",
161                 dcerpc_server_name(state->my_domain->netlogon_pipe));
162         state->g.in.domainname = state->info->name;
163
164         req = dcerpc_netr_GetAnyDCName_send(state->my_domain->netlogon_pipe,
165                                             state, &state->g);
166         if (composite_nomem(req, state->ctx)) return;
167
168         composite_continue_rpc(state->ctx, req, trusted_dom_info_recv_dcname,
169                                state);
170 }
171
172 static void trusted_dom_info_recv_dcname(struct rpc_request *req)
173 {
174         struct trusted_dom_info_state *state =
175                 talloc_get_type(req->async.private,
176                                 struct trusted_dom_info_state);
177         struct composite_context *ctx;
178         struct nbt_name name;
179
180         state->ctx->status = dcerpc_ndr_request_recv(req);
181         if (!composite_is_ok(state->ctx)) return;
182         state->ctx->status = werror_to_ntstatus(state->g.out.result);
183         if (!composite_is_ok(state->ctx)) return;
184
185         state->info->dc_name = talloc_steal(state->info,
186                                             state->g.out.dcname);
187
188         if (*state->info->dc_name == '\\') state->info->dc_name++;
189         if (*state->info->dc_name == '\\') state->info->dc_name++;
190         
191         make_nbt_name(&name, state->info->dc_name, 0x20);
192         ctx = resolve_name_send(&name, state->service->task->event_ctx,
193                                 lp_name_resolve_order());
194
195         composite_continue(state->ctx, ctx, trusted_dom_info_recv_dcaddr,
196                            state);
197 }
198
199 static void trusted_dom_info_recv_dcaddr(struct composite_context *ctx)
200 {
201         struct trusted_dom_info_state *state =
202                 talloc_get_type(ctx->async.private_data,
203                                 struct trusted_dom_info_state);
204
205         state->ctx->status = resolve_name_recv(ctx, state->info,
206                                                &state->info->dc_address);
207         if (!composite_is_ok(state->ctx)) return;
208
209         composite_done(state->ctx);
210 }
211
212 NTSTATUS wb_trusted_dom_info_recv(struct composite_context *ctx,
213                                   TALLOC_CTX *mem_ctx,
214                                   struct wb_dom_info **result)
215 {
216         NTSTATUS status = composite_wait(ctx);
217         if (NT_STATUS_IS_OK(status)) {
218                 struct trusted_dom_info_state *state =
219                         talloc_get_type(ctx->private_data,
220                                         struct trusted_dom_info_state);
221                 *result = talloc_steal(mem_ctx, state->info);
222         }
223         talloc_free(ctx);
224         return status;
225 }
226
227 NTSTATUS wb_trusted_dom_info(TALLOC_CTX *mem_ctx,
228                              struct wbsrv_service *service,
229                              const char *domain_name,
230                              const struct dom_sid *sid,
231                              struct wb_dom_info **result)
232 {
233         struct composite_context *ctx =
234                 wb_trusted_dom_info_send(mem_ctx, service, domain_name, sid);
235         return wb_trusted_dom_info_recv(ctx, mem_ctx, result);
236 }