Work around old buggy program which cannot cope with memcpy semantics.
[glibc.git] / grp / initgroups.c
blob180e653c30e6ee19f8124e3093367a93367042cd
1 /* Copyright (C) 1989,91,93,1996-2006,2008,2010 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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. */
19 #include <alloca.h>
20 #include <assert.h>
21 #include <errno.h>
22 #include <grp.h>
23 #include <limits.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/param.h>
28 #include <sys/types.h>
29 #include <nsswitch.h>
31 #include "../nscd/nscd-client.h"
32 #include "../nscd/nscd_proto.h"
35 /* Type of the lookup function. */
36 typedef enum nss_status (*initgroups_dyn_function) (const char *, gid_t,
37 long int *, long int *,
38 gid_t **, long int, int *);
40 /* The lookup function for the first entry of this service. */
41 extern int __nss_group_lookup (service_user **nip, const char *name,
42 void **fctp);
43 extern void *__nss_lookup_function (service_user *ni, const char *fct_name);
45 extern service_user *__nss_group_database attribute_hidden;
48 #include "compat-initgroups.c"
51 static int
52 internal_getgrouplist (const char *user, gid_t group, long int *size,
53 gid_t **groupsp, long int limit)
55 #ifdef USE_NSCD
56 if (__nss_not_use_nscd_group > 0
57 && ++__nss_not_use_nscd_group > NSS_NSCD_RETRY)
58 __nss_not_use_nscd_group = 0;
59 if (!__nss_not_use_nscd_group
60 && !__nss_database_custom[NSS_DBSIDX_group])
62 int n = __nscd_getgrouplist (user, group, size, groupsp, limit);
63 if (n >= 0)
64 return n;
66 /* nscd is not usable. */
67 __nss_not_use_nscd_group = 1;
69 #endif
71 service_user *nip = NULL;
72 initgroups_dyn_function fct;
73 enum nss_status status = NSS_STATUS_UNAVAIL;
74 int no_more;
75 /* Start is one, because we have the first group as parameter. */
76 long int start = 1;
78 /* Never store more than the starting *SIZE number of elements. */
79 assert (*size > 0);
80 (*groupsp)[0] = group;
82 if (__nss_group_database != NULL)
84 no_more = 0;
85 nip = __nss_group_database;
87 else
88 no_more = __nss_database_lookup ("group", NULL,
89 "compat [NOTFOUND=return] files", &nip);
91 while (! no_more)
93 long int prev_start = start;
95 fct = __nss_lookup_function (nip, "initgroups_dyn");
97 if (fct == NULL)
98 status = compat_call (nip, user, group, &start, size, groupsp,
99 limit, &errno);
100 else
101 status = DL_CALL_FCT (fct, (user, group, &start, size, groupsp,
102 limit, &errno));
104 /* Remove duplicates. */
105 long int cnt = prev_start;
106 while (cnt < start)
108 long int inner;
109 for (inner = 0; inner < prev_start; ++inner)
110 if ((*groupsp)[inner] == (*groupsp)[cnt])
111 break;
113 if (inner < prev_start)
114 (*groupsp)[cnt] = (*groupsp)[--start];
115 else
116 ++cnt;
119 /* This is really only for debugging. */
120 if (NSS_STATUS_TRYAGAIN > status || status > NSS_STATUS_RETURN)
121 __libc_fatal ("illegal status in internal_getgrouplist");
123 if (status != NSS_STATUS_SUCCESS
124 && nss_next_action (nip, status) == NSS_ACTION_RETURN)
125 break;
127 if (nip->next == NULL)
128 no_more = -1;
129 else
130 nip = nip->next;
133 return start;
136 /* Store at most *NGROUPS members of the group set for USER into
137 *GROUPS. Also include GROUP. The actual number of groups found is
138 returned in *NGROUPS. Return -1 if the if *NGROUPS is too small. */
140 getgrouplist (const char *user, gid_t group, gid_t *groups, int *ngroups)
142 long int size = MAX (1, *ngroups);
144 gid_t *newgroups = (gid_t *) malloc (size * sizeof (gid_t));
145 if (__builtin_expect (newgroups == NULL, 0))
146 /* No more memory. */
147 // XXX This is wrong. The user provided memory, we have to use
148 // XXX it. The internal functions must be called with the user
149 // XXX provided buffer and not try to increase the size if it is
150 // XXX too small. For initgroups a flag could say: increase size.
151 return -1;
153 int total = internal_getgrouplist (user, group, &size, &newgroups, -1);
155 memcpy (groups, newgroups, MIN (*ngroups, total) * sizeof (gid_t));
157 free (newgroups);
159 int retval = total > *ngroups ? -1 : total;
160 *ngroups = total;
162 return retval;
165 static_link_warning (getgrouplist)
167 /* Initialize the group set for the current user
168 by reading the group database and using all groups
169 of which USER is a member. Also include GROUP. */
171 initgroups (const char *user, gid_t group)
173 #if defined NGROUPS_MAX && NGROUPS_MAX == 0
175 /* No extra groups allowed. */
176 return 0;
178 #else
180 long int size;
181 gid_t *groups;
182 int ngroups;
183 int result;
185 /* We always use sysconf even if NGROUPS_MAX is defined. That way, the
186 limit can be raised in the kernel configuration without having to
187 recompile libc. */
188 long int limit = __sysconf (_SC_NGROUPS_MAX);
190 if (limit > 0)
191 /* We limit the size of the intially allocated array. */
192 size = MIN (limit, 64);
193 else
194 /* No fixed limit on groups. Pick a starting buffer size. */
195 size = 16;
197 groups = (gid_t *) malloc (size * sizeof (gid_t));
198 if (__builtin_expect (groups == NULL, 0))
199 /* No more memory. */
200 return -1;
202 ngroups = internal_getgrouplist (user, group, &size, &groups, limit);
204 /* Try to set the maximum number of groups the kernel can handle. */
206 result = setgroups (ngroups, groups);
207 while (result == -1 && errno == EINVAL && --ngroups > 0);
209 free (groups);
211 return result;
212 #endif
215 static_link_warning (initgroups)