r13121: Tag 4.0.0TP1
[Samba.git] / source / winbind / wb_cmd_userdomgroups.c
bloba0ac939a1e933d4fa3d34354c7609cdc323cd0f1
1 /*
2 Unix SMB/CIFS implementation.
4 Command backend for wbinfo --user-domgroups
6 Copyright (C) Volker Lendecke 2005
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.
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.
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.
23 #include "includes.h"
24 #include "libcli/composite/composite.h"
25 #include "winbind/wb_server.h"
26 #include "smbd/service_task.h"
28 struct cmd_userdomgroups_state {
29 struct composite_context *ctx;
30 struct dom_sid *dom_sid;
31 uint32_t user_rid;
32 int num_rids;
33 uint32_t *rids;
36 static void userdomgroups_recv_domain(struct composite_context *ctx);
37 static void userdomgroups_recv_rids(struct composite_context *ctx);
39 struct composite_context *wb_cmd_userdomgroups_send(TALLOC_CTX *mem_ctx,
40 struct wbsrv_service *service,
41 const struct dom_sid *sid)
43 struct composite_context *result, *ctx;
44 struct cmd_userdomgroups_state *state;
46 result = talloc(mem_ctx, struct composite_context);
47 if (result == NULL) goto failed;
48 result->state = COMPOSITE_STATE_IN_PROGRESS;
49 result->async.fn = NULL;
50 result->event_ctx = service->task->event_ctx;
52 state = talloc(result, struct cmd_userdomgroups_state);
53 if (state == NULL) goto failed;
54 state->ctx = result;
55 result->private_data = state;
57 state->dom_sid = dom_sid_dup(state, sid);
58 if (state->dom_sid == NULL) goto failed;
59 state->dom_sid->num_auths -= 1;
61 state->user_rid = sid->sub_auths[sid->num_auths-1];
63 ctx = wb_sid2domain_send(state, service, sid);
64 if (ctx == NULL) goto failed;
66 ctx->async.fn = userdomgroups_recv_domain;
67 ctx->async.private_data = state;
68 return result;
70 failed:
71 talloc_free(result);
72 return NULL;
75 static void userdomgroups_recv_domain(struct composite_context *ctx)
77 struct cmd_userdomgroups_state *state =
78 talloc_get_type(ctx->async.private_data,
79 struct cmd_userdomgroups_state);
80 struct wbsrv_domain *domain;
82 state->ctx->status = wb_sid2domain_recv(ctx, &domain);
83 if (!composite_is_ok(state->ctx)) return;
85 ctx = wb_samr_userdomgroups_send(state, domain->samr_pipe,
86 domain->domain_handle,
87 state->user_rid);
88 composite_continue(state->ctx, ctx, userdomgroups_recv_rids, state);
92 static void userdomgroups_recv_rids(struct composite_context *ctx)
94 struct cmd_userdomgroups_state *state =
95 talloc_get_type(ctx->async.private_data,
96 struct cmd_userdomgroups_state);
98 state->ctx->status = wb_samr_userdomgroups_recv(ctx, state,
99 &state->num_rids,
100 &state->rids);
101 if (!composite_is_ok(state->ctx)) return;
103 composite_done(state->ctx);
106 NTSTATUS wb_cmd_userdomgroups_recv(struct composite_context *c,
107 TALLOC_CTX *mem_ctx,
108 int *num_sids, struct dom_sid ***sids)
110 struct cmd_userdomgroups_state *state =
111 talloc_get_type(c->private_data,
112 struct cmd_userdomgroups_state);
113 int i;
114 NTSTATUS status;
116 status = composite_wait(c);
117 if (!NT_STATUS_IS_OK(status)) goto done;
119 *num_sids = state->num_rids;
120 *sids = talloc_array(mem_ctx, struct dom_sid *, state->num_rids);
121 if (*sids == NULL) {
122 status = NT_STATUS_NO_MEMORY;
123 goto done;
126 for (i=0; i<state->num_rids; i++) {
127 (*sids)[i] = dom_sid_add_rid((*sids), state->dom_sid,
128 state->rids[i]);
129 if ((*sids)[i] == NULL) {
130 status = NT_STATUS_NO_MEMORY;
131 goto done;
135 done:
136 talloc_free(c);
137 return status;
140 NTSTATUS wb_cmd_userdomgroups(TALLOC_CTX *mem_ctx,
141 struct wbsrv_service *service,
142 const struct dom_sid *sid,
143 int *num_sids, struct dom_sid ***sids)
145 struct composite_context *c =
146 wb_cmd_userdomgroups_send(mem_ctx, service, sid);
147 return wb_cmd_userdomgroups_recv(c, mem_ctx, num_sids, sids);