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