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