tests/krb5: Fix PA-PAC-OPTIONS checking
[Samba.git] / nsswitch / wb_common.c
blob45c1969acc225bd699445caa26befd68b8164fae
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"
30 #ifdef HAVE_PTHREAD_H
31 #include <pthread.h>
32 #endif
34 static char client_name[32];
36 /* Global context */
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 */
44 #ifdef HAVE_PTHREAD
45 static pthread_mutex_t wb_global_ctx_mutex = PTHREAD_MUTEX_INITIALIZER;
46 #endif
48 static struct winbindd_context *get_wb_global_ctx(void)
50 static struct winbindd_context wb_global_ctx = {
51 .winbindd_fd = -1,
52 .is_privileged = false,
53 .our_pid = 0
56 #ifdef HAVE_PTHREAD
57 pthread_mutex_lock(&wb_global_ctx_mutex);
58 #endif
59 return &wb_global_ctx;
62 static void put_wb_global_ctx(void)
64 #ifdef HAVE_PTHREAD
65 pthread_mutex_unlock(&wb_global_ctx_mutex);
66 #endif
67 return;
70 /* Free a response structure */
72 void winbindd_free_response(struct winbindd_response *response)
74 /* Free any allocated extra_data */
76 if (response)
77 SAFE_FREE(response->extra_data.data);
80 void winbind_set_client_name(const char *name)
82 if (name == NULL || strlen(name) == 0) {
83 return;
86 (void)snprintf(client_name, sizeof(client_name), "%s", name);
89 static const char *winbind_get_client_name(void)
91 if (client_name[0] == '\0') {
92 const char *progname = getprogname();
93 int len;
95 if (progname == NULL) {
96 progname = "<unknown>";
99 len = snprintf(client_name,
100 sizeof(client_name),
101 "%s",
102 progname);
103 if (len <= 0) {
104 return progname;
108 return client_name;
111 /* Initialise a request structure */
113 static void winbindd_init_request(struct winbindd_request *request,
114 int request_type)
116 request->length = sizeof(struct winbindd_request);
118 request->cmd = (enum winbindd_cmd)request_type;
119 request->pid = getpid();
121 (void)snprintf(request->client_name,
122 sizeof(request->client_name),
123 "%s",
124 winbind_get_client_name());
127 /* Initialise a response structure */
129 static void init_response(struct winbindd_response *response)
131 /* Initialise return value */
133 response->result = WINBINDD_ERROR;
136 /* Close established socket */
138 static void winbind_close_sock(struct winbindd_context *ctx)
140 if (!ctx) {
141 return;
144 if (ctx->winbindd_fd != -1) {
145 close(ctx->winbindd_fd);
146 ctx->winbindd_fd = -1;
150 /* Destructor for global context to ensure fd is closed */
152 #ifdef HAVE_DESTRUCTOR_ATTRIBUTE
153 __attribute__((destructor))
154 #elif defined (HAVE_PRAGMA_FINI)
155 #pragma fini (winbind_destructor)
156 #endif
157 static void winbind_destructor(void)
159 struct winbindd_context *ctx;
161 ctx = get_wb_global_ctx();
162 winbind_close_sock(ctx);
163 put_wb_global_ctx();
166 #define CONNECT_TIMEOUT 30
168 /* Make sure socket handle isn't stdin, stdout or stderr */
169 #define RECURSION_LIMIT 3
171 static int make_nonstd_fd_internals(int fd, int limit /* Recursion limiter */)
173 int new_fd;
174 if (fd >= 0 && fd <= 2) {
175 #ifdef F_DUPFD
176 if ((new_fd = fcntl(fd, F_DUPFD, 3)) == -1) {
177 return -1;
179 /* Paranoia */
180 if (new_fd < 3) {
181 close(new_fd);
182 return -1;
184 close(fd);
185 return new_fd;
186 #else
187 if (limit <= 0)
188 return -1;
190 new_fd = dup(fd);
191 if (new_fd == -1)
192 return -1;
194 /* use the program stack to hold our list of FDs to close */
195 new_fd = make_nonstd_fd_internals(new_fd, limit - 1);
196 close(fd);
197 return new_fd;
198 #endif
200 return fd;
203 /****************************************************************************
204 Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available,
205 else
206 if SYSV use O_NDELAY
207 if BSD use FNDELAY
208 Set close on exec also.
209 ****************************************************************************/
211 static int make_safe_fd(int fd)
213 int result, flags;
214 int new_fd = make_nonstd_fd_internals(fd, RECURSION_LIMIT);
215 if (new_fd == -1) {
216 close(fd);
217 return -1;
220 /* Socket should be nonblocking. */
221 #ifdef O_NONBLOCK
222 #define FLAG_TO_SET O_NONBLOCK
223 #else
224 #ifdef SYSV
225 #define FLAG_TO_SET O_NDELAY
226 #else /* BSD */
227 #define FLAG_TO_SET FNDELAY
228 #endif
229 #endif
231 if ((flags = fcntl(new_fd, F_GETFL)) == -1) {
232 close(new_fd);
233 return -1;
236 flags |= FLAG_TO_SET;
237 if (fcntl(new_fd, F_SETFL, flags) == -1) {
238 close(new_fd);
239 return -1;
242 #undef FLAG_TO_SET
244 /* Socket should be closed on exec() */
245 #ifdef FD_CLOEXEC
246 result = flags = fcntl(new_fd, F_GETFD, 0);
247 if (flags >= 0) {
248 flags |= FD_CLOEXEC;
249 result = fcntl( new_fd, F_SETFD, flags );
251 if (result < 0) {
252 close(new_fd);
253 return -1;
255 #endif
256 return new_fd;
260 * @internal
262 * @brief Check if we talk to the privileged pipe which should be owned by root.
264 * This checks if we have uid_wrapper running and if this is the case it will
265 * allow one to connect to the winbind privileged pipe even it is not owned by root.
267 * @param[in] uid The uid to check if we can safely talk to the pipe.
269 * @return If we have access it returns true, else false.
271 static bool winbind_privileged_pipe_is_root(uid_t uid)
273 if (uid == 0) {
274 return true;
277 if (uid_wrapper_enabled()) {
278 return true;
281 return false;
284 /* Connect to winbindd socket */
286 static int winbind_named_pipe_sock(const char *dir)
288 struct sockaddr_un sunaddr;
289 struct stat st;
290 int fd;
291 int wait_time;
292 int slept;
293 int ret;
295 /* Check permissions on unix socket directory */
297 if (lstat(dir, &st) == -1) {
298 errno = ENOENT;
299 return -1;
303 * This tells us that the pipe is owned by a privileged
304 * process, as we will be sending passwords to it.
306 if (!S_ISDIR(st.st_mode) ||
307 !winbind_privileged_pipe_is_root(st.st_uid)) {
308 errno = ENOENT;
309 return -1;
312 /* Connect to socket */
314 sunaddr = (struct sockaddr_un) { .sun_family = AF_UNIX };
316 ret = snprintf(sunaddr.sun_path, sizeof(sunaddr.sun_path),
317 "%s/%s", dir, WINBINDD_SOCKET_NAME);
318 if ((ret == -1) || (ret >= sizeof(sunaddr.sun_path))) {
319 errno = ENAMETOOLONG;
320 return -1;
323 /* If socket file doesn't exist, don't bother trying to connect
324 with retry. This is an attempt to make the system usable when
325 the winbindd daemon is not running. */
327 if (lstat(sunaddr.sun_path, &st) == -1) {
328 errno = ENOENT;
329 return -1;
332 /* Check permissions on unix socket file */
335 * This tells us that the pipe is owned by a privileged
336 * process, as we will be sending passwords to it.
338 if (!S_ISSOCK(st.st_mode) ||
339 !winbind_privileged_pipe_is_root(st.st_uid)) {
340 errno = ENOENT;
341 return -1;
344 /* Connect to socket */
346 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
347 return -1;
350 /* Set socket non-blocking and close on exec. */
352 if ((fd = make_safe_fd( fd)) == -1) {
353 return fd;
356 for (wait_time = 0; connect(fd, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) == -1;
357 wait_time += slept) {
358 struct pollfd pfd;
359 int connect_errno = 0;
360 socklen_t errnosize;
362 if (wait_time >= CONNECT_TIMEOUT)
363 goto error_out;
365 switch (errno) {
366 case EINPROGRESS:
367 pfd.fd = fd;
368 pfd.events = POLLOUT;
370 ret = poll(&pfd, 1, (CONNECT_TIMEOUT - wait_time) * 1000);
372 if (ret > 0) {
373 errnosize = sizeof(connect_errno);
375 ret = getsockopt(fd, SOL_SOCKET,
376 SO_ERROR, &connect_errno, &errnosize);
378 if (ret >= 0 && connect_errno == 0) {
379 /* Connect succeed */
380 goto out;
384 slept = CONNECT_TIMEOUT;
385 break;
386 case EAGAIN:
387 slept = rand() % 3 + 1;
388 sleep(slept);
389 break;
390 default:
391 goto error_out;
396 out:
398 return fd;
400 error_out:
402 close(fd);
403 return -1;
406 static const char *winbindd_socket_dir(void)
408 if (nss_wrapper_enabled()) {
409 const char *env_dir;
411 env_dir = getenv("SELFTEST_WINBINDD_SOCKET_DIR");
412 if (env_dir != NULL) {
413 return env_dir;
417 return WINBINDD_SOCKET_DIR;
420 /* Connect to winbindd socket */
422 static int winbind_open_pipe_sock(struct winbindd_context *ctx,
423 int recursing, int need_priv)
425 #ifdef HAVE_UNIXSOCKET
426 struct winbindd_request request;
427 struct winbindd_response response;
429 ZERO_STRUCT(request);
430 ZERO_STRUCT(response);
432 if (!ctx) {
433 return -1;
436 if (ctx->our_pid != getpid()) {
437 winbind_close_sock(ctx);
438 ctx->our_pid = getpid();
441 if ((need_priv != 0) && !ctx->is_privileged) {
442 winbind_close_sock(ctx);
445 if (ctx->winbindd_fd != -1) {
446 return ctx->winbindd_fd;
449 if (recursing) {
450 return -1;
453 ctx->winbindd_fd = winbind_named_pipe_sock(winbindd_socket_dir());
455 if (ctx->winbindd_fd == -1) {
456 return -1;
459 ctx->is_privileged = false;
461 /* version-check the socket */
463 request.wb_flags = WBFLAG_RECURSE;
464 if ((winbindd_request_response(ctx, WINBINDD_INTERFACE_VERSION, &request,
465 &response) != NSS_STATUS_SUCCESS) ||
466 (response.data.interface_version != WINBIND_INTERFACE_VERSION)) {
467 winbind_close_sock(ctx);
468 return -1;
471 if (need_priv == 0) {
472 return ctx->winbindd_fd;
475 /* try and get priv pipe */
477 request.wb_flags = WBFLAG_RECURSE;
479 /* Note that response needs to be initialized to avoid
480 * crashing on clean up after WINBINDD_PRIV_PIPE_DIR call failed
481 * as interface version (from the first request) returned as a fstring,
482 * thus response.extra_data.data will not be NULL even though
483 * winbindd response did not write over it due to a failure */
484 ZERO_STRUCT(response);
485 if (winbindd_request_response(ctx, WINBINDD_PRIV_PIPE_DIR, &request,
486 &response) == NSS_STATUS_SUCCESS) {
487 int fd;
488 fd = winbind_named_pipe_sock((char *)response.extra_data.data);
489 if (fd != -1) {
490 close(ctx->winbindd_fd);
491 ctx->winbindd_fd = fd;
492 ctx->is_privileged = true;
495 SAFE_FREE(response.extra_data.data);
498 if (!ctx->is_privileged) {
499 return -1;
502 return ctx->winbindd_fd;
503 #else
504 return -1;
505 #endif /* HAVE_UNIXSOCKET */
508 /* Write data to winbindd socket */
510 static int winbind_write_sock(struct winbindd_context *ctx, void *buffer,
511 int count, int recursing, int need_priv)
513 int fd, result, nwritten;
515 /* Open connection to winbind daemon */
517 restart:
519 fd = winbind_open_pipe_sock(ctx, recursing, need_priv);
520 if (fd == -1) {
521 errno = ENOENT;
522 return -1;
525 /* Write data to socket */
527 nwritten = 0;
529 while(nwritten < count) {
530 struct pollfd pfd;
531 int ret;
533 /* Catch pipe close on other end by checking if a read()
534 call would not block by calling poll(). */
536 pfd.fd = fd;
537 pfd.events = POLLIN|POLLOUT|POLLHUP;
539 ret = poll(&pfd, 1, -1);
540 if (ret == -1) {
541 winbind_close_sock(ctx);
542 return -1; /* poll error */
545 /* Write should be OK if fd not available for reading */
547 if ((ret == 1) && (pfd.revents & (POLLIN|POLLHUP|POLLERR))) {
549 /* Pipe has closed on remote end */
551 winbind_close_sock(ctx);
552 goto restart;
555 /* Do the write */
557 result = write(fd, (char *)buffer + nwritten,
558 count - nwritten);
560 if ((result == -1) || (result == 0)) {
562 /* Write failed */
564 winbind_close_sock(ctx);
565 return -1;
568 nwritten += result;
571 return nwritten;
574 /* Read data from winbindd socket */
576 static int winbind_read_sock(struct winbindd_context *ctx,
577 void *buffer, int count)
579 int fd;
580 int nread = 0;
581 int total_time = 0;
583 fd = winbind_open_pipe_sock(ctx, false, false);
584 if (fd == -1) {
585 return -1;
588 /* Read data from socket */
589 while(nread < count) {
590 struct pollfd pfd;
591 int ret;
593 /* Catch pipe close on other end by checking if a read()
594 call would not block by calling poll(). */
596 pfd.fd = fd;
597 pfd.events = POLLIN|POLLHUP;
599 /* Wait for 5 seconds for a reply. May need to parameterise this... */
601 ret = poll(&pfd, 1, 5000);
602 if (ret == -1) {
603 winbind_close_sock(ctx);
604 return -1; /* poll error */
607 if (ret == 0) {
608 /* Not ready for read yet... */
609 if (total_time >= 300) {
610 /* Timeout */
611 winbind_close_sock(ctx);
612 return -1;
614 total_time += 5;
615 continue;
618 if ((ret == 1) && (pfd.revents & (POLLIN|POLLHUP|POLLERR))) {
620 /* Do the Read */
622 int result = read(fd, (char *)buffer + nread,
623 count - nread);
625 if ((result == -1) || (result == 0)) {
627 /* Read failed. I think the only useful thing we
628 can do here is just return -1 and fail since the
629 transaction has failed half way through. */
631 winbind_close_sock(ctx);
632 return -1;
635 nread += result;
640 return nread;
643 /* Read reply */
645 static int winbindd_read_reply(struct winbindd_context *ctx,
646 struct winbindd_response *response)
648 int result1, result2 = 0;
650 if (!response) {
651 return -1;
654 /* Read fixed length response */
656 result1 = winbind_read_sock(ctx, response,
657 sizeof(struct winbindd_response));
659 /* We actually send the pointer value of the extra_data field from
660 the server. This has no meaning in the client's address space
661 so we clear it out. */
663 response->extra_data.data = NULL;
665 if (result1 == -1) {
666 return -1;
669 if (response->length < sizeof(struct winbindd_response)) {
670 return -1;
673 /* Read variable length response */
675 if (response->length > sizeof(struct winbindd_response)) {
676 int extra_data_len = response->length -
677 sizeof(struct winbindd_response);
679 /* Mallocate memory for extra data */
681 if (!(response->extra_data.data = malloc(extra_data_len))) {
682 return -1;
685 result2 = winbind_read_sock(ctx, response->extra_data.data,
686 extra_data_len);
687 if (result2 == -1) {
688 winbindd_free_response(response);
689 return -1;
693 /* Return total amount of data read */
695 return result1 + result2;
699 * send simple types of requests
702 static NSS_STATUS winbindd_send_request(
703 struct winbindd_context *ctx,
704 int req_type,
705 int need_priv,
706 struct winbindd_request *request)
708 struct winbindd_request lrequest;
710 /* Check for our tricky environment variable */
712 if (winbind_env_set()) {
713 return NSS_STATUS_NOTFOUND;
716 if (!request) {
717 ZERO_STRUCT(lrequest);
718 request = &lrequest;
721 /* Fill in request and send down pipe */
723 winbindd_init_request(request, req_type);
725 if (winbind_write_sock(ctx, request, sizeof(*request),
726 request->wb_flags & WBFLAG_RECURSE,
727 need_priv) == -1)
729 /* Set ENOENT for consistency. Required by some apps */
730 errno = ENOENT;
732 return NSS_STATUS_UNAVAIL;
735 if ((request->extra_len != 0) &&
736 (winbind_write_sock(ctx, request->extra_data.data,
737 request->extra_len,
738 request->wb_flags & WBFLAG_RECURSE,
739 need_priv) == -1))
741 /* Set ENOENT for consistency. Required by some apps */
742 errno = ENOENT;
744 return NSS_STATUS_UNAVAIL;
747 return NSS_STATUS_SUCCESS;
751 * Get results from winbindd request
754 static NSS_STATUS winbindd_get_response(struct winbindd_context *ctx,
755 struct winbindd_response *response)
757 struct winbindd_response lresponse;
759 if (!response) {
760 ZERO_STRUCT(lresponse);
761 response = &lresponse;
764 init_response(response);
766 /* Wait for reply */
767 if (winbindd_read_reply(ctx, response) == -1) {
768 /* Set ENOENT for consistency. Required by some apps */
769 errno = ENOENT;
771 return NSS_STATUS_UNAVAIL;
774 /* Throw away extra data if client didn't request it */
775 if (response == &lresponse) {
776 winbindd_free_response(response);
779 /* Copy reply data from socket */
780 if (response->result != WINBINDD_OK) {
781 return NSS_STATUS_NOTFOUND;
784 return NSS_STATUS_SUCCESS;
787 /* Handle simple types of requests */
789 NSS_STATUS winbindd_request_response(struct winbindd_context *ctx,
790 int req_type,
791 struct winbindd_request *request,
792 struct winbindd_response *response)
794 NSS_STATUS status = NSS_STATUS_UNAVAIL;
795 bool release_global_ctx = false;
797 if (ctx == NULL) {
798 ctx = get_wb_global_ctx();
799 release_global_ctx = true;
802 status = winbindd_send_request(ctx, req_type, 0, request);
803 if (status != NSS_STATUS_SUCCESS) {
804 goto out;
806 status = winbindd_get_response(ctx, response);
808 out:
809 if (release_global_ctx) {
810 put_wb_global_ctx();
812 return status;
815 NSS_STATUS winbindd_priv_request_response(struct winbindd_context *ctx,
816 int req_type,
817 struct winbindd_request *request,
818 struct winbindd_response *response)
820 NSS_STATUS status = NSS_STATUS_UNAVAIL;
821 bool release_global_ctx = false;
823 if (ctx == NULL) {
824 ctx = get_wb_global_ctx();
825 release_global_ctx = true;
828 status = winbindd_send_request(ctx, req_type, 1, request);
829 if (status != NSS_STATUS_SUCCESS) {
830 goto out;
832 status = winbindd_get_response(ctx, response);
834 out:
835 if (release_global_ctx) {
836 put_wb_global_ctx();
838 return status;
841 /* Create and free winbindd context */
843 struct winbindd_context *winbindd_ctx_create(void)
845 struct winbindd_context *ctx;
847 ctx = calloc(1, sizeof(struct winbindd_context));
849 if (!ctx) {
850 return NULL;
853 ctx->winbindd_fd = -1;
855 return ctx;
858 void winbindd_ctx_free(struct winbindd_context *ctx)
860 winbind_close_sock(ctx);
861 free(ctx);