hg-ssh
111 lines
| 3.2 KiB
| text/plain
|
TextLexer
/ contrib / hg-ssh
Thomas Arendsen Hein
|
r1537 | #!/usr/bin/env python | ||
# | ||||
Thomas Arendsen Hein
|
r5191 | # Copyright 2005-2007 by Intevation GmbH <intevation@intevation.de> | ||
Martin Geisler
|
r8228 | # | ||
Thomas Arendsen Hein
|
r1537 | # Author(s): | ||
# Thomas Arendsen Hein <thomas@intevation.de> | ||||
# | ||||
Martin Geisler
|
r8225 | # This software may be used and distributed according to the terms of the | ||
Matt Mackall
|
r10263 | # GNU General Public License version 2 or any later version. | ||
Thomas Arendsen Hein
|
r1537 | |||
""" | ||||
hg-ssh - a wrapper for ssh access to a limited set of mercurial repos | ||||
To be used in ~/.ssh/authorized_keys with the "command" option, see sshd(8): | ||||
command="hg-ssh path/to/repo1 /path/to/repo2 ~/repo3 ~user/repo4" ssh-dss ... | ||||
(probably together with these other useful options: | ||||
no-port-forwarding,no-X11-forwarding,no-agent-forwarding) | ||||
Andreas Freimuth
|
r13996 | This allows pull/push over ssh from/to the repositories given as arguments. | ||
Thomas Arendsen Hein
|
r1537 | |||
If all your repositories are subdirectories of a common directory, you can | ||||
allow shorter paths with: | ||||
command="cd path/to/my/repositories && hg-ssh repo1 subdir/repo2" | ||||
Thomas Arendsen Hein
|
r1640 | |||
You can use pattern matching of your normal shell, e.g.: | ||||
command="cd repos && hg-ssh user/thomas/* projects/{mercurial,foo}" | ||||
David Schleimer
|
r16836 | |||
You can also add a --read-only flag to allow read-only access to a key, e.g.: | ||||
command="hg-ssh --read-only repos/*" | ||||
Thomas Arendsen Hein
|
r1537 | """ | ||
Augie Fackler
|
r33892 | from __future__ import absolute_import | ||
import os | ||||
import shlex | ||||
import sys | ||||
Thomas Arendsen Hein
|
r1537 | |||
Thomas Arendsen Hein
|
r5197 | # enable importing on demand to reduce startup time | ||
Gregory Szorc
|
r44058 | import hgdemandimport | ||
hgdemandimport.enable() | ||||
Thomas Arendsen Hein
|
r5197 | |||
Augie Fackler
|
r33892 | from mercurial import ( | ||
dispatch, | ||||
Pulkit Goyal
|
r38121 | pycompat, | ||
Augie Fackler
|
r33892 | ui as uimod, | ||
) | ||||
Thomas Arendsen Hein
|
r1537 | |||
Gregory Szorc
|
r44058 | |||
David Schleimer
|
r16779 | def main(): | ||
Yuya Nishihara
|
r37963 | # Prevent insertion/deletion of CRs | ||
dispatch.initstdio() | ||||
David Schleimer
|
r16779 | cwd = os.getcwd() | ||
David Schleimer
|
r16836 | readonly = False | ||
args = sys.argv[1:] | ||||
while len(args): | ||||
if args[0] == '--read-only': | ||||
readonly = True | ||||
args.pop(0) | ||||
else: | ||||
break | ||||
Gregory Szorc
|
r44058 | allowed_paths = [ | ||
os.path.normpath(os.path.join(cwd, os.path.expanduser(path))) | ||||
for path in args | ||||
] | ||||
David Schleimer
|
r16779 | orig_cmd = os.getenv('SSH_ORIGINAL_COMMAND', '?') | ||
try: | ||||
cmdargv = shlex.split(orig_cmd) | ||||
FUJIWARA Katsunori
|
r28047 | except ValueError as e: | ||
David Schleimer
|
r16779 | sys.stderr.write('Illegal command "%s": %s\n' % (orig_cmd, e)) | ||
sys.exit(255) | ||||
Thomas Arendsen Hein
|
r1537 | |||
David Schleimer
|
r16779 | if cmdargv[:2] == ['hg', '-R'] and cmdargv[3:] == ['serve', '--stdio']: | ||
path = cmdargv[2] | ||||
repo = os.path.normpath(os.path.join(cwd, os.path.expanduser(path))) | ||||
if repo in allowed_paths: | ||||
Pulkit Goyal
|
r38121 | cmd = [b'-R', pycompat.fsencode(repo), b'serve', b'--stdio'] | ||
Augie Fackler
|
r32050 | req = dispatch.request(cmd) | ||
David Schleimer
|
r16836 | if readonly: | ||
Augie Fackler
|
r32050 | if not req.ui: | ||
req.ui = uimod.ui.load() | ||||
Gregory Szorc
|
r44058 | req.ui.setconfig( | ||
b'hooks', | ||||
b'pretxnopen.hg-ssh', | ||||
b'python:__main__.rejectpush', | ||||
b'hg-ssh', | ||||
) | ||||
req.ui.setconfig( | ||||
b'hooks', | ||||
b'prepushkey.hg-ssh', | ||||
b'python:__main__.rejectpush', | ||||
b'hg-ssh', | ||||
) | ||||
Augie Fackler
|
r32050 | dispatch.dispatch(req) | ||
David Schleimer
|
r16779 | else: | ||
sys.stderr.write('Illegal repository "%s"\n' % repo) | ||||
sys.exit(255) | ||||
Thomas Arendsen Hein
|
r1537 | else: | ||
David Schleimer
|
r16779 | sys.stderr.write('Illegal command "%s"\n' % orig_cmd) | ||
Mads Kiilerich
|
r16607 | sys.exit(255) | ||
Thomas Arendsen Hein
|
r1537 | |||
Gregory Szorc
|
r44058 | |||
David Schleimer
|
r16836 | def rejectpush(ui, **kwargs): | ||
Pulkit Goyal
|
r38119 | ui.warn((b"Permission denied\n")) | ||
David Schleimer
|
r16836 | # mercurial hooks use unix process conventions for hook return values | ||
# so a truthy return means failure | ||||
return True | ||||
Gregory Szorc
|
r44058 | |||
David Schleimer
|
r16779 | if __name__ == '__main__': | ||
main() | ||||