Annotation of embedaddon/rsync/support/rrsync, revision 1.1.1.4

1.1.1.4 ! misho       1: #!/usr/bin/env perl
1.1       misho       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 <js-cgi@inwap.com> 30-Sep-2004
1.1.1.4 ! misho       5: # Modified by: Wayne Davison <wayne@opencoder.net>
1.1       misho       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;
1.1.1.3   misho      18: Use 'command="$0 [-ro|-wo] SUBDIR"'
1.1       misho      19:        in front of lines in $ENV{HOME}/.ssh/authorized_keys
                     20: EOM
                     21: 
1.1.1.3   misho      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: 
1.1       misho      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: #
1.1.1.3   misho      44: # Format of the environment variables set by sshd:
1.1       misho      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!
1.1.1.3   misho      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;
1.1       misho      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: 
1.1.1.4 ! misho      65: our $short_no_arg = 'ACDEHIJKLORSUWXbcdgklmnopqrstuvxyz'; # DO NOT REMOVE ANY
        !            66: our $short_with_num = '@B'; # DO NOT REMOVE ANY
1.1       misho      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,
1.1.1.3   misho      74:   'block-size' => 1,
1.1       misho      75:   'bwlimit' => 1,
1.1.1.4 ! misho      76:   'checksum-choice' => 1,
1.1       misho      77:   'checksum-seed' => 1,
                     78:   'compare-dest' => 2,
1.1.1.4 ! misho      79:   'compress-choice' => 1,
1.1       misho      80:   'compress-level' => 1,
                     81:   'copy-dest' => 2,
                     82:   'copy-unsafe-links' => 0,
                     83:   'daemon' => -1,
1.1.1.3   misho      84:   'debug' => 1,
1.1       misho      85:   'delay-updates' => 0,
                     86:   'delete' => 0,
                     87:   'delete-after' => 0,
                     88:   'delete-before' => 0,
                     89:   'delete-delay' => 0,
                     90:   'delete-during' => 0,
                     91:   'delete-excluded' => 0,
1.1.1.2   misho      92:   'delete-missing-args' => 0,
1.1       misho      93:   'existing' => 0,
                     94:   'fake-super' => 0,
                     95:   'files-from' => 3,
                     96:   'force' => 0,
                     97:   'from0' => 0,
                     98:   'fuzzy' => 0,
1.1.1.3   misho      99:   'group' => 0,
1.1.1.2   misho     100:   'groupmap' => 1,
1.1.1.3   misho     101:   'hard-links' => 0,
1.1       misho     102:   'iconv' => 1,
                    103:   'ignore-errors' => 0,
                    104:   'ignore-existing' => 0,
1.1.1.2   misho     105:   'ignore-missing-args' => 0,
1.1.1.3   misho     106:   'ignore-times' => 0,
                    107:   'info' => 1,
1.1       misho     108:   'inplace' => 0,
                    109:   'link-dest' => 2,
1.1.1.3   misho     110:   'links' => 0,
1.1       misho     111:   'list-only' => 0,
1.1.1.4 ! misho     112:   'log-file' => $only eq 'r' ? -1 : 3,
1.1       misho     113:   'log-format' => 1,
1.1.1.4 ! misho     114:   'max-alloc' => 1,
1.1       misho     115:   'max-delete' => 1,
                    116:   'max-size' => 1,
                    117:   'min-size' => 1,
                    118:   'modify-window' => 1,
1.1.1.3   misho     119:   'new-compress' => 0,
1.1       misho     120:   'no-implied-dirs' => 0,
                    121:   'no-r' => 0,
                    122:   'no-relative' => 0,
                    123:   'no-specials' => 0,
                    124:   'numeric-ids' => 0,
1.1.1.4 ! misho     125:   'old-compress' => 0,
1.1.1.3   misho     126:   'one-file-system' => 0,
1.1       misho     127:   'only-write-batch' => 1,
1.1.1.4 ! misho     128:   'open-noatime' => 0,
1.1.1.3   misho     129:   'owner' => 0,
1.1       misho     130:   'partial' => 0,
                    131:   'partial-dir' => 2,
1.1.1.3   misho     132:   'perms' => 0,
                    133:   'preallocate' => 0,
                    134:   'recursive' => 0,
                    135:   'remove-sent-files' => $only eq 'r' ? -1 : 0,
                    136:   'remove-source-files' => $only eq 'r' ? -1 : 0,
1.1       misho     137:   'safe-links' => 0,
1.1.1.4 ! misho     138:   'sender' => $only eq 'w' ? -1 : 0,
1.1       misho     139:   'server' => 0,
                    140:   'size-only' => 0,
                    141:   'skip-compress' => 1,
                    142:   'specials' => 0,
                    143:   'stats' => 0,
                    144:   'suffix' => 1,
                    145:   'super' => 0,
                    146:   'temp-dir' => 2,
                    147:   'timeout' => 1,
