1 /* Copyright (C) 1998-2014 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
27 #include <rpcsvc/yp.h>
28 #include <rpcsvc/ypclnt.h>
29 #include <sys/param.h>
34 /* Get the declaration of the parser function. */
36 #define STRUCTURE group
38 #include <nss/nss_files/files-parse.c>
41 static enum nss_status
42 internal_setgrent (char *domainname, intern_t *intern)
44 struct ypall_callback ypcb;
45 enum nss_status status;
47 ypcb.foreach = _nis_saveit;
48 ypcb.data = (char *) intern;
49 status = yperr2nss (yp_all (domainname, "group.byname", &ypcb));
51 /* Mark the last buffer as full. */
52 if (intern->next != NULL)
53 intern->next->size = intern->offset;
55 intern->next = intern->start;
62 static enum nss_status
63 internal_getgrent_r (struct group *grp, char *buffer, size_t buflen,
64 int *errnop, intern_t *intern)
66 if (intern->start == NULL)
67 return NSS_STATUS_NOTFOUND;
69 /* Get the next entry until we found a correct one. */
73 struct response_t *bucket = intern->next;
75 if (__glibc_unlikely (intern->offset >= bucket->size))
77 if (bucket->next == NULL)
78 return NSS_STATUS_NOTFOUND;
80 /* We look at all the content in the current bucket. Go on
82 bucket = intern->next = bucket->next;
87 for (p = &bucket->mem[intern->offset]; isspace (*p); ++p)
90 size_t len = strlen (p) + 1;
91 if (__glibc_unlikely (len > buflen))
94 return NSS_STATUS_TRYAGAIN;
97 /* We unfortunately have to copy the data in the user-provided
98 buffer because that buffer might be around for a very long
99 time and the servent structure must remain valid. If we would
100 rely on the BUCKET memory the next 'setservent' or 'endservent'
101 call would destroy it.
103 The important thing is that it is a single NUL-terminated
104 string. This is what the parsing routine expects. */
105 p = memcpy (buffer, &bucket->mem[intern->offset], len);
107 parse_res = _nss_files_parse_grent (p, grp, (void *) buffer, buflen,
109 if (__glibc_unlikely (parse_res == -1))
110 return NSS_STATUS_TRYAGAIN;
112 intern->offset += len;
116 return NSS_STATUS_SUCCESS;
121 get_uid (const char *user, uid_t *uidp)
123 size_t buflen = sysconf (_SC_GETPW_R_SIZE_MAX);
124 char *buf = (char *) alloca (buflen);
128 struct passwd result;
131 int r = getpwnam_r (user, &result, buf, buflen, &resp);
132 if (r == 0 && resp != NULL)
134 *uidp = resp->pw_uid;
141 buf = extend_alloca (buf, buflen, 2 * buflen);
148 static enum nss_status
149 initgroups_netid (uid_t uid, gid_t group, long int *start, long int *size,
150 gid_t **groupsp, long int limit, int *errnop,
151 const char *domainname)
153 /* Prepare the key. The form is "unix.UID@DOMAIN" with the UID and
154 DOMAIN field filled in appropriately. */
155 char key[sizeof ("unix.@") + sizeof (uid_t) * 3 + strlen (domainname)];
156 ssize_t keylen = snprintf (key, sizeof (key), "unix.%lu@%s",
157 (unsigned long int) uid, domainname);
161 int yperr = yp_match (domainname, "netid.byname", key, keylen, &result,
163 if (__glibc_unlikely (yperr != YPERR_SUCCESS))
164 return yperr2nss (yperr);
166 /* Parse the result: following the colon is a comma separated list of
168 char *cp = strchr (result, ':');
173 return NSS_STATUS_NOTFOUND;
175 /* Skip the colon. */
178 gid_t *groups = *groupsp;
182 unsigned long int gid = strtoul (cp, &endp, 0);
187 else if (*endp != '\0')
192 /* We do not need this group again. */
195 /* Insert this group. */
198 /* Need a bigger buffer. */
201 if (limit > 0 && *size == limit)
202 /* We reached the maximum. */
208 newsize = MIN (limit, 2 * *size);
210 gid_t *newgroups = realloc (groups, newsize * sizeof (*groups));
211 if (newgroups == NULL)
213 *groupsp = groups = newgroups;
217 groups[*start] = gid;
223 return NSS_STATUS_SUCCESS;
228 _nss_nis_initgroups_dyn (const char *user, gid_t group, long int *start,
229 long int *size, gid_t **groupsp, long int limit,
232 /* We always need the domain name. */
234 if (yp_get_default_domain (&domainname))
235 return NSS_STATUS_UNAVAIL;
237 /* Check whether we are supposed to use the netid.byname map. */
238 if (_nsl_default_nss () & NSS_FLAG_NETID_AUTHORITATIVE)
240 /* We need the user ID. */
243 if (get_uid (user, &uid) == 0
244 && initgroups_netid (uid, group, start, size, groupsp, limit,
245 errnop, domainname) == NSS_STATUS_SUCCESS)
246 return NSS_STATUS_SUCCESS;
249 struct group grpbuf, *g;
250 size_t buflen = sysconf (_SC_GETPW_R_SIZE_MAX);
252 enum nss_status status;
253 intern_t intern = { NULL, NULL, 0 };
254 gid_t *groups = *groupsp;
256 status = internal_setgrent (domainname, &intern);
257 if (status != NSS_STATUS_SUCCESS)
260 tmpbuf = __alloca (buflen);
265 internal_getgrent_r (&grpbuf, tmpbuf, buflen, errnop,
266 &intern)) == NSS_STATUS_TRYAGAIN
267 && *errnop == ERANGE)
268 tmpbuf = extend_alloca (tmpbuf, buflen, 2 * buflen);
270 if (status != NSS_STATUS_SUCCESS)
275 if (g->gr_gid != group)
279 for (m = g->gr_mem; *m != NULL; ++m)
280 if (strcmp (*m, user) == 0)
282 /* Matches user. Insert this group. */
285 /* Need a bigger buffer. */
289 if (limit > 0 && *size == limit)
290 /* We reached the maximum. */
296 newsize = MIN (limit, 2 * *size);
298 newgroups = realloc (groups, newsize * sizeof (*groups));
299 if (newgroups == NULL)
301 *groupsp = groups = newgroups;
305 groups[*start] = g->gr_gid;
312 while (status == NSS_STATUS_SUCCESS);
315 while (intern.start != NULL)
317 intern.next = intern.start;
318 intern.start = intern.start->next;
322 return NSS_STATUS_SUCCESS;