2 Unix SMB/CIFS implementation.
4 Command backend for wbinfo --user-sids
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_samr.h"
29 /* Calculate the token in two steps: Go the user's originating domain, ask for
30 * the user's domain groups. Then with the resulting list of sids go to our
31 * own domain to expand the aliases aka domain local groups. */
33 struct cmd_usersids_state {
34 struct composite_context *ctx;
35 struct wbsrv_service *service;
36 struct dom_sid *user_sid;
38 struct dom_sid **domgroups;
40 struct lsa_SidArray lsa_sids;
42 struct samr_GetAliasMembership r;
45 struct dom_sid **sids;
48 static void usersids_recv_domgroups(struct composite_context *ctx);
49 static void usersids_recv_domain(struct composite_context *ctx);
50 static void usersids_recv_aliases(struct rpc_request *req);
52 struct composite_context *wb_cmd_usersids_send(TALLOC_CTX *mem_ctx,
53 struct wbsrv_service *service,
54 const struct dom_sid *sid)
56 struct composite_context *result, *ctx;
57 struct cmd_usersids_state *state;
59 result = talloc(mem_ctx, struct composite_context);
60 if (result == NULL) goto failed;
61 result->state = COMPOSITE_STATE_IN_PROGRESS;
62 result->async.fn = NULL;
63 result->event_ctx = service->task->event_ctx;
65 state = talloc(result, struct cmd_usersids_state);
66 if (state == NULL) goto failed;
68 result->private_data = state;
70 state->service = service;
71 state->user_sid = dom_sid_dup(state, sid);
72 if (state->user_sid == NULL) goto failed;
74 ctx = wb_cmd_userdomgroups_send(state, service, sid);
75 if (ctx == NULL) goto failed;
77 ctx->async.fn = usersids_recv_domgroups;
78 ctx->async.private_data = state;
86 static void usersids_recv_domgroups(struct composite_context *ctx)
88 struct cmd_usersids_state *state =
89 talloc_get_type(ctx->async.private_data,
90 struct cmd_usersids_state);
92 state->ctx->status = wb_cmd_userdomgroups_recv(ctx, state,
93 &state->num_domgroups,
95 if (!composite_is_ok(state->ctx)) return;
97 ctx = wb_sid2domain_send(state, state->service,
98 state->service->primary_sid);
99 composite_continue(state->ctx, ctx, usersids_recv_domain, state);
102 static void usersids_recv_domain(struct composite_context *ctx)
104 struct cmd_usersids_state *state =
105 talloc_get_type(ctx->async.private_data,
106 struct cmd_usersids_state);
107 struct rpc_request *req;
108 struct wbsrv_domain *domain;
111 state->ctx->status = wb_sid2domain_recv(ctx, &domain);
112 if (!composite_is_ok(state->ctx)) return;
114 state->lsa_sids.num_sids = state->num_domgroups+1;
115 state->lsa_sids.sids = talloc_array(state, struct lsa_SidPtr,
116 state->lsa_sids.num_sids);
117 if (composite_nomem(state->lsa_sids.sids, state->ctx)) return;
119 state->lsa_sids.sids[0].sid = state->user_sid;
120 for (i=0; i<state->num_domgroups; i++) {
121 state->lsa_sids.sids[i+1].sid = state->domgroups[i];
124 state->rids.count = 0;
125 state->rids.ids = NULL;
127 state->r.in.domain_handle = domain->domain_handle;
128 state->r.in.sids = &state->lsa_sids;
129 state->r.out.rids = &state->rids;
131 req = dcerpc_samr_GetAliasMembership_send(domain->samr_pipe, state,
133 composite_continue_rpc(state->ctx, req, usersids_recv_aliases, state);
136 static void usersids_recv_aliases(struct rpc_request *req)
138 struct cmd_usersids_state *state =
139 talloc_get_type(req->async.private,
140 struct cmd_usersids_state);
143 state->ctx->status = dcerpc_ndr_request_recv(req);
144 if (!composite_is_ok(state->ctx)) return;
145 state->ctx->status = state->r.out.result;
146 if (!composite_is_ok(state->ctx)) return;
148 state->num_sids = 1 + state->num_domgroups + state->r.out.rids->count;
149 state->sids = talloc_array(state, struct dom_sid *, state->num_sids);
150 if (composite_nomem(state->sids, state->ctx)) return;
152 state->sids[0] = talloc_steal(state->sids, state->user_sid);
154 for (i=0; i<state->num_domgroups; i++) {
156 talloc_steal(state->sids, state->domgroups[i]);
159 for (i=0; i<state->r.out.rids->count; i++) {
160 state->sids[1+state->num_domgroups+i] = dom_sid_add_rid(
161 state->sids, state->service->primary_sid,
162 state->r.out.rids->ids[i]);
164 if (composite_nomem(state->sids[1+state->num_domgroups+i],
168 composite_done(state->ctx);
171 NTSTATUS wb_cmd_usersids_recv(struct composite_context *ctx,
173 int *num_sids, struct dom_sid ***sids)
175 NTSTATUS status = composite_wait(ctx);
176 if (NT_STATUS_IS_OK(status)) {
177 struct cmd_usersids_state *state =
178 talloc_get_type(ctx->private_data,
179 struct cmd_usersids_state);
180 *num_sids = state->num_sids;
181 *sids = talloc_steal(mem_ctx, state->sids);
187 NTSTATUS wb_cmd_usersids(TALLOC_CTX *mem_ctx, struct wbsrv_service *service,
188 const struct dom_sid *sid,
189 int *num_sids, struct dom_sid ***sids)
191 struct composite_context *c =
192 wb_cmd_usersids_send(mem_ctx, service, sid);
193 return wb_cmd_usersids_recv(c, mem_ctx, num_sids, sids);