xref: /illumos-gate/usr/src/cmd/chmod/common.c (revision 2a8bcb4e)
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 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved						*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
32*7c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
33*7c478bd9Sstevel@tonic-gate  */
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate /*
36*7c478bd9Sstevel@tonic-gate  * Use of this object by a utility (so far chmod, mkdir and mkfifo use
37*7c478bd9Sstevel@tonic-gate  * it) requires that the utility implement an error-processing routine
38*7c478bd9Sstevel@tonic-gate  * named errmsg(), with a prototype as specified below.
39*7c478bd9Sstevel@tonic-gate  *
40*7c478bd9Sstevel@tonic-gate  * This is necessary because the mode-parsing code here makes use of such
41*7c478bd9Sstevel@tonic-gate  * a routine, located in chmod.c.  The error-reporting style of the
42*7c478bd9Sstevel@tonic-gate  * utilities sharing this code differs enough that it is difficult to
43*7c478bd9Sstevel@tonic-gate  * implement a common version of this routine to be used by all.
44*7c478bd9Sstevel@tonic-gate  */
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate /*
47*7c478bd9Sstevel@tonic-gate  *  Note that many convolutions are necessary
48*7c478bd9Sstevel@tonic-gate  *  due to the re-use of bits between locking
49*7c478bd9Sstevel@tonic-gate  *  and setgid
50*7c478bd9Sstevel@tonic-gate  */
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate #include <ctype.h>
53*7c478bd9Sstevel@tonic-gate #include <stdio.h>
54*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
55*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
56*7c478bd9Sstevel@tonic-gate #include <dirent.h>
57*7c478bd9Sstevel@tonic-gate #include <locale.h>
58*7c478bd9Sstevel@tonic-gate #include <string.h>	/* strerror() */
59*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate #define	USER	05700	/* user's bits */
62*7c478bd9Sstevel@tonic-gate #define	GROUP	02070	/* group's bits */
63*7c478bd9Sstevel@tonic-gate #define	OTHER	00007	/* other's bits */
64*7c478bd9Sstevel@tonic-gate #define	ALL	07777	/* all */
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate #define	READ	00444	/* read permit */
67*7c478bd9Sstevel@tonic-gate #define	WRITE	00222	/* write permit */
68*7c478bd9Sstevel@tonic-gate #define	EXEC	00111	/* exec permit */
69*7c478bd9Sstevel@tonic-gate #define	SETID	06000	/* set[ug]id */
70*7c478bd9Sstevel@tonic-gate #define	LOCK	02000	/* lock permit */
71*7c478bd9Sstevel@tonic-gate #define	STICKY	01000	/* sticky bit */
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate #define	GROUP_RWX	(GROUP & (READ | WRITE | EXEC))
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate #define	WHO_EMPTY 0
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate static char *msp;
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate extern void
80*7c478bd9Sstevel@tonic-gate errmsg(int severity, int code, char *format, ...);
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate static int
83*7c478bd9Sstevel@tonic-gate what(void);
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate static mode_t
86*7c478bd9Sstevel@tonic-gate abs(mode_t mode, o_mode_t *group_clear_bits, o_mode_t *group_set_bits),
87*7c478bd9Sstevel@tonic-gate who(void);
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate mode_t
90*7c478bd9Sstevel@tonic-gate newmode_common(char *ms, mode_t new_mode, mode_t umsk, char *file, char *path,
91*7c478bd9Sstevel@tonic-gate     o_mode_t *group_clear_bits, o_mode_t *group_set_bits);
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate /*
94*7c478bd9Sstevel@tonic-gate  * Wrapper for newmode_common.  This function is called by mkdir and
95*7c478bd9Sstevel@tonic-gate  * mkfifo.
96*7c478bd9Sstevel@tonic-gate  */
97*7c478bd9Sstevel@tonic-gate mode_t
newmode(char * ms,mode_t new_mode,mode_t umsk,char * file,char * path)98*7c478bd9Sstevel@tonic-gate newmode(char *ms, mode_t new_mode, mode_t umsk, char *file, char *path)
99*7c478bd9Sstevel@tonic-gate {
100*7c478bd9Sstevel@tonic-gate 	o_mode_t tmp1, tmp2;
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	return (newmode_common(ms, new_mode, umsk, file, path, &tmp1, &tmp2));
103*7c478bd9Sstevel@tonic-gate }
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate /*
106*7c478bd9Sstevel@tonic-gate  *  We are parsing a comma-separated list of mode expressions of the form:
107*7c478bd9Sstevel@tonic-gate  *
108*7c478bd9Sstevel@tonic-gate  *			 [<who>] <op> [<perms>]
109*7c478bd9Sstevel@tonic-gate  */
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
112*7c478bd9Sstevel@tonic-gate mode_t
newmode_common(char * ms,mode_t new_mode,mode_t umsk,char * file,char * path,o_mode_t * group_clear_bits,o_mode_t * group_set_bits)113*7c478bd9Sstevel@tonic-gate newmode_common(char *ms, mode_t new_mode, mode_t umsk, char *file, char *path,
114*7c478bd9Sstevel@tonic-gate     o_mode_t *group_clear_bits, o_mode_t *group_set_bits)
115*7c478bd9Sstevel@tonic-gate {
116*7c478bd9Sstevel@tonic-gate 	/*
117*7c478bd9Sstevel@tonic-gate 	 * new_mode  contains the mode value constructed by parsing the
118*7c478bd9Sstevel@tonic-gate 	 *			 expression pointed to by ms
119*7c478bd9Sstevel@tonic-gate 	 * old_mode  contains the mode provided by the caller
120*7c478bd9Sstevel@tonic-gate 	 * oper		 contains +|-|= information
121*7c478bd9Sstevel@tonic-gate 	 * perms_msk contains rwx(slt) information
122*7c478bd9Sstevel@tonic-gate 	 * umsk		 contains the umask value to be assumed.
123*7c478bd9Sstevel@tonic-gate 	 * who_empty is non-zero if the <who> clause did not appear.
124*7c478bd9Sstevel@tonic-gate 	 * who_msk   contains USER|GROUP|OTHER information
125*7c478bd9Sstevel@tonic-gate 	 */
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 	int oper;	/* <op> */
128*7c478bd9Sstevel@tonic-gate 	int lcheck;
129*7c478bd9Sstevel@tonic-gate 	int scheck;
130*7c478bd9Sstevel@tonic-gate 	int xcheck;
131*7c478bd9Sstevel@tonic-gate 	int goon;
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	int operand_empty = 0;
134*7c478bd9Sstevel@tonic-gate 	int who_empty;
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate 	mode_t who_msk;
137*7c478bd9Sstevel@tonic-gate 	mode_t perms_msk;
138*7c478bd9Sstevel@tonic-gate 	mode_t old_mode = new_mode;	/* save original mode */
139*7c478bd9Sstevel@tonic-gate 	mode_t grp_change;
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 	msp = ms;
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 	*group_clear_bits = 0;
144*7c478bd9Sstevel@tonic-gate 	*group_set_bits = 0;
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate 	if (isdigit(*msp))
147*7c478bd9Sstevel@tonic-gate 		return (abs(old_mode, group_clear_bits, group_set_bits));
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 	do {
150*7c478bd9Sstevel@tonic-gate 		/*
151*7c478bd9Sstevel@tonic-gate 		 * When <who> is empty, and <oper> == `=`, the umask is
152*7c478bd9Sstevel@tonic-gate 		 * obeyed.  So we need to make note of it here, for use
153*7c478bd9Sstevel@tonic-gate 		 * later.
154*7c478bd9Sstevel@tonic-gate 		 */
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 		if ((who_msk = who()) == WHO_EMPTY) {
157*7c478bd9Sstevel@tonic-gate 			who_empty = 1;
158*7c478bd9Sstevel@tonic-gate 			who_msk = ALL;
159*7c478bd9Sstevel@tonic-gate 		} else {
160*7c478bd9Sstevel@tonic-gate 			who_empty = 0;
161*7c478bd9Sstevel@tonic-gate 		}
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate 		while (oper = what()) {
164*7c478bd9Sstevel@tonic-gate 			/*
165*7c478bd9Sstevel@tonic-gate 			 *  this section processes permissions
166*7c478bd9Sstevel@tonic-gate 			 */
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate 			operand_empty++;
169*7c478bd9Sstevel@tonic-gate 			perms_msk = 0;
170*7c478bd9Sstevel@tonic-gate 			goon = 0;
171*7c478bd9Sstevel@tonic-gate 			lcheck = scheck = xcheck = 0;
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate 			switch (*msp) {
174*7c478bd9Sstevel@tonic-gate 			case 'u':
175*7c478bd9Sstevel@tonic-gate 				perms_msk = (new_mode & USER) >> 6;
176*7c478bd9Sstevel@tonic-gate 				goto dup;
177*7c478bd9Sstevel@tonic-gate 			case 'g':
178*7c478bd9Sstevel@tonic-gate 				perms_msk = (new_mode & GROUP) >> 3;
179*7c478bd9Sstevel@tonic-gate 				goto dup;
180*7c478bd9Sstevel@tonic-gate 			case 'o':
181*7c478bd9Sstevel@tonic-gate 				perms_msk = (new_mode & OTHER);
182*7c478bd9Sstevel@tonic-gate 			dup:
183*7c478bd9Sstevel@tonic-gate 				perms_msk &= (READ|WRITE|EXEC);
184*7c478bd9Sstevel@tonic-gate 				perms_msk |= (perms_msk << 3) |
185*7c478bd9Sstevel@tonic-gate 				    (perms_msk << 6);
186*7c478bd9Sstevel@tonic-gate 				msp++;
187*7c478bd9Sstevel@tonic-gate 				goon = 1;
188*7c478bd9Sstevel@tonic-gate 			}
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate 			while (goon == 0) {
191*7c478bd9Sstevel@tonic-gate 				switch (*msp++) {
192*7c478bd9Sstevel@tonic-gate 				case 'r':
193*7c478bd9Sstevel@tonic-gate 					perms_msk |= READ;
194*7c478bd9Sstevel@tonic-gate 					continue;
195*7c478bd9Sstevel@tonic-gate 				case 'w':
196*7c478bd9Sstevel@tonic-gate 					perms_msk |= WRITE;
197*7c478bd9Sstevel@tonic-gate 					continue;
198*7c478bd9Sstevel@tonic-gate 				case 'x':
199*7c478bd9Sstevel@tonic-gate 					perms_msk |= EXEC;
200*7c478bd9Sstevel@tonic-gate 					xcheck = 1;
201*7c478bd9Sstevel@tonic-gate 					continue;
202*7c478bd9Sstevel@tonic-gate 				case 'X':
203*7c478bd9Sstevel@tonic-gate 					if (((old_mode & S_IFMT) == S_IFDIR) ||
204*7c478bd9Sstevel@tonic-gate 					    (old_mode & EXEC)) {
205*7c478bd9Sstevel@tonic-gate 						perms_msk |= EXEC;
206*7c478bd9Sstevel@tonic-gate 						xcheck = 1;
207*7c478bd9Sstevel@tonic-gate 					}
208*7c478bd9Sstevel@tonic-gate 					continue;
209*7c478bd9Sstevel@tonic-gate 				case 'l':
210*7c478bd9Sstevel@tonic-gate 					perms_msk |= LOCK;
211*7c478bd9Sstevel@tonic-gate 					who_msk |= LOCK;
212*7c478bd9Sstevel@tonic-gate 					lcheck = 1;
213*7c478bd9Sstevel@tonic-gate 					continue;
214*7c478bd9Sstevel@tonic-gate 				case 's':
215*7c478bd9Sstevel@tonic-gate 					perms_msk |= SETID;
216*7c478bd9Sstevel@tonic-gate 					scheck = 1;
217*7c478bd9Sstevel@tonic-gate 					continue;
218*7c478bd9Sstevel@tonic-gate 				case 't':
219*7c478bd9Sstevel@tonic-gate 					perms_msk |= STICKY;
220*7c478bd9Sstevel@tonic-gate 					continue;
221*7c478bd9Sstevel@tonic-gate 				default:
222*7c478bd9Sstevel@tonic-gate 					msp--;
223*7c478bd9Sstevel@tonic-gate 					goon = 1;
224*7c478bd9Sstevel@tonic-gate 				}
225*7c478bd9Sstevel@tonic-gate 			}
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 			perms_msk &= who_msk;
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate 			switch (oper) {
230*7c478bd9Sstevel@tonic-gate 			case '+':
231*7c478bd9Sstevel@tonic-gate 				if (who_empty) {
232*7c478bd9Sstevel@tonic-gate 					perms_msk &= ~umsk;
233*7c478bd9Sstevel@tonic-gate 				}
234*7c478bd9Sstevel@tonic-gate 
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate 				/* is group execution requested? */
237*7c478bd9Sstevel@tonic-gate 				if (xcheck == 1 &&
238*7c478bd9Sstevel@tonic-gate 				    (perms_msk & GROUP & EXEC) ==
239*7c478bd9Sstevel@tonic-gate 				    (GROUP & EXEC)) {
240*7c478bd9Sstevel@tonic-gate 					/* not locking, too! */
241*7c478bd9Sstevel@tonic-gate 					if (lcheck == 1 && !S_ISDIR(new_mode)) {
242*7c478bd9Sstevel@tonic-gate 						errmsg(1, 3,
243*7c478bd9Sstevel@tonic-gate 						    gettext("Group execution "
244*7c478bd9Sstevel@tonic-gate 						    "and locking not permitted "
245*7c478bd9Sstevel@tonic-gate 						    "together\n"));
246*7c478bd9Sstevel@tonic-gate 					}
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate 					/*
249*7c478bd9Sstevel@tonic-gate 					 * not if the file is already
250*7c478bd9Sstevel@tonic-gate 					 * lockable.
251*7c478bd9Sstevel@tonic-gate 					 */
252*7c478bd9Sstevel@tonic-gate 					if (((new_mode & GROUP &
253*7c478bd9Sstevel@tonic-gate 					    (LOCK | EXEC)) == LOCK) &&
254*7c478bd9Sstevel@tonic-gate 					    !S_ISDIR(new_mode)) {
255*7c478bd9Sstevel@tonic-gate 						errmsg(2, 0,
256*7c478bd9Sstevel@tonic-gate 						    gettext("%s: Group "
257*7c478bd9Sstevel@tonic-gate 						    "execution not permitted "
258*7c478bd9Sstevel@tonic-gate 						    "on a lockable file\n"),
259*7c478bd9Sstevel@tonic-gate 						    path);
260*7c478bd9Sstevel@tonic-gate 						return (old_mode);
261*7c478bd9Sstevel@tonic-gate 					}
262*7c478bd9Sstevel@tonic-gate 				}
263*7c478bd9Sstevel@tonic-gate 
264*7c478bd9Sstevel@tonic-gate 				/* is setgid on execution requested? */
265*7c478bd9Sstevel@tonic-gate 				if (scheck == 1 && (perms_msk & GROUP & SETID)
266*7c478bd9Sstevel@tonic-gate 				    == (GROUP & SETID)) {
267*7c478bd9Sstevel@tonic-gate 					/* not locking, too! */
268*7c478bd9Sstevel@tonic-gate 					if (lcheck == 1 &&
269*7c478bd9Sstevel@tonic-gate 					    ((perms_msk & GROUP & EXEC) ==
270*7c478bd9Sstevel@tonic-gate 					    (GROUP & EXEC)) &&
271*7c478bd9Sstevel@tonic-gate 					    !S_ISDIR(new_mode)) {
272*7c478bd9Sstevel@tonic-gate 						errmsg(1, 4,
273*7c478bd9Sstevel@tonic-gate 						    gettext("Set-group-ID and "
274*7c478bd9Sstevel@tonic-gate 						    "locking not permitted "
275*7c478bd9Sstevel@tonic-gate 						    "together\n"));
276*7c478bd9Sstevel@tonic-gate 					}
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate 					/*
279*7c478bd9Sstevel@tonic-gate 					 * not if the file is already
280*7c478bd9Sstevel@tonic-gate 					 * lockable
281*7c478bd9Sstevel@tonic-gate 					 */
282*7c478bd9Sstevel@tonic-gate 
283*7c478bd9Sstevel@tonic-gate 					if (((new_mode & GROUP &
284*7c478bd9Sstevel@tonic-gate 					    (LOCK | EXEC)) == LOCK) &&
285*7c478bd9Sstevel@tonic-gate 					    !S_ISDIR(new_mode)) {
286*7c478bd9Sstevel@tonic-gate 						errmsg(2, 0,
287*7c478bd9Sstevel@tonic-gate 						    gettext("%s: Set-group-ID "
288*7c478bd9Sstevel@tonic-gate 						    "not permitted on a "
289*7c478bd9Sstevel@tonic-gate 						    "lockable file\n"), path);
290*7c478bd9Sstevel@tonic-gate 						return (old_mode);
291*7c478bd9Sstevel@tonic-gate 					}
292*7c478bd9Sstevel@tonic-gate 				}
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate 				/* is setid on execution requested? */
295*7c478bd9Sstevel@tonic-gate 				if ((scheck == 1) &&
296*7c478bd9Sstevel@tonic-gate 				    ((new_mode & S_IFMT) != S_IFDIR)) {
297*7c478bd9Sstevel@tonic-gate 					/*
298*7c478bd9Sstevel@tonic-gate 					 * the corresponding execution must
299*7c478bd9Sstevel@tonic-gate 					 * be requested or already set
300*7c478bd9Sstevel@tonic-gate 					 */
301*7c478bd9Sstevel@tonic-gate 					if (((new_mode | perms_msk) &
302*7c478bd9Sstevel@tonic-gate 					    who_msk & EXEC & (USER | GROUP)) !=
303*7c478bd9Sstevel@tonic-gate 					    (who_msk & EXEC & (USER | GROUP))) {
304*7c478bd9Sstevel@tonic-gate 						errmsg(2, 0,
305*7c478bd9Sstevel@tonic-gate 						    gettext("%s: Execute "
306*7c478bd9Sstevel@tonic-gate 						    "permission required "
307*7c478bd9Sstevel@tonic-gate 						    "for set-ID on "
308*7c478bd9Sstevel@tonic-gate 						    "execution \n"),
309*7c478bd9Sstevel@tonic-gate 						    path);
310*7c478bd9Sstevel@tonic-gate 						return (old_mode);
311*7c478bd9Sstevel@tonic-gate 					}
312*7c478bd9Sstevel@tonic-gate 				}
313*7c478bd9Sstevel@tonic-gate 
314*7c478bd9Sstevel@tonic-gate 				/* is locking requested? */
315*7c478bd9Sstevel@tonic-gate 				if (lcheck == 1) {
316*7c478bd9Sstevel@tonic-gate 					/*
317*7c478bd9Sstevel@tonic-gate 					 * not if the file has group execution
318*7c478bd9Sstevel@tonic-gate 					 * set.
319*7c478bd9Sstevel@tonic-gate 					 * NOTE: this also covers files with
320*7c478bd9Sstevel@tonic-gate 					 * setgid
321*7c478bd9Sstevel@tonic-gate 					 */
322*7c478bd9Sstevel@tonic-gate 					if ((new_mode & GROUP & EXEC) ==
323*7c478bd9Sstevel@tonic-gate 					    (GROUP & EXEC) &&
324*7c478bd9Sstevel@tonic-gate 					    !S_ISDIR(new_mode)) {
325*7c478bd9Sstevel@tonic-gate 						errmsg(2, 0,
326*7c478bd9Sstevel@tonic-gate 						    gettext("%s: Locking not "
327*7c478bd9Sstevel@tonic-gate 						    "permitted on "
328*7c478bd9Sstevel@tonic-gate 						    "a group executable "
329*7c478bd9Sstevel@tonic-gate 						    "file\n"),
330*7c478bd9Sstevel@tonic-gate 						    path);
331*7c478bd9Sstevel@tonic-gate 						return (old_mode);
332*7c478bd9Sstevel@tonic-gate 					}
333*7c478bd9Sstevel@tonic-gate 				}
334*7c478bd9Sstevel@tonic-gate 
335*7c478bd9Sstevel@tonic-gate 				if ((grp_change = (perms_msk & GROUP_RWX) >> 3)
336*7c478bd9Sstevel@tonic-gate 				    != 0) {
337*7c478bd9Sstevel@tonic-gate 					*group_clear_bits &= ~grp_change;
338*7c478bd9Sstevel@tonic-gate 					*group_set_bits |= grp_change;
339*7c478bd9Sstevel@tonic-gate 				}
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate 				/* create new mode */
342*7c478bd9Sstevel@tonic-gate 				new_mode |= perms_msk;
343*7c478bd9Sstevel@tonic-gate 				break;
344*7c478bd9Sstevel@tonic-gate 
345*7c478bd9Sstevel@tonic-gate 			case '-':
346*7c478bd9Sstevel@tonic-gate 				if (who_empty) {
347*7c478bd9Sstevel@tonic-gate 					perms_msk &= ~umsk;
348*7c478bd9Sstevel@tonic-gate 				}
349*7c478bd9Sstevel@tonic-gate 
350*7c478bd9Sstevel@tonic-gate 				/* don't turn off locking, unless it's on */
351*7c478bd9Sstevel@tonic-gate 				if (lcheck == 1 && scheck == 0 &&
352*7c478bd9Sstevel@tonic-gate 				    (new_mode & GROUP & (LOCK | EXEC)) !=
353*7c478bd9Sstevel@tonic-gate 				    LOCK) {
354*7c478bd9Sstevel@tonic-gate 					perms_msk &= ~LOCK;
355*7c478bd9Sstevel@tonic-gate 				}
356*7c478bd9Sstevel@tonic-gate 
357*7c478bd9Sstevel@tonic-gate 				/* don't turn off setgid, unless it's on */
358*7c478bd9Sstevel@tonic-gate 				if (scheck == 1 &&
359*7c478bd9Sstevel@tonic-gate 				    ((new_mode & S_IFMT) != S_IFDIR) &&
360*7c478bd9Sstevel@tonic-gate 				    lcheck == 0 &&
361*7c478bd9Sstevel@tonic-gate 				    (new_mode & GROUP & (LOCK | EXEC)) ==
362*7c478bd9Sstevel@tonic-gate 				    LOCK) {
363*7c478bd9Sstevel@tonic-gate 					perms_msk &= ~(GROUP & SETID);
364*7c478bd9Sstevel@tonic-gate 				}
365*7c478bd9Sstevel@tonic-gate 
366*7c478bd9Sstevel@tonic-gate 				/*
367*7c478bd9Sstevel@tonic-gate 				 * if execution is being turned off and the
368*7c478bd9Sstevel@tonic-gate 				 * corresponding setid is not, turn setid off,
369*7c478bd9Sstevel@tonic-gate 				 * too & warn the user
370*7c478bd9Sstevel@tonic-gate 				 */
371*7c478bd9Sstevel@tonic-gate 				if (xcheck == 1 && scheck == 0 &&
372*7c478bd9Sstevel@tonic-gate 				    ((who_msk & GROUP) == GROUP ||
373*7c478bd9Sstevel@tonic-gate 				    (who_msk & USER) == USER) &&
374*7c478bd9Sstevel@tonic-gate 				    (new_mode & who_msk & (SETID | EXEC)) ==
375*7c478bd9Sstevel@tonic-gate 				    (who_msk & (SETID | EXEC)) &&
376*7c478bd9Sstevel@tonic-gate 				    !S_ISDIR(new_mode)) {
377*7c478bd9Sstevel@tonic-gate 					errmsg(2, 0,
378*7c478bd9Sstevel@tonic-gate 					    gettext("%s: Corresponding set-ID "
379*7c478bd9Sstevel@tonic-gate 					    "also disabled on file since "
380*7c478bd9Sstevel@tonic-gate 					    "set-ID requires execute "
381*7c478bd9Sstevel@tonic-gate 					    "permission\n"),
382*7c478bd9Sstevel@tonic-gate 					    path);
383*7c478bd9Sstevel@tonic-gate 
384*7c478bd9Sstevel@tonic-gate 					if ((perms_msk & USER & SETID) !=
385*7c478bd9Sstevel@tonic-gate 					    (USER & SETID) && (new_mode &
386*7c478bd9Sstevel@tonic-gate 					    USER & (SETID | EXEC)) ==
387*7c478bd9Sstevel@tonic-gate 					    (who_msk & USER &
388*7c478bd9Sstevel@tonic-gate 					    (SETID | EXEC))) {
389*7c478bd9Sstevel@tonic-gate 						perms_msk |= USER & SETID;
390*7c478bd9Sstevel@tonic-gate 					}
391*7c478bd9Sstevel@tonic-gate 					if ((perms_msk & GROUP & SETID) !=
392*7c478bd9Sstevel@tonic-gate 					    (GROUP & SETID) &&
393*7c478bd9Sstevel@tonic-gate 					    (new_mode & GROUP &
394*7c478bd9Sstevel@tonic-gate 					    (SETID | EXEC)) ==
395*7c478bd9Sstevel@tonic-gate 					    (who_msk & GROUP &
396*7c478bd9Sstevel@tonic-gate 					    (SETID | EXEC))) {
397*7c478bd9Sstevel@tonic-gate 						perms_msk |= GROUP & SETID;
398*7c478bd9Sstevel@tonic-gate 					}
399*7c478bd9Sstevel@tonic-gate 				}
400*7c478bd9Sstevel@tonic-gate 
401*7c478bd9Sstevel@tonic-gate 				if ((grp_change = (perms_msk & GROUP_RWX) >> 3)
402*7c478bd9Sstevel@tonic-gate 				    != 0) {
403*7c478bd9Sstevel@tonic-gate 					*group_clear_bits |= grp_change;
404*7c478bd9Sstevel@tonic-gate 					*group_set_bits &= ~grp_change;
405*7c478bd9Sstevel@tonic-gate 				}
406*7c478bd9Sstevel@tonic-gate 
407*7c478bd9Sstevel@tonic-gate 				/* create new mode */
408*7c478bd9Sstevel@tonic-gate 				new_mode &= ~perms_msk;
409*7c478bd9Sstevel@tonic-gate 				break;
410*7c478bd9Sstevel@tonic-gate 
411*7c478bd9Sstevel@tonic-gate 			case '=':
412*7c478bd9Sstevel@tonic-gate 				if (who_empty) {
413*7c478bd9Sstevel@tonic-gate 					perms_msk &= ~umsk;
414*7c478bd9Sstevel@tonic-gate 				}
415*7c478bd9Sstevel@tonic-gate 				/* is locking requested? */
416*7c478bd9Sstevel@tonic-gate 				if (lcheck == 1) {
417*7c478bd9Sstevel@tonic-gate 					/* not group execution, too! */
418*7c478bd9Sstevel@tonic-gate 					if ((perms_msk & GROUP & EXEC) ==
419*7c478bd9Sstevel@tonic-gate 					    (GROUP & EXEC) &&
420*7c478bd9Sstevel@tonic-gate 					    !S_ISDIR(new_mode)) {
421*7c478bd9Sstevel@tonic-gate 						errmsg(1, 3,
422*7c478bd9Sstevel@tonic-gate 						    gettext("Group execution "
423*7c478bd9Sstevel@tonic-gate 						    "and locking not "
424*7c478bd9Sstevel@tonic-gate 						    "permitted together\n"));
425*7c478bd9Sstevel@tonic-gate 					}
426*7c478bd9Sstevel@tonic-gate 
427*7c478bd9Sstevel@tonic-gate 					/*
428*7c478bd9Sstevel@tonic-gate 					 * if the file has group execution set,
429*7c478bd9Sstevel@tonic-gate 					 * turn it off!
430*7c478bd9Sstevel@tonic-gate 					 */
431*7c478bd9Sstevel@tonic-gate 					if ((who_msk & GROUP) != GROUP) {
432*7c478bd9Sstevel@tonic-gate 						new_mode &= ~(GROUP & EXEC);
433*7c478bd9Sstevel@tonic-gate 					}
434*7c478bd9Sstevel@tonic-gate 				}
435*7c478bd9Sstevel@tonic-gate 
436*7c478bd9Sstevel@tonic-gate 				/*
437*7c478bd9Sstevel@tonic-gate 				 * is setid on execution requested? the
438*7c478bd9Sstevel@tonic-gate 				 * corresponding execution must be requested,
439*7c478bd9Sstevel@tonic-gate 				 * too!
440*7c478bd9Sstevel@tonic-gate 				 */
441*7c478bd9Sstevel@tonic-gate 				if (scheck == 1 &&
442*7c478bd9Sstevel@tonic-gate 				    (perms_msk & EXEC & (USER | GROUP)) !=
443*7c478bd9Sstevel@tonic-gate 				    (who_msk & EXEC & (USER | GROUP)) &&
444*7c478bd9Sstevel@tonic-gate 					!S_ISDIR(new_mode)) {
445*7c478bd9Sstevel@tonic-gate 					errmsg(1, 2,
446*7c478bd9Sstevel@tonic-gate 					    gettext("Execute permission "
447*7c478bd9Sstevel@tonic-gate 					    "required for set-ID on "
448*7c478bd9Sstevel@tonic-gate 					    "execution\n"));
449*7c478bd9Sstevel@tonic-gate 				}
450*7c478bd9Sstevel@tonic-gate 
451*7c478bd9Sstevel@tonic-gate 				/*
452*7c478bd9Sstevel@tonic-gate 				 * The ISGID bit on directories will not be
453*7c478bd9Sstevel@tonic-gate 				 * changed when the mode argument is a string
454*7c478bd9Sstevel@tonic-gate 				 * with "=".
455*7c478bd9Sstevel@tonic-gate 				 */
456*7c478bd9Sstevel@tonic-gate 				if ((old_mode & S_IFMT) == S_IFDIR)
457*7c478bd9Sstevel@tonic-gate 					perms_msk = (perms_msk &
458*7c478bd9Sstevel@tonic-gate 					    ~S_ISGID) | (old_mode & S_ISGID);
459*7c478bd9Sstevel@tonic-gate 
460*7c478bd9Sstevel@tonic-gate 				/*
461*7c478bd9Sstevel@tonic-gate 				 * create new mode:
462*7c478bd9Sstevel@tonic-gate 				 *   clear the who_msk bits
463*7c478bd9Sstevel@tonic-gate 				 *   set the perms_mks bits (which have
464*7c478bd9Sstevel@tonic-gate 				 *   been trimmed to fit the who_msk.
465*7c478bd9Sstevel@tonic-gate 				 */
466*7c478bd9Sstevel@tonic-gate 
467*7c478bd9Sstevel@tonic-gate 				if ((grp_change = (perms_msk & GROUP_RWX) >> 3)
468*7c478bd9Sstevel@tonic-gate 				    != 0) {
469*7c478bd9Sstevel@tonic-gate 					*group_clear_bits = GROUP_RWX >> 3;
470*7c478bd9Sstevel@tonic-gate 					*group_set_bits = grp_change;
471*7c478bd9Sstevel@tonic-gate 				}
472*7c478bd9Sstevel@tonic-gate 
473*7c478bd9Sstevel@tonic-gate 				new_mode &= ~who_msk;
474*7c478bd9Sstevel@tonic-gate 				new_mode |= perms_msk;
475*7c478bd9Sstevel@tonic-gate 				break;
476*7c478bd9Sstevel@tonic-gate 			}
477*7c478bd9Sstevel@tonic-gate 		}
478*7c478bd9Sstevel@tonic-gate 	} while (*msp++ == ',');
479*7c478bd9Sstevel@tonic-gate 
480*7c478bd9Sstevel@tonic-gate 	if (*--msp || operand_empty == 0) {
481*7c478bd9Sstevel@tonic-gate 		errmsg(1, 5, gettext("invalid mode\n"));
482*7c478bd9Sstevel@tonic-gate 	}
483*7c478bd9Sstevel@tonic-gate 
484*7c478bd9Sstevel@tonic-gate 	return (new_mode);
485*7c478bd9Sstevel@tonic-gate }
486*7c478bd9Sstevel@tonic-gate 
487*7c478bd9Sstevel@tonic-gate mode_t
abs(mode_t mode,o_mode_t * group_clear_bits,o_mode_t * group_set_bits)488*7c478bd9Sstevel@tonic-gate abs(mode_t mode, o_mode_t *group_clear_bits, o_mode_t *group_set_bits)
489*7c478bd9Sstevel@tonic-gate {
490*7c478bd9Sstevel@tonic-gate 	int c;
491*7c478bd9Sstevel@tonic-gate 	mode_t i;
492*7c478bd9Sstevel@tonic-gate 
493*7c478bd9Sstevel@tonic-gate 	for (i = 0; (c = *msp) >= '0' && c <= '7'; msp++)
494*7c478bd9Sstevel@tonic-gate 		i = (mode_t)((i << 3) + (c - '0'));
495*7c478bd9Sstevel@tonic-gate 	if (*msp)
496*7c478bd9Sstevel@tonic-gate 		errmsg(1, 6, gettext("invalid mode\n"));
497*7c478bd9Sstevel@tonic-gate 
498*7c478bd9Sstevel@tonic-gate /*
499*7c478bd9Sstevel@tonic-gate  * The ISGID bit on directories will not be changed when the mode argument is
500*7c478bd9Sstevel@tonic-gate  * octal numeric. Only "g+s" and "g-s" arguments can change ISGID bit when
501*7c478bd9Sstevel@tonic-gate  * applied to directories.
502*7c478bd9Sstevel@tonic-gate  */
503*7c478bd9Sstevel@tonic-gate 	*group_clear_bits = GROUP_RWX >> 3;
504*7c478bd9Sstevel@tonic-gate 	*group_set_bits = (i & GROUP_RWX) >> 3;
505*7c478bd9Sstevel@tonic-gate 	if ((mode & S_IFMT) == S_IFDIR)
506*7c478bd9Sstevel@tonic-gate 		return ((i & ~S_ISGID) | (mode & S_ISGID));
507*7c478bd9Sstevel@tonic-gate 	return (i);
508*7c478bd9Sstevel@tonic-gate }
509*7c478bd9Sstevel@tonic-gate 
510*7c478bd9Sstevel@tonic-gate static mode_t
who(void)511*7c478bd9Sstevel@tonic-gate who(void)
512*7c478bd9Sstevel@tonic-gate {
513*7c478bd9Sstevel@tonic-gate 	mode_t m;
514*7c478bd9Sstevel@tonic-gate 
515*7c478bd9Sstevel@tonic-gate 	m = WHO_EMPTY;
516*7c478bd9Sstevel@tonic-gate 
517*7c478bd9Sstevel@tonic-gate 	for (; ; msp++) {
518*7c478bd9Sstevel@tonic-gate 		switch (*msp) {
519*7c478bd9Sstevel@tonic-gate 		case 'u':
520*7c478bd9Sstevel@tonic-gate 			m |= USER;
521*7c478bd9Sstevel@tonic-gate 			continue;
522*7c478bd9Sstevel@tonic-gate 		case 'g':
523*7c478bd9Sstevel@tonic-gate 			m |= GROUP;
524*7c478bd9Sstevel@tonic-gate 			continue;
525*7c478bd9Sstevel@tonic-gate 		case 'o':
526*7c478bd9Sstevel@tonic-gate 			m |= OTHER;
527*7c478bd9Sstevel@tonic-gate 			continue;
528*7c478bd9Sstevel@tonic-gate 		case 'a':
529*7c478bd9Sstevel@tonic-gate 			m |= ALL;
530*7c478bd9Sstevel@tonic-gate 			continue;
531*7c478bd9Sstevel@tonic-gate 		default:
532*7c478bd9Sstevel@tonic-gate 			return (m);
533*7c478bd9Sstevel@tonic-gate 		}
534*7c478bd9Sstevel@tonic-gate 	}
535*7c478bd9Sstevel@tonic-gate }
536*7c478bd9Sstevel@tonic-gate 
537*7c478bd9Sstevel@tonic-gate static int
what(void)538*7c478bd9Sstevel@tonic-gate what(void)
539*7c478bd9Sstevel@tonic-gate {
540*7c478bd9Sstevel@tonic-gate 	switch (*msp) {
541*7c478bd9Sstevel@tonic-gate 	case '+':
542*7c478bd9Sstevel@tonic-gate 	case '-':
543*7c478bd9Sstevel@tonic-gate 	case '=':
544*7c478bd9Sstevel@tonic-gate 		return (*msp++);
545*7c478bd9Sstevel@tonic-gate 	}
546*7c478bd9Sstevel@tonic-gate 	return (0);
547*7c478bd9Sstevel@tonic-gate }
548