#!/bin/bash ############################################################################### # # # IPFire.org - A linux based firewall # # Copyright (C) 2012 IPFire Network Development Team # # # # This program is free software: you can redistribute it and/or modify # # it under the terms of the GNU General Public License as published by # # the Free Software Foundation, either version 3 of the License, or # # (at your option) any later version. # # # # This program is distributed in the hope that it will be useful, # # but WITHOUT ANY WARRANTY; without even the implied warranty of # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # # GNU General Public License for more details. # # # # You should have received a copy of the GNU General Public License # # along with this program. If not, see . # # # ############################################################################### WPA_SUPPLICANT_SOCKET_DIR="${RUN_DIR}/wpa_supplicant/ctrl" function wpa_supplicant_config_write() { local device=${1} assert isset device local file=${2} assert isset file shift 2 local ap_scan=1 country_code mode key ssid local arg for arg in "$@"; do case "${arg}" in --ap-scan=*) ap_scan=$(cli_get_val ${arg}) ;; --country-code=*) country_code=$(cli_get_val ${arg}) ;; --mode=*) mode=$(cli_get_val ${arg}) # Empty signals no encryption. isset mode || mode="NONE" ;; --ssid=*) ssid=$(cli_get_val ${arg}) ;; --key=*) key=$(cli_get_val ${arg}) ;; *) error "Unrecognized argument: ${arg}" return ${EXIT_ERROR} ;; esac done assert isinteger ap_scan assert isset mode local auth_alg key_mgmt proto ssid psk wep_key0 wep_tx_keyidx case "${mode}" in # Normal WPA. WPA-PSK) auth_alg="OPEN" key_mgmt="WPA-PSK" proto="WPA" pairwise="CCMP TKIP" group="CCMP TKIP WEP104 WEP40" ;; # WPA with stronger algorithms. WPA-PSK-SHA256) auth_alg="OPEN" key_mgmt="WPA-PSK-SHA256" proto="WPA" pairwise="CCMP TKIP" group="CCMP TKIP WEP104 WEP40" ;; # Normal WPA2 (802.11i). WPA2-PSK) auth_alg="OPEN" key_mgmt="WPA-PSK" proto="RSN" pairwise="CCMP TKIP" group="CCMP TKIP WEP104 WEP40" ;; # WPA2 with stronger algorithms. WPA2-PSK-SHA256) auth_alg="OPEN" key_mgmt="WPA-PSK-SHA256" proto="RSN" pairwise="CCMP TKIP" group="CCMP TKIP WEP104 WEP40" ;; # WEP. WEP) auth_alg="SHARED" wep_key0="${key}" wep_tx_keyidx="0" # Reset PSK. psk="" ;; # IEEE 802.1X 8021X) key_mgmt="IEEE8021X" ;; # No encryption. DANGEROUS! NONE) auth_alg="OPEN" key_mgmt="NONE" ;; *) log ERROR "Unknown mode: ${mode}" return ${EXIT_ERROR} ;; esac local config_dir=$(dirname ${file}) mkdir -p ${config_dir} 2>/dev/null config_header "WPA supplicant configuration file" > ${file} # AP scanning/selection print "ap_scan=${ap_scan}" >> ${file} # Set country code, if known. if isset country_code; then print "country=\"${country_code}\"" >> ${file} fi # Set control socket directory. print "ctrl_interface=${WPA_SUPPLICANT_SOCKET_DIR}" >> ${file} ( print # Network section print "network={" if isset auth_alg; then print " auth_alg=${auth_alg}" fi if isset key_mgmt; then print " key_mgmt=${key_mgmt}" fi if isset proto; then print " proto=${proto}" fi if isset ssid; then print " ssid=${ssid}" fi if isset key; then print " psk=\"${key}\"" fi if isset wep_key0; then print " wep_key0=\"${wep_key0}\"" fi if isset wep_tx_keyidx; then print " wep_tx_keyidx=${wep_tx_keyidx}" fi print "}" ) >> ${file} return ${EXIT_OK} } function wpa_supplicant_config_dir() { local device=${1} assert isset device echo "${RUN_DIR}/wpa_supplicant/${device}" } function wpa_supplicant_start() { local device=${1} assert isset device service_start "wpa_supplicant@${device}.service" } function wpa_supplicant_stop() { local device=${1} assert isset device service_stop "wpa_supplicant@${device}.service" } function wpa_supplicant_client() { local device=${1} assert isset device shift local cmd="$@" assert isset cmd # Run the command and return the output. cmd wpa_cli -p${WPA_SUPPLICANT_SOCKET_DIR} -i${device} ${cmd} } function wpa_cli_status() { local device=${1} assert isset device wpa_supplicant_client ${device} status verbose } function wpa_cli_status_get() { local device=${1} assert isset device local arg=${2} assert isset arg local line key while read -r line; do key=$(cli_get_key ${line}) if [ "${key}" = "${arg}" ]; then cli_get_val "${line}" return ${EXIT_OK} fi done <<< "$(wpa_cli_status ${device})" return ${EXIT_ERROR} } function wpa_cli_bss() { local device=${1} assert isset device local bss=${2} assert isset bss wpa_supplicant_client ${device} bss ${bss} } function wpa_cli_bss_get() { local device=${1} assert isset device local bss=${2} assert isset bss local arg=${3} assert isset arg local line key while read -r line; do key=$(cli_get_key ${line}) if [ "${key}" = "${arg}" ]; then cli_get_val "${line}" return ${EXIT_OK} fi done <<< "$(wpa_cli_bss ${device} ${bss})" return ${EXIT_ERROR} } function wpa_cli_bss_get_frequency() { local device=${1} assert isset device local bssid=${2} assert isset bssid wpa_cli_bss_get ${device} ${bssid} freq } function wpa_cli_bss_get_noise() { local device=${1} assert isset device local bssid=${2} assert isset bssid wpa_cli_bss_get ${device} ${bssid} noise } function wpa_cli_bss_get_quality() { local device=${1} assert isset device local bssid=${2} assert isset bssid wpa_cli_bss_get ${device} ${bssid} qual } function wpa_cli_bss_get_flags() { local device=${1} assert isset device local bssid=${2} assert isset bssid wpa_cli_bss_get ${device} ${bssid} flags }