Search in sources :

Example 1 with NetworkPair

use of org.eclipse.kura.net.NetworkPair in project kura by eclipse.

the class LinuxNamed method getForwardingNamedFile.

private String getForwardingNamedFile() {
    StringBuilder sb = new StringBuilder().append("// Forwarding and Caching Name Server Configuration\n").append(// 
    "options {\n").append(// 
    "\tdirectory \"/var/named\";\n").append(// 
    "\tversion \"not currently available\";\n").append("\tforwarders {");
    Set<IP4Address> forwarders = this.m_dnsServerConfigIP4.getForwarders();
    for (IP4Address forwarder : forwarders) {
        sb.append(forwarder.getHostAddress()).append(";");
    }
    sb.append("};\n");
    // 
    sb.append("\tforward only;\n").append(// 
    "\tallow-transfer{\"none\";};\n").append("\tallow-query {");
    Set<NetworkPair<IP4Address>> allowedNetworks = this.m_dnsServerConfigIP4.getAllowedNetworks();
    for (NetworkPair<IP4Address> pair : allowedNetworks) {
        // 
        sb.append(pair.getIpAddress().getHostAddress()).append(// 
        "/").append(// 
        pair.getPrefix()).append(";");
    }
    sb.append("};\n");
    sb.append("\tmax-cache-ttl 30;\n");
    sb.append("\tmax-ncache-ttl 30;\n");
    // 
    sb.append("};\n").append(// 
    "logging{\n").append(// 
    "\tchannel named_log {\n").append(// 
    "\t\tfile \"").append(// 
    s_logFileName).append(// 
    "\" versions 3;\n").append(// 
    "\t\tseverity info;\n").append(// 
    "\t\tprint-severity yes;\n").append(// 
    "\t\tprint-time yes;\n").append(// 
    "\t\tprint-category yes;\n").append(// 
    "\t};\n").append(// 
    "\tcategory default{\n").append(// 
    "\t\tnamed_log;\n").append(// 
    "\t};\n").append(// 
    "};\n").append(// 
    "zone \".\" IN {\n").append(// 
    "\ttype hint;\n").append(// 
    "\tfile \"named.ca\";\n").append(// 
    "};\n").append(// 
    "include \"").append(// 
    s_rfc1912ZonesFilename).append("\";\n");
    return sb.toString();
}
Also used : NetworkPair(org.eclipse.kura.net.NetworkPair) IP4Address(org.eclipse.kura.net.IP4Address)

Example 2 with NetworkPair

use of org.eclipse.kura.net.NetworkPair in project kura by eclipse.

the class LinuxFirewall method addLocalRule.

public void addLocalRule(int port, String protocol, String permittedNetwork, String permittedNetworkPrefix, String permittedInterfaceName, String unpermittedInterfaceName, String permittedMAC, String sourcePortRange) throws KuraException {
    try {
        LocalRule newLocalRule = null;
        if (permittedNetwork != null && permittedNetworkPrefix != null) {
            s_logger.debug("permittedNetwork: {}", permittedNetwork);
            s_logger.debug("permittedNetworkPrefix: {}", permittedNetworkPrefix);
            newLocalRule = new LocalRule(port, protocol, new NetworkPair<IP4Address>((IP4Address) IPAddress.parseHostAddress(permittedNetwork), Short.parseShort(permittedNetworkPrefix)), permittedInterfaceName, unpermittedInterfaceName, permittedMAC, sourcePortRange);
        } else {
            newLocalRule = new LocalRule(port, protocol, new NetworkPair<IP4Address>((IP4Address) IPAddress.parseHostAddress("0.0.0.0"), (short) 0), permittedInterfaceName, permittedInterfaceName, permittedMAC, sourcePortRange);
        }
        ArrayList<LocalRule> localRules = new ArrayList<LocalRule>();
        localRules.add(newLocalRule);
        addLocalRules(localRules);
    } catch (Exception e) {
        throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
    }
}
Also used : NetworkPair(org.eclipse.kura.net.NetworkPair) KuraException(org.eclipse.kura.KuraException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) KuraException(org.eclipse.kura.KuraException)

Example 3 with NetworkPair

use of org.eclipse.kura.net.NetworkPair in project kura by eclipse.

the class FirewallConfigurationServiceImpl method setFirewallOpenPortConfiguration.

