set upper limit and lower limit in time for user before executing shutdown command in linux

145 Views Asked by At

let's say I want one of the non-root users named "Alex" to only allow to shutdown command but with the restriction like, he can only shutdown after minimum of 120 min and no delay more than 1000mins. Basically, to set upper limit and lower limit for executing the shutdown command. To add on I am using Rocky Linux 9.

1

There are 1 best solutions below

4
KamilCuk On

You can:

  • download shutdown C source code, modify it to check if the user is Alex, and if so, check the required time range. Then compile this source code to a program named like my_shutdown, install it to /usr/local/bin
    • and add it with NOPASSWD for alex in sudoers
    • or just for every other user exit your program, and set SUID bit on your executable.
    • or you can write the program from scratch instead of using existing shutdown.c implementation
  • Or you can add specific list of combinations of arguments as NOPASSWD to sudoers, like alex ALL=NOPASSWD: shutdown 120m , ... 121m ... 122m etc. for every combination of arguments you want to support
  • or you can write a wrapper that will parse the argument to shut down and check if it's in range, if it is, it will forward the call to shutdown executable. And add that wrapper into sudoers, or add SUID on it. Strongly consider not using a scripting language - for example, with python with any import and by manipulating PYTHONPATH environment variable, the user will be able to execute any code as root.