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