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/>.
22 #include "libcli/security/dom_sid.h"
23 #include "lib/util/string_wrappers.h"
25 struct winbindd_getgrnam_state
{
26 struct tevent_context
*ev
;
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
;
51 req
= tevent_req_create(mem_ctx
, &state
,
52 struct winbindd_getgrnam_state
);
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",
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
,
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
,
106 if (tevent_req_nomem(subreq
, req
)) {
107 return tevent_req_post(req
, ev
);
109 tevent_req_set_callback(subreq
, winbindd_getgrnam_lookupname_done
,
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
;
123 status
= wb_lookupname_recv(subreq
, &state
->sid
, &type
);
125 if (tevent_req_nterror(req
, status
)) {
130 case SID_NAME_DOM_GRP
:
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.
139 case SID_NAME_COMPUTER
:
142 tevent_req_nterror(req
, NT_STATUS_NO_SUCH_GROUP
);
146 subreq
= wb_getgrsid_send(state
, state
->ev
, &state
->sid
,
147 lp_winbind_expand_groups());
148 if (tevent_req_nomem(subreq
, req
)) {
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
);
162 status
= wb_getgrsid_recv(subreq
, state
, &state
->domname
, &state
->name
,
163 &state
->gid
, &state
->members
);
165 if (tevent_req_nterror(req
, status
)) {
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
);
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
),
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
,
196 if (!NT_STATUS_IS_OK(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
);