fix the build (temporary)
[samba-svnmirror.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/resolve/resolve.h"
26 #include "libcli/security/security.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 = composite_create(mem_ctx, service->task->event_ctx);
56         if (result == NULL) goto failed;
57
58 composite_error(result, NT_STATUS_FOOBAR);
59 return result;
60 failed:
61 return NULL;
62 }
63 #if 0
64         state = talloc(result, struct trusted_dom_info_state);
65         if (state == NULL) goto failed;
66         state->ctx = result;
67         result->private_data = state;
68
69         state->info = talloc_zero(state, struct wb_dom_info);
70         if (state->info == NULL) goto failed;
71
72         state->service = service;
73
74         state->info->sid = dom_sid_dup(state->info, sid);
75         if (state->info->sid == NULL) goto failed;
76
77         state->info->name = talloc_strdup(state->info, domain_name);
78         if (state->info->name == NULL) goto failed;
79
80         ctx = wb_sid2domain_send(state, service, service->primary_sid);
81         if (ctx == NULL) goto failed;
82
83         ctx->async.fn = trusted_dom_info_recv_domain;
84         ctx->async.private_data = state;
85         return result;
86
87  failed:
88         talloc_free(result);
89         return NULL;
90 }
91
92 static void trusted_dom_info_recv_domain(struct composite_context *ctx)
93 {
94         struct trusted_dom_info_state *state =
95                 talloc_get_type(ctx->async.private_data,
96                                 struct trusted_dom_info_state);
97         struct rpc_request *req;
98
99         state->ctx->status = wb_sid2domain_recv(ctx, &state->my_domain);
100         if (!composite_is_ok(state->ctx)) return;
101
102         state->d.in.server_unc =
103                 talloc_asprintf(state, "\\\\%s",
104                                 state->my_domain->info->dc_name);
105         if (composite_nomem(state->d.in.server_unc,
106                             state->ctx)) return;
107
108         state->d.in.domain_name = state->info->name;
109         state->d.in.domain_guid = NULL;
110         state->d.in.site_guid = NULL;
111         state->d.in.flags = 0x40000000;
112
113         req = dcerpc_netr_DsRGetDCName_send(state->my_domain->netlogon_pipe,
114                                             state, &state->d);
115         composite_continue_rpc(state->ctx, req, trusted_dom_info_recv_dsr,
116                                state);
117 }
118
119 /*
120  * dcerpc_netr_DsRGetDCName has replied
121  */
122
123 static void trusted_dom_info_recv_dsr(struct rpc_request *req)
124 {
125         struct trusted_dom_info_state *state =
126                 talloc_get_type(req->async.private_data,
127                                 struct trusted_dom_info_state);
128
129         state->ctx->status = dcerpc_ndr_request_recv(req);
130         if (!NT_STATUS_IS_OK(state->ctx->status)) {
131                 DEBUG(9, ("dcerpc_ndr_request_recv returned %s\n",
132                           nt_errstr(state->ctx->status)));
133                 goto fallback;
134         }
135
136         state->ctx->status =
137                 werror_to_ntstatus(state->d.out.result);
138         if (!NT_STATUS_IS_OK(state->ctx->status)) {
139                 DEBUG(9, ("dsrgetdcname returned %s\n",
140                           nt_errstr(state->ctx->status)));
141                 goto fallback;
142         }
143
144         /* Hey, that was easy! */
145
146         state->info->dc_name = talloc_steal(state->info,
147                                             state->d.out.info->dc_unc);
148         if (*state->info->dc_name == '\\') state->info->dc_name++;
149         if (*state->info->dc_name == '\\') state->info->dc_name++;
150
151         state->info->dc_address = talloc_steal(state->info,
152                                                state->d.out.info->dc_address);
153         if (*state->info->dc_address == '\\') state->info->dc_address++;
154         if (*state->info->dc_address == '\\') state->info->dc_address++;
155
156         state->info->dns_name = talloc_steal(state->info,
157                                              state->d.out.info->domain_name);
158
159         composite_done(state->ctx);
160         return;
161
162  fallback:
163
164         state->g.in.logon_server = talloc_asprintf(
165                 state, "\\\\%s",
166                 dcerpc_server_name(state->my_domain->netlogon_pipe));
167         state->g.in.domainname = state->info->name;
168
169         req = dcerpc_netr_GetAnyDCName_send(state->my_domain->netlogon_pipe,
170                                             state, &state->g);
171         if (composite_nomem(req, state->ctx)) return;
172
173         composite_continue_rpc(state->ctx, req, trusted_dom_info_recv_dcname,
174                                state);
175 }
176
177 static void trusted_dom_info_recv_dcname(struct rpc_request *req)
178 {
179         struct trusted_dom_info_state *state =
180                 talloc_get_type(req->async.private_data,
181                                 struct trusted_dom_info_state);
182         struct composite_context *ctx;
183         struct nbt_name name;
184
185         state->ctx->status = dcerpc_ndr_request_recv(req);
186         if (!composite_is_ok(state->ctx)) return;
187         state->ctx->status = werror_to_ntstatus(state->g.out.result);
188         if (!composite_is_ok(state->ctx)) return;
189
190         state->info->dc_name = talloc_steal(state->info,
191                                             state->g.out.dcname);
192
193         if (*state->info->dc_name == '\\') state->info->dc_name++;
194         if (*state->info->dc_name == '\\') state->info->dc_name++;
195         
196         make_nbt_name(&name, state->info->dc_name, 0x20);
197         ctx = resolve_name_send(&name, state->service->task->event_ctx,
198                                 lp_name_resolve_order());
199
200         composite_continue(state->ctx, ctx, trusted_dom_info_recv_dcaddr,
201                            state);
202 }
203
204 static void trusted_dom_info_recv_dcaddr(struct composite_context *ctx)
205 {
206         struct trusted_dom_info_state *state =
207                 talloc_get_type(ctx->async.private_data,
208                                 struct trusted_dom_info_state);
209
210         state->ctx->status = resolve_name_recv(ctx, state->info,
211                                                &state->info->dc_address);
212         if (!composite_is_ok(state->ctx)) return;
213
214         composite_done(state->ctx);
215 }
216 #endif
217
218 NTSTATUS wb_trusted_dom_info_recv(struct composite_context *ctx,
219                                   TALLOC_CTX *mem_ctx,
220                                   struct wb_dom_info **result)
221 {
222         NTSTATUS status = composite_wait(ctx);
223         if (NT_STATUS_IS_OK(status)) {
224                 struct trusted_dom_info_state *state =
225                         talloc_get_type(ctx->private_data,
226                                         struct trusted_dom_info_state);
227                 *result = talloc_steal(mem_ctx, state->info);
228         }
229         talloc_free(ctx);
230         return status;
231 }
232
233 NTSTATUS wb_trusted_dom_info(TALLOC_CTX *mem_ctx,
234                              struct wbsrv_service *service,
235                              const char *domain_name,
236                              const struct dom_sid *sid,
237                              struct wb_dom_info **result)
238 {
239         struct composite_context *ctx =
240                 wb_trusted_dom_info_send(mem_ctx, service, domain_name, sid);
241         return wb_trusted_dom_info_recv(ctx, mem_ctx, result);
242 }