Search in sources :

Example 1 with WifiCiphers

use of org.eclipse.kura.net.wifi.WifiCiphers in project kura by eclipse.

the class WpaSupplicant method parseWpaConfig.

private static WpaSupplicant parseWpaConfig(String ifaceName) throws KuraException {
    Properties props = parseConfigFile();
    if (props == null) {
        s_logger.warn("WPA in client mode is not configured");
        return null;
    }
    String ssid = props.getProperty("ssid");
    if (ssid == null) {
        s_logger.warn("WPA in client mode is not configured");
        return null;
    }
    s_logger.debug("curent wpa_supplicant.conf: ssid={}", ssid);
    int[] channels = null;
    // wifi mode
    int mode = props.getProperty("mode") != null ? Integer.parseInt(props.getProperty("mode")) : MODE_INFRA;
    s_logger.debug("current wpa_supplicant.conf: mode={}", mode);
    WifiMode wifiMode = null;
    switch(mode) {
        case MODE_INFRA:
            wifiMode = WifiMode.INFRA;
            String scan_freq = props.getProperty("scan_freq");
            if (scan_freq != null && scan_freq.length() > 0) {
                s_logger.debug("current wpa_supplicant.conf: scan_freq={}", scan_freq);
                String[] saScanFreq = scan_freq.split(" ");
                channels = new int[saScanFreq.length];
                for (int i = 0; i < channels.length; i++) {
                    try {
                        channels[i] = frequencyMhz2Channel(Integer.parseInt(saScanFreq[i]));
                    } catch (NumberFormatException e) {
                        s_logger.warn("Invalid string in wpa_supplicant.conf for scan_freq: " + scan_freq);
                    }
                }
            } else {
                channels = new int[11];
                for (int i = 0; i < channels.length; i++) {
                    channels[i] = i + 1;
                }
            }
            break;
        case MODE_IBSS:
            channels = new int[1];
            wifiMode = WifiMode.ADHOC;
            String frequency = props.getProperty("frequency");
            s_logger.debug("current wpa_supplicant.conf: frequency={}", frequency);
            int freq = 2412;
            if (frequency != null) {
                try {
                    freq = Integer.parseInt(frequency);
                    channels[0] = frequencyMhz2Channel(freq);
                } catch (NumberFormatException e) {
                    freq = 2412;
                }
            }
            break;
        case MODE_AP:
            throw KuraException.internalError("wpa_supplicant failed to parse its configuration file: MODE_AP is invalid");
        default:
            throw KuraException.internalError("wpa_supplicant failed to parse its configuration file: invalid mode: " + mode);
    }
    String proto = props.getProperty("proto");
    if (proto != null) {
        s_logger.debug("current wpa_supplicant.conf: proto={}", proto);
    }
    WifiCiphers pairwiseCiphers = null;
    String pairwise = props.getProperty("pairwise");
    if (pairwise != null) {
        s_logger.debug("current wpa_supplicant.conf: pairwise={}", pairwise);
        if (pairwise.contains(WifiCiphers.toString(WifiCiphers.CCMP_TKIP))) {
            pairwiseCiphers = WifiCiphers.CCMP_TKIP;
        } else if (pairwise.contains(WifiCiphers.toString(WifiCiphers.TKIP))) {
            pairwiseCiphers = WifiCiphers.TKIP;
        } else if (pairwise.contains(WifiCiphers.toString(WifiCiphers.CCMP))) {
            pairwiseCiphers = WifiCiphers.CCMP;
        }
    }
    WifiCiphers groupCiphers = null;
    String group = props.getProperty("group");
    if (group != null) {
        s_logger.debug("current wpa_supplicant.conf: group={}", group);
        if (group.contains(WifiCiphers.toString(WifiCiphers.CCMP_TKIP))) {
            groupCiphers = WifiCiphers.CCMP_TKIP;
        } else if (group.contains(WifiCiphers.toString(WifiCiphers.TKIP))) {
            groupCiphers = WifiCiphers.TKIP;
        } else if (group.contains(WifiCiphers.toString(WifiCiphers.CCMP))) {
            groupCiphers = WifiCiphers.CCMP;
        }
    }
    // security
    WifiSecurity wifiSecurity = null;
    String password = null;
    String keyMgmt = props.getProperty("key_mgmt");
    s_logger.debug("current wpa_supplicant.conf: key_mgmt={}", keyMgmt);
    if (keyMgmt != null && keyMgmt.equalsIgnoreCase("WPA-PSK")) {
        password = props.getProperty("psk");
        if (proto != null) {
            if (proto.trim().equals("WPA")) {
                wifiSecurity = WifiSecurity.SECURITY_WPA;
            } else if (proto.trim().equals("RSN")) {
                wifiSecurity = WifiSecurity.SECURITY_WPA2;
            } else if (proto.trim().equals("WPA RSN")) {
                wifiSecurity = WifiSecurity.SECURITY_WPA_WPA2;
            }
        } else {
            wifiSecurity = WifiSecurity.SECURITY_WPA_WPA2;
        }
    } else {
        password = props.getProperty("wep_key0");
        if (password != null) {
            wifiSecurity = WifiSecurity.SECURITY_WEP;
        } else {
            wifiSecurity = WifiSecurity.SECURITY_NONE;
        }
        pairwiseCiphers = null;
        groupCiphers = null;
    }
    WifiBgscan bgscan = null;
    String sBgscan = props.getProperty("bgscan");
    if (sBgscan != null) {
        s_logger.debug("current wpa_supplicant.conf: bgscan={}", sBgscan);
        bgscan = new WifiBgscan(sBgscan);
    }
    return getWpaSupplicant(ifaceName, wifiMode, null, ssid, wifiSecurity, pairwiseCiphers, groupCiphers, channels, password, bgscan);
}
Also used : WifiCiphers(org.eclipse.kura.net.wifi.WifiCiphers) WifiMode(org.eclipse.kura.net.wifi.WifiMode) WifiSecurity(org.eclipse.kura.net.wifi.WifiSecurity) WifiBgscan(org.eclipse.kura.net.wifi.WifiBgscan) Properties(java.util.Properties)

