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"
32 struct winbindd_context
{
33 int winbindd_fd
; /* winbind file descriptor */
34 bool is_privileged
; /* using the privileged socket? */
35 pid_t our_pid
; /* calling process pid */
38 static struct winbindd_context wb_global_ctx
= {
44 /* Free a response structure */
46 void winbindd_free_response(struct winbindd_response
*response
)
48 /* Free any allocated extra_data */
51 SAFE_FREE(response
->extra_data
.data
);
54 /* Initialise a request structure */
56 static void winbindd_init_request(struct winbindd_request
*request
,
59 request
->length
= sizeof(struct winbindd_request
);
61 request
->cmd
= (enum winbindd_cmd
)request_type
;
62 request
->pid
= getpid();
66 /* Initialise a response structure */
68 static void init_response(struct winbindd_response
*response
)
70 /* Initialise return value */
72 response
->result
= WINBINDD_ERROR
;
75 /* Close established socket */
77 static void winbind_close_sock(struct winbindd_context
*ctx
)
83 if (ctx
->winbindd_fd
!= -1) {
84 close(ctx
->winbindd_fd
);
85 ctx
->winbindd_fd
= -1;
89 /* Destructor for global context to ensure fd is closed */
91 #if HAVE_FUNCTION_ATTRIBUTE_DESTRUCTOR
92 __attribute__((destructor
))
94 static void winbind_destructor(void)
96 winbind_close_sock(&wb_global_ctx
);
99 #define CONNECT_TIMEOUT 30
101 /* Make sure socket handle isn't stdin, stdout or stderr */
102 #define RECURSION_LIMIT 3
104 static int make_nonstd_fd_internals(int fd
, int limit
/* Recursion limiter */)
107 if (fd
>= 0 && fd
<= 2) {
109 if ((new_fd
= fcntl(fd
, F_DUPFD
, 3)) == -1) {
127 /* use the program stack to hold our list of FDs to close */
128 new_fd
= make_nonstd_fd_internals(new_fd
, limit
- 1);
136 /****************************************************************************
137 Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available,
141 Set close on exec also.
142 ****************************************************************************/
144 static int make_safe_fd(int fd
)
147 int new_fd
= make_nonstd_fd_internals(fd
, RECURSION_LIMIT
);
153 /* Socket should be nonblocking. */
155 #define FLAG_TO_SET O_NONBLOCK
158 #define FLAG_TO_SET O_NDELAY
160 #define FLAG_TO_SET FNDELAY
164 if ((flags
= fcntl(new_fd
, F_GETFL
)) == -1) {
169 flags
|= FLAG_TO_SET
;
170 if (fcntl(new_fd
, F_SETFL
, flags
) == -1) {
177 /* Socket should be closed on exec() */
179 result
= flags
= fcntl(new_fd
, F_GETFD
, 0);
182 result
= fcntl( new_fd
, F_SETFD
, flags
);
195 * @brief Check if we talk to the priviliged pipe which should be owned by root.
197 * This checks if we have uid_wrapper running and if this is the case it will
198 * allow to connect to the winbind privileged pipe even it is not owned by root.
200 * @param[in] uid The uid to check if we can safely talk to the pipe.
202 * @return If we have access it returns true, else false.
204 static bool winbind_privileged_pipe_is_root(uid_t uid
)
210 if (uid_wrapper_enabled()) {
217 /* Connect to winbindd socket */
219 static int winbind_named_pipe_sock(const char *dir
)
221 struct sockaddr_un sunaddr
;
228 /* Check permissions on unix socket directory */
230 if (lstat(dir
, &st
) == -1) {
236 * This tells us that the pipe is owned by a privileged
237 * process, as we will be sending passwords to it.
239 if (!S_ISDIR(st
.st_mode
) ||
240 !winbind_privileged_pipe_is_root(st
.st_uid
)) {
245 /* Connect to socket */
247 sunaddr
= (struct sockaddr_un
) { .sun_family
= AF_UNIX
};
249 ret
= snprintf(sunaddr
.sun_path
, sizeof(sunaddr
.sun_path
),
250 "%s/%s", dir
, WINBINDD_SOCKET_NAME
);
251 if ((ret
== -1) || (ret
>= sizeof(sunaddr
.sun_path
))) {
252 errno
= ENAMETOOLONG
;
256 /* If socket file doesn't exist, don't bother trying to connect
257 with retry. This is an attempt to make the system usable when
258 the winbindd daemon is not running. */
260 if (lstat(sunaddr
.sun_path
, &st
) == -1) {
265 /* Check permissions on unix socket file */
268 * This tells us that the pipe is owned by a privileged
269 * process, as we will be sending passwords to it.
271 if (!S_ISSOCK(st
.st_mode
) ||
272 !winbind_privileged_pipe_is_root(st
.st_uid
)) {
277 /* Connect to socket */
279 if ((fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
283 /* Set socket non-blocking and close on exec. */
285 if ((fd
= make_safe_fd( fd
)) == -1) {
289 for (wait_time
= 0; connect(fd
, (struct sockaddr
*)&sunaddr
, sizeof(sunaddr
)) == -1;
290 wait_time
+= slept
) {
292 int connect_errno
= 0;
295 if (wait_time
>= CONNECT_TIMEOUT
)
301 pfd
.events
= POLLOUT
;
303 ret
= poll(&pfd
, 1, (CONNECT_TIMEOUT
- wait_time
) * 1000);
306 errnosize
= sizeof(connect_errno
);
308 ret
= getsockopt(fd
, SOL_SOCKET
,
309 SO_ERROR
, &connect_errno
, &errnosize
);
311 if (ret
>= 0 && connect_errno
== 0) {
312 /* Connect succeed */
317 slept
= CONNECT_TIMEOUT
;
320 slept
= rand() % 3 + 1;
339 static const char *winbindd_socket_dir(void)
341 if (nss_wrapper_enabled()) {
344 env_dir
= getenv("SELFTEST_WINBINDD_SOCKET_DIR");
345 if (env_dir
!= NULL
) {
350 return WINBINDD_SOCKET_DIR
;
353 /* Connect to winbindd socket */
355 static int winbind_open_pipe_sock(struct winbindd_context
*ctx
,
356 int recursing
, int need_priv
)
358 #ifdef HAVE_UNIXSOCKET
359 struct winbindd_request request
;
360 struct winbindd_response response
;
362 ZERO_STRUCT(request
);
363 ZERO_STRUCT(response
);
369 if (ctx
->our_pid
!= getpid()) {
370 winbind_close_sock(ctx
);
371 ctx
->our_pid
= getpid();
374 if ((need_priv
!= 0) && (ctx
->is_privileged
== 0)) {
375 winbind_close_sock(ctx
);
378 if (ctx
->winbindd_fd
!= -1) {
379 return ctx
->winbindd_fd
;
386 ctx
->winbindd_fd
= winbind_named_pipe_sock(winbindd_socket_dir());
388 if (ctx
->winbindd_fd
== -1) {
392 ctx
->is_privileged
= 0;
394 /* version-check the socket */
396 request
.wb_flags
= WBFLAG_RECURSE
;
397 if ((winbindd_request_response(ctx
, WINBINDD_INTERFACE_VERSION
, &request
,
398 &response
) != NSS_STATUS_SUCCESS
) ||
399 (response
.data
.interface_version
!= WINBIND_INTERFACE_VERSION
)) {
400 winbind_close_sock(ctx
);
404 /* try and get priv pipe */
406 request
.wb_flags
= WBFLAG_RECURSE
;
408 /* Note that response needs to be initialized to avoid
409 * crashing on clean up after WINBINDD_PRIV_PIPE_DIR call failed
410 * as interface version (from the first request) returned as a fstring,
411 * thus response.extra_data.data will not be NULL even though
412 * winbindd response did not write over it due to a failure */
413 ZERO_STRUCT(response
);
414 if (winbindd_request_response(ctx
, WINBINDD_PRIV_PIPE_DIR
, &request
,
415 &response
) == NSS_STATUS_SUCCESS
) {
417 fd
= winbind_named_pipe_sock((char *)response
.extra_data
.data
);
419 close(ctx
->winbindd_fd
);
420 ctx
->winbindd_fd
= fd
;
421 ctx
->is_privileged
= 1;
425 if ((need_priv
!= 0) && (ctx
->is_privileged
== 0)) {
429 SAFE_FREE(response
.extra_data
.data
);
431 return ctx
->winbindd_fd
;
434 #endif /* HAVE_UNIXSOCKET */
437 /* Write data to winbindd socket */
439 static int winbind_write_sock(struct winbindd_context
*ctx
, void *buffer
,
440 int count
, int recursing
, int need_priv
)
442 int fd
, result
, nwritten
;
444 /* Open connection to winbind daemon */
448 fd
= winbind_open_pipe_sock(ctx
, recursing
, need_priv
);
454 /* Write data to socket */
458 while(nwritten
< count
) {
462 /* Catch pipe close on other end by checking if a read()
463 call would not block by calling poll(). */
466 pfd
.events
= POLLIN
|POLLOUT
|POLLHUP
;
468 ret
= poll(&pfd
, 1, -1);
470 winbind_close_sock(ctx
);
471 return -1; /* poll error */
474 /* Write should be OK if fd not available for reading */
476 if ((ret
== 1) && (pfd
.revents
& (POLLIN
|POLLHUP
|POLLERR
))) {
478 /* Pipe has closed on remote end */
480 winbind_close_sock(ctx
);
486 result
= write(fd
, (char *)buffer
+ nwritten
,
489 if ((result
== -1) || (result
== 0)) {
493 winbind_close_sock(ctx
);
503 /* Read data from winbindd socket */
505 static int winbind_read_sock(struct winbindd_context
*ctx
,
506 void *buffer
, int count
)
512 fd
= winbind_open_pipe_sock(ctx
, false, false);
517 /* Read data from socket */
518 while(nread
< count
) {
522 /* Catch pipe close on other end by checking if a read()
523 call would not block by calling poll(). */
526 pfd
.events
= POLLIN
|POLLHUP
;
528 /* Wait for 5 seconds for a reply. May need to parameterise this... */
530 ret
= poll(&pfd
, 1, 5000);
532 winbind_close_sock(ctx
);
533 return -1; /* poll error */
537 /* Not ready for read yet... */
538 if (total_time
>= 300) {
540 winbind_close_sock(ctx
);
547 if ((ret
== 1) && (pfd
.revents
& (POLLIN
|POLLHUP
|POLLERR
))) {
551 int result
= read(fd
, (char *)buffer
+ nread
,
554 if ((result
== -1) || (result
== 0)) {
556 /* Read failed. I think the only useful thing we
557 can do here is just return -1 and fail since the
558 transaction has failed half way through. */
560 winbind_close_sock(ctx
);
574 static int winbindd_read_reply(struct winbindd_context
*ctx
,
575 struct winbindd_response
*response
)
577 int result1
, result2
= 0;
583 /* Read fixed length response */
585 result1
= winbind_read_sock(ctx
, response
,
586 sizeof(struct winbindd_response
));
588 /* We actually send the pointer value of the extra_data field from
589 the server. This has no meaning in the client's address space
590 so we clear it out. */
592 response
->extra_data
.data
= NULL
;
598 if (response
->length
< sizeof(struct winbindd_response
)) {
602 /* Read variable length response */
604 if (response
->length
> sizeof(struct winbindd_response
)) {
605 int extra_data_len
= response
->length
-
606 sizeof(struct winbindd_response
);
608 /* Mallocate memory for extra data */
610 if (!(response
->extra_data
.data
= malloc(extra_data_len
))) {
614 result2
= winbind_read_sock(ctx
, response
->extra_data
.data
,
617 winbindd_free_response(response
);
622 /* Return total amount of data read */
624 return result1
+ result2
;
628 * send simple types of requests
631 NSS_STATUS
winbindd_send_request(struct winbindd_context
*ctx
,
632 int req_type
, int need_priv
,
633 struct winbindd_request
*request
)
635 struct winbindd_request lrequest
;
637 /* Check for our tricky environment variable */
639 if (winbind_env_set()) {
640 return NSS_STATUS_NOTFOUND
;
644 ZERO_STRUCT(lrequest
);
648 /* Fill in request and send down pipe */
650 winbindd_init_request(request
, req_type
);
652 if (winbind_write_sock(ctx
, request
, sizeof(*request
),
653 request
->wb_flags
& WBFLAG_RECURSE
,
656 /* Set ENOENT for consistency. Required by some apps */
659 return NSS_STATUS_UNAVAIL
;
662 if ((request
->extra_len
!= 0) &&
663 (winbind_write_sock(ctx
, request
->extra_data
.data
,
665 request
->wb_flags
& WBFLAG_RECURSE
,
668 /* Set ENOENT for consistency. Required by some apps */
671 return NSS_STATUS_UNAVAIL
;
674 return NSS_STATUS_SUCCESS
;
678 * Get results from winbindd request
681 NSS_STATUS
winbindd_get_response(struct winbindd_context
*ctx
,
682 struct winbindd_response
*response
)
684 struct winbindd_response lresponse
;
687 ZERO_STRUCT(lresponse
);
688 response
= &lresponse
;
691 init_response(response
);
694 if (winbindd_read_reply(ctx
, response
) == -1) {
695 /* Set ENOENT for consistency. Required by some apps */
698 return NSS_STATUS_UNAVAIL
;
701 /* Throw away extra data if client didn't request it */
702 if (response
== &lresponse
) {
703 winbindd_free_response(response
);
706 /* Copy reply data from socket */
707 if (response
->result
!= WINBINDD_OK
) {
708 return NSS_STATUS_NOTFOUND
;
711 return NSS_STATUS_SUCCESS
;
714 /* Handle simple types of requests */
716 NSS_STATUS
winbindd_request_response(struct winbindd_context
*ctx
,
718 struct winbindd_request
*request
,
719 struct winbindd_response
*response
)
721 NSS_STATUS status
= NSS_STATUS_UNAVAIL
;
722 struct winbindd_context
*wb_ctx
= ctx
;
725 wb_ctx
= &wb_global_ctx
;
728 status
= winbindd_send_request(wb_ctx
, req_type
, 0, request
);
729 if (status
!= NSS_STATUS_SUCCESS
)
731 status
= winbindd_get_response(wb_ctx
, response
);
736 NSS_STATUS
winbindd_priv_request_response(struct winbindd_context
*ctx
,
738 struct winbindd_request
*request
,
739 struct winbindd_response
*response
)
741 NSS_STATUS status
= NSS_STATUS_UNAVAIL
;
742 struct winbindd_context
*wb_ctx
= ctx
;
745 wb_ctx
= &wb_global_ctx
;
748 status
= winbindd_send_request(wb_ctx
, req_type
, 1, request
);
749 if (status
!= NSS_STATUS_SUCCESS
)
751 status
= winbindd_get_response(wb_ctx
, response
);
756 /* Create and free winbindd context */
758 struct winbindd_context
*winbindd_ctx_create(void)
760 struct winbindd_context
*ctx
;
762 ctx
= calloc(1, sizeof(struct winbindd_context
));
768 ctx
->winbindd_fd
= -1;
773 void winbindd_ctx_free(struct winbindd_context
*ctx
)
775 winbind_close_sock(ctx
);