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