-2

Is it possible to make a shutdown script which will run as user only without using root privileges?

I found this code but it seems to do nothing:

#include <unistd.h>
#include <sys/reboot.h>

int main () {
    sync();    // If reboot() not preceded by a sync(), data will be lost.
    setuid(0); // set uid to root, the running uid must already have the
               // appropriate permissions to do this.
    reboot(RB_AUTOBOOT); // note, this reboots the system, it's not as
    return(0);           // graceful as asking the init system to reboot.
}

System info:

Linux hosek 4.15.0-48-generic #51-Ubuntu SMP Wed Apr 3 08:28:49 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
genpfault
  • 51,148
  • 11
  • 85
  • 139
genderbee
  • 203
  • 2
  • 10
  • 2
    Yes, but the program has to be marked setuid root. Only root can do that. But after root does it once, it stays that way. – Omnifarious Sep 27 '19 at 13:11
  • You ignored the return value from `setuid()` - it will return -1, set `errno` to `EPERM` and leave the uid unchanged if you don't have sufficient permission. You really want `if (setuid(0) != 0) { perror(""); return 1; }` – Toby Speight Sep 27 '19 at 13:17
  • @Omnifarious So it is not possible for example make this `c` program, upload to another server and run this command to reboot/shutdown server? – genderbee Sep 27 '19 at 13:20
  • @genderbee Is that your intention? – François Andrieux Sep 27 '19 at 13:23
  • 1
    @genderbee - Look at it this way, would you want someone you just gave login permission to on your server to be able to shut it down? The answer is no, and it should be no. Random people on Unix systems do not have permission to shut the system down. If you are looking to shut down some other random person (or corporation)'s system without their consent, then you are asking us how to do something that's actually illegal in most jurisdictions. – Omnifarious Sep 27 '19 at 13:24
  • @Omnifarious I ask because I think it is possible to load files to server, for example via `php` script or some other method, and then run shell command. It is about safety question. – genderbee Sep 27 '19 at 13:25
  • @genderbee - Uh, huh. Pull the other one. – Omnifarious Sep 27 '19 at 13:26
  • @Omnifarious Ok, for example I have some `php` script on my server to load some files on server. When I load `run.php` and `reboot` files to the server, where `run.php` contains `shell_exec()` which run `reboot` command I ask in my question, server can be shutdowned by link to `php` script. Before some time, I had this c program and it was possible run this `command` to reboot system without root privileges. But now I am not possible to find this c program. – genderbee Sep 27 '19 at 13:33

2 Answers2

1

A desktop system with systemd and the appropriate permissions can interface to systemd from the console:

systemctl poweroff

https://wiki.debian.org/UserShutdown

Or by unsing the systemd api from c/c++

Simple C or C++ API for controlling systemd services

Wolfgang Brehm
  • 1,491
  • 18
  • 21
-2

First, don't do this. No matter how much you think you want to do it, you don't. Seriously. You're messing around with system security in a major way by doing this. These systems are difficult to get correct, even by people who know them really well. So, don't do it. If you need to give someone permission to shut down your system, figure out how to configure sudo to allow it. Don't go down the path you're going down.

If you absolutely insist on doing this, or you're just playing around on a test VM that you intend to wipe after you finish playing, learn how to set the 'setuid' bit on an executable.

Do these three commands:

$ chmod a-w name_of_executable
$ sudo chown root name_of_executable
$ sudo chmod u+s name_of_executable

that will set it up so that the program has permission to become root no matter who runs it. Needless to say, that's a very dangerous situation to create, and many things can go wrong. There are executables on your system right now that are set up this way, but they are hardened, very carefully written executables that have been extensively scoured for bugs and get fixed quickly if someone notices something wrong with them. Don't create one yourself.

Also, to solve the problem you stated in your question, the other answer is much better. I'm giving you the answer of how to make the program you posted work. You shouldn't use that program though. You should do what that other answer says.

Omnifarious
  • 54,333
  • 19
  • 131
  • 194
  • Too much 'don't do'. There is nothing wrong in principle with suid programs. In fact, they are often very useful on enterprise dev boxes. – SergeyA Sep 27 '19 at 13:35
  • @SergeyA - I was pitching to the level of experience of the person asking the question. – Omnifarious Sep 27 '19 at 16:23