77f4d64890cde1b727590b53b619be2f7e0d20fe
[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 /*
11  * osi_groups.c
12  *
13  * Implements:
14  * Afs_xsetgroups (syscall)
15  * setpag
16  *
17  */
18 #include <afsconfig.h>
19 #include "../afs/param.h"
20
21 RCSID("$Header$");
22
23 #include "../afs/sysincludes.h"
24 #include "../afs/afsincludes.h"
25 #include "../afs/afs_stats.h"  /* statistics */
26
27 #define NOCRED  ((struct ucred *) -1)
28 #define NOUID   ((uid_t) -1)
29 #define NOGID   ((gid_t) -1)
30
31
32 static int
33 afs_getgroups(
34     struct ucred *cred,
35     int ngroups,
36     gid_t *gidset);
37
38 static int
39 afs_setgroups(
40     struct proc *proc,
41     struct ucred **cred,
42     int ngroups,
43     gid_t *gidset,
44     int change_parent);
45
46 int
47 Afs_xsetgroups(p, args, retval)
48     struct proc *p;
49     void *args;
50     int *retval;
51 {
52     int code = 0;
53     struct vrequest treq;
54
55     AFS_STATCNT(afs_xsetgroups);
56     AFS_GLOCK();
57
58     /*    code = afs_InitReq(&treq, u.u_cred); */
59     code = afs_InitReq(&treq, curproc->p_cred->pc_ucred);
60     AFS_GUNLOCK();
61     if (code) return code;
62
63     code = setgroups(p, args, retval);
64     /* Note that if there is a pag already in the new groups we don't
65      * overwrite it with the old pag.
66      */
67     if (PagInCred(curproc->p_cred->pc_ucred) == NOPAG) {
68         if (((treq.uid >> 24) & 0xff) == 'A') {
69             AFS_GLOCK();
70             /* we've already done a setpag, so now we redo it */
71             AddPag(p, treq.uid, &p->p_rcred);
72             AFS_GUNLOCK();
73         }
74     }
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[0], gidset[1]) == 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 >= 0; 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[0], &gidset[1]);
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 *newcr, *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     if (!change_parent) {
151         crhold(cr);
152         newcr = crcopy(cr);
153     } else
154         newcr = cr;
155     newcr->cr_ngroups = ngroups;
156     gp = newcr->cr_groups;
157     while (ngroups--)
158         *gp++ = *gidset++;
159     if (!change_parent) {
160         substitute_real_creds(proc, NOUID, NOUID, NOGID, NOGID, newcr);
161     }
162     *cred = newcr;
163     return(0);
164 }