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 <wayne@opencoder.net>
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|-wo|-no-munge] SUBDIR"'
19 in front of lines in $ENV{HOME}/.ssh/authorized_keys
22 # Handle the -ro, -wo, & -no-munge options.
26 if ($ARGV[0] =~ /^-([rw])o$/) {
28 if ($only && $only ne $r_or_w) {
29 die "$0: the -ro and -wo options conflict.\n";
32 } elsif ($ARGV[0] eq '-no-munge') {
41 die "$0: No subdirectory specified\n$Usage" unless defined $subdir;
42 $subdir = abs_path($subdir);
43 die "$0: Restricted directory does not exist!\n" if $subdir ne '/' && !-d $subdir;
45 # The client uses "rsync -av -e ssh src/ server:dir/", and sshd on the server
46 # executes this program when .ssh/authorized_keys has 'command="..."'.
48 # command="rrsync logs/client" ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEAzGhEeNlPr...
49 # command="rrsync -ro results" ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEAmkHG1WCjC...
51 # Format of the environment variables set by sshd:
52 # SSH_ORIGINAL_COMMAND=rsync --server -vlogDtpr --partial . ARG # push
53 # SSH_ORIGINAL_COMMAND=rsync --server --sender -vlogDtpr --partial . ARGS # pull
54 # SSH_CONNECTION=client_addr client_port server_port
56 my $command = $ENV{SSH_ORIGINAL_COMMAND};
57 die "$0: Not invoked via sshd\n$Usage" unless defined $command;
58 die "$0: SSH_ORIGINAL_COMMAND='$command' is not rsync\n" unless $command =~ s/^rsync\s+//;
59 die "$0: --server option is not first\n" unless $command =~ /^--server\s/;
60 our $am_sender = $command =~ /^--server\s+--sender\s/; # Restrictive on purpose!
61 die "$0 sending to read-only server not allowed\n" if $only eq 'r' && !$am_sender;
62 die "$0 reading from write-only server not allowed\n" if $only eq 'w' && $am_sender;
64 ### START of options data produced by the cull_options script. ###
66 # These options are the only options that rsync might send to the server,
67 # and only in the option format that the stock rsync produces.
69 # To disable a short-named option, add its letter to this string:
70 our $short_disabled = 's';
72 our $short_no_arg = 'ACDEHIJKLNORSUWXbcdgklmnopqrstuvxyz'; # DO NOT REMOVE ANY
73 our $short_with_num = '@B'; # DO NOT REMOVE ANY
75 # To disable a long-named option, change its value to a -1. The values mean:
76 # 0 = the option has no arg; 1 = the arg doesn't need any checking; 2 = only
77 # check the arg when receiving; and 3 = always check the arg.
83 'checksum-choice' => 1,
86 'compress-choice' => 1,
87 'compress-level' => 1,
89 'copy-unsafe-links' => 0,
98 'delete-excluded' => 0,
99 'delete-missing-args' => 0,
111 'ignore-errors' => 0,
112 'ignore-existing' => 0,
113 'ignore-missing-args' => 0,
127 'modify-window' => 1,
132 'no-implied-dirs' => 0,
133 'no-msgs2stderr' => 0,
134 'no-munge-links' => -1,
140 'one-file-system' => 0,
141 'only-write-batch' => 1,
149 'remove-sent-files' => 0,
150 'remove-source-files' => 0,
155 'skip-compress' => 1,
165 'write-devices' => -1,
168 ### END of options data produced by the cull_options script. ###
171 foreach my $opt (keys %long_opt) {
172 if ($opt =~ /^(remove-|log-file)/) {
173 $long_opt{$opt} = -1;
176 } elsif ($only eq 'w') {
177 $long_opt{'sender'} = -1;
180 if ($short_disabled ne '') {
181 $short_no_arg =~ s/[$short_disabled]//go;
182 $short_with_num =~ s/[$short_disabled]//go;
184 $short_no_arg = "[$short_no_arg]" if length($short_no_arg) > 1;
185 $short_with_num = "[$short_with_num]" if length($short_with_num) > 1;
187 my $write_log = -f LOGFILE && open(LOG, '>>', LOGFILE);
189 chdir($subdir) or die "$0: Unable to chdir to restricted dir: $!\n";
195 while ($command =~ /((?:[^\s\\]+|\\.[^\s\\]*)+)/g) {
198 push(@opts, check_arg($last_opt, $_, $check_type));
200 } elsif ($in_options) {
204 die "$0: invalid option: '-'\n" if $_ eq '-';
206 next if /^-$short_no_arg*(e\d*\.\w*)?$/o || /^-$short_with_num\d+$/o;
208 my($opt,$arg) = /^--([^=]+)(?:=(.*))?$/;
211 my $ct = $long_opt{$opt};
212 last unless defined $ct;
220 $arg = check_arg($opt, $arg, $ct);
221 $opts[-1] =~ s/=.*/=$arg/;
226 } elsif ($short_disabled ne '') {
227 $disabled = /^-$short_no_arg*([$short_disabled])/o;
231 last unless $disabled; # Generate generic failure
232 die "$0: option $opt has been disabled on this server.\n";
235 if ($subdir ne '/') {
236 # Validate args to ensure they don't try to leave our restricted dir.
241 push(@args, bsd_glob($_, GLOB_LIMIT|GLOB_NOCHECK|GLOB_BRACE|GLOB_QUOTE));
244 die "$0: invalid rsync-command syntax or options\n" if $in_options;
246 if ($subdir ne '/') {
247 die "$0: do not use .. in any path!\n" if grep m{(^|/)\.\.(/|$)}, @args;
251 push(@opts, '--munge-links');
254 @args = ( '.' ) if !@args;
257 my ($mm,$hh) = (localtime)[1,2];
258 my $host = $ENV{SSH_CONNECTION} || 'unknown';
259 $host =~ s/ .*//; # Keep only the client's IP addr
260 $host =~ s/^::ffff://;
261 $host = gethostbyaddr(inet_aton($host),AF_INET) || $host;
262 printf LOG "%02d:%02d %-13s [%s]\n", $hh, $mm, $host, "@opts @args";
266 # Note: This assumes that the rsync protocol will not be maliciously hijacked.
267 exec(RSYNC, @opts, '--', '.', @args) or die "exec(rsync @opts -- . @args) failed: $? $!";
271 my($opt, $arg, $type) = @_;
272 $arg =~ s/\\(.)/$1/g;
273 if ($subdir ne '/' && ($type == 3 || ($type == 2 && !$am_sender))) {
275 die "Do not use .. in --$opt; anchor the path at the root of your restricted dir.\n"
276 if $arg =~ m{(^|/)\.\.(/|$)};
277 $arg =~ s{^/}{$subdir/};