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