Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / afs / UKERNEL / 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 /*
11 * afs_xsetgroups (asserts FALSE)
12 * setpag (aliased to use_setpag in sysincludes.h)
13 */
14 #include <afsconfig.h>
15 #include "afs/param.h"
16
17
18 #include "afs/sysincludes.h"
19 #include "afsincludes.h"
20 #include "afs/afs_stats.h" /* statistics */
21 #include "afs/afs_prototypes.h"
22
23 int
24 afs_xsetgroups(void)
25 {
26 usr_assert(0);
27 return 0;
28 }
29
30 static int
31 afs_getgroups(afs_ucred_t *cred, gid_t * gidset)
32 {
33 int ngrps, savengrps;
34 gid_t *gp;
35
36 AFS_STATCNT(afs_getgroups);
37
38 gidset[0] = gidset[1] = 0;
39 savengrps = ngrps = cred->cr_ngroups;
40 gp = cred->cr_groups;
41 while (ngrps--)
42 *gidset++ = *gp++;
43 return savengrps;
44 }
45
46
47
48 static int
49 afs_setgroups(afs_ucred_t **cred, int ngroups, gid_t * gidset,
50 int change_parent)
51 {
52 gid_t *gp;
53
54 AFS_STATCNT(afs_setgroups);
55
56 if (ngroups > NGROUPS_MAX)
57 return EINVAL;
58 if (!change_parent)
59 *cred = (afs_ucred_t *)crcopy(*cred);
60 (*cred)->cr_ngroups = ngroups;
61 gp = (*cred)->cr_groups;
62 while (ngroups--)
63 *gp++ = *gidset++;
64 return (0);
65 }
66
67 int
68 usr_setpag(struct usr_ucred **cred, afs_uint32 pagvalue, afs_uint32 * newpag,
69 int change_parent)
70 {
71 gid_t *gidset;
72 int ngroups, code;
73 int j;
74
75 AFS_STATCNT(setpag);
76
77 gidset = osi_AllocSmallSpace(AFS_SMALLOCSIZ);
78 ngroups = afs_getgroups(*cred, gidset);
79
80 if (afs_get_pag_from_groups(gidset[0], gidset[1]) == NOPAG) {
81 /* We will have to shift grouplist to make room for pag */
82 if ((sizeof gidset[0]) * (ngroups + 2) > AFS_SMALLOCSIZ) {
83 osi_FreeSmallSpace((char *)gidset);
84 return (E2BIG);
85 }
86 for (j = ngroups - 1; j >= 0; j--) {
87 gidset[j + 2] = gidset[j];
88 }
89 ngroups += 2;
90 }
91 *newpag = (pagvalue == -1 ? genpag() : pagvalue);
92 afs_get_groups_from_pag(*newpag, &gidset[0], &gidset[1]);
93 if ((code = afs_setgroups(cred, ngroups, gidset, change_parent))) {
94 osi_FreeSmallSpace((char *)gidset);
95 return (code);
96 }
97 osi_FreeSmallSpace((char *)gidset);
98
99 return 0;
100 }