summaryrefslogtreecommitdiffstats
path: root/generators/sshbase.py
blob: 5ab69a015aeec1c60b0deac61af8c678e1833546 (plain)
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
#!/usr/bin/env python

from glob import glob
from os import rename, stat
from socket import gethostbyname

from Types import ConfigFile
from Generator import Generator

class sshbase(Generator):
    __name__ = 'sshbase'
    __version__ = '$Id$'
    __author__ = 'bcfg-dev@mcs.anl.gov'
    __build__ = { '/etc/ssh/ssh_known_hosts':'build_skn',
                  '/etc/ssh/ssh_host_dsa_key':'build_hk',
                  '/etc/ssh/ssh_host_rsa_key':'build_hk',
                  '/etc/ssh/ssh_host_dsa_key.pub':'build_hk',
                  '/etc/ssh/ssh_host_rsa_key.pub':'build_hk'}

    def build_skn(self,name,client):
        data=file("%s/ssh_known_hosts"%(self.data)).read()
        ip=gethostbyname(client)
        for hostkey in ["ssh_host_dsa_key.pub.H_%s","ssh_host_rsa_key.pub.H_%s","ssh_host_key.pub.H_%s"]:
            filename="%s/%s"%(self.data,hostkey)%(client)
            hdata=file(filename).read()
            data+="%s,%s,%s %s"%(client,"%s.mcs.anl.gov"%(client),ip,hdata)
        return ConfigFile(name,'root','root','0644',data)

    def build_hk(self,name,client):
        reponame="%s/%s.H_%s"%(self.data,name.split('/')[-1],client)
        try:
            stat(reponame)
        except IOError:
            self.GenerateHostKeys(client)
            self.GenerateKnownHosts()
        # then we read the data file
        keydata=file(reponame).read()
        if "ssh_host_key.H_" in reponame:
            return ConfigFile(name,'root','root','0600',keydata,'base64')
        return ConfigFile(name,'root','root','0600',keydata)

    def GenerateKnownHosts(self):
        output=file("%s/ssh_known_hosts"%(self.__data__),'w')
        for f in glob("%s/ssh_host_key.pub.H_*"%(self.__data__)) + glob("%s/ssh_host_*sa_key.pub.H_*"%(self.__data__)):
            host=f.split('_')[-1]
            data=file(f).read()
            output.write("%s,%s.mcs.anl.gov,%s %s"%(host,host,gethostbyname(host),data))
        output.close()

    def GenerateHostKeys(self,client):
        for hostkey in ["ssh_host_dsa_key.H_%s","ssh_host_rsa_key.H_%s","ssh_host_key.H_%s"]:
            filename="%s/%s"%(self.data,hostkey)%(client)
            if "ssh_host_rsa_key.H_" in filename:
                keytype='rsa'
            elif "ssh_host_dsa_key.H_" in filename:
                keytype='dsa'
            else:
                keytype='rsa1'
                
            try:
                stat(filename)
            except:
                system('ssh-keygen -f %s -N "" -t %s -C root@%s'%(filename,keytype,client))
                rename("%s.pub"%(filename),".".join(filename.split('.')[:-1]+['pub']+filename.split('.')[-1]))
        # call the notifier for global