r14402: Generate seperate headers for RPC client functions.
[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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
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"
30
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. */
34
35 struct cmd_usersids_state {
36         struct composite_context *ctx;
37         struct wbsrv_service *service;
38         struct dom_sid *user_sid;
39         int num_domgroups;
40         struct dom_sid **domgroups;
41
42         struct lsa_SidArray lsa_sids;
43         struct samr_Ids rids;
44         struct samr_GetAliasMembership r;
45
46         int num_sids;
47         struct dom_sid **sids;
48 };
49
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);
53
54 struct composite_context *wb_cmd_usersids_send(TALLOC_CTX *mem_ctx,
55                                                struct wbsrv_service *service,
56                                                const struct dom_sid *sid)
57 {
58         struct composite_context *result, *ctx;
59         struct cmd_usersids_state *state;
60
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;
66
67         state = talloc(result, struct cmd_usersids_state);
68         if (state == NULL) goto failed;
69         state->ctx = result;
70         result->private_data = state;
71
72         state->service = service;
73         state->user_sid = dom_sid_dup(state, sid);
74         if (state->user_sid == NULL) goto failed;
75
76         ctx = wb_cmd_userdomgroups_send(state, service, sid);
77         if (ctx == NULL) goto failed;
78
79         ctx->async.fn = usersids_recv_domgroups;
80         ctx->async.private_data = state;
81         return result;
82
83  failed:
84         talloc_free(result);
85         return NULL;
86 }
87
88 static void usersids_recv_domgroups(struct composite_context *ctx)
89 {
90         struct cmd_usersids_state *state =
91                 talloc_get_type(ctx->async.private_data,
92                                 struct cmd_usersids_state);
93
94         state->ctx->status = wb_cmd_userdomgroups_recv(ctx, state,
95                                                        &state->num_domgroups,
96                                                        &state->domgroups);
97         if (!composite_is_ok(state->ctx)) return;
98
99         ctx = wb_sid2domain_send(state, state->service,
100                                  state->service->primary_sid);
101         composite_continue(state->ctx, ctx, usersids_recv_domain, state);
102 }
103
104 static void usersids_recv_domain(struct composite_context *ctx)
105 {
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;
111         int i;
112
113         state->ctx->status = wb_sid2domain_recv(ctx, &domain);
114         if (!composite_is_ok(state->ctx)) return;
115
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;
120
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];
124         }
125
126         state->rids.count = 0;
127         state->rids.ids = NULL;
128
129         state->r.in.domain_handle = domain->domain_handle;
130         state->r.in.sids = &state->lsa_sids;
131         state->r.out.rids = &state->rids;
132
133         req = dcerpc_samr_GetAliasMembership_send(domain->samr_pipe, state,
134                                                   &state->r);
135         composite_continue_rpc(state->ctx, req, usersids_recv_aliases, state);
136 }
137
138 static void usersids_recv_aliases(struct rpc_request *req)
139 {
140         struct cmd_usersids_state *state =
141                 talloc_get_type(req->async.private,
142                                 struct cmd_usersids_state);
143         int i;
144
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;
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                               int *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                          int *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