2 Unix SMB/CIFS implementation.
4 Async transfer of winbindd_request and _response structs
6 Copyright (C) Volker Lendecke 2008
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program 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
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "wbc_async.h"
26 #define DBGC_CLASS DBGC_WINBIND
28 struct req_read_state {
29 struct winbindd_request *wb_req;
30 struct tevent_context *ev;
31 size_t max_extra_data;
35 bool async_req_is_wbcerr(struct async_req *req, wbcErr *pwbc_err)
37 enum async_req_state state;
39 if (!async_req_is_error(req, &state, &error)) {
40 *pwbc_err = WBC_ERR_SUCCESS;
45 case ASYNC_REQ_USER_ERROR:
48 case ASYNC_REQ_TIMED_OUT:
49 *pwbc_err = WBC_ERR_UNKNOWN_FAILURE;
51 case ASYNC_REQ_NO_MEMORY:
52 *pwbc_err = WBC_ERR_NO_MEMORY;
55 *pwbc_err = WBC_ERR_UNKNOWN_FAILURE;
61 wbcErr map_wbc_err_from_errno(int error)
66 return WBC_ERR_AUTH_ERROR;
68 return WBC_ERR_NO_MEMORY;
71 return WBC_ERR_UNKNOWN_FAILURE;
75 wbcErr async_req_simple_recv_wbcerr(struct async_req *req)
79 if (async_req_is_wbcerr(req, &wbc_err)) {
83 return WBC_ERR_SUCCESS;
86 static void wb_req_read_len(struct async_req *subreq);
87 static void wb_req_read_main(struct async_req *subreq);
88 static void wb_req_read_extra(struct async_req *subreq);
90 struct async_req *wb_req_read_send(TALLOC_CTX *mem_ctx,
91 struct tevent_context *ev,
92 int fd, size_t max_extra_data)
94 struct async_req *result, *subreq;
95 struct req_read_state *state;
97 if (!async_req_setup(mem_ctx, &result, &state,
98 struct req_read_state)) {
103 state->max_extra_data = max_extra_data;
104 state->wb_req = talloc(state, struct winbindd_request);
105 if (state->wb_req == NULL) {
109 subreq = recvall_send(state, ev, state->fd, &(state->wb_req->length),
110 sizeof(state->wb_req->length), 0);
111 if (subreq == NULL) {
115 subreq->async.fn = wb_req_read_len;
116 subreq->async.priv = result;
124 static void wb_req_read_len(struct async_req *subreq)
126 struct async_req *req = talloc_get_type_abort(
127 subreq->async.priv, struct async_req);
128 struct req_read_state *state = talloc_get_type_abort(
129 req->private_data, struct req_read_state);
133 ret = recvall_recv(subreq, &err);
136 async_req_error(req, map_wbc_err_from_errno(err));
140 if (state->wb_req->length != sizeof(struct winbindd_request)) {
141 DEBUG(0, ("wb_req_read_len: Invalid request size received: "
142 "%d (expected %d)\n", (int)state->wb_req->length,
143 (int)sizeof(struct winbindd_request)));
144 async_req_error(req, WBC_ERR_INVALID_RESPONSE);
148 subreq = recvall_send(
149 req, state->ev, state->fd, (uint32 *)(state->wb_req)+1,
150 sizeof(struct winbindd_request) - sizeof(uint32), 0);
151 if (async_req_nomem(subreq, req)) {
155 subreq->async.fn = wb_req_read_main;
156 subreq->async.priv = req;
159 static void wb_req_read_main(struct async_req *subreq)
161 struct async_req *req = talloc_get_type_abort(
162 subreq->async.priv, struct async_req);
163 struct req_read_state *state = talloc_get_type_abort(
164 req->private_data, struct req_read_state);
168 ret = recvall_recv(subreq, &err);
171 async_req_error(req, map_wbc_err_from_errno(err));
175 if ((state->max_extra_data != 0)
176 && (state->wb_req->extra_len > state->max_extra_data)) {
177 DEBUG(3, ("Got request with %d bytes extra data on "
178 "unprivileged socket\n",
179 (int)state->wb_req->extra_len));
180 async_req_error(req, WBC_ERR_INVALID_RESPONSE);
184 if (state->wb_req->extra_len == 0) {
189 state->wb_req->extra_data.data = TALLOC_ARRAY(
190 state->wb_req, char, state->wb_req->extra_len + 1);
191 if (async_req_nomem(state->wb_req->extra_data.data, req)) {
195 state->wb_req->extra_data.data[state->wb_req->extra_len] = 0;
197 subreq = recvall_send(
198 req, state->ev, state->fd, state->wb_req->extra_data.data,
199 state->wb_req->extra_len, 0);
200 if (async_req_nomem(subreq, req)) {
204 subreq->async.fn = wb_req_read_extra;
205 subreq->async.priv = req;
208 static void wb_req_read_extra(struct async_req *subreq)
210 struct async_req *req = talloc_get_type_abort(
211 subreq->async.priv, struct async_req);
215 ret = recvall_recv(subreq, &err);
218 async_req_error(req, map_wbc_err_from_errno(err));
225 wbcErr wb_req_read_recv(struct async_req *req, TALLOC_CTX *mem_ctx,
226 struct winbindd_request **preq)
228 struct req_read_state *state = talloc_get_type_abort(
229 req->private_data, struct req_read_state);
232 if (async_req_is_wbcerr(req, &wbc_err)) {
235 *preq = talloc_move(mem_ctx, &state->wb_req);
236 return WBC_ERR_SUCCESS;
239 struct req_write_state {
243 static void wb_req_write_done(struct tevent_req *subreq);
245 struct async_req *wb_req_write_send(TALLOC_CTX *mem_ctx,
246 struct tevent_context *ev, int fd,
247 struct winbindd_request *wb_req)
249 struct async_req *result;
250 struct tevent_req *subreq;
251 struct req_write_state *state;
254 if (!async_req_setup(mem_ctx, &result, &state,
255 struct req_write_state)) {
259 state->iov[0].iov_base = wb_req;
260 state->iov[0].iov_len = sizeof(struct winbindd_request);
262 if (wb_req->extra_len != 0) {
263 state->iov[1].iov_base = wb_req->extra_data.data;
264 state->iov[1].iov_len = wb_req->extra_len;
268 subreq = writev_send(state, ev, fd, state->iov, count);
269 if (subreq == NULL) {
272 subreq->async.fn = wb_req_write_done;
273 subreq->async.private_data = result;
281 static void wb_req_write_done(struct tevent_req *subreq)
283 struct async_req *req = talloc_get_type_abort(
284 subreq->async.private_data, struct async_req);
288 ret = writev_recv(subreq, &err);
291 async_req_error(req, map_wbc_err_from_errno(err));
297 wbcErr wb_req_write_recv(struct async_req *req)
299 return async_req_simple_recv_wbcerr(req);
302 struct resp_read_state {
303 struct winbindd_response *wb_resp;
304 struct tevent_context *ev;
305 size_t max_extra_data;
309 static void wb_resp_read_len(struct async_req *subreq);
310 static void wb_resp_read_main(struct async_req *subreq);
311 static void wb_resp_read_extra(struct async_req *subreq);
313 struct async_req *wb_resp_read_send(TALLOC_CTX *mem_ctx,
314 struct tevent_context *ev, int fd)
316 struct async_req *result, *subreq;
317 struct resp_read_state *state;
319 if (!async_req_setup(mem_ctx, &result, &state,
320 struct resp_read_state)) {
325 state->wb_resp = talloc(state, struct winbindd_response);
326 if (state->wb_resp == NULL) {
330 subreq = recvall_send(state, ev, state->fd, &(state->wb_resp->length),
331 sizeof(state->wb_resp->length), 0);
332 if (subreq == NULL) {
336 subreq->async.fn = wb_resp_read_len;
337 subreq->async.priv = result;
345 static void wb_resp_read_len(struct async_req *subreq)
347 struct async_req *req = talloc_get_type_abort(
348 subreq->async.priv, struct async_req);
349 struct resp_read_state *state = talloc_get_type_abort(
350 req->private_data, struct resp_read_state);
354 ret = recvall_recv(subreq, &err);
357 async_req_error(req, map_wbc_err_from_errno(err));
361 if (state->wb_resp->length < sizeof(struct winbindd_response)) {
362 DEBUG(0, ("wb_resp_read_len: Invalid response size received: "
363 "%d (expected at least%d)\n",
364 (int)state->wb_resp->length,
365 (int)sizeof(struct winbindd_response)));
366 async_req_error(req, WBC_ERR_INVALID_RESPONSE);
370 subreq = recvall_send(
371 req, state->ev, state->fd, (uint32 *)(state->wb_resp)+1,
372 sizeof(struct winbindd_response) - sizeof(uint32), 0);
373 if (async_req_nomem(subreq, req)) {
377 subreq->async.fn = wb_resp_read_main;
378 subreq->async.priv = req;
381 static void wb_resp_read_main(struct async_req *subreq)
383 struct async_req *req = talloc_get_type_abort(
384 subreq->async.priv, struct async_req);
385 struct resp_read_state *state = talloc_get_type_abort(
386 req->private_data, struct resp_read_state);
391 ret = recvall_recv(subreq, &err);
394 async_req_error(req, map_wbc_err_from_errno(err));
398 extra_len = state->wb_resp->length - sizeof(struct winbindd_response);
399 if (extra_len == 0) {
404 state->wb_resp->extra_data.data = TALLOC_ARRAY(
405 state->wb_resp, char, extra_len+1);
406 if (async_req_nomem(state->wb_resp->extra_data.data, req)) {
409 ((char *)state->wb_resp->extra_data.data)[extra_len] = 0;
411 subreq = recvall_send(
412 req, state->ev, state->fd, state->wb_resp->extra_data.data,
414 if (async_req_nomem(subreq, req)) {
418 subreq->async.fn = wb_resp_read_extra;
419 subreq->async.priv = req;
422 static void wb_resp_read_extra(struct async_req *subreq)
424 struct async_req *req = talloc_get_type_abort(
425 subreq->async.priv, struct async_req);
429 ret = recvall_recv(subreq, &err);
432 async_req_error(req, map_wbc_err_from_errno(err));
439 wbcErr wb_resp_read_recv(struct async_req *req, TALLOC_CTX *mem_ctx,
440 struct winbindd_response **presp)
442 struct resp_read_state *state = talloc_get_type_abort(
443 req->private_data, struct resp_read_state);
446 if (async_req_is_wbcerr(req, &wbc_err)) {
449 *presp = talloc_move(mem_ctx, &state->wb_resp);
450 return WBC_ERR_SUCCESS;
453 struct resp_write_state {
454 struct winbindd_response *wb_resp;
455 struct tevent_context *ev;
459 static void wb_resp_write_main(struct async_req *subreq);
460 static void wb_resp_write_extra(struct async_req *subreq);
462 struct async_req *wb_resp_write_send(TALLOC_CTX *mem_ctx,
463 struct tevent_context *ev, int fd,
464 struct winbindd_response *wb_resp)
466 struct async_req *result, *subreq;
467 struct resp_write_state *state;
469 if (!async_req_setup(mem_ctx, &result, &state,
470 struct resp_write_state)) {
475 state->wb_resp = wb_resp;
477 subreq = sendall_send(state, state->ev, state->fd, state->wb_resp,
478 sizeof(struct winbindd_response), 0);
479 if (subreq == NULL) {
483 subreq->async.fn = wb_resp_write_main;
484 subreq->async.priv = result;
492 static void wb_resp_write_main(struct async_req *subreq)
494 struct async_req *req = talloc_get_type_abort(
495 subreq->async.priv, struct async_req);
496 struct resp_write_state *state = talloc_get_type_abort(
497 req->private_data, struct resp_write_state);
501 ret = sendall_recv(subreq, &err);
504 async_req_error(req, map_wbc_err_from_errno(err));
508 if (state->wb_resp->length == sizeof(struct winbindd_response)) {
513 subreq = sendall_send(
514 state, state->ev, state->fd,
515 state->wb_resp->extra_data.data,
516 state->wb_resp->length - sizeof(struct winbindd_response), 0);
517 if (async_req_nomem(subreq, req)) {
521 subreq->async.fn = wb_resp_write_extra;
522 subreq->async.priv = req;
525 static void wb_resp_write_extra(struct async_req *subreq)
527 struct async_req *req = talloc_get_type_abort(
528 subreq->async.priv, struct async_req);
532 ret = sendall_recv(subreq, &err);
535 async_req_error(req, map_wbc_err_from_errno(err));
542 wbcErr wb_resp_write_recv(struct async_req *req)
544 return async_req_simple_recv_wbcerr(req);