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"
23 #include "../afs/sysincludes.h"
24 #include "../afs/afsincludes.h"
25 #include "../afs/afs_stats.h" /* statistics */
42 /* This is common code between SGI's DFS and our AFS. Do *not* alter it's
43 * interface or semantics without notifying SGI.
46 /* fixup_pags returns error code if relevant or 0 on no error.
47 * Sets up the cred for the call to estgroups. This is pretty convoluted
48 * in order to avoid including the private proc.h header file.
50 int fixup_pags(int **credpp, int ngroups, gid_t *gidset, int old_afs_pag,
58 gid_t groups[NGROUPS_UMAX];
61 if (ngroups < 0 || ngroups > ngroups_max)
65 AFS_COPYIN(gidset, groups, ngroups * sizeof(gid_t), code);
70 if (ngroups >= 2) { /* possibly an AFS PAG */
71 new_afs_pag = (afs_get_pag_from_groups(groups[0], groups[1]) != NOPAG);
73 if (ngroups >= 1) { /* possibly a DFS PAG */
74 new_dfs_pag = (int) groups[ngroups-1];
75 if (((new_dfs_pag >> 24) & 0xff) == 'A')
76 new_dfs_pag = (int) groups[ngroups-1];
81 /* Now compute the number of groups we will need. */
83 if (old_afs_pag && !new_afs_pag) /* prepend old AFS pag */
85 if (old_dfs_pag && !new_dfs_pag) /* append old DFS pag */
88 if (new > ngroups_max)
89 return EINVAL; /* sorry */
91 cr = crdup(OSI_GET_CURRENT_CRED()); /* we will replace all the groups. */
92 bzero((char*)&cr->cr_groups, ngroups_max * sizeof(gid_t));
94 /* Now cobble the new groups list together. */
97 if (old_afs_pag && !new_afs_pag) { /* prepend old AFS pag */
100 afs_get_groups_from_pag(old_afs_pag, &g0, &g1);
101 cr->cr_groups[new++] = g0;
102 cr->cr_groups[new++] = g1;
105 for (old = 0; old < ngroups; old++)
106 cr->cr_groups[new++] = groups[old];
108 if (old_dfs_pag && !new_dfs_pag) { /* append old DFS pag */
110 cr->cr_groups[new++] = old_dfs_pag;
113 /* Now, did we do anything? */
115 cr->cr_ngroups = new;
126 * Generic routine to set the PAG in the cred for AFS and DFS.
127 * If flag = 0 this is a DFS pag held in one group.
128 * If flag = 1 this is a AFS pag held in two group entries
130 static int afsDFS_SetPagInCred(struct ucred *credp, int pag, int flag)
136 struct ucred *newcredp;
137 int groups_taken = (flag ? 2 : 1);
139 ngrps = credp->cr_ngroups + groups_taken;
140 if (ngrps >= ngroups_max)
145 /* Break out the AFS pag into two groups */
146 afs_get_groups_from_pag(pag, &g0, &g1);
149 newcredp = crdup(credp);
150 newcredp->cr_ngroups = ngrps;
154 newcredp->cr_groups[0] = g0;
155 newcredp->cr_groups[1] = g1;
158 if (PagInCred(newcredp) != NOPAG){
159 /* found an AFS PAG is set in this cred */
162 newcredp->cr_groups[n] = pag;
164 for (i=n; i<credp->cr_ngroups; i++)
165 newcredp->cr_groups[i+groups_taken] = credp->cr_groups[i];
167 /* estgroups sets current threads cred from newcredp and crfree's credp */
168 estgroups(credp, newcredp);
172 #endif /* AFS_SGI65_ENV */
174 /* SGI's osi_GetPagFromCred - They return a long. */
175 int osi_DFSGetPagFromCred(struct ucred *credp)
181 * For IRIX, the PAG is stored in the first entry
182 * of the gruop list in the cred structure. gid_t's
183 * are 32 bits on 64 bit and 32 bit hardware types.
184 * As of Irix 6.5, the DFS pag is the last group in the list.
186 ngroups = credp->cr_ngroups;
190 * Keep in mind that we might be living with AFS here.
191 * This means we don't really know if our DFS PAG is in
192 * the first or third group entry.
195 pag = credp->cr_groups[ngroups-1];
197 pag = credp->cr_groups[0];
198 if (PagInCred(credp) != NOPAG ){
199 /* AFS has a PAG value in the first two group entries */
202 pag = credp->cr_groups[2];
205 if (((pag >> 24) & 0xff) == 'A')
212 Afs_xsetgroups(int ngroups, gid_t *gidset)
214 int old_afs_pag = NOPAG;
215 int old_dfs_pag = NOPAG;
217 struct ucred *credp = OSI_GET_CURRENT_CRED();
218 struct ucred *modcredp;
221 credp = OSI_GET_CURRENT_CRED();
222 /* First get any old PAG's */
223 old_afs_pag = PagInCred(credp);
224 old_dfs_pag = osi_DFSGetPagFromCred(credp);
226 /* Set the passed in group list. */
227 if (code = setgroups(ngroups, gidset))
231 if (old_afs_pag == NOPAG && old_dfs_pag == NOPAG)
234 /* Well, we could get the cred, except it's in the proc struct which
235 * is not a publicly available header. And the cred won't be valid on
236 * the uthread until we return to user space. So, we examine the passed
237 * in groups in fixup_pags.
239 code = fixup_pags(&modcredp, ngroups, gidset,
240 (old_afs_pag == NOPAG) ? 0 : old_afs_pag,
241 (old_dfs_pag == NOPAG) ? 0 : old_dfs_pag);
242 if (!code && modcredp)
243 estgroups(OSI_GET_CURRENT_PROCP(), modcredp);
247 * The setgroups gave our curent thread a new cred pointer
248 * Get the value again
250 credp = OSI_GET_CURRENT_CRED();
251 if ((PagInCred(credp) == NOPAG) && (old_afs_pag != NOPAG)) {
252 /* reset the AFS PAG */
253 code = afsDFS_SetPagInCred(credp, old_afs_pag, 1);
256 * Once again get the credp because the afsDFS_SetPagInCred might have
257 * assigned a new one.
259 credp = OSI_GET_CURRENT_CRED();
260 if ((osi_DFSGetPagFromCred(credp)==NOPAG)
261 && (old_dfs_pag != NOPAG)) {
262 code = afsDFS_SetPagInCred(credp, old_dfs_pag, 0);
264 #endif /* AFS_SGI65_ENV */
270 setpag(cred, pagvalue, newpag, change_parent)
274 afs_uint32 change_parent;
276 gid_t gidset[NGROUPS];
282 ngroups = afs_getgroups(*cred, NGROUPS, gidset);
283 if (afs_get_pag_from_groups(gidset[0], gidset[1]) == NOPAG) {
284 /* We will have to shift grouplist to make room for pag */
285 if (ngroups + 2 > NGROUPS) {
289 return (setuerror(E2BIG), E2BIG);
292 for (j = ngroups -1; j >= 0; j--) {
293 gidset[j+2] = gidset[j];
297 *newpag = (pagvalue == -1 ? genpag(): pagvalue);
298 afs_get_groups_from_pag(*newpag, &gidset[0], &gidset[1]);
299 if (code = afs_setgroups(cred, ngroups, gidset, change_parent)) {
303 return (setuerror(code), code);
316 int ngrps, savengrps;
319 gidset[0] = gidset[1] = 0;
320 AFS_STATCNT(afs_getgroups);
321 savengrps = ngrps = MIN(ngroups, cred->cr_ngroups);
322 gp = cred->cr_groups;
340 AFS_STATCNT(afs_setgroups);
342 if (ngroups > ngroups_max)
349 newcr->cr_ngroups = ngroups;
350 gp = newcr->cr_groups;
353 if (!change_parent) {
355 estgroups(OSI_GET_CURRENT_PROCP(), newcr);
357 estgroups(cr, newcr);