sharesec: Check if share exists in configuration
[samba.git] / source3 / winbindd / winbindd_getgrnam.c
blob24fef6c2c20df8c835b1b36ba3e7913942bd81be
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 (NT_STATUS_IS_OK(status) && type == SID_NAME_UNKNOWN) {
126 status = NT_STATUS_NONE_MAPPED;
128 if (tevent_req_nterror(req, status)) {
129 return;
132 switch (type) {
133 case SID_NAME_DOM_GRP:
134 case SID_NAME_ALIAS:
135 case SID_NAME_WKN_GRP:
137 * Also give user types a chance:
138 * These might be user sids mapped to the ID_TYPE_BOTH,
139 * and in that case we should construct a group struct.
141 case SID_NAME_USER:
142 case SID_NAME_COMPUTER:
143 break;
144 default:
145 tevent_req_nterror(req, NT_STATUS_NO_SUCH_GROUP);
146 return;
149 subreq = wb_getgrsid_send(state, state->ev, &state->sid,
150 lp_winbind_expand_groups());
151 if (tevent_req_nomem(subreq, req)) {
152 return;
154 tevent_req_set_callback(subreq, winbindd_getgrnam_done, req);
157 static void winbindd_getgrnam_done(struct tevent_req *subreq)
159 struct tevent_req *req = tevent_req_callback_data(
160 subreq, struct tevent_req);
161 struct winbindd_getgrnam_state *state = tevent_req_data(
162 req, struct winbindd_getgrnam_state);
163 NTSTATUS status;
165 status = wb_getgrsid_recv(subreq, state, &state->domname, &state->name,
166 &state->gid, &state->members);
167 TALLOC_FREE(subreq);
168 if (tevent_req_nterror(req, status)) {
169 return;
171 tevent_req_done(req);
174 NTSTATUS winbindd_getgrnam_recv(struct tevent_req *req,
175 struct winbindd_response *response)
177 struct winbindd_getgrnam_state *state = tevent_req_data(
178 req, struct winbindd_getgrnam_state);
179 NTSTATUS status;
180 int num_members;
181 char *buf;
183 if (tevent_req_is_nterror(req, &status)) {
184 struct dom_sid_buf sidbuf;
185 D_WARNING("Could not convert sid %s: %s\n",
186 dom_sid_str_buf(&state->sid, &sidbuf),
187 nt_errstr(status));
188 return status;
191 if (!fill_grent(talloc_tos(), &response->data.gr, state->domname,
192 state->name, state->gid)) {
193 D_WARNING("fill_grent failed\n");
194 return NT_STATUS_NO_MEMORY;
197 status = winbindd_print_groupmembers(state->members, response,
198 &num_members, &buf);
199 if (!NT_STATUS_IS_OK(status)) {
200 return status;
203 response->data.gr.num_gr_mem = (uint32_t)num_members;
205 /* Group membership lives at start of extra data */
207 response->data.gr.gr_mem_ofs = 0;
208 response->extra_data.data = buf;
209 response->length += talloc_get_size(response->extra_data.data);
211 D_NOTICE("Winbind external command GETGRNAM end.\n"
212 "Returning %"PRIu32" member(s).\n",
213 response->data.gr.num_gr_mem);
215 return NT_STATUS_OK;