2 Unix SMB/Netbios implementation.
5 Windows NT Domain nsswitch module
7 Copyright (C) Tim Potter 2000
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public
11 License as published by the Free Software Foundation; either
12 version 2 of the License, or (at your option) any later version.
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Library General Public License for more details.
19 You should have received a copy of the GNU Library General Public
20 License along with this library; if not, write to the
21 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA.
25 #include "winbind_nss_config.h"
26 #include "winbindd_nss.h"
31 #include <ns_daemon.h>
34 #define MAX_GETPWENT_USERS 250
35 #define MAX_GETGRENT_USERS 250
37 /* Prototypes from wb_common.c */
39 extern int winbindd_fd;
41 void init_request(struct winbindd_request *req,int rq_type);
42 NSS_STATUS winbindd_send_request(int req_type,
43 struct winbindd_request *request);
44 NSS_STATUS winbindd_get_response(struct winbindd_response *response);
45 NSS_STATUS winbindd_request(int req_type,
46 struct winbindd_request *request,
47 struct winbindd_response *response);
48 int winbind_open_pipe_sock(void);
49 int write_sock(void *buffer, int count);
50 int read_reply(struct winbindd_response *response);
51 void free_response(struct winbindd_response *response);
56 static int send_next_request(nsd_file_t *, struct winbindd_request *);
57 static int do_list(int state, nsd_file_t *rq);
59 static nsd_file_t *current_rq = NULL;
60 static int current_winbind_xid = 0;
61 static int next_winbind_xid = 0;
63 typedef struct winbind_xid {
66 struct winbindd_request *request;
67 struct winbind_xid *next;
70 static winbind_xid_t *winbind_xids = (winbind_xid_t *)0;
73 winbind_xid_new(int xid, nsd_file_t *rq, struct winbindd_request *request)
77 nsd_logprintf(NSD_LOG_LOW,
78 "entering winbind_xid_new xid = %d rq = 0x%x, request = 0x%x\n",
80 new = (winbind_xid_t *)nsd_calloc(1,sizeof(winbind_xid_t));
82 nsd_logprintf(NSD_LOG_RESOURCE,"winbind_xid_new: failed malloc\n");
88 new->request = request;
89 new->next = winbind_xids;
96 ** This routine will look down the xid list and return the request
97 ** associated with an xid. We remove the record if it is found.
100 winbind_xid_lookup(int xid, struct winbindd_request **requestp)
102 winbind_xid_t **last, *dx;
103 nsd_file_t *result=0;
105 for (last = &winbind_xids, dx = winbind_xids; dx && (dx->xid != xid);
106 last = &dx->next, dx = dx->next);
110 *requestp = dx->request;
113 nsd_logprintf(NSD_LOG_LOW,
114 "entering winbind_xid_lookup xid = %d rq = 0x%x, request = 0x%x\n",
115 xid, result, dx->request);
121 winbind_startnext_timeout(nsd_file_t **rqp, nsd_times_t *to)
124 struct winbindd_request *request;
126 nsd_logprintf(NSD_LOG_MIN, "timeout (winbind startnext)\n");
129 nsd_timeout_remove(rq);
130 request = to->t_clientdata;
131 return(send_next_request(rq, request));
138 struct winbindd_request *request;
141 * Check for queued requests
144 nsd_logprintf(NSD_LOG_MIN, "timeout (winbind) unqueue xid %d\n",
145 current_winbind_xid);
146 rq = winbind_xid_lookup(current_winbind_xid++, &request);
147 /* cause a timeout on the queued request so we can send it */
148 nsd_timeout_new(rq,1,winbind_startnext_timeout,request);
153 do_request(nsd_file_t *rq, struct winbindd_request *request)
155 if (winbind_xids == NULL) {
157 * No outstanding requests.
158 * Send off the request to winbindd
160 nsd_logprintf(NSD_LOG_MIN, "lookup (winbind) sending request\n");
161 return(send_next_request(rq, request));
164 * Just queue it up for now - previous callout or timout
167 nsd_logprintf(NSD_LOG_MIN,
168 "lookup (winbind): queue request xid = %d\n",
170 return(winbind_xid_new(next_winbind_xid++, rq, request));
175 winbind_callback(nsd_file_t **rqp, int fd)
177 struct winbindd_response response;
178 struct winbindd_pw *pw = &response.data.pw;
179 struct winbindd_gr *gr = &response.data.gr;
188 nsd_logprintf(NSD_LOG_MIN, "entering callback (winbind)\n");
193 nsd_timeout_remove(rq);
194 nsd_callback_remove(fd);
196 ZERO_STRUCT(response);
197 status = winbindd_get_response(&response);
199 if (status != NSS_STATUS_SUCCESS) {
200 /* free any extra data area in response structure */
201 free_response(&response);
202 nsd_logprintf(NSD_LOG_MIN,
203 "callback (winbind) returning not found, status = %d\n",
205 rq->f_status = NS_NOTFOUND;
208 switch ((int)rq->f_cmd_data) {
209 case WINBINDD_GETPWNAM_FROM_UID:
210 case WINBINDD_GETPWNAM_FROM_USER:
211 snprintf(result,1023,"%s:%s:%d:%d:%s:%s:%s\n",
220 case WINBINDD_GETGRNAM_FROM_GROUP:
221 case WINBINDD_GETGRNAM_FROM_GID:
222 if (gr->num_gr_mem && response.extra_data)
223 members = response.extra_data;
226 snprintf(result,1023,"%s:%s:%d:%s\n",
227 gr->gr_name, gr->gr_passwd, gr->gr_gid, members);
229 case WINBINDD_SETGRENT:
230 case WINBINDD_SETPWENT:
231 nsd_logprintf(NSD_LOG_MIN, "callback (winbind) - SETPWENT/SETGRENT\n");
232 free_response(&response);
233 return(do_list(1,rq));
234 case WINBINDD_GETGRENT:
235 nsd_logprintf(NSD_LOG_MIN,
236 "callback (winbind) - %d GETGRENT responses\n",
237 response.data.num_entries);
238 if (response.data.num_entries) {
239 gr = (struct winbindd_gr *)response.extra_data;
241 nsd_logprintf(NSD_LOG_MIN, " no extra_data\n");
242 free_response(&response);
245 members = (char *)response.extra_data +
246 (response.data.num_entries * sizeof(struct winbindd_gr));
247 for (i = 0; i < response.data.num_entries; i++) {
248 snprintf(result,1023,"%s:%s:%d:%s\n",
249 gr->gr_name, gr->gr_passwd, gr->gr_gid,
250 &members[gr->gr_mem_ofs]);
251 nsd_logprintf(NSD_LOG_MIN, " GETGRENT %s\n",result);
252 nsd_append_element(rq,NS_SUCCESS,result,strlen(result));
256 i = response.data.num_entries;
257 free_response(&response);
258 if (i < MAX_GETPWENT_USERS)
259 return(do_list(2,rq));
261 return(do_list(1,rq));
262 case WINBINDD_GETPWENT:
263 nsd_logprintf(NSD_LOG_MIN,
264 "callback (winbind) - %d GETPWENT responses\n",
265 response.data.num_entries);
266 if (response.data.num_entries) {
267 pw = (struct winbindd_pw *)response.extra_data;
269 nsd_logprintf(NSD_LOG_MIN, " no extra_data\n");
270 free_response(&response);
273 for (i = 0; i < response.data.num_entries; i++) {
274 snprintf(result,1023,"%s:%s:%d:%d:%s:%s:%s",
282 nsd_logprintf(NSD_LOG_MIN, " GETPWENT %s\n",result);
283 nsd_append_element(rq,NS_SUCCESS,result,strlen(result));
287 i = response.data.num_entries;
288 free_response(&response);
289 if (i < MAX_GETPWENT_USERS)
290 return(do_list(2,rq));
292 return(do_list(1,rq));
293 case WINBINDD_ENDGRENT:
294 case WINBINDD_ENDPWENT:
295 nsd_logprintf(NSD_LOG_MIN, "callback (winbind) - ENDPWENT/ENDGRENT\n");
296 nsd_append_element(rq,NS_SUCCESS,"\n",1);
297 free_response(&response);
300 free_response(&response);
301 nsd_logprintf(NSD_LOG_MIN, "callback (winbind) - no valid command\n");
304 nsd_logprintf(NSD_LOG_MIN, "callback (winbind) %s\n", result);
305 /* free any extra data area in response structure */
306 free_response(&response);
307 nsd_set_result(rq,NS_SUCCESS,result,strlen(result),VOLATILE);
312 winbind_timeout(nsd_file_t **rqp, nsd_times_t *to)
318 nsd_logprintf(NSD_LOG_MIN, "timeout (winbind)\n");
323 /* Remove the callback and timeout */
324 nsd_callback_remove(winbindd_fd);
325 nsd_timeout_remove(rq);
327 rq->f_status = NS_NOTFOUND;
332 send_next_request(nsd_file_t *rq, struct winbindd_request *request)
339 nsd_logprintf(NSD_LOG_MIN, "send_next_request (winbind) %d to = %d\n",
340 rq->f_cmd_data, timeout);
341 status = winbindd_send_request((int)rq->f_cmd_data,request);
344 if (status != NSS_STATUS_SUCCESS) {
345 nsd_logprintf(NSD_LOG_MIN,
346 "send_next_request (winbind) error status = %d\n",status);
347 rq->f_status = status;
354 * Set up callback and timeouts
356 nsd_logprintf(NSD_LOG_MIN, "send_next_request (winbind) fd = %d\n",winbindd_fd);
357 nsd_callback_new(winbindd_fd,winbind_callback,NSD_READ);
358 nsd_timeout_new(rq,timeout,winbind_timeout,(void *)0);
364 nsd_logprintf(NSD_LOG_MIN, "entering init (winbind)\n");
368 int lookup(nsd_file_t *rq)
372 struct winbindd_request *request;
374 nsd_logprintf(NSD_LOG_MIN, "entering lookup (winbind)\n");
378 map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
379 key = nsd_attr_fetch_string(rq->f_attrs, "key", (char*)0);
380 if (! map || ! key) {
381 nsd_logprintf(NSD_LOG_MIN, "lookup (winbind) table or key not defined\n");
382 rq->f_status = NS_BADREQ;
386 nsd_logprintf(NSD_LOG_MIN, "lookup (winbind %s)\n",map);
388 request = (struct winbindd_request *)nsd_calloc(1,sizeof(struct winbindd_request));
390 nsd_logprintf(NSD_LOG_RESOURCE,
391 "lookup (winbind): failed malloc\n");
395 if (strcasecmp(map,"passwd.byuid") == 0) {
396 request->data.uid = atoi(key);
397 rq->f_cmd_data = (void *)WINBINDD_GETPWNAM_FROM_UID;
398 } else if (strcasecmp(map,"passwd.byname") == 0) {
399 strncpy(request->data.username, key,
400 sizeof(request->data.username) - 1);
401 request->data.username[sizeof(request->data.username) - 1] = '\0';
402 rq->f_cmd_data = (void *)WINBINDD_GETPWNAM_FROM_USER;
403 } else if (strcasecmp(map,"group.byname") == 0) {
404 strncpy(request->data.groupname, key,
405 sizeof(request->data.groupname) - 1);
406 request->data.groupname[sizeof(request->data.groupname) - 1] = '\0';
407 rq->f_cmd_data = (void *)WINBINDD_GETGRNAM_FROM_GROUP;
408 } else if (strcasecmp(map,"group.bygid") == 0) {
409 request->data.gid = atoi(key);
410 rq->f_cmd_data = (void *)WINBINDD_GETGRNAM_FROM_GID;
413 * Don't understand this map - just return not found
415 nsd_logprintf(NSD_LOG_MIN, "lookup (winbind) unknown table\n");
417 rq->f_status = NS_NOTFOUND;
421 return(do_request(rq, request));
424 int list(nsd_file_t *rq)
428 nsd_logprintf(NSD_LOG_MIN, "entering list (winbind)\n");
432 map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
434 nsd_logprintf(NSD_LOG_MIN, "list (winbind) table not defined\n");
435 rq->f_status = NS_BADREQ;
439 nsd_logprintf(NSD_LOG_MIN, "list (winbind %s)\n",map);
441 return (do_list(0,rq));
445 do_list(int state, nsd_file_t *rq)
448 struct winbindd_request *request;
450 nsd_logprintf(NSD_LOG_MIN, "entering do_list (winbind) state = %d\n",state);
452 map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
453 request = (struct winbindd_request *)nsd_calloc(1,sizeof(struct winbindd_request));
455 nsd_logprintf(NSD_LOG_RESOURCE,
456 "do_list (winbind): failed malloc\n");
460 if (strcasecmp(map,"passwd.byname") == 0) {
463 rq->f_cmd_data = (void *)WINBINDD_SETPWENT;
466 request->data.num_entries = MAX_GETPWENT_USERS;
467 rq->f_cmd_data = (void *)WINBINDD_GETPWENT;
470 rq->f_cmd_data = (void *)WINBINDD_ENDPWENT;
473 nsd_logprintf(NSD_LOG_MIN, "do_list (winbind) unknown state\n");
475 rq->f_status = NS_NOTFOUND;
478 } else if (strcasecmp(map,"group.byname") == 0) {
481 rq->f_cmd_data = (void *)WINBINDD_SETGRENT;
484 request->data.num_entries = MAX_GETGRENT_USERS;
485 rq->f_cmd_data = (void *)WINBINDD_GETGRENT;
488 rq->f_cmd_data = (void *)WINBINDD_ENDGRENT;
491 nsd_logprintf(NSD_LOG_MIN, "do_list (winbind) unknown state\n");
493 rq->f_status = NS_NOTFOUND;
498 * Don't understand this map - just return not found
500 nsd_logprintf(NSD_LOG_MIN, "do_list (winbind) unknown table\n");
502 rq->f_status = NS_NOTFOUND;
506 return(do_request(rq, request));
511 /* Allocate some space from the nss static buffer. The buffer and buflen
512 are the pointers passed in by the C library to the _nss_ntdom_*
515 static char *get_static(char **buffer, int *buflen, int len)
519 /* Error check. We return false if things aren't set up right, or
520 there isn't enough buffer space left. */
522 if ((buffer == NULL) || (buflen == NULL) || (*buflen < len)) {
526 /* Return an index into the static buffer */
535 /* I've copied the strtok() replacement function next_token() from
536 lib/util_str.c as I really don't want to have to link in any other
537 objects if I can possibly avoid it. */
539 BOOL next_token(char **ptr,char *buff,char *sep, size_t bufsize)
545 if (!ptr) return(False);
549 /* default to simple separators */
550 if (!sep) sep = " \t\n\r";
552 /* find the first non sep char */
553 while (*s && strchr(sep,*s)) s++;
556 if (! *s) return(False);
558 /* copy over the token */
559 for (quoted = False; len < bufsize && *s && (quoted || !strchr(sep,*s)); s++) {
568 *ptr = (*s) ? s+1 : s;
575 /* Fill a pwent structure from a winbindd_response structure. We use
576 the static data passed to us by libc to put strings and stuff in.
577 Return NSS_STATUS_TRYAGAIN if we run out of memory. */
579 static NSS_STATUS fill_pwent(struct passwd *result,
580 struct winbindd_pw *pw,
581 char **buffer, int *buflen)
585 if ((result->pw_name =
586 get_static(buffer, buflen, strlen(pw->pw_name) + 1)) == NULL) {
590 return NSS_STATUS_TRYAGAIN;
593 strcpy(result->pw_name, pw->pw_name);
597 if ((result->pw_passwd =
598 get_static(buffer, buflen, strlen(pw->pw_passwd) + 1)) == NULL) {
602 return NSS_STATUS_TRYAGAIN;
605 strcpy(result->pw_passwd, pw->pw_passwd);
609 result->pw_uid = pw->pw_uid;
610 result->pw_gid = pw->pw_gid;
614 if ((result->pw_gecos =
615 get_static(buffer, buflen, strlen(pw->pw_gecos) + 1)) == NULL) {
619 return NSS_STATUS_TRYAGAIN;
622 strcpy(result->pw_gecos, pw->pw_gecos);
626 if ((result->pw_dir =
627 get_static(buffer, buflen, strlen(pw->pw_dir) + 1)) == NULL) {
631 return NSS_STATUS_TRYAGAIN;
634 strcpy(result->pw_dir, pw->pw_dir);
638 if ((result->pw_shell =
639 get_static(buffer, buflen, strlen(pw->pw_shell) + 1)) == NULL) {
643 return NSS_STATUS_TRYAGAIN;
646 strcpy(result->pw_shell, pw->pw_shell);
648 return NSS_STATUS_SUCCESS;
651 /* Fill a grent structure from a winbindd_response structure. We use
652 the static data passed to us by libc to put strings and stuff in.
653 Return NSS_STATUS_TRYAGAIN if we run out of memory. */
655 static int fill_grent(struct group *result, struct winbindd_gr *gr,
656 char *gr_mem, char **buffer, int *buflen)
664 if ((result->gr_name =
665 get_static(buffer, buflen, strlen(gr->gr_name) + 1)) == NULL) {
669 return NSS_STATUS_TRYAGAIN;
672 strcpy(result->gr_name, gr->gr_name);
676 if ((result->gr_passwd =
677 get_static(buffer, buflen, strlen(gr->gr_passwd) + 1)) == NULL) {
681 return NSS_STATUS_TRYAGAIN;
684 strcpy(result->gr_passwd, gr->gr_passwd);
688 result->gr_gid = gr->gr_gid;
690 /* Group membership */
692 if ((gr->num_gr_mem < 0) || !gr_mem) {
696 /* this next value is a pointer to a pointer so let's align it */
698 /* Calculate number of extra bytes needed to align on pointer size boundry */
699 if ((i = (int)*buffer % sizeof(char*)) != 0)
700 i = sizeof(char*) - i;
702 if ((tst = get_static(buffer, buflen, ((gr->num_gr_mem + 1) *
703 sizeof(char *)+i))) == NULL) {
707 return NSS_STATUS_TRYAGAIN;
709 result->gr_mem = (char **)(tst + i);
711 if (gr->num_gr_mem == 0) {
715 *(result->gr_mem) = NULL;
716 return NSS_STATUS_SUCCESS;
719 /* Start looking at extra data */
723 while(next_token((char **)&gr_mem, name, ",", sizeof(fstring))) {
725 /* Allocate space for member */
727 if (((result->gr_mem)[i] =
728 get_static(buffer, buflen, strlen(name) + 1)) == NULL) {
732 return NSS_STATUS_TRYAGAIN;
735 strcpy((result->gr_mem)[i], name);
741 (result->gr_mem)[i] = NULL;
743 return NSS_STATUS_SUCCESS;
750 static struct winbindd_response getpwent_response;
752 static int ndx_pw_cache; /* Current index into pwd cache */
753 static int num_pw_cache; /* Current size of pwd cache */
755 /* Rewind "file pointer" to start of ntdom password database */
758 _nss_winbind_setpwent(void)
761 fprintf(stderr, "[%5d]: setpwent\n", getpid());
764 if (num_pw_cache > 0) {
765 ndx_pw_cache = num_pw_cache = 0;
766 free_response(&getpwent_response);
769 return winbindd_request(WINBINDD_SETPWENT, NULL, NULL);
772 /* Close ntdom password database "file pointer" */
775 _nss_winbind_endpwent(void)
778 fprintf(stderr, "[%5d]: endpwent\n", getpid());
781 if (num_pw_cache > 0) {
782 ndx_pw_cache = num_pw_cache = 0;
783 free_response(&getpwent_response);
786 return winbindd_request(WINBINDD_ENDPWENT, NULL, NULL);
789 /* Fetch the next password entry from ntdom password database */
792 _nss_winbind_getpwent_r(struct passwd *result, char *buffer,
793 size_t buflen, int *errnop)
796 struct winbindd_request request;
797 static int called_again;
800 fprintf(stderr, "[%5d]: getpwent\n", getpid());
803 /* Return an entry from the cache if we have one, or if we are
804 called again because we exceeded our static buffer. */
806 if ((ndx_pw_cache < num_pw_cache) || called_again) {
810 /* Else call winbindd to get a bunch of entries */
812 if (num_pw_cache > 0) {
813 free_response(&getpwent_response);
816 ZERO_STRUCT(request);
817 ZERO_STRUCT(getpwent_response);
819 request.data.num_entries = MAX_GETPWENT_USERS;
821 ret = winbindd_request(WINBINDD_GETPWENT, &request,
824 if (ret == NSS_STATUS_SUCCESS) {
825 struct winbindd_pw *pw_cache;
830 num_pw_cache = getpwent_response.data.num_entries;
832 /* Return a result */
836 pw_cache = getpwent_response.extra_data;
838 /* Check data is valid */
840 if (pw_cache == NULL) {
841 return NSS_STATUS_NOTFOUND;
844 ret = fill_pwent(result, &pw_cache[ndx_pw_cache],
847 /* Out of memory - try again */
849 if (ret == NSS_STATUS_TRYAGAIN) {
851 *errnop = errno = ERANGE;
856 called_again = False;
859 /* If we've finished with this lot of results free cache */
861 if (ndx_pw_cache == num_pw_cache) {
862 ndx_pw_cache = num_pw_cache = 0;
863 free_response(&getpwent_response);
870 /* Return passwd struct from uid */
873 _nss_winbind_getpwuid_r(uid_t uid, struct passwd *result, char *buffer,
874 size_t buflen, int *errnop)
877 static struct winbindd_response response;
878 struct winbindd_request request;
879 static int keep_response=0;
881 /* If our static buffer needs to be expanded we are called again */
882 if (!keep_response) {
884 /* Call for the first time */
886 ZERO_STRUCT(response);
887 ZERO_STRUCT(request);
889 request.data.uid = uid;
891 ret = winbindd_request(WINBINDD_GETPWUID, &request, &response);
893 if (ret == NSS_STATUS_SUCCESS) {
894 ret = fill_pwent(result, &response.data.pw,
897 if (ret == NSS_STATUS_TRYAGAIN) {
898 keep_response = True;
899 *errnop = errno = ERANGE;
906 /* We've been called again */
908 ret = fill_pwent(result, &response.data.pw, &buffer, &buflen);
910 if (ret == NSS_STATUS_TRYAGAIN) {
911 keep_response = True;
912 *errnop = errno = ERANGE;
916 keep_response = False;
920 free_response(&response);
924 /* Return passwd struct from username */
927 _nss_winbind_getpwnam_r(const char *name, struct passwd *result, char *buffer,
928 size_t buflen, int *errnop)
931 static struct winbindd_response response;
932 struct winbindd_request request;
933 static int keep_response;
936 fprintf(stderr, "[%5d]: getpwnam %s\n", getpid(), name);
939 /* If our static buffer needs to be expanded we are called again */
941 if (!keep_response) {
943 /* Call for the first time */
945 ZERO_STRUCT(response);
946 ZERO_STRUCT(request);
948 strncpy(request.data.username, name,
949 sizeof(request.data.username) - 1);
950 request.data.username
951 [sizeof(request.data.username) - 1] = '\0';
953 ret = winbindd_request(WINBINDD_GETPWNAM, &request, &response);
955 if (ret == NSS_STATUS_SUCCESS) {
956 ret = fill_pwent(result, &response.data.pw, &buffer,
959 if (ret == NSS_STATUS_TRYAGAIN) {
960 keep_response = True;
961 *errnop = errno = ERANGE;
968 /* We've been called again */
970 ret = fill_pwent(result, &response.data.pw, &buffer, &buflen);
972 if (ret == NSS_STATUS_TRYAGAIN) {
973 keep_response = True;
974 *errnop = errno = ERANGE;
978 keep_response = False;
982 free_response(&response);
987 * NSS group functions
990 static struct winbindd_response getgrent_response;
992 static int ndx_gr_cache; /* Current index into grp cache */
993 static int num_gr_cache; /* Current size of grp cache */
995 /* Rewind "file pointer" to start of ntdom group database */
998 _nss_winbind_setgrent(void)
1001 fprintf(stderr, "[%5d]: setgrent\n", getpid());
1004 if (num_gr_cache > 0) {
1005 ndx_gr_cache = num_gr_cache = 0;
1006 free_response(&getgrent_response);
1009 return winbindd_request(WINBINDD_SETGRENT, NULL, NULL);
1012 /* Close "file pointer" for ntdom group database */
1015 _nss_winbind_endgrent(void)
1018 fprintf(stderr, "[%5d]: endgrent\n", getpid());
1021 if (num_gr_cache > 0) {
1022 ndx_gr_cache = num_gr_cache = 0;
1023 free_response(&getgrent_response);
1026 return winbindd_request(WINBINDD_ENDGRENT, NULL, NULL);
1029 /* Get next entry from ntdom group database */
1032 _nss_winbind_getgrent_r(struct group *result,
1033 char *buffer, size_t buflen, int *errnop)
1036 static struct winbindd_request request;
1037 static int called_again;
1040 fprintf(stderr, "[%5d]: getgrent\n", getpid());
1043 /* Return an entry from the cache if we have one, or if we are
1044 called again because we exceeded our static buffer. */
1046 if ((ndx_gr_cache < num_gr_cache) || called_again) {
1050 /* Else call winbindd to get a bunch of entries */
1052 if (num_gr_cache > 0) {
1053 free_response(&getgrent_response);
1056 ZERO_STRUCT(request);
1057 ZERO_STRUCT(getgrent_response);
1059 request.data.num_entries = MAX_GETGRENT_USERS;
1061 ret = winbindd_request(WINBINDD_GETGRENT, &request,
1062 &getgrent_response);
1064 if (ret == NSS_STATUS_SUCCESS) {
1065 struct winbindd_gr *gr_cache;
1071 num_gr_cache = getgrent_response.data.num_entries;
1073 /* Return a result */
1077 gr_cache = getgrent_response.extra_data;
1079 /* Check data is valid */
1081 if (gr_cache == NULL) {
1082 return NSS_STATUS_NOTFOUND;
1085 /* Fill group membership. The offset into the extra data
1086 for the group membership is the reported offset plus the
1087 size of all the winbindd_gr records returned. */
1089 mem_ofs = gr_cache[ndx_gr_cache].gr_mem_ofs +
1090 num_gr_cache * sizeof(struct winbindd_gr);
1092 ret = fill_grent(result, &gr_cache[ndx_gr_cache],
1093 ((char *)getgrent_response.extra_data)+mem_ofs,
1096 /* Out of memory - try again */
1098 if (ret == NSS_STATUS_TRYAGAIN) {
1099 called_again = True;
1100 *errnop = errno = ERANGE;
1105 called_again = False;
1108 /* If we've finished with this lot of results free cache */
1110 if (ndx_gr_cache == num_gr_cache) {
1111 ndx_gr_cache = num_gr_cache = 0;
1112 free_response(&getgrent_response);
1119 /* Return group struct from group name */
1122 _nss_winbind_getgrnam_r(const char *name,
1123 struct group *result, char *buffer,
1124 size_t buflen, int *errnop)
1127 static struct winbindd_response response;
1128 struct winbindd_request request;
1129 static int keep_response;
1132 fprintf(stderr, "[%5d]: getgrnam %s\n", getpid(), name);
1135 /* If our static buffer needs to be expanded we are called again */
1137 if (!keep_response) {
1139 /* Call for the first time */
1141 ZERO_STRUCT(request);
1142 ZERO_STRUCT(response);
1144 strncpy(request.data.groupname, name,
1145 sizeof(request.data.groupname));
1146 request.data.groupname
1147 [sizeof(request.data.groupname) - 1] = '\0';
1149 ret = winbindd_request(WINBINDD_GETGRNAM, &request, &response);
1151 if (ret == NSS_STATUS_SUCCESS) {
1152 ret = fill_grent(result, &response.data.gr,
1153 response.extra_data,
1156 if (ret == NSS_STATUS_TRYAGAIN) {
1157 keep_response = True;
1158 *errnop = errno = ERANGE;
1165 /* We've been called again */
1167 ret = fill_grent(result, &response.data.gr,
1168 response.extra_data, &buffer, &buflen);
1170 if (ret == NSS_STATUS_TRYAGAIN) {
1171 keep_response = True;
1172 *errnop = errno = ERANGE;
1176 keep_response = False;
1180 free_response(&response);
1184 /* Return group struct from gid */
1187 _nss_winbind_getgrgid_r(gid_t gid,
1188 struct group *result, char *buffer,
1189 size_t buflen, int *errnop)
1192 static struct winbindd_response response;
1193 struct winbindd_request request;
1194 static int keep_response;
1197 fprintf(stderr, "[%5d]: getgrgid %d\n", getpid(), gid);
1200 /* If our static buffer needs to be expanded we are called again */
1202 if (!keep_response) {
1204 /* Call for the first time */
1206 ZERO_STRUCT(request);
1207 ZERO_STRUCT(response);
1209 request.data.gid = gid;
1211 ret = winbindd_request(WINBINDD_GETGRGID, &request, &response);
1213 if (ret == NSS_STATUS_SUCCESS) {
1215 ret = fill_grent(result, &response.data.gr,
1216 response.extra_data,
1219 if (ret == NSS_STATUS_TRYAGAIN) {
1220 keep_response = True;
1221 *errnop = errno = ERANGE;
1228 /* We've been called again */
1230 ret = fill_grent(result, &response.data.gr,
1231 response.extra_data, &buffer, &buflen);
1233 if (ret == NSS_STATUS_TRYAGAIN) {
1234 keep_response = True;
1235 *errnop = errno = ERANGE;
1239 keep_response = False;
1243 free_response(&response);
1247 /* Initialise supplementary groups */
1250 _nss_winbind_initgroups_dyn(char *user, gid_t group, long int *start,
1251 long int *size, gid_t **groups, long int limit,
1255 struct winbindd_request request;
1256 struct winbindd_response response;
1260 fprintf(stderr, "[%5d]: initgroups %s (%d)\n", getpid(),
1264 ZERO_STRUCT(request);
1265 ZERO_STRUCT(response);
1267 strncpy(request.data.username, user,
1268 sizeof(request.data.username) - 1);
1270 ret = winbindd_request(WINBINDD_GETGROUPS, &request, &response);
1272 if (ret == NSS_STATUS_SUCCESS) {
1273 int num_gids = response.data.num_entries;
1274 gid_t *gid_list = (gid_t *)response.extra_data;
1276 /* Copy group list to client */
1278 for (i = 0; i < num_gids; i++) {
1280 /* Skip primary group */
1282 if (gid_list[i] == group) continue;
1286 if (*start == *size && limit <= 0) {
1287 (*groups) = realloc(
1288 (*groups), (2 * (*size) + 1) * sizeof(**groups));
1289 if (! *groups) goto done;
1290 *size = 2 * (*size) + 1;
1293 if (*start == *size) goto done;
1295 (*groups)[*start] = gid_list[i];
1298 /* Filled buffer? */
1300 if (*start == limit) goto done;
1304 /* Back to your regularly scheduled programming */