4c5dd2aa7d83e59e22a9a1f9ac33fea322499794
[rsync.git] / support / rrsync
1 #!/usr/bin/env 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 <wayne@opencoder.net>
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|-no-munge] SUBDIR"'
19         in front of lines in $ENV{HOME}/.ssh/authorized_keys
20 EOM
21
22 # Handle the -ro, -wo, & -no-munge options.
23 our $only = '';
24 our $force_munge = 1;
25 while (@ARGV) {
26   if ($ARGV[0] =~ /^-([rw])o$/) {
27     my $r_or_w = $1;
28     if ($only && $only ne $r_or_w) {
29       die "$0: the -ro and -wo options conflict.\n";
30     }
31     $only = $r_or_w;
32   } elsif ($ARGV[0] eq '-no-munge') {
33     $force_munge = 0;
34   } else {
35     last;
36   }
37   shift;
38 }
39
40 our $subdir = shift;
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;
44
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="..."'.
47 # For example:
48 # command="rrsync logs/client" ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEAzGhEeNlPr...
49 # command="rrsync -ro results" ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEAmkHG1WCjC...
50 #
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
55
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;
63
64 ### START of options data produced by the cull_options script. ###
65
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.
68
69 # To disable a short-named option, add its letter to this string:
70 our $short_disabled = 's';
71
72 our $short_no_arg = 'ACDEHIJKLNORSUWXbcdgklmnopqrstuvxyz'; # DO NOT REMOVE ANY
73 our $short_with_num = '@B'; # DO NOT REMOVE ANY
74
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.
78 our %long_opt = (
79   'append' => 0,
80   'backup-dir' => 2,
81   'block-size' => 1,
82   'bwlimit' => 1,
83   'checksum-choice' => 1,
84   'checksum-seed' => 1,
85   'compare-dest' => 2,
86   'compress-choice' => 1,
87   'compress-level' => 1,
88   'copy-dest' => 2,
89   'copy-unsafe-links' => 0,
90   'daemon' => -1,
91   'debug' => 1,
92   'delay-updates' => 0,
93   'delete' => 0,
94   'delete-after' => 0,
95   'delete-before' => 0,
96   'delete-delay' => 0,
97   'delete-during' => 0,
98   'delete-excluded' => 0,
99   'delete-missing-args' => 0,
100   'existing' => 0,
101   'fake-super' => 0,
102   'files-from' => 3,
103   'force' => 0,
104   'from0' => 0,
105   'fsync' => 2,
106   'fuzzy' => 0,
107   'group' => 0,
108   'groupmap' => 1,
109   'hard-links' => 0,
110   'iconv' => 1,
111   'ignore-errors' => 0,
112   'ignore-existing' => 0,
113   'ignore-missing-args' => 0,
114   'ignore-times' => 0,
115   'info' => 1,
116   'inplace' => 0,
117   'link-dest' => 2,
118   'links' => 0,
119   'list-only' => 0,
120   'log-file' => 3,
121   'log-format' => 1,
122   'max-alloc' => 1,
123   'max-delete' => 1,
124   'max-size' => 1,
125   'min-size' => 1,
126   'mkpath' => 0,
127   'modify-window' => 1,
128   'msgs2stderr' => 0,
129   'munge-links' => 0,
130   'new-compress' => 0,
131   'no-W' => 0,
132   'no-implied-dirs' => 0,
133   'no-msgs2stderr' => 0,
134   'no-munge-links' => -1,
135   'no-r' => 0,
136   'no-relative' => 0,
137   'no-specials' => 0,
138   'numeric-ids' => 0,
139   'old-compress' => 0,
140   'one-file-system' => 0,
141   'only-write-batch' => 1,
142   'open-noatime' => 0,
143   'owner' => 0,
144   'partial' => 0,
145   'partial-dir' => 2,
146   'perms' => 0,
147   'preallocate' => 0,
148   'recursive' => 0,
149   'remove-sent-files' => 0,
150   'remove-source-files' => 0,
151   'safe-links' => 0,
152   'sender' => 0,
153   'server' => 0,
154   'size-only' => 0,
155   'skip-compress' => 1,
156   'specials' => 0,
157   'stats' => 0,
158   'suffix' => 1,
159   'super' => 0,
160   'temp-dir' => 2,
161   'timeout' => 1,
162   'times' => 0,
163   'use-qsort' => 0,
164   'usermap' => 1,
165   'write-devices' => -1,
166 );
167
168 ### END of options data produced by the cull_options script. ###
169
170 if ($only eq 'r') {
171   foreach my $opt (keys %long_opt) {
172     if ($opt =~ /^(remove-|log-file)/) {
173       $long_opt{$opt} = -1;
174     }
175   }
176 } elsif ($only eq 'w') {
177   $long_opt{'sender'} = -1;
178 }
179
180 if ($short_disabled ne '') {
181     $short_no_arg =~ s/[$short_disabled]//go;
182     $short_with_num =~ s/[$short_disabled]//go;
183 }
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;
186
187 my $write_log = -f LOGFILE && open(LOG, '>>', LOGFILE);
188
189 chdir($subdir) or die "$0: Unable to chdir to restricted dir: $!\n";
190
191 my(@opts, @args);
192 my $in_options = 1;
193 my $last_opt = '';
194 my $check_type;
195 while ($command =~ /((?:[^\s\\]+|\\.[^\s\\]*)+)/g) {
196   $_ = $1;
197   if ($check_type) {
198     push(@opts, check_arg($last_opt, $_, $check_type));
199     $check_type = 0;
200   } elsif ($in_options) {
201     if ($_ eq '.') {
202       $in_options = 0;
203     } else {
204       die "$0: invalid option: '-'\n" if $_ eq '-';
205       push(@opts, $_);
206       next if /^-$short_no_arg*(e\d*\.\w*)?$/o || /^-$short_with_num\d+$/o;
207
208       my($opt,$arg) = /^--([^=]+)(?:=(.*))?$/;
209       my $disabled;
210       if (defined $opt) {
211         my $ct = $long_opt{$opt};
212         last unless defined $ct;
213         next if $ct == 0;
214         if ($ct > 0) {
215           if (!defined $arg) {
216             $check_type = $ct;
217             $last_opt = $opt;
218             next;
219           }
220           $arg = check_arg($opt, $arg, $ct);
221           $opts[-1] =~ s/=.*/=$arg/;
222           next;
223         }
224         $disabled = 1;
225         $opt = "--$opt";
226       } elsif ($short_disabled ne '') {
227         $disabled = /^-$short_no_arg*([$short_disabled])/o;
228         $opt = "-$1";
229       }
230
231       last unless $disabled; # Generate generic failure
232       die "$0: option $opt has been disabled on this server.\n";
233     }
234   } else {
235     if ($subdir ne '/') {
236       # Validate args to ensure they don't try to leave our restricted dir.
237       s{//+}{/}g;
238       s{^/}{};
239       s{^$}{.};
240     }
241     push(@args, bsd_glob($_, GLOB_LIMIT|GLOB_NOCHECK|GLOB_BRACE|GLOB_QUOTE));
242   }
243 }
244 die "$0: invalid rsync-command syntax or options\n" if $in_options;
245
246 if ($subdir ne '/') {
247   die "$0: do not use .. in any path!\n" if grep m{(^|/)\.\.(/|$)}, @args;
248 }
249
250 if ($force_munge) {
251   push(@opts, '--munge-links');
252 }
253
254 @args = ( '.' ) if !@args;
255
256 if ($write_log) {
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";
263   close LOG;
264 }
265
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: $? $!";
268
269 sub check_arg
270 {
271   my($opt, $arg, $type) = @_;
272   $arg =~ s/\\(.)/$1/g;
273   if ($subdir ne '/' && ($type == 3 || ($type == 2 && !$am_sender))) {
274     $arg =~ s{//}{/}g;
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/};
278   }
279   $arg;
280 }
281
282 # vim: sw=2