s3:pylibsmb: Make .list() work for SMBv2
[samba.git] / source3 / winbindd / wb_getgrsid.c
1 /*
2    Unix SMB/CIFS implementation.
3    async getgrsid
4    Copyright (C) Volker Lendecke 2009
5
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.
10
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.
15
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/>.
18 */
19
20 #include "includes.h"
21 #include "winbindd.h"
22 #include "librpc/gen_ndr/ndr_winbind_c.h"
23 #include "../libcli/security/security.h"
24 #include "lib/dbwrap/dbwrap_rbt.h"
25
26 struct wb_getgrsid_state {
27         struct tevent_context *ev;
28         struct dom_sid sid;
29         int max_nesting;
30         const char *domname;
31         const char *name;
32         enum lsa_SidType type;
33         gid_t gid;
34         struct db_context *members;
35 };
36
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);
40
41 struct tevent_req *wb_getgrsid_send(TALLOC_CTX *mem_ctx,
42                                     struct tevent_context *ev,
43                                     const struct dom_sid *group_sid,
44                                     int max_nesting)
45 {
46         struct tevent_req *req, *subreq;
47         struct wb_getgrsid_state *state;
48
49         req = tevent_req_create(mem_ctx, &state, struct wb_getgrsid_state);
50         if (req == NULL) {
51                 return NULL;
52         }
53         sid_copy(&state->sid, group_sid);
54         state->ev = ev;
55         state->max_nesting = max_nesting;
56
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);
61         }
62
63         subreq = wb_lookupsid_send(state, ev, &state->sid);
64         if (tevent_req_nomem(subreq, req)) {
65                 return tevent_req_post(req, ev);
66         }
67         tevent_req_set_callback(subreq, wb_getgrsid_lookupsid_done, req);
68         return req;
69 }
70
71 static void wb_getgrsid_lookupsid_done(struct tevent_req *subreq)
72 {
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);
77         NTSTATUS status;
78
79         status = wb_lookupsid_recv(subreq, state, &state->type,
80                                    &state->domname, &state->name);
81         TALLOC_FREE(subreq);
82         if (tevent_req_nterror(req, status)) {
83                 return;
84         }
85
86         switch (state->type) {
87         case SID_NAME_DOM_GRP:
88         case SID_NAME_ALIAS:
89         case SID_NAME_WKN_GRP:
90         /*
91          * also treat user-type SIDS (they might map to ID_TYPE_BOTH)
92          */
93         case SID_NAME_USER:
94         case SID_NAME_COMPUTER:
95                 break;
96         default:
97                 tevent_req_nterror(req, NT_STATUS_NO_SUCH_GROUP);
98                 return;
99         }
100
101         subreq = wb_sids2xids_send(state, state->ev, &state->sid, 1);
102         if (tevent_req_nomem(subreq, req)) {
103                 return;
104         }
105         tevent_req_set_callback(subreq, wb_getgrsid_sid2gid_done, req);
106 }
107
108 static void wb_getgrsid_sid2gid_done(struct tevent_req *subreq)
109 {
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);
114         NTSTATUS status;
115         struct unixid xids[1];
116
117         status = wb_sids2xids_recv(subreq, xids, ARRAY_SIZE(xids));
118         TALLOC_FREE(subreq);
119         if (tevent_req_nterror(req, status)) {
120                 return;
121         }
122
123         /*
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.
128          */
129         if (!(xids[0].type == ID_TYPE_GID || xids[0].type == ID_TYPE_BOTH)) {
130                 tevent_req_nterror(req, NT_STATUS_NONE_MAPPED);
131                 return;
132         }
133
134         state->gid = (gid_t)xids[0].id;
135
136         if (state->type == SID_NAME_USER || state->type == SID_NAME_COMPUTER) {
137                 /*
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
141                  */
142                 const char *name;
143
144                 if (xids[0].type != ID_TYPE_BOTH) {
145                         tevent_req_nterror(req, NT_STATUS_NO_SUCH_GROUP);
146                         return;
147                 }
148
149                 state->members = db_open_rbt(state);
150                 if (tevent_req_nomem(state->members, req)) {
151                         return;
152                 }
153
154                 name = fill_domain_username_talloc(talloc_tos(),
155                                                    state->domname,
156                                                    state->name,
157                                                    true /* can_assume */);
158                 if (tevent_req_nomem(name, req)) {
159                         return;
160                 }
161
162                 status = add_member_to_db(state->members, &state->sid, name);
163                 if (!NT_STATUS_IS_OK(status)) {
164                         tevent_req_nterror(req, status);
165                         return;
166                 }
167
168                 tevent_req_done(req);
169                 return;
170         }
171
172         /*
173          * the "regular" case of a group type sid.
174          */
175
176         subreq = wb_group_members_send(state, state->ev, &state->sid,
177                                        state->type, state->max_nesting);
178         if (tevent_req_nomem(subreq, req)) {
179                 return;
180         }
181         tevent_req_set_callback(subreq, wb_getgrsid_got_members, req);
182 }
183
184 static void wb_getgrsid_got_members(struct tevent_req *subreq)
185 {
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);
190         NTSTATUS status;
191
192         status = wb_group_members_recv(subreq, state, &state->members);
193         TALLOC_FREE(subreq);
194         if (tevent_req_nterror(req, status)) {
195                 return;
196         }
197         tevent_req_done(req);
198 }
199
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)
203 {
204         struct wb_getgrsid_state *state = tevent_req_data(
205                 req, struct wb_getgrsid_state);
206         NTSTATUS status;
207
208         if (tevent_req_is_nterror(req, &status)) {
209                 return status;
210         }
211         *domname = talloc_move(mem_ctx, &state->domname);
212         *name = talloc_move(mem_ctx, &state->name);
213         *gid = state->gid;
214         *members = talloc_move(mem_ctx, &state->members);
215         return NT_STATUS_OK;
216 }