DietPi-Login | Replace "sudo systemctl" by "pgrep" to allow non-sudoers login
ADMIN EDIT
Solution:
sed -i "/until sudo systemctl/c\ while pgrep -f '/DietPi/dietpi/postboot' &> /dev/null" /DietPi/dietpi/dietpi-login
Creating a bug report/issue
Required Information
- DietPi version | 6.22.3
- Distro version | stretch
- Kernel version | Linux raspi 4.14.98-v7+ #1200 SMP Tue Feb 12 20:27:48 GMT 2019 armv7l GNU/Linux
- SBC device | RPi 2 Model B (armv7l)
- Power supply used | 5V 1A RAVpower
- SDcard used | SanDisk
Additional Information (if applicable)
- Software title | dietpi-postboot
- The system was just updated to 6.22.3 (from 6.21.x)
- Can this issue be replicated on a fresh installation of DietPi?
dietpi-bugreportID |sed -n 5p /DietPi/dietpi/.hw_model
Steps to reproduce
- update to 6.22.3
- login with user via ssh with passwordless keys method
- sudo password asked to executes command:
sudo systemctl -ln 0 --no-pager status dietpi-postbootand user should enter their own password...
Expected behaviour
- Login without asking for sudo password
Actual behaviour
- Asks for sudo password
Extra details
- I log with my user to the freshly updated dietpi v6.22.3
- It asks for password, but I do not enter it, meanwhile...
- On another ssh terminal I login to the machine (it asks for sudo password and I enter it here)
- Checking the suspect program with
ps -ef | grep sudo - The result is: "sudo systemctl -ln 0 --no-pager status dietpi-postboot"
- Workaround: Edit /etc/sudoers with command
visudo: - Add line: "%sudo ALL=(ALL:ALL) NOPASSWD: /bin/systemctl -ln 0 --no-pager status dietpi-postboot"
@xsak Many thanks for your report.
So you didn't add your user to the sudoers list in a way that it can run sudo without password, right?
Hmm yeah indeed we should allow login even as non-sudo user without such prompt and/or error. sudo ALL=(ALL:ALL) NOPASSWD: requires at least sudo permissions. I think pgrep -f '/DietPi/dietpi/postboot'' should work instead without root/sudo permissions.
Just as background:
- We want to hold our login script until dietpi-postboot has finished which is the boot systemd unit that starts user services (webservers, media servers, databases and such) at the end of the boot process.
- This is since the login script in case starts Kodi, Chromium, a desktop or such, if chosen as autostart option. And in some cases related background services need to be started for this to succeed.
- We added
systemctl status dietpi-postbootto check whether the service has existed already or not yet. Butsystemctlrequires root permissions. - Yeah but adding
sudois no great solution as login should work without prompt/error with any kind of user. I think replacing that withpgrep -f '/DietPi/dietpi/postboot'should be the even quicker and simpler check.
Quick check and it works great:
root@VM-Buster:~# systemctl restart dietpi-postboot
root@VM-Buster:~# pgrep -f '/DietPi/dietpi/postboot' && echo yes
999
yes
root@VM-Buster:~# systemctl stop dietpi-postboot
root@VM-Buster:~# pgrep -f '/DietPi/dietpi/postboot' && echo yes
root@VM-Buster:~#
and as non-sudoers:
root@VM-Buster:~# sudo -u www-data pgrep -f '/DietPi/dietpi/postboot' && echo yes
1006
yes
@Fourdee
Do you remember/know any reason why we added the more complicated check via sudo systemctl -ln 0 --no-pager status dietpi-postboot | grep -qi 'exited'? Okay perhaps this is the obvious idea since it is a systemd unit in the first place, but pgrep is the way simpler and faster command and it does not require root permissions.
This would even cause an endless loop on non-sudoers logins (until cancelled), since the check returns "false" if the command fails due to missing permissions.
Fixed: https://github.com/MichaIng/DietPi/commit/4417d8cb065e273a01e0b08c7868d29d6ca5f236 Changelog: https://github.com/MichaIng/DietPi/commit/9fdf8ae4756d4b2c7d5dff5cc8859c91331104ac
Many thanks again for reporting, this was indeed a nasty one!