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