@Override
public void setFirewallOpenPortConfiguration(List<FirewallOpenPortConfigIP<? extends IPAddress>> firewallConfiguration) throws KuraException {
    s_logger.debug("setFirewallOpenPortConfiguration() :: Deleting local rules");
    LinuxFirewall firewall = LinuxFirewall.getInstance();
    firewall.deleteAllLocalRules();
    ArrayList<LocalRule> localRules = new ArrayList<LocalRule>();
    for (FirewallOpenPortConfigIP<? extends IPAddress> openPortEntry : firewallConfiguration) {
        if (openPortEntry.getPermittedNetwork() == null || openPortEntry.getPermittedNetwork().getIpAddress() == null) {
            try {
                openPortEntry.setPermittedNetwork(new NetworkPair(IPAddress.parseHostAddress("0.0.0.0"), (short) 0));
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
        }
        try {
            LocalRule localRule = null;
            if (openPortEntry.getPortRange() != null) {
                s_logger.debug("setFirewallOpenPortConfiguration() :: Adding local rule for: {}", openPortEntry.getPortRange());
                localRule = new LocalRule(openPortEntry.getPortRange(), openPortEntry.getProtocol().name(), new NetworkPair(IPAddress.parseHostAddress(openPortEntry.getPermittedNetwork().getIpAddress().getHostAddress()), openPortEntry.getPermittedNetwork().getPrefix()), openPortEntry.getPermittedInterfaceName(), openPortEntry.getUnpermittedInterfaceName(), openPortEntry.getPermittedMac(), openPortEntry.getSourcePortRange());
            } else {
                s_logger.debug("setFirewallOpenPortConfiguration() :: Adding local rule for: {}", openPortEntry.getPort());
                localRule = new LocalRule(openPortEntry.getPort(), openPortEntry.getProtocol().name(), new NetworkPair(IPAddress.parseHostAddress(openPortEntry.getPermittedNetwork().getIpAddress().getHostAddress()), openPortEntry.getPermittedNetwork().getPrefix()), openPortEntry.getPermittedInterfaceName(), openPortEntry.getUnpermittedInterfaceName(), openPortEntry.getPermittedMac(), openPortEntry.getSourcePortRange());
            }
            localRules.add(localRule);
        } catch (Exception e) {
            s_logger.error("setFirewallOpenPortConfiguration() :: Failed to add local rule for: {} - {}", openPortEntry.getPort(), e);
        }
    }
    firewall.addLocalRules(localRules);
}
Also used : NetworkPair(org.eclipse.kura.net.NetworkPair) UnknownHostException(java.net.UnknownHostException) LinuxFirewall(org.eclipse.kura.linux.net.iptables.LinuxFirewall) ArrayList(java.util.ArrayList) LocalRule(org.eclipse.kura.linux.net.iptables.LocalRule) KuraException(org.eclipse.kura.KuraException) UnknownHostException(java.net.UnknownHostException)

Example 4 with NetworkPair

use of org.eclipse.kura.net.NetworkPair in project kura by eclipse.

the class DnsMonitorServiceImpl method updateDnsProxyConfig.

private void updateDnsProxyConfig() {
    this.m_enabled = false;
    this.m_allowedNetworks = new HashSet<NetworkPair<IP4Address>>();
    this.m_forwarders = new HashSet<IP4Address>();
    if (this.m_networkConfiguration != null) {
        if (this.m_networkConfiguration.getNetInterfaceConfigs() != null) {
            List<NetInterfaceConfig<? extends NetInterfaceAddressConfig>> netInterfaceConfigs = this.m_networkConfiguration.getNetInterfaceConfigs();
            for (NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig : netInterfaceConfigs) {
                if (netInterfaceConfig.getType() == NetInterfaceType.ETHERNET || netInterfaceConfig.getType() == NetInterfaceType.WIFI || netInterfaceConfig.getType() == NetInterfaceType.MODEM) {
                    try {
                        getAllowedNetworks(netInterfaceConfig);
                    } catch (KuraException e) {
                        s_logger.error("Error updating dns proxy", e);
                    }
                }
            }
        }
    }
    Set<IPAddress> dnsServers = LinuxDns.getInstance().getDnServers();
    if (dnsServers != null && !dnsServers.isEmpty()) {
        for (IPAddress dnsServer : dnsServers) {
            s_logger.debug("Found DNS Server: {}", dnsServer.getHostAddress());
            this.m_forwarders.add((IP4Address) dnsServer);
        }
    }
    try {
        LinuxNamed linuxNamed = LinuxNamed.getInstance();
        s_logger.debug("Disabling DNS proxy");
        linuxNamed.disable();
        s_logger.debug("Writing config");
        DnsServerConfigIP4 dnsServerConfigIP4 = new DnsServerConfigIP4(this.m_forwarders, this.m_allowedNetworks);
        linuxNamed.setConfig(dnsServerConfigIP4);
        if (this.m_enabled) {
            sleep(500);
            s_logger.debug("Starting DNS proxy");
            linuxNamed.enable();
        } else {
            s_logger.debug("DNS proxy not enabled");
        }
    } catch (KuraException e) {
        e.printStackTrace();
    }
}
Also used : NetInterfaceConfig(org.eclipse.kura.net.NetInterfaceConfig) IP4Address(org.eclipse.kura.net.IP4Address) DnsServerConfigIP4(org.eclipse.kura.net.dns.DnsServerConfigIP4) LinuxNamed(org.eclipse.kura.linux.net.dns.LinuxNamed) NetworkPair(org.eclipse.kura.net.NetworkPair) KuraException(org.eclipse.kura.KuraException) IPAddress(org.eclipse.kura.net.IPAddress) NetInterfaceAddressConfig(org.eclipse.kura.net.NetInterfaceAddressConfig)

Example 5 with NetworkPair

use of org.eclipse.kura.net.NetworkPair in project kura by eclipse.

the class LinuxNamed method init.

private void init() throws KuraException {
    // TODO
    File configFile = new File(s_persistentConfigFileName);
    if (configFile.exists()) {
        s_logger.debug("initing DNS Server configuration");
        try {
            Set<IP4Address> forwarders = new HashSet<IP4Address>();
            Set<NetworkPair<IP4Address>> allowedNetworks = new HashSet<NetworkPair<IP4Address>>();
            BufferedReader br = new BufferedReader(new FileReader(configFile));
            boolean forwardingConfig = true;
            String line = null;
            while ((line = br.readLine()) != null) {
                if (line.trim().equals("forward only;")) {
                    forwardingConfig = true;
                    break;
                }
            }
            br.close();
            br = null;
            if (forwardingConfig) {
                br = new BufferedReader(new FileReader(configFile));
                while ((line = br.readLine()) != null) {
                    // TODO - really simple for now
                    StringTokenizer st = new StringTokenizer(line);
                    while (st.hasMoreTokens()) {
                        String token = st.nextToken();
                        if (token.equals("forwarders")) {
                            // get the forwarders 'forwarders {192.168.1.1;192.168.2.1;};'
                            StringTokenizer st2 = new StringTokenizer(st.nextToken(), "{} ;");
                            while (st2.hasMoreTokens()) {
                                String forwarder = st2.nextToken();
                                if (forwarder != null && !forwarder.trim().equals("")) {
                                    s_logger.debug("found forwarder: {}", forwarder);
                                    forwarders.add((IP4Address) IPAddress.parseHostAddress(forwarder));
                                }
                            }
                        } else if (token.equals("allow-query")) {
                            // get the networks 'allow-query {192.168.2.0/24;192.168.3.0/24};'
                            StringTokenizer st2 = new StringTokenizer(st.nextToken(), "{} ;");
                            while (st2.hasMoreTokens()) {
                                String allowedNetwork = st2.nextToken();
                                if (allowedNetwork != null && !allowedNetwork.trim().equals("")) {
                                    String[] splitNetwork = allowedNetwork.split("/");
                                    allowedNetworks.add(new NetworkPair<IP4Address>((IP4Address) IPAddress.parseHostAddress(splitNetwork[0]), Short.parseShort(splitNetwork[1])));
                                }
                            }
                        }
                    }
                }
                br.close();
                br = null;
                // set the configuration and return
                this.m_dnsServerConfigIP4 = new DnsServerConfigIP4(forwarders, allowedNetworks);
                return;
            }
        } catch (FileNotFoundException e) {
            throw new KuraException(KuraErrorCode.CONFIGURATION_ERROR, e);
        } catch (IOException e) {
            throw new KuraException(KuraErrorCode.CONFIGURATION_ERROR, e);
        }
    } else {
        s_logger.debug("There is no current DNS server configuration that allows forwarding");
    }
}
Also used : IP4Address(org.eclipse.kura.net.IP4Address) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) DnsServerConfigIP4(org.eclipse.kura.net.dns.DnsServerConfigIP4) StringTokenizer(java.util.StringTokenizer) NetworkPair(org.eclipse.kura.net.NetworkPair) KuraException(org.eclipse.kura.KuraException) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File) HashSet(java.util.HashSet)

Aggregations

NetworkPair (org.eclipse.kura.net.NetworkPair)7 KuraException (org.eclipse.kura.KuraException)5 IP4Address (org.eclipse.kura.net.IP4Address)4 UnknownHostException (java.net.UnknownHostException)3 ArrayList (java.util.ArrayList)3 LinuxFirewall (org.eclipse.kura.linux.net.iptables.LinuxFirewall)3 IOException (java.io.IOException)2 LocalRule (org.eclipse.kura.linux.net.iptables.LocalRule)2 PortForwardRule (org.eclipse.kura.linux.net.iptables.PortForwardRule)2 DnsServerConfigIP4 (org.eclipse.kura.net.dns.DnsServerConfigIP4)2 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1 HashSet (java.util.HashSet)1 StringTokenizer (java.util.StringTokenizer)1 FirewallConfiguration (org.eclipse.kura.core.net.FirewallConfiguration)1 LinuxNamed (org.eclipse.kura.linux.net.dns.LinuxNamed)1 NATRule (org.eclipse.kura.linux.net.iptables.NATRule)1 IPAddress (org.eclipse.kura.net.IPAddress)1