blob: c04201eecfae0f7d1b503591bac94bc250a679e2 [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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000017#include "libbb.h"
Eric Andersen9f0fedb2001-04-24 18:07:19 +000018
Denis Vlasenko99912ca2007-04-10 15:43:37 +000019/* This is a NOEXEC applet. Be very careful! */
20
21
Denis Vlasenkofefb2792006-10-27 15:13:54 +000022#define OPT_RECURSE (option_mask32 & 1)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000023#define OPT_VERBOSE (IF_DESKTOP(option_mask32 & 2) IF_NOT_DESKTOP(0))
24#define OPT_CHANGED (IF_DESKTOP(option_mask32 & 4) IF_NOT_DESKTOP(0))
25#define OPT_QUIET (IF_DESKTOP(option_mask32 & 8) IF_NOT_DESKTOP(0))
26#define OPT_STR "R" IF_DESKTOP("vcf")
Denis Vlasenkofefb2792006-10-27 15:13:54 +000027
Denis Vlasenko8c35d652006-10-27 23:42:25 +000028/* coreutils:
Denis Vlasenko51b4c922006-10-27 16:07:20 +000029 * chmod never changes the permissions of symbolic links; the chmod
30 * system call cannot change their permissions. This is not a problem
31 * since the permissions of symbolic links are never used.
32 * However, for each symbolic link listed on the command line, chmod changes
33 * the permissions of the pointed-to file. In contrast, chmod ignores
34 * symbolic links encountered during recursive directory traversals.
35 */
36
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000037static int FAST_FUNC fileAction(const char *fileName, struct stat *statbuf, void* param, int depth)
Eric Andersen9f0fedb2001-04-24 18:07:19 +000038{
Denis Vlasenko8c35d652006-10-27 23:42:25 +000039 mode_t newmode;
Denis Vlasenko51b4c922006-10-27 16:07:20 +000040
Denis Vlasenko8c35d652006-10-27 23:42:25 +000041 /* match coreutils behavior */
42 if (depth == 0) {
43 /* statbuf holds lstat result, but we need stat (follow link) */
44 if (stat(fileName, statbuf))
45 goto err;
46 } else { /* depth > 0: skip links */
47 if (S_ISLNK(statbuf->st_mode))
48 return TRUE;
49 }
50 newmode = statbuf->st_mode;
Denis Vlasenko51b4c922006-10-27 16:07:20 +000051
Denis Vlasenkofc7f9222007-01-26 23:00:05 +000052 if (!bb_parse_mode((char *)param, &newmode))
Denys Vlasenko651a2692010-03-23 16:25:17 +010053 bb_error_msg_and_die("invalid mode '%s'", (char *)param);
Denis Vlasenkofefb2792006-10-27 15:13:54 +000054
Denis Vlasenko8c35d652006-10-27 23:42:25 +000055 if (chmod(fileName, newmode) == 0) {
56 if (OPT_VERBOSE
Denis Vlasenkofefb2792006-10-27 15:13:54 +000057 || (OPT_CHANGED && statbuf->st_mode != newmode)
58 ) {
59 printf("mode of '%s' changed to %04o (%s)\n", fileName,
Denis Vlasenko51b4c922006-10-27 16:07:20 +000060 newmode & 07777, bb_mode_string(newmode)+1);
Denis Vlasenkofefb2792006-10-27 15:13:54 +000061 }
62 return TRUE;
63 }
Denis Vlasenko8c35d652006-10-27 23:42:25 +000064 err:
65 if (!OPT_QUIET)
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +000066 bb_simple_perror_msg(fileName);
Denis Vlasenkofefb2792006-10-27 15:13:54 +000067 return FALSE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000068}
69
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000070int chmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000071int chmod_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen9f0fedb2001-04-24 18:07:19 +000072{
Manuel Novoa III cad53642003-03-19 09:13:01 +000073 int retval = EXIT_SUCCESS;
Denis Vlasenkofefb2792006-10-27 15:13:54 +000074 char *arg, **argp;
Eric Andersend274b532002-10-10 03:47:01 +000075 char *smode;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000076
Denis Vlasenko8c35d652006-10-27 23:42:25 +000077 /* Convert first encountered -r into ar, -w into aw etc
78 * so that getopt would not eat it */
79 argp = argv;
80 while ((arg = *++argp)) {
Denis Vlasenko51b4c922006-10-27 16:07:20 +000081 /* Mode spec must be the first arg (sans -R etc) */
82 /* (protect against mishandling e.g. "chmod 644 -r") */
Denis Vlasenko8c35d652006-10-27 23:42:25 +000083 if (arg[0] != '-') {
84 arg = NULL;
Denis Vlasenkofefb2792006-10-27 15:13:54 +000085 break;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000086 }
Denis Vlasenko8c35d652006-10-27 23:42:25 +000087 /* An option. Not a -- or valid option? */
88 if (arg[1] && !strchr("-"OPT_STR, arg[1])) {
89 arg[0] = 'a';
90 break;
91 }
Eric Andersen9f0fedb2001-04-24 18:07:19 +000092 }
93
Denis Vlasenko8c0c1192006-10-28 12:42:40 +000094 /* Parse options */
Denis Vlasenkofefb2792006-10-27 15:13:54 +000095 opt_complementary = "-2";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000096 getopt32(argv, ("-"OPT_STR) + 1); /* Reuse string */
Denis Vlasenkofefb2792006-10-27 15:13:54 +000097 argv += optind;
Eric Andersend274b532002-10-10 03:47:01 +000098
Denis Vlasenko8c35d652006-10-27 23:42:25 +000099 /* Restore option-like mode if needed */
100 if (arg) arg[0] = '-';
Manuel Novoa III cad53642003-03-19 09:13:01 +0000101
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000102 /* Ok, ready to do the deed now */
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000103 smode = *argv++;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000104 do {
Denis Vlasenko51b4c922006-10-27 16:07:20 +0000105 if (!recursive_action(*argv,
Denis Vlasenko8c0c1192006-10-28 12:42:40 +0000106 OPT_RECURSE, // recurse
Denis Vlasenko8c0c1192006-10-28 12:42:40 +0000107 fileAction, // file action
108 fileAction, // dir action
109 smode, // user data
110 0) // depth
Denis Vlasenko51b4c922006-10-27 16:07:20 +0000111 ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000112 retval = EXIT_FAILURE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000113 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000114 } while (*++argv);
115
116 return retval;
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000117}
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000118
119/*
120Security: chmod is too important and too subtle.
121This is a test script (busybox chmod versus coreutils).
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000122Run it in empty directory.
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000123
124#!/bin/sh
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000125t1="/tmp/busybox chmod"
126t2="/usr/bin/chmod"
127create() {
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000128 rm -rf $1; mkdir $1
129 (
130 cd $1 || exit 1
131 mkdir dir
132 >up
133 >file
134 >dir/file
135 ln -s dir linkdir
136 ln -s file linkfile
137 ln -s ../up dir/up
138 )
139}
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000140tst() {
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000141 (cd test1; $t1 $1)
142 (cd test2; $t2 $1)
143 (cd test1; ls -lR) >out1
144 (cd test2; ls -lR) >out2
145 echo "chmod $1" >out.diff
146 if ! diff -u out1 out2 >>out.diff; then exit 1; fi
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000147 rm out.diff
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000148}
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000149echo "If script produced 'out.diff' file, then at least one testcase failed"
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000150create test1; create test2
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000151tst "a+w file"
152tst "a-w dir"
153tst "a+w linkfile"
154tst "a-w linkdir"
155tst "-R a+w file"
156tst "-R a-w dir"
157tst "-R a+w linkfile"
158tst "-R a-w linkdir"
159tst "a-r,a+x linkfile"
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000160*/