~certify-web-dev/twisted/certify-trunk

« back to all changes in this revision

Viewing changes to twisted/conch/openssh_compat/factory.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-01-17 14:52:35 UTC
  • mfrom: (1.1.5 upstream) (2.1.2 etch)
  • Revision ID: james.westby@ubuntu.com-20070117145235-btmig6qfmqfen0om
Tags: 2.5.0-0ubuntu1
New upstream version, compatible with python2.5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from twisted.conch.ssh import keys, factory, common
 
2
from twisted.python import log
 
3
import primes
 
4
import os
 
5
 
 
6
class OpenSSHFactory(factory.SSHFactory):
 
7
    dataRoot = '/usr/local/etc'
 
8
    moduliRoot = '/usr/local/etc' # for openbsd which puts moduli in a different
 
9
                                  # directory from keys
 
10
    def getPublicKeys(self):
 
11
        ks = {}
 
12
        for file in os.listdir(self.dataRoot):
 
13
            if file[:9] == 'ssh_host_' and file[-8:]=='_key.pub':
 
14
                try:
 
15
                    k = keys.getPublicKeyString(self.dataRoot+'/'+file)
 
16
                    t = common.getNS(k)[0]
 
17
                    ks[t] = k
 
18
                except Exception, e:
 
19
                    log.msg('bad public key file %s: %s' % (file,e))
 
20
        return ks
 
21
    def getPrivateKeys(self):
 
22
        ks = {}
 
23
        euid,egid = os.geteuid(), os.getegid()
 
24
        os.setegid(0) # gain priviledges
 
25
        os.seteuid(0)
 
26
        for file in os.listdir(self.dataRoot):
 
27
            if file[:9] == 'ssh_host_' and file[-4:]=='_key':
 
28
                try:
 
29
                    k = keys.getPrivateKeyObject(self.dataRoot+'/'+file)
 
30
                    t = keys.objectType(k)
 
31
                    ks[t] = k
 
32
                except Exception, e:
 
33
                    log.msg('bad private key file %s: %s' % (file, e))
 
34
        os.setegid(egid) # drop them just as quickily
 
35
        os.seteuid(euid)
 
36
        return ks
 
37
 
 
38
    def getPrimes(self):
 
39
        try:
 
40
            return primes.parseModuliFile(self.moduliRoot+'/moduli')
 
41
        except IOError:
 
42
            return None
 
43