blob: a21c6d501871e9d1ddfb44a7580f38fc3f6d6d45 [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020010 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
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
Pere Orga34425382011-03-31 14:43:25 +020017//usage:#define chmod_trivial_usage
18//usage: "[-R"IF_DESKTOP("cvf")"] MODE[,MODE]... FILE..."
19//usage:#define chmod_full_usage "\n\n"
20//usage: "Each MODE is one or more of the letters ugoa, one of the\n"
21//usage: "symbols +-= and one or more of the letters rwxst\n"
Pere Orga34425382011-03-31 14:43:25 +020022//usage: "\n -R Recurse"
23//usage: IF_DESKTOP(
24//usage: "\n -c List changed files"
25//usage: "\n -v List all files"
26//usage: "\n -f Hide errors"
27//usage: )
28//usage:
29//usage:#define chmod_example_usage
30//usage: "$ ls -l /tmp/foo\n"
31//usage: "-rw-rw-r-- 1 root root 0 Apr 12 18:25 /tmp/foo\n"
32//usage: "$ chmod u+x /tmp/foo\n"
33//usage: "$ ls -l /tmp/foo\n"
34//usage: "-rwxrw-r-- 1 root root 0 Apr 12 18:25 /tmp/foo*\n"
35//usage: "$ chmod 444 /tmp/foo\n"
36//usage: "$ ls -l /tmp/foo\n"
37//usage: "-r--r--r-- 1 root root 0 Apr 12 18:25 /tmp/foo\n"
38
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000039#include "libbb.h"
Eric Andersen9f0fedb2001-04-24 18:07:19 +000040
Denis Vlasenko99912ca2007-04-10 15:43:37 +000041/* This is a NOEXEC applet. Be very careful! */
42
43
Denis Vlasenkofefb2792006-10-27 15:13:54 +000044#define OPT_RECURSE (option_mask32 & 1)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000045#define OPT_VERBOSE (IF_DESKTOP(option_mask32 & 2) IF_NOT_DESKTOP(0))
46#define OPT_CHANGED (IF_DESKTOP(option_mask32 & 4) IF_NOT_DESKTOP(0))
47#define OPT_QUIET (IF_DESKTOP(option_mask32 & 8) IF_NOT_DESKTOP(0))
48#define OPT_STR "R" IF_DESKTOP("vcf")
Denis Vlasenkofefb2792006-10-27 15:13:54 +000049
Denis Vlasenko8c35d652006-10-27 23:42:25 +000050/* coreutils:
Denis Vlasenko51b4c922006-10-27 16:07:20 +000051 * chmod never changes the permissions of symbolic links; the chmod
52 * system call cannot change their permissions. This is not a problem
53 * since the permissions of symbolic links are never used.
54 * However, for each symbolic link listed on the command line, chmod changes
55 * the permissions of the pointed-to file. In contrast, chmod ignores
56 * symbolic links encountered during recursive directory traversals.
57 */
58
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000059static int FAST_FUNC fileAction(const char *fileName, struct stat *statbuf, void* param, int depth)
Eric Andersen9f0fedb2001-04-24 18:07:19 +000060{
Denis Vlasenko8c35d652006-10-27 23:42:25 +000061 mode_t newmode;
Denis Vlasenko51b4c922006-10-27 16:07:20 +000062
Denis Vlasenko8c35d652006-10-27 23:42:25 +000063 /* match coreutils behavior */
64 if (depth == 0) {
65 /* statbuf holds lstat result, but we need stat (follow link) */
66 if (stat(fileName, statbuf))
67 goto err;
68 } else { /* depth > 0: skip links */
69 if (S_ISLNK(statbuf->st_mode))
70 return TRUE;
71 }
Denis Vlasenko51b4c922006-10-27 16:07:20 +000072
Denys Vlasenko5711a2a2015-10-07 17:55:33 +020073 newmode = bb_parse_mode((char *)param, statbuf->st_mode);
74 if (newmode == (mode_t)-1)
Denys Vlasenko651a2692010-03-23 16:25:17 +010075 bb_error_msg_and_die("invalid mode '%s'", (char *)param);
Denis Vlasenkofefb2792006-10-27 15:13:54 +000076
Denis Vlasenko8c35d652006-10-27 23:42:25 +000077 if (chmod(fileName, newmode) == 0) {
78 if (OPT_VERBOSE
Denis Vlasenkofefb2792006-10-27 15:13:54 +000079 || (OPT_CHANGED && statbuf->st_mode != newmode)
80 ) {
81 printf("mode of '%s' changed to %04o (%s)\n", fileName,
Denis Vlasenko51b4c922006-10-27 16:07:20 +000082 newmode & 07777, bb_mode_string(newmode)+1);
Denis Vlasenkofefb2792006-10-27 15:13:54 +000083 }
84 return TRUE;
85 }
Denis Vlasenko8c35d652006-10-27 23:42:25 +000086 err:
87 if (!OPT_QUIET)
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +000088 bb_simple_perror_msg(fileName);
Denis Vlasenkofefb2792006-10-27 15:13:54 +000089 return FALSE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000090}
91
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000092int chmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000093int chmod_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen9f0fedb2001-04-24 18:07:19 +000094{
Manuel Novoa III cad53642003-03-19 09:13:01 +000095 int retval = EXIT_SUCCESS;
Denis Vlasenkofefb2792006-10-27 15:13:54 +000096 char *arg, **argp;
Eric Andersend274b532002-10-10 03:47:01 +000097 char *smode;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000098
Denis Vlasenko8c35d652006-10-27 23:42:25 +000099 /* Convert first encountered -r into ar, -w into aw etc
100 * so that getopt would not eat it */
101 argp = argv;
102 while ((arg = *++argp)) {
Denis Vlasenko51b4c922006-10-27 16:07:20 +0000103 /* Mode spec must be the first arg (sans -R etc) */
104 /* (protect against mishandling e.g. "chmod 644 -r") */
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000105 if (arg[0] != '-') {
106 arg = NULL;
Denis Vlasenkofefb2792006-10-27 15:13:54 +0000107 break;
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000108 }
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000109 /* An option. Not a -- or valid option? */
110 if (arg[1] && !strchr("-"OPT_STR, arg[1])) {
111 arg[0] = 'a';
112 break;
113 }
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000114 }
115
Denis Vlasenko8c0c1192006-10-28 12:42:40 +0000116 /* Parse options */
Denis Vlasenkofefb2792006-10-27 15:13:54 +0000117 opt_complementary = "-2";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000118 getopt32(argv, ("-"OPT_STR) + 1); /* Reuse string */
Denis Vlasenkofefb2792006-10-27 15:13:54 +0000119 argv += optind;
Eric Andersend274b532002-10-10 03:47:01 +0000120
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000121 /* Restore option-like mode if needed */
122 if (arg) arg[0] = '-';
Manuel Novoa III cad53642003-03-19 09:13:01 +0000123
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000124 /* Ok, ready to do the deed now */
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000125 smode = *argv++;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000126 do {
Denis Vlasenko51b4c922006-10-27 16:07:20 +0000127 if (!recursive_action(*argv,
Denis Vlasenko8c0c1192006-10-28 12:42:40 +0000128 OPT_RECURSE, // recurse
Denis Vlasenko8c0c1192006-10-28 12:42:40 +0000129 fileAction, // file action
130 fileAction, // dir action
131 smode, // user data
132 0) // depth
Denis Vlasenko51b4c922006-10-27 16:07:20 +0000133 ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000134 retval = EXIT_FAILURE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000135 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000136 } while (*++argv);
137
138 return retval;
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000139}
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000140
141/*
142Security: chmod is too important and too subtle.
143This is a test script (busybox chmod versus coreutils).
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000144Run it in empty directory.
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000145
146#!/bin/sh
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000147t1="/tmp/busybox chmod"
148t2="/usr/bin/chmod"
149create() {
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000150 rm -rf $1; mkdir $1
151 (
152 cd $1 || exit 1
153 mkdir dir
154 >up
155 >file
156 >dir/file
157 ln -s dir linkdir
158 ln -s file linkfile
159 ln -s ../up dir/up
160 )
161}
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000162tst() {
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000163 (cd test1; $t1 $1)
164 (cd test2; $t2 $1)
165 (cd test1; ls -lR) >out1
166 (cd test2; ls -lR) >out2
167 echo "chmod $1" >out.diff
168 if ! diff -u out1 out2 >>out.diff; then exit 1; fi
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000169 rm out.diff
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000170}
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000171echo "If script produced 'out.diff' file, then at least one testcase failed"
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000172create test1; create test2
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000173tst "a+w file"
174tst "a-w dir"
175tst "a+w linkfile"
176tst "a-w linkdir"
177tst "-R a+w file"
178tst "-R a-w dir"
179tst "-R a+w linkfile"
180tst "-R a-w linkdir"
181tst "a-r,a+x linkfile"
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000182*/