Update.
[platform/upstream/glibc.git] / grp / initgroups.c
1 /* Copyright (C) 1989, 91, 93, 96, 97, 98, 99 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 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.
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    Library General Public License for more details.
13
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.  */
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_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 *groups, 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
62   getgrent_fct = __nss_lookup_function (nip, "getgrent_r");
63   if (getgrent_fct == NULL)
64     return NSS_STATUS_UNAVAIL;
65
66   setgrent_fct = __nss_lookup_function (nip, "setgrent");
67   if (setgrent_fct)
68     {
69       status = DL_CALL_FCT (setgrent_fct, ());
70       if (status != NSS_STATUS_SUCCESS)
71         return status;
72     }
73
74   endgrent_fct = __nss_lookup_function (nip, "endgrent");
75
76   tmpbuf = __alloca (buflen);
77
78   do
79     {
80       while ((status = DL_CALL_FCT (getgrent_fct,
81                                      (&grpbuf, tmpbuf, buflen, errnop)),
82               status == NSS_STATUS_TRYAGAIN)
83              && *errnop == ERANGE)
84         {
85           buflen *= 2;
86           tmpbuf = __alloca (buflen);
87         }
88
89       if (status != NSS_STATUS_SUCCESS)
90         goto done;
91
92       if (grpbuf.gr_gid != group)
93         {
94           char **m;
95
96           for (m = grpbuf.gr_mem; *m != NULL; ++m)
97             if (strcmp (*m, user) == 0)
98               {
99                 /* Matches user.  Insert this group.  */
100                 if (*start == *size && limit <= 0)
101                   {
102                     /* Need a bigger buffer.  */
103                     groups = realloc (groups, 2 * *size * sizeof (*groups));
104                     if (groups == NULL)
105                       goto done;
106                     *size *= 2;
107                   }
108
109                 groups[*start] = grpbuf.gr_gid;
110                 *start += 1;
111
112                 if (*start == limit)
113                   /* Can't take any more groups; stop searching.  */
114                   goto done;
115
116                 break;
117               }
118         }
119     }
120   while (status == NSS_STATUS_SUCCESS);
121
122  done:
123   if (endgrent_fct)
124     DL_CALL_FCT (endgrent_fct, ());
125
126   return NSS_STATUS_SUCCESS;
127 }
128
129 /* Initialize the group set for the current user
130    by reading the group database and using all groups
131    of which USER is a member.  Also include GROUP.  */
132 int
133 initgroups (user, group)
134      const char *user;
135      gid_t group;
136 {
137 #if defined NGROUPS_MAX && NGROUPS_MAX == 0
138
139   /* No extra groups allowed.  */
140   return 0;
141
142 #else
143
144   service_user *nip = NULL;
145   initgroups_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   long int size;
151   gid_t *groups;
152 #ifdef NGROUPS_MAX
153 # define limit NGROUPS_MAX
154
155   size = limit;
156 #else
157   long int limit = __sysconf (_SC_NGROUPS_MAX);
158
159   if (limit > 0)
160     size = limit;
161   else
162     /* No fixed limit on groups.  Pick a starting buffer size.  */
163     size = 16;
164 #endif
165
166   groups = malloc (size * sizeof (gid_t *));
167
168   groups[0] = group;
169
170   if (__nss_group_database != NULL)
171     {
172       no_more = 0;
173       nip = __nss_group_database;
174     }
175   else
176     no_more = __nss_database_lookup ("group", NULL,
177                                      "compat [NOTFOUND=return] files", &nip);
178
179   while (! no_more)
180     {
181       fct = __nss_lookup_function (nip, "initgroups");
182
183       if (fct == NULL)
184         {
185           status = compat_call (nip, user, group, &start, &size, groups,
186                                 limit, &errno);
187
188           if (nss_next_action (nip, NSS_STATUS_UNAVAIL) != NSS_ACTION_CONTINUE)
189             break;
190         }
191       else
192         status = DL_CALL_FCT (fct, (user, group, &start, &size, groups, limit,
193                                      &errno));
194
195       /* This is really only for debugging.  */
196       if (NSS_STATUS_TRYAGAIN > status || status > NSS_STATUS_RETURN)
197         __libc_fatal ("illegal status in " __FUNCTION__);
198
199       if (status != NSS_STATUS_SUCCESS
200           && nss_next_action (nip, status) == NSS_ACTION_RETURN)
201          break;
202
203       if (nip->next == NULL)
204         no_more = -1;
205       else
206         nip = nip->next;
207     }
208
209   return setgroups (start, groups);
210 #endif
211 }