initial-darwin-support-20010327
[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 "../afs/param.h"
18 #include "../afs/sysincludes.h"
19 #include "../afs/afsincludes.h"
20 #include "../afs/afs_stats.h"  /* statistics */
21
22 static int
23 afs_getgroups(
24     struct ucred *cred,
25     int ngroups,
26     gid_t *gidset);
27
28 static int
29 afs_setgroups(
30     struct proc *proc,
31     struct ucred **cred,
32     int ngroups,
33     gid_t *gidset,
34     int change_parent);
35
36 int
37 Afs_xsetgroups(p, args, retval)
38     struct proc *p;
39     void *args;
40     int *retval;
41 {
42     int code = 0;
43     struct vrequest treq;
44     struct ucred *cr;
45
46     pcred_readlock(p);
47     cr=crdup(p->p_cred->pc_ucred);
48     pcred_unlock(p);
49
50     AFS_STATCNT(afs_xsetgroups);
51     AFS_GLOCK();
52     
53     code = afs_InitReq(&treq, cr);
54     AFS_GUNLOCK();
55     crfree(cr);
56     if (code) return setgroups(p, args, retval); /* afs has shut down */
57
58     code = setgroups(p, args, retval);
59     /* Note that if there is a pag already in the new groups we don't
60      * overwrite it with the old pag.
61      */
62     pcred_readlock(p);
63     cr=crdup(p->p_cred->pc_ucred);
64     pcred_unlock(p);
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         pcred_writelock(proc->p_pptr);
157         oldcr=proc->p_pptr->p_cred->pc_ucred;
158         proc->p_pptr->p_cred->pc_ucred=cr;
159         pcred_unlock(proc->p_pptr);
160         crfree(oldcr);
161     }
162     crhold(cr);
163     pcred_writelock(proc);
164     oldcr=proc->p_cred->pc_ucred;
165     proc->p_cred->pc_ucred=cr;
166     pcred_unlock(proc);
167     crfree(oldcr);
168     return(0);
169 }