Proper way to let user enter password for a bash script using only the GUI (with the terminal hidden)

Posted by MountainX on Ask Ubuntu See other posts from Ask Ubuntu or by MountainX
Published on 2013-06-29T18:05:53Z Indexed on 2013/06/30 4:28 UTC
Read the original article Hit count: 402

Filed under:
|
|
|
|

I have made a bash script that uses kdialog exclusively for interacting with the user. It is launched from a ".desktop" file so the user never sees the terminal. It looks 100% like a GUI app (even though it is just a bash script). It runs in KDE only (Kubuntu 12.04).

My only problem is handling password input securely and conveniently. I can't find a satisfactory solution.

The script was designed to be run as a normal user and to prompt for the password when a sudo command is first needed. In this way, most commands, those not requiring sudo rights, are run as the normal user. What happens (when the script is run from the terminal) is that the user is prompted for their password once and the default sudo timeout allows the script to finish, including any additional sudo commands, without prompting the user again. This is how I want it to work when run behind the GUI too.

The main problem is that using kdesudo to launch my script, which is the standard GUI way, means that the entire script is executed by the root user. So file ownerships get assigned to the root user, I can't rely upon ~/ in paths, and many other things are less than ideal. Running the entire script as the root user is just a very unsatisfactory solution and I think it is a bad practice.

I appreciate any ideas for letting a user enter the sudo password just once via GUI while not running the whole script as root. Thanks.

© Ask Ubuntu or respective owner

Related posts about bash

Related posts about scripts