s4:winbind: let wb_lsa_lookupsids_send() take tevent_context/dcerpc_binding_handle
[samba.git] / source4 / winbind / wb_cmd_usersids.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Command backend for wbinfo --user-sids
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_samr_c.h"
27 #include "libcli/security/security.h"
28
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. */
32
33 struct cmd_usersids_state {
34         struct composite_context *ctx;
35         struct wbsrv_service *service;
36         struct dom_sid *user_sid;
37         uint32_t num_domgroups;
38         struct dom_sid **domgroups;
39
40         struct lsa_SidArray lsa_sids;
41         struct samr_Ids rids;
42         struct samr_GetAliasMembership r;
43
44         uint32_t num_sids;
45         struct dom_sid **sids;
46 };
47
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 tevent_req *subreq);
51
52 struct composite_context *wb_cmd_usersids_send(TALLOC_CTX *mem_ctx,
53                                                struct wbsrv_service *service,
54                                                const struct dom_sid *sid)
55 {
56         struct composite_context *result, *ctx;
57         struct cmd_usersids_state *state;
58
59
60         result = composite_create(mem_ctx, service->task->event_ctx);
61         if (result == NULL) goto failed;
62
63         state = talloc(result, struct cmd_usersids_state);
64         if (state == NULL) goto failed;
65         state->ctx = result;
66         result->private_data = state;
67
68         state->service = service;
69         state->user_sid = dom_sid_dup(state, sid);
70         if (state->user_sid == NULL) goto failed;
71
72         ctx = wb_cmd_userdomgroups_send(state, service, sid);
73         if (ctx == NULL) goto failed;
74
75         ctx->async.fn = usersids_recv_domgroups;
76         ctx->async.private_data = state;
77         return result;
78
79  failed:
80         talloc_free(result);
81         return NULL;
82 }
83
84 static void usersids_recv_domgroups(struct composite_context *ctx)
85 {
86         struct cmd_usersids_state *state =
87                 talloc_get_type(ctx->async.private_data,
88                                 struct cmd_usersids_state);
89
90         state->ctx->status = wb_cmd_userdomgroups_recv(ctx, state,
91                                                        &state->num_domgroups,
92                                                        &state->domgroups);
93         if (!composite_is_ok(state->ctx)) return;
94
95         ctx = wb_sid2domain_send(state, state->service,
96                                  state->service->primary_sid);
97         composite_continue(state->ctx, ctx, usersids_recv_domain, state);
98 }
99
100 static void usersids_recv_domain(struct composite_context *ctx)
101 {
102         struct cmd_usersids_state *state =
103                 talloc_get_type(ctx->async.private_data,
104                                 struct cmd_usersids_state);
105         struct tevent_req *subreq;
106         struct wbsrv_domain *domain;
107         uint32_t i;
108
109         state->ctx->status = wb_sid2domain_recv(ctx, &domain);
110         if (!composite_is_ok(state->ctx)) return;
111
112         state->lsa_sids.num_sids = state->num_domgroups+1;
113         state->lsa_sids.sids = talloc_array(state, struct lsa_SidPtr,
114                                             state->lsa_sids.num_sids);
115         if (composite_nomem(state->lsa_sids.sids, state->ctx)) return;
116
117         state->lsa_sids.sids[0].sid = state->user_sid;
118         for (i=0; i<state->num_domgroups; i++) {
119                 state->lsa_sids.sids[i+1].sid = state->domgroups[i];
120         }
121
122         state->rids.count = 0;
123         state->rids.ids = NULL;
124
125         state->r.in.domain_handle = &domain->libnet_ctx->samr.handle;
126         state->r.in.sids = &state->lsa_sids;
127         state->r.out.rids = &state->rids;
128
129         subreq = dcerpc_samr_GetAliasMembership_r_send(state,
130                                                        state->ctx->event_ctx,
131                                                        domain->libnet_ctx->samr.pipe->binding_handle,
132                                                        &state->r);
133         if (composite_nomem(subreq, state->ctx)) return;
134         tevent_req_set_callback(subreq, usersids_recv_aliases, state);
135 }
136
137 static void usersids_recv_aliases(struct tevent_req *subreq)
138 {
139         struct cmd_usersids_state *state =
140                 tevent_req_callback_data(subreq,
141                 struct cmd_usersids_state);
142         uint32_t i;
143
144         state->ctx->status = dcerpc_samr_GetAliasMembership_r_recv(subreq, state);
145         TALLOC_FREE(subreq);
146         if (!composite_is_ok(state->ctx)) return;
147         state->ctx->status = state->r.out.result;
148         if (!composite_is_ok(state->ctx)) return;
149
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;
153
154         state->sids[0] = talloc_steal(state->sids, state->user_sid);
155
156         for (i=0; i<state->num_domgroups; i++) {
157                 state->sids[1+i] =
158                         talloc_steal(state->sids, state->domgroups[i]);
159         }
160
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]);
165
166                 if (composite_nomem(state->sids[1+state->num_domgroups+i],
167                                     state->ctx)) return;
168         }
169
170         composite_done(state->ctx);
171 }
172
173 NTSTATUS wb_cmd_usersids_recv(struct composite_context *ctx,
174                               TALLOC_CTX *mem_ctx,
175                               uint32_t *num_sids, struct dom_sid ***sids)
176 {
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);
184         }
185         talloc_free(ctx);
186         return status;
187 }
188
189 NTSTATUS wb_cmd_usersids(TALLOC_CTX *mem_ctx, struct wbsrv_service *service,
190                          const struct dom_sid *sid,
191                          uint32_t *num_sids, struct dom_sid ***sids)
192 {
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);
196 }
197