s4:rpc_srv:getncchanges: 4.5 anc emulation uses qsort(), not ldb_qsort()
[samba.git] / source3 / winbindd / winbindd_getgrnam.c
blob6b277c2dc9708c1ab7b5e7517cc7cf06f92f0d42
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"
22 #include "libcli/security/dom_sid.h"
23 #include "lib/util/string_wrappers.h"
25 struct winbindd_getgrnam_state {
26 struct tevent_context *ev;
27 char *name_namespace;
28 char *name_domain;
29 char *name_group;
30 struct dom_sid sid;
31 const char *domname;
32 const char *name;
33 gid_t gid;
34 struct db_context *members;
37 static void winbindd_getgrnam_lookupname_done(struct tevent_req *subreq);
38 static void winbindd_getgrnam_done(struct tevent_req *subreq);
40 struct tevent_req *winbindd_getgrnam_send(TALLOC_CTX *mem_ctx,
41 struct tevent_context *ev,
42 struct winbindd_cli_state *cli,
43 struct winbindd_request *request)
45 struct tevent_req *req, *subreq;
46 struct winbindd_getgrnam_state *state;
47 char *tmp;
48 NTSTATUS nt_status;
49 bool ok;
51 req = tevent_req_create(mem_ctx, &state,
52 struct winbindd_getgrnam_state);
53 if (req == NULL) {
54 return NULL;
56 state->ev = ev;
58 /* Ensure null termination */
59 request->data.groupname[sizeof(request->data.groupname)-1]='\0';
61 D_NOTICE("[%s (%u)] Winbind external command GETGRNAM start.\n"
62 "Searching group name '%s'.\n",
63 cli->client_name,
64 (unsigned int)cli->pid,
65 request->data.groupname);
67 nt_status = normalize_name_unmap(state, request->data.groupname, &tmp);
68 /* If we didn't map anything in the above call, just reset the
69 tmp pointer to the original string */
70 if (!NT_STATUS_IS_OK(nt_status) &&
71 !NT_STATUS_EQUAL(nt_status, NT_STATUS_FILE_RENAMED))
73 tmp = request->data.groupname;
76 /* Parse domain and groupname */
78 ok = parse_domain_user(state, tmp,
79 &state->name_namespace,
80 &state->name_domain,
81 &state->name_group);
82 if (!ok) {
83 DBG_INFO("Could not parse domain user: %s\n", tmp);
84 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
85 return tevent_req_post(req, ev);
88 /* if no domain or our local domain and no local tdb group, default to
89 * our local domain for aliases */
91 if ( !*(state->name_domain) || strequal(state->name_domain,
92 get_global_sam_name()) ) {
93 TALLOC_FREE(state->name_domain);
94 state->name_domain = talloc_strdup(state,
95 get_global_sam_name());
96 if (tevent_req_nomem(state->name_domain, req)) {
97 return tevent_req_post(req, ev);
101 subreq = wb_lookupname_send(state, ev,
102 state->name_namespace,
103 state->name_domain,
104 state->name_group,
106 if (tevent_req_nomem(subreq, req)) {
107 return tevent_req_post(req, ev);
109 tevent_req_set_callback(subreq, winbindd_getgrnam_lookupname_done,
110 req);
111 return req;
114 static void winbindd_getgrnam_lookupname_done(struct tevent_req *subreq)
116 struct tevent_req *req = tevent_req_callback_data(
117 subreq, struct tevent_req);
118 struct winbindd_getgrnam_state *state = tevent_req_data(
119 req, struct winbindd_getgrnam_state);
120 enum lsa_SidType type;
121 NTSTATUS status;
123 status = wb_lookupname_recv(subreq, &state->sid, &type);
124 TALLOC_FREE(subreq);
125 if (tevent_req_nterror(req, status)) {
126 return;
129 switch (type) {
130 case SID_NAME_DOM_GRP:
131 case SID_NAME_ALIAS:
132 case SID_NAME_WKN_GRP:
134 * Also give user types a chance:
135 * These might be user sids mapped to the ID_TYPE_BOTH,
136 * and in that case we should construct a group struct.
138 case SID_NAME_USER:
139 case SID_NAME_COMPUTER:
140 break;
141 default:
142 tevent_req_nterror(req, NT_STATUS_NO_SUCH_GROUP);
143 return;
146 subreq = wb_getgrsid_send(state, state->ev, &state->sid,
147 lp_winbind_expand_groups());
148 if (tevent_req_nomem(subreq, req)) {
149 return;
151 tevent_req_set_callback(subreq, winbindd_getgrnam_done, req);
154 static void winbindd_getgrnam_done(struct tevent_req *subreq)
156 struct tevent_req *req = tevent_req_callback_data(
157 subreq, struct tevent_req);
158 struct winbindd_getgrnam_state *state = tevent_req_data(
159 req, struct winbindd_getgrnam_state);
160 NTSTATUS status;
162 status = wb_getgrsid_recv(subreq, state, &state->domname, &state->name,
163 &state->gid, &state->members);
164 TALLOC_FREE(subreq);
165 if (tevent_req_nterror(req, status)) {
166 return;
168 tevent_req_done(req);
171 NTSTATUS winbindd_getgrnam_recv(struct tevent_req *req,
172 struct winbindd_response *response)
174 struct winbindd_getgrnam_state *state = tevent_req_data(
175 req, struct winbindd_getgrnam_state);
176 NTSTATUS status;
177 int num_members;
178 char *buf;
180 if (tevent_req_is_nterror(req, &status)) {
181 struct dom_sid_buf sidbuf;
182 D_WARNING("Could not convert sid %s: %s\n",
183 dom_sid_str_buf(&state->sid, &sidbuf),
184 nt_errstr(status));
185 return status;
188 if (!fill_grent(talloc_tos(), &response->data.gr, state->domname,
189 state->name, state->gid)) {
190 D_WARNING("fill_grent failed\n");
191 return NT_STATUS_NO_MEMORY;
194 status = winbindd_print_groupmembers(state->members, response,
195 &num_members, &buf);
196 if (!NT_STATUS_IS_OK(status)) {
197 return status;
200 response->data.gr.num_gr_mem = (uint32_t)num_members;
202 /* Group membership lives at start of extra data */
204 response->data.gr.gr_mem_ofs = 0;
205 response->extra_data.data = buf;
206 response->length += talloc_get_size(response->extra_data.data);
208 D_NOTICE("Winbind external command GETGRNAM end.\n"
209 "Returning %"PRIu32" member(s).\n",
210 response->data.gr.num_gr_mem);
212 return NT_STATUS_OK;