2 Unix SMB/CIFS implementation.
4 Copyright (C) Volker Lendecke 2009
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "util/debug.h"
23 #include "librpc/gen_ndr/ndr_winbind_c.h"
24 #include "../libcli/security/security.h"
25 #include "passdb/machine_sid.h"
27 struct wb_gettoken_state
{
28 struct tevent_context
*ev
;
29 struct dom_sid usersid
;
30 bool expand_local_aliases
;
35 static NTSTATUS
wb_add_rids_to_sids(TALLOC_CTX
*mem_ctx
,
37 struct dom_sid
**psids
,
38 const struct dom_sid
*domain_sid
,
39 uint32_t num_rids
, uint32_t *rids
);
41 static void wb_gettoken_gotuser(struct tevent_req
*subreq
);
42 static void wb_gettoken_gotgroups(struct tevent_req
*subreq
);
43 static void wb_gettoken_trylocalgroups(struct tevent_req
*req
);
44 static void wb_gettoken_gotlocalgroups(struct tevent_req
*subreq
);
45 static void wb_gettoken_trybuiltins(struct tevent_req
*req
);
46 static void wb_gettoken_gotbuiltins(struct tevent_req
*subreq
);
48 struct tevent_req
*wb_gettoken_send(TALLOC_CTX
*mem_ctx
,
49 struct tevent_context
*ev
,
50 const struct dom_sid
*sid
,
51 bool expand_local_aliases
)
53 struct tevent_req
*req
, *subreq
;
54 struct wb_gettoken_state
*state
;
55 struct dom_sid_buf buf
;
57 req
= tevent_req_create(mem_ctx
, &state
, struct wb_gettoken_state
);
61 sid_copy(&state
->usersid
, sid
);
63 state
->expand_local_aliases
= expand_local_aliases
;
65 D_INFO("WB command gettoken start.\n"
66 "Query user SID %s (expand local aliases is %d).\n",
67 dom_sid_str_buf(sid
, &buf
),
68 expand_local_aliases
);
69 subreq
= wb_queryuser_send(state
, ev
, &state
->usersid
);
70 if (tevent_req_nomem(subreq
, req
)) {
71 return tevent_req_post(req
, ev
);
73 tevent_req_set_callback(subreq
, wb_gettoken_gotuser
, req
);
77 static void wb_gettoken_gotuser(struct tevent_req
*subreq
)
79 struct tevent_req
*req
= tevent_req_callback_data(
80 subreq
, struct tevent_req
);
81 struct wb_gettoken_state
*state
= tevent_req_data(
82 req
, struct wb_gettoken_state
);
83 struct wbint_userinfo
*info
;
85 struct dom_sid_buf buf0
, buf1
;
87 status
= wb_queryuser_recv(subreq
, state
, &info
);
89 if (tevent_req_nterror(req
, status
)) {
93 state
->sids
= talloc_array(state
, struct dom_sid
, 2);
94 if (tevent_req_nomem(state
->sids
, req
)) {
99 D_DEBUG("Got user SID %s and group SID %s\n",
100 dom_sid_str_buf(&info
->user_sid
, &buf0
),
101 dom_sid_str_buf(&info
->group_sid
, &buf1
));
102 sid_copy(&state
->sids
[0], &info
->user_sid
);
103 sid_copy(&state
->sids
[1], &info
->group_sid
);
105 D_DEBUG("Looking up user groups for the user SID.\n");
106 subreq
= wb_lookupusergroups_send(state
, state
->ev
, &info
->user_sid
);
107 if (tevent_req_nomem(subreq
, req
)) {
110 tevent_req_set_callback(subreq
, wb_gettoken_gotgroups
, req
);
113 static void wb_gettoken_gotgroups(struct tevent_req
*subreq
)
115 struct tevent_req
*req
= tevent_req_callback_data(
116 subreq
, struct tevent_req
);
117 struct wb_gettoken_state
*state
= tevent_req_data(
118 req
, struct wb_gettoken_state
);
119 uint32_t i
, num_groups
;
120 struct dom_sid
*groups
;
122 struct dom_sid_buf buf
;
124 status
= wb_lookupusergroups_recv(subreq
, state
, &num_groups
, &groups
);
126 if (!NT_STATUS_IS_OK(status
)) {
127 tevent_req_done(req
);
131 D_DEBUG("Received %"PRIu32
" group(s).\n", num_groups
);
132 for (i
= 0; i
< num_groups
; i
++) {
133 D_DEBUG("Adding SID %s.\n", dom_sid_str_buf(&groups
[i
], &buf
));
134 status
= add_sid_to_array_unique(
135 state
, &groups
[i
], &state
->sids
, &state
->num_sids
);
137 if (tevent_req_nterror(req
, status
)) {
142 wb_gettoken_trylocalgroups(req
);
145 static void wb_gettoken_trylocalgroups(struct tevent_req
*req
)
147 struct wb_gettoken_state
*state
= tevent_req_data(
148 req
, struct wb_gettoken_state
);
149 struct winbindd_domain
*domain
= NULL
;
150 struct tevent_req
*subreq
= NULL
;
152 if (!state
->expand_local_aliases
) {
153 D_DEBUG("Done. Not asked to expand local aliases.\n");
154 tevent_req_done(req
);
159 * Expand our domain's aliases
161 domain
= find_domain_from_sid_noinit(get_global_sam_sid());
162 if (domain
== NULL
) {
163 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
167 D_DEBUG("Expand domain's aliases for %"PRIu32
" SID(s).\n",
169 subreq
= wb_lookupuseraliases_send(state
, state
->ev
, domain
,
170 state
->num_sids
, state
->sids
);
171 if (tevent_req_nomem(subreq
, req
)) {
174 tevent_req_set_callback(subreq
, wb_gettoken_gotlocalgroups
, req
);
177 static void wb_gettoken_gotlocalgroups(struct tevent_req
*subreq
)
179 struct tevent_req
*req
= tevent_req_callback_data(
180 subreq
, struct tevent_req
);
181 struct wb_gettoken_state
*state
= tevent_req_data(
182 req
, struct wb_gettoken_state
);
187 status
= wb_lookupuseraliases_recv(subreq
, state
, &num_rids
, &rids
);
189 if (tevent_req_nterror(req
, status
)) {
193 D_DEBUG("Got %"PRIu32
" RID(s).\n", num_rids
);
194 status
= wb_add_rids_to_sids(state
, &state
->num_sids
, &state
->sids
,
195 get_global_sam_sid(), num_rids
, rids
);
196 if (tevent_req_nterror(req
, status
)) {
201 wb_gettoken_trybuiltins(req
);
204 static void wb_gettoken_trybuiltins(struct tevent_req
*req
)
206 struct wb_gettoken_state
*state
= tevent_req_data(
207 req
, struct wb_gettoken_state
);
208 struct winbindd_domain
*domain
= NULL
;
209 struct tevent_req
*subreq
= NULL
;
212 * Now expand the builtin groups
215 D_DEBUG("Expand the builtin groups for %"PRIu32
" SID(s).\n",
217 domain
= find_domain_from_sid(&global_sid_Builtin
);
218 if (domain
== NULL
) {
219 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
223 subreq
= wb_lookupuseraliases_send(state
, state
->ev
, domain
,
224 state
->num_sids
, state
->sids
);
225 if (tevent_req_nomem(subreq
, req
)) {
228 tevent_req_set_callback(subreq
, wb_gettoken_gotbuiltins
, req
);
231 static void wb_gettoken_gotbuiltins(struct tevent_req
*subreq
)
233 struct tevent_req
*req
= tevent_req_callback_data(
234 subreq
, struct tevent_req
);
235 struct wb_gettoken_state
*state
= tevent_req_data(
236 req
, struct wb_gettoken_state
);
241 status
= wb_lookupuseraliases_recv(subreq
, state
, &num_rids
, &rids
);
243 if (tevent_req_nterror(req
, status
)) {
246 D_DEBUG("Got %"PRIu32
" RID(s).\n", num_rids
);
247 status
= wb_add_rids_to_sids(state
, &state
->num_sids
, &state
->sids
,
248 &global_sid_Builtin
, num_rids
, rids
);
249 if (tevent_req_nterror(req
, status
)) {
252 tevent_req_done(req
);
255 NTSTATUS
wb_gettoken_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
256 uint32_t *num_sids
, struct dom_sid
**sids
)
258 struct wb_gettoken_state
*state
= tevent_req_data(
259 req
, struct wb_gettoken_state
);
263 if (tevent_req_is_nterror(req
, &status
)) {
266 *num_sids
= state
->num_sids
;
267 D_INFO("WB command gettoken end.\nReceived %"PRIu32
" SID(s).\n",
270 if (CHECK_DEBUGLVL(DBGLVL_INFO
)) {
271 for (i
= 0; i
< state
->num_sids
; i
++) {
272 struct dom_sid_buf sidbuf
;
273 D_INFO("%"PRIu32
": %s\n",
275 dom_sid_str_buf(&state
->sids
[i
],
280 *sids
= talloc_move(mem_ctx
, &state
->sids
);
284 static NTSTATUS
wb_add_rids_to_sids(TALLOC_CTX
*mem_ctx
,
286 struct dom_sid
**psids
,
287 const struct dom_sid
*domain_sid
,
288 uint32_t num_rids
, uint32_t *rids
)
292 D_DEBUG("%"PRIu32
" SID(s) will be uniquely added to the SID array.\n"
293 "Before the addition the array has %"PRIu32
" SID(s).\n",
294 num_rids
, *pnum_sids
);
296 for (i
= 0; i
< num_rids
; i
++) {
300 sid_compose(&sid
, domain_sid
, rids
[i
]);
301 status
= add_sid_to_array_unique(
302 mem_ctx
, &sid
, psids
, pnum_sids
);
303 if (!NT_STATUS_IS_OK(status
)) {
307 D_DEBUG("After the addition the array has %"PRIu32
" SID(s).\n",