Example 2 with WifiCiphers

use of org.eclipse.kura.net.wifi.WifiCiphers in project kura by eclipse.

the class WpaSupplicantConfigReader method getWifiClientConfig.

private static WifiConfig getWifiClientConfig(String ifaceName, WifiMode wifiMode) throws KuraException {
    WifiConfig wifiConfig = new WifiConfig();
    String ssid = "";
    WifiSecurity wifiSecurity = WifiSecurity.NONE;
    String password = "";
    int[] channels = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
    WifiBgscan bgscan = new WifiBgscan("");
    WifiCiphers pairwiseCiphers = null;
    WifiCiphers groupCiphers = null;
    // Get properties from config file
    Properties props = parseConfigFile(ifaceName);
    if (props == null) {
        s_logger.warn("WPA in client mode is not configured");
    } else {
        ssid = props.getProperty("ssid");
        if (ssid == null) {
            s_logger.warn("WPA in client mode is not configured");
        } else {
            s_logger.debug("curent wpa_supplicant.conf: ssid={}", ssid);
            // wifi mode
            int currentMode = props.getProperty("mode") != null ? Integer.parseInt(props.getProperty("mode")) : WpaSupplicantUtil.MODE_INFRA;
            s_logger.debug("current wpa_supplicant.conf: mode={}", currentMode);
            switch(wifiMode) {
                case INFRA:
                    String scan_freq = props.getProperty("scan_freq");
                    if (scan_freq != null) {
                        s_logger.debug("current wpa_supplicant.conf: scan_freq={}", scan_freq);
                        String[] saScanFreq = scan_freq.split(" ");
                        channels = new int[saScanFreq.length];
                        for (int i = 0; i < channels.length; i++) {
                            try {
                                channels[i] = WpaSupplicantUtil.convFrequencyToChannel(Integer.parseInt(saScanFreq[i]));
                            } catch (NumberFormatException e) {
                            }
                        }
                    }
                    break;
                /*
                 * case ADHOC:
                 * channels = new int [1];
                 * String frequency = props.getProperty("frequency");
                 * s_logger.debug("current wpa_supplicant.conf: frequency=" + frequency);
                 * int freq = 2412;
                 * if (frequency != null) {
                 * try {
                 * freq = Integer.parseInt(frequency);
                 * } catch (NumberFormatException e) {
                 * }
                 * }
                 * channels[0] = WpaSupplicantUtil.convFrequencyToChannel(freq);
                 * wifiDriver = KuranetConfig.getProperty(adhocDriverKey.toString());
                 * if(wifiDriver == null) {
                 * wifiDriver = KuranetConfig.getProperty(infraDriverKey.toString());
                 * }
                 * break;
                 */
                case MASTER:
                    throw KuraException.internalError("failed to get wpa_supplicant configuration: MASTER mode is invalid");
                default:
                    throw KuraException.internalError("failed to get wpa_supplicant configuration: invalid mode: " + wifiMode);
            }
            String proto = props.getProperty("proto");
            if (proto != null) {
                s_logger.debug("current wpa_supplicant.conf: proto={}", proto);
            }
            String pairwise = props.getProperty("pairwise");
            if (pairwise != null) {
                s_logger.debug("current wpa_supplicant.conf: pairwise={}", pairwise);
                if (pairwise.contains(WifiCiphers.toString(WifiCiphers.CCMP_TKIP))) {
                    pairwiseCiphers = WifiCiphers.CCMP_TKIP;
                } else if (pairwise.contains(WifiCiphers.toString(WifiCiphers.TKIP))) {
                    pairwiseCiphers = WifiCiphers.TKIP;
                } else if (pairwise.contains(WifiCiphers.toString(WifiCiphers.CCMP))) {
                    pairwiseCiphers = WifiCiphers.CCMP;
                }
            }
            String group = props.getProperty("group");
            if (group != null) {
                s_logger.debug("current wpa_supplicant.conf: group={}", group);
                if (group.contains(WifiCiphers.toString(WifiCiphers.CCMP_TKIP))) {
                    groupCiphers = WifiCiphers.CCMP_TKIP;
                } else if (group.contains(WifiCiphers.toString(WifiCiphers.TKIP))) {
                    groupCiphers = WifiCiphers.TKIP;
                } else if (group.contains(WifiCiphers.toString(WifiCiphers.CCMP))) {
                    groupCiphers = WifiCiphers.CCMP;
                }
            }
            // security
            String keyMgmt = props.getProperty("key_mgmt");
            s_logger.debug("current wpa_supplicant.conf: key_mgmt={}", keyMgmt);
            if (keyMgmt != null && keyMgmt.equalsIgnoreCase("WPA-PSK")) {
                password = props.getProperty("psk");
                if (proto != null) {
                    if (proto.trim().equals("WPA")) {
                        wifiSecurity = WifiSecurity.SECURITY_WPA;
                    } else if (proto.trim().equals("RSN")) {
                        wifiSecurity = WifiSecurity.SECURITY_WPA2;
                    } else if (proto.trim().equals("WPA RSN")) {
                        wifiSecurity = WifiSecurity.SECURITY_WPA_WPA2;
                    }
                } else {
                    wifiSecurity = WifiSecurity.SECURITY_WPA2;
                }
            } else {
                password = props.getProperty("wep_key0");
                if (password != null) {
                    wifiSecurity = WifiSecurity.SECURITY_WEP;
                } else {
                    wifiSecurity = WifiSecurity.SECURITY_NONE;
                }
                pairwiseCiphers = null;
                groupCiphers = null;
            }
            if (password == null) {
                password = "";
            }
            String sBgscan = props.getProperty("bgscan");
            if (sBgscan != null) {
                s_logger.debug("current wpa_supplicant.conf: bgscan={}", sBgscan);
                bgscan = new WifiBgscan(sBgscan);
            }
        }
    }
    // Populate the config
    wifiConfig.setMode(wifiMode);
    wifiConfig.setSSID(ssid);
    wifiConfig.setSecurity(wifiSecurity);
    wifiConfig.setPasskey(password);
    wifiConfig.setHardwareMode("");
    wifiConfig.setPairwiseCiphers(pairwiseCiphers);
    wifiConfig.setGroupCiphers(groupCiphers);
    wifiConfig.setChannels(channels);
    wifiConfig.setBgscan(bgscan);
    // Get self-stored properties
    boolean pingAP = false;
    StringBuilder key = new StringBuilder().append("net.interface.").append(ifaceName).append(".config.wifi.infra.pingAccessPoint");
    String statusString = KuranetConfig.getProperty(key.toString());
    if (statusString != null && !statusString.isEmpty()) {
        pingAP = Boolean.parseBoolean(statusString);
    }
    wifiConfig.setPingAccessPoint(pingAP);
    boolean ignoreSSID = false;
    key = new StringBuilder().append("net.interface.").append(ifaceName).append(".config.wifi.infra.ignoreSSID");
    statusString = KuranetConfig.getProperty(key.toString());
    if (statusString != null && !statusString.isEmpty()) {
        ignoreSSID = Boolean.parseBoolean(statusString);
    }
    wifiConfig.setIgnoreSSID(ignoreSSID);
    StringBuilder infraDriverKey = new StringBuilder("net.interface.").append(ifaceName).append(".config.wifi.infra.driver");
    // StringBuilder adhocDriverKey = new
    // StringBuilder("net.interface.").append(ifaceName).append(".config.wifi.adhoc.driver");
    String wifiDriver = KuranetConfig.getProperty(infraDriverKey.toString());
    if (wifiDriver == null || wifiDriver.isEmpty()) {
        wifiDriver = "nl80211";
    }
    wifiConfig.setDriver(wifiDriver);
    return wifiConfig;
}
Also used : WifiCiphers(org.eclipse.kura.net.wifi.WifiCiphers) WifiConfig(org.eclipse.kura.net.wifi.WifiConfig) WifiSecurity(org.eclipse.kura.net.wifi.WifiSecurity) WifiBgscan(org.eclipse.kura.net.wifi.WifiBgscan) Properties(java.util.Properties)

