Updated rrsync to deal with the latest 3.0.0's use of the -e option.
[rsync/rsync.git] / support / rrsync
... / ...
CommitLineData
1#!/usr/bin/perl
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>
6use strict;
7
8use Socket;
9use Cwd 'abs_path';
10use File::Glob ':glob';
11
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.
14use constant RSYNC => '/usr/bin/rsync';
15use constant LOGFILE => 'rrsync.log';
16
17my $Usage = <<EOM;
18Use 'command="$0 [-ro] SUBDIR"'
19 in front of lines in $ENV{HOME}/.ssh/authorized_keys
20EOM
21
22our $ro = (@ARGV && $ARGV[0] eq '-ro') ? shift : ''; # -ro = Read-Only
23our $subdir = shift;
24die "$0: No subdirectory specified\n$Usage" unless defined $subdir;
25$subdir = abs_path($subdir);
26die "$0: Restricted directory does not exist!\n" if $subdir ne '/' && !-d $subdir;
27
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="..."'.
30# For example:
31# command="rrsync logs/client" ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEAzGhEeNlPr...
32# command="rrsync -ro results" ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEAmkHG1WCjC...
33#
34# Format of the envrionment 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
38
39my $command = $ENV{SSH_ORIGINAL_COMMAND};
40die "$0: Not invoked via sshd\n$Usage" unless defined $command;
41die "$0: SSH_ORIGINAL_COMMAND='$command' is not rsync\n" unless $command =~ s/^rsync\s+//;
42our $am_sender = $command =~ /^--server\s+--sender\s/; # Restrictive on purpose!
43die "$0 -ro: sending to read-only server not allowed\n" if $ro && !$am_sender;
44
45### START of options data produced by the cull_options script. ###
46
47# These options are the only options that rsync might send to the server,
48# and only in the option format that the stock rsync produces.
49
50# To disable a short-named option, add its letter to this string:
51our $short_disabled = 's';
52
53our $short_no_arg = 'ACDEHIKLORSWXbcdgklmnoprstuvxz'; # DO NOT REMOVE ANY
54our $short_with_num = 'B'; # DO NOT REMOVE ANY
55
56# To disable a long-named option, change its value to a -1. The values mean:
57# 0 = the option has no arg; 1 = the arg doesn't need any checking; 2 = only
58# check the arg when receiving; and 3 = always check the arg.
59our %long_opt = (
60 'append' => 0,
61 'backup-dir' => 2,
62 'bwlimit' => 1,
63 'checksum-seed' => 1,
64 'compare-dest' => 2,
65 'compress-level' => 1,
66 'copy-dest' => 2,
67 'copy-unsafe-links' => 0,
68 'daemon' => 0,
69 'delay-updates' => 0,
70 'delete' => 0,
71 'delete-after' => 0,
72 'delete-before' => 0,
73 'delete-delay' => 0,
74 'delete-during' => 0,
75 'delete-excluded' => 0,
76 'existing' => 0,
77 'fake-super' => 0,
78 'files-from' => 3,
79 'force' => 0,
80 'from0' => 0,
81 'fuzzy' => 0,
82 'iconv' => 1,
83 'ignore-errors' => 0,
84 'ignore-existing' => 0,
85 'inplace' => 0,
86 'link-dest' => 2,
87 'list-only' => 0,
88 'log-file' => 3,
89 'log-format' => 1,
90 'max-delete' => 1,
91 'max-size' => 1,
92 'min-size' => 1,
93 'modify-window' => 1,
94 'no-i-r' => 0,
95 'no-implied-dirs' => 0,
96 'no-r' => 0,
97 'no-relative' => 0,
98 'no-specials' => 0,
99 'numeric-ids' => 0,
100 'only-write-batch' => 1,
101 'partial' => 0,
102 'partial-dir' => 2,
103 'remove-sent-files' => $ro ? -1 : 0,
104 'remove-source-files' => $ro ? -1 : 0,
105 'safe-links' => 0,
106 'sender' => 0,
107 'server' => 0,
108 'size-only' => 0,
109 'skip-compress' => 1,
110 'specials' => 0,
111 'suffix' => 1,
112 'super' => 0,
113 'temp-dir' => 2,
114 'timeout' => 1,
115 'use-qsort' => 0,
116);
117
118### END of options data produced by the cull_options script. ###
119
120if ($short_disabled ne '') {
121 $short_no_arg =~ s/[$short_disabled]//go;
122 $short_with_num =~ s/[$short_disabled]//go;
123}
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;
126
127my $write_log = -f LOGFILE && open(LOG, '>>', LOGFILE);
128
129chdir($subdir) or die "$0: Unable to chdir to restricted dir: $!\n";
130
131my(@opts, @args);
132my $in_options = 1;
133my $last_opt = '';
134my $check_type;
135while ($command =~ /((?:[^\s\\]+|\\.[^\s\\]*)+)/g) {
136 $_ = $1;
137 if ($check_type) {
138 push(@opts, check_arg($last_opt, $_, $check_type));
139 $check_type = 0;
140 } elsif ($in_options) {
141 push(@opts, $_);
142 if ($_ eq '.') {
143 $in_options = 0;
144 } else {
145 next if /^-$short_no_arg+(e\d*\.\w*)?$/o || /^-$short_with_num\d+$/o;
146
147 my($opt,$arg) = /^--([^=]+)(?:=(.*))?$/;
148 my $disabled;
149 if (defined $opt) {
150 my $ct = $long_opt{$opt};
151 last unless defined $ct;
152 next if $ct == 0;
153 if ($ct > 0) {
154 if (!defined $arg) {
155 $check_type = $ct;
156 $last_opt = $opt;
157 next;
158 }
159 $arg = check_arg($opt, $arg, $ct);
160 $opts[-1] =~ s/=.*/=$arg/;
161 next;
162 }
163 $disabled = 1;
164 $opt = "--$opt";
165 } elsif ($short_disabled ne '') {
166 $disabled = /^-$short_no_arg*([$short_disabled])/o;
167 $opt = "-$1";
168 }
169
170 last unless $disabled; # Generate generic failure
171 die "$0: option $opt has been disabled on this server.\n";
172 }
173 } else {
174 if ($subdir ne '/') {
175 # Validate args to ensure they don't try to leave our restricted dir.
176 s#//+#/#g;
177 s#^/##;
178 s#^$#.#;
179 die "Do not use .. in any path!\n" if m#(^|/)\\?\.\\?\.(\\?/|$)#;
180 }
181 push(@args, bsd_glob($_, GLOB_LIMIT|GLOB_NOCHECK|GLOB_BRACE|GLOB_QUOTE));
182 }
183}
184die "$0: invalid rsync-command syntax or options\n" if $in_options;
185
186@args = ( '.' ) if !@args;
187
188if ($write_log) {
189 my ($mm,$hh) = (localtime)[1,2];
190 my $host = $ENV{SSH_CONNECTION} || 'unknown';
191 $host =~ s/ .*//; # Keep only the client's IP addr
192 $host =~ s/^::ffff://;
193 $host = gethostbyaddr(inet_aton($host),AF_INET) || $host;
194 printf LOG "%02d:%02d %-13s [%s]\n", $hh, $mm, $host, "@opts @args";
195 close LOG;
196}
197
198# Note: This assumes that the rsync protocol will not be maliciously hijacked.
199exec(RSYNC, @opts, @args) or die "exec(rsync @opts @args) failed: $? $!";
200
201sub check_arg
202{
203 my($opt, $arg, $type) = @_;
204 $arg =~ s/\\(.)/$1/g;
205 if ($subdir ne '/' && ($type == 3 || ($type == 2 && !$am_sender))) {
206 $arg =~ s#//#/#g;
207 die "Do not use .. in --$opt; anchor the path at the root of your restricted dir.\n"
208 if $arg =~ m#(^|/)\.\.(/|$)#;
209 $arg =~ s#^/#$subdir/#;
210 }
211 $arg;
212}