3 # Name: /usr/local/bin/rrsync (should also have a symlink in /usr/bin)
4 # Purpose: Restricts rsync to subdirectory declared in .ssh/authorized_keys
5 # Author: Joe Smith <js-cgi@inwap.com> 30-Sep-2004
6 # Modified by: Wayne Davison <wayned@samba.org>
9 use File::Glob ':glob';
10 use constant RSYNC => 'rsync'; # Optionally set the path of rsync here.
11 use constant LOGFILE => 'rrsync.log';
13 Use 'command="$0 [-ro] SUBDIR"'
14 in front of lines in $ENV{HOME}/.ssh/authorized_keys
17 our $ro = (@ARGV && $ARGV[0] eq '-ro') ? shift : ''; # -ro = Read-Only
19 die "$0: No subdirectory specified\n$Usage" unless defined $subdir;
20 die "$0: Restricted subdirectory does not exist!\n" if $subdir ne '/' && !-d $subdir;
22 # The client uses "rsync -av -e ssh src/ server:dir/", and sshd on the server
23 # executes this program when .ssh/authorized_keys has 'command="..."'.
25 # command="rrsync logs/client" ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEAzGhEeNlPr...
26 # command="rrsync -ro results" ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEAmkHG1WCjC...
28 # Format of the envrionment variables set by sshd:
29 # SSH_ORIGINAL_COMMAND=rsync --server -vlogDtpr --partial . ARG # push
30 # SSH_ORIGINAL_COMMAND=rsync --server --sender -vlogDtpr --partial . ARGS # pull
31 # SSH_CONNECTION=client_addr client_port server_port
33 my $command = $ENV{SSH_ORIGINAL_COMMAND};
34 die "$0: Not invoked via sshd\n$Usage" unless defined $command;
35 die "$0: SSH_ORIGINAL_COMMAND='$command' is not rsync\n" unless $command =~ s/^rsync\s+//;
36 our $am_sender = $command =~ /\s--sender\s/;
37 die "$0 -ro: sending to read-only server not allowed\n" if $ro && !$am_sender;
39 # These options are only the options that rsync might send to the
40 # server, and only in the arg format that the stock rsync uses.
41 ### START of options data output by the cull-options script. ###
42 our $short_no_arg = 'CDHIKLORSWbcdglnoprtuvxz';
43 our $short_with_num = 'B';
44 # To disable a short-named option, add its letter to this string:
45 our $short_disabled = '';
46 # To disable a long-named option, change its value to a 0. A value of -1
47 # means the arg doesn't need checking, a 2 means only check when receiving.
49 'copy-unsafe-links' => -1,
51 'delay-updates' => -1,
54 'delete-before' => -1,
55 'delete-during' => -1,
56 'delete-excluded' => -1,
61 'ignore-errors' => -1,
62 'ignore-existing' => -1,
65 'no-implied-dirs' => -1,
69 'remove-sent-files' => $ro ? 0 : -1,
75 our %long_with_arg = (
77 'checksum-seed' => -1,
81 'modify-window' => -1,
82 'only-write-batch' => -1,
86 our %long_before_arg = (
93 ### END of options data output by the cull-options script. ###
95 if ($short_disabled ne '') {
96 $short_no_arg =~ s/[$short_disabled]//go;
97 $short_with_num =~ s/[$short_disabled]//go;
104 foreach (split(/(?<!\\)\s+/, $command)) {
107 push(@opts, check_arg($last_opt, $_, $check_type));
109 } elsif ($in_options) {
115 next if /^-[$short_no_arg]+$/o || /^-[$short_with_num]\d+$/o;
117 my($opt,$arg) = /^--([^=]+)(?:=(.*))?$/;
120 my $ct = $long_with_arg{$opt};
122 $arg = check_arg($opt, $arg, $ct);
123 $opts[-1] =~ s/=.*/=$arg/;
126 $disabled = defined $long_with_arg{$opt};
128 } elsif (defined $opt) {
129 if (defined $long_no_arg{$opt}) {
130 next if $long_no_arg{$opt};
133 $check_type = $long_before_arg{$opt};
138 $disabled = defined $check_type;
141 } elsif ($short_disabled ne '') {
142 $disabled = /^-[$short_no_arg]*([$short_disabled])/o;
143 $opt = "-$1" if $disabled;
146 die "$0: option $opt has been disabled on this server.\n" if $disabled;
147 die "$0: invalid rsync-command syntax or options\n";
154 my $write_log = -f LOGFILE && open(LOG, '>>', LOGFILE);
156 chdir($subdir) or die "$0: Unable to chdir to $subdir: $!\n";
158 # Validate args to ensure they don't try to leave our restricted dir.
159 if ($subdir ne '/') {
162 s#//+#/#g; # Turn multiple slashes into a single slash
163 s#^/##; # Don't allow absolute paths
164 s#^$#.#; # Turn empty arg into "."
165 die "Do not use .. in any path!\n" if m#(^|/)\.\.(/|$)#;
166 push(@new, bsd_glob($_, GLOB_LIMIT | GLOB_NOCHECK | GLOB_BRACE | GLOB_QUOTE));
171 @args = ( '.' ) if !@args;
174 my ($mm,$hh) = (localtime)[1,2];
175 my $host = $ENV{SSH_CONNECTION} || 'unknown';
176 $host =~ s/ .*//; # Keep only the client's IP addr
177 $host =~ s/^::ffff://;
178 $host = gethostbyaddr(inet_aton($host),AF_INET) || $host;
179 printf LOG "%02d:%02d %-13s [%s]\n", $hh, $mm, $host, "@opts @args";
183 # Note: This assumes that the rsync protocol will not be maliciously hijacked.
184 exec(RSYNC, @opts, @args) or die "exec(rsync @opts @args) failed: $? $!";
188 my($opt, $arg, $type) = @_;
189 if ($subdir ne '/' && $type > 0 && ($type < 2 || !$am_sender)) {
191 die "Do not use .. in --$opt; anchor the path at the root of your restricted dir.\n"
192 if $arg =~ m#(^|/)\.\.(/|$)#;
193 $arg =~ s#^/#$subdir/#;