rrsync 6.4 KB

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