Function to silence rm -rf option for my user


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Function to silence rm -rf option for my user
# 1  
Old 11-05-2015
Function to silence rm -rf option for my user

Hi Gurus,

I am trying to silence or supress rm -rf option for a particular user(venkat).
for that am going to write a function in a script test_fun_ls.sh like below

Code:
#!/bin/bash
RM_FUNCTION ()
{
        if [ $# -ne 0 ]
        then
                case ${1} in
                -r)
                        echo "user $USER Restricted to use rm -r option"
                ;;
                -rf)
                        echo "user $USER Restricted to use rm -rf option"
                ;;
                -fr)
                        echo "user $USER Restricted to use rm -rf option"
                ;;
                *)
                        echo "OK"
                        if [ $# -ge 1 ]
                        then
                                rm $@
                        else
                                rm $1
                        fi
                ;;
                esac
        else
                echo "OK"
                rm
        fi
}

and reading it in .bash_profile

like below

Code:
source /home/venkat/scripts/test_fun_ls.sh
alias ls='RM_FUNCTION'

above is working fine but if i use ls with /bin/rm -rf it will have no effect for the function above.
please help.
Also Someone please advice me above process is ok or not

Regard's
Venkat
# 2  
Old 11-05-2015
What exactly are you trying to achieve? Be aware that any bash alias or function can be circumvented in one way or the other.
# 3  
Old 11-05-2015
Hi RudiC,

AM trying to supress -rf argument for rm command.

Regard's
Venkat
# 4  
Old 11-05-2015
Hello Venkat,

You can either remove the execute permission from the rm command but that will restrict other users also from executing the rmcommand. So, you can make bin folder under user's home folder containing softlinks to executable in /bin folder and just remove rm softlinkfrom user's ~/bin folder and set user's default path under ~/.bashrc file to use ~/bin for search for executable.
Note: As mentioned on link user can overcome this approach by resetting $PATH, also would suggest you to try it at dev environment first please.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 11-05-2015
Quote:
Originally Posted by venky.b5
Hi RudiC,

AM trying to supress -rf argument for rm command.

Regard's
Venkat
No way unless you recompile the sources. Anything you do on shell level can be circumvented. (I'm not sure if sudo would offer an option to implement this)
This User Gave Thanks to RudiC For This Post:
# 6  
Old 11-05-2015
What would happen if another flag was in argument 1, then -rf was argument 2? I feel you would never catch it.

If you are concerned that people may delete files by mistake, you would be better to remove their access to do so, else there are many other ways to destroy things, e.g. renaming the files, overwriting etc.



Robin
This User Gave Thanks to rbatte1 For This Post:
# 7  
Old 11-05-2015
In addition to what has already been said by others, the lack of quoting with your expansions of $1 and $@ make it impossible to remove any file with a pathname containing any whitespace characters. And, expanding on what rbatte1 said, your entire scheme is defeated by simply using any of the following:
Code:
/bin/rm -rf file...
rm -fR file...
rm -Rf file...
rm -R -f file...
rm -r -f file...
rm -i -r -f file...
rm -i -f -r file...
rm -i -rf file...
rm -i -fr file...
rm -rif file...
rm -ifr file...
rm -i -f -i -f -i -r -f file...

and billions of others.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Solaris

Apache 2.4 User/Group option with svcadm

Hello all, Solaris 11. Branch: 0.175.3.35.0.6.0 Asking for some assistance in trying to understand how Apache24 works with svcadm. I used: svccfg -s network/http:apache24 listprop setprop start/user=<rabbit> setprop start/group=<pod> This is also set in... (1 Reply)
Discussion started by: smiloo
1 Replies

2. Shell Programming and Scripting

awk - user function?

Here's a snippit from a script I'm working on srvctl config database | \ awk -F\" '{print "srvctl config database -d " $1 " -a" \ RS "echo =================================================="}' \ > /tmp/x$$.sh chmod 777 /tmp/x$$.sh /tmp/x$$.sh As you can see, I'm piping the... (12 Replies)
Discussion started by: edstevens
12 Replies

3. Shell Programming and Scripting

KSH- perform a function if user selects option from menu

Hi, I have a script that copies a file from one directory to another and compiles it. What I have now is a menu that calls functions and each function compiles the file. I want to have a function that compiles the file at the end of the script if the user selects options 1-3 in the menu, but... (0 Replies)
Discussion started by: amitlib
0 Replies

4. Shell Programming and Scripting

Interactive script to give user option of what text to add

Hello all, I have created a script that will remove the first two lines of one text file (body.txt) and then add the text from a different text file (header.txt) directly to the beginning of the freshly modified body.txt file. It is as follows: #!/bin/bash ## This script will add a header... (2 Replies)
Discussion started by: marcozd
2 Replies

5. Shell Programming and Scripting

recently introduced to the newer option for find...does an older option exist?

To find all the files in your home directory that have been edited in some way since the last tar file, use this command: find . -newer backup.tar.gz Is anyone familiar with an older solution? looking to identify files older then 15mins across several directories. thanks, manny (2 Replies)
Discussion started by: mr_manny
2 Replies

6. Shell Programming and Scripting

Call Function in .pm as another user

Hello, I need to call a function which reside in some package moudle (.pm) as another user by using su -. Are anyone know how can I do it? Thanks (1 Reply)
Discussion started by: Alalush
1 Replies

7. Shell Programming and Scripting

need help with User Defined Function

Dear Friends, I need a help regarding User defined function in shell script. My problem is as follows: my_func.sh my_funcI(){ grep 'mystring' I.dat } my_funcQ(){ grep 'mystring' Q.dat } myfuncI myfuncQ But As both the function has same function only the... (11 Replies)
Discussion started by: user_prady
11 Replies
Login or Register to Ask a Question