dac9f3f49b997ab6b86b470b3f3bcaa1c2a7d854
[openafs.git] / src / afs / LINUX / osi_groups.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11  * Implements:
12  * setgroups (syscall)
13  * setpag
14  *
15  */
16 #include <afsconfig.h>
17 #include "afs/param.h"
18
19 RCSID
20     ("$Header$");
21
22 #include "afs/sysincludes.h"
23 #include "afsincludes.h"
24 #include "afs/afs_stats.h"      /* statistics */
25 #ifdef AFS_LINUX22_ENV
26 #include "h/smp_lock.h"
27 #endif
28
29 #if defined(AFS_LINUX26_ENV)
30 static int
31 afs_setgroups(cred_t **cr, struct group_info *group_info, int change_parent)
32 {
33     struct group_info *old_info;
34
35     AFS_STATCNT(afs_setgroups);
36
37     old_info = (*cr)->cr_group_info;
38     get_group_info(group_info);
39     (*cr)->cr_group_info = group_info;
40     put_group_info(old_info);
41
42     crset(*cr);
43
44     if (change_parent) {
45         old_info = current->parent->group_info;
46         get_group_info(group_info);
47         current->parent->group_info = group_info;
48         put_group_info(old_info);
49     }
50
51     return (0);
52 }
53 #else
54 static int
55 afs_setgroups(cred_t **cr, int ngroups, gid_t * gidset, int change_parent)
56 {
57     int ngrps;
58     int i;
59     gid_t *gp;
60
61     AFS_STATCNT(afs_setgroups);
62
63     if (ngroups > NGROUPS)
64         return EINVAL;
65
66     gp = (*cr)->cr_groups;
67     if (ngroups < NGROUPS)
68         gp[ngroups] = (gid_t) NOGROUP;
69
70     for (i = ngroups; i > 0; i--) {
71         *gp++ = *gidset++;
72     }
73
74     (*cr)->cr_ngroups = ngroups;
75     crset(*cr);
76     return (0);
77 }
78 #endif
79
80 #if defined(AFS_LINUX26_ENV)
81 static struct group_info *
82 afs_getgroups(cred_t * cr)
83 {
84     AFS_STATCNT(afs_getgroups);
85
86     get_group_info(cr->cr_group_info);
87     return cr->cr_group_info;
88 }
89 #else
90 /* Returns number of groups. And we trust groups to be large enough to
91  * hold all the groups.
92  */
93 static int
94 afs_getgroups(cred_t *cr, gid_t *groups)
95 {
96     int i;
97     AFS_STATCNT(afs_getgroups);
98
99     gid_t *gp = cr->cr_groups;
100     int n = cr->cr_ngroups;
101
102     for (i = 0; (i < n) && (*gp != (gid_t) NOGROUP); i++)
103         *groups++ = *gp++;
104     return i;
105 }
106 #endif
107
108 #if !defined(AFS_LINUX26_ENV)
109 /* Only propogate the PAG to the parent process. Unix's propogate to 
110  * all processes sharing the cred.
111  */
112 int
113 set_pag_in_parent(int pag, int g0, int g1)
114 {
115     int i;
116 #ifdef STRUCT_TASK_STRUCT_HAS_PARENT
117     gid_t *gp = current->parent->groups;
118     int ngroups = current->parent->ngroups;
119 #else
120     gid_t *gp = current->p_pptr->groups;
121     int ngroups = current->p_pptr->ngroups;
122 #endif
123
124     if ((ngroups < 2) || (afs_get_pag_from_groups(gp[0], gp[1]) == NOPAG)) {
125         /* We will have to shift grouplist to make room for pag */
126         if (ngroups + 2 > NGROUPS) {
127             return EINVAL;
128         }
129         for (i = ngroups - 1; i >= 0; i--) {
130             gp[i + 2] = gp[i];
131         }
132         ngroups += 2;
133     }
134     gp[0] = g0;
135     gp[1] = g1;
136     if (ngroups < NGROUPS)
137         gp[ngroups] = NOGROUP;
138
139 #ifdef STRUCT_TASK_STRUCT_HAS_PARENT
140     current->parent->ngroups = ngroups;
141 #else
142     current->p_pptr->ngroups = ngroups;
143 #endif
144     return 0;
145 }
146 #endif
147
148 int
149 setpag(cred_t ** cr, afs_uint32 pagvalue, afs_uint32 * newpag,
150        int change_parent)
151 {
152 #if defined(AFS_LINUX26_ENV)
153     struct group_info *group_info;
154     gid_t g0, g1;
155
156     AFS_STATCNT(setpag);
157
158     group_info = afs_getgroups(*cr);
159     if (group_info->ngroups < 2
160         ||  afs_get_pag_from_groups(GROUP_AT(group_info, 0),
161                                     GROUP_AT(group_info, 1)) == NOPAG) {
162         /* We will have to make sure group_info is big enough for pag */
163         struct group_info *tmp;
164         int i;
165         
166         tmp = groups_alloc(group_info->ngroups + 2);
167         for (i = 0; i < group_info->ngroups; ++i)
168                 GROUP_AT(tmp, i + 2) = GROUP_AT(group_info, i);
169         put_group_info(group_info);
170         group_info = tmp;
171     }
172
173     *newpag = (pagvalue == -1 ? genpag() : pagvalue);
174     afs_get_groups_from_pag(*newpag, &g0, &g1);
175     GROUP_AT(group_info, 0) = g0;
176     GROUP_AT(group_info, 1) = g1;
177
178     afs_setgroups(cr, group_info, change_parent);
179
180     put_group_info(group_info);
181
182     return 0;
183 #else
184     gid_t *gidset;
185     afs_int32 ngroups, code = 0;
186     int j;
187
188     AFS_STATCNT(setpag);
189
190     gidset = (gid_t *) osi_Alloc(NGROUPS * sizeof(gidset[0]));
191     ngroups = afs_getgroups(*cr, gidset);
192
193     if (afs_get_pag_from_groups(gidset[0], gidset[1]) == NOPAG) {
194         /* We will have to shift grouplist to make room for pag */
195         if (ngroups + 2 > NGROUPS) {
196             osi_Free((char *)gidset, NGROUPS * sizeof(int));
197             return EINVAL;
198         }
199         for (j = ngroups - 1; j >= 0; j--) {
200             gidset[j + 2] = gidset[j];
201         }
202         ngroups += 2;
203     }
204     *newpag = (pagvalue == -1 ? genpag() : pagvalue);
205     afs_get_groups_from_pag(*newpag, &gidset[0], &gidset[1]);
206     code = afs_setgroups(cr, ngroups, gidset, change_parent);
207
208     /* If change_parent is set, then we should set the pag in the parent as
209      * well.
210      */
211     if (change_parent && !code) {
212         code = set_pag_in_parent(*newpag, gidset[0], gidset[1]);
213     }
214
215     osi_Free((char *)gidset, NGROUPS * sizeof(int));
216     return code;
217 #endif
218 }
219
220
221 /* Intercept the standard system call. */
222 extern long (*sys_setgroupsp) (int gidsetsize, gid_t * grouplist);
223 asmlinkage long
224 afs_xsetgroups(int gidsetsize, gid_t * grouplist)
225 {
226     long code;
227     cred_t *cr = crref();
228     afs_uint32 junk;
229     int old_pag;
230
231     lock_kernel();
232     old_pag = PagInCred(cr);
233     crfree(cr);
234     unlock_kernel();
235
236     code = (*sys_setgroupsp) (gidsetsize, grouplist);
237     if (code) {
238         return code;
239     }
240
241     lock_kernel();
242     cr = crref();
243     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
244         /* re-install old pag if there's room. */
245         code = setpag(&cr, old_pag, &junk, 0);
246     }
247     crfree(cr);
248     unlock_kernel();
249
250     /* Linux syscall ABI returns errno as negative */
251     return (-code);
252 }
253
254 #if defined(AFS_LINUX24_ENV)
255 /* Intercept the standard uid32 system call. */
256 extern long (*sys_setgroups32p) (int gidsetsize, gid_t * grouplist);
257 asmlinkage long
258 afs_xsetgroups32(int gidsetsize, gid_t * grouplist)
259 {
260     long code;
261     cred_t *cr = crref();
262     afs_uint32 junk;
263     int old_pag;
264
265     lock_kernel();
266     old_pag = PagInCred(cr);
267     crfree(cr);
268     unlock_kernel();
269
270     code = (*sys_setgroups32p) (gidsetsize, grouplist);
271
272     if (code) {
273         return code;
274     }
275
276     lock_kernel();
277     cr = crref();
278     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
279         /* re-install old pag if there's room. */
280         code = setpag(&cr, old_pag, &junk, 0);
281     }
282     crfree(cr);
283     unlock_kernel();
284
285     /* Linux syscall ABI returns errno as negative */
286     return (-code);
287 }
288 #endif
289
290 #if defined(AFS_PPC64_LINUX20_ENV)
291 /* Intercept the uid16 system call as used by 32bit programs. */
292 extern long (*sys32_setgroupsp)(int gidsetsize, gid_t *grouplist);
293 asmlinkage long afs32_xsetgroups(int gidsetsize, gid_t *grouplist)
294 {
295     long code;
296     cred_t *cr = crref();
297     afs_uint32 junk;
298     int old_pag;
299     
300     lock_kernel();
301     old_pag = PagInCred(cr);
302     crfree(cr);
303     unlock_kernel();
304     
305     code = (*sys32_setgroupsp)(gidsetsize, grouplist);
306     if (code) {
307         return code;
308     }
309     
310     lock_kernel();
311     cr = crref();
312     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
313         /* re-install old pag if there's room. */
314         code = setpag(&cr, old_pag, &junk, 0);
315     }
316     crfree(cr);
317     unlock_kernel();
318     
319     /* Linux syscall ABI returns errno as negative */
320     return (-code);
321 }
322 #endif
323
324 #if defined(AFS_SPARC64_LINUX20_ENV) || defined(AFS_AMD64_LINUX20_ENV)
325 /* Intercept the uid16 system call as used by 32bit programs. */
326 extern long (*sys32_setgroupsp) (int gidsetsize, u16 * grouplist);
327 asmlinkage long
328 afs32_xsetgroups(int gidsetsize, u16 * grouplist)
329 {
330     long code;
331     cred_t *cr = crref();
332     afs_uint32 junk;
333     int old_pag;
334     
335     lock_kernel();
336     old_pag = PagInCred(cr);
337     crfree(cr);
338     unlock_kernel();
339     
340     code = (*sys32_setgroupsp) (gidsetsize, grouplist);
341     if (code) {
342         return code;
343     }
344     
345     lock_kernel();
346     cr = crref();
347     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
348         /* re-install old pag if there's room. */
349         code = setpag(&cr, old_pag, &junk, 0);
350     }
351     crfree(cr);
352     unlock_kernel();
353     
354     /* Linux syscall ABI returns errno as negative */
355     return (-code);
356 }
357
358 #ifdef AFS_LINUX24_ENV
359 /* Intercept the uid32 system call as used by 32bit programs. */
360 extern long (*sys32_setgroups32p) (int gidsetsize, gid_t * grouplist);
361 asmlinkage long
362 afs32_xsetgroups32(int gidsetsize, gid_t * grouplist)
363 {
364     long code;
365     cred_t *cr = crref();
366     afs_uint32 junk;
367     int old_pag;
368
369     lock_kernel();
370     old_pag = PagInCred(cr);
371     crfree(cr);
372     unlock_kernel();
373
374     code = (*sys32_setgroups32p) (gidsetsize, grouplist);
375     if (code) {
376         return code;
377     }
378
379     lock_kernel();
380     cr = crref();
381     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
382         /* re-install old pag if there's room. */
383         code = setpag(&cr, old_pag, &junk, 0);
384     }
385     crfree(cr);
386     unlock_kernel();
387
388     /* Linux syscall ABI returns errno as negative */
389     return (-code);
390 }
391 #endif
392 #endif
393