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"
34 static char client_name[32];
38 struct winbindd_context {
39 int winbindd_fd; /* winbind file descriptor */
40 bool is_privileged; /* using the privileged socket? */
41 pid_t our_pid; /* calling process pid */
45 static pthread_mutex_t wb_global_ctx_mutex = PTHREAD_MUTEX_INITIALIZER;
48 static struct winbindd_context *get_wb_global_ctx(void)
50 static struct winbindd_context wb_global_ctx = {
52 .is_privileged = false,
57 pthread_mutex_lock(&wb_global_ctx_mutex);
59 return &wb_global_ctx;
62 static void put_wb_global_ctx(void)
65 pthread_mutex_unlock(&wb_global_ctx_mutex);
70 /* Free a response structure */
72 void winbindd_free_response(struct winbindd_response *response)
74 /* Free any allocated extra_data */
77 SAFE_FREE(response->extra_data.data);
80 static const char *winbind_get_client_name(void)
82 if (client_name[0] == '\0') {
85 len = snprintf(client_name,
97 /* Initialise a request structure */
99 static void winbindd_init_request(struct winbindd_request *request,
102 request->length = sizeof(struct winbindd_request);
104 request->cmd = (enum winbindd_cmd)request_type;
105 request->pid = getpid();
107 (void)snprintf(request->client_name,
108 sizeof(request->client_name),
110 winbind_get_client_name());
113 /* Initialise a response structure */
115 static void init_response(struct winbindd_response *response)
117 /* Initialise return value */
119 response->result = WINBINDD_ERROR;
122 /* Close established socket */
124 static void winbind_close_sock(struct winbindd_context *ctx)
130 if (ctx->winbindd_fd != -1) {
131 close(ctx->winbindd_fd);
132 ctx->winbindd_fd = -1;
136 /* Destructor for global context to ensure fd is closed */
138 #if HAVE_DESTRUCTOR_ATTRIBUTE
139 __attribute__((destructor))
141 static void winbind_destructor(void)
143 struct winbindd_context *ctx;
145 ctx = get_wb_global_ctx();
146 winbind_close_sock(ctx);
150 #define CONNECT_TIMEOUT 30
152 /* Make sure socket handle isn't stdin, stdout or stderr */
153 #define RECURSION_LIMIT 3
155 static int make_nonstd_fd_internals(int fd, int limit /* Recursion limiter */)
158 if (fd >= 0 && fd <= 2) {
160 if ((new_fd = fcntl(fd, F_DUPFD, 3)) == -1) {
178 /* use the program stack to hold our list of FDs to close */
179 new_fd = make_nonstd_fd_internals(new_fd, limit - 1);
187 /****************************************************************************
188 Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available,
192 Set close on exec also.
193 ****************************************************************************/
195 static int make_safe_fd(int fd)
198 int new_fd = make_nonstd_fd_internals(fd, RECURSION_LIMIT);
204 /* Socket should be nonblocking. */
206 #define FLAG_TO_SET O_NONBLOCK
209 #define FLAG_TO_SET O_NDELAY
211 #define FLAG_TO_SET FNDELAY
215 if ((flags = fcntl(new_fd, F_GETFL)) == -1) {
220 flags |= FLAG_TO_SET;
221 if (fcntl(new_fd, F_SETFL, flags) == -1) {
228 /* Socket should be closed on exec() */
230 result = flags = fcntl(new_fd, F_GETFD, 0);
233 result = fcntl( new_fd, F_SETFD, flags );
246 * @brief Check if we talk to the priviliged pipe which should be owned by root.
248 * This checks if we have uid_wrapper running and if this is the case it will
249 * allow one to connect to the winbind privileged pipe even it is not owned by root.
251 * @param[in] uid The uid to check if we can safely talk to the pipe.
253 * @return If we have access it returns true, else false.
255 static bool winbind_privileged_pipe_is_root(uid_t uid)
261 if (uid_wrapper_enabled()) {
268 /* Connect to winbindd socket */
270 static int winbind_named_pipe_sock(const char *dir)
272 struct sockaddr_un sunaddr;
279 /* Check permissions on unix socket directory */
281 if (lstat(dir, &st) == -1) {
287 * This tells us that the pipe is owned by a privileged
288 * process, as we will be sending passwords to it.
290 if (!S_ISDIR(st.st_mode) ||
291 !winbind_privileged_pipe_is_root(st.st_uid)) {
296 /* Connect to socket */
298 sunaddr = (struct sockaddr_un) { .sun_family = AF_UNIX };
300 ret = snprintf(sunaddr.sun_path, sizeof(sunaddr.sun_path),
301 "%s/%s", dir, WINBINDD_SOCKET_NAME);
302 if ((ret == -1) || (ret >= sizeof(sunaddr.sun_path))) {
303 errno = ENAMETOOLONG;
307 /* If socket file doesn't exist, don't bother trying to connect
308 with retry. This is an attempt to make the system usable when
309 the winbindd daemon is not running. */
311 if (lstat(sunaddr.sun_path, &st) == -1) {
316 /* Check permissions on unix socket file */
319 * This tells us that the pipe is owned by a privileged
320 * process, as we will be sending passwords to it.
322 if (!S_ISSOCK(st.st_mode) ||
323 !winbind_privileged_pipe_is_root(st.st_uid)) {
328 /* Connect to socket */
330 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
334 /* Set socket non-blocking and close on exec. */
336 if ((fd = make_safe_fd( fd)) == -1) {
340 for (wait_time = 0; connect(fd, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) == -1;
341 wait_time += slept) {
343 int connect_errno = 0;
346 if (wait_time >= CONNECT_TIMEOUT)
352 pfd.events = POLLOUT;
354 ret = poll(&pfd, 1, (CONNECT_TIMEOUT - wait_time) * 1000);
357 errnosize = sizeof(connect_errno);
359 ret = getsockopt(fd, SOL_SOCKET,
360 SO_ERROR, &connect_errno, &errnosize);
362 if (ret >= 0 && connect_errno == 0) {
363 /* Connect succeed */
368 slept = CONNECT_TIMEOUT;
371 slept = rand() % 3 + 1;
390 static const char *winbindd_socket_dir(void)
392 if (nss_wrapper_enabled()) {
395 env_dir = getenv("SELFTEST_WINBINDD_SOCKET_DIR");
396 if (env_dir != NULL) {
401 return WINBINDD_SOCKET_DIR;
404 /* Connect to winbindd socket */
406 static int winbind_open_pipe_sock(struct winbindd_context *ctx,
407 int recursing, int need_priv)
409 #ifdef HAVE_UNIXSOCKET
410 struct winbindd_request request;
411 struct winbindd_response response;
413 ZERO_STRUCT(request);
414 ZERO_STRUCT(response);
420 if (ctx->our_pid != getpid()) {
421 winbind_close_sock(ctx);
422 ctx->our_pid = getpid();
425 if ((need_priv != 0) && !ctx->is_privileged) {
426 winbind_close_sock(ctx);
429 if (ctx->winbindd_fd != -1) {
430 return ctx->winbindd_fd;
437 ctx->winbindd_fd = winbind_named_pipe_sock(winbindd_socket_dir());
439 if (ctx->winbindd_fd == -1) {
443 ctx->is_privileged = false;
445 /* version-check the socket */
447 request.wb_flags = WBFLAG_RECURSE;
448 if ((winbindd_request_response(ctx, WINBINDD_INTERFACE_VERSION, &request,
449 &response) != NSS_STATUS_SUCCESS) ||
450 (response.data.interface_version != WINBIND_INTERFACE_VERSION)) {
451 winbind_close_sock(ctx);
455 if (need_priv == 0) {
456 return ctx->winbindd_fd;
459 /* try and get priv pipe */
461 request.wb_flags = WBFLAG_RECURSE;
463 /* Note that response needs to be initialized to avoid
464 * crashing on clean up after WINBINDD_PRIV_PIPE_DIR call failed
465 * as interface version (from the first request) returned as a fstring,
466 * thus response.extra_data.data will not be NULL even though
467 * winbindd response did not write over it due to a failure */
468 ZERO_STRUCT(response);
469 if (winbindd_request_response(ctx, WINBINDD_PRIV_PIPE_DIR, &request,
470 &response) == NSS_STATUS_SUCCESS) {
472 fd = winbind_named_pipe_sock((char *)response.extra_data.data);
474 close(ctx->winbindd_fd);
475 ctx->winbindd_fd = fd;
476 ctx->is_privileged = true;
479 SAFE_FREE(response.extra_data.data);
482 if (!ctx->is_privileged) {
486 return ctx->winbindd_fd;
489 #endif /* HAVE_UNIXSOCKET */
492 /* Write data to winbindd socket */
494 static int winbind_write_sock(struct winbindd_context *ctx, void *buffer,
495 int count, int recursing, int need_priv)
497 int fd, result, nwritten;
499 /* Open connection to winbind daemon */
503 fd = winbind_open_pipe_sock(ctx, recursing, need_priv);
509 /* Write data to socket */
513 while(nwritten < count) {
517 /* Catch pipe close on other end by checking if a read()
518 call would not block by calling poll(). */
521 pfd.events = POLLIN|POLLOUT|POLLHUP;
523 ret = poll(&pfd, 1, -1);
525 winbind_close_sock(ctx);
526 return -1; /* poll error */
529 /* Write should be OK if fd not available for reading */
531 if ((ret == 1) && (pfd.revents & (POLLIN|POLLHUP|POLLERR))) {
533 /* Pipe has closed on remote end */
535 winbind_close_sock(ctx);
541 result = write(fd, (char *)buffer + nwritten,
544 if ((result == -1) || (result == 0)) {
548 winbind_close_sock(ctx);
558 /* Read data from winbindd socket */
560 static int winbind_read_sock(struct winbindd_context *ctx,
561 void *buffer, int count)
567 fd = winbind_open_pipe_sock(ctx, false, false);
572 /* Read data from socket */
573 while(nread < count) {
577 /* Catch pipe close on other end by checking if a read()
578 call would not block by calling poll(). */
581 pfd.events = POLLIN|POLLHUP;
583 /* Wait for 5 seconds for a reply. May need to parameterise this... */
585 ret = poll(&pfd, 1, 5000);
587 winbind_close_sock(ctx);
588 return -1; /* poll error */
592 /* Not ready for read yet... */
593 if (total_time >= 300) {
595 winbind_close_sock(ctx);
602 if ((ret == 1) && (pfd.revents & (POLLIN|POLLHUP|POLLERR))) {
606 int result = read(fd, (char *)buffer + nread,
609 if ((result == -1) || (result == 0)) {
611 /* Read failed. I think the only useful thing we
612 can do here is just return -1 and fail since the
613 transaction has failed half way through. */
615 winbind_close_sock(ctx);
629 static int winbindd_read_reply(struct winbindd_context *ctx,
630 struct winbindd_response *response)
632 int result1, result2 = 0;
638 /* Read fixed length response */
640 result1 = winbind_read_sock(ctx, response,
641 sizeof(struct winbindd_response));
643 /* We actually send the pointer value of the extra_data field from
644 the server. This has no meaning in the client's address space
645 so we clear it out. */
647 response->extra_data.data = NULL;
653 if (response->length < sizeof(struct winbindd_response)) {
657 /* Read variable length response */
659 if (response->length > sizeof(struct winbindd_response)) {
660 int extra_data_len = response->length -
661 sizeof(struct winbindd_response);
663 /* Mallocate memory for extra data */
665 if (!(response->extra_data.data = malloc(extra_data_len))) {
669 result2 = winbind_read_sock(ctx, response->extra_data.data,
672 winbindd_free_response(response);
677 /* Return total amount of data read */
679 return result1 + result2;
683 * send simple types of requests
686 static NSS_STATUS winbindd_send_request(
687 struct winbindd_context *ctx,
690 struct winbindd_request *request)
692 struct winbindd_request lrequest;
694 /* Check for our tricky environment variable */
696 if (winbind_env_set()) {
697 return NSS_STATUS_NOTFOUND;
701 ZERO_STRUCT(lrequest);
705 /* Fill in request and send down pipe */
707 winbindd_init_request(request, req_type);
709 if (winbind_write_sock(ctx, request, sizeof(*request),
710 request->wb_flags & WBFLAG_RECURSE,
713 /* Set ENOENT for consistency. Required by some apps */
716 return NSS_STATUS_UNAVAIL;
719 if ((request->extra_len != 0) &&
720 (winbind_write_sock(ctx, request->extra_data.data,
722 request->wb_flags & WBFLAG_RECURSE,
725 /* Set ENOENT for consistency. Required by some apps */
728 return NSS_STATUS_UNAVAIL;
731 return NSS_STATUS_SUCCESS;
735 * Get results from winbindd request
738 static NSS_STATUS winbindd_get_response(struct winbindd_context *ctx,
739 struct winbindd_response *response)
741 struct winbindd_response lresponse;
744 ZERO_STRUCT(lresponse);
745 response = &lresponse;
748 init_response(response);
751 if (winbindd_read_reply(ctx, response) == -1) {
752 /* Set ENOENT for consistency. Required by some apps */
755 return NSS_STATUS_UNAVAIL;
758 /* Throw away extra data if client didn't request it */
759 if (response == &lresponse) {
760 winbindd_free_response(response);
763 /* Copy reply data from socket */
764 if (response->result != WINBINDD_OK) {
765 return NSS_STATUS_NOTFOUND;
768 return NSS_STATUS_SUCCESS;
771 /* Handle simple types of requests */
773 NSS_STATUS winbindd_request_response(struct winbindd_context *ctx,
775 struct winbindd_request *request,
776 struct winbindd_response *response)
778 NSS_STATUS status = NSS_STATUS_UNAVAIL;
779 bool release_global_ctx = false;
782 ctx = get_wb_global_ctx();
783 release_global_ctx = true;
786 status = winbindd_send_request(ctx, req_type, 0, request);
787 if (status != NSS_STATUS_SUCCESS) {
790 status = winbindd_get_response(ctx, response);
793 if (release_global_ctx) {
799 NSS_STATUS winbindd_priv_request_response(struct winbindd_context *ctx,
801 struct winbindd_request *request,
802 struct winbindd_response *response)
804 NSS_STATUS status = NSS_STATUS_UNAVAIL;
805 bool release_global_ctx = false;
808 ctx = get_wb_global_ctx();
809 release_global_ctx = true;
812 status = winbindd_send_request(ctx, req_type, 1, request);
813 if (status != NSS_STATUS_SUCCESS) {
816 status = winbindd_get_response(ctx, response);
819 if (release_global_ctx) {
825 /* Create and free winbindd context */
827 struct winbindd_context *winbindd_ctx_create(void)
829 struct winbindd_context *ctx;
831 ctx = calloc(1, sizeof(struct winbindd_context));
837 ctx->winbindd_fd = -1;
842 void winbindd_ctx_free(struct winbindd_context *ctx)
844 winbind_close_sock(ctx);