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