s3-auth Change type of num_sids to uint32_t
[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/cli_wbint.h"
23
24 struct wb_getgrsid_state {
25         struct tevent_context *ev;
26         struct dom_sid sid;
27         int max_nesting;
28         const char *domname;
29         const char *name;
30         enum lsa_SidType type;
31         gid_t gid;
32         struct talloc_dict *members;
33 };
34
35 static void wb_getgrsid_lookupsid_done(struct tevent_req *subreq);
36 static void wb_getgrsid_sid2gid_done(struct tevent_req *subreq);
37 static void wb_getgrsid_got_members(struct tevent_req *subreq);
38
39 struct tevent_req *wb_getgrsid_send(TALLOC_CTX *mem_ctx,
40                                     struct tevent_context *ev,
41                                     const struct dom_sid *group_sid,
42                                     int max_nesting)
43 {
44         struct tevent_req *req, *subreq;
45         struct wb_getgrsid_state *state;
46
47         req = tevent_req_create(mem_ctx, &state, struct wb_getgrsid_state);
48         if (req == NULL) {
49                 return NULL;
50         }
51         sid_copy(&state->sid, group_sid);
52         state->ev = ev;
53         state->max_nesting = max_nesting;
54
55         if (lp_winbind_trusted_domains_only()) {
56                 struct winbindd_domain *our_domain = find_our_domain();
57
58                 if (sid_compare_domain(group_sid, &our_domain->sid) == 0) {
59                         DEBUG(7, ("winbindd_getgrsid: My domain -- rejecting "
60                                   "getgrsid() for %s\n", sid_string_tos(group_sid)));
61                         tevent_req_nterror(req, NT_STATUS_NO_SUCH_GROUP);
62                         return tevent_req_post(req, ev);
63                 }
64         }
65
66         subreq = wb_lookupsid_send(state, ev, &state->sid);
67         if (tevent_req_nomem(subreq, req)) {
68                 return tevent_req_post(req, ev);
69         }
70         tevent_req_set_callback(subreq, wb_getgrsid_lookupsid_done, req);
71         return req;
72 }
73
74 static void wb_getgrsid_lookupsid_done(struct tevent_req *subreq)
75 {
76         struct tevent_req *req = tevent_req_callback_data(
77                 subreq, struct tevent_req);
78         struct wb_getgrsid_state *state = tevent_req_data(
79                 req, struct wb_getgrsid_state);
80         NTSTATUS status;
81
82         status = wb_lookupsid_recv(subreq, state, &state->type,
83                                    &state->domname, &state->name);
84         TALLOC_FREE(subreq);
85         if (!NT_STATUS_IS_OK(status)) {
86                 tevent_req_nterror(req, status);
87                 return;
88         }
89
90         switch (state->type) {
91         case SID_NAME_DOM_GRP:
92         case SID_NAME_ALIAS:
93         case SID_NAME_WKN_GRP:
94                 break;
95         default:
96                 tevent_req_nterror(req, NT_STATUS_NO_SUCH_GROUP);
97                 return;
98         }
99
100         subreq = wb_sid2gid_send(state, state->ev, &state->sid);
101         if (tevent_req_nomem(subreq, req)) {
102                 return;
103         }
104         tevent_req_set_callback(subreq, wb_getgrsid_sid2gid_done, req);
105 }
106
107 static void wb_getgrsid_sid2gid_done(struct tevent_req *subreq)
108 {
109         struct tevent_req *req = tevent_req_callback_data(
110                 subreq, struct tevent_req);
111         struct wb_getgrsid_state *state = tevent_req_data(
112                 req, struct wb_getgrsid_state);
113         NTSTATUS status;
114
115         status = wb_sid2gid_recv(subreq, &state->gid);
116         TALLOC_FREE(subreq);
117         if (!NT_STATUS_IS_OK(status)) {
118                 tevent_req_nterror(req, status);
119                 return;
120         }
121         subreq = wb_group_members_send(state, state->ev, &state->sid,
122                                        state->type, state->max_nesting);
123         if (tevent_req_nomem(subreq, req)) {
124                 return;
125         }
126         tevent_req_set_callback(subreq, wb_getgrsid_got_members, req);
127 }
128
129 static void wb_getgrsid_got_members(struct tevent_req *subreq)
130 {
131         struct tevent_req *req = tevent_req_callback_data(
132                 subreq, struct tevent_req);
133         struct wb_getgrsid_state *state = tevent_req_data(
134                 req, struct wb_getgrsid_state);
135         NTSTATUS status;
136
137         status = wb_group_members_recv(subreq, state, &state->members);
138         TALLOC_FREE(subreq);
139         if (!NT_STATUS_IS_OK(status)) {
140                 tevent_req_nterror(req, status);
141                 return;
142         }
143         tevent_req_done(req);
144 }
145
146 NTSTATUS wb_getgrsid_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
147                           const char **domname, const char **name, gid_t *gid,
148                           struct talloc_dict **members)
149 {
150         struct wb_getgrsid_state *state = tevent_req_data(
151                 req, struct wb_getgrsid_state);
152         NTSTATUS status;
153
154         if (tevent_req_is_nterror(req, &status)) {
155                 return status;
156         }
157         *domname = talloc_move(mem_ctx, &state->domname);
158         *name = talloc_move(mem_ctx, &state->name);
159         *gid = state->gid;
160         *members = talloc_move(mem_ctx, &state->members);
161         return NT_STATUS_OK;
162 }