author | Cédric Krier <ced@b2ck.com> |
Sun, 14 Dec 2014 20:30:25 +0100 | |
changeset 374 | 7a1d6b228af6 |
parent 306 | f832a8aeef44 |
permissions | -rwxr-xr-x |
0
41ecb5a3172c
separate out executables and data
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
1 |
#!/usr/bin/env python |
41ecb5a3172c
separate out executables and data
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
2 |
|
41ecb5a3172c
separate out executables and data
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
3 |
""" |
10 | 4 |
hg-ssh - limit access to hg repositories reached via ssh. Part of |
36
b3237aabd0fe
Change the name to mercurial-server
Paul Crowley <paul@lshift.net>
parents:
33
diff
changeset
|
5 |
mercurial-server. |
0
41ecb5a3172c
separate out executables and data
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
6 |
|
214
7374d0147875
Script docstring was full of lies - now up to date
Paul Crowley <paul@lshift.net>
parents:
212
diff
changeset
|
7 |
It is called by ssh due to an entry in the authorized_keys file, |
7374d0147875
Script docstring was full of lies - now up to date
Paul Crowley <paul@lshift.net>
parents:
212
diff
changeset
|
8 |
with the name for the key passed on the command line. |
18
538d6b198f4a
Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
15
diff
changeset
|
9 |
|
538d6b198f4a
Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
15
diff
changeset
|
10 |
It uses SSH_ORIGINAL_COMMAND to determine what the user was trying to |
538d6b198f4a
Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
15
diff
changeset
|
11 |
do and to what repository, and then checks each rule in the rule file |
538d6b198f4a
Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
15
diff
changeset
|
12 |
in turn for a matching rule which decides what to do, defaulting to |
538d6b198f4a
Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
15
diff
changeset
|
13 |
disallowing the action. |
538d6b198f4a
Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents:
15
diff
changeset
|
14 |
|
0
41ecb5a3172c
separate out executables and data
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
15 |
""" |
41ecb5a3172c
separate out executables and data
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
16 |
|
41ecb5a3172c
separate out executables and data
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
17 |
# enable importing on demand to reduce startup time |
41ecb5a3172c
separate out executables and data
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
18 |
from mercurial import demandimport; demandimport.enable() |
41ecb5a3172c
separate out executables and data
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
19 |
|
41ecb5a3172c
separate out executables and data
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
20 |
from mercurial import dispatch |
41ecb5a3172c
separate out executables and data
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
21 |
|
304
6e575b602d2b
Mercurial 1.9 compatibility
Andrej Krpic <akrpic77@gmail.com>
parents:
303
diff
changeset
|
22 |
try: |
306
f832a8aeef44
Replace 1.9 test with one that works with demandimport
Paul Crowley <paul@lshift.net>
parents:
305
diff
changeset
|
23 |
request = dispatch.request |
f832a8aeef44
Replace 1.9 test with one that works with demandimport
Paul Crowley <paul@lshift.net>
parents:
305
diff
changeset
|
24 |
except AttributeError: |
304
6e575b602d2b
Mercurial 1.9 compatibility
Andrej Krpic <akrpic77@gmail.com>
parents:
303
diff
changeset
|
25 |
request = list |
6e575b602d2b
Mercurial 1.9 compatibility
Andrej Krpic <akrpic77@gmail.com>
parents:
303
diff
changeset
|
26 |
|
106
0519745e7a57
Much less strict about most things
Paul Crowley <paul@lshift.net>
parents:
103
diff
changeset
|
27 |
import sys, os, os.path |
107
84e9e33d866b
Fixes, plus base64 what you don't trust
Paul Crowley <paul@lshift.net>
parents:
106
diff
changeset
|
28 |
import base64 |
211
0cd59649772c
Rename paths.py ot config.py
Paul Crowley <paul@lshift.net>
parents:
165
diff
changeset
|
29 |
from mercurialserver import config, ruleset |
0
41ecb5a3172c
separate out executables and data
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
30 |
|
41ecb5a3172c
separate out executables and data
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
31 |
def fail(message): |
106
0519745e7a57
Much less strict about most things
Paul Crowley <paul@lshift.net>
parents:
103
diff
changeset
|
32 |
sys.stderr.write("mercurial-server: %s\n" % message) |
0
41ecb5a3172c
separate out executables and data
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
33 |
sys.exit(-1) |
41ecb5a3172c
separate out executables and data
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
34 |
|
303 | 35 |
config.initExe() |
36 |
||
37 |
for k,v in config.getEnv(): |
|
38 |
os.environ[k.upper()] = v |
|
39 |
||
40 |
if len(sys.argv) == 3 and sys.argv[1] == "--base64": |
|
374 | 41 |
user = base64.b64decode(sys.argv[2]) |
42 |
ruleset.rules.set(user = user) |
|
303 | 43 |
elif len(sys.argv) == 2: |
374 | 44 |
user = sys.argv[1] |
45 |
ruleset.rules.set(user = user) |
|
303 | 46 |
else: |
47 |
fail("hg-ssh wrongly called, is authorized_keys corrupt? (%s)" |
|
48 |
% sys.argv) |
|
49 |
||
374 | 50 |
paths = [] |
51 |
path = user |
|
52 |
while path: |
|
53 |
path, tail = os.path.split(path) |
|
54 |
paths.append(tail) |
|
55 |
paths.reverse() |
|
56 |
i = config.getUserPathIndex() |
|
57 |
if i >= 0 and i < len(paths): |
|
58 |
user = paths[i] |
|
59 |
os.environ['LOGNAME'] = user |
|
60 |
||
303 | 61 |
os.chdir(config.getReposPath()) |
62 |
||
63 |
for f in config.getAccessPaths(): |
|
64 |
if os.path.isfile(f): |
|
65 |
ruleset.rules.readfile(f) |
|
66 |
||
67 |
alloweddots = config.getAllowedDots() |
|
68 |
||
69 |
def dotException(pathtail): |
|
70 |
for ex in alloweddots: |
|
71 |
splex = ex.split("/") |
|
72 |
if len(pathtail) >= len(splex) and pathtail[:len(splex)] == splex: |
|
73 |
return True |
|
74 |
return False |
|
75 |
||
76 |
def checkDots(path, pathtail = []): |
|
110
69596fffcf7d
Less canonicalisation, use os.path to check for dotfiles
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
77 |
head, tail = os.path.split(path) |
303 | 78 |
pathtail = [tail] + pathtail |
79 |
if tail.startswith(".") and not dotException(pathtail): |
|
80 |
fail("paths cannot contain dot file components") |
|
110
69596fffcf7d
Less canonicalisation, use os.path to check for dotfiles
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
81 |
if head: |
303 | 82 |
checkDots(head, pathtail) |
110
69596fffcf7d
Less canonicalisation, use os.path to check for dotfiles
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
83 |
|
106
0519745e7a57
Much less strict about most things
Paul Crowley <paul@lshift.net>
parents:
103
diff
changeset
|
84 |
def getrepo(op, repo): |
110
69596fffcf7d
Less canonicalisation, use os.path to check for dotfiles
Paul Crowley <paul@lshift.net>
parents:
109
diff
changeset
|
85 |
# First canonicalise, then check the string, then the rules |
244
48fab30c38e1
Strip repo name in case of tortoiseHG
Paul Crowley <paul@lshift.net>
parents:
242
diff
changeset
|
86 |
repo = repo.strip().rstrip("/") |
106
0519745e7a57
Much less strict about most things
Paul Crowley <paul@lshift.net>
parents:
103
diff
changeset
|
87 |
if len(repo) == 0: |
0519745e7a57
Much less strict about most things
Paul Crowley <paul@lshift.net>
parents:
103
diff
changeset
|
88 |
fail("path to repository seems to be empty") |
0519745e7a57
Much less strict about most things
Paul Crowley <paul@lshift.net>
parents:
103
diff
changeset
|
89 |
if repo.startswith("/"): |
0519745e7a57
Much less strict about most things
Paul Crowley <paul@lshift.net>
parents:
103
diff
changeset
|
90 |
fail("absolute paths are not supported") |
117 | 91 |
checkDots(repo) |
106
0519745e7a57
Much less strict about most things
Paul Crowley <paul@lshift.net>
parents:
103
diff
changeset
|
92 |
ruleset.rules.set(repo=repo) |
109
72100d3ed1bd
Don't mix exceptions and sys.exit based failures
Paul Crowley <paul@lshift.net>
parents:
108
diff
changeset
|
93 |
if not ruleset.rules.allow(op, branch=None, file=None): |
72100d3ed1bd
Don't mix exceptions and sys.exit based failures
Paul Crowley <paul@lshift.net>
parents:
108
diff
changeset
|
94 |
fail("access denied") |
106
0519745e7a57
Much less strict about most things
Paul Crowley <paul@lshift.net>
parents:
103
diff
changeset
|
95 |
return repo |
0
41ecb5a3172c
separate out executables and data
Paul Crowley <paul@lshift.net>
parents:
diff
changeset
|
96 |
|
108
00b48d7bdfa0
Improve check on whether user supplied a command
Paul Crowley <paul@lshift.net>
parents:
107
diff
changeset
|
97 |
cmd = os.environ.get('SSH_ORIGINAL_COMMAND', None) |
109
72100d3ed1bd
Don't mix exceptions and sys.exit based failures
Paul Crowley <paul@lshift.net>
parents:
108
diff
changeset
|
98 |
if cmd is None: |
72100d3ed1bd
Don't mix exceptions and sys.exit based failures
Paul Crowley <paul@lshift.net>
parents:
108
diff
changeset
|
99 |
fail("direct logins on the hg account prohibited") |
72100d3ed1bd
Don't mix exceptions and sys.exit based failures
Paul Crowley <paul@lshift.net>
parents:
108
diff
changeset
|
100 |
elif cmd.startswith('hg -R ') and cmd.endswith(' serve --stdio'): |
72100d3ed1bd
Don't mix exceptions and sys.exit based failures
Paul Crowley <paul@lshift.net>
parents:
108
diff
changeset
|
101 |
repo = getrepo("read", cmd[6:-14]) |
72100d3ed1bd
Don't mix exceptions and sys.exit based failures
Paul Crowley <paul@lshift.net>
parents:
108
diff
changeset
|
102 |
if not os.path.isdir(repo + "/.hg"): |
72100d3ed1bd
Don't mix exceptions and sys.exit based failures
Paul Crowley <paul@lshift.net>
parents:
108
diff
changeset
|
103 |
fail("no such repository %s" % repo) |
304
6e575b602d2b
Mercurial 1.9 compatibility
Andrej Krpic <akrpic77@gmail.com>
parents:
303
diff
changeset
|
104 |
dispatch.dispatch(request(['-R', repo, 'serve', '--stdio'])) |
109
72100d3ed1bd
Don't mix exceptions and sys.exit based failures
Paul Crowley <paul@lshift.net>
parents:
108
diff
changeset
|
105 |
elif cmd.startswith('hg init '): |
72100d3ed1bd
Don't mix exceptions and sys.exit based failures
Paul Crowley <paul@lshift.net>
parents:
108
diff
changeset
|
106 |
repo = getrepo("init", cmd[8:]) |
72100d3ed1bd
Don't mix exceptions and sys.exit based failures
Paul Crowley <paul@lshift.net>
parents:
108
diff
changeset
|
107 |
if os.path.exists(repo): |
72100d3ed1bd
Don't mix exceptions and sys.exit based failures
Paul Crowley <paul@lshift.net>
parents:
108
diff
changeset
|
108 |
fail("%s exists" % repo) |
72100d3ed1bd
Don't mix exceptions and sys.exit based failures
Paul Crowley <paul@lshift.net>
parents:
108
diff
changeset
|
109 |
d = os.path.dirname(repo) |
72100d3ed1bd
Don't mix exceptions and sys.exit based failures
Paul Crowley <paul@lshift.net>
parents:
108
diff
changeset
|
110 |
if d != "" and not os.path.isdir(d): |
72100d3ed1bd
Don't mix exceptions and sys.exit based failures
Paul Crowley <paul@lshift.net>
parents:
108
diff
changeset
|
111 |
os.makedirs(d) |
304
6e575b602d2b
Mercurial 1.9 compatibility
Andrej Krpic <akrpic77@gmail.com>
parents:
303
diff
changeset
|
112 |
dispatch.dispatch(request(['init', repo])) |
109
72100d3ed1bd
Don't mix exceptions and sys.exit based failures
Paul Crowley <paul@lshift.net>
parents:
108
diff
changeset
|
113 |
else: |
72100d3ed1bd
Don't mix exceptions and sys.exit based failures
Paul Crowley <paul@lshift.net>
parents:
108
diff
changeset
|
114 |
fail("illegal command %r" % cmd) |