Standardize License information
[openafs.git] / src / afs / SOLARIS / 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  * Implements:
12  * setgroups (syscall)
13  * setpag
14  *
15  */
16 #include "../afs/param.h"
17 #include "../afs/sysincludes.h"
18 #include "../afs/afsincludes.h"
19 #include "../afs/afs_stats.h"  /* statistics */
20
21
22 static int
23 afs_getgroups(
24     struct cred *cred,
25     gid_t *gidset);
26
27 static int
28 afs_setgroups(
29     struct cred **cred,
30     int ngroups,
31     gid_t *gidset,
32     int change_parent);
33
34
35 #if     defined(AFS_SUN55_ENV)
36 int
37 afs_xsetgroups(uap, rvp)
38 u_int uap; /* this is gidsetsize */
39 gid_t *rvp; /* this is gidset */
40 #else 
41 struct setgroupsa {
42     u_int gidsetsize;
43     gid_t *gidset;
44 };
45
46 afs_xsetgroups(uap, rvp) 
47     struct setgroupsa *uap;
48     rval_t *rvp;
49 #endif
50 {
51     int code = 0;
52     struct vrequest treq;
53     struct proc *proc = ttoproc(curthread);
54
55     AFS_STATCNT(afs_xsetgroups);
56     AFS_GLOCK();
57     code = afs_InitReq(&treq, proc->p_cred);
58     AFS_GUNLOCK();
59     if (code) return code;
60     code = setgroups(uap, rvp);
61
62     /* Note that if there is a pag already in the new groups we don't
63      * overwrite it with the old pag.
64      */
65     if (PagInCred(proc->p_cred) == NOPAG) {
66         if (((treq.uid >> 24) & 0xff) == 'A') {
67             AFS_GLOCK();
68             /* we've already done a setpag, so now we redo it */
69             AddPag(treq.uid, &proc->p_cred);
70             AFS_GUNLOCK();
71         }
72     }
73     return code;
74 }
75
76 int
77 setpag(cred, pagvalue, newpag, change_parent)
78     struct cred **cred;
79     afs_uint32 pagvalue;
80     afs_uint32 *newpag;
81     afs_uint32 change_parent;
82 {
83     gid_t *gidset;
84     int ngroups, code;
85     int j;
86
87     AFS_STATCNT(setpag);
88
89     gidset = (gid_t *) osi_AllocSmallSpace(AFS_SMALLOCSIZ);
90     ngroups = afs_getgroups(*cred, gidset);
91
92     if (afs_get_pag_from_groups(gidset[0], gidset[1]) == NOPAG) {
93         /* We will have to shift grouplist to make room for pag */
94         if ((sizeof gidset[0])*(ngroups + 2) > AFS_SMALLOCSIZ) {
95             osi_FreeSmallSpace((char *)gidset);
96             return (E2BIG);
97         }
98         for (j = ngroups -1; j >= 0; j--) {
99             gidset[j+2] = gidset[j];
100         }
101         ngroups += 2;
102     }
103     *newpag = (pagvalue == -1 ? genpag(): pagvalue);
104     afs_get_groups_from_pag(*newpag, &gidset[0], &gidset[1]);
105     if (code = afs_setgroups(cred, ngroups, gidset, change_parent)) {
106         osi_FreeSmallSpace((char *)gidset);
107         return (code);
108     }
109     osi_FreeSmallSpace((char *)gidset);
110     return code;
111 }
112
113
114 static int
115 afs_getgroups(
116     struct cred *cred,
117     gid_t *gidset)
118 {
119     int ngrps, savengrps;
120     gid_t *gp;
121
122     AFS_STATCNT(afs_getgroups);
123
124     gidset[0] = gidset[1] = 0;
125     savengrps = ngrps = cred->cr_ngroups;
126     gp = cred->cr_groups;
127     while (ngrps--)
128         *gidset++ = *gp++;   
129     return savengrps;
130 }
131
132
133
134 static int
135 afs_setgroups(
136     struct cred **cred,
137     int ngroups,
138     gid_t *gidset,
139     int change_parent)
140 {
141     int ngrps;
142     int i;
143     gid_t *gp;
144
145     AFS_STATCNT(afs_setgroups);
146
147     if (ngroups > ngroups_max)
148         return EINVAL;
149     if (!change_parent)
150         *cred = (struct cred *)crcopy(*cred);
151     (*cred)->cr_ngroups = ngroups;
152     gp = (*cred)->cr_groups;
153     while (ngroups--)
154         *gp++ = *gidset++;
155     if (!change_parent) {
156         struct proc *proc = ttoproc(curthread);
157         crset(proc, *cred); /* broadcast to all threads */
158     }
159     return (0);
160 }