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;
207 switch ((int)rq->f_cmd_data) {
208 case WINBINDD_GETPWUID:
209 case WINBINDD_GETPWNAM:
210 snprintf(result,1023,"%s:%s:%d:%d:%s:%s:%s\n",
219 case WINBINDD_GETGRNAM:
220 case WINBINDD_GETGRGID:
221 if (gr->num_gr_mem && response.extra_data)
222 members = response.extra_data;
225 snprintf(result,1023,"%s:%s:%d:%s\n",
226 gr->gr_name, gr->gr_passwd, gr->gr_gid, members);
228 case WINBINDD_SETGRENT:
229 case WINBINDD_SETPWENT:
230 nsd_logprintf(NSD_LOG_MIN, "callback (winbind) - SETPWENT/SETGRENT\n");
231 free_response(&response);
232 return(do_list(1,rq));
233 case WINBINDD_GETGRENT:
234 nsd_logprintf(NSD_LOG_MIN,
235 "callback (winbind) - %d GETGRENT responses\n",
236 response.data.num_entries);
237 if (response.data.num_entries) {
238 gr = (struct winbindd_gr *)response.extra_data;
240 nsd_logprintf(NSD_LOG_MIN, " no extra_data\n");
241 free_response(&response);
244 members = (char *)response.extra_data +
245 (response.data.num_entries * sizeof(struct winbindd_gr));
246 for (i = 0; i < response.data.num_entries; i++) {
247 snprintf(result,1023,"%s:%s:%d:%s\n",
248 gr->gr_name, gr->gr_passwd, gr->gr_gid,
249 &members[gr->gr_mem_ofs]);
250 nsd_logprintf(NSD_LOG_MIN, " GETGRENT %s\n",result);
251 nsd_append_element(rq,NS_SUCCESS,result,strlen(result));
255 i = response.data.num_entries;
256 free_response(&response);
257 if (i < MAX_GETPWENT_USERS)
258 return(do_list(2,rq));
260 return(do_list(1,rq));
261 case WINBINDD_GETPWENT:
262 nsd_logprintf(NSD_LOG_MIN,
263 "callback (winbind) - %d GETPWENT responses\n",
264 response.data.num_entries);
265 if (response.data.num_entries) {
266 pw = (struct winbindd_pw *)response.extra_data;
268 nsd_logprintf(NSD_LOG_MIN, " no extra_data\n");
269 free_response(&response);
272 for (i = 0; i < response.data.num_entries; i++) {
273 snprintf(result,1023,"%s:%s:%d:%d:%s:%s:%s",
281 nsd_logprintf(NSD_LOG_MIN, " GETPWENT %s\n",result);
282 nsd_append_element(rq,NS_SUCCESS,result,strlen(result));
286 i = response.data.num_entries;
287 free_response(&response);
288 if (i < MAX_GETPWENT_USERS)
289 return(do_list(2,rq));
291 return(do_list(1,rq));
292 case WINBINDD_ENDGRENT:
293 case WINBINDD_ENDPWENT:
294 nsd_logprintf(NSD_LOG_MIN, "callback (winbind) - ENDPWENT/ENDGRENT\n");
295 nsd_append_element(rq,NS_SUCCESS,"\n",1);
296 free_response(&response);
299 free_response(&response);
300 nsd_logprintf(NSD_LOG_MIN, "callback (winbind) - no valid command\n");
303 nsd_logprintf(NSD_LOG_MIN, "callback (winbind) %s\n", result);
304 /* free any extra data area in response structure */
305 free_response(&response);
306 nsd_set_result(rq,NS_SUCCESS,result,strlen(result),VOLATILE);
311 winbind_timeout(nsd_file_t **rqp, nsd_times_t *to)
317 nsd_logprintf(NSD_LOG_MIN, "timeout (winbind)\n");
322 /* Remove the callback and timeout */
323 nsd_callback_remove(winbindd_fd);
324 nsd_timeout_remove(rq);
326 rq->f_status = NS_NOTFOUND;
331 send_next_request(nsd_file_t *rq, struct winbindd_request *request)
338 nsd_logprintf(NSD_LOG_MIN, "send_next_request (winbind) %d to = %d\n",
339 rq->f_cmd_data, timeout);
340 status = winbindd_send_request((int)rq->f_cmd_data,request);
343 if (status != NSS_STATUS_SUCCESS) {
344 nsd_logprintf(NSD_LOG_MIN,
345 "send_next_request (winbind) error status = %d\n",status);
346 rq->f_status = status;
353 * Set up callback and timeouts
355 nsd_logprintf(NSD_LOG_MIN, "send_next_request (winbind) fd = %d\n",winbindd_fd);
356 nsd_callback_new(winbindd_fd,winbind_callback,NSD_READ);
357 nsd_timeout_new(rq,timeout,winbind_timeout,(void *)0);
363 nsd_logprintf(NSD_LOG_MIN, "entering init (winbind)\n");
367 int lookup(nsd_file_t *rq)
371 struct winbindd_request *request;
373 nsd_logprintf(NSD_LOG_MIN, "entering lookup (winbind)\n");
377 map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
378 key = nsd_attr_fetch_string(rq->f_attrs, "key", (char*)0);
379 if (! map || ! key) {
380 nsd_logprintf(NSD_LOG_MIN, "lookup (winbind) table or key not defined\n");
381 rq->f_status = NS_BADREQ;
385 nsd_logprintf(NSD_LOG_MIN, "lookup (winbind %s)\n",map);
387 request = (struct winbindd_request *)nsd_calloc(1,sizeof(struct winbindd_request));
389 nsd_logprintf(NSD_LOG_RESOURCE,
390 "lookup (winbind): failed malloc\n");
394 if (strcasecmp(map,"passwd.byuid") == 0) {
395 request->data.uid = atoi(key);
396 rq->f_cmd_data = (void *)WINBINDD_GETPWUID;
397 } else if (strcasecmp(map,"passwd.byname") == 0) {
398 strncpy(request->data.username, key,
399 sizeof(request->data.username) - 1);
400 request->data.username[sizeof(request->data.username) - 1] = '\0';
401 rq->f_cmd_data = (void *)WINBINDD_GETPWNAM;
402 } else if (strcasecmp(map,"group.byname") == 0) {
403 strncpy(request->data.groupname, key,
404 sizeof(request->data.groupname) - 1);
405 request->data.groupname[sizeof(request->data.groupname) - 1] = '\0';
406 rq->f_cmd_data = (void *)WINBINDD_GETGRNAM;
407 } else if (strcasecmp(map,"group.bygid") == 0) {
408 request->data.gid = atoi(key);
409 rq->f_cmd_data = (void *)WINBINDD_GETGRGID;
412 * Don't understand this map - just return not found
414 nsd_logprintf(NSD_LOG_MIN, "lookup (winbind) unknown table\n");
416 rq->f_status = NS_NOTFOUND;
420 return(do_request(rq, request));
423 int list(nsd_file_t *rq)
427 nsd_logprintf(NSD_LOG_MIN, "entering list (winbind)\n");
431 map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
433 nsd_logprintf(NSD_LOG_MIN, "list (winbind) table not defined\n");
434 rq->f_status = NS_BADREQ;
438 nsd_logprintf(NSD_LOG_MIN, "list (winbind %s)\n",map);
440 return (do_list(0,rq));
444 do_list(int state, nsd_file_t *rq)
447 struct winbindd_request *request;
449 nsd_logprintf(NSD_LOG_MIN, "entering do_list (winbind) state = %d\n",state);
451 map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
452 request = (struct winbindd_request *)nsd_calloc(1,sizeof(struct winbindd_request));
454 nsd_logprintf(NSD_LOG_RESOURCE,
455 "do_list (winbind): failed malloc\n");
459 if (strcasecmp(map,"passwd.byname") == 0) {
462 rq->f_cmd_data = (void *)WINBINDD_SETPWENT;
465 request->data.num_entries = MAX_GETPWENT_USERS;
466 rq->f_cmd_data = (void *)WINBINDD_GETPWENT;
469 rq->f_cmd_data = (void *)WINBINDD_ENDPWENT;
472 nsd_logprintf(NSD_LOG_MIN, "do_list (winbind) unknown state\n");
474 rq->f_status = NS_NOTFOUND;
477 } else if (strcasecmp(map,"group.byname") == 0) {
480 rq->f_cmd_data = (void *)WINBINDD_SETGRENT;
483 request->data.num_entries = MAX_GETGRENT_USERS;
484 rq->f_cmd_data = (void *)WINBINDD_GETGRENT;
487 rq->f_cmd_data = (void *)WINBINDD_ENDGRENT;
490 nsd_logprintf(NSD_LOG_MIN, "do_list (winbind) unknown state\n");
492 rq->f_status = NS_NOTFOUND;
497 * Don't understand this map - just return not found
499 nsd_logprintf(NSD_LOG_MIN, "do_list (winbind) unknown table\n");
501 rq->f_status = NS_NOTFOUND;
505 return(do_request(rq, request));
510 /* Allocate some space from the nss static buffer. The buffer and buflen
511 are the pointers passed in by the C library to the _nss_ntdom_*
514 static char *get_static(char **buffer, int *buflen, int len)
518 /* Error check. We return false if things aren't set up right, or
519 there isn't enough buffer space left. */
521 if ((buffer == NULL) || (buflen == NULL) || (*buflen < len)) {
525 /* Return an index into the static buffer */
534 /* I've copied the strtok() replacement function next_token() from
535 lib/util_str.c as I really don't want to have to link in any other
536 objects if I can possibly avoid it. */
538 BOOL next_token(char **ptr,char *buff,char *sep, size_t bufsize)
544 if (!ptr) return(False);
548 /* default to simple separators */
549 if (!sep) sep = " \t\n\r";
551 /* find the first non sep char */
552 while (*s && strchr(sep,*s)) s++;
555 if (! *s) return(False);
557 /* copy over the token */
558 for (quoted = False; len < bufsize && *s && (quoted || !strchr(sep,*s)); s++) {
567 *ptr = (*s) ? s+1 : s;
574 /* Fill a pwent structure from a winbindd_response structure. We use
575 the static data passed to us by libc to put strings and stuff in.
576 Return NSS_STATUS_TRYAGAIN if we run out of memory. */
578 static NSS_STATUS fill_pwent(struct passwd *result,
579 struct winbindd_pw *pw,
580 char **buffer, int *buflen)
584 if ((result->pw_name =
585 get_static(buffer, buflen, strlen(pw->pw_name) + 1)) == NULL) {
589 return NSS_STATUS_TRYAGAIN;
592 strcpy(result->pw_name, pw->pw_name);
596 if ((result->pw_passwd =
597 get_static(buffer, buflen, strlen(pw->pw_passwd) + 1)) == NULL) {
601 return NSS_STATUS_TRYAGAIN;
604 strcpy(result->pw_passwd, pw->pw_passwd);
608 result->pw_uid = pw->pw_uid;
609 result->pw_gid = pw->pw_gid;
613 if ((result->pw_gecos =
614 get_static(buffer, buflen, strlen(pw->pw_gecos) + 1)) == NULL) {
618 return NSS_STATUS_TRYAGAIN;
621 strcpy(result->pw_gecos, pw->pw_gecos);
625 if ((result->pw_dir =
626 get_static(buffer, buflen, strlen(pw->pw_dir) + 1)) == NULL) {
630 return NSS_STATUS_TRYAGAIN;
633 strcpy(result->pw_dir, pw->pw_dir);
637 if ((result->pw_shell =
638 get_static(buffer, buflen, strlen(pw->pw_shell) + 1)) == NULL) {
642 return NSS_STATUS_TRYAGAIN;
645 strcpy(result->pw_shell, pw->pw_shell);
647 return NSS_STATUS_SUCCESS;
650 /* Fill a grent structure from a winbindd_response structure. We use
651 the static data passed to us by libc to put strings and stuff in.
652 Return NSS_STATUS_TRYAGAIN if we run out of memory. */
654 static int fill_grent(struct group *result, struct winbindd_gr *gr,
655 char *gr_mem, char **buffer, int *buflen)
663 if ((result->gr_name =
664 get_static(buffer, buflen, strlen(gr->gr_name) + 1)) == NULL) {
668 return NSS_STATUS_TRYAGAIN;
671 strcpy(result->gr_name, gr->gr_name);
675 if ((result->gr_passwd =
676 get_static(buffer, buflen, strlen(gr->gr_passwd) + 1)) == NULL) {
680 return NSS_STATUS_TRYAGAIN;
683 strcpy(result->gr_passwd, gr->gr_passwd);
687 result->gr_gid = gr->gr_gid;
689 /* Group membership */
691 if ((gr->num_gr_mem < 0) || !gr_mem) {
695 /* this next value is a pointer to a pointer so let's align it */
697 /* Calculate number of extra bytes needed to align on pointer size boundry */
698 if ((i = (int)*buffer % sizeof(char*)) != 0)
699 i = sizeof(char*) - i;
701 if ((tst = get_static(buffer, buflen, ((gr->num_gr_mem + 1) *
702 sizeof(char *)+i))) == NULL) {
706 return NSS_STATUS_TRYAGAIN;
708 result->gr_mem = (char **)(tst + i);
710 if (gr->num_gr_mem == 0) {
714 *(result->gr_mem) = NULL;
715 return NSS_STATUS_SUCCESS;
718 /* Start looking at extra data */
722 while(next_token((char **)&gr_mem, name, ",", sizeof(fstring))) {
724 /* Allocate space for member */
726 if (((result->gr_mem)[i] =
727 get_static(buffer, buflen, strlen(name) + 1)) == NULL) {
731 return NSS_STATUS_TRYAGAIN;
734 strcpy((result->gr_mem)[i], name);
740 (result->gr_mem)[i] = NULL;
742 return NSS_STATUS_SUCCESS;
749 static struct winbindd_response getpwent_response;
751 static int ndx_pw_cache; /* Current index into pwd cache */
752 static int num_pw_cache; /* Current size of pwd cache */
754 /* Rewind "file pointer" to start of ntdom password database */
757 _nss_winbind_setpwent(void)
760 fprintf(stderr, "[%5d]: setpwent\n", getpid());
763 if (num_pw_cache > 0) {
764 ndx_pw_cache = num_pw_cache = 0;
765 free_response(&getpwent_response);
768 return winbindd_request(WINBINDD_SETPWENT, NULL, NULL);
771 /* Close ntdom password database "file pointer" */
774 _nss_winbind_endpwent(void)
777 fprintf(stderr, "[%5d]: endpwent\n", getpid());
780 if (num_pw_cache > 0) {
781 ndx_pw_cache = num_pw_cache = 0;
782 free_response(&getpwent_response);
785 return winbindd_request(WINBINDD_ENDPWENT, NULL, NULL);
788 /* Fetch the next password entry from ntdom password database */
791 _nss_winbind_getpwent_r(struct passwd *result, char *buffer,
792 size_t buflen, int *errnop)
795 struct winbindd_request request;
796 static int called_again;
799 fprintf(stderr, "[%5d]: getpwent\n", getpid());
802 /* Return an entry from the cache if we have one, or if we are
803 called again because we exceeded our static buffer. */
805 if ((ndx_pw_cache < num_pw_cache) || called_again) {
809 /* Else call winbindd to get a bunch of entries */
811 if (num_pw_cache > 0) {
812 free_response(&getpwent_response);
815 ZERO_STRUCT(request);
816 ZERO_STRUCT(getpwent_response);
818 request.data.num_entries = MAX_GETPWENT_USERS;
820 ret = winbindd_request(WINBINDD_GETPWENT, &request,
823 if (ret == NSS_STATUS_SUCCESS) {
824 struct winbindd_pw *pw_cache;
829 num_pw_cache = getpwent_response.data.num_entries;
831 /* Return a result */
835 pw_cache = getpwent_response.extra_data;
837 /* Check data is valid */
839 if (pw_cache == NULL) {
840 return NSS_STATUS_NOTFOUND;
843 ret = fill_pwent(result, &pw_cache[ndx_pw_cache],
846 /* Out of memory - try again */
848 if (ret == NSS_STATUS_TRYAGAIN) {
850 *errnop = errno = ERANGE;
855 called_again = False;
858 /* If we've finished with this lot of results free cache */
860 if (ndx_pw_cache == num_pw_cache) {
861 ndx_pw_cache = num_pw_cache = 0;
862 free_response(&getpwent_response);
869 /* Return passwd struct from uid */
872 _nss_winbind_getpwuid_r(uid_t uid, struct passwd *result, char *buffer,
873 size_t buflen, int *errnop)
876 static struct winbindd_response response;
877 struct winbindd_request request;
878 static int keep_response=0;
880 /* If our static buffer needs to be expanded we are called again */
881 if (!keep_response) {
883 /* Call for the first time */
885 ZERO_STRUCT(response);
886 ZERO_STRUCT(request);
888 request.data.uid = uid;
890 ret = winbindd_request(WINBINDD_GETPWUID, &request, &response);
892 if (ret == NSS_STATUS_SUCCESS) {
893 ret = fill_pwent(result, &response.data.pw,
896 if (ret == NSS_STATUS_TRYAGAIN) {
897 keep_response = True;
898 *errnop = errno = ERANGE;
905 /* We've been called again */
907 ret = fill_pwent(result, &response.data.pw, &buffer, &buflen);
909 if (ret == NSS_STATUS_TRYAGAIN) {
910 keep_response = True;
911 *errnop = errno = ERANGE;
915 keep_response = False;
919 free_response(&response);
923 /* Return passwd struct from username */
926 _nss_winbind_getpwnam_r(const char *name, struct passwd *result, char *buffer,
927 size_t buflen, int *errnop)
930 static struct winbindd_response response;
931 struct winbindd_request request;
932 static int keep_response;
935 fprintf(stderr, "[%5d]: getpwnam %s\n", getpid(), name);
938 /* If our static buffer needs to be expanded we are called again */
940 if (!keep_response) {
942 /* Call for the first time */
944 ZERO_STRUCT(response);
945 ZERO_STRUCT(request);
947 strncpy(request.data.username, name,
948 sizeof(request.data.username) - 1);
949 request.data.username
950 [sizeof(request.data.username) - 1] = '\0';
952 ret = winbindd_request(WINBINDD_GETPWNAM, &request, &response);
954 if (ret == NSS_STATUS_SUCCESS) {
955 ret = fill_pwent(result, &response.data.pw, &buffer,
958 if (ret == NSS_STATUS_TRYAGAIN) {
959 keep_response = True;
960 *errnop = errno = ERANGE;
967 /* We've been called again */
969 ret = fill_pwent(result, &response.data.pw, &buffer, &buflen);
971 if (ret == NSS_STATUS_TRYAGAIN) {
972 keep_response = True;
973 *errnop = errno = ERANGE;
977 keep_response = False;
981 free_response(&response);
986 * NSS group functions
989 static struct winbindd_response getgrent_response;
991 static int ndx_gr_cache; /* Current index into grp cache */
992 static int num_gr_cache; /* Current size of grp cache */
994 /* Rewind "file pointer" to start of ntdom group database */
997 _nss_winbind_setgrent(void)
1000 fprintf(stderr, "[%5d]: setgrent\n", getpid());
1003 if (num_gr_cache > 0) {
1004 ndx_gr_cache = num_gr_cache = 0;
1005 free_response(&getgrent_response);
1008 return winbindd_request(WINBINDD_SETGRENT, NULL, NULL);
1011 /* Close "file pointer" for ntdom group database */
1014 _nss_winbind_endgrent(void)
1017 fprintf(stderr, "[%5d]: endgrent\n", getpid());
1020 if (num_gr_cache > 0) {
1021 ndx_gr_cache = num_gr_cache = 0;
1022 free_response(&getgrent_response);
1025 return winbindd_request(WINBINDD_ENDGRENT, NULL, NULL);
1028 /* Get next entry from ntdom group database */
1031 _nss_winbind_getgrent_r(struct group *result,
1032 char *buffer, size_t buflen, int *errnop)
1035 static struct winbindd_request request;
1036 static int called_again;
1039 fprintf(stderr, "[%5d]: getgrent\n", getpid());
1042 /* Return an entry from the cache if we have one, or if we are
1043 called again because we exceeded our static buffer. */
1045 if ((ndx_gr_cache < num_gr_cache) || called_again) {
1049 /* Else call winbindd to get a bunch of entries */
1051 if (num_gr_cache > 0) {
1052 free_response(&getgrent_response);
1055 ZERO_STRUCT(request);
1056 ZERO_STRUCT(getgrent_response);
1058 request.data.num_entries = MAX_GETGRENT_USERS;
1060 ret = winbindd_request(WINBINDD_GETGRENT, &request,
1061 &getgrent_response);
1063 if (ret == NSS_STATUS_SUCCESS) {
1064 struct winbindd_gr *gr_cache;
1070 num_gr_cache = getgrent_response.data.num_entries;
1072 /* Return a result */
1076 gr_cache = getgrent_response.extra_data;
1078 /* Check data is valid */
1080 if (gr_cache == NULL) {
1081 return NSS_STATUS_NOTFOUND;
1084 /* Fill group membership. The offset into the extra data
1085 for the group membership is the reported offset plus the
1086 size of all the winbindd_gr records returned. */
1088 mem_ofs = gr_cache[ndx_gr_cache].gr_mem_ofs +
1089 num_gr_cache * sizeof(struct winbindd_gr);
1091 ret = fill_grent(result, &gr_cache[ndx_gr_cache],
1092 ((char *)getgrent_response.extra_data)+mem_ofs,
1095 /* Out of memory - try again */
1097 if (ret == NSS_STATUS_TRYAGAIN) {
1098 called_again = True;
1099 *errnop = errno = ERANGE;
1104 called_again = False;
1107 /* If we've finished with this lot of results free cache */
1109 if (ndx_gr_cache == num_gr_cache) {
1110 ndx_gr_cache = num_gr_cache = 0;
1111 free_response(&getgrent_response);
1118 /* Return group struct from group name */
1121 _nss_winbind_getgrnam_r(const char *name,
1122 struct group *result, char *buffer,
1123 size_t buflen, int *errnop)
1126 static struct winbindd_response response;
1127 struct winbindd_request request;
1128 static int keep_response;
1131 fprintf(stderr, "[%5d]: getgrnam %s\n", getpid(), name);
1134 /* If our static buffer needs to be expanded we are called again */
1136 if (!keep_response) {
1138 /* Call for the first time */
1140 ZERO_STRUCT(request);
1141 ZERO_STRUCT(response);
1143 strncpy(request.data.groupname, name,
1144 sizeof(request.data.groupname));
1145 request.data.groupname
1146 [sizeof(request.data.groupname) - 1] = '\0';
1148 ret = winbindd_request(WINBINDD_GETGRNAM, &request, &response);
1150 if (ret == NSS_STATUS_SUCCESS) {
1151 ret = fill_grent(result, &response.data.gr,
1152 response.extra_data,
1155 if (ret == NSS_STATUS_TRYAGAIN) {
1156 keep_response = True;
1157 *errnop = errno = ERANGE;
1164 /* We've been called again */
1166 ret = fill_grent(result, &response.data.gr,
1167 response.extra_data, &buffer, &buflen);
1169 if (ret == NSS_STATUS_TRYAGAIN) {
1170 keep_response = True;
1171 *errnop = errno = ERANGE;
1175 keep_response = False;
1179 free_response(&response);
1183 /* Return group struct from gid */
1186 _nss_winbind_getgrgid_r(gid_t gid,
1187 struct group *result, char *buffer,
1188 size_t buflen, int *errnop)
1191 static struct winbindd_response response;
1192 struct winbindd_request request;
1193 static int keep_response;
1196 fprintf(stderr, "[%5d]: getgrgid %d\n", getpid(), gid);
1199 /* If our static buffer needs to be expanded we are called again */
1201 if (!keep_response) {
1203 /* Call for the first time */
1205 ZERO_STRUCT(request);
1206 ZERO_STRUCT(response);
1208 request.data.gid = gid;
1210 ret = winbindd_request(WINBINDD_GETGRGID, &request, &response);
1212 if (ret == NSS_STATUS_SUCCESS) {
1214 ret = fill_grent(result, &response.data.gr,
1215 response.extra_data,
1218 if (ret == NSS_STATUS_TRYAGAIN) {
1219 keep_response = True;
1220 *errnop = errno = ERANGE;
1227 /* We've been called again */
1229 ret = fill_grent(result, &response.data.gr,
1230 response.extra_data, &buffer, &buflen);
1232 if (ret == NSS_STATUS_TRYAGAIN) {
1233 keep_response = True;
1234 *errnop = errno = ERANGE;
1238 keep_response = False;
1242 free_response(&response);
1246 /* Initialise supplementary groups */
1249 _nss_winbind_initgroups_dyn(char *user, gid_t group, long int *start,
1250 long int *size, gid_t **groups, long int limit,
1254 struct winbindd_request request;
1255 struct winbindd_response response;
1259 fprintf(stderr, "[%5d]: initgroups %s (%d)\n", getpid(),
1263 ZERO_STRUCT(request);
1264 ZERO_STRUCT(response);
1266 strncpy(request.data.username, user,
1267 sizeof(request.data.username) - 1);
1269 ret = winbindd_request(WINBINDD_GETGROUPS, &request, &response);
1271 if (ret == NSS_STATUS_SUCCESS) {
1272 int num_gids = response.data.num_entries;
1273 gid_t *gid_list = (gid_t *)response.extra_data;
1275 /* Copy group list to client */
1277 for (i = 0; i < num_gids; i++) {
1279 /* Skip primary group */
1281 if (gid_list[i] == group) continue;
1285 if (*start == *size && limit <= 0) {
1286 (*groups) = realloc(
1287 (*groups), (2 * (*size) + 1) * sizeof(**groups));
1288 if (! *groups) goto done;
1289 *size = 2 * (*size) + 1;
1292 if (*start == *size) goto done;
1294 (*groups)[*start] = gid_list[i];
1297 /* Filled buffer? */
1299 if (*start == limit) goto done;
1303 /* Back to your regularly scheduled programming */