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 | * |
Bernhard Reutner-Fischer | 20f4000 | 2006-01-30 17:17:14 +0000 | [diff] [blame] | 10 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
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 | |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 17 | #include "busybox.h" |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 18 | |
Denis Vlasenko | fefb279 | 2006-10-27 15:13:54 +0000 | [diff] [blame] | 19 | #define OPT_RECURSE (option_mask32 & 1) |
| 20 | #define OPT_VERBOSE (USE_DESKTOP(option_mask32 & 2) SKIP_DESKTOP(0)) |
| 21 | #define OPT_CHANGED (USE_DESKTOP(option_mask32 & 4) SKIP_DESKTOP(0)) |
| 22 | #define OPT_QUIET (USE_DESKTOP(option_mask32 & 8) SKIP_DESKTOP(0)) |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 23 | #define OPT_STR "R" USE_DESKTOP("vcf") |
Denis Vlasenko | fefb279 | 2006-10-27 15:13:54 +0000 | [diff] [blame] | 24 | |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 25 | /* coreutils: |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 26 | * chmod never changes the permissions of symbolic links; the chmod |
| 27 | * system call cannot change their permissions. This is not a problem |
| 28 | * since the permissions of symbolic links are never used. |
| 29 | * However, for each symbolic link listed on the command line, chmod changes |
| 30 | * the permissions of the pointed-to file. In contrast, chmod ignores |
| 31 | * symbolic links encountered during recursive directory traversals. |
| 32 | */ |
| 33 | |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 34 | static int fileAction(const char *fileName, struct stat *statbuf, void* param, int depth) |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 35 | { |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 36 | mode_t newmode; |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 37 | |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 38 | /* match coreutils behavior */ |
| 39 | if (depth == 0) { |
| 40 | /* statbuf holds lstat result, but we need stat (follow link) */ |
| 41 | if (stat(fileName, statbuf)) |
| 42 | goto err; |
| 43 | } else { /* depth > 0: skip links */ |
| 44 | if (S_ISLNK(statbuf->st_mode)) |
| 45 | return TRUE; |
| 46 | } |
| 47 | newmode = statbuf->st_mode; |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 48 | |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 49 | if (!bb_parse_mode((char *)param, &newmode)) |
| 50 | bb_error_msg_and_die("invalid mode: %s", (char *)param); |
Denis Vlasenko | fefb279 | 2006-10-27 15:13:54 +0000 | [diff] [blame] | 51 | |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 52 | if (chmod(fileName, newmode) == 0) { |
| 53 | if (OPT_VERBOSE |
Denis Vlasenko | fefb279 | 2006-10-27 15:13:54 +0000 | [diff] [blame] | 54 | || (OPT_CHANGED && statbuf->st_mode != newmode) |
| 55 | ) { |
| 56 | printf("mode of '%s' changed to %04o (%s)\n", fileName, |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 57 | newmode & 07777, bb_mode_string(newmode)+1); |
Denis Vlasenko | fefb279 | 2006-10-27 15:13:54 +0000 | [diff] [blame] | 58 | } |
| 59 | return TRUE; |
| 60 | } |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 61 | err: |
| 62 | if (!OPT_QUIET) |
Denis Vlasenko | fefb279 | 2006-10-27 15:13:54 +0000 | [diff] [blame] | 63 | bb_perror_msg("%s", fileName); |
| 64 | return FALSE; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 67 | int chmod_main(int argc, char **argv); |
Denis Vlasenko | fefb279 | 2006-10-27 15:13:54 +0000 | [diff] [blame] | 68 | int chmod_main(int argc, char **argv) |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 69 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 70 | int retval = EXIT_SUCCESS; |
Denis Vlasenko | fefb279 | 2006-10-27 15:13:54 +0000 | [diff] [blame] | 71 | char *arg, **argp; |
Eric Andersen | d274b53 | 2002-10-10 03:47:01 +0000 | [diff] [blame] | 72 | char *smode; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 73 | |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 74 | /* Convert first encountered -r into ar, -w into aw etc |
| 75 | * so that getopt would not eat it */ |
| 76 | argp = argv; |
| 77 | while ((arg = *++argp)) { |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 78 | /* Mode spec must be the first arg (sans -R etc) */ |
| 79 | /* (protect against mishandling e.g. "chmod 644 -r") */ |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 80 | if (arg[0] != '-') { |
| 81 | arg = NULL; |
Denis Vlasenko | fefb279 | 2006-10-27 15:13:54 +0000 | [diff] [blame] | 82 | break; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 83 | } |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 84 | /* An option. Not a -- or valid option? */ |
| 85 | if (arg[1] && !strchr("-"OPT_STR, arg[1])) { |
| 86 | arg[0] = 'a'; |
| 87 | break; |
| 88 | } |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Denis Vlasenko | 8c0c119 | 2006-10-28 12:42:40 +0000 | [diff] [blame] | 91 | /* Parse options */ |
Denis Vlasenko | fefb279 | 2006-10-27 15:13:54 +0000 | [diff] [blame] | 92 | opt_complementary = "-2"; |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 93 | getopt32(argc, argv, ("-"OPT_STR) + 1); /* Reuse string */ |
Denis Vlasenko | fefb279 | 2006-10-27 15:13:54 +0000 | [diff] [blame] | 94 | argv += optind; |
Eric Andersen | d274b53 | 2002-10-10 03:47:01 +0000 | [diff] [blame] | 95 | |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 96 | /* Restore option-like mode if needed */ |
| 97 | if (arg) arg[0] = '-'; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 98 | |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 99 | /* Ok, ready to do the deed now */ |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 100 | smode = *argv++; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 101 | do { |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 102 | if (!recursive_action(*argv, |
Denis Vlasenko | 8c0c119 | 2006-10-28 12:42:40 +0000 | [diff] [blame] | 103 | OPT_RECURSE, // recurse |
| 104 | FALSE, // follow links: coreutils doesn't |
| 105 | FALSE, // depth first |
| 106 | fileAction, // file action |
| 107 | fileAction, // dir action |
| 108 | smode, // user data |
| 109 | 0) // depth |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 110 | ) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 111 | retval = EXIT_FAILURE; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 112 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 113 | } while (*++argv); |
| 114 | |
| 115 | return retval; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 116 | } |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 117 | |
| 118 | /* |
| 119 | Security: chmod is too important and too subtle. |
| 120 | This is a test script (busybox chmod versus coreutils). |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 121 | Run it in empty directory. |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 122 | |
| 123 | #!/bin/sh |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 124 | t1="/tmp/busybox chmod" |
| 125 | t2="/usr/bin/chmod" |
| 126 | create() { |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 127 | rm -rf $1; mkdir $1 |
| 128 | ( |
| 129 | cd $1 || exit 1 |
| 130 | mkdir dir |
| 131 | >up |
| 132 | >file |
| 133 | >dir/file |
| 134 | ln -s dir linkdir |
| 135 | ln -s file linkfile |
| 136 | ln -s ../up dir/up |
| 137 | ) |
| 138 | } |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 139 | tst() { |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 140 | (cd test1; $t1 $1) |
| 141 | (cd test2; $t2 $1) |
| 142 | (cd test1; ls -lR) >out1 |
| 143 | (cd test2; ls -lR) >out2 |
| 144 | echo "chmod $1" >out.diff |
| 145 | if ! diff -u out1 out2 >>out.diff; then exit 1; fi |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 146 | rm out.diff |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 147 | } |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 148 | 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] | 149 | create test1; create test2 |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 150 | tst "a+w file" |
| 151 | tst "a-w dir" |
| 152 | tst "a+w linkfile" |
| 153 | tst "a-w linkdir" |
| 154 | tst "-R a+w file" |
| 155 | tst "-R a-w dir" |
| 156 | tst "-R a+w linkfile" |
| 157 | tst "-R a-w linkdir" |
| 158 | tst "a-r,a+x linkfile" |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 159 | */ |