Merge 2610c05b5b95cc7036b3d6dfb894c6cfbdb68483 as Samba-4.0alpha16
[kamenim/samba-autobuild/.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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "libcli/composite/composite.h"
24 #include "winbind/wb_server.h"
25 #include "smbd/service_task.h"
26 #include "librpc/gen_ndr/ndr_lsa_c.h"
27
28 /* List trusted domains. To avoid the trouble with having to wait for other
29  * conflicting requests waiting for the lsa pipe we're opening our own lsa
30  * pipe here. */
31
32 struct cmd_list_trustdom_state {
33         struct composite_context *ctx;
34         struct dcerpc_pipe *lsa_pipe;
35         struct policy_handle *lsa_policy;
36         uint32_t num_domains;
37         struct wb_dom_info **domains;
38
39         uint32_t resume_handle;
40         struct lsa_DomainList domainlist;
41         struct lsa_EnumTrustDom r;
42 };
43
44 static void cmd_list_trustdoms_recv_domain(struct composite_context *ctx);
45 static void cmd_list_trustdoms_recv_lsa(struct composite_context *ctx);
46 static void cmd_list_trustdoms_recv_doms(struct tevent_req *subreq);
47
48 struct composite_context *wb_cmd_list_trustdoms_send(TALLOC_CTX *mem_ctx,
49                                                      struct wbsrv_service *service)
50 {
51         struct composite_context *result, *ctx;
52         struct cmd_list_trustdom_state *state;
53
54         result = composite_create(mem_ctx, service->task->event_ctx);
55         if (result == NULL) goto failed;
56
57         state = talloc(result, struct cmd_list_trustdom_state);
58         if (state == NULL) goto failed;
59         state->ctx = result;
60         result->private_data = state;
61
62         ctx = wb_sid2domain_send(state, service, service->primary_sid);
63         if (ctx == NULL) goto failed;
64         ctx->async.fn = cmd_list_trustdoms_recv_domain;
65         ctx->async.private_data = state;
66         return result;
67
68  failed:
69         talloc_free(result);
70         return NULL;
71 }
72
73 static void cmd_list_trustdoms_recv_domain(struct composite_context *ctx)
74 {
75         struct cmd_list_trustdom_state *state =
76                 talloc_get_type(ctx->async.private_data,
77                                 struct cmd_list_trustdom_state);
78         struct wbsrv_domain *domain;
79         struct smbcli_tree *tree;
80
81         state->ctx->status = wb_sid2domain_recv(ctx, &domain);
82         if (!composite_is_ok(state->ctx)) return;
83
84         tree = dcerpc_smb_tree(domain->libnet_ctx->lsa.pipe->conn);
85         if (composite_nomem(tree, state->ctx)) return;
86
87         ctx = wb_init_lsa_send(state, domain);
88         composite_continue(state->ctx, ctx, cmd_list_trustdoms_recv_lsa,
89                            state);
90 }
91
92 static void cmd_list_trustdoms_recv_lsa(struct composite_context *ctx)
93 {
94         struct cmd_list_trustdom_state *state =
95                 talloc_get_type(ctx->async.private_data,
96                                 struct cmd_list_trustdom_state);
97         struct tevent_req *subreq;
98
99         state->ctx->status = wb_init_lsa_recv(ctx, state,
100                                               &state->lsa_pipe,
101                                               &state->lsa_policy);
102         if (!composite_is_ok(state->ctx)) return;
103
104         state->num_domains = 0;
105         state->domains = NULL;
106
107         state->domainlist.count = 0;
108         state->domainlist.domains = NULL;
109
110         state->resume_handle = 0;
111         state->r.in.handle = state->lsa_policy;
112         state->r.in.resume_handle = &state->resume_handle;
113         state->r.in.max_size = 1000;
114         state->r.out.resume_handle = &state->resume_handle;
115         state->r.out.domains = &state->domainlist;
116
117         subreq = dcerpc_lsa_EnumTrustDom_r_send(state,
118                                                 state->ctx->event_ctx,
119                                                 state->lsa_pipe->binding_handle,
120                                                 &state->r);
121         if (composite_nomem(subreq, state->ctx)) return;
122         tevent_req_set_callback(subreq, cmd_list_trustdoms_recv_doms, state);
123 }
124
125 static void cmd_list_trustdoms_recv_doms(struct tevent_req *subreq)
126 {
127         struct cmd_list_trustdom_state *state =
128                 tevent_req_callback_data(subreq,
129                 struct cmd_list_trustdom_state);
130         uint32_t i, old_num_domains;
131
132         state->ctx->status = dcerpc_lsa_EnumTrustDom_r_recv(subreq, state);
133         TALLOC_FREE(subreq);
134         if (!composite_is_ok(state->ctx)) return;
135         state->ctx->status = state->r.out.result;
136
137         if (!NT_STATUS_IS_OK(state->ctx->status) &&
138             !NT_STATUS_EQUAL(state->ctx->status, NT_STATUS_NO_MORE_ENTRIES) &&
139             !NT_STATUS_EQUAL(state->ctx->status, STATUS_MORE_ENTRIES)) {
140                 composite_error(state->ctx, state->ctx->status);
141                 return;
142         }
143
144         old_num_domains = state->num_domains;
145
146         state->num_domains += state->r.out.domains->count;
147         state->domains = talloc_realloc(state, state->domains,
148                                         struct wb_dom_info *,
149                                         state->num_domains);
150         if (state->num_domains && 
151             composite_nomem(state->domains, state->ctx)) return;
152
153         for (i=0; i<state->r.out.domains->count; i++) {
154                 uint32_t 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) || NT_STATUS_EQUAL(state->ctx->status, NT_STATUS_NO_MORE_ENTRIES)) {
167                 state->ctx->status = NT_STATUS_OK;
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         subreq = dcerpc_lsa_EnumTrustDom_r_send(state,
181                                                 state->ctx->event_ctx,
182                                                 state->lsa_pipe->binding_handle,
183                                                 &state->r);
184         if (composite_nomem(subreq, state->ctx)) return;
185         tevent_req_set_callback(subreq, cmd_list_trustdoms_recv_doms, state);
186 }
187
188 NTSTATUS wb_cmd_list_trustdoms_recv(struct composite_context *ctx,
189                                     TALLOC_CTX *mem_ctx,
190                                     uint32_t *num_domains,
191                                     struct wb_dom_info ***domains)
192 {
193         NTSTATUS status = composite_wait(ctx);
194         if (NT_STATUS_IS_OK(status)) {
195                 struct cmd_list_trustdom_state *state =
196                         talloc_get_type(ctx->private_data,
197                                         struct cmd_list_trustdom_state);
198                 *num_domains = state->num_domains;
199                 *domains = talloc_steal(mem_ctx, state->domains);
200         }
201         talloc_free(ctx);
202         return status;
203 }