WHATSNEW: Add release notes for Samba 4.9.17.
[Samba.git] / source3 / winbindd / winbindd_getgrnam.c
blob37c205ddba4dd6b8b078e845b0a205aa275cc540
1 /*
2 Unix SMB/CIFS implementation.
3 async implementation of WINBINDD_GETGRNAM
4 Copyright (C) Volker Lendecke 2009
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.
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.
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/>.
20 #include "includes.h"
21 #include "winbindd.h"
23 struct winbindd_getgrnam_state {
24 struct tevent_context *ev;
25 fstring name_namespace, name_domain, name_group;
26 struct dom_sid sid;
27 const char *domname;
28 const char *name;
29 gid_t gid;
30 struct db_context *members;
33 static void winbindd_getgrnam_lookupname_done(struct tevent_req *subreq);
34 static void winbindd_getgrnam_done(struct tevent_req *subreq);
36 struct tevent_req *winbindd_getgrnam_send(TALLOC_CTX *mem_ctx,
37 struct tevent_context *ev,
38 struct winbindd_cli_state *cli,
39 struct winbindd_request *request)
41 struct tevent_req *req, *subreq;
42 struct winbindd_getgrnam_state *state;
43 char *tmp;
44 NTSTATUS nt_status;
45 bool ok;
47 req = tevent_req_create(mem_ctx, &state,
48 struct winbindd_getgrnam_state);
49 if (req == NULL) {
50 return NULL;
52 state->ev = ev;
54 /* Ensure null termination */
55 request->data.groupname[sizeof(request->data.groupname)-1]='\0';
57 DEBUG(3, ("getgrnam %s\n", request->data.groupname));
59 nt_status = normalize_name_unmap(state, request->data.groupname, &tmp);
60 /* If we didn't map anything in the above call, just reset the
61 tmp pointer to the original string */
62 if (!NT_STATUS_IS_OK(nt_status) &&
63 !NT_STATUS_EQUAL(nt_status, NT_STATUS_FILE_RENAMED))
65 tmp = request->data.groupname;
68 /* Parse domain and groupname */
70 ok = parse_domain_user(tmp,
71 state->name_namespace,
72 state->name_domain,
73 state->name_group);
74 if (!ok) {
75 DBG_INFO("Could not parse domain user: %s\n", tmp);
76 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
77 return tevent_req_post(req, ev);
80 /* if no domain or our local domain and no local tdb group, default to
81 * our local domain for aliases */
83 if ( !*(state->name_domain) || strequal(state->name_domain,
84 get_global_sam_name()) ) {
85 fstrcpy(state->name_domain, get_global_sam_name());
88 subreq = wb_lookupname_send(state, ev,
89 state->name_namespace,
90 state->name_domain,
91 state->name_group,
92 0);
93 if (tevent_req_nomem(subreq, req)) {
94 return tevent_req_post(req, ev);
96 tevent_req_set_callback(subreq, winbindd_getgrnam_lookupname_done,
97 req);
98 return req;
101 static void winbindd_getgrnam_lookupname_done(struct tevent_req *subreq)
103 struct tevent_req *req = tevent_req_callback_data(
104 subreq, struct tevent_req);
105 struct winbindd_getgrnam_state *state = tevent_req_data(
106 req, struct winbindd_getgrnam_state);
107 enum lsa_SidType type;
108 NTSTATUS status;
110 status = wb_lookupname_recv(subreq, &state->sid, &type);
111 TALLOC_FREE(subreq);
112 if (tevent_req_nterror(req, status)) {
113 return;
116 switch (type) {
117 case SID_NAME_DOM_GRP:
118 case SID_NAME_ALIAS:
119 case SID_NAME_WKN_GRP:
121 * Also give user types a chance:
122 * These might be user sids mapped to the ID_TYPE_BOTH,
123 * and in that case we should construct a group struct.
125 case SID_NAME_USER:
126 case SID_NAME_COMPUTER:
127 break;
128 default:
129 tevent_req_nterror(req, NT_STATUS_NO_SUCH_GROUP);
130 return;
133 subreq = wb_getgrsid_send(state, state->ev, &state->sid,
134 lp_winbind_expand_groups());
135 if (tevent_req_nomem(subreq, req)) {
136 return;
138 tevent_req_set_callback(subreq, winbindd_getgrnam_done, req);
141 static void winbindd_getgrnam_done(struct tevent_req *subreq)
143 struct tevent_req *req = tevent_req_callback_data(
144 subreq, struct tevent_req);
145 struct winbindd_getgrnam_state *state = tevent_req_data(
146 req, struct winbindd_getgrnam_state);
147 NTSTATUS status;
149 status = wb_getgrsid_recv(subreq, state, &state->domname, &state->name,
150 &state->gid, &state->members);
151 TALLOC_FREE(subreq);
152 if (tevent_req_nterror(req, status)) {
153 return;
155 tevent_req_done(req);
158 NTSTATUS winbindd_getgrnam_recv(struct tevent_req *req,
159 struct winbindd_response *response)
161 struct winbindd_getgrnam_state *state = tevent_req_data(
162 req, struct winbindd_getgrnam_state);
163 NTSTATUS status;
164 int num_members;
165 char *buf;
167 if (tevent_req_is_nterror(req, &status)) {
168 DEBUG(5, ("Could not convert sid %s: %s\n",
169 sid_string_dbg(&state->sid), nt_errstr(status)));
170 return status;
173 if (!fill_grent(talloc_tos(), &response->data.gr, state->domname,
174 state->name, state->gid)) {
175 DEBUG(5, ("fill_grent failed\n"));
176 return NT_STATUS_NO_MEMORY;
179 status = winbindd_print_groupmembers(state->members, response,
180 &num_members, &buf);
181 if (!NT_STATUS_IS_OK(status)) {
182 return status;
185 response->data.gr.num_gr_mem = (uint32_t)num_members;
187 /* Group membership lives at start of extra data */
189 response->data.gr.gr_mem_ofs = 0;
190 response->extra_data.data = buf;
191 response->length += talloc_get_size(response->extra_data.data);
193 return NT_STATUS_OK;