Wednesday, May 1, 2024
 Popular · Latest · Hot · Upcoming
18
rated 0 times [  18] [ 0]  / answers: 1 / hits: 26570  / 2 Years ago, fri, december 24, 2021, 8:29:44

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.


More From » bash

 Answers
6

The -A sudo option allows you to specify a helper program (in the SUDO_ASKPASS variable) that will ask for the password.



Create a script to ask the password (myaskpass.sh):



#!/bin/bash
zenity --password --title=Authentication


Then insert this line at the beginning of your script:



export SUDO_ASKPASS="/path/to/myaskpass.sh"


and replace all occurences of sudo <command> with:



sudo -A <command>


You can use whatever password asking program you want instead of zenity. I had to encapsulate it within a script because SUDO_ASKPASS must point to a file, so it won't work with the --password option required by zenity.



The above works like a charm if it runs from command line or if you choose Run in terminal after double click the script file in the file manager, but if you choose Run or try to launch it from a .desktop file every sudo will ask for the for password again.






If you don't want a terminal window at all, you can store the password in a variable and pipe it to sudo -S. Maybe there's some security concerns, but I think it's pretty safe (read the comments on this answer).



Insert this line at the beginning of your script:



PASSWD="$(zenity --password --title=Authentication)
"


and replace all occurences of sudo <command> with:



echo -e $PASSWD | sudo -S <command>

[#30525] Saturday, December 25, 2021, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mocipe

Total Points: 161
Total Questions: 106
Total Answers: 118

Location: Cambodia
Member since Thu, Oct 7, 2021
3 Years ago
;