freebsd-almost-working-client-20020216
[openafs.git] / src / afs / FBSD / 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  * osi_groups.c
11  *
12  * Implements:
13  * Afs_xsetgroups (syscall)
14  * setpag
15  *
16  */
17 #include <afsconfig.h>
18 #include "../afs/param.h"
19
20 RCSID("$Header$");
21
22 #include "../afs/sysincludes.h"
23 #include "../afs/afsincludes.h"
24 #include "../afs/afs_stats.h"  /* statistics */
25
26 static int
27 afs_getgroups(
28     struct ucred *cred,
29     int ngroups,
30     gid_t *gidset);
31
32 static int
33 afs_setgroups(
34     struct proc *proc,
35     struct ucred **cred,
36     int ngroups,
37     gid_t *gidset,
38     int change_parent);
39
40 int
41 Afs_xsetgroups(p, args, retval)
42     struct proc *p;
43     void *args;
44     int *retval;
45 {
46     int code = 0;
47     struct vrequest treq;
48     struct ucred *cr;
49
50     cr=crdup(p->p_cred->pc_ucred);
51
52     AFS_STATCNT(afs_xsetgroups);
53     AFS_GLOCK();
54     
55     code = afs_InitReq(&treq, cr);
56     AFS_GUNLOCK();
57     crfree(cr);
58     if (code) return setgroups(p, args, retval); /* afs has shut down */
59
60     code = setgroups(p, args, retval);
61     /* Note that if there is a pag already in the new groups we don't
62      * overwrite it with the old pag.
63      */
64     cr=crdup(p->p_cred->pc_ucred);
65
66     if (PagInCred(cr) == NOPAG) {
67         if (((treq.uid >> 24) & 0xff) == 'A') {
68             AFS_GLOCK();
69             /* we've already done a setpag, so now we redo it */
70             AddPag(p, treq.uid, &cr );
71             AFS_GUNLOCK();
72         }
73     }
74     crfree(cr);
75     return code;
76 }
77
78
79 int
80 setpag(proc, cred, pagvalue, newpag, change_parent)
81     struct proc *proc;
82     struct ucred **cred;
83     afs_uint32 pagvalue;
84     afs_uint32 *newpag;
85     afs_uint32 change_parent;
86 {
87     gid_t gidset[NGROUPS];
88     int ngroups, code;
89     int j;
90
91     AFS_STATCNT(setpag);
92     ngroups = afs_getgroups(*cred, NGROUPS, gidset);
93     if (afs_get_pag_from_groups(gidset[1], gidset[2]) == NOPAG) {
94         /* We will have to shift grouplist to make room for pag */
95         if (ngroups + 2 > NGROUPS) {
96             return (E2BIG);
97         }
98         for (j = ngroups -1; j >= 1; j--) {
99             gidset[j+2] = gidset[j];
100         }
101         ngroups += 2;
102     }
103     *newpag = (pagvalue == -1 ? genpag(): pagvalue);
104     afs_get_groups_from_pag(*newpag, &gidset[1], &gidset[2]);
105     code = afs_setgroups(proc, cred, ngroups, gidset, change_parent);
106     return code;
107 }
108
109
110 static int
111 afs_getgroups(
112     struct ucred *cred,
113     int ngroups,
114     gid_t *gidset)
115 {
116     int ngrps, savengrps;
117     gid_t *gp;
118
119     AFS_STATCNT(afs_getgroups);
120     savengrps = ngrps = MIN(ngroups, cred->cr_ngroups);
121     gp = cred->cr_groups;
122     while (ngrps--)
123         *gidset++ = *gp++;   
124     return savengrps;
125 }
126
127
128
129 static int
130 afs_setgroups(
131     struct proc *proc,
132     struct ucred **cred,
133     int ngroups,
134     gid_t *gidset,
135     int change_parent)
136 {
137     int ngrps;
138     int i;
139     gid_t *gp;
140     struct ucred *oldcr, *cr;
141
142     AFS_STATCNT(afs_setgroups);
143     /*
144      * The real setgroups() call does this, so maybe we should too.
145      *
146      */
147     if (ngroups > NGROUPS)
148         return EINVAL;
149     cr = *cred;
150     cr->cr_ngroups = ngroups;
151     gp = cr->cr_groups;
152     while (ngroups--)
153         *gp++ = *gidset++;
154     if (change_parent) {
155         crhold(cr);
156         oldcr=proc->p_pptr->p_cred->pc_ucred;
157         proc->p_pptr->p_cred->pc_ucred=cr;
158         crfree(oldcr);
159     }
160     crhold(cr);
161     oldcr=proc->p_cred->pc_ucred;
162     proc->p_cred->pc_ucred=cr;
163     crfree(oldcr);
164     return(0);
165 }