2 Unix SMB/CIFS implementation.
6 Copyright (C) 2009,2010 Kai Blin <kai@samba.org>
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 3 of the License, or (at your option) any later version.
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Library General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 /* Required Headers */
25 #include "libwbclient.h"
26 #include "../winbind_client.h"
27 #include "wbc_async.h"
29 struct wbc_lookup_name_state
{
30 struct winbindd_request req
;
31 struct wb_context
*wb_ctx
;
32 struct wbcDomainSid
*sid
;
33 enum wbcSidType name_type
;
36 static void wbcLookupName_done(struct tevent_req
*subreq
);
39 * @brief Request a conversion of a domain and name to a domain sid
41 * @param mem_ctx talloc context to allocate the request from
42 * @param ev tevent context to use for async operation
43 * @param wb_ctx winbind context to use
44 * @param *domain Pointer to the domain to be resolved
45 * @param *name Pointer to the name to be resolved
47 * @return tevent_req on success, NULL on error
50 struct tevent_req
*wbcLookupName_send(TALLOC_CTX
*mem_ctx
,
51 struct tevent_context
*ev
,
52 struct wb_context
*wb_ctx
,
56 struct tevent_req
*req
, *subreq
;
57 struct wbc_lookup_name_state
*state
;
59 req
= tevent_req_create(mem_ctx
, &state
, struct wbc_lookup_name_state
);
64 ZERO_STRUCT(state
->req
);
66 state
->req
.cmd
= WINBINDD_LOOKUPNAME
;
67 strncpy(state
->req
.data
.name
.dom_name
, domain
,
68 sizeof(state
->req
.data
.name
.dom_name
)-1);
69 strncpy(state
->req
.data
.name
.name
, name
,
70 sizeof(state
->req
.data
.name
.name
)-1);
71 state
->wb_ctx
= wb_ctx
;
74 subreq
= wb_trans_send(state
, ev
, wb_ctx
, false, &state
->req
);
75 if (tevent_req_nomem(subreq
, req
)) {
76 return tevent_req_post(req
, ev
);
79 tevent_req_set_callback(subreq
, wbcLookupName_done
, req
);
83 static void wbcLookupName_done(struct tevent_req
*subreq
)
85 struct tevent_req
*req
= tevent_req_callback_data(
86 subreq
, struct tevent_req
);
87 struct wbc_lookup_name_state
*state
= tevent_req_data(
88 req
, struct wbc_lookup_name_state
);
89 struct winbindd_response
*resp
;
92 wbc_status
= wb_trans_recv(subreq
, state
, &resp
);
94 if (!WBC_ERROR_IS_OK(wbc_status
)) {
95 tevent_req_error(req
, wbc_status
);
99 state
->sid
= talloc(state
, struct wbcDomainSid
);
100 if (tevent_req_nomem(state
->sid
, req
)) {
104 wbc_status
= wbcStringToSid(resp
->data
.sid
.sid
, state
->sid
);
105 if (!WBC_ERROR_IS_OK(wbc_status
)) {
106 wbcDebug(state
->wb_ctx
, WBC_DEBUG_ERROR
,
107 "wbcStringToSid returned %s!\n",
108 wbcErrorString(wbc_status
));
109 tevent_req_error(req
, wbc_status
);
113 state
->name_type
= (enum wbcSidType
)resp
->data
.sid
.type
;
117 tevent_req_done(req
);
121 * @brief Receive a conversion of a domain and name to a domain SID.
123 * @param req The tevent request calling this function.
125 * @param sid A pointer to store the sid looked up.
127 * @param name_type Pointer to store the resolved SID name type.
131 wbcErr
wbcLookupName_recv(struct tevent_req
*req
,
132 struct wbcDomainSid
*sid
,
133 enum wbcSidType
*name_type
)
135 struct wbc_lookup_name_state
*state
= tevent_req_data(
136 req
, struct wbc_lookup_name_state
);
137 wbcErr wbc_status
= WBC_ERR_SUCCESS
;
139 if (!sid
|| !name_type
) {
140 wbcDebug(state
->wb_ctx
, WBC_DEBUG_TRACE
,
141 "Sid is %p, name_type is %p\n", sid
, name_type
);
142 wbc_status
= WBC_ERR_INVALID_PARAM
;
146 if (tevent_req_is_wbcerr(req
, &wbc_status
)) {
150 memcpy(sid
, state
->sid
, sizeof(struct wbcDomainSid
));
151 *name_type
= state
->name_type
;
154 tevent_req_received(req
);
159 struct wbc_lookup_sid_state
{
160 struct winbindd_request req
;
163 enum wbcSidType name_type
;
166 static void wbcLookupSid_done(struct tevent_req
*subreq
);
169 * @brief Request a conversion of a SID to a domain and name
171 * @param mem_ctx talloc context to allocate the request from
172 * @param ev tevent context to use for async operation
173 * @param wb_ctx winbind context to use
174 * @param *sid Pointer to the domain SID to be resolved
176 * @return tevent_req on success, NULL on error
179 struct tevent_req
*wbcLookupSid_send(TALLOC_CTX
*mem_ctx
,
180 struct tevent_context
*ev
,
181 struct wb_context
*wb_ctx
,
182 const struct wbcDomainSid
*sid
)
184 struct tevent_req
*req
, *subreq
;
185 struct wbc_lookup_sid_state
*state
;
187 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
189 req
= tevent_req_create(mem_ctx
, &state
, struct wbc_lookup_sid_state
);
194 ZERO_STRUCT(state
->req
);
196 state
->req
.cmd
= WINBINDD_LOOKUPSID
;
197 wbc_status
= wbcSidToString(sid
, &sid_string
);
198 if (!WBC_ERROR_IS_OK(wbc_status
)) {
199 tevent_req_error(req
, wbc_status
);
200 return tevent_req_post(req
, ev
);
202 strncpy(state
->req
.data
.sid
, sid_string
, sizeof(state
->req
.data
.sid
)-1);
203 wbcFreeMemory(sid_string
);
205 subreq
= wb_trans_send(state
, ev
, wb_ctx
, false, &state
->req
);
206 if (tevent_req_nomem(subreq
, req
)) {
207 return tevent_req_post(req
, ev
);
210 tevent_req_set_callback(subreq
, wbcLookupSid_done
, req
);
214 static void wbcLookupSid_done(struct tevent_req
*subreq
)
216 struct tevent_req
*req
= tevent_req_callback_data(
217 subreq
, struct tevent_req
);
218 struct wbc_lookup_sid_state
*state
= tevent_req_data(
219 req
, struct wbc_lookup_sid_state
);
220 struct winbindd_response
*resp
;
223 wbc_status
= wb_trans_recv(subreq
, state
, &resp
);
225 if (!WBC_ERROR_IS_OK(wbc_status
)) {
226 tevent_req_error(req
, wbc_status
);
229 state
->domain
= talloc_strdup(state
, resp
->data
.name
.dom_name
);
230 if (tevent_req_nomem(state
->domain
, req
)) {
234 state
->name
= talloc_strdup(state
, resp
->data
.name
.name
);
235 if (tevent_req_nomem(state
->name
, req
)) {
239 state
->name_type
= (enum wbcSidType
)resp
->data
.name
.type
;
243 tevent_req_done(req
);
247 * @brief Receive a conversion a SID to a domain and name
249 * @param req The tevent request calling this function.
251 * @param mem_ctx A talloc context to move results to.
253 * @param pdomain A pointer to store the resolved domain name
256 * @param pname A pointer to store the resolved user or group name.
258 * @param pname_type A pointer to store the resolved SID type.
262 wbcErr
wbcLookupSid_recv(struct tevent_req
*req
,
266 enum wbcSidType
*pname_type
)
268 struct wbc_lookup_sid_state
*state
= tevent_req_data(
269 req
, struct wbc_lookup_sid_state
);
272 if (tevent_req_is_wbcerr(req
, &wbc_status
)) {
273 tevent_req_received(req
);
277 if (pdomain
!= NULL
) {
278 *pdomain
= talloc_steal(mem_ctx
, state
->domain
);
282 *pname
= talloc_steal(mem_ctx
, state
->name
);
285 if (pname_type
!= NULL
) {
286 *pname_type
= state
->name_type
;
289 tevent_req_received(req
);
290 return WBC_ERR_SUCCESS
;