gri6507 has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks, I am writing a perl program targeted only for Linux installations. One of this program's functions is to do something that requires root priviledges.

Since this program is always started as a regular non-priviledged user, is there a safe, reliable way to prompt the user for the root password, attempt to switch to root and, if successful, do the the necessary task, exit from root, and then continue running as normal user?

Replies are listed 'Best First'.
Re: getting root permissions via program
by stiller (Friar) on Feb 25, 2008 at 13:25 UTC
    I really think you should use sudo for that, and not from within your program, but make a sudo entry to allow running of your program which is otherwise owned and executable by root.

      Or make a small helper utility which does the bare minimum of work that requires elevated privileges and run that via sudo. But seconded; you really don't want to try and do it yourself if you can get away with off the shellf. Just let sudo do the heavy lifting for you (something that's its specialized in; not to mention it's high profile and will have way more eyeballs scrutinizing it than your code would).

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

        One rather important point is that one would not want to make users accustomed to give root passwords to arbitrary programs asking for it.
      ok, I can go with that. In that case, my question becomes how can I test to see that my program was started via sudo? If I'm not mistaken, sudo does not check UID?
        If your program is owned by root, and only executable by root, then no one will be allowed to run it unless they are root, or use sudo, so you don't really need to check further.