1.1.1.3   misho     148:   'times' => 0,
1.1       misho     149:   'use-qsort' => 0,
1.1.1.2   misho     150:   'usermap' => 1,
1.1.1.4 ! misho     151:   'write-devices' => -1,
1.1       misho     152: );
                    153: 
                    154: ### END of options data produced by the cull_options script. ###
                    155: 
                    156: if ($short_disabled ne '') {
                    157:     $short_no_arg =~ s/[$short_disabled]//go;
                    158:     $short_with_num =~ s/[$short_disabled]//go;
                    159: }
                    160: $short_no_arg = "[$short_no_arg]" if length($short_no_arg) > 1;
                    161: $short_with_num = "[$short_with_num]" if length($short_with_num) > 1;
                    162: 
                    163: my $write_log = -f LOGFILE && open(LOG, '>>', LOGFILE);
                    164: 
                    165: chdir($subdir) or die "$0: Unable to chdir to restricted dir: $!\n";
                    166: 
                    167: my(@opts, @args);
                    168: my $in_options = 1;
                    169: my $last_opt = '';
                    170: my $check_type;
                    171: while ($command =~ /((?:[^\s\\]+|\\.[^\s\\]*)+)/g) {
                    172:   $_ = $1;
                    173:   if ($check_type) {
                    174:     push(@opts, check_arg($last_opt, $_, $check_type));
                    175:     $check_type = 0;
                    176:   } elsif ($in_options) {
                    177:     push(@opts, $_);
                    178:     if ($_ eq '.') {
                    179:       $in_options = 0;
                    180:     } else {
                    181:       die "$0: invalid option: '-'\n" if $_ eq '-';
                    182:       next if /^-$short_no_arg*(e\d*\.\w*)?$/o || /^-$short_with_num\d+$/o;
                    183: 
                    184:       my($opt,$arg) = /^--([^=]+)(?:=(.*))?$/;
                    185:       my $disabled;
                    186:       if (defined $opt) {
                    187:        my $ct = $long_opt{$opt};
                    188:        last unless defined $ct;
                    189:        next if $ct == 0;
                    190:        if ($ct > 0) {
                    191:          if (!defined $arg) {
                    192:            $check_type = $ct;
                    193:            $last_opt = $opt;
                    194:            next;
                    195:          }
                    196:          $arg = check_arg($opt, $arg, $ct);
                    197:          $opts[-1] =~ s/=.*/=$arg/;
                    198:          next;
                    199:        }
                    200:        $disabled = 1;
                    201:        $opt = "--$opt";
                    202:       } elsif ($short_disabled ne '') {
                    203:        $disabled = /^-$short_no_arg*([$short_disabled])/o;
                    204:        $opt = "-$1";
                    205:       }
                    206: 
                    207:       last unless $disabled; # Generate generic failure
                    208:       die "$0: option $opt has been disabled on this server.\n";
                    209:     }
                    210:   } else {
                    211:     if ($subdir ne '/') {
                    212:       # Validate args to ensure they don't try to leave our restricted dir.
                    213:       s{//+}{/}g;
                    214:       s{^/}{};
                    215:       s{^$}{.};
                    216:     }
                    217:     push(@args, bsd_glob($_, GLOB_LIMIT|GLOB_NOCHECK|GLOB_BRACE|GLOB_QUOTE));
                    218:   }
                    219: }
                    220: die "$0: invalid rsync-command syntax or options\n" if $in_options;
                    221: 
1.1.1.4 ! misho     222: if ($subdir ne '/') {
        !           223:     die "$0: do not use .. in any path!\n" if grep m{(^|/)\.\.(/|$)}, @args;
        !           224: }
        !           225: 
1.1       misho     226: @args = ( '.' ) if !@args;
                    227: 
                    228: if ($write_log) {
                    229:   my ($mm,$hh) = (localtime)[1,2];
                    230:   my $host = $ENV{SSH_CONNECTION} || 'unknown';
                    231:   $host =~ s/ .*//; # Keep only the client's IP addr
                    232:   $host =~ s/^::ffff://;
                    233:   $host = gethostbyaddr(inet_aton($host),AF_INET) || $host;
                    234:   printf LOG "%02d:%02d %-13s [%s]\n", $hh, $mm, $host, "@opts @args";
                    235:   close LOG;
                    236: }
                    237: 
                    238: # Note: This assumes that the rsync protocol will not be maliciously hijacked.
1.1.1.4 ! misho     239: exec(RSYNC, @opts, '--', @args) or die "exec(rsync @opts -- @args) failed: $? $!";
1.1       misho     240: 
                    241: sub check_arg
                    242: {
                    243:   my($opt, $arg, $type) = @_;
                    244:   $arg =~ s/\\(.)/$1/g;
                    245:   if ($subdir ne '/' && ($type == 3 || ($type == 2 && !$am_sender))) {
                    246:     $arg =~ s{//}{/}g;
                    247:     die "Do not use .. in --$opt; anchor the path at the root of your restricted dir.\n"
                    248:       if $arg =~ m{(^|/)\.\.(/|$)};
                    249:     $arg =~ s{^/}{$subdir/};
                    250:   }
                    251:   $arg;
                    252: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>