Author Topic: Privileges within a Bash Script  (Read 1585 times)

Offline Was_Just19

  • Hero Member
  • *****
  • Posts: 6852
  • MLU
Privileges within a Bash Script
« on: October 09, 2010, 08:03:30 AM »
I have been attempting to get some knowledge of how privileges can be changed within a running Bash script, with some little success.

From within a running Bash script a command can be launched as root which will ask for the required password. Of course the command can be launched as any user too .....  Options -c for root and -u followed by username, for another user, when using kdesu.

kdesu -u test dolphin    #  launches Dolphin as user 'test'

kdesu -c dolphin            # launches Dolphin as user root


So what if you have several commands to be executed?
This format works ....  giving both Dolphin and Konqueror launched as user root. It requires the password to be input only once to run both commands.

kdesu -c 'dolphin; konqueror --profile filemanagement'

So now I am wondering how far this can be taken in a Bash script.
On initial reading I was unsure this much was even possible

It might be useful for instance if a bunch of commands could be placed in a function and the function called as root, but I believe this is not possible.

So all that leads to the question ......  is there some way that commands that require root privileges can be run in a user-level Bash script, requiring the input of the password only once for all of them, without changing the privileg of the running Bash script itself?

I am just trying to explore the limits of what is possible in Bash scripts.
Using the one line example above would not be suitable for lots of commands.

I just know I will be asked ......  real world application of this? .....  I have no idea at the moment ......  just want to know the limitations ...

regards.

Offline travisN000

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1758
Re: Privileges within a Bash Script
« Reply #1 on: December 24, 2010, 06:57:22 PM »
...its been awhile, but I just saw the post...

The easiest way to deal with this situation if you have a long list of commands, or just want to have your kdesu/gksu box less cluttered is to make a short shell script on the fly with the cat command, then call that sub-script with kdesu/gksu, etc.

Code: [Select]
#!/bin/bash
# REQUIRES NON-ROOT user

if [ "$UID" == "0" ]; then
    zenity --error --title="Sample script" \
           --text=$"Please DO NOT run this script as root!"
    echo -e $"\nPlease use this as normal user, not root."
    exit 1
fi

TMP_DIR="$HOME/tmp"
SUBSCRIPT="my_subscript"
SUBSCRIPT_PTH="$TMP_DIR/$SUBSCRIPT"




#Begin Sub-script
cat > $SUBSCRIPT_PTH << _EOF_
#!/bin/bash

if [ \$UID -ne 0 ]; then
    zenity --error --title="Sample script" \
           --text=$"Please run this script as root!"
    echo -e $"\nPlease run this script as root user.\n"
    exit 1
fi

dophin
konqueror --profile filemanagement

_EOF_
#End Sub-script

# make sub-script executable
chmod 755 $SUBSCRIPT_PTH

#Display script contents before running as root
zenity --text-info --window-icon="$WINDOW_ICON" --width=600 --height=300 \
       --title=$"(ROOT) Sub-script contents:" \
       --filename="$SUBSCRIPT_PTH"

# run as root user using kdesu or gksu
# ...cd to script directory to keep dialog tidy
cd $TMP_DIR
mysu=`which kdesu 2>/dev/null` || mysu=`which gksu 2>/dev/null` && $mysu "./$SUBSCRIPT"

rm -f $SUBSCRIPT_PTH

exit


« Last Edit: December 24, 2010, 07:14:33 PM by travisn000 »