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 (NT_STATUS_IS_OK(status
) && type
== SID_NAME_UNKNOWN
) {
126 status
= NT_STATUS_NONE_MAPPED
;
128 if (tevent_req_nterror(req
, status
)) {
133 case SID_NAME_DOM_GRP
:
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.
142 case SID_NAME_COMPUTER
:
145 tevent_req_nterror(req
, NT_STATUS_NO_SUCH_GROUP
);
149 subreq
= wb_getgrsid_send(state
, state
->ev
, &state
->sid
,
150 lp_winbind_expand_groups());
151 if (tevent_req_nomem(subreq
, req
)) {
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
);
165 status
= wb_getgrsid_recv(subreq
, state
, &state
->domname
, &state
->name
,
166 &state
->gid
, &state
->members
);
168 if (tevent_req_nterror(req
, status
)) {
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
);
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
),
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
,
199 if (!NT_STATUS_IS_OK(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
);