- Made it easy to configure the path of the rsync executable.
[rsync/rsync.git] / support / rrsync
CommitLineData
44a82a17 1#!/usr/bin/perl
70318468 2use strict;
106a8ad9 3# Name: /usr/local/bin/rrsync (should also have a symlink in /usr/bin)
44a82a17 4# Purpose: Restricts rsync to subdirectory declared in .ssh/authorized_keys
106a8ad9 5# Author: Joe Smith <js-cgi@inwap.com> 30-Sep-2004
70318468 6# Modified by: Wayne Davison <wayned@samba.org>
44a82a17
WD
7
8use Socket;
70318468
WD
9use File::Glob ':glob';
10use constant RSYNC => 'rsync'; # Optionally set the path of rsync here.
44a82a17
WD
11use constant LOGFILE => 'rrsync.log';
12my $Usage = <<EOM;
106a8ad9 13Use 'command="$0 [-ro] SUBDIR"'
44a82a17
WD
14 in front of lines in $ENV{HOME}/.ssh/authorized_keys
15EOM
16
70318468
WD
17our $ro = (@ARGV && $ARGV[0] eq '-ro') ? shift : ''; # -ro = Read-Only
18our $subdir = shift;
19die "$0: No subdirectory specified\n$Usage" unless defined $subdir;
20die "$0: Restricted subdirectory does not exist!\n" if $subdir ne '/' && !-d $subdir;
44a82a17
WD
21
22# The client uses "rsync -av -e ssh src/ server:dir/", and sshd on the server
23# executes this program when .ssh/authorized_keys has 'command="..."'.
24# For example:
25# command="rrsync logs/client" ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEAzGhEeNlPr...
26# command="rrsync -ro results" ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEAmkHG1WCjC...
27#
28# Format of the envrionment variables set by sshd:
70318468
WD
29# SSH_ORIGINAL_COMMAND=rsync --server -vlogDtpr --partial . ARG # push
30# SSH_ORIGINAL_COMMAND=rsync --server --sender -vlogDtpr --partial . ARGS # pull
106a8ad9 31# SSH_CONNECTION=client_addr client_port server_port
44a82a17
WD
32
33my $command = $ENV{SSH_ORIGINAL_COMMAND};
70318468
WD
34die "$0: Not invoked via sshd\n$Usage" unless defined $command;
35die "$0: SSH_ORIGINAL_COMMAND='$command' is not rsync\n" unless $command =~ s/^rsync\s+//;
36our $am_sender = $command =~ /\s--sender\s/;
37die "$0 -ro: sending to read-only server not allowed\n" if $ro && !$am_sender;
38
39# These options are only the options that rsync might send to the
40# server, and only in the arg format that the stock rsync uses.
41### START of options data output by the cull-options script. ###
42our $short_no_arg = 'CDHIKLORSWbcdglnoprtuvxz';
43our $short_with_num = 'B';
44# To disable a short-named option, add its letter to this string:
45our $short_disabled = '';
46# To disable a long-named option, change its value to a 0. A value of -1
47# means the arg doesn't need checking, a 2 means only check when receiving.
48our %long_no_arg = (
49 'copy-unsafe-links' => -1,
50 'daemon' => -1,
51 'delay-updates' => -1,
52 'delete' => -1,
53 'delete-after' => -1,
54 'delete-before' => -1,
55 'delete-during' => -1,
56 'delete-excluded' => -1,
57 'existing' => -1,
58 'force' => -1,
59 'from0' => -1,
60 'fuzzy' => -1,
61 'ignore-errors' => -1,
62 'ignore-existing' => -1,
63 'inplace' => -1,
64 'list-only' => -1,
65 'no-implied-dirs' => -1,
66 'no-relative' => -1,
67 'numeric-ids' => -1,
68 'partial' => -1,
69 'remove-sent-files' => $ro ? 0 : -1,
70 'safe-links' => -1,
71 'sender' => -1,
72 'server' => -1,
73 'size-only' => -1,
74);
75our %long_with_arg = (
76 'bwlimit' => -1,
77 'checksum-seed' => -1,
78 'files-from' => 1,
79 'log-format' => -1,
80 'max-delete' => -1,
81 'modify-window' => -1,
82 'only-write-batch' => -1,
83 'suffix' => -1,
84 'timeout' => -1,
85);
86our %long_before_arg = (
87 'backup-dir' => 2,
88 'files-from' => 1,
89 'max-size' => -1,
90 'partial-dir' => 2,
91 'temp-dir' => 2,
92);
93### END of options data output by the cull-options script. ###
94
95if ($short_disabled ne '') {
96 $short_no_arg =~ s/[$short_disabled]//go;
97 $short_with_num =~ s/[$short_disabled]//go;
98}
99
100my(@opts, @args);
101my $in_options = 1;
102my $last_opt = '';
103my $check_type;
104foreach (split(/(?<!\\)\s+/, $command)) {
105 if ($check_type) {
106 s/\\(.)/$1/g;
107 push(@opts, check_arg($last_opt, $_, $check_type));
108 $check_type = 0;
109 } elsif ($in_options) {
110 s/\\(.)/$1/g;
111 push(@opts, $_);
112 if ($_ eq '.') {
113 $in_options = 0;
114 } else {
115 next if /^-[$short_no_arg]+$/o || /^-[$short_with_num]\d+$/o;
116
117 my($opt,$arg) = /^--([^=]+)(?:=(.*))?$/;
118 my $disabled;
119 if (defined $arg) {
120 my $ct = $long_with_arg{$opt};
121 if ($ct) {
122 $arg = check_arg($opt, $arg, $ct);
123 $opts[-1] =~ s/=.*/=$arg/;
124 next;
125 }
126 $disabled = defined $long_with_arg{$opt};
127 $opt = "--$opt";
128 } elsif (defined $opt) {
129 if (defined $long_no_arg{$opt}) {
130 next if $long_no_arg{$opt};
131 $disabled = 1;
132 } else {
133 $check_type = $long_before_arg{$opt};
134 if ($check_type) {
135 $last_opt = $opt;
136 next;
137 }
138 $disabled = defined $check_type;
139 }
140 $opt = "--$opt";
141 } elsif ($short_disabled ne '') {
142 $disabled = /^-[$short_no_arg]*([$short_disabled])/o;
143 $opt = "-$1" if $disabled;
144 }
145
146 die "$0: option $opt has been disabled on this server.\n" if $disabled;
147 die "$0: invalid rsync-command syntax or options\n";
148 }
106a8ad9 149 } else {
70318468 150 push(@args, $_);
106a8ad9 151 }
106a8ad9 152}
44a82a17 153
70318468
WD
154my $write_log = -f LOGFILE && open(LOG, '>>', LOGFILE);
155
156chdir($subdir) or die "$0: Unable to chdir to $subdir: $!\n";
157
158# Validate args to ensure they don't try to leave our restricted dir.
159if ($subdir ne '/') {
160 my @new;
161 foreach (@args) {
162 s#//+#/#g; # Turn multiple slashes into a single slash
163 s#^/##; # Don't allow absolute paths
164 s#^$#.#; # Turn empty arg into "."
165 die "Do not use .. in any path!\n" if m#(^|/)\.\.(/|$)#;
166 push(@new, bsd_glob($_, GLOB_LIMIT | GLOB_NOCHECK | GLOB_BRACE | GLOB_QUOTE));
167 }
168 @args = @new;
169}
170
171@args = ( '.' ) if !@args;
172
173if ($write_log) {
44a82a17 174 my ($mm,$hh) = (localtime)[1,2];
106a8ad9 175 my $host = $ENV{SSH_CONNECTION} || 'unknown';
44a82a17 176 $host =~ s/ .*//; # Keep only the client's IP addr
106a8ad9 177 $host =~ s/^::ffff://;
44a82a17 178 $host = gethostbyaddr(inet_aton($host),AF_INET) || $host;
70318468 179 printf LOG "%02d:%02d %-13s [%s]\n", $hh, $mm, $host, "@opts @args";
44a82a17
WD
180 close LOG;
181}
182
44a82a17 183# Note: This assumes that the rsync protocol will not be maliciously hijacked.
70318468
WD
184exec(RSYNC, @opts, @args) or die "exec(rsync @opts @args) failed: $? $!";
185
186sub check_arg
187{
188 my($opt, $arg, $type) = @_;
189 if ($subdir ne '/' && $type > 0 && ($type < 2 || !$am_sender)) {
190 $arg =~ s#//#/#g;
191 die "Do not use .. in --$opt; anchor the path at the root of your restricted dir.\n"
192 if $arg =~ m#(^|/)\.\.(/|$)#;
193 $arg =~ s#^/#$subdir/#;
194 }
195 $arg;
196}