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
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.
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]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * Portions of this source code were derived from Berkeley 4.3 BSD
32 * under license from the Regents of the University of California.
35 #pragma ident "%Z%%M% %I% %E% SMI"
38 * Use of this object by a utility (so far chmod, mkdir and mkfifo use
39 * it) requires that the utility implement an error-processing routine
40 * named errmsg(), with a prototype as specified below.
42 * This is necessary because the mode-parsing code here makes use of such
43 * a routine, located in chmod.c. The error-reporting style of the
44 * utilities sharing this code differs enough that it is difficult to
45 * implement a common version of this routine to be used by all.
49 * Note that many convolutions are necessary
50 * due to the re-use of bits between locking
56 #include <sys/types.h>
60 #include <string.h> /* strerror() */
63 #define USER 05700 /* user's bits */
64 #define GROUP 02070 /* group's bits */
65 #define OTHER 00007 /* other's bits */
66 #define ALL 07777 /* all */
68 #define READ 00444 /* read permit */
69 #define WRITE 00222 /* write permit */
70 #define EXEC 00111 /* exec permit */
71 #define SETID 06000 /* set[ug]id */
72 #define LOCK 02000 /* lock permit */
73 #define STICKY 01000 /* sticky bit */
75 #define GROUP_RWX (GROUP & (READ | WRITE | EXEC))
82 errmsg(int severity
, int code
, char *format
, ...);
88 abs(mode_t mode
, o_mode_t
*group_clear_bits
, o_mode_t
*group_set_bits
),
92 newmode_common(char *ms
, mode_t new_mode
, mode_t umsk
, char *file
, char *path
,
93 o_mode_t
*group_clear_bits
, o_mode_t
*group_set_bits
);
96 * Wrapper for newmode_common. This function is called by mkdir and
100 newmode(char *ms
, mode_t new_mode
, mode_t umsk
, char *file
, char *path
)
104 return (newmode_common(ms
, new_mode
, umsk
, file
, path
, &tmp1
, &tmp2
));
108 * We are parsing a comma-separated list of mode expressions of the form:
110 * [<who>] <op> [<perms>]
115 newmode_common(char *ms
, mode_t new_mode
, mode_t umsk
, char *file
, char *path
,
116 o_mode_t
*group_clear_bits
, o_mode_t
*group_set_bits
)
119 * new_mode contains the mode value constructed by parsing the
120 * expression pointed to by ms
121 * old_mode contains the mode provided by the caller
122 * oper contains +|-|= information
123 * perms_msk contains rwx(slt) information
124 * umsk contains the umask value to be assumed.
125 * who_empty is non-zero if the <who> clause did not appear.
126 * who_msk contains USER|GROUP|OTHER information
135 int operand_empty
= 0;
140 mode_t old_mode
= new_mode
; /* save original mode */
145 *group_clear_bits
= 0;
149 return (abs(old_mode
, group_clear_bits
, group_set_bits
));
153 * When <who> is empty, and <oper> == `=`, the umask is
154 * obeyed. So we need to make note of it here, for use
158 if ((who_msk
= who()) == WHO_EMPTY
) {
165 while (oper
= what()) {
167 * this section processes permissions
173 lcheck
= scheck
= xcheck
= 0;
177 perms_msk
= (new_mode
& USER
) >> 6;
180 perms_msk
= (new_mode
& GROUP
) >> 3;
183 perms_msk
= (new_mode
& OTHER
);
185 perms_msk
&= (READ
|WRITE
|EXEC
);
186 perms_msk
|= (perms_msk
<< 3) |
205 if (((old_mode
& S_IFMT
) == S_IFDIR
) ||
229 perms_msk
&= who_msk
;
238 /* is group execution requested? */
240 (perms_msk
& GROUP
& EXEC
) ==
242 /* not locking, too! */
243 if (lcheck
== 1 && !S_ISDIR(new_mode
)) {
245 gettext("Group execution "
246 "and locking not permitted "
251 * not if the file is already
254 if (((new_mode
& GROUP
&
255 (LOCK
| EXEC
)) == LOCK
) &&
256 !S_ISDIR(new_mode
)) {
259 "execution not permitted "
260 "on a lockable file\n"),
266 /* is setgid on execution requested? */
267 if (scheck
== 1 && (perms_msk
& GROUP
& SETID
)
268 == (GROUP
& SETID
)) {
269 /* not locking, too! */
271 ((perms_msk
& GROUP
& EXEC
) ==
273 !S_ISDIR(new_mode
)) {
275 gettext("Set-group-ID and "
276 "locking not permitted "
281 * not if the file is already
285 if (((new_mode
& GROUP
&
286 (LOCK
| EXEC
)) == LOCK
) &&
287 !S_ISDIR(new_mode
)) {
289 gettext("%s: Set-group-ID "
290 "not permitted on a "
291 "lockable file\n"), path
);
296 /* is setid on execution requested? */
298 ((new_mode
& S_IFMT
) != S_IFDIR
)) {
300 * the corresponding execution must
301 * be requested or already set
303 if (((new_mode
| perms_msk
) &
304 who_msk
& EXEC
& (USER
| GROUP
)) !=
305 (who_msk
& EXEC
& (USER
| GROUP
))) {
307 gettext("%s: Execute "
308 "permission required "
316 /* is locking requested? */
319 * not if the file has group execution
321 * NOTE: this also covers files with
324 if ((new_mode
& GROUP
& EXEC
) ==
326 !S_ISDIR(new_mode
)) {
328 gettext("%s: Locking not "
330 "a group executable "
337 if ((grp_change
= (perms_msk
& GROUP_RWX
) >> 3)
339 *group_clear_bits
&= ~grp_change
;
340 *group_set_bits
|= grp_change
;
343 /* create new mode */
344 new_mode
|= perms_msk
;
352 /* don't turn off locking, unless it's on */
353 if (lcheck
== 1 && scheck
== 0 &&
354 (new_mode
& GROUP
& (LOCK
| EXEC
)) !=
359 /* don't turn off setgid, unless it's on */
361 ((new_mode
& S_IFMT
) != S_IFDIR
) &&
363 (new_mode
& GROUP
& (LOCK
| EXEC
)) ==
365 perms_msk
&= ~(GROUP
& SETID
);
369 * if execution is being turned off and the
370 * corresponding setid is not, turn setid off,
371 * too & warn the user
373 if (xcheck
== 1 && scheck
== 0 &&
374 ((who_msk
& GROUP
) == GROUP
||
375 (who_msk
& USER
) == USER
) &&
376 (new_mode
& who_msk
& (SETID
| EXEC
)) ==
377 (who_msk
& (SETID
| EXEC
)) &&
378 !S_ISDIR(new_mode
)) {
380 gettext("%s: Corresponding set-ID "
381 "also disabled on file since "
382 "set-ID requires execute "
386 if ((perms_msk
& USER
& SETID
) !=
387 (USER
& SETID
) && (new_mode
&
388 USER
& (SETID
| EXEC
)) ==
391 perms_msk
|= USER
& SETID
;
393 if ((perms_msk
& GROUP
& SETID
) !=
399 perms_msk
|= GROUP
& SETID
;
403 if ((grp_change
= (perms_msk
& GROUP_RWX
) >> 3)
405 *group_clear_bits
|= grp_change
;
406 *group_set_bits
&= ~grp_change
;
409 /* create new mode */
410 new_mode
&= ~perms_msk
;
417 /* is locking requested? */
419 /* not group execution, too! */
420 if ((perms_msk
& GROUP
& EXEC
) ==
422 !S_ISDIR(new_mode
)) {
424 gettext("Group execution "
426 "permitted together\n"));
430 * if the file has group execution set,
433 if ((who_msk
& GROUP
) != GROUP
) {
434 new_mode
&= ~(GROUP
& EXEC
);
439 * is setid on execution requested? the
440 * corresponding execution must be requested,
444 (perms_msk
& EXEC
& (USER
| GROUP
)) !=
445 (who_msk
& EXEC
& (USER
| GROUP
)) &&
446 !S_ISDIR(new_mode
)) {
448 gettext("Execute permission "
449 "required for set-ID on "
454 * The ISGID bit on directories will not be
455 * changed when the mode argument is a string
458 if ((old_mode
& S_IFMT
) == S_IFDIR
)
459 perms_msk
= (perms_msk
&
460 ~S_ISGID
) | (old_mode
& S_ISGID
);
464 * clear the who_msk bits
465 * set the perms_mks bits (which have
466 * been trimmed to fit the who_msk.
469 if ((grp_change
= (perms_msk
& GROUP_RWX
) >> 3)
471 *group_clear_bits
= GROUP_RWX
>> 3;
472 *group_set_bits
= grp_change
;
475 new_mode
&= ~who_msk
;
476 new_mode
|= perms_msk
;
480 } while (*msp
++ == ',');
482 if (*--msp
|| operand_empty
== 0) {
483 errmsg(1, 5, gettext("invalid mode\n"));
490 abs(mode_t mode
, o_mode_t
*group_clear_bits
, o_mode_t
*group_set_bits
)
495 for (i
= 0; (c
= *msp
) >= '0' && c
<= '7'; msp
++)
496 i
= (mode_t
)((i
<< 3) + (c
- '0'));
498 errmsg(1, 6, gettext("invalid mode\n"));
501 * The ISGID bit on directories will not be changed when the mode argument is
502 * octal numeric. Only "g+s" and "g-s" arguments can change ISGID bit when
503 * applied to directories.
505 *group_clear_bits
= GROUP_RWX
>> 3;
506 *group_set_bits
= (i
& GROUP_RWX
) >> 3;
507 if ((mode
& S_IFMT
) == S_IFDIR
)
508 return ((i
& ~S_ISGID
) | (mode
& S_ISGID
));