- Fixed a bug where the --*-dest options weren't being culled.
[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
985af703
WD
45### START of options data produced by the cull-options script. ###
46
85fbfa10
WD
47# These options are the only options that rsync might send to the server,
48# and only in the option/arg format that the stock rsync produces.
49
70318468
WD
50# To disable a short-named option, add its letter to this string:
51our $short_disabled = '';
85fbfa10
WD
52
53our $short_no_arg = 'CDHIKLORSWbcdglnoprtuvxz'; # 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 0 (NOTE: at least
57# one option appears in two places!). A value of -1 means the arg doesn't
58# need checking, a 1 means to check it, a 2 means only check when receiving.
70318468
WD
59our %long_no_arg = (
60 'copy-unsafe-links' => -1,
61 'daemon' => -1,
62 'delay-updates' => -1,
63 'delete' => -1,
64 'delete-after' => -1,
65 'delete-before' => -1,
66 'delete-during' => -1,
67 'delete-excluded' => -1,
68 'existing' => -1,
69 'force' => -1,
70 'from0' => -1,
71 'fuzzy' => -1,
72 'ignore-errors' => -1,
73 'ignore-existing' => -1,
74 'inplace' => -1,
75 'list-only' => -1,
76 'no-implied-dirs' => -1,
77 'no-relative' => -1,
78 'numeric-ids' => -1,
79 'partial' => -1,
80 'remove-sent-files' => $ro ? 0 : -1,
81 'safe-links' => -1,
82 'sender' => -1,
83 'server' => -1,
84 'size-only' => -1,
85);
86our %long_with_arg = (
87 'bwlimit' => -1,
88 'checksum-seed' => -1,
89 'files-from' => 1,
90 'log-format' => -1,
91 'max-delete' => -1,
92 'modify-window' => -1,
93 'only-write-batch' => -1,
94 'suffix' => -1,
95 'timeout' => -1,
96);
97our %long_before_arg = (
98 'backup-dir' => 2,
99 'files-from' => 1,
100 'max-size' => -1,
101 'partial-dir' => 2,
102 'temp-dir' => 2,
103);
985af703
WD
104
105### END of options data produced by the cull-options script. ###
70318468
WD
106
107if ($short_disabled ne '') {
108 $short_no_arg =~ s/[$short_disabled]//go;
109 $short_with_num =~ s/[$short_disabled]//go;
110}
85fbfa10
WD
111$short_no_arg = "[$short_no_arg]" if length($short_no_arg) > 1;
112$short_with_num = "[$short_with_num]" if length($short_with_num) > 1;
70318468 113
1524815e
WD
114my $write_log = -f LOGFILE && open(LOG, '>>', LOGFILE);
115
116chdir($subdir) or die "$0: Unable to chdir to restricted dir: $!\n";
117
70318468
WD
118my(@opts, @args);
119my $in_options = 1;
120my $last_opt = '';
121my $check_type;
122foreach (split(/(?<!\\)\s+/, $command)) {
123 if ($check_type) {
70318468
WD
124 push(@opts, check_arg($last_opt, $_, $check_type));
125 $check_type = 0;
126 } elsif ($in_options) {
70318468
WD
127 push(@opts, $_);
128 if ($_ eq '.') {
129 $in_options = 0;
130 } else {
85fbfa10 131 next if /^-$short_no_arg+$/o || /^-$short_with_num\d+$/o;
70318468
WD
132
133 my($opt,$arg) = /^--([^=]+)(?:=(.*))?$/;
134 my $disabled;
135 if (defined $arg) {
136 my $ct = $long_with_arg{$opt};
137 if ($ct) {
138 $arg = check_arg($opt, $arg, $ct);
139 $opts[-1] =~ s/=.*/=$arg/;
140 next;
141 }
142 $disabled = defined $long_with_arg{$opt};
143 $opt = "--$opt";
144 } elsif (defined $opt) {
145 if (defined $long_no_arg{$opt}) {
146 next if $long_no_arg{$opt};
147 $disabled = 1;
148 } else {
149 $check_type = $long_before_arg{$opt};
150 if ($check_type) {
151 $last_opt = $opt;
152 next;
153 }
154 $disabled = defined $check_type;
155 }
156 $opt = "--$opt";
157 } elsif ($short_disabled ne '') {
85fbfa10 158 $disabled = /^-$short_no_arg*([$short_disabled])/o;
70318468
WD
159 $opt = "-$1" if $disabled;
160 }
161
85fbfa10
WD
162 last unless $disabled; # Generate generic failure
163 die "$0: option $opt has been disabled on this server.\n";
70318468 164 }
106a8ad9 165 } else {
1524815e
WD
166 if ($subdir ne '/') {
167 # Validate args to ensure they don't try to leave our restricted dir.
168 s#//+#/#g;
169 s#^/##;
170 s#^$#.#;
171 die "Do not use .. in any path!\n" if m#(^|/)\\?\.\\?\.(\\?/|$)#;
172 }
173 push(@args, bsd_glob($_, GLOB_LIMIT|GLOB_NOCHECK|GLOB_BRACE|GLOB_QUOTE));
70318468 174 }
70318468 175}
1524815e 176die "$0: invalid rsync-command syntax or options\n" if $in_options;
70318468
WD
177
178@args = ( '.' ) if !@args;
179
180if ($write_log) {
44a82a17 181 my ($mm,$hh) = (localtime)[1,2];
106a8ad9 182 my $host = $ENV{SSH_CONNECTION} || 'unknown';
1524815e 183 $host =~ s/ .*//; # Keep only the client's IP addr
106a8ad9 184 $host =~ s/^::ffff://;
44a82a17 185 $host = gethostbyaddr(inet_aton($host),AF_INET) || $host;
70318468 186 printf LOG "%02d:%02d %-13s [%s]\n", $hh, $mm, $host, "@opts @args";
44a82a17
WD
187 close LOG;
188}
189
44a82a17 190# Note: This assumes that the rsync protocol will not be maliciously hijacked.
70318468
WD
191exec(RSYNC, @opts, @args) or die "exec(rsync @opts @args) failed: $? $!";
192
193sub check_arg
194{
195 my($opt, $arg, $type) = @_;
85fbfa10
WD
196 $arg =~ s/\\(.)/$1/g;
197 if ($subdir ne '/' && ($type == 1 || ($type == 2 && !$am_sender))) {
70318468
WD
198 $arg =~ s#//#/#g;
199 die "Do not use .. in --$opt; anchor the path at the root of your restricted dir.\n"
200 if $arg =~ m#(^|/)\.\.(/|$)#;
201 $arg =~ s#^/#$subdir/#;
202 }
203 $arg;
204}