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"
29 #include "lib/util/dlinklist.h"
36 static __thread
char client_name
[32];
40 struct winbindd_context
{
41 struct winbindd_context
*prev
, *next
;
42 int winbindd_fd
; /* winbind file descriptor */
43 bool is_privileged
; /* using the privileged socket? */
44 pid_t our_pid
; /* calling process pid */
45 bool autofree
; /* this is a thread global context */
48 static struct wb_global_ctx
{
50 pthread_once_t control
;
53 #ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
54 #define WB_GLOBAL_MUTEX_INITIALIZER PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
56 #define WB_GLOBAL_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
58 #define WB_GLOBAL_LIST_LOCK do { \
59 int __pret = pthread_mutex_lock(&wb_global_ctx.list_mutex); \
60 assert(__pret == 0); \
62 #define WB_GLOBAL_LIST_UNLOCK do { \
63 int __pret = pthread_mutex_unlock(&wb_global_ctx.list_mutex); \
64 assert(__pret == 0); \
66 pthread_mutex_t list_mutex
;
67 #else /* => not HAVE_PTHREAD */
68 #define WB_GLOBAL_LIST_LOCK do { } while(0)
69 #define WB_GLOBAL_LIST_UNLOCK do { } while(0)
70 #endif /* not HAVE_PTHREAD */
71 struct winbindd_context
*list
;
74 .control
= PTHREAD_ONCE_INIT
,
75 .list_mutex
= WB_GLOBAL_MUTEX_INITIALIZER
,
80 static void winbind_close_sock(struct winbindd_context
*ctx
);
81 static void winbind_ctx_free_locked(struct winbindd_context
*ctx
);
82 static void winbind_cleanup_list(void);
85 static void wb_thread_ctx_initialize(void);
87 static void wb_atfork_prepare(void)
92 static void wb_atfork_parent(void)
94 WB_GLOBAL_LIST_UNLOCK
;
97 static void wb_atfork_child(void)
99 wb_global_ctx
.list_mutex
= (pthread_mutex_t
)WB_GLOBAL_MUTEX_INITIALIZER
;
101 if (wb_global_ctx
.key_initialized
) {
105 * After a fork the child still believes
106 * it is the same thread as in the parent.
107 * So pthread_getspecific() would return the
108 * value of the thread that called fork().
110 * But we don't want that behavior, so
111 * we just clear the reference and let
112 * winbind_cleanup_list() below 'autofree'
113 * the parent threads global context.
115 ret
= pthread_setspecific(wb_global_ctx
.key
, NULL
);
120 * But we need to close/cleanup the global state
121 * of the parents threads.
123 winbind_cleanup_list();
126 static void wb_thread_ctx_destructor(void *p
)
128 struct winbindd_context
*ctx
= (struct winbindd_context
*)p
;
130 winbindd_ctx_free(ctx
);
133 static void wb_thread_ctx_initialize(void)
137 ret
= pthread_atfork(wb_atfork_prepare
,
142 ret
= pthread_key_create(&wb_global_ctx
.key
,
143 wb_thread_ctx_destructor
);
146 wb_global_ctx
.key_initialized
= true;
149 static struct winbindd_context
*get_wb_thread_ctx(void)
151 struct winbindd_context
*ctx
= NULL
;
154 ret
= pthread_once(&wb_global_ctx
.control
,
155 wb_thread_ctx_initialize
);
158 ctx
= (struct winbindd_context
*)pthread_getspecific(
164 ctx
= malloc(sizeof(struct winbindd_context
));
169 *ctx
= (struct winbindd_context
) {
171 .is_privileged
= false,
177 DLIST_ADD_END(wb_global_ctx
.list
, ctx
);
178 WB_GLOBAL_LIST_UNLOCK
;
180 ret
= pthread_setspecific(wb_global_ctx
.key
, ctx
);
187 #endif /* HAVE_PTHREAD */
189 static struct winbindd_context
*get_wb_global_ctx(void)
191 struct winbindd_context
*ctx
= NULL
;
193 static struct winbindd_context _ctx
= {
195 .is_privileged
= false,
202 ctx
= get_wb_thread_ctx();
205 if (ctx
->prev
== NULL
&& ctx
->next
== NULL
) {
206 DLIST_ADD_END(wb_global_ctx
.list
, ctx
);
213 void winbind_set_client_name(const char *name
)
215 if (name
== NULL
|| strlen(name
) == 0) {
219 (void)snprintf(client_name
, sizeof(client_name
), "%s", name
);
222 static const char *winbind_get_client_name(void)
224 if (client_name
[0] == '\0') {
225 const char *progname
= getprogname();
228 if (progname
== NULL
) {
229 progname
= "<unknown>";
232 len
= snprintf(client_name
,
244 /* Initialise a request structure */
246 static void winbindd_init_request(struct winbindd_request
*request
,
249 request
->length
= sizeof(struct winbindd_request
);
251 request
->cmd
= (enum winbindd_cmd
)request_type
;
252 request
->pid
= getpid();
254 (void)snprintf(request
->client_name
,
255 sizeof(request
->client_name
),
257 winbind_get_client_name());
260 /* Initialise a response structure */
262 static void init_response(struct winbindd_response
*response
)
264 /* Initialise return value */
266 response
->result
= WINBINDD_ERROR
;
269 /* Close established socket */
271 static void winbind_close_sock(struct winbindd_context
*ctx
)
277 if (ctx
->winbindd_fd
!= -1) {
278 close(ctx
->winbindd_fd
);
279 ctx
->winbindd_fd
= -1;
283 static void winbind_ctx_free_locked(struct winbindd_context
*ctx
)
285 winbind_close_sock(ctx
);
286 DLIST_REMOVE(wb_global_ctx
.list
, ctx
);
290 static void winbind_cleanup_list(void)
292 struct winbindd_context
*ctx
= NULL
, *next
= NULL
;
295 for (ctx
= wb_global_ctx
.list
; ctx
!= NULL
; ctx
= next
) {
299 winbind_ctx_free_locked(ctx
);
301 winbind_close_sock(ctx
);
304 WB_GLOBAL_LIST_UNLOCK
;
307 /* Destructor for global context to ensure fd is closed */
309 #ifdef HAVE_DESTRUCTOR_ATTRIBUTE
310 __attribute__((destructor
))
311 #elif defined (HAVE_PRAGMA_FINI)
312 #pragma fini (winbind_destructor)
314 static void winbind_destructor(void)
317 if (wb_global_ctx
.key_initialized
) {
319 ret
= pthread_key_delete(wb_global_ctx
.key
);
321 wb_global_ctx
.key_initialized
= false;
324 wb_global_ctx
.control
= (pthread_once_t
)PTHREAD_ONCE_INIT
;
325 #endif /* HAVE_PTHREAD */
327 winbind_cleanup_list();
330 #define CONNECT_TIMEOUT 30
332 /* Make sure socket handle isn't stdin, stdout or stderr */
333 #define RECURSION_LIMIT 3
335 static int make_nonstd_fd_internals(int fd
, int limit
/* Recursion limiter */)
338 if (fd
>= 0 && fd
<= 2) {
340 if ((new_fd
= fcntl(fd
, F_DUPFD
, 3)) == -1) {
358 /* use the program stack to hold our list of FDs to close */
359 new_fd
= make_nonstd_fd_internals(new_fd
, limit
- 1);
367 /****************************************************************************
368 Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available,
372 Set close on exec also.
373 ****************************************************************************/
375 static int make_safe_fd(int fd
)
378 int new_fd
= make_nonstd_fd_internals(fd
, RECURSION_LIMIT
);
384 /* Socket should be nonblocking. */
386 #define FLAG_TO_SET O_NONBLOCK
389 #define FLAG_TO_SET O_NDELAY
391 #define FLAG_TO_SET FNDELAY
395 if ((flags
= fcntl(new_fd
, F_GETFL
)) == -1) {
400 flags
|= FLAG_TO_SET
;
401 if (fcntl(new_fd
, F_SETFL
, flags
) == -1) {
408 /* Socket should be closed on exec() */
410 result
= flags
= fcntl(new_fd
, F_GETFD
, 0);
413 result
= fcntl( new_fd
, F_SETFD
, flags
);
426 * @brief Check if we talk to the privileged pipe which should be owned by root.
428 * This checks if we have uid_wrapper running and if this is the case it will
429 * allow one to connect to the winbind privileged pipe even it is not owned by root.
431 * @param[in] uid The uid to check if we can safely talk to the pipe.
433 * @return If we have access it returns true, else false.
435 static bool winbind_privileged_pipe_is_root(uid_t uid
)
441 if (uid_wrapper_enabled()) {
448 /* Connect to winbindd socket */
450 static int winbind_named_pipe_sock(const char *dir
)
452 struct sockaddr_un sunaddr
;
459 /* Check permissions on unix socket directory */
461 if (lstat(dir
, &st
) == -1) {
467 * This tells us that the pipe is owned by a privileged
468 * process, as we will be sending passwords to it.
470 if (!S_ISDIR(st
.st_mode
) ||
471 !winbind_privileged_pipe_is_root(st
.st_uid
)) {
476 /* Connect to socket */
478 sunaddr
= (struct sockaddr_un
) { .sun_family
= AF_UNIX
};
480 ret
= snprintf(sunaddr
.sun_path
, sizeof(sunaddr
.sun_path
),
481 "%s/%s", dir
, WINBINDD_SOCKET_NAME
);
482 if ((ret
== -1) || (ret
>= sizeof(sunaddr
.sun_path
))) {
483 errno
= ENAMETOOLONG
;
487 /* If socket file doesn't exist, don't bother trying to connect
488 with retry. This is an attempt to make the system usable when
489 the winbindd daemon is not running. */
491 if (lstat(sunaddr
.sun_path
, &st
) == -1) {
496 /* Check permissions on unix socket file */
499 * This tells us that the pipe is owned by a privileged
500 * process, as we will be sending passwords to it.
502 if (!S_ISSOCK(st
.st_mode
) ||
503 !winbind_privileged_pipe_is_root(st
.st_uid
)) {
508 /* Connect to socket */
510 if ((fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
514 /* Set socket non-blocking and close on exec. */
516 if ((fd
= make_safe_fd( fd
)) == -1) {
520 for (wait_time
= 0; connect(fd
, (struct sockaddr
*)&sunaddr
, sizeof(sunaddr
)) == -1;
521 wait_time
+= slept
) {
523 int connect_errno
= 0;
526 if (wait_time
>= CONNECT_TIMEOUT
)
532 pfd
.events
= POLLOUT
;
534 ret
= poll(&pfd
, 1, (CONNECT_TIMEOUT
- wait_time
) * 1000);
537 errnosize
= sizeof(connect_errno
);
539 ret
= getsockopt(fd
, SOL_SOCKET
,
540 SO_ERROR
, &connect_errno
, &errnosize
);
542 if (ret
>= 0 && connect_errno
== 0) {
543 /* Connect succeed */
548 slept
= CONNECT_TIMEOUT
;
551 slept
= rand() % 3 + 1;
570 static const char *winbindd_socket_dir(void)
572 if (nss_wrapper_enabled()) {
575 env_dir
= getenv("SELFTEST_WINBINDD_SOCKET_DIR");
576 if (env_dir
!= NULL
) {
581 return WINBINDD_SOCKET_DIR
;
584 /* Connect to winbindd socket */
586 static int winbind_open_pipe_sock(struct winbindd_context
*ctx
,
587 int recursing
, int need_priv
)
589 #ifdef HAVE_UNIXSOCKET
590 struct winbindd_request request
;
591 struct winbindd_response response
;
593 ZERO_STRUCT(request
);
594 ZERO_STRUCT(response
);
600 if (ctx
->our_pid
!= getpid()) {
601 winbind_close_sock(ctx
);
602 ctx
->our_pid
= getpid();
605 if ((need_priv
!= 0) && !ctx
->is_privileged
) {
606 winbind_close_sock(ctx
);
609 if (ctx
->winbindd_fd
!= -1) {
610 return ctx
->winbindd_fd
;
617 ctx
->winbindd_fd
= winbind_named_pipe_sock(winbindd_socket_dir());
619 if (ctx
->winbindd_fd
== -1) {
623 ctx
->is_privileged
= false;
625 /* version-check the socket */
627 request
.wb_flags
= WBFLAG_RECURSE
;
628 if ((winbindd_request_response(ctx
, WINBINDD_INTERFACE_VERSION
, &request
,
629 &response
) != NSS_STATUS_SUCCESS
) ||
630 (response
.data
.interface_version
!= WINBIND_INTERFACE_VERSION
)) {
631 winbind_close_sock(ctx
);
635 if (need_priv
== 0) {
636 return ctx
->winbindd_fd
;
639 /* try and get priv pipe */
641 request
.wb_flags
= WBFLAG_RECURSE
;
643 /* Note that response needs to be initialized to avoid
644 * crashing on clean up after WINBINDD_PRIV_PIPE_DIR call failed
645 * as interface version (from the first request) returned as a fstring,
646 * thus response.extra_data.data will not be NULL even though
647 * winbindd response did not write over it due to a failure */
648 ZERO_STRUCT(response
);
649 if (winbindd_request_response(ctx
, WINBINDD_PRIV_PIPE_DIR
, &request
,
650 &response
) == NSS_STATUS_SUCCESS
) {
652 fd
= winbind_named_pipe_sock((char *)response
.extra_data
.data
);
654 close(ctx
->winbindd_fd
);
655 ctx
->winbindd_fd
= fd
;
656 ctx
->is_privileged
= true;
659 SAFE_FREE(response
.extra_data
.data
);
662 if (!ctx
->is_privileged
) {
666 return ctx
->winbindd_fd
;
669 #endif /* HAVE_UNIXSOCKET */
672 /* Write data to winbindd socket */
674 static ssize_t
winbind_write_sock(struct winbindd_context
*ctx
,
683 /* Open connection to winbind daemon */
687 fd
= winbind_open_pipe_sock(ctx
, recursing
, need_priv
);
693 /* Write data to socket */
697 while(nwritten
< count
) {
702 /* Catch pipe close on other end by checking if a read()
703 call would not block by calling poll(). */
706 pfd
.events
= POLLIN
|POLLOUT
|POLLHUP
;
708 ret
= poll(&pfd
, 1, -1);
710 winbind_close_sock(ctx
);
711 return -1; /* poll error */
714 /* Write should be OK if fd not available for reading */
716 if ((ret
== 1) && (pfd
.revents
& (POLLIN
|POLLHUP
|POLLERR
))) {
718 /* Pipe has closed on remote end */
720 winbind_close_sock(ctx
);
726 result
= write(fd
, (char *)buffer
+ nwritten
,
729 if ((result
== -1) || (result
== 0)) {
733 winbind_close_sock(ctx
);
743 /* Read data from winbindd socket */
745 static int winbind_read_sock(struct winbindd_context
*ctx
,
746 void *buffer
, int count
)
752 fd
= winbind_open_pipe_sock(ctx
, false, false);
757 /* Read data from socket */
758 while(nread
< count
) {
762 /* Catch pipe close on other end by checking if a read()
763 call would not block by calling poll(). */
766 pfd
.events
= POLLIN
|POLLHUP
;
768 /* Wait for 5 seconds for a reply. May need to parameterise this... */
770 ret
= poll(&pfd
, 1, 5000);
772 winbind_close_sock(ctx
);
773 return -1; /* poll error */
777 /* Not ready for read yet... */
778 if (total_time
>= 300) {
780 winbind_close_sock(ctx
);
787 if ((ret
== 1) && (pfd
.revents
& (POLLIN
|POLLHUP
|POLLERR
))) {
791 int result
= read(fd
, (char *)buffer
+ nread
,
794 if ((result
== -1) || (result
== 0)) {
796 /* Read failed. I think the only useful thing we
797 can do here is just return -1 and fail since the
798 transaction has failed half way through. */
800 winbind_close_sock(ctx
);
814 static int winbindd_read_reply(struct winbindd_context
*ctx
,
815 struct winbindd_response
*response
)
817 int result1
, result2
= 0;
823 /* Read fixed length response */
825 result1
= winbind_read_sock(ctx
, response
,
826 sizeof(struct winbindd_response
));
828 /* We actually send the pointer value of the extra_data field from
829 the server. This has no meaning in the client's address space
830 so we clear it out. */
832 response
->extra_data
.data
= NULL
;
838 if (response
->length
< sizeof(struct winbindd_response
)) {
842 /* Read variable length response */
844 if (response
->length
> sizeof(struct winbindd_response
)) {
845 int extra_data_len
= response
->length
-
846 sizeof(struct winbindd_response
);
848 /* Mallocate memory for extra data */
850 if (!(response
->extra_data
.data
= malloc(extra_data_len
))) {
854 result2
= winbind_read_sock(ctx
, response
->extra_data
.data
,
857 winbindd_free_response(response
);
862 /* Return total amount of data read */
864 return result1
+ result2
;
868 * send simple types of requests
871 static NSS_STATUS
winbindd_send_request(
872 struct winbindd_context
*ctx
,
875 struct winbindd_request
*request
)
877 struct winbindd_request lrequest
;
879 /* Check for our tricky environment variable */
881 if (winbind_env_set()) {
882 return NSS_STATUS_NOTFOUND
;
886 ZERO_STRUCT(lrequest
);
890 /* Fill in request and send down pipe */
892 winbindd_init_request(request
, req_type
);
894 if (winbind_write_sock(ctx
, request
, sizeof(*request
),
895 request
->wb_flags
& WBFLAG_RECURSE
,
898 /* Set ENOENT for consistency. Required by some apps */
901 return NSS_STATUS_UNAVAIL
;
904 if ((request
->extra_len
!= 0) &&
905 (winbind_write_sock(ctx
, request
->extra_data
.data
,
907 request
->wb_flags
& WBFLAG_RECURSE
,
910 /* Set ENOENT for consistency. Required by some apps */
913 return NSS_STATUS_UNAVAIL
;
916 return NSS_STATUS_SUCCESS
;
920 * Get results from winbindd request
923 static NSS_STATUS
winbindd_get_response(struct winbindd_context
*ctx
,
924 struct winbindd_response
*response
)
926 struct winbindd_response lresponse
;
929 ZERO_STRUCT(lresponse
);
930 response
= &lresponse
;
933 init_response(response
);
936 if (winbindd_read_reply(ctx
, response
) == -1) {
937 /* Set ENOENT for consistency. Required by some apps */
940 return NSS_STATUS_UNAVAIL
;
943 /* Throw away extra data if client didn't request it */
944 if (response
== &lresponse
) {
945 winbindd_free_response(response
);
948 /* Copy reply data from socket */
949 if (response
->result
!= WINBINDD_OK
) {
950 return NSS_STATUS_NOTFOUND
;
953 return NSS_STATUS_SUCCESS
;
956 /* Handle simple types of requests */
958 NSS_STATUS
winbindd_request_response(struct winbindd_context
*ctx
,
960 struct winbindd_request
*request
,
961 struct winbindd_response
*response
)
963 NSS_STATUS status
= NSS_STATUS_UNAVAIL
;
966 ctx
= get_wb_global_ctx();
969 status
= winbindd_send_request(ctx
, req_type
, 0, request
);
970 if (status
!= NSS_STATUS_SUCCESS
) {
973 status
= winbindd_get_response(ctx
, response
);
979 NSS_STATUS
winbindd_priv_request_response(struct winbindd_context
*ctx
,
981 struct winbindd_request
*request
,
982 struct winbindd_response
*response
)
984 NSS_STATUS status
= NSS_STATUS_UNAVAIL
;
987 ctx
= get_wb_global_ctx();
990 status
= winbindd_send_request(ctx
, req_type
, 1, request
);
991 if (status
!= NSS_STATUS_SUCCESS
) {
994 status
= winbindd_get_response(ctx
, response
);
1000 /* Create and free winbindd context */
1002 struct winbindd_context
*winbindd_ctx_create(void)
1004 struct winbindd_context
*ctx
;
1006 ctx
= calloc(1, sizeof(struct winbindd_context
));
1012 ctx
->winbindd_fd
= -1;
1014 WB_GLOBAL_LIST_LOCK
;
1015 DLIST_ADD_END(wb_global_ctx
.list
, ctx
);
1016 WB_GLOBAL_LIST_UNLOCK
;
1021 void winbindd_ctx_free(struct winbindd_context
*ctx
)
1023 WB_GLOBAL_LIST_LOCK
;
1024 winbind_ctx_free_locked(ctx
);
1025 WB_GLOBAL_LIST_UNLOCK
;