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 int winbind_write_sock(struct winbindd_context
*ctx
, void *buffer
,
675 int count
, int recursing
, int need_priv
)
677 int fd
, result
, nwritten
;
679 /* Open connection to winbind daemon */
683 fd
= winbind_open_pipe_sock(ctx
, recursing
, need_priv
);
689 /* Write data to socket */
693 while(nwritten
< count
) {
697 /* Catch pipe close on other end by checking if a read()
698 call would not block by calling poll(). */
701 pfd
.events
= POLLIN
|POLLOUT
|POLLHUP
;
703 ret
= poll(&pfd
, 1, -1);
705 winbind_close_sock(ctx
);
706 return -1; /* poll error */
709 /* Write should be OK if fd not available for reading */
711 if ((ret
== 1) && (pfd
.revents
& (POLLIN
|POLLHUP
|POLLERR
))) {
713 /* Pipe has closed on remote end */
715 winbind_close_sock(ctx
);
721 result
= write(fd
, (char *)buffer
+ nwritten
,
724 if ((result
== -1) || (result
== 0)) {
728 winbind_close_sock(ctx
);
738 /* Read data from winbindd socket */
740 static int winbind_read_sock(struct winbindd_context
*ctx
,
741 void *buffer
, int count
)
747 fd
= winbind_open_pipe_sock(ctx
, false, false);
752 /* Read data from socket */
753 while(nread
< count
) {
757 /* Catch pipe close on other end by checking if a read()
758 call would not block by calling poll(). */
761 pfd
.events
= POLLIN
|POLLHUP
;
763 /* Wait for 5 seconds for a reply. May need to parameterise this... */
765 ret
= poll(&pfd
, 1, 5000);
767 winbind_close_sock(ctx
);
768 return -1; /* poll error */
772 /* Not ready for read yet... */
773 if (total_time
>= 300) {
775 winbind_close_sock(ctx
);
782 if ((ret
== 1) && (pfd
.revents
& (POLLIN
|POLLHUP
|POLLERR
))) {
786 int result
= read(fd
, (char *)buffer
+ nread
,
789 if ((result
== -1) || (result
== 0)) {
791 /* Read failed. I think the only useful thing we
792 can do here is just return -1 and fail since the
793 transaction has failed half way through. */
795 winbind_close_sock(ctx
);
809 static int winbindd_read_reply(struct winbindd_context
*ctx
,
810 struct winbindd_response
*response
)
812 int result1
, result2
= 0;
818 /* Read fixed length response */
820 result1
= winbind_read_sock(ctx
, response
,
821 sizeof(struct winbindd_response
));
823 /* We actually send the pointer value of the extra_data field from
824 the server. This has no meaning in the client's address space
825 so we clear it out. */
827 response
->extra_data
.data
= NULL
;
833 if (response
->length
< sizeof(struct winbindd_response
)) {
837 /* Read variable length response */
839 if (response
->length
> sizeof(struct winbindd_response
)) {
840 int extra_data_len
= response
->length
-
841 sizeof(struct winbindd_response
);
843 /* Mallocate memory for extra data */
845 if (!(response
->extra_data
.data
= malloc(extra_data_len
))) {
849 result2
= winbind_read_sock(ctx
, response
->extra_data
.data
,
852 winbindd_free_response(response
);
857 /* Return total amount of data read */
859 return result1
+ result2
;
863 * send simple types of requests
866 static NSS_STATUS
winbindd_send_request(
867 struct winbindd_context
*ctx
,
870 struct winbindd_request
*request
)
872 struct winbindd_request lrequest
;
874 /* Check for our tricky environment variable */
876 if (winbind_env_set()) {
877 return NSS_STATUS_NOTFOUND
;
881 ZERO_STRUCT(lrequest
);
885 /* Fill in request and send down pipe */
887 winbindd_init_request(request
, req_type
);
889 if (winbind_write_sock(ctx
, request
, sizeof(*request
),
890 request
->wb_flags
& WBFLAG_RECURSE
,
893 /* Set ENOENT for consistency. Required by some apps */
896 return NSS_STATUS_UNAVAIL
;
899 if ((request
->extra_len
!= 0) &&
900 (winbind_write_sock(ctx
, request
->extra_data
.data
,
902 request
->wb_flags
& WBFLAG_RECURSE
,
905 /* Set ENOENT for consistency. Required by some apps */
908 return NSS_STATUS_UNAVAIL
;
911 return NSS_STATUS_SUCCESS
;
915 * Get results from winbindd request
918 static NSS_STATUS
winbindd_get_response(struct winbindd_context
*ctx
,
919 struct winbindd_response
*response
)
921 struct winbindd_response lresponse
;
924 ZERO_STRUCT(lresponse
);
925 response
= &lresponse
;
928 init_response(response
);
931 if (winbindd_read_reply(ctx
, response
) == -1) {
932 /* Set ENOENT for consistency. Required by some apps */
935 return NSS_STATUS_UNAVAIL
;
938 /* Throw away extra data if client didn't request it */
939 if (response
== &lresponse
) {
940 winbindd_free_response(response
);
943 /* Copy reply data from socket */
944 if (response
->result
!= WINBINDD_OK
) {
945 return NSS_STATUS_NOTFOUND
;
948 return NSS_STATUS_SUCCESS
;
951 /* Handle simple types of requests */
953 NSS_STATUS
winbindd_request_response(struct winbindd_context
*ctx
,
955 struct winbindd_request
*request
,
956 struct winbindd_response
*response
)
958 NSS_STATUS status
= NSS_STATUS_UNAVAIL
;
961 ctx
= get_wb_global_ctx();
964 status
= winbindd_send_request(ctx
, req_type
, 0, request
);
965 if (status
!= NSS_STATUS_SUCCESS
) {
968 status
= winbindd_get_response(ctx
, response
);
974 NSS_STATUS
winbindd_priv_request_response(struct winbindd_context
*ctx
,
976 struct winbindd_request
*request
,
977 struct winbindd_response
*response
)
979 NSS_STATUS status
= NSS_STATUS_UNAVAIL
;
982 ctx
= get_wb_global_ctx();
985 status
= winbindd_send_request(ctx
, req_type
, 1, request
);
986 if (status
!= NSS_STATUS_SUCCESS
) {
989 status
= winbindd_get_response(ctx
, response
);
995 /* Create and free winbindd context */
997 struct winbindd_context
*winbindd_ctx_create(void)
999 struct winbindd_context
*ctx
;
1001 ctx
= calloc(1, sizeof(struct winbindd_context
));
1007 ctx
->winbindd_fd
= -1;
1009 WB_GLOBAL_LIST_LOCK
;
1010 DLIST_ADD_END(wb_global_ctx
.list
, ctx
);
1011 WB_GLOBAL_LIST_UNLOCK
;
1016 void winbindd_ctx_free(struct winbindd_context
*ctx
)
1018 WB_GLOBAL_LIST_LOCK
;
1019 winbind_ctx_free_locked(ctx
);
1020 WB_GLOBAL_LIST_UNLOCK
;