blob: 5832cc51b10dda2120634578c36024384a20efa4 [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 Vlasenkob097a842018-12-28 03:20:17 +010013//config: bool "chmod (5.5 kb)"
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010014//config: default y
15//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020016//config: chmod is used to change the access permission of files.
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010017
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"
Denys Vlasenkoe2b92152021-06-14 20:47:20 +020029//usage: "MODE is octal number (bit pattern sstrwxrwxrwx) or [ugoa]{+|-|=}[rwxXst]"
30//usage: "\n"
31//next 4 options are the same for chmod/chown/chgrp:
Pere Orga34425382011-03-31 14:43:25 +020032//usage: "\n -R Recurse"
33//usage: IF_DESKTOP(
34//usage: "\n -c List changed files"
Denys Vlasenkoe2b92152021-06-14 20:47:20 +020035//usage: "\n -v Verbose"
Pere Orga34425382011-03-31 14:43:25 +020036//usage: "\n -f Hide errors"
37//usage: )
38//usage:
39//usage:#define chmod_example_usage
40//usage: "$ ls -l /tmp/foo\n"
41//usage: "-rw-rw-r-- 1 root root 0 Apr 12 18:25 /tmp/foo\n"
42//usage: "$ chmod u+x /tmp/foo\n"
43//usage: "$ ls -l /tmp/foo\n"
44//usage: "-rwxrw-r-- 1 root root 0 Apr 12 18:25 /tmp/foo*\n"
45//usage: "$ chmod 444 /tmp/foo\n"
46//usage: "$ ls -l /tmp/foo\n"
47//usage: "-r--r--r-- 1 root root 0 Apr 12 18:25 /tmp/foo\n"
48
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000049#include "libbb.h"
Eric Andersen9f0fedb2001-04-24 18:07:19 +000050
Denis Vlasenko99912ca2007-04-10 15:43:37 +000051/* This is a NOEXEC applet. Be very careful! */
52
53
Denis Vlasenkofefb2792006-10-27 15:13:54 +000054#define OPT_RECURSE (option_mask32 & 1)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000055#define OPT_VERBOSE (IF_DESKTOP(option_mask32 & 2) IF_NOT_DESKTOP(0))
56#define OPT_CHANGED (IF_DESKTOP(option_mask32 & 4) IF_NOT_DESKTOP(0))
57#define OPT_QUIET (IF_DESKTOP(option_mask32 & 8) IF_NOT_DESKTOP(0))
58#define OPT_STR "R" IF_DESKTOP("vcf")
Denis Vlasenkofefb2792006-10-27 15:13:54 +000059
Denis Vlasenko8c35d652006-10-27 23:42:25 +000060/* coreutils:
Denis Vlasenko51b4c922006-10-27 16:07:20 +000061 * chmod never changes the permissions of symbolic links; the chmod
62 * system call cannot change their permissions. This is not a problem
63 * since the permissions of symbolic links are never used.
64 * However, for each symbolic link listed on the command line, chmod changes
65 * the permissions of the pointed-to file. In contrast, chmod ignores
66 * symbolic links encountered during recursive directory traversals.
67 */
68
Denys Vlasenko689d0652020-10-01 21:52:16 +020069static int FAST_FUNC fileAction(struct recursive_state *state,
70 const char *fileName,
71 struct stat *statbuf)
Eric Andersen9f0fedb2001-04-24 18:07:19 +000072{
Denis Vlasenko8c35d652006-10-27 23:42:25 +000073 mode_t newmode;
Denis Vlasenko51b4c922006-10-27 16:07:20 +000074
Denis Vlasenko8c35d652006-10-27 23:42:25 +000075 /* match coreutils behavior */
Denys Vlasenko689d0652020-10-01 21:52:16 +020076 if (state->depth == 0) {
Denis Vlasenko8c35d652006-10-27 23:42:25 +000077 /* statbuf holds lstat result, but we need stat (follow link) */
78 if (stat(fileName, statbuf))
79 goto err;
80 } else { /* depth > 0: skip links */
81 if (S_ISLNK(statbuf->st_mode))
82 return TRUE;
83 }
Denis Vlasenko51b4c922006-10-27 16:07:20 +000084
Denys Vlasenko689d0652020-10-01 21:52:16 +020085 newmode = bb_parse_mode((char *)state->userData, statbuf->st_mode);
Denys Vlasenko5711a2a2015-10-07 17:55:33 +020086 if (newmode == (mode_t)-1)
Denys Vlasenko689d0652020-10-01 21:52:16 +020087 bb_error_msg_and_die("invalid mode '%s'", (char *)state->userData);
Denis Vlasenkofefb2792006-10-27 15:13:54 +000088
Denis Vlasenko8c35d652006-10-27 23:42:25 +000089 if (chmod(fileName, newmode) == 0) {
90 if (OPT_VERBOSE
Denys Vlasenko2df4e0a2021-09-16 01:53:55 +020091 || (OPT_CHANGED
92 && (statbuf->st_mode & 07777) != (newmode & 07777))
Denis Vlasenkofefb2792006-10-27 15:13:54 +000093 ) {
Denys Vlasenko59ac4672021-09-17 01:13:58 +020094 char modestr[12];
Denis Vlasenkofefb2792006-10-27 15:13:54 +000095 printf("mode of '%s' changed to %04o (%s)\n", fileName,
Denys Vlasenko59ac4672021-09-17 01:13:58 +020096 newmode & 07777, bb_mode_string(modestr, newmode)+1);
Denis Vlasenkofefb2792006-10-27 15:13:54 +000097 }
98 return TRUE;
99 }
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000100 err:
101 if (!OPT_QUIET)
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000102 bb_simple_perror_msg(fileName);
Denis Vlasenkofefb2792006-10-27 15:13:54 +0000103 return FALSE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000104}
105
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000106int chmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000107int chmod_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000108{
Manuel Novoa III cad53642003-03-19 09:13:01 +0000109 int retval = EXIT_SUCCESS;
Denis Vlasenkofefb2792006-10-27 15:13:54 +0000110 char *arg, **argp;
Eric Andersend274b532002-10-10 03:47:01 +0000111 char *smode;
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000112
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000113 /* Convert first encountered -r into ar, -w into aw etc
114 * so that getopt would not eat it */
115 argp = argv;
116 while ((arg = *++argp)) {
Denis Vlasenko51b4c922006-10-27 16:07:20 +0000117 /* Mode spec must be the first arg (sans -R etc) */
118 /* (protect against mishandling e.g. "chmod 644 -r") */
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000119 if (arg[0] != '-') {
120 arg = NULL;
Denis Vlasenkofefb2792006-10-27 15:13:54 +0000121 break;
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000122 }
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000123 /* An option. Not a -- or valid option? */
124 if (arg[1] && !strchr("-"OPT_STR, arg[1])) {
125 arg[0] = 'a';
126 break;
127 }
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000128 }
129
Denis Vlasenko8c0c1192006-10-28 12:42:40 +0000130 /* Parse options */
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200131 getopt32(argv, "^" OPT_STR "\0" "-2");
Denis Vlasenkofefb2792006-10-27 15:13:54 +0000132 argv += optind;
Eric Andersend274b532002-10-10 03:47:01 +0000133
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000134 /* Restore option-like mode if needed */
135 if (arg) arg[0] = '-';
Manuel Novoa III cad53642003-03-19 09:13:01 +0000136
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000137 /* Ok, ready to do the deed now */
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000138 smode = *argv++;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000139 do {
Denis Vlasenko51b4c922006-10-27 16:07:20 +0000140 if (!recursive_action(*argv,
Denis Vlasenko8c0c1192006-10-28 12:42:40 +0000141 OPT_RECURSE, // recurse
Denis Vlasenko8c0c1192006-10-28 12:42:40 +0000142 fileAction, // file action
143 fileAction, // dir action
Denys Vlasenko689d0652020-10-01 21:52:16 +0200144 smode) // user data
Denis Vlasenko51b4c922006-10-27 16:07:20 +0000145 ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000146 retval = EXIT_FAILURE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000147 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000148 } while (*++argv);
149
150 return retval;
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000151}
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000152
153/*
154Security: chmod is too important and too subtle.
155This is a test script (busybox chmod versus coreutils).
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000156Run it in empty directory.
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000157
158#!/bin/sh
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000159t1="/tmp/busybox chmod"
160t2="/usr/bin/chmod"
161create() {
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000162 rm -rf $1; mkdir $1
163 (
164 cd $1 || exit 1
165 mkdir dir
166 >up
167 >file
168 >dir/file
169 ln -s dir linkdir
170 ln -s file linkfile
171 ln -s ../up dir/up
172 )
173}
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000174tst() {
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000175 (cd test1; $t1 $1)
176 (cd test2; $t2 $1)
177 (cd test1; ls -lR) >out1
178 (cd test2; ls -lR) >out2
179 echo "chmod $1" >out.diff
180 if ! diff -u out1 out2 >>out.diff; then exit 1; fi
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000181 rm out.diff
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000182}
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000183echo "If script produced 'out.diff' file, then at least one testcase failed"
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000184create test1; create test2
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000185tst "a+w file"
186tst "a-w dir"
187tst "a+w linkfile"
188tst "a-w linkdir"
189tst "-R a+w file"
190tst "-R a-w dir"
191tst "-R a+w linkfile"
192tst "-R a-w linkdir"
193tst "a-r,a+x linkfile"
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000194*/