def getSubnetAndZoneFreeIp(subnet, zone, current = None): ret = "" dhcp = Dhcp() dns = Dns() ip = dhcp.getSubnetFreeIp(subnet, current) while ip: if not dns.ipExists(zone, ip): ret = ip break ip = dhcp.getSubnetFreeIp(subnet, ip) return ret
def getSubnetAndZoneFreeIp(subnet, zone, current=None): ret = "" dhcp = Dhcp() dns = Dns() ip = dhcp.getSubnetFreeIp(subnet, current) while ip: if not dns.ipExists(zone, ip): ret = ip break ip = dhcp.getSubnetFreeIp(subnet, ip) return ret
def setFailoverConfig(primaryIp, secondaryIp, serverPort=647, peerPort=647, delay=30, update=10, balance=3, mclt=1800, split=128): return Dhcp().setFailoverConfig(primaryIp, secondaryIp, serverPort, peerPort, delay, update, balance, mclt, split)
def addZoneWithSubnet(zonename, network, netmask, reverse=False, description=None, nameserver=None, nameserverip=None): Dns().addZone(zonename, network, netmask, reverse, description, nameserver, nameserverip) d = Dhcp() d.addSubnet(network, netmask, zonename) d.setSubnetOption(network, "domain-name", '"' + zonename + '"') if nameserverip: d.setSubnetOption(network, "domain-name-servers", nameserverip)
def addSubnet(network, netmask, name): Dhcp().addSubnet(network, netmask, name)
def setSubnetDescription(subnet, description): Dhcp().setSubnetDescription(subnet, description)
def ipExistsInSubnet(subnet, ip): return Dhcp().ipExistsInSubnet(subnet, ip)
def activate(): config = NetworkConfig("network") logger = logging.getLogger() if config.disabled: logger.warning("Plugin network: disabled by configuration.") return False if not config.dhcpEnable and not config.dnsEnable: logger.warning("Plugin network: disabled by configuration.") return False try: ldapObj = ldapUserGroupControl() except ldap.INVALID_CREDENTIALS: logger.error("Can't bind to LDAP: invalid credentials.") return False if config.dhcpEnable: # Test if the DHCP/LDAP schema is available in the directory try: schema = ldapObj.getSchema("dhcpServer") if len(schema) <= 0: logger.error("DHCP schema is not included in LDAP directory") return False # Test if DHCP/LDAP schema contains the dhcpComments attribute if "dhcpComments" not in schema: logger.error( "DHCP/LDAP schema does not support the dhcpComments attribute. Please use the latest version of DCHP/LDAP schema." ) return False except: logger.exception("invalid schema") return False else: logger.info("DHCP submodule is disabled") if config.dnsEnable: # Test if the DNS/LDAP schema is available in the directory serverType = config.dnsType if serverType == "pdns": try: schema = ldapObj.getSchema("dNSDomain2") if len(schema) <= 0: logger.error( "DNS zone schema (dnsdomain2.schema) is not included in LDAP directory" ) return False except: logger.exception("invalid DNS schema") return False elif serverType == "bind": try: schema = ldapObj.getSchema("dNSZone") if len(schema) <= 0: logger.error( "DNS zone schema (dnszone.schema) is not included in LDAP directory" ) return False except: logger.exception("invalid DNS schema") return False else: logger.error("%s : Unknown DNS server." % serverType) return False else: logger.info("DNS submodule is disabled") if config.dhcpEnable: # Create DHCP ou head, path = config.dhcpDN.split(",", 1) ouName = head.split("=")[1] ldapObj.addOu(ouName, path) # Create DHCP config base structure d = Dhcp() try: d.addServiceConfig("DHCP config") logger.info("Created DHCP config object") except ldap.ALREADY_EXISTS: pass hostname = d.configDhcp.dhcpHostname try: d.addServer(hostname) d.setServiceConfigStatement("not", "authoritative") logging.info("The DHCP server '%s' was added." % hostname) except ldap.ALREADY_EXISTS: pass d.setServiceServerStatus(hostname, "primary") logging.info( "The server '%s' has been set as the primary DHCP server" % hostname) # Create DNS config base structure if config.dnsEnable: # Create DNS ou head, path = config.dnsDN.split(",", 1) ouName = head.split("=")[1] ldapObj.addOu(ouName, path) if serverType == "bind": try: gidNumber = grp.getgrnam(config.bindGroup) except KeyError: logger.error('The group "%s" does not exist.' % config.bindGroup) return False gidNumber = gidNumber[2] try: os.mkdir(config.bindLdapDir) os.chmod(config.bindLdapDir, 02750) os.chown(config.bindLdapDir, -1, gidNumber) except OSError, e: # errno = 17 is "File exists" if e.errno != 17: raise if not os.path.exists(config.bindLdap): f = open(config.bindLdap, "w") f.close() os.chmod(config.bindLdap, 0640) os.chown(config.bindLdap, -1, gidNumber)
def setHostHWAddress(subnet, host, address): Dhcp().setHostHWAddress(subnet, host, address)
def getHost(subnet, host): return Dhcp().getHost(subnet, host)
def addHostToSubnet(subnet, hostname): Dhcp().addHostToSubnet(subnet, hostname)
def setHostOption(subnet, host, option, value=None): Dhcp().setHostOption(subnet, host, option, value)
def getPool(poolname): return Dhcp().getPool(poolname)
def setSubnetOption(subnet, option, value=None): Dhcp().setSubnetOption(subnet, option, value)
def getSubnets(f): return Dhcp().getSubnets(f)
def getSubnet(subnet): return Dhcp().getSubnet(subnet)
def delSubnet(network): Dhcp().delSubnet(network)
def setPoolRange(poolname, start, end): Dhcp().setPoolRange(poolname, start, end)
def setSubnetNetmask(subnet, netmask): Dhcp().setSubnetNetmask(subnet, netmask)
def delHost(subnet, hostname): Dhcp().delHost(subnet, hostname)
def setSubnetAuthoritative(subnet, flag=True): Dhcp().setSubnetAuthoritative(subnet, flag)
def setHostStatement(subnet, host, option, value=None): Dhcp().setHostStatement(subnet, host, option, value)
def getSubnetHosts(network, filter): return Dhcp().getSubnetHosts(network, filter)
def getHostHWAddress(subnet, host, address): Dhcp().getHostHWAddress(subnet, host, address)
def getSubnetHostsCount(zone): return Dhcp().getSubnetHostsCount(zone)
def hostExistsInSubnet(subnet, hostname): return Dhcp().hostExistsInSubnet(subnet, hostname)
def getPoolsRanges(subnet): return Dhcp().getPoolsRanges(subnet)
def getSubnetFreeIp(subnet, startAt): return Dhcp().getSubnetFreeIp(subnet, startAt)
def setPoolsRanges(subnet, ranges): return Dhcp().setPoolsRanges(subnet, ranges)
def addZoneWithSubnet(zonename, network, netmask, reverse = False, description = None, nameserver = None, nameserverip = None): Dns().addZone(zonename, network, netmask, reverse, description, nameserver, nameserverip) d = Dhcp() d.addSubnet(network, netmask, zonename) d.setSubnetOption(network, "domain-name", '"' + zonename +'"') if nameserverip: d.setSubnetOption(network, "domain-name-servers", nameserverip)
def addPool(subnet, poolname, start, end): Dhcp().addPool(subnet, poolname, start, end)
def activate(): config = NetworkConfig("network") logger = logging.getLogger() if config.disabled: logger.warning("Plugin network: disabled by configuration.") return False try: ldapObj = ldapUserGroupControl() except ldap.INVALID_CREDENTIALS: logger.error("Can't bind to LDAP: invalid credentials.") return False # Test if the DHCP/LDAP schema is available in the directory try: schema = ldapObj.getSchema("dhcpServer") if len(schema) <= 0: logger.error("DHCP schema is not included in LDAP directory"); return False # Test if DHCP/LDAP schema contains the dhcpComments attribute if "dhcpComments" not in schema: logger.error("DHCP/LDAP schema does not support the dhcpComments attribute. Please use the latest version of DCHP/LDAP schema.") return False except: logger.exception("invalid schema") return False # Test if the DNS/LDAP schema is available in the directory serverType = config.dnsType if serverType == "pdns": try: schema = ldapObj.getSchema("dNSDomain2") if len(schema) <= 0: logger.error("DNS zone schema (dnsdomain2.schema) is not included in LDAP directory"); return False except: logger.exception("invalid DNS schema") return False elif serverType == "bind": try: schema = ldapObj.getSchema("dNSZone") if len(schema) <= 0: logger.error("DNS zone schema (dnszone.schema) is not included in LDAP directory"); return False except: logger.exception("invalid DNS schema") return False else: logger.error("%s : Unknown DNS server."%serverType); return False # Create required OUs config = NetworkConfig("network") for dn in [config.dhcpDN, config.dnsDN]: head, path = dn.split(",", 1) ouName = head.split("=")[1] ldapObj.addOu(ouName, path) # Create DHCP config base structure d = Dhcp() try: d.addServiceConfig("DHCP config") logger.info("Created DHCP config object") except ldap.ALREADY_EXISTS: pass hostname = d.configDhcp.dhcpHostname try: d.addServer(hostname) d.setServiceConfigStatement("not", "authoritative") logging.info("The DHCP server '%s' was added." % hostname) except ldap.ALREADY_EXISTS: pass d.setServiceServerStatus(hostname, "primary") logging.info("The server '%s' has been set as the primary DHCP server" % hostname) # Create DNS config base structure if serverType == "bind": try: gidNumber = grp.getgrnam(config.bindGroup) except KeyError: logger.error('The group "%s" does not exist.' % config.bindGroup) return False gidNumber = gidNumber[2] try: os.mkdir(config.bindLdapDir) os.chmod(config.bindLdapDir, 02750) os.chown(config.bindLdapDir, -1, gidNumber) except OSError, e: # errno = 17 is "File exists" if e.errno != 17: raise if not os.path.exists(config.bindLdap): f = open(config.bindLdap, "w") f.close() os.chmod(config.bindLdap, 0640) os.chown(config.bindLdap, -1, gidNumber)
def delPool(poolname): Dhcp().delPool(poolname)