r12608: Remove some unused #include lines.
[abartlet/samba.git/.git] / source4 / winbind / wb_cmd_list_trustdom.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Command backend for wbinfo -m
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_lsa.h"
28
29 /* List trusted domains. To avoid the trouble with having to wait for other
30  * conflicting requests waiting for the lsa pipe we're opening our own lsa
31  * pipe here. */
32
33 struct cmd_list_trustdom_state {
34         struct composite_context *ctx;
35         struct dcerpc_pipe *lsa_pipe;
36         struct policy_handle *lsa_policy;
37         int num_domains;
38         struct wb_dom_info **domains;
39
40         uint32_t resume_handle;
41         struct lsa_DomainList domainlist;
42         struct lsa_EnumTrustDom r;
43 };
44
45 static void cmd_list_trustdoms_recv_domain(struct composite_context *ctx);
46 static void cmd_list_trustdoms_recv_lsa(struct composite_context *ctx);
47 static void cmd_list_trustdoms_recv_doms(struct rpc_request *req);
48
49 struct composite_context *wb_cmd_list_trustdoms_send(TALLOC_CTX *mem_ctx,
50                                                      struct wbsrv_service *service)
51 {
52         struct composite_context *result, *ctx;
53         struct cmd_list_trustdom_state *state;
54
55         result = talloc_zero(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 cmd_list_trustdom_state);
62         if (state == NULL) goto failed;
63         state->ctx = result;
64         result->private_data = state;
65
66         ctx = wb_sid2domain_send(state, service, service->primary_sid);
67         if (ctx == NULL) goto failed;
68         ctx->async.fn = cmd_list_trustdoms_recv_domain;
69         ctx->async.private_data = state;
70         return result;
71
72  failed:
73         talloc_free(result);
74         return NULL;
75 }
76
77 static void cmd_list_trustdoms_recv_domain(struct composite_context *ctx)
78 {
79         struct cmd_list_trustdom_state *state =
80                 talloc_get_type(ctx->async.private_data,
81                                 struct cmd_list_trustdom_state);
82         struct wbsrv_domain *domain;
83         struct smbcli_tree *tree;
84
85         state->ctx->status = wb_sid2domain_recv(ctx, &domain);
86         if (!composite_is_ok(state->ctx)) return;
87
88         tree = dcerpc_smb_tree(domain->lsa_pipe->conn);
89         if (composite_nomem(tree, state->ctx)) return;
90
91         ctx = wb_init_lsa_send(state, tree, domain->lsa_auth_type,
92                                domain->schannel_creds);
93         composite_continue(state->ctx, ctx, cmd_list_trustdoms_recv_lsa,
94                            state);
95 }
96
97 static void cmd_list_trustdoms_recv_lsa(struct composite_context *ctx)
98 {
99         struct cmd_list_trustdom_state *state =
100                 talloc_get_type(ctx->async.private_data,
101                                 struct cmd_list_trustdom_state);
102         struct rpc_request *req;
103
104         state->ctx->status = wb_init_lsa_recv(ctx, state,
105                                               &state->lsa_pipe,
106                                               &state->lsa_policy);
107         if (!composite_is_ok(state->ctx)) return;
108
109         state->num_domains = 0;
110         state->domains = NULL;
111
112         state->domainlist.count = 0;
113         state->domainlist.domains = NULL;
114
115         state->resume_handle = 0;
116         state->r.in.handle = state->lsa_policy;
117         state->r.in.resume_handle = &state->resume_handle;
118         state->r.in.max_size = 1000;
119         state->r.out.resume_handle = &state->resume_handle;
120         state->r.out.domains = &state->domainlist;
121
122         req = dcerpc_lsa_EnumTrustDom_send(state->lsa_pipe, state, &state->r);
123         composite_continue_rpc(state->ctx, req, cmd_list_trustdoms_recv_doms,
124                                state);
125 }
126
127 static void cmd_list_trustdoms_recv_doms(struct rpc_request *req)
128 {
129         struct cmd_list_trustdom_state *state =
130                 talloc_get_type(req->async.private,
131                                 struct cmd_list_trustdom_state);
132         int i, old_num_domains;
133
134         state->ctx->status = dcerpc_ndr_request_recv(req);
135         if (!composite_is_ok(state->ctx)) return;
136         state->ctx->status = state->r.out.result;
137
138         if (!NT_STATUS_IS_OK(state->ctx->status) &&
139             !NT_STATUS_EQUAL(state->ctx->status, NT_STATUS_NO_MORE_ENTRIES) &&
140             !NT_STATUS_EQUAL(state->ctx->status, STATUS_MORE_ENTRIES)) {
141                 composite_error(state->ctx, state->ctx->status);
142                 return;
143         }
144
145         old_num_domains = state->num_domains;
146
147         state->num_domains += state->r.out.domains->count;
148         state->domains = talloc_realloc(state, state->domains,
149                                         struct wb_dom_info *,
150                                         state->num_domains);
151         if (composite_nomem(state->domains, state->ctx)) return;
152
153         for (i=0; i<state->r.out.domains->count; i++) {
154                 int j = i+old_num_domains;
155                 state->domains[j] = talloc(state->domains,
156                                            struct wb_dom_info);
157                 if (composite_nomem(state->domains[i], state->ctx)) return;
158                 state->domains[j]->name = talloc_steal(
159                         state->domains[j],
160                         state->r.out.domains->domains[i].name.string);
161                 state->domains[j]->sid = talloc_steal(
162                         state->domains[j],
163                         state->r.out.domains->domains[i].sid);
164         }
165
166         if (NT_STATUS_IS_OK(state->ctx->status)) {
167                 composite_done(state->ctx);
168                 return;
169         }
170
171         state->domainlist.count = 0;
172         state->domainlist.domains = NULL;
173         state->r.in.handle = state->lsa_policy;
174         state->r.in.resume_handle = &state->resume_handle;
175         state->r.in.max_size = 1000;
176         state->r.out.resume_handle = &state->resume_handle;
177         state->r.out.domains = &state->domainlist;
178         
179         req = dcerpc_lsa_EnumTrustDom_send(state->lsa_pipe, state, &state->r);
180         composite_continue_rpc(state->ctx, req, cmd_list_trustdoms_recv_doms,
181                                state);
182 }
183
184 NTSTATUS wb_cmd_list_trustdoms_recv(struct composite_context *ctx,
185                                     TALLOC_CTX *mem_ctx,
186                                     int *num_domains,
187                                     struct wb_dom_info ***domains)
188 {
189         NTSTATUS status = composite_wait(ctx);
190         if (NT_STATUS_IS_OK(status)) {
191                 struct cmd_list_trustdom_state *state =
192                         talloc_get_type(ctx->private_data,
193                                         struct cmd_list_trustdom_state);
194                 *num_domains = state->num_domains;
195                 *domains = talloc_steal(mem_ctx, state->domains);
196         }
197         talloc_free(ctx);
198         return status;
199 }