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"
28 #include "librpc/gen_ndr/ndr_samr_c.h"
29 #include "libcli/security/proto.h"
31 /* Calculate the token in two steps: Go the user's originating domain, ask for
32 * the user's domain groups. Then with the resulting list of sids go to our
33 * own domain to expand the aliases aka domain local groups. */
35 struct cmd_usersids_state {
36 struct composite_context *ctx;
37 struct wbsrv_service *service;
38 struct dom_sid *user_sid;
40 struct dom_sid **domgroups;
42 struct lsa_SidArray lsa_sids;
44 struct samr_GetAliasMembership r;
47 struct dom_sid **sids;
50 static void usersids_recv_domgroups(struct composite_context *ctx);
51 static void usersids_recv_domain(struct composite_context *ctx);
52 static void usersids_recv_aliases(struct rpc_request *req);
54 struct composite_context *wb_cmd_usersids_send(TALLOC_CTX *mem_ctx,
55 struct wbsrv_service *service,
56 const struct dom_sid *sid)
58 struct composite_context *result, *ctx;
59 struct cmd_usersids_state *state;
61 result = talloc(mem_ctx, struct composite_context);
62 if (result == NULL) goto failed;
63 result->state = COMPOSITE_STATE_IN_PROGRESS;
64 result->async.fn = NULL;
65 result->event_ctx = service->task->event_ctx;
67 state = talloc(result, struct cmd_usersids_state);
68 if (state == NULL) goto failed;
70 result->private_data = state;
72 state->service = service;
73 state->user_sid = dom_sid_dup(state, sid);
74 if (state->user_sid == NULL) goto failed;
76 ctx = wb_cmd_userdomgroups_send(state, service, sid);
77 if (ctx == NULL) goto failed;
79 ctx->async.fn = usersids_recv_domgroups;
80 ctx->async.private_data = state;
88 static void usersids_recv_domgroups(struct composite_context *ctx)
90 struct cmd_usersids_state *state =
91 talloc_get_type(ctx->async.private_data,
92 struct cmd_usersids_state);
94 state->ctx->status = wb_cmd_userdomgroups_recv(ctx, state,
95 &state->num_domgroups,
97 if (!composite_is_ok(state->ctx)) return;
99 ctx = wb_sid2domain_send(state, state->service,
100 state->service->primary_sid);
101 composite_continue(state->ctx, ctx, usersids_recv_domain, state);
104 static void usersids_recv_domain(struct composite_context *ctx)
106 struct cmd_usersids_state *state =
107 talloc_get_type(ctx->async.private_data,
108 struct cmd_usersids_state);
109 struct rpc_request *req;
110 struct wbsrv_domain *domain;
113 state->ctx->status = wb_sid2domain_recv(ctx, &domain);
114 if (!composite_is_ok(state->ctx)) return;
116 state->lsa_sids.num_sids = state->num_domgroups+1;
117 state->lsa_sids.sids = talloc_array(state, struct lsa_SidPtr,
118 state->lsa_sids.num_sids);
119 if (composite_nomem(state->lsa_sids.sids, state->ctx)) return;
121 state->lsa_sids.sids[0].sid = state->user_sid;
122 for (i=0; i<state->num_domgroups; i++) {
123 state->lsa_sids.sids[i+1].sid = state->domgroups[i];
126 state->rids.count = 0;
127 state->rids.ids = NULL;
129 state->r.in.domain_handle = domain->domain_handle;
130 state->r.in.sids = &state->lsa_sids;
131 state->r.out.rids = &state->rids;
133 req = dcerpc_samr_GetAliasMembership_send(domain->samr_pipe, state,
135 composite_continue_rpc(state->ctx, req, usersids_recv_aliases, state);
138 static void usersids_recv_aliases(struct rpc_request *req)
140 struct cmd_usersids_state *state =
141 talloc_get_type(req->async.private,
142 struct cmd_usersids_state);
145 state->ctx->status = dcerpc_ndr_request_recv(req);
146 if (!composite_is_ok(state->ctx)) return;
147 state->ctx->status = state->r.out.result;
148 if (!composite_is_ok(state->ctx)) return;
150 state->num_sids = 1 + state->num_domgroups + state->r.out.rids->count;
151 state->sids = talloc_array(state, struct dom_sid *, state->num_sids);
152 if (composite_nomem(state->sids, state->ctx)) return;
154 state->sids[0] = talloc_steal(state->sids, state->user_sid);
156 for (i=0; i<state->num_domgroups; i++) {
158 talloc_steal(state->sids, state->domgroups[i]);
161 for (i=0; i<state->r.out.rids->count; i++) {
162 state->sids[1+state->num_domgroups+i] = dom_sid_add_rid(
163 state->sids, state->service->primary_sid,
164 state->r.out.rids->ids[i]);
166 if (composite_nomem(state->sids[1+state->num_domgroups+i],
170 composite_done(state->ctx);
173 NTSTATUS wb_cmd_usersids_recv(struct composite_context *ctx,
175 int *num_sids, struct dom_sid ***sids)
177 NTSTATUS status = composite_wait(ctx);
178 if (NT_STATUS_IS_OK(status)) {
179 struct cmd_usersids_state *state =
180 talloc_get_type(ctx->private_data,
181 struct cmd_usersids_state);
182 *num_sids = state->num_sids;
183 *sids = talloc_steal(mem_ctx, state->sids);
189 NTSTATUS wb_cmd_usersids(TALLOC_CTX *mem_ctx, struct wbsrv_service *service,
190 const struct dom_sid *sid,
191 int *num_sids, struct dom_sid ***sids)
193 struct composite_context *c =
194 wb_cmd_usersids_send(mem_ctx, service, sid);
195 return wb_cmd_usersids_recv(c, mem_ctx, num_sids, sids);