X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FObject%2FRemote%2FConnector%2FLocalSudo.pm;h=043b688bf378fbf417aecc5f9ad0c80a476b1e00;hb=a6786ddab559c869f448c1cb963e83faeb5efd40;hp=1bd6d6e7c26182dcf9aa8209f667fea1a08bbb62;hpb=859f44514ab7fa00b25a56d72dafc802720eec33;p=scpubgit%2FObject-Remote.git diff --git a/lib/Object/Remote/Connector/LocalSudo.pm b/lib/Object/Remote/Connector/LocalSudo.pm index 1bd6d6e..043b688 100644 --- a/lib/Object/Remote/Connector/LocalSudo.pm +++ b/lib/Object/Remote/Connector/LocalSudo.pm @@ -1,20 +1,34 @@ package Object::Remote::Connector::LocalSudo; use Symbol qw(gensym); +use Module::Runtime qw(use_module); use IPC::Open3; use Moo; extends 'Object::Remote::Connector::Local'; -has password_callback => (is => 'ro'); +has target_user => (is => 'ro', required => 1); -sub _sudo_perl_command { - my ($self, $target_user) = @_; +has password_callback => (is => 'lazy'); + +sub _build_password_callback { + my ($self) = @_; + my $pw_prompt = use_module('Object::Remote::Prompt')->can('prompt_pw'); + my $user = $self->target_user; + return sub { + $pw_prompt->("sudo password for ${user}", undef, { cache => 1 }) + } +} + +has sudo_perl_command => (is => 'lazy'); + +sub _build_sudo_perl_command { + my ($self) = @_; return - 'sudo', '-S', '-u', $target_user, '-p', "[sudo] password please\n", + 'sudo', '-S', '-u', $self->target_user, '-p', "[sudo] password please\n", 'perl', '-MPOSIX=dup2', '-e', 'print STDERR "GO\n"; exec(@ARGV);', - $self->_perl_command($target_user); + $self->perl_command; } sub _start_perl { @@ -24,7 +38,7 @@ sub _start_perl { my $foreign_stdin, my $foreign_stdout, $sudo_stderr, - $self->_sudo_perl_command(@_) + @{$self->sudo_perl_command} ) or die "open3 failed: $!"; chomp(my $line = <$sudo_stderr>); if ($line eq "GO") { @@ -35,11 +49,14 @@ sub _start_perl { unless $cb; print $foreign_stdin $cb->($line, @_), "\n"; chomp($line = <$sudo_stderr>); - die "sent password and expected newline from sudo, got ${line}" - if $line; - chomp($line = <$sudo_stderr>); - die "sent password but next line was ${line}" - unless $line eq "GO"; + if ($line and $line ne 'GO') { + die "sent password and expected newline from sudo, got ${line}"; + } + elsif (not $line) { + chomp($line = <$sudo_stderr>); + die "sent password but next line was ${line}" + unless $line eq "GO"; + } } else { die "Got inexplicable line ${line} trying to sudo"; }; @@ -67,7 +84,7 @@ push @Object::Remote::Connection::Guess, sub { for ($_[0]) { # username followed by @ if (defined and !ref and /^ ([^\@]*?) \@ $/x) { - return __PACKAGE__->new->connect($1); + return __PACKAGE__->new(target_user => $1); } } return;