deny-rsync 997 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #!/bin/bash
  2. # Send an error message via the rsync-protocol to a non-daemon client rsync.
  3. #
  4. # Usage: deny-rsync "message"
  5. protocol_version=29
  6. exit_code=4 # same as a daemon that refuses an option
  7. # e.g. byte_escape 29 => \035
  8. function byte_escape {
  9. echo -ne "\\0$(printf "%o" $1)"
  10. }
  11. msg="$1"
  12. if [ "${#msg}" -gt 254 ]; then
  13. # truncate a message that is too long for this naive script to handle
  14. msg="${msg:0:251}..."
  15. fi
  16. msglen=$(( ${#msg} + 1 )) # add 1 for the newline we append below
  17. # Send protocol version. All numbers are LSB-first 4-byte ints.
  18. echo -ne "$(byte_escape $protocol_version)\\000\\000\\000"
  19. # Send a zero checksum seed.
  20. echo -ne "\\000\\000\\000\\000"
  21. # The following is equivalent to rprintf(FERROR_XFER, "%s\n", $msg).
  22. # 1. Message header: ((MPLEX_BASE + FERROR_XFER) << 24) + $msglen.
  23. echo -ne "$(byte_escape $msglen)\\000\\000\\010"
  24. # 2. The actual data.
  25. echo -E "$msg"
  26. # Make sure the client gets our message, not a write failure.
  27. sleep 1
  28. exit $exit_code