Update.
[glibc.git] / grp / initgroups.c
blob6ae73687feb8bb2743d64a7cfc706964631fc5e6
1 /* Copyright (C) 1989, 91, 93, 1996-1999, 2000 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. */
19 #include <alloca.h>
20 #include <errno.h>
21 #include <grp.h>
22 #include <limits.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <nsswitch.h>
29 /* Type of the lookup function. */
30 typedef enum nss_status (*initgroups_dyn_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 *,
41 size_t, int *);
43 /* The lookup function for the first entry of this service. */
44 extern int __nss_group_lookup (service_user **nip, const char *name,
45 void **fctp);
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 **groupsp, long int limit, int *errnop)
54 struct group grpbuf;
55 size_t buflen = __sysconf (_SC_GETGR_R_SIZE_MAX);
56 char *tmpbuf;
57 enum nss_status status;
58 set_function setgrent_fct;
59 get_function getgrent_fct;
60 end_function endgrent_fct;
61 gid_t *groups = *groupsp;
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");
68 if (setgrent_fct)
70 status = DL_CALL_FCT (setgrent_fct, ());
71 if (status != NSS_STATUS_SUCCESS)
72 return status;
75 endgrent_fct = __nss_lookup_function (nip, "endgrent");
77 tmpbuf = __alloca (buflen);
81 while ((status = DL_CALL_FCT (getgrent_fct,
82 (&grpbuf, tmpbuf, buflen, errnop)),
83 status == NSS_STATUS_TRYAGAIN)
84 && *errnop == ERANGE)
86 buflen *= 2;
87 tmpbuf = __alloca (buflen);
90 if (status != NSS_STATUS_SUCCESS)
91 goto done;
93 if (grpbuf.gr_gid != group)
95 char **m;
97 for (m = grpbuf.gr_mem; *m != NULL; ++m)
98 if (strcmp (*m, user) == 0)
100 /* Matches user. Insert this group. */
101 if (__builtin_expect (*start == *size, 0))
103 /* Need a bigger buffer. */
104 gid_t *newgroups;
105 long int newsize;
107 if (limit > 0 && *size == limit)
108 /* We reached the maximum. */
109 goto done;
111 if (limit <= 0)
112 newsize = 2 * *size;
113 else
114 newsize = MIN (limit, 2 * *size);
116 newgroups = realloc (groups, newsize * sizeof (*groups));
117 if (newgroups == NULL)
118 goto done;
119 *groupsp = groups = newgroups;
120 *size = newsize;
123 groups[*start] = grpbuf.gr_gid;
124 *start += 1;
126 break;
130 while (status == NSS_STATUS_SUCCESS);
132 done:
133 if (endgrent_fct)
134 DL_CALL_FCT (endgrent_fct, ());
136 return NSS_STATUS_SUCCESS;
139 /* Initialize the group set for the current user
140 by reading the group database and using all groups
141 of which USER is a member. Also include GROUP. */
143 initgroups (user, group)
144 const char *user;
145 gid_t group;
147 #if defined NGROUPS_MAX && NGROUPS_MAX == 0
149 /* No extra groups allowed. */
150 return 0;
152 #else
154 service_user *nip = NULL;
155 initgroups_dyn_function fct;
156 enum nss_status status = NSS_STATUS_UNAVAIL;
157 int no_more;
158 /* Start is one, because we have the first group as parameter. */
159 long int start = 1;
160 long int size;
161 long int limit;
162 gid_t *groups;
163 int result;
164 #ifdef NGROUPS_MAX
165 size = NGROUPS_MAX;
166 limit = -1;
167 #else
168 long int limit = __sysconf (_SC_NGROUPS_MAX);
170 if (limit > 0)
171 size = limit;
172 else
173 /* No fixed limit on groups. Pick a starting buffer size. */
174 size = 16;
175 #endif
177 groups = (gid_t *) malloc (size * sizeof (gid_t));
178 if (__builtin_expect (groups == NULL, 0))
179 /* No more memory. */
180 return -1;
182 groups[0] = group;
184 if (__nss_group_database != NULL)
186 no_more = 0;
187 nip = __nss_group_database;
189 else
190 no_more = __nss_database_lookup ("group", NULL,
191 "compat [NOTFOUND=return] files", &nip);
193 while (! no_more)
195 fct = __nss_lookup_function (nip, "initgroups_dyn");
197 if (fct == NULL)
199 status = compat_call (nip, user, group, &start, &size, &groups,
200 limit, &errno);
202 if (nss_next_action (nip, NSS_STATUS_UNAVAIL) != NSS_ACTION_CONTINUE)
203 break;
205 else
206 status = DL_CALL_FCT (fct, (user, group, &start, &size, &groups,
207 limit, &errno));
209 /* This is really only for debugging. */
210 if (NSS_STATUS_TRYAGAIN > status || status > NSS_STATUS_RETURN)
211 __libc_fatal ("illegal status in " __FUNCTION__);
213 if (status != NSS_STATUS_SUCCESS
214 && nss_next_action (nip, status) == NSS_ACTION_RETURN)
215 break;
217 if (nip->next == NULL)
218 no_more = -1;
219 else
220 nip = nip->next;
223 /* Try to set the maximum number of groups the kernel can handle. */
225 result = setgroups (start, groups);
226 while (result == -1 && errno == EINVAL && --start > 0);
228 free (groups);
230 return result;
231 #endif