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 void winbind_set_client_name(const char *name
)
82 if (name
== NULL
|| strlen(name
) == 0) {
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();
95 if (progname
== NULL
) {
96 progname
= "<unknown>";
99 len
= snprintf(client_name
,
111 /* Initialise a request structure */
113 static void winbindd_init_request(struct winbindd_request
*request
,
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
),
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
)
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)
157 static void winbind_destructor(void)
159 struct winbindd_context
*ctx
;
161 ctx
= get_wb_global_ctx();
162 winbind_close_sock(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 */)
174 if (fd
>= 0 && fd
<= 2) {
176 if ((new_fd
= fcntl(fd
, F_DUPFD
, 3)) == -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);
203 /****************************************************************************
204 Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available,
208 Set close on exec also.
209 ****************************************************************************/
211 static int make_safe_fd(int fd
)
214 int new_fd
= make_nonstd_fd_internals(fd
, RECURSION_LIMIT
);
220 /* Socket should be nonblocking. */
222 #define FLAG_TO_SET O_NONBLOCK
225 #define FLAG_TO_SET O_NDELAY
227 #define FLAG_TO_SET FNDELAY
231 if ((flags
= fcntl(new_fd
, F_GETFL
)) == -1) {
236 flags
|= FLAG_TO_SET
;
237 if (fcntl(new_fd
, F_SETFL
, flags
) == -1) {
244 /* Socket should be closed on exec() */
246 result
= flags
= fcntl(new_fd
, F_GETFD
, 0);
249 result
= fcntl( new_fd
, F_SETFD
, flags
);
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
)
277 if (uid_wrapper_enabled()) {
284 /* Connect to winbindd socket */
286 static int winbind_named_pipe_sock(const char *dir
)
288 struct sockaddr_un sunaddr
;
295 /* Check permissions on unix socket directory */
297 if (lstat(dir
, &st
) == -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
)) {
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
;
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) {
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
)) {
344 /* Connect to socket */
346 if ((fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
350 /* Set socket non-blocking and close on exec. */
352 if ((fd
= make_safe_fd( fd
)) == -1) {
356 for (wait_time
= 0; connect(fd
, (struct sockaddr
*)&sunaddr
, sizeof(sunaddr
)) == -1;
357 wait_time
+= slept
) {
359 int connect_errno
= 0;
362 if (wait_time
>= CONNECT_TIMEOUT
)
368 pfd
.events
= POLLOUT
;
370 ret
= poll(&pfd
, 1, (CONNECT_TIMEOUT
- wait_time
) * 1000);
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 */
384 slept
= CONNECT_TIMEOUT
;
387 slept
= rand() % 3 + 1;
406 static const char *winbindd_socket_dir(void)
408 if (nss_wrapper_enabled()) {
411 env_dir
= getenv("SELFTEST_WINBINDD_SOCKET_DIR");
412 if (env_dir
!= NULL
) {
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
);
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
;
453 ctx
->winbindd_fd
= winbind_named_pipe_sock(winbindd_socket_dir());
455 if (ctx
->winbindd_fd
== -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
);
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
) {
488 fd
= winbind_named_pipe_sock((char *)response
.extra_data
.data
);
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
) {
502 return ctx
->winbindd_fd
;
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 */
519 fd
= winbind_open_pipe_sock(ctx
, recursing
, need_priv
);
525 /* Write data to socket */
529 while(nwritten
< count
) {
533 /* Catch pipe close on other end by checking if a read()
534 call would not block by calling poll(). */
537 pfd
.events
= POLLIN
|POLLOUT
|POLLHUP
;
539 ret
= poll(&pfd
, 1, -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
);
557 result
= write(fd
, (char *)buffer
+ nwritten
,
560 if ((result
== -1) || (result
== 0)) {
564 winbind_close_sock(ctx
);
574 /* Read data from winbindd socket */
576 static int winbind_read_sock(struct winbindd_context
*ctx
,
577 void *buffer
, int count
)
583 fd
= winbind_open_pipe_sock(ctx
, false, false);
588 /* Read data from socket */
589 while(nread
< count
) {
593 /* Catch pipe close on other end by checking if a read()
594 call would not block by calling poll(). */
597 pfd
.events
= POLLIN
|POLLHUP
;
599 /* Wait for 5 seconds for a reply. May need to parameterise this... */
601 ret
= poll(&pfd
, 1, 5000);
603 winbind_close_sock(ctx
);
604 return -1; /* poll error */
608 /* Not ready for read yet... */
609 if (total_time
>= 300) {
611 winbind_close_sock(ctx
);
618 if ((ret
== 1) && (pfd
.revents
& (POLLIN
|POLLHUP
|POLLERR
))) {
622 int result
= read(fd
, (char *)buffer
+ 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
);
645 static int winbindd_read_reply(struct winbindd_context
*ctx
,
646 struct winbindd_response
*response
)
648 int result1
, result2
= 0;
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
;
669 if (response
->length
< sizeof(struct winbindd_response
)) {
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
))) {
685 result2
= winbind_read_sock(ctx
, response
->extra_data
.data
,
688 winbindd_free_response(response
);
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
,
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
;
717 ZERO_STRUCT(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
,
729 /* Set ENOENT for consistency. Required by some apps */
732 return NSS_STATUS_UNAVAIL
;
735 if ((request
->extra_len
!= 0) &&
736 (winbind_write_sock(ctx
, request
->extra_data
.data
,
738 request
->wb_flags
& WBFLAG_RECURSE
,
741 /* Set ENOENT for consistency. Required by some apps */
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
;
760 ZERO_STRUCT(lresponse
);
761 response
= &lresponse
;
764 init_response(response
);
767 if (winbindd_read_reply(ctx
, response
) == -1) {
768 /* Set ENOENT for consistency. Required by some apps */
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
,
791 struct winbindd_request
*request
,
792 struct winbindd_response
*response
)
794 NSS_STATUS status
= NSS_STATUS_UNAVAIL
;
795 bool release_global_ctx
= false;
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
) {
806 status
= winbindd_get_response(ctx
, response
);
809 if (release_global_ctx
) {
815 NSS_STATUS
winbindd_priv_request_response(struct winbindd_context
*ctx
,
817 struct winbindd_request
*request
,
818 struct winbindd_response
*response
)
820 NSS_STATUS status
= NSS_STATUS_UNAVAIL
;
821 bool release_global_ctx
= false;
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
) {
832 status
= winbindd_get_response(ctx
, response
);
835 if (release_global_ctx
) {
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
));
853 ctx
->winbindd_fd
= -1;
858 void winbindd_ctx_free(struct winbindd_context
*ctx
)
860 winbind_close_sock(ctx
);