torture4: Replace direct netlogon ping calls with netlogon_pings()
[samba4-gss.git] / nsswitch / wb_common.c
blob9898b037c5560f2ba2dd5e0c5990473877bc97ef
1 /*
2 Unix SMB/CIFS implementation.
4 winbind client common code
6 Copyright (C) Tim Potter 2000
7 Copyright (C) Andrew Tridgell 2000
8 Copyright (C) Andrew Bartlett 2002
9 Copyright (C) Matthew Newton 2015
12 This library is free software; you can redistribute it and/or
13 modify it under the terms of the GNU Lesser General Public
14 License as published by the Free Software Foundation; either
15 version 3 of the License, or (at your option) any later version.
17 This library is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 Library General Public License for more details.
22 You should have received a copy of the GNU Lesser General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "replace.h"
27 #include "system/select.h"
28 #include "winbind_client.h"
29 #include "lib/util/dlinklist.h"
30 #include <assert.h>
32 #ifdef HAVE_PTHREAD_H
33 #include <pthread.h>
34 #endif
36 static __thread char client_name[32];
38 /* Global context */
40 struct winbindd_context {
41 struct winbindd_context *prev, *next;
42 int winbindd_fd; /* winbind file descriptor */
43 bool is_privileged; /* using the privileged socket? */
44 pid_t our_pid; /* calling process pid */
45 bool autofree; /* this is a thread global context */
48 static struct wb_global_ctx {
49 #ifdef HAVE_PTHREAD
50 pthread_once_t control;
51 pthread_key_t key;
52 bool key_initialized;
53 #ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
54 #define WB_GLOBAL_MUTEX_INITIALIZER PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
55 #else
56 #define WB_GLOBAL_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
57 #endif
58 #define WB_GLOBAL_LIST_LOCK do { \
59 int __pret = pthread_mutex_lock(&wb_global_ctx.list_mutex); \
60 assert(__pret == 0); \
61 } while(0)
62 #define WB_GLOBAL_LIST_UNLOCK do { \
63 int __pret = pthread_mutex_unlock(&wb_global_ctx.list_mutex); \
64 assert(__pret == 0); \
65 } while(0)
66 pthread_mutex_t list_mutex;
67 #else /* => not HAVE_PTHREAD */
68 #define WB_GLOBAL_LIST_LOCK do { } while(0)
69 #define WB_GLOBAL_LIST_UNLOCK do { } while(0)
70 #endif /* not HAVE_PTHREAD */
71 struct winbindd_context *list;
72 } wb_global_ctx = {
73 #ifdef HAVE_PTHREAD
74 .control = PTHREAD_ONCE_INIT,
75 .list_mutex = WB_GLOBAL_MUTEX_INITIALIZER,
76 #endif
77 .list = NULL,
80 static void winbind_close_sock(struct winbindd_context *ctx);
81 static void winbind_ctx_free_locked(struct winbindd_context *ctx);
82 static void winbind_cleanup_list(void);
84 #ifdef HAVE_PTHREAD
85 static void wb_thread_ctx_initialize(void);
87 static void wb_atfork_prepare(void)
89 WB_GLOBAL_LIST_LOCK;
92 static void wb_atfork_parent(void)
94 WB_GLOBAL_LIST_UNLOCK;
97 static void wb_atfork_child(void)
99 wb_global_ctx.list_mutex = (pthread_mutex_t)WB_GLOBAL_MUTEX_INITIALIZER;
101 if (wb_global_ctx.key_initialized) {
102 int ret;
105 * After a fork the child still believes
106 * it is the same thread as in the parent.
107 * So pthread_getspecific() would return the
108 * value of the thread that called fork().
110 * But we don't want that behavior, so
111 * we just clear the reference and let
112 * winbind_cleanup_list() below 'autofree'
113 * the parent threads global context.
115 ret = pthread_setspecific(wb_global_ctx.key, NULL);
116 assert(ret == 0);
120 * But we need to close/cleanup the global state
121 * of the parents threads.
123 winbind_cleanup_list();
126 static void wb_thread_ctx_destructor(void *p)
128 struct winbindd_context *ctx = (struct winbindd_context *)p;
130 winbindd_ctx_free(ctx);
133 static void wb_thread_ctx_initialize(void)
135 int ret;
137 ret = pthread_atfork(wb_atfork_prepare,
138 wb_atfork_parent,
139 wb_atfork_child);
140 assert(ret == 0);
142 ret = pthread_key_create(&wb_global_ctx.key,
143 wb_thread_ctx_destructor);
144 assert(ret == 0);
146 wb_global_ctx.key_initialized = true;
149 static struct winbindd_context *get_wb_thread_ctx(void)
151 struct winbindd_context *ctx = NULL;
152 int ret;
154 ret = pthread_once(&wb_global_ctx.control,
155 wb_thread_ctx_initialize);
156 assert(ret == 0);
158 ctx = (struct winbindd_context *)pthread_getspecific(
159 wb_global_ctx.key);
160 if (ctx != NULL) {
161 return ctx;
164 ctx = malloc(sizeof(struct winbindd_context));
165 if (ctx == NULL) {
166 return NULL;
169 *ctx = (struct winbindd_context) {
170 .winbindd_fd = -1,
171 .is_privileged = false,
172 .our_pid = 0,
173 .autofree = true,
176 WB_GLOBAL_LIST_LOCK;
177 DLIST_ADD_END(wb_global_ctx.list, ctx);
178 WB_GLOBAL_LIST_UNLOCK;
180 ret = pthread_setspecific(wb_global_ctx.key, ctx);
181 if (ret != 0) {
182 free(ctx);
183 return NULL;
185 return ctx;
187 #endif /* HAVE_PTHREAD */
189 static struct winbindd_context *get_wb_global_ctx(void)
191 struct winbindd_context *ctx = NULL;
192 #ifndef HAVE_PTHREAD
193 static struct winbindd_context _ctx = {
194 .winbindd_fd = -1,
195 .is_privileged = false,
196 .our_pid = 0,
197 .autofree = false,
199 #endif
201 #ifdef HAVE_PTHREAD
202 ctx = get_wb_thread_ctx();
203 #else
204 ctx = &_ctx;
205 if (ctx->prev == NULL && ctx->next == NULL) {
206 DLIST_ADD_END(wb_global_ctx.list, ctx);
208 #endif
210 return ctx;
213 void winbind_set_client_name(const char *name)
215 if (name == NULL || strlen(name) == 0) {
216 return;
219 (void)snprintf(client_name, sizeof(client_name), "%s", name);
222 static const char *winbind_get_client_name(void)
224 if (client_name[0] == '\0') {
225 const char *progname = getprogname();
226 int len;
228 if (progname == NULL) {
229 progname = "<unknown>";
232 len = snprintf(client_name,
233 sizeof(client_name),
234 "%s",
235 progname);
236 if (len <= 0) {
237 return progname;
241 return client_name;
244 /* Initialise a request structure */
246 static void winbindd_init_request(struct winbindd_request *request,
247 int request_type)
249 request->length = sizeof(struct winbindd_request);
251 request->cmd = (enum winbindd_cmd)request_type;
252 request->pid = getpid();
254 (void)snprintf(request->client_name,
255 sizeof(request->client_name),
256 "%s",
257 winbind_get_client_name());
260 /* Initialise a response structure */
262 static void init_response(struct winbindd_response *response)
264 /* Initialise return value */
266 response->result = WINBINDD_ERROR;
269 /* Close established socket */
271 static void winbind_close_sock(struct winbindd_context *ctx)
273 if (!ctx) {
274 return;
277 if (ctx->winbindd_fd != -1) {
278 close(ctx->winbindd_fd);
279 ctx->winbindd_fd = -1;
283 static void winbind_ctx_free_locked(struct winbindd_context *ctx)
285 winbind_close_sock(ctx);
286 DLIST_REMOVE(wb_global_ctx.list, ctx);
287 free(ctx);
290 static void winbind_cleanup_list(void)
292 struct winbindd_context *ctx = NULL, *next = NULL;
294 WB_GLOBAL_LIST_LOCK;
295 for (ctx = wb_global_ctx.list; ctx != NULL; ctx = next) {
296 next = ctx->next;
298 if (ctx->autofree) {
299 winbind_ctx_free_locked(ctx);
300 } else {
301 winbind_close_sock(ctx);
304 WB_GLOBAL_LIST_UNLOCK;
307 /* Destructor for global context to ensure fd is closed */
309 #ifdef HAVE_DESTRUCTOR_ATTRIBUTE
310 __attribute__((destructor))
311 #elif defined (HAVE_PRAGMA_FINI)
312 #pragma fini (winbind_destructor)
313 #endif
314 static void winbind_destructor(void)
316 #ifdef HAVE_PTHREAD
317 if (wb_global_ctx.key_initialized) {
318 int ret;
319 ret = pthread_key_delete(wb_global_ctx.key);
320 assert(ret == 0);
321 wb_global_ctx.key_initialized = false;
324 wb_global_ctx.control = (pthread_once_t)PTHREAD_ONCE_INIT;
325 #endif /* HAVE_PTHREAD */
327 winbind_cleanup_list();
330 #define CONNECT_TIMEOUT 30
332 /* Make sure socket handle isn't stdin, stdout or stderr */
333 #define RECURSION_LIMIT 3
335 static int make_nonstd_fd_internals(int fd, int limit /* Recursion limiter */)
337 int new_fd;
338 if (fd >= 0 && fd <= 2) {
339 #ifdef F_DUPFD
340 if ((new_fd = fcntl(fd, F_DUPFD, 3)) == -1) {
341 return -1;
343 /* Paranoia */
344 if (new_fd < 3) {
345 close(new_fd);
346 return -1;
348 close(fd);
349 return new_fd;
350 #else
351 if (limit <= 0)
352 return -1;
354 new_fd = dup(fd);
355 if (new_fd == -1)
356 return -1;
358 /* use the program stack to hold our list of FDs to close */
359 new_fd = make_nonstd_fd_internals(new_fd, limit - 1);
360 close(fd);
361 return new_fd;
362 #endif
364 return fd;
367 /****************************************************************************
368 Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available,
369 else
370 if SYSV use O_NDELAY
371 if BSD use FNDELAY
372 Set close on exec also.
373 ****************************************************************************/
375 static int make_safe_fd(int fd)
377 int result, flags;
378 int new_fd = make_nonstd_fd_internals(fd, RECURSION_LIMIT);
379 if (new_fd == -1) {
380 close(fd);
381 return -1;
384 /* Socket should be nonblocking. */
385 #ifdef O_NONBLOCK
386 #define FLAG_TO_SET O_NONBLOCK
387 #else
388 #ifdef SYSV
389 #define FLAG_TO_SET O_NDELAY
390 #else /* BSD */
391 #define FLAG_TO_SET FNDELAY
392 #endif
393 #endif
395 if ((flags = fcntl(new_fd, F_GETFL)) == -1) {
396 close(new_fd);
397 return -1;
400 flags |= FLAG_TO_SET;
401 if (fcntl(new_fd, F_SETFL, flags) == -1) {
402 close(new_fd);
403 return -1;
406 #undef FLAG_TO_SET
408 /* Socket should be closed on exec() */
409 #ifdef FD_CLOEXEC
410 result = flags = fcntl(new_fd, F_GETFD, 0);
411 if (flags >= 0) {
412 flags |= FD_CLOEXEC;
413 result = fcntl( new_fd, F_SETFD, flags );
415 if (result < 0) {
416 close(new_fd);
417 return -1;
419 #endif
420 return new_fd;
424 * @internal
426 * @brief Check if we talk to the privileged pipe which should be owned by root.
428 * This checks if we have uid_wrapper running and if this is the case it will
429 * allow one to connect to the winbind privileged pipe even it is not owned by root.
431 * @param[in] uid The uid to check if we can safely talk to the pipe.
433 * @return If we have access it returns true, else false.
435 static bool winbind_privileged_pipe_is_root(uid_t uid)
437 if (uid == 0) {
438 return true;
441 if (uid_wrapper_enabled()) {
442 return true;
445 return false;
448 /* Connect to winbindd socket */
450 static int winbind_named_pipe_sock(const char *dir)
452 struct sockaddr_un sunaddr;
453 struct stat st;
454 int fd;
455 int wait_time;
456 int slept;
457 int ret;
459 /* Check permissions on unix socket directory */
461 if (lstat(dir, &st) == -1) {
462 errno = ENOENT;
463 return -1;
467 * This tells us that the pipe is owned by a privileged
468 * process, as we will be sending passwords to it.
470 if (!S_ISDIR(st.st_mode) ||
471 !winbind_privileged_pipe_is_root(st.st_uid)) {
472 errno = ENOENT;
473 return -1;
476 /* Connect to socket */
478 sunaddr = (struct sockaddr_un) { .sun_family = AF_UNIX };
480 ret = snprintf(sunaddr.sun_path, sizeof(sunaddr.sun_path),
481 "%s/%s", dir, WINBINDD_SOCKET_NAME);
482 if ((ret == -1) || (ret >= sizeof(sunaddr.sun_path))) {
483 errno = ENAMETOOLONG;
484 return -1;
487 /* If socket file doesn't exist, don't bother trying to connect
488 with retry. This is an attempt to make the system usable when
489 the winbindd daemon is not running. */
491 if (lstat(sunaddr.sun_path, &st) == -1) {
492 errno = ENOENT;
493 return -1;
496 /* Check permissions on unix socket file */
499 * This tells us that the pipe is owned by a privileged
500 * process, as we will be sending passwords to it.
502 if (!S_ISSOCK(st.st_mode) ||
503 !winbind_privileged_pipe_is_root(st.st_uid)) {
504 errno = ENOENT;
505 return -1;
508 /* Connect to socket */
510 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
511 return -1;
514 /* Set socket non-blocking and close on exec. */
516 if ((fd = make_safe_fd( fd)) == -1) {
517 return fd;
520 for (wait_time = 0; connect(fd, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) == -1;
521 wait_time += slept) {
522 struct pollfd pfd;
523 int connect_errno = 0;
524 socklen_t errnosize;
526 if (wait_time >= CONNECT_TIMEOUT)
527 goto error_out;
529 switch (errno) {
530 case EINPROGRESS:
531 pfd.fd = fd;
532 pfd.events = POLLOUT;
534 ret = poll(&pfd, 1, (CONNECT_TIMEOUT - wait_time) * 1000);
536 if (ret > 0) {
537 errnosize = sizeof(connect_errno);
539 ret = getsockopt(fd, SOL_SOCKET,
540 SO_ERROR, &connect_errno, &errnosize);
542 if (ret >= 0 && connect_errno == 0) {
543 /* Connect succeed */
544 goto out;
548 slept = CONNECT_TIMEOUT;
549 break;
550 case EAGAIN:
551 slept = rand() % 3 + 1;
552 sleep(slept);
553 break;
554 default:
555 goto error_out;
560 out:
562 return fd;
564 error_out:
566 close(fd);
567 return -1;
570 static const char *winbindd_socket_dir(void)
572 if (nss_wrapper_enabled()) {
573 const char *env_dir;
575 env_dir = getenv("SELFTEST_WINBINDD_SOCKET_DIR");
576 if (env_dir != NULL) {
577 return env_dir;
581 return WINBINDD_SOCKET_DIR;
584 /* Connect to winbindd socket */
586 static int winbind_open_pipe_sock(struct winbindd_context *ctx,
587 int recursing, int need_priv)
589 #ifdef HAVE_UNIXSOCKET
590 struct winbindd_request request;
591 struct winbindd_response response;
593 ZERO_STRUCT(request);
594 ZERO_STRUCT(response);
596 if (!ctx) {
597 return -1;
600 if (ctx->our_pid != getpid()) {
601 winbind_close_sock(ctx);
602 ctx->our_pid = getpid();
605 if ((need_priv != 0) && !ctx->is_privileged) {
606 winbind_close_sock(ctx);
609 if (ctx->winbindd_fd != -1) {
610 return ctx->winbindd_fd;
613 if (recursing) {
614 return -1;
617 ctx->winbindd_fd = winbind_named_pipe_sock(winbindd_socket_dir());
619 if (ctx->winbindd_fd == -1) {
620 return -1;
623 ctx->is_privileged = false;
625 /* version-check the socket */
627 request.wb_flags = WBFLAG_RECURSE;
628 if ((winbindd_request_response(ctx, WINBINDD_INTERFACE_VERSION, &request,
629 &response) != NSS_STATUS_SUCCESS) ||
630 (response.data.interface_version != WINBIND_INTERFACE_VERSION)) {
631 winbind_close_sock(ctx);
632 return -1;
635 if (need_priv == 0) {
636 return ctx->winbindd_fd;
639 /* try and get priv pipe */
641 request.wb_flags = WBFLAG_RECURSE;
643 /* Note that response needs to be initialized to avoid
644 * crashing on clean up after WINBINDD_PRIV_PIPE_DIR call failed
645 * as interface version (from the first request) returned as a fstring,
646 * thus response.extra_data.data will not be NULL even though
647 * winbindd response did not write over it due to a failure */
648 ZERO_STRUCT(response);
649 if (winbindd_request_response(ctx, WINBINDD_PRIV_PIPE_DIR, &request,
650 &response) == NSS_STATUS_SUCCESS) {
651 int fd;
652 fd = winbind_named_pipe_sock((char *)response.extra_data.data);
653 if (fd != -1) {
654 close(ctx->winbindd_fd);
655 ctx->winbindd_fd = fd;
656 ctx->is_privileged = true;
659 SAFE_FREE(response.extra_data.data);
662 if (!ctx->is_privileged) {
663 return -1;
666 return ctx->winbindd_fd;
667 #else
668 return -1;
669 #endif /* HAVE_UNIXSOCKET */
672 /* Write data to winbindd socket */
674 static ssize_t winbind_write_sock(struct winbindd_context *ctx,
675 void *buffer,
676 size_t count,
677 int recursing,
678 int need_priv)
680 int fd;
681 ssize_t nwritten;
683 /* Open connection to winbind daemon */
685 restart:
687 fd = winbind_open_pipe_sock(ctx, recursing, need_priv);
688 if (fd == -1) {
689 errno = ENOENT;
690 return -1;
693 /* Write data to socket */
695 nwritten = 0;
697 while(nwritten < count) {
698 struct pollfd pfd;
699 ssize_t result;
700 int ret;
702 /* Catch pipe close on other end by checking if a read()
703 call would not block by calling poll(). */
705 pfd.fd = fd;
706 pfd.events = POLLIN|POLLOUT|POLLHUP;
708 ret = poll(&pfd, 1, -1);
709 if (ret == -1) {
710 winbind_close_sock(ctx);
711 return -1; /* poll error */
714 /* Write should be OK if fd not available for reading */
716 if ((ret == 1) && (pfd.revents & (POLLIN|POLLHUP|POLLERR))) {
718 /* Pipe has closed on remote end */
720 winbind_close_sock(ctx);
721 goto restart;
724 /* Do the write */
726 result = write(fd, (char *)buffer + nwritten,
727 count - nwritten);
729 if ((result == -1) || (result == 0)) {
731 /* Write failed */
733 winbind_close_sock(ctx);
734 return -1;
737 nwritten += result;
740 return nwritten;
743 /* Read data from winbindd socket */
745 static int winbind_read_sock(struct winbindd_context *ctx,
746 void *buffer, int count)
748 int fd;
749 int nread = 0;
750 int total_time = 0;
752 fd = winbind_open_pipe_sock(ctx, false, false);
753 if (fd == -1) {
754 return -1;
757 /* Read data from socket */
758 while(nread < count) {
759 struct pollfd pfd;
760 int ret;
762 /* Catch pipe close on other end by checking if a read()
763 call would not block by calling poll(). */
765 pfd.fd = fd;
766 pfd.events = POLLIN|POLLHUP;
768 /* Wait for 5 seconds for a reply. May need to parameterise this... */
770 ret = poll(&pfd, 1, 5000);
771 if (ret == -1) {
772 winbind_close_sock(ctx);
773 return -1; /* poll error */
776 if (ret == 0) {
777 /* Not ready for read yet... */
778 if (total_time >= 300) {
779 /* Timeout */
780 winbind_close_sock(ctx);
781 return -1;
783 total_time += 5;
784 continue;
787 if ((ret == 1) && (pfd.revents & (POLLIN|POLLHUP|POLLERR))) {
789 /* Do the Read */
791 int result = read(fd, (char *)buffer + nread,
792 count - nread);
794 if ((result == -1) || (result == 0)) {
796 /* Read failed. I think the only useful thing we
797 can do here is just return -1 and fail since the
798 transaction has failed half way through. */
800 winbind_close_sock(ctx);
801 return -1;
804 nread += result;
809 return nread;
812 /* Read reply */
814 static int winbindd_read_reply(struct winbindd_context *ctx,
815 struct winbindd_response *response)
817 int result1, result2 = 0;
819 if (!response) {
820 return -1;
823 /* Read fixed length response */
825 result1 = winbind_read_sock(ctx, response,
826 sizeof(struct winbindd_response));
828 /* We actually send the pointer value of the extra_data field from
829 the server. This has no meaning in the client's address space
830 so we clear it out. */
832 response->extra_data.data = NULL;
834 if (result1 == -1) {
835 return -1;
838 if (response->length < sizeof(struct winbindd_response)) {
839 return -1;
842 /* Read variable length response */
844 if (response->length > sizeof(struct winbindd_response)) {
845 int extra_data_len = response->length -
846 sizeof(struct winbindd_response);
848 /* Mallocate memory for extra data */
850 if (!(response->extra_data.data = malloc(extra_data_len))) {
851 return -1;
854 result2 = winbind_read_sock(ctx, response->extra_data.data,
855 extra_data_len);
856 if (result2 == -1) {
857 winbindd_free_response(response);
858 return -1;
862 /* Return total amount of data read */
864 return result1 + result2;
868 * send simple types of requests
871 static NSS_STATUS winbindd_send_request(
872 struct winbindd_context *ctx,
873 int req_type,
874 int need_priv,
875 struct winbindd_request *request)
877 struct winbindd_request lrequest;
879 /* Check for our tricky environment variable */
881 if (winbind_env_set()) {
882 return NSS_STATUS_NOTFOUND;
885 if (!request) {
886 ZERO_STRUCT(lrequest);
887 request = &lrequest;
890 /* Fill in request and send down pipe */
892 winbindd_init_request(request, req_type);
894 if (winbind_write_sock(ctx, request, sizeof(*request),
895 request->wb_flags & WBFLAG_RECURSE,
896 need_priv) == -1)
898 /* Set ENOENT for consistency. Required by some apps */
899 errno = ENOENT;
901 return NSS_STATUS_UNAVAIL;
904 if ((request->extra_len != 0) &&
905 (winbind_write_sock(ctx, request->extra_data.data,
906 request->extra_len,
907 request->wb_flags & WBFLAG_RECURSE,
908 need_priv) == -1))
910 /* Set ENOENT for consistency. Required by some apps */
911 errno = ENOENT;
913 return NSS_STATUS_UNAVAIL;
916 return NSS_STATUS_SUCCESS;
920 * Get results from winbindd request
923 static NSS_STATUS winbindd_get_response(struct winbindd_context *ctx,
924 struct winbindd_response *response)
926 struct winbindd_response lresponse;
928 if (!response) {
929 ZERO_STRUCT(lresponse);
930 response = &lresponse;
933 init_response(response);
935 /* Wait for reply */
936 if (winbindd_read_reply(ctx, response) == -1) {
937 /* Set ENOENT for consistency. Required by some apps */
938 errno = ENOENT;
940 return NSS_STATUS_UNAVAIL;
943 /* Throw away extra data if client didn't request it */
944 if (response == &lresponse) {
945 winbindd_free_response(response);
948 /* Copy reply data from socket */
949 if (response->result != WINBINDD_OK) {
950 return NSS_STATUS_NOTFOUND;
953 return NSS_STATUS_SUCCESS;
956 /* Handle simple types of requests */
958 NSS_STATUS winbindd_request_response(struct winbindd_context *ctx,
959 int req_type,
960 struct winbindd_request *request,
961 struct winbindd_response *response)
963 NSS_STATUS status = NSS_STATUS_UNAVAIL;
965 if (ctx == NULL) {
966 ctx = get_wb_global_ctx();
969 status = winbindd_send_request(ctx, req_type, 0, request);
970 if (status != NSS_STATUS_SUCCESS) {
971 goto out;
973 status = winbindd_get_response(ctx, response);
975 out:
976 return status;
979 NSS_STATUS winbindd_priv_request_response(struct winbindd_context *ctx,
980 int req_type,
981 struct winbindd_request *request,
982 struct winbindd_response *response)
984 NSS_STATUS status = NSS_STATUS_UNAVAIL;
986 if (ctx == NULL) {
987 ctx = get_wb_global_ctx();
990 status = winbindd_send_request(ctx, req_type, 1, request);
991 if (status != NSS_STATUS_SUCCESS) {
992 goto out;
994 status = winbindd_get_response(ctx, response);
996 out:
997 return status;
1000 /* Create and free winbindd context */
1002 struct winbindd_context *winbindd_ctx_create(void)
1004 struct winbindd_context *ctx;
1006 ctx = calloc(1, sizeof(struct winbindd_context));
1008 if (!ctx) {
1009 return NULL;
1012 ctx->winbindd_fd = -1;
1014 WB_GLOBAL_LIST_LOCK;
1015 DLIST_ADD_END(wb_global_ctx.list, ctx);
1016 WB_GLOBAL_LIST_UNLOCK;
1018 return ctx;
1021 void winbindd_ctx_free(struct winbindd_context *ctx)
1023 WB_GLOBAL_LIST_LOCK;
1024 winbind_ctx_free_locked(ctx);
1025 WB_GLOBAL_LIST_UNLOCK;