Improve the options info a bit more.
[rsync.git] / support / lsh
blob40f70c88b0889b8904bc25606c4df2b5e78fdab3
1 #!/usr/bin/env perl
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).
9 use strict;
10 use warnings;
11 use Getopt::Long;
12 use English '-no_match_vars';
14 &Getopt::Long::Configure('bundling');
15 &Getopt::Long::Configure('require_order');
16 GetOptions(
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 ),
22 ) or &usage;
23 &usage unless @ARGV > 1;
25 my $host = shift;
26 if ($host =~ s/^([^@]+)\@//) {
27 $login_name = $1;
29 if ($host eq 'lh') {
30 $no_chdir = 1;
31 } elsif ($host ne 'localhost') {
32 die "lsh: unable to connect to host $host\n";
35 my ($home_dir, @cmd);
36 if ($login_name) {
37 my ($uid, $gid);
38 if ($login_name =~ /\D/) {
39 $uid = getpwnam($login_name);
40 die "Unknown user: $login_name\n" unless defined $uid;
41 } else {
42 $uid = $login_name;
44 ($login_name, $gid, $home_dir) = (getpwuid($uid))[0,3,7];
45 if ($use_sudo) {
46 unshift @ARGV, "cd '$home_dir' &&" unless $no_chdir;
47 unshift @cmd, qw( sudo -H -u ), $login_name;
48 $no_chdir = 1;
49 } else {
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) {
53 $groups .= " $grgid";
57 my ($ruid, $euid) = ($UID, $EUID);
58 $GID = $EGID = $groups;
59 $UID = $EUID = $uid;
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;
66 } else {
67 $home_dir = (getpwuid($UID))[7];
70 unless ($no_chdir) {
71 chdir $home_dir or die "Unable to chdir to $home_dir: $!\n";
74 push @cmd, '/bin/sh', '-c', "@ARGV";
75 exec @cmd;
76 die "Failed to exec: $!\n";
78 sub usage
80 die <<EOT;
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.
85 EOT