2 # This is a "local shell" command that works like a remote shell but only for
3 # the local host. See the usage message for more details.
8 use English
'-no_match_vars';
10 &Getopt
::Long
::Configure
('bundling');
11 &Getopt
::Long
::Configure
('require_order');
13 'l=s' => \
( my $login_name ),
14 '1|2|4|6|A|a|C|f|g|k|M|N|n|q|s|T|t|V|v|X|x|Y' => sub { }, # Ignore
15 'b|c|D|e|F|i|L|m|O|o|p|R|S|w=s' => sub { }, # Ignore
16 'no-cd' => \
( my $no_chdir ),
17 'sudo' => \
( my $use_sudo ),
18 'rrsync=s' => \
( my $rrsync_dir ),
19 'rropts=s' => \
( my $rrsync_opts ),
21 &usage
unless @ARGV > 1;
24 if ($host =~ s/^([^@]+)\@//) {
29 } elsif ($host ne 'localhost') {
30 die "lsh: unable to connect to host $host\n";
36 if ($login_name =~ /\D/) {
37 $uid = getpwnam($login_name);
38 die "Unknown user: $login_name\n" unless defined $uid;
42 ($login_name, $gid, $home_dir) = (getpwuid($uid))[0,3,7];
44 unshift @ARGV, "cd '$home_dir' &&" unless $no_chdir;
45 unshift @cmd, qw( sudo -H -u ), $login_name;
48 my $groups = "$gid $gid";
49 while (my ($grgid, $grmembers) = (getgrent)[2,3]) {
50 if ($grgid != $gid && $grmembers =~ /(^|\s)\Q$login_name\E(\s|$)/o) {
55 my ($ruid, $euid) = ($UID, $EUID);
56 $GID = $EGID = $groups;
58 die "Cannot set ruid: $! (use --sudo?)\n" if $UID == $ruid && $ruid != $uid;
59 die "Cannot set euid: $! (use --sudo?)\n" if $EUID == $euid && $euid != $uid;
61 $ENV{USER
} = $ENV{USERNAME
} = $login_name;
62 $ENV{HOME
} = $home_dir;
65 $home_dir = (getpwuid($UID))[7];
69 chdir $home_dir or die "Unable to chdir to $home_dir: $!\n";
73 $ENV{SSH_ORIGINAL_COMMAND
} = join(' ', @ARGV);
76 foreach my $opt (split(/[ ,]+/, $rrsync_opts)) {
77 $opt = "-$opt" unless $opt =~ /^-/;
81 push @cmd, $rrsync_dir;
83 push @cmd, '/bin/sh', '-c', "@ARGV";
86 die "Failed to exec: $!\n";
91 Usage: lsh [OPTIONS] localhost|lh COMMAND [...]
93 This is a "local shell" command that works like a remote shell but only for the
94 local host. This is useful for rsync testing or for running a local copy where
95 the sender and the receiver need to use different options (e.g. --fake-super).
99 -l USER Choose the USER that lsh tries to become.
100 --no-cd Skip the chdir \$HOME (the default with hostname "lh")
101 --sudo Use sudo -H -l USER to become root or the specified USER.
102 --rrsync=DIR Test rrsync restricted copying without using ssh.
103 --rropts=STR The string "munge,no-del,no-lock" would pass 3 options to
104 rrsync (must be combined with --rrsync=DIR).
106 The script also ignores a bunch of single-letter ssh options.