2 # Name: /usr/local/bin/rrsync Author: Joe Smith <js-cgi@inwap.com> 30-Sep-2004
3 # Purpose: Restricts rsync to subdirectory declared in .ssh/authorized_keys
4 # (should have a symlink in /usr/bin)
7 use constant LOGFILE => 'rrsync.log';
9 Use 'command="$0 [-ro] subdir"'
10 in front of lines in $ENV{HOME}/.ssh/authorized_keys
13 my $ro = (@ARGV and $ARGV[0] eq '-ro') ? shift : ''; # -ro = Read-Only
15 die "No subdirectory specified\n$Usage" unless defined $subdir;
17 # The client uses "rsync -av -e ssh src/ server:dir/", and sshd on the server
18 # executes this program when .ssh/authorized_keys has 'command="..."'.
20 # command="rrsync logs/client" ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEAzGhEeNlPr...
21 # command="rrsync -ro results" ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEAmkHG1WCjC...
23 # Format of the envrionment variables set by sshd:
24 # SSH_ORIGINAL_COMMAND=rsync --server -vlogDtpr --partial . dir # push
25 # SSH_ORIGINAL_COMMAND=rsync --server --sender -vlogDtpr --partial . dir # pull
26 # SSH_CLIENT=client_addr client_port server_port
28 my $command = $ENV{SSH_ORIGINAL_COMMAND};
29 die "Not invoked via sshd\n$Usage" unless defined $command;
31 my ($cmd,$dir) = $command =~ /(.* \.) ?(.*)/;
32 die "SSH_ORIGINAL_COMMAND='$command' is not rsync\n" unless $cmd =~ /^rsync\s/;
33 die "$0 -ro: sending to read-only directory $dir not allowed\n"
34 if $ro and $cmd !~ /^rsync --server --sender /;
37 $dir = $subdir if $dir eq ''; # Use subdir instead of $HOME
38 $dir =~ s%^/%%; # Don't allow absolute paths
39 $dir = "$subdir/$dir" unless $dir eq $subdir or $dir =~ m%^\Q$subdir/%;
40 $dir =~ s%/\.\.(?=/)%__%g; # Don't allow foo/../../etc
41 $dir =~ tr|-_/a-zA-Z0-9.,|_|c; # Don't allow ;|][}{*?
43 if (-f LOGFILE and open LOG,'>>',LOGFILE) {
44 my ($mm,$hh) = (localtime)[1,2];
45 my $host = $ENV{SSH_CLIENT} || 'unknown';
46 $host =~ s/ .*//; # Keep only the client's IP addr
47 $host = gethostbyaddr(inet_aton($host),AF_INET) || $host;
48 $_ = sprintf "%-13s",$host;
49 print LOG "$hh:$mm $_ [$command] =",($dir eq $orig ? " OK" : "> $dir"),"\n";
53 exec "$cmd $dir" or die "exec($cmd $dir) failed: $? $!";
54 # Note: This assumes that the rsync protocol will not be maliciously hijacked.