libsmbconf: testsuite: add talloc_stackframe to main().
[Samba.git] / source / nsswitch / winbind_nss_linux.c
blobbfac2ecdd14a106efd5bcb031929d9e8a0953a23
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 NSS_STATUS _nss_winbind_setpwent(void);
40 NSS_STATUS _nss_winbind_endpwent(void);
41 NSS_STATUS _nss_winbind_getpwent_r(struct passwd *result, char *buffer,
42 size_t buflen, int *errnop);
43 NSS_STATUS _nss_winbind_getpwuid_r(uid_t uid, struct passwd *result,
44 char *buffer, size_t buflen, int *errnop);
45 NSS_STATUS _nss_winbind_getpwnam_r(const char *name, struct passwd *result,
46 char *buffer, size_t buflen, int *errnop);
47 NSS_STATUS _nss_winbind_setgrent(void);
48 NSS_STATUS _nss_winbind_endgrent(void);
49 NSS_STATUS _nss_winbind_getgrent_r(struct group *result, char *buffer,
50 size_t buflen, int *errnop);
51 NSS_STATUS _nss_winbind_getgrlst_r(struct group *result, char *buffer,
52 size_t buflen, int *errnop);
53 NSS_STATUS _nss_winbind_getgrnam_r(const char *name, struct group *result,
54 char *buffer, size_t buflen, int *errnop);
55 NSS_STATUS _nss_winbind_getgrgid_r(gid_t gid, struct group *result, char *buffer,
56 size_t buflen, int *errnop);
57 NSS_STATUS _nss_winbind_initgroups_dyn(char *user, gid_t group, long int *start,
58 long int *size, gid_t **groups,
59 long int limit, int *errnop);
60 NSS_STATUS _nss_winbind_getusersids(const char *user_sid, char **group_sids,
61 int *num_groups, char *buffer, size_t buf_size,
62 int *errnop);
63 NSS_STATUS _nss_winbind_nametosid(const char *name, char **sid, char *buffer,
64 size_t buflen, int *errnop);
65 NSS_STATUS _nss_winbind_sidtoname(const char *sid, char **name, char *buffer,
66 size_t buflen, int *errnop);
67 NSS_STATUS _nss_winbind_sidtouid(const char *sid, uid_t *uid, int *errnop);
68 NSS_STATUS _nss_winbind_sidtogid(const char *sid, gid_t *gid, int *errnop);
69 NSS_STATUS _nss_winbind_uidtosid(uid_t uid, char **sid, char *buffer,
70 size_t buflen, int *errnop);
71 NSS_STATUS _nss_winbind_gidtosid(gid_t gid, char **sid, char *buffer,
72 size_t buflen, int *errnop);
74 /* Prototypes from wb_common.c */
76 extern int winbindd_fd;
78 /* Allocate some space from the nss static buffer. The buffer and buflen
79 are the pointers passed in by the C library to the _nss_ntdom_*
80 functions. */
82 static char *get_static(char **buffer, size_t *buflen, size_t len)
84 char *result;
86 /* Error check. We return false if things aren't set up right, or
87 there isn't enough buffer space left. */
89 if ((buffer == NULL) || (buflen == NULL) || (*buflen < len)) {
90 return NULL;
93 /* Return an index into the static buffer */
95 result = *buffer;
96 *buffer += len;
97 *buflen -= len;
99 return result;
102 /* I've copied the strtok() replacement function next_token_Xalloc() from
103 lib/util_str.c as I really don't want to have to link in any other
104 objects if I can possibly avoid it. */
106 static bool next_token_alloc(const char **ptr,
107 char **pp_buff,
108 const char *sep)
110 char *s;
111 char *saved_s;
112 char *pbuf;
113 bool quoted;
114 size_t len=1;
116 *pp_buff = NULL;
117 if (!ptr) {
118 return(false);
121 s = (char *)*ptr;
123 /* default to simple separators */
124 if (!sep) {
125 sep = " \t\n\r";
128 /* find the first non sep char */
129 while (*s && strchr(sep,*s)) {
130 s++;
133 /* nothing left? */
134 if (!*s) {
135 return false;
138 /* When restarting we need to go from here. */
139 saved_s = s;
141 /* Work out the length needed. */
142 for (quoted = false; *s &&
143 (quoted || !strchr(sep,*s)); s++) {
144 if (*s == '\"') {
145 quoted = !quoted;
146 } else {
147 len++;
151 /* We started with len = 1 so we have space for the nul. */
152 *pp_buff = malloc(len);
153 if (!*pp_buff) {
154 return false;
157 /* copy over the token */
158 pbuf = *pp_buff;
159 s = saved_s;
160 for (quoted = false; *s &&
161 (quoted || !strchr(sep,*s)); s++) {
162 if ( *s == '\"' ) {
163 quoted = !quoted;
164 } else {
165 *pbuf++ = *s;
169 *ptr = (*s) ? s+1 : s;
170 *pbuf = 0;
172 return true;
175 /* Fill a pwent structure from a winbindd_response structure. We use
176 the static data passed to us by libc to put strings and stuff in.
177 Return NSS_STATUS_TRYAGAIN if we run out of memory. */
179 static NSS_STATUS fill_pwent(struct passwd *result,
180 struct winbindd_pw *pw,
181 char **buffer, size_t *buflen)
183 /* User name */
185 if ((result->pw_name =
186 get_static(buffer, buflen, strlen(pw->pw_name) + 1)) == NULL) {
188 /* Out of memory */
190 return NSS_STATUS_TRYAGAIN;
193 strcpy(result->pw_name, pw->pw_name);
195 /* Password */
197 if ((result->pw_passwd =
198 get_static(buffer, buflen, strlen(pw->pw_passwd) + 1)) == NULL) {
200 /* Out of memory */
202 return NSS_STATUS_TRYAGAIN;
205 strcpy(result->pw_passwd, pw->pw_passwd);
207 /* [ug]id */
209 result->pw_uid = pw->pw_uid;
210 result->pw_gid = pw->pw_gid;
212 /* GECOS */
214 if ((result->pw_gecos =
215 get_static(buffer, buflen, strlen(pw->pw_gecos) + 1)) == NULL) {
217 /* Out of memory */
219 return NSS_STATUS_TRYAGAIN;
222 strcpy(result->pw_gecos, pw->pw_gecos);
224 /* Home directory */
226 if ((result->pw_dir =
227 get_static(buffer, buflen, strlen(pw->pw_dir) + 1)) == NULL) {
229 /* Out of memory */
231 return NSS_STATUS_TRYAGAIN;
234 strcpy(result->pw_dir, pw->pw_dir);
236 /* Logon shell */
238 if ((result->pw_shell =
239 get_static(buffer, buflen, strlen(pw->pw_shell) + 1)) == NULL) {
241 /* Out of memory */
243 return NSS_STATUS_TRYAGAIN;
246 strcpy(result->pw_shell, pw->pw_shell);
248 /* The struct passwd for Solaris has some extra fields which must
249 be initialised or nscd crashes. */
251 #if HAVE_PASSWD_PW_COMMENT
252 result->pw_comment = "";
253 #endif
255 #if HAVE_PASSWD_PW_AGE
256 result->pw_age = "";
257 #endif
259 return NSS_STATUS_SUCCESS;
262 /* Fill a grent structure from a winbindd_response structure. We use
263 the static data passed to us by libc to put strings and stuff in.
264 Return NSS_STATUS_TRYAGAIN if we run out of memory. */
266 static NSS_STATUS fill_grent(struct group *result, struct winbindd_gr *gr,
267 char *gr_mem, char **buffer, size_t *buflen)
269 char *name;
270 int i;
271 char *tst;
273 /* Group name */
275 if ((result->gr_name =
276 get_static(buffer, buflen, strlen(gr->gr_name) + 1)) == NULL) {
278 /* Out of memory */
280 return NSS_STATUS_TRYAGAIN;
283 strcpy(result->gr_name, gr->gr_name);
285 /* Password */
287 if ((result->gr_passwd =
288 get_static(buffer, buflen, strlen(gr->gr_passwd) + 1)) == NULL) {
290 /* Out of memory */
291 return NSS_STATUS_TRYAGAIN;
294 strcpy(result->gr_passwd, gr->gr_passwd);
296 /* gid */
298 result->gr_gid = gr->gr_gid;
300 /* Group membership */
302 if ((gr->num_gr_mem < 0) || !gr_mem) {
303 gr->num_gr_mem = 0;
306 /* this next value is a pointer to a pointer so let's align it */
308 /* Calculate number of extra bytes needed to align on pointer size boundry */
309 if ((i = (unsigned long)(*buffer) % sizeof(char*)) != 0)
310 i = sizeof(char*) - i;
312 if ((tst = get_static(buffer, buflen, ((gr->num_gr_mem + 1) *
313 sizeof(char *)+i))) == NULL) {
315 /* Out of memory */
317 return NSS_STATUS_TRYAGAIN;
319 result->gr_mem = (char **)(tst + i);
321 if (gr->num_gr_mem == 0) {
323 /* Group is empty */
325 *(result->gr_mem) = NULL;
326 return NSS_STATUS_SUCCESS;
329 /* Start looking at extra data */
331 i = 0;
333 while(next_token_alloc((const char **)&gr_mem, &name, ",")) {
334 /* Allocate space for member */
335 if (((result->gr_mem)[i] =
336 get_static(buffer, buflen, strlen(name) + 1)) == NULL) {
337 free(name);
338 /* Out of memory */
339 return NSS_STATUS_TRYAGAIN;
341 strcpy((result->gr_mem)[i], name);
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(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(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(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(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(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(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(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(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(WINBINDD_GETGRNAM, &request, &response);
900 if (ret == NSS_STATUS_SUCCESS) {
901 ret = fill_grent(result, &response.data.gr,
902 (char *)response.extra_data.data,
903 &buffer, &buflen);
905 if (ret == NSS_STATUS_TRYAGAIN) {
906 keep_response = true;
907 *errnop = errno = ERANGE;
908 goto done;
912 } else {
914 /* We've been called again */
916 ret = fill_grent(result, &response.data.gr,
917 (char *)response.extra_data.data, &buffer,
918 &buflen);
920 if (ret == NSS_STATUS_TRYAGAIN) {
921 keep_response = true;
922 *errnop = errno = ERANGE;
923 goto done;
926 keep_response = false;
927 *errnop = 0;
930 winbindd_free_response(&response);
931 done:
932 #ifdef DEBUG_NSS
933 fprintf(stderr, "[%5d]: getgrnam %s returns %s (%d)\n", getpid(),
934 name, nss_err_str(ret), ret);
935 #endif
937 #if HAVE_PTHREAD
938 pthread_mutex_unlock(&winbind_nss_mutex);
939 #endif
941 return ret;
944 /* Return group struct from gid */
946 NSS_STATUS
947 _nss_winbind_getgrgid_r(gid_t gid,
948 struct group *result, char *buffer,
949 size_t buflen, int *errnop)
951 NSS_STATUS ret;
952 static struct winbindd_response response;
953 struct winbindd_request request;
954 static int keep_response;
956 #ifdef DEBUG_NSS
957 fprintf(stderr, "[%5d]: getgrgid %d\n", getpid(), gid);
958 #endif
960 #if HAVE_PTHREAD
961 pthread_mutex_lock(&winbind_nss_mutex);
962 #endif
964 /* If our static buffer needs to be expanded we are called again */
965 /* Or if the stored response group name differs from the request. */
967 if (!keep_response || gid != response.data.gr.gr_gid) {
969 /* Call for the first time */
971 ZERO_STRUCT(request);
972 ZERO_STRUCT(response);
974 request.data.gid = gid;
976 ret = winbindd_request_response(WINBINDD_GETGRGID, &request, &response);
978 if (ret == NSS_STATUS_SUCCESS) {
980 ret = fill_grent(result, &response.data.gr,
981 (char *)response.extra_data.data,
982 &buffer, &buflen);
984 if (ret == NSS_STATUS_TRYAGAIN) {
985 keep_response = true;
986 *errnop = errno = ERANGE;
987 goto done;
991 } else {
993 /* We've been called again */
995 ret = fill_grent(result, &response.data.gr,
996 (char *)response.extra_data.data, &buffer,
997 &buflen);
999 if (ret == NSS_STATUS_TRYAGAIN) {
1000 keep_response = true;
1001 *errnop = errno = ERANGE;
1002 goto done;
1005 keep_response = false;
1006 *errnop = 0;
1009 winbindd_free_response(&response);
1010 done:
1011 #ifdef DEBUG_NSS
1012 fprintf(stderr, "[%5d]: getgrgid %d returns %s (%d)\n", getpid(),
1013 (unsigned int)gid, nss_err_str(ret), ret);
1014 #endif
1016 #if HAVE_PTHREAD
1017 pthread_mutex_unlock(&winbind_nss_mutex);
1018 #endif
1019 return ret;
1022 /* Initialise supplementary groups */
1024 NSS_STATUS
1025 _nss_winbind_initgroups_dyn(char *user, gid_t group, long int *start,
1026 long int *size, gid_t **groups, long int limit,
1027 int *errnop)
1029 NSS_STATUS ret;
1030 struct winbindd_request request;
1031 struct winbindd_response response;
1032 int i;
1034 #ifdef DEBUG_NSS
1035 fprintf(stderr, "[%5d]: initgroups %s (%d)\n", getpid(),
1036 user, group);
1037 #endif
1039 #if HAVE_PTHREAD
1040 pthread_mutex_lock(&winbind_nss_mutex);
1041 #endif
1043 ZERO_STRUCT(request);
1044 ZERO_STRUCT(response);
1046 strncpy(request.data.username, user,
1047 sizeof(request.data.username) - 1);
1049 ret = winbindd_request_response(WINBINDD_GETGROUPS, &request, &response);
1051 if (ret == NSS_STATUS_SUCCESS) {
1052 int num_gids = response.data.num_entries;
1053 gid_t *gid_list = (gid_t *)response.extra_data.data;
1055 #ifdef DEBUG_NSS
1056 fprintf(stderr, "[%5d]: initgroups %s: got NSS_STATUS_SUCCESS "
1057 "and %d gids\n", getpid(),
1058 user, num_gids);
1059 #endif
1060 if (gid_list == NULL) {
1061 ret = NSS_STATUS_NOTFOUND;
1062 goto done;
1065 /* Copy group list to client */
1067 for (i = 0; i < num_gids; i++) {
1069 #ifdef DEBUG_NSS
1070 fprintf(stderr, "[%5d]: initgroups %s (%d): "
1071 "processing gid %d \n", getpid(),
1072 user, group, gid_list[i]);
1073 #endif
1075 /* Skip primary group */
1077 if (gid_list[i] == group) {
1078 continue;
1081 /* Filled buffer ? If so, resize. */
1083 if (*start == *size) {
1084 long int newsize;
1085 gid_t *newgroups;
1087 newsize = 2 * (*size);
1088 if (limit > 0) {
1089 if (*size == limit) {
1090 goto done;
1092 if (newsize > limit) {
1093 newsize = limit;
1097 newgroups = (gid_t *)
1098 realloc((*groups),
1099 newsize * sizeof(**groups));
1100 if (!newgroups) {
1101 *errnop = ENOMEM;
1102 ret = NSS_STATUS_NOTFOUND;
1103 goto done;
1105 *groups = newgroups;
1106 *size = newsize;
1109 /* Add to buffer */
1111 (*groups)[*start] = gid_list[i];
1112 *start += 1;
1116 /* Back to your regularly scheduled programming */
1118 done:
1119 #ifdef DEBUG_NSS
1120 fprintf(stderr, "[%5d]: initgroups %s returns %s (%d)\n", getpid(),
1121 user, nss_err_str(ret), ret);
1122 #endif
1124 #if HAVE_PTHREAD
1125 pthread_mutex_unlock(&winbind_nss_mutex);
1126 #endif
1128 return ret;
1132 /* return a list of group SIDs for a user SID */
1133 NSS_STATUS
1134 _nss_winbind_getusersids(const char *user_sid, char **group_sids,
1135 int *num_groups,
1136 char *buffer, size_t buf_size, int *errnop)
1138 NSS_STATUS ret;
1139 struct winbindd_request request;
1140 struct winbindd_response response;
1142 #ifdef DEBUG_NSS
1143 fprintf(stderr, "[%5d]: getusersids %s\n", getpid(), user_sid);
1144 #endif
1146 #if HAVE_PTHREAD
1147 pthread_mutex_lock(&winbind_nss_mutex);
1148 #endif
1150 ZERO_STRUCT(request);
1151 ZERO_STRUCT(response);
1153 strncpy(request.data.sid, user_sid,sizeof(request.data.sid) - 1);
1154 request.data.sid[sizeof(request.data.sid) - 1] = '\0';
1156 ret = winbindd_request_response(WINBINDD_GETUSERSIDS, &request, &response);
1158 if (ret != NSS_STATUS_SUCCESS) {
1159 goto done;
1162 if (buf_size < response.length - sizeof(response)) {
1163 ret = NSS_STATUS_TRYAGAIN;
1164 errno = *errnop = ERANGE;
1165 goto done;
1168 *num_groups = response.data.num_entries;
1169 *group_sids = buffer;
1170 memcpy(buffer, response.extra_data.data, response.length - sizeof(response));
1171 errno = *errnop = 0;
1173 done:
1174 winbindd_free_response(&response);
1176 #if HAVE_PTHREAD
1177 pthread_mutex_unlock(&winbind_nss_mutex);
1178 #endif
1180 return ret;
1184 /* map a user or group name to a SID string */
1185 NSS_STATUS
1186 _nss_winbind_nametosid(const char *name, char **sid, char *buffer,
1187 size_t buflen, int *errnop)
1189 NSS_STATUS ret;
1190 struct winbindd_response response;
1191 struct winbindd_request request;
1193 #ifdef DEBUG_NSS
1194 fprintf(stderr, "[%5d]: nametosid %s\n", getpid(), name);
1195 #endif
1197 #if HAVE_PTHREAD
1198 pthread_mutex_lock(&winbind_nss_mutex);
1199 #endif
1201 ZERO_STRUCT(response);
1202 ZERO_STRUCT(request);
1204 strncpy(request.data.name.name, name,
1205 sizeof(request.data.name.name) - 1);
1206 request.data.name.name[sizeof(request.data.name.name) - 1] = '\0';
1208 ret = winbindd_request_response(WINBINDD_LOOKUPNAME, &request, &response);
1209 if (ret != NSS_STATUS_SUCCESS) {
1210 *errnop = errno = EINVAL;
1211 goto failed;
1214 if (buflen < strlen(response.data.sid.sid)+1) {
1215 ret = NSS_STATUS_TRYAGAIN;
1216 *errnop = errno = ERANGE;
1217 goto failed;
1220 *errnop = errno = 0;
1221 *sid = buffer;
1222 strcpy(*sid, response.data.sid.sid);
1224 failed:
1225 winbindd_free_response(&response);
1227 #if HAVE_PTHREAD
1228 pthread_mutex_unlock(&winbind_nss_mutex);
1229 #endif
1231 return ret;
1234 /* map a sid string to a user or group name */
1235 NSS_STATUS
1236 _nss_winbind_sidtoname(const char *sid, char **name, char *buffer,
1237 size_t buflen, int *errnop)
1239 NSS_STATUS ret;
1240 struct winbindd_response response;
1241 struct winbindd_request request;
1242 static char sep_char;
1243 unsigned needed;
1245 #ifdef DEBUG_NSS
1246 fprintf(stderr, "[%5d]: sidtoname %s\n", getpid(), sid);
1247 #endif
1249 #if HAVE_PTHREAD
1250 pthread_mutex_lock(&winbind_nss_mutex);
1251 #endif
1253 ZERO_STRUCT(response);
1254 ZERO_STRUCT(request);
1256 /* we need to fetch the separator first time through */
1257 if (!sep_char) {
1258 ret = winbindd_request_response(WINBINDD_INFO, &request, &response);
1259 if (ret != NSS_STATUS_SUCCESS) {
1260 *errnop = errno = EINVAL;
1261 goto failed;
1264 sep_char = response.data.info.winbind_separator;
1265 winbindd_free_response(&response);
1269 strncpy(request.data.sid, sid,
1270 sizeof(request.data.sid) - 1);
1271 request.data.sid[sizeof(request.data.sid) - 1] = '\0';
1273 ret = winbindd_request_response(WINBINDD_LOOKUPSID, &request, &response);
1274 if (ret != NSS_STATUS_SUCCESS) {
1275 *errnop = errno = EINVAL;
1276 goto failed;
1279 needed =
1280 strlen(response.data.name.dom_name) +
1281 strlen(response.data.name.name) + 2;
1283 if (buflen < needed) {
1284 ret = NSS_STATUS_TRYAGAIN;
1285 *errnop = errno = ERANGE;
1286 goto failed;
1289 snprintf(buffer, needed, "%s%c%s",
1290 response.data.name.dom_name,
1291 sep_char,
1292 response.data.name.name);
1294 *name = buffer;
1295 *errnop = errno = 0;
1297 failed:
1298 winbindd_free_response(&response);
1300 #if HAVE_PTHREAD
1301 pthread_mutex_unlock(&winbind_nss_mutex);
1302 #endif
1304 return ret;
1307 /* map a sid to a uid */
1308 NSS_STATUS
1309 _nss_winbind_sidtouid(const char *sid, uid_t *uid, int *errnop)
1311 NSS_STATUS ret;
1312 struct winbindd_response response;
1313 struct winbindd_request request;
1315 #ifdef DEBUG_NSS
1316 fprintf(stderr, "[%5d]: sidtouid %s\n", getpid(), sid);
1317 #endif
1319 #if HAVE_PTHREAD
1320 pthread_mutex_lock(&winbind_nss_mutex);
1321 #endif
1323 ZERO_STRUCT(request);
1324 ZERO_STRUCT(response);
1326 strncpy(request.data.sid, sid, sizeof(request.data.sid) - 1);
1327 request.data.sid[sizeof(request.data.sid) - 1] = '\0';
1329 ret = winbindd_request_response(WINBINDD_SID_TO_UID, &request, &response);
1330 if (ret != NSS_STATUS_SUCCESS) {
1331 *errnop = errno = EINVAL;
1332 goto failed;
1335 *uid = response.data.uid;
1337 failed:
1339 #if HAVE_PTHREAD
1340 pthread_mutex_unlock(&winbind_nss_mutex);
1341 #endif
1343 return ret;
1346 /* map a sid to a gid */
1347 NSS_STATUS
1348 _nss_winbind_sidtogid(const char *sid, gid_t *gid, int *errnop)
1350 NSS_STATUS ret;
1351 struct winbindd_response response;
1352 struct winbindd_request request;
1354 #ifdef DEBUG_NSS
1355 fprintf(stderr, "[%5d]: sidtogid %s\n", getpid(), sid);
1356 #endif
1358 #if HAVE_PTHREAD
1359 pthread_mutex_lock(&winbind_nss_mutex);
1360 #endif
1362 ZERO_STRUCT(request);
1363 ZERO_STRUCT(response);
1365 strncpy(request.data.sid, sid, sizeof(request.data.sid) - 1);
1366 request.data.sid[sizeof(request.data.sid) - 1] = '\0';
1368 ret = winbindd_request_response(WINBINDD_SID_TO_GID, &request, &response);
1369 if (ret != NSS_STATUS_SUCCESS) {
1370 *errnop = errno = EINVAL;
1371 goto failed;
1374 *gid = response.data.gid;
1376 failed:
1378 #if HAVE_PTHREAD
1379 pthread_mutex_unlock(&winbind_nss_mutex);
1380 #endif
1382 return ret;
1385 /* map a uid to a SID string */
1386 NSS_STATUS
1387 _nss_winbind_uidtosid(uid_t uid, char **sid, char *buffer,
1388 size_t buflen, int *errnop)
1390 NSS_STATUS ret;
1391 struct winbindd_response response;
1392 struct winbindd_request request;
1394 #ifdef DEBUG_NSS
1395 fprintf(stderr, "[%5u]: uidtosid %u\n", (unsigned int)getpid(), (unsigned int)uid);
1396 #endif
1398 #if HAVE_PTHREAD
1399 pthread_mutex_lock(&winbind_nss_mutex);
1400 #endif
1402 ZERO_STRUCT(response);
1403 ZERO_STRUCT(request);
1405 request.data.uid = uid;
1407 ret = winbindd_request_response(WINBINDD_UID_TO_SID, &request, &response);
1408 if (ret != NSS_STATUS_SUCCESS) {
1409 *errnop = errno = EINVAL;
1410 goto failed;
1413 if (buflen < strlen(response.data.sid.sid)+1) {
1414 ret = NSS_STATUS_TRYAGAIN;
1415 *errnop = errno = ERANGE;
1416 goto failed;
1419 *errnop = errno = 0;
1420 *sid = buffer;
1421 strcpy(*sid, response.data.sid.sid);
1423 failed:
1424 winbindd_free_response(&response);
1426 #if HAVE_PTHREAD
1427 pthread_mutex_unlock(&winbind_nss_mutex);
1428 #endif
1430 return ret;
1433 /* map a gid to a SID string */
1434 NSS_STATUS
1435 _nss_winbind_gidtosid(gid_t gid, char **sid, char *buffer,
1436 size_t buflen, int *errnop)
1438 NSS_STATUS ret;
1439 struct winbindd_response response;
1440 struct winbindd_request request;
1442 #ifdef DEBUG_NSS
1443 fprintf(stderr, "[%5u]: gidtosid %u\n", (unsigned int)getpid(), (unsigned int)gid);
1444 #endif
1446 #if HAVE_PTHREAD
1447 pthread_mutex_lock(&winbind_nss_mutex);
1448 #endif
1450 ZERO_STRUCT(response);
1451 ZERO_STRUCT(request);
1453 request.data.gid = gid;
1455 ret = winbindd_request_response(WINBINDD_GID_TO_SID, &request, &response);
1456 if (ret != NSS_STATUS_SUCCESS) {
1457 *errnop = errno = EINVAL;
1458 goto failed;
1461 if (buflen < strlen(response.data.sid.sid)+1) {
1462 ret = NSS_STATUS_TRYAGAIN;
1463 *errnop = errno = ERANGE;
1464 goto failed;
1467 *errnop = errno = 0;
1468 *sid = buffer;
1469 strcpy(*sid, response.data.sid.sid);
1471 failed:
1472 winbindd_free_response(&response);
1474 #if HAVE_PTHREAD
1475 pthread_mutex_unlock(&winbind_nss_mutex);
1476 #endif
1478 return ret;