views:

387

answers:

5

I have a Qt 4 GUI where I need to have a option in a drop-down menu that allows the user to choose to restart the computer. I realize this might seem redunant with the ability to restart the computer in other ways, but the choice needs to stay there. I've tried using system() to call the following:

  1. a suid-root shell script
  2. a non-suid shell script
  3. a suid-root binary program

and all of them just cause

reboot: must be superuser
to be printed. Using system() to call reboot directly does the same thing. I'm not especially attached to using system() to do this, but it seemed like the most direct choice.

How can I reboot the system from the GUI?

+2  A: 

That user is able to reboot machine normally from outside this app? I doubt that.

Zhinkaas
This belongs in a comment -- oh wait, you cant -- +1 then :)
Billy ONeal
They can reboot the machine with the "Restart Computer" option in the KDE (or GNOME, it doesn't matter which) session termination GUI.
Dave K
This is pure conjecture, but it's possible that that works by ending the gnome/kde process with an exit status that is acted on by kdm/gdm. In other words they may not be using suid to do this.
intuited
+4  A: 

Have you tried running a shell script, using gksudo? Something like

gksudo shutdown -r

With any luck, that should pull up a modal dialogue to get user credentials.

Ink-Jet
Well, you don't really need to run it as a shell script, just `execv` it, or if you really want the PATH functionality that comes along with shell scripts, `execvp`.
Jefromi
A: 

In binary try to call

setuid (0);

before system() call.

el.pescado
Thanks for the suggestion, but this didn't work when the program was called from another program. It does work if a program is called directly from the command line though.
Dave K
A: 

how would you reboot the system from the command line on your system?

basically do

system( <however you wouuld do it from the command line> );
frankster
Thanks for the suggestion, but that's what we've tried, and it doesn't work.
Dave K
A: 

suid-ing shell scripts is just dangerous as already mentioned (which is why that didn't work).

I suspect that suid-ing the binary doesn't work because system spawns its subprocess with the user's actual uid and not the suid one, again for security reasons (it would let you substitute any binary for the one being called and run it as root).

You could put a copy of reboot in a location protected such that only users you want have permission to can execute it, and then suid-root THAT.

Alternately give them sudoer privilege to execute JUST the command you care about and system out to something like "ksh -c 'sudo reboot'"

Mark B