aboutsummaryrefslogtreecommitdiff
path: root/coreutils/chmod.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2006-10-27 23:42:25 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2006-10-27 23:42:25 +0000
commit8c35d65c43216bb840326ac7476a180e2ae36fe9 (patch)
tree13d20b31e817dcff5124498ca0bec2cdf9781014 /coreutils/chmod.c
parente80e2a3660bf09cc549cb2dfd2bdeb77ccde1231 (diff)
downloadbusybox-w32-8c35d65c43216bb840326ac7476a180e2ae36fe9.tar.gz
busybox-w32-8c35d65c43216bb840326ac7476a180e2ae36fe9.tar.bz2
busybox-w32-8c35d65c43216bb840326ac7476a180e2ae36fe9.zip
recursive_action: add depth param
chmod: match coreutils versus following links
Diffstat (limited to 'coreutils/chmod.c')
-rw-r--r--coreutils/chmod.c98
1 files changed, 76 insertions, 22 deletions
diff --git a/coreutils/chmod.c b/coreutils/chmod.c
index c4f8fa0b2..b601504f8 100644
--- a/coreutils/chmod.c
+++ b/coreutils/chmod.c
@@ -20,9 +20,9 @@
20#define OPT_VERBOSE (USE_DESKTOP(option_mask32 & 2) SKIP_DESKTOP(0)) 20#define OPT_VERBOSE (USE_DESKTOP(option_mask32 & 2) SKIP_DESKTOP(0))
21#define OPT_CHANGED (USE_DESKTOP(option_mask32 & 4) 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)) 22#define OPT_QUIET (USE_DESKTOP(option_mask32 & 8) SKIP_DESKTOP(0))
23#define OPT_STR ("-R" USE_DESKTOP("vcf")) 23#define OPT_STR "R" USE_DESKTOP("vcf")
24 24
25/* TODO: 25/* coreutils:
26 * chmod never changes the permissions of symbolic links; the chmod 26 * chmod never changes the permissions of symbolic links; the chmod
27 * system call cannot change their permissions. This is not a problem 27 * system call cannot change their permissions. This is not a problem
28 * since the permissions of symbolic links are never used. 28 * since the permissions of symbolic links are never used.
@@ -31,19 +31,26 @@
31 * symbolic links encountered during recursive directory traversals. 31 * symbolic links encountered during recursive directory traversals.
32 */ 32 */
33 33
34static int fileAction(const char *fileName, struct stat *statbuf, void* junk) 34static int fileAction(const char *fileName, struct stat *statbuf, void* junk, int depth)
35{ 35{
36 mode_t newmode = statbuf->st_mode; 36 mode_t newmode;
37 37
38 // TODO: match GNU behavior: 38 /* match coreutils behavior */
39 // if (depth > 0 && S_ISLNK(statbuf->st_mode)) return TRUE; 39 if (depth == 0) {
40 // if (depth == 0) follow link 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;
41 48
42 if (!bb_parse_mode((char *)junk, &newmode)) 49 if (!bb_parse_mode((char *)junk, &newmode))
43 bb_error_msg_and_die("invalid mode: %s", (char *)junk); 50 bb_error_msg_and_die("invalid mode: %s", (char *)junk);
44 51
45 if (chmod(fileName, statbuf->st_mode) == 0) { 52 if (chmod(fileName, newmode) == 0) {
46 if (OPT_VERBOSE /* -v verbose? or -c changed? */ 53 if (OPT_VERBOSE
47 || (OPT_CHANGED && statbuf->st_mode != newmode) 54 || (OPT_CHANGED && statbuf->st_mode != newmode)
48 ) { 55 ) {
49 printf("mode of '%s' changed to %04o (%s)\n", fileName, 56 printf("mode of '%s' changed to %04o (%s)\n", fileName,
@@ -51,7 +58,8 @@ static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
51 } 58 }
52 return TRUE; 59 return TRUE;
53 } 60 }
54 if (!OPT_QUIET) /* not silent (-f)? */ 61 err:
62 if (!OPT_QUIET)
55 bb_perror_msg("%s", fileName); 63 bb_perror_msg("%s", fileName);
56 return FALSE; 64 return FALSE;
57} 65}
@@ -62,30 +70,33 @@ int chmod_main(int argc, char **argv)
62 char *arg, **argp; 70 char *arg, **argp;
63 char *smode; 71 char *smode;
64 72
65 /* Convert first encountered -r into a-r, -w into a-w etc */ 73 /* Convert first encountered -r into ar, -w into aw etc
66 argp = argv + 1; 74 * so that getopt would not eat it */
67 while ((arg = *argp)) { 75 argp = argv;
76 while ((arg = *++argp)) {
68 /* Mode spec must be the first arg (sans -R etc) */ 77 /* Mode spec must be the first arg (sans -R etc) */
69 /* (protect against mishandling e.g. "chmod 644 -r") */ 78 /* (protect against mishandling e.g. "chmod 644 -r") */
70 if (arg[0] != '-') 79 if (arg[0] != '-') {
80 arg = NULL;
71 break; 81 break;
82 }
72 /* An option. Not a -- or valid option? */ 83 /* An option. Not a -- or valid option? */
73 if (arg[1] && !strchr(OPT_STR, arg[1])) { 84 if (arg[1] && !strchr("-"OPT_STR, arg[1])) {
74 argp[0] = xasprintf("a%s", arg); 85 arg[0] = 'a';
75 break; 86 break;
76 } 87 }
77 argp++;
78 } 88 }
79 /* "chmod -rzzz abc" will say "invalid mode: a-rzzz"!
80 * It is easily fixable, but deemed not worth the code */
81 89
90 /* Paerse options */
82 opt_complementary = "-2"; 91 opt_complementary = "-2";
83 getopt32(argc, argv, OPT_STR + 1); /* Reuse string */ 92 getopt32(argc, argv, ("-"OPT_STR) + 1); /* Reuse string */
84 argv += optind; 93 argv += optind;
85 94
86 smode = *argv++; 95 /* Restore option-like mode if needed */
96 if (arg) arg[0] = '-';
87 97
88 /* Ok, ready to do the deed now */ 98 /* Ok, ready to do the deed now */
99 smode = *argv++;
89 do { 100 do {
90 if (!recursive_action(*argv, 101 if (!recursive_action(*argv,
91 OPT_RECURSE, // recurse 102 OPT_RECURSE, // recurse
@@ -93,7 +104,8 @@ int chmod_main(int argc, char **argv)
93 FALSE, // depth first 104 FALSE, // depth first
94 fileAction, // file action 105 fileAction, // file action
95 fileAction, // dir action 106 fileAction, // dir action
96 smode) // user data 107 smode, // user data
108 0) // depth
97 ) { 109 ) {
98 retval = EXIT_FAILURE; 110 retval = EXIT_FAILURE;
99 } 111 }
@@ -101,3 +113,45 @@ int chmod_main(int argc, char **argv)
101 113
102 return retval; 114 return retval;
103} 115}
116
117/*
118Security: chmod is too important and too subtle.
119This is a test script (busybox chmod versus coreutils).
120Run it in empty dir. Probably requires bash.
121
122#!/bin/sh
123function create() {
124 rm -rf $1; mkdir $1
125 (
126 cd $1 || exit 1
127 mkdir dir
128 >up
129 >file
130 >dir/file
131 ln -s dir linkdir
132 ln -s file linkfile
133 ln -s ../up dir/up
134 )
135}
136function test() {
137 (cd test1; $t1 $1)
138 (cd test2; $t2 $1)
139 (cd test1; ls -lR) >out1
140 (cd test2; ls -lR) >out2
141 echo "chmod $1" >out.diff
142 if ! diff -u out1 out2 >>out.diff; then exit 1; fi
143 mv out.diff out1.diff
144}
145t1="/tmp/busybox chmod"
146t2="/usr/bin/chmod"
147create test1; create test2
148test "a+w file"
149test "a-w dir"
150test "a+w linkfile"
151test "a-w linkdir"
152test "-R a+w file"
153test "-R a-w dir"
154test "-R a+w linkfile"
155test "-R a-w linkdir"
156test "a-r,a+x linkfile"
157*/