libwbclient: Fix wbcListUsers against too small num_entries
[Samba/ekacnet.git] / nsswitch / libwbclient / wbc_sid_async.c
blobb94b88d491d428193130e7ce0fe6e30ca9038b84
1 /*
2 Unix SMB/CIFS implementation.
4 Winbind client API
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 */
24 #include "replace.h"
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);
38 /**
39 * @brief Request a conversion of a domaind 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
48 **/
50 struct tevent_req *wbcLookupName_send(TALLOC_CTX *mem_ctx,
51 struct tevent_context *ev,
52 struct wb_context *wb_ctx,
53 const char *domain,
54 const char *name)
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);
60 if (req == NULL) {
61 return NULL;
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);
80 return 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;
90 wbcErr wbc_status;
92 wbc_status = wb_trans_recv(subreq, state, &resp);
93 TALLOC_FREE(subreq);
94 if (!WBC_ERROR_IS_OK(wbc_status)) {
95 tevent_req_error(req, wbc_status);
96 return;
99 state->sid = talloc(state, struct wbcDomainSid);
100 if (tevent_req_nomem(state->sid, req)) {
101 return;
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);
110 return;
113 state->name_type = (enum wbcSidType)resp->data.sid.type;
115 TALLOC_FREE(resp);
117 tevent_req_done(req);
121 * @brief Receive a conversion a SID to a domain and name
123 * @param *
124 * @param *pname Resolved User or group name
125 * @param *pname_type Pointer to the resolved SID type
127 * @return #wbcErr
130 wbcErr wbcLookupName_recv(struct tevent_req *req,
131 struct wbcDomainSid *sid,
132 enum wbcSidType *name_type)
134 struct wbc_lookup_name_state *state = tevent_req_data(
135 req, struct wbc_lookup_name_state);
136 wbcErr wbc_status = WBC_ERR_SUCCESS;
138 if (!sid || !name_type) {
139 wbcDebug(state->wb_ctx, WBC_DEBUG_TRACE,
140 "Sid is %p, name_type is %p\n", sid, name_type);
141 wbc_status = WBC_ERR_INVALID_PARAM;
142 goto failed;
145 if (tevent_req_is_wbcerr(req, &wbc_status)) {
146 goto failed;
149 memcpy(sid, state->sid, sizeof(struct wbcDomainSid));
150 *name_type = state->name_type;
152 failed:
153 tevent_req_received(req);
154 return wbc_status;
158 struct wbc_lookup_sid_state {
159 struct winbindd_request req;
160 char *domain;
161 char *name;
162 enum wbcSidType name_type;
165 static void wbcLookupSid_done(struct tevent_req *subreq);
168 * @brief Request a conversion of a SID to a domain and name
170 * @param mem_ctx talloc context to allocate the request from
171 * @param ev tevent context to use for async operation
172 * @param wb_ctx winbind context to use
173 * @param *sid Pointer to the domain SID to be resolved
175 * @return tevent_req on success, NULL on error
178 struct tevent_req *wbcLookupSid_send(TALLOC_CTX *mem_ctx,
179 struct tevent_context *ev,
180 struct wb_context *wb_ctx,
181 const struct wbcDomainSid *sid)
183 struct tevent_req *req, *subreq;
184 struct wbc_lookup_sid_state *state;
185 char *sid_string;
186 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
188 req = tevent_req_create(mem_ctx, &state, struct wbc_lookup_sid_state);
189 if (req == NULL) {
190 return NULL;
193 ZERO_STRUCT(state->req);
195 state->req.cmd = WINBINDD_LOOKUPSID;
196 wbc_status = wbcSidToString(sid, &sid_string);
197 if (!WBC_ERROR_IS_OK(wbc_status)) {
198 tevent_req_error(req, wbc_status);
199 return tevent_req_post(req, ev);
201 strncpy(state->req.data.sid, sid_string, sizeof(state->req.data.sid)-1);
202 wbcFreeMemory(sid_string);
204 subreq = wb_trans_send(state, ev, wb_ctx, false, &state->req);
205 if (tevent_req_nomem(subreq, req)) {
206 return tevent_req_post(req, ev);
209 tevent_req_set_callback(subreq, wbcLookupSid_done, req);
210 return req;
213 static void wbcLookupSid_done(struct tevent_req *subreq)
215 struct tevent_req *req = tevent_req_callback_data(
216 subreq, struct tevent_req);
217 struct wbc_lookup_sid_state *state = tevent_req_data(
218 req, struct wbc_lookup_sid_state);
219 struct winbindd_response *resp;
220 wbcErr wbc_status;
222 wbc_status = wb_trans_recv(subreq, state, &resp);
223 TALLOC_FREE(subreq);
224 if (!WBC_ERROR_IS_OK(wbc_status)) {
225 tevent_req_error(req, wbc_status);
226 return;
228 state->domain = talloc_strdup(state, resp->data.name.dom_name);
229 if (tevent_req_nomem(state->domain, req)) {
230 return;
233 state->name = talloc_strdup(state, resp->data.name.name);
234 if (tevent_req_nomem(state->name, req)) {
235 return;
238 state->name_type = (enum wbcSidType)resp->data.name.type;
240 TALLOC_FREE(resp);
242 tevent_req_done(req);
246 * @brief Receive a conversion a SID to a domain and name
248 * @param *mem_ctx, talloc context to move results to
249 * @param *pdomain Resolved Domain name (possibly "")
250 * @param *pname Resolved User or group name
251 * @param *pname_type Pointer to the resolved SID type
253 * @return #wbcErr
256 wbcErr wbcLookupSid_recv(struct tevent_req *req,
257 TALLOC_CTX *mem_ctx,
258 char **pdomain,
259 char **pname,
260 enum wbcSidType *pname_type)
262 struct wbc_lookup_sid_state *state = tevent_req_data(
263 req, struct wbc_lookup_sid_state);
264 wbcErr wbc_status;
266 if (tevent_req_is_wbcerr(req, &wbc_status)) {
267 tevent_req_received(req);
268 return wbc_status;
271 if (pdomain != NULL) {
272 *pdomain = talloc_steal(mem_ctx, state->domain);
275 if (pname != NULL) {
276 *pname = talloc_steal(mem_ctx, state->name);
279 if (pname_type != NULL) {
280 *pname_type = state->name_type;
283 tevent_req_received(req);
284 return WBC_ERR_SUCCESS;