Example 3 with WifiCiphers

use of org.eclipse.kura.net.wifi.WifiCiphers in project kura by eclipse.

the class HostapdConfigWriter method generateHostapdConf.

/*
     * This method generates hostapd configuration file
     */
private void generateHostapdConf(WifiConfig wifiConfig, String interfaceName, String interfaceDriver) throws Exception {
    s_logger.debug("Generating Hostapd Config");
    if (wifiConfig.getSecurity() == WifiSecurity.SECURITY_NONE || wifiConfig.getSecurity() == WifiSecurity.NONE) {
        File outputFile = new File(HOSTAPD_TMP_CONFIG_FILE);
        // replace the necessary components
        String fileAsString = IOUtil.readResource(FrameworkUtil.getBundle(getClass()), "/src/main/resources/wifi/hostapd.conf_no_security");
        if (interfaceName != null) {
            fileAsString = fileAsString.replaceFirst("KURA_INTERFACE", interfaceName);
        } else {
            throw KuraException.internalError("the interface name can not be null");
        }
        if (interfaceDriver != null && interfaceDriver.length() > 0) {
            fileAsString = fileAsString.replaceFirst("KURA_DRIVER", interfaceDriver);
        } else {
            String drv = Hostapd.getDriver(interfaceName);
            s_logger.warn("The 'driver' parameter must be set: setting to: {}", drv);
            fileAsString = fileAsString.replaceFirst("KURA_DRIVER", drv);
        // throw KuraException.internalError("the driver name can not be null");
        }
        if (wifiConfig.getSSID() != null) {
            fileAsString = fileAsString.replaceFirst("KURA_ESSID", wifiConfig.getSSID());
        } else {
            throw KuraException.internalError("the essid can not be null");
        }
        WifiRadioMode radioMode = wifiConfig.getRadioMode();
        if (radioMode == WifiRadioMode.RADIO_MODE_80211a) {
            fileAsString = fileAsString.replaceFirst("KURA_HW_MODE", "a");
            fileAsString = fileAsString.replaceFirst("KURA_WME_ENABLED", "0");
            fileAsString = fileAsString.replaceFirst("KURA_IEEE80211N", "0");
            fileAsString = fileAsString.replaceFirst("ht_capab=KURA_HTCAPAB", "");
        } else if (radioMode == WifiRadioMode.RADIO_MODE_80211b) {
            fileAsString = fileAsString.replaceFirst("KURA_HW_MODE", "b");
            fileAsString = fileAsString.replaceFirst("KURA_WME_ENABLED", "0");
            fileAsString = fileAsString.replaceFirst("KURA_IEEE80211N", "0");
            fileAsString = fileAsString.replaceFirst("ht_capab=KURA_HTCAPAB", "");
        } else if (radioMode == WifiRadioMode.RADIO_MODE_80211g) {
            fileAsString = fileAsString.replaceFirst("KURA_HW_MODE", "g");
            fileAsString = fileAsString.replaceFirst("KURA_WME_ENABLED", "0");
            fileAsString = fileAsString.replaceFirst("KURA_IEEE80211N", "0");
            fileAsString = fileAsString.replaceFirst("ht_capab=KURA_HTCAPAB", "");
        } else if (radioMode == WifiRadioMode.RADIO_MODE_80211nHT20) {
            fileAsString = fileAsString.replaceFirst("KURA_HW_MODE", "g");
            fileAsString = fileAsString.replaceFirst("KURA_WME_ENABLED", "1");
            fileAsString = fileAsString.replaceFirst("KURA_IEEE80211N", "1");
            fileAsString = fileAsString.replaceFirst("KURA_HTCAPAB", "[SHORT-GI-20]");
        } else if (radioMode == WifiRadioMode.RADIO_MODE_80211nHT40above) {
            fileAsString = fileAsString.replaceFirst("KURA_HW_MODE", "g");
            fileAsString = fileAsString.replaceFirst("KURA_WME_ENABLED", "1");
            fileAsString = fileAsString.replaceFirst("KURA_IEEE80211N", "1");
            fileAsString = fileAsString.replaceFirst("KURA_HTCAPAB", "[HT40+][SHORT-GI-20][SHORT-GI-40]");
        } else if (radioMode == WifiRadioMode.RADIO_MODE_80211nHT40below) {
            fileAsString = fileAsString.replaceFirst("KURA_HW_MODE", "g");
            fileAsString = fileAsString.replaceFirst("KURA_WME_ENABLED", "1");
            fileAsString = fileAsString.replaceFirst("KURA_IEEE80211N", "1");
            fileAsString = fileAsString.replaceFirst("KURA_HTCAPAB", "[HT40-][SHORT-GI-20][SHORT-GI-40]");
        } else {
            throw KuraException.internalError("invalid hardware mode");
        }
        if (wifiConfig.getChannels()[0] > 0 && wifiConfig.getChannels()[0] < 14) {
            fileAsString = fileAsString.replaceFirst("KURA_CHANNEL", Integer.toString(wifiConfig.getChannels()[0]));
        } else {
            throw KuraException.internalError("the channel " + wifiConfig.getChannels()[0] + " must be between 1 (inclusive) and 11 (inclusive) or 1 (inclusive) and 13 (inclusive) depending on your locale");
        }
        if (wifiConfig.ignoreSSID()) {
            fileAsString = fileAsString.replaceFirst("KURA_IGNORE_BROADCAST_SSID", "2");
        } else {
            fileAsString = fileAsString.replaceFirst("KURA_IGNORE_BROADCAST_SSID", "0");
        }
        // everything is set and we haven't failed - write the file
        copyFile(fileAsString, outputFile);
        // move the file if we made it this far
        moveFile(interfaceName);
        return;
    } else if (wifiConfig.getSecurity() == WifiSecurity.SECURITY_WEP) {
        File outputFile = new File(HOSTAPD_TMP_CONFIG_FILE);
        // replace the necessary components
        String fileAsString = IOUtil.readResource(FrameworkUtil.getBundle(getClass()), "/src/main/resources/wifi/hostapd.conf_wep");
        if (interfaceName != null) {
            fileAsString = fileAsString.replaceFirst("KURA_INTERFACE", interfaceName);
        } else {
            throw KuraException.internalError("the interface name can not be null");
        }
        if (interfaceDriver != null && interfaceDriver.length() > 0) {
            fileAsString = fileAsString.replaceFirst("KURA_DRIVER", interfaceDriver);
        } else {
            String drv = Hostapd.getDriver(interfaceName);
            s_logger.warn("The 'driver' parameter must be set: setting to: {}", drv);
            fileAsString = fileAsString.replaceFirst("KURA_DRIVER", drv);
        // throw KuraException.internalError("the driver name can not be null");
        }
        if (wifiConfig.getSSID() != null) {
            fileAsString = fileAsString.replaceFirst("KURA_ESSID", wifiConfig.getSSID());
        } else {
            throw KuraException.internalError("the essid can not be null");
        }
        WifiRadioMode radioMode = wifiConfig.getRadioMode();
        if (radioMode == WifiRadioMode.RADIO_MODE_80211a) {
            fileAsString = fileAsString.replaceFirst("KURA_HW_MODE", "a");
            fileAsString = fileAsString.replaceFirst("KURA_WME_ENABLED", "0");
            fileAsString = fileAsString.replaceFirst("KURA_IEEE80211N", "0");
            fileAsString = fileAsString.replaceFirst("ht_capab=KURA_HTCAPAB", "");
        } else if (radioMode == WifiRadioMode.RADIO_MODE_80211b) {
            fileAsString = fileAsString.replaceFirst("KURA_HW_MODE", "b");
            fileAsString = fileAsString.replaceFirst("KURA_WME_ENABLED", "0");
            fileAsString = fileAsString.replaceFirst("KURA_IEEE80211N", "0");
            fileAsString = fileAsString.replaceFirst("ht_capab=KURA_HTCAPAB", "");
        } else if (radioMode == WifiRadioMode.RADIO_MODE_80211g) {
            fileAsString = fileAsString.replaceFirst("KURA_HW_MODE", "g");
            fileAsString = fileAsString.replaceFirst("KURA_WME_ENABLED", "0");
            fileAsString = fileAsString.replaceFirst("KURA_IEEE80211N", "0");
            fileAsString = fileAsString.replaceFirst("ht_capab=KURA_HTCAPAB", "");
        } else if (radioMode == WifiRadioMode.RADIO_MODE_80211nHT20) {
            fileAsString = fileAsString.replaceFirst("KURA_HW_MODE", "g");
            fileAsString = fileAsString.replaceFirst("KURA_WME_ENABLED", "1");
            fileAsString = fileAsString.replaceFirst("KURA_IEEE80211N", "1");
            fileAsString = fileAsString.replaceFirst("KURA_HTCAPAB", "[SHORT-GI-20]");
        } else if (radioMode == WifiRadioMode.RADIO_MODE_80211nHT40above) {
            fileAsString = fileAsString.replaceFirst("KURA_HW_MODE", "g");
            fileAsString = fileAsString.replaceFirst("KURA_WME_ENABLED", "1");
            fileAsString = fileAsString.replaceFirst("KURA_IEEE80211N", "1");
            fileAsString = fileAsString.replaceFirst("KURA_HTCAPAB", "[HT40+][SHORT-GI-20][SHORT-GI-40]");
        } else if (radioMode == WifiRadioMode.RADIO_MODE_80211nHT40below) {
            fileAsString = fileAsString.replaceFirst("KURA_HW_MODE", "g");
            fileAsString = fileAsString.replaceFirst("KURA_WME_ENABLED", "1");
            fileAsString = fileAsString.replaceFirst("KURA_IEEE80211N", "1");
            fileAsString = fileAsString.replaceFirst("KURA_HTCAPAB", "[HT40-][SHORT-GI-20][SHORT-GI-40]");
        } else {
            throw KuraException.internalError("invalid hardware mode");
        }
        if (wifiConfig.getChannels()[0] > 0 && wifiConfig.getChannels()[0] < 14) {
            fileAsString = fileAsString.replaceFirst("KURA_CHANNEL", Integer.toString(wifiConfig.getChannels()[0]));
        } else {
            throw KuraException.internalError("the channel must be between 1 (inclusive) and 11 (inclusive) or 1 (inclusive) and 13 (inclusive) depending on your locale");
        }
        String passKey = new String(wifiConfig.getPasskey().getPassword());
        if (passKey != null) {
            if (passKey.length() == 10) {
                // check to make sure it is all hex
                try {
                    Long.parseLong(passKey, 16);
                } catch (Exception e) {
                    throw KuraException.internalError("the WEP key (passwd) must be all HEX characters (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, and f");
                }
                // since we're here - save the password
                fileAsString = fileAsString.replaceFirst("KURA_WEP_KEY", passKey);
            } else if (passKey.length() == 26) {
                String part1 = passKey.substring(0, 13);
                String part2 = passKey.substring(13);
                try {
                    Long.parseLong(part1, 16);
                    Long.parseLong(part2, 16);
                } catch (Exception e) {
                    throw KuraException.internalError("the WEP key (passwd) must be all HEX characters (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, and f");
                }
                // since we're here - save the password
                fileAsString = fileAsString.replaceFirst("KURA_WEP_KEY", passKey);
            } else if (passKey.length() == 32) {
                String part1 = passKey.substring(0, 10);
                String part2 = passKey.substring(10, 20);
                String part3 = passKey.substring(20);
                try {
                    Long.parseLong(part1, 16);
                    Long.parseLong(part2, 16);
                    Long.parseLong(part3, 16);
                } catch (Exception e) {
                    throw KuraException.internalError("the WEP key (passwd) must be all HEX characters (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, and f");
                }
                // since we're here - save the password
                fileAsString = fileAsString.replaceFirst("KURA_WEP_KEY", passKey);
            } else if (passKey.length() == 5 || passKey.length() == 13 || passKey.length() == 16) {
                // 5, 13, or 16 ASCII characters
                passKey = toHex(passKey);
                // since we're here - save the password
                fileAsString = fileAsString.replaceFirst("KURA_WEP_KEY", passKey);
            } else {
                throw KuraException.internalError("the WEP key (passwd) must be 10, 26, or 32 HEX characters in length");
            }
        }
        if (wifiConfig.ignoreSSID()) {
            fileAsString = fileAsString.replaceFirst("KURA_IGNORE_BROADCAST_SSID", "2");
        } else {
            fileAsString = fileAsString.replaceFirst("KURA_IGNORE_BROADCAST_SSID", "0");
        }
        // everything is set and we haven't failed - write the file
        copyFile(fileAsString, outputFile);
        // move the file if we made it this far
        moveFile(interfaceName);
        return;
    } else if (wifiConfig.getSecurity() == WifiSecurity.SECURITY_WPA || wifiConfig.getSecurity() == WifiSecurity.SECURITY_WPA2 || wifiConfig.getSecurity() == WifiSecurity.SECURITY_WPA_WPA2) {
        File tmpOutputFile = new File(HOSTAPD_TMP_CONFIG_FILE);
        /*
             * String resName = null;
             * if (wifiConfig.getSecurity() == WifiSecurity.SECURITY_WPA) {
             * resName = "/src/main/resources/wifi/hostapd.conf_master_wpa_psk";
             * } else if (wifiConfig.getSecurity() == WifiSecurity.SECURITY_WPA2) {
             * resName = "/src/main/resources/wifi/hostapd.conf_master_wpa2_psk";
             * }
             */
        String resName = "/src/main/resources/wifi/hostapd.conf_master_wpa_wpa2_psk";
        // replace the necessary components
        String fileAsString = IOUtil.readResource(FrameworkUtil.getBundle(getClass()), resName);
        if (interfaceName != null) {
            fileAsString = fileAsString.replaceFirst("KURA_INTERFACE", interfaceName);
        } else {
            throw KuraException.internalError("the interface name can not be null");
        }
        if (interfaceDriver != null && interfaceDriver.length() > 0) {
            fileAsString = fileAsString.replaceFirst("KURA_DRIVER", interfaceDriver);
        } else {
            String drv = Hostapd.getDriver(interfaceName);
            s_logger.warn("The 'driver' parameter must be set: setting to: {}", drv);
            fileAsString = fileAsString.replaceFirst("KURA_DRIVER", drv);
        // throw KuraException.internalError("the driver name can not be null");
        }
        if (wifiConfig.getSSID() != null) {
            fileAsString = fileAsString.replaceFirst("KURA_ESSID", wifiConfig.getSSID());
        } else {
            throw KuraException.internalError("the essid can not be null");
        }
        WifiRadioMode radioMode = wifiConfig.getRadioMode();
        if (radioMode == WifiRadioMode.RADIO_MODE_80211a) {
            fileAsString = fileAsString.replaceFirst("KURA_HW_MODE", "a");
            fileAsString = fileAsString.replaceFirst("KURA_WME_ENABLED", "0");
            fileAsString = fileAsString.replaceFirst("KURA_IEEE80211N", "0");
            fileAsString = fileAsString.replaceFirst("ht_capab=KURA_HTCAPAB", "");
        } else if (radioMode == WifiRadioMode.RADIO_MODE_80211b) {
            fileAsString = fileAsString.replaceFirst("KURA_HW_MODE", "b");
            fileAsString = fileAsString.replaceFirst("KURA_WME_ENABLED", "0");
            fileAsString = fileAsString.replaceFirst("KURA_IEEE80211N", "0");
            fileAsString = fileAsString.replaceFirst("ht_capab=KURA_HTCAPAB", "");
        } else if (radioMode == WifiRadioMode.RADIO_MODE_80211g) {
            fileAsString = fileAsString.replaceFirst("KURA_HW_MODE", "g");
            fileAsString = fileAsString.replaceFirst("KURA_WME_ENABLED", "0");
            fileAsString = fileAsString.replaceFirst("KURA_IEEE80211N", "0");
            fileAsString = fileAsString.replaceFirst("ht_capab=KURA_HTCAPAB", "");
        } else if (radioMode == WifiRadioMode.RADIO_MODE_80211nHT20) {
            fileAsString = fileAsString.replaceFirst("KURA_HW_MODE", "g");
            fileAsString = fileAsString.replaceFirst("KURA_WME_ENABLED", "1");
            fileAsString = fileAsString.replaceFirst("KURA_IEEE80211N", "1");
            fileAsString = fileAsString.replaceFirst("KURA_HTCAPAB", "[SHORT-GI-20]");
        } else if (radioMode == WifiRadioMode.RADIO_MODE_80211nHT40above) {
            fileAsString = fileAsString.replaceFirst("KURA_HW_MODE", "g");
            fileAsString = fileAsString.replaceFirst("KURA_WME_ENABLED", "1");
            fileAsString = fileAsString.replaceFirst("KURA_IEEE80211N", "1");
            fileAsString = fileAsString.replaceFirst("KURA_HTCAPAB", "[HT40+][SHORT-GI-20][SHORT-GI-40]");
        } else if (radioMode == WifiRadioMode.RADIO_MODE_80211nHT40below) {
            fileAsString = fileAsString.replaceFirst("KURA_HW_MODE", "g");
            fileAsString = fileAsString.replaceFirst("KURA_WME_ENABLED", "1");
            fileAsString = fileAsString.replaceFirst("KURA_IEEE80211N", "1");
            fileAsString = fileAsString.replaceFirst("KURA_HTCAPAB", "[HT40-][SHORT-GI-20][SHORT-GI-40]");
        } else {
            throw KuraException.internalError("invalid hardware mode");
        }
        if (wifiConfig.getChannels()[0] > 0 && wifiConfig.getChannels()[0] < 14) {
            fileAsString = fileAsString.replaceFirst("KURA_CHANNEL", Integer.toString(wifiConfig.getChannels()[0]));
        } else {
            throw KuraException.internalError("the channel must be between 1 (inclusive) and 11 (inclusive) or 1 (inclusive) and 13 (inclusive) depending on your locale");
        }
        if (wifiConfig.getSecurity() == WifiSecurity.SECURITY_WPA) {
            fileAsString = fileAsString.replaceFirst("KURA_SECURITY", "1");
        } else if (wifiConfig.getSecurity() == WifiSecurity.SECURITY_WPA2) {
            fileAsString = fileAsString.replaceFirst("KURA_SECURITY", "2");
        } else if (wifiConfig.getSecurity() == WifiSecurity.SECURITY_WPA_WPA2) {
            fileAsString = fileAsString.replaceFirst("KURA_SECURITY", "3");
        } else {
            throw KuraException.internalError("invalid WiFi Security");
        }
        WifiCiphers wifiCiphers = wifiConfig.getPairwiseCiphers();
        if (wifiCiphers == WifiCiphers.TKIP) {
            fileAsString = fileAsString.replaceFirst("KURA_PAIRWISE_CIPHER", "TKIP");
        } else if (wifiCiphers == WifiCiphers.CCMP) {
            fileAsString = fileAsString.replaceFirst("KURA_PAIRWISE_CIPHER", "CCMP");
        } else if (wifiCiphers == WifiCiphers.CCMP_TKIP) {
            fileAsString = fileAsString.replaceFirst("KURA_PAIRWISE_CIPHER", "CCMP TKIP");
        } else {
            throw KuraException.internalError("invalid WiFi Pairwise Ciphers");
        }
        String passKey = new String(wifiConfig.getPasskey().getPassword());
        if (wifiConfig.getPasskey() != null && passKey.trim().length() > 0) {
            if (passKey.length() < 8 || passKey.length() > 63) {
                throw KuraException.internalError("the WPA passphrase (passwd) must be between 8 (inclusive) and 63 (inclusive) characters in length: " + wifiConfig.getPasskey());
            } else {
                fileAsString = fileAsString.replaceFirst("KURA_PASSPHRASE", passKey.trim());
            }
        } else {
            throw KuraException.internalError("the passwd can not be null");
        }
        if (wifiConfig.ignoreSSID()) {
            fileAsString = fileAsString.replaceFirst("KURA_IGNORE_BROADCAST_SSID", "2");
        } else {
            fileAsString = fileAsString.replaceFirst("KURA_IGNORE_BROADCAST_SSID", "0");
        }
        // everything is set and we haven't failed - write the file
        copyFile(fileAsString, tmpOutputFile);
        // move the file if we made it this far
        moveFile(interfaceName);
        return;
    } else {
        s_logger.error("Unsupported security type: {}. It must be WifiSecurity.NONE, WifiSecurity.SECURITY_NONE, WifiSecurity.SECURITY_WEP, WifiSecurity.SECURITY_WPA, or WifiSecurity.SECURITY_WPA2", wifiConfig.getSecurity());
        throw KuraException.internalError("unsupported security type: " + wifiConfig.getSecurity());
    }
}
Also used : WifiCiphers(org.eclipse.kura.net.wifi.WifiCiphers) WifiRadioMode(org.eclipse.kura.net.wifi.WifiRadioMode) File(java.io.File) KuraException(org.eclipse.kura.KuraException) IOException(java.io.IOException)

