Search in sources :

Example 1 with FirewallOpenPortConfigIP4

use of org.eclipse.kura.net.firewall.FirewallOpenPortConfigIP4 in project kura by eclipse.

the class GwtNetworkServiceImpl method findDeviceFirewallOpenPorts.

@Override
public ArrayList<GwtFirewallOpenPortEntry> findDeviceFirewallOpenPorts(GwtXSRFToken xsrfToken) throws GwtKuraException {
    checkXSRFToken(xsrfToken);
    NetworkAdminService nas = ServiceLocator.getInstance().getService(NetworkAdminService.class);
    List<GwtFirewallOpenPortEntry> gwtOpenPortEntries = new ArrayList<GwtFirewallOpenPortEntry>();
    try {
        List<NetConfig> firewallConfigs = nas.getFirewallConfiguration();
        if (firewallConfigs != null && !firewallConfigs.isEmpty()) {
            for (NetConfig netConfig : firewallConfigs) {
                if (netConfig instanceof FirewallOpenPortConfigIP4) {
                    s_logger.debug("findDeviceFirewallOpenPorts() :: adding new Open Port Entry: {}", ((FirewallOpenPortConfigIP4) netConfig).getPort());
                    GwtFirewallOpenPortEntry entry = new GwtFirewallOpenPortEntry();
                    if (((FirewallOpenPortConfigIP4) netConfig).getPortRange() != null) {
                        entry.setPortRange(((FirewallOpenPortConfigIP4) netConfig).getPortRange());
                    } else {
                        entry.setPortRange(String.valueOf(((FirewallOpenPortConfigIP4) netConfig).getPort()));
                    }
                    entry.setProtocol(((FirewallOpenPortConfigIP4) netConfig).getProtocol().toString());
                    entry.setPermittedNetwork(((FirewallOpenPortConfigIP4) netConfig).getPermittedNetwork().getIpAddress().getHostAddress() + "/" + ((FirewallOpenPortConfigIP4) netConfig).getPermittedNetwork().getPrefix());
                    entry.setPermittedInterfaceName(((FirewallOpenPortConfigIP4) netConfig).getPermittedInterfaceName());
                    entry.setUnpermittedInterfaceName(((FirewallOpenPortConfigIP4) netConfig).getUnpermittedInterfaceName());
                    entry.setPermittedMAC(((FirewallOpenPortConfigIP4) netConfig).getPermittedMac());
                    entry.setSourcePortRange(((FirewallOpenPortConfigIP4) netConfig).getSourcePortRange());
                    gwtOpenPortEntries.add(entry);
                }
            }
        }
        return new ArrayList<GwtFirewallOpenPortEntry>(gwtOpenPortEntries);
    } catch (KuraException e) {
        throw new GwtKuraException(GwtKuraErrorCode.INTERNAL_ERROR, e);
    }
}
Also used : GwtFirewallOpenPortEntry(org.eclipse.kura.web.shared.model.GwtFirewallOpenPortEntry) GwtKuraException(org.eclipse.kura.web.shared.GwtKuraException) KuraException(org.eclipse.kura.KuraException) GwtKuraException(org.eclipse.kura.web.shared.GwtKuraException) ArrayList(java.util.ArrayList) NetConfig(org.eclipse.kura.net.NetConfig) NetworkAdminService(org.eclipse.kura.net.NetworkAdminService) FirewallOpenPortConfigIP4(org.eclipse.kura.net.firewall.FirewallOpenPortConfigIP4)

Example 2 with FirewallOpenPortConfigIP4

use of org.eclipse.kura.net.firewall.FirewallOpenPortConfigIP4 in project kura by eclipse.

the class GwtNetworkServiceImpl method updateDeviceFirewallOpenPorts.

