Put unixsocket calls between #ifdef HAVE_UNIXSOCKET's - required for Stratus VOS
[tprouty/samba.git] / source / nsswitch / wb_common.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    winbind client common code
5
6    Copyright (C) Tim Potter 2000
7    Copyright (C) Andrew Tridgell 2000
8    Copyright (C) Andrew Bartlett 2002
9    
10    
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.
15    
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.
20    
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.   
25 */
26
27 #include "winbind_nss_config.h"
28 #include "winbindd_nss.h"
29
30 /* Global variables.  These are effectively the client state information */
31
32 int winbindd_fd = -1;           /* fd for winbindd socket */
33
34 /* Free a response structure */
35
36 void free_response(struct winbindd_response *response)
37 {
38         /* Free any allocated extra_data */
39
40         if (response)
41                 SAFE_FREE(response->extra_data);
42 }
43
44 /* Initialise a request structure */
45
46 void init_request(struct winbindd_request *request, int request_type)
47 {
48         static char *domain_env;
49         static BOOL initialised;
50
51         request->length = sizeof(struct winbindd_request);
52
53         request->cmd = (enum winbindd_cmd)request_type;
54         request->pid = getpid();
55         request->domain[0] = '\0';
56
57         if (!initialised) {
58                 initialised = True;
59                 domain_env = getenv(WINBINDD_DOMAIN_ENV);
60         }
61
62         if (domain_env) {
63                 strncpy(request->domain, domain_env,
64                         sizeof(request->domain) - 1);
65                 request->domain[sizeof(request->domain) - 1] = '\0';
66         }
67 }
68
69 /* Initialise a response structure */
70
71 void init_response(struct winbindd_response *response)
72 {
73         /* Initialise return value */
74
75         response->result = WINBINDD_ERROR;
76 }
77
78 /* Close established socket */
79
80 void close_sock(void)
81 {
82         if (winbindd_fd != -1) {
83                 close(winbindd_fd);
84                 winbindd_fd = -1;
85         }
86 }
87
88 /* Make sure socket handle isn't stdin, stdout or stderr */
89 #define RECURSION_LIMIT 3
90
91 static int make_nonstd_fd_internals(int fd, int limit /* Recursion limiter */) 
92 {
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;
98                 }
99                 /* Parinoia */
100                 if (new_fd < 3) {
101                         close(new_fd);
102                         return -1;
103                 }
104                 close(fd);
105                 return new_fd;
106 #else
107                 if (limit <= 0)
108                         return -1;
109                 
110                 new_fd = dup(fd);
111                 if (new_fd == -1) 
112                         return -1;
113
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
119         }
120         return fd;
121 }
122
123 static int make_safe_fd(int fd) 
124 {
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;
130         }
131         /* Socket should be closed on exec() */
132         
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 );
138         }
139         if (result < 0) {
140                 close(new_fd);
141                 return -1;
142         }
143 #endif
144         return new_fd;
145 }
146
147 /* Connect to winbindd socket */
148
149 int winbind_open_pipe_sock(void)
150 {
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;
157         
158         if (our_pid != getpid()) {
159                 close_sock();
160                 our_pid = getpid();
161         }
162         
163         if (winbindd_fd != -1) {
164                 return winbindd_fd;
165         }
166         
167         /* Check permissions on unix socket directory */
168         
169         if (lstat(WINBINDD_SOCKET_DIR, &st) == -1) {
170                 return -1;
171         }
172         
173         if (!S_ISDIR(st.st_mode) || 
174             (st.st_uid != 0 && st.st_uid != geteuid())) {
175                 return -1;
176         }
177         
178         /* Connect to socket */
179         
180         strncpy(path, WINBINDD_SOCKET_DIR, sizeof(path) - 1);
181         path[sizeof(path) - 1] = '\0';
182         
183         strncat(path, "/", sizeof(path) - 1);
184         path[sizeof(path) - 1] = '\0';
185         
186         strncat(path, WINBINDD_SOCKET_NAME, sizeof(path) - 1);
187         path[sizeof(path) - 1] = '\0';
188         
189         ZERO_STRUCT(sunaddr);
190         sunaddr.sun_family = AF_UNIX;
191         strncpy(sunaddr.sun_path, path, sizeof(sunaddr.sun_path) - 1);
192         
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. */
196
197         if (lstat(path, &st) == -1) {
198                 return -1;
199         }
200         
201         /* Check permissions on unix socket file */
202         
203         if (!S_ISSOCK(st.st_mode) || 
204             (st.st_uid != 0 && st.st_uid != geteuid())) {
205                 return -1;
206         }
207         
208         /* Connect to socket */
209         
210         if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
211                 return -1;
212         }
213
214         if ((winbindd_fd = make_safe_fd( fd)) == -1) {
215                 return winbindd_fd;
216         }
217         
218         if (connect(winbindd_fd, (struct sockaddr *)&sunaddr, 
219                     sizeof(sunaddr)) == -1) {
220                 close_sock();
221                 return -1;
222         }
223         
224         return winbindd_fd;
225 #else
226         return -1;
227 #endif /* HAVE_UNIXSOCKET */
228 }
229
230 /* Write data to winbindd socket */
231
232 int write_sock(void *buffer, int count)
233 {
234         int result, nwritten;
235         
236         /* Open connection to winbind daemon */
237         
238  restart:
239         
240         if (winbind_open_pipe_sock() == -1) {
241                 return -1;
242         }
243         
244         /* Write data to socket */
245         
246         nwritten = 0;
247         
248         while(nwritten < count) {
249                 struct timeval tv;
250                 fd_set r_fds;
251                 
252                 /* Catch pipe close on other end by checking if a read()
253                    call would not block by calling select(). */
254
255                 FD_ZERO(&r_fds);
256                 FD_SET(winbindd_fd, &r_fds);
257                 ZERO_STRUCT(tv);
258                 
259                 if (select(winbindd_fd + 1, &r_fds, NULL, NULL, &tv) == -1) {
260                         close_sock();
261                         return -1;                   /* Select error */
262                 }
263                 
264                 /* Write should be OK if fd not available for reading */
265                 
266                 if (!FD_ISSET(winbindd_fd, &r_fds)) {
267                         
268                         /* Do the write */
269                         
270                         result = write(winbindd_fd,
271                                        (char *)buffer + nwritten, 
272                                        count - nwritten);
273                         
274                         if ((result == -1) || (result == 0)) {
275                                 
276                                 /* Write failed */
277                                 
278                                 close_sock();
279                                 return -1;
280                         }
281                         
282                         nwritten += result;
283                         
284                 } else {
285                         
286                         /* Pipe has closed on remote end */
287                         
288                         close_sock();
289                         goto restart;
290                 }
291         }
292         
293         return nwritten;
294 }
295
296 /* Read data from winbindd socket */
297
298 static int read_sock(void *buffer, int count)
299 {
300         int result = 0, nread = 0;
301
302         /* Read data from socket */
303         
304         while(nread < count) {
305                 
306                 result = read(winbindd_fd, (char *)buffer + nread, 
307                               count - nread);
308                 
309                 if ((result == -1) || (result == 0)) {
310                         
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. */
314                         
315                         close_sock();
316                         return -1;
317                 }
318                 
319                 nread += result;
320         }
321         
322         return result;
323 }
324
325 /* Read reply */
326
327 int read_reply(struct winbindd_response *response)
328 {
329         int result1, result2 = 0;
330
331         if (!response) {
332                 return -1;
333         }
334         
335         /* Read fixed length response */
336         
337         if ((result1 = read_sock(response, sizeof(struct winbindd_response)))
338             == -1) {
339                 
340                 return -1;
341         }
342         
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. */
346
347         response->extra_data = NULL;
348
349         /* Read variable length response */
350         
351         if (response->length > sizeof(struct winbindd_response)) {
352                 int extra_data_len = response->length - 
353                         sizeof(struct winbindd_response);
354                 
355                 /* Mallocate memory for extra data */
356                 
357                 if (!(response->extra_data = malloc(extra_data_len))) {
358                         return -1;
359                 }
360                 
361                 if ((result2 = read_sock(response->extra_data, extra_data_len))
362                     == -1) {
363                         free_response(response);
364                         return -1;
365                 }
366         }
367         
368         /* Return total amount of data read */
369         
370         return result1 + result2;
371 }
372
373 /* 
374  * send simple types of requests 
375  */
376
377 NSS_STATUS winbindd_send_request(int req_type, struct winbindd_request *request)
378 {
379         struct winbindd_request lrequest;
380
381         /* Check for our tricky environment variable */
382
383         if (getenv(WINBINDD_DONT_ENV)) {
384                 return NSS_STATUS_NOTFOUND;
385         }
386
387         if (!request) {
388                 ZERO_STRUCT(lrequest);
389                 request = &lrequest;
390         }
391         
392         /* Fill in request and send down pipe */
393
394         init_request(request, req_type);
395         
396         if (write_sock(request, sizeof(*request)) == -1) {
397                 return NSS_STATUS_UNAVAIL;
398         }
399         
400         return NSS_STATUS_SUCCESS;
401 }
402
403 /*
404  * Get results from winbindd request
405  */
406
407 NSS_STATUS winbindd_get_response(struct winbindd_response *response)
408 {
409         struct winbindd_response lresponse;
410
411         if (!response) {
412                 ZERO_STRUCT(lresponse);
413                 response = &lresponse;
414         }
415
416         init_response(response);
417
418         /* Wait for reply */
419         if (read_reply(response) == -1) {
420                 return NSS_STATUS_UNAVAIL;
421         }
422
423         /* Throw away extra data if client didn't request it */
424         if (response == &lresponse) {
425                 free_response(response);
426         }
427
428         /* Copy reply data from socket */
429         if (response->result != WINBINDD_OK) {
430                 return NSS_STATUS_NOTFOUND;
431         }
432         
433         return NSS_STATUS_SUCCESS;
434 }
435
436 /* Handle simple types of requests */
437
438 NSS_STATUS winbindd_request(int req_type, 
439                             struct winbindd_request *request,
440                             struct winbindd_response *response)
441 {
442         NSS_STATUS status;
443
444         status = winbindd_send_request(req_type, request);
445         if (status != NSS_STATUS_SUCCESS) 
446                 return(status);
447         return winbindd_get_response(response);
448 }