linux-redhat-81-beta-support-20020107
[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("$Header$");
20
21 #include "afs/sysincludes.h"
22 #include "afsincludes.h"
23 #include "afs/afs_stats.h"  /* statistics */
24 #ifdef AFS_LINUX22_ENV
25 #include "h/smp_lock.h"
26 #endif
27
28 static int afs_getgroups(cred_t *cr, gid_t *groups);
29 static int afs_setgroups(cred_t **cr, int ngroups, gid_t *gidset, int change_parent);
30
31 /* Only propogate the PAG to the parent process. Unix's propogate to 
32  * all processes sharing the cred.
33  */
34 int set_pag_in_parent(int pag, int g0, int g1)
35 {
36 #ifdef STRUCT_TASK_STRUCT_HAS_PARENT
37     gid_t *gp = current->parent->groups;
38 #else
39     gid_t *gp = current->p_pptr->groups;
40 #endif
41     int ngroups;
42     int i;
43
44     
45 #ifdef STRUCT_TASK_STRUCT_HAS_PARENT
46     ngroups = current->parent->ngroups;
47 #else
48     ngroups = current->p_pptr->ngroups;
49 #endif
50
51     if ((ngroups < 2) || (afs_get_pag_from_groups(gp[0], gp[1]) == NOPAG)) {
52         /* We will have to shift grouplist to make room for pag */
53         if (ngroups + 2 > NGROUPS) {
54             return EINVAL;
55         }
56         for (i = ngroups-1; i >= 0; i--) {
57             gp[i+2] = gp[i];
58         }
59         ngroups += 2;
60     }
61     gp[0] = g0;
62     gp[1] = g1;
63     if (ngroups < NGROUPS)
64         gp[ngroups] = NOGROUP;
65
66 #ifdef STRUCT_TASK_STRUCT_HAS_PARENT
67     current->parent->ngroups = ngroups;
68 #else
69     current->p_pptr->ngroups = ngroups;
70 #endif
71     return 0;
72 }
73
74 int setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag, int change_parent)
75 {
76     gid_t *gidset;
77     afs_int32 ngroups, code = 0;
78     int j;
79
80     AFS_STATCNT(setpag);
81
82     gidset = (gid_t *) osi_Alloc(NGROUPS*sizeof(gidset[0]));
83     ngroups = afs_getgroups(*cr, gidset);
84
85     if (afs_get_pag_from_groups(gidset[0], gidset[1]) == NOPAG) {
86         /* We will have to shift grouplist to make room for pag */
87         if (ngroups + 2 > NGROUPS) {
88             osi_Free((char *)gidset, NGROUPS*sizeof(int));
89             return EINVAL;
90         }
91         for (j = ngroups - 1; j >= 0; j--) {
92             gidset[j+2] = gidset[j];
93         }
94         ngroups += 2;
95     }
96     *newpag = (pagvalue == -1 ? genpag(): pagvalue);
97     afs_get_groups_from_pag(*newpag, &gidset[0], &gidset[1]);
98     code = afs_setgroups(cr, ngroups, gidset, change_parent);
99
100     /* If change_parent is set, then we should set the pag in the parent as
101      * well.
102      */
103     if (change_parent && !code) {
104         code = set_pag_in_parent(*newpag, gidset[0], gidset[1]);
105     }
106
107     osi_Free((char *)gidset, NGROUPS*sizeof(int));
108     return code;
109 }
110
111
112 /* Intercept the standard system call. */
113 extern long (*sys_setgroupsp)(int gidsetsize, gid_t *grouplist);
114 asmlinkage long afs_xsetgroups(int gidsetsize, gid_t *grouplist)
115 {
116     int code;
117     cred_t *cr = crref();
118     afs_uint32 junk;
119     int old_pag;
120
121     lock_kernel();
122     old_pag = PagInCred(cr);
123     crfree(cr);
124     unlock_kernel();
125
126     code = (*sys_setgroupsp)(gidsetsize, grouplist);
127     if (code) {
128         return code;
129     }
130
131     lock_kernel();
132     cr = crref();
133     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
134         /* re-install old pag if there's room. */
135         code = setpag(&cr, old_pag, &junk, 0);
136     }
137     crfree(cr);
138     unlock_kernel();
139
140     return code;
141 }
142
143 #if defined(AFS_LINUX24_ENV)
144 /* Intercept the standard uid32 system call. */
145 extern int (*sys_setgroups32p)(int gidsetsize, gid_t *grouplist);
146 asmlinkage int afs_xsetgroups32(int gidsetsize, gid_t *grouplist)
147 {
148     int code;
149     cred_t *cr = crref();
150     afs_uint32 junk;
151     int old_pag;
152     
153     lock_kernel();
154     old_pag = PagInCred(cr);
155     crfree(cr);
156     unlock_kernel();
157
158     code = (*sys_setgroups32p)(gidsetsize, grouplist);
159     if (code) {
160         return code;
161     }
162
163     lock_kernel();
164     cr = crref();
165     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
166         /* re-install old pag if there's room. */
167         code = setpag(&cr, old_pag, &junk, 0);
168     }
169     crfree(cr);
170     unlock_kernel();
171
172     return code;
173 }
174 #endif
175
176 #if defined(AFS_SPARC64_LINUX20_ENV)
177 /* Intercept the uid16 system call as used by 32bit programs. */
178 extern int (*sys32_setgroupsp)(int gidsetsize, __kernel_gid_t32 *grouplist);
179 asmlinkage int afs32_xsetgroups(int gidsetsize, __kernel_gid_t32 *grouplist)
180 {
181     int code;
182     cred_t *cr = crref();
183     afs_uint32 junk;
184     int old_pag;
185
186     lock_kernel();
187     old_pag = PagInCred(cr);
188     crfree(cr);
189     unlock_kernel();
190
191     code = (*sys32_setgroupsp)(gidsetsize, grouplist);
192     if (code) {
193         return code;
194     }
195
196     lock_kernel();
197     cr = crref();
198     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
199         /* re-install old pag if there's room. */
200         code = setpag(&cr, old_pag, &junk, 0);
201     }
202     crfree(cr);
203     unlock_kernel();
204
205     return code;
206 }
207 #ifdef AFS_LINUX24_ENV
208 /* Intercept the uid32 system call as used by 32bit programs. */
209 extern int (*sys32_setgroups32p)(int gidsetsize, __kernel_gid_t32 *grouplist);
210 asmlinkage int afs32_xsetgroups32(int gidsetsize, __kernel_gid_t32 *grouplist)
211 {
212     int code;
213     cred_t *cr = crref();
214     afs_uint32 junk;
215     int old_pag;
216
217     lock_kernel();
218     old_pag = PagInCred(cr);
219     crfree(cr);
220     unlock_kernel();
221
222     code = (*sys32_setgroups32p)(gidsetsize, grouplist);
223     if (code) {
224         return code;
225     }
226
227     lock_kernel();
228     cr = crref();
229     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
230         /* re-install old pag if there's room. */
231         code = setpag(&cr, old_pag, &junk, 0);
232     }
233     crfree(cr);
234     unlock_kernel();
235
236     return code;
237 }
238 #endif
239 #endif
240
241 static int afs_setgroups(cred_t **cr, int ngroups, gid_t *gidset, int change_parent)
242 {
243     int ngrps;
244     int i;
245     gid_t *gp;
246
247     AFS_STATCNT(afs_setgroups);
248
249     if (ngroups > NGROUPS)
250         return EINVAL;
251
252     gp = (*cr)->cr_groups;
253     if (ngroups < NGROUPS)
254         gp[ngroups] = (gid_t)NOGROUP;
255
256     for (i = ngroups; i > 0; i--) {
257         *gp++ = *gidset++;
258     }
259
260     (*cr)->cr_ngroups = ngroups;
261     crset(*cr);
262     return (0);
263 }
264
265 /* Returns number of groups. And we trust groups to be large enough to
266  * hold all the groups.
267  */
268 static int afs_getgroups(cred_t *cr, gid_t *groups)
269 {
270     int i;
271     gid_t *gp = cr->cr_groups;
272     int n = cr->cr_ngroups;
273     AFS_STATCNT(afs_getgroups);
274
275     for (i = 0; (i < n) && (*gp != (gid_t)NOGROUP); i++) {
276         *groups++ = *gp++;
277     }
278     return i;
279 }
280