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
30 /* Type of the lookup function.  */
31 typedef enum nss_status (*initgroups_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;
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 *groups, long int limit, int *errnop)
54 {
55   struct group grpbuf, *g;
56   size_t buflen = __sysconf (_SC_GETPW_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
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 = _CALL_DL_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 = _CALL_DL_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       g = &grpbuf;
94       if (g->gr_gid != group)
95         {
96           char **m;
97
98           for (m = g->gr_mem; *m != NULL; ++m)
99             if (strcmp (*m, user) == 0)
100               {
101                 /* Matches user.  Insert this group.  */
102                 if (*start == *size && limit <= 0)
103                   {
104                     /* Need a bigger buffer.  */
105                     groups = realloc (groups, *size * sizeof (*groups));
106                     if (groups == NULL)
107                       goto done;
108                     *size *= 2;
109                   }
110
111                 groups[*start] = g->gr_gid;
112                 *start += 1;
113
114                 if (*start == limit)
115                   /* Can't take any more groups; stop searching.  */
116                   goto done;
117
118                 break;
119               }
120         }
121     }
122   while (status == NSS_STATUS_SUCCESS);
123
124  done:
125   if (endgrent_fct)
126     _CALL_DL_FCT (endgrent_fct, ());
127
128   return NSS_STATUS_SUCCESS;
129 }
130
131 /* Initialize the group set for the current user
132    by reading the group database and using all groups
133    of which USER is a member.  Also include GROUP.  */
134 int
135 initgroups (user, group)
136      const char *user;
137      gid_t group;
138 {
139 #if defined NGROUPS_MAX && NGROUPS_MAX == 0
140
141   /* No extra groups allowed.  */
142   return 0;
143
144 #else
145
146   service_user *nip = NULL;
147   initgroups_function fct;
148   enum nss_status status = NSS_STATUS_UNAVAIL;
149   int no_more;
150   /* Start is one, because we have the first group as parameter.  */
151   long int start = 1;
152   long int size;
153   gid_t *groups;
154 #ifdef NGROUPS_MAX
155 # define limit NGROUPS_MAX
156
157   size = limit;
158 #else
159   long int limit = __sysconf (_SC_NGROUPS_MAX);
160
161   if (limit > 0)
162     size = limit;
163   else
164     /* No fixed limit on groups.  Pick a starting buffer size.  */
165     size = 16;
166 #endif
167
168   groups = malloc (size * sizeof (gid_t *));
169
170   groups[0] = group;
171
172   if (__nss_group_database != NULL)
173     {
174       no_more = 0;
175       nip = __nss_group_database;
176     }
177   else
178     no_more = __nss_database_lookup ("group", NULL,
179                                      "compat [NOTFOUND=return] files", &nip);
180
181   while (! no_more)
182     {
183       fct = __nss_lookup_function (nip, "initgroups");
184
185       if (fct == NULL)
186         {
187           status = compat_call (nip, user, group, &start, &size, groups,
188                                 limit, &errno);
189
190           if (nss_next_action (nip, NSS_STATUS_UNAVAIL) != NSS_ACTION_CONTINUE)
191             break;
192         }
193       else
194         status = _CALL_DL_FCT (fct, (user, group, &start, &size, groups, limit,
195                                      &errno));
196
197       /* This is really only for debugging.  */
198       if (NSS_STATUS_TRYAGAIN > status || status > NSS_STATUS_RETURN)
199          __libc_fatal ("illegal status in " __FUNCTION__);
200
201       if (status != NSS_STATUS_SUCCESS
202           && nss_next_action (nip, status) == NSS_ACTION_RETURN)
203          break;
204
205       if (nip->next == NULL)
206         no_more = -1;
207       else
208         nip = nip->next;
209     }
210
211   return setgroups (start, groups);
212 #endif
213 }