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