Since AB has been changing the winbind interface it's time to add the "mock
[kai/samba.git] / source3 / 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 int winbindd_fd = -1;           /* fd for winbindd socket */
32 static char *excluded_domain;
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 /*
45   smbd needs to be able to exclude lookups for its own domain
46 */
47 void winbind_exclude_domain(const char *domain)
48 {
49         SAFE_FREE(excluded_domain);
50         excluded_domain = strdup(domain);
51 }
52
53
54 /* Initialise a request structure */
55
56 void init_request(struct winbindd_request *request, int request_type)
57 {
58         static char *domain_env;
59         static BOOL initialised;
60
61         request->length = sizeof(struct winbindd_request);
62
63         request->cmd = (enum winbindd_cmd)request_type;
64         request->pid = getpid();
65         request->domain[0] = '\0';
66
67         if (!initialised) {
68                 initialised = True;
69                 domain_env = getenv(WINBINDD_DOMAIN_ENV);
70         }
71
72         if (domain_env) {
73                 strncpy(request->domain, domain_env,
74                         sizeof(request->domain) - 1);
75                 request->domain[sizeof(request->domain) - 1] = '\0';
76         }
77 }
78
79 /* Initialise a response structure */
80
81 void init_response(struct winbindd_response *response)
82 {
83         /* Initialise return value */
84
85         response->result = WINBINDD_ERROR;
86 }
87
88 /* Close established socket */
89
90 void close_sock(void)
91 {
92         if (winbindd_fd != -1) {
93                 close(winbindd_fd);
94                 winbindd_fd = -1;
95         }
96 }
97
98 /* Connect to winbindd socket */
99
100 int winbind_open_pipe_sock(void)
101 {
102         struct sockaddr_un sunaddr;
103         static pid_t our_pid;
104         struct stat st;
105         pstring path;
106         
107         if (our_pid != getpid()) {
108                 close_sock();
109                 our_pid = getpid();
110         }
111         
112         if (winbindd_fd != -1) {
113                 return winbindd_fd;
114         }
115         
116         /* Check permissions on unix socket directory */
117         
118         if (lstat(WINBINDD_SOCKET_DIR, &st) == -1) {
119                 return -1;
120         }
121         
122         if (!S_ISDIR(st.st_mode) || 
123             (st.st_uid != 0 && st.st_uid != geteuid())) {
124                 return -1;
125         }
126         
127         /* Connect to socket */
128         
129         strncpy(path, WINBINDD_SOCKET_DIR, sizeof(path) - 1);
130         path[sizeof(path) - 1] = '\0';
131         
132         strncat(path, "/", sizeof(path) - 1);
133         path[sizeof(path) - 1] = '\0';
134         
135         strncat(path, WINBINDD_SOCKET_NAME, sizeof(path) - 1);
136         path[sizeof(path) - 1] = '\0';
137         
138         ZERO_STRUCT(sunaddr);
139         sunaddr.sun_family = AF_UNIX;
140         strncpy(sunaddr.sun_path, path, sizeof(sunaddr.sun_path) - 1);
141         
142         /* If socket file doesn't exist, don't bother trying to connect
143            with retry.  This is an attempt to make the system usable when
144            the winbindd daemon is not running. */
145
146         if (lstat(path, &st) == -1) {
147                 return -1;
148         }
149         
150         /* Check permissions on unix socket file */
151         
152         if (!S_ISSOCK(st.st_mode) || 
153             (st.st_uid != 0 && st.st_uid != geteuid())) {
154                 return -1;
155         }
156         
157         /* Connect to socket */
158         
159         if ((winbindd_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
160                 return -1;
161         }
162         
163         if (connect(winbindd_fd, (struct sockaddr *)&sunaddr, 
164                     sizeof(sunaddr)) == -1) {
165                 close_sock();
166                 return -1;
167         }
168         
169         return winbindd_fd;
170 }
171
172 /* Write data to winbindd socket with timeout */
173
174 int write_sock(void *buffer, int count)
175 {
176         int result, nwritten;
177         
178         /* Open connection to winbind daemon */
179         
180  restart:
181         
182         if (winbind_open_pipe_sock() == -1) {
183                 return -1;
184         }
185         
186         /* Write data to socket */
187         
188         nwritten = 0;
189         
190         while(nwritten < count) {
191                 struct timeval tv;
192                 fd_set r_fds;
193                 
194                 /* Catch pipe close on other end by checking if a read()
195                    call would not block by calling select(). */
196
197                 FD_ZERO(&r_fds);
198                 FD_SET(winbindd_fd, &r_fds);
199                 ZERO_STRUCT(tv);
200                 
201                 if (select(winbindd_fd + 1, &r_fds, NULL, NULL, &tv) == -1) {
202                         close_sock();
203                         return -1;                   /* Select error */
204                 }
205                 
206                 /* Write should be OK if fd not available for reading */
207                 
208                 if (!FD_ISSET(winbindd_fd, &r_fds)) {
209                         
210                         /* Do the write */
211                         
212                         result = write(winbindd_fd,
213                                        (char *)buffer + nwritten, 
214                                        count - nwritten);
215                         
216                         if ((result == -1) || (result == 0)) {
217                                 
218                                 /* Write failed */
219                                 
220                                 close_sock();
221                                 return -1;
222                         }
223                         
224                         nwritten += result;
225                         
226                 } else {
227                         
228                         /* Pipe has closed on remote end */
229                         
230                         close_sock();
231                         goto restart;
232                 }
233         }
234         
235         return nwritten;
236 }
237
238 /* Read data from winbindd socket with timeout */
239
240 static int read_sock(void *buffer, int count)
241 {
242         int result = 0, nread = 0;
243
244         /* Read data from socket */
245         
246         while(nread < count) {
247                 
248                 result = read(winbindd_fd, (char *)buffer + nread, 
249                               count - nread);
250                 
251                 if ((result == -1) || (result == 0)) {
252                         
253                         /* Read failed.  I think the only useful thing we
254                            can do here is just return -1 and fail since the
255                            transaction has failed half way through. */
256                         
257                         close_sock();
258                         return -1;
259                 }
260                 
261                 nread += result;
262         }
263         
264         return result;
265 }
266
267 /* Read reply */
268
269 int read_reply(struct winbindd_response *response)
270 {
271         int result1, result2 = 0;
272
273         if (!response) {
274                 return -1;
275         }
276         
277         /* Read fixed length response */
278         
279         if ((result1 = read_sock(response, sizeof(struct winbindd_response)))
280             == -1) {
281                 
282                 return -1;
283         }
284         
285         /* We actually send the pointer value of the extra_data field from
286            the server.  This has no meaning in the client's address space
287            so we clear it out. */
288
289         response->extra_data = NULL;
290
291         /* Read variable length response */
292         
293         if (response->length > sizeof(struct winbindd_response)) {
294                 int extra_data_len = response->length - 
295                         sizeof(struct winbindd_response);
296                 
297                 /* Mallocate memory for extra data */
298                 
299                 if (!(response->extra_data = malloc(extra_data_len))) {
300                         return -1;
301                 }
302                 
303                 if ((result2 = read_sock(response->extra_data, extra_data_len))
304                     == -1) {
305                         free_response(response);
306                         return -1;
307                 }
308         }
309         
310         /* Return total amount of data read */
311         
312         return result1 + result2;
313 }
314
315 /* 
316  * send simple types of requests 
317  */
318
319 NSS_STATUS winbindd_send_request(int req_type, struct winbindd_request *request)
320 {
321         struct winbindd_request lrequest;
322
323         /* Check for our tricky environment variable */
324
325         if (getenv(WINBINDD_DONT_ENV)) {
326                 return NSS_STATUS_NOTFOUND;
327         }
328
329         /* smbd may have excluded this domain */
330         if (excluded_domain && 
331             strcasecmp(excluded_domain, request->domain) == 0) {
332                 return NSS_STATUS_NOTFOUND;
333         }
334
335         if (!request) {
336                 ZERO_STRUCT(lrequest);
337                 request = &lrequest;
338         }
339         
340         /* Fill in request and send down pipe */
341
342         init_request(request, req_type);
343         
344         if (write_sock(request, sizeof(*request)) == -1) {
345                 return NSS_STATUS_UNAVAIL;
346         }
347         
348         return NSS_STATUS_SUCCESS;
349 }
350
351 /*
352  * Get results from winbindd request
353  */
354
355 NSS_STATUS winbindd_get_response(struct winbindd_response *response)
356 {
357         struct winbindd_response lresponse;
358
359         if (!response) {
360                 ZERO_STRUCT(lresponse);
361                 response = &lresponse;
362         }
363
364         init_response(response);
365
366         /* Wait for reply */
367         if (read_reply(response) == -1) {
368                 return NSS_STATUS_UNAVAIL;
369         }
370
371         /* Throw away extra data if client didn't request it */
372         if (response == &lresponse) {
373                 free_response(response);
374         }
375
376         /* Copy reply data from socket */
377         if (response->result != WINBINDD_OK) {
378                 return NSS_STATUS_NOTFOUND;
379         }
380         
381         return NSS_STATUS_SUCCESS;
382 }
383
384 /* Handle simple types of requests */
385
386 NSS_STATUS winbindd_request(int req_type, 
387                                  struct winbindd_request *request,
388                                  struct winbindd_response *response)
389 {
390         NSS_STATUS status;
391
392         status = winbindd_send_request(req_type, request);
393         if (status != NSS_STATUS_SUCCESS) 
394                 return(status);
395         return winbindd_get_response(response);
396 }