winbind: Pass upn unmodified to lookup names
[Samba.git] / source3 / winbindd / winbindd_getgrnam.c
blob1d9a8b94d48f8eac89d7a9867a6319604eefc1e5
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_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;
46 req = tevent_req_create(mem_ctx, &state,
47 struct winbindd_getgrnam_state);
48 if (req == NULL) {
49 return NULL;
51 state->ev = ev;
53 /* Ensure null termination */
54 request->data.groupname[sizeof(request->data.groupname)-1]='\0';
56 DEBUG(3, ("getgrnam %s\n", request->data.groupname));
58 nt_status = normalize_name_unmap(state, request->data.groupname, &tmp);
59 /* If we didn't map anything in the above call, just reset the
60 tmp pointer to the original string */
61 if (!NT_STATUS_IS_OK(nt_status) &&
62 !NT_STATUS_EQUAL(nt_status, NT_STATUS_FILE_RENAMED))
64 tmp = request->data.groupname;
67 /* Parse domain and groupname */
69 parse_domain_user(tmp, state->name_domain, state->name_group);
71 /* if no domain or our local domain and no local tdb group, default to
72 * our local domain for aliases */
74 if ( !*(state->name_domain) || strequal(state->name_domain,
75 get_global_sam_name()) ) {
76 fstrcpy(state->name_domain, get_global_sam_name());
79 subreq = wb_lookupname_send(state, ev,
80 state->name_domain,
81 state->name_domain,
82 state->name_group,
83 0);
84 if (tevent_req_nomem(subreq, req)) {
85 return tevent_req_post(req, ev);
87 tevent_req_set_callback(subreq, winbindd_getgrnam_lookupname_done,
88 req);
89 return req;
92 static void winbindd_getgrnam_lookupname_done(struct tevent_req *subreq)
94 struct tevent_req *req = tevent_req_callback_data(
95 subreq, struct tevent_req);
96 struct winbindd_getgrnam_state *state = tevent_req_data(
97 req, struct winbindd_getgrnam_state);
98 enum lsa_SidType type;
99 NTSTATUS status;
101 status = wb_lookupname_recv(subreq, &state->sid, &type);
102 TALLOC_FREE(subreq);
103 if (tevent_req_nterror(req, status)) {
104 return;
107 switch (type) {
108 case SID_NAME_DOM_GRP:
109 case SID_NAME_ALIAS:
110 case SID_NAME_WKN_GRP:
112 * Also give user types a chance:
113 * These might be user sids mapped to the ID_TYPE_BOTH,
114 * and in that case we should construct a group struct.
116 case SID_NAME_USER:
117 case SID_NAME_COMPUTER:
118 break;
119 default:
120 tevent_req_nterror(req, NT_STATUS_NO_SUCH_GROUP);
121 return;
124 subreq = wb_getgrsid_send(state, state->ev, &state->sid,
125 lp_winbind_expand_groups());
126 if (tevent_req_nomem(subreq, req)) {
127 return;
129 tevent_req_set_callback(subreq, winbindd_getgrnam_done, req);
132 static void winbindd_getgrnam_done(struct tevent_req *subreq)
134 struct tevent_req *req = tevent_req_callback_data(
135 subreq, struct tevent_req);
136 struct winbindd_getgrnam_state *state = tevent_req_data(
137 req, struct winbindd_getgrnam_state);
138 NTSTATUS status;
140 status = wb_getgrsid_recv(subreq, state, &state->domname, &state->name,
141 &state->gid, &state->members);
142 TALLOC_FREE(subreq);
143 if (tevent_req_nterror(req, status)) {
144 return;
146 tevent_req_done(req);
149 NTSTATUS winbindd_getgrnam_recv(struct tevent_req *req,
150 struct winbindd_response *response)
152 struct winbindd_getgrnam_state *state = tevent_req_data(
153 req, struct winbindd_getgrnam_state);
154 NTSTATUS status;
155 int num_members;
156 char *buf;
158 if (tevent_req_is_nterror(req, &status)) {
159 DEBUG(5, ("Could not convert sid %s: %s\n",
160 sid_string_dbg(&state->sid), nt_errstr(status)));
161 return status;
164 if (!fill_grent(talloc_tos(), &response->data.gr, state->domname,
165 state->name, state->gid)) {
166 DEBUG(5, ("fill_grent failed\n"));
167 return NT_STATUS_NO_MEMORY;
170 status = winbindd_print_groupmembers(state->members, response,
171 &num_members, &buf);
172 if (!NT_STATUS_IS_OK(status)) {
173 return status;
176 response->data.gr.num_gr_mem = (uint32_t)num_members;
178 /* Group membership lives at start of extra data */
180 response->data.gr.gr_mem_ofs = 0;
181 response->extra_data.data = buf;
182 response->length += talloc_get_size(response->extra_data.data);
184 return NT_STATUS_OK;