Commit 4f266701 authored by Leigh Stoller's avatar Leigh Stoller

Add ExecuteAs() to run a system command as a user and flip back.

parent 51a6d136
...@@ -1887,6 +1887,8 @@ sub GList($$) ...@@ -1887,6 +1887,8 @@ sub GList($$)
print STDERR "*** Unexpected results from 'id -G $user_uid': $glist\n"; print STDERR "*** Unexpected results from 'id -G $user_uid': $glist\n";
return undef; return undef;
} }
return $glist
if (!defined($default));
# #
# Remove current group from glist, then add gid twice at the front # Remove current group from glist, then add gid twice at the front
...@@ -1973,6 +1975,35 @@ sub FlipTo($$) ...@@ -1973,6 +1975,35 @@ sub FlipTo($$)
return 0; return 0;
} }
#
# Execute a command as user and then return to original.
#
sub ExecuteAs($$;$)
{
my ($self, $command, $default_gid) = @_;
my $current_uid = $UID;
my $current_euid = $EUID;
my $current_gid = $GID;
my $current_egid = $EGID;
my $USER = $ENV{'USER'};
my $LOGNAME = $ENV{'LOGNAME'};
$self->FlipTo($default_gid);
my $output = emutil::ExecQuiet($command);
my $status = $?;
$EUID = 0;
$GID = $current_gid;
$EGID = $current_egid;
$UID = $current_uid;
$EUID = $current_euid;
$ENV{'USER'} = $USER;
$ENV{'LOGNAME'} = $LOGNAME;
return ($status, $output);
}
# #
# Update aggregate stats. # Update aggregate stats.
# #
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment