2 Unix SMB/Netbios implementation.
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 */
34 * Utility and helper functions
37 void init_request(struct winbindd_request *req,int rq_type)
39 static char *domain_env;
40 static BOOL initialised;
44 req->domain[0] = '\0';
48 domain_env = getenv(WINBINDD_DOMAIN_ENV);
52 strncpy(req->domain, domain_env,
53 sizeof(req->domain) - 1);
54 req->domain[sizeof(req->domain) - 1] = '\0';
58 /* Close established socket */
62 if (established_socket != -1) {
63 close(established_socket);
64 established_socket = -1;
68 /* Connect to winbindd socket */
70 static int open_pipe_sock(void)
72 struct sockaddr_un sunaddr;
77 if (our_pid != getpid()) {
78 if (established_socket != -1) {
79 close(established_socket);
81 established_socket = -1;
85 if (established_socket != -1) {
86 return established_socket;
89 /* Check permissions on unix socket directory */
91 if (lstat(WINBINDD_SOCKET_DIR, &st) == -1) {
95 if (!S_ISDIR(st.st_mode) || (st.st_uid != 0)) {
99 /* Connect to socket */
101 strncpy(path, WINBINDD_SOCKET_DIR, sizeof(path) - 1);
102 path[sizeof(path) - 1] = '\0';
104 strncat(path, "/", sizeof(path) - 1);
105 path[sizeof(path) - 1] = '\0';
107 strncat(path, WINBINDD_SOCKET_NAME, sizeof(path) - 1);
108 path[sizeof(path) - 1] = '\0';
110 ZERO_STRUCT(sunaddr);
111 sunaddr.sun_family = AF_UNIX;
112 strncpy(sunaddr.sun_path, path, sizeof(sunaddr.sun_path) - 1);
114 /* If socket file doesn't exist, don't bother trying to connect
115 with retry. This is an attempt to make the system usable when
116 the winbindd daemon is not running. */
118 if (lstat(path, &st) == -1) {
122 /* Check permissions on unix socket file */
124 if (!S_ISSOCK(st.st_mode) || (st.st_uid != 0)) {
128 /* Connect to socket */
130 if ((established_socket = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
134 if (connect(established_socket, (struct sockaddr *)&sunaddr,
135 sizeof(sunaddr)) == -1) {
140 return established_socket;
143 /* Write data to winbindd socket with timeout */
145 int write_sock(void *buffer, int count)
147 int result, nwritten;
149 /* Open connection to winbind daemon */
153 if (open_pipe_sock() == -1) {
157 /* Write data to socket */
161 while(nwritten < count) {
166 /* Catch pipe close on other end by checking if a read()
167 call would not block by calling select(). */
170 FD_SET(established_socket, &r_fds);
173 if ((selret = select(established_socket + 1, &r_fds,
174 NULL, NULL, &tv)) == -1) {
176 return -1; /* Select error */
179 /* Write should be OK if fd not available for reading */
181 if (!FD_ISSET(established_socket, &r_fds)) {
185 result = write(established_socket,
186 (char *)buffer + nwritten,
189 if ((result == -1) || (result == 0)) {
201 /* Pipe has closed on remote end */
211 /* Read data from winbindd socket with timeout */
213 static int read_sock(void *buffer, int count)
215 int result = 0, nread = 0;
217 /* Read data from socket */
219 while(nread < count) {
221 result = read(established_socket, (char *)buffer + nread,
224 if ((result == -1) || (result == 0)) {
226 /* Read failed. I think the only useful thing we
227 can do here is just return -1 and fail since the
228 transaction has failed half way through. */
242 int read_reply(struct winbindd_response *response)
244 int result1, result2 = 0;
250 /* Read fixed length response */
252 if ((result1 = read_sock(response, sizeof(struct winbindd_response)))
258 /* We actually send the pointer value of the extra_data field from
259 the server. This has no meaning in the client's address space
260 so we clear it out. */
262 response->extra_data = NULL;
264 /* Read variable length response */
266 if (response->length > sizeof(struct winbindd_response)) {
267 int extra_data_len = response->length -
268 sizeof(struct winbindd_response);
270 /* Mallocate memory for extra data */
272 if (!(response->extra_data = malloc(extra_data_len))) {
276 if ((result2 = read_sock(response->extra_data, extra_data_len))
282 /* Return total amount of data read */
284 return result1 + result2;
287 /* Free a response structure */
289 void free_response(struct winbindd_response *response)
291 /* Free any allocated extra_data */
293 if (response && response->extra_data) {
294 free(response->extra_data);
295 response->extra_data = NULL;
299 /* Handle simple types of requests */
301 enum nss_status winbindd_request(int req_type,
302 struct winbindd_request *request,
303 struct winbindd_response *response)
305 struct winbindd_request lrequest;
306 struct winbindd_response lresponse;
308 /* Check for our tricky environment variable */
310 if (getenv(WINBINDD_DONT_ENV)) {
311 return NSS_STATUS_NOTFOUND;
315 ZERO_STRUCT(lresponse);
316 response = &lresponse;
320 ZERO_STRUCT(lrequest);
324 /* Fill in request and send down pipe */
325 init_request(request, req_type);
327 if (write_sock(request, sizeof(*request)) == -1) {
328 return NSS_STATUS_UNAVAIL;
332 if (read_reply(response) == -1) {
333 return NSS_STATUS_UNAVAIL;
336 /* Throw away extra data if client didn't request it */
337 if (response == &lresponse) {
338 free_response(response);
341 /* Copy reply data from socket */
342 if (response->result != WINBINDD_OK) {
343 return NSS_STATUS_NOTFOUND;
346 return NSS_STATUS_SUCCESS;