Back to index

glibc  2.9
initgroups.c
Go to the documentation of this file.
00001 /* Copyright (C) 1989,91,93,1996-2005,2006,2008 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <alloca.h>
00020 #include <assert.h>
00021 #include <errno.h>
00022 #include <grp.h>
00023 #include <limits.h>
00024 #include <stdlib.h>
00025 #include <string.h>
00026 #include <unistd.h>
00027 #include <sys/param.h>
00028 #include <sys/types.h>
00029 #include <nsswitch.h>
00030 
00031 #include "../nscd/nscd-client.h"
00032 #include "../nscd/nscd_proto.h"
00033 
00034 
00035 /* Type of the lookup function.  */
00036 typedef enum nss_status (*initgroups_dyn_function) (const char *, gid_t,
00037                                               long int *, long int *,
00038                                               gid_t **, long int, int *);
00039 
00040 /* The lookup function for the first entry of this service.  */
00041 extern int __nss_group_lookup (service_user **nip, const char *name,
00042                                void **fctp);
00043 extern void *__nss_lookup_function (service_user *ni, const char *fct_name);
00044 
00045 extern service_user *__nss_group_database attribute_hidden;
00046 
00047 
00048 #include "compat-initgroups.c"
00049 
00050 
00051 static int
00052 internal_getgrouplist (const char *user, gid_t group, long int *size,
00053                      gid_t **groupsp, long int limit)
00054 {
00055 #ifdef USE_NSCD
00056   if (__nss_not_use_nscd_group > 0
00057       && ++__nss_not_use_nscd_group > NSS_NSCD_RETRY)
00058     __nss_not_use_nscd_group = 0;
00059   if (!__nss_not_use_nscd_group)
00060     {
00061       int n = __nscd_getgrouplist (user, group, size, groupsp, limit);
00062       if (n >= 0)
00063        return n;
00064 
00065       /* nscd is not usable.  */
00066       __nss_not_use_nscd_group = 1;
00067     }
00068 #endif
00069 
00070   service_user *nip = NULL;
00071   initgroups_dyn_function fct;
00072   enum nss_status status = NSS_STATUS_UNAVAIL;
00073   int no_more;
00074   /* Start is one, because we have the first group as parameter.  */
00075   long int start = 1;
00076 
00077   /* Never store more than the starting *SIZE number of elements.  */
00078   assert (*size > 0);
00079   (*groupsp)[0] = group;
00080 
00081   if (__nss_group_database != NULL)
00082     {
00083       no_more = 0;
00084       nip = __nss_group_database;
00085     }
00086   else
00087     no_more = __nss_database_lookup ("group", NULL,
00088                                  "compat [NOTFOUND=return] files", &nip);
00089 
00090   while (! no_more)
00091     {
00092       long int prev_start = start;
00093 
00094       fct = __nss_lookup_function (nip, "initgroups_dyn");
00095 
00096       if (fct == NULL)
00097        status = compat_call (nip, user, group, &start, size, groupsp,
00098                            limit, &errno);
00099       else
00100        status = DL_CALL_FCT (fct, (user, group, &start, size, groupsp,
00101                                 limit, &errno));
00102 
00103       /* Remove duplicates.  */
00104       long int cnt = prev_start;
00105       while (cnt < start)
00106        {
00107          long int inner;
00108          for (inner = 0; inner < prev_start; ++inner)
00109            if ((*groupsp)[inner] == (*groupsp)[cnt])
00110              break;
00111 
00112          if (inner < prev_start)
00113            (*groupsp)[cnt] = (*groupsp)[--start];
00114          else
00115            ++cnt;
00116        }
00117 
00118       /* This is really only for debugging.  */
00119       if (NSS_STATUS_TRYAGAIN > status || status > NSS_STATUS_RETURN)
00120        __libc_fatal ("illegal status in internal_getgrouplist");
00121 
00122       if (status != NSS_STATUS_SUCCESS
00123          && nss_next_action (nip, status) == NSS_ACTION_RETURN)
00124         break;
00125 
00126       if (nip->next == NULL)
00127        no_more = -1;
00128       else
00129        nip = nip->next;
00130     }
00131 
00132   return start;
00133 }
00134 
00135 /* Store at most *NGROUPS members of the group set for USER into
00136    *GROUPS.  Also include GROUP.  The actual number of groups found is
00137    returned in *NGROUPS.  Return -1 if the if *NGROUPS is too small.  */
00138 int
00139 getgrouplist (const char *user, gid_t group, gid_t *groups, int *ngroups)
00140 {
00141   long int size = MAX (1, *ngroups);
00142 
00143   gid_t *newgroups = (gid_t *) malloc (size * sizeof (gid_t));
00144   if (__builtin_expect (newgroups == NULL, 0))
00145     /* No more memory.  */
00146     // XXX This is wrong.  The user provided memory, we have to use
00147     // XXX it.  The internal functions must be called with the user
00148     // XXX provided buffer and not try to increase the size if it is
00149     // XXX too small.  For initgroups a flag could say: increase size.
00150     return -1;
00151 
00152   int total = internal_getgrouplist (user, group, &size, &newgroups, -1);
00153 
00154   memcpy (groups, newgroups, MIN (*ngroups, total) * sizeof (gid_t));
00155 
00156   free (newgroups);
00157 
00158   int retval = total > *ngroups ? -1 : total;
00159   *ngroups = total;
00160 
00161   return retval;
00162 }
00163 
00164 static_link_warning (getgrouplist)
00165 
00166 /* Initialize the group set for the current user
00167    by reading the group database and using all groups
00168    of which USER is a member.  Also include GROUP.  */
00169 int
00170 initgroups (const char *user, gid_t group)
00171 {
00172 #if defined NGROUPS_MAX && NGROUPS_MAX == 0
00173 
00174   /* No extra groups allowed.  */
00175   return 0;
00176 
00177 #else
00178 
00179   long int size;
00180   gid_t *groups;
00181   int ngroups;
00182   int result;
00183 
00184  /* We always use sysconf even if NGROUPS_MAX is defined.  That way, the
00185      limit can be raised in the kernel configuration without having to
00186      recompile libc.  */
00187   long int limit = __sysconf (_SC_NGROUPS_MAX);
00188 
00189   if (limit > 0)
00190     /* We limit the size of the intially allocated array.  */
00191     size = MIN (limit, 64);
00192   else
00193     /* No fixed limit on groups.  Pick a starting buffer size.  */
00194     size = 16;
00195 
00196   groups = (gid_t *) malloc (size * sizeof (gid_t));
00197   if (__builtin_expect (groups == NULL, 0))
00198     /* No more memory.  */
00199     return -1;
00200 
00201   ngroups = internal_getgrouplist (user, group, &size, &groups, limit);
00202 
00203   /* Try to set the maximum number of groups the kernel can handle.  */
00204   do
00205     result = setgroups (ngroups, groups);
00206   while (result == -1 && errno == EINVAL && --ngroups > 0);
00207 
00208   free (groups);
00209 
00210   return result;
00211 #endif
00212 }
00213 
00214 static_link_warning (initgroups)