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 */
33 /* Initialise a request structure */
35 void init_request(struct winbindd_request *request, int request_type)
37 static char *domain_env;
38 static BOOL initialised;
40 request->cmd = request_type;
41 request->pid = getpid();
42 request->domain[0] = '\0';
46 domain_env = getenv(WINBINDD_DOMAIN_ENV);
50 strncpy(request->domain, domain_env,
51 sizeof(request->domain) - 1);
52 request->domain[sizeof(request->domain) - 1] = '\0';
56 /* Initialise a response structure */
58 void init_response(struct winbindd_response *response)
60 /* Initialise return value */
62 response->result = WINBINDD_ERROR;
65 /* Close established socket */
69 if (established_socket != -1) {
70 close(established_socket);
71 established_socket = -1;
75 /* Connect to winbindd socket */
77 static int open_pipe_sock(void)
79 struct sockaddr_un sunaddr;
84 if (our_pid != getpid()) {
85 if (established_socket != -1) {
86 close(established_socket);
88 established_socket = -1;
92 if (established_socket != -1) {
93 return established_socket;
96 /* Check permissions on unix socket directory */
98 if (lstat(WINBINDD_SOCKET_DIR, &st) == -1) {
102 if (!S_ISDIR(st.st_mode) || (st.st_uid != 0)) {
106 /* Connect to socket */
108 strncpy(path, WINBINDD_SOCKET_DIR, sizeof(path) - 1);
109 path[sizeof(path) - 1] = '\0';
111 strncat(path, "/", sizeof(path) - 1);
112 path[sizeof(path) - 1] = '\0';
114 strncat(path, WINBINDD_SOCKET_NAME, sizeof(path) - 1);
115 path[sizeof(path) - 1] = '\0';
117 ZERO_STRUCT(sunaddr);
118 sunaddr.sun_family = AF_UNIX;
119 strncpy(sunaddr.sun_path, path, sizeof(sunaddr.sun_path) - 1);
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. */
125 if (lstat(path, &st) == -1) {
129 /* Check permissions on unix socket file */
131 if (!S_ISSOCK(st.st_mode) || (st.st_uid != 0)) {
135 /* Connect to socket */
137 if ((established_socket = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
141 if (connect(established_socket, (struct sockaddr *)&sunaddr,
142 sizeof(sunaddr)) == -1) {
144 established_socket = -1;
148 return established_socket;
151 /* Write data to winbindd socket with timeout */
153 int write_sock(void *buffer, int count)
155 int result, nwritten;
157 /* Open connection to winbind daemon */
161 if (open_pipe_sock() == -1) {
165 /* Write data to socket */
169 while(nwritten < count) {
174 /* Catch pipe close on other end by checking if a read()
175 call would not block by calling select(). */
178 FD_SET(established_socket, &r_fds);
181 if ((selret = select(established_socket + 1, &r_fds,
182 NULL, NULL, &tv)) == -1) {
184 return -1; /* Select error */
187 /* Write should be OK if fd not available for reading */
189 if (!FD_ISSET(established_socket, &r_fds)) {
193 result = write(established_socket,
194 (char *)buffer + nwritten,
197 if ((result == -1) || (result == 0)) {
209 /* Pipe has closed on remote end */
219 /* Read data from winbindd socket with timeout */
221 static int read_sock(void *buffer, int count)
223 int result = 0, nread = 0;
225 /* Read data from socket */
227 while(nread < count) {
229 result = read(established_socket, (char *)buffer + nread,
232 if ((result == -1) || (result == 0)) {
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. */
250 int read_reply(struct winbindd_response *response)
252 int result1, result2 = 0;
258 /* Read fixed length response */
260 if ((result1 = read_sock(response, sizeof(struct winbindd_response)))
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. */
270 response->extra_data = NULL;
272 /* Read variable length response */
274 if (response->length > sizeof(struct winbindd_response)) {
275 int extra_data_len = response->length -
276 sizeof(struct winbindd_response);
278 /* Mallocate memory for extra data */
280 if (!(response->extra_data = malloc(extra_data_len))) {
284 if ((result2 = read_sock(response->extra_data, extra_data_len))
290 /* Return total amount of data read */
292 return result1 + result2;
295 /* Free a response structure */
297 void free_response(struct winbindd_response *response)
299 /* Free any allocated extra_data */
301 if (response && response->extra_data) {
302 free(response->extra_data);
303 response->extra_data = NULL;
307 /* Handle simple types of requests */
309 NSS_STATUS winbindd_request(int req_type,
310 struct winbindd_request *request,
311 struct winbindd_response *response)
313 struct winbindd_request lrequest;
314 struct winbindd_response lresponse;
316 /* Check for our tricky environment variable */
318 if (getenv(WINBINDD_DONT_ENV)) {
319 return NSS_STATUS_NOTFOUND;
323 ZERO_STRUCT(lresponse);
324 response = &lresponse;
328 ZERO_STRUCT(lrequest);
332 /* Fill in request and send down pipe */
334 init_request(request, req_type);
335 init_response(response);
337 if (write_sock(request, sizeof(*request)) == -1) {
338 return NSS_STATUS_UNAVAIL;
342 if (read_reply(response) == -1) {
343 return NSS_STATUS_UNAVAIL;
346 /* Throw away extra data if client didn't request it */
347 if (response == &lresponse) {
348 free_response(response);
351 /* Copy reply data from socket */
352 if (response->result != WINBINDD_OK) {
353 return NSS_STATUS_NOTFOUND;
356 return NSS_STATUS_SUCCESS;