1 /* Copyright (C) 1989, 91, 93, 96, 97, 98 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. */
25 #include <sys/types.h>
29 /* Type of the lookup function. */
30 typedef enum nss_status (*initgroups_function) (const char *, gid_t,
31 long int *, long int *,
32 gid_t *, long int, int *);
33 /* Prototype for the setgrent functions we use here. */
34 typedef enum nss_status (*set_function) (void);
36 /* Prototype for the endgrent functions we use here. */
37 typedef enum nss_status (*end_function) (void);
39 /* Prototype for the setgrent functions we use here. */
40 typedef enum nss_status (*get_function) (struct group *, char *,
43 /* The lookup function for the first entry of this service. */
44 extern int __nss_group_lookup (service_user **nip, const char *name,
46 extern void *__nss_lookup_function (service_user *ni, const char *fct_name);
48 extern service_user *__nss_group_database;
50 static enum nss_status
51 compat_call (service_user *nip, const char *user, gid_t group, long int *start,
52 long int *size, gid_t *groups, long int limit, int *errnop)
54 struct group grpbuf, *g;
55 size_t buflen = sysconf (_SC_GETPW_R_SIZE_MAX);
57 enum nss_status status;
58 set_function setgrent_fct;
59 get_function getgrent_fct;
60 end_function endgrent_fct;
62 setgrent_fct = __nss_lookup_function (nip, "setgrent");
63 status = _CALL_DL_FCT (setgrent_fct, ());
64 if (status != NSS_STATUS_SUCCESS)
67 getgrent_fct = __nss_lookup_function (nip, "getgrent_r");
68 endgrent_fct = __nss_lookup_function (nip, "endgrent");
70 tmpbuf = __alloca (buflen);
74 while ((status = _CALL_DL_FCT (getgrent_fct,
75 (&grpbuf, tmpbuf, buflen, errnop)),
76 status == NSS_STATUS_TRYAGAIN)
80 tmpbuf = __alloca (buflen);
83 if (status != NSS_STATUS_SUCCESS)
87 if (g->gr_gid != group)
91 for (m = g->gr_mem; *m != NULL; ++m)
92 if (strcmp (*m, user) == 0)
94 /* Matches user. Insert this group. */
95 if (*start == *size && limit <= 0)
97 /* Need a bigger buffer. */
98 groups = realloc (groups, *size * sizeof (*groups));
104 groups[*start] = g->gr_gid;
108 /* Can't take any more groups; stop searching. */
115 while (status == NSS_STATUS_SUCCESS);
118 _CALL_DL_FCT (endgrent_fct, ());
120 return NSS_STATUS_SUCCESS;
123 /* Initialize the group set for the current user
124 by reading the group database and using all groups
125 of which USER is a member. Also include GROUP. */
127 initgroups (user, group)
131 #if defined NGROUPS_MAX && NGROUPS_MAX == 0
133 /* No extra groups allowed. */
138 service_user *nip = NULL;
139 initgroups_function fct;
140 enum nss_status status = NSS_STATUS_UNAVAIL;
142 /* Start is one, because we have the first group as parameter. */
147 # define limit NGROUPS_MAX
151 long int limit = sysconf (_SC_NGROUPS_MAX);
156 /* No fixed limit on groups. Pick a starting buffer size. */
160 groups = malloc (size * sizeof (gid_t *));
164 if (__nss_group_database != NULL)
167 nip = __nss_group_database;
170 no_more = __nss_database_lookup ("group", NULL,
171 "compat [NOTFOUND=return] files", &nip);
175 fct = __nss_lookup_function (nip, "initgroups");
178 status = compat_call (nip, user, group, &start, &size, groups,
181 status = _CALL_DL_FCT (fct, (user, group, &start, &size, groups, limit,
184 if (nip->next == NULL)
190 return setgroups (start, groups);