busybox/coreutils/chmod.c
<<
>>
Prefs
   1/* vi: set sw=4 ts=4: */
   2/*
   3 * Mini chmod implementation for busybox
   4 *
   5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
   6 *
   7 * Reworked by (C) 2002 Vladimir Oleynik <dzo@simtreas.ru>
   8 *  to correctly parse '-rwxgoa'
   9 *
  10 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  11 */
  12//config:config CHMOD
  13//config:       bool "chmod (5.5 kb)"
  14//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
  21
  22/* BB_AUDIT SUSv3 compliant */
  23/* BB_AUDIT GNU defects - unsupported long options. */
  24/* http://www.opengroup.org/onlinepubs/007904975/utilities/chmod.html */
  25
  26//usage:#define chmod_trivial_usage
  27//usage:       "[-R"IF_DESKTOP("cvf")"] MODE[,MODE]... FILE..."
  28//usage:#define chmod_full_usage "\n\n"
  29//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:
  32//usage:     "\n        -R      Recurse"
  33//usage:        IF_DESKTOP(
  34//usage:     "\n        -c      List changed files"
  35//usage:     "\n        -v      Verbose"
  36//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
  49#include "libbb.h"
  50
  51/* This is a NOEXEC applet. Be very careful! */
  52
  53
  54#define OPT_RECURSE (option_mask32 & 1)
  55#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")
  59
  60/* coreutils:
  61 * 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
  69static int FAST_FUNC fileAction(struct recursive_state *state,
  70                const char *fileName,
  71                struct stat *statbuf)
  72{
  73        mode_t newmode;
  74
  75        /* match coreutils behavior */
  76        if (state->depth == 0) {
  77                /* 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        }
  84
  85        newmode = bb_parse_mode((char *)state->userData, statbuf->st_mode);
  86        if (newmode == (mode_t)-1)
  87                bb_error_msg_and_die("invalid mode '%s'", (char *)state->userData);
  88
  89        if (chmod(fileName, newmode) == 0) {
  90                if (OPT_VERBOSE
  91                 || (OPT_CHANGED && statbuf->st_mode != newmode)
  92                ) {
  93                        printf("mode of '%s' changed to %04o (%s)\n", fileName,
  94                                newmode & 07777, bb_mode_string(newmode)+1);
  95                }
  96                return TRUE;
  97        }
  98 err:
  99        if (!OPT_QUIET)
 100                bb_simple_perror_msg(fileName);
 101        return FALSE;
 102}
 103
 104int chmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 105int chmod_main(int argc UNUSED_PARAM, char **argv)
 106{
 107        int retval = EXIT_SUCCESS;
 108        char *arg, **argp;
 109        char *smode;
 110
 111        /* Convert first encountered -r into ar, -w into aw etc
 112         * so that getopt would not eat it */
 113        argp = argv;
 114        while ((arg = *++argp)) {
 115                /* Mode spec must be the first arg (sans -R etc) */
 116                /* (protect against mishandling e.g. "chmod 644 -r") */
 117                if (arg[0] != '-') {
 118                        arg = NULL;
 119                        break;
 120                }
 121                /* An option. Not a -- or valid option? */
 122                if (arg[1] && !strchr("-"OPT_STR, arg[1])) {
 123                        arg[0] = 'a';
 124                        break;
 125                }
 126        }
 127
 128        /* Parse options */
 129        getopt32(argv, "^" OPT_STR "\0" "-2");
 130        argv += optind;
 131
 132        /* Restore option-like mode if needed */
 133        if (arg) arg[0] = '-';
 134
 135        /* Ok, ready to do the deed now */
 136        smode = *argv++;
 137        do {
 138                if (!recursive_action(*argv,
 139                        OPT_RECURSE,    // recurse
 140                        fileAction,     // file action
 141                        fileAction,     // dir action
 142                        smode)          // user data
 143                ) {
 144                        retval = EXIT_FAILURE;
 145                }
 146        } while (*++argv);
 147
 148        return retval;
 149}
 150
 151/*
 152Security: chmod is too important and too subtle.
 153This is a test script (busybox chmod versus coreutils).
 154Run it in empty directory.
 155
 156#!/bin/sh
 157t1="/tmp/busybox chmod"
 158t2="/usr/bin/chmod"
 159create() {
 160    rm -rf $1; mkdir $1
 161    (
 162    cd $1 || exit 1
 163    mkdir dir
 164    >up
 165    >file
 166    >dir/file
 167    ln -s dir linkdir
 168    ln -s file linkfile
 169    ln -s ../up dir/up
 170    )
 171}
 172tst() {
 173    (cd test1; $t1 $1)
 174    (cd test2; $t2 $1)
 175    (cd test1; ls -lR) >out1
 176    (cd test2; ls -lR) >out2
 177    echo "chmod $1" >out.diff
 178    if ! diff -u out1 out2 >>out.diff; then exit 1; fi
 179    rm out.diff
 180}
 181echo "If script produced 'out.diff' file, then at least one testcase failed"
 182create test1; create test2
 183tst "a+w file"
 184tst "a-w dir"
 185tst "a+w linkfile"
 186tst "a-w linkdir"
 187tst "-R a+w file"
 188tst "-R a-w dir"
 189tst "-R a+w linkfile"
 190tst "-R a-w linkdir"
 191tst "a-r,a+x linkfile"
 192*/
 193