xref: /titanic_50/usr/src/lib/libsec/common/aclmode.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1993-1997 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate /* LINTLIBRARY */
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * Convert ACL to/from permission bits
32*7c478bd9Sstevel@tonic-gate  */
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #include <errno.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/acl.h>
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate int
acltomode(aclent_t * aclbufp,int nentries,mode_t * modep)38*7c478bd9Sstevel@tonic-gate acltomode(aclent_t *aclbufp, int nentries, mode_t *modep)
39*7c478bd9Sstevel@tonic-gate {
40*7c478bd9Sstevel@tonic-gate 	aclent_t		*tp;
41*7c478bd9Sstevel@tonic-gate 	unsigned long		mode;
42*7c478bd9Sstevel@tonic-gate 	unsigned long		grpmode;
43*7c478bd9Sstevel@tonic-gate 	unsigned long		mask;
44*7c478bd9Sstevel@tonic-gate 	int			which;
45*7c478bd9Sstevel@tonic-gate 	int			got_mask = 0;
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate 	*modep = 0;
48*7c478bd9Sstevel@tonic-gate 	if (aclcheck(aclbufp, nentries, &which) != 0) {
49*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
50*7c478bd9Sstevel@tonic-gate 		return (-1);	/* errno is set in aclcheck() */
51*7c478bd9Sstevel@tonic-gate 	}
52*7c478bd9Sstevel@tonic-gate 	for (tp = aclbufp; nentries--; tp++) {
53*7c478bd9Sstevel@tonic-gate 		if (tp->a_type == USER_OBJ) {
54*7c478bd9Sstevel@tonic-gate 			mode = tp->a_perm;
55*7c478bd9Sstevel@tonic-gate 			if (mode > 07)
56*7c478bd9Sstevel@tonic-gate 				return (-1);
57*7c478bd9Sstevel@tonic-gate 			*modep |= (mode << 6);
58*7c478bd9Sstevel@tonic-gate 			continue;
59*7c478bd9Sstevel@tonic-gate 		}
60*7c478bd9Sstevel@tonic-gate 		if (tp->a_type == GROUP_OBJ) {
61*7c478bd9Sstevel@tonic-gate 			grpmode = tp->a_perm;
62*7c478bd9Sstevel@tonic-gate 			if (grpmode > 07)
63*7c478bd9Sstevel@tonic-gate 				return (-1);
64*7c478bd9Sstevel@tonic-gate 			continue;
65*7c478bd9Sstevel@tonic-gate 		}
66*7c478bd9Sstevel@tonic-gate 		if (tp->a_type == CLASS_OBJ) {
67*7c478bd9Sstevel@tonic-gate 			got_mask = 1;
68*7c478bd9Sstevel@tonic-gate 			mask = tp->a_perm;
69*7c478bd9Sstevel@tonic-gate 			if (mask > 07)
70*7c478bd9Sstevel@tonic-gate 				return (-1);
71*7c478bd9Sstevel@tonic-gate 			*modep |= (mask << 3);
72*7c478bd9Sstevel@tonic-gate 			continue;
73*7c478bd9Sstevel@tonic-gate 		}
74*7c478bd9Sstevel@tonic-gate 		if (tp->a_type == OTHER_OBJ) {
75*7c478bd9Sstevel@tonic-gate 			mode = tp->a_perm;
76*7c478bd9Sstevel@tonic-gate 			if (mode > 07)
77*7c478bd9Sstevel@tonic-gate 				return (-1);
78*7c478bd9Sstevel@tonic-gate 			*modep |= mode;
79*7c478bd9Sstevel@tonic-gate 			continue; /* we may break here if it is sorted */
80*7c478bd9Sstevel@tonic-gate 		}
81*7c478bd9Sstevel@tonic-gate 	}
82*7c478bd9Sstevel@tonic-gate 	if (!got_mask)
83*7c478bd9Sstevel@tonic-gate 		*modep |= (grpmode << 3);
84*7c478bd9Sstevel@tonic-gate 	return (0);
85*7c478bd9Sstevel@tonic-gate }
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate int
aclfrommode(aclent_t * aclbufp,int nentries,mode_t * modep)89*7c478bd9Sstevel@tonic-gate aclfrommode(aclent_t *aclbufp, int nentries, mode_t *modep)
90*7c478bd9Sstevel@tonic-gate {
91*7c478bd9Sstevel@tonic-gate 	aclent_t		*tp;
92*7c478bd9Sstevel@tonic-gate 	aclent_t		*savp;
93*7c478bd9Sstevel@tonic-gate 	mode_t 			mode;
94*7c478bd9Sstevel@tonic-gate 	mode_t 			grpmode;
95*7c478bd9Sstevel@tonic-gate 	int			which;
96*7c478bd9Sstevel@tonic-gate 	int			got_mask = 0;
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 	if (aclcheck(aclbufp, nentries, &which) != 0) {
99*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
100*7c478bd9Sstevel@tonic-gate 		return (-1);	/* errno is set in aclcheck() */
101*7c478bd9Sstevel@tonic-gate 	}
102*7c478bd9Sstevel@tonic-gate 	for (tp = aclbufp; nentries--; tp++) {
103*7c478bd9Sstevel@tonic-gate 		if (tp->a_type == USER_OBJ) {
104*7c478bd9Sstevel@tonic-gate 			mode = (*modep & 0700);
105*7c478bd9Sstevel@tonic-gate 			tp->a_perm = (mode >> 6);
106*7c478bd9Sstevel@tonic-gate 			continue;
107*7c478bd9Sstevel@tonic-gate 		}
108*7c478bd9Sstevel@tonic-gate 		if (tp->a_type == GROUP_OBJ) {
109*7c478bd9Sstevel@tonic-gate 			grpmode = (*modep & 070);
110*7c478bd9Sstevel@tonic-gate 			savp = tp;
111*7c478bd9Sstevel@tonic-gate 			continue;
112*7c478bd9Sstevel@tonic-gate 		}
113*7c478bd9Sstevel@tonic-gate 		if (tp->a_type == CLASS_OBJ) {
114*7c478bd9Sstevel@tonic-gate 			got_mask = 1;
115*7c478bd9Sstevel@tonic-gate 			mode = (*modep & 070);
116*7c478bd9Sstevel@tonic-gate 			tp->a_perm = (mode >> 3);
117*7c478bd9Sstevel@tonic-gate 			continue;
118*7c478bd9Sstevel@tonic-gate 		}
119*7c478bd9Sstevel@tonic-gate 		if (tp->a_type == OTHER_OBJ) {
120*7c478bd9Sstevel@tonic-gate 			mode = (*modep & 07);
121*7c478bd9Sstevel@tonic-gate 			tp->a_perm = (o_mode_t)mode;
122*7c478bd9Sstevel@tonic-gate 			continue; /* we may break here if it is sorted */
123*7c478bd9Sstevel@tonic-gate 		}
124*7c478bd9Sstevel@tonic-gate 	}
125*7c478bd9Sstevel@tonic-gate 	if (!got_mask)
126*7c478bd9Sstevel@tonic-gate 		savp->a_perm = (grpmode >> 3);
127*7c478bd9Sstevel@tonic-gate 	return (0);
128*7c478bd9Sstevel@tonic-gate }
129