talloc: release 2.4.2
[Samba.git] / nsswitch / wb_common.c
blobb7f84435a4ee8f57ded9bd3761e4e93b020e7ad3
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 int winbind_write_sock(struct winbindd_context *ctx, void *buffer,
675 int count, int recursing, int need_priv)
677 int fd, result, nwritten;
679 /* Open connection to winbind daemon */
681 restart:
683 fd = winbind_open_pipe_sock(ctx, recursing, need_priv);
684 if (fd == -1) {
685 errno = ENOENT;
686 return -1;
689 /* Write data to socket */
691 nwritten = 0;
693 while(nwritten < count) {
694 struct pollfd pfd;
695 int ret;
697 /* Catch pipe close on other end by checking if a read()
698 call would not block by calling poll(). */
700 pfd.fd = fd;
701 pfd.events = POLLIN|POLLOUT|POLLHUP;
703 ret = poll(&pfd, 1, -1);
704 if (ret == -1) {
705 winbind_close_sock(ctx);
706 return -1; /* poll error */
709 /* Write should be OK if fd not available for reading */
711 if ((ret == 1) && (pfd.revents & (POLLIN|POLLHUP|POLLERR))) {
713 /* Pipe has closed on remote end */
715 winbind_close_sock(ctx);
716 goto restart;
719 /* Do the write */
721 result = write(fd, (char *)buffer + nwritten,
722 count - nwritten);
724 if ((result == -1) || (result == 0)) {
726 /* Write failed */
728 winbind_close_sock(ctx);
729 return -1;
732 nwritten += result;
735 return nwritten;
738 /* Read data from winbindd socket */
740 static int winbind_read_sock(struct winbindd_context *ctx,
741 void *buffer, int count)
743 int fd;
744 int nread = 0;
745 int total_time = 0;
747 fd = winbind_open_pipe_sock(ctx, false, false);
748 if (fd == -1) {
749 return -1;
752 /* Read data from socket */
753 while(nread < count) {
754 struct pollfd pfd;
755 int ret;
757 /* Catch pipe close on other end by checking if a read()
758 call would not block by calling poll(). */
760 pfd.fd = fd;
761 pfd.events = POLLIN|POLLHUP;
763 /* Wait for 5 seconds for a reply. May need to parameterise this... */
765 ret = poll(&pfd, 1, 5000);
766 if (ret == -1) {
767 winbind_close_sock(ctx);
768 return -1; /* poll error */
771 if (ret == 0) {
772 /* Not ready for read yet... */
773 if (total_time >= 300) {
774 /* Timeout */
775 winbind_close_sock(ctx);
776 return -1;
778 total_time += 5;
779 continue;
782 if ((ret == 1) && (pfd.revents & (POLLIN|POLLHUP|POLLERR))) {
784 /* Do the Read */
786 int result = read(fd, (char *)buffer + nread,
787 count - nread);
789 if ((result == -1) || (result == 0)) {
791 /* Read failed. I think the only useful thing we
792 can do here is just return -1 and fail since the
793 transaction has failed half way through. */
795 winbind_close_sock(ctx);
796 return -1;
799 nread += result;
804 return nread;
807 /* Read reply */
809 static int winbindd_read_reply(struct winbindd_context *ctx,
810 struct winbindd_response *response)
812 int result1, result2 = 0;
814 if (!response) {
815 return -1;
818 /* Read fixed length response */
820 result1 = winbind_read_sock(ctx, response,
821 sizeof(struct winbindd_response));
823 /* We actually send the pointer value of the extra_data field from
824 the server. This has no meaning in the client's address space
825 so we clear it out. */
827 response->extra_data.data = NULL;
829 if (result1 == -1) {
830 return -1;
833 if (response->length < sizeof(struct winbindd_response)) {
834 return -1;
837 /* Read variable length response */
839 if (response->length > sizeof(struct winbindd_response)) {
840 int extra_data_len = response->length -
841 sizeof(struct winbindd_response);
843 /* Mallocate memory for extra data */
845 if (!(response->extra_data.data = malloc(extra_data_len))) {
846 return -1;
849 result2 = winbind_read_sock(ctx, response->extra_data.data,
850 extra_data_len);
851 if (result2 == -1) {
852 winbindd_free_response(response);
853 return -1;
857 /* Return total amount of data read */
859 return result1 + result2;
863 * send simple types of requests
866 static NSS_STATUS winbindd_send_request(
867 struct winbindd_context *ctx,
868 int req_type,
869 int need_priv,
870 struct winbindd_request *request)
872 struct winbindd_request lrequest;
874 /* Check for our tricky environment variable */
876 if (winbind_env_set()) {
877 return NSS_STATUS_NOTFOUND;
880 if (!request) {
881 ZERO_STRUCT(lrequest);
882 request = &lrequest;
885 /* Fill in request and send down pipe */
887 winbindd_init_request(request, req_type);
889 if (winbind_write_sock(ctx, request, sizeof(*request),
890 request->wb_flags & WBFLAG_RECURSE,
891 need_priv) == -1)
893 /* Set ENOENT for consistency. Required by some apps */
894 errno = ENOENT;
896 return NSS_STATUS_UNAVAIL;
899 if ((request->extra_len != 0) &&
900 (winbind_write_sock(ctx, request->extra_data.data,
901 request->extra_len,
902 request->wb_flags & WBFLAG_RECURSE,
903 need_priv) == -1))
905 /* Set ENOENT for consistency. Required by some apps */
906 errno = ENOENT;
908 return NSS_STATUS_UNAVAIL;
911 return NSS_STATUS_SUCCESS;
915 * Get results from winbindd request
918 static NSS_STATUS winbindd_get_response(struct winbindd_context *ctx,
919 struct winbindd_response *response)
921 struct winbindd_response lresponse;
923 if (!response) {
924 ZERO_STRUCT(lresponse);
925 response = &lresponse;
928 init_response(response);
930 /* Wait for reply */
931 if (winbindd_read_reply(ctx, response) == -1) {
932 /* Set ENOENT for consistency. Required by some apps */
933 errno = ENOENT;
935 return NSS_STATUS_UNAVAIL;
938 /* Throw away extra data if client didn't request it */
939 if (response == &lresponse) {
940 winbindd_free_response(response);
943 /* Copy reply data from socket */
944 if (response->result != WINBINDD_OK) {
945 return NSS_STATUS_NOTFOUND;
948 return NSS_STATUS_SUCCESS;
951 /* Handle simple types of requests */
953 NSS_STATUS winbindd_request_response(struct winbindd_context *ctx,
954 int req_type,
955 struct winbindd_request *request,
956 struct winbindd_response *response)
958 NSS_STATUS status = NSS_STATUS_UNAVAIL;
960 if (ctx == NULL) {
961 ctx = get_wb_global_ctx();
964 status = winbindd_send_request(ctx, req_type, 0, request);
965 if (status != NSS_STATUS_SUCCESS) {
966 goto out;
968 status = winbindd_get_response(ctx, response);
970 out:
971 return status;
974 NSS_STATUS winbindd_priv_request_response(struct winbindd_context *ctx,
975 int req_type,
976 struct winbindd_request *request,
977 struct winbindd_response *response)
979 NSS_STATUS status = NSS_STATUS_UNAVAIL;
981 if (ctx == NULL) {
982 ctx = get_wb_global_ctx();
985 status = winbindd_send_request(ctx, req_type, 1, request);
986 if (status != NSS_STATUS_SUCCESS) {
987 goto out;
989 status = winbindd_get_response(ctx, response);
991 out:
992 return status;
995 /* Create and free winbindd context */
997 struct winbindd_context *winbindd_ctx_create(void)
999 struct winbindd_context *ctx;
1001 ctx = calloc(1, sizeof(struct winbindd_context));
1003 if (!ctx) {
1004 return NULL;
1007 ctx->winbindd_fd = -1;
1009 WB_GLOBAL_LIST_LOCK;
1010 DLIST_ADD_END(wb_global_ctx.list, ctx);
1011 WB_GLOBAL_LIST_UNLOCK;
1013 return ctx;
1016 void winbindd_ctx_free(struct winbindd_context *ctx)
1018 WB_GLOBAL_LIST_LOCK;
1019 winbind_ctx_free_locked(ctx);
1020 WB_GLOBAL_LIST_UNLOCK;