Fixed a problem with --fake-super not getting the fully tweaked new_mode
[rsync/rsync.git] / support / rrsync
CommitLineData
44a82a17 1#!/usr/bin/perl
106a8ad9 2# Name: /usr/local/bin/rrsync (should also have a symlink in /usr/bin)
44a82a17 3# Purpose: Restricts rsync to subdirectory declared in .ssh/authorized_keys
106a8ad9 4# Author: Joe Smith <js-cgi@inwap.com> 30-Sep-2004
70318468 5# Modified by: Wayne Davison <wayned@samba.org>
2e5a7629 6use strict;
44a82a17
WD
7
8use Socket;
2e5a7629 9use Cwd 'abs_path';
70318468 10use File::Glob ':glob';
85fbfa10
WD
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';
44a82a17 15use constant LOGFILE => 'rrsync.log';
85fbfa10 16
44a82a17 17my $Usage = <<EOM;
106a8ad9 18Use 'command="$0 [-ro] SUBDIR"'
44a82a17
WD
19 in front of lines in $ENV{HOME}/.ssh/authorized_keys
20EOM
21
70318468
WD
22our $ro = (@ARGV && $ARGV[0] eq '-ro') ? shift : ''; # -ro = Read-Only
23our $subdir = shift;
24die "$0: No subdirectory specified\n$Usage" unless defined $subdir;
2e5a7629 25$subdir = abs_path($subdir);
1524815e 26die "$0: Restricted directory does not exist!\n" if $subdir ne '/' && !-d $subdir;
44a82a17
WD
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:
70318468
WD
35# SSH_ORIGINAL_COMMAND=rsync --server -vlogDtpr --partial . ARG # push
36# SSH_ORIGINAL_COMMAND=rsync --server --sender -vlogDtpr --partial . ARGS # pull
106a8ad9 37# SSH_CONNECTION=client_addr client_port server_port
44a82a17
WD
38
39my $command = $ENV{SSH_ORIGINAL_COMMAND};
70318468
WD
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+//;
985af703 42our $am_sender = $command =~ /^--server\s+--sender\s/; # Restrictive on purpose!
70318468
WD
43die "$0 -ro: sending to read-only server not allowed\n" if $ro && !$am_sender;
44
b2145610 45### START of options data produced by the cull_options script. ###
985af703 46
85fbfa10 47# These options are the only options that rsync might send to the server,
26c87bb6 48# and only in the option format that the stock rsync produces.
85fbfa10 49
70318468 50# To disable a short-named option, add its letter to this string:
2e8259bb 51our $short_disabled = 's';
85fbfa10 52
5d3e5f64 53our $short_no_arg = 'ACDEHIKLORSWXbcdgklmnoprstuvxz'; # DO NOT REMOVE ANY
85fbfa10
WD
54our $short_with_num = 'B'; # DO NOT REMOVE ANY
55
b2145610
WD
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 = (
174e51b5 60 'append' => 0,
70318468 61 'backup-dir' => 2,
b2145610
WD
62 'bwlimit' => 1,
63 'checksum-seed' => 1,
64 'compare-dest' => 2,
174e51b5 65 'compress-level' => 1,
b2145610
WD
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,
d2745243 73 'delete-delay' => 0,
b2145610
WD
74 'delete-during' => 0,
75 'delete-excluded' => 0,
76 'existing' => 0,
77 'files-from' => 3,
78 'force' => 0,
79 'from0' => 0,
80 'fuzzy' => 0,
d2745243 81 'iconv' => 1,
b2145610
WD
82 'ignore-errors' => 0,
83 'ignore-existing' => 0,
84 'inplace' => 0,
85 'link-dest' => 2,
86 'list-only' => 0,
87 'log-format' => 1,
88 'max-delete' => 1,
89 'max-size' => 1,
174e51b5 90 'min-size' => 1,
b2145610 91 'modify-window' => 1,
d2745243 92 'no-i-r' => 0,
b2145610 93 'no-implied-dirs' => 0,
174e51b5 94 'no-r' => 0,
b2145610 95 'no-relative' => 0,
174e51b5 96 'no-specials' => 0,
b2145610
WD
97 'numeric-ids' => 0,
98 'only-write-batch' => 1,
99 'partial' => 0,
70318468 100 'partial-dir' => 2,
b2145610 101 'remove-sent-files' => $ro ? -1 : 0,
174e51b5 102 'remove-source-files' => $ro ? -1 : 0,
b2145610
WD
103 'safe-links' => 0,
104 'sender' => 0,
105 'server' => 0,
106 'size-only' => 0,
d2745243 107 'skip-compress' => 1,
174e51b5 108 'specials' => 0,
b2145610 109 'suffix' => 1,
174e51b5 110 'super' => 0,
70318468 111 'temp-dir' => 2,
b2145610 112 'timeout' => 1,
70318468 113);
985af703 114
b2145610 115### END of options data produced by the cull_options script. ###
70318468
WD
116
117if ($short_disabled ne '') {
118 $short_no_arg =~ s/[$short_disabled]//go;
119 $short_with_num =~ s/[$short_disabled]//go;
120}
85fbfa10
WD
121$short_no_arg = "[$short_no_arg]" if length($short_no_arg) > 1;
122$short_with_num = "[$short_with_num]" if length($short_with_num) > 1;
70318468 123
1524815e
WD
124my $write_log = -f LOGFILE && open(LOG, '>>', LOGFILE);
125
126chdir($subdir) or die "$0: Unable to chdir to restricted dir: $!\n";
127
70318468
WD
128my(@opts, @args);
129my $in_options = 1;
130my $last_opt = '';
131my $check_type;
26c87bb6
WD
132while ($command =~ /((?:[^\s\\]+|\\.[^\s\\]*)+)/g) {
133 $_ = $1;
70318468 134 if ($check_type) {
70318468
WD
135 push(@opts, check_arg($last_opt, $_, $check_type));
136 $check_type = 0;
137 } elsif ($in_options) {
70318468
WD
138 push(@opts, $_);
139 if ($_ eq '.') {
140 $in_options = 0;
141 } else {
5d3e5f64 142 next if /^-$short_no_arg+(e\d+\.\d+)?$/o || /^-$short_with_num\d+$/o;
70318468
WD
143
144 my($opt,$arg) = /^--([^=]+)(?:=(.*))?$/;
145 my $disabled;
b2145610
WD
146 if (defined $opt) {
147 my $ct = $long_opt{$opt};
148 last unless defined $ct;
149 next if $ct == 0;
150 if ($ct > 0) {
151 if (!defined $arg) {
152 $check_type = $ct;
70318468
WD
153 $last_opt = $opt;
154 next;
155 }
b2145610
WD
156 $arg = check_arg($opt, $arg, $ct);
157 $opts[-1] =~ s/=.*/=$arg/;
158 next;
70318468 159 }
b2145610 160 $disabled = 1;
70318468
WD
161 $opt = "--$opt";
162 } elsif ($short_disabled ne '') {
85fbfa10 163 $disabled = /^-$short_no_arg*([$short_disabled])/o;
b2145610 164 $opt = "-$1";
70318468
WD
165 }
166
85fbfa10
WD
167 last unless $disabled; # Generate generic failure
168 die "$0: option $opt has been disabled on this server.\n";
70318468 169 }
106a8ad9 170 } else {
1524815e
WD
171 if ($subdir ne '/') {
172 # Validate args to ensure they don't try to leave our restricted dir.
173 s#//+#/#g;
174 s#^/##;
175 s#^$#.#;
176 die "Do not use .. in any path!\n" if m#(^|/)\\?\.\\?\.(\\?/|$)#;
177 }
178 push(@args, bsd_glob($_, GLOB_LIMIT|GLOB_NOCHECK|GLOB_BRACE|GLOB_QUOTE));
70318468 179 }
70318468 180}
1524815e 181die "$0: invalid rsync-command syntax or options\n" if $in_options;
70318468
WD
182
183@args = ( '.' ) if !@args;
184
185if ($write_log) {
44a82a17 186 my ($mm,$hh) = (localtime)[1,2];
106a8ad9 187 my $host = $ENV{SSH_CONNECTION} || 'unknown';
1524815e 188 $host =~ s/ .*//; # Keep only the client's IP addr
106a8ad9 189 $host =~ s/^::ffff://;
44a82a17 190 $host = gethostbyaddr(inet_aton($host),AF_INET) || $host;
70318468 191 printf LOG "%02d:%02d %-13s [%s]\n", $hh, $mm, $host, "@opts @args";
44a82a17
WD
192 close LOG;
193}
194
44a82a17 195# Note: This assumes that the rsync protocol will not be maliciously hijacked.
70318468
WD
196exec(RSYNC, @opts, @args) or die "exec(rsync @opts @args) failed: $? $!";
197
198sub check_arg
199{
200 my($opt, $arg, $type) = @_;
85fbfa10 201 $arg =~ s/\\(.)/$1/g;
b2145610 202 if ($subdir ne '/' && ($type == 3 || ($type == 2 && !$am_sender))) {
70318468
WD
203 $arg =~ s#//#/#g;
204 die "Do not use .. in --$opt; anchor the path at the root of your restricted dir.\n"
205 if $arg =~ m#(^|/)\.\.(/|$)#;
206 $arg =~ s#^/#$subdir/#;
207 }
208 $arg;
209}