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"
29 static pthread_mutex_t winbind_nss_mutex = PTHREAD_MUTEX_INITIALIZER;
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 ************************************************************************/
43 static const char *nss_err_str(NSS_STATUS 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";
59 return "UNKNOWN RETURN CODE!!!!!!!";
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_*
70 static char *get_static(char **buffer, size_t *buflen, size_t len)
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)) {
81 /* Return an index into the static buffer */
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,
111 /* default to simple separators */
116 /* find the first non sep char */
117 while (*s && strchr(sep,*s)) {
126 /* When restarting we need to go from here. */
129 /* Work out the length needed. */
130 for (quoted = false; *s &&
131 (quoted || !strchr(sep,*s)); s++) {
139 /* We started with len = 1 so we have space for the nul. */
140 *pp_buff = (char *)malloc(len);
145 /* copy over the token */
148 for (quoted = false; *s &&
149 (quoted || !strchr(sep,*s)); s++) {
157 *ptr = (*s) ? s+1 : s;
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)
174 len = strlen(pw->pw_name) + 1;
176 if ((result->pw_name =
177 get_static(buffer, buflen, len)) == NULL) {
181 return NSS_STATUS_TRYAGAIN;
184 memcpy(result->pw_name, pw->pw_name, len);
187 len = strlen(pw->pw_passwd) + 1;
189 if ((result->pw_passwd =
190 get_static(buffer, buflen, len)) == NULL) {
194 return NSS_STATUS_TRYAGAIN;
197 memcpy(result->pw_passwd, pw->pw_passwd, len);
201 result->pw_uid = pw->pw_uid;
202 result->pw_gid = pw->pw_gid;
205 len = strlen(pw->pw_gecos) + 1;
207 if ((result->pw_gecos =
208 get_static(buffer, buflen, len)) == NULL) {
212 return NSS_STATUS_TRYAGAIN;
215 memcpy(result->pw_gecos, pw->pw_gecos, len);
218 len = strlen(pw->pw_dir) + 1;
220 if ((result->pw_dir =
221 get_static(buffer, buflen, len)) == NULL) {
225 return NSS_STATUS_TRYAGAIN;
228 memcpy(result->pw_dir, pw->pw_dir, len);
231 len = strlen(pw->pw_shell) + 1;
233 if ((result->pw_shell =
234 get_static(buffer, buflen, len)) == NULL) {
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 = "";
250 #if HAVE_PASSWD_PW_AGE
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)
270 len = strlen(gr->gr_name) + 1;
272 if ((result->gr_name =
273 get_static(buffer, buflen, len)) == NULL) {
277 return NSS_STATUS_TRYAGAIN;
280 memcpy(result->gr_name, gr->gr_name, len);
283 len = strlen(gr->gr_passwd) + 1;
285 if ((result->gr_passwd =
286 get_static(buffer, buflen, len)) == NULL) {
289 return NSS_STATUS_TRYAGAIN;
292 memcpy(result->gr_passwd, gr->gr_passwd, len);
296 result->gr_gid = gr->gr_gid;
298 /* Group membership */
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) {
315 return NSS_STATUS_TRYAGAIN;
317 result->gr_mem = (char **)(tst + i);
319 if (gr->num_gr_mem == 0) {
323 *(result->gr_mem) = NULL;
324 return NSS_STATUS_SUCCESS;
327 /* Start looking at extra data */
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) {
339 return NSS_STATUS_TRYAGAIN;
341 memcpy((result->gr_mem)[i], name, len);
348 (result->gr_mem)[i] = NULL;
350 return NSS_STATUS_SUCCESS;
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 */
365 _nss_winbind_setpwent(void)
369 fprintf(stderr, "[%5d]: setpwent\n", getpid());
373 pthread_mutex_lock(&winbind_nss_mutex);
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);
383 fprintf(stderr, "[%5d]: setpwent returns %s (%d)\n", getpid(),
384 nss_err_str(ret), ret);
388 pthread_mutex_unlock(&winbind_nss_mutex);
393 /* Close ntdom password database "file pointer" */
396 _nss_winbind_endpwent(void)
400 fprintf(stderr, "[%5d]: endpwent\n", getpid());
404 pthread_mutex_lock(&winbind_nss_mutex);
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);
414 fprintf(stderr, "[%5d]: endpwent returns %s (%d)\n", getpid(),
415 nss_err_str(ret), ret);
419 pthread_mutex_unlock(&winbind_nss_mutex);
425 /* Fetch the next password entry from ntdom password database */
428 _nss_winbind_getpwent_r(struct passwd *result, char *buffer,
429 size_t buflen, int *errnop)
432 struct winbindd_request request;
433 static int called_again;
436 fprintf(stderr, "[%5d]: getpwent\n", getpid());
440 pthread_mutex_lock(&winbind_nss_mutex);
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) {
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,
464 if (ret == NSS_STATUS_SUCCESS) {
465 struct winbindd_pw *pw_cache;
470 num_pw_cache = getpwent_response.data.num_entries;
472 /* Return a 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;
486 ret = fill_pwent(result, &pw_cache[ndx_pw_cache],
489 /* Out of memory - try again */
491 if (ret == NSS_STATUS_TRYAGAIN) {
493 *errnop = errno = ERANGE;
498 called_again = false;
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);
510 fprintf(stderr, "[%5d]: getpwent returns %s (%d)\n", getpid(),
511 nss_err_str(ret), ret);
515 pthread_mutex_unlock(&winbind_nss_mutex);
520 /* Return passwd struct from uid */
523 _nss_winbind_getpwuid_r(uid_t uid, struct passwd *result, char *buffer,
524 size_t buflen, int *errnop)
527 static struct winbindd_response response;
528 struct winbindd_request request;
529 static int keep_response;
532 fprintf(stderr, "[%5d]: getpwuid_r %d\n", getpid(), (unsigned int)uid);
536 pthread_mutex_lock(&winbind_nss_mutex);
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,
555 if (ret == NSS_STATUS_TRYAGAIN) {
556 keep_response = true;
557 *errnop = errno = ERANGE;
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;
573 keep_response = false;
577 winbindd_free_response(&response);
582 fprintf(stderr, "[%5d]: getpwuid %d returns %s (%d)\n", getpid(),
583 (unsigned int)uid, nss_err_str(ret), ret);
587 pthread_mutex_unlock(&winbind_nss_mutex);
593 /* Return passwd struct from username */
595 _nss_winbind_getpwnam_r(const char *name, struct passwd *result, char *buffer,
596 size_t buflen, int *errnop)
599 static struct winbindd_response response;
600 struct winbindd_request request;
601 static int keep_response;
604 fprintf(stderr, "[%5d]: getpwnam_r %s\n", getpid(), name);
608 pthread_mutex_lock(&winbind_nss_mutex);
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,
631 if (ret == NSS_STATUS_TRYAGAIN) {
632 keep_response = true;
633 *errnop = errno = ERANGE;
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;
650 keep_response = false;
654 winbindd_free_response(&response);
657 fprintf(stderr, "[%5d]: getpwnam %s returns %s (%d)\n", getpid(),
658 name, nss_err_str(ret), ret);
662 pthread_mutex_unlock(&winbind_nss_mutex);
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 */
680 _nss_winbind_setgrent(void)
684 fprintf(stderr, "[%5d]: setgrent\n", getpid());
688 pthread_mutex_lock(&winbind_nss_mutex);
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);
698 fprintf(stderr, "[%5d]: setgrent returns %s (%d)\n", getpid(),
699 nss_err_str(ret), ret);
703 pthread_mutex_unlock(&winbind_nss_mutex);
709 /* Close "file pointer" for ntdom group database */
712 _nss_winbind_endgrent(void)
716 fprintf(stderr, "[%5d]: endgrent\n", getpid());
720 pthread_mutex_lock(&winbind_nss_mutex);
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);
730 fprintf(stderr, "[%5d]: endgrent returns %s (%d)\n", getpid(),
731 nss_err_str(ret), ret);
735 pthread_mutex_unlock(&winbind_nss_mutex);
741 /* Get next entry from ntdom group database */
744 winbind_getgrent(enum winbindd_cmd cmd,
745 struct group *result,
746 char *buffer, size_t buflen, int *errnop)
749 static struct winbindd_request request;
750 static int called_again;
754 fprintf(stderr, "[%5d]: getgrent\n", getpid());
758 pthread_mutex_lock(&winbind_nss_mutex);
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) {
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,
782 if (ret == NSS_STATUS_SUCCESS) {
783 struct winbindd_gr *gr_cache;
789 num_gr_cache = getgrent_response.data.num_entries;
791 /* Return a 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;
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,
816 /* Out of memory - try again */
818 if (ret == NSS_STATUS_TRYAGAIN) {
820 *errnop = errno = ERANGE;
825 called_again = false;
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);
837 fprintf(stderr, "[%5d]: getgrent returns %s (%d)\n", getpid(),
838 nss_err_str(ret), ret);
842 pthread_mutex_unlock(&winbind_nss_mutex);
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);
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 */
866 _nss_winbind_getgrnam_r(const char *name,
867 struct group *result, char *buffer,
868 size_t buflen, int *errnop)
871 static struct winbindd_response response;
872 struct winbindd_request request;
873 static int keep_response;
876 fprintf(stderr, "[%5d]: getgrnam %s\n", getpid(), name);
880 pthread_mutex_lock(&winbind_nss_mutex);
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,
906 if (ret == NSS_STATUS_TRYAGAIN) {
907 keep_response = true;
908 *errnop = errno = ERANGE;
915 /* We've been called again */
917 ret = fill_grent(result, &response.data.gr,
918 (char *)response.extra_data.data, &buffer,
921 if (ret == NSS_STATUS_TRYAGAIN) {
922 keep_response = true;
923 *errnop = errno = ERANGE;
927 keep_response = false;
931 winbindd_free_response(&response);
934 fprintf(stderr, "[%5d]: getgrnam %s returns %s (%d)\n", getpid(),
935 name, nss_err_str(ret), ret);
939 pthread_mutex_unlock(&winbind_nss_mutex);
945 /* Return group struct from gid */
948 _nss_winbind_getgrgid_r(gid_t gid,
949 struct group *result, char *buffer,
950 size_t buflen, int *errnop)
953 static struct winbindd_response response;
954 struct winbindd_request request;
955 static int keep_response;
958 fprintf(stderr, "[%5d]: getgrgid %d\n", getpid(), gid);
962 pthread_mutex_lock(&winbind_nss_mutex);
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,
986 if (ret == NSS_STATUS_TRYAGAIN) {
987 keep_response = true;
988 *errnop = errno = ERANGE;
995 /* We've been called again */
997 ret = fill_grent(result, &response.data.gr,
998 (char *)response.extra_data.data, &buffer,
1001 if (ret == NSS_STATUS_TRYAGAIN) {
1002 keep_response = true;
1003 *errnop = errno = ERANGE;
1007 keep_response = false;
1011 winbindd_free_response(&response);
1014 fprintf(stderr, "[%5d]: getgrgid %d returns %s (%d)\n", getpid(),
1015 (unsigned int)gid, nss_err_str(ret), ret);
1019 pthread_mutex_unlock(&winbind_nss_mutex);
1024 /* Initialise supplementary groups */
1027 _nss_winbind_initgroups_dyn(const char *user, gid_t group, long int *start,
1028 long int *size, gid_t **groups, long int limit,
1032 struct winbindd_request request;
1033 struct winbindd_response response;
1037 fprintf(stderr, "[%5d]: initgroups %s (%d)\n", getpid(),
1042 pthread_mutex_lock(&winbind_nss_mutex);
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;
1059 fprintf(stderr, "[%5d]: initgroups %s: got NSS_STATUS_SUCCESS "
1060 "and %d gids\n", getpid(),
1063 if (gid_list == NULL) {
1064 ret = NSS_STATUS_NOTFOUND;
1068 /* Copy group list to client */
1070 for (i = 0; i < num_gids; i++) {
1073 fprintf(stderr, "[%5d]: initgroups %s (%d): "
1074 "processing gid %d \n", getpid(),
1075 user, group, gid_list[i]);
1078 /* Skip primary group */
1080 if (gid_list[i] == group) {
1084 /* Skip groups without a mapping */
1085 if (gid_list[i] == (uid_t)-1) {
1089 /* Filled buffer ? If so, resize. */
1091 if (*start == *size) {
1095 newsize = 2 * (*size);
1097 if (*size == limit) {
1100 if (newsize > limit) {
1105 newgroups = (gid_t *)
1107 newsize * sizeof(**groups));
1110 ret = NSS_STATUS_NOTFOUND;
1113 *groups = newgroups;
1119 (*groups)[*start] = gid_list[i];
1124 /* Back to your regularly scheduled programming */
1128 fprintf(stderr, "[%5d]: initgroups %s returns %s (%d)\n", getpid(),
1129 user, nss_err_str(ret), ret);
1133 pthread_mutex_unlock(&winbind_nss_mutex);