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
))
155 static void winbind_destructor(void)
157 struct winbindd_context
*ctx
;
159 ctx
= get_wb_global_ctx();
160 winbind_close_sock(ctx
);
164 #define CONNECT_TIMEOUT 30
166 /* Make sure socket handle isn't stdin, stdout or stderr */
167 #define RECURSION_LIMIT 3
169 static int make_nonstd_fd_internals(int fd
, int limit
/* Recursion limiter */)
172 if (fd
>= 0 && fd
<= 2) {
174 if ((new_fd
= fcntl(fd
, F_DUPFD
, 3)) == -1) {
192 /* use the program stack to hold our list of FDs to close */
193 new_fd
= make_nonstd_fd_internals(new_fd
, limit
- 1);
201 /****************************************************************************
202 Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available,
206 Set close on exec also.
207 ****************************************************************************/
209 static int make_safe_fd(int fd
)
212 int new_fd
= make_nonstd_fd_internals(fd
, RECURSION_LIMIT
);
218 /* Socket should be nonblocking. */
220 #define FLAG_TO_SET O_NONBLOCK
223 #define FLAG_TO_SET O_NDELAY
225 #define FLAG_TO_SET FNDELAY
229 if ((flags
= fcntl(new_fd
, F_GETFL
)) == -1) {
234 flags
|= FLAG_TO_SET
;
235 if (fcntl(new_fd
, F_SETFL
, flags
) == -1) {
242 /* Socket should be closed on exec() */
244 result
= flags
= fcntl(new_fd
, F_GETFD
, 0);
247 result
= fcntl( new_fd
, F_SETFD
, flags
);
260 * @brief Check if we talk to the priviliged pipe which should be owned by root.
262 * This checks if we have uid_wrapper running and if this is the case it will
263 * allow one to connect to the winbind privileged pipe even it is not owned by root.
265 * @param[in] uid The uid to check if we can safely talk to the pipe.
267 * @return If we have access it returns true, else false.
269 static bool winbind_privileged_pipe_is_root(uid_t uid
)
275 if (uid_wrapper_enabled()) {
282 /* Connect to winbindd socket */
284 static int winbind_named_pipe_sock(const char *dir
)
286 struct sockaddr_un sunaddr
;
293 /* Check permissions on unix socket directory */
295 if (lstat(dir
, &st
) == -1) {
301 * This tells us that the pipe is owned by a privileged
302 * process, as we will be sending passwords to it.
304 if (!S_ISDIR(st
.st_mode
) ||
305 !winbind_privileged_pipe_is_root(st
.st_uid
)) {
310 /* Connect to socket */
312 sunaddr
= (struct sockaddr_un
) { .sun_family
= AF_UNIX
};
314 ret
= snprintf(sunaddr
.sun_path
, sizeof(sunaddr
.sun_path
),
315 "%s/%s", dir
, WINBINDD_SOCKET_NAME
);
316 if ((ret
== -1) || (ret
>= sizeof(sunaddr
.sun_path
))) {
317 errno
= ENAMETOOLONG
;
321 /* If socket file doesn't exist, don't bother trying to connect
322 with retry. This is an attempt to make the system usable when
323 the winbindd daemon is not running. */
325 if (lstat(sunaddr
.sun_path
, &st
) == -1) {
330 /* Check permissions on unix socket file */
333 * This tells us that the pipe is owned by a privileged
334 * process, as we will be sending passwords to it.
336 if (!S_ISSOCK(st
.st_mode
) ||
337 !winbind_privileged_pipe_is_root(st
.st_uid
)) {
342 /* Connect to socket */
344 if ((fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
348 /* Set socket non-blocking and close on exec. */
350 if ((fd
= make_safe_fd( fd
)) == -1) {
354 for (wait_time
= 0; connect(fd
, (struct sockaddr
*)&sunaddr
, sizeof(sunaddr
)) == -1;
355 wait_time
+= slept
) {
357 int connect_errno
= 0;
360 if (wait_time
>= CONNECT_TIMEOUT
)
366 pfd
.events
= POLLOUT
;
368 ret
= poll(&pfd
, 1, (CONNECT_TIMEOUT
- wait_time
) * 1000);
371 errnosize
= sizeof(connect_errno
);
373 ret
= getsockopt(fd
, SOL_SOCKET
,
374 SO_ERROR
, &connect_errno
, &errnosize
);
376 if (ret
>= 0 && connect_errno
== 0) {
377 /* Connect succeed */
382 slept
= CONNECT_TIMEOUT
;
385 slept
= rand() % 3 + 1;
404 static const char *winbindd_socket_dir(void)
406 if (nss_wrapper_enabled()) {
409 env_dir
= getenv("SELFTEST_WINBINDD_SOCKET_DIR");
410 if (env_dir
!= NULL
) {
415 return WINBINDD_SOCKET_DIR
;
418 /* Connect to winbindd socket */
420 static int winbind_open_pipe_sock(struct winbindd_context
*ctx
,
421 int recursing
, int need_priv
)
423 #ifdef HAVE_UNIXSOCKET
424 struct winbindd_request request
;
425 struct winbindd_response response
;
427 ZERO_STRUCT(request
);
428 ZERO_STRUCT(response
);
434 if (ctx
->our_pid
!= getpid()) {
435 winbind_close_sock(ctx
);
436 ctx
->our_pid
= getpid();
439 if ((need_priv
!= 0) && !ctx
->is_privileged
) {
440 winbind_close_sock(ctx
);
443 if (ctx
->winbindd_fd
!= -1) {
444 return ctx
->winbindd_fd
;
451 ctx
->winbindd_fd
= winbind_named_pipe_sock(winbindd_socket_dir());
453 if (ctx
->winbindd_fd
== -1) {
457 ctx
->is_privileged
= false;
459 /* version-check the socket */
461 request
.wb_flags
= WBFLAG_RECURSE
;
462 if ((winbindd_request_response(ctx
, WINBINDD_INTERFACE_VERSION
, &request
,
463 &response
) != NSS_STATUS_SUCCESS
) ||
464 (response
.data
.interface_version
!= WINBIND_INTERFACE_VERSION
)) {
465 winbind_close_sock(ctx
);
469 if (need_priv
== 0) {
470 return ctx
->winbindd_fd
;
473 /* try and get priv pipe */
475 request
.wb_flags
= WBFLAG_RECURSE
;
477 /* Note that response needs to be initialized to avoid
478 * crashing on clean up after WINBINDD_PRIV_PIPE_DIR call failed
479 * as interface version (from the first request) returned as a fstring,
480 * thus response.extra_data.data will not be NULL even though
481 * winbindd response did not write over it due to a failure */
482 ZERO_STRUCT(response
);
483 if (winbindd_request_response(ctx
, WINBINDD_PRIV_PIPE_DIR
, &request
,
484 &response
) == NSS_STATUS_SUCCESS
) {
486 fd
= winbind_named_pipe_sock((char *)response
.extra_data
.data
);
488 close(ctx
->winbindd_fd
);
489 ctx
->winbindd_fd
= fd
;
490 ctx
->is_privileged
= true;
493 SAFE_FREE(response
.extra_data
.data
);
496 if (!ctx
->is_privileged
) {
500 return ctx
->winbindd_fd
;
503 #endif /* HAVE_UNIXSOCKET */
506 /* Write data to winbindd socket */
508 static int winbind_write_sock(struct winbindd_context
*ctx
, void *buffer
,
509 int count
, int recursing
, int need_priv
)
511 int fd
, result
, nwritten
;
513 /* Open connection to winbind daemon */
517 fd
= winbind_open_pipe_sock(ctx
, recursing
, need_priv
);
523 /* Write data to socket */
527 while(nwritten
< count
) {
531 /* Catch pipe close on other end by checking if a read()
532 call would not block by calling poll(). */
535 pfd
.events
= POLLIN
|POLLOUT
|POLLHUP
;
537 ret
= poll(&pfd
, 1, -1);
539 winbind_close_sock(ctx
);
540 return -1; /* poll error */
543 /* Write should be OK if fd not available for reading */
545 if ((ret
== 1) && (pfd
.revents
& (POLLIN
|POLLHUP
|POLLERR
))) {
547 /* Pipe has closed on remote end */
549 winbind_close_sock(ctx
);
555 result
= write(fd
, (char *)buffer
+ nwritten
,
558 if ((result
== -1) || (result
== 0)) {
562 winbind_close_sock(ctx
);
572 /* Read data from winbindd socket */
574 static int winbind_read_sock(struct winbindd_context
*ctx
,
575 void *buffer
, int count
)
581 fd
= winbind_open_pipe_sock(ctx
, false, false);
586 /* Read data from socket */
587 while(nread
< count
) {
591 /* Catch pipe close on other end by checking if a read()
592 call would not block by calling poll(). */
595 pfd
.events
= POLLIN
|POLLHUP
;
597 /* Wait for 5 seconds for a reply. May need to parameterise this... */
599 ret
= poll(&pfd
, 1, 5000);
601 winbind_close_sock(ctx
);
602 return -1; /* poll error */
606 /* Not ready for read yet... */
607 if (total_time
>= 300) {
609 winbind_close_sock(ctx
);
616 if ((ret
== 1) && (pfd
.revents
& (POLLIN
|POLLHUP
|POLLERR
))) {
620 int result
= read(fd
, (char *)buffer
+ nread
,
623 if ((result
== -1) || (result
== 0)) {
625 /* Read failed. I think the only useful thing we
626 can do here is just return -1 and fail since the
627 transaction has failed half way through. */
629 winbind_close_sock(ctx
);
643 static int winbindd_read_reply(struct winbindd_context
*ctx
,
644 struct winbindd_response
*response
)
646 int result1
, result2
= 0;
652 /* Read fixed length response */
654 result1
= winbind_read_sock(ctx
, response
,
655 sizeof(struct winbindd_response
));
657 /* We actually send the pointer value of the extra_data field from
658 the server. This has no meaning in the client's address space
659 so we clear it out. */
661 response
->extra_data
.data
= NULL
;
667 if (response
->length
< sizeof(struct winbindd_response
)) {
671 /* Read variable length response */
673 if (response
->length
> sizeof(struct winbindd_response
)) {
674 int extra_data_len
= response
->length
-
675 sizeof(struct winbindd_response
);
677 /* Mallocate memory for extra data */
679 if (!(response
->extra_data
.data
= malloc(extra_data_len
))) {
683 result2
= winbind_read_sock(ctx
, response
->extra_data
.data
,
686 winbindd_free_response(response
);
691 /* Return total amount of data read */
693 return result1
+ result2
;
697 * send simple types of requests
700 static NSS_STATUS
winbindd_send_request(
701 struct winbindd_context
*ctx
,
704 struct winbindd_request
*request
)
706 struct winbindd_request lrequest
;
708 /* Check for our tricky environment variable */
710 if (winbind_env_set()) {
711 return NSS_STATUS_NOTFOUND
;
715 ZERO_STRUCT(lrequest
);
719 /* Fill in request and send down pipe */
721 winbindd_init_request(request
, req_type
);
723 if (winbind_write_sock(ctx
, request
, sizeof(*request
),
724 request
->wb_flags
& WBFLAG_RECURSE
,
727 /* Set ENOENT for consistency. Required by some apps */
730 return NSS_STATUS_UNAVAIL
;
733 if ((request
->extra_len
!= 0) &&
734 (winbind_write_sock(ctx
, request
->extra_data
.data
,
736 request
->wb_flags
& WBFLAG_RECURSE
,
739 /* Set ENOENT for consistency. Required by some apps */
742 return NSS_STATUS_UNAVAIL
;
745 return NSS_STATUS_SUCCESS
;
749 * Get results from winbindd request
752 static NSS_STATUS
winbindd_get_response(struct winbindd_context
*ctx
,
753 struct winbindd_response
*response
)
755 struct winbindd_response lresponse
;
758 ZERO_STRUCT(lresponse
);
759 response
= &lresponse
;
762 init_response(response
);
765 if (winbindd_read_reply(ctx
, response
) == -1) {
766 /* Set ENOENT for consistency. Required by some apps */
769 return NSS_STATUS_UNAVAIL
;
772 /* Throw away extra data if client didn't request it */
773 if (response
== &lresponse
) {
774 winbindd_free_response(response
);
777 /* Copy reply data from socket */
778 if (response
->result
!= WINBINDD_OK
) {
779 return NSS_STATUS_NOTFOUND
;
782 return NSS_STATUS_SUCCESS
;
785 /* Handle simple types of requests */
787 NSS_STATUS
winbindd_request_response(struct winbindd_context
*ctx
,
789 struct winbindd_request
*request
,
790 struct winbindd_response
*response
)
792 NSS_STATUS status
= NSS_STATUS_UNAVAIL
;
793 bool release_global_ctx
= false;
796 ctx
= get_wb_global_ctx();
797 release_global_ctx
= true;
800 status
= winbindd_send_request(ctx
, req_type
, 0, request
);
801 if (status
!= NSS_STATUS_SUCCESS
) {
804 status
= winbindd_get_response(ctx
, response
);
807 if (release_global_ctx
) {
813 NSS_STATUS
winbindd_priv_request_response(struct winbindd_context
*ctx
,
815 struct winbindd_request
*request
,
816 struct winbindd_response
*response
)
818 NSS_STATUS status
= NSS_STATUS_UNAVAIL
;
819 bool release_global_ctx
= false;
822 ctx
= get_wb_global_ctx();
823 release_global_ctx
= true;
826 status
= winbindd_send_request(ctx
, req_type
, 1, request
);
827 if (status
!= NSS_STATUS_SUCCESS
) {
830 status
= winbindd_get_response(ctx
, response
);
833 if (release_global_ctx
) {
839 /* Create and free winbindd context */
841 struct winbindd_context
*winbindd_ctx_create(void)
843 struct winbindd_context
*ctx
;
845 ctx
= calloc(1, sizeof(struct winbindd_context
));
851 ctx
->winbindd_fd
= -1;
856 void winbindd_ctx_free(struct winbindd_context
*ctx
)
858 winbind_close_sock(ctx
);