| 1 | package Psh::Builtins::Sudo; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | =item * C<sudo> | ||
| 6 | |||
| 7 | Wrapper around the sudo program which sets $ENV{PATH}, | ||
| 8 | expands command aliases and defers shell globbing | ||
| 9 | until sudo runs. | ||
| 10 | |||
| 11 | =cut | ||
| 12 | |||
| 13 | sub bi_sudo { | ||
| 14 | |||
| 15 | # Fix up $ENV{PATH} for root commands: | ||
| 16 | # Replace with sudo_replace_path (if set) | ||
| 17 | # Add any elements in sudo_add_path (if set) | ||
| 18 | |||
| 19 | my $path_sep = Psh::Options::get_option('array_exports')->{'path'}; | ||
| 20 | |||
| 21 | my $path = Psh::Options::get_option('path'); | ||
| 22 | my $add_path = Psh::Options::get_option('sudo_add_path'); | ||
| 23 | my $replace_path = Psh::Options::get_option('sudo_replace_path'); | ||
| 24 | |||
| 25 | @$path = @$replace_path if $replace_path and ref $replace_path eq 'ARRAY'; | ||
| 26 | push @$path, @$add_path if $add_path and ref $add_path eq 'ARRAY'; | ||
| 27 | |||
| 28 | local $ENV{'PATH'} = join $path_sep, @$path; | ||
| 29 | |||
| 30 | |||
| 31 | # We run sudo through a shell, in order to defer shell expansions until | ||
| 32 | # we are running as root. We could use any shell here (including psh) | ||
| 33 | # but psh takes quite a long time to start up, so the default is /bin/sh | ||
| 34 | |||
| 35 | my $sudo_shell = Psh::Options::get_option('sudo_shell'); | ||
| 36 | $sudo_shell ||= '/bin/sh -c'; | ||
| 37 | my @sudo_shell = split /\s+/, $sudo_shell; | ||
| 38 | |||
| 39 | |||
| 40 | # Parse the command line to expand the command (e.g. aliases) | ||
| 41 | |||
| 42 | my @result = Psh::Parser::parse_line($_[0], 'executable'); | ||
| 43 | |||
| 44 | my $cmd = $result[0][2][4]; | ||
| 45 | |||
| 46 | # Expand the 'sudo' command itself (in case it is also an alias) | ||
| 47 | |||
| 48 | @result = Psh::Parser::parse_line('sudo', 'executable'); | ||
| 49 | |||
| 50 | # We want the sudo command as an array of words (for passing to system) | ||
| 51 | my @sudo_cmd = split /\s+/, $result[0][2][4]; | ||
| 52 | |||
| 53 | system(@sudo_cmd, @sudo_shell, $cmd); | ||
| 54 | } | ||
| 55 | |||
| 56 | 1; | ||
| 57 |