smb2_server: add and use a function that calculated the remaining channels
[gd/samba-autobuild/.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         DBG_NOTICE("[%s (%u)] getgrgid %d\n",
53                    cli->client_name,
54                    (unsigned int)cli->pid,
55                    (int)request->data.gid);
56
57         state->xid = (struct unixid) {
58                 .id = request->data.uid, .type = ID_TYPE_GID };
59
60         subreq = wb_xids2sids_send(state, ev, &state->xid, 1);
61         if (tevent_req_nomem(subreq, req)) {
62                 return tevent_req_post(req, ev);
63         }
64         tevent_req_set_callback(subreq, winbindd_getgrgid_gid2sid_done,
65                                 req);
66         return req;
67 }
68
69 static void winbindd_getgrgid_gid2sid_done(struct tevent_req *subreq)
70 {
71         struct tevent_req *req = tevent_req_callback_data(
72                 subreq, struct tevent_req);
73         struct winbindd_getgrgid_state *state = tevent_req_data(
74                 req, struct winbindd_getgrgid_state);
75         NTSTATUS status;
76
77         status = wb_xids2sids_recv(subreq, state, &state->sid);
78         TALLOC_FREE(subreq);
79         if (tevent_req_nterror(req, status)) {
80                 return;
81         }
82         if (is_null_sid(state->sid)) {
83                 tevent_req_nterror(req, NT_STATUS_NO_SUCH_GROUP);
84                 return;
85         }
86
87         subreq = wb_getgrsid_send(state, state->ev, state->sid,
88                                   lp_winbind_expand_groups());
89         if (tevent_req_nomem(subreq, req)) {
90                 return;
91         }
92         tevent_req_set_callback(subreq, winbindd_getgrgid_done, req);
93 }
94
95 static void winbindd_getgrgid_done(struct tevent_req *subreq)
96 {
97         struct tevent_req *req = tevent_req_callback_data(
98                 subreq, struct tevent_req);
99         struct winbindd_getgrgid_state *state = tevent_req_data(
100                 req, struct winbindd_getgrgid_state);
101         NTSTATUS status;
102
103         status = wb_getgrsid_recv(subreq, state, &state->domname, &state->name,
104                                   &state->gid, &state->members);
105         TALLOC_FREE(subreq);
106         if (tevent_req_nterror(req, status)) {
107                 return;
108         }
109         tevent_req_done(req);
110 }
111
112 NTSTATUS winbindd_getgrgid_recv(struct tevent_req *req,
113                                 struct winbindd_response *response)
114 {
115         struct winbindd_getgrgid_state *state = tevent_req_data(
116                 req, struct winbindd_getgrgid_state);
117         NTSTATUS status;
118         int num_members;
119         char *buf;
120
121         if (tevent_req_is_nterror(req, &status)) {
122                 struct dom_sid_buf sidbuf;
123                 DEBUG(5, ("Could not convert sid %s: %s\n",
124                           dom_sid_str_buf(state->sid, &sidbuf),
125                           nt_errstr(status)));
126                 return status;
127         }
128
129         if (!fill_grent(talloc_tos(), &response->data.gr, state->domname,
130                         state->name, state->gid)) {
131                 DEBUG(5, ("fill_grent failed\n"));
132                 return NT_STATUS_NO_MEMORY;
133         }
134
135         status = winbindd_print_groupmembers(state->members, response,
136                                              &num_members, &buf);
137         if (!NT_STATUS_IS_OK(status)) {
138                 return status;
139         }
140
141         response->data.gr.num_gr_mem = (uint32_t)num_members;
142
143         /* Group membership lives at start of extra data */
144
145         response->data.gr.gr_mem_ofs = 0;
146         response->extra_data.data = buf;
147         response->length += talloc_get_size(response->extra_data.data);
148
149         return NT_STATUS_OK;
150 }