1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate
23*0Sstevel@tonic-gate /*
24*0Sstevel@tonic-gate * Copyright (c) 1993-1997 by Sun Microsystems, Inc.
25*0Sstevel@tonic-gate * All rights reserved
26*0Sstevel@tonic-gate */
27*0Sstevel@tonic-gate
28*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
29*0Sstevel@tonic-gate /*LINTLIBRARY*/
30*0Sstevel@tonic-gate
31*0Sstevel@tonic-gate /*
32*0Sstevel@tonic-gate * aclsort():
33*0Sstevel@tonic-gate * Sort an ACL by entry type according to the following order:
34*0Sstevel@tonic-gate * USER_OBJ, USER, GROUP_OBJ, GROUP, CLASS_OBJ, OTHER_OBJ
35*0Sstevel@tonic-gate * DEF_USER_OBJ, DEF_USER, DEF_GROUP_OBJ, DEF_GROUP, DEF_CLASS_OBJ,
36*0Sstevel@tonic-gate * DEF_OTHER_OBJ.
37*0Sstevel@tonic-gate * For USER, GROUP, DEF_USER, and DEF_GROUP entries, the entries
38*0Sstevel@tonic-gate * are further sorted by ids.
39*0Sstevel@tonic-gate */
40*0Sstevel@tonic-gate
41*0Sstevel@tonic-gate #include <stdlib.h>
42*0Sstevel@tonic-gate #include <sys/acl.h>
43*0Sstevel@tonic-gate
44*0Sstevel@tonic-gate #define TOTAL_ENTRY_TYPES 12
45*0Sstevel@tonic-gate
46*0Sstevel@tonic-gate /*
47*0Sstevel@tonic-gate * This maps the entry defined value to a value for sorting.
48*0Sstevel@tonic-gate * These values may not be the same. It is easier to add an
49*0Sstevel@tonic-gate * entry type with this map.
50*0Sstevel@tonic-gate *
51*0Sstevel@tonic-gate * Because the defines and sorting order are not the same,
52*0Sstevel@tonic-gate * the following map_to_sort table is needed.
53*0Sstevel@tonic-gate */
54*0Sstevel@tonic-gate struct map {
55*0Sstevel@tonic-gate int sort_order;
56*0Sstevel@tonic-gate int entry_type;
57*0Sstevel@tonic-gate };
58*0Sstevel@tonic-gate
59*0Sstevel@tonic-gate static struct map map_to_sort[] = {
60*0Sstevel@tonic-gate {0, 0}, /* UNUSED */
61*0Sstevel@tonic-gate {1, USER_OBJ},
62*0Sstevel@tonic-gate {2, USER},
63*0Sstevel@tonic-gate {3, GROUP_OBJ},
64*0Sstevel@tonic-gate {4, GROUP},
65*0Sstevel@tonic-gate {5, CLASS_OBJ},
66*0Sstevel@tonic-gate {6, OTHER_OBJ},
67*0Sstevel@tonic-gate {7, DEF_USER_OBJ},
68*0Sstevel@tonic-gate {8, DEF_USER},
69*0Sstevel@tonic-gate {9, DEF_GROUP_OBJ},
70*0Sstevel@tonic-gate {10, DEF_GROUP},
71*0Sstevel@tonic-gate {11, DEF_CLASS_OBJ},
72*0Sstevel@tonic-gate {12, DEF_OTHER_OBJ},
73*0Sstevel@tonic-gate };
74*0Sstevel@tonic-gate
75*0Sstevel@tonic-gate static int entrycmp(const aclent_t *, const aclent_t *);
76*0Sstevel@tonic-gate static int idcmp(const aclent_t *, const aclent_t *);
77*0Sstevel@tonic-gate static void sortid(aclent_t *, int, int);
78*0Sstevel@tonic-gate
79*0Sstevel@tonic-gate int
aclsort(int nentries,int calcmask,aclent_t * aclbufp)80*0Sstevel@tonic-gate aclsort(int nentries, int calcmask, aclent_t *aclbufp)
81*0Sstevel@tonic-gate {
82*0Sstevel@tonic-gate aclent_t *tp;
83*0Sstevel@tonic-gate unsigned int newmask = 0;
84*0Sstevel@tonic-gate int which;
85*0Sstevel@tonic-gate int i;
86*0Sstevel@tonic-gate int k;
87*0Sstevel@tonic-gate
88*0Sstevel@tonic-gate /* check validity first before sorting */
89*0Sstevel@tonic-gate if (aclcheck(aclbufp, nentries, &which) != 0)
90*0Sstevel@tonic-gate return (-1);
91*0Sstevel@tonic-gate
92*0Sstevel@tonic-gate /*
93*0Sstevel@tonic-gate * Performance enhancement:
94*0Sstevel@tonic-gate * We change entry type to sort order in the ACL, do the sorting.
95*0Sstevel@tonic-gate * We then change sort order back to entry type.
96*0Sstevel@tonic-gate * This makes entrycmp() very "light" and thus improves performance.
97*0Sstevel@tonic-gate * Contrast to original implementation that had to find out
98*0Sstevel@tonic-gate * the sorting order each time it is called.
99*0Sstevel@tonic-gate */
100*0Sstevel@tonic-gate for (tp = aclbufp, i = 0; i < nentries; tp++, i++) {
101*0Sstevel@tonic-gate for (k = 1; k <= TOTAL_ENTRY_TYPES; k++) {
102*0Sstevel@tonic-gate if (tp->a_type == map_to_sort[k].entry_type) {
103*0Sstevel@tonic-gate tp->a_type = map_to_sort[k].sort_order;
104*0Sstevel@tonic-gate break;
105*0Sstevel@tonic-gate }
106*0Sstevel@tonic-gate }
107*0Sstevel@tonic-gate }
108*0Sstevel@tonic-gate
109*0Sstevel@tonic-gate /* typecast to remove incompatible type warning */
110*0Sstevel@tonic-gate qsort(aclbufp, nentries, sizeof (aclent_t),
111*0Sstevel@tonic-gate (int (*)(const void *, const void *))entrycmp);
112*0Sstevel@tonic-gate
113*0Sstevel@tonic-gate for (tp = aclbufp, i = 0; i < nentries; tp++, i++) {
114*0Sstevel@tonic-gate for (k = 1; k <= TOTAL_ENTRY_TYPES; k++) {
115*0Sstevel@tonic-gate if (tp->a_type == map_to_sort[k].sort_order) {
116*0Sstevel@tonic-gate tp->a_type = map_to_sort[k].entry_type;
117*0Sstevel@tonic-gate break;
118*0Sstevel@tonic-gate }
119*0Sstevel@tonic-gate }
120*0Sstevel@tonic-gate }
121*0Sstevel@tonic-gate
122*0Sstevel@tonic-gate /*
123*0Sstevel@tonic-gate * Start sorting id within USER and GROUP
124*0Sstevel@tonic-gate * sortid() could return a pointer and entries left
125*0Sstevel@tonic-gate * so that we dont have to search from the beginning
126*0Sstevel@tonic-gate * every time it calls
127*0Sstevel@tonic-gate */
128*0Sstevel@tonic-gate sortid(aclbufp, nentries, USER);
129*0Sstevel@tonic-gate sortid(aclbufp, nentries, GROUP);
130*0Sstevel@tonic-gate sortid(aclbufp, nentries, DEF_USER);
131*0Sstevel@tonic-gate sortid(aclbufp, nentries, DEF_GROUP);
132*0Sstevel@tonic-gate
133*0Sstevel@tonic-gate /*
134*0Sstevel@tonic-gate * Recalculate mask entry
135*0Sstevel@tonic-gate */
136*0Sstevel@tonic-gate if (calcmask != 0) {
137*0Sstevel@tonic-gate /*
138*0Sstevel@tonic-gate * At this point, ACL is valid and sorted. We may find a
139*0Sstevel@tonic-gate * CLASS_OBJ entry and stop. Because of the case of minimum ACL,
140*0Sstevel@tonic-gate * we still have to check till OTHER_OBJ entry is shown.
141*0Sstevel@tonic-gate */
142*0Sstevel@tonic-gate for (tp = aclbufp; tp->a_type != OTHER_OBJ; tp++) {
143*0Sstevel@tonic-gate if (tp->a_type == USER || tp->a_type == GROUP ||
144*0Sstevel@tonic-gate tp->a_type == GROUP_OBJ)
145*0Sstevel@tonic-gate newmask |= tp->a_perm;
146*0Sstevel@tonic-gate if (tp->a_type == CLASS_OBJ)
147*0Sstevel@tonic-gate break;
148*0Sstevel@tonic-gate }
149*0Sstevel@tonic-gate if (tp->a_type == CLASS_OBJ)
150*0Sstevel@tonic-gate tp->a_perm = (unsigned char)newmask;
151*0Sstevel@tonic-gate }
152*0Sstevel@tonic-gate return (0);
153*0Sstevel@tonic-gate }
154*0Sstevel@tonic-gate
155*0Sstevel@tonic-gate /*
156*0Sstevel@tonic-gate * sortid() sorts the ids with the same entry type in increasing order
157*0Sstevel@tonic-gate */
158*0Sstevel@tonic-gate static void
sortid(aclent_t * ap,int cnt,int type)159*0Sstevel@tonic-gate sortid(aclent_t *ap, int cnt, int type)
160*0Sstevel@tonic-gate {
161*0Sstevel@tonic-gate aclent_t *tp;
162*0Sstevel@tonic-gate aclent_t *startp; /* start of the desired entry type */
163*0Sstevel@tonic-gate int howmany;
164*0Sstevel@tonic-gate
165*0Sstevel@tonic-gate for (tp = ap; cnt-- > 0; tp++) {
166*0Sstevel@tonic-gate if (tp->a_type != type)
167*0Sstevel@tonic-gate continue;
168*0Sstevel@tonic-gate startp = tp;
169*0Sstevel@tonic-gate howmany = 1;
170*0Sstevel@tonic-gate for (tp++, cnt--; cnt > 0 && tp->a_type == type; tp++, cnt--)
171*0Sstevel@tonic-gate howmany++;
172*0Sstevel@tonic-gate /* typecast to remove incompatible type warning */
173*0Sstevel@tonic-gate qsort(startp, howmany, sizeof (aclent_t),
174*0Sstevel@tonic-gate (int (*)(const void*, const void*))idcmp);
175*0Sstevel@tonic-gate }
176*0Sstevel@tonic-gate }
177*0Sstevel@tonic-gate
178*0Sstevel@tonic-gate /*
179*0Sstevel@tonic-gate * compare the field a_type
180*0Sstevel@tonic-gate */
181*0Sstevel@tonic-gate static int
entrycmp(const aclent_t * i,const aclent_t * j)182*0Sstevel@tonic-gate entrycmp(const aclent_t *i, const aclent_t *j)
183*0Sstevel@tonic-gate {
184*0Sstevel@tonic-gate return ((int)(i->a_type) - (int)(j->a_type));
185*0Sstevel@tonic-gate }
186*0Sstevel@tonic-gate
187*0Sstevel@tonic-gate /*
188*0Sstevel@tonic-gate * compare the field a_id
189*0Sstevel@tonic-gate */
190*0Sstevel@tonic-gate static int
idcmp(const aclent_t * i,const aclent_t * j)191*0Sstevel@tonic-gate idcmp(const aclent_t *i, const aclent_t *j)
192*0Sstevel@tonic-gate {
193*0Sstevel@tonic-gate return ((int)(i->a_id) - (int)(j->a_id));
194*0Sstevel@tonic-gate }
195