1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
/*
Unix SMB/CIFS implementation.
Backend for getgroups
Copyright (C) Matthieu Patou 2010
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
#include "libcli/composite/composite.h"
#include "winbind/wb_server.h"
#include "smbd/service_task.h"
#include "libcli/security/dom_sid.h"
struct cmd_getgroups_state {
struct composite_context *ctx;
struct wbsrv_service *service;
char* username;
uint32_t num_groups;
uint32_t current_group;
struct dom_sid **sids;
gid_t *gids;
};
/* The idea is to get the groups for a user
We receive one user from this we search for his uid
From the uid we search for his SID
From the SID we search for the list of groups
And with the list of groups we search for each group its gid
*/
static void cmd_getgroups_recv_pwnam(struct composite_context *ctx);
static void wb_getgroups_uid2sid_recv(struct composite_context *ctx);
static void wb_getgroups_userdomsgroups_recv(struct composite_context *ctx);
static void cmd_getgroups_recv_gid(struct composite_context *ctx);
/*
Ask for the uid from the username
*/
struct composite_context *wb_cmd_getgroups_send(TALLOC_CTX *mem_ctx,
struct wbsrv_service *service,
const char* username)
{
struct composite_context *ctx, *result;
struct cmd_getgroups_state *state;
DEBUG(5, ("wb_cmd_getgroups_send called\n"));
result = composite_create(mem_ctx, service->task->event_ctx);
if (!result) return NULL;
state = talloc(mem_ctx, struct cmd_getgroups_state);
if (composite_nomem(state, result)) return result;
state->ctx = result;
result->private_data = state;
state->service = service;
state->num_groups = 0;
state->username = talloc_strdup(state,username);
if (composite_nomem(ctx, result)) return result;
ctx = wb_cmd_getpwnam_send(state, service, username);
if (composite_nomem(ctx, result)) return result;
composite_continue(result, ctx, cmd_getgroups_recv_pwnam, state);
return result;
}
/*
Receive the uid and send request for SID
*/
static void cmd_getgroups_recv_pwnam(struct composite_context *ctx)
{
struct composite_context *res;
struct cmd_getgroups_state *state =
talloc_get_type(ctx->async.private_data,
struct cmd_getgroups_state);
struct winbindd_pw *pw;
struct wbsrv_service *service = state->service;
DEBUG(5, ("cmd_getgroups_recv_pwnam called\n"));
state->ctx->status = wb_cmd_getpwnam_recv(ctx, state, &pw);
if (composite_is_ok(state->ctx)) {
res = wb_uid2sid_send(state, service, pw->pw_uid);
if (res == NULL) {
composite_error(state->ctx, NT_STATUS_NO_MEMORY);
return;
}
DEBUG(6, ("cmd_getgroups_recv_pwnam uid %d\n",pw->pw_uid));
composite_continue(ctx, res, wb_getgroups_uid2sid_recv, state);
}
}
/*
Receive the SID and request groups through the userdomgroups helper
*/
static void wb_getgroups_uid2sid_recv(struct composite_context *ctx)
{
struct composite_context *res;
struct cmd_getgroups_state *state =
talloc_get_type(ctx->async.private_data,
struct cmd_getgroups_state);
NTSTATUS status;
struct dom_sid *sid;
char *sid_str;
DEBUG(5, ("wb_getgroups_uid2sid_recv called\n"));
status = wb_uid2sid_recv(ctx, state, &sid);
if(NT_STATUS_IS_OK(status)) {
sid_str = dom_sid_string(state, sid);
/* If the conversion failed, bail out with a failure. */
if (sid_str != NULL) {
DEBUG(7, ("wb_getgroups_uid2sid_recv SID = %s\n",sid_str));
/* Ok got the SID now get the groups */
res = wb_cmd_userdomgroups_send(state, state->service, sid);
if (res == NULL) {
composite_error(state->ctx,
NT_STATUS_NO_MEMORY);
return;
}
composite_continue(ctx, res, wb_getgroups_userdomsgroups_recv, state);
} else {
composite_error(state->ctx, NT_STATUS_UNSUCCESSFUL);
}
}
}
/*
Receive groups and search for uid for the first group
*/
static void wb_getgroups_userdomsgroups_recv(struct composite_context *ctx) {
struct cmd_getgroups_state *state =
talloc_get_type(ctx->async.private_data,
struct cmd_getgroups_state);
uint32_t num_sids;
struct dom_sid **sids;
DEBUG(5, ("wb_getgroups_userdomsgroups_recv called\n"));
state->ctx->status = wb_cmd_userdomgroups_recv(ctx,state,&num_sids,&sids);
if (!composite_is_ok(state->ctx)) return;
DEBUG(5, ("wb_getgroups_userdomsgroups_recv %d groups\n",num_sids));
state->sids=sids;
state->num_groups=num_sids;
state->current_group=0;
if(num_sids > 0) {
state->gids = talloc_array(state, gid_t, state->num_groups);
ctx = wb_sid2gid_send(state, state->service, state->sids[state->current_group]);
composite_continue(state->ctx, ctx, cmd_getgroups_recv_gid, state);
} else {
composite_done(state->ctx);
}
}
/*
Receive and uid the previous searched group and request the uid for the next one
*/
static void cmd_getgroups_recv_gid(struct composite_context *ctx)
{
struct cmd_getgroups_state *state =
talloc_get_type(ctx->async.private_data,
struct cmd_getgroups_state);
gid_t gid;
DEBUG(5, ("cmd_getgroups_recv_gid called\n"));
state->ctx->status = wb_sid2gid_recv(ctx, &gid);
if(!composite_is_ok(state->ctx)) return;
state->gids[state->current_group] = gid;
DEBUG(5, ("cmd_getgroups_recv_gid group %d \n",state->current_group));
state->current_group++;
if(state->current_group < state->num_groups ) {
ctx = wb_sid2gid_send(state, state->service, state->sids[state->current_group]);
composite_continue(state->ctx, ctx, cmd_getgroups_recv_gid, state);
} else {
composite_done(state->ctx);
}
}
/*
Return list of uids when finished
*/
NTSTATUS wb_cmd_getgroups_recv(struct composite_context *ctx,
TALLOC_CTX *mem_ctx, gid_t **groups,
uint32_t *num_groups)
{
NTSTATUS status = composite_wait(ctx);
DEBUG(5, ("wb_cmd_getgroups_recv called\n"));
if (NT_STATUS_IS_OK(status)) {
struct cmd_getgroups_state *state =
talloc_get_type(ctx->private_data,
struct cmd_getgroups_state);
*groups = talloc_steal(mem_ctx, state->gids);
*num_groups = state->num_groups;
}
talloc_free(ctx);
return status;
}
|