@Override
public void updateDeviceFirewallOpenPorts(GwtXSRFToken xsrfToken, List<GwtFirewallOpenPortEntry> entries) throws GwtKuraException {
    checkXSRFToken(xsrfToken);
    NetworkAdminService nas = ServiceLocator.getInstance().getService(NetworkAdminService.class);
    List<FirewallOpenPortConfigIP<? extends IPAddress>> firewallOpenPortConfigIPs = new ArrayList<FirewallOpenPortConfigIP<? extends IPAddress>>();
    s_logger.debug("updating open ports");
    try {
        for (GwtFirewallOpenPortEntry entry : entries) {
            String network = null;
            String prefix = null;
            if (entry.getPermittedNetwork() != null) {
                String[] parts = entry.getPermittedNetwork().split("/");
                network = parts[0];
                prefix = parts[1];
            }
            FirewallOpenPortConfigIP<IP4Address> firewallOpenPortConfigIP = new FirewallOpenPortConfigIP4();
            if (entry.getPortRange() != null) {
                if (entry.getPortRange().indexOf(':') > 0) {
                    firewallOpenPortConfigIP.setPortRange(entry.getPortRange());
                } else {
                    firewallOpenPortConfigIP.setPort(Integer.parseInt(entry.getPortRange()));
                }
            }
            firewallOpenPortConfigIP.setProtocol(NetProtocol.valueOf(GwtSafeHtmlUtils.htmlEscape(entry.getProtocol())));
            if (network != null && prefix != null) {
                firewallOpenPortConfigIP.setPermittedNetwork(new NetworkPair<IP4Address>((IP4Address) IPAddress.parseHostAddress(network), Short.parseShort(prefix)));
            }
            firewallOpenPortConfigIP.setPermittedInterfaceName(GwtSafeHtmlUtils.htmlEscape(entry.getPermittedInterfaceName()));
            firewallOpenPortConfigIP.setUnpermittedInterfaceName(GwtSafeHtmlUtils.htmlEscape(entry.getUnpermittedInterfaceName()));
            firewallOpenPortConfigIP.setPermittedMac(GwtSafeHtmlUtils.htmlEscape(entry.getPermittedMAC()));
            firewallOpenPortConfigIP.setSourcePortRange(GwtSafeHtmlUtils.htmlEscape(entry.getSourcePortRange()));
            s_logger.debug("adding open port entry for {}", entry.getPortRange());
            firewallOpenPortConfigIPs.add(firewallOpenPortConfigIP);
        }
        nas.setFirewallOpenPortConfiguration(firewallOpenPortConfigIPs);
    } catch (KuraException e) {
        s_logger.warn("Exception while updating firewall open ports", e);
        throw new GwtKuraException(GwtKuraErrorCode.INTERNAL_ERROR, e);
    } catch (NumberFormatException e) {
        s_logger.warn("Exception while updating firewall open ports", e);
        throw new GwtKuraException(GwtKuraErrorCode.INTERNAL_ERROR, e);
    } catch (UnknownHostException e) {
        s_logger.warn("Exception while updating firewall open ports", e);
        throw new GwtKuraException(GwtKuraErrorCode.INTERNAL_ERROR, e);
    }
}
Also used : GwtFirewallOpenPortEntry(org.eclipse.kura.web.shared.model.GwtFirewallOpenPortEntry) GwtKuraException(org.eclipse.kura.web.shared.GwtKuraException) UnknownHostException(java.net.UnknownHostException) IP4Address(org.eclipse.kura.net.IP4Address) ArrayList(java.util.ArrayList) FirewallOpenPortConfigIP(org.eclipse.kura.net.firewall.FirewallOpenPortConfigIP) KuraException(org.eclipse.kura.KuraException) GwtKuraException(org.eclipse.kura.web.shared.GwtKuraException) NetworkAdminService(org.eclipse.kura.net.NetworkAdminService) FirewallOpenPortConfigIP4(org.eclipse.kura.net.firewall.FirewallOpenPortConfigIP4) IPAddress(org.eclipse.kura.net.IPAddress)

Example 3 with FirewallOpenPortConfigIP4

use of org.eclipse.kura.net.firewall.FirewallOpenPortConfigIP4 in project kura by eclipse.

the class FirewallConfigurationServiceImpl method getFirewallConfiguration.

