[Za-pm] Perl/Tk As Root

Nick Cleaton nick at cleaton.net
Fri May 16 08:47:48 CDT 2003


On Fri, May 16, 2003 at 12:42:15PM +0200, DIRK KONRAD wrote:
> 
> I've got this Perl/Tk script/app that does all sorts of magick. But I
> first have to su to root and then run it as root from a shell.(It
> manipulates some config files)
> 
> How can I go about doing it the way Mandrake does it, where if the
> user clicks on the icon, it will pop-up a ?root password? screen with
> a message, and then, if successful, will run that script as root, but
> still staying away from SUID.
> 
> If this app is to have mass appeal, this, for me is a must!
> 
> I Dont want to pop up a message telling the user ? Please open a
> shell, SU to root and then type in ?/opt/foo/bar.pl?
> 
> Much easier to tell them ?password Please? <enter> and off you go.

The only way I can see is to have two scripts: one to get the root
password from the user and invoke the 'su' command to run the other
script as root.

You can use the 'Expect' perl module to interact with the 'su' command,
so the first script might look something like this:

---- cut here ----
#!/usr/local/bin/perl -w
use strict;

use Tk;
use Expect;
$Expect::Log_Stdout = 0;

my $main = MainWindow->new;

$main->Label(-text => 'root password')->pack;

my $pw = $main->Entry(-show => '*');
$pw->pack;

$main->Button(-text => 'OK',
              -command => \&ok,
             )->pack;

MainLoop;

sub ok {
    run_as_root($pw->get, '/opt/foo/bar.pl');
    $main->destroy;
}

sub run_as_root {
    my ($rootpw, $command) = @_;

    my $exp = Expect->spawn('su', '-m', 'root', '-c', $command)
        or die "spawn $command: $!";

    $exp->expect(5, 'word:') or die 'no password prompt';
    $exp->send("$rootpw\r");

    $exp->soft_close and die 'su failed';
}

---- cut here ----

--
Nick Cleaton
nick at cleaton.net



More information about the Za-pm mailing list