Example 4 with WifiCiphers

use of org.eclipse.kura.net.wifi.WifiCiphers in project kura by eclipse.

the class HostapdConfigReader method getWifiHostConfig.

private static WifiConfig getWifiHostConfig(String ifaceName) throws KuraException {
    try {
        WifiConfig wifiConfig = new WifiConfig();
        wifiConfig.setMode(WifiMode.MASTER);
        File configFile = new File(HostapdManager.getHostapdConfigFileName(ifaceName));
        Properties hostapdProps = new Properties();
        s_logger.debug("parsing hostapd config file: " + configFile.getAbsolutePath());
        if (configFile.exists()) {
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(configFile);
                hostapdProps.load(fis);
            } finally {
                if (null != fis) {
                    fis.close();
                }
            }
            // remove any quotes around the values
            Enumeration<Object> keys = hostapdProps.keys();
            while (keys.hasMoreElements()) {
                String key = keys.nextElement().toString();
                String val = hostapdProps.getProperty(key);
                if (val.startsWith("\"") && val.endsWith("\"") && val.length() > 1) {
                    hostapdProps.setProperty(key, val.substring(1, val.length() - 1));
                }
            }
            String iface = hostapdProps.getProperty("interface");
            if (ifaceName != null && ifaceName.equals(iface)) {
                String driver = hostapdProps.getProperty("driver");
                String essid = hostapdProps.getProperty("ssid");
                int channel = Integer.parseInt(hostapdProps.getProperty("channel"));
                int ignoreSSID = Integer.parseInt(hostapdProps.getProperty("ignore_broadcast_ssid"));
                // Determine radio mode
                WifiRadioMode wifiRadioMode = null;
                String hwModeStr = hostapdProps.getProperty("hw_mode");
                if ("a".equals(hwModeStr)) {
                    wifiRadioMode = WifiRadioMode.RADIO_MODE_80211a;
                } else if ("b".equals(hwModeStr)) {
                    wifiRadioMode = WifiRadioMode.RADIO_MODE_80211b;
                } else if ("g".equals(hwModeStr)) {
                    wifiRadioMode = WifiRadioMode.RADIO_MODE_80211g;
                    if ("1".equals(hostapdProps.getProperty("ieee80211n"))) {
                        wifiRadioMode = WifiRadioMode.RADIO_MODE_80211nHT20;
                        String ht_capab = hostapdProps.getProperty("ht_capab");
                        if (ht_capab != null) {
                            if (ht_capab.contains("HT40+")) {
                                wifiRadioMode = WifiRadioMode.RADIO_MODE_80211nHT40above;
                            } else if (ht_capab.contains("HT40-")) {
                                wifiRadioMode = WifiRadioMode.RADIO_MODE_80211nHT40below;
                            }
                        }
                    }
                } else {
                    throw KuraException.internalError("malformatted config file, unexpected hw_mode: " + configFile.getAbsolutePath());
                }
                // Determine security and pass
                WifiSecurity security = WifiSecurity.SECURITY_NONE;
                String password = "";
                if (hostapdProps.containsKey("wpa")) {
                    if ("1".equals(hostapdProps.getProperty("wpa"))) {
                        security = WifiSecurity.SECURITY_WPA;
                    } else if ("2".equals(hostapdProps.getProperty("wpa"))) {
                        security = WifiSecurity.SECURITY_WPA2;
                    } else if ("3".equals(hostapdProps.getProperty("wpa"))) {
                        security = WifiSecurity.SECURITY_WPA_WPA2;
                    } else {
                        throw KuraException.internalError("malformatted config file: " + configFile.getAbsolutePath());
                    }
                    if (hostapdProps.containsKey("wpa_passphrase")) {
                        password = hostapdProps.getProperty("wpa_passphrase");
                    } else if (hostapdProps.containsKey("wpa_psk")) {
                        password = hostapdProps.getProperty("wpa_psk");
                    } else {
                        throw KuraException.internalError("malformatted config file, no wpa passphrase: " + configFile.getAbsolutePath());
                    }
                } else if (hostapdProps.containsKey("wep_key0")) {
                    security = WifiSecurity.SECURITY_WEP;
                    password = hostapdProps.getProperty("wep_key0");
                }
                WifiCiphers pairwise = null;
                if (hostapdProps.containsKey("wpa_pairwise")) {
                    if ("TKIP".equals(hostapdProps.getProperty("wpa_pairwise"))) {
                        pairwise = WifiCiphers.TKIP;
                    } else if ("CCMP".equals(hostapdProps.getProperty("wpa_pairwise"))) {
                        pairwise = WifiCiphers.CCMP;
                    } else if ("CCMP TKIP".equals(hostapdProps.getProperty("wpa_pairwise"))) {
                        pairwise = WifiCiphers.CCMP_TKIP;
                    } else {
                        throw KuraException.internalError("malformatted config file: " + configFile.getAbsolutePath());
                    }
                }
                // Populate the config
                wifiConfig.setSSID(essid);
                wifiConfig.setDriver(driver);
                wifiConfig.setChannels(new int[] { channel });
                wifiConfig.setPasskey(password);
                wifiConfig.setSecurity(security);
                wifiConfig.setPairwiseCiphers(pairwise);
                wifiConfig.setRadioMode(wifiRadioMode);
                if (ignoreSSID == 0) {
                    wifiConfig.setIgnoreSSID(false);
                    wifiConfig.setBroadcast(true);
                } else {
                    wifiConfig.setIgnoreSSID(true);
                    wifiConfig.setBroadcast(false);
                }
                // hw mode
                if (wifiRadioMode == WifiRadioMode.RADIO_MODE_80211b) {
                    wifiConfig.setHardwareMode("b");
                } else if (wifiRadioMode == WifiRadioMode.RADIO_MODE_80211g) {
                    wifiConfig.setHardwareMode("g");
                } else if (wifiRadioMode == WifiRadioMode.RADIO_MODE_80211nHT20 || wifiRadioMode == WifiRadioMode.RADIO_MODE_80211nHT40above || wifiRadioMode == WifiRadioMode.RADIO_MODE_80211nHT40below) {
                    // TODO: specify these 'n' modes separately?
                    wifiConfig.setHardwareMode("n");
                }
            }
        } else {
            s_logger.warn("getWifiHostConfig() :: {} file doesn't exist, will generate default wifiConfig", configFile.getName());
            wifiConfig.setSSID("kura_gateway");
            wifiConfig.setDriver("nl80211");
            wifiConfig.setChannels(new int[] { 11 });
            wifiConfig.setPasskey("");
            wifiConfig.setSecurity(WifiSecurity.SECURITY_NONE);
            wifiConfig.setPairwiseCiphers(WifiCiphers.CCMP);
            wifiConfig.setRadioMode(WifiRadioMode.RADIO_MODE_80211b);
            wifiConfig.setIgnoreSSID(false);
            wifiConfig.setBroadcast(true);
            wifiConfig.setHardwareMode("b");
        }
        return wifiConfig;
    } catch (Exception e) {
        s_logger.error("Exception getting WiFi configuration", e);
        throw KuraException.internalError(e);
    }
}
Also used : WifiCiphers(org.eclipse.kura.net.wifi.WifiCiphers) WifiRadioMode(org.eclipse.kura.net.wifi.WifiRadioMode) WifiConfig(org.eclipse.kura.net.wifi.WifiConfig) WifiSecurity(org.eclipse.kura.net.wifi.WifiSecurity) Properties(java.util.Properties) File(java.io.File) FileInputStream(java.io.FileInputStream) KuraException(org.eclipse.kura.KuraException)

Aggregations

WifiCiphers (org.eclipse.kura.net.wifi.WifiCiphers)4 Properties (java.util.Properties)3 WifiSecurity (org.eclipse.kura.net.wifi.WifiSecurity)3 File (java.io.File)2 KuraException (org.eclipse.kura.KuraException)2 WifiBgscan (org.eclipse.kura.net.wifi.WifiBgscan)2 WifiConfig (org.eclipse.kura.net.wifi.WifiConfig)2 WifiRadioMode (org.eclipse.kura.net.wifi.WifiRadioMode)2 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 WifiMode (org.eclipse.kura.net.wifi.WifiMode)1