2 * Copyright 2000, International Business Machines Corporation and others.
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
12 * afsDFS_SetPagInCred (shared with SGI)
13 * osi_DFSGetPagFromCred (shared with SGI)
14 * Afs_xsetgroups (syscall)
18 #include <afsconfig.h>
19 #include "afs/param.h"
22 #include "afs/sysincludes.h"
23 #include "afsincludes.h"
24 #include "afs/afs_stats.h" /* statistics */
28 afs_getgroups(struct ucred *cred, int ngroups, gid_t * gidset);
31 afs_setgroups(struct ucred **cred, int ngroups, gid_t * gidset,
35 /* This is common code between SGI's DFS and our AFS. Do *not* alter it's
36 * interface or semantics without notifying SGI.
38 /* fixup_pags returns error code if relevant or 0 on no error.
39 * Sets up the cred for the call to estgroups. This is pretty convoluted
40 * in order to avoid including the private proc.h header file.
43 fixup_pags(int **credpp, int ngroups, gid_t * gidset, int old_afs_pag,
51 gid_t groups[NGROUPS_UMAX];
54 if (ngroups < 0 || ngroups > ngroups_max)
58 AFS_COPYIN(gidset, groups, ngroups * sizeof(gid_t), code);
63 if (ngroups >= 2) { /* possibly an AFS PAG */
65 (afs_get_pag_from_groups(groups[0], groups[1]) != NOPAG);
67 if (ngroups >= 1) { /* possibly a DFS PAG */
68 new_dfs_pag = (int)groups[ngroups - 1];
69 if (((new_dfs_pag >> 24) & 0xff) == 'A')
70 new_dfs_pag = (int)groups[ngroups - 1];
75 /* Now compute the number of groups we will need. */
77 if (old_afs_pag && !new_afs_pag) /* prepend old AFS pag */
79 if (old_dfs_pag && !new_dfs_pag) /* append old DFS pag */
82 if (new > ngroups_max)
83 return EINVAL; /* sorry */
85 cr = crdup(OSI_GET_CURRENT_CRED()); /* we will replace all the groups. */
86 memset(&cr->cr_groups, 0, ngroups_max * sizeof(gid_t));
88 /* Now cobble the new groups list together. */
91 if (old_afs_pag && !new_afs_pag) { /* prepend old AFS pag */
94 afs_get_groups_from_pag(old_afs_pag, &g0, &g1);
95 cr->cr_groups[new++] = g0;
96 cr->cr_groups[new++] = g1;
99 for (old = 0; old < ngroups; old++)
100 cr->cr_groups[new++] = groups[old];
102 if (old_dfs_pag && !new_dfs_pag) { /* append old DFS pag */
104 cr->cr_groups[new++] = old_dfs_pag;
107 /* Now, did we do anything? */
109 cr->cr_ngroups = new;
118 /* SGI's osi_GetPagFromCred - They return a long. */
120 osi_DFSGetPagFromCred(struct ucred *credp)
126 * For IRIX, the PAG is stored in the first entry
127 * of the gruop list in the cred structure. gid_t's
128 * are 32 bits on 64 bit and 32 bit hardware types.
129 * As of Irix 6.5, the DFS pag is the last group in the list.
131 ngroups = credp->cr_ngroups;
135 * Keep in mind that we might be living with AFS here.
136 * This means we don't really know if our DFS PAG is in
137 * the first or third group entry.
139 pag = credp->cr_groups[ngroups - 1];
140 if (((pag >> 24) & 0xff) == 'A')
147 Afs_xsetgroups(int ngroups, gid_t * gidset)
149 int old_afs_pag = NOPAG;
150 int old_dfs_pag = NOPAG;
152 struct ucred *credp = OSI_GET_CURRENT_CRED();
153 struct ucred *modcredp;
156 credp = OSI_GET_CURRENT_CRED();
157 /* First get any old PAG's */
158 old_afs_pag = PagInCred(credp);
159 old_dfs_pag = osi_DFSGetPagFromCred(credp);
161 /* Set the passed in group list. */
162 if (code = setgroups(ngroups, gidset))
165 if (old_afs_pag == NOPAG && old_dfs_pag == NOPAG)
168 /* Well, we could get the cred, except it's in the proc struct which
169 * is not a publicly available header. And the cred won't be valid on
170 * the uthread until we return to user space. So, we examine the passed
171 * in groups in fixup_pags.
174 fixup_pags(&modcredp, ngroups, gidset,
175 (old_afs_pag == NOPAG) ? 0 : old_afs_pag,
176 (old_dfs_pag == NOPAG) ? 0 : old_dfs_pag);
177 if (!code && modcredp)
178 estgroups(OSI_GET_CURRENT_PROCP(), modcredp);
184 setpag(cred, pagvalue, newpag, change_parent)
188 afs_uint32 change_parent;
190 gid_t gidset[NGROUPS];
196 ngroups = afs_getgroups(*cred, NGROUPS, gidset);
197 if (afs_get_pag_from_groups(gidset[0], gidset[1]) == NOPAG) {
198 /* We will have to shift grouplist to make room for pag */
199 if (ngroups + 2 > NGROUPS) {
200 #if defined(KERNEL_HAVE_UERROR)
201 return (setuerror(E2BIG), E2BIG);
206 for (j = ngroups - 1; j >= 0; j--) {
207 gidset[j + 2] = gidset[j];
211 *newpag = (pagvalue == -1 ? genpag() : pagvalue);
212 afs_get_groups_from_pag(*newpag, &gidset[0], &gidset[1]);
213 if (code = afs_setgroups(cred, ngroups, gidset, change_parent)) {
214 #if defined(KERNEL_HAVE_UERROR)
215 return (setuerror(code), code);
225 afs_getgroups(struct ucred *cred, int ngroups, gid_t * gidset)
227 int ngrps, savengrps;
230 gidset[0] = gidset[1] = 0;
231 AFS_STATCNT(afs_getgroups);
232 savengrps = ngrps = MIN(ngroups, cred->cr_ngroups);
233 gp = cred->cr_groups;
242 afs_setgroups(struct ucred **cred, int ngroups, gid_t * gidset,
248 AFS_STATCNT(afs_setgroups);
250 if (ngroups > ngroups_max)
257 newcr->cr_ngroups = ngroups;
258 gp = newcr->cr_groups;
261 if (!change_parent) {
262 estgroups(OSI_GET_CURRENT_PROCP(), newcr);