blob: 990e9b4ced5e955681b9d634b4d5f976334babfb [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 Vlasenko8c35d652006-10-27 23:42:25 +000034static int fileAction(const char *fileName, struct stat *statbuf, void* junk, 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 Vlasenkofefb2792006-10-27 15:13:54 +000049 if (!bb_parse_mode((char *)junk, &newmode))
50 bb_error_msg_and_die("invalid mode: %s", (char *)junk);
51
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 Vlasenkofefb2792006-10-27 15:13:54 +000067int chmod_main(int argc, char **argv)
Eric Andersen9f0fedb2001-04-24 18:07:19 +000068{
Manuel Novoa III cad53642003-03-19 09:13:01 +000069 int retval = EXIT_SUCCESS;
Denis Vlasenkofefb2792006-10-27 15:13:54 +000070 char *arg, **argp;
Eric Andersend274b532002-10-10 03:47:01 +000071 char *smode;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000072
Denis Vlasenko8c35d652006-10-27 23:42:25 +000073 /* Convert first encountered -r into ar, -w into aw etc
74 * so that getopt would not eat it */
75 argp = argv;
76 while ((arg = *++argp)) {
Denis Vlasenko51b4c922006-10-27 16:07:20 +000077 /* Mode spec must be the first arg (sans -R etc) */
78 /* (protect against mishandling e.g. "chmod 644 -r") */
Denis Vlasenko8c35d652006-10-27 23:42:25 +000079 if (arg[0] != '-') {
80 arg = NULL;
Denis Vlasenkofefb2792006-10-27 15:13:54 +000081 break;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000082 }
Denis Vlasenko8c35d652006-10-27 23:42:25 +000083 /* An option. Not a -- or valid option? */
84 if (arg[1] && !strchr("-"OPT_STR, arg[1])) {
85 arg[0] = 'a';
86 break;
87 }
Eric Andersen9f0fedb2001-04-24 18:07:19 +000088 }
89
Denis Vlasenko8c0c1192006-10-28 12:42:40 +000090 /* Parse options */
Denis Vlasenkofefb2792006-10-27 15:13:54 +000091 opt_complementary = "-2";
Denis Vlasenko8c35d652006-10-27 23:42:25 +000092 getopt32(argc, argv, ("-"OPT_STR) + 1); /* Reuse string */
Denis Vlasenkofefb2792006-10-27 15:13:54 +000093 argv += optind;
Eric Andersend274b532002-10-10 03:47:01 +000094
Denis Vlasenko8c35d652006-10-27 23:42:25 +000095 /* Restore option-like mode if needed */
96 if (arg) arg[0] = '-';
Manuel Novoa III cad53642003-03-19 09:13:01 +000097
Eric Andersen9f0fedb2001-04-24 18:07:19 +000098 /* Ok, ready to do the deed now */
Denis Vlasenko8c35d652006-10-27 23:42:25 +000099 smode = *argv++;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000100 do {
Denis Vlasenko51b4c922006-10-27 16:07:20 +0000101 if (!recursive_action(*argv,
Denis Vlasenko8c0c1192006-10-28 12:42:40 +0000102 OPT_RECURSE, // recurse
103 FALSE, // follow links: coreutils doesn't
104 FALSE, // depth first
105 fileAction, // file action
106 fileAction, // dir action
107 smode, // user data
108 0) // depth
Denis Vlasenko51b4c922006-10-27 16:07:20 +0000109 ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000110 retval = EXIT_FAILURE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000111 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000112 } while (*++argv);
113
114 return retval;
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000115}
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000116
117/*
118Security: chmod is too important and too subtle.
119This is a test script (busybox chmod versus coreutils).
120Run it in empty dir. Probably requires bash.
121
122#!/bin/sh
123function create() {
124 rm -rf $1; mkdir $1
125 (
126 cd $1 || exit 1
127 mkdir dir
128 >up
129 >file
130 >dir/file
131 ln -s dir linkdir
132 ln -s file linkfile
133 ln -s ../up dir/up
134 )
135}
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000136function tst() {
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000137 (cd test1; $t1 $1)
138 (cd test2; $t2 $1)
139 (cd test1; ls -lR) >out1
140 (cd test2; ls -lR) >out2
141 echo "chmod $1" >out.diff
142 if ! diff -u out1 out2 >>out.diff; then exit 1; fi
143 mv out.diff out1.diff
144}
145t1="/tmp/busybox chmod"
146t2="/usr/bin/chmod"
147create test1; create test2
Denis Vlasenko94cf69f2006-10-28 12:37:51 +0000148tst "a+w file"
149tst "a-w dir"
150tst "a+w linkfile"
151tst "a-w linkdir"
152tst "-R a+w file"
153tst "-R a-w dir"
154tst "-R a+w linkfile"
155tst "-R a-w linkdir"
156tst "a-r,a+x linkfile"
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000157*/