blob: 11c6731a190fce41375f308039813e4e423ba350 [file] [log] [blame]
Eric Andersen9f0fedb2001-04-24 18:07:19 +00001/* vi: set sw=4 ts=4: */
2/*
Eric Andersenbdfd0d72001-10-24 05:00:29 +00003 * Mini chmod implementation for busybox
Eric Andersen9f0fedb2001-04-24 18:07:19 +00004 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersen9f0fedb2001-04-24 18:07:19 +00006 *
Eric Andersend274b532002-10-10 03:47:01 +00007 * Reworked by (C) 2002 Vladimir Oleynik <dzo@simtreas.ru>
8 * to correctly parse '-rwxgoa'
9 *
Bernhard Reutner-Fischer20f40002006-01-30 17:17:14 +000010 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersen9f0fedb2001-04-24 18:07:19 +000011 */
12
Manuel Novoa III cad53642003-03-19 09:13:01 +000013/* BB_AUDIT SUSv3 compliant */
Denis Vlasenkofefb2792006-10-27 15:13:54 +000014/* BB_AUDIT GNU defects - unsupported long options. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000015/* http://www.opengroup.org/onlinepubs/007904975/utilities/chmod.html */
16
Eric Andersen9f0fedb2001-04-24 18:07:19 +000017#include "busybox.h"
Eric Andersen9f0fedb2001-04-24 18:07:19 +000018
Denis Vlasenkofefb2792006-10-27 15:13:54 +000019#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 Vlasenko8c35d652006-10-27 23:42:25 +000023#define OPT_STR "R" USE_DESKTOP("vcf")
Denis Vlasenkofefb2792006-10-27 15:13:54 +000024
Denis Vlasenko8c35d652006-10-27 23:42:25 +000025/* coreutils:
Denis Vlasenko51b4c922006-10-27 16:07:20 +000026 * 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 Vlasenkofc7f9222007-01-26 23:00:05 +000034static int fileAction(const char *fileName, struct stat *statbuf, void* param, int depth)
Eric Andersen9f0fedb2001-04-24 18:07:19 +000035{
Denis Vlasenko8c35d652006-10-27 23:42:25 +000036 mode_t newmode;
Denis Vlasenko51b4c922006-10-27 16:07:20 +000037
Denis Vlasenko8c35d652006-10-27 23:42:25 +000038 /* 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 Vlasenko51b4c922006-10-27 16:07:20 +000048
Denis Vlasenkofc7f9222007-01-26 23:00:05 +000049 if (!bb_parse_mode((char *)param, &newmode))
50 bb_error_msg_and_die("invalid mode: %s", (char *)param);
Denis Vlasenkofefb2792006-10-27 15:13:54 +000051
Denis Vlasenko8c35d652006-10-27 23:42:25 +000052 if (chmod(fileName, newmode) == 0) {
53 if (OPT_VERBOSE
Denis Vlasenkofefb2792006-10-27 15:13:54 +000054 || (OPT_CHANGED && statbuf->st_mode != newmode)
55 ) {
56 printf("mode of '%s' changed to %04o (%s)\n", fileName,
Denis Vlasenko51b4c922006-10-27 16:07:20 +000057 newmode & 07777, bb_mode_string(newmode)+1);
Denis Vlasenkofefb2792006-10-27 15:13:54 +000058 }
59 return TRUE;
60 }
Denis Vlasenko8c35d652006-10-27 23:42:25 +000061 err:
62 if (!OPT_QUIET)
Denis Vlasenkofefb2792006-10-27 15:13:54 +000063 bb_perror_msg("%s", fileName);
64 return FALSE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000065}
66
Denis Vlasenko06af2162007-02-03 17:28:39 +000067int chmod_main(int argc, char **argv);
Denis Vlasenkofefb2792006-10-27 15:13:54 +000068int chmod_main(int argc, char **argv)
Eric Andersen9f0fedb2001-04-24 18:07:19 +000069{
Manuel Novoa III cad53642003-03-19 09:13:01 +000070 int retval = EXIT_SUCCESS;
Denis Vlasenkofefb2792006-10-27 15:13:54 +000071 char *arg, **argp;
Eric Andersend274b532002-10-10 03:47:01 +000072 char *smode;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000073
Denis Vlasenko8c35d652006-10-27 23:42:25 +000074 /* 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 Vlasenko51b4c922006-10-27 16:07:20 +000078 /* Mode spec must be the first arg (sans -R etc) */
79 /* (protect against mishandling e.g. "chmod 644 -r") */
Denis Vlasenko8c35d652006-10-27 23:42:25 +000080 if (arg[0] != '-') {
81 arg = NULL;
Denis Vlasenkofefb2792006-10-27 15:13:54 +000082 break;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000083 }
Denis Vlasenko8c35d652006-10-27 23:42:25 +000084 /* An option. Not a -- or valid option? */
85 if (arg[1] && !strchr("-"OPT_STR, arg[1])) {
86 arg[0] = 'a';
87 break;
88 }
Eric Andersen9f0fedb2001-04-24 18:07:19 +000089 }
90
Denis Vlasenko8c0c1192006-10-28 12:42:40 +000091 /* Parse options */
Denis Vlasenkofefb2792006-10-27 15:13:54 +000092 opt_complementary = "-2";
Denis Vlasenko8c35d652006-10-27 23:42:25 +000093 getopt32(argc, argv, ("-"OPT_STR) + 1); /* Reuse string */
Denis Vlasenkofefb2792006-10-27 15:13:54 +000094 argv += optind;
Eric Andersend274b532002-10-10 03:47:01 +000095
Denis Vlasenko8c35d652006-10-27 23:42:25 +000096 /* Restore option-like mode if needed */
97 if (arg) arg[0] = '-';
Manuel Novoa III cad53642003-03-19 09:13:01 +000098
Eric Andersen9f0fedb2001-04-24 18:07:19 +000099 /* Ok, ready to do the deed now */
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000100 smode = *argv++;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000101 do {
Denis Vlasenko51b4c922006-10-27 16:07:20 +0000102 if (!recursive_action(*argv,
Denis Vlasenko8c0c1192006-10-28 12:42:40 +0000103 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 Vlasenko51b4c922006-10-27 16:07:20 +0000110 ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000111 retval = EXIT_FAILURE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000112 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000113 } while (*++argv);
114
115 return retval;
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000116}
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000117
118/*
119Security: chmod is too important and too subtle.
120This is a test script (busybox chmod versus coreutils).
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000121Run it in empty directory.
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000122
123#!/bin/sh
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000124t1="/tmp/busybox chmod"
125t2="/usr/bin/chmod"
126create() {
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000127 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 Vlasenkocd27c422007-03-08 13:37:43 +0000139tst() {
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000140 (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 Vlasenkocd27c422007-03-08 13:37:43 +0000146 rm out.diff
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000147}
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000148echo "If script produced 'out.diff' file, then at least one testcase failed"
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000149create test1; create test2
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000150tst "a+w file"
151tst "a-w dir"
152tst "a+w linkfile"
153tst "a-w linkdir"
154tst "-R a+w file"
155tst "-R a-w dir"
156tst "-R a+w linkfile"
157tst "-R a-w linkdir"
158tst "a-r,a+x linkfile"
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000159*/