Debian Codegraph @

support/rrsync

raw
1 #!/usr/bin/perl
2 # Name: /usr/local/bin/rrsync (should also have a symlink in /usr/bin)
3 # Purpose: Restricts rsync to subdirectory declared in .ssh/authorized_keys
4 # Author: Joe Smith <[email protected]> 30-Sep-2004
5 # Modified by: Wayne Davison <[email protected]>
6 use strict;
7
8 use Socket;
9 use Cwd 'abs_path';
10 use File::Glob ':glob';
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.
14 use constant RSYNC => '/usr/bin/rsync';
15 use constant LOGFILE => 'rrsync.log';
16
17 my $Usage = <<EOM;
18 Use 'command="$0 [-ro|-wo] SUBDIR"'
19 in front of lines in $ENV{HOME}/.ssh/authorized_keys
20 EOM
21
22 # Handle the -ro and -wo options.
23 our $only = '';
24 while (@ARGV && $ARGV[0] =~ /^-([rw])o$/) {
25 my $r_or_w = $1;
26 if ($only && $only ne $r_or_w) {
27 die "$0: the -ro and -wo options conflict.\n";
28 }
29 $only = $r_or_w;
30 shift;
31 }
32
33 our $subdir = shift;
34 die "$0: No subdirectory specified\n$Usage" unless defined $subdir;
35 $subdir = abs_path($subdir);
36 die "$0: Restricted directory does not exist!\n" if $subdir ne '/' && !-d $subdir;
37
38 # The client uses "rsync -av -e ssh src/ server:dir/", and sshd on the server
39 # executes this program when .ssh/authorized_keys has 'command="..."'.
40 # For example:
41 # command="rrsync logs/client" ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEAzGhEeNlPr...
42 # command="rrsync -ro results" ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEAmkHG1WCjC...
43 #
44 # Format of the environment variables set by sshd:
45 # SSH_ORIGINAL_COMMAND=rsync --server -vlogDtpr --partial . ARG # push
46 # SSH_ORIGINAL_COMMAND=rsync --server --sender -vlogDtpr --partial . ARGS # pull
47 # SSH_CONNECTION=client_addr client_port server_port
48
49 my $command = $ENV{SSH_ORIGINAL_COMMAND};
50 die "$0: Not invoked via sshd\n$Usage" unless defined $command;
51 die "$0: SSH_ORIGINAL_COMMAND='$command' is not rsync\n" unless $command =~ s/^rsync\s+//;
52 die "$0: --server option is not first\n" unless $command =~ /^--server\s/;
53 our $am_sender = $command =~ /^--server\s+--sender\s/; # Restrictive on purpose!
54 die "$0 sending to read-only server not allowed\n" if $only eq 'r' && !$am_sender;
55 die "$0 reading from write-only server not allowed\n" if $only eq 'w' && $am_sender;
56
57 ### START of options data produced by the cull_options script. ###
58
59 # These options are the only options that rsync might send to the server,
60 # and only in the option format that the stock rsync produces.
61
62 # To disable a short-named option, add its letter to this string:
63 our $short_disabled = 's';
64
65 our $short_no_arg = 'ACDEHIJKLORSWXbcdgklmnoprstuvxyz'; # DO NOT REMOVE ANY
66 our $short_with_num = 'B'; # DO NOT REMOVE ANY
67
68 # To disable a long-named option, change its value to a -1. The values mean:
69 # 0 = the option has no arg; 1 = the arg doesn't need any checking; 2 = only
70 # check the arg when receiving; and 3 = always check the arg.
71 our %long_opt = (
72 'append' => 0,
73 'backup-dir' => 2,
74 'block-size' => 1,
75 'bwlimit' => 1,
76 'checksum-seed' => 1,
77 'compare-dest' => 2,
78 'compress-level' => 1,
79 'copy-dest' => 2,
80 'copy-unsafe-links' => 0,
81 'daemon' => -1,
82 'debug' => 1,
83 'delay-updates' => 0,
84 'delete' => 0,
85 'delete-after' => 0,
86 'delete-before' => 0,
87 'delete-delay' => 0,
88 'delete-during' => 0,
89 'delete-excluded' => 0,
90 'delete-missing-args' => 0,
91 'existing' => 0,
92 'fake-super' => 0,
93 'files-from' => 3,
94 'force' => 0,
95 'from0' => 0,
96 'fuzzy' => 0,
97 'group' => 0,
98 'groupmap' => 1,
99 'hard-links' => 0,
100 'iconv' => 1,
101 'ignore-errors' => 0,
102 'ignore-existing' => 0,
103 'ignore-missing-args' => 0,
104 'ignore-times' => 0,
105 'info' => 1,
106 'inplace' => 0,
107 'link-dest' => 2,
108 'links' => 0,
109 'list-only' => 0,
110 'log-file' => 3,
111 'log-format' => 1,
112 'max-delete' => 1,
113 'max-size' => 1,
114 'min-size' => 1,
115 'modify-window' => 1,
116 'new-compress' => 0,
117 'no-implied-dirs' => 0,
118 'no-r' => 0,
119 'no-relative' => 0,
120 'no-specials' => 0,
121 'numeric-ids' => 0,
122 'one-file-system' => 0,
123 'only-write-batch' => 1,
124 'owner' => 0,
125 'partial' => 0,
126 'partial-dir' => 2,
127 'perms' => 0,
128 'preallocate' => 0,
129 'recursive' => 0,
130 'remove-sent-files' => $only eq 'r' ? -1 : 0,
131 'remove-source-files' => $only eq 'r' ? -1 : 0,
132 'safe-links' => 0,
133 'sender' => 0,
134 'server' => 0,
135 'size-only' => 0,
136 'skip-compress' => 1,
137 'specials' => 0,
138 'stats' => 0,
139 'suffix' => 1,
140 'super' => 0,
141 'temp-dir' => 2,
142 'timeout' => 1,
143 'times' => 0,
144 'use-qsort' => 0,
145 'usermap' => 1,
146 );
147
148 ### END of options data produced by the cull_options script. ###
149
150 if ($short_disabled ne '') {
151 $short_no_arg =~ s/[$short_disabled]//go;
152 $short_with_num =~ s/[$short_disabled]//go;
153 }
154 $short_no_arg = "[$short_no_arg]" if length($short_no_arg) > 1;
155 $short_with_num = "[$short_with_num]" if length($short_with_num) > 1;
156
157 my $write_log = -f LOGFILE && open(LOG, '>>', LOGFILE);
158
159 chdir($subdir) or die "$0: Unable to chdir to restricted dir: $!\n";
160
161 my(@opts, @args);
162 my $in_options = 1;
163 my $last_opt = '';
164 my $check_type;
165 while ($command =~ /((?:[^\s\\]+|\\.[^\s\\]*)+)/g) {
166 $_ = $1;
167 if ($check_type) {
168 push(@opts, check_arg($last_opt, $_, $check_type));
169 $check_type = 0;
170 } elsif ($in_options) {
171 push(@opts, $_);
172 if ($_ eq '.') {
173 $in_options = 0;
174 } else {
175 die "$0: invalid option: '-'\n" if $_ eq '-';
176 next if /^-$short_no_arg*(e\d*\.\w*)?$/o || /^-$short_with_num\d+$/o;
177
178 my($opt,$arg) = /^--([^=]+)(?:=(.*))?$/;
179 my $disabled;
180 if (defined $opt) {
181 my $ct = $long_opt{$opt};
182 last unless defined $ct;
183 next if $ct == 0;
184 if ($ct > 0) {
185 if (!defined $arg) {
186 $check_type = $ct;
187 $last_opt = $opt;
188 next;
189 }
190 $arg = check_arg($opt, $arg, $ct);
191 $opts[-1] =~ s/=.*/=$arg/;
192 next;
193 }
194 $disabled = 1;
195 $opt = "--$opt";
196 } elsif ($short_disabled ne '') {
197 $disabled = /^-$short_no_arg*([$short_disabled])/o;
198 $opt = "-$1";
199 }
200
201 last unless $disabled; # Generate generic failure
202 die "$0: option $opt has been disabled on this server.\n";
203 }
204 } else {
205 if ($subdir ne '/') {
206 # Validate args to ensure they don't try to leave our restricted dir.
207 s{//+}{/}g;
208 s{^/}{};
209 s{^$}{.};
210 die "$0: do not use .. in any path!\n" if m{(^|/)\\?\.\\?\.(\\?/|$)};
211 }
212 push(@args, bsd_glob($_, GLOB_LIMIT|GLOB_NOCHECK|GLOB_BRACE|GLOB_QUOTE));
213 }
214 }
215 die "$0: invalid rsync-command syntax or options\n" if $in_options;
216
217 @args = ( '.' ) if !@args;
218
219 if ($write_log) {
220 my ($mm,$hh) = (localtime)[1,2];
221 my $host = $ENV{SSH_CONNECTION} || 'unknown';
222 $host =~ s/ .*//; # Keep only the client's IP addr
223 $host =~ s/^::ffff://;
224 $host = gethostbyaddr(inet_aton($host),AF_INET) || $host;
225 printf LOG "%02d:%02d %-13s [%s]\n", $hh, $mm, $host, "@opts @args";
226 close LOG;
227 }
228
229 # Note: This assumes that the rsync protocol will not be maliciously hijacked.
230 exec(RSYNC, @opts, @args) or die "exec(rsync @opts @args) failed: $? $!";
231
232 sub check_arg
233 {
234 my($opt, $arg, $type) = @_;
235 $arg =~ s/\\(.)/$1/g;
236 if ($subdir ne '/' && ($type == 3 || ($type == 2 && !$am_sender))) {
237 $arg =~ s{//}{/}g;
238 die "Do not use .. in --$opt; anchor the path at the root of your restricted dir.\n"
239 if $arg =~ m{(^|/)\.\.(/|$)};
240 $arg =~ s{^/}{$subdir/};
241 }
242 $arg;
243 }