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