s3: Lift the server_messaging_context from notify_job_status
[Samba/gbeck.git] / source3 / winbindd / winbindd_getgroups.c
blob736eba698a251e2a3c7b531401503d132859112b
1 /*
2 Unix SMB/CIFS implementation.
3 async implementation of WINBINDD_GETGROUPS
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/>.
20 #include "includes.h"
21 #include "winbindd.h"
23 struct winbindd_getgroups_state {
24 struct tevent_context *ev;
25 fstring domname;
26 fstring username;
27 struct dom_sid sid;
28 enum lsa_SidType type;
29 int num_sids;
30 struct dom_sid *sids;
31 int next_sid;
32 int num_gids;
33 gid_t *gids;
36 static void winbindd_getgroups_lookupname_done(struct tevent_req *subreq);
37 static void winbindd_getgroups_gettoken_done(struct tevent_req *subreq);
38 static void winbindd_getgroups_sid2gid_done(struct tevent_req *subreq);
40 struct tevent_req *winbindd_getgroups_send(TALLOC_CTX *mem_ctx,
41 struct tevent_context *ev,
42 struct winbindd_cli_state *cli,
43 struct winbindd_request *request)
45 struct tevent_req *req, *subreq;
46 struct winbindd_getgroups_state *state;
47 char *domuser, *mapped_user;
48 NTSTATUS status;
50 req = tevent_req_create(mem_ctx, &state,
51 struct winbindd_getgroups_state);
52 if (req == NULL) {
53 return NULL;
55 state->ev = ev;
57 /* Ensure null termination */
58 request->data.username[sizeof(request->data.username)-1]='\0';
60 DEBUG(3, ("getgroups %s\n", request->data.username));
62 domuser = request->data.username;
64 status = normalize_name_unmap(state, domuser, &mapped_user);
66 if (NT_STATUS_IS_OK(status)
67 || NT_STATUS_EQUAL(status, NT_STATUS_FILE_RENAMED)) {
68 /* normalize_name_unmapped did something */
69 domuser = mapped_user;
72 if (!parse_domain_user(domuser, state->domname, state->username)) {
73 DEBUG(5, ("Could not parse domain user: %s\n", domuser));
74 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
75 return tevent_req_post(req, ev);
78 subreq = wb_lookupname_send(state, ev, state->domname, state->username,
79 LOOKUP_NAME_NO_NSS);
80 if (tevent_req_nomem(subreq, req)) {
81 return tevent_req_post(req, ev);
83 tevent_req_set_callback(subreq, winbindd_getgroups_lookupname_done,
84 req);
85 return req;
88 static void winbindd_getgroups_lookupname_done(struct tevent_req *subreq)
90 struct tevent_req *req = tevent_req_callback_data(
91 subreq, struct tevent_req);
92 struct winbindd_getgroups_state *state = tevent_req_data(
93 req, struct winbindd_getgroups_state);
94 NTSTATUS status;
96 status = wb_lookupname_recv(subreq, &state->sid, &state->type);
97 TALLOC_FREE(subreq);
98 if (!NT_STATUS_IS_OK(status)) {
99 tevent_req_nterror(req, status);
100 return;
103 subreq = wb_gettoken_send(state, state->ev, &state->sid);
104 if (tevent_req_nomem(subreq, req)) {
105 return;
107 tevent_req_set_callback(subreq, winbindd_getgroups_gettoken_done, req);
110 static void winbindd_getgroups_gettoken_done(struct tevent_req *subreq)
112 struct tevent_req *req = tevent_req_callback_data(
113 subreq, struct tevent_req);
114 struct winbindd_getgroups_state *state = tevent_req_data(
115 req, struct winbindd_getgroups_state);
116 NTSTATUS status;
118 status = wb_gettoken_recv(subreq, state, &state->num_sids,
119 &state->sids);
120 TALLOC_FREE(subreq);
121 if (!NT_STATUS_IS_OK(status)) {
122 tevent_req_nterror(req, status);
123 return;
127 * Convert the group SIDs to gids. state->sids[0] contains the user
128 * sid, so start at index 1.
131 state->gids = talloc_array(state, gid_t, state->num_sids-1);
132 if (tevent_req_nomem(state->gids, req)) {
133 return;
135 state->num_gids = 0;
136 state->next_sid = 1;
138 subreq = wb_sid2gid_send(state, state->ev,
139 &state->sids[state->next_sid]);
140 if (tevent_req_nomem(subreq, req)) {
141 return;
143 tevent_req_set_callback(subreq, winbindd_getgroups_sid2gid_done, req);
146 static void winbindd_getgroups_sid2gid_done(struct tevent_req *subreq)
148 struct tevent_req *req = tevent_req_callback_data(
149 subreq, struct tevent_req);
150 struct winbindd_getgroups_state *state = tevent_req_data(
151 req, struct winbindd_getgroups_state);
152 NTSTATUS status;
154 status = wb_sid2gid_recv(subreq, &state->gids[state->num_gids]);
155 TALLOC_FREE(subreq);
158 * In case of failure, just continue with the next gid
160 if (NT_STATUS_IS_OK(status)) {
161 state->num_gids += 1;
163 state->next_sid += 1;
165 if (state->next_sid >= state->num_sids) {
166 tevent_req_done(req);
167 return;
170 subreq = wb_sid2gid_send(state, state->ev,
171 &state->sids[state->next_sid]);
172 if (tevent_req_nomem(subreq, req)) {
173 return;
175 tevent_req_set_callback(subreq, winbindd_getgroups_sid2gid_done, req);
178 NTSTATUS winbindd_getgroups_recv(struct tevent_req *req,
179 struct winbindd_response *response)
181 struct winbindd_getgroups_state *state = tevent_req_data(
182 req, struct winbindd_getgroups_state);
183 NTSTATUS status;
185 if (tevent_req_is_nterror(req, &status)) {
186 DEBUG(5, ("Could not convert sid %s: %s\n",
187 sid_string_dbg(&state->sid), nt_errstr(status)));
188 return status;
191 response->data.num_entries = state->num_gids;
193 if (state->num_gids > 0) {
194 response->extra_data.data = talloc_move(response,
195 &state->gids);
196 response->length += state->num_gids * sizeof(gid_t);
198 return NT_STATUS_OK;