xref: /illumos-gate/usr/src/lib/libsec/common/aclmode.c (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
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 /* LINTLIBRARY */
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * Convert ACL to/from permission bits
31*7c478bd9Sstevel@tonic-gate  */
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <errno.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/acl.h>
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate int
acltomode(aclent_t * aclbufp,int nentries,mode_t * modep)37*7c478bd9Sstevel@tonic-gate acltomode(aclent_t *aclbufp, int nentries, mode_t *modep)
38*7c478bd9Sstevel@tonic-gate {
39*7c478bd9Sstevel@tonic-gate 	aclent_t		*tp;
40*7c478bd9Sstevel@tonic-gate 	unsigned long		mode;
41*7c478bd9Sstevel@tonic-gate 	unsigned long		grpmode;
42*7c478bd9Sstevel@tonic-gate 	unsigned long		mask;
43*7c478bd9Sstevel@tonic-gate 	int			which;
44*7c478bd9Sstevel@tonic-gate 	int			got_mask = 0;
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate 	*modep = 0;
47*7c478bd9Sstevel@tonic-gate 	if (aclcheck(aclbufp, nentries, &which) != 0) {
48*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
49*7c478bd9Sstevel@tonic-gate 		return (-1);	/* errno is set in aclcheck() */
50*7c478bd9Sstevel@tonic-gate 	}
51*7c478bd9Sstevel@tonic-gate 	for (tp = aclbufp; nentries--; tp++) {
52*7c478bd9Sstevel@tonic-gate 		if (tp->a_type == USER_OBJ) {
53*7c478bd9Sstevel@tonic-gate 			mode = tp->a_perm;
54*7c478bd9Sstevel@tonic-gate 			if (mode > 07)
55*7c478bd9Sstevel@tonic-gate 				return (-1);
56*7c478bd9Sstevel@tonic-gate 			*modep |= (mode << 6);
57*7c478bd9Sstevel@tonic-gate 			continue;
58*7c478bd9Sstevel@tonic-gate 		}
59*7c478bd9Sstevel@tonic-gate 		if (tp->a_type == GROUP_OBJ) {
60*7c478bd9Sstevel@tonic-gate 			grpmode = tp->a_perm;
61*7c478bd9Sstevel@tonic-gate 			if (grpmode > 07)
62*7c478bd9Sstevel@tonic-gate 				return (-1);
63*7c478bd9Sstevel@tonic-gate 			continue;
64*7c478bd9Sstevel@tonic-gate 		}
65*7c478bd9Sstevel@tonic-gate 		if (tp->a_type == CLASS_OBJ) {
66*7c478bd9Sstevel@tonic-gate 			got_mask = 1;
67*7c478bd9Sstevel@tonic-gate 			mask = tp->a_perm;
68*7c478bd9Sstevel@tonic-gate 			if (mask > 07)
69*7c478bd9Sstevel@tonic-gate 				return (-1);
70*7c478bd9Sstevel@tonic-gate 			*modep |= (mask << 3);
71*7c478bd9Sstevel@tonic-gate 			continue;
72*7c478bd9Sstevel@tonic-gate 		}
73*7c478bd9Sstevel@tonic-gate 		if (tp->a_type == OTHER_OBJ) {
74*7c478bd9Sstevel@tonic-gate 			mode = tp->a_perm;
75*7c478bd9Sstevel@tonic-gate 			if (mode > 07)
76*7c478bd9Sstevel@tonic-gate 				return (-1);
77*7c478bd9Sstevel@tonic-gate 			*modep |= mode;
78*7c478bd9Sstevel@tonic-gate 			continue; /* we may break here if it is sorted */
79*7c478bd9Sstevel@tonic-gate 		}
80*7c478bd9Sstevel@tonic-gate 	}
81*7c478bd9Sstevel@tonic-gate 	if (!got_mask)
82*7c478bd9Sstevel@tonic-gate 		*modep |= (grpmode << 3);
83*7c478bd9Sstevel@tonic-gate 	return (0);
84*7c478bd9Sstevel@tonic-gate }
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate int
aclfrommode(aclent_t * aclbufp,int nentries,mode_t * modep)88*7c478bd9Sstevel@tonic-gate aclfrommode(aclent_t *aclbufp, int nentries, mode_t *modep)
89*7c478bd9Sstevel@tonic-gate {
90*7c478bd9Sstevel@tonic-gate 	aclent_t		*tp;
91*7c478bd9Sstevel@tonic-gate 	aclent_t		*savp;
92*7c478bd9Sstevel@tonic-gate 	mode_t 			mode;
93*7c478bd9Sstevel@tonic-gate 	mode_t 			grpmode;
94*7c478bd9Sstevel@tonic-gate 	int			which;
95*7c478bd9Sstevel@tonic-gate 	int			got_mask = 0;
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 	if (aclcheck(aclbufp, nentries, &which) != 0) {
98*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
99*7c478bd9Sstevel@tonic-gate 		return (-1);	/* errno is set in aclcheck() */
100*7c478bd9Sstevel@tonic-gate 	}
101*7c478bd9Sstevel@tonic-gate 	for (tp = aclbufp; nentries--; tp++) {
102*7c478bd9Sstevel@tonic-gate 		if (tp->a_type == USER_OBJ) {
103*7c478bd9Sstevel@tonic-gate 			mode = (*modep & 0700);
104*7c478bd9Sstevel@tonic-gate 			tp->a_perm = (mode >> 6);
105*7c478bd9Sstevel@tonic-gate 			continue;
106*7c478bd9Sstevel@tonic-gate 		}
107*7c478bd9Sstevel@tonic-gate 		if (tp->a_type == GROUP_OBJ) {
108*7c478bd9Sstevel@tonic-gate 			grpmode = (*modep & 070);
109*7c478bd9Sstevel@tonic-gate 			savp = tp;
110*7c478bd9Sstevel@tonic-gate 			continue;
111*7c478bd9Sstevel@tonic-gate 		}
112*7c478bd9Sstevel@tonic-gate 		if (tp->a_type == CLASS_OBJ) {
113*7c478bd9Sstevel@tonic-gate 			got_mask = 1;
114*7c478bd9Sstevel@tonic-gate 			mode = (*modep & 070);
115*7c478bd9Sstevel@tonic-gate 			tp->a_perm = (mode >> 3);
116*7c478bd9Sstevel@tonic-gate 			continue;
117*7c478bd9Sstevel@tonic-gate 		}
118*7c478bd9Sstevel@tonic-gate 		if (tp->a_type == OTHER_OBJ) {
119*7c478bd9Sstevel@tonic-gate 			mode = (*modep & 07);
120*7c478bd9Sstevel@tonic-gate 			tp->a_perm = (o_mode_t)mode;
121*7c478bd9Sstevel@tonic-gate 			continue; /* we may break here if it is sorted */
122*7c478bd9Sstevel@tonic-gate 		}
123*7c478bd9Sstevel@tonic-gate 	}
124*7c478bd9Sstevel@tonic-gate 	if (!got_mask)
125*7c478bd9Sstevel@tonic-gate 		savp->a_perm = (grpmode >> 3);
126*7c478bd9Sstevel@tonic-gate 	return (0);
127*7c478bd9Sstevel@tonic-gate }
128