4e2195d88367dc18be15d6733093da39f45ab472
[kai/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 "libcli/security/proto.h"
29
30 /* Calculate the token in two steps: Go the user's originating domain, ask for
31  * the user's domain groups. Then with the resulting list of sids go to our
32  * own domain to expand the aliases aka domain local groups. */
33
34 struct cmd_usersids_state {
35         struct composite_context *ctx;
36         struct wbsrv_service *service;
37         struct dom_sid *user_sid;
38         int num_domgroups;
39         struct dom_sid **domgroups;
40
41         struct lsa_SidArray lsa_sids;
42         struct samr_Ids rids;
43         struct samr_GetAliasMembership r;
44
45         int num_sids;
46         struct dom_sid **sids;
47 };
48
49 static void usersids_recv_domgroups(struct composite_context *ctx);
50 static void usersids_recv_domain(struct composite_context *ctx);
51 static void usersids_recv_aliases(struct rpc_request *req);
52
53 struct composite_context *wb_cmd_usersids_send(TALLOC_CTX *mem_ctx,
54                                                struct wbsrv_service *service,
55                                                const struct dom_sid *sid)
56 {
57         struct composite_context *result, *ctx;
58         struct cmd_usersids_state *state;
59
60         result = talloc(mem_ctx, struct composite_context);
61         if (result == NULL) goto failed;
62         result->state = COMPOSITE_STATE_IN_PROGRESS;
63         result->async.fn = NULL;
64         result->event_ctx = service->task->event_ctx;
65
66         state = talloc(result, struct cmd_usersids_state);
67         if (state == NULL) goto failed;
68         state->ctx = result;
69         result->private_data = state;
70
71         state->service = service;
72         state->user_sid = dom_sid_dup(state, sid);
73         if (state->user_sid == NULL) goto failed;
74
75         ctx = wb_cmd_userdomgroups_send(state, service, sid);
76         if (ctx == NULL) goto failed;
77
78         ctx->async.fn = usersids_recv_domgroups;
79         ctx->async.private_data = state;
80         return result;
81
82  failed:
83         talloc_free(result);
84         return NULL;
85 }
86
87 static void usersids_recv_domgroups(struct composite_context *ctx)
88 {
89         struct cmd_usersids_state *state =
90                 talloc_get_type(ctx->async.private_data,
91                                 struct cmd_usersids_state);
92
93         state->ctx->status = wb_cmd_userdomgroups_recv(ctx, state,
94                                                        &state->num_domgroups,
95                                                        &state->domgroups);
96         if (!composite_is_ok(state->ctx)) return;
97
98         ctx = wb_sid2domain_send(state, state->service,
99                                  state->service->primary_sid);
100         composite_continue(state->ctx, ctx, usersids_recv_domain, state);
101 }
102
103 static void usersids_recv_domain(struct composite_context *ctx)
104 {
105         struct cmd_usersids_state *state =
106                 talloc_get_type(ctx->async.private_data,
107                                 struct cmd_usersids_state);
108         struct rpc_request *req;
109         struct wbsrv_domain *domain;
110         int i;
111
112         state->ctx->status = wb_sid2domain_recv(ctx, &domain);
113         if (!composite_is_ok(state->ctx)) return;
114
115         state->lsa_sids.num_sids = state->num_domgroups+1;
116         state->lsa_sids.sids = talloc_array(state, struct lsa_SidPtr,
117                                             state->lsa_sids.num_sids);
118         if (composite_nomem(state->lsa_sids.sids, state->ctx)) return;
119
120         state->lsa_sids.sids[0].sid = state->user_sid;
121         for (i=0; i<state->num_domgroups; i++) {
122                 state->lsa_sids.sids[i+1].sid = state->domgroups[i];
123         }
124
125         state->rids.count = 0;
126         state->rids.ids = NULL;
127
128         state->r.in.domain_handle = domain->domain_handle;
129         state->r.in.sids = &state->lsa_sids;
130         state->r.out.rids = &state->rids;
131
132         req = dcerpc_samr_GetAliasMembership_send(domain->samr_pipe, state,
133                                                   &state->r);
134         composite_continue_rpc(state->ctx, req, usersids_recv_aliases, state);
135 }
136
137 static void usersids_recv_aliases(struct rpc_request *req)
138 {
139         struct cmd_usersids_state *state =
140                 talloc_get_type(req->async.private,
141                                 struct cmd_usersids_state);
142         int i;
143
144         state->ctx->status = dcerpc_ndr_request_recv(req);
145         if (!composite_is_ok(state->ctx)) return;
146         state->ctx->status = state->r.out.result;
147         if (!composite_is_ok(state->ctx)) return;
148
149         state->num_sids = 1 + state->num_domgroups + state->r.out.rids->count;
150         state->sids = talloc_array(state, struct dom_sid *, state->num_sids);
151         if (composite_nomem(state->sids, state->ctx)) return;
152
153         state->sids[0] = talloc_steal(state->sids, state->user_sid);
154
155         for (i=0; i<state->num_domgroups; i++) {
156                 state->sids[1+i] =
157                         talloc_steal(state->sids, state->domgroups[i]);
158         }
159
160         for (i=0; i<state->r.out.rids->count; i++) {
161                 state->sids[1+state->num_domgroups+i] = dom_sid_add_rid(
162                         state->sids, state->service->primary_sid,
163                         state->r.out.rids->ids[i]);
164
165                 if (composite_nomem(state->sids[1+state->num_domgroups+i],
166                                     state->ctx)) return;
167         }
168
169         composite_done(state->ctx);
170 }
171
172 NTSTATUS wb_cmd_usersids_recv(struct composite_context *ctx,
173                               TALLOC_CTX *mem_ctx,
174                               int *num_sids, struct dom_sid ***sids)
175 {
176         NTSTATUS status = composite_wait(ctx);
177         if (NT_STATUS_IS_OK(status)) {
178                 struct cmd_usersids_state *state =
179                         talloc_get_type(ctx->private_data,
180                                         struct cmd_usersids_state);
181                 *num_sids = state->num_sids;
182                 *sids = talloc_steal(mem_ctx, state->sids);
183         }
184         talloc_free(ctx);
185         return status;
186 }
187
188 NTSTATUS wb_cmd_usersids(TALLOC_CTX *mem_ctx, struct wbsrv_service *service,
189                          const struct dom_sid *sid,
190                          int *num_sids, struct dom_sid ***sids)
191 {
192         struct composite_context *c =
193                 wb_cmd_usersids_send(mem_ctx, service, sid);
194         return wb_cmd_usersids_recv(c, mem_ctx, num_sids, sids);
195 }
196