Update.
[platform/upstream/glibc.git] / grp / initgroups.c
1 /* Copyright (C) 1989,91,93,1996-2002, 2003 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8
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    Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, write to the Free
16    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17    02111-1307 USA.  */
18
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/param.h>
27 #include <sys/types.h>
28 #include <nsswitch.h>
29
30 /* Type of the lookup function.  */
31 typedef enum nss_status (*initgroups_dyn_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);
36
37 /* Prototype for the endgrent functions we use here.  */
38 typedef enum nss_status (*end_function) (void);
39
40 /* Prototype for the setgrent functions we use here.  */
41 typedef enum nss_status (*get_function) (struct group *, char *,
42                                          size_t, int *);
43
44 /* The lookup function for the first entry of this service.  */
45 extern int __nss_group_lookup (service_user **nip, const char *name,
46                                    void **fctp);
47 extern void *__nss_lookup_function (service_user *ni, const char *fct_name);
48
49 extern service_user *__nss_group_database attribute_hidden;
50
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 **groupsp, long int limit, int *errnop)
54 {
55   struct group grpbuf;
56   size_t buflen = __sysconf (_SC_GETGR_R_SIZE_MAX);
57   char *tmpbuf;
58   enum nss_status status;
59   set_function setgrent_fct;
60   get_function getgrent_fct;
61   end_function endgrent_fct;
62   gid_t *groups = *groupsp;
63
64   getgrent_fct = __nss_lookup_function (nip, "getgrent_r");
65   if (getgrent_fct == NULL)
66     return NSS_STATUS_UNAVAIL;
67
68   setgrent_fct = __nss_lookup_function (nip, "setgrent");
69   if (setgrent_fct)
70     {
71       status = DL_CALL_FCT (setgrent_fct, ());
72       if (status != NSS_STATUS_SUCCESS)
73         return status;
74     }
75
76   endgrent_fct = __nss_lookup_function (nip, "endgrent");
77
78   tmpbuf = __alloca (buflen);
79
80   do
81     {
82       while ((status = DL_CALL_FCT (getgrent_fct,
83                                      (&grpbuf, tmpbuf, buflen, errnop)),
84               status == NSS_STATUS_TRYAGAIN)
85              && *errnop == ERANGE)
86         {
87           buflen *= 2;
88           tmpbuf = __alloca (buflen);
89         }
90
91       if (status != NSS_STATUS_SUCCESS)
92         goto done;
93
94       if (grpbuf.gr_gid != group)
95         {
96           char **m;
97
98           for (m = grpbuf.gr_mem; *m != NULL; ++m)
99             if (strcmp (*m, user) == 0)
100               {
101                 /* Matches user.  Insert this group.  */
102                 if (__builtin_expect (*start == *size, 0))
103                   {
104                     /* Need a bigger buffer.  */
105                     gid_t *newgroups;
106                     long int newsize;
107
108                     if (limit > 0 && *size == limit)
109                       /* We reached the maximum.  */
110                       goto done;
111
112                     if (limit <= 0)
113                       newsize = 2 * *size;
114                     else
115                       newsize = MIN (limit, 2 * *size);
116
117                     newgroups = realloc (groups, newsize * sizeof (*groups));
118                     if (newgroups == NULL)
119                       goto done;
120                     *groupsp = groups = newgroups;
121                     *size = newsize;
122                   }
123
124                 groups[*start] = grpbuf.gr_gid;
125                 *start += 1;
126
127                 break;
128               }
129         }
130     }
131   while (status == NSS_STATUS_SUCCESS);
132
133  done:
134   if (endgrent_fct)
135     DL_CALL_FCT (endgrent_fct, ());
136
137   return NSS_STATUS_SUCCESS;
138 }
139
140 static int
141 internal_getgrouplist (const char *user, gid_t group, long int *size,
142                        gid_t **groupsp, long int limit)
143 {
144   service_user *nip = NULL;
145   initgroups_dyn_function fct;
146   enum nss_status status = NSS_STATUS_UNAVAIL;
147   int no_more;
148   /* Start is one, because we have the first group as parameter.  */
149   long int start = 1;
150
151   *groupsp[0] = group;
152
153   if (__nss_group_database != NULL)
154     {
155       no_more = 0;
156       nip = __nss_group_database;
157     }
158   else
159     no_more = __nss_database_lookup ("group", NULL,
160                                      "compat [NOTFOUND=return] files", &nip);
161
162   while (! no_more)
163     {
164       fct = __nss_lookup_function (nip, "initgroups_dyn");
165
166       if (fct == NULL)
167         {
168           status = compat_call (nip, user, group, &start, size, groupsp,
169                                 limit, &errno);
170
171           if (nss_next_action (nip, NSS_STATUS_UNAVAIL) != NSS_ACTION_CONTINUE)
172             break;
173         }
174       else
175         status = DL_CALL_FCT (fct, (user, group, &start, size, groupsp,
176                                     limit, &errno));
177
178       /* This is really only for debugging.  */
179       if (NSS_STATUS_TRYAGAIN > status || status > NSS_STATUS_RETURN)
180         __libc_fatal ("illegal status in internal_getgrouplist");
181
182       if (status != NSS_STATUS_SUCCESS
183           && nss_next_action (nip, status) == NSS_ACTION_RETURN)
184          break;
185
186       if (nip->next == NULL)
187         no_more = -1;
188       else
189         nip = nip->next;
190     }
191
192   return start;
193 }
194
195 /* Store at most *NGROUPS members of the group set for USER into
196    *GROUPS.  Also include GROUP.  The actual number of groups found is
197    returned in *NGROUPS.  Return -1 if the if *NGROUPS is too small.  */
198 int
199 getgrouplist (const char *user, gid_t group, gid_t *groups, int *ngroups)
200 {
201   gid_t *newgroups;
202   long int size = *ngroups;
203   int result;
204
205   newgroups = (gid_t *) malloc (size * sizeof (gid_t));
206   if (__builtin_expect (newgroups == NULL, 0))
207     /* No more memory.  */
208     return -1;
209
210   result = internal_getgrouplist (user, group, &size, &newgroups, -1);
211
212   memcpy (groups, newgroups, MIN (*ngroups, result) * sizeof (gid_t));
213
214   if (result > *ngroups)
215     {
216       *ngroups = result;
217       result = -1;
218     }
219   else
220     *ngroups = result;
221
222   free (newgroups);
223   return result;
224 }
225
226 static_link_warning (getgrouplist)
227
228 /* Initialize the group set for the current user
229    by reading the group database and using all groups
230    of which USER is a member.  Also include GROUP.  */
231 int
232 initgroups (const char *user, gid_t group)
233 {
234 #if defined NGROUPS_MAX && NGROUPS_MAX == 0
235
236   /* No extra groups allowed.  */
237   return 0;
238
239 #else
240
241   long int size;
242   gid_t *groups;
243   int ngroups;
244   int result;
245
246  /* We always use sysconf even if NGROUPS_MAX is defined.  That way, the
247      limit can be raised in the kernel configuration without having to
248      recompile libc.  */
249   long int limit = __sysconf (_SC_NGROUPS_MAX);
250
251   if (limit > 0)
252     size = limit;
253   else
254     {
255       /* No fixed limit on groups.  Pick a starting buffer size.  */
256       size = 16;
257     }
258
259   groups = (gid_t *) malloc (size * sizeof (gid_t));
260   if (__builtin_expect (groups == NULL, 0))
261     /* No more memory.  */
262     return -1;
263
264   ngroups = internal_getgrouplist (user, group, &size, &groups, limit);
265
266   /* Try to set the maximum number of groups the kernel can handle.  */
267   do
268     result = setgroups (ngroups, groups);
269   while (result == -1 && errno == EINVAL && --ngroups > 0);
270
271   free (groups);
272
273   return result;
274 #endif
275 }
276
277 static_link_warning (initgroups)