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/>.
27 #include "system/select.h"
28 #include "winbind_client.h"
36 struct winbindd_context {
37 int winbindd_fd; /* winbind file descriptor */
38 bool is_privileged; /* using the privileged socket? */
39 pid_t our_pid; /* calling process pid */
43 static pthread_mutex_t wb_global_ctx_mutex = PTHREAD_MUTEX_INITIALIZER;
46 static struct winbindd_context *get_wb_global_ctx(void)
48 static struct winbindd_context wb_global_ctx = {
50 .is_privileged = false,
55 pthread_mutex_lock(&wb_global_ctx_mutex);
57 return &wb_global_ctx;
60 static void put_wb_global_ctx(void)
63 pthread_mutex_unlock(&wb_global_ctx_mutex);
68 /* Free a response structure */
70 void winbindd_free_response(struct winbindd_response *response)
72 /* Free any allocated extra_data */
75 SAFE_FREE(response->extra_data.data);
78 /* Initialise a request structure */
80 static void winbindd_init_request(struct winbindd_request *request,
83 request->length = sizeof(struct winbindd_request);
85 request->cmd = (enum winbindd_cmd)request_type;
86 request->pid = getpid();
90 /* Initialise a response structure */
92 static void init_response(struct winbindd_response *response)
94 /* Initialise return value */
96 response->result = WINBINDD_ERROR;
99 /* Close established socket */
101 static void winbind_close_sock(struct winbindd_context *ctx)
107 if (ctx->winbindd_fd != -1) {
108 close(ctx->winbindd_fd);
109 ctx->winbindd_fd = -1;
113 /* Destructor for global context to ensure fd is closed */
115 #if HAVE_DESTRUCTOR_ATTRIBUTE
116 __attribute__((destructor))
118 static void winbind_destructor(void)
120 struct winbindd_context *ctx;
122 ctx = get_wb_global_ctx();
123 winbind_close_sock(ctx);
127 #define CONNECT_TIMEOUT 30
129 /* Make sure socket handle isn't stdin, stdout or stderr */
130 #define RECURSION_LIMIT 3
132 static int make_nonstd_fd_internals(int fd, int limit /* Recursion limiter */)
135 if (fd >= 0 && fd <= 2) {
137 if ((new_fd = fcntl(fd, F_DUPFD, 3)) == -1) {
155 /* use the program stack to hold our list of FDs to close */
156 new_fd = make_nonstd_fd_internals(new_fd, limit - 1);
164 /****************************************************************************
165 Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available,
169 Set close on exec also.
170 ****************************************************************************/
172 static int make_safe_fd(int fd)
175 int new_fd = make_nonstd_fd_internals(fd, RECURSION_LIMIT);
181 /* Socket should be nonblocking. */
183 #define FLAG_TO_SET O_NONBLOCK
186 #define FLAG_TO_SET O_NDELAY
188 #define FLAG_TO_SET FNDELAY
192 if ((flags = fcntl(new_fd, F_GETFL)) == -1) {
197 flags |= FLAG_TO_SET;
198 if (fcntl(new_fd, F_SETFL, flags) == -1) {
205 /* Socket should be closed on exec() */
207 result = flags = fcntl(new_fd, F_GETFD, 0);
210 result = fcntl( new_fd, F_SETFD, flags );
223 * @brief Check if we talk to the priviliged pipe which should be owned by root.
225 * This checks if we have uid_wrapper running and if this is the case it will
226 * allow one to connect to the winbind privileged pipe even it is not owned by root.
228 * @param[in] uid The uid to check if we can safely talk to the pipe.
230 * @return If we have access it returns true, else false.
232 static bool winbind_privileged_pipe_is_root(uid_t uid)
238 if (uid_wrapper_enabled()) {
245 /* Connect to winbindd socket */
247 static int winbind_named_pipe_sock(const char *dir)
249 struct sockaddr_un sunaddr;
256 /* Check permissions on unix socket directory */
258 if (lstat(dir, &st) == -1) {
264 * This tells us that the pipe is owned by a privileged
265 * process, as we will be sending passwords to it.
267 if (!S_ISDIR(st.st_mode) ||
268 !winbind_privileged_pipe_is_root(st.st_uid)) {
273 /* Connect to socket */
275 sunaddr = (struct sockaddr_un) { .sun_family = AF_UNIX };
277 ret = snprintf(sunaddr.sun_path, sizeof(sunaddr.sun_path),
278 "%s/%s", dir, WINBINDD_SOCKET_NAME);
279 if ((ret == -1) || (ret >= sizeof(sunaddr.sun_path))) {
280 errno = ENAMETOOLONG;
284 /* If socket file doesn't exist, don't bother trying to connect
285 with retry. This is an attempt to make the system usable when
286 the winbindd daemon is not running. */
288 if (lstat(sunaddr.sun_path, &st) == -1) {
293 /* Check permissions on unix socket file */
296 * This tells us that the pipe is owned by a privileged
297 * process, as we will be sending passwords to it.
299 if (!S_ISSOCK(st.st_mode) ||
300 !winbind_privileged_pipe_is_root(st.st_uid)) {
305 /* Connect to socket */
307 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
311 /* Set socket non-blocking and close on exec. */
313 if ((fd = make_safe_fd( fd)) == -1) {
317 for (wait_time = 0; connect(fd, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) == -1;
318 wait_time += slept) {
320 int connect_errno = 0;
323 if (wait_time >= CONNECT_TIMEOUT)
329 pfd.events = POLLOUT;
331 ret = poll(&pfd, 1, (CONNECT_TIMEOUT - wait_time) * 1000);
334 errnosize = sizeof(connect_errno);
336 ret = getsockopt(fd, SOL_SOCKET,
337 SO_ERROR, &connect_errno, &errnosize);
339 if (ret >= 0 && connect_errno == 0) {
340 /* Connect succeed */
345 slept = CONNECT_TIMEOUT;
348 slept = rand() % 3 + 1;
367 static const char *winbindd_socket_dir(void)
369 if (nss_wrapper_enabled()) {
372 env_dir = getenv("SELFTEST_WINBINDD_SOCKET_DIR");
373 if (env_dir != NULL) {
378 return WINBINDD_SOCKET_DIR;
381 /* Connect to winbindd socket */
383 static int winbind_open_pipe_sock(struct winbindd_context *ctx,
384 int recursing, int need_priv)
386 #ifdef HAVE_UNIXSOCKET
387 struct winbindd_request request;
388 struct winbindd_response response;
390 ZERO_STRUCT(request);
391 ZERO_STRUCT(response);
397 if (ctx->our_pid != getpid()) {
398 winbind_close_sock(ctx);
399 ctx->our_pid = getpid();
402 if ((need_priv != 0) && !ctx->is_privileged) {
403 winbind_close_sock(ctx);
406 if (ctx->winbindd_fd != -1) {
407 return ctx->winbindd_fd;
414 ctx->winbindd_fd = winbind_named_pipe_sock(winbindd_socket_dir());
416 if (ctx->winbindd_fd == -1) {
420 ctx->is_privileged = false;
422 /* version-check the socket */
424 request.wb_flags = WBFLAG_RECURSE;
425 if ((winbindd_request_response(ctx, WINBINDD_INTERFACE_VERSION, &request,
426 &response) != NSS_STATUS_SUCCESS) ||
427 (response.data.interface_version != WINBIND_INTERFACE_VERSION)) {
428 winbind_close_sock(ctx);
432 if (need_priv == 0) {
433 return ctx->winbindd_fd;
436 /* try and get priv pipe */
438 request.wb_flags = WBFLAG_RECURSE;
440 /* Note that response needs to be initialized to avoid
441 * crashing on clean up after WINBINDD_PRIV_PIPE_DIR call failed
442 * as interface version (from the first request) returned as a fstring,
443 * thus response.extra_data.data will not be NULL even though
444 * winbindd response did not write over it due to a failure */
445 ZERO_STRUCT(response);
446 if (winbindd_request_response(ctx, WINBINDD_PRIV_PIPE_DIR, &request,
447 &response) == NSS_STATUS_SUCCESS) {
449 fd = winbind_named_pipe_sock((char *)response.extra_data.data);
451 close(ctx->winbindd_fd);
452 ctx->winbindd_fd = fd;
453 ctx->is_privileged = true;
456 SAFE_FREE(response.extra_data.data);
459 if (!ctx->is_privileged) {
463 return ctx->winbindd_fd;
466 #endif /* HAVE_UNIXSOCKET */
469 /* Write data to winbindd socket */
471 static int winbind_write_sock(struct winbindd_context *ctx, void *buffer,
472 int count, int recursing, int need_priv)
474 int fd, result, nwritten;
476 /* Open connection to winbind daemon */
480 fd = winbind_open_pipe_sock(ctx, recursing, need_priv);
486 /* Write data to socket */
490 while(nwritten < count) {
494 /* Catch pipe close on other end by checking if a read()
495 call would not block by calling poll(). */
498 pfd.events = POLLIN|POLLOUT|POLLHUP;
500 ret = poll(&pfd, 1, -1);
502 winbind_close_sock(ctx);
503 return -1; /* poll error */
506 /* Write should be OK if fd not available for reading */
508 if ((ret == 1) && (pfd.revents & (POLLIN|POLLHUP|POLLERR))) {
510 /* Pipe has closed on remote end */
512 winbind_close_sock(ctx);
518 result = write(fd, (char *)buffer + nwritten,
521 if ((result == -1) || (result == 0)) {
525 winbind_close_sock(ctx);
535 /* Read data from winbindd socket */
537 static int winbind_read_sock(struct winbindd_context *ctx,
538 void *buffer, int count)
544 fd = winbind_open_pipe_sock(ctx, false, false);
549 /* Read data from socket */
550 while(nread < count) {
554 /* Catch pipe close on other end by checking if a read()
555 call would not block by calling poll(). */
558 pfd.events = POLLIN|POLLHUP;
560 /* Wait for 5 seconds for a reply. May need to parameterise this... */
562 ret = poll(&pfd, 1, 5000);
564 winbind_close_sock(ctx);
565 return -1; /* poll error */
569 /* Not ready for read yet... */
570 if (total_time >= 300) {
572 winbind_close_sock(ctx);
579 if ((ret == 1) && (pfd.revents & (POLLIN|POLLHUP|POLLERR))) {
583 int result = read(fd, (char *)buffer + nread,
586 if ((result == -1) || (result == 0)) {
588 /* Read failed. I think the only useful thing we
589 can do here is just return -1 and fail since the
590 transaction has failed half way through. */
592 winbind_close_sock(ctx);
606 static int winbindd_read_reply(struct winbindd_context *ctx,
607 struct winbindd_response *response)
609 int result1, result2 = 0;
615 /* Read fixed length response */
617 result1 = winbind_read_sock(ctx, response,
618 sizeof(struct winbindd_response));
620 /* We actually send the pointer value of the extra_data field from
621 the server. This has no meaning in the client's address space
622 so we clear it out. */
624 response->extra_data.data = NULL;
630 if (response->length < sizeof(struct winbindd_response)) {
634 /* Read variable length response */
636 if (response->length > sizeof(struct winbindd_response)) {
637 int extra_data_len = response->length -
638 sizeof(struct winbindd_response);
640 /* Mallocate memory for extra data */
642 if (!(response->extra_data.data = malloc(extra_data_len))) {
646 result2 = winbind_read_sock(ctx, response->extra_data.data,
649 winbindd_free_response(response);
654 /* Return total amount of data read */
656 return result1 + result2;
660 * send simple types of requests
663 static NSS_STATUS winbindd_send_request(
664 struct winbindd_context *ctx,
667 struct winbindd_request *request)
669 struct winbindd_request lrequest;
671 /* Check for our tricky environment variable */
673 if (winbind_env_set()) {
674 return NSS_STATUS_NOTFOUND;
678 ZERO_STRUCT(lrequest);
682 /* Fill in request and send down pipe */
684 winbindd_init_request(request, req_type);
686 if (winbind_write_sock(ctx, request, sizeof(*request),
687 request->wb_flags & WBFLAG_RECURSE,
690 /* Set ENOENT for consistency. Required by some apps */
693 return NSS_STATUS_UNAVAIL;
696 if ((request->extra_len != 0) &&
697 (winbind_write_sock(ctx, request->extra_data.data,
699 request->wb_flags & WBFLAG_RECURSE,
702 /* Set ENOENT for consistency. Required by some apps */
705 return NSS_STATUS_UNAVAIL;
708 return NSS_STATUS_SUCCESS;
712 * Get results from winbindd request
715 static NSS_STATUS winbindd_get_response(struct winbindd_context *ctx,
716 struct winbindd_response *response)
718 struct winbindd_response lresponse;
721 ZERO_STRUCT(lresponse);
722 response = &lresponse;
725 init_response(response);
728 if (winbindd_read_reply(ctx, response) == -1) {
729 /* Set ENOENT for consistency. Required by some apps */
732 return NSS_STATUS_UNAVAIL;
735 /* Throw away extra data if client didn't request it */
736 if (response == &lresponse) {
737 winbindd_free_response(response);
740 /* Copy reply data from socket */
741 if (response->result != WINBINDD_OK) {
742 return NSS_STATUS_NOTFOUND;
745 return NSS_STATUS_SUCCESS;
748 /* Handle simple types of requests */
750 NSS_STATUS winbindd_request_response(struct winbindd_context *ctx,
752 struct winbindd_request *request,
753 struct winbindd_response *response)
755 NSS_STATUS status = NSS_STATUS_UNAVAIL;
756 bool release_global_ctx = false;
759 ctx = get_wb_global_ctx();
760 release_global_ctx = true;
763 status = winbindd_send_request(ctx, req_type, 0, request);
764 if (status != NSS_STATUS_SUCCESS) {
767 status = winbindd_get_response(ctx, response);
770 if (release_global_ctx) {
776 NSS_STATUS winbindd_priv_request_response(struct winbindd_context *ctx,
778 struct winbindd_request *request,
779 struct winbindd_response *response)
781 NSS_STATUS status = NSS_STATUS_UNAVAIL;
782 bool release_global_ctx = false;
785 ctx = get_wb_global_ctx();
786 release_global_ctx = true;
789 status = winbindd_send_request(ctx, req_type, 1, request);
790 if (status != NSS_STATUS_SUCCESS) {
793 status = winbindd_get_response(ctx, response);
796 if (release_global_ctx) {
802 /* Create and free winbindd context */
804 struct winbindd_context *winbindd_ctx_create(void)
806 struct winbindd_context *ctx;
808 ctx = calloc(1, sizeof(struct winbindd_context));
814 ctx->winbindd_fd = -1;
819 void winbindd_ctx_free(struct winbindd_context *ctx)
821 winbind_close_sock(ctx);