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