def writeConfig(self, *a): SOA = """@ 86400 IN SOA %s.%s. postmaster.%s. ( 1 ; Serial 28800 ; Refresh 7200 ; Retry 604800 ; Expire 3600) ; Minimum TTL NS %s.%s """ % (config.Hostname, config.Domain, config.Domain, config.Hostname, config.Domain) serverIP = config.EthernetDevices[config.LANPrimary]['ip'].split( '/')[0] forward = SOA + " A %s\n" % serverIP for cname in config.TCSAliases: spaces = ''.join([" " for i in range(20 - len(cname))]) forward += "%s %s CNAME %s\n" % (cname, spaces, config.Hostname) forward += "%s A %s\n" % (config.Hostname, serverIP) ipv6listen = "" ipv6query = "" if config.EthernetDevices[config.LANPrimary].get('ipv6', False): ipv6addr = config.EthernetDevices[config.LANPrimary]['ipv6'].split( '/')[0] forward += "%s AAAA %s\n" % (config.Hostname, ipv6addr) ipv6listen = "listen-on-v6 {\n %s;\n };" % ipv6addr ipv6query = "%s;" % Utils.getV6Network( config.EthernetDevices[config.LANPrimary]['ipv6']) reverse = SOA + "%s PTR %s.%s.\n" % ( serverIP.split('.')[-1], config.Hostname, config.Domain) os.system('mkdir -p /etc/bind/pri > /dev/null 2>&1') os.system('chown -R bind:bind /etc/bind/pri ') Utils.writeConf('/etc/bind/pri/forward.zone', forward, ';') Utils.writeConf('/etc/bind/pri/reverse.zone', reverse, ';') options = """options { listen-on { 127.0.0.1; %s; }; %s allow-query { %s; 127.0.0.1; %s }; forwarders { %s }; directory "/var/bind"; pid-file "/var/run/named/named.pid"; forward first;\n};\n\n""" % (serverIP, ipv6listen, config.EthernetDevices[ config.LANPrimary]['network'], ipv6query, ''.join( [" %s;\n" % i for i in config.ForwardingNameservers])) zones = """zone "." in { type hint; file "named.ca"; }; zone "0.0.127.in-addr.arpa" in { type master; file "pri/127.zone"; notify no; }; zone "localhost" in { type master; file "pri/localhost.zone"; notify no; }; zone "%s.in-addr.arpa" in { type master; file "pri/reverse.zone"; notify no; allow-update { 127.0.0.1; }; }; zone "%s" in { type master; file "pri/forward.zone"; notify no; allow-update { 127.0.0.1; }; };\n""" % ('.'.join([i for i in reversed(serverIP.split('.')[:3]) ]), config.Domain) # Debian modifications if os.path.exists('/etc/debian_version'): zones = zones.replace('named.ca', 'db.root') zones = zones.replace('pri/localhost.zone', 'db.local') zones = zones.replace('pri/127.zone', 'db.127') options = options.replace('pid-file "/var/run/named/named.pid";', '') options = options.replace('directory "/var/bind";', 'directory "/etc/bind";') # Rechown os.system('chown -R bind:bind /etc/bind/*') bindFile = options + zones Utils.writeConf('/etc/bind/named.conf', bindFile, '//')
def writeConfig(self, *a): try: serverIP = Utils.getLanIPs(config)[0] except: # We aren't sure what our IP is.. oh dear serverIP = "127.0.0.1" # This all needs to happen first hosts = """127.0.0.1 localhost 127.0.1.1 %(host)s.%(domain)s %(host)s # The following lines are desirable for IPv6 capable hosts ::1 ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters ff02::3 ip6-allhosts\n""" % { 'host': config.Hostname, 'domain': config.Domain } l = open('/etc/hosts', 'wt') l.write(hosts) l.close() if config.General.get('sysresolv', []): ns = "" for i in config.General['sysresolv']: ns += 'nameserver %s\n' % i else: if serverIP == "127.0.0.1": ns = "" else: ns = 'nameserver 127.0.0.1\n' resolv = """search %s domain %s %s\n""" % (config.Domain, config.Domain, ns) l = open('/etc/resolv.conf', 'wt') l.write(resolv) l.close() os.system('mkdir -p /etc/bind/pri > /dev/null 2>&1') SOA = """@ 86400 IN SOA %s. postmaster.%s. ( 1 ; Serial 28800 ; Refresh 7200 ; Retry 604800 ; Expire 3600) ; Minimum TTL\n""" zones = "" ipv6addr = "" ipv6listen = "" ipv6query = "" if Utils.getLanIP6s(config): ipv6addr = Utils.getLanIP6s(config) ipv6listen = "listen-on-v6 {\n %s;\n };" % ';'.join( [n.split('/')[0] for n in ipv6addr]) ipv6query = "%s;" % Utils.getV6Network(ipv6addr[0]) TSOA = SOA % ("%s.%s" % (config.Hostname, config.Domain), config.Domain) TSOA += " NS %s.%s.\n" % ( config.Hostname, config.Domain) if not config.General.get('zones', {}).get(config.Domain, None): if not serverIP: serverIP = "127.0.0.1" forward = TSOA + " A %s\n" % serverIP for cname in config.TCSAliases: spaces = ''.join([" " for i in range(20 - len(cname))]) forward += "%s %s CNAME %s\n" % (cname, spaces, config.Hostname) forward += "%s A %s\n" % (config.Hostname, serverIP) if ipv6addr: forward += "%s AAAA %s\n" % ( config.Hostname, ipv6addr[0]) Utils.writeConf('/etc/bind/pri/%s.zone' % (config.Domain, ), forward, ';') zones += """ zone "%s" in { type master; allow-update { 127.0.0.1; }; notify no; file "pri/%s.zone"; };\n\n""" % (config.Domain, config.Domain) if config.General.get('zones', None): for domain, info in config.General['zones'].items(): zones += " zone \"%s\" in {\n" % domain # Get options if info.get('options', None): for opt in info['options']: zones += " %s;\n" % opt else: # Use defaults zones += " type master;\n notify no;\n" if 'type forward' not in info.get('options'): # check updaters if info.get('update', None): zones += " allow-update {\n" for update in info['update']: zones += " %s;\n" % update zones += " };\n" else: # Use default localhost. zones += " allow-update {\n 127.0.0.1;\n };" ns = info.get('ns', []) if len(ns) < 1: print "ERROR!! Domain %s requires at least 1 name server!" % domain sys.exit() # Create zone data zonedata = "" for i in ns: zonedata += " NS %s.\n" % i if info.get('records'): maxRec = max([ len(rec.split()[0]) for rec in info.get('records', []) ]) + 5 else: maxRec = 4 colspace = [maxRec, 8] first = "" for rec in info.get('records', []): if "MX" in rec: host, type, prio, data = rec.split() type = "%s %s" % (type, prio) else: host, type, data = tuple(rec.split()) theseSpaces = [ colspace[c] - len(i) for c, i in enumerate([host, type]) ] recLine = "%s%s%s%s%s\n" % (host, " " * theseSpaces[0], type, " " * theseSpaces[1], data) if type == "NS" or "MX" in rec: first += recLine else: zonedata += recLine if 'type forward' in info.get('options'): zones += " forwarders {\n %s;\n };\n" % info.get( 'forward') if 'type slave' in info.get('options'): uservs = [] for i in info.get('update'): if i != "127.0.0.1": uservs.append(i) zones += " masters {\n %s;\n };\n" % ';'.join( uservs) if 'type master' in info.get('options'): zones += " file \"pri/%s.zone\";\n" % domain # Terminate entry zones += " };\n" l = open('/etc/bind/pri/%s.zone' % domain, 'wt') l.write(SOA % (ns[0], domain)) l.write(first) l.write(zonedata) l.close() reverse = TSOA + "%s PTR %s.%s.\n" % ( serverIP.split('.')[-1], config.Hostname, config.Domain) os.system('chown -R bind:bind /etc/bind/pri ') Utils.writeConf('/etc/bind/pri/reverse.zone', reverse, ';') lanv4Ips = Utils.getLanIPs(config) if lanv4Ips: listenv4 = ';'.join(lanv4Ips) + ';' else: listenv4 = "" options = """options { listen-on { 127.0.0.1; %s }; %s allow-query { %s 127.0.0.1; 0.0.0.0/0; }; forwarders { %s }; directory "/var/bind"; pid-file "/var/run/named/named.pid"; forward first;\n};\n\n""" % (listenv4, ipv6listen, ipv6query, ''.join( [" %s;\n" % i for i in config.ForwardingNameservers])) zones = """zone "." in { type hint; file "named.ca"; }; zone "0.0.127.in-addr.arpa" in { type master; file "pri/127.zone"; notify no; }; zone "localhost" in { type master; file "pri/localhost.zone"; notify no; }; zone "%s.in-addr.arpa" in { type master; file "pri/reverse.zone"; notify no; allow-update { 127.0.0.1; }; }; %s\n""" % ('.'.join([i for i in reversed(serverIP.split('.')[:3])]), zones) # Debian modifications zones = zones.replace('named.ca', 'db.root') zones = zones.replace('pri/localhost.zone', 'db.local') zones = zones.replace('pri/127.zone', 'db.127') options = options.replace('pid-file "/var/run/named/named.pid";', '') options = options.replace('directory "/var/bind";', 'directory "/etc/bind";') # Rechown os.system('chown -R bind:bind /etc/bind/*') bindFile = options + zones Utils.writeConf('/etc/bind/named.conf', bindFile, '//') os.system('rm /etc/bind/pri/*.jnl > /dev/null 2>&1')
def writeConfig(self, *a): routes = "! -*- Automaticaly created by TUMS Configurator -*-\n" routes += "hostname %s\npassword %s\nenable password %s\n" % ( config.Hostname, config.LDAPPassword, config.LDAPPassword) bgp = routes # copy it over for BGP config too :) ifaceConfig = "" for interface, defin in config.EthernetDevices.items(): # check routes - add to Zebra if not a default if defin.get('routes', None): for dest, gw in defin['routes']: if dest != 'default': routes += ' ip route %s %s\n' % (dest, gw) if defin.get('ipv6', None): ifaceConfig += '\ninterface %s\n' % interface ifaceConfig += ' no ipv6 nd suppress-ra\n' ifaceConfig += ' ipv6 nd prefix %s\n\n' % Utils.getV6Network( defin['ipv6']) routes += '\n' try: if config.LocalRoute: lp = open('/usr/local/tcs/tums/configs/local_routes') for l in lp: ln = l.strip('\n').strip() if ln: if not "/" in ln: ln += "/32" routes += "ip route %s %s\n" % (ln, config.LocalRoute) except: print "No local-only routing" zebra = open('/etc/quagga/zebra.conf', 'wt') zebra.write(routes) zebra.write(ifaceConfig) zebra.close() for asn, conf in config.BGP.items(): bgp += "router bgp %s\n" % asn bgp += " bgp router-id %s\n" % conf['router-id'] for net in conf.get('networks', []): bgp += " network %s\n" % net for neigh, nconf in conf.get('neighbors', {}).items(): # BGP remote-as is either our AS or it's our AS (odd default but handy) bgp += " neighbor %s remote-as %s\n" % ( neigh, nconf.get('as', asn)) bgp += '\n' bgpconf = open('/etc/quagga/bgpd.conf', 'wt') bgpconf.write(bgp) bgpconf.close() # Write startup daemons file (only used by Debian, doesn't hurt to write it anyway) daemons = open('/etc/quagga/daemons', 'wt') # enabled daemons.write("zebra=yes\n") daemons.write("bgpd=yes\n") # disabled daemons.write("ospfd=no\nospf6d=no\nripd=no\nripngd=no\nisisd=no\n") daemons.close()