nsswitch: add some const to _nss_winbind_initgroups_dyn() prototype
[Samba.git] / nsswitch / winbind_nss_linux.c
blob442c06e612ff34c7cb4c69fb61a65bfc5ab01089
1 /*
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 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.
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 Lesser General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "winbind_client.h"
24 #if HAVE_PTHREAD_H
25 #include <pthread.h>
26 #endif
28 #if HAVE_PTHREAD
29 static pthread_mutex_t winbind_nss_mutex = PTHREAD_MUTEX_INITIALIZER;
30 #endif
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. */
36 #define MAX_GETPWENT_USERS 250
37 #define MAX_GETGRENT_USERS 250
39 /*************************************************************************
40 ************************************************************************/
42 #ifdef DEBUG_NSS
43 static const char *nss_err_str(NSS_STATUS ret)
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!!!!!!!";
62 #endif
64 /* Prototypes from wb_common.c */
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. */
70 static char *get_static(char **buffer, size_t *buflen, size_t len)
72 char *result;
74 /* Error check. We return false if things aren't set up right, or
75 there isn't enough buffer space left. */
77 if ((buffer == NULL) || (buflen == NULL) || (*buflen < len)) {
78 return NULL;
81 /* Return an index into the static buffer */
83 result = *buffer;
84 *buffer += len;
85 *buflen -= len;
87 return result;
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. */
94 static bool next_token_alloc(const char **ptr,
95 char **pp_buff,
96 const char *sep)
98 const char *s;
99 const char *saved_s;
100 char *pbuf;
101 bool quoted;
102 size_t len=1;
104 *pp_buff = NULL;
105 if (!ptr) {
106 return(false);
109 s = *ptr;
111 /* default to simple separators */
112 if (!sep) {
113 sep = " \t\n\r";
116 /* find the first non sep char */
117 while (*s && strchr(sep,*s)) {
118 s++;
121 /* nothing left? */
122 if (!*s) {
123 return false;
126 /* When restarting we need to go from here. */
127 saved_s = s;
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++;
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;
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;
157 *ptr = (*s) ? s+1 : s;
158 *pbuf = 0;
160 return true;
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. */
167 static NSS_STATUS fill_pwent(struct passwd *result,
168 struct winbindd_pw *pw,
169 char **buffer, size_t *buflen)
171 size_t len;
173 /* User name */
174 len = strlen(pw->pw_name) + 1;
176 if ((result->pw_name =
177 get_static(buffer, buflen, len)) == NULL) {
179 /* Out of memory */
181 return NSS_STATUS_TRYAGAIN;
184 memcpy(result->pw_name, pw->pw_name, len);
186 /* Password */
187 len = strlen(pw->pw_passwd) + 1;
189 if ((result->pw_passwd =
190 get_static(buffer, buflen, len)) == NULL) {
192 /* Out of memory */
194 return NSS_STATUS_TRYAGAIN;
197 memcpy(result->pw_passwd, pw->pw_passwd, len);
199 /* [ug]id */
201 result->pw_uid = pw->pw_uid;
202 result->pw_gid = pw->pw_gid;
204 /* GECOS */
205 len = strlen(pw->pw_gecos) + 1;
207 if ((result->pw_gecos =
208 get_static(buffer, buflen, len)) == NULL) {
210 /* Out of memory */
212 return NSS_STATUS_TRYAGAIN;
215 memcpy(result->pw_gecos, pw->pw_gecos, len);
217 /* Home directory */
218 len = strlen(pw->pw_dir) + 1;
220 if ((result->pw_dir =
221 get_static(buffer, buflen, len)) == NULL) {
223 /* Out of memory */
225 return NSS_STATUS_TRYAGAIN;
228 memcpy(result->pw_dir, pw->pw_dir, len);
230 /* Logon shell */
231 len = strlen(pw->pw_shell) + 1;
233 if ((result->pw_shell =
234 get_static(buffer, buflen, len)) == NULL) {
236 /* Out of memory */
238 return NSS_STATUS_TRYAGAIN;
241 memcpy(result->pw_shell, pw->pw_shell, len);
243 /* The struct passwd for Solaris has some extra fields which must
244 be initialised or nscd crashes. */
246 #if HAVE_PASSWD_PW_COMMENT
247 result->pw_comment = "";
248 #endif
250 #if HAVE_PASSWD_PW_AGE
251 result->pw_age = "";
252 #endif
254 return NSS_STATUS_SUCCESS;
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. */
261 static NSS_STATUS fill_grent(struct group *result, struct winbindd_gr *gr,
262 const char *gr_mem, char **buffer, size_t *buflen)
264 char *name;
265 int i;
266 char *tst;
267 size_t len;
269 /* Group name */
270 len = strlen(gr->gr_name) + 1;
272 if ((result->gr_name =
273 get_static(buffer, buflen, len)) == NULL) {
275 /* Out of memory */
277 return NSS_STATUS_TRYAGAIN;
280 memcpy(result->gr_name, gr->gr_name, len);
282 /* Password */
283 len = strlen(gr->gr_passwd) + 1;
285 if ((result->gr_passwd =
286 get_static(buffer, buflen, len)) == NULL) {
288 /* Out of memory */
289 return NSS_STATUS_TRYAGAIN;
292 memcpy(result->gr_passwd, gr->gr_passwd, len);
294 /* gid */
296 result->gr_gid = gr->gr_gid;
298 /* Group membership */
300 if (!gr_mem) {
301 gr->num_gr_mem = 0;
304 /* this next value is a pointer to a pointer so let's align it */
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;
310 if ((tst = get_static(buffer, buflen, ((gr->num_gr_mem + 1) *
311 sizeof(char *)+i))) == NULL) {
313 /* Out of memory */
315 return NSS_STATUS_TRYAGAIN;
317 result->gr_mem = (char **)(tst + i);
319 if (gr->num_gr_mem == 0) {
321 /* Group is empty */
323 *(result->gr_mem) = NULL;
324 return NSS_STATUS_SUCCESS;
327 /* Start looking at extra data */
329 i = 0;
331 while(next_token_alloc((const char **)&gr_mem, &name, ",")) {
332 /* Allocate space for member */
333 len = strlen(name) + 1;
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;
341 memcpy((result->gr_mem)[i], name, len);
342 free(name);
343 i++;
346 /* Terminate list */
348 (result->gr_mem)[i] = NULL;
350 return NSS_STATUS_SUCCESS;
354 * NSS user functions
357 static struct winbindd_response getpwent_response;
359 static int ndx_pw_cache; /* Current index into pwd cache */
360 static int num_pw_cache; /* Current size of pwd cache */
362 /* Rewind "file pointer" to start of ntdom password database */
364 NSS_STATUS
365 _nss_winbind_setpwent(void)
367 NSS_STATUS ret;
368 #ifdef DEBUG_NSS
369 fprintf(stderr, "[%5d]: setpwent\n", getpid());
370 #endif
372 #if HAVE_PTHREAD
373 pthread_mutex_lock(&winbind_nss_mutex);
374 #endif
376 if (num_pw_cache > 0) {
377 ndx_pw_cache = num_pw_cache = 0;
378 winbindd_free_response(&getpwent_response);
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
387 #if HAVE_PTHREAD
388 pthread_mutex_unlock(&winbind_nss_mutex);
389 #endif
390 return ret;
393 /* Close ntdom password database "file pointer" */
395 NSS_STATUS
396 _nss_winbind_endpwent(void)
398 NSS_STATUS ret;
399 #ifdef DEBUG_NSS
400 fprintf(stderr, "[%5d]: endpwent\n", getpid());
401 #endif
403 #if HAVE_PTHREAD
404 pthread_mutex_lock(&winbind_nss_mutex);
405 #endif
407 if (num_pw_cache > 0) {
408 ndx_pw_cache = num_pw_cache = 0;
409 winbindd_free_response(&getpwent_response);
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
418 #if HAVE_PTHREAD
419 pthread_mutex_unlock(&winbind_nss_mutex);
420 #endif
422 return ret;
425 /* Fetch the next password entry from ntdom password database */
427 NSS_STATUS
428 _nss_winbind_getpwent_r(struct passwd *result, char *buffer,
429 size_t buflen, int *errnop)
431 NSS_STATUS ret;
432 struct winbindd_request request;
433 static int called_again;
435 #ifdef DEBUG_NSS
436 fprintf(stderr, "[%5d]: getpwent\n", getpid());
437 #endif
439 #if HAVE_PTHREAD
440 pthread_mutex_lock(&winbind_nss_mutex);
441 #endif
443 /* Return an entry from the cache if we have one, or if we are
444 called again because we exceeded our static buffer. */
446 if ((ndx_pw_cache < num_pw_cache) || called_again) {
447 goto return_result;
450 /* Else call winbindd to get a bunch of entries */
452 if (num_pw_cache > 0) {
453 winbindd_free_response(&getpwent_response);
456 ZERO_STRUCT(request);
457 ZERO_STRUCT(getpwent_response);
459 request.data.num_entries = MAX_GETPWENT_USERS;
461 ret = winbindd_request_response(NULL, WINBINDD_GETPWENT, &request,
462 &getpwent_response);
464 if (ret == NSS_STATUS_SUCCESS) {
465 struct winbindd_pw *pw_cache;
467 /* Fill cache */
469 ndx_pw_cache = 0;
470 num_pw_cache = getpwent_response.data.num_entries;
472 /* Return a result */
474 return_result:
476 pw_cache = (struct winbindd_pw *)
477 getpwent_response.extra_data.data;
479 /* Check data is valid */
481 if (pw_cache == NULL) {
482 ret = NSS_STATUS_NOTFOUND;
483 goto done;
486 ret = fill_pwent(result, &pw_cache[ndx_pw_cache],
487 &buffer, &buflen);
489 /* Out of memory - try again */
491 if (ret == NSS_STATUS_TRYAGAIN) {
492 called_again = true;
493 *errnop = errno = ERANGE;
494 goto done;
497 *errnop = errno = 0;
498 called_again = false;
499 ndx_pw_cache++;
501 /* If we've finished with this lot of results free cache */
503 if (ndx_pw_cache == num_pw_cache) {
504 ndx_pw_cache = num_pw_cache = 0;
505 winbindd_free_response(&getpwent_response);
508 done:
509 #ifdef DEBUG_NSS
510 fprintf(stderr, "[%5d]: getpwent returns %s (%d)\n", getpid(),
511 nss_err_str(ret), ret);
512 #endif
514 #if HAVE_PTHREAD
515 pthread_mutex_unlock(&winbind_nss_mutex);
516 #endif
517 return ret;
520 /* Return passwd struct from uid */
522 NSS_STATUS
523 _nss_winbind_getpwuid_r(uid_t uid, struct passwd *result, char *buffer,
524 size_t buflen, int *errnop)
526 NSS_STATUS ret;
527 static struct winbindd_response response;
528 struct winbindd_request request;
529 static int keep_response;
531 #ifdef DEBUG_NSS
532 fprintf(stderr, "[%5d]: getpwuid_r %d\n", getpid(), (unsigned int)uid);
533 #endif
535 #if HAVE_PTHREAD
536 pthread_mutex_lock(&winbind_nss_mutex);
537 #endif
539 /* If our static buffer needs to be expanded we are called again */
540 if (!keep_response || uid != response.data.pw.pw_uid) {
542 /* Call for the first time */
544 ZERO_STRUCT(response);
545 ZERO_STRUCT(request);
547 request.data.uid = uid;
549 ret = winbindd_request_response(NULL, WINBINDD_GETPWUID, &request, &response);
551 if (ret == NSS_STATUS_SUCCESS) {
552 ret = fill_pwent(result, &response.data.pw,
553 &buffer, &buflen);
555 if (ret == NSS_STATUS_TRYAGAIN) {
556 keep_response = true;
557 *errnop = errno = ERANGE;
558 goto done;
562 } else {
564 /* We've been called again */
566 ret = fill_pwent(result, &response.data.pw, &buffer, &buflen);
568 if (ret == NSS_STATUS_TRYAGAIN) {
569 *errnop = errno = ERANGE;
570 goto done;
573 keep_response = false;
574 *errnop = errno = 0;
577 winbindd_free_response(&response);
579 done:
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
586 #if HAVE_PTHREAD
587 pthread_mutex_unlock(&winbind_nss_mutex);
588 #endif
590 return ret;
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)
598 NSS_STATUS ret;
599 static struct winbindd_response response;
600 struct winbindd_request request;
601 static int keep_response;
603 #ifdef DEBUG_NSS
604 fprintf(stderr, "[%5d]: getpwnam_r %s\n", getpid(), name);
605 #endif
607 #if HAVE_PTHREAD
608 pthread_mutex_lock(&winbind_nss_mutex);
609 #endif
611 /* If our static buffer needs to be expanded we are called again */
613 if (!keep_response || strcmp(name,response.data.pw.pw_name) != 0) {
615 /* Call for the first time */
617 ZERO_STRUCT(response);
618 ZERO_STRUCT(request);
620 strncpy(request.data.username, name,
621 sizeof(request.data.username) - 1);
622 request.data.username
623 [sizeof(request.data.username) - 1] = '\0';
625 ret = winbindd_request_response(NULL, WINBINDD_GETPWNAM, &request, &response);
627 if (ret == NSS_STATUS_SUCCESS) {
628 ret = fill_pwent(result, &response.data.pw, &buffer,
629 &buflen);
631 if (ret == NSS_STATUS_TRYAGAIN) {
632 keep_response = true;
633 *errnop = errno = ERANGE;
634 goto done;
638 } else {
640 /* We've been called again */
642 ret = fill_pwent(result, &response.data.pw, &buffer, &buflen);
644 if (ret == NSS_STATUS_TRYAGAIN) {
645 keep_response = true;
646 *errnop = errno = ERANGE;
647 goto done;
650 keep_response = false;
651 *errnop = errno = 0;
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
661 #if HAVE_PTHREAD
662 pthread_mutex_unlock(&winbind_nss_mutex);
663 #endif
665 return ret;
669 * NSS group functions
672 static struct winbindd_response getgrent_response;
674 static int ndx_gr_cache; /* Current index into grp cache */
675 static int num_gr_cache; /* Current size of grp cache */
677 /* Rewind "file pointer" to start of ntdom group database */
679 NSS_STATUS
680 _nss_winbind_setgrent(void)
682 NSS_STATUS ret;
683 #ifdef DEBUG_NSS
684 fprintf(stderr, "[%5d]: setgrent\n", getpid());
685 #endif
687 #if HAVE_PTHREAD
688 pthread_mutex_lock(&winbind_nss_mutex);
689 #endif
691 if (num_gr_cache > 0) {
692 ndx_gr_cache = num_gr_cache = 0;
693 winbindd_free_response(&getgrent_response);
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
702 #if HAVE_PTHREAD
703 pthread_mutex_unlock(&winbind_nss_mutex);
704 #endif
706 return ret;
709 /* Close "file pointer" for ntdom group database */
711 NSS_STATUS
712 _nss_winbind_endgrent(void)
714 NSS_STATUS ret;
715 #ifdef DEBUG_NSS
716 fprintf(stderr, "[%5d]: endgrent\n", getpid());
717 #endif
719 #if HAVE_PTHREAD
720 pthread_mutex_lock(&winbind_nss_mutex);
721 #endif
723 if (num_gr_cache > 0) {
724 ndx_gr_cache = num_gr_cache = 0;
725 winbindd_free_response(&getgrent_response);
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
734 #if HAVE_PTHREAD
735 pthread_mutex_unlock(&winbind_nss_mutex);
736 #endif
738 return ret;
741 /* Get next entry from ntdom group database */
743 static NSS_STATUS
744 winbind_getgrent(enum winbindd_cmd cmd,
745 struct group *result,
746 char *buffer, size_t buflen, int *errnop)
748 NSS_STATUS ret;
749 static struct winbindd_request request;
750 static int called_again;
753 #ifdef DEBUG_NSS
754 fprintf(stderr, "[%5d]: getgrent\n", getpid());
755 #endif
757 #if HAVE_PTHREAD
758 pthread_mutex_lock(&winbind_nss_mutex);
759 #endif
761 /* Return an entry from the cache if we have one, or if we are
762 called again because we exceeded our static buffer. */
764 if ((ndx_gr_cache < num_gr_cache) || called_again) {
765 goto return_result;
768 /* Else call winbindd to get a bunch of entries */
770 if (num_gr_cache > 0) {
771 winbindd_free_response(&getgrent_response);
774 ZERO_STRUCT(request);
775 ZERO_STRUCT(getgrent_response);
777 request.data.num_entries = MAX_GETGRENT_USERS;
779 ret = winbindd_request_response(NULL, cmd, &request,
780 &getgrent_response);
782 if (ret == NSS_STATUS_SUCCESS) {
783 struct winbindd_gr *gr_cache;
784 int mem_ofs;
786 /* Fill cache */
788 ndx_gr_cache = 0;
789 num_gr_cache = getgrent_response.data.num_entries;
791 /* Return a result */
793 return_result:
795 gr_cache = (struct winbindd_gr *)
796 getgrent_response.extra_data.data;
798 /* Check data is valid */
800 if (gr_cache == NULL) {
801 ret = NSS_STATUS_NOTFOUND;
802 goto done;
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. */
809 mem_ofs = gr_cache[ndx_gr_cache].gr_mem_ofs +
810 num_gr_cache * sizeof(struct winbindd_gr);
812 ret = fill_grent(result, &gr_cache[ndx_gr_cache],
813 ((char *)getgrent_response.extra_data.data)+mem_ofs,
814 &buffer, &buflen);
816 /* Out of memory - try again */
818 if (ret == NSS_STATUS_TRYAGAIN) {
819 called_again = true;
820 *errnop = errno = ERANGE;
821 goto done;
824 *errnop = 0;
825 called_again = false;
826 ndx_gr_cache++;
828 /* If we've finished with this lot of results free cache */
830 if (ndx_gr_cache == num_gr_cache) {
831 ndx_gr_cache = num_gr_cache = 0;
832 winbindd_free_response(&getgrent_response);
835 done:
836 #ifdef DEBUG_NSS
837 fprintf(stderr, "[%5d]: getgrent returns %s (%d)\n", getpid(),
838 nss_err_str(ret), ret);
839 #endif
841 #if HAVE_PTHREAD
842 pthread_mutex_unlock(&winbind_nss_mutex);
843 #endif
845 return ret;
849 NSS_STATUS
850 _nss_winbind_getgrent_r(struct group *result,
851 char *buffer, size_t buflen, int *errnop)
853 return winbind_getgrent(WINBINDD_GETGRENT, result, buffer, buflen, errnop);
856 NSS_STATUS
857 _nss_winbind_getgrlst_r(struct group *result,
858 char *buffer, size_t buflen, int *errnop)
860 return winbind_getgrent(WINBINDD_GETGRLST, result, buffer, buflen, errnop);
863 /* Return group struct from group name */
865 NSS_STATUS
866 _nss_winbind_getgrnam_r(const char *name,
867 struct group *result, char *buffer,
868 size_t buflen, int *errnop)
870 NSS_STATUS ret;
871 static struct winbindd_response response;
872 struct winbindd_request request;
873 static int keep_response;
875 #ifdef DEBUG_NSS
876 fprintf(stderr, "[%5d]: getgrnam %s\n", getpid(), name);
877 #endif
879 #if HAVE_PTHREAD
880 pthread_mutex_lock(&winbind_nss_mutex);
881 #endif
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. */
886 if (!keep_response || strcmp(name,response.data.gr.gr_name) != 0) {
888 /* Call for the first time */
890 ZERO_STRUCT(request);
891 ZERO_STRUCT(response);
893 strncpy(request.data.groupname, name,
894 sizeof(request.data.groupname));
895 request.data.groupname
896 [sizeof(request.data.groupname) - 1] = '\0';
898 ret = winbindd_request_response(NULL, WINBINDD_GETGRNAM,
899 &request, &response);
901 if (ret == NSS_STATUS_SUCCESS) {
902 ret = fill_grent(result, &response.data.gr,
903 (char *)response.extra_data.data,
904 &buffer, &buflen);
906 if (ret == NSS_STATUS_TRYAGAIN) {
907 keep_response = true;
908 *errnop = errno = ERANGE;
909 goto done;
913 } else {
915 /* We've been called again */
917 ret = fill_grent(result, &response.data.gr,
918 (char *)response.extra_data.data, &buffer,
919 &buflen);
921 if (ret == NSS_STATUS_TRYAGAIN) {
922 keep_response = true;
923 *errnop = errno = ERANGE;
924 goto done;
927 keep_response = false;
928 *errnop = 0;
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
938 #if HAVE_PTHREAD
939 pthread_mutex_unlock(&winbind_nss_mutex);
940 #endif
942 return ret;
945 /* Return group struct from gid */
947 NSS_STATUS
948 _nss_winbind_getgrgid_r(gid_t gid,
949 struct group *result, char *buffer,
950 size_t buflen, int *errnop)
952 NSS_STATUS ret;
953 static struct winbindd_response response;
954 struct winbindd_request request;
955 static int keep_response;
957 #ifdef DEBUG_NSS
958 fprintf(stderr, "[%5d]: getgrgid %d\n", getpid(), gid);
959 #endif
961 #if HAVE_PTHREAD
962 pthread_mutex_lock(&winbind_nss_mutex);
963 #endif
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. */
968 if (!keep_response || gid != response.data.gr.gr_gid) {
970 /* Call for the first time */
972 ZERO_STRUCT(request);
973 ZERO_STRUCT(response);
975 request.data.gid = gid;
977 ret = winbindd_request_response(NULL, WINBINDD_GETGRGID,
978 &request, &response);
980 if (ret == NSS_STATUS_SUCCESS) {
982 ret = fill_grent(result, &response.data.gr,
983 (char *)response.extra_data.data,
984 &buffer, &buflen);
986 if (ret == NSS_STATUS_TRYAGAIN) {
987 keep_response = true;
988 *errnop = errno = ERANGE;
989 goto done;
993 } else {
995 /* We've been called again */
997 ret = fill_grent(result, &response.data.gr,
998 (char *)response.extra_data.data, &buffer,
999 &buflen);
1001 if (ret == NSS_STATUS_TRYAGAIN) {
1002 keep_response = true;
1003 *errnop = errno = ERANGE;
1004 goto done;
1007 keep_response = false;
1008 *errnop = 0;
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
1018 #if HAVE_PTHREAD
1019 pthread_mutex_unlock(&winbind_nss_mutex);
1020 #endif
1021 return ret;
1024 /* Initialise supplementary groups */
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)
1031 NSS_STATUS ret;
1032 struct winbindd_request request;
1033 struct winbindd_response response;
1034 int i;
1036 #ifdef DEBUG_NSS
1037 fprintf(stderr, "[%5d]: initgroups %s (%d)\n", getpid(),
1038 user, group);
1039 #endif
1041 #if HAVE_PTHREAD
1042 pthread_mutex_lock(&winbind_nss_mutex);
1043 #endif
1045 ZERO_STRUCT(request);
1046 ZERO_STRUCT(response);
1048 strncpy(request.data.username, user,
1049 sizeof(request.data.username) - 1);
1051 ret = winbindd_request_response(NULL, WINBINDD_GETGROUPS,
1052 &request, &response);
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;
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;
1068 /* Copy group list to client */
1070 for (i = 0; i < num_gids; i++) {
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
1078 /* Skip primary group */
1080 if (gid_list[i] == group) {
1081 continue;
1084 /* Skip groups without a mapping */
1085 if (gid_list[i] == (uid_t)-1) {
1086 continue;
1089 /* Filled buffer ? If so, resize. */
1091 if (*start == *size) {
1092 long int newsize;
1093 gid_t *newgroups;
1095 newsize = 2 * (*size);
1096 if (limit > 0) {
1097 if (*size == limit) {
1098 goto done;
1100 if (newsize > limit) {
1101 newsize = limit;
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;
1113 *groups = newgroups;
1114 *size = newsize;
1117 /* Add to buffer */
1119 (*groups)[*start] = gid_list[i];
1120 *start += 1;
1124 /* Back to your regularly scheduled programming */
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
1132 #if HAVE_PTHREAD
1133 pthread_mutex_unlock(&winbind_nss_mutex);
1134 #endif
1136 return ret;