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';
12 # You may configure these values to your liking. See also the section
13 # of options if you want to disable any options that rsync accepts.
14 use constant RSYNC => '/usr/bin/rsync';
15 use constant LOGFILE => 'rrsync.log';
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 sshd on the server
29 # executes this program when .ssh/authorized_keys has 'command="..."'.
31 # command="rrsync logs/client" ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEAzGhEeNlPr...
32 # command="rrsync -ro results" ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEAmkHG1WCjC...
34 # Format of the environment variables set by sshd:
35 # SSH_ORIGINAL_COMMAND=rsync --server -vlogDtpr --partial . ARG # push
36 # SSH_ORIGINAL_COMMAND=rsync --server --sender -vlogDtpr --partial . ARGS # pull
37 # SSH_CONNECTION=client_addr client_port server_port
39 my $command = $ENV{SSH_ORIGINAL_COMMAND};
40 die "$0: Not invoked via sshd\n$Usage" unless defined $command;
41 die "$0: SSH_ORIGINAL_COMMAND='$command' is not rsync\n" unless $command =~ s/^rsync\s+//;
42 die "$0: --server option is not first\n" unless $command =~ /^--server\s/;
43 our $am_sender = $command =~ /^--server\s+--sender\s/; # Restrictive on purpose!
44 die "$0 -ro: sending to read-only server not allowed\n" if $ro && !$am_sender;
46 ### START of options data produced by the cull_options script. ###
48 # These options are the only options that rsync might send to the server,
49 # and only in the option format that the stock rsync produces.
51 # To disable a short-named option, add its letter to this string:
52 our $short_disabled = 's';
54 our $short_no_arg = 'ACDEHIKLORSWXbcdgklmnoprstuvxz'; # DO NOT REMOVE ANY
55 our $short_with_num = 'B'; # DO NOT REMOVE ANY
57 # To disable a long-named option, change its value to a -1. The values mean:
58 # 0 = the option has no arg; 1 = the arg doesn't need any checking; 2 = only
59 # check the arg when receiving; and 3 = always check the arg.
66 'compress-level' => 1,
68 'copy-unsafe-links' => 0,
76 'delete-excluded' => 0,
77 'delete-missing-args' => 0,
87 'ignore-existing' => 0,
88 'ignore-missing-args' => 0,
98 'no-implied-dirs' => 0,
103 'only-write-batch' => 1,
106 'remove-sent-files' => $ro ? -1 : 0,
107 'remove-source-files' => $ro ? -1 : 0,
112 'skip-compress' => 1,
123 ### END of options data produced by the cull_options script. ###
125 if ($short_disabled ne '') {
126 $short_no_arg =~ s/[$short_disabled]//go;
127 $short_with_num =~ s/[$short_disabled]//go;
129 $short_no_arg = "[$short_no_arg]" if length($short_no_arg) > 1;
130 $short_with_num = "[$short_with_num]" if length($short_with_num) > 1;
132 my $write_log = -f LOGFILE && open(LOG, '>>', LOGFILE);
134 chdir($subdir) or die "$0: Unable to chdir to restricted dir: $!\n";
140 while ($command =~ /((?:[^\s\\]+|\\.[^\s\\]*)+)/g) {
143 push(@opts, check_arg($last_opt, $_, $check_type));
145 } elsif ($in_options) {
150 die "$0: invalid option: '-'\n" if $_ eq '-';
151 next if /^-$short_no_arg*(e\d*\.\w*)?$/o || /^-$short_with_num\d+$/o;
153 my($opt,$arg) = /^--([^=]+)(?:=(.*))?$/;
156 my $ct = $long_opt{$opt};
157 last unless defined $ct;
165 $arg = check_arg($opt, $arg, $ct);
166 $opts[-1] =~ s/=.*/=$arg/;
171 } elsif ($short_disabled ne '') {
172 $disabled = /^-$short_no_arg*([$short_disabled])/o;
176 last unless $disabled; # Generate generic failure
177 die "$0: option $opt has been disabled on this server.\n";
180 if ($subdir ne '/') {
181 # Validate args to ensure they don't try to leave our restricted dir.
185 die "$0: do not use .. in any path!\n" if m{(^|/)\\?\.\\?\.(\\?/|$)};
187 push(@args, bsd_glob($_, GLOB_LIMIT|GLOB_NOCHECK|GLOB_BRACE|GLOB_QUOTE));
190 die "$0: invalid rsync-command syntax or options\n" if $in_options;
192 @args = ( '.' ) if !@args;
195 my ($mm,$hh) = (localtime)[1,2];
196 my $host = $ENV{SSH_CONNECTION} || 'unknown';
197 $host =~ s/ .*//; # Keep only the client's IP addr
198 $host =~ s/^::ffff://;
199 $host = gethostbyaddr(inet_aton($host),AF_INET) || $host;
200 printf LOG "%02d:%02d %-13s [%s]\n", $hh, $mm, $host, "@opts @args";
204 # Note: This assumes that the rsync protocol will not be maliciously hijacked.
205 exec(RSYNC, @opts, @args) or die "exec(rsync @opts @args) failed: $? $!";
209 my($opt, $arg, $type) = @_;
210 $arg =~ s/\\(.)/$1/g;
211 if ($subdir ne '/' && ($type == 3 || ($type == 2 && !$am_sender))) {
213 die "Do not use .. in --$opt; anchor the path at the root of your restricted dir.\n"
214 if $arg =~ m{(^|/)\.\.(/|$)};
215 $arg =~ s{^/}{$subdir/};