1 /* Copyright (C) 1989, 91, 93, 96, 97, 98, 99 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
26 #include <sys/types.h>
30 /* Type of the lookup function. */
31 typedef enum nss_status (*initgroups_function) (const char *, gid_t,
32 long int *, long int *,
33 gid_t *, long int, int *);
34 /* Prototype for the setgrent functions we use here. */
35 typedef enum nss_status (*set_function) (void);
37 /* Prototype for the endgrent functions we use here. */
38 typedef enum nss_status (*end_function) (void);
40 /* Prototype for the setgrent functions we use here. */
41 typedef enum nss_status (*get_function) (struct group *, char *,
44 /* The lookup function for the first entry of this service. */
45 extern int __nss_group_lookup (service_user **nip, const char *name,
47 extern void *__nss_lookup_function (service_user *ni, const char *fct_name);
49 extern service_user *__nss_group_database;
51 static enum nss_status
52 compat_call (service_user *nip, const char *user, gid_t group, long int *start,
53 long int *size, gid_t *groups, long int limit, int *errnop)
56 size_t buflen = __sysconf (_SC_GETPW_R_SIZE_MAX);
58 enum nss_status status;
59 set_function setgrent_fct;
60 get_function getgrent_fct;
61 end_function endgrent_fct;
63 getgrent_fct = __nss_lookup_function (nip, "getgrent_r");
64 if (getgrent_fct == NULL)
65 return NSS_STATUS_UNAVAIL;
67 setgrent_fct = __nss_lookup_function (nip, "setgrent");
70 status = _CALL_DL_FCT (setgrent_fct, ());
71 if (status != NSS_STATUS_SUCCESS)
75 endgrent_fct = __nss_lookup_function (nip, "endgrent");
77 tmpbuf = __alloca (buflen);
81 while ((status = _CALL_DL_FCT (getgrent_fct,
82 (&grpbuf, tmpbuf, buflen, errnop)),
83 status == NSS_STATUS_TRYAGAIN)
87 tmpbuf = __alloca (buflen);
90 if (status != NSS_STATUS_SUCCESS)
93 if (grpbuf.gr_gid != group)
97 for (m = grpbuf.gr_mem; *m != NULL; ++m)
98 if (strcmp (*m, user) == 0)
100 /* Matches user. Insert this group. */
101 if (*start == *size && limit <= 0)
103 /* Need a bigger buffer. */
104 groups = realloc (groups, 2 * *size * sizeof (*groups));
110 groups[*start] = grpbuf.gr_gid;
114 /* Can't take any more groups; stop searching. */
121 while (status == NSS_STATUS_SUCCESS);
125 _CALL_DL_FCT (endgrent_fct, ());
127 return NSS_STATUS_SUCCESS;
130 /* Initialize the group set for the current user
131 by reading the group database and using all groups
132 of which USER is a member. Also include GROUP. */
134 initgroups (user, group)
138 #if defined NGROUPS_MAX && NGROUPS_MAX == 0
140 /* No extra groups allowed. */
145 service_user *nip = NULL;
146 initgroups_function fct;
147 enum nss_status status = NSS_STATUS_UNAVAIL;
149 /* Start is one, because we have the first group as parameter. */
154 # define limit NGROUPS_MAX
158 long int limit = __sysconf (_SC_NGROUPS_MAX);
163 /* No fixed limit on groups. Pick a starting buffer size. */
167 groups = malloc (size * sizeof (gid_t *));
171 if (__nss_group_database != NULL)
174 nip = __nss_group_database;
177 no_more = __nss_database_lookup ("group", NULL,
178 "compat [NOTFOUND=return] files", &nip);
182 fct = __nss_lookup_function (nip, "initgroups");
186 status = compat_call (nip, user, group, &start, &size, groups,
189 if (nss_next_action (nip, NSS_STATUS_UNAVAIL) != NSS_ACTION_CONTINUE)
193 status = _CALL_DL_FCT (fct, (user, group, &start, &size, groups, limit,
196 /* This is really only for debugging. */
197 if (NSS_STATUS_TRYAGAIN > status || status > NSS_STATUS_RETURN)
198 __libc_fatal ("illegal status in " __FUNCTION__);
200 if (status != NSS_STATUS_SUCCESS
201 && nss_next_action (nip, status) == NSS_ACTION_RETURN)
204 if (nip->next == NULL)
210 return setgroups (start, groups);