r23792: convert Samba4 to GPLv3
[kai/samba.git] / source4 / winbind / wb_cmd_userdomgroups.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Command backend for wbinfo --user-domgroups
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 "libcli/security/security.h"
25 #include "winbind/wb_server.h"
26 #include "winbind/wb_helper.h"
27 #include "smbd/service_task.h"
28
29 struct cmd_userdomgroups_state {
30         struct composite_context *ctx;
31         struct dom_sid *dom_sid;
32         uint32_t user_rid;
33         int num_rids;
34         uint32_t *rids;
35 };
36
37 static void userdomgroups_recv_domain(struct composite_context *ctx);
38 static void userdomgroups_recv_rids(struct composite_context *ctx);
39
40 struct composite_context *wb_cmd_userdomgroups_send(TALLOC_CTX *mem_ctx,
41                                                     struct wbsrv_service *service,
42                                                     const struct dom_sid *sid)
43 {
44         struct composite_context *result, *ctx;
45         struct cmd_userdomgroups_state *state;
46
47         result = composite_create(mem_ctx, service->task->event_ctx);
48         if (result == NULL) goto failed;
49
50         state = talloc(result, struct cmd_userdomgroups_state);
51         if (state == NULL) goto failed;
52         state->ctx = result;
53         result->private_data = state;
54
55         state->dom_sid = dom_sid_dup(state, sid);
56         if (state->dom_sid == NULL) goto failed;
57         state->dom_sid->num_auths -= 1;
58
59         state->user_rid = sid->sub_auths[sid->num_auths-1];
60
61         ctx = wb_sid2domain_send(state, service, sid);
62         if (ctx == NULL) goto failed;
63
64         ctx->async.fn = userdomgroups_recv_domain;
65         ctx->async.private_data = state;
66         return result;
67
68  failed:
69         talloc_free(result);
70         return NULL;
71 }
72
73 static void userdomgroups_recv_domain(struct composite_context *ctx)
74 {
75         struct cmd_userdomgroups_state *state =
76                 talloc_get_type(ctx->async.private_data,
77                                 struct cmd_userdomgroups_state);
78         struct wbsrv_domain *domain;
79
80         state->ctx->status = wb_sid2domain_recv(ctx, &domain);
81         if (!composite_is_ok(state->ctx)) return;
82
83         ctx = wb_samr_userdomgroups_send(state, domain->samr_pipe,
84                                          domain->domain_handle,
85                                          state->user_rid);
86         composite_continue(state->ctx, ctx, userdomgroups_recv_rids, state);
87         
88 }
89
90 static void userdomgroups_recv_rids(struct composite_context *ctx)
91 {
92         struct cmd_userdomgroups_state *state =
93                 talloc_get_type(ctx->async.private_data,
94                                 struct cmd_userdomgroups_state);
95
96         state->ctx->status = wb_samr_userdomgroups_recv(ctx, state,
97                                                         &state->num_rids,
98                                                         &state->rids);
99         if (!composite_is_ok(state->ctx)) return;
100         
101         composite_done(state->ctx);
102 }
103
104 NTSTATUS wb_cmd_userdomgroups_recv(struct composite_context *c,
105                                    TALLOC_CTX *mem_ctx,
106                                    int *num_sids, struct dom_sid ***sids)
107 {
108         struct cmd_userdomgroups_state *state =
109                 talloc_get_type(c->private_data,
110                                 struct cmd_userdomgroups_state);
111         int i;
112         NTSTATUS status;
113
114         status = composite_wait(c);
115         if (!NT_STATUS_IS_OK(status)) goto done;
116
117         *num_sids = state->num_rids;
118         *sids = talloc_array(mem_ctx, struct dom_sid *, state->num_rids);
119         if (*sids == NULL) {
120                 status = NT_STATUS_NO_MEMORY;
121                 goto done;
122         }
123
124         for (i=0; i<state->num_rids; i++) {
125                 (*sids)[i] = dom_sid_add_rid((*sids), state->dom_sid,
126                                              state->rids[i]);
127                 if ((*sids)[i] == NULL) {
128                         status = NT_STATUS_NO_MEMORY;
129                         goto done;
130                 }
131         }
132
133 done:
134         talloc_free(c);
135         return status;
136 }
137
138 NTSTATUS wb_cmd_userdomgroups(TALLOC_CTX *mem_ctx,
139                               struct wbsrv_service *service,
140                               const struct dom_sid *sid,
141                               int *num_sids, struct dom_sid ***sids)
142 {
143         struct composite_context *c =
144                 wb_cmd_userdomgroups_send(mem_ctx, service, sid);
145         return wb_cmd_userdomgroups_recv(c, mem_ctx, num_sids, sids);
146 }