2 # This script can be used as a "remote shell" command that is only
3 # capable of pretending to connect to "localhost". This is useful
4 # for testing or for running a local copy where the sender and the
5 # receiver needs to use different options (e.g. --fake-super). If
6 # we get -l USER, we try to become the USER, either directly (must
7 # be root) or by using "sudo -H -u USER" (requires --sudo option).
12 use English
'-no_match_vars';
14 &Getopt
::Long
::Configure
('bundling');
15 &Getopt
::Long
::Configure
('require_order');
17 'l=s' => \
( my $login_name ),
18 '1|2|4|6|A|a|C|f|g|k|M|N|n|q|s|T|t|V|v|X|x|Y' => sub { }, # Ignore
19 'b|c|D|e|F|i|L|m|O|o|p|R|S|w=s' => sub { }, # Ignore
20 'no-cd' => \
( my $no_chdir ),
21 'sudo' => \
( my $use_sudo ),
23 &usage
unless @ARGV > 1;
26 if ($host =~ s/^([^@]+)\@//) {
31 } elsif ($host ne 'localhost') {
32 die "lsh: unable to connect to host $host\n";
38 if ($login_name =~ /\D/) {
39 $uid = getpwnam($login_name);
40 die "Unknown user: $login_name\n" unless defined $uid;
44 ($login_name, $gid, $home_dir) = (getpwuid($uid))[0,3,7];
46 unshift @ARGV, "cd '$home_dir' &&" unless $no_chdir;
47 unshift @cmd, qw( sudo -H -u ), $login_name;
50 my $groups = "$gid $gid";
51 while (my ($grgid, $grmembers) = (getgrent)[2,3]) {
52 if ($grgid != $gid && $grmembers =~ /(^|\s)\Q$login_name\E(\s|$)/o) {
57 my ($ruid, $euid) = ($UID, $EUID);
58 $GID = $EGID = $groups;
60 die "Cannot set ruid: $! (use --sudo?)\n" if $UID == $ruid && $ruid != $uid;
61 die "Cannot set euid: $! (use --sudo?)\n" if $EUID == $euid && $euid != $uid;
63 $ENV{USER
} = $ENV{USERNAME
} = $login_name;
64 $ENV{HOME
} = $home_dir;
67 $home_dir = (getpwuid($UID))[7];
71 chdir $home_dir or die "Unable to chdir to $home_dir: $!\n";
74 push @cmd, '/bin/sh', '-c', "@ARGV";
76 die "Failed to exec: $!\n";
81 Usage: lsh [-l user] [--sudo] [--no-cd] localhost COMMAND [...]
83 Note that if you pass hostname "lh" instead of "localhost" that
84 the --no-cd option is implied.