| | 1 | #!/usr/bin/env python |
| | 2 | |
| | 3 | from glob import glob |
| | 4 | from os import rename |
| | 5 | from socket import gethostbyname |
| | 6 | |
| | 7 | from Types import ConfigFile |
| | 8 | from Generator import Generator |
| | 9 | |
| | 10 | class sshbase(Generator): |
| | 11 | __name__ = 'sshbase' |
| | 12 | __version__ = '$Id: $' |
| | 13 | __author__ = 'bcfg-dev@mcs.anl.gov' |
| | 14 | |
| | 15 | __build__ = { '/etc/ssh/ssh_known_hosts':build_skn, |
| | 16 | '/etc/ssh/ssh_host_key':build_hk} |
| | 17 | |
| | 18 | def build_skn(self,name,client): |
| | 19 | data=file("%s/ssh_known_hosts"%(self.data)).read() |
| | 20 | ip=gethostbyname(client) |
| | 21 | for hostkey in ["ssh_host_dsa_key.pub.H_%s","ssh_host_rsa_key.pub.H_%s","ssh_host_key.pub.H_%s"]: |
| | 22 | filename="%s/%s"%(self.data,hostkey)%(client) |
| | 23 | hdata=file(filename).read() |
| | 24 | data+="%s,%s,%s %s"%(client,"%.mcs.anl.gov"%(client),ip,hdata) |
| | 25 | return ConfigFile(name,'root','root','0644',data) |
| | 26 | |
| | 27 | def build_hk(self,name,client): |
| | 28 | reponame="%s/%s.H_%s"%(self.__data__,name.split('/')[-1],client) |
| | 29 | try: |
| | 30 | stat(reponame) |
| | 31 | except IOError: |
| | 32 | self.GenerateHostKeys(client) |
| | 33 | self.GenerateKnownHosts() |
| | 34 | # then we read the data file |
| | 35 | keydata=file(reponame).read() |
| | 36 | if "ssh_host_key.H_" in reponame: |
| | 37 | return ConfigFile(name,'root','root','0600',keydata,'base64') |
| | 38 | return ConfigFile(name,'root','root','0600',keydata) |
| | 39 | |
| | 40 | def GenerateKnownHosts(self): |
| | 41 | output=file("%s/ssh_known_hosts"%(self.__data__),'w') |
| | 42 | for f in glob("%s/ssh_host_key.pub.H_*"%(self.__data__)) + glob("%s/ssh_host_*sa_key.pub.H_*"%(self.__data__)): |
| | 43 | host=f.split('_')[-1] |
| | 44 | data=file(f).read() |
| | 45 | output.write("%s,%s.mcs.anl.gov,%s %s"%(host,host,gethostbyname(host),data)) |
| | 46 | output.close() |
| | 47 | |
| | 48 | def GenerateHostKeys(self,client): |
| | 49 | for hostkey in ["ssh_host_dsa_key.H_%s","ssh_host_rsa_key.H_%s","ssh_host_key.H_%s"]: |
| | 50 | filename="%s/%s"%(self.data,hostkey)%(client) |
| | 51 | if "ssh_host_rsa_key.H_" in filename: |
| | 52 | keytype='rsa' |
| | 53 | elif "ssh_host_dsa_key.H_" in filename: |
| | 54 | keytype='dsa' |
| | 55 | else: |
| | 56 | keytype='rsa1' |
| | 57 | |
| | 58 | try: |
| | 59 | stat(filename) |
| | 60 | except: |
| | 61 | system('ssh-keygen -f %s -N "" -t %s -C root@%s'%(filename,keytype,client)) |
| | 62 | rename("%s.pub"%(filename),".".join(filename.split('.')[:-1]+['pub']+filename.split('.')[-1])) |
| | 63 | # call the notifier for global |