libwbclient: Add async call framework.
[Samba/gebeck_regimport.git] / nsswitch / libwbclient / wbc_async.c
blob141c9816aabfe5114146b9837ba3f866dfc775dd
1 /*
2 Unix SMB/CIFS implementation.
3 Infrastructure for async winbind requests
4 Copyright (C) Volker Lendecke 2008
6 ** NOTE! The following LGPL license applies to the wbclient
7 ** library. This does NOT imply that all of Samba is released
8 ** under the LGPL
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 3 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Library General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "replace.h"
25 #include "system/filesys.h"
26 #include "system/network.h"
27 #include <talloc.h>
28 #include <tevent.h>
29 struct fd_event;
30 struct event_context;
31 #include "lib/async_req/async_sock.h"
32 #include "nsswitch/winbind_struct_protocol.h"
33 #include "nsswitch/libwbclient/wbclient.h"
34 #include "nsswitch/libwbclient/wbc_async.h"
36 wbcErr map_wbc_err_from_errno(int error)
38 switch(error) {
39 case EPERM:
40 case EACCES:
41 return WBC_ERR_AUTH_ERROR;
42 case ENOMEM:
43 return WBC_ERR_NO_MEMORY;
44 case EIO:
45 default:
46 return WBC_ERR_UNKNOWN_FAILURE;
50 bool tevent_req_is_wbcerr(struct tevent_req *req, wbcErr *pwbc_err)
52 enum tevent_req_state state;
53 uint64_t error;
54 if (!tevent_req_is_error(req, &state, &error)) {
55 *pwbc_err = WBC_ERR_SUCCESS;
56 return false;
59 switch (state) {
60 case TEVENT_REQ_USER_ERROR:
61 *pwbc_err = error;
62 break;
63 case TEVENT_REQ_TIMED_OUT:
64 *pwbc_err = WBC_ERR_UNKNOWN_FAILURE;
65 break;
66 case TEVENT_REQ_NO_MEMORY:
67 *pwbc_err = WBC_ERR_NO_MEMORY;
68 break;
69 default:
70 *pwbc_err = WBC_ERR_UNKNOWN_FAILURE;
71 break;
73 return true;
76 wbcErr tevent_req_simple_recv_wbcerr(struct tevent_req *req)
78 wbcErr wbc_err;
80 if (tevent_req_is_wbcerr(req, &wbc_err)) {
81 return wbc_err;
84 return WBC_ERR_SUCCESS;
87 struct wb_context {
88 struct tevent_queue *queue;
89 int fd;
90 bool is_priv;
93 static int make_nonstd_fd(int fd)
95 int i;
96 int sys_errno = 0;
97 int fds[3];
98 int num_fds = 0;
100 if (fd == -1) {
101 return -1;
103 while (fd < 3) {
104 fds[num_fds++] = fd;
105 fd = dup(fd);
106 if (fd == -1) {
107 sys_errno = errno;
108 break;
111 for (i=0; i<num_fds; i++) {
112 close(fds[i]);
114 if (fd == -1) {
115 errno = sys_errno;
117 return fd;
120 /****************************************************************************
121 Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available,
122 else
123 if SYSV use O_NDELAY
124 if BSD use FNDELAY
125 Set close on exec also.
126 ****************************************************************************/
128 static int make_safe_fd(int fd)
130 int result, flags;
131 int new_fd = make_nonstd_fd(fd);
133 if (new_fd == -1) {
134 goto fail;
137 /* Socket should be nonblocking. */
139 #ifdef O_NONBLOCK
140 #define FLAG_TO_SET O_NONBLOCK
141 #else
142 #ifdef SYSV
143 #define FLAG_TO_SET O_NDELAY
144 #else /* BSD */
145 #define FLAG_TO_SET FNDELAY
146 #endif
147 #endif
149 if ((flags = fcntl(new_fd, F_GETFL)) == -1) {
150 goto fail;
153 flags |= FLAG_TO_SET;
154 if (fcntl(new_fd, F_SETFL, flags) == -1) {
155 goto fail;
158 #undef FLAG_TO_SET
160 /* Socket should be closed on exec() */
161 #ifdef FD_CLOEXEC
162 result = flags = fcntl(new_fd, F_GETFD, 0);
163 if (flags >= 0) {
164 flags |= FD_CLOEXEC;
165 result = fcntl( new_fd, F_SETFD, flags );
167 if (result < 0) {
168 goto fail;
170 #endif
171 return new_fd;
173 fail:
174 if (new_fd != -1) {
175 int sys_errno = errno;
176 close(new_fd);
177 errno = sys_errno;
179 return -1;
182 struct wb_context *wb_context_init(TALLOC_CTX *mem_ctx)
184 struct wb_context *result;
186 result = talloc(mem_ctx, struct wb_context);
187 if (result == NULL) {
188 return NULL;
190 result->queue = tevent_queue_create(result, "wb_trans");
191 if (result->queue == NULL) {
192 TALLOC_FREE(result);
193 return NULL;
195 result->fd = -1;
196 result->is_priv = false;
197 return result;
200 struct wb_connect_state {
201 int dummy;
204 static void wbc_connect_connected(struct tevent_req *subreq);
206 static struct tevent_req *wb_connect_send(TALLOC_CTX *mem_ctx,
207 struct tevent_context *ev,
208 struct wb_context *wb_ctx,
209 const char *dir)
211 struct tevent_req *result, *subreq;
212 struct wb_connect_state *state;
213 struct sockaddr_un sunaddr;
214 struct stat st;
215 char *path = NULL;
216 wbcErr wbc_err;
218 result = tevent_req_create(mem_ctx, &state, struct wb_connect_state);
219 if (result == NULL) {
220 return NULL;
223 if (wb_ctx->fd != -1) {
224 close(wb_ctx->fd);
225 wb_ctx->fd = -1;
228 /* Check permissions on unix socket directory */
230 if (lstat(dir, &st) == -1) {
231 wbc_err = WBC_ERR_WINBIND_NOT_AVAILABLE;
232 goto post_status;
235 if (!S_ISDIR(st.st_mode) ||
236 (st.st_uid != 0 && st.st_uid != geteuid())) {
237 wbc_err = WBC_ERR_WINBIND_NOT_AVAILABLE;
238 goto post_status;
241 /* Connect to socket */
243 path = talloc_asprintf(talloc_tos(), "%s/%s", dir,
244 WINBINDD_SOCKET_NAME);
245 if (path == NULL) {
246 goto nomem;
249 sunaddr.sun_family = AF_UNIX;
250 strlcpy(sunaddr.sun_path, path, sizeof(sunaddr.sun_path));
251 TALLOC_FREE(path);
253 /* If socket file doesn't exist, don't bother trying to connect
254 with retry. This is an attempt to make the system usable when
255 the winbindd daemon is not running. */
257 if ((lstat(sunaddr.sun_path, &st) == -1)
258 || !S_ISSOCK(st.st_mode)
259 || (st.st_uid != 0 && st.st_uid != geteuid())) {
260 wbc_err = WBC_ERR_WINBIND_NOT_AVAILABLE;
261 goto post_status;
264 wb_ctx->fd = make_safe_fd(socket(AF_UNIX, SOCK_STREAM, 0));
265 if (wb_ctx->fd == -1) {
266 wbc_err = map_wbc_err_from_errno(errno);
267 goto post_status;
270 subreq = async_connect_send(mem_ctx, ev, wb_ctx->fd,
271 (struct sockaddr *)(void *)&sunaddr,
272 sizeof(sunaddr));
273 if (subreq == NULL) {
274 goto nomem;
276 tevent_req_set_callback(subreq, wbc_connect_connected, result);
277 return result;
279 post_status:
280 tevent_req_error(result, wbc_err);
281 return tevent_req_post(result, ev);
282 nomem:
283 TALLOC_FREE(result);
284 return NULL;
287 static void wbc_connect_connected(struct tevent_req *subreq)
289 struct tevent_req *req = tevent_req_callback_data(
290 subreq, struct tevent_req);
291 int res, err;
293 res = async_connect_recv(subreq, &err);
294 TALLOC_FREE(subreq);
295 if (res == -1) {
296 tevent_req_error(req, map_wbc_err_from_errno(err));
297 return;
299 tevent_req_done(req);
302 static wbcErr wb_connect_recv(struct tevent_req *req)
304 return tevent_req_simple_recv_wbcerr(req);
307 static const char *winbindd_socket_dir(void)
309 #ifdef SOCKET_WRAPPER
310 const char *env_dir;
312 env_dir = getenv(WINBINDD_SOCKET_DIR_ENVVAR);
313 if (env_dir) {
314 return env_dir;
316 #endif
318 return WINBINDD_SOCKET_DIR;
321 struct wb_open_pipe_state {
322 struct wb_context *wb_ctx;
323 struct tevent_context *ev;
324 bool need_priv;
325 struct winbindd_request wb_req;
328 static void wb_open_pipe_connect_nonpriv_done(struct tevent_req *subreq);
329 static void wb_open_pipe_ping_done(struct tevent_req *subreq);
330 static void wb_open_pipe_getpriv_done(struct tevent_req *subreq);
331 static void wb_open_pipe_connect_priv_done(struct tevent_req *subreq);
333 static struct tevent_req *wb_open_pipe_send(TALLOC_CTX *mem_ctx,
334 struct tevent_context *ev,
335 struct wb_context *wb_ctx,
336 bool need_priv)
338 struct tevent_req *result, *subreq;
339 struct wb_open_pipe_state *state;
341 result = tevent_req_create(mem_ctx, &state, struct wb_open_pipe_state);
342 if (result == NULL) {
343 return NULL;
345 state->wb_ctx = wb_ctx;
346 state->ev = ev;
347 state->need_priv = need_priv;
349 if (wb_ctx->fd != -1) {
350 close(wb_ctx->fd);
351 wb_ctx->fd = -1;
354 subreq = wb_connect_send(state, ev, wb_ctx, winbindd_socket_dir());
355 if (subreq == NULL) {
356 goto fail;
358 tevent_req_set_callback(subreq, wb_open_pipe_connect_nonpriv_done,
359 result);
360 return result;
362 fail:
363 TALLOC_FREE(result);
364 return NULL;
367 static void wb_open_pipe_connect_nonpriv_done(struct tevent_req *subreq)
369 struct tevent_req *req = tevent_req_callback_data(
370 subreq, struct tevent_req);
371 struct wb_open_pipe_state *state = tevent_req_data(
372 req, struct wb_open_pipe_state);
373 wbcErr wbc_err;
375 wbc_err = wb_connect_recv(subreq);
376 TALLOC_FREE(subreq);
377 if (!WBC_ERROR_IS_OK(wbc_err)) {
378 state->wb_ctx->is_priv = true;
379 tevent_req_error(req, wbc_err);
380 return;
383 ZERO_STRUCT(state->wb_req);
384 state->wb_req.cmd = WINBINDD_INTERFACE_VERSION;
385 state->wb_req.pid = getpid();
387 subreq = wb_simple_trans_send(state, state->ev, NULL,
388 state->wb_ctx->fd, &state->wb_req);
389 if (tevent_req_nomem(subreq, req)) {
390 return;
392 tevent_req_set_callback(subreq, wb_open_pipe_ping_done, req);
395 static void wb_open_pipe_ping_done(struct tevent_req *subreq)
397 struct tevent_req *req = tevent_req_callback_data(
398 subreq, struct tevent_req);
399 struct wb_open_pipe_state *state = tevent_req_data(
400 req, struct wb_open_pipe_state);
401 struct winbindd_response *wb_resp;
402 int ret, err;
404 ret = wb_simple_trans_recv(subreq, state, &wb_resp, &err);
405 TALLOC_FREE(subreq);
406 if (ret == -1) {
407 tevent_req_error(req, map_wbc_err_from_errno(err));
408 return;
411 if (!state->need_priv) {
412 tevent_req_done(req);
413 return;
416 state->wb_req.cmd = WINBINDD_PRIV_PIPE_DIR;
417 state->wb_req.pid = getpid();
419 subreq = wb_simple_trans_send(state, state->ev, NULL,
420 state->wb_ctx->fd, &state->wb_req);
421 if (tevent_req_nomem(subreq, req)) {
422 return;
424 tevent_req_set_callback(subreq, wb_open_pipe_getpriv_done, req);
427 static void wb_open_pipe_getpriv_done(struct tevent_req *subreq)
429 struct tevent_req *req = tevent_req_callback_data(
430 subreq, struct tevent_req);
431 struct wb_open_pipe_state *state = tevent_req_data(
432 req, struct wb_open_pipe_state);
433 struct winbindd_response *wb_resp = NULL;
434 int ret, err;
436 ret = wb_simple_trans_recv(subreq, state, &wb_resp, &err);
437 TALLOC_FREE(subreq);
438 if (ret == -1) {
439 tevent_req_error(req, map_wbc_err_from_errno(err));
440 return;
443 close(state->wb_ctx->fd);
444 state->wb_ctx->fd = -1;
446 subreq = wb_connect_send(state, state->ev, state->wb_ctx,
447 (char *)wb_resp->extra_data.data);
448 TALLOC_FREE(wb_resp);
449 if (tevent_req_nomem(subreq, req)) {
450 return;
452 tevent_req_set_callback(subreq, wb_open_pipe_connect_priv_done, req);
455 static void wb_open_pipe_connect_priv_done(struct tevent_req *subreq)
457 struct tevent_req *req = tevent_req_callback_data(
458 subreq, struct tevent_req);
459 struct wb_open_pipe_state *state = tevent_req_data(
460 req, struct wb_open_pipe_state);
461 wbcErr wbc_err;
463 wbc_err = wb_connect_recv(subreq);
464 TALLOC_FREE(subreq);
465 if (!WBC_ERROR_IS_OK(wbc_err)) {
466 tevent_req_error(req, wbc_err);
467 return;
469 state->wb_ctx->is_priv = true;
470 tevent_req_done(req);
473 static wbcErr wb_open_pipe_recv(struct tevent_req *req)
475 return tevent_req_simple_recv_wbcerr(req);
478 struct wb_trans_state {
479 struct wb_trans_state *prev, *next;
480 struct wb_context *wb_ctx;
481 struct tevent_context *ev;
482 struct winbindd_request *wb_req;
483 struct winbindd_response *wb_resp;
484 bool need_priv;
487 static bool closed_fd(int fd)
489 struct timeval tv;
490 fd_set r_fds;
491 int selret;
493 if (fd == -1) {
494 return true;
497 FD_ZERO(&r_fds);
498 FD_SET(fd, &r_fds);
499 ZERO_STRUCT(tv);
501 selret = select(fd+1, &r_fds, NULL, NULL, &tv);
502 if (selret == -1) {
503 return true;
505 if (selret == 0) {
506 return false;
508 return (FD_ISSET(fd, &r_fds));
511 static void wb_trans_trigger(struct tevent_req *req, void *private_data);
512 static void wb_trans_connect_done(struct tevent_req *subreq);
513 static void wb_trans_done(struct tevent_req *subreq);
514 static void wb_trans_retry_wait_done(struct tevent_req *subreq);
516 struct tevent_req *wb_trans_send(TALLOC_CTX *mem_ctx,
517 struct tevent_context *ev,
518 struct wb_context *wb_ctx, bool need_priv,
519 struct winbindd_request *wb_req)
521 struct tevent_req *req;
522 struct wb_trans_state *state;
524 req = tevent_req_create(mem_ctx, &state, struct wb_trans_state);
525 if (req == NULL) {
526 return NULL;
528 state->wb_ctx = wb_ctx;
529 state->ev = ev;
530 state->wb_req = wb_req;
531 state->need_priv = need_priv;
533 if (!tevent_queue_add(wb_ctx->queue, ev, req, wb_trans_trigger,
534 NULL)) {
535 tevent_req_nomem(NULL, req);
536 return tevent_req_post(req, ev);
538 return req;
541 static void wb_trans_trigger(struct tevent_req *req, void *private_data)
543 struct wb_trans_state *state = tevent_req_data(
544 req, struct wb_trans_state);
545 struct tevent_req *subreq;
547 if ((state->wb_ctx->fd != -1) && closed_fd(state->wb_ctx->fd)) {
548 close(state->wb_ctx->fd);
549 state->wb_ctx->fd = -1;
552 if ((state->wb_ctx->fd == -1)
553 || (state->need_priv && !state->wb_ctx->is_priv)) {
554 subreq = wb_open_pipe_send(state, state->ev, state->wb_ctx,
555 state->need_priv);
556 if (tevent_req_nomem(subreq, req)) {
557 return;
559 tevent_req_set_callback(subreq, wb_trans_connect_done, req);
560 return;
563 state->wb_req->pid = getpid();
565 subreq = wb_simple_trans_send(state, state->ev, NULL,
566 state->wb_ctx->fd, state->wb_req);
567 if (tevent_req_nomem(subreq, req)) {
568 return;
570 tevent_req_set_callback(subreq, wb_trans_done, req);
573 static bool wb_trans_retry(struct tevent_req *req,
574 struct wb_trans_state *state,
575 wbcErr wbc_err)
577 struct tevent_req *subreq;
579 if (WBC_ERROR_IS_OK(wbc_err)) {
580 return false;
583 if (wbc_err == WBC_ERR_WINBIND_NOT_AVAILABLE) {
585 * Winbind not around or we can't connect to the pipe. Fail
586 * immediately.
588 tevent_req_error(req, wbc_err);
589 return true;
593 * The transfer as such failed, retry after one second
596 if (state->wb_ctx->fd != -1) {
597 close(state->wb_ctx->fd);
598 state->wb_ctx->fd = -1;
601 subreq = tevent_wakeup_send(state, state->ev,
602 timeval_current_ofs(1, 0));
603 if (tevent_req_nomem(subreq, req)) {
604 return true;
606 tevent_req_set_callback(subreq, wb_trans_retry_wait_done, req);
607 return true;
610 static void wb_trans_retry_wait_done(struct tevent_req *subreq)
612 struct tevent_req *req = tevent_req_callback_data(
613 subreq, struct tevent_req);
614 struct wb_trans_state *state = tevent_req_data(
615 req, struct wb_trans_state);
616 bool ret;
618 ret = tevent_wakeup_recv(subreq);
619 TALLOC_FREE(subreq);
620 if (!ret) {
621 tevent_req_error(req, WBC_ERR_UNKNOWN_FAILURE);
622 return;
625 subreq = wb_open_pipe_send(state, state->ev, state->wb_ctx,
626 state->need_priv);
627 if (tevent_req_nomem(subreq, req)) {
628 return;
630 tevent_req_set_callback(subreq, wb_trans_connect_done, req);
633 static void wb_trans_connect_done(struct tevent_req *subreq)
635 struct tevent_req *req = tevent_req_callback_data(
636 subreq, struct tevent_req);
637 struct wb_trans_state *state = tevent_req_data(
638 req, struct wb_trans_state);
639 wbcErr wbc_err;
641 wbc_err = wb_open_pipe_recv(subreq);
642 TALLOC_FREE(subreq);
644 if (wb_trans_retry(req, state, wbc_err)) {
645 return;
648 subreq = wb_simple_trans_send(state, state->ev, NULL,
649 state->wb_ctx->fd, state->wb_req);
650 if (tevent_req_nomem(subreq, req)) {
651 return;
653 tevent_req_set_callback(subreq, wb_trans_done, req);
656 static void wb_trans_done(struct tevent_req *subreq)
658 struct tevent_req *req = tevent_req_callback_data(
659 subreq, struct tevent_req);
660 struct wb_trans_state *state = tevent_req_data(
661 req, struct wb_trans_state);
662 int ret, err;
664 ret = wb_simple_trans_recv(subreq, state, &state->wb_resp, &err);
665 TALLOC_FREE(subreq);
666 if ((ret == -1)
667 && wb_trans_retry(req, state, map_wbc_err_from_errno(err))) {
668 return;
671 tevent_req_done(req);
674 wbcErr wb_trans_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
675 struct winbindd_response **presponse)
677 struct wb_trans_state *state = tevent_req_data(
678 req, struct wb_trans_state);
679 wbcErr wbc_err;
681 if (tevent_req_is_wbcerr(req, &wbc_err)) {
682 return wbc_err;
685 *presponse = talloc_move(mem_ctx, &state->wb_resp);
686 return WBC_ERR_SUCCESS;