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
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Library General Public
13 License as published by the Free Software Foundation; either
14 version 2 of the License, or (at your option) any later version.
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Library General Public License for more details.
21 You should have received a copy of the GNU Library General Public
22 License along with this library; if not, write to the
23 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 Boston, MA 02111-1307, USA.
27 #include "winbind_client.h"
29 BOOL
winbind_env_set( void );
30 BOOL
winbind_off( void );
31 BOOL
winbind_on( void );
33 /* Global variables. These are effectively the client state information */
35 int winbindd_fd
= -1; /* fd for winbindd socket */
36 static int is_privileged
= 0;
38 /* Free a response structure */
40 void free_response(struct winbindd_response
*response
)
42 /* Free any allocated extra_data */
45 SAFE_FREE(response
->extra_data
.data
);
48 /* Initialise a request structure */
50 void init_request(struct winbindd_request
*request
, int request_type
)
52 request
->length
= sizeof(struct winbindd_request
);
54 request
->cmd
= (enum winbindd_cmd
)request_type
;
55 request
->pid
= getpid();
59 /* Initialise a response structure */
61 static void init_response(struct winbindd_response
*response
)
63 /* Initialise return value */
65 response
->result
= WINBINDD_ERROR
;
68 /* Close established socket */
72 if (winbindd_fd
!= -1) {
78 #define CONNECT_TIMEOUT 30
80 /* Make sure socket handle isn't stdin, stdout or stderr */
81 #define RECURSION_LIMIT 3
83 static int make_nonstd_fd_internals(int fd
, int limit
/* Recursion limiter */)
86 if (fd
>= 0 && fd
<= 2) {
88 if ((new_fd
= fcntl(fd
, F_DUPFD
, 3)) == -1) {
106 /* use the program stack to hold our list of FDs to close */
107 new_fd
= make_nonstd_fd_internals(new_fd
, limit
- 1);
115 /****************************************************************************
116 Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available,
120 Set close on exec also.
121 ****************************************************************************/
123 static int make_safe_fd(int fd
)
126 int new_fd
= make_nonstd_fd_internals(fd
, RECURSION_LIMIT
);
132 /* Socket should be nonblocking. */
134 #define FLAG_TO_SET O_NONBLOCK
137 #define FLAG_TO_SET O_NDELAY
139 #define FLAG_TO_SET FNDELAY
143 if ((flags
= fcntl(new_fd
, F_GETFL
)) == -1) {
148 flags
|= FLAG_TO_SET
;
149 if (fcntl(new_fd
, F_SETFL
, flags
) == -1) {
156 /* Socket should be closed on exec() */
158 result
= flags
= fcntl(new_fd
, F_GETFD
, 0);
161 result
= fcntl( new_fd
, F_SETFD
, flags
);
171 /* Connect to winbindd socket */
173 static int winbind_named_pipe_sock(const char *dir
)
175 struct sockaddr_un sunaddr
;
182 /* Check permissions on unix socket directory */
184 if (lstat(dir
, &st
) == -1) {
188 if (!S_ISDIR(st
.st_mode
) ||
189 (st
.st_uid
!= 0 && st
.st_uid
!= geteuid())) {
193 /* Connect to socket */
195 strncpy(path
, dir
, sizeof(path
) - 1);
196 path
[sizeof(path
) - 1] = '\0';
198 strncat(path
, "/", sizeof(path
) - 1 - strlen(path
));
199 path
[sizeof(path
) - 1] = '\0';
201 strncat(path
, WINBINDD_SOCKET_NAME
, sizeof(path
) - 1 - strlen(path
));
202 path
[sizeof(path
) - 1] = '\0';
204 ZERO_STRUCT(sunaddr
);
205 sunaddr
.sun_family
= AF_UNIX
;
206 strncpy(sunaddr
.sun_path
, path
, sizeof(sunaddr
.sun_path
) - 1);
208 /* If socket file doesn't exist, don't bother trying to connect
209 with retry. This is an attempt to make the system usable when
210 the winbindd daemon is not running. */
212 if (lstat(path
, &st
) == -1) {
216 /* Check permissions on unix socket file */
218 if (!S_ISSOCK(st
.st_mode
) ||
219 (st
.st_uid
!= 0 && st
.st_uid
!= geteuid())) {
223 /* Connect to socket */
225 if ((fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
229 /* Set socket non-blocking and close on exec. */
231 if ((fd
= make_safe_fd( fd
)) == -1) {
235 for (wait_time
= 0; connect(fd
, (struct sockaddr
*)&sunaddr
, sizeof(sunaddr
)) == -1;
236 wait_time
+= slept
) {
240 int connect_errno
= 0;
243 if (wait_time
>= CONNECT_TIMEOUT
)
250 tv
.tv_sec
= CONNECT_TIMEOUT
- wait_time
;
253 ret
= select(fd
+ 1, NULL
, &w_fds
, NULL
, &tv
);
256 errnosize
= sizeof(connect_errno
);
258 ret
= getsockopt(fd
, SOL_SOCKET
,
259 SO_ERROR
, &connect_errno
, &errnosize
);
261 if (ret
>= 0 && connect_errno
== 0) {
262 /* Connect succeed */
267 slept
= CONNECT_TIMEOUT
;
270 slept
= rand() % 3 + 1;
289 /* Connect to winbindd socket */
291 static int winbind_open_pipe_sock(int recursing
, int need_priv
)
293 #ifdef HAVE_UNIXSOCKET
294 static pid_t our_pid
;
295 struct winbindd_request request
;
296 struct winbindd_response response
;
297 ZERO_STRUCT(request
);
298 ZERO_STRUCT(response
);
300 if (our_pid
!= getpid()) {
305 if ((need_priv
!= 0) && (is_privileged
== 0)) {
309 if (winbindd_fd
!= -1) {
317 if ((winbindd_fd
= winbind_named_pipe_sock(WINBINDD_SOCKET_DIR
)) == -1) {
323 /* version-check the socket */
325 request
.flags
= WBFLAG_RECURSE
;
326 if ((winbindd_request_response(WINBINDD_INTERFACE_VERSION
, &request
, &response
) != NSS_STATUS_SUCCESS
) || (response
.data
.interface_version
!= WINBIND_INTERFACE_VERSION
)) {
331 /* try and get priv pipe */
333 request
.flags
= WBFLAG_RECURSE
;
334 if (winbindd_request_response(WINBINDD_PRIV_PIPE_DIR
, &request
, &response
) == NSS_STATUS_SUCCESS
) {
336 if ((fd
= winbind_named_pipe_sock((char *)response
.extra_data
.data
)) != -1) {
343 if ((need_priv
!= 0) && (is_privileged
== 0)) {
347 SAFE_FREE(response
.extra_data
.data
);
352 #endif /* HAVE_UNIXSOCKET */
355 /* Write data to winbindd socket */
357 int write_sock(void *buffer
, int count
, int recursing
, int need_priv
)
359 int result
, nwritten
;
361 /* Open connection to winbind daemon */
365 if (winbind_open_pipe_sock(recursing
, need_priv
) == -1) {
369 /* Write data to socket */
373 while(nwritten
< count
) {
377 /* Catch pipe close on other end by checking if a read()
378 call would not block by calling select(). */
381 FD_SET(winbindd_fd
, &r_fds
);
384 if (select(winbindd_fd
+ 1, &r_fds
, NULL
, NULL
, &tv
) == -1) {
386 return -1; /* Select error */
389 /* Write should be OK if fd not available for reading */
391 if (!FD_ISSET(winbindd_fd
, &r_fds
)) {
395 result
= write(winbindd_fd
,
396 (char *)buffer
+ nwritten
,
399 if ((result
== -1) || (result
== 0)) {
411 /* Pipe has closed on remote end */
421 /* Read data from winbindd socket */
423 static int read_sock(void *buffer
, int count
)
425 int result
= 0, nread
= 0;
426 int total_time
= 0, selret
;
428 if (winbindd_fd
== -1) {
432 /* Read data from socket */
433 while(nread
< count
) {
437 /* Catch pipe close on other end by checking if a read()
438 call would not block by calling select(). */
441 FD_SET(winbindd_fd
, &r_fds
);
443 /* Wait for 5 seconds for a reply. May need to parameterise this... */
446 if ((selret
= select(winbindd_fd
+ 1, &r_fds
, NULL
, NULL
, &tv
)) == -1) {
448 return -1; /* Select error */
452 /* Not ready for read yet... */
453 if (total_time
>= 30) {
462 if (FD_ISSET(winbindd_fd
, &r_fds
)) {
466 result
= read(winbindd_fd
, (char *)buffer
+ nread
,
469 if ((result
== -1) || (result
== 0)) {
471 /* Read failed. I think the only useful thing we
472 can do here is just return -1 and fail since the
473 transaction has failed half way through. */
489 int read_reply(struct winbindd_response
*response
)
491 int result1
, result2
= 0;
497 /* Read fixed length response */
499 if ((result1
= read_sock(response
, sizeof(struct winbindd_response
)))
505 /* We actually send the pointer value of the extra_data field from
506 the server. This has no meaning in the client's address space
507 so we clear it out. */
509 response
->extra_data
.data
= NULL
;
511 /* Read variable length response */
513 if (response
->length
> sizeof(struct winbindd_response
)) {
514 int extra_data_len
= response
->length
-
515 sizeof(struct winbindd_response
);
517 /* Mallocate memory for extra data */
519 if (!(response
->extra_data
.data
= malloc(extra_data_len
))) {
523 if ((result2
= read_sock(response
->extra_data
.data
, extra_data_len
))
525 free_response(response
);
530 /* Return total amount of data read */
532 return result1
+ result2
;
535 BOOL
winbind_env_set( void )
539 if ((env
=getenv(WINBINDD_DONT_ENV
)) != NULL
) {
540 if(strcmp(env
, "1") == 0) {
548 * send simple types of requests
551 NSS_STATUS
winbindd_send_request(int req_type
, int need_priv
,
552 struct winbindd_request
*request
)
554 struct winbindd_request lrequest
;
556 /* Check for our tricky environment variable */
558 if (winbind_env_set()) {
559 return NSS_STATUS_NOTFOUND
;
563 ZERO_STRUCT(lrequest
);
567 /* Fill in request and send down pipe */
569 init_request(request
, req_type
);
571 if (write_sock(request
, sizeof(*request
),
572 request
->flags
& WBFLAG_RECURSE
, need_priv
) == -1) {
573 return NSS_STATUS_UNAVAIL
;
576 if ((request
->extra_len
!= 0) &&
577 (write_sock(request
->extra_data
.data
, request
->extra_len
,
578 request
->flags
& WBFLAG_RECURSE
, need_priv
) == -1)) {
579 return NSS_STATUS_UNAVAIL
;
582 return NSS_STATUS_SUCCESS
;
586 * Get results from winbindd request
589 NSS_STATUS
winbindd_get_response(struct winbindd_response
*response
)
591 struct winbindd_response lresponse
;
594 ZERO_STRUCT(lresponse
);
595 response
= &lresponse
;
598 init_response(response
);
601 if (read_reply(response
) == -1) {
602 return NSS_STATUS_UNAVAIL
;
605 /* Throw away extra data if client didn't request it */
606 if (response
== &lresponse
) {
607 free_response(response
);
610 /* Copy reply data from socket */
611 if (response
->result
!= WINBINDD_OK
) {
612 return NSS_STATUS_NOTFOUND
;
615 return NSS_STATUS_SUCCESS
;
618 /* Handle simple types of requests */
620 NSS_STATUS
winbindd_request_response(int req_type
,
621 struct winbindd_request
*request
,
622 struct winbindd_response
*response
)
624 NSS_STATUS status
= NSS_STATUS_UNAVAIL
;
627 while ((status
== NSS_STATUS_UNAVAIL
) && (count
< 10)) {
628 status
= winbindd_send_request(req_type
, 0, request
);
629 if (status
!= NSS_STATUS_SUCCESS
)
631 status
= winbindd_get_response(response
);
638 NSS_STATUS
winbindd_priv_request_response(int req_type
,
639 struct winbindd_request
*request
,
640 struct winbindd_response
*response
)
642 NSS_STATUS status
= NSS_STATUS_UNAVAIL
;
645 while ((status
== NSS_STATUS_UNAVAIL
) && (count
< 10)) {
646 status
= winbindd_send_request(req_type
, 1, request
);
647 if (status
!= NSS_STATUS_SUCCESS
)
649 status
= winbindd_get_response(response
);
656 /*************************************************************************
657 A couple of simple functions to disable winbindd lookups and re-
659 ************************************************************************/
661 /* Use putenv() instead of setenv() in these functions as not all
662 environments have the latter. */
664 BOOL
winbind_off( void )
666 static char *s
= CONST_DISCARD(char *, WINBINDD_DONT_ENV
"=1");
668 return putenv(s
) != -1;
671 BOOL
winbind_on( void )
673 static char *s
= CONST_DISCARD(char *, WINBINDD_DONT_ENV
"=0");
675 return putenv(s
) != -1;