emulate/traffic: add sAMAccountName in create_group
[amitay/samba.git] / nsswitch / winbind_nss_linux.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Windows NT Domain nsswitch module
5
6    Copyright (C) Tim Potter 2000
7
8    This library is free software; you can redistribute it and/or
9    modify it under the terms of the GNU Lesser General Public
10    License as published by the Free Software Foundation; either
11    version 3 of the License, or (at your option) any later version.
12
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.
17
18    You should have received a copy of the GNU Lesser General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "winbind_client.h"
23
24 #if HAVE_PTHREAD_H
25 #include <pthread.h>
26 #endif
27
28 #if HAVE_PTHREAD
29 static pthread_mutex_t winbind_nss_mutex = PTHREAD_MUTEX_INITIALIZER;
30 #endif
31
32 /* Maximum number of users to pass back over the unix domain socket
33    per call. This is not a static limit on the total number of users
34    or groups returned in total. */
35
36 #define MAX_GETPWENT_USERS 250
37 #define MAX_GETGRENT_USERS 250
38
39 /*************************************************************************
40  ************************************************************************/
41
42 #ifdef DEBUG_NSS
43 static const char *nss_err_str(NSS_STATUS ret)
44 {
45         switch (ret) {
46                 case NSS_STATUS_TRYAGAIN:
47                         return "NSS_STATUS_TRYAGAIN";
48                 case NSS_STATUS_SUCCESS:
49                         return "NSS_STATUS_SUCCESS";
50                 case NSS_STATUS_NOTFOUND:
51                         return "NSS_STATUS_NOTFOUND";
52                 case NSS_STATUS_UNAVAIL:
53                         return "NSS_STATUS_UNAVAIL";
54 #ifdef NSS_STATUS_RETURN
55                 case NSS_STATUS_RETURN:
56                         return "NSS_STATUS_RETURN";
57 #endif
58                 default:
59                         return "UNKNOWN RETURN CODE!!!!!!!";
60         }
61 }
62 #endif
63
64 /* Prototypes from wb_common.c */
65
66 /* Allocate some space from the nss static buffer.  The buffer and buflen
67    are the pointers passed in by the C library to the _nss_ntdom_*
68    functions. */
69
70 static char *get_static(char **buffer, size_t *buflen, size_t len)
71 {
72         char *result;
73
74         /* Error check.  We return false if things aren't set up right, or
75            there isn't enough buffer space left. */
76
77         if ((buffer == NULL) || (buflen == NULL) || (*buflen < len)) {
78                 return NULL;
79         }
80
81         /* Return an index into the static buffer */
82
83         result = *buffer;
84         *buffer += len;
85         *buflen -= len;
86
87         return result;
88 }
89
90 /* I've copied the strtok() replacement function next_token_Xalloc() from
91    lib/util_str.c as I really don't want to have to link in any other
92    objects if I can possibly avoid it. */
93
94 static bool next_token_alloc(const char **ptr,
95                                 char **pp_buff,
96                                 const char *sep)
97 {
98         const char *s;
99         const char *saved_s;
100         char *pbuf;
101         bool quoted;
102         size_t len=1;
103
104         *pp_buff = NULL;
105         if (!ptr) {
106                 return(false);
107         }
108
109         s = *ptr;
110
111         /* default to simple separators */
112         if (!sep) {
113                 sep = " \t\n\r";
114         }
115
116         /* find the first non sep char */
117         while (*s && strchr(sep,*s)) {
118                 s++;
119         }
120
121         /* nothing left? */
122         if (!*s) {
123                 return false;
124         }
125
126         /* When restarting we need to go from here. */
127         saved_s = s;
128
129         /* Work out the length needed. */
130         for (quoted = false; *s &&
131                         (quoted || !strchr(sep,*s)); s++) {
132                 if (*s == '\"') {
133                         quoted = !quoted;
134                 } else {
135                         len++;
136                 }
137         }
138
139         /* We started with len = 1 so we have space for the nul. */
140         *pp_buff = (char *)malloc(len);
141         if (!*pp_buff) {
142                 return false;
143         }
144
145         /* copy over the token */
146         pbuf = *pp_buff;
147         s = saved_s;
148         for (quoted = false; *s &&
149                         (quoted || !strchr(sep,*s)); s++) {
150                 if ( *s == '\"' ) {
151                         quoted = !quoted;
152                 } else {
153                         *pbuf++ = *s;
154                 }
155         }
156
157         *ptr = (*s) ? s+1 : s;
158         *pbuf = 0;
159
160         return true;
161 }
162
163 /* Fill a pwent structure from a winbindd_response structure.  We use
164    the static data passed to us by libc to put strings and stuff in.
165    Return NSS_STATUS_TRYAGAIN if we run out of memory. */
166
167 static NSS_STATUS fill_pwent(struct passwd *result,
168                                   struct winbindd_pw *pw,
169                                   char **buffer, size_t *buflen)
170 {
171         size_t len;
172
173         /* User name */
174         len = strlen(pw->pw_name) + 1;
175
176         if ((result->pw_name =
177              get_static(buffer, buflen, len)) == NULL) {
178
179                 /* Out of memory */
180
181                 return NSS_STATUS_TRYAGAIN;
182         }
183
184         memcpy(result->pw_name, pw->pw_name, len);
185
186         /* Password */
187         len = strlen(pw->pw_passwd) + 1;
188
189         if ((result->pw_passwd =
190              get_static(buffer, buflen, len)) == NULL) {
191
192                 /* Out of memory */
193
194                 return NSS_STATUS_TRYAGAIN;
195         }
196
197         memcpy(result->pw_passwd, pw->pw_passwd, len);
198
199         /* [ug]id */
200
201         result->pw_uid = pw->pw_uid;
202         result->pw_gid = pw->pw_gid;
203
204         /* GECOS */
205         len = strlen(pw->pw_gecos) + 1;
206
207         if ((result->pw_gecos =
208              get_static(buffer, buflen, len)) == NULL) {
209
210                 /* Out of memory */
211
212                 return NSS_STATUS_TRYAGAIN;
213         }
214
215         memcpy(result->pw_gecos, pw->pw_gecos, len);
216
217         /* Home directory */
218         len = strlen(pw->pw_dir) + 1;
219
220         if ((result->pw_dir =
221              get_static(buffer, buflen, len)) == NULL) {
222
223                 /* Out of memory */
224
225                 return NSS_STATUS_TRYAGAIN;
226         }
227
228         memcpy(result->pw_dir, pw->pw_dir, len);
229
230         /* Logon shell */
231         len = strlen(pw->pw_shell) + 1;
232
233         if ((result->pw_shell =
234              get_static(buffer, buflen, len)) == NULL) {
235
236                 /* Out of memory */
237
238                 return NSS_STATUS_TRYAGAIN;
239         }
240
241         memcpy(result->pw_shell, pw->pw_shell, len);
242
243         /* The struct passwd for Solaris has some extra fields which must
244            be initialised or nscd crashes. */
245
246 #if HAVE_PASSWD_PW_COMMENT
247         result->pw_comment = "";
248 #endif
249
250 #if HAVE_PASSWD_PW_AGE
251         result->pw_age = "";
252 #endif
253
254         return NSS_STATUS_SUCCESS;
255 }
256
257 /* Fill a grent structure from a winbindd_response structure.  We use
258    the static data passed to us by libc to put strings and stuff in.
259    Return NSS_STATUS_TRYAGAIN if we run out of memory. */
260
261 static NSS_STATUS fill_grent(struct group *result, struct winbindd_gr *gr,
262                       const char *gr_mem, char **buffer, size_t *buflen)
263 {
264         char *name;
265         int i;
266         char *tst;
267         size_t len;
268
269         /* Group name */
270         len = strlen(gr->gr_name) + 1;
271
272         if ((result->gr_name =
273              get_static(buffer, buflen, len)) == NULL) {
274
275                 /* Out of memory */
276
277                 return NSS_STATUS_TRYAGAIN;
278         }
279
280         memcpy(result->gr_name, gr->gr_name, len);
281
282         /* Password */
283         len = strlen(gr->gr_passwd) + 1;
284
285         if ((result->gr_passwd =
286              get_static(buffer, buflen, len)) == NULL) {
287
288                 /* Out of memory */
289                 return NSS_STATUS_TRYAGAIN;
290         }
291
292         memcpy(result->gr_passwd, gr->gr_passwd, len);
293
294         /* gid */
295
296         result->gr_gid = gr->gr_gid;
297
298         /* Group membership */
299
300         if (!gr_mem) {
301                 gr->num_gr_mem = 0;
302         }
303
304         /* this next value is a pointer to a pointer so let's align it */
305
306         /* Calculate number of extra bytes needed to align on pointer size boundry */
307         if ((i = (unsigned long)(*buffer) % sizeof(char*)) != 0)
308                 i = sizeof(char*) - i;
309
310         if ((tst = get_static(buffer, buflen, ((gr->num_gr_mem + 1) *
311                                  sizeof(char *)+i))) == NULL) {
312
313                 /* Out of memory */
314
315                 return NSS_STATUS_TRYAGAIN;
316         }
317         result->gr_mem = (char **)(tst + i);
318
319         if (gr->num_gr_mem == 0) {
320
321                 /* Group is empty */
322
323                 *(result->gr_mem) = NULL;
324                 return NSS_STATUS_SUCCESS;
325         }
326
327         /* Start looking at extra data */
328
329         i = 0;
330
331         while(next_token_alloc((const char **)&gr_mem, &name, ",")) {
332                 /* Allocate space for member */
333                 len = strlen(name) + 1;
334
335                 if (((result->gr_mem)[i] =
336                      get_static(buffer, buflen, len)) == NULL) {
337                         free(name);
338                         /* Out of memory */
339                         return NSS_STATUS_TRYAGAIN;
340                 }
341                 memcpy((result->gr_mem)[i], name, len);
342                 free(name);
343                 i++;
344         }
345
346         /* Terminate list */
347
348         (result->gr_mem)[i] = NULL;
349
350         return NSS_STATUS_SUCCESS;
351 }
352
353 /*
354  * NSS user functions
355  */
356
357 static struct winbindd_response getpwent_response;
358
359 static int ndx_pw_cache;                 /* Current index into pwd cache */
360 static int num_pw_cache;                 /* Current size of pwd cache */
361
362 /* Rewind "file pointer" to start of ntdom password database */
363
364 NSS_STATUS
365 _nss_winbind_setpwent(void)
366 {
367         NSS_STATUS ret;
368 #ifdef DEBUG_NSS
369         fprintf(stderr, "[%5d]: setpwent\n", getpid());
370 #endif
371
372 #if HAVE_PTHREAD
373         pthread_mutex_lock(&winbind_nss_mutex);
374 #endif
375
376         if (num_pw_cache > 0) {
377                 ndx_pw_cache = num_pw_cache = 0;
378                 winbindd_free_response(&getpwent_response);
379         }
380
381         ret = winbindd_request_response(NULL, WINBINDD_SETPWENT, NULL, NULL);
382 #ifdef DEBUG_NSS
383         fprintf(stderr, "[%5d]: setpwent returns %s (%d)\n", getpid(),
384                 nss_err_str(ret), ret);
385 #endif
386
387 #if HAVE_PTHREAD
388         pthread_mutex_unlock(&winbind_nss_mutex);
389 #endif
390         return ret;
391 }
392
393 /* Close ntdom password database "file pointer" */
394
395 NSS_STATUS
396 _nss_winbind_endpwent(void)
397 {
398         NSS_STATUS ret;
399 #ifdef DEBUG_NSS
400         fprintf(stderr, "[%5d]: endpwent\n", getpid());
401 #endif
402
403 #if HAVE_PTHREAD
404         pthread_mutex_lock(&winbind_nss_mutex);
405 #endif
406
407         if (num_pw_cache > 0) {
408                 ndx_pw_cache = num_pw_cache = 0;
409                 winbindd_free_response(&getpwent_response);
410         }
411
412         ret = winbindd_request_response(NULL, WINBINDD_ENDPWENT, NULL, NULL);
413 #ifdef DEBUG_NSS
414         fprintf(stderr, "[%5d]: endpwent returns %s (%d)\n", getpid(),
415                 nss_err_str(ret), ret);
416 #endif
417
418 #if HAVE_PTHREAD
419         pthread_mutex_unlock(&winbind_nss_mutex);
420 #endif
421
422         return ret;
423 }
424
425 /* Fetch the next password entry from ntdom password database */
426
427 NSS_STATUS
428 _nss_winbind_getpwent_r(struct passwd *result, char *buffer,
429                         size_t buflen, int *errnop)
430 {
431         NSS_STATUS ret;
432         struct winbindd_request request;
433         static int called_again;
434
435 #ifdef DEBUG_NSS
436         fprintf(stderr, "[%5d]: getpwent\n", getpid());
437 #endif
438
439 #if HAVE_PTHREAD
440         pthread_mutex_lock(&winbind_nss_mutex);
441 #endif
442
443         /* Return an entry from the cache if we have one, or if we are
444            called again because we exceeded our static buffer.  */
445
446         if ((ndx_pw_cache < num_pw_cache) || called_again) {
447                 goto return_result;
448         }
449
450         /* Else call winbindd to get a bunch of entries */
451
452         if (num_pw_cache > 0) {
453                 winbindd_free_response(&getpwent_response);
454         }
455
456         ZERO_STRUCT(request);
457         ZERO_STRUCT(getpwent_response);
458
459         request.data.num_entries = MAX_GETPWENT_USERS;
460
461         ret = winbindd_request_response(NULL, WINBINDD_GETPWENT, &request,
462                                &getpwent_response);
463
464         if (ret == NSS_STATUS_SUCCESS) {
465                 struct winbindd_pw *pw_cache;
466
467                 /* Fill cache */
468
469                 ndx_pw_cache = 0;
470                 num_pw_cache = getpwent_response.data.num_entries;
471
472                 /* Return a result */
473
474         return_result:
475
476                 pw_cache = (struct winbindd_pw *)
477                         getpwent_response.extra_data.data;
478
479                 /* Check data is valid */
480
481                 if (pw_cache == NULL) {
482                         ret = NSS_STATUS_NOTFOUND;
483                         goto done;
484                 }
485
486                 ret = fill_pwent(result, &pw_cache[ndx_pw_cache],
487                                  &buffer, &buflen);
488
489                 /* Out of memory - try again */
490
491                 if (ret == NSS_STATUS_TRYAGAIN) {
492                         called_again = true;
493                         *errnop = errno = ERANGE;
494                         goto done;
495                 }
496
497                 *errnop = errno = 0;
498                 called_again = false;
499                 ndx_pw_cache++;
500
501                 /* If we've finished with this lot of results free cache */
502
503                 if (ndx_pw_cache == num_pw_cache) {
504                         ndx_pw_cache = num_pw_cache = 0;
505                         winbindd_free_response(&getpwent_response);
506                 }
507         }
508         done:
509 #ifdef DEBUG_NSS
510         fprintf(stderr, "[%5d]: getpwent returns %s (%d)\n", getpid(),
511                 nss_err_str(ret), ret);
512 #endif
513
514 #if HAVE_PTHREAD
515         pthread_mutex_unlock(&winbind_nss_mutex);
516 #endif
517         return ret;
518 }
519
520 /* Return passwd struct from uid */
521
522 NSS_STATUS
523 _nss_winbind_getpwuid_r(uid_t uid, struct passwd *result, char *buffer,
524                         size_t buflen, int *errnop)
525 {
526         NSS_STATUS ret;
527         static struct winbindd_response response;
528         struct winbindd_request request;
529         static int keep_response;
530
531 #ifdef DEBUG_NSS
532         fprintf(stderr, "[%5d]: getpwuid_r %d\n", getpid(), (unsigned int)uid);
533 #endif
534
535 #if HAVE_PTHREAD
536         pthread_mutex_lock(&winbind_nss_mutex);
537 #endif
538
539         /* If our static buffer needs to be expanded we are called again */
540         if (!keep_response || uid != response.data.pw.pw_uid) {
541
542                 /* Call for the first time */
543
544                 ZERO_STRUCT(response);
545                 ZERO_STRUCT(request);
546
547                 request.data.uid = uid;
548
549                 ret = winbindd_request_response(NULL, WINBINDD_GETPWUID, &request, &response);
550
551                 if (ret == NSS_STATUS_SUCCESS) {
552                         ret = fill_pwent(result, &response.data.pw,
553                                          &buffer, &buflen);
554
555                         if (ret == NSS_STATUS_TRYAGAIN) {
556                                 keep_response = true;
557                                 *errnop = errno = ERANGE;
558                                 goto done;
559                         }
560                 }
561
562         } else {
563
564                 /* We've been called again */
565
566                 ret = fill_pwent(result, &response.data.pw, &buffer, &buflen);
567
568                 if (ret == NSS_STATUS_TRYAGAIN) {
569                         *errnop = errno = ERANGE;
570                         goto done;
571                 }
572
573                 keep_response = false;
574                 *errnop = errno = 0;
575         }
576
577         winbindd_free_response(&response);
578
579         done:
580
581 #ifdef DEBUG_NSS
582         fprintf(stderr, "[%5d]: getpwuid %d returns %s (%d)\n", getpid(),
583                 (unsigned int)uid, nss_err_str(ret), ret);
584 #endif
585
586 #if HAVE_PTHREAD
587         pthread_mutex_unlock(&winbind_nss_mutex);
588 #endif
589
590         return ret;
591 }
592
593 /* Return passwd struct from username */
594 NSS_STATUS
595 _nss_winbind_getpwnam_r(const char *name, struct passwd *result, char *buffer,
596                         size_t buflen, int *errnop)
597 {
598         NSS_STATUS ret;
599         static struct winbindd_response response;
600         struct winbindd_request request;
601         static int keep_response;
602
603 #ifdef DEBUG_NSS
604         fprintf(stderr, "[%5d]: getpwnam_r %s\n", getpid(), name);
605 #endif
606
607 #if HAVE_PTHREAD
608         pthread_mutex_lock(&winbind_nss_mutex);
609 #endif
610
611         /* If our static buffer needs to be expanded we are called again */
612
613         if (!keep_response || strcmp(name,response.data.pw.pw_name) != 0) {
614
615                 /* Call for the first time */
616
617                 ZERO_STRUCT(response);
618                 ZERO_STRUCT(request);
619
620                 strncpy(request.data.username, name,
621                         sizeof(request.data.username) - 1);
622                 request.data.username
623                         [sizeof(request.data.username) - 1] = '\0';
624
625                 ret = winbindd_request_response(NULL, WINBINDD_GETPWNAM, &request, &response);
626
627                 if (ret == NSS_STATUS_SUCCESS) {
628                         ret = fill_pwent(result, &response.data.pw, &buffer,
629                                          &buflen);
630
631                         if (ret == NSS_STATUS_TRYAGAIN) {
632                                 keep_response = true;
633                                 *errnop = errno = ERANGE;
634                                 goto done;
635                         }
636                 }
637
638         } else {
639
640                 /* We've been called again */
641
642                 ret = fill_pwent(result, &response.data.pw, &buffer, &buflen);
643
644                 if (ret == NSS_STATUS_TRYAGAIN) {
645                         keep_response = true;
646                         *errnop = errno = ERANGE;
647                         goto done;
648                 }
649
650                 keep_response = false;
651                 *errnop = errno = 0;
652         }
653
654         winbindd_free_response(&response);
655         done:
656 #ifdef DEBUG_NSS
657         fprintf(stderr, "[%5d]: getpwnam %s returns %s (%d)\n", getpid(),
658                 name, nss_err_str(ret), ret);
659 #endif
660
661 #if HAVE_PTHREAD
662         pthread_mutex_unlock(&winbind_nss_mutex);
663 #endif
664
665         return ret;
666 }
667
668 /*
669  * NSS group functions
670  */
671
672 static struct winbindd_response getgrent_response;
673
674 static int ndx_gr_cache;                 /* Current index into grp cache */
675 static int num_gr_cache;                 /* Current size of grp cache */
676
677 /* Rewind "file pointer" to start of ntdom group database */
678
679 NSS_STATUS
680 _nss_winbind_setgrent(void)
681 {
682         NSS_STATUS ret;
683 #ifdef DEBUG_NSS
684         fprintf(stderr, "[%5d]: setgrent\n", getpid());
685 #endif
686
687 #if HAVE_PTHREAD
688         pthread_mutex_lock(&winbind_nss_mutex);
689 #endif
690
691         if (num_gr_cache > 0) {
692                 ndx_gr_cache = num_gr_cache = 0;
693                 winbindd_free_response(&getgrent_response);
694         }
695
696         ret = winbindd_request_response(NULL, WINBINDD_SETGRENT, NULL, NULL);
697 #ifdef DEBUG_NSS
698         fprintf(stderr, "[%5d]: setgrent returns %s (%d)\n", getpid(),
699                 nss_err_str(ret), ret);
700 #endif
701
702 #if HAVE_PTHREAD
703         pthread_mutex_unlock(&winbind_nss_mutex);
704 #endif
705
706         return ret;
707 }
708
709 /* Close "file pointer" for ntdom group database */
710
711 NSS_STATUS
712 _nss_winbind_endgrent(void)
713 {
714         NSS_STATUS ret;
715 #ifdef DEBUG_NSS
716         fprintf(stderr, "[%5d]: endgrent\n", getpid());
717 #endif
718
719 #if HAVE_PTHREAD
720         pthread_mutex_lock(&winbind_nss_mutex);
721 #endif
722
723         if (num_gr_cache > 0) {
724                 ndx_gr_cache = num_gr_cache = 0;
725                 winbindd_free_response(&getgrent_response);
726         }
727
728         ret = winbindd_request_response(NULL, WINBINDD_ENDGRENT, NULL, NULL);
729 #ifdef DEBUG_NSS
730         fprintf(stderr, "[%5d]: endgrent returns %s (%d)\n", getpid(),
731                 nss_err_str(ret), ret);
732 #endif
733
734 #if HAVE_PTHREAD
735         pthread_mutex_unlock(&winbind_nss_mutex);
736 #endif
737
738         return ret;
739 }
740
741 /* Get next entry from ntdom group database */
742
743 static NSS_STATUS
744 winbind_getgrent(enum winbindd_cmd cmd,
745                  struct group *result,
746                  char *buffer, size_t buflen, int *errnop)
747 {
748         NSS_STATUS ret;
749         static struct winbindd_request request;
750         static int called_again;
751
752
753 #ifdef DEBUG_NSS
754         fprintf(stderr, "[%5d]: getgrent\n", getpid());
755 #endif
756
757 #if HAVE_PTHREAD
758         pthread_mutex_lock(&winbind_nss_mutex);
759 #endif
760
761         /* Return an entry from the cache if we have one, or if we are
762            called again because we exceeded our static buffer.  */
763
764         if ((ndx_gr_cache < num_gr_cache) || called_again) {
765                 goto return_result;
766         }
767
768         /* Else call winbindd to get a bunch of entries */
769
770         if (num_gr_cache > 0) {
771                 winbindd_free_response(&getgrent_response);
772         }
773
774         ZERO_STRUCT(request);
775         ZERO_STRUCT(getgrent_response);
776
777         request.data.num_entries = MAX_GETGRENT_USERS;
778
779         ret = winbindd_request_response(NULL, cmd, &request,
780                                &getgrent_response);
781
782         if (ret == NSS_STATUS_SUCCESS) {
783                 struct winbindd_gr *gr_cache;
784                 int mem_ofs;
785
786                 /* Fill cache */
787
788                 ndx_gr_cache = 0;
789                 num_gr_cache = getgrent_response.data.num_entries;
790
791                 /* Return a result */
792
793         return_result:
794
795                 gr_cache = (struct winbindd_gr *)
796                         getgrent_response.extra_data.data;
797
798                 /* Check data is valid */
799
800                 if (gr_cache == NULL) {
801                         ret = NSS_STATUS_NOTFOUND;
802                         goto done;
803                 }
804
805                 /* Fill group membership.  The offset into the extra data
806                    for the group membership is the reported offset plus the
807                    size of all the winbindd_gr records returned. */
808
809                 mem_ofs = gr_cache[ndx_gr_cache].gr_mem_ofs +
810                         num_gr_cache * sizeof(struct winbindd_gr);
811
812                 ret = fill_grent(result, &gr_cache[ndx_gr_cache],
813                                  ((char *)getgrent_response.extra_data.data)+mem_ofs,
814                                  &buffer, &buflen);
815
816                 /* Out of memory - try again */
817
818                 if (ret == NSS_STATUS_TRYAGAIN) {
819                         called_again = true;
820                         *errnop = errno = ERANGE;
821                         goto done;
822                 }
823
824                 *errnop = 0;
825                 called_again = false;
826                 ndx_gr_cache++;
827
828                 /* If we've finished with this lot of results free cache */
829
830                 if (ndx_gr_cache == num_gr_cache) {
831                         ndx_gr_cache = num_gr_cache = 0;
832                         winbindd_free_response(&getgrent_response);
833                 }
834         }
835         done:
836 #ifdef DEBUG_NSS
837         fprintf(stderr, "[%5d]: getgrent returns %s (%d)\n", getpid(),
838                 nss_err_str(ret), ret);
839 #endif
840
841 #if HAVE_PTHREAD
842         pthread_mutex_unlock(&winbind_nss_mutex);
843 #endif
844
845         return ret;
846 }
847
848
849 NSS_STATUS
850 _nss_winbind_getgrent_r(struct group *result,
851                         char *buffer, size_t buflen, int *errnop)
852 {
853         return winbind_getgrent(WINBINDD_GETGRENT, result, buffer, buflen, errnop);
854 }
855
856 NSS_STATUS
857 _nss_winbind_getgrlst_r(struct group *result,
858                         char *buffer, size_t buflen, int *errnop)
859 {
860         return winbind_getgrent(WINBINDD_GETGRLST, result, buffer, buflen, errnop);
861 }
862
863 /* Return group struct from group name */
864
865 NSS_STATUS
866 _nss_winbind_getgrnam_r(const char *name,
867                         struct group *result, char *buffer,
868                         size_t buflen, int *errnop)
869 {
870         NSS_STATUS ret;
871         static struct winbindd_response response;
872         struct winbindd_request request;
873         static int keep_response;
874
875 #ifdef DEBUG_NSS
876         fprintf(stderr, "[%5d]: getgrnam %s\n", getpid(), name);
877 #endif
878
879 #if HAVE_PTHREAD
880         pthread_mutex_lock(&winbind_nss_mutex);
881 #endif
882
883         /* If our static buffer needs to be expanded we are called again */
884         /* Or if the stored response group name differs from the request. */
885
886         if (!keep_response || strcmp(name,response.data.gr.gr_name) != 0) {
887
888                 /* Call for the first time */
889
890                 ZERO_STRUCT(request);
891                 ZERO_STRUCT(response);
892
893                 strncpy(request.data.groupname, name,
894                         sizeof(request.data.groupname));
895                 request.data.groupname
896                         [sizeof(request.data.groupname) - 1] = '\0';
897
898                 ret = winbindd_request_response(NULL, WINBINDD_GETGRNAM,
899                                                 &request, &response);
900
901                 if (ret == NSS_STATUS_SUCCESS) {
902                         ret = fill_grent(result, &response.data.gr,
903                                          (char *)response.extra_data.data,
904                                          &buffer, &buflen);
905
906                         if (ret == NSS_STATUS_TRYAGAIN) {
907                                 keep_response = true;
908                                 *errnop = errno = ERANGE;
909                                 goto done;
910                         }
911                 }
912
913         } else {
914
915                 /* We've been called again */
916
917                 ret = fill_grent(result, &response.data.gr,
918                                  (char *)response.extra_data.data, &buffer,
919                                  &buflen);
920
921                 if (ret == NSS_STATUS_TRYAGAIN) {
922                         keep_response = true;
923                         *errnop = errno = ERANGE;
924                         goto done;
925                 }
926
927                 keep_response = false;
928                 *errnop = 0;
929         }
930
931         winbindd_free_response(&response);
932         done:
933 #ifdef DEBUG_NSS
934         fprintf(stderr, "[%5d]: getgrnam %s returns %s (%d)\n", getpid(),
935                 name, nss_err_str(ret), ret);
936 #endif
937
938 #if HAVE_PTHREAD
939         pthread_mutex_unlock(&winbind_nss_mutex);
940 #endif
941
942         return ret;
943 }
944
945 /* Return group struct from gid */
946
947 NSS_STATUS
948 _nss_winbind_getgrgid_r(gid_t gid,
949                         struct group *result, char *buffer,
950                         size_t buflen, int *errnop)
951 {
952         NSS_STATUS ret;
953         static struct winbindd_response response;
954         struct winbindd_request request;
955         static int keep_response;
956
957 #ifdef DEBUG_NSS
958         fprintf(stderr, "[%5d]: getgrgid %d\n", getpid(), gid);
959 #endif
960
961 #if HAVE_PTHREAD
962         pthread_mutex_lock(&winbind_nss_mutex);
963 #endif
964
965         /* If our static buffer needs to be expanded we are called again */
966         /* Or if the stored response group name differs from the request. */
967
968         if (!keep_response || gid != response.data.gr.gr_gid) {
969
970                 /* Call for the first time */
971
972                 ZERO_STRUCT(request);
973                 ZERO_STRUCT(response);
974
975                 request.data.gid = gid;
976
977                 ret = winbindd_request_response(NULL, WINBINDD_GETGRGID,
978                                                 &request, &response);
979
980                 if (ret == NSS_STATUS_SUCCESS) {
981
982                         ret = fill_grent(result, &response.data.gr,
983                                          (char *)response.extra_data.data,
984                                          &buffer, &buflen);
985
986                         if (ret == NSS_STATUS_TRYAGAIN) {
987                                 keep_response = true;
988                                 *errnop = errno = ERANGE;
989                                 goto done;
990                         }
991                 }
992
993         } else {
994
995                 /* We've been called again */
996
997                 ret = fill_grent(result, &response.data.gr,
998                                  (char *)response.extra_data.data, &buffer,
999                                  &buflen);
1000
1001                 if (ret == NSS_STATUS_TRYAGAIN) {
1002                         keep_response = true;
1003                         *errnop = errno = ERANGE;
1004                         goto done;
1005                 }
1006
1007                 keep_response = false;
1008                 *errnop = 0;
1009         }
1010
1011         winbindd_free_response(&response);
1012         done:
1013 #ifdef DEBUG_NSS
1014         fprintf(stderr, "[%5d]: getgrgid %d returns %s (%d)\n", getpid(),
1015                 (unsigned int)gid, nss_err_str(ret), ret);
1016 #endif
1017
1018 #if HAVE_PTHREAD
1019         pthread_mutex_unlock(&winbind_nss_mutex);
1020 #endif
1021         return ret;
1022 }
1023
1024 /* Initialise supplementary groups */
1025
1026 NSS_STATUS
1027 _nss_winbind_initgroups_dyn(const char *user, gid_t group, long int *start,
1028                             long int *size, gid_t **groups, long int limit,
1029                             int *errnop)
1030 {
1031         NSS_STATUS ret;
1032         struct winbindd_request request;
1033         struct winbindd_response response;
1034         int i;
1035
1036 #ifdef DEBUG_NSS
1037         fprintf(stderr, "[%5d]: initgroups %s (%d)\n", getpid(),
1038                 user, group);
1039 #endif
1040
1041 #if HAVE_PTHREAD
1042         pthread_mutex_lock(&winbind_nss_mutex);
1043 #endif
1044
1045         ZERO_STRUCT(request);
1046         ZERO_STRUCT(response);
1047
1048         strncpy(request.data.username, user,
1049                 sizeof(request.data.username) - 1);
1050
1051         ret = winbindd_request_response(NULL, WINBINDD_GETGROUPS,
1052                                         &request, &response);
1053
1054         if (ret == NSS_STATUS_SUCCESS) {
1055                 int num_gids = response.data.num_entries;
1056                 gid_t *gid_list = (gid_t *)response.extra_data.data;
1057
1058 #ifdef DEBUG_NSS
1059                 fprintf(stderr, "[%5d]: initgroups %s: got NSS_STATUS_SUCCESS "
1060                                 "and %d gids\n", getpid(),
1061                                 user, num_gids);
1062 #endif
1063                 if (gid_list == NULL) {
1064                         ret = NSS_STATUS_NOTFOUND;
1065                         goto done;
1066                 }
1067
1068                 /* Copy group list to client */
1069
1070                 for (i = 0; i < num_gids; i++) {
1071
1072 #ifdef DEBUG_NSS
1073                         fprintf(stderr, "[%5d]: initgroups %s (%d): "
1074                                         "processing gid %d \n", getpid(),
1075                                         user, group, gid_list[i]);
1076 #endif
1077
1078                         /* Skip primary group */
1079
1080                         if (gid_list[i] == group) {
1081                                 continue;
1082                         }
1083
1084                         /* Skip groups without a mapping */
1085                         if (gid_list[i] == (uid_t)-1) {
1086                                 continue;
1087                         }
1088
1089                         /* Filled buffer ? If so, resize. */
1090
1091                         if (*start == *size) {
1092                                 long int newsize;
1093                                 gid_t *newgroups;
1094
1095                                 newsize = 2 * (*size);
1096                                 if (limit > 0) {
1097                                         if (*size == limit) {
1098                                                 goto done;
1099                                         }
1100                                         if (newsize > limit) {
1101                                                 newsize = limit;
1102                                         }
1103                                 }
1104
1105                                 newgroups = (gid_t *)
1106                                         realloc((*groups),
1107                                                 newsize * sizeof(**groups));
1108                                 if (!newgroups) {
1109                                         *errnop = ENOMEM;
1110                                         ret = NSS_STATUS_NOTFOUND;
1111                                         goto done;
1112                                 }
1113                                 *groups = newgroups;
1114                                 *size = newsize;
1115                         }
1116
1117                         /* Add to buffer */
1118
1119                         (*groups)[*start] = gid_list[i];
1120                         *start += 1;
1121                 }
1122         }
1123
1124         /* Back to your regularly scheduled programming */
1125
1126  done:
1127 #ifdef DEBUG_NSS
1128         fprintf(stderr, "[%5d]: initgroups %s returns %s (%d)\n", getpid(),
1129                 user, nss_err_str(ret), ret);
1130 #endif
1131
1132 #if HAVE_PTHREAD
1133         pthread_mutex_unlock(&winbind_nss_mutex);
1134 #endif
1135
1136         return ret;
1137 }