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 KEY PRIORITY SSID"
24 WIRELESS_NETWORKS_VALID_ENCRYPTION_MODES
="WPA2-PSK"
26 cli_wireless_network
() {
27 if wireless_network_exists
"${1}"; then
34 encryption_mode|key|priority
)
35 wireless_network_
${key} "${ssid}" "$@"
38 wireless_network_show
"${ssid}"
42 error
"Unrecognized argument: ${key}"
52 wireless_network_new
"$@"
55 wireless_network_destroy
"$@"
58 if [ -n "${action}" ]; then
59 error
"Unrecognized argument: '${action}'"
67 # This function writes all values to a via ${ssid} specificated wireless network configuration file
68 wireless_network_write_config
() {
73 local ssid_hash
="$(wireless_network_hash "${ssid}")"
74 assert isset ssid_hash
76 if ! wireless_network_exists
"${ssid}"; then
77 log ERROR
"No such wireless network: '${ssid}'"
81 local path
="${NETWORK_WIRELESS_NETWORKS_DIR}/${ssid_hash}/settings"
83 if ! settings_write
"${path}" ${WIRELESS_NETWORK_CONFIG_SETTINGS}; then
84 log ERROR
"Could not write configuration settings for wireless network ${ssid}"
88 # When we get here the writing of the config file was successful
92 # This funtion writes the value for one key to a via ${ssid} specificated
93 # wireless network configuration file
94 wireless_network_write_config_key
() {
103 if ! wireless_network_exists
"${ssid}"; then
104 log ERROR
"No such wireless network: ${ssid}"
108 log DEBUG
"Set '${key}' to new value '${value}' in wireless network '${ssid}'"
110 local ${WIRELESS_NETWORK_CONFIG_SETTINGS}
112 # Read the config settings
113 if ! wireless_network_read_config
"${ssid}"; then
117 # Set the key to a new value
118 assign
"${key}" "${value}"
120 if ! wireless_network_write_config
"${ssid}"; then
127 # Reads one or more keys out of a settings file or all if no key is provided.
128 wireless_network_read_config
() {
134 local ssid_hash
="$(wireless_network_hash "${ssid}")"
135 assert isset ssid_hash
137 if ! wireless_network_exists
"${ssid}"; then
138 log ERROR
"No such wireless network : ${ssid}"
143 if [ $# -eq 0 ] && [ -n "${WIRELESS_NETWORK_CONFIG_SETTINGS}" ]; then
144 list_append args
${WIRELESS_NETWORK_CONFIG_SETTINGS}
146 list_append args
"$@"
149 local path
="${NETWORK_WIRELESS_NETWORKS_DIR}/${ssid_hash}/settings"
151 if ! settings_read
"${path}" ${args}; then
152 log ERROR
"Could not read settings for wireless network ${ssid}"
157 # This function checks if a wireless network exists
158 # Returns True when yes and false when not
159 wireless_network_exists
() {
163 local ssid_hash
="$(wireless_network_hash "${ssid}")"
164 assert isset ssid_hash
166 local path
="${NETWORK_WIRELESS_NETWORKS_DIR}/${ssid_hash}"
168 # We cannot use wireless_network_read_config here beacuse we would end in a loop
171 local path_settings
="${NETWORK_WIRELESS_NETWORKS_DIR}/${ssid_hash}/settings"
173 if ! settings_read
"${path_settings}" SSID
; then
179 if [ -d "${path}" ] && [[ "${ssid}" = "${SSID}" ]]; then
186 wireless_network_hash
() {
191 local hash=$
(echo -n "${string}" |
md5sum )
194 local path
="${NETWORK_WIRELESS_NETWORKS_DIR}/*${hash}"
196 if [ -d "${path}" ]; then
199 local normalized
=$
(normalize
"${string}")
200 normalized
=${normalized%-}
201 echo "${normalized}-${hash}"
205 wireless_network_new
() {
206 if [ $# -gt 1 ]; then
207 error
"Too many arguments"
212 if ! isset ssid
; then
213 error
"Please provide a SSID"
217 local ssid_hash
="$(wireless_network_hash "${ssid}")"
218 assert isset ssid_hash
220 # Check for duplicates
221 if wireless_network_exists
"${ssid}"; then
222 error
"The wireless network ${ssid} already exists"
226 log DEBUG
"Creating wireless network '${ssid}'"
228 if ! mkdir
-p "${NETWORK_WIRELESS_NETWORKS_DIR}/${ssid_hash}"; then
229 log ERROR
"Could not create config directory for wireless network ${ssid}"
233 # When the ssid is not set in the settings file we cannot write it because wireless_network_exists fails
234 echo "SSID=\"${ssid}\"" >>"${NETWORK_WIRELESS_NETWORKS_DIR}/${ssid_hash}/settings"
236 local ${WIRELESS_NETWORK_CONFIG_SETTINGS}
240 if ! wireless_network_write_config
"${ssid}"; then
241 log ERROR
"Could not write new config file"
246 # Function that deletes based on the passed parameters
247 # one ore more wireless networks
248 wireless_network_destroy
() {
251 local ssid_hash
="$(wireless_network_hash "${ssid}")"
252 assert isset ssid_hash
254 if ! wireless_network_exists
"${ssid}"; then
255 log ERROR
"The wireless network ${ssid} does not exist."
259 log DEBUG
"Deleting wireless network ${ssid}"
261 if ! rm -rf "${NETWORK_WIRELESS_NETWORKS_DIR}/${ssid_hash}"; then
262 log ERROR
"Deleting the wireless network ${ssid} was not sucessful"
268 wireless_network_encryption_mode
() {
269 if [ ! $# -eq 2 ]; then
270 log ERROR
"Not enough arguments"
276 if ! isoneof mode
${WIRELESS_NETWORKS_VALID_ENCRYPTION_MODES}; then
277 log ERROR
"Encryption mode '${mode}' is invalid"
281 if ! wireless_network_write_config_key
"${ssid}" "ENCRYPTION_MODE" ${mode^^}; then
282 log ERROR
"Could not write configuration settings"
287 wireless_network_key
() {
288 if [ ! $# -eq 2 ]; then
289 log ERROR
"Not enough arguments"
295 if ! wireless_network_write_config_key
"${ssid}" "KEY" "${key}"; then
296 log ERROR
"Could not write configuration settings"
301 wireless_network_priority
() {
302 if [ ! $# -eq 2 ]; then
303 log ERROR
"Not enough arguments"
309 if ! isinteger priority
&& [ ! ${priority} -ge 0 ]; then
310 log ERROR
"The priority must be an integer greater or eqal zero"
314 if ! wireless_network_write_config_key
"${ssid}" "PRIORITY" "${priority}"; then
315 log ERROR
"Could not write configuration settings"