1 /* Copyright (C) 1998-2000,2002,2003,2004,2006 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28 #include <rpcsvc/yp.h>
29 #include <rpcsvc/ypclnt.h>
30 #include <sys/param.h>
35 /* Get the declaration of the parser function. */
37 #define STRUCTURE group
39 #include <nss/nss_files/files-parse.c>
42 static enum nss_status
43 internal_setgrent (char *domainname
, intern_t
*intern
)
45 struct ypall_callback ypcb
;
46 enum nss_status status
;
48 ypcb
.foreach
= _nis_saveit
;
49 ypcb
.data
= (char *) intern
;
50 status
= yperr2nss (yp_all (domainname
, "group.byname", &ypcb
));
52 /* Mark the last buffer as full. */
53 if (intern
->next
!= NULL
)
54 intern
->next
->size
= intern
->offset
;
56 intern
->next
= intern
->start
;
63 static enum nss_status
64 internal_getgrent_r (struct group
*grp
, char *buffer
, size_t buflen
,
65 int *errnop
, intern_t
*intern
)
67 if (intern
->start
== NULL
)
68 return NSS_STATUS_NOTFOUND
;
70 /* Get the next entry until we found a correct one. */
74 struct response_t
*bucket
= intern
->next
;
76 if (__builtin_expect (intern
->offset
>= bucket
->size
, 0))
78 if (bucket
->next
== NULL
)
79 return NSS_STATUS_NOTFOUND
;
81 /* We look at all the content in the current bucket. Go on
83 bucket
= intern
->next
= bucket
->next
;
88 for (p
= &bucket
->mem
[intern
->offset
]; isspace (*p
); ++p
)
91 size_t len
= strlen (p
) + 1;
92 if (__builtin_expect (len
> buflen
, 0))
95 return NSS_STATUS_TRYAGAIN
;
98 /* We unfortunately have to copy the data in the user-provided
99 buffer because that buffer might be around for a very long
100 time and the servent structure must remain valid. If we would
101 rely on the BUCKET memory the next 'setservent' or 'endservent'
102 call would destroy it.
104 The important thing is that it is a single NUL-terminated
105 string. This is what the parsing routine expects. */
106 p
= memcpy (buffer
, &bucket
->mem
[intern
->offset
], len
);
108 parse_res
= _nss_files_parse_grent (p
, grp
, (void *) buffer
, buflen
,
110 if (__builtin_expect (parse_res
== -1, 0))
111 return NSS_STATUS_TRYAGAIN
;
113 intern
->offset
+= len
;
117 return NSS_STATUS_SUCCESS
;
122 get_uid (const char *user
, uid_t
*uidp
)
124 size_t buflen
= sysconf (_SC_GETPW_R_SIZE_MAX
);
125 char *buf
= (char *) alloca (buflen
);
129 struct passwd result
;
132 int r
= getpwnam_r (user
, &result
, buf
, buflen
, &resp
);
133 if (r
== 0 && resp
!= NULL
)
135 *uidp
= resp
->pw_uid
;
142 extend_alloca (buf
, buflen
, 2 * buflen
);
149 static enum nss_status
150 initgroups_netid (uid_t uid
, gid_t group
, long int *start
, long int *size
,
151 gid_t
**groupsp
, long int limit
, int *errnop
,
152 const char *domainname
)
154 /* Prepare the key. The form is "unix.UID@DOMAIN" with the UID and
155 DOMAIN field filled in appropriately. */
156 char key
[sizeof ("unix.@") + sizeof (uid_t
) * 3 + strlen (domainname
)];
157 ssize_t keylen
= snprintf (key
, sizeof (key
), "unix.%lu@%s",
158 (unsigned long int) uid
, domainname
);
162 int yperr
= yp_match (domainname
, "netid.byname", key
, keylen
, &result
,
164 if (__builtin_expect (yperr
!= YPERR_SUCCESS
, 0))
165 return yperr2nss (yperr
);
167 /* Parse the result: following the colon is a comma separated list of
169 char *cp
= strchr (result
, ':');
174 return NSS_STATUS_NOTFOUND
;
176 /* Skip the colon. */
179 gid_t
*groups
= *groupsp
;
183 unsigned long int gid
= strtoul (cp
, &endp
, 0);
188 else if (*endp
!= '\0')
193 /* We do not need this group again. */
196 /* Insert this group. */
199 /* Need a bigger buffer. */
202 if (limit
> 0 && *size
== limit
)
203 /* We reached the maximum. */
209 newsize
= MIN (limit
, 2 * *size
);
211 gid_t
*newgroups
= realloc (groups
, newsize
* sizeof (*groups
));
212 if (newgroups
== NULL
)
214 *groupsp
= groups
= newgroups
;
218 groups
[*start
] = gid
;
224 return NSS_STATUS_SUCCESS
;
229 _nss_nis_initgroups_dyn (const char *user
, gid_t group
, long int *start
,
230 long int *size
, gid_t
**groupsp
, long int limit
,
233 /* We always need the domain name. */
235 if (yp_get_default_domain (&domainname
))
236 return NSS_STATUS_UNAVAIL
;
238 /* Check whether we are supposed to use the netid.byname map. */
239 if (_nsl_default_nss () & NSS_FLAG_NETID_AUTHORITATIVE
)
241 /* We need the user ID. */
244 if (get_uid (user
, &uid
) == 0
245 && initgroups_netid (uid
, group
, start
, size
, groupsp
, limit
,
246 errnop
, domainname
) == NSS_STATUS_SUCCESS
)
247 return NSS_STATUS_SUCCESS
;
250 struct group grpbuf
, *g
;
251 size_t buflen
= sysconf (_SC_GETPW_R_SIZE_MAX
);
253 enum nss_status status
;
254 intern_t intern
= { NULL
, NULL
, 0 };
255 gid_t
*groups
= *groupsp
;
257 status
= internal_setgrent (domainname
, &intern
);
258 if (status
!= NSS_STATUS_SUCCESS
)
261 tmpbuf
= __alloca (buflen
);
266 internal_getgrent_r (&grpbuf
, tmpbuf
, buflen
, errnop
,
267 &intern
)) == NSS_STATUS_TRYAGAIN
268 && *errnop
== ERANGE
)
269 tmpbuf
= extend_alloca (tmpbuf
, buflen
, 2 * buflen
);
271 if (status
!= NSS_STATUS_SUCCESS
)
276 if (g
->gr_gid
!= group
)
280 for (m
= g
->gr_mem
; *m
!= NULL
; ++m
)
281 if (strcmp (*m
, user
) == 0)
283 /* Matches user. Insert this group. */
286 /* Need a bigger buffer. */
290 if (limit
> 0 && *size
== limit
)
291 /* We reached the maximum. */
297 newsize
= MIN (limit
, 2 * *size
);
299 newgroups
= realloc (groups
, newsize
* sizeof (*groups
));
300 if (newgroups
== NULL
)
302 *groupsp
= groups
= newgroups
;
306 groups
[*start
] = g
->gr_gid
;
313 while (status
== NSS_STATUS_SUCCESS
);
316 while (intern
.start
!= NULL
)
318 intern
.next
= intern
.start
;
319 intern
.start
= intern
.start
->next
;
323 return NSS_STATUS_SUCCESS
;