Show More
@@ -1,96 +1,97 | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 | # |
|
2 | # | |
3 | # Copyright 2005-2007 by Intevation GmbH <intevation@intevation.de> |
|
3 | # Copyright 2005-2007 by Intevation GmbH <intevation@intevation.de> | |
4 | # |
|
4 | # | |
5 | # Author(s): |
|
5 | # Author(s): | |
6 | # Thomas Arendsen Hein <thomas@intevation.de> |
|
6 | # Thomas Arendsen Hein <thomas@intevation.de> | |
7 | # |
|
7 | # | |
8 | # This software may be used and distributed according to the terms of the |
|
8 | # This software may be used and distributed according to the terms of the | |
9 | # GNU General Public License version 2 or any later version. |
|
9 | # GNU General Public License version 2 or any later version. | |
10 |
|
10 | |||
11 | """ |
|
11 | """ | |
12 | hg-ssh - a wrapper for ssh access to a limited set of mercurial repos |
|
12 | hg-ssh - a wrapper for ssh access to a limited set of mercurial repos | |
13 |
|
13 | |||
14 | To be used in ~/.ssh/authorized_keys with the "command" option, see sshd(8): |
|
14 | To be used in ~/.ssh/authorized_keys with the "command" option, see sshd(8): | |
15 | command="hg-ssh path/to/repo1 /path/to/repo2 ~/repo3 ~user/repo4" ssh-dss ... |
|
15 | command="hg-ssh path/to/repo1 /path/to/repo2 ~/repo3 ~user/repo4" ssh-dss ... | |
16 | (probably together with these other useful options: |
|
16 | (probably together with these other useful options: | |
17 | no-port-forwarding,no-X11-forwarding,no-agent-forwarding) |
|
17 | no-port-forwarding,no-X11-forwarding,no-agent-forwarding) | |
18 |
|
18 | |||
19 | This allows pull/push over ssh from/to the repositories given as arguments. |
|
19 | This allows pull/push over ssh from/to the repositories given as arguments. | |
20 |
|
20 | |||
21 | If all your repositories are subdirectories of a common directory, you can |
|
21 | If all your repositories are subdirectories of a common directory, you can | |
22 | allow shorter paths with: |
|
22 | allow shorter paths with: | |
23 | command="cd path/to/my/repositories && hg-ssh repo1 subdir/repo2" |
|
23 | command="cd path/to/my/repositories && hg-ssh repo1 subdir/repo2" | |
24 |
|
24 | |||
25 | You can use pattern matching of your normal shell, e.g.: |
|
25 | You can use pattern matching of your normal shell, e.g.: | |
26 | command="cd repos && hg-ssh user/thomas/* projects/{mercurial,foo}" |
|
26 | command="cd repos && hg-ssh user/thomas/* projects/{mercurial,foo}" | |
27 |
|
27 | |||
28 | You can also add a --read-only flag to allow read-only access to a key, e.g.: |
|
28 | You can also add a --read-only flag to allow read-only access to a key, e.g.: | |
29 | command="hg-ssh --read-only repos/*" |
|
29 | command="hg-ssh --read-only repos/*" | |
30 | """ |
|
30 | """ | |
31 | from __future__ import absolute_import |
|
31 | from __future__ import absolute_import | |
32 |
|
32 | |||
33 | import os |
|
33 | import os | |
34 | import shlex |
|
34 | import shlex | |
35 | import sys |
|
35 | import sys | |
36 |
|
36 | |||
37 | # enable importing on demand to reduce startup time |
|
37 | # enable importing on demand to reduce startup time | |
38 | import hgdemandimport ; hgdemandimport.enable() |
|
38 | import hgdemandimport ; hgdemandimport.enable() | |
39 |
|
39 | |||
40 | from mercurial import ( |
|
40 | from mercurial import ( | |
41 | dispatch, |
|
41 | dispatch, | |
|
42 | pycompat, | |||
42 | ui as uimod, |
|
43 | ui as uimod, | |
43 | ) |
|
44 | ) | |
44 |
|
45 | |||
45 | def main(): |
|
46 | def main(): | |
46 | # Prevent insertion/deletion of CRs |
|
47 | # Prevent insertion/deletion of CRs | |
47 | dispatch.initstdio() |
|
48 | dispatch.initstdio() | |
48 |
|
49 | |||
49 | cwd = os.getcwd() |
|
50 | cwd = os.getcwd() | |
50 | readonly = False |
|
51 | readonly = False | |
51 | args = sys.argv[1:] |
|
52 | args = sys.argv[1:] | |
52 | while len(args): |
|
53 | while len(args): | |
53 | if args[0] == '--read-only': |
|
54 | if args[0] == '--read-only': | |
54 | readonly = True |
|
55 | readonly = True | |
55 | args.pop(0) |
|
56 | args.pop(0) | |
56 | else: |
|
57 | else: | |
57 | break |
|
58 | break | |
58 | allowed_paths = [os.path.normpath(os.path.join(cwd, |
|
59 | allowed_paths = [os.path.normpath(os.path.join(cwd, | |
59 | os.path.expanduser(path))) |
|
60 | os.path.expanduser(path))) | |
60 | for path in args] |
|
61 | for path in args] | |
61 | orig_cmd = os.getenv('SSH_ORIGINAL_COMMAND', '?') |
|
62 | orig_cmd = os.getenv('SSH_ORIGINAL_COMMAND', '?') | |
62 | try: |
|
63 | try: | |
63 | cmdargv = shlex.split(orig_cmd) |
|
64 | cmdargv = shlex.split(orig_cmd) | |
64 | except ValueError as e: |
|
65 | except ValueError as e: | |
65 | sys.stderr.write('Illegal command "%s": %s\n' % (orig_cmd, e)) |
|
66 | sys.stderr.write('Illegal command "%s": %s\n' % (orig_cmd, e)) | |
66 | sys.exit(255) |
|
67 | sys.exit(255) | |
67 |
|
68 | |||
68 | if cmdargv[:2] == ['hg', '-R'] and cmdargv[3:] == ['serve', '--stdio']: |
|
69 | if cmdargv[:2] == ['hg', '-R'] and cmdargv[3:] == ['serve', '--stdio']: | |
69 | path = cmdargv[2] |
|
70 | path = cmdargv[2] | |
70 | repo = os.path.normpath(os.path.join(cwd, os.path.expanduser(path))) |
|
71 | repo = os.path.normpath(os.path.join(cwd, os.path.expanduser(path))) | |
71 | if repo in allowed_paths: |
|
72 | if repo in allowed_paths: | |
72 | cmd = [b'-R', repo, b'serve', b'--stdio'] |
|
73 | cmd = [b'-R', pycompat.fsencode(repo), b'serve', b'--stdio'] | |
73 | req = dispatch.request(cmd) |
|
74 | req = dispatch.request(cmd) | |
74 | if readonly: |
|
75 | if readonly: | |
75 | if not req.ui: |
|
76 | if not req.ui: | |
76 | req.ui = uimod.ui.load() |
|
77 | req.ui = uimod.ui.load() | |
77 | req.ui.setconfig(b'hooks', b'pretxnopen.hg-ssh', |
|
78 | req.ui.setconfig(b'hooks', b'pretxnopen.hg-ssh', | |
78 | b'python:__main__.rejectpush', b'hg-ssh') |
|
79 | b'python:__main__.rejectpush', b'hg-ssh') | |
79 | req.ui.setconfig(b'hooks', b'prepushkey.hg-ssh', |
|
80 | req.ui.setconfig(b'hooks', b'prepushkey.hg-ssh', | |
80 | b'python:__main__.rejectpush', b'hg-ssh') |
|
81 | b'python:__main__.rejectpush', b'hg-ssh') | |
81 | dispatch.dispatch(req) |
|
82 | dispatch.dispatch(req) | |
82 | else: |
|
83 | else: | |
83 | sys.stderr.write('Illegal repository "%s"\n' % repo) |
|
84 | sys.stderr.write('Illegal repository "%s"\n' % repo) | |
84 | sys.exit(255) |
|
85 | sys.exit(255) | |
85 | else: |
|
86 | else: | |
86 | sys.stderr.write('Illegal command "%s"\n' % orig_cmd) |
|
87 | sys.stderr.write('Illegal command "%s"\n' % orig_cmd) | |
87 | sys.exit(255) |
|
88 | sys.exit(255) | |
88 |
|
89 | |||
89 | def rejectpush(ui, **kwargs): |
|
90 | def rejectpush(ui, **kwargs): | |
90 | ui.warn((b"Permission denied\n")) |
|
91 | ui.warn((b"Permission denied\n")) | |
91 | # mercurial hooks use unix process conventions for hook return values |
|
92 | # mercurial hooks use unix process conventions for hook return values | |
92 | # so a truthy return means failure |
|
93 | # so a truthy return means failure | |
93 | return True |
|
94 | return True | |
94 |
|
95 | |||
95 | if __name__ == '__main__': |
|
96 | if __name__ == '__main__': | |
96 | main() |
|
97 | main() |
General Comments 0
You need to be logged in to leave comments.
Login now