Qt Shutdown

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 1386 Views - Last Post: 06 September 2012 - 08:28 AM Rate Topic: -----

#16 jimblumberg  Icon User is online

  • member icon

Reputation: 3112
  • View blog
  • Posts: 9,490
  • Joined: 25-December 09

Re: Qt Shutdown

Posted 04 September 2012 - 12:27 PM

Quote

I'll assume that halt requires root privileges just like shutdown does

Fine, but as I said halt on my system does not require root privileges.

Also you may want to look into using the -a parameter with shutdown. See this link: shutdown and then possibly add your user to the /etc/shutdown.allow file.

Jim
Was This Post Helpful? 0
  • +
  • -

#17 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 2037
  • View blog
  • Posts: 6,066
  • Joined: 05-May 12

Re: Qt Shutdown

Posted 04 September 2012 - 12:38 PM

This is a Windows guy asking this silly question so feel free to spank my hands and say "No, that is not the Linux away. Linux believes in chaining together utilities to do an operation, not big monolith applications."

Since Linux is open source, I would presume that you can see the source code for the shutdown command. Why not crib enough of the source that makes the appropriate kill() API calls? Presumably, the appropriate API calls also need to be cribbed from sudo to get higher privileges in order to call the kill APIs.
Was This Post Helpful? 0
  • +
  • -

#18 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1716
  • View blog
  • Posts: 2,592
  • Joined: 21-June 11

Re: Qt Shutdown

Posted 04 September 2012 - 12:38 PM

View Postjimblumberg, on 04 September 2012 - 09:27 PM, said:

Fine, but as I said halt on my system does not require root privileges.


On my system (and, I imagine, most systems) it does. So I'd say TwoOfDiamonds is right not to assume anything.

Quote

Also you may want to look into using the -a parameter with shutdown. See this link: shutdown and then possibly add your user to the /etc/shutdown.allow file.


That doesn't allow non-root users to call shutdown. All it does is prevent users who aren't in the file from using Ctrl-Alt-Del to shutdown the computer. From the manpage you linked:

Quote

shutdown can be called from init(8) when the magic keys CTRL-ALT-DEL are pressed, by creating an appropriate entry in /etc/inittab. This means that everyone who has physical access to the console keyboard can shut the system down. To prevent this, shutdown can check to see if an authorized user is logged in on one of the virtual consoles. If shutdown is called with the -a argument (add this to the invocation of shutdown in /etc/inittab), it checks to see if the file /etc/shutdown.allow is present. It then compares the login names in that file with the list of people that are logged in on a virtual console (from /var/run/utmp). Only if one of those authorized users or root is logged in, it will proceed.

Was This Post Helpful? 0
  • +
  • -

#19 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1716
  • View blog
  • Posts: 2,592
  • Joined: 21-June 11

Re: Qt Shutdown

Posted 04 September 2012 - 12:43 PM

View PostSkydiver, on 04 September 2012 - 09:38 PM, said:

Since Linux is open source, I would presume that you can see the source code for the shutdown command. Why not crib enough of the source that makes the appropriate kill() API calls? Presumably, the appropriate API calls also need to be cribbed from sudo to get higher privileges in order to call the kill APIs.


You can't do what shutdown or sudo do without root privileges. If you copied the entire source of shutdown into your application (minus the part that exits with an error message if you're not root of course), the code would simply fail because of insufficient permissions.
Was This Post Helpful? 0
  • +
  • -

#20 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 2037
  • View blog
  • Posts: 6,066
  • Joined: 05-May 12

Re: Qt Shutdown

Posted 04 September 2012 - 12:47 PM

Yup, I understand that part, but can't you also do the appropriate set user id (or equivalent) API call to elevate yourself before calling kill() ?

This post has been edited by Skydiver: 04 September 2012 - 12:47 PM

Was This Post Helpful? 0
  • +
  • -

#21 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1716
  • View blog
  • Posts: 2,592
  • Joined: 21-June 11

Re: Qt Shutdown

Posted 04 September 2012 - 01:13 PM

