Page 1 of 1

A small guide to restore default file permissions and owner on CMSMS filesystem

Posted: Sat Mar 29, 2008 12:22 am
by blast2007
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.

***

Code: Select all

#!/bin/bash
#
#  chmodr - Un chmod Ricorsivo, che rispetta le differenze file-directory
#  [color=green]chmodr - a recursive chmod that respects difference between file-directory[/color]
#  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 [color=green]DESCRIPTION[/color]
  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.

  [color=green]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.[/color]
  
  USO  [color=green]USAGE[/color]
  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.
  
  [color=green] Without parameters, it starts from current directory and sets to all files 
   640 (- rw- r-- ---) permission and 750 (- rwx r-x ---) to all subdirectories[/color]
 
  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).

  [color=green]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).[/color]

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

  [color=green]As third argument is possible to choose a different start directory, not current one.[/color]

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

Re: A small guide to restore default file permissions and owner on CMSMS filesys

Posted: Sat Mar 29, 2008 1:27 pm
by alby
blast2007 wrote: 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

Re: A small guide to restore default file permissions and owner on CMSMS filesys

Posted: Sat Mar 29, 2008 2:20 pm
by blast2007
alby wrote: Possibly enter in code a fourth parameter for the change of owner
Good idea,
maybe after row

Code: Select all

cd "$BASE_DIR"
we can think to add another row with

Code: Select all

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

Re: A small guide to restore default file permissions and owner on CMSMS filesys

Posted: Sat Mar 29, 2008 3:13 pm
by alby
blast2007 wrote:
alby wrote: Possibly enter in code a fourth parameter for the change of owner
Good idea,
maybe after row

Code: Select all

cd "$BASE_DIR"
Yes and a fifth parameter (group)  ;)
chmodr [file_permission] [directory_permission] [start_directory] [owner] [group]
............
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

Re: A small guide to restore default file permissions and owner on CMSMS filesys

Posted: Sat Mar 29, 2008 3:17 pm
by calguy1000
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%.

Re: A small guide to restore default file permissions and owner on CMSMS filesys

Posted: Wed May 21, 2008 2:26 am
by timh
I'm in the process of setting up my first CMSMS site and did an FTP install using Filezilla (see http://filezilla-project.org/) which has a utility to change permissions for directories and / or files... it seems like an easier option!

I have done some searching but is there a definitive guide to what the best settings are e.g. Directories to 755, files to 644? And any exceptions where security needs to be less / more?

TIA

Re: A small guide to restore default file permissions and owner on CMSMS filesys

Posted: Wed May 21, 2008 7:29 pm
by Dr.CSS
Here, step 4 & 5 then step 12 when done installing...

http://wiki.cmsmadesimple.org/index.php ... ck_Install

Re: A small guide to restore default file permissions and owner on CMSMS filesystem

Posted: Mon Jun 09, 2008 2:10 am
by aspires
I haven't been able to have any effect on my access to files in the File Manager by changing the file permissions through my control panel of my site.

I looked at the guide you linked to and followed it with no change.

Do you have any further advice please?

Thank you
'
andrea

Re: A small guide to restore default file permissions and owner on CMSMS filesys

Posted: Mon Jun 09, 2008 2:15 am
by Dr.CSS
@andrea

Did you FTP the unpacked folders/files to site?...

If so you should be able to change permissions/file attributes thru your FTP client...

Re: A small guide to restore default file permissions and owner on CMSMS filesystem

Posted: Mon Jun 16, 2008 1:00 am
by aspires
Yes I did. Is that the best way?

Re: A small guide to restore default file permissions and owner on CMSMS filesys

Posted: Mon Jun 16, 2008 2:09 am
by Dr.CSS
What permission shows in File manager, 755, 775?...

If you go to Site Admin » Global Settings the set unmask: to 002 and apply then clear cache it may help, but only for future files current ones will stay as is...