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>
7 # Modified by: Ralf Jung <post@ralfj.de>
8 # This file is taken from the Debian rsync package, where it resides in
9 # </usr/share/doc/rsync/scripts/rrsync.gz>. According to the Debian package
10 # copyright information, it is available under GPL-3. Quoting from that information:
14 # > Copyright (C) 1996-2011 by Andrew Tridgell, Wayne Davison, and others.
16 # > Rsync was originally written by Andrew Tridgell and is currently
17 # > maintained by Wayne Davison. It has been improved by many developers
18 # > from around the world.
20 # > Rsync may be used, modified and redistributed only under the terms of
21 # > the GNU General Public License, found in the file:
23 # > /usr/share/common-licenses/GPL-3
25 # > on Debian systems, or at
27 # > http://www.fsf.org/licensing/licenses/gpl.html
28 # You can also find the license text of GPL-3 in the LICENSE-GPL3 file that
35 use File::Glob ':glob';
37 # You may configure these values to your liking. See also the section
38 # of options if you want to disable any options that rsync accepts.
39 use constant RSYNC => '/usr/bin/rsync';
42 Use 'command="$0 [-ro] SUBDIR"'
43 in front of lines in $ENV{HOME}/.ssh/authorized_keys
46 our $ro = (@ARGV && $ARGV[0] eq '-ro') ? shift : ''; # -ro = Read-Only
48 die "$0: No subdirectory specified\n$Usage" unless defined $subdir;
49 $subdir = abs_path($subdir);
50 die "$0: Restricted directory does not exist!\n" if $subdir ne '/' && !-d $subdir;
52 # The client uses "rsync -av -e ssh src/ server:dir/", and schsh makes the entire rsync call
53 # the third argument to rrsnc: rrsync (-ro)? SUBDIR COMMAND
54 # Format of the COMMAND:
55 # COMMAND=rsync --server -vlogDtpr --partial . ARG # push
56 # COMMAND=rsync --server --sender -vlogDtpr --partial . ARGS # pull
59 die "$0: Not invoked via sshd\n$Usage" unless defined $command;
60 die "$0: SSH_ORIGINAL_COMMAND='$command' is not rsync\n" unless $command =~ s/^rsync\s+//;
61 die "$0: --server option is not first\n" unless $command =~ /^--server\s/;
62 our $am_sender = $command =~ /^--server\s+--sender\s/; # Restrictive on purpose!
63 die "$0 -ro: sending to read-only server not allowed\n" if $ro && !$am_sender;
65 ### START of options data produced by the cull_options script. ###
67 # These options are the only options that rsync might send to the server,
68 # and only in the option format that the stock rsync produces.
70 # To disable a short-named option, add its letter to this string:
71 our $short_disabled = 's';
73 our $short_no_arg = 'ACDEHIKLORSWXbcdgklmnoprstuvxz'; # DO NOT REMOVE ANY
74 our $short_with_num = 'B'; # DO NOT REMOVE ANY
76 # To disable a long-named option, change its value to a -1. The values mean:
77 # 0 = the option has no arg; 1 = the arg doesn't need any checking; 2 = only
78 # check the arg when receiving; and 3 = always check the arg.
85 'compress-level' => 1,
87 'copy-unsafe-links' => 0,
95 'delete-excluded' => 0,
96 'delete-missing-args' => 0,
105 'ignore-errors' => 0,
106 'ignore-existing' => 0,
107 'ignore-missing-args' => 0,
116 'modify-window' => 1,
117 'no-implied-dirs' => 0,
122 'only-write-batch' => 1,
125 'remove-sent-files' => $ro ? -1 : 0,
126 'remove-source-files' => $ro ? -1 : 0,
131 'skip-compress' => 1,
142 ### END of options data produced by the cull_options script. ###
144 if ($short_disabled ne '') {
145 $short_no_arg =~ s/[$short_disabled]//go;
146 $short_with_num =~ s/[$short_disabled]//go;
148 $short_no_arg = "[$short_no_arg]" if length($short_no_arg) > 1;
149 $short_with_num = "[$short_with_num]" if length($short_with_num) > 1;
155 while ($command =~ /((?:[^\s\\]+|\\.[^\s\\]*)+)/g) {
158 push(@opts, check_arg($last_opt, $_, $check_type));
160 } elsif ($in_options) {
165 die "$0: invalid option: '-'\n" if $_ eq '-';
166 next if /^-$short_no_arg*(e\d*\.\w*)?$/o || /^-$short_with_num\d+$/o;
168 my($opt,$arg) = /^--([^=]+)(?:=(.*))?$/;
171 my $ct = $long_opt{$opt};
172 last unless defined $ct;
180 $arg = check_arg($opt, $arg, $ct);
181 $opts[-1] =~ s/=.*/=$arg/;
186 } elsif ($short_disabled ne '') {
187 $disabled = /^-$short_no_arg*([$short_disabled])/o;
191 last unless $disabled; # Generate generic failure
192 die "$0: option $opt has been disabled on this server.\n";
195 if ($subdir ne '/') {
196 # Validate args to ensure they don't try to leave our restricted dir.
200 die "$0: do not use .. in any path!\n" if m{(^|/)\\?\.\\?\.(\\?/|$)};
202 push(@args, bsd_glob($_, GLOB_LIMIT|GLOB_NOCHECK|GLOB_BRACE|GLOB_QUOTE));
205 die "$0: invalid rsync-command syntax or options\n" if $in_options;
207 @args = ( '.' ) if !@args;
209 # Note: This assumes that the rsync protocol will not be maliciously hijacked.
210 exec(RSYNC, @opts, @args) or die "exec(rsync @opts @args) failed: $? $!";
214 my($opt, $arg, $type) = @_;
215 $arg =~ s/\\(.)/$1/g;
216 if ($subdir ne '/' && ($type == 3 || ($type == 2 && !$am_sender))) {
218 die "Do not use .. in --$opt; anchor the path at the root of your restricted dir.\n"
219 if $arg =~ m{(^|/)\.\.(/|$)};
220 $arg =~ s{^/}{$subdir/};