Update.
[platform/upstream/glibc.git] / grp / initgroups.c
1 /* Copyright (C) 1989, 91, 93, 96, 97, 98 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 <string.h>
24 #include <unistd.h>
25 #include <sys/types.h>
26 #include <nsswitch.h>
27
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, *g;
55   size_t buflen = sysconf (_SC_GETPW_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   setgrent_fct = __nss_lookup_function (nip, "setgrent");
63   status = (*setgrent_fct) ();
64   if (status != NSS_STATUS_SUCCESS)
65     return status;
66
67   getgrent_fct = __nss_lookup_function (nip, "getgrent_r");
68   endgrent_fct = __nss_lookup_function (nip, "endgrent");
69
70   tmpbuf = __alloca (buflen);
71
72   do
73     {
74       while ((status =
75               (*getgrent_fct) (&grpbuf, tmpbuf, buflen, errnop)) ==
76              NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
77         {
78           buflen *= 2;
79           tmpbuf = __alloca (buflen);
80         }
81
82       if (status != NSS_STATUS_SUCCESS)
83         goto done;
84
85       g = &grpbuf;
86       if (g->gr_gid != group)
87         {
88           char **m;
89
90           for (m = g->gr_mem; *m != NULL; ++m)
91             if (strcmp (*m, user) == 0)
92               {
93                 /* Matches user.  Insert this group.  */
94                 if (*start == *size && limit <= 0)
95                   {
96                     /* Need a bigger buffer.  */
97                     groups = realloc (groups, *size * sizeof (*groups));
98                     if (groups == NULL)
99                       goto done;
100                     *size *= 2;
101                   }
102
103                 groups[*start] = g->gr_gid;
104                 *start += 1;
105
106                 if (*start == limit)
107                   /* Can't take any more groups; stop searching.  */
108                   goto done;
109
110                 break;
111               }
112         }
113     }
114   while (status == NSS_STATUS_SUCCESS);
115
116  done:
117   (*endgrent_fct) ();
118
119   return NSS_STATUS_SUCCESS;
120 }
121
122 /* Initialize the group set for the current user
123    by reading the group database and using all groups
124    of which USER is a member.  Also include GROUP.  */
125 int
126 initgroups (user, group)
127      const char *user;
128      gid_t group;
129 {
130 #if defined NGROUPS_MAX && NGROUPS_MAX == 0
131
132   /* No extra groups allowed.  */
133   return 0;
134
135 #else
136
137   service_user *nip = NULL;
138   initgroups_function fct;
139   enum nss_status status = NSS_STATUS_UNAVAIL;
140   int no_more;
141   /* Start is one, because we have the first group as parameter.  */
142   long int start = 1;
143   long int size;
144   gid_t *groups;
145 #ifdef NGROUPS_MAX
146 # define limit NGROUPS_MAX
147
148   size = limit;
149 #else
150   long int limit = sysconf (_SC_NGROUPS_MAX);
151
152   if (limit > 0)
153     size = limit;
154   else
155     /* No fixed limit on groups.  Pick a starting buffer size.  */
156     size = 16;
157 #endif
158
159   groups = malloc (size * sizeof (gid_t *));
160
161   groups[0] = group;
162
163   if (__nss_group_database != NULL)
164     {
165       no_more = 0;
166       nip = __nss_group_database;
167     }
168   else
169     no_more = __nss_database_lookup ("group", NULL,
170                                      "compat [NOTFOUND=return] files", &nip);
171
172   while (! no_more)
173     {
174       fct = __nss_lookup_function (nip, "initgroups");
175
176       if (fct == NULL)
177         status = compat_call (nip, user, group, &start, &size, groups,
178                               limit, &errno);
179       else
180         status = (*fct) (user, group, &start, &size, groups, limit,
181                          &errno);
182
183       if (nip->next == NULL)
184         no_more = -1;
185       else
186         nip = nip->next;
187     }
188
189   return setgroups (start, groups);
190 #endif
191 }