You also can't use setuid() and co. to change your user id if you're not already root. If you could, anybody with access to a C compiler could gain root access.

The way that sudo works is that the executable's owner is root and its setuid permission bit is set. This means that when the executable is run, it executes with the privileges of its owner (root), not with those of the user who invoked it (as would usually be the case). Since you can't make root the owner of a file unless you are root, this can't be abused the way that an unrestricted API call could be.

And yes, TwoOfDiamonds could make his application setuid, but as I mentioned in one of my earlier posts, that would probably be a bad idea. Generally you don't want many setuid applications lying around on your system - especially not ones you aren't sure are 100% bug-free.
Was This Post Helpful? 1
  • +
  • -

#22 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 2037
  • View blog
  • Posts: 6,066
  • Joined: 05-May 12

Re: Qt Shutdown

Posted 04 September 2012 - 01:18 PM

Thanks! That was the key piece of information I was missing: "You also can't use setuid() and co. to change your user id if you're not already root."
Was This Post Helpful? 0
  • +
  • -

#23 TwoOfDiamonds  Icon User is offline

  • D.I.C Head

Reputation: 49
  • View blog
  • Posts: 245
  • Joined: 27-July 12

Re: Qt Shutdown

Posted 04 September 2012 - 11:23 PM

Good morning , guys and gals !

I was thinking , is there any linux command that will autehnticate the user for the whole session?

For example , the terminal asks the user for his/her password and then the user inputs it and the system won't request the password for anything for the rest of the session :)
I think there should be something like that since as far as I saw in my not-whole-1-day-linux-experience when you want to install something from the terminal it will request the password for the first install and the next ones will bypass the password request .
Was This Post Helpful? 0
  • +
  • -

#24 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1716
  • View blog
  • Posts: 2,592
  • Joined: 21-June 11

Re: Qt Shutdown

Posted 05 September 2012 - 04:01 AM

View PostTwoOfDiamonds, on 05 September 2012 - 08:23 AM, said:

I was thinking , is there any linux command that will autehnticate the user for the whole session?


Not as far as I know.

Quote

I think there should be something like that since as far as I saw in my not-whole-1-day-linux-experience when you want to install something from the terminal it will request the password for the first install and the next ones will bypass the password request .


That's a feature of sudo and it only lasts a couple of minutes - not for the rest of the session.
Was This Post Helpful? 0
  • +
  • -

#25 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5723
  • View blog
  • Posts: 22,637
  • Joined: 23-August 08

Re: Qt Shutdown

Posted 05 September 2012 - 04:38 AM

If you require root privileges on install, you could add a sudoers entry with NOPASSWD for the shutdown command, which would forgo the password prompt on that user issuing a shutdown. Downside is that it's not limited to your app.
Was This Post Helpful? 0
  • +
  • -

#26 TwoOfDiamonds  Icon User is offline

  • D.I.C Head

Reputation: 49
  • View blog
  • Posts: 245
  • Joined: 27-July 12

Re: Qt Shutdown

Posted 06 September 2012 - 07:53 AM

Sorry to reopen the post , but I found the sollution thanks to everyone who posted here so I thought I should share it:

system ("echo \"password\" | sudo -S shutdown -P now");


will do exactly what I want :) (password will be replaced by the password that the user will input)
Was This Post Helpful? 0
  • +
  • -

#27 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1716
  • View blog
  • Posts: 2,592
  • Joined: 21-June 11

Re: Qt Shutdown

Posted 06 September 2012 - 07:56 AM

This will break if the password contains double quotes or other meta characters that still have special meaning inside double quotes (which would be most of them - including dollar signs).

Using QProcess seems like a much more robust approach.
Was This Post Helpful? 1
  • +
  • -

#28 TwoOfDiamonds  Icon User is offline

  • D.I.C Head

Reputation: 49
  • View blog
  • Posts: 245
  • Joined: 27-July 12

Re: Qt Shutdown

Posted 06 September 2012 - 08:28 AM

Oh , thanks for pointing that out :)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2