2 # Name: /usr/local/bin/rrsync (should also have a symlink in /usr/bin)
3 # Purpose: Restricts rsync to subdirectory declared in .ssh/authorized_keys
4 # Author: Joe Smith <js-cgi@inwap.com> 30-Sep-2004
5 # Modified by: Wayne Davison <wayned@samba.org>
10 use File::Glob ':glob';
11 use constant RSYNC => 'rsync'; # Optionally set the path of rsync here.
12 use constant LOGFILE => 'rrsync.log';
14 Use 'command="$0 [-ro] SUBDIR"'
15 in front of lines in $ENV{HOME}/.ssh/authorized_keys
18 our $ro = (@ARGV && $ARGV[0] eq '-ro') ? shift : ''; # -ro = Read-Only
20 die "$0: No subdirectory specified\n$Usage" unless defined $subdir;
21 $subdir = abs_path($subdir);
22 die "$0: Restricted directory does not exist!\n" if $subdir ne '/' && !-d $subdir;
24 # The client uses "rsync -av -e ssh src/ server:dir/", and sshd on the server
25 # executes this program when .ssh/authorized_keys has 'command="..."'.
27 # command="rrsync logs/client" ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEAzGhEeNlPr...
28 # command="rrsync -ro results" ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEAmkHG1WCjC...
30 # Format of the envrionment variables set by sshd:
31 # SSH_ORIGINAL_COMMAND=rsync --server -vlogDtpr --partial . ARG # push
32 # SSH_ORIGINAL_COMMAND=rsync --server --sender -vlogDtpr --partial . ARGS # pull
33 # SSH_CONNECTION=client_addr client_port server_port
35 my $command = $ENV{SSH_ORIGINAL_COMMAND};
36 die "$0: Not invoked via sshd\n$Usage" unless defined $command;
37 die "$0: SSH_ORIGINAL_COMMAND='$command' is not rsync\n" unless $command =~ s/^rsync\s+//;
38 our $am_sender = $command =~ /^--server\s+--sender\s/; # Restrictive on purpose!
39 die "$0 -ro: sending to read-only server not allowed\n" if $ro && !$am_sender;
41 ### START of options data produced by the cull-options script. ###
43 # These options are the only options that rsync might send to the
44 # server, and only in the arg styles that the stock rsync produces.
45 our $short_no_arg = 'CDHIKLORSWbcdglnoprtuvxz';
46 our $short_with_num = 'B';
47 # To disable a short-named option, add its letter to this string:
48 our $short_disabled = '';
49 # To disable a long-named option, change its value to a 0. A value of -1
50 # means the arg doesn't need checking, a 2 means only check when receiving.
52 'copy-unsafe-links' => -1,
54 'delay-updates' => -1,
57 'delete-before' => -1,
58 'delete-during' => -1,
59 'delete-excluded' => -1,
64 'ignore-errors' => -1,
65 'ignore-existing' => -1,
68 'no-implied-dirs' => -1,
72 'remove-sent-files' => $ro ? 0 : -1,
78 our %long_with_arg = (
80 'checksum-seed' => -1,
84 'modify-window' => -1,
85 'only-write-batch' => -1,
89 our %long_before_arg = (
97 ### END of options data produced by the cull-options script. ###
99 if ($short_disabled ne '') {
100 $short_no_arg =~ s/[$short_disabled]//go;
101 $short_with_num =~ s/[$short_disabled]//go;
104 my $write_log = -f LOGFILE && open(LOG, '>>', LOGFILE);
106 chdir($subdir) or die "$0: Unable to chdir to restricted dir: $!\n";
112 foreach (split(/(?<!\\)\s+/, $command)) {
115 push(@opts, check_arg($last_opt, $_, $check_type));
117 } elsif ($in_options) {
123 next if /^-[$short_no_arg]+$/o || /^-[$short_with_num]\d+$/o;
125 my($opt,$arg) = /^--([^=]+)(?:=(.*))?$/;
128 my $ct = $long_with_arg{$opt};
130 $arg = check_arg($opt, $arg, $ct);
131 $opts[-1] =~ s/=.*/=$arg/;
134 $disabled = defined $long_with_arg{$opt};
136 } elsif (defined $opt) {
137 if (defined $long_no_arg{$opt}) {
138 next if $long_no_arg{$opt};
141 $check_type = $long_before_arg{$opt};
146 $disabled = defined $check_type;
149 } elsif ($short_disabled ne '') {
150 $disabled = /^-[$short_no_arg]*([$short_disabled])/o;
151 $opt = "-$1" if $disabled;
154 die "$0: option $opt has been disabled on this server.\n" if $disabled;
158 if ($subdir ne '/') {
159 # Validate args to ensure they don't try to leave our restricted dir.
163 die "Do not use .. in any path!\n" if m#(^|/)\\?\.\\?\.(\\?/|$)#;
165 push(@args, bsd_glob($_, GLOB_LIMIT|GLOB_NOCHECK|GLOB_BRACE|GLOB_QUOTE));
168 die "$0: invalid rsync-command syntax or options\n" if $in_options;
170 @args = ( '.' ) if !@args;
173 my ($mm,$hh) = (localtime)[1,2];
174 my $host = $ENV{SSH_CONNECTION} || 'unknown';
175 $host =~ s/ .*//; # Keep only the client's IP addr
176 $host =~ s/^::ffff://;
177 $host = gethostbyaddr(inet_aton($host),AF_INET) || $host;
178 printf LOG "%02d:%02d %-13s [%s]\n", $hh, $mm, $host, "@opts @args";
182 # Note: This assumes that the rsync protocol will not be maliciously hijacked.
183 exec(RSYNC, @opts, @args) or die "exec(rsync @opts @args) failed: $? $!";
187 my($opt, $arg, $type) = @_;
188 if ($subdir ne '/' && $type > 0 && ($type < 2 || !$am_sender)) {
190 die "Do not use .. in --$opt; anchor the path at the root of your restricted dir.\n"
191 if $arg =~ m#(^|/)\.\.(/|$)#;
192 $arg =~ s#^/#$subdir/#;