This repository has been archived by the owner on Dec 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
gitssh.py
executable file
·159 lines (126 loc) · 5.22 KB
/
gitssh.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python
import os
import shlex
import sys
from twisted.conch.avatar import ConchUser
from twisted.conch.checkers import SSHPublicKeyDatabase
from twisted.conch.error import ConchError
from twisted.conch.ssh import common
from twisted.conch.ssh.session import (ISession,
SSHSession,
SSHSessionProcessProtocol)
from twisted.conch.ssh.factory import SSHFactory
from twisted.conch.ssh.keys import Key
from twisted.cred.portal import IRealm, Portal
from twisted.internet import reactor
from twisted.python import components, log
from zope import interface
log.startLogging(sys.stderr)
class IGitMetadata(interface.Interface):
'API for authentication and access control.'
def repopath(self, username, reponame):
'''
Given a username and repo name, return the full path of the repo on
the file system.
'''
def pubkeys(self, username):
'''
Given a username return a list of OpenSSH compatible public key
strings.
'''
BSHIPK = r'ssh-dss AAAAB3NzaC1kc3MAAACBAJW6g6QMZ7n7SJG3WBUcMTlgYJiuX61CPOMQj0F3srftuB81T5Y7r7T72zsov81crIi+PU5GRO80Umhu68nSPQmfXmYzkqNgU0XFHXuZXYnCW9l6hxn94Qr+XBPZMLwHUQvVzaFneFuFXS0Lx3iKa3HH43OOCjDkDnLVx39rvvUdAAAAFQCEBsryZKy+t8iNwGjKd4Q34F2T/wAAAIAV0CwoJHYCks4WXKTOLA080JaeFwBQEBJZgi5itrHMZJpISPQNrXV7EUTTMYLk5m4rWdiWYX24Witbu8VBwrRW8FMqLpd1x5fXvJrfqFxcJryASl6fCM3PVSmRgRMiJ1MyqKfovjur959k7LEtQsMEDgMnDgBwawHy8t38fMA7rwAAAIAEqVDoUOoZHr6l0Q/I+YpbZiCj1c6Ny5tfl+fy5bt2Ab6I7R3bUMUM2I5ycpGgFpIU96JiXbA35ga7VXOCn5qmAOjOk4DtmGIomi+z4k6kViQr1IFAb366Dyh/CV/Dkm7b9klugjZW/xsiBibS5kgTD1eFxbvvW10a8+8iJLgoiQ== bshi@Bo-Shis-MacBook-Pro.local'
class BallinMockMeta(object):
'Mock persistence layer.'
interface.implements(IGitMetadata)
# To test, you need to fill in pubkeys sequences with real public keys.
def __init__(self):
self.db = {
'jane': {
'pubkeys': ('a', 'b'),
'repos': {
'/foobar.git': '/path/to/foobar.git',
'/project.git': '/path/to/project.git',
},
},
'john': {
'pubkeys': ('c', 'd'),
'repos': {
'/helloworld.git': '/path/to/helloworld.git',
},
},
'bshi': {
'pubkeys': (BSHIPK,),
'repos': {
'/poop.git': '/Users/bshi/sandbox/poop/',
},
}
}
def repopath(self, username, reponame):
if username not in self.db:
return None
return self.db[username]['repos'].get(reponame, None)
def pubkeys(self, username):
if username not in self.db:
return None
return self.db[username]['pubkeys']
def find_git_shell():
# Find git-shell path.
# Adapted from http://bugs.python.org/file15381/shutil_which.patch
path = os.environ.get("PATH", os.defpath)
for dir in path.split(os.pathsep):
full_path = os.path.join(dir, 'git-shell')
if (os.path.exists(full_path) and
os.access(full_path, (os.F_OK | os.X_OK))):
return full_path
raise Exception('Could not find git executable!')
class GitSession(object):
interface.implements(ISession)
def __init__(self, user):
self.user = user
def execCommand(self, proto, cmd):
argv = shlex.split(cmd)
reponame = argv[-1]
sh = self.user.shell
# Check permissions by mapping requested path to file system path
repopath = self.user.meta.repopath(self.user.username, reponame)
if repopath is None:
raise ConchError('Invalid repository.')
command = ' '.join(argv[:-1] + ["'%s'" % (repopath,)])
reactor.spawnProcess(proto, sh,(sh, '-c', command))
def eofReceived(self): pass
def closed(self): pass
class GitConchUser(ConchUser):
shell = find_git_shell()
def __init__(self, username, meta):
ConchUser.__init__(self)
self.username = username
self.channelLookup.update({"session": SSHSession})
self.meta = meta
def logout(self): pass
class GitRealm(object):
interface.implements(IRealm)
def __init__(self, meta):
self.meta = meta
def requestAvatar(self, username, mind, *interfaces):
user = GitConchUser(username, self.meta)
return interfaces[0], user, user.logout
class GitPubKeyChecker(SSHPublicKeyDatabase):
def __init__(self, meta):
self.meta = meta
def checkKey(self, credentials):
for k in self.meta.pubkeys(credentials.username):
if Key.fromString(k).blob() == credentials.blob:
return True
return False
class GitServer(SSHFactory):
authmeta = BallinMockMeta()
portal = Portal(GitRealm(authmeta))
portal.registerChecker(GitPubKeyChecker(authmeta))
def __init__(self, privkey):
pubkey = '.'.join((privkey, 'pub'))
self.privateKeys = {'ssh-rsa': Key.fromFile(privkey)}
self.publicKeys = {'ssh-rsa': Key.fromFile(pubkey)}
if __name__ == '__main__':
components.registerAdapter(GitSession, GitConchUser, ISession)
reactor.listenTCP(2222, GitServer(sys.argv[1]))
reactor.run()