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>
6 # Modified by: Ralf Jung <post@ralfj.de>
11 use File::Glob ':glob';
13 # You may configure these values to your liking. See also the section
14 # of options if you want to disable any options that rsync accepts.
15 use constant RSYNC => '/usr/bin/rsync';
18 Use 'command="$0 [-ro] SUBDIR"'
19 in front of lines in $ENV{HOME}/.ssh/authorized_keys
22 our $ro = (@ARGV && $ARGV[0] eq '-ro') ? shift : ''; # -ro = Read-Only
24 die "$0: No subdirectory specified\n$Usage" unless defined $subdir;
25 $subdir = abs_path($subdir);
26 die "$0: Restricted directory does not exist!\n" if $subdir ne '/' && !-d $subdir;
28 # The client uses "rsync -av -e ssh src/ server:dir/", and schsh makes the entire rsync call
29 # the third argument to rrsnc: rrsync (-ro)? SUBDIR COMMAND
30 # Format of the COMMAND:
31 # COMMAND=rsync --server -vlogDtpr --partial . ARG # push
32 # COMMAND=rsync --server --sender -vlogDtpr --partial . ARGS # pull
35 die "$0: Not invoked via sshd\n$Usage" unless defined $command;
36 die "$0: SSH_ORIGINAL_COMMAND='$command' is not rsync\n" unless $command =~ s/^rsync\s+//;
37 die "$0: --server option is not first\n" unless $command =~ /^--server\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 server,
44 # and only in the option format that the stock rsync produces.
46 # To disable a short-named option, add its letter to this string:
47 our $short_disabled = 's';
49 our $short_no_arg = 'ACDEHIKLORSWXbcdgklmnoprstuvxz'; # DO NOT REMOVE ANY
50 our $short_with_num = 'B'; # DO NOT REMOVE ANY
52 # To disable a long-named option, change its value to a -1. The values mean:
53 # 0 = the option has no arg; 1 = the arg doesn't need any checking; 2 = only
54 # check the arg when receiving; and 3 = always check the arg.
61 'compress-level' => 1,
63 'copy-unsafe-links' => 0,
71 'delete-excluded' => 0,
72 'delete-missing-args' => 0,
82 'ignore-existing' => 0,
83 'ignore-missing-args' => 0,
93 'no-implied-dirs' => 0,
98 'only-write-batch' => 1,
101 'remove-sent-files' => $ro ? -1 : 0,
102 'remove-source-files' => $ro ? -1 : 0,
107 'skip-compress' => 1,
118 ### END of options data produced by the cull_options script. ###
120 if ($short_disabled ne '') {
121 $short_no_arg =~ s/[$short_disabled]//go;
122 $short_with_num =~ s/[$short_disabled]//go;
124 $short_no_arg = "[$short_no_arg]" if length($short_no_arg) > 1;
125 $short_with_num = "[$short_with_num]" if length($short_with_num) > 1;
131 while ($command =~ /((?:[^\s\\]+|\\.[^\s\\]*)+)/g) {
134 push(@opts, check_arg($last_opt, $_, $check_type));
136 } elsif ($in_options) {
141 die "$0: invalid option: '-'\n" if $_ eq '-';
142 next if /^-$short_no_arg*(e\d*\.\w*)?$/o || /^-$short_with_num\d+$/o;
144 my($opt,$arg) = /^--([^=]+)(?:=(.*))?$/;
147 my $ct = $long_opt{$opt};
148 last unless defined $ct;
156 $arg = check_arg($opt, $arg, $ct);
157 $opts[-1] =~ s/=.*/=$arg/;
162 } elsif ($short_disabled ne '') {
163 $disabled = /^-$short_no_arg*([$short_disabled])/o;
167 last unless $disabled; # Generate generic failure
168 die "$0: option $opt has been disabled on this server.\n";
171 if ($subdir ne '/') {
172 # Validate args to ensure they don't try to leave our restricted dir.
176 die "$0: do not use .. in any path!\n" if m{(^|/)\\?\.\\?\.(\\?/|$)};
178 push(@args, bsd_glob($_, GLOB_LIMIT|GLOB_NOCHECK|GLOB_BRACE|GLOB_QUOTE));
181 die "$0: invalid rsync-command syntax or options\n" if $in_options;
183 @args = ( '.' ) if !@args;
185 # Note: This assumes that the rsync protocol will not be maliciously hijacked.
186 exec(RSYNC, @opts, @args) or die "exec(rsync @opts @args) failed: $? $!";
190 my($opt, $arg, $type) = @_;
191 $arg =~ s/\\(.)/$1/g;
192 if ($subdir ne '/' && ($type == 3 || ($type == 2 && !$am_sender))) {
194 die "Do not use .. in --$opt; anchor the path at the root of your restricted dir.\n"
195 if $arg =~ m{(^|/)\.\.(/|$)};
196 $arg =~ s{^/}{$subdir/};