8bf01bdcc3048dd495437625c28672b506f86c72
[openafs.git] / src / afs / DARWIN / 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     pcred_readlock(p);
51     cr=crdup(p->p_cred->pc_ucred);
52     pcred_unlock(p);
53
54     AFS_STATCNT(afs_xsetgroups);
55     AFS_GLOCK();
56     
57     code = afs_InitReq(&treq, cr);
58     AFS_GUNLOCK();
59     crfree(cr);
60     if (code) return setgroups(p, args, retval); /* afs has shut down */
61
62     code = setgroups(p, args, retval);
63     /* Note that if there is a pag already in the new groups we don't
64      * overwrite it with the old pag.
65      */
66     pcred_readlock(p);
67     cr=crdup(p->p_cred->pc_ucred);
68     pcred_unlock(p);
69
70     if (PagInCred(cr) == NOPAG) {
71         if (((treq.uid >> 24) & 0xff) == 'A') {
72             AFS_GLOCK();
73             /* we've already done a setpag, so now we redo it */
74             AddPag(p, treq.uid, &cr );
75             AFS_GUNLOCK();
76         }
77     }
78     crfree(cr);
79     return code;
80 }
81
82
83 int
84 setpag(proc, cred, pagvalue, newpag, change_parent)
85     struct proc *proc;
86     struct ucred **cred;
87     afs_uint32 pagvalue;
88     afs_uint32 *newpag;
89     afs_uint32 change_parent;
90 {
91     gid_t gidset[NGROUPS];
92     int ngroups, code;
93     int j;
94
95     AFS_STATCNT(setpag);
96     ngroups = afs_getgroups(*cred, NGROUPS, gidset);
97     if (afs_get_pag_from_groups(gidset[1], gidset[2]) == NOPAG) {
98         /* We will have to shift grouplist to make room for pag */
99         if (ngroups + 2 > NGROUPS) {
100             return (E2BIG);
101         }
102         for (j = ngroups -1; j >= 1; j--) {
103             gidset[j+2] = gidset[j];
104         }
105         ngroups += 2;
106     }
107     *newpag = (pagvalue == -1 ? genpag(): pagvalue);
108     afs_get_groups_from_pag(*newpag, &gidset[1], &gidset[2]);
109     code = afs_setgroups(proc, cred, ngroups, gidset, change_parent);
110     return code;
111 }
112
113
114 static int
115 afs_getgroups(
116     struct ucred *cred,
117     int ngroups,
118     gid_t *gidset)
119 {
120     int ngrps, savengrps;
121     gid_t *gp;
122
123     AFS_STATCNT(afs_getgroups);
124     savengrps = ngrps = MIN(ngroups, cred->cr_ngroups);
125     gp = cred->cr_groups;
126     while (ngrps--)
127         *gidset++ = *gp++;   
128     return savengrps;
129 }
130
131
132
133 static int
134 afs_setgroups(
135     struct proc *proc,
136     struct ucred **cred,
137     int ngroups,
138     gid_t *gidset,
139     int change_parent)
140 {
141     int ngrps;
142     int i;
143     gid_t *gp;
144     struct ucred *oldcr, *cr;
145
146     AFS_STATCNT(afs_setgroups);
147     /*
148      * The real setgroups() call does this, so maybe we should too.
149      *
150      */
151     if (ngroups > NGROUPS)
152         return EINVAL;
153     cr = *cred;
154     cr->cr_ngroups = ngroups;
155     gp = cr->cr_groups;
156     while (ngroups--)
157         *gp++ = *gidset++;
158     if (change_parent) {
159         crhold(cr);
160         pcred_writelock(proc->p_pptr);
161         oldcr=proc->p_pptr->p_cred->pc_ucred;
162         proc->p_pptr->p_cred->pc_ucred=cr;
163         pcred_unlock(proc->p_pptr);
164         crfree(oldcr);
165     }
166     crhold(cr);
167     pcred_writelock(proc);
168     oldcr=proc->p_cred->pc_ucred;
169     proc->p_cred->pc_ucred=cr;
170     pcred_unlock(proc);
171     crfree(oldcr);
172     return(0);
173 }