2 Unix SMB/CIFS implementation.
4 Windows NT Domain nsswitch module
6 Copyright (C) Tim Potter 2000
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public
10 License as published by the Free Software Foundation; either
11 version 2 of the License, or (at your option) any later version.
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Library General Public License for more details.
18 You should have received a copy of the GNU Library General Public
19 License along with this library; if not, write to the
20 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.
24 #include "winbind_nss_config.h"
25 #include "winbindd_nss.h"
30 #include <ns_daemon.h>
33 #define MAX_GETPWENT_USERS 250
34 #define MAX_GETGRENT_USERS 250
36 /* Prototypes from wb_common.c */
38 extern int winbindd_fd;
40 void init_request(struct winbindd_request *req,int rq_type);
41 NSS_STATUS winbindd_send_request(int req_type,
42 struct winbindd_request *request);
43 NSS_STATUS winbindd_get_response(struct winbindd_response *response);
44 NSS_STATUS winbindd_request(int req_type,
45 struct winbindd_request *request,
46 struct winbindd_response *response);
47 int winbind_open_pipe_sock(void);
48 int write_sock(void *buffer, int count);
49 int read_reply(struct winbindd_response *response);
50 void free_response(struct winbindd_response *response);
55 static int send_next_request(nsd_file_t *, struct winbindd_request *);
56 static int do_list(int state, nsd_file_t *rq);
58 static nsd_file_t *current_rq = NULL;
59 static int current_winbind_xid = 0;
60 static int next_winbind_xid = 0;
62 typedef struct winbind_xid {
65 struct winbindd_request *request;
66 struct winbind_xid *next;
69 static winbind_xid_t *winbind_xids = (winbind_xid_t *)0;
72 winbind_xid_new(int xid, nsd_file_t *rq, struct winbindd_request *request)
76 nsd_logprintf(NSD_LOG_LOW,
77 "entering winbind_xid_new xid = %d rq = 0x%x, request = 0x%x\n",
79 new = (winbind_xid_t *)nsd_calloc(1,sizeof(winbind_xid_t));
81 nsd_logprintf(NSD_LOG_RESOURCE,"winbind_xid_new: failed malloc\n");
87 new->request = request;
88 new->next = winbind_xids;
95 ** This routine will look down the xid list and return the request
96 ** associated with an xid. We remove the record if it is found.
99 winbind_xid_lookup(int xid, struct winbindd_request **requestp)
101 winbind_xid_t **last, *dx;
102 nsd_file_t *result=0;
104 for (last = &winbind_xids, dx = winbind_xids; dx && (dx->xid != xid);
105 last = &dx->next, dx = dx->next);
109 *requestp = dx->request;
112 nsd_logprintf(NSD_LOG_LOW,
113 "entering winbind_xid_lookup xid = %d rq = 0x%x, request = 0x%x\n",
114 xid, result, dx->request);
120 winbind_startnext_timeout(nsd_file_t **rqp, nsd_times_t *to)
123 struct winbindd_request *request;
125 nsd_logprintf(NSD_LOG_MIN, "timeout (winbind startnext)\n");
128 nsd_timeout_remove(rq);
129 request = to->t_clientdata;
130 return(send_next_request(rq, request));
137 struct winbindd_request *request;
140 * Check for queued requests
143 nsd_logprintf(NSD_LOG_MIN, "timeout (winbind) unqueue xid %d\n",
144 current_winbind_xid);
145 rq = winbind_xid_lookup(current_winbind_xid++, &request);
146 /* cause a timeout on the queued request so we can send it */
147 nsd_timeout_new(rq,1,winbind_startnext_timeout,request);
152 do_request(nsd_file_t *rq, struct winbindd_request *request)
154 if (winbind_xids == NULL) {
156 * No outstanding requests.
157 * Send off the request to winbindd
159 nsd_logprintf(NSD_LOG_MIN, "lookup (winbind) sending request\n");
160 return(send_next_request(rq, request));
163 * Just queue it up for now - previous callout or timout
166 nsd_logprintf(NSD_LOG_MIN,
167 "lookup (winbind): queue request xid = %d\n",
169 return(winbind_xid_new(next_winbind_xid++, rq, request));
174 winbind_callback(nsd_file_t **rqp, int fd)
176 struct winbindd_response response;
177 struct winbindd_pw *pw = &response.data.pw;
178 struct winbindd_gr *gr = &response.data.gr;
187 nsd_logprintf(NSD_LOG_MIN, "entering callback (winbind)\n");
192 nsd_timeout_remove(rq);
193 nsd_callback_remove(fd);
195 ZERO_STRUCT(response);
196 status = winbindd_get_response(&response);
198 if (status != NSS_STATUS_SUCCESS) {
199 /* free any extra data area in response structure */
200 free_response(&response);
201 nsd_logprintf(NSD_LOG_MIN,
202 "callback (winbind) returning not found, status = %d\n",
204 rq->f_status = NS_NOTFOUND;
208 maxlen = sizeof(result) - 1;
210 switch ((int)rq->f_cmd_data) {
211 case WINBINDD_WINS_BYNAME:
212 case WINBINDD_WINS_BYIP:
213 snprintf(result,maxlen,"%s\n",response.data.winsresp);
215 case WINBINDD_GETPWUID:
216 case WINBINDD_GETPWNAM:
217 snprintf(result,maxlen,"%s:%s:%d:%d:%s:%s:%s\n",
226 case WINBINDD_GETGRNAM:
227 case WINBINDD_GETGRGID:
228 if (gr->num_gr_mem && response.extra_data)
229 members = response.extra_data;
232 snprintf(result,maxlen,"%s:%s:%d:%s\n",
233 gr->gr_name, gr->gr_passwd, gr->gr_gid, members);
235 case WINBINDD_SETGRENT:
236 case WINBINDD_SETPWENT:
237 nsd_logprintf(NSD_LOG_MIN, "callback (winbind) - SETPWENT/SETGRENT\n");
238 free_response(&response);
239 return(do_list(1,rq));
240 case WINBINDD_GETGRENT:
241 nsd_logprintf(NSD_LOG_MIN,
242 "callback (winbind) - %d GETGRENT responses\n",
243 response.data.num_entries);
244 if (response.data.num_entries) {
245 gr = (struct winbindd_gr *)response.extra_data;
247 nsd_logprintf(NSD_LOG_MIN, " no extra_data\n");
248 free_response(&response);
251 members = (char *)response.extra_data +
252 (response.data.num_entries * sizeof(struct winbindd_gr));
253 for (i = 0; i < response.data.num_entries; i++) {
254 snprintf(result,maxlen,"%s:%s:%d:%s\n",
255 gr->gr_name, gr->gr_passwd, gr->gr_gid,
256 &members[gr->gr_mem_ofs]);
257 nsd_logprintf(NSD_LOG_MIN, " GETGRENT %s\n",result);
258 nsd_append_element(rq,NS_SUCCESS,result,strlen(result));
262 i = response.data.num_entries;
263 free_response(&response);
264 if (i < MAX_GETPWENT_USERS)
265 return(do_list(2,rq));
267 return(do_list(1,rq));
268 case WINBINDD_GETPWENT:
269 nsd_logprintf(NSD_LOG_MIN,
270 "callback (winbind) - %d GETPWENT responses\n",
271 response.data.num_entries);
272 if (response.data.num_entries) {
273 pw = (struct winbindd_pw *)response.extra_data;
275 nsd_logprintf(NSD_LOG_MIN, " no extra_data\n");
276 free_response(&response);
279 for (i = 0; i < response.data.num_entries; i++) {
280 snprintf(result,maxlen,"%s:%s:%d:%d:%s:%s:%s",
288 nsd_logprintf(NSD_LOG_MIN, " GETPWENT %s\n",result);
289 nsd_append_element(rq,NS_SUCCESS,result,strlen(result));
293 i = response.data.num_entries;
294 free_response(&response);
295 if (i < MAX_GETPWENT_USERS)
296 return(do_list(2,rq));
298 return(do_list(1,rq));
299 case WINBINDD_ENDGRENT:
300 case WINBINDD_ENDPWENT:
301 nsd_logprintf(NSD_LOG_MIN, "callback (winbind) - ENDPWENT/ENDGRENT\n");
302 nsd_append_element(rq,NS_SUCCESS,"\n",1);
303 free_response(&response);
306 free_response(&response);
307 nsd_logprintf(NSD_LOG_MIN, "callback (winbind) - no valid command\n");
310 nsd_logprintf(NSD_LOG_MIN, "callback (winbind) %s\n", result);
311 /* free any extra data area in response structure */
312 free_response(&response);
313 nsd_set_result(rq,NS_SUCCESS,result,strlen(result),VOLATILE);
318 winbind_timeout(nsd_file_t **rqp, nsd_times_t *to)
324 nsd_logprintf(NSD_LOG_MIN, "timeout (winbind)\n");
329 /* Remove the callback and timeout */
330 nsd_callback_remove(winbindd_fd);
331 nsd_timeout_remove(rq);
333 rq->f_status = NS_NOTFOUND;
338 send_next_request(nsd_file_t *rq, struct winbindd_request *request)
345 nsd_logprintf(NSD_LOG_MIN, "send_next_request (winbind) %d to = %d\n",
346 rq->f_cmd_data, timeout);
347 status = winbindd_send_request((int)rq->f_cmd_data,request);
350 if (status != NSS_STATUS_SUCCESS) {
351 nsd_logprintf(NSD_LOG_MIN,
352 "send_next_request (winbind) error status = %d\n",status);
353 rq->f_status = status;
360 * Set up callback and timeouts
362 nsd_logprintf(NSD_LOG_MIN, "send_next_request (winbind) fd = %d\n",winbindd_fd);
363 nsd_callback_new(winbindd_fd,winbind_callback,NSD_READ);
364 nsd_timeout_new(rq,timeout,winbind_timeout,(void *)0);
370 nsd_logprintf(NSD_LOG_MIN, "entering init (winbind)\n");
374 int lookup(nsd_file_t *rq)
378 struct winbindd_request *request;
380 nsd_logprintf(NSD_LOG_MIN, "entering lookup (winbind)\n");
384 map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
385 key = nsd_attr_fetch_string(rq->f_attrs, "key", (char*)0);
386 if (! map || ! key) {
387 nsd_logprintf(NSD_LOG_MIN, "lookup (winbind) table or key not defined\n");
388 rq->f_status = NS_BADREQ;
392 nsd_logprintf(NSD_LOG_MIN, "lookup (winbind %s)\n",map);
394 request = (struct winbindd_request *)nsd_calloc(1,sizeof(struct winbindd_request));
396 nsd_logprintf(NSD_LOG_RESOURCE,
397 "lookup (winbind): failed malloc\n");
401 if (strcasecmp(map,"passwd.byuid") == 0) {
402 request->data.uid = atoi(key);
403 rq->f_cmd_data = (void *)WINBINDD_GETPWUID;
404 } else if (strcasecmp(map,"passwd.byname") == 0) {
405 strncpy(request->data.username, key,
406 sizeof(request->data.username) - 1);
407 request->data.username[sizeof(request->data.username) - 1] = '\0';
408 rq->f_cmd_data = (void *)WINBINDD_GETPWNAM;
409 } else if (strcasecmp(map,"group.byname") == 0) {
410 strncpy(request->data.groupname, key,
411 sizeof(request->data.groupname) - 1);
412 request->data.groupname[sizeof(request->data.groupname) - 1] = '\0';
413 rq->f_cmd_data = (void *)WINBINDD_GETGRNAM;
414 } else if (strcasecmp(map,"group.bygid") == 0) {
415 request->data.gid = atoi(key);
416 rq->f_cmd_data = (void *)WINBINDD_GETGRGID;
417 } else if (strcasecmp(map,"hosts.byname") == 0) {
418 strncpy(request->data.winsreq, key, sizeof(request->data.winsreq) - 1);
419 request->data.winsreq[sizeof(request->data.winsreq) - 1] = '\0';
420 rq->f_cmd_data = (void *)WINBINDD_WINS_BYNAME;
421 } else if (strcasecmp(map,"hosts.byaddr") == 0) {
422 strncpy(request->data.winsreq, key, sizeof(request->data.winsreq) - 1);
423 request->data.winsreq[sizeof(request->data.winsreq) - 1] = '\0';
424 rq->f_cmd_data = (void *)WINBINDD_WINS_BYIP;
427 * Don't understand this map - just return not found
429 nsd_logprintf(NSD_LOG_MIN, "lookup (winbind) unknown table\n");
431 rq->f_status = NS_NOTFOUND;
435 return(do_request(rq, request));
438 int list(nsd_file_t *rq)
442 nsd_logprintf(NSD_LOG_MIN, "entering list (winbind)\n");
446 map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
448 nsd_logprintf(NSD_LOG_MIN, "list (winbind) table not defined\n");
449 rq->f_status = NS_BADREQ;
453 nsd_logprintf(NSD_LOG_MIN, "list (winbind %s)\n",map);
455 return (do_list(0,rq));
459 do_list(int state, nsd_file_t *rq)
462 struct winbindd_request *request;
464 nsd_logprintf(NSD_LOG_MIN, "entering do_list (winbind) state = %d\n",state);
466 map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
467 request = (struct winbindd_request *)nsd_calloc(1,sizeof(struct winbindd_request));
469 nsd_logprintf(NSD_LOG_RESOURCE,
470 "do_list (winbind): failed malloc\n");
474 if (strcasecmp(map,"passwd.byname") == 0) {
477 rq->f_cmd_data = (void *)WINBINDD_SETPWENT;
480 request->data.num_entries = MAX_GETPWENT_USERS;
481 rq->f_cmd_data = (void *)WINBINDD_GETPWENT;
484 rq->f_cmd_data = (void *)WINBINDD_ENDPWENT;
487 nsd_logprintf(NSD_LOG_MIN, "do_list (winbind) unknown state\n");
489 rq->f_status = NS_NOTFOUND;
492 } else if (strcasecmp(map,"group.byname") == 0) {
495 rq->f_cmd_data = (void *)WINBINDD_SETGRENT;
498 request->data.num_entries = MAX_GETGRENT_USERS;
499 rq->f_cmd_data = (void *)WINBINDD_GETGRENT;
502 rq->f_cmd_data = (void *)WINBINDD_ENDGRENT;
505 nsd_logprintf(NSD_LOG_MIN, "do_list (winbind) unknown state\n");
507 rq->f_status = NS_NOTFOUND;
512 * Don't understand this map - just return not found
514 nsd_logprintf(NSD_LOG_MIN, "do_list (winbind) unknown table\n");
516 rq->f_status = NS_NOTFOUND;
520 return(do_request(rq, request));
525 /* Allocate some space from the nss static buffer. The buffer and buflen
526 are the pointers passed in by the C library to the _nss_ntdom_*
529 static char *get_static(char **buffer, int *buflen, int len)
533 /* Error check. We return false if things aren't set up right, or
534 there isn't enough buffer space left. */
536 if ((buffer == NULL) || (buflen == NULL) || (*buflen < len)) {
540 /* Return an index into the static buffer */
549 /* I've copied the strtok() replacement function next_token() from
550 lib/util_str.c as I really don't want to have to link in any other
551 objects if I can possibly avoid it. */
553 BOOL next_token(char **ptr,char *buff,char *sep, size_t bufsize)
559 if (!ptr) return(False);
563 /* default to simple separators */
564 if (!sep) sep = " \t\n\r";
566 /* find the first non sep char */
567 while (*s && strchr(sep,*s)) s++;
570 if (! *s) return(False);
572 /* copy over the token */
573 for (quoted = False; len < bufsize && *s && (quoted || !strchr(sep,*s)); s++) {
582 *ptr = (*s) ? s+1 : s;
589 /* Fill a pwent structure from a winbindd_response structure. We use
590 the static data passed to us by libc to put strings and stuff in.
591 Return NSS_STATUS_TRYAGAIN if we run out of memory. */
593 static NSS_STATUS fill_pwent(struct passwd *result,
594 struct winbindd_pw *pw,
595 char **buffer, int *buflen)
599 if ((result->pw_name =
600 get_static(buffer, buflen, strlen(pw->pw_name) + 1)) == NULL) {
604 return NSS_STATUS_TRYAGAIN;
607 strcpy(result->pw_name, pw->pw_name);
611 if ((result->pw_passwd =
612 get_static(buffer, buflen, strlen(pw->pw_passwd) + 1)) == NULL) {
616 return NSS_STATUS_TRYAGAIN;
619 strcpy(result->pw_passwd, pw->pw_passwd);
623 result->pw_uid = pw->pw_uid;
624 result->pw_gid = pw->pw_gid;
628 if ((result->pw_gecos =
629 get_static(buffer, buflen, strlen(pw->pw_gecos) + 1)) == NULL) {
633 return NSS_STATUS_TRYAGAIN;
636 strcpy(result->pw_gecos, pw->pw_gecos);
640 if ((result->pw_dir =
641 get_static(buffer, buflen, strlen(pw->pw_dir) + 1)) == NULL) {
645 return NSS_STATUS_TRYAGAIN;
648 strcpy(result->pw_dir, pw->pw_dir);
652 if ((result->pw_shell =
653 get_static(buffer, buflen, strlen(pw->pw_shell) + 1)) == NULL) {
657 return NSS_STATUS_TRYAGAIN;
660 strcpy(result->pw_shell, pw->pw_shell);
662 return NSS_STATUS_SUCCESS;
665 /* Fill a grent structure from a winbindd_response structure. We use
666 the static data passed to us by libc to put strings and stuff in.
667 Return NSS_STATUS_TRYAGAIN if we run out of memory. */
669 static int fill_grent(struct group *result, struct winbindd_gr *gr,
670 char *gr_mem, char **buffer, int *buflen)
678 if ((result->gr_name =
679 get_static(buffer, buflen, strlen(gr->gr_name) + 1)) == NULL) {
683 return NSS_STATUS_TRYAGAIN;
686 strcpy(result->gr_name, gr->gr_name);
690 if ((result->gr_passwd =
691 get_static(buffer, buflen, strlen(gr->gr_passwd) + 1)) == NULL) {
695 return NSS_STATUS_TRYAGAIN;
698 strcpy(result->gr_passwd, gr->gr_passwd);
702 result->gr_gid = gr->gr_gid;
704 /* Group membership */
706 if ((gr->num_gr_mem < 0) || !gr_mem) {
710 /* this next value is a pointer to a pointer so let's align it */
712 /* Calculate number of extra bytes needed to align on pointer size boundry */
713 if ((i = (int)*buffer % sizeof(char*)) != 0)
714 i = sizeof(char*) - i;
716 if ((tst = get_static(buffer, buflen, ((gr->num_gr_mem + 1) *
717 sizeof(char *)+i))) == NULL) {
721 return NSS_STATUS_TRYAGAIN;
723 result->gr_mem = (char **)(tst + i);
725 if (gr->num_gr_mem == 0) {
729 *(result->gr_mem) = NULL;
730 return NSS_STATUS_SUCCESS;
733 /* Start looking at extra data */
737 while(next_token((char **)&gr_mem, name, ",", sizeof(fstring))) {
739 /* Allocate space for member */
741 if (((result->gr_mem)[i] =
742 get_static(buffer, buflen, strlen(name) + 1)) == NULL) {
746 return NSS_STATUS_TRYAGAIN;
749 strcpy((result->gr_mem)[i], name);
755 (result->gr_mem)[i] = NULL;
757 return NSS_STATUS_SUCCESS;
764 static struct winbindd_response getpwent_response;
766 static int ndx_pw_cache; /* Current index into pwd cache */
767 static int num_pw_cache; /* Current size of pwd cache */
769 /* Rewind "file pointer" to start of ntdom password database */
772 _nss_winbind_setpwent(void)
775 fprintf(stderr, "[%5d]: setpwent\n", getpid());
778 if (num_pw_cache > 0) {
779 ndx_pw_cache = num_pw_cache = 0;
780 free_response(&getpwent_response);
783 return winbindd_request(WINBINDD_SETPWENT, NULL, NULL);
786 /* Close ntdom password database "file pointer" */
789 _nss_winbind_endpwent(void)
792 fprintf(stderr, "[%5d]: endpwent\n", getpid());
795 if (num_pw_cache > 0) {
796 ndx_pw_cache = num_pw_cache = 0;
797 free_response(&getpwent_response);
800 return winbindd_request(WINBINDD_ENDPWENT, NULL, NULL);
803 /* Fetch the next password entry from ntdom password database */
806 _nss_winbind_getpwent_r(struct passwd *result, char *buffer,
807 size_t buflen, int *errnop)
810 struct winbindd_request request;
811 static int called_again;
814 fprintf(stderr, "[%5d]: getpwent\n", getpid());
817 /* Return an entry from the cache if we have one, or if we are
818 called again because we exceeded our static buffer. */
820 if ((ndx_pw_cache < num_pw_cache) || called_again) {
824 /* Else call winbindd to get a bunch of entries */
826 if (num_pw_cache > 0) {
827 free_response(&getpwent_response);
830 ZERO_STRUCT(request);
831 ZERO_STRUCT(getpwent_response);
833 request.data.num_entries = MAX_GETPWENT_USERS;
835 ret = winbindd_request(WINBINDD_GETPWENT, &request,
838 if (ret == NSS_STATUS_SUCCESS) {
839 struct winbindd_pw *pw_cache;
844 num_pw_cache = getpwent_response.data.num_entries;
846 /* Return a result */
850 pw_cache = getpwent_response.extra_data;
852 /* Check data is valid */
854 if (pw_cache == NULL) {
855 return NSS_STATUS_NOTFOUND;
858 ret = fill_pwent(result, &pw_cache[ndx_pw_cache],
861 /* Out of memory - try again */
863 if (ret == NSS_STATUS_TRYAGAIN) {
865 *errnop = errno = ERANGE;
870 called_again = False;
873 /* If we've finished with this lot of results free cache */
875 if (ndx_pw_cache == num_pw_cache) {
876 ndx_pw_cache = num_pw_cache = 0;
877 free_response(&getpwent_response);
884 /* Return passwd struct from uid */
887 _nss_winbind_getpwuid_r(uid_t uid, struct passwd *result, char *buffer,
888 size_t buflen, int *errnop)
891 static struct winbindd_response response;
892 struct winbindd_request request;
893 static int keep_response=0;
895 /* If our static buffer needs to be expanded we are called again */
896 if (!keep_response) {
898 /* Call for the first time */
900 ZERO_STRUCT(response);
901 ZERO_STRUCT(request);
903 request.data.uid = uid;
905 ret = winbindd_request(WINBINDD_GETPWUID, &request, &response);
907 if (ret == NSS_STATUS_SUCCESS) {
908 ret = fill_pwent(result, &response.data.pw,
911 if (ret == NSS_STATUS_TRYAGAIN) {
912 keep_response = True;
913 *errnop = errno = ERANGE;
920 /* We've been called again */
922 ret = fill_pwent(result, &response.data.pw, &buffer, &buflen);
924 if (ret == NSS_STATUS_TRYAGAIN) {
925 keep_response = True;
926 *errnop = errno = ERANGE;
930 keep_response = False;
934 free_response(&response);
938 /* Return passwd struct from username */
941 _nss_winbind_getpwnam_r(const char *name, struct passwd *result, char *buffer,
942 size_t buflen, int *errnop)
945 static struct winbindd_response response;
946 struct winbindd_request request;
947 static int keep_response;
950 fprintf(stderr, "[%5d]: getpwnam %s\n", getpid(), name);
953 /* If our static buffer needs to be expanded we are called again */
955 if (!keep_response) {
957 /* Call for the first time */
959 ZERO_STRUCT(response);
960 ZERO_STRUCT(request);
962 strncpy(request.data.username, name,
963 sizeof(request.data.username) - 1);
964 request.data.username
965 [sizeof(request.data.username) - 1] = '\0';
967 ret = winbindd_request(WINBINDD_GETPWNAM, &request, &response);
969 if (ret == NSS_STATUS_SUCCESS) {
970 ret = fill_pwent(result, &response.data.pw, &buffer,
973 if (ret == NSS_STATUS_TRYAGAIN) {
974 keep_response = True;
975 *errnop = errno = ERANGE;
982 /* We've been called again */
984 ret = fill_pwent(result, &response.data.pw, &buffer, &buflen);
986 if (ret == NSS_STATUS_TRYAGAIN) {
987 keep_response = True;
988 *errnop = errno = ERANGE;
992 keep_response = False;
996 free_response(&response);
1001 * NSS group functions
1004 static struct winbindd_response getgrent_response;
1006 static int ndx_gr_cache; /* Current index into grp cache */
1007 static int num_gr_cache; /* Current size of grp cache */
1009 /* Rewind "file pointer" to start of ntdom group database */
1012 _nss_winbind_setgrent(void)
1015 fprintf(stderr, "[%5d]: setgrent\n", getpid());
1018 if (num_gr_cache > 0) {
1019 ndx_gr_cache = num_gr_cache = 0;
1020 free_response(&getgrent_response);
1023 return winbindd_request(WINBINDD_SETGRENT, NULL, NULL);
1026 /* Close "file pointer" for ntdom group database */
1029 _nss_winbind_endgrent(void)
1032 fprintf(stderr, "[%5d]: endgrent\n", getpid());
1035 if (num_gr_cache > 0) {
1036 ndx_gr_cache = num_gr_cache = 0;
1037 free_response(&getgrent_response);
1040 return winbindd_request(WINBINDD_ENDGRENT, NULL, NULL);
1043 /* Get next entry from ntdom group database */
1046 _nss_winbind_getgrent_r(struct group *result,
1047 char *buffer, size_t buflen, int *errnop)
1050 static struct winbindd_request request;
1051 static int called_again;
1054 fprintf(stderr, "[%5d]: getgrent\n", getpid());
1057 /* Return an entry from the cache if we have one, or if we are
1058 called again because we exceeded our static buffer. */
1060 if ((ndx_gr_cache < num_gr_cache) || called_again) {
1064 /* Else call winbindd to get a bunch of entries */
1066 if (num_gr_cache > 0) {
1067 free_response(&getgrent_response);
1070 ZERO_STRUCT(request);
1071 ZERO_STRUCT(getgrent_response);
1073 request.data.num_entries = MAX_GETGRENT_USERS;
1075 ret = winbindd_request(WINBINDD_GETGRENT, &request,
1076 &getgrent_response);
1078 if (ret == NSS_STATUS_SUCCESS) {
1079 struct winbindd_gr *gr_cache;
1085 num_gr_cache = getgrent_response.data.num_entries;
1087 /* Return a result */
1091 gr_cache = getgrent_response.extra_data;
1093 /* Check data is valid */
1095 if (gr_cache == NULL) {
1096 return NSS_STATUS_NOTFOUND;
1099 /* Fill group membership. The offset into the extra data
1100 for the group membership is the reported offset plus the
1101 size of all the winbindd_gr records returned. */
1103 mem_ofs = gr_cache[ndx_gr_cache].gr_mem_ofs +
1104 num_gr_cache * sizeof(struct winbindd_gr);
1106 ret = fill_grent(result, &gr_cache[ndx_gr_cache],
1107 ((char *)getgrent_response.extra_data)+mem_ofs,
1110 /* Out of memory - try again */
1112 if (ret == NSS_STATUS_TRYAGAIN) {
1113 called_again = True;
1114 *errnop = errno = ERANGE;
1119 called_again = False;
1122 /* If we've finished with this lot of results free cache */
1124 if (ndx_gr_cache == num_gr_cache) {
1125 ndx_gr_cache = num_gr_cache = 0;
1126 free_response(&getgrent_response);
1133 /* Return group struct from group name */
1136 _nss_winbind_getgrnam_r(const char *name,
1137 struct group *result, char *buffer,
1138 size_t buflen, int *errnop)
1141 static struct winbindd_response response;
1142 struct winbindd_request request;
1143 static int keep_response;
1146 fprintf(stderr, "[%5d]: getgrnam %s\n", getpid(), name);
1149 /* If our static buffer needs to be expanded we are called again */
1151 if (!keep_response) {
1153 /* Call for the first time */
1155 ZERO_STRUCT(request);
1156 ZERO_STRUCT(response);
1158 strncpy(request.data.groupname, name,
1159 sizeof(request.data.groupname));
1160 request.data.groupname
1161 [sizeof(request.data.groupname) - 1] = '\0';
1163 ret = winbindd_request(WINBINDD_GETGRNAM, &request, &response);
1165 if (ret == NSS_STATUS_SUCCESS) {
1166 ret = fill_grent(result, &response.data.gr,
1167 response.extra_data,
1170 if (ret == NSS_STATUS_TRYAGAIN) {
1171 keep_response = True;
1172 *errnop = errno = ERANGE;
1179 /* We've been called again */
1181 ret = fill_grent(result, &response.data.gr,
1182 response.extra_data, &buffer, &buflen);
1184 if (ret == NSS_STATUS_TRYAGAIN) {
1185 keep_response = True;
1186 *errnop = errno = ERANGE;
1190 keep_response = False;
1194 free_response(&response);
1198 /* Return group struct from gid */
1201 _nss_winbind_getgrgid_r(gid_t gid,
1202 struct group *result, char *buffer,
1203 size_t buflen, int *errnop)
1206 static struct winbindd_response response;
1207 struct winbindd_request request;
1208 static int keep_response;
1211 fprintf(stderr, "[%5d]: getgrgid %d\n", getpid(), gid);
1214 /* If our static buffer needs to be expanded we are called again */
1216 if (!keep_response) {
1218 /* Call for the first time */
1220 ZERO_STRUCT(request);
1221 ZERO_STRUCT(response);
1223 request.data.gid = gid;
1225 ret = winbindd_request(WINBINDD_GETGRGID, &request, &response);
1227 if (ret == NSS_STATUS_SUCCESS) {
1229 ret = fill_grent(result, &response.data.gr,
1230 response.extra_data,
1233 if (ret == NSS_STATUS_TRYAGAIN) {
1234 keep_response = True;
1235 *errnop = errno = ERANGE;
1242 /* We've been called again */
1244 ret = fill_grent(result, &response.data.gr,
1245 response.extra_data, &buffer, &buflen);
1247 if (ret == NSS_STATUS_TRYAGAIN) {
1248 keep_response = True;
1249 *errnop = errno = ERANGE;
1253 keep_response = False;
1257 free_response(&response);
1261 /* Initialise supplementary groups */
1264 _nss_winbind_initgroups_dyn(char *user, gid_t group, long int *start,
1265 long int *size, gid_t **groups, long int limit,
1269 struct winbindd_request request;
1270 struct winbindd_response response;
1274 fprintf(stderr, "[%5d]: initgroups %s (%d)\n", getpid(),
1278 ZERO_STRUCT(request);
1279 ZERO_STRUCT(response);
1281 strncpy(request.data.username, user,
1282 sizeof(request.data.username) - 1);
1284 ret = winbindd_request(WINBINDD_GETGROUPS, &request, &response);
1286 if (ret == NSS_STATUS_SUCCESS) {
1287 int num_gids = response.data.num_entries;
1288 gid_t *gid_list = (gid_t *)response.extra_data;
1290 /* Copy group list to client */
1292 for (i = 0; i < num_gids; i++) {
1294 /* Skip primary group */
1296 if (gid_list[i] == group) continue;
1300 if (*start == *size && limit <= 0) {
1301 (*groups) = realloc(
1302 (*groups), (2 * (*size) + 1) * sizeof(**groups));
1303 if (! *groups) goto done;
1304 *size = 2 * (*size) + 1;
1307 if (*start == *size) goto done;
1309 (*groups)[*start] = gid_list[i];
1312 /* Filled buffer? */
1314 if (*start == limit) goto done;
1318 /* Back to your regularly scheduled programming */