1 /* Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1998.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
26 #include <rpcsvc/yp.h>
27 #include <rpcsvc/ypclnt.h>
31 /* Get the declaration of the parser function. */
33 #define STRUCTURE group
35 #include <nss/nss_files/files-parse.c>
40 struct response_t *next;
45 struct response_t *start;
46 struct response_t *next;
48 typedef struct intern_t intern_t;
51 saveit (int instatus, char *inkey, int inkeylen, char *inval,
52 int invallen, char *indata)
54 intern_t *intern = (intern_t *) indata;
56 if (instatus != YP_TRUE)
59 if (inkey && inkeylen > 0 && inval && invallen > 0)
61 if (intern->start == NULL)
63 intern->start = malloc (sizeof (struct response_t));
64 if (intern->start == NULL)
66 intern->next = intern->start;
70 intern->next->next = malloc (sizeof (struct response_t));
71 if (intern->next->next == NULL)
73 intern->next = intern->next->next;
75 intern->next->next = NULL;
76 intern->next->val = malloc (invallen + 1);
77 if (intern->next->val == NULL)
79 strncpy (intern->next->val, inval, invallen);
80 intern->next->val[invallen] = '\0';
86 static enum nss_status
87 internal_setgrent (intern_t *intern)
90 struct ypall_callback ypcb;
91 enum nss_status status;
93 if (yp_get_default_domain (&domainname))
94 return NSS_STATUS_UNAVAIL;
98 ypcb.foreach = saveit;
99 ypcb.data = (char *) intern;
100 status = yperr2nss (yp_all (domainname, "group.byname", &ypcb));
101 intern->next = intern->start;
106 static enum nss_status
107 internal_getgrent_r (struct group *grp, char *buffer, size_t buflen,
108 int *errnop, intern_t *intern)
110 struct parser_data *data = (void *) buffer;
114 if (intern->start == NULL)
115 internal_setgrent (intern);
117 /* Get the next entry until we found a correct one. */
120 if (intern->next == NULL)
123 return NSS_STATUS_NOTFOUND;
125 p = strncpy (buffer, intern->next->val, buflen);
129 parse_res = _nss_files_parse_grent (p, grp, data, buflen, errnop);
131 return NSS_STATUS_TRYAGAIN;
132 intern->next = intern->next->next;
136 return NSS_STATUS_SUCCESS;
140 _nss_nis_initgroups (const char *user, gid_t group, long int *start,
141 long int *size, gid_t *groups, long int limit,
144 struct group grpbuf, *g;
145 size_t buflen = sysconf (_SC_GETPW_R_SIZE_MAX);
147 enum nss_status status;
148 intern_t intern = { NULL, NULL };
150 status = internal_setgrent (&intern);
151 if (status != NSS_STATUS_SUCCESS)
154 tmpbuf = __alloca (buflen);
159 internal_getgrent_r (&grpbuf, tmpbuf, buflen, errnop,
160 &intern)) == NSS_STATUS_TRYAGAIN
161 && *errnop == ERANGE)
164 tmpbuf = __alloca (buflen);
167 if (status != NSS_STATUS_SUCCESS)
172 if (g->gr_gid != group)
176 for (m = g->gr_mem; *m != NULL; ++m)
177 if (strcmp (*m, user) == 0)
179 /* Matches user. Insert this group. */
180 if (*start == *size && limit <= 0)
182 /* Need a bigger buffer. */
183 groups = realloc (groups, 2 * *size * sizeof (*groups));
189 groups[*start] = g->gr_gid;
193 /* Can't take any more groups; stop searching. */
200 while (status == NSS_STATUS_SUCCESS);
203 while (intern.start != NULL)
205 if (intern.start->val != NULL)
206 free (intern.start->val);
207 intern.next = intern.start;
208 intern.start = intern.start->next;
212 return NSS_STATUS_SUCCESS;