blob: ec075f7c9cb7238120e6c97beb4dfd18a4bc5099 [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 */
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010012//config:config CHMOD
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020013//config: bool "chmod (5.1 kb)"
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010014//config: default y
15//config: help
16//config: chmod is used to change the access permission of files.
17
18//applet:IF_CHMOD(APPLET_NOEXEC(chmod, chmod, BB_DIR_BIN, BB_SUID_DROP, chmod))
19
20//kbuild:lib-$(CONFIG_CHMOD) += chmod.o
Eric Andersen9f0fedb2001-04-24 18:07:19 +000021
Manuel Novoa III cad53642003-03-19 09:13:01 +000022/* BB_AUDIT SUSv3 compliant */
Denis Vlasenkofefb2792006-10-27 15:13:54 +000023/* BB_AUDIT GNU defects - unsupported long options. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000024/* http://www.opengroup.org/onlinepubs/007904975/utilities/chmod.html */
25
Pere Orga34425382011-03-31 14:43:25 +020026//usage:#define chmod_trivial_usage
27//usage: "[-R"IF_DESKTOP("cvf")"] MODE[,MODE]... FILE..."
28//usage:#define chmod_full_usage "\n\n"
29//usage: "Each MODE is one or more of the letters ugoa, one of the\n"
30//usage: "symbols +-= and one or more of the letters rwxst\n"
Pere Orga34425382011-03-31 14:43:25 +020031//usage: "\n -R Recurse"
32//usage: IF_DESKTOP(
33//usage: "\n -c List changed files"
34//usage: "\n -v List all files"
35//usage: "\n -f Hide errors"
36//usage: )
37//usage:
38//usage:#define chmod_example_usage
39//usage: "$ ls -l /tmp/foo\n"
40//usage: "-rw-rw-r-- 1 root root 0 Apr 12 18:25 /tmp/foo\n"
41//usage: "$ chmod u+x /tmp/foo\n"
42//usage: "$ ls -l /tmp/foo\n"
43//usage: "-rwxrw-r-- 1 root root 0 Apr 12 18:25 /tmp/foo*\n"
44//usage: "$ chmod 444 /tmp/foo\n"
45//usage: "$ ls -l /tmp/foo\n"
46//usage: "-r--r--r-- 1 root root 0 Apr 12 18:25 /tmp/foo\n"
47
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000048#include "libbb.h"
Eric Andersen9f0fedb2001-04-24 18:07:19 +000049
Denis Vlasenko99912ca2007-04-10 15:43:37 +000050/* This is a NOEXEC applet. Be very careful! */
51
52
Denis Vlasenkofefb2792006-10-27 15:13:54 +000053#define OPT_RECURSE (option_mask32 & 1)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000054#define OPT_VERBOSE (IF_DESKTOP(option_mask32 & 2) IF_NOT_DESKTOP(0))
55#define OPT_CHANGED (IF_DESKTOP(option_mask32 & 4) IF_NOT_DESKTOP(0))
56#define OPT_QUIET (IF_DESKTOP(option_mask32 & 8) IF_NOT_DESKTOP(0))
57#define OPT_STR "R" IF_DESKTOP("vcf")
Denis Vlasenkofefb2792006-10-27 15:13:54 +000058
Denis Vlasenko8c35d652006-10-27 23:42:25 +000059/* coreutils:
Denis Vlasenko51b4c922006-10-27 16:07:20 +000060 * chmod never changes the permissions of symbolic links; the chmod
61 * system call cannot change their permissions. This is not a problem
62 * since the permissions of symbolic links are never used.
63 * However, for each symbolic link listed on the command line, chmod changes
64 * the permissions of the pointed-to file. In contrast, chmod ignores
65 * symbolic links encountered during recursive directory traversals.
66 */
67
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000068static int FAST_FUNC fileAction(const char *fileName, struct stat *statbuf, void* param, int depth)
Eric Andersen9f0fedb2001-04-24 18:07:19 +000069{
Denis Vlasenko8c35d652006-10-27 23:42:25 +000070 mode_t newmode;
Denis Vlasenko51b4c922006-10-27 16:07:20 +000071
Denis Vlasenko8c35d652006-10-27 23:42:25 +000072 /* match coreutils behavior */
73 if (depth == 0) {
74 /* statbuf holds lstat result, but we need stat (follow link) */
75 if (stat(fileName, statbuf))
76 goto err;
77 } else { /* depth > 0: skip links */
78 if (S_ISLNK(statbuf->st_mode))
79 return TRUE;
80 }
Denis Vlasenko51b4c922006-10-27 16:07:20 +000081
Denys Vlasenko5711a2a2015-10-07 17:55:33 +020082 newmode = bb_parse_mode((char *)param, statbuf->st_mode);
83 if (newmode == (mode_t)-1)
Denys Vlasenko651a2692010-03-23 16:25:17 +010084 bb_error_msg_and_die("invalid mode '%s'", (char *)param);
Denis Vlasenkofefb2792006-10-27 15:13:54 +000085
Denis Vlasenko8c35d652006-10-27 23:42:25 +000086 if (chmod(fileName, newmode) == 0) {
87 if (OPT_VERBOSE
Denis Vlasenkofefb2792006-10-27 15:13:54 +000088 || (OPT_CHANGED && statbuf->st_mode != newmode)
89 ) {
90 printf("mode of '%s' changed to %04o (%s)\n", fileName,
Denis Vlasenko51b4c922006-10-27 16:07:20 +000091 newmode & 07777, bb_mode_string(newmode)+1);
Denis Vlasenkofefb2792006-10-27 15:13:54 +000092 }
93 return TRUE;
94 }
Denis Vlasenko8c35d652006-10-27 23:42:25 +000095 err:
96 if (!OPT_QUIET)
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +000097 bb_simple_perror_msg(fileName);
Denis Vlasenkofefb2792006-10-27 15:13:54 +000098 return FALSE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000099}
100
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000101int chmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000102int chmod_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000103{
Manuel Novoa III cad53642003-03-19 09:13:01 +0000104 int retval = EXIT_SUCCESS;
Denis Vlasenkofefb2792006-10-27 15:13:54 +0000105 char *arg, **argp;
Eric Andersend274b532002-10-10 03:47:01 +0000106 char *smode;
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000107
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000108 /* Convert first encountered -r into ar, -w into aw etc
109 * so that getopt would not eat it */
110 argp = argv;
111 while ((arg = *++argp)) {
Denis Vlasenko51b4c922006-10-27 16:07:20 +0000112 /* Mode spec must be the first arg (sans -R etc) */
113 /* (protect against mishandling e.g. "chmod 644 -r") */
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000114 if (arg[0] != '-') {
115 arg = NULL;
Denis Vlasenkofefb2792006-10-27 15:13:54 +0000116 break;
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000117 }
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000118 /* An option. Not a -- or valid option? */
119 if (arg[1] && !strchr("-"OPT_STR, arg[1])) {
120 arg[0] = 'a';
121 break;
122 }
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000123 }
124
Denis Vlasenko8c0c1192006-10-28 12:42:40 +0000125 /* Parse options */
Denis Vlasenkofefb2792006-10-27 15:13:54 +0000126 opt_complementary = "-2";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000127 getopt32(argv, ("-"OPT_STR) + 1); /* Reuse string */
Denis Vlasenkofefb2792006-10-27 15:13:54 +0000128 argv += optind;
Eric Andersend274b532002-10-10 03:47:01 +0000129
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000130 /* Restore option-like mode if needed */
131 if (arg) arg[0] = '-';
Manuel Novoa III cad53642003-03-19 09:13:01 +0000132
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000133 /* Ok, ready to do the deed now */
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000134 smode = *argv++;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000135 do {
Denis Vlasenko51b4c922006-10-27 16:07:20 +0000136 if (!recursive_action(*argv,
Denis Vlasenko8c0c1192006-10-28 12:42:40 +0000137 OPT_RECURSE, // recurse
Denis Vlasenko8c0c1192006-10-28 12:42:40 +0000138 fileAction, // file action
139 fileAction, // dir action
140 smode, // user data
141 0) // depth
Denis Vlasenko51b4c922006-10-27 16:07:20 +0000142 ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000143 retval = EXIT_FAILURE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000144 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000145 } while (*++argv);
146
147 return retval;
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000148}
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000149
150/*
151Security: chmod is too important and too subtle.
152This is a test script (busybox chmod versus coreutils).
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000153Run it in empty directory.
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000154
155#!/bin/sh
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000156t1="/tmp/busybox chmod"
157t2="/usr/bin/chmod"
158create() {
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000159 rm -rf $1; mkdir $1
160 (
161 cd $1 || exit 1
162 mkdir dir
163 >up
164 >file
165 >dir/file
166 ln -s dir linkdir
167 ln -s file linkfile
168 ln -s ../up dir/up
169 )
170}
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000171tst() {
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000172 (cd test1; $t1 $1)
173 (cd test2; $t2 $1)
174 (cd test1; ls -lR) >out1
175 (cd test2; ls -lR) >out2
176 echo "chmod $1" >out.diff
177 if ! diff -u out1 out2 >>out.diff; then exit 1; fi
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000178 rm out.diff
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000179}
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000180echo "If script produced 'out.diff' file, then at least one testcase failed"
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000181create test1; create test2
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000182tst "a+w file"
183tst "a-w dir"
184tst "a+w linkfile"
185tst "a-w linkdir"
186tst "-R a+w file"
187tst "-R a-w dir"
188tst "-R a+w linkfile"
189tst "-R a-w linkdir"
190tst "a-r,a+x linkfile"
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000191*/