]>
Commit | Line | Data |
---|---|---|
e9df08ad MT |
1 | #!/bin/bash |
2 | ############################################################################### | |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
5 | # Copyright (C) 2014 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 | ||
1c6a4e30 | 22 | settings_read() { |
2472e0ea | 23 | local file="${1}" |
e9df08ad MT |
24 | assert isset file |
25 | shift | |
26 | ||
2472e0ea MT |
27 | local valid_keys |
28 | local ignore_superfluous_settings="false" | |
29 | ||
30 | local arg | |
31 | while read -r arg; do | |
32 | case "${arg}" in | |
33 | --ignore-superfluous-settings) | |
34 | ignore_superfluous_settings="true" | |
35 | ;; | |
36 | *) | |
37 | list_append valid_keys "${arg}" | |
38 | ;; | |
39 | esac | |
2212045f | 40 | done <<< "$(args "$@")" |
2472e0ea MT |
41 | |
42 | if [ -d "${file}" ]; then | |
43 | error "Not a configuration file: '${file}'" | |
44 | return ${EXIT_ERROR} | |
45 | fi | |
e9df08ad MT |
46 | |
47 | # Exit if the file cannot be read. | |
48 | [ -r "${file}" ] || return ${EXIT_ERROR} | |
49 | ||
50 | local line key val | |
51 | while read -r line; do | |
52 | case "${line}" in | |
53 | *=*) | |
54 | key=$(cli_get_key ${line}) | |
55 | ||
56 | # If valid keys is set, key must be in the list. | |
57 | if [ -n "${valid_keys}" ]; then | |
1ef1c5b4 | 58 | list_match ${key} ${valid_keys} || continue |
e9df08ad MT |
59 | fi |
60 | ||
2212045f | 61 | val=$(cli_get_val "${line}") |
e9df08ad MT |
62 | val=$(settings_strip ${val}) |
63 | ||
64 | # Assign variable. | |
65 | printf -v ${key} "%s" "${val}" | |
66 | ;; | |
67 | *) | |
68 | log DEBUG "Invalid line in configuration file: ${line}" | |
69 | ;; | |
70 | esac | |
71 | done < ${file} | |
72 | } | |
73 | ||
1c6a4e30 | 74 | settings_read_array() { |
e9df08ad MT |
75 | local file=${1} |
76 | assert isset file | |
77 | shift | |
78 | ||
79 | local array=${1} | |
80 | assert isset array | |
81 | shift | |
82 | ||
83 | local valid_keys=$@ | |
84 | ||
85 | # Exit if the file cannot be read. | |
86 | [ -r "${file}" ] || return ${EXIT_ERROR} | |
87 | ||
88 | local line key val | |
89 | while read -r line; do | |
90 | case "${line}" in | |
91 | *=*) | |
92 | key=$(cli_get_key ${line}) | |
93 | ||
94 | # If valid_keys is set, key must be in the list. | |
95 | if [ -n "${valid_keys}" ]; then | |
8c9205b1 | 96 | if ! list_match ${key} ${valid_keys}; then |
e9df08ad MT |
97 | log DEBUG "Ignoring configuration setting: ${key}" |
98 | continue | |
99 | fi | |
100 | fi | |
101 | ||
2212045f | 102 | val=$(cli_get_val "${line}") |
e9df08ad MT |
103 | val=$(settings_strip ${val}) |
104 | ||
105 | # Assign variable. | |
106 | printf -v "${array}["${key}"]" "%s" "${val}" | |
107 | ;; | |
108 | *) | |
109 | log DEBUG "Invalid line in configuration file: ${line}" | |
110 | ;; | |
111 | esac | |
112 | done < ${file} | |
113 | } | |
114 | ||
115 | # Strip leading and trailing "s. | |
1c6a4e30 | 116 | settings_strip() { |
e9df08ad MT |
117 | local var="$@" |
118 | ||
119 | # Do nothing for strings that contain spaces. | |
1ba6a2bb MT |
120 | #if contains_spaces ${var}; then |
121 | # print "${var}" | |
122 | # return ${EXIT_OK} | |
123 | #fi | |
e9df08ad MT |
124 | |
125 | unquote "${var}" | |
126 | } | |
127 | ||
1c6a4e30 | 128 | settings_write() { |
1e6f187e | 129 | local settings_file="${1}" |
e9df08ad MT |
130 | assert isset settings_file |
131 | shift | |
132 | ||
1e6f187e MT |
133 | local check_func |
134 | ||
135 | local arg | |
136 | while read arg; do | |
137 | case "${arg}" in | |
138 | --check=*) | |
139 | check_func="$(cli_get_val "${arg}")" | |
140 | ;; | |
141 | ||
142 | # Stop argument processing when reaching the first | |
143 | # configuration parameter | |
144 | *) | |
145 | break | |
146 | ;; | |
147 | esac | |
148 | shift | |
2212045f | 149 | done <<< "$(args "$@")" |
1e6f187e | 150 | |
e9df08ad | 151 | # Check if all values to be written are sane |
1e6f187e | 152 | if isset check_func && ! settings_check "${check_func}"; then |
e9df08ad MT |
153 | return ${EXIT_ERROR} |
154 | fi | |
155 | ||
7eb800e4 | 156 | log DEBUG "Writing settings file '${settings_file}'" |
e9df08ad MT |
157 | |
158 | mkdir -p $(dirname ${settings_file}) 2>/dev/null | |
159 | > ${settings_file} | |
160 | ||
161 | local param | |
2212045f | 162 | for param in $(list_sort "$@"); do |
e9df08ad MT |
163 | echo "${param}=\"${!param}\"" >> ${settings_file} |
164 | done | |
165 | } | |
166 | ||
1c6a4e30 | 167 | settings_remove() { |
e9df08ad MT |
168 | local settings_file="${1}" |
169 | ||
170 | local abspath="$(readlink -e "${settings_file}")" | |
171 | if [ "${settings_file}" != "${abspath}" ]; then | |
172 | log ERROR "Can only handle absolute paths" | |
173 | return ${EXIT_ERROR} | |
174 | fi | |
175 | ||
8f202217 | 176 | file_delete "${settings_file}" |
e9df08ad MT |
177 | } |
178 | ||
1c6a4e30 | 179 | settings_print() { |
e9df08ad MT |
180 | local param |
181 | ||
2212045f | 182 | for param in $(list_sort "$@"); do |
e9df08ad MT |
183 | printf "%-32s = %s\n" "${param}" "${!param}" |
184 | done | |
185 | } | |
186 | ||
1c6a4e30 | 187 | settings_check() { |
1e6f187e MT |
188 | local check_func="${1}" |
189 | ||
190 | # Execute the check function | |
191 | "${check_func}" | |
192 | local ret="${?}" | |
193 | ||
194 | case "${ret}" in | |
195 | # OK | |
196 | ${EXIT_OK}|${EXIT_TRUE}) | |
197 | log DEBUG "Configuration check succeeded." | |
198 | return ${EXIT_TRUE} | |
199 | ;; | |
200 | ||
201 | # Error | |
202 | ${EXIT_ERROR}|${EXIT_FALSE}) | |
203 | log CRITICAL "Configuration check failed. No settings have been written." | |
204 | return ${EXIT_FALSE} | |
205 | ;; | |
206 | ||
207 | # Command not found | |
208 | ${EXIT_COMMAND_NOT_FOUND}) | |
209 | log CRITICAL "Configuration check function '${check_func}' was not found." | |
210 | return ${EXIT_FALSE} | |
211 | ;; | |
212 | esac | |
213 | ||
214 | log CRITICAL "Unhandled exit code for '${check_func}': ${ret}" | |
215 | return ${EXIT_ERROR} | |
e9df08ad MT |
216 | } |
217 | ||
1c6a4e30 | 218 | settings_set() { |
e9df08ad MT |
219 | while [ $# -gt 0 ]; do |
220 | case "${1}" in | |
221 | *=*) | |
2212045f JS |
222 | local key=$(cli_get_key "${1}") |
223 | local val=$(cli_get_val "${1}") | |
e9df08ad MT |
224 | |
225 | log INFO "Setting configuration option '${key}=${val}'". | |
226 | ||
227 | printf -v ${key} "%s" "${val}" | |
228 | ;; | |
229 | *) | |
230 | warning "Invalid parameter given: ${1}" | |
231 | ;; | |
232 | esac | |
233 | shift | |
234 | done | |
235 | } | |
236 | ||
1c6a4e30 | 237 | network_settings_read() { |
e9df08ad MT |
238 | local options="${NETWORK_SETTINGS_FILE_PARAMS}" |
239 | ||
240 | # If the DEBUG variable has already been set, | |
241 | # don't overwrite it. | |
242 | if [ -n "${DEBUG}" ]; then | |
243 | list_remove options DEBUG | |
244 | fi | |
245 | ||
246 | settings_read "${NETWORK_SETTINGS_FILE}" ${options} | |
247 | } | |
248 | ||
1c6a4e30 | 249 | network_settings_write() { |
e9df08ad | 250 | settings_write "${NETWORK_SETTINGS_FILE}" ${NETWORK_SETTINGS_FILE_PARAMS} |
e9df08ad MT |
251 | } |
252 | ||
cfa738f1 | 253 | network_settings_set() { |
cfa738f1 MT |
254 | # Process any settings that require immediate actin |
255 | while [ $# -gt 0 ]; do | |
ab9e0fd0 MT |
256 | local arg=${1} |
257 | shift | |
258 | ||
259 | case "${arg}" in | |
cfa738f1 | 260 | *=*) |
2212045f JS |
261 | local key=$(cli_get_key "${arg}") |
262 | local val=$(cli_get_val "${arg}") | |
cfa738f1 MT |
263 | |
264 | case "${key}" in | |
c8425c75 MT |
265 | DNS_RANDOMIZE|DNS_SEARCH_DOMAIN|DNS_USE_LOCAL_RESOLVER) |
266 | dns_generate_resolvconf | |
267 | ;; | |
268 | ||
cfa738f1 | 269 | WIRELESS_REGULATORY_DOMAIN) |
ab9e0fd0 MT |
270 | if ! wireless_valid_reg_domain "${val}"; then |
271 | warning "Ignoring invalid wireless regulatory domain: ${val}" | |
272 | continue | |
273 | fi | |
274 | ||
275 | if ! wireless_set_reg_domain "${val}"; then | |
276 | error "Error setting wireless regulatory domain: ${val}" | |
277 | fi | |
cfa738f1 MT |
278 | ;; |
279 | esac | |
280 | ;; | |
281 | esac | |
ab9e0fd0 MT |
282 | |
283 | # Save setting | |
284 | settings_set ${arg} | |
cfa738f1 MT |
285 | done |
286 | ||
287 | return ${EXIT_OK} | |
288 | } | |
289 | ||
1c6a4e30 | 290 | network_settings_print() { |
e9df08ad MT |
291 | settings_print ${NETWORK_SETTINGS_FILE_PARAMS} |
292 | } | |
293 | ||
1c6a4e30 | 294 | network_settings_list() { |
bae37360 MT |
295 | print "${NETWORK_SETTINGS_FILE_PARAMS}" |
296 | } | |
297 | ||
1c6a4e30 | 298 | firewall_settings_read() { |
e9df08ad MT |
299 | settings_read "${FIREWALL_SETTINGS_FILE}" "${FIREWALL_SETTINGS_PARAMS}" |
300 | } | |
301 | ||
1c6a4e30 | 302 | firewall_settings_write() { |
e9df08ad MT |
303 | settings_write "${FIREWALL_SETTINGS_FILE}" "${FIREWALL_SETTINGS_PARAMS}" |
304 | } | |
305 | ||
1c6a4e30 | 306 | firewall_settings_print() { |
e9df08ad MT |
307 | settings_print "${FIREWALL_SETTINGS_PARAMS}" |
308 | } |