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