Line data Source code
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 2154 : 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 0 : struct tevent_req *req, *subreq;
43 0 : struct winbindd_getgrgid_state *state;
44 :
45 2154 : req = tevent_req_create(mem_ctx, &state,
46 : struct winbindd_getgrgid_state);
47 2154 : if (req == NULL) {
48 0 : return NULL;
49 : }
50 2154 : state->ev = ev;
51 :
52 2154 : D_NOTICE("[%s (%u)] Winbind external command GETGRGID start.\n"
53 : "gid=%u\n",
54 : cli->client_name,
55 : (unsigned int)cli->pid,
56 : (int)request->data.gid);
57 :
58 2154 : state->xid = (struct unixid) {
59 2154 : .id = request->data.uid, .type = ID_TYPE_GID };
60 :
61 2154 : subreq = wb_xids2sids_send(state, ev, &state->xid, 1);
62 2154 : if (tevent_req_nomem(subreq, req)) {
63 0 : return tevent_req_post(req, ev);
64 : }
65 2154 : tevent_req_set_callback(subreq, winbindd_getgrgid_gid2sid_done,
66 : req);
67 2154 : return req;
68 : }
69 :
70 2154 : static void winbindd_getgrgid_gid2sid_done(struct tevent_req *subreq)
71 : {
72 2154 : struct tevent_req *req = tevent_req_callback_data(
73 : subreq, struct tevent_req);
74 2154 : struct winbindd_getgrgid_state *state = tevent_req_data(
75 : req, struct winbindd_getgrgid_state);
76 0 : NTSTATUS status;
77 :
78 2154 : status = wb_xids2sids_recv(subreq, state, &state->sid);
79 2154 : TALLOC_FREE(subreq);
80 2154 : if (tevent_req_nterror(req, status)) {
81 6 : return;
82 : }
83 2154 : if (is_null_sid(state->sid)) {
84 6 : tevent_req_nterror(req, NT_STATUS_NO_SUCH_GROUP);
85 6 : return;
86 : }
87 :
88 2148 : subreq = wb_getgrsid_send(state, state->ev, state->sid,
89 : lp_winbind_expand_groups());
90 2148 : if (tevent_req_nomem(subreq, req)) {
91 0 : return;
92 : }
93 2148 : tevent_req_set_callback(subreq, winbindd_getgrgid_done, req);
94 : }
95 :
96 2148 : static void winbindd_getgrgid_done(struct tevent_req *subreq)
97 : {
98 2148 : struct tevent_req *req = tevent_req_callback_data(
99 : subreq, struct tevent_req);
100 2148 : struct winbindd_getgrgid_state *state = tevent_req_data(
101 : req, struct winbindd_getgrgid_state);
102 0 : NTSTATUS status;
103 :
104 2148 : status = wb_getgrsid_recv(subreq, state, &state->domname, &state->name,
105 : &state->gid, &state->members);
106 2148 : TALLOC_FREE(subreq);
107 2148 : if (tevent_req_nterror(req, status)) {
108 0 : return;
109 : }
110 2148 : tevent_req_done(req);
111 : }
112 :
113 2154 : NTSTATUS winbindd_getgrgid_recv(struct tevent_req *req,
114 : struct winbindd_response *response)
115 : {
116 2154 : struct winbindd_getgrgid_state *state = tevent_req_data(
117 : req, struct winbindd_getgrgid_state);
118 0 : NTSTATUS status;
119 0 : int num_members;
120 0 : char *buf;
121 :
122 2154 : if (tevent_req_is_nterror(req, &status)) {
123 0 : struct dom_sid_buf sidbuf;
124 6 : D_WARNING("Could not convert sid %s: %s\n",
125 : dom_sid_str_buf(state->sid, &sidbuf),
126 : nt_errstr(status));
127 6 : return status;
128 : }
129 :
130 2148 : if (!fill_grent(talloc_tos(), &response->data.gr, state->domname,
131 : state->name, state->gid)) {
132 0 : D_WARNING("fill_grent failed\n");
133 0 : return NT_STATUS_NO_MEMORY;
134 : }
135 :
136 2148 : status = winbindd_print_groupmembers(state->members, response,
137 : &num_members, &buf);
138 2148 : if (!NT_STATUS_IS_OK(status)) {
139 0 : D_WARNING("Failed with %s.\n", nt_errstr(status));
140 0 : return status;
141 : }
142 :
143 2148 : response->data.gr.num_gr_mem = (uint32_t)num_members;
144 :
145 : /* Group membership lives at start of extra data */
146 :
147 2148 : response->data.gr.gr_mem_ofs = 0;
148 2148 : response->extra_data.data = buf;
149 2148 : response->length += talloc_get_size(response->extra_data.data);
150 :
151 2148 : D_NOTICE("Winbind external command GETGRGID end.\n"
152 : "Returning %"PRIu32" group member(s).\n",
153 : response->data.gr.num_gr_mem);
154 :
155 2148 : return NT_STATUS_OK;
156 : }
|