git-remote-only 1.4 KB

123456789101112131415161718192021222324252627282930313233
  1. #!/usr/bin/env python3
  2. #
  3. # Restrict an ssh authorized_keys entry to be used only for git push
  4. # and git fetch. Use thusly:
  5. #
  6. # command="git-remote-only /path/to/repository.git alice@example.org" ssh-rsa ABCDEF....== alice@example.org dedicated git key
  7. #
  8. # You might also want options like no-port-forwarding,no-X11-forwarding,no-agent-forwarding.
  9. #
  10. # Copyright (c) 2017, Grunchweather Associates
  11. #
  12. # Permission to use, copy, modify, and/or distribute this software for any
  13. # purpose with or without fee is hereby granted, provided that the above
  14. # copyright notice and this permission notice appear in all copies.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
  17. # REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  18. # AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
  19. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  20. # LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  21. # OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  22. # PERFORMANCE OF THIS SOFTWARE.
  23. import os, sys, shlex
  24. os.environ.update(GIT_REMOTE_ONLY_COMMAND = " ".join(sys.argv))
  25. cmd = shlex.split(os.getenv("SSH_ORIGINAL_COMMAND", ""))
  26. if len(cmd) == 2 and cmd[0] in ("git-upload-pack", "git-receive-pack") and cmd[1] == sys.argv[1]:
  27. os.execv("/usr/bin/" + cmd[0], cmd)
  28. sys.exit("Not authorized: {}".format(" ".join(cmd)))