preparing for release of 3.0-alpha11
[Samba/ekacnet.git] / source / nsswitch / wb_common.c
blob0cfefa6f860381f6704fb069401769d323f76f80
1 /*
2 Unix SMB/Netbios implementation.
3 Version 2.0
5 winbind client common code
7 Copyright (C) Tim Potter 2000
8 Copyright (C) Andrew Tridgell 2000
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.
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.
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.
26 #include "winbind_nss_config.h"
27 #include "winbindd_nss.h"
29 /* Global variables. These are effectively the client state information */
31 static int established_socket = -1; /* fd for winbindd socket */
32 static char *excluded_domain;
35 smbd needs to be able to exclude lookups for its own domain
37 void winbind_exclude_domain(const char *domain)
39 SAFE_FREE(excluded_domain);
40 excluded_domain = strdup(domain);
44 /* Initialise a request structure */
46 void init_request(struct winbindd_request *request, int request_type)
48 static char *domain_env;
49 static BOOL initialised;
51 request->cmd = (enum winbindd_cmd)request_type;
52 request->pid = getpid();
53 request->domain[0] = '\0';
55 if (!initialised) {
56 initialised = True;
57 domain_env = getenv(WINBINDD_DOMAIN_ENV);
60 if (domain_env) {
61 strncpy(request->domain, domain_env,
62 sizeof(request->domain) - 1);
63 request->domain[sizeof(request->domain) - 1] = '\0';
67 /* Initialise a response structure */
69 void init_response(struct winbindd_response *response)
71 /* Initialise return value */
73 response->result = WINBINDD_ERROR;
76 /* Close established socket */
78 void close_sock(void)
80 if (established_socket != -1) {
81 close(established_socket);
82 established_socket = -1;
86 /* Connect to winbindd socket */
88 static int open_pipe_sock(void)
90 struct sockaddr_un sunaddr;
91 static pid_t our_pid;
92 struct stat st;
93 pstring path;
95 if (our_pid != getpid()) {
96 if (established_socket != -1) {
97 close(established_socket);
99 established_socket = -1;
100 our_pid = getpid();
103 if (established_socket != -1) {
104 return established_socket;
107 /* Check permissions on unix socket directory */
109 if (lstat(WINBINDD_SOCKET_DIR, &st) == -1) {
110 return -1;
113 if (!S_ISDIR(st.st_mode) ||
114 (st.st_uid != 0 && st.st_uid != geteuid())) {
115 return -1;
118 /* Connect to socket */
120 strncpy(path, WINBINDD_SOCKET_DIR, sizeof(path) - 1);
121 path[sizeof(path) - 1] = '\0';
123 strncat(path, "/", sizeof(path) - 1);
124 path[sizeof(path) - 1] = '\0';
126 strncat(path, WINBINDD_SOCKET_NAME, sizeof(path) - 1);
127 path[sizeof(path) - 1] = '\0';
129 ZERO_STRUCT(sunaddr);
130 sunaddr.sun_family = AF_UNIX;
131 strncpy(sunaddr.sun_path, path, sizeof(sunaddr.sun_path) - 1);
133 /* If socket file doesn't exist, don't bother trying to connect
134 with retry. This is an attempt to make the system usable when
135 the winbindd daemon is not running. */
137 if (lstat(path, &st) == -1) {
138 return -1;
141 /* Check permissions on unix socket file */
143 if (!S_ISSOCK(st.st_mode) ||
144 (st.st_uid != 0 && st.st_uid != geteuid())) {
145 return -1;
148 /* Connect to socket */
150 if ((established_socket = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
151 return -1;
154 if (connect(established_socket, (struct sockaddr *)&sunaddr,
155 sizeof(sunaddr)) == -1) {
156 close_sock();
157 established_socket = -1;
158 return -1;
161 return established_socket;
164 /* Write data to winbindd socket with timeout */
166 int write_sock(void *buffer, int count)
168 int result, nwritten;
170 /* Open connection to winbind daemon */
172 restart:
174 if (open_pipe_sock() == -1) {
175 return -1;
178 /* Write data to socket */
180 nwritten = 0;
182 while(nwritten < count) {
183 struct timeval tv;
184 fd_set r_fds;
185 int selret;
187 /* Catch pipe close on other end by checking if a read()
188 call would not block by calling select(). */
190 FD_ZERO(&r_fds);
191 FD_SET(established_socket, &r_fds);
192 ZERO_STRUCT(tv);
194 if ((selret = select(established_socket + 1, &r_fds,
195 NULL, NULL, &tv)) == -1) {
196 close_sock();
197 return -1; /* Select error */
200 /* Write should be OK if fd not available for reading */
202 if (!FD_ISSET(established_socket, &r_fds)) {
204 /* Do the write */
206 result = write(established_socket,
207 (char *)buffer + nwritten,
208 count - nwritten);
210 if ((result == -1) || (result == 0)) {
212 /* Write failed */
214 close_sock();
215 return -1;
218 nwritten += result;
220 } else {
222 /* Pipe has closed on remote end */
224 close_sock();
225 goto restart;
229 return nwritten;
232 /* Read data from winbindd socket with timeout */
234 static int read_sock(void *buffer, int count)
236 int result = 0, nread = 0;
238 /* Read data from socket */
240 while(nread < count) {
242 result = read(established_socket, (char *)buffer + nread,
243 count - nread);
245 if ((result == -1) || (result == 0)) {
247 /* Read failed. I think the only useful thing we
248 can do here is just return -1 and fail since the
249 transaction has failed half way through. */
251 close_sock();
252 return -1;
255 nread += result;
258 return result;
261 /* Read reply */
263 int read_reply(struct winbindd_response *response)
265 int result1, result2 = 0;
267 if (!response) {
268 return -1;
271 /* Read fixed length response */
273 if ((result1 = read_sock(response, sizeof(struct winbindd_response)))
274 == -1) {
276 return -1;
279 /* We actually send the pointer value of the extra_data field from
280 the server. This has no meaning in the client's address space
281 so we clear it out. */
283 response->extra_data = NULL;
285 /* Read variable length response */
287 if (response->length > sizeof(struct winbindd_response)) {
288 int extra_data_len = response->length -
289 sizeof(struct winbindd_response);
291 /* Mallocate memory for extra data */
293 if (!(response->extra_data = malloc(extra_data_len))) {
294 return -1;
297 if ((result2 = read_sock(response->extra_data, extra_data_len))
298 == -1) {
299 return -1;
303 /* Return total amount of data read */
305 return result1 + result2;
308 /* Free a response structure */
310 void free_response(struct winbindd_response *response)
312 /* Free any allocated extra_data */
314 if (response)
315 SAFE_FREE(response->extra_data);
318 /* Handle simple types of requests */
320 NSS_STATUS winbindd_request(int req_type,
321 struct winbindd_request *request,
322 struct winbindd_response *response)
324 struct winbindd_request lrequest;
325 struct winbindd_response lresponse;
327 /* Check for our tricky environment variable */
329 if (getenv(WINBINDD_DONT_ENV)) {
330 return NSS_STATUS_NOTFOUND;
333 /* smbd may have excluded this domain */
334 if (excluded_domain &&
335 strcasecmp(excluded_domain, request->domain) == 0) {
336 return NSS_STATUS_NOTFOUND;
339 if (!response) {
340 ZERO_STRUCT(lresponse);
341 response = &lresponse;
344 if (!request) {
345 ZERO_STRUCT(lrequest);
346 request = &lrequest;
349 /* Fill in request and send down pipe */
351 init_request(request, req_type);
352 init_response(response);
354 if (write_sock(request, sizeof(*request)) == -1) {
355 return NSS_STATUS_UNAVAIL;
358 /* Wait for reply */
359 if (read_reply(response) == -1) {
360 return NSS_STATUS_UNAVAIL;
363 /* Throw away extra data if client didn't request it */
364 if (response == &lresponse) {
365 free_response(response);
368 /* Copy reply data from socket */
369 if (response->result != WINBINDD_OK) {
370 return NSS_STATUS_NOTFOUND;
373 return NSS_STATUS_SUCCESS;