1 /* Copyright (C) 1998-2020 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 <https://www.gnu.org/licenses/>. */
26 #include <rpcsvc/yp.h>
27 #include <rpcsvc/ypclnt.h>
28 #include <sys/param.h>
29 #include <scratch_buffer.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 struct scratch_buffer tmpbuf
;
124 scratch_buffer_init (&tmpbuf
);
128 struct passwd result
;
131 int r
= getpwnam_r (user
, &result
, tmpbuf
.data
, tmpbuf
.length
, &resp
);
132 if (r
== 0 && resp
!= NULL
)
134 *uidp
= resp
->pw_uid
;
135 scratch_buffer_free (&tmpbuf
);
142 if (!scratch_buffer_grow (&tmpbuf
))
146 scratch_buffer_free (&tmpbuf
);
151 static enum nss_status
152 initgroups_netid (uid_t uid
, gid_t group
, long int *start
, long int *size
,
153 gid_t
**groupsp
, long int limit
, int *errnop
,
154 const char *domainname
)
156 /* Limit domainname length to the maximum size of an RPC packet. */
157 if (strlen (domainname
) > UDPMSGSIZE
)
160 return NSS_STATUS_UNAVAIL
;
163 /* Prepare the key. The form is "unix.UID@DOMAIN" with the UID and
164 DOMAIN field filled in appropriately. */
165 char key
[sizeof ("unix.@") + sizeof (uid_t
) * 3 + strlen (domainname
)];
166 ssize_t keylen
= snprintf (key
, sizeof (key
), "unix.%lu@%s",
167 (unsigned long int) uid
, domainname
);
171 int yperr
= yp_match (domainname
, "netid.byname", key
, keylen
, &result
,
173 if (__glibc_unlikely (yperr
!= YPERR_SUCCESS
))
174 return yperr2nss (yperr
);
176 /* Parse the result: following the colon is a comma separated list of
178 char *cp
= strchr (result
, ':');
183 return NSS_STATUS_NOTFOUND
;
185 /* Skip the colon. */
188 gid_t
*groups
= *groupsp
;
192 unsigned long int gid
= strtoul (cp
, &endp
, 0);
197 else if (*endp
!= '\0')
202 /* We do not need this group again. */
205 /* Insert this group. */
208 /* Need a bigger buffer. */
211 if (limit
> 0 && *size
== limit
)
212 /* We reached the maximum. */
218 newsize
= MIN (limit
, 2 * *size
);
220 gid_t
*newgroups
= realloc (groups
, newsize
* sizeof (*groups
));
221 if (newgroups
== NULL
)
223 *groupsp
= groups
= newgroups
;
227 groups
[*start
] = gid
;
233 return NSS_STATUS_SUCCESS
;
238 _nss_nis_initgroups_dyn (const char *user
, gid_t group
, long int *start
,
239 long int *size
, gid_t
**groupsp
, long int limit
,
242 /* We always need the domain name. */
244 if (yp_get_default_domain (&domainname
))
245 return NSS_STATUS_UNAVAIL
;
247 /* Check whether we are supposed to use the netid.byname map. */
248 if (_nsl_default_nss () & NSS_FLAG_NETID_AUTHORITATIVE
)
250 /* We need the user ID. */
253 if (get_uid (user
, &uid
) == 0
254 && initgroups_netid (uid
, group
, start
, size
, groupsp
, limit
,
255 errnop
, domainname
) == NSS_STATUS_SUCCESS
)
256 return NSS_STATUS_SUCCESS
;
259 struct group grpbuf
, *g
;
260 enum nss_status status
;
261 intern_t intern
= { NULL
, NULL
, 0 };
262 gid_t
*groups
= *groupsp
;
264 status
= internal_setgrent (domainname
, &intern
);
265 if (status
!= NSS_STATUS_SUCCESS
)
268 struct scratch_buffer tmpbuf
;
269 scratch_buffer_init (&tmpbuf
);
274 internal_getgrent_r (&grpbuf
, tmpbuf
.data
, tmpbuf
.length
, errnop
,
275 &intern
)) == NSS_STATUS_TRYAGAIN
276 && *errnop
== ERANGE
)
277 if (!scratch_buffer_grow (&tmpbuf
))
279 status
= NSS_STATUS_TRYAGAIN
;
283 if (status
!= NSS_STATUS_SUCCESS
)
285 if (status
== NSS_STATUS_NOTFOUND
)
286 status
= NSS_STATUS_SUCCESS
;
291 if (g
->gr_gid
!= group
)
295 for (m
= g
->gr_mem
; *m
!= NULL
; ++m
)
296 if (strcmp (*m
, user
) == 0)
298 /* Matches user. Insert this group. */
301 /* Need a bigger buffer. */
305 if (limit
> 0 && *size
== limit
)
306 /* We reached the maximum. */
312 newsize
= MIN (limit
, 2 * *size
);
314 newgroups
= realloc (groups
, newsize
* sizeof (*groups
));
315 if (newgroups
== NULL
)
317 status
= NSS_STATUS_TRYAGAIN
;
321 *groupsp
= groups
= newgroups
;
325 groups
[*start
] = g
->gr_gid
;
334 while (intern
.start
!= NULL
)
336 intern
.next
= intern
.start
;
337 intern
.start
= intern
.start
->next
;
340 scratch_buffer_free (&tmpbuf
);