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 WD |
52 | |
53 | our $short_no_arg = 'CDHIKLORSWbcdglnoprtuvxz'; # DO NOT REMOVE ANY | |
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 = ( | |
70318468 | 60 | 'backup-dir' => 2, |
b2145610 WD |
61 | 'bwlimit' => 1, |
62 | 'checksum-seed' => 1, | |
63 | 'compare-dest' => 2, | |
64 | 'copy-dest' => 2, | |
65 | 'copy-unsafe-links' => 0, | |
66 | 'daemon' => 0, | |
67 | 'delay-updates' => 0, | |
68 | 'delete' => 0, | |
69 | 'delete-after' => 0, | |
70 | 'delete-before' => 0, | |
71 | 'delete-during' => 0, | |
72 | 'delete-excluded' => 0, | |
73 | 'existing' => 0, | |
74 | 'files-from' => 3, | |
75 | 'force' => 0, | |
76 | 'from0' => 0, | |
77 | 'fuzzy' => 0, | |
78 | 'ignore-errors' => 0, | |
79 | 'ignore-existing' => 0, | |
80 | 'inplace' => 0, | |
81 | 'link-dest' => 2, | |
82 | 'list-only' => 0, | |
83 | 'log-format' => 1, | |
84 | 'max-delete' => 1, | |
85 | 'max-size' => 1, | |
86 | 'modify-window' => 1, | |
87 | 'no-implied-dirs' => 0, | |
88 | 'no-relative' => 0, | |
89 | 'numeric-ids' => 0, | |
90 | 'only-write-batch' => 1, | |
91 | 'partial' => 0, | |
70318468 | 92 | 'partial-dir' => 2, |
b2145610 WD |
93 | 'remove-sent-files' => $ro ? -1 : 0, |
94 | 'safe-links' => 0, | |
95 | 'sender' => 0, | |
96 | 'server' => 0, | |
97 | 'size-only' => 0, | |
98 | 'suffix' => 1, | |
70318468 | 99 | 'temp-dir' => 2, |
b2145610 | 100 | 'timeout' => 1, |
70318468 | 101 | ); |
985af703 | 102 | |
b2145610 | 103 | ### END of options data produced by the cull_options script. ### |
70318468 WD |
104 | |
105 | if ($short_disabled ne '') { | |
106 | $short_no_arg =~ s/[$short_disabled]//go; | |
107 | $short_with_num =~ s/[$short_disabled]//go; | |
108 | } | |
85fbfa10 WD |
109 | $short_no_arg = "[$short_no_arg]" if length($short_no_arg) > 1; |
110 | $short_with_num = "[$short_with_num]" if length($short_with_num) > 1; | |
70318468 | 111 | |
1524815e WD |
112 | my $write_log = -f LOGFILE && open(LOG, '>>', LOGFILE); |
113 | ||
114 | chdir($subdir) or die "$0: Unable to chdir to restricted dir: $!\n"; | |
115 | ||
70318468 WD |
116 | my(@opts, @args); |
117 | my $in_options = 1; | |
118 | my $last_opt = ''; | |
119 | my $check_type; | |
26c87bb6 WD |
120 | while ($command =~ /((?:[^\s\\]+|\\.[^\s\\]*)+)/g) { |
121 | $_ = $1; | |
70318468 | 122 | if ($check_type) { |
70318468 WD |
123 | push(@opts, check_arg($last_opt, $_, $check_type)); |
124 | $check_type = 0; | |
125 | } elsif ($in_options) { | |
70318468 WD |
126 | push(@opts, $_); |
127 | if ($_ eq '.') { | |
128 | $in_options = 0; | |
129 | } else { | |
85fbfa10 | 130 | next if /^-$short_no_arg+$/o || /^-$short_with_num\d+$/o; |
70318468 WD |
131 | |
132 | my($opt,$arg) = /^--([^=]+)(?:=(.*))?$/; | |
133 | my $disabled; | |
b2145610 WD |
134 | if (defined $opt) { |
135 | my $ct = $long_opt{$opt}; | |
136 | last unless defined $ct; | |
137 | next if $ct == 0; | |
138 | if ($ct > 0) { | |
139 | if (!defined $arg) { | |
140 | $check_type = $ct; | |
70318468 WD |
141 | $last_opt = $opt; |
142 | next; | |
143 | } | |
b2145610 WD |
144 | $arg = check_arg($opt, $arg, $ct); |
145 | $opts[-1] =~ s/=.*/=$arg/; | |
146 | next; | |
70318468 | 147 | } |
b2145610 | 148 | $disabled = 1; |
70318468 WD |
149 | $opt = "--$opt"; |
150 | } elsif ($short_disabled ne '') { | |
85fbfa10 | 151 | $disabled = /^-$short_no_arg*([$short_disabled])/o; |
b2145610 | 152 | $opt = "-$1"; |
70318468 WD |
153 | } |
154 | ||
85fbfa10 WD |
155 | last unless $disabled; # Generate generic failure |
156 | die "$0: option $opt has been disabled on this server.\n"; | |
70318468 | 157 | } |
106a8ad9 | 158 | } else { |
1524815e WD |
159 | if ($subdir ne '/') { |
160 | # Validate args to ensure they don't try to leave our restricted dir. | |
161 | s#//+#/#g; | |
162 | s#^/##; | |
163 | s#^$#.#; | |
164 | die "Do not use .. in any path!\n" if m#(^|/)\\?\.\\?\.(\\?/|$)#; | |
165 | } | |
166 | push(@args, bsd_glob($_, GLOB_LIMIT|GLOB_NOCHECK|GLOB_BRACE|GLOB_QUOTE)); | |
70318468 | 167 | } |
70318468 | 168 | } |
1524815e | 169 | die "$0: invalid rsync-command syntax or options\n" if $in_options; |
70318468 WD |
170 | |
171 | @args = ( '.' ) if !@args; | |
172 | ||
173 | if ($write_log) { | |
44a82a17 | 174 | my ($mm,$hh) = (localtime)[1,2]; |
106a8ad9 | 175 | my $host = $ENV{SSH_CONNECTION} || 'unknown'; |
1524815e | 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 |
184 | exec(RSYNC, @opts, @args) or die "exec(rsync @opts @args) failed: $? $!"; |
185 | ||
186 | sub check_arg | |
187 | { | |
188 | my($opt, $arg, $type) = @_; | |
85fbfa10 | 189 | $arg =~ s/\\(.)/$1/g; |
b2145610 | 190 | if ($subdir ne '/' && ($type == 3 || ($type == 2 && !$am_sender))) { |
70318468 WD |
191 | $arg =~ s#//#/#g; |
192 | die "Do not use .. in --$opt; anchor the path at the root of your restricted dir.\n" | |
193 | if $arg =~ m#(^|/)\.\.(/|$)#; | |
194 | $arg =~ s#^/#$subdir/#; | |
195 | } | |
196 | $arg; | |
197 | } |