Welcome, Guest. Please login or register.
Did you miss your activation email?
09 May 2008, 14:54

Login with username, password and session length
Home Chat Help Search Calendar Login Register
Pages: [1]
Print
Author Topic: A small guide to restore default file permissions and owner on CMSMS filesystem  (Read 282 times)
0 Members and 1 Guest are viewing this topic.
blast2007
Forum Member
*

Karma: 1
Offline Offline

Posts: 161

Location: Italy



« on: 28 Mar 2008, 19:22 »

Sometimes happens, while uploading new files or creating new directory in CMSMS system dir, to modify unintentionally file/directory permissions/owner.

When you manually try to restore file permissions, it's very boring to restore different permissions for directories and files, especially when number of files to change is huge.

My first approach to solve this problem was to chmod files recursively, for example on directory /uploads I used:

# chmod 755 * -R

but this command chmods everything, files and directories with the same permission (755) without distinction. That was not a good solution.

Digging in google I've found a nice bash script, handful to restore all permissions to default settings.
By default It restores 640 to files and 750 to directory but you can set your own permissions, passing them as argument to the script.

The script name is chmodr. Here following the code. In green a  (bad) translation.

***

#!/bin/bash
#
#  chmodr - Un chmod Ricorsivo, che rispetta le differenze file-directory
chmodr - a recursive chmod that respects difference between file-directory
#  Copyright (C) 2004, 2006 - Antonio Ingargiola <debian@fastwebnet.it>

#  Released under the GNU GENERAL PUBLIC LICENSE.
#

help () {
    cat << EOF

  chmodr - Un chmod Ricorsivo, che rispetta le differenze file-directory
 
  Copyright (C) 2004, 2006 - Antonio Ingargiola <debian@fastwebnet.it>
 
  Released under the GNU GENERAL PUBLIC LICENSE.


  DESCRIZIONE DESCRIPTION
  Cambia ricorsivamente i permessi di file e directory, ma impostando
  permessi diversi per i file e per le directory. Nella ricorsione non
  segue link simbolici a directory.

  This script allow to change recursively permissions of files and directories,
  using different permissions for files and for directories. it doesn't follow symlink to directory.

 
  USO  USAGE
  chmodr
    Senza parametri, partendo dalla directory corrente imposta i permessi
    640 (- rw- r-- ---) a tutti i file e 750 (- rwx r-x ---) a tutte le
    sotto-directory.
 
  Without parameters, it starts from current directory and sets to all files
   640 (- rw- r-- ---) permission and 750 (- rwx r-x ---) to all subdirectories

 
  chmodr permessi_file permessi_dir
    Applica ai file nella directory corrente e sotto-directory i
    'permessi_file' e tutte le directory i 'permessi_dir'. La sintassi
    per i permessi e' la stessa di chmod quindi sia ottale (es. 640) che
    simbolica (es. o-rwx).

  chmodr file_permission directory_permission
   Apply to all files in current dir and to all subdir all  file_permission
   directory_permission. Syntax is the same of chmod, both octal (eg. 640)
   than symbolic (eg. o-rwx).


  E' possibile specificare un terzo parametro opzionale che indica una
  directory iniziale diversa da quella corrente.

  As third argument is possible to choose a different start directory, not current one.

EOF
    exit 1
}

valid_mod () {
# Controlla che i permessi siano sintatticamente corretti
   
    echo "$1" |\
    perl -n -e 'if ($_ =~ /^[01234567]{3}$/) {exit 0}; exit 1'
    ok_numerical=$?

    echo "$1" |\
    perl -n -e 'if ($_ =~ /^[ugoa]{0,3}[+-=]{1}[rwx]{1,3}$/) {exit 0}; exit 1'
    ok_symbolical=$?
   
    # echo "num $ok_numerical, sym $ok_symbolical" # DEBUG
    [ "$ok_numerical" = 0 -o "$ok_symbolical" = 0 ]
    return $?
}

[[ "$1" = -* ]] && help

FILE_MOD="$1"
DIR_MOD="$2"
BASE_DIR="$3"
[ -z "$1" ] && FILE_MOD="640"
[ -z "$2" ] && DIR_MOD="750"
[ -z "$3" ] && BASE_DIR="./"

cd "$BASE_DIR"

if !( valid_mod $FILE_MOD && valid_mod $DIR_MOD ); then
    echo -e "\n ERRORE: Il formato dei permessi e' errato. Per i dettagli"
    echo -e "         vedere la pagina di manuale di chmod.\n"
    exit 2
