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"
32 struct winbindd_context {
33 int winbindd_fd; /* winbind file descriptor */
34 bool is_privileged; /* using the privileged socket? */
35 pid_t our_pid; /* calling process pid */
38 static struct winbindd_context *get_wb_global_ctx(void)
40 static struct winbindd_context wb_global_ctx = {
42 .is_privileged = false,
46 return &wb_global_ctx;
49 static void put_wb_global_ctx(void)
55 /* Free a response structure */
57 void winbindd_free_response(struct winbindd_response *response)
59 /* Free any allocated extra_data */
62 SAFE_FREE(response->extra_data.data);
65 /* Initialise a request structure */
67 static void winbindd_init_request(struct winbindd_request *request,
70 request->length = sizeof(struct winbindd_request);
72 request->cmd = (enum winbindd_cmd)request_type;
73 request->pid = getpid();
77 /* Initialise a response structure */
79 static void init_response(struct winbindd_response *response)
81 /* Initialise return value */
83 response->result = WINBINDD_ERROR;
86 /* Close established socket */
88 static void winbind_close_sock(struct winbindd_context *ctx)
94 if (ctx->winbindd_fd != -1) {
95 close(ctx->winbindd_fd);
96 ctx->winbindd_fd = -1;
100 /* Destructor for global context to ensure fd is closed */
102 #if HAVE_DESTRUCTOR_ATTRIBUTE
103 __attribute__((destructor))
105 static void winbind_destructor(void)
107 struct winbindd_context *ctx;
109 ctx = get_wb_global_ctx();
110 winbind_close_sock(ctx);
114 #define CONNECT_TIMEOUT 30
116 /* Make sure socket handle isn't stdin, stdout or stderr */
117 #define RECURSION_LIMIT 3
119 static int make_nonstd_fd_internals(int fd, int limit /* Recursion limiter */)
122 if (fd >= 0 && fd <= 2) {
124 if ((new_fd = fcntl(fd, F_DUPFD, 3)) == -1) {
142 /* use the program stack to hold our list of FDs to close */
143 new_fd = make_nonstd_fd_internals(new_fd, limit - 1);
151 /****************************************************************************
152 Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available,
156 Set close on exec also.
157 ****************************************************************************/
159 static int make_safe_fd(int fd)
162 int new_fd = make_nonstd_fd_internals(fd, RECURSION_LIMIT);
168 /* Socket should be nonblocking. */
170 #define FLAG_TO_SET O_NONBLOCK
173 #define FLAG_TO_SET O_NDELAY
175 #define FLAG_TO_SET FNDELAY
179 if ((flags = fcntl(new_fd, F_GETFL)) == -1) {
184 flags |= FLAG_TO_SET;
185 if (fcntl(new_fd, F_SETFL, flags) == -1) {
192 /* Socket should be closed on exec() */
194 result = flags = fcntl(new_fd, F_GETFD, 0);
197 result = fcntl( new_fd, F_SETFD, flags );
210 * @brief Check if we talk to the priviliged pipe which should be owned by root.
212 * This checks if we have uid_wrapper running and if this is the case it will
213 * allow one to connect to the winbind privileged pipe even it is not owned by root.
215 * @param[in] uid The uid to check if we can safely talk to the pipe.
217 * @return If we have access it returns true, else false.
219 static bool winbind_privileged_pipe_is_root(uid_t uid)
225 if (uid_wrapper_enabled()) {
232 /* Connect to winbindd socket */
234 static int winbind_named_pipe_sock(const char *dir)
236 struct sockaddr_un sunaddr;
243 /* Check permissions on unix socket directory */
245 if (lstat(dir, &st) == -1) {
251 * This tells us that the pipe is owned by a privileged
252 * process, as we will be sending passwords to it.
254 if (!S_ISDIR(st.st_mode) ||
255 !winbind_privileged_pipe_is_root(st.st_uid)) {
260 /* Connect to socket */
262 sunaddr = (struct sockaddr_un) { .sun_family = AF_UNIX };
264 ret = snprintf(sunaddr.sun_path, sizeof(sunaddr.sun_path),
265 "%s/%s", dir, WINBINDD_SOCKET_NAME);
266 if ((ret == -1) || (ret >= sizeof(sunaddr.sun_path))) {
267 errno = ENAMETOOLONG;
271 /* If socket file doesn't exist, don't bother trying to connect
272 with retry. This is an attempt to make the system usable when
273 the winbindd daemon is not running. */
275 if (lstat(sunaddr.sun_path, &st) == -1) {
280 /* Check permissions on unix socket file */
283 * This tells us that the pipe is owned by a privileged
284 * process, as we will be sending passwords to it.
286 if (!S_ISSOCK(st.st_mode) ||
287 !winbind_privileged_pipe_is_root(st.st_uid)) {
292 /* Connect to socket */
294 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
298 /* Set socket non-blocking and close on exec. */
300 if ((fd = make_safe_fd( fd)) == -1) {
304 for (wait_time = 0; connect(fd, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) == -1;
305 wait_time += slept) {
307 int connect_errno = 0;
310 if (wait_time >= CONNECT_TIMEOUT)
316 pfd.events = POLLOUT;
318 ret = poll(&pfd, 1, (CONNECT_TIMEOUT - wait_time) * 1000);
321 errnosize = sizeof(connect_errno);
323 ret = getsockopt(fd, SOL_SOCKET,
324 SO_ERROR, &connect_errno, &errnosize);
326 if (ret >= 0 && connect_errno == 0) {
327 /* Connect succeed */
332 slept = CONNECT_TIMEOUT;
335 slept = rand() % 3 + 1;
354 static const char *winbindd_socket_dir(void)
356 if (nss_wrapper_enabled()) {
359 env_dir = getenv("SELFTEST_WINBINDD_SOCKET_DIR");
360 if (env_dir != NULL) {
365 return WINBINDD_SOCKET_DIR;
368 /* Connect to winbindd socket */
370 static int winbind_open_pipe_sock(struct winbindd_context *ctx,
371 int recursing, int need_priv)
373 #ifdef HAVE_UNIXSOCKET
374 struct winbindd_request request;
375 struct winbindd_response response;
377 ZERO_STRUCT(request);
378 ZERO_STRUCT(response);
384 if (ctx->our_pid != getpid()) {
385 winbind_close_sock(ctx);
386 ctx->our_pid = getpid();
389 if ((need_priv != 0) && !ctx->is_privileged) {
390 winbind_close_sock(ctx);
393 if (ctx->winbindd_fd != -1) {
394 return ctx->winbindd_fd;
401 ctx->winbindd_fd = winbind_named_pipe_sock(winbindd_socket_dir());
403 if (ctx->winbindd_fd == -1) {
407 ctx->is_privileged = false;
409 /* version-check the socket */
411 request.wb_flags = WBFLAG_RECURSE;
412 if ((winbindd_request_response(ctx, WINBINDD_INTERFACE_VERSION, &request,
413 &response) != NSS_STATUS_SUCCESS) ||
414 (response.data.interface_version != WINBIND_INTERFACE_VERSION)) {
415 winbind_close_sock(ctx);
419 if (need_priv == 0) {
420 return ctx->winbindd_fd;
423 /* try and get priv pipe */
425 request.wb_flags = WBFLAG_RECURSE;
427 /* Note that response needs to be initialized to avoid
428 * crashing on clean up after WINBINDD_PRIV_PIPE_DIR call failed
429 * as interface version (from the first request) returned as a fstring,
430 * thus response.extra_data.data will not be NULL even though
431 * winbindd response did not write over it due to a failure */
432 ZERO_STRUCT(response);
433 if (winbindd_request_response(ctx, WINBINDD_PRIV_PIPE_DIR, &request,
434 &response) == NSS_STATUS_SUCCESS) {
436 fd = winbind_named_pipe_sock((char *)response.extra_data.data);
438 close(ctx->winbindd_fd);
439 ctx->winbindd_fd = fd;
440 ctx->is_privileged = true;
443 SAFE_FREE(response.extra_data.data);
446 if (!ctx->is_privileged) {
450 return ctx->winbindd_fd;
453 #endif /* HAVE_UNIXSOCKET */
456 /* Write data to winbindd socket */
458 static int winbind_write_sock(struct winbindd_context *ctx, void *buffer,
459 int count, int recursing, int need_priv)
461 int fd, result, nwritten;
463 /* Open connection to winbind daemon */
467 fd = winbind_open_pipe_sock(ctx, recursing, need_priv);
473 /* Write data to socket */
477 while(nwritten < count) {
481 /* Catch pipe close on other end by checking if a read()
482 call would not block by calling poll(). */
485 pfd.events = POLLIN|POLLOUT|POLLHUP;
487 ret = poll(&pfd, 1, -1);
489 winbind_close_sock(ctx);
490 return -1; /* poll error */
493 /* Write should be OK if fd not available for reading */
495 if ((ret == 1) && (pfd.revents & (POLLIN|POLLHUP|POLLERR))) {
497 /* Pipe has closed on remote end */
499 winbind_close_sock(ctx);
505 result = write(fd, (char *)buffer + nwritten,
508 if ((result == -1) || (result == 0)) {
512 winbind_close_sock(ctx);
522 /* Read data from winbindd socket */
524 static int winbind_read_sock(struct winbindd_context *ctx,
525 void *buffer, int count)
531 fd = winbind_open_pipe_sock(ctx, false, false);
536 /* Read data from socket */
537 while(nread < count) {
541 /* Catch pipe close on other end by checking if a read()
542 call would not block by calling poll(). */
545 pfd.events = POLLIN|POLLHUP;
547 /* Wait for 5 seconds for a reply. May need to parameterise this... */
549 ret = poll(&pfd, 1, 5000);
551 winbind_close_sock(ctx);
552 return -1; /* poll error */
556 /* Not ready for read yet... */
557 if (total_time >= 300) {
559 winbind_close_sock(ctx);
566 if ((ret == 1) && (pfd.revents & (POLLIN|POLLHUP|POLLERR))) {
570 int result = read(fd, (char *)buffer + nread,
573 if ((result == -1) || (result == 0)) {
575 /* Read failed. I think the only useful thing we
576 can do here is just return -1 and fail since the
577 transaction has failed half way through. */
579 winbind_close_sock(ctx);
593 static int winbindd_read_reply(struct winbindd_context *ctx,
594 struct winbindd_response *response)
596 int result1, result2 = 0;
602 /* Read fixed length response */
604 result1 = winbind_read_sock(ctx, response,
605 sizeof(struct winbindd_response));
607 /* We actually send the pointer value of the extra_data field from
608 the server. This has no meaning in the client's address space
609 so we clear it out. */
611 response->extra_data.data = NULL;
617 if (response->length < sizeof(struct winbindd_response)) {
621 /* Read variable length response */
623 if (response->length > sizeof(struct winbindd_response)) {
624 int extra_data_len = response->length -
625 sizeof(struct winbindd_response);
627 /* Mallocate memory for extra data */
629 if (!(response->extra_data.data = malloc(extra_data_len))) {
633 result2 = winbind_read_sock(ctx, response->extra_data.data,
636 winbindd_free_response(response);
641 /* Return total amount of data read */
643 return result1 + result2;
647 * send simple types of requests
650 static NSS_STATUS winbindd_send_request(
651 struct winbindd_context *ctx,
654 struct winbindd_request *request)
656 struct winbindd_request lrequest;
658 /* Check for our tricky environment variable */
660 if (winbind_env_set()) {
661 return NSS_STATUS_NOTFOUND;
665 ZERO_STRUCT(lrequest);
669 /* Fill in request and send down pipe */
671 winbindd_init_request(request, req_type);
673 if (winbind_write_sock(ctx, request, sizeof(*request),
674 request->wb_flags & WBFLAG_RECURSE,
677 /* Set ENOENT for consistency. Required by some apps */
680 return NSS_STATUS_UNAVAIL;
683 if ((request->extra_len != 0) &&
684 (winbind_write_sock(ctx, request->extra_data.data,
686 request->wb_flags & WBFLAG_RECURSE,
689 /* Set ENOENT for consistency. Required by some apps */
692 return NSS_STATUS_UNAVAIL;
695 return NSS_STATUS_SUCCESS;
699 * Get results from winbindd request
702 static NSS_STATUS winbindd_get_response(struct winbindd_context *ctx,
703 struct winbindd_response *response)
705 struct winbindd_response lresponse;
708 ZERO_STRUCT(lresponse);
709 response = &lresponse;
712 init_response(response);
715 if (winbindd_read_reply(ctx, response) == -1) {
716 /* Set ENOENT for consistency. Required by some apps */
719 return NSS_STATUS_UNAVAIL;
722 /* Throw away extra data if client didn't request it */
723 if (response == &lresponse) {
724 winbindd_free_response(response);
727 /* Copy reply data from socket */
728 if (response->result != WINBINDD_OK) {
729 return NSS_STATUS_NOTFOUND;
732 return NSS_STATUS_SUCCESS;
735 /* Handle simple types of requests */
737 NSS_STATUS winbindd_request_response(struct winbindd_context *ctx,
739 struct winbindd_request *request,
740 struct winbindd_response *response)
742 NSS_STATUS status = NSS_STATUS_UNAVAIL;
743 bool release_global_ctx = false;
746 ctx = get_wb_global_ctx();
747 release_global_ctx = true;
750 status = winbindd_send_request(ctx, req_type, 0, request);
751 if (status != NSS_STATUS_SUCCESS) {
754 status = winbindd_get_response(ctx, response);
757 if (release_global_ctx) {
763 NSS_STATUS winbindd_priv_request_response(struct winbindd_context *ctx,
765 struct winbindd_request *request,
766 struct winbindd_response *response)
768 NSS_STATUS status = NSS_STATUS_UNAVAIL;
769 bool release_global_ctx = false;
772 ctx = get_wb_global_ctx();
773 release_global_ctx = true;
776 status = winbindd_send_request(ctx, req_type, 1, request);
777 if (status != NSS_STATUS_SUCCESS) {
780 status = winbindd_get_response(ctx, response);
783 if (release_global_ctx) {
789 /* Create and free winbindd context */
791 struct winbindd_context *winbindd_ctx_create(void)
793 struct winbindd_context *ctx;
795 ctx = calloc(1, sizeof(struct winbindd_context));
801 ctx->winbindd_fd = -1;
806 void winbindd_ctx_free(struct winbindd_context *ctx)
808 winbind_close_sock(ctx);