r12608: Remove some unused #include lines.
[abartlet/samba.git/.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
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         int 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         int 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 rpc_request *req);
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         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;
64
65         state = talloc(result, struct cmd_usersids_state);
66         if (state == NULL) goto failed;
67         state->ctx = result;
68         result->private_data = state;
69
70         state->service = service;
71         state->user_sid = dom_sid_dup(state, sid);
72         if (state->user_sid == NULL) goto failed;
73
74         ctx = wb_cmd_userdomgroups_send(state, service, sid);
75         if (ctx == NULL) goto failed;
76
77         ctx->async.fn = usersids_recv_domgroups;
78         ctx->async.private_data = state;
79         return result;
80
81  failed:
82         talloc_free(result);
83         return NULL;
84 }
85
86 static void usersids_recv_domgroups(struct composite_context *ctx)
87 {
88         struct cmd_usersids_state *state =
89                 talloc_get_type(ctx->async.private_data,
90                                 struct cmd_usersids_state);
91
92         state->ctx->status = wb_cmd_userdomgroups_recv(ctx, state,
93                                                        &state->num_domgroups,
94                                                        &state->domgroups);
95         if (!composite_is_ok(state->ctx)) return;
96
97         ctx = wb_sid2domain_send(state, state->service,
98                                  state->service->primary_sid);
99         composite_continue(state->ctx, ctx, usersids_recv_domain, state);
100 }
101
102 static void usersids_recv_domain(struct composite_context *ctx)
103 {
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;
109         int i;
110
111         state->ctx->status = wb_sid2domain_recv(ctx, &domain);
112         if (!composite_is_ok(state->ctx)) return;
113
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;
118
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];
122         }
123
124         state->rids.count = 0;
125         state->rids.ids = NULL;
126
127         state->r.in.domain_handle = domain->domain_handle;
128         state->r.in.sids = &state->lsa_sids;
129         state->r.out.rids = &state->rids;
130
131         req = dcerpc_samr_GetAliasMembership_send(domain->samr_pipe, state,
132                                                   &state->r);
133         composite_continue_rpc(state->ctx, req, usersids_recv_aliases, state);
134 }
135
136 static void usersids_recv_aliases(struct rpc_request *req)
137 {
138         struct cmd_usersids_state *state =
139                 talloc_get_type(req->async.private,
140                                 struct cmd_usersids_state);
141         int i;
142
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;
147
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;
151
152         state->sids[0] = talloc_steal(state->sids, state->user_sid);
153
154         for (i=0; i<state->num_domgroups; i++) {
155                 state->sids[1+i] =
156                         talloc_steal(state->sids, state->domgroups[i]);
157         }
158
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]);
163
164                 if (composite_nomem(state->sids[1+state->num_domgroups+i],
165                                     state->ctx)) return;
166         }
167
168         composite_done(state->ctx);
169 }
170
171 NTSTATUS wb_cmd_usersids_recv(struct composite_context *ctx,
172                               TALLOC_CTX *mem_ctx,
173                               int *num_sids, struct dom_sid ***sids)
174 {
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);
182         }
183         talloc_free(ctx);
184         return status;
185 }
186
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)
190 {
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);
194 }
195