~dustin-spy/twisted/dustin

« back to all changes in this revision

Viewing changes to twisted/conch/factory.py

  • Committer: z3p
  • Date: 2002-07-17 14:44:36 UTC
  • Revision ID: vcs-imports@canonical.com-20020717144436-6dce525e73d836e2
moving secsh to conch.
Conch: The Twisted Shell

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import md5, os
 
2
 
 
3
from twisted.internet import protocol
 
4
 
 
5
import common, userauth, keys, transport, primes, connection
 
6
 
 
7
class Nothing: pass
 
8
class SSHFactory(protocol.Factory):
 
9
    services = {
 
10
        'ssh-userauth':userauth.SSHUserAuthServer,
 
11
        'ssh-connection':connection.SSHConnection
 
12
    }
 
13
    def startFactory(self):
 
14
        if not hasattr(self,'publicKeys'):
 
15
            self.publicKeys = self.getPublicKey()
 
16
        if not hasattr(self,'privateKeys'):
 
17
            self.privateKeys = self.getPrivateKey()
 
18
        if not hasattr(self,'primes'):
 
19
            self.primes = self.getPrimes()
 
20
 
 
21
    def buildProtocol(self, addr):
 
22
        t = transport.SSHServerTransport()
 
23
        t.supportedPublicKeys = self.privateKeys.keys()
 
24
        t.factory = self
 
25
        return t
 
26
 
 
27
    def getFingerprint(self):
 
28
        return ':'.join(map(lambda c:'%02x'%ord(c),md5.new(self.publicKey).digest()))
 
29
 
 
30
    def getDHPrime(self, bits):
 
31
        # returns g, p
 
32
        return primes.getDHPrimeOfBits(self.primes, bits)
 
33
 
 
34
class OpenSSHFactory(SSHFactory):
 
35
    dataRoot = '/usr/local/etc'
 
36
    def getPublicKey(self):
 
37
        ks = {}
 
38
        for file in os.listdir(self.dataRoot):
 
39
            if file[:9] == 'ssh_host_' and file[-8:]=='_key.pub':
 
40
                try:
 
41
                    k = keys.getPublicKeyString(self.dataRoot+'/'+file)
 
42
                    t = common.getNS(k)[0]
 
43
                    ks[t] = k
 
44
                except:
 
45
                    print 'bad key file', file
 
46
        return ks
 
47
    def getPrivateKey(self):
 
48
        ks = {}
 
49
        for file in os.listdir(self.dataRoot):
 
50
            if file[:9] == 'ssh_host_' and file[-4:]=='_key':
 
51
                try:
 
52
                    k = keys.getPrivateKeyObject(self.dataRoot+'/'+file)
 
53
                    t = keys.objectType(k)
 
54
                    ks[t] = k
 
55
                except:
 
56
                    print 'bad key file', file
 
57
        return ks
 
58
    def getPrimes(self):
 
59
        return primes.parseModuliFile(self.dataRoot+'/moduli')
 
60
 
 
61
 
 
62