]>
Commit | Line | Data |
---|---|---|
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 | ||
22 | settings_read() { | |
23 | local file="${1}" | |
24 | assert isset file | |
25 | shift | |
26 | ||
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 | |
40 | done <<< "$(args "$@")" | |
41 | ||
42 | if [ -d "${file}" ]; then | |
43 | error "Not a configuration file: '${file}'" | |
44 | return ${EXIT_ERROR} | |
45 | fi | |
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 | |
58 | list_match ${key} ${valid_keys} || continue | |
59 | fi | |
60 | ||
61 | val=$(cli_get_val "${line}") | |
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 | ||
74 | settings_read_array() { | |
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 | |
96 | if ! list_match ${key} ${valid_keys}; then | |
97 | log DEBUG "Ignoring configuration setting: ${key}" | |
98 | continue | |
99 | fi | |
100 | fi | |
101 | ||
102 | val=$(cli_get_val "${line}") | |
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. | |
116 | settings_strip() { | |
117 | local var="$@" | |
118 | ||
119 | # Do nothing for strings that contain spaces. | |
120 | #if contains_spaces ${var}; then | |
121 | # print "${var}" | |
122 | # return ${EXIT_OK} | |
123 | #fi | |
124 | ||
125 | unquote "${var}" | |
126 | } | |
127 | ||
128 | settings_write() { | |
129 | local settings_file="${1}" | |
130 | assert isset settings_file | |
131 | shift | |
132 | ||
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 | |
149 | done <<< "$(args "$@")" | |
150 | ||
151 | # Check if all values to be written are sane | |
152 | if isset check_func && ! settings_check "${check_func}"; then | |
153 | return ${EXIT_ERROR} | |
154 | fi | |
155 | ||
156 | if ! make_parent_directory "${settings_file}"; then | |
157 | return ${EXIT_ERROR} | |
158 | fi | |
159 | ||
160 | log DEBUG "Writing settings file '${settings_file}'" | |
161 | ||
162 | local param | |
163 | for param in $(list_sort "$@"); do | |
164 | echo "${param}=\"${!param}\"" | |
165 | done > ${settings_file} | |
166 | } | |
167 | ||
168 | settings_remove() { | |
169 | local settings_file="${1}" | |
170 | ||
171 | local abspath="$(readlink -e "${settings_file}")" | |
172 | if [ "${settings_file}" != "${abspath}" ]; then | |
173 | log ERROR "Can only handle absolute paths" | |
174 | return ${EXIT_ERROR} | |
175 | fi | |
176 | ||
177 | file_delete "${settings_file}" | |
178 | } | |
179 | ||
180 | settings_print() { | |
181 | local param | |
182 | ||
183 | for param in $(list_sort "$@"); do | |
184 | printf "%-32s = %s\n" "${param}" "${!param}" | |
185 | done | |
186 | } | |
187 | ||
188 | settings_check() { | |
189 | local check_func="${1}" | |
190 | ||
191 | # Execute the check function | |
192 | "${check_func}" | |
193 | local ret="${?}" | |
194 | ||
195 | case "${ret}" in | |
196 | # OK | |
197 | ${EXIT_OK}|${EXIT_TRUE}) | |
198 | log DEBUG "Configuration check succeeded." | |
199 | return ${EXIT_TRUE} | |
200 | ;; | |
201 | ||
202 | # Error | |
203 | ${EXIT_ERROR}|${EXIT_FALSE}) | |
204 | log CRITICAL "Configuration check failed. No settings have been written." | |
205 | return ${EXIT_FALSE} | |
206 | ;; | |
207 | ||
208 | # Command not found | |
209 | ${EXIT_COMMAND_NOT_FOUND}) | |
210 | log CRITICAL "Configuration check function '${check_func}' was not found." | |
211 | return ${EXIT_FALSE} | |
212 | ;; | |
213 | esac | |
214 | ||
215 | log CRITICAL "Unhandled exit code for '${check_func}': ${ret}" | |
216 | return ${EXIT_ERROR} | |
217 | } | |
218 | ||
219 | settings_set() { | |
220 | while [ $# -gt 0 ]; do | |
221 | case "${1}" in | |
222 | *=*) | |
223 | local key=$(cli_get_key "${1}") | |
224 | local val=$(cli_get_val "${1}") | |
225 | ||
226 | log INFO "Setting configuration option '${key}=${val}'". | |
227 | ||
228 | printf -v ${key} "%s" "${val}" | |
229 | ;; | |
230 | *) | |
231 | warning "Invalid parameter given: ${1}" | |
232 | ;; | |
233 | esac | |
234 | shift | |
235 | done | |
236 | } | |
237 | ||
238 | network_settings_read() { | |
239 | local options="${NETWORK_SETTINGS_FILE_PARAMS}" | |
240 | ||
241 | # If the DEBUG variable has already been set, | |
242 | # don't overwrite it. | |
243 | if [ -n "${DEBUG}" ]; then | |
244 | list_remove options DEBUG | |
245 | fi | |
246 | ||
247 | settings_read "${NETWORK_SETTINGS_FILE}" ${options} | |
248 | } | |
249 | ||
250 | network_settings_write() { | |
251 | settings_write "${NETWORK_SETTINGS_FILE}" ${NETWORK_SETTINGS_FILE_PARAMS} | |
252 | } | |
253 | ||
254 | network_settings_set() { | |
255 | # Process any settings that require immediate actin | |
256 | while [ $# -gt 0 ]; do | |
257 | local arg=${1} | |
258 | shift | |
259 | ||
260 | case "${arg}" in | |
261 | *=*) | |
262 | local key=$(cli_get_key "${arg}") | |
263 | local val=$(cli_get_val "${arg}") | |
264 | ||
265 | case "${key}" in | |
266 | DNS_RANDOMIZE|DNS_SEARCH_DOMAIN|DNS_USE_LOCAL_RESOLVER) | |
267 | dns_generate_resolvconf | |
268 | ;; | |
269 | ||
270 | WIRELESS_REGULATORY_DOMAIN) | |
271 | if ! wireless_valid_reg_domain "${val}"; then | |
272 | warning "Ignoring invalid wireless regulatory domain: ${val}" | |
273 | continue | |
274 | fi | |
275 | ||
276 | if ! wireless_set_reg_domain "${val}"; then | |
277 | error "Error setting wireless regulatory domain: ${val}" | |
278 | fi | |
279 | ;; | |
280 | esac | |
281 | ;; | |
282 | esac | |
283 | ||
284 | # Save setting | |
285 | settings_set ${arg} | |
286 | done | |
287 | ||
288 | return ${EXIT_OK} | |
289 | } | |
290 | ||
291 | network_settings_print() { | |
292 | settings_print ${NETWORK_SETTINGS_FILE_PARAMS} | |
293 | } | |
294 | ||
295 | network_settings_list() { | |
296 | print "${NETWORK_SETTINGS_FILE_PARAMS}" | |
297 | } | |
298 | ||
299 | firewall_settings_read() { | |
300 | settings_read "${FIREWALL_SETTINGS_FILE}" "${FIREWALL_SETTINGS[*]}" | |
301 | } | |
302 | ||
303 | firewall_settings_write() { | |
304 | settings_write "${FIREWALL_SETTINGS_FILE}" "${FIREWALL_SETTINGS[*]}" | |
305 | } | |
306 | ||
307 | firewall_settings_print() { | |
308 | settings_print "${FIREWALL_SETTINGS[*]}" | |
309 | } |