s3-smbldap: use smbldap_ prefix for all functions
[gd/samba-autobuild/.git] / source3 / winbindd / winbindd_getgroups.c
1 /*
2    Unix SMB/CIFS implementation.
3    async implementation of WINBINDD_GETGROUPS
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 "passdb/lookup_sid.h" /* only for LOOKUP_NAME_NO_NSS flag */
23
24 struct winbindd_getgroups_state {
25         struct tevent_context *ev;
26         fstring domname;
27         fstring username;
28         struct dom_sid sid;
29         enum lsa_SidType type;
30         int num_sids;
31         struct dom_sid *sids;
32         int next_sid;
33         int num_gids;
34         gid_t *gids;
35 };
36
37 static void winbindd_getgroups_lookupname_done(struct tevent_req *subreq);
38 static void winbindd_getgroups_gettoken_done(struct tevent_req *subreq);
39 static void winbindd_getgroups_sid2gid_done(struct tevent_req *subreq);
40
41 struct tevent_req *winbindd_getgroups_send(TALLOC_CTX *mem_ctx,
42                                            struct tevent_context *ev,
43                                            struct winbindd_cli_state *cli,
44                                            struct winbindd_request *request)
45 {
46         struct tevent_req *req, *subreq;
47         struct winbindd_getgroups_state *state;
48         char *domuser, *mapped_user;
49         NTSTATUS status;
50
51         req = tevent_req_create(mem_ctx, &state,
52                                 struct winbindd_getgroups_state);
53         if (req == NULL) {
54                 return NULL;
55         }
56         state->ev = ev;
57
58         /* Ensure null termination */
59         request->data.username[sizeof(request->data.username)-1]='\0';
60
61         DEBUG(3, ("getgroups %s\n", request->data.username));
62
63         domuser = request->data.username;
64
65         status = normalize_name_unmap(state, domuser, &mapped_user);
66
67         if (NT_STATUS_IS_OK(status)
68             || NT_STATUS_EQUAL(status, NT_STATUS_FILE_RENAMED)) {
69                 /* normalize_name_unmapped did something */
70                 domuser = mapped_user;
71         }
72
73         if (!parse_domain_user(domuser, state->domname, state->username)) {
74                 DEBUG(5, ("Could not parse domain user: %s\n", domuser));
75                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
76                 return tevent_req_post(req, ev);
77         }
78
79         subreq = wb_lookupname_send(state, ev, state->domname, state->username,
80                                     LOOKUP_NAME_NO_NSS);
81         if (tevent_req_nomem(subreq, req)) {
82                 return tevent_req_post(req, ev);
83         }
84         tevent_req_set_callback(subreq, winbindd_getgroups_lookupname_done,
85                                 req);
86         return req;
87 }
88
89 static void winbindd_getgroups_lookupname_done(struct tevent_req *subreq)
90 {
91         struct tevent_req *req = tevent_req_callback_data(
92                 subreq, struct tevent_req);
93         struct winbindd_getgroups_state *state = tevent_req_data(
94                 req, struct winbindd_getgroups_state);
95         NTSTATUS status;
96
97         status = wb_lookupname_recv(subreq, &state->sid, &state->type);
98         TALLOC_FREE(subreq);
99         if (tevent_req_nterror(req, status)) {
100                 return;
101         }
102
103         subreq = wb_gettoken_send(state, state->ev, &state->sid);
104         if (tevent_req_nomem(subreq, req)) {
105                 return;
106         }
107         tevent_req_set_callback(subreq, winbindd_getgroups_gettoken_done, req);
108 }
109
110 static void winbindd_getgroups_gettoken_done(struct tevent_req *subreq)
111 {
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;
117
118         status = wb_gettoken_recv(subreq, state, &state->num_sids,
119                                   &state->sids);
120         TALLOC_FREE(subreq);
121         if (tevent_req_nterror(req, status)) {
122                 return;
123         }
124
125         /*
126          * Convert the group SIDs to gids. state->sids[0] contains the user
127          * sid, so start at index 1.
128          */
129
130         state->gids = talloc_array(state, gid_t, state->num_sids-1);
131         if (tevent_req_nomem(state->gids, req)) {
132                 return;
133         }
134         state->num_gids = 0;
135         state->next_sid = 1;
136
137         subreq = wb_sid2gid_send(state, state->ev,
138                                  &state->sids[state->next_sid]);
139         if (tevent_req_nomem(subreq, req)) {
140                 return;
141         }
142         tevent_req_set_callback(subreq, winbindd_getgroups_sid2gid_done, req);
143 }
144
145 static void winbindd_getgroups_sid2gid_done(struct tevent_req *subreq)
146 {
147         struct tevent_req *req = tevent_req_callback_data(
148                 subreq, struct tevent_req);
149         struct winbindd_getgroups_state *state = tevent_req_data(
150                 req, struct winbindd_getgroups_state);
151         NTSTATUS status;
152
153         status = wb_sid2gid_recv(subreq, &state->gids[state->num_gids]);
154         TALLOC_FREE(subreq);
155
156         /*
157          * In case of failure, just continue with the next gid
158          */
159         if (NT_STATUS_IS_OK(status)) {
160                 state->num_gids += 1;
161         }
162         state->next_sid += 1;
163
164         if (state->next_sid >= state->num_sids) {
165                 tevent_req_done(req);
166                 return;
167         }
168
169         subreq = wb_sid2gid_send(state, state->ev,
170                                  &state->sids[state->next_sid]);
171         if (tevent_req_nomem(subreq, req)) {
172                 return;
173         }
174         tevent_req_set_callback(subreq, winbindd_getgroups_sid2gid_done, req);
175 }
176
177 NTSTATUS winbindd_getgroups_recv(struct tevent_req *req,
178                                  struct winbindd_response *response)
179 {
180         struct winbindd_getgroups_state *state = tevent_req_data(
181                 req, struct winbindd_getgroups_state);
182         NTSTATUS status;
183
184         if (tevent_req_is_nterror(req, &status)) {
185                 DEBUG(5, ("Could not convert sid %s: %s\n",
186                           sid_string_dbg(&state->sid), nt_errstr(status)));
187                 return status;
188         }
189
190         response->data.num_entries = state->num_gids;
191
192         if (state->num_gids > 0) {
193                 response->extra_data.data = talloc_move(response,
194                                                         &state->gids);
195                 response->length += state->num_gids * sizeof(gid_t);
196         }
197         return NT_STATUS_OK;
198 }