@Override
public FirewallConfiguration getFirewallConfiguration() throws KuraException {
    s_logger.debug("getting the firewall configuration");
    FirewallConfiguration firewallConfiguration = new FirewallConfiguration();
    LinuxFirewall firewall = LinuxFirewall.getInstance();
    Iterator<LocalRule> localRules = firewall.getLocalRules().iterator();
    while (localRules.hasNext()) {
        LocalRule localRule = localRules.next();
        if (localRule.getPortRange() != null) {
            s_logger.debug("getFirewallConfiguration() :: Adding local rule for {}", localRule.getPortRange());
            firewallConfiguration.addConfig(new FirewallOpenPortConfigIP4(localRule.getPortRange(), NetProtocol.valueOf(localRule.getProtocol()), localRule.getPermittedNetwork(), localRule.getPermittedInterfaceName(), localRule.getUnpermittedInterfaceName(), localRule.getPermittedMAC(), localRule.getSourcePortRange()));
        } else {
            s_logger.debug("getFirewallConfiguration() :: Adding local rule for {}", localRule.getPort());
            firewallConfiguration.addConfig(new FirewallOpenPortConfigIP4(localRule.getPort(), NetProtocol.valueOf(localRule.getProtocol()), localRule.getPermittedNetwork(), localRule.getPermittedInterfaceName(), localRule.getUnpermittedInterfaceName(), localRule.getPermittedMAC(), localRule.getSourcePortRange()));
        }
    }
    Iterator<PortForwardRule> portForwardRules = firewall.getPortForwardRules().iterator();
    while (portForwardRules.hasNext()) {
        PortForwardRule portForwardRule = portForwardRules.next();
        try {
            s_logger.debug("getFirewallConfiguration() :: Adding port forwarding - inbound iface is {}", portForwardRule.getInboundIface());
            firewallConfiguration.addConfig(new FirewallPortForwardConfigIP4(portForwardRule.getInboundIface(), portForwardRule.getOutboundIface(), (IP4Address) IPAddress.parseHostAddress(portForwardRule.getAddress()), NetProtocol.valueOf(portForwardRule.getProtocol()), portForwardRule.getInPort(), portForwardRule.getOutPort(), portForwardRule.isMasquerade(), new NetworkPair<IP4Address>((IP4Address) IPAddress.parseHostAddress(portForwardRule.getPermittedNetwork()), (short) portForwardRule.getPermittedNetworkMask()), portForwardRule.getPermittedMAC(), portForwardRule.getSourcePortRange()));
        } catch (UnknownHostException e) {
            e.printStackTrace();
            throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
        }
    }
    Iterator<NATRule> autoNatRules = firewall.getAutoNatRules().iterator();
    while (autoNatRules.hasNext()) {
        NATRule autoNatRule = autoNatRules.next();
        s_logger.debug("getFirewallConfiguration() :: Adding auto NAT rules {}", autoNatRule.getSourceInterface());
        firewallConfiguration.addConfig(new FirewallAutoNatConfig(autoNatRule.getSourceInterface(), autoNatRule.getDestinationInterface(), autoNatRule.isMasquerade()));
    }
    Iterator<NATRule> natRules = firewall.getNatRules().iterator();
    while (natRules.hasNext()) {
        NATRule natRule = natRules.next();
        s_logger.debug("getFirewallConfiguration() :: Adding NAT rules {}", natRule.getSourceInterface());
        firewallConfiguration.addConfig(new FirewallNatConfig(natRule.getSourceInterface(), natRule.getDestinationInterface(), natRule.getProtocol(), natRule.getSource(), natRule.getDestination(), natRule.isMasquerade()));
    }
    return firewallConfiguration;
}
Also used : UnknownHostException(java.net.UnknownHostException) FirewallAutoNatConfig(org.eclipse.kura.net.firewall.FirewallAutoNatConfig) PortForwardRule(org.eclipse.kura.linux.net.iptables.PortForwardRule) IP4Address(org.eclipse.kura.net.IP4Address) FirewallConfiguration(org.eclipse.kura.core.net.FirewallConfiguration) LinuxFirewall(org.eclipse.kura.linux.net.iptables.LinuxFirewall) NATRule(org.eclipse.kura.linux.net.iptables.NATRule) FirewallNatConfig(org.eclipse.kura.net.firewall.FirewallNatConfig) NetworkPair(org.eclipse.kura.net.NetworkPair) KuraException(org.eclipse.kura.KuraException) FirewallPortForwardConfigIP4(org.eclipse.kura.net.firewall.FirewallPortForwardConfigIP4) FirewallOpenPortConfigIP4(org.eclipse.kura.net.firewall.FirewallOpenPortConfigIP4) LocalRule(org.eclipse.kura.linux.net.iptables.LocalRule)

Aggregations

KuraException (org.eclipse.kura.KuraException)3 FirewallOpenPortConfigIP4 (org.eclipse.kura.net.firewall.FirewallOpenPortConfigIP4)3 UnknownHostException (java.net.UnknownHostException)2 ArrayList (java.util.ArrayList)2 IP4Address (org.eclipse.kura.net.IP4Address)2 NetworkAdminService (org.eclipse.kura.net.NetworkAdminService)2 GwtKuraException (org.eclipse.kura.web.shared.GwtKuraException)2 GwtFirewallOpenPortEntry (org.eclipse.kura.web.shared.model.GwtFirewallOpenPortEntry)2 FirewallConfiguration (org.eclipse.kura.core.net.FirewallConfiguration)1 LinuxFirewall (org.eclipse.kura.linux.net.iptables.LinuxFirewall)1 LocalRule (org.eclipse.kura.linux.net.iptables.LocalRule)1 NATRule (org.eclipse.kura.linux.net.iptables.NATRule)1 PortForwardRule (org.eclipse.kura.linux.net.iptables.PortForwardRule)1 IPAddress (org.eclipse.kura.net.IPAddress)1 NetConfig (org.eclipse.kura.net.NetConfig)1 NetworkPair (org.eclipse.kura.net.NetworkPair)1 FirewallAutoNatConfig (org.eclipse.kura.net.firewall.FirewallAutoNatConfig)1 FirewallNatConfig (org.eclipse.kura.net.firewall.FirewallNatConfig)1 FirewallOpenPortConfigIP (org.eclipse.kura.net.firewall.FirewallOpenPortConfigIP)1 FirewallPortForwardConfigIP4 (org.eclipse.kura.net.firewall.FirewallPortForwardConfigIP4)1