sync'ing up for 3.0alpha20 release
[Samba.git] / source / nsswitch / wb_common.c
blob51792f63fe2ef768b543032529bd75f81d1d4a74
1 /*
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 */
40 if (response)
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';
57 if (!initialised) {
58 initialised = True;
59 domain_env = getenv(WINBINDD_DOMAIN_ENV);
62 if (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 */
80 void close_sock(void)
82 if (winbindd_fd != -1) {
83 close(winbindd_fd);
84 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 */)
93 int new_fd;
94 if (fd >= 0 && fd <= 2) {
95 #ifdef F_DUPFD
96 if ((new_fd = fcntl(fd, F_DUPFD, 3)) == -1) {
97 return -1;
99 /* Parinoia */
100 if (new_fd < 3) {
101 close(new_fd);
102 return -1;
104 close(fd);
105 return new_fd;
106 #else
107 if (limit <= 0)
108 return -1;
110 new_fd = dup(fd);
111 if (new_fd == -1)
112 return -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);
116 close(fd);
117 return new_fd;
118 #endif
120 return fd;
123 static int make_safe_fd(int fd)
125 int result, flags;
126 int new_fd = make_nonstd_fd_internals(fd, RECURSION_LIMIT);
127 if (new_fd == -1) {
128 close(fd);
129 return -1;
131 /* Socket should be closed on exec() */
133 #ifdef FD_CLOEXEC
134 result = flags = fcntl(new_fd, F_GETFD, 0);
135 if (flags >= 0) {
136 flags |= FD_CLOEXEC;
137 result = fcntl( new_fd, F_SETFD, flags );
139 if (result < 0) {
140 close(new_fd);
141 return -1;
143 #endif
144 return new_fd;
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;
154 struct stat st;
155 pstring path;
156 int fd;
158 if (our_pid != getpid()) {
159 close_sock();
160 our_pid = getpid();
163 if (winbindd_fd != -1) {
164 return winbindd_fd;
167 /* Check permissions on unix socket directory */
169 if (lstat(WINBINDD_SOCKET_DIR, &st) == -1) {
170 return -1;
173 if (!S_ISDIR(st.st_mode) ||
174 (st.st_uid != 0 && st.st_uid != geteuid())) {
175 return -1;
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) {
198 return -1;
201 /* Check permissions on unix socket file */
203 if (!S_ISSOCK(st.st_mode) ||
204 (st.st_uid != 0 && st.st_uid != geteuid())) {
205 return -1;
208 /* Connect to socket */
210 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
211 return -1;
214 if ((winbindd_fd = make_safe_fd( fd)) == -1) {
215 return winbindd_fd;
218 if (connect(winbindd_fd, (struct sockaddr *)&sunaddr,
219 sizeof(sunaddr)) == -1) {
220 close_sock();
221 return -1;
224 return winbindd_fd;
225 #else
226 return -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 */
238 restart:
240 if (winbind_open_pipe_sock() == -1) {
241 return -1;
244 /* Write data to socket */
246 nwritten = 0;
248 while(nwritten < count) {
249 struct timeval tv;
250 fd_set r_fds;
252 /* Catch pipe close on other end by checking if a read()
253 call would not block by calling select(). */
255 FD_ZERO(&r_fds);
256 FD_SET(winbindd_fd, &r_fds);
257 ZERO_STRUCT(tv);
259 if (select(winbindd_fd + 1, &r_fds, NULL, NULL, &tv) == -1) {
260 close_sock();
261 return -1; /* Select error */
264 /* Write should be OK if fd not available for reading */
266 if (!FD_ISSET(winbindd_fd, &r_fds)) {
268 /* Do the write */
270 result = write(winbindd_fd,
271 (char *)buffer + nwritten,
272 count - nwritten);
274 if ((result == -1) || (result == 0)) {
276 /* Write failed */
278 close_sock();
279 return -1;
282 nwritten += result;
284 } else {
286 /* Pipe has closed on remote end */
288 close_sock();
289 goto restart;
293 return nwritten;
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,
307 count - 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. */
315 close_sock();
316 return -1;
319 nread += result;
322 return result;
325 /* Read reply */
327 int read_reply(struct winbindd_response *response)
329 int result1, result2 = 0;
331 if (!response) {
332 return -1;
335 /* Read fixed length response */
337 if ((result1 = read_sock(response, sizeof(struct winbindd_response)))
338 == -1) {
340 return -1;
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))) {
358 return -1;
361 if ((result2 = read_sock(response->extra_data, extra_data_len))
362 == -1) {
363 free_response(response);
364 return -1;
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;
387 if (!request) {
388 ZERO_STRUCT(lrequest);
389 request = &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;
411 if (!response) {
412 ZERO_STRUCT(lresponse);
413 response = &lresponse;
416 init_response(response);
418 /* Wait for reply */
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)
442 NSS_STATUS status;
444 status = winbindd_send_request(req_type, request);
445 if (status != NSS_STATUS_SUCCESS)
446 return(status);
447 return winbindd_get_response(response);