fi

ls | while read file; do
    if [ -L "$file" ]; then
        echo "   ==>> '$file' e' un link simbolico, lo ignoro."
   continue
    elif [ -d "$file" ]; then
   echo " Entro nella directory '$file'"
   
   chmod u+rwx "$file" 2> /dev/null ||\
   { echo " Non ho i permessi per entrare in '$file'"; continue; }
   cd "$file"; $0 $@; cd - > /dev/null
   
   chmod $DIR_MOD "$file" && echo -e " Directory '$file' impostata.\n"
    elif [ -f "$file" ]; then
   echo -n "   $file ... "
   chmod $FILE_MOD "$file" && echo " [ OK ]"
    else
   echo " Ignoro '$file'."
    fi
done

***
Finally, to restore default owner of files and directories just open a shell and write (if owner is www-data)
chmod www-data:www-data * -R

That's all
Feedback are welcome.

Regards
blast
Logged
alby
Member Support Team
Support Guru
Power Poster
****

Karma: 99
Offline Offline

Posts: 2125

Location: Ferrara, Italy


My kids


« Reply #1 on: 29 Mar 2008, 08:27 »

Finally, to restore default owner of files and directories just open a shell and write (if owner is www-data)
chmod www-data:www-data * -R

chown  www-data:www-data * -R

Possibly enter in code a fourth parameter for the change of owner

Alby
Logged

Member CMSMS Support Team
Italian Forum Moderator - Italian admin Project

Plugins: Geolocate hostip - Multiple random image - Image rotator (PLEASE test beta) - Content Pagination (TEST PLEASE)

Modules: ForumMadeSimple - Howto (FMS)

Multilingual: CMSMS MLE 1.2.4b - Doc/Howto/Tips (#9)
blast2007
Forum Member
*

Karma: 1
Offline Offline

Posts: 161

Location: Italy



« Reply #2 on: 29 Mar 2008, 09:20 »

Possibly enter in code a fourth parameter for the change of owner

Good idea,
maybe after row
Code:
cd "$BASE_DIR"
we can think to add another row with
Code:
chown  $4:$4 * -R

Complete command line for user www-data, file permission 640 and directory permission 750 in current directory will be:

chmodr 640 750 ./ www-data

Is it correct?
Regards
blast


« Last Edit: 29 Mar 2008, 09:22 by blast2007 » Logged
alby
Member Support Team
Support Guru
Power Poster
****

Karma: 99
Offline Offline

Posts: 2125

Location: Ferrara, Italy


My kids


« Reply #3 on: 29 Mar 2008, 10:13 »

Possibly enter in code a fourth parameter for the change of owner

Good idea,
maybe after row
Code:
cd "$BASE_DIR"

Yes and a fifth parameter (group)  Wink
chmodr [file_permission] [directory_permission] [start_directory] [owner] [group]

Quote
............
cd "$BASE_DIR"

if [ -n "$4" ]; then
 GROUP="$5"
 [ -n "$5" ] && GROUP="$5"
 chown -R $4:$GROUP * || exit 1
fi


if !( valid_mod $FILE_MOD && valid_mod $DIR_MOD ); then
............

Alby
Logged

Member CMSMS Support Team
Italian Forum Moderator - Italian admin Project

Plugins: Geolocate hostip - Multiple random image - Image rotator (PLEASE test beta) - Content Pagination (TEST PLEASE)

Modules: ForumMadeSimple - Howto (FMS)

Multilingual: CMSMS MLE 1.2.4b - Doc/Howto/Tips (#9)
calguy1000
Posts that don't follow the rules (don't ask what the rules are, read them) will be ignored, moved or deleted
Dev Team Member
Power Poster
*****

Karma: 141
Offline Offline

Posts: 3455

Location: Calgary, Canada



WWW
« Reply #4 on: 29 Mar 2008, 10:17 »

Unfortunately guys.... only root can change the ownership of files.  So you may as well take that out of there.  Other users can use chgrp I think, but not chown.

And this script will only benefit people that have shell access to their servers.... so that's maybe 10%.

 
Logged

----
CMS Made Simple is Simple - For experienced developers.   You don't need to know php to use CMS but you should have some experience with website design, website development, and supporting dynamic applications in a hosted environment.   The words CSS, XHTML, and permissions should not be new to you.
Pages: [1]
Print
Jump to: