Remove the RCSID macro
[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 #include <sys/param.h>
20 #ifdef AFS_FBSD50_ENV
21 #include <sys/sysproto.h>
22 #endif
23
24
25 #include "afs/sysincludes.h"
26 #include "afsincludes.h"
27 #include "afs/afs_stats.h"      /* statistics */
28
29 static int
30   afs_getgroups(struct ucred *cred, int ngroups, gid_t * gidset);
31
32 static int
33   afs_setgroups(struct proc *proc, struct ucred **cred, int ngroups,
34                 gid_t * gidset, int change_parent);
35
36 #ifdef AFS_FBSD50_ENV
37 /*
38  * This does nothing useful yet.
39  * In 5.0, creds are associated not with a process, but with a thread.
40  * Probably the right thing to do is replace struct proc with struct thread
41  * everywhere, including setpag.
42  * That will be a tedious undertaking.
43  * For now, I'm just passing curproc to AddPag.
44  * This is probably wrong and I don't know what the consequences might be.
45  */
46
47 int
48 Afs_xsetgroups(struct thread *td, struct setgroups_args *uap)
49 {
50     int code = 0;
51     struct vrequest treq;
52     struct ucred *cr;
53
54     cr = crdup(td->td_ucred);
55
56     AFS_STATCNT(afs_xsetgroups);
57     AFS_GLOCK();
58
59     code = afs_InitReq(&treq, cr);
60     AFS_GUNLOCK();
61     crfree(cr);
62     if (code)
63         return setgroups(td, uap);      /* afs has shut down */
64
65     code = setgroups(td, uap);
66     /* Note that if there is a pag already in the new groups we don't
67      * overwrite it with the old pag.
68      */
69     cr = crdup(td->td_ucred);
70
71     if (PagInCred(cr) == NOPAG) {
72         if (((treq.uid >> 24) & 0xff) == 'A') {
73             AFS_GLOCK();
74             /* we've already done a setpag, so now we redo it */
75             AddPag(curproc, treq.uid, &cr);
76             AFS_GUNLOCK();
77         }
78     }
79     crfree(cr);
80     return code;
81 }
82 #else /* FBSD50 */
83 int
84 Afs_xsetgroups(p, args, retval)
85      struct proc *p;
86      void *args;
87      int *retval;
88 {
89     int code = 0;
90     struct vrequest treq;
91     struct ucred *cr;
92
93     cr = crdup(p->p_cred->pc_ucred);
94
95     AFS_STATCNT(afs_xsetgroups);
96     AFS_GLOCK();
97
98     code = afs_InitReq(&treq, cr);
99     AFS_GUNLOCK();
100     crfree(cr);
101     if (code)
102         return setgroups(p, args, retval);      /* afs has shut down */
103
104     code = setgroups(p, args, retval);
105     /* Note that if there is a pag already in the new groups we don't
106      * overwrite it with the old pag.
107      */
108     cr = crdup(p->p_cred->pc_ucred);
109
110     if (PagInCred(cr) == NOPAG) {
111         if (((treq.uid >> 24) & 0xff) == 'A') {
112             AFS_GLOCK();
113             /* we've already done a setpag, so now we redo it */
114             AddPag(p, treq.uid, &cr);
115             AFS_GUNLOCK();
116         }
117     }
118     crfree(cr);
119     return code;
120 }
121 #endif
122
123
124 int
125 setpag(struct proc *proc, struct ucred **cred, afs_uint32 pagvalue,
126        afs_uint32 * newpag, int change_parent)
127 {
128     gid_t gidset[NGROUPS];
129     int ngroups, code;
130     int j;
131
132     AFS_STATCNT(setpag);
133     ngroups = afs_getgroups(*cred, NGROUPS, gidset);
134     if (afs_get_pag_from_groups(gidset[1], gidset[2]) == NOPAG) {
135         /* We will have to shift grouplist to make room for pag */
136         if (ngroups + 2 > NGROUPS) {
137             return (E2BIG);
138         }
139         for (j = ngroups - 1; j >= 1; j--) {
140             gidset[j + 2] = gidset[j];
141         }
142         ngroups += 2;
143     }
144     *newpag = (pagvalue == -1 ? genpag() : pagvalue);
145     afs_get_groups_from_pag(*newpag, &gidset[1], &gidset[2]);
146     code = afs_setgroups(proc, cred, ngroups, gidset, change_parent);
147     return code;
148 }
149
150
151 static int
152 afs_getgroups(struct ucred *cred, int ngroups, gid_t * gidset)
153 {
154     int ngrps, savengrps;
155     gid_t *gp;
156
157     AFS_STATCNT(afs_getgroups);
158     savengrps = ngrps = MIN(ngroups, cred->cr_ngroups);
159     gp = cred->cr_groups;
160     while (ngrps--)
161         *gidset++ = *gp++;
162     return savengrps;
163 }
164
165
166 static int
167 afs_setgroups(struct proc *proc, struct ucred **cred, int ngroups,
168               gid_t * gidset, int change_parent)
169 {
170 #ifndef AFS_FBSD50_ENV
171     int ngrps;
172     int i;
173     gid_t *gp;
174     struct ucred *oldcr, *cr;
175
176     AFS_STATCNT(afs_setgroups);
177     /*
178      * The real setgroups() call does this, so maybe we should too.
179      *
180      */
181     if (ngroups > NGROUPS)
182         return EINVAL;
183     cr = *cred;
184     cr->cr_ngroups = ngroups;
185     gp = cr->cr_groups;
186     while (ngroups--)
187         *gp++ = *gidset++;
188     if (change_parent) {
189         crhold(cr);
190         oldcr = proc->p_pptr->p_cred->pc_ucred;
191         proc->p_pptr->p_cred->pc_ucred = cr;
192         crfree(oldcr);
193     }
194     crhold(cr);
195     oldcr = proc->p_cred->pc_ucred;
196     proc->p_cred->pc_ucred = cr;
197     crfree(oldcr);
198 #endif
199     return (0);
200 }