aboutsummaryrefslogtreecommitdiff
path: root/coreutils
diff options
context:
space:
mode:
Diffstat (limited to 'coreutils')
-rw-r--r--coreutils/chmod.c98
-rw-r--r--coreutils/chown.c8
-rw-r--r--coreutils/diff.c6
3 files changed, 84 insertions, 28 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*/
diff --git a/coreutils/chown.c b/coreutils/chown.c
index bef89ce86..fddce7cf1 100644
--- a/coreutils/chown.c
+++ b/coreutils/chown.c
@@ -32,7 +32,7 @@ static int (*chown_func)(const char *, uid_t, gid_t) = chown;
32 */ 32 */
33 33
34static int fileAction(const char *fileName, struct stat *statbuf, 34static int fileAction(const char *fileName, struct stat *statbuf,
35 void ATTRIBUTE_UNUSED *junk) 35 void ATTRIBUTE_UNUSED *junk, int depth)
36{ 36{
37 // TODO: -H/-L/-P 37 // TODO: -H/-L/-P
38 // if (depth ... && S_ISLNK(statbuf->st_mode)) .... 38 // if (depth ... && S_ISLNK(statbuf->st_mode)) ....
@@ -75,7 +75,8 @@ int chown_main(int argc, char **argv)
75 *groupName++ = '\0'; 75 *groupName++ = '\0';
76 gid = get_ug_id(groupName, bb_xgetgrnam); 76 gid = get_ug_id(groupName, bb_xgetgrnam);
77 } 77 }
78 if (--groupName != *argv) uid = get_ug_id(*argv, bb_xgetpwnam); 78 if (--groupName != *argv)
79 uid = get_ug_id(*argv, bb_xgetpwnam);
79 ++argv; 80 ++argv;
80 81
81 /* Ok, ready to do the deed now */ 82 /* Ok, ready to do the deed now */
@@ -86,7 +87,8 @@ int chown_main(int argc, char **argv)
86 FALSE, // depth first 87 FALSE, // depth first
87 fileAction, // file action 88 fileAction, // file action
88 fileAction, // dir action 89 fileAction, // dir action
89 NULL) // user data 90 NULL, // user data
91 0) // depth
90 ) { 92 ) {
91 retval = EXIT_FAILURE; 93 retval = EXIT_FAILURE;
92 } 94 }
diff --git a/coreutils/diff.c b/coreutils/diff.c
index 2915d4009..f26bcca86 100644
--- a/coreutils/diff.c
+++ b/coreutils/diff.c
@@ -1030,7 +1030,7 @@ static int dir_strcmp(const void *p1, const void *p2)
1030/* This function adds a filename to dl, the directory listing. */ 1030/* This function adds a filename to dl, the directory listing. */
1031 1031
1032static int add_to_dirlist(const char *filename, 1032static int add_to_dirlist(const char *filename,
1033 struct stat ATTRIBUTE_UNUSED * sb, void *userdata) 1033 struct stat ATTRIBUTE_UNUSED * sb, void *userdata, int depth)
1034{ 1034{
1035 dl_count++; 1035 dl_count++;
1036 dl = xrealloc(dl, dl_count * sizeof(char *)); 1036 dl = xrealloc(dl, dl_count * sizeof(char *));
@@ -1067,7 +1067,7 @@ static char **get_dir(char *path)
1067 /* Now fill dl with a listing. */ 1067 /* Now fill dl with a listing. */
1068 if (cmd_flags & FLAG_r) 1068 if (cmd_flags & FLAG_r)
1069 recursive_action(path, TRUE, TRUE, FALSE, add_to_dirlist, NULL, 1069 recursive_action(path, TRUE, TRUE, FALSE, add_to_dirlist, NULL,
1070 userdata); 1070 userdata, 0);
1071 else { 1071 else {
1072 DIR *dp; 1072 DIR *dp;
1073 struct dirent *ep; 1073 struct dirent *ep;
@@ -1076,7 +1076,7 @@ static char **get_dir(char *path)
1076 while ((ep = readdir(dp))) { 1076 while ((ep = readdir(dp))) {
1077 if ((!strcmp(ep->d_name, "..")) || (!strcmp(ep->d_name, "."))) 1077 if ((!strcmp(ep->d_name, "..")) || (!strcmp(ep->d_name, ".")))
1078 continue; 1078 continue;
1079 add_to_dirlist(ep->d_name, NULL, NULL); 1079 add_to_dirlist(ep->d_name, NULL, NULL, 0);
1080 } 1080 }
1081 closedir(dp); 1081 closedir(dp);
1082 } 1082 }