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