Preliminary merge of winbind into HEAD. Note that this compiles and links
[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 = 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 = WINBINDD_ERROR;
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                 established_socket = -1;
145                 return -1;
146         }
147         
148         return established_socket;
149 }
150
151 /* Write data to winbindd socket with timeout */
152
153 int write_sock(void *buffer, int count)
154 {
155         int result, nwritten;
156         
157         /* Open connection to winbind daemon */
158         
159  restart:
160         
161         if (open_pipe_sock() == -1) {
162                 return -1;
163         }
164         
165         /* Write data to socket */
166         
167         nwritten = 0;
168         
169         while(nwritten < count) {
170                 struct timeval tv;
171                 fd_set r_fds;
172                 int selret;
173                 
174                 /* Catch pipe close on other end by checking if a read()
175                    call would not block by calling select(). */
176
177                 FD_ZERO(&r_fds);
178                 FD_SET(established_socket, &r_fds);
179                 ZERO_STRUCT(tv);
180                 
181                 if ((selret = select(established_socket + 1, &r_fds, 
182                                      NULL, NULL, &tv)) == -1) {
183                         close_sock();
184                         return -1;                   /* Select error */
185                 }
186                 
187                 /* Write should be OK if fd not available for reading */
188                 
189                 if (!FD_ISSET(established_socket, &r_fds)) {
190                         
191                         /* Do the write */
192                         
193                         result = write(established_socket,
194                                        (char *)buffer + nwritten, 
195                                        count - nwritten);
196                         
197                         if ((result == -1) || (result == 0)) {
198                                 
199                                 /* Write failed */
200                                 
201                                 close_sock();
202                                 return -1;
203                         }
204                         
205                         nwritten += result;
206                         
207                 } else {
208                         
209                         /* Pipe has closed on remote end */
210                         
211                         close_sock();
212                         goto restart;
213                 }
214         }
215         
216         return nwritten;
217 }
218
219 /* Read data from winbindd socket with timeout */
220
221 static int read_sock(void *buffer, int count)
222 {
223         int result = 0, nread = 0;
224
225         /* Read data from socket */
226         
227         while(nread < count) {
228                 
229                 result = read(established_socket, (char *)buffer + nread, 
230                               count - nread);
231                 
232                 if ((result == -1) || (result == 0)) {
233                         
234                         /* Read failed.  I think the only useful thing we
235                            can do here is just return -1 and fail since the
236                            transaction has failed half way through. */
237                         
238                         close_sock();
239                         return -1;
240                 }
241                 
242                 nread += result;
243         }
244         
245         return result;
246 }
247
248 /* Read reply */
249
250 int read_reply(struct winbindd_response *response)
251 {
252         int result1, result2 = 0;
253
254         if (!response) {
255                 return -1;
256         }
257         
258         /* Read fixed length response */
259         
260         if ((result1 = read_sock(response, sizeof(struct winbindd_response)))
261             == -1) {
262                 
263                 return -1;
264         }
265         
266         /* We actually send the pointer value of the extra_data field from
267            the server.  This has no meaning in the client's address space
268            so we clear it out. */
269
270         response->extra_data = NULL;
271
272         /* Read variable length response */
273         
274         if (response->length > sizeof(struct winbindd_response)) {
275                 int extra_data_len = response->length - 
276                         sizeof(struct winbindd_response);
277                 
278                 /* Mallocate memory for extra data */
279                 
280                 if (!(response->extra_data = malloc(extra_data_len))) {
281                         return -1;
282                 }
283                 
284                 if ((result2 = read_sock(response->extra_data, extra_data_len))
285                     == -1) {
286                         return -1;
287                 }
288         }
289         
290         /* Return total amount of data read */
291         
292         return result1 + result2;
293 }
294
295 /* Free a response structure */
296
297 void free_response(struct winbindd_response *response)
298 {
299         /* Free any allocated extra_data */
300
301         if (response && response->extra_data) {
302                 free(response->extra_data);
303                 response->extra_data = NULL;
304         }
305 }
306
307 /* Handle simple types of requests */
308
309 NSS_STATUS winbindd_request(int req_type, 
310                                  struct winbindd_request *request,
311                                  struct winbindd_response *response)
312 {
313         struct winbindd_request lrequest;
314         struct winbindd_response lresponse;
315
316         /* Check for our tricky environment variable */
317
318         if (getenv(WINBINDD_DONT_ENV)) {
319                 return NSS_STATUS_NOTFOUND;
320         }
321
322         if (!response) {
323                 ZERO_STRUCT(lresponse);
324                 response = &lresponse;
325         }
326
327         if (!request) {
328                 ZERO_STRUCT(lrequest);
329                 request = &lrequest;
330         }
331         
332         /* Fill in request and send down pipe */
333
334         init_request(request, req_type);
335         init_response(response);
336         
337         if (write_sock(request, sizeof(*request)) == -1) {
338                 return NSS_STATUS_UNAVAIL;
339         }
340         
341         /* Wait for reply */
342         if (read_reply(response) == -1) {
343                 return NSS_STATUS_UNAVAIL;
344         }
345
346         /* Throw away extra data if client didn't request it */
347         if (response == &lresponse) {
348                 free_response(response);
349         }
350
351         /* Copy reply data from socket */
352         if (response->result != WINBINDD_OK) {
353                 return NSS_STATUS_NOTFOUND;
354         }
355         
356         return NSS_STATUS_SUCCESS;
357 }