98a4b6758bc01a826f33cb2f8244b6348efa9543
[tprouty/samba.git] / source / nsswitch / wb_common.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 2.0
4
5    winbind client common code
6
7    Copyright (C) Tim Potter 2000
8    Copyright (C) Andrew Tridgell 2000
9    
10    This library is free software; you can redistribute it and/or
11    modify it under the terms of the GNU Library General Public
12    License as published by the Free Software Foundation; either
13    version 2 of the License, or (at your option) any later version.
14    
15    This library is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    Library General Public License for more details.
19    
20    You should have received a copy of the GNU Library General Public
21    License along with this library; if not, write to the
22    Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23    Boston, MA  02111-1307, USA.   
24 */
25
26 #include "winbind_nss_config.h"
27 #include "winbindd_nss.h"
28
29 /* Global variables.  These are effectively the client state information */
30
31 static int established_socket = -1;           /* fd for winbindd socket */
32
33 /* Initialise a request structure */
34
35 void init_request(struct winbindd_request *request, int request_type)
36 {
37         static char *domain_env;
38         static BOOL initialised;
39
40         request->cmd = (enum winbindd_cmd)request_type;
41         request->pid = getpid();
42         request->domain[0] = '\0';
43
44         if (!initialised) {
45                 initialised = True;
46                 domain_env = getenv(WINBINDD_DOMAIN_ENV);
47         }
48
49         if (domain_env) {
50                 strncpy(request->domain, domain_env,
51                         sizeof(request->domain) - 1);
52                 request->domain[sizeof(request->domain) - 1] = '\0';
53         }
54 }
55
56 /* Initialise a response structure */
57
58 void init_response(struct winbindd_response *response)
59 {
60         /* Initialise return value */
61
62         response->result = (enum winbindd_result)NSS_STATUS_UNAVAIL;
63 }
64
65 /* Close established socket */
66
67 void close_sock(void)
68 {
69         if (established_socket != -1) {
70                 close(established_socket);
71                 established_socket = -1;
72         }
73 }
74
75 /* Connect to winbindd socket */
76
77 static int open_pipe_sock(void)
78 {
79         struct sockaddr_un sunaddr;
80         static pid_t our_pid;
81         struct stat st;
82         pstring path;
83         
84         if (our_pid != getpid()) {
85                 if (established_socket != -1) {
86                         close(established_socket);
87                 }
88                 established_socket = -1;
89                 our_pid = getpid();
90         }
91         
92         if (established_socket != -1) {
93                 return established_socket;
94         }
95         
96         /* Check permissions on unix socket directory */
97         
98         if (lstat(WINBINDD_SOCKET_DIR, &st) == -1) {
99                 return -1;
100         }
101         
102         if (!S_ISDIR(st.st_mode) || (st.st_uid != 0)) {
103                 return -1;
104         }
105         
106         /* Connect to socket */
107         
108         strncpy(path, WINBINDD_SOCKET_DIR, sizeof(path) - 1);
109         path[sizeof(path) - 1] = '\0';
110         
111         strncat(path, "/", sizeof(path) - 1);
112         path[sizeof(path) - 1] = '\0';
113         
114         strncat(path, WINBINDD_SOCKET_NAME, sizeof(path) - 1);
115         path[sizeof(path) - 1] = '\0';
116         
117         ZERO_STRUCT(sunaddr);
118         sunaddr.sun_family = AF_UNIX;
119         strncpy(sunaddr.sun_path, path, sizeof(sunaddr.sun_path) - 1);
120         
121         /* If socket file doesn't exist, don't bother trying to connect
122            with retry.  This is an attempt to make the system usable when
123            the winbindd daemon is not running. */
124
125         if (lstat(path, &st) == -1) {
126                 return -1;
127         }
128         
129         /* Check permissions on unix socket file */
130         
131         if (!S_ISSOCK(st.st_mode) || (st.st_uid != 0)) {
132                 return -1;
133         }
134         
135         /* Connect to socket */
136         
137         if ((established_socket = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
138                 return -1;
139         }
140         
141         if (connect(established_socket, (struct sockaddr *)&sunaddr, 
142                     sizeof(sunaddr)) == -1) {
143                 close_sock();
144                 return -1;
145         }
146         
147         return established_socket;
148 }
149
150 /* Write data to winbindd socket with timeout */
151
152 int write_sock(void *buffer, int count)
153 {
154         int result, nwritten;
155         
156         /* Open connection to winbind daemon */
157         
158  restart:
159         
160         if (open_pipe_sock() == -1) {
161                 return -1;
162         }
163         
164         /* Write data to socket */
165         
166         nwritten = 0;
167         
168         while(nwritten < count) {
169                 struct timeval tv;
170                 fd_set r_fds;
171                 
172                 /* Catch pipe close on other end by checking if a read()
173                    call would not block by calling select(). */
174
175                 FD_ZERO(&r_fds);
176                 FD_SET(established_socket, &r_fds);
177                 ZERO_STRUCT(tv);
178                 
179                 if (select(established_socket + 1, &r_fds, 
180                                      NULL, NULL, &tv) == -1) {
181                         close_sock();
182                         return -1;                   /* Select error */
183                 }
184                 
185                 /* Write should be OK if fd not available for reading */
186                 
187                 if (!FD_ISSET(established_socket, &r_fds)) {
188                         
189                         /* Do the write */
190                         
191                         result = write(established_socket,
192                                        (char *)buffer + nwritten, 
193                                        count - nwritten);
194                         
195                         if ((result == -1) || (result == 0)) {
196                                 
197                                 /* Write failed */
198                                 
199                                 close_sock();
200                                 return -1;
201                         }
202                         
203                         nwritten += result;
204                         
205                 } else {
206                         
207                         /* Pipe has closed on remote end */
208                         
209                         close_sock();
210                         goto restart;
211                 }
212         }
213         
214         return nwritten;
215 }
216
217 /* Read data from winbindd socket with timeout */
218
219 static int read_sock(void *buffer, int count)
220 {
221         int result = 0, nread = 0;
222
223         /* Read data from socket */
224         
225         while(nread < count) {
226                 
227                 result = read(established_socket, (char *)buffer + nread, 
228                               count - nread);
229                 
230                 if ((result == -1) || (result == 0)) {
231                         
232                         /* Read failed.  I think the only useful thing we
233                            can do here is just return -1 and fail since the
234                            transaction has failed half way through. */
235                         
236                         close_sock();
237                         return -1;
238                 }
239                 
240                 nread += result;
241         }
242         
243         return result;
244 }
245
246 /* Read reply */
247
248 int read_reply(struct winbindd_response *response)
249 {
250         int result1, result2 = 0;
251
252         if (!response) {
253                 return -1;
254         }
255         
256         /* Read fixed length response */
257         
258         if ((result1 = read_sock(response, sizeof(struct winbindd_response)))
259             == -1) {
260                 
261                 return -1;
262         }
263         
264         /* We actually send the pointer value of the extra_data field from
265            the server.  This has no meaning in the client's address space
266            so we clear it out. */
267
268         response->extra_data = NULL;
269
270         /* Read variable length response */
271         
272         if (response->length > sizeof(struct winbindd_response)) {
273                 int extra_data_len = response->length - 
274                         sizeof(struct winbindd_response);
275                 
276                 /* Mallocate memory for extra data */
277                 
278                 if (!(response->extra_data = malloc(extra_data_len))) {
279                         return -1;
280                 }
281                 
282                 if ((result2 = read_sock(response->extra_data, extra_data_len))
283                     == -1) {
284                         return -1;
285                 }
286         }
287         
288         /* Return total amount of data read */
289         
290         return result1 + result2;
291 }
292
293 /* Free a response structure */
294
295 void free_response(struct winbindd_response *response)
296 {
297         /* Free any allocated extra_data */
298
299         if (response && response->extra_data) {
300                 free(response->extra_data);
301                 response->extra_data = NULL;
302         }
303 }
304
305 /* Handle simple types of requests */
306
307 enum nss_status winbindd_request(int req_type, 
308                                  struct winbindd_request *request,
309                                  struct winbindd_response *response)
310 {
311         struct winbindd_request lrequest;
312         struct winbindd_response lresponse;
313
314         /* Check for our tricky environment variable */
315
316         if (getenv(WINBINDD_DONT_ENV)) {
317                 return NSS_STATUS_NOTFOUND;
318         }
319
320         if (!response) {
321                 ZERO_STRUCT(lresponse);
322                 response = &lresponse;
323         }
324
325         if (!request) {
326                 ZERO_STRUCT(lrequest);
327                 request = &lrequest;
328         }
329         
330         /* Fill in request and send down pipe */
331
332         init_request(request, req_type);
333         init_response(response);
334         
335         if (write_sock(request, sizeof(*request)) == -1) {
336                 return NSS_STATUS_UNAVAIL;
337         }
338         
339         /* Wait for reply */
340         if (read_reply(response) == -1) {
341                 return NSS_STATUS_UNAVAIL;
342         }
343
344         /* Throw away extra data if client didn't request it */
345         if (response == &lresponse) {
346                 free_response(response);
347         }
348
349         /* Copy reply data from socket */
350         if (response->result != WINBINDD_OK) {
351                 return NSS_STATUS_NOTFOUND;
352         }
353         
354         return NSS_STATUS_SUCCESS;
355 }