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/>.
22 #include "librpc/gen_ndr/ndr_winbind_c.h"
23 #include "../libcli/security/security.h"
24 #include "lib/dbwrap/dbwrap_rbt.h"
26 struct wb_getgrsid_state {
27 struct tevent_context *ev;
32 enum lsa_SidType type;
34 struct db_context *members;
37 static void wb_getgrsid_lookupsid_done(struct tevent_req *subreq);
38 static void wb_getgrsid_sid2gid_done(struct tevent_req *subreq);
39 static void wb_getgrsid_got_members(struct tevent_req *subreq);
41 struct tevent_req *wb_getgrsid_send(TALLOC_CTX *mem_ctx,
42 struct tevent_context *ev,
43 const struct dom_sid *group_sid,
46 struct tevent_req *req, *subreq;
47 struct wb_getgrsid_state *state;
49 req = tevent_req_create(mem_ctx, &state, struct wb_getgrsid_state);
53 sid_copy(&state->sid, group_sid);
55 state->max_nesting = max_nesting;
57 if (dom_sid_in_domain(&global_sid_Unix_Groups, group_sid)) {
58 /* unmapped Unix groups must be resolved locally */
59 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
60 return tevent_req_post(req, ev);
63 subreq = wb_lookupsid_send(state, ev, &state->sid);
64 if (tevent_req_nomem(subreq, req)) {
65 return tevent_req_post(req, ev);
67 tevent_req_set_callback(subreq, wb_getgrsid_lookupsid_done, req);
71 static void wb_getgrsid_lookupsid_done(struct tevent_req *subreq)
73 struct tevent_req *req = tevent_req_callback_data(
74 subreq, struct tevent_req);
75 struct wb_getgrsid_state *state = tevent_req_data(
76 req, struct wb_getgrsid_state);
79 status = wb_lookupsid_recv(subreq, state, &state->type,
80 &state->domname, &state->name);
82 if (tevent_req_nterror(req, status)) {
86 switch (state->type) {
87 case SID_NAME_DOM_GRP:
89 case SID_NAME_WKN_GRP:
91 * also treat user-type SIDS (they might map to ID_TYPE_BOTH)
94 case SID_NAME_COMPUTER:
97 tevent_req_nterror(req, NT_STATUS_NO_SUCH_GROUP);
101 subreq = wb_sids2xids_send(state, state->ev, &state->sid, 1);
102 if (tevent_req_nomem(subreq, req)) {
105 tevent_req_set_callback(subreq, wb_getgrsid_sid2gid_done, req);
108 static void wb_getgrsid_sid2gid_done(struct tevent_req *subreq)
110 struct tevent_req *req = tevent_req_callback_data(
111 subreq, struct tevent_req);
112 struct wb_getgrsid_state *state = tevent_req_data(
113 req, struct wb_getgrsid_state);
115 struct unixid xids[1];
117 status = wb_sids2xids_recv(subreq, xids, ARRAY_SIZE(xids));
119 if (tevent_req_nterror(req, status)) {
124 * We are filtering further down in sids2xids, but that filtering
125 * depends on the actual type of the sid handed in (as determined
126 * by lookupsids). Here we need to filter for the type of object
127 * actually requested, in this case uid.
129 if (!(xids[0].type == ID_TYPE_GID || xids[0].type == ID_TYPE_BOTH)) {
130 tevent_req_nterror(req, NT_STATUS_NONE_MAPPED);
134 state->gid = (gid_t)xids[0].id;
136 if (state->type == SID_NAME_USER || state->type == SID_NAME_COMPUTER) {
138 * special treatment for a user sid that is
139 * mapped to ID_TYPE_BOTH:
140 * create a group with the sid/xid as only member
144 if (xids[0].type != ID_TYPE_BOTH) {
145 tevent_req_nterror(req, NT_STATUS_NO_SUCH_GROUP);
149 state->members = db_open_rbt(state);
150 if (tevent_req_nomem(state->members, req)) {
154 name = fill_domain_username_talloc(talloc_tos(),
157 true /* can_assume */);
158 if (tevent_req_nomem(name, req)) {
162 status = add_member_to_db(state->members, &state->sid, name);
163 if (!NT_STATUS_IS_OK(status)) {
164 tevent_req_nterror(req, status);
168 tevent_req_done(req);
173 * the "regular" case of a group type sid.
176 subreq = wb_group_members_send(state, state->ev, &state->sid,
177 state->type, state->max_nesting);
178 if (tevent_req_nomem(subreq, req)) {
181 tevent_req_set_callback(subreq, wb_getgrsid_got_members, req);
184 static void wb_getgrsid_got_members(struct tevent_req *subreq)
186 struct tevent_req *req = tevent_req_callback_data(
187 subreq, struct tevent_req);
188 struct wb_getgrsid_state *state = tevent_req_data(
189 req, struct wb_getgrsid_state);
192 status = wb_group_members_recv(subreq, state, &state->members);
194 if (tevent_req_nterror(req, status)) {
197 tevent_req_done(req);
200 NTSTATUS wb_getgrsid_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
201 const char **domname, const char **name, gid_t *gid,
202 struct db_context **members)
204 struct wb_getgrsid_state *state = tevent_req_data(
205 req, struct wb_getgrsid_state);
208 if (tevent_req_is_nterror(req, &status)) {
211 *domname = talloc_move(mem_ctx, &state->domname);
212 *name = talloc_move(mem_ctx, &state->name);
214 *members = talloc_move(mem_ctx, &state->members);