Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 3 | * Mini chmod implementation for busybox |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 6 | * |
Eric Andersen | d274b53 | 2002-10-10 03:47:01 +0000 | [diff] [blame] | 7 | * Reworked by (C) 2002 Vladimir Oleynik <dzo@simtreas.ru> |
| 8 | * to correctly parse '-rwxgoa' |
| 9 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 10 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 11 | */ |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 12 | //config:config CHMOD |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 13 | //config: bool "chmod (5.5 kb)" |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 14 | //config: default y |
| 15 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 16 | //config: chmod is used to change the access permission of files. |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 17 | |
| 18 | //applet:IF_CHMOD(APPLET_NOEXEC(chmod, chmod, BB_DIR_BIN, BB_SUID_DROP, chmod)) |
| 19 | |
| 20 | //kbuild:lib-$(CONFIG_CHMOD) += chmod.o |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 21 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 22 | /* BB_AUDIT SUSv3 compliant */ |
Denis Vlasenko | fefb279 | 2006-10-27 15:13:54 +0000 | [diff] [blame] | 23 | /* BB_AUDIT GNU defects - unsupported long options. */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 24 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/chmod.html */ |
| 25 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 26 | //usage:#define chmod_trivial_usage |
| 27 | //usage: "[-R"IF_DESKTOP("cvf")"] MODE[,MODE]... FILE..." |
| 28 | //usage:#define chmod_full_usage "\n\n" |
Denys Vlasenko | e2b9215 | 2021-06-14 20:47:20 +0200 | [diff] [blame] | 29 | //usage: "MODE is octal number (bit pattern sstrwxrwxrwx) or [ugoa]{+|-|=}[rwxXst]" |
| 30 | //usage: "\n" |
| 31 | //next 4 options are the same for chmod/chown/chgrp: |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 32 | //usage: "\n -R Recurse" |
| 33 | //usage: IF_DESKTOP( |
| 34 | //usage: "\n -c List changed files" |
Denys Vlasenko | e2b9215 | 2021-06-14 20:47:20 +0200 | [diff] [blame] | 35 | //usage: "\n -v Verbose" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 36 | //usage: "\n -f Hide errors" |
| 37 | //usage: ) |
| 38 | //usage: |
| 39 | //usage:#define chmod_example_usage |
| 40 | //usage: "$ ls -l /tmp/foo\n" |
| 41 | //usage: "-rw-rw-r-- 1 root root 0 Apr 12 18:25 /tmp/foo\n" |
| 42 | //usage: "$ chmod u+x /tmp/foo\n" |
| 43 | //usage: "$ ls -l /tmp/foo\n" |
| 44 | //usage: "-rwxrw-r-- 1 root root 0 Apr 12 18:25 /tmp/foo*\n" |
| 45 | //usage: "$ chmod 444 /tmp/foo\n" |
| 46 | //usage: "$ ls -l /tmp/foo\n" |
| 47 | //usage: "-r--r--r-- 1 root root 0 Apr 12 18:25 /tmp/foo\n" |
| 48 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 49 | #include "libbb.h" |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 50 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 51 | /* This is a NOEXEC applet. Be very careful! */ |
| 52 | |
| 53 | |
Denis Vlasenko | fefb279 | 2006-10-27 15:13:54 +0000 | [diff] [blame] | 54 | #define OPT_RECURSE (option_mask32 & 1) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 55 | #define OPT_VERBOSE (IF_DESKTOP(option_mask32 & 2) IF_NOT_DESKTOP(0)) |
| 56 | #define OPT_CHANGED (IF_DESKTOP(option_mask32 & 4) IF_NOT_DESKTOP(0)) |
| 57 | #define OPT_QUIET (IF_DESKTOP(option_mask32 & 8) IF_NOT_DESKTOP(0)) |
| 58 | #define OPT_STR "R" IF_DESKTOP("vcf") |
Denis Vlasenko | fefb279 | 2006-10-27 15:13:54 +0000 | [diff] [blame] | 59 | |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 60 | /* coreutils: |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 61 | * chmod never changes the permissions of symbolic links; the chmod |
| 62 | * system call cannot change their permissions. This is not a problem |
| 63 | * since the permissions of symbolic links are never used. |
| 64 | * However, for each symbolic link listed on the command line, chmod changes |
| 65 | * the permissions of the pointed-to file. In contrast, chmod ignores |
| 66 | * symbolic links encountered during recursive directory traversals. |
| 67 | */ |
| 68 | |
Denys Vlasenko | 689d065 | 2020-10-01 21:52:16 +0200 | [diff] [blame] | 69 | static int FAST_FUNC fileAction(struct recursive_state *state, |
| 70 | const char *fileName, |
| 71 | struct stat *statbuf) |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 72 | { |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 73 | mode_t newmode; |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 74 | |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 75 | /* match coreutils behavior */ |
Denys Vlasenko | 689d065 | 2020-10-01 21:52:16 +0200 | [diff] [blame] | 76 | if (state->depth == 0) { |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 77 | /* statbuf holds lstat result, but we need stat (follow link) */ |
| 78 | if (stat(fileName, statbuf)) |
| 79 | goto err; |
| 80 | } else { /* depth > 0: skip links */ |
| 81 | if (S_ISLNK(statbuf->st_mode)) |
| 82 | return TRUE; |
| 83 | } |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 84 | |
Denys Vlasenko | 689d065 | 2020-10-01 21:52:16 +0200 | [diff] [blame] | 85 | newmode = bb_parse_mode((char *)state->userData, statbuf->st_mode); |
Denys Vlasenko | 5711a2a | 2015-10-07 17:55:33 +0200 | [diff] [blame] | 86 | if (newmode == (mode_t)-1) |
Denys Vlasenko | 689d065 | 2020-10-01 21:52:16 +0200 | [diff] [blame] | 87 | bb_error_msg_and_die("invalid mode '%s'", (char *)state->userData); |
Denis Vlasenko | fefb279 | 2006-10-27 15:13:54 +0000 | [diff] [blame] | 88 | |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 89 | if (chmod(fileName, newmode) == 0) { |
| 90 | if (OPT_VERBOSE |
Denys Vlasenko | 2df4e0a | 2021-09-16 01:53:55 +0200 | [diff] [blame] | 91 | || (OPT_CHANGED |
| 92 | && (statbuf->st_mode & 07777) != (newmode & 07777)) |
Denis Vlasenko | fefb279 | 2006-10-27 15:13:54 +0000 | [diff] [blame] | 93 | ) { |
| 94 | printf("mode of '%s' changed to %04o (%s)\n", fileName, |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 95 | newmode & 07777, bb_mode_string(newmode)+1); |
Denis Vlasenko | fefb279 | 2006-10-27 15:13:54 +0000 | [diff] [blame] | 96 | } |
| 97 | return TRUE; |
| 98 | } |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 99 | err: |
| 100 | if (!OPT_QUIET) |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 101 | bb_simple_perror_msg(fileName); |
Denis Vlasenko | fefb279 | 2006-10-27 15:13:54 +0000 | [diff] [blame] | 102 | return FALSE; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 105 | int chmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 106 | int chmod_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 107 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 108 | int retval = EXIT_SUCCESS; |
Denis Vlasenko | fefb279 | 2006-10-27 15:13:54 +0000 | [diff] [blame] | 109 | char *arg, **argp; |
Eric Andersen | d274b53 | 2002-10-10 03:47:01 +0000 | [diff] [blame] | 110 | char *smode; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 111 | |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 112 | /* Convert first encountered -r into ar, -w into aw etc |
| 113 | * so that getopt would not eat it */ |
| 114 | argp = argv; |
| 115 | while ((arg = *++argp)) { |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 116 | /* Mode spec must be the first arg (sans -R etc) */ |
| 117 | /* (protect against mishandling e.g. "chmod 644 -r") */ |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 118 | if (arg[0] != '-') { |
| 119 | arg = NULL; |
Denis Vlasenko | fefb279 | 2006-10-27 15:13:54 +0000 | [diff] [blame] | 120 | break; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 121 | } |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 122 | /* An option. Not a -- or valid option? */ |
| 123 | if (arg[1] && !strchr("-"OPT_STR, arg[1])) { |
| 124 | arg[0] = 'a'; |
| 125 | break; |
| 126 | } |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Denis Vlasenko | 8c0c119 | 2006-10-28 12:42:40 +0000 | [diff] [blame] | 129 | /* Parse options */ |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 130 | getopt32(argv, "^" OPT_STR "\0" "-2"); |
Denis Vlasenko | fefb279 | 2006-10-27 15:13:54 +0000 | [diff] [blame] | 131 | argv += optind; |
Eric Andersen | d274b53 | 2002-10-10 03:47:01 +0000 | [diff] [blame] | 132 | |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 133 | /* Restore option-like mode if needed */ |
| 134 | if (arg) arg[0] = '-'; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 135 | |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 136 | /* Ok, ready to do the deed now */ |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 137 | smode = *argv++; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 138 | do { |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 139 | if (!recursive_action(*argv, |
Denis Vlasenko | 8c0c119 | 2006-10-28 12:42:40 +0000 | [diff] [blame] | 140 | OPT_RECURSE, // recurse |
Denis Vlasenko | 8c0c119 | 2006-10-28 12:42:40 +0000 | [diff] [blame] | 141 | fileAction, // file action |
| 142 | fileAction, // dir action |
Denys Vlasenko | 689d065 | 2020-10-01 21:52:16 +0200 | [diff] [blame] | 143 | smode) // user data |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 144 | ) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 145 | retval = EXIT_FAILURE; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 146 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 147 | } while (*++argv); |
| 148 | |
| 149 | return retval; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 150 | } |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 151 | |
| 152 | /* |
| 153 | Security: chmod is too important and too subtle. |
| 154 | This is a test script (busybox chmod versus coreutils). |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 155 | Run it in empty directory. |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 156 | |
| 157 | #!/bin/sh |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 158 | t1="/tmp/busybox chmod" |
| 159 | t2="/usr/bin/chmod" |
| 160 | create() { |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 161 | rm -rf $1; mkdir $1 |
| 162 | ( |
| 163 | cd $1 || exit 1 |
| 164 | mkdir dir |
| 165 | >up |
| 166 | >file |
| 167 | >dir/file |
| 168 | ln -s dir linkdir |
| 169 | ln -s file linkfile |
| 170 | ln -s ../up dir/up |
| 171 | ) |
| 172 | } |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 173 | tst() { |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 174 | (cd test1; $t1 $1) |
| 175 | (cd test2; $t2 $1) |
| 176 | (cd test1; ls -lR) >out1 |
| 177 | (cd test2; ls -lR) >out2 |
| 178 | echo "chmod $1" >out.diff |
| 179 | if ! diff -u out1 out2 >>out.diff; then exit 1; fi |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 180 | rm out.diff |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 181 | } |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 182 | echo "If script produced 'out.diff' file, then at least one testcase failed" |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 183 | create test1; create test2 |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 184 | tst "a+w file" |
| 185 | tst "a-w dir" |
| 186 | tst "a+w linkfile" |
| 187 | tst "a-w linkdir" |
| 188 | tst "-R a+w file" |
| 189 | tst "-R a-w dir" |
| 190 | tst "-R a+w linkfile" |
| 191 | tst "-R a-w linkdir" |
| 192 | tst "a-r,a+x linkfile" |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 193 | */ |