]>
Commit | Line | Data |
---|---|---|
49958b8c MT |
1 | #!/bin/bash |
2 | ############################################################################### | |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
5 | # Copyright (C) 2017 IPFire Network Development Team # | |
6 | # # | |
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. # | |
11 | # # | |
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. # | |
16 | # # | |
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/>. # | |
19 | # # | |
20 | ############################################################################### | |
21 | ||
364da6f4 | 22 | WIRELESS_NETWORK_CONFIG_SETTINGS="ENCRYPTION_MODE PRIORITY PSK SSID" |
49958b8c | 23 | |
49958b8c | 24 | cli_wireless_network() { |
479b2273 MT |
25 | case "${1}" in |
26 | new) | |
27 | wireless_network_new "${@:2}" | |
28 | ;; | |
29 | destroy) | |
30 | wireless_network_destroy "${@:2}" | |
31 | ;; | |
32 | *) | |
33 | local ssid="${1}" | |
34 | local key="${2//-/_}" | |
549c5b97 | 35 | shift 2 |
49958b8c | 36 | |
479b2273 MT |
37 | if ! wireless_network_exists "${ssid}"; then |
38 | error "No such wireless network: ${ssid}" | |
39 | return ${EXIT_ERROR} | |
40 | fi | |
49958b8c | 41 | |
479b2273 | 42 | case "${key}" in |
364da6f4 | 43 | encryption_mode|pre_shared_key|priority) |
479b2273 MT |
44 | wireless_network_${key} "${ssid}" "$@" |
45 | ;; | |
46 | show) | |
47 | wireless_network_show "${ssid}" | |
48 | exit $? | |
49 | ;; | |
50 | *) | |
51 | error "Unrecognized argument: ${key}" | |
52 | exit ${EXIT_ERROR} | |
53 | ;; | |
54 | esac | |
55 | ;; | |
56 | esac | |
49958b8c MT |
57 | } |
58 | ||
59 | # This function writes all values to a via ${ssid} specificated wireless network configuration file | |
60 | wireless_network_write_config() { | |
61 | assert [ $# -ge 1 ] | |
62 | ||
63 | local ssid="${1}" | |
64 | ||
65 | local ssid_hash="$(wireless_network_hash "${ssid}")" | |
66 | assert isset ssid_hash | |
67 | ||
68 | if ! wireless_network_exists "${ssid}"; then | |
69 | log ERROR "No such wireless network: '${ssid}'" | |
70 | return ${EXIT_ERROR} | |
71 | fi | |
72 | ||
73 | local path="${NETWORK_WIRELESS_NETWORKS_DIR}/${ssid_hash}/settings" | |
74 | ||
75 | if ! settings_write "${path}" ${WIRELESS_NETWORK_CONFIG_SETTINGS}; then | |
76 | log ERROR "Could not write configuration settings for wireless network ${ssid}" | |
77 | return ${EXIT_ERROR} | |
78 | fi | |
79 | ||
80 | # When we get here the writing of the config file was successful | |
81 | return ${EXIT_OK} | |
82 | } | |
83 | ||
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() { | |
87 | assert [ $# -ge 3 ] | |
88 | ||
89 | local ssid="${1}" | |
90 | local key="${2}" | |
91 | shift 2 | |
92 | ||
93 | local value="$@" | |
94 | ||
95 | if ! wireless_network_exists "${ssid}"; then | |
96 | log ERROR "No such wireless network: ${ssid}" | |
97 | return ${EXIT_ERROR} | |
98 | fi | |
99 | ||
100 | log DEBUG "Set '${key}' to new value '${value}' in wireless network '${ssid}'" | |
101 | ||
102 | local ${WIRELESS_NETWORK_CONFIG_SETTINGS} | |
103 | ||
104 | # Read the config settings | |
105 | if ! wireless_network_read_config "${ssid}"; then | |
106 | return ${EXIT_ERROR} | |
107 | fi | |
108 | ||
109 | # Set the key to a new value | |
110 | assign "${key}" "${value}" | |
111 | ||
112 | if ! wireless_network_write_config "${ssid}"; then | |
113 | return ${EXIT_ERROR} | |
114 | fi | |
115 | ||
116 | return ${EXIT_OK} | |
117 | } | |
118 | ||
119 | # Reads one or more keys out of a settings file or all if no key is provided. | |
120 | wireless_network_read_config() { | |
121 | assert [ $# -ge 1 ] | |
122 | ||
123 | local ssid="${1}" | |
124 | shift 1 | |
125 | ||
126 | local ssid_hash="$(wireless_network_hash "${ssid}")" | |
127 | assert isset ssid_hash | |
128 | ||
129 | if ! wireless_network_exists "${ssid}"; then | |
130 | log ERROR "No such wireless network : ${ssid}" | |
131 | return ${EXIT_ERROR} | |
132 | fi | |
133 | ||
134 | local args | |
135 | if [ $# -eq 0 ] && [ -n "${WIRELESS_NETWORK_CONFIG_SETTINGS}" ]; then | |
136 | list_append args ${WIRELESS_NETWORK_CONFIG_SETTINGS} | |
137 | else | |
138 | list_append args "$@" | |
139 | fi | |
140 | ||
141 | local path="${NETWORK_WIRELESS_NETWORKS_DIR}/${ssid_hash}/settings" | |
142 | ||
143 | if ! settings_read "${path}" ${args}; then | |
144 | log ERROR "Could not read settings for wireless network ${ssid}" | |
145 | return ${EXIT_ERROR} | |
146 | fi | |
147 | } | |
148 | ||
149 | # This function checks if a wireless network exists | |
150 | # Returns True when yes and false when not | |
151 | wireless_network_exists() { | |
152 | assert [ $# -eq 1 ] | |
153 | ||
154 | local ssid="${1}" | |
155 | local ssid_hash="$(wireless_network_hash "${ssid}")" | |
156 | assert isset ssid_hash | |
157 | ||
158 | local path="${NETWORK_WIRELESS_NETWORKS_DIR}/${ssid_hash}" | |
159 | ||
160 | # We cannot use wireless_network_read_config here beacuse we would end in a loop | |
161 | local SSID | |
162 | ||
163 | local path_settings="${NETWORK_WIRELESS_NETWORKS_DIR}/${ssid_hash}/settings" | |
164 | ||
165 | if ! settings_read "${path_settings}" SSID; then | |
166 | return ${EXIT_FALSE} | |
167 | fi | |
168 | ||
169 | assert isset SSID | |
170 | ||
171 | if [ -d "${path}" ] && [[ "${ssid}" = "${SSID}" ]]; then | |
172 | return ${EXIT_TRUE} | |
173 | else | |
174 | return ${EXIT_FALSE} | |
175 | fi | |
176 | } | |
177 | ||
178 | wireless_network_hash() { | |
179 | assert [ $# -eq 1 ] | |
180 | ||
181 | local string="${1}" | |
182 | ||
183 | local hash=$(echo -n "${string}" | md5sum ) | |
184 | hash=${hash%% -} | |
185 | ||
186 | local path="${NETWORK_WIRELESS_NETWORKS_DIR}/*${hash}" | |
187 | ||
188 | if [ -d "${path}" ]; then | |
189 | basename "${path}" | |
190 | else | |
191 | local normalized=$(normalize "${string}") | |
192 | normalized=${normalized%-} | |
193 | echo "${normalized}-${hash}" | |
194 | fi | |
195 | } | |
196 | ||
197 | wireless_network_new() { | |
198 | if [ $# -gt 1 ]; then | |
199 | error "Too many arguments" | |
200 | return ${EXIT_ERROR} | |
201 | fi | |
202 | ||
203 | local ssid="${1}" | |
204 | if ! isset ssid; then | |
205 | error "Please provide a SSID" | |
206 | return ${EXIT_ERROR} | |
207 | fi | |
208 | ||
209 | local ssid_hash="$(wireless_network_hash "${ssid}")" | |
210 | assert isset ssid_hash | |
211 | ||
212 | # Check for duplicates | |
213 | if wireless_network_exists "${ssid}"; then | |
214 | error "The wireless network ${ssid} already exists" | |
215 | return ${EXIT_ERROR} | |
216 | fi | |
217 | ||
218 | log DEBUG "Creating wireless network '${ssid}'" | |
219 | ||
220 | if ! mkdir -p "${NETWORK_WIRELESS_NETWORKS_DIR}/${ssid_hash}"; then | |
221 | log ERROR "Could not create config directory for wireless network ${ssid}" | |
222 | return ${EXIT_ERROR} | |
223 | fi | |
224 | ||
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" | |
227 | ||
228 | local ${WIRELESS_NETWORK_CONFIG_SETTINGS} | |
1c9e2fa8 | 229 | ENCRYPTION_MODE="${WIRELESS_DEFAULT_ENCRYPTION_MODE}" |
49958b8c MT |
230 | SSID="${ssid}" |
231 | PRIORITY=500 | |
232 | ||
233 | if ! wireless_network_write_config "${ssid}"; then | |
234 | log ERROR "Could not write new config file" | |
235 | return ${EXIT_ERROR} | |
236 | fi | |
237 | } | |
238 | ||
4b08d574 | 239 | # Deletes a wireless network |
49958b8c | 240 | wireless_network_destroy() { |
4b08d574 MT |
241 | local ssid="${1}" |
242 | ||
243 | if ! wireless_network_exists "${ssid}"; then | |
244 | error "No such wireless network: ${ssid}" | |
245 | return ${EXIT_ERROR} | |
246 | fi | |
247 | ||
248 | local ssid_hash="$(wireless_network_hash "${ssid}")" | |
249 | assert isset ssid_hash | |
250 | ||
251 | if ! rm -rf "${NETWORK_WIRELESS_NETWORKS_DIR}/${ssid_hash}"; then | |
252 | error "Could not delete the wireless network" | |
253 | return ${EXIT_ERROR} | |
254 | fi | |
255 | ||
256 | log INFO "Successfully destroyed wireless network ${ssid}" | |
257 | return ${EXIT_OK} | |
49958b8c MT |
258 | } |
259 | ||
260 | wireless_network_encryption_mode() { | |
261 | if [ ! $# -eq 2 ]; then | |
262 | log ERROR "Not enough arguments" | |
263 | return ${EXIT_ERROR} | |
264 | fi | |
265 | local ssid="${1}" | |
266 | local mode="${2}" | |
267 | ||
1c9e2fa8 | 268 | if ! isoneof mode ${WIRELESS_VALID_ENCRYPTION_MODES}; then |
49958b8c MT |
269 | log ERROR "Encryption mode '${mode}' is invalid" |
270 | return ${EXIT_ERROR} | |
271 | fi | |
272 | ||
549c5b97 MT |
273 | local ${WIRELESS_NETWORK_CONFIG_SETTINGS} |
274 | if ! wireless_network_read_config "${ssid}"; then | |
275 | error "Could not read configuration for ${ssid}" | |
276 | return ${EXIT_ERROR} | |
277 | fi | |
278 | ||
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" | |
283 | PSK="" | |
284 | fi | |
285 | ||
286 | # Save new encryption mode | |
287 | ENCRYPTION_MODE="${mode}" | |
288 | ||
289 | if ! wireless_network_write_config "${ssid}"; then | |
49958b8c MT |
290 | log ERROR "Could not write configuration settings" |
291 | return ${EXIT_ERROR} | |
292 | fi | |
293 | } | |
294 | ||
364da6f4 | 295 | wireless_network_pre_shared_key() { |
49958b8c MT |
296 | if [ ! $# -eq 2 ]; then |
297 | log ERROR "Not enough arguments" | |
298 | return ${EXIT_ERROR} | |
299 | fi | |
300 | local ssid="${1}" | |
364da6f4 | 301 | local psk="${2}" |
49958b8c | 302 | |
549c5b97 MT |
303 | local ${WIRELESS_NETWORK_CONFIG_SETTINGS} |
304 | if ! wireless_network_read_config "${ssid}"; then | |
305 | error "Could not read configuration for ${ssid}" | |
306 | return ${EXIT_ERROR} | |
307 | fi | |
308 | ||
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}" | |
313 | return ${EXIT_ERROR} | |
314 | fi | |
315 | fi | |
316 | ||
364da6f4 | 317 | if ! wireless_network_write_config_key "${ssid}" "PSK" "${psk}"; then |
49958b8c MT |
318 | log ERROR "Could not write configuration settings" |
319 | return ${EXIT_ERROR} | |
320 | fi | |
321 | } | |
322 | ||
323 | wireless_network_priority() { | |
324 | if [ ! $# -eq 2 ]; then | |
325 | log ERROR "Not enough arguments" | |
326 | return ${EXIT_ERROR} | |
327 | fi | |
328 | local ssid="${1}" | |
329 | local priority=${2} | |
330 | ||
331 | if ! isinteger priority && [ ! ${priority} -ge 0 ]; then | |
332 | log ERROR "The priority must be an integer greater or eqal zero" | |
333 | return ${EXIT_ERROR} | |
334 | fi | |
335 | ||
336 | if ! wireless_network_write_config_key "${ssid}" "PRIORITY" "${priority}"; then | |
337 | log ERROR "Could not write configuration settings" | |
338 | return ${EXIT_ERROR} | |
339 | fi | |
340 | } |