winbind: Remove the use of "talloc_dict"
[samba.git] / source3 / winbindd / winbindd_getgrgid.c
1 /*
2    Unix SMB/CIFS implementation.
3    async implementation of WINBINDD_GETGRGID
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 "libcli/security/dom_sid.h"
23
24 struct winbindd_getgrgid_state {
25         struct tevent_context *ev;
26         struct unixid xid;
27         struct dom_sid *sid;
28         const char *domname;
29         const char *name;
30         gid_t gid;
31         struct db_context *members;
32 };
33
34 static void winbindd_getgrgid_gid2sid_done(struct tevent_req *subreq);
35 static void winbindd_getgrgid_done(struct tevent_req *subreq);
36
37 struct tevent_req *winbindd_getgrgid_send(TALLOC_CTX *mem_ctx,
38                                           struct tevent_context *ev,
39                                           struct winbindd_cli_state *cli,
40                                           struct winbindd_request *request)
41 {
42         struct tevent_req *req, *subreq;
43         struct winbindd_getgrgid_state *state;
44
45         req = tevent_req_create(mem_ctx, &state,
46                                 struct winbindd_getgrgid_state);
47         if (req == NULL) {
48                 return NULL;
49         }
50         state->ev = ev;
51
52         DEBUG(3, ("getgrgid %d\n", (int)request->data.gid));
53
54         state->xid = (struct unixid) {
55                 .id = request->data.uid, .type = ID_TYPE_GID };
56
57         subreq = wb_xids2sids_send(state, ev, &state->xid, 1);
58         if (tevent_req_nomem(subreq, req)) {
59                 return tevent_req_post(req, ev);
60         }
61         tevent_req_set_callback(subreq, winbindd_getgrgid_gid2sid_done,
62                                 req);
63         return req;
64 }
65
66 static void winbindd_getgrgid_gid2sid_done(struct tevent_req *subreq)
67 {
68         struct tevent_req *req = tevent_req_callback_data(
69                 subreq, struct tevent_req);
70         struct winbindd_getgrgid_state *state = tevent_req_data(
71                 req, struct winbindd_getgrgid_state);
72         NTSTATUS status;
73
74         status = wb_xids2sids_recv(subreq, state, &state->sid);
75         TALLOC_FREE(subreq);
76         if (tevent_req_nterror(req, status)) {
77                 return;
78         }
79
80         subreq = wb_getgrsid_send(state, state->ev, state->sid,
81                                   lp_winbind_expand_groups());
82         if (tevent_req_nomem(subreq, req)) {
83                 return;
84         }
85         tevent_req_set_callback(subreq, winbindd_getgrgid_done, req);
86 }
87
88 static void winbindd_getgrgid_done(struct tevent_req *subreq)
89 {
90         struct tevent_req *req = tevent_req_callback_data(
91                 subreq, struct tevent_req);
92         struct winbindd_getgrgid_state *state = tevent_req_data(
93                 req, struct winbindd_getgrgid_state);
94         NTSTATUS status;
95
96         status = wb_getgrsid_recv(subreq, state, &state->domname, &state->name,
97                                   &state->gid, &state->members);
98         TALLOC_FREE(subreq);
99         if (tevent_req_nterror(req, status)) {
100                 return;
101         }
102         tevent_req_done(req);
103 }
104
105 NTSTATUS winbindd_getgrgid_recv(struct tevent_req *req,
106                                 struct winbindd_response *response)
107 {
108         struct winbindd_getgrgid_state *state = tevent_req_data(
109                 req, struct winbindd_getgrgid_state);
110         NTSTATUS status;
111         int num_members;
112         char *buf;
113
114         if (tevent_req_is_nterror(req, &status)) {
115                 DEBUG(5, ("Could not convert sid %s: %s\n",
116                           sid_string_dbg(state->sid), nt_errstr(status)));
117                 return status;
118         }
119
120         if (!fill_grent(talloc_tos(), &response->data.gr, state->domname,
121                         state->name, state->gid)) {
122                 DEBUG(5, ("fill_grent failed\n"));
123                 return NT_STATUS_NO_MEMORY;
124         }
125
126         status = winbindd_print_groupmembers(state->members, response,
127                                              &num_members, &buf);
128         if (!NT_STATUS_IS_OK(status)) {
129                 return status;
130         }
131
132         response->data.gr.num_gr_mem = (uint32_t)num_members;
133
134         /* Group membership lives at start of extra data */
135
136         response->data.gr.gr_mem_ofs = 0;
137         response->extra_data.data = buf;
138         response->length += talloc_get_size(response->extra_data.data);
139
140         return NT_STATUS_OK;
141 }