2 Unix SMB/CIFS implementation.
4 Command backend for wbinfo -m
6 Copyright (C) Volker Lendecke 2005
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.
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.
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.
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_c.h"
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
33 struct cmd_list_trustdom_state {
34 struct composite_context *ctx;
35 struct dcerpc_pipe *lsa_pipe;
36 struct policy_handle *lsa_policy;
38 struct wb_dom_info **domains;
40 uint32_t resume_handle;
41 struct lsa_DomainList domainlist;
42 struct lsa_EnumTrustDom r;
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);
49 struct composite_context *wb_cmd_list_trustdoms_send(TALLOC_CTX *mem_ctx,
50 struct wbsrv_service *service)
52 struct composite_context *result, *ctx;
53 struct cmd_list_trustdom_state *state;
55 result = composite_create(mem_ctx, service->task->event_ctx);
56 if (result == NULL) goto failed;
58 state = talloc(result, struct cmd_list_trustdom_state);
59 if (state == NULL) goto failed;
61 result->private_data = state;
63 ctx = wb_sid2domain_send(state, service, service->primary_sid);
64 if (ctx == NULL) goto failed;
65 ctx->async.fn = cmd_list_trustdoms_recv_domain;
66 ctx->async.private_data = state;
74 static void cmd_list_trustdoms_recv_domain(struct composite_context *ctx)
76 struct cmd_list_trustdom_state *state =
77 talloc_get_type(ctx->async.private_data,
78 struct cmd_list_trustdom_state);
79 struct wbsrv_domain *domain;
80 struct smbcli_tree *tree;
82 state->ctx->status = wb_sid2domain_recv(ctx, &domain);
83 if (!composite_is_ok(state->ctx)) return;
85 tree = dcerpc_smb_tree(domain->lsa_pipe->conn);
86 if (composite_nomem(tree, state->ctx)) return;
88 ctx = wb_init_lsa_send(state, domain);
89 composite_continue(state->ctx, ctx, cmd_list_trustdoms_recv_lsa,
93 static void cmd_list_trustdoms_recv_lsa(struct composite_context *ctx)
95 struct cmd_list_trustdom_state *state =
96 talloc_get_type(ctx->async.private_data,
97 struct cmd_list_trustdom_state);
98 struct rpc_request *req;
100 state->ctx->status = wb_init_lsa_recv(ctx, state,
103 if (!composite_is_ok(state->ctx)) return;
105 state->num_domains = 0;
106 state->domains = NULL;
108 state->domainlist.count = 0;
109 state->domainlist.domains = NULL;
111 state->resume_handle = 0;
112 state->r.in.handle = state->lsa_policy;
113 state->r.in.resume_handle = &state->resume_handle;
114 state->r.in.max_size = 1000;
115 state->r.out.resume_handle = &state->resume_handle;
116 state->r.out.domains = &state->domainlist;
118 req = dcerpc_lsa_EnumTrustDom_send(state->lsa_pipe, state, &state->r);
119 composite_continue_rpc(state->ctx, req, cmd_list_trustdoms_recv_doms,
123 static void cmd_list_trustdoms_recv_doms(struct rpc_request *req)
125 struct cmd_list_trustdom_state *state =
126 talloc_get_type(req->async.private_data,
127 struct cmd_list_trustdom_state);
128 int i, old_num_domains;
130 state->ctx->status = dcerpc_ndr_request_recv(req);
131 if (!composite_is_ok(state->ctx)) return;
132 state->ctx->status = state->r.out.result;
134 if (!NT_STATUS_IS_OK(state->ctx->status) &&
135 !NT_STATUS_EQUAL(state->ctx->status, NT_STATUS_NO_MORE_ENTRIES) &&
136 !NT_STATUS_EQUAL(state->ctx->status, STATUS_MORE_ENTRIES)) {
137 composite_error(state->ctx, state->ctx->status);
141 old_num_domains = state->num_domains;
143 state->num_domains += state->r.out.domains->count;
144 state->domains = talloc_realloc(state, state->domains,
145 struct wb_dom_info *,
147 if (composite_nomem(state->domains, state->ctx)) return;
149 for (i=0; i<state->r.out.domains->count; i++) {
150 int j = i+old_num_domains;
151 state->domains[j] = talloc(state->domains,
153 if (composite_nomem(state->domains[i], state->ctx)) return;
154 state->domains[j]->name = talloc_steal(
156 state->r.out.domains->domains[i].name.string);
157 state->domains[j]->sid = talloc_steal(
159 state->r.out.domains->domains[i].sid);
162 if (NT_STATUS_IS_OK(state->ctx->status)) {
163 composite_done(state->ctx);
167 state->domainlist.count = 0;
168 state->domainlist.domains = NULL;
169 state->r.in.handle = state->lsa_policy;
170 state->r.in.resume_handle = &state->resume_handle;
171 state->r.in.max_size = 1000;
172 state->r.out.resume_handle = &state->resume_handle;
173 state->r.out.domains = &state->domainlist;
175 req = dcerpc_lsa_EnumTrustDom_send(state->lsa_pipe, state, &state->r);
176 composite_continue_rpc(state->ctx, req, cmd_list_trustdoms_recv_doms,
180 NTSTATUS wb_cmd_list_trustdoms_recv(struct composite_context *ctx,
183 struct wb_dom_info ***domains)
185 NTSTATUS status = composite_wait(ctx);
186 if (NT_STATUS_IS_OK(status)) {
187 struct cmd_list_trustdom_state *state =
188 talloc_get_type(ctx->private_data,
189 struct cmd_list_trustdom_state);
190 *num_domains = state->num_domains;
191 *domains = talloc_steal(mem_ctx, state->domains);