2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2017 IPFire Network Development Team #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
22 WIRELESS_NETWORK_CONFIG_SETTINGS
="ENCRYPTION_MODE PRIORITY PSK SSID"
24 cli_wireless_network
() {
27 wireless_network_new
"${@:2}"
30 wireless_network_destroy
"${@:2}"
37 if ! wireless_network_exists
"${ssid}"; then
38 error
"No such wireless network: ${ssid}"
43 encryption_mode|pre_shared_key|priority
)
44 wireless_network_
${key} "${ssid}" "$@"
47 wireless_network_show
"${ssid}"
51 error
"Unrecognized argument: ${key}"
59 # This function writes all values to a via ${ssid} specificated wireless network configuration file
60 wireless_network_write_config
() {
65 local ssid_hash
="$(wireless_network_hash "${ssid}")"
66 assert isset ssid_hash
68 if ! wireless_network_exists
"${ssid}"; then
69 log ERROR
"No such wireless network: '${ssid}'"
73 local path
="${NETWORK_WIRELESS_NETWORKS_DIR}/${ssid_hash}/settings"
75 if ! settings_write
"${path}" ${WIRELESS_NETWORK_CONFIG_SETTINGS}; then
76 log ERROR
"Could not write configuration settings for wireless network ${ssid}"
80 # When we get here the writing of the config file was successful
84 # This funtion writes the value for one key to a via ${ssid} specificated
85 # wireless network configuration file
86 wireless_network_write_config_key
() {
95 if ! wireless_network_exists
"${ssid}"; then
96 log ERROR
"No such wireless network: ${ssid}"
100 log DEBUG
"Set '${key}' to new value '${value}' in wireless network '${ssid}'"
102 local ${WIRELESS_NETWORK_CONFIG_SETTINGS}
104 # Read the config settings
105 if ! wireless_network_read_config
"${ssid}"; then
109 # Set the key to a new value
110 assign
"${key}" "${value}"
112 if ! wireless_network_write_config
"${ssid}"; then
119 # Reads one or more keys out of a settings file or all if no key is provided.
120 wireless_network_read_config
() {
126 local ssid_hash
="$(wireless_network_hash "${ssid}")"
127 assert isset ssid_hash
129 if ! wireless_network_exists
"${ssid}"; then
130 log ERROR
"No such wireless network : ${ssid}"
135 if [ $# -eq 0 ] && [ -n "${WIRELESS_NETWORK_CONFIG_SETTINGS}" ]; then
136 list_append args
${WIRELESS_NETWORK_CONFIG_SETTINGS}
138 list_append args
"$@"
141 local path
="${NETWORK_WIRELESS_NETWORKS_DIR}/${ssid_hash}/settings"
143 if ! settings_read
"${path}" ${args}; then
144 log ERROR
"Could not read settings for wireless network ${ssid}"
149 # This function checks if a wireless network exists
150 # Returns True when yes and false when not
151 wireless_network_exists
() {
155 local ssid_hash
="$(wireless_network_hash "${ssid}")"
156 assert isset ssid_hash
158 local path
="${NETWORK_WIRELESS_NETWORKS_DIR}/${ssid_hash}"
160 # We cannot use wireless_network_read_config here beacuse we would end in a loop
163 local path_settings
="${NETWORK_WIRELESS_NETWORKS_DIR}/${ssid_hash}/settings"
165 if ! settings_read
"${path_settings}" SSID
; then
171 if [ -d "${path}" ] && [[ "${ssid}" = "${SSID}" ]]; then
178 wireless_network_hash
() {
183 local hash=$
(echo -n "${string}" |
md5sum )
186 local path
="${NETWORK_WIRELESS_NETWORKS_DIR}/*${hash}"
188 if [ -d "${path}" ]; then
191 local normalized
=$
(normalize
"${string}")
192 normalized
=${normalized%-}
193 echo "${normalized}-${hash}"
197 wireless_network_new
() {
198 if [ $# -gt 1 ]; then
199 error
"Too many arguments"
204 if ! isset ssid
; then
205 error
"Please provide a SSID"
209 local ssid_hash
="$(wireless_network_hash "${ssid}")"
210 assert isset ssid_hash
212 # Check for duplicates
213 if wireless_network_exists
"${ssid}"; then
214 error
"The wireless network ${ssid} already exists"
218 log DEBUG
"Creating wireless network '${ssid}'"
220 if ! mkdir
-p "${NETWORK_WIRELESS_NETWORKS_DIR}/${ssid_hash}"; then
221 log ERROR
"Could not create config directory for wireless network ${ssid}"
225 # When the ssid is not set in the settings file we cannot write it because wireless_network_exists fails
226 echo "SSID=\"${ssid}\"" >>"${NETWORK_WIRELESS_NETWORKS_DIR}/${ssid_hash}/settings"
228 local ${WIRELESS_NETWORK_CONFIG_SETTINGS}
229 ENCRYPTION_MODE
="${WIRELESS_DEFAULT_ENCRYPTION_MODE}"
233 if ! wireless_network_write_config
"${ssid}"; then
234 log ERROR
"Could not write new config file"
239 # Deletes a wireless network
240 wireless_network_destroy
() {
243 if ! wireless_network_exists
"${ssid}"; then
244 error
"No such wireless network: ${ssid}"
248 local ssid_hash
="$(wireless_network_hash "${ssid}")"
249 assert isset ssid_hash
251 if ! rm -rf "${NETWORK_WIRELESS_NETWORKS_DIR}/${ssid_hash}"; then
252 error
"Could not delete the wireless network"
256 log INFO
"Successfully destroyed wireless network ${ssid}"
260 wireless_network_encryption_mode
() {
261 if [ ! $# -eq 2 ]; then
262 log ERROR
"Not enough arguments"
268 if ! isoneof mode
${WIRELESS_VALID_ENCRYPTION_MODES}; then
269 log ERROR
"Encryption mode '${mode}' is invalid"
273 local ${WIRELESS_NETWORK_CONFIG_SETTINGS}
274 if ! wireless_network_read_config
"${ssid}"; then
275 error
"Could not read configuration for ${ssid}"
279 # Validate the PSK when changing mode and reset if needed
280 if isset PSK
&& [ "${mode}" != "NONE" ] && \
281 ! wireless_pre_shared_key_is_valid
"${mode}" "${PSK}"; then
282 log WARNING
"The configured pre-shared-key is incompatible with this encryption mode and has been reset"
286 # Save new encryption mode
287 ENCRYPTION_MODE
="${mode}"
289 if ! wireless_network_write_config
"${ssid}"; then
290 log ERROR
"Could not write configuration settings"
295 wireless_network_pre_shared_key
() {
296 if [ ! $# -eq 2 ]; then
297 log ERROR
"Not enough arguments"
303 local ${WIRELESS_NETWORK_CONFIG_SETTINGS}
304 if ! wireless_network_read_config
"${ssid}"; then
305 error
"Could not read configuration for ${ssid}"
309 # Validate the key if encryption mode is known
310 if isset ENCRYPTION_MODE
&& [ "${ENCRYPTION_MODE}" != "NONE" ]; then
311 if ! wireless_pre_share_key_is_valid
"${ENCRYPTION_MODE}" "${psk}"; then
312 error
"The pre-shared-key is invalid for this wireless network: ${psk}"
317 if ! wireless_network_write_config_key
"${ssid}" "PSK" "${psk}"; then
318 log ERROR
"Could not write configuration settings"
323 wireless_network_priority
() {
324 if [ ! $# -eq 2 ]; then
325 log ERROR
"Not enough arguments"
331 if ! isinteger priority
&& [ ! ${priority} -ge 0 ]; then
332 log ERROR
"The priority must be an integer greater or eqal zero"
336 if ! wireless_network_write_config_key
"${ssid}" "PRIORITY" "${priority}"; then
337 log ERROR
"Could not write configuration settings"