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