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_nss_config.h"
28 #include "winbindd_nss.h"
30 /* Global variables. These are effectively the client state information */
32 int winbindd_fd = -1; /* fd for winbindd socket */
34 /* Free a response structure */
36 void free_response(struct winbindd_response *response)
38 /* Free any allocated extra_data */
41 SAFE_FREE(response->extra_data);
44 /* Initialise a request structure */
46 void init_request(struct winbindd_request *request, int request_type)
48 static char *domain_env;
49 static BOOL initialised;
51 request->length = sizeof(struct winbindd_request);
53 request->cmd = (enum winbindd_cmd)request_type;
54 request->pid = getpid();
55 request->domain[0] = '\0';
59 domain_env = getenv(WINBINDD_DOMAIN_ENV);
63 strncpy(request->domain, domain_env,
64 sizeof(request->domain) - 1);
65 request->domain[sizeof(request->domain) - 1] = '\0';
69 /* Initialise a response structure */
71 void init_response(struct winbindd_response *response)
73 /* Initialise return value */
75 response->result = WINBINDD_ERROR;
78 /* Close established socket */
82 if (winbindd_fd != -1) {
88 /* Make sure socket handle isn't stdin, stdout or stderr */
89 #define RECURSION_LIMIT 3
91 static int make_nonstd_fd_internals(int fd, int limit /* Recursion limiter */)
94 if (fd >= 0 && fd <= 2) {
96 if ((new_fd = fcntl(fd, F_DUPFD, 3)) == -1) {
114 /* use the program stack to hold our list of FDs to close */
115 new_fd = make_nonstd_fd_internals(new_fd, limit - 1);
123 static int make_safe_fd(int fd)
126 int new_fd = make_nonstd_fd_internals(fd, RECURSION_LIMIT);
131 /* Socket should be closed on exec() */
134 result = flags = fcntl(new_fd, F_GETFD, 0);
137 result = fcntl( new_fd, F_SETFD, flags );
147 /* Connect to winbindd socket */
149 int winbind_open_pipe_sock(void)
151 #ifdef HAVE_UNIXSOCKET
152 struct sockaddr_un sunaddr;
153 static pid_t our_pid;
158 if (our_pid != getpid()) {
163 if (winbindd_fd != -1) {
167 /* Check permissions on unix socket directory */
169 if (lstat(WINBINDD_SOCKET_DIR, &st) == -1) {
173 if (!S_ISDIR(st.st_mode) ||
174 (st.st_uid != 0 && st.st_uid != geteuid())) {
178 /* Connect to socket */
180 strncpy(path, WINBINDD_SOCKET_DIR, sizeof(path) - 1);
181 path[sizeof(path) - 1] = '\0';
183 strncat(path, "/", sizeof(path) - 1);
184 path[sizeof(path) - 1] = '\0';
186 strncat(path, WINBINDD_SOCKET_NAME, sizeof(path) - 1);
187 path[sizeof(path) - 1] = '\0';
189 ZERO_STRUCT(sunaddr);
190 sunaddr.sun_family = AF_UNIX;
191 strncpy(sunaddr.sun_path, path, sizeof(sunaddr.sun_path) - 1);
193 /* If socket file doesn't exist, don't bother trying to connect
194 with retry. This is an attempt to make the system usable when
195 the winbindd daemon is not running. */
197 if (lstat(path, &st) == -1) {
201 /* Check permissions on unix socket file */
203 if (!S_ISSOCK(st.st_mode) ||
204 (st.st_uid != 0 && st.st_uid != geteuid())) {
208 /* Connect to socket */
210 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
214 if ((winbindd_fd = make_safe_fd( fd)) == -1) {
218 if (connect(winbindd_fd, (struct sockaddr *)&sunaddr,
219 sizeof(sunaddr)) == -1) {
227 #endif /* HAVE_UNIXSOCKET */
230 /* Write data to winbindd socket */
232 int write_sock(void *buffer, int count)
234 int result, nwritten;
236 /* Open connection to winbind daemon */
240 if (winbind_open_pipe_sock() == -1) {
244 /* Write data to socket */
248 while(nwritten < count) {
252 /* Catch pipe close on other end by checking if a read()
253 call would not block by calling select(). */
256 FD_SET(winbindd_fd, &r_fds);
259 if (select(winbindd_fd + 1, &r_fds, NULL, NULL, &tv) == -1) {
261 return -1; /* Select error */
264 /* Write should be OK if fd not available for reading */
266 if (!FD_ISSET(winbindd_fd, &r_fds)) {
270 result = write(winbindd_fd,
271 (char *)buffer + nwritten,
274 if ((result == -1) || (result == 0)) {
286 /* Pipe has closed on remote end */
296 /* Read data from winbindd socket */
298 static int read_sock(void *buffer, int count)
300 int result = 0, nread = 0;
302 /* Read data from socket */
304 while(nread < count) {
306 result = read(winbindd_fd, (char *)buffer + nread,
309 if ((result == -1) || (result == 0)) {
311 /* Read failed. I think the only useful thing we
312 can do here is just return -1 and fail since the
313 transaction has failed half way through. */
327 int read_reply(struct winbindd_response *response)
329 int result1, result2 = 0;
335 /* Read fixed length response */
337 if ((result1 = read_sock(response, sizeof(struct winbindd_response)))
343 /* We actually send the pointer value of the extra_data field from
344 the server. This has no meaning in the client's address space
345 so we clear it out. */
347 response->extra_data = NULL;
349 /* Read variable length response */
351 if (response->length > sizeof(struct winbindd_response)) {
352 int extra_data_len = response->length -
353 sizeof(struct winbindd_response);
355 /* Mallocate memory for extra data */
357 if (!(response->extra_data = malloc(extra_data_len))) {
361 if ((result2 = read_sock(response->extra_data, extra_data_len))
363 free_response(response);
368 /* Return total amount of data read */
370 return result1 + result2;
374 * send simple types of requests
377 NSS_STATUS winbindd_send_request(int req_type, struct winbindd_request *request)
379 struct winbindd_request lrequest;
381 /* Check for our tricky environment variable */
383 if (getenv(WINBINDD_DONT_ENV)) {
384 return NSS_STATUS_NOTFOUND;
388 ZERO_STRUCT(lrequest);
392 /* Fill in request and send down pipe */
394 init_request(request, req_type);
396 if (write_sock(request, sizeof(*request)) == -1) {
397 return NSS_STATUS_UNAVAIL;
400 return NSS_STATUS_SUCCESS;
404 * Get results from winbindd request
407 NSS_STATUS winbindd_get_response(struct winbindd_response *response)
409 struct winbindd_response lresponse;
412 ZERO_STRUCT(lresponse);
413 response = &lresponse;
416 init_response(response);
419 if (read_reply(response) == -1) {
420 return NSS_STATUS_UNAVAIL;
423 /* Throw away extra data if client didn't request it */
424 if (response == &lresponse) {
425 free_response(response);
428 /* Copy reply data from socket */
429 if (response->result != WINBINDD_OK) {
430 return NSS_STATUS_NOTFOUND;
433 return NSS_STATUS_SUCCESS;
436 /* Handle simple types of requests */
438 NSS_STATUS winbindd_request(int req_type,
439 struct winbindd_request *request,
440 struct winbindd_response *response)
444 status = winbindd_send_request(req_type, request);
445 if (status != NSS_STATUS_SUCCESS)
447 return winbindd_get_response(response);