I'm just writing a script that adds a new User in Linux, samba, mysql and in a record in a mysql-db. This script obviously has to be run as root. I am aware of the security issues when saving a root password in a bash variable, but I guess typing it over and over again isn't much less secure. And this post isn't about the security aspect, so please try and help me with the real question.
In a pure bash environment I can use the following code to look if I'm root, and if not, restart the same script with "su"
CODE
if [ $UID -ne 0 ]; then
echo "Please, type root's password..."
su -c "$0 $@"
exit
fi
# rest of program here...
echo "Please, type root's password..."
su -c "$0 $@"
exit
fi
# rest of program here...
This will cause su to ask the user for the password.
Now I want to run this in a graphical environment. Of course, I can use "kdesu" instead of "su", but I'm not sure, kdesu is even there. But there is zenity. Now I tried:
CODE
if [ $UID -ne 0 ]; then
rootpw=`zenity --title="Root-Password" --text='Please, type root's password...' --hide-text="" --entry`
echo $PASSWD | su -c "$0 $@"
exit
fi
# rest of program here...
rootpw=`zenity --title="Root-Password" --text='Please, type root's password...' --hide-text="" --entry`
echo $PASSWD | su -c "$0 $@"
exit
fi
# rest of program here...
But su responds with "standard in must be a tty".
sudo on the other hand doesn't seem to have the same problem, so e.g. in Ubuntu this works fine, as the user is always a sudoer. But by default sudo isn't installed in Mandriva 2008 and the user is no sudoer, so my script won't work on the servers I maintain without installing and configuring sudo first or installing kdesu...
So: Is there any way to execute a bash script as a user, having zenity ask for the root PW and execute something with this PW which is stored in a variable?
Thanks a lot.
PeterPanic
