Kill FBSD4X with fire
[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 #include <sys/sysproto.h>
21
22
23 #include "afs/sysincludes.h"
24 #include "afsincludes.h"
25 #include "afs/afs_stats.h"      /* statistics */
26
27 static int
28   afs_getgroups(struct ucred *cred, int ngroups, gid_t * gidset);
29
30 static int
31   afs_setgroups(struct thread *td, struct ucred **cred, int ngroups,
32                 gid_t * gidset, int change_parent);
33
34
35 int
36 Afs_xsetgroups(struct thread *td, struct setgroups_args *uap)
37 {
38     int code = 0;
39     struct vrequest treq;
40     struct ucred *cr;
41
42     cr = crdup(td->td_ucred);
43
44     AFS_STATCNT(afs_xsetgroups);
45     AFS_GLOCK();
46
47     code = afs_InitReq(&treq, cr);
48     AFS_GUNLOCK();
49     crfree(cr);
50     if (code)
51         return setgroups(td, uap);      /* afs has shut down */
52
53     code = setgroups(td, uap);
54     /* Note that if there is a pag already in the new groups we don't
55      * overwrite it with the old pag.
56      */
57     cr = crdup(td->td_ucred);
58
59     if (PagInCred(cr) == NOPAG) {
60         if (((treq.uid >> 24) & 0xff) == 'A') {
61             AFS_GLOCK();
62             /* we've already done a setpag, so now we redo it */
63             AddPag(td, treq.uid, &cr);
64             AFS_GUNLOCK();
65         }
66     }
67     crfree(cr);
68     return code;
69 }
70
71
72 int
73 setpag(struct thread *td, struct ucred **cred, afs_uint32 pagvalue,
74        afs_uint32 * newpag, int change_parent)
75 {
76 #if defined(AFS_FBSD81_ENV)
77     gid_t *gidset;
78     int gidset_len = ngroups_max + 1;
79 #elif defined(AFS_FBSD80_ENV)
80     gid_t *gidset;
81     int gidset_len = NGROUPS;   /* 1024 */
82 #else
83     gid_t gidset[NGROUPS];
84     int gidset_len = NGROUPS;   /* 16 */
85 #endif
86     int ngroups, code;
87     int j;
88
89     AFS_STATCNT(setpag);
90 #ifdef AFS_FBSD80_ENV
91     gidset = osi_Alloc(gidset_len * sizeof(gid_t));
92 #endif
93     ngroups = afs_getgroups(*cred, gidset_len, gidset);
94     if (afs_get_pag_from_groups(gidset[1], gidset[2]) == NOPAG) {
95         /* We will have to shift grouplist to make room for pag */
96         if (ngroups + 2 > gidset_len) {
97             return (E2BIG);
98         }
99         for (j = ngroups - 1; j >= 1; j--) {
100             gidset[j + 2] = gidset[j];
101         }
102         ngroups += 2;
103     }
104     *newpag = (pagvalue == -1 ? genpag() : pagvalue);
105     afs_get_groups_from_pag(*newpag, &gidset[1], &gidset[2]);
106     code = afs_setgroups(td, cred, ngroups, gidset, change_parent);
107 #ifdef AFS_FBSD80_ENV
108     osi_Free(gidset, gidset_len * sizeof(gid_t));
109 #endif
110     return code;
111 }
112
113
114 static int
115 afs_getgroups(struct ucred *cred, int ngroups, gid_t * gidset)
116 {
117     int ngrps, savengrps;
118     gid_t *gp;
119
120     AFS_STATCNT(afs_getgroups);
121     savengrps = ngrps = MIN(ngroups, cred->cr_ngroups);
122     gp = cred->cr_groups;
123     while (ngrps--)
124         *gidset++ = *gp++;
125     return savengrps;
126 }
127
128
129 static int
130 afs_setgroups(struct thread *td, struct ucred **cred, int ngroups,
131               gid_t * gidset, int change_parent)
132 {
133     return (0);
134 }