idmap_ad: Pass tldap debug messages on to DEBUG()
[Samba.git] / source3 / winbindd / winbindd_getgrnam.c
blobd43fc3042b55a4d21ef274b593ed8fddb2b3f6b9
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"
24 struct winbindd_getgrnam_state {
25 struct tevent_context *ev;
26 fstring name_namespace, name_domain, name_group;
27 struct dom_sid sid;
28 const char *domname;
29 const char *name;
30 gid_t gid;
31 struct db_context *members;
34 static void winbindd_getgrnam_lookupname_done(struct tevent_req *subreq);
35 static void winbindd_getgrnam_done(struct tevent_req *subreq);
37 struct tevent_req *winbindd_getgrnam_send(TALLOC_CTX *mem_ctx,
38 struct tevent_context *ev,
39 struct winbindd_cli_state *cli,
40 struct winbindd_request *request)
42 struct tevent_req *req, *subreq;
43 struct winbindd_getgrnam_state *state;
44 char *tmp;
45 NTSTATUS nt_status;
46 bool ok;
48 req = tevent_req_create(mem_ctx, &state,
49 struct winbindd_getgrnam_state);
50 if (req == NULL) {
51 return NULL;
53 state->ev = ev;
55 /* Ensure null termination */
56 request->data.groupname[sizeof(request->data.groupname)-1]='\0';
58 DBG_NOTICE("[%s (%u)] getgrnam %s\n",
59 cli->client_name,
60 (unsigned int)cli->pid,
61 request->data.groupname);
63 nt_status = normalize_name_unmap(state, request->data.groupname, &tmp);
64 /* If we didn't map anything in the above call, just reset the
65 tmp pointer to the original string */
66 if (!NT_STATUS_IS_OK(nt_status) &&
67 !NT_STATUS_EQUAL(nt_status, NT_STATUS_FILE_RENAMED))
69 tmp = request->data.groupname;
72 /* Parse domain and groupname */
74 ok = parse_domain_user(tmp,
75 state->name_namespace,
76 state->name_domain,
77 state->name_group);
78 if (!ok) {
79 DBG_INFO("Could not parse domain user: %s\n", tmp);
80 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
81 return tevent_req_post(req, ev);
84 /* if no domain or our local domain and no local tdb group, default to
85 * our local domain for aliases */
87 if ( !*(state->name_domain) || strequal(state->name_domain,
88 get_global_sam_name()) ) {
89 fstrcpy(state->name_domain, get_global_sam_name());
92 subreq = wb_lookupname_send(state, ev,
93 state->name_namespace,
94 state->name_domain,
95 state->name_group,
96 0);
97 if (tevent_req_nomem(subreq, req)) {
98 return tevent_req_post(req, ev);
100 tevent_req_set_callback(subreq, winbindd_getgrnam_lookupname_done,
101 req);
102 return req;
105 static void winbindd_getgrnam_lookupname_done(struct tevent_req *subreq)
107 struct tevent_req *req = tevent_req_callback_data(
108 subreq, struct tevent_req);
109 struct winbindd_getgrnam_state *state = tevent_req_data(
110 req, struct winbindd_getgrnam_state);
111 enum lsa_SidType type;
112 NTSTATUS status;
114 status = wb_lookupname_recv(subreq, &state->sid, &type);
115 TALLOC_FREE(subreq);
116 if (tevent_req_nterror(req, status)) {
117 return;
120 switch (type) {
121 case SID_NAME_DOM_GRP:
122 case SID_NAME_ALIAS:
123 case SID_NAME_WKN_GRP:
125 * Also give user types a chance:
126 * These might be user sids mapped to the ID_TYPE_BOTH,
127 * and in that case we should construct a group struct.
129 case SID_NAME_USER:
130 case SID_NAME_COMPUTER:
131 break;
132 default:
133 tevent_req_nterror(req, NT_STATUS_NO_SUCH_GROUP);
134 return;
137 subreq = wb_getgrsid_send(state, state->ev, &state->sid,
138 lp_winbind_expand_groups());
139 if (tevent_req_nomem(subreq, req)) {
140 return;
142 tevent_req_set_callback(subreq, winbindd_getgrnam_done, req);
145 static void winbindd_getgrnam_done(struct tevent_req *subreq)
147 struct tevent_req *req = tevent_req_callback_data(
148 subreq, struct tevent_req);
149 struct winbindd_getgrnam_state *state = tevent_req_data(
150 req, struct winbindd_getgrnam_state);
151 NTSTATUS status;
153 status = wb_getgrsid_recv(subreq, state, &state->domname, &state->name,
154 &state->gid, &state->members);
155 TALLOC_FREE(subreq);
156 if (tevent_req_nterror(req, status)) {
157 return;
159 tevent_req_done(req);
162 NTSTATUS winbindd_getgrnam_recv(struct tevent_req *req,
163 struct winbindd_response *response)
165 struct winbindd_getgrnam_state *state = tevent_req_data(
166 req, struct winbindd_getgrnam_state);
167 NTSTATUS status;
168 int num_members;
169 char *buf;
171 if (tevent_req_is_nterror(req, &status)) {
172 struct dom_sid_buf sidbuf;
173 DEBUG(5, ("Could not convert sid %s: %s\n",
174 dom_sid_str_buf(&state->sid, &sidbuf),
175 nt_errstr(status)));
176 return status;
179 if (!fill_grent(talloc_tos(), &response->data.gr, state->domname,
180 state->name, state->gid)) {
181 DEBUG(5, ("fill_grent failed\n"));
182 return NT_STATUS_NO_MEMORY;
185 status = winbindd_print_groupmembers(state->members, response,
186 &num_members, &buf);
187 if (!NT_STATUS_IS_OK(status)) {
188 return status;
191 response->data.gr.num_gr_mem = (uint32_t)num_members;
193 /* Group membership lives at start of extra data */
195 response->data.gr.gr_mem_ofs = 0;
196 response->extra_data.data = buf;
197 response->length += talloc_get_size(response->extra_data.data);
199 return NT_STATUS_OK;