2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2014 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 function settings_read
() {
28 local ignore_superfluous_settings
="false"
33 --ignore-superfluous-settings)
34 ignore_superfluous_settings
="true"
37 list_append valid_keys
"${arg}"
42 if [ -d "${file}" ]; then
43 error
"Not a configuration file: '${file}'"
47 # Exit if the file cannot be read.
48 [ -r "${file}" ] ||
return ${EXIT_ERROR}
51 while read -r line
; do
54 key
=$
(cli_get_key
${line})
56 # If valid keys is set, key must be in the list.
57 if [ -n "${valid_keys}" ]; then
58 if ! listmatch
${key} ${valid_keys}; then
59 if ! enabled ignore_superfluous_settings
; then
60 log DEBUG
"Ignoring configuration setting: ${key}"
67 val
=$
(cli_get_val
${line})
68 val
=$
(settings_strip
${val})
71 printf -v ${key} "%s" "${val}"
74 log DEBUG
"Invalid line in configuration file: ${line}"
80 function settings_read_array
() {
91 # Exit if the file cannot be read.
92 [ -r "${file}" ] ||
return ${EXIT_ERROR}
95 while read -r line
; do
98 key
=$
(cli_get_key
${line})
100 # If valid_keys is set, key must be in the list.
101 if [ -n "${valid_keys}" ]; then
102 if ! listmatch
${key} ${valid_keys}; then
103 log DEBUG
"Ignoring configuration setting: ${key}"
108 val
=$
(cli_get_val
${line})
109 val
=$
(settings_strip
${val})
112 printf -v "${array}["${key}"]" "%s" "${val}"
115 log DEBUG
"Invalid line in configuration file: ${line}"
121 # Strip leading and trailing "s.
122 function settings_strip
() {
125 # Do nothing for strings that contain spaces.
126 #if contains_spaces ${var}; then
134 function settings_write
() {
135 local settings_file
="${1}"
136 assert isset settings_file
145 check_func
="$(cli_get_val "${arg}")"
148 # Stop argument processing when reaching the first
149 # configuration parameter
155 done <<< "$(args $@)"
157 # Check if all values to be written are sane
158 if isset check_func
&& ! settings_check
"${check_func}"; then
162 log DEBUG
"Writing settings file ${settings_file}."
164 mkdir
-p $
(dirname ${settings_file}) 2>/dev
/null
168 for param
in $
(listsort $@
); do
169 echo "${param}=\"${!param}\"" >> ${settings_file}
173 function settings_remove() {
174 local settings_file="${1}"
176 local abspath="$
(readlink
-e "${settings_file}")"
177 if [ "${settings_file}" != "${abspath}" ]; then
178 log ERROR "Can only handle absolute paths
"
182 rm -f "${settings_file}"
185 function settings_print() {
188 for param in $(listsort $@); do
189 printf "%-32s = %s
\n" "${param}" "${!param}"
193 function settings_check() {
194 local check_func="${1}"
196 # Execute the check function
202 ${EXIT_OK}|${EXIT_TRUE})
203 log DEBUG "Configuration check succeeded.
"
208 ${EXIT_ERROR}|${EXIT_FALSE})
209 log CRITICAL "Configuration check failed. No settings have been written.
"
214 ${EXIT_COMMAND_NOT_FOUND})
215 log CRITICAL "Configuration check
function '${check_func}' was not found.
"
220 log CRITICAL "Unhandled
exit code
for '${check_func}': ${ret}"
224 function settings_set() {
225 while [ $# -gt 0 ]; do
228 local key=$(cli_get_key ${1})
229 local val=$(cli_get_val ${1})
231 log INFO "Setting configuration option
'${key}=${val}'".
233 printf -v ${key} "%s
" "${val}"
236 warning "Invalid parameter given
: ${1}"
243 function network_settings_read() {
244 local options="${NETWORK_SETTINGS_FILE_PARAMS}"
246 # If the DEBUG variable has already been set,
247 # don't overwrite it.
248 if [ -n "${DEBUG}" ]; then
249 list_remove options DEBUG
252 settings_read "${NETWORK_SETTINGS_FILE}" ${options}
255 function network_settings_write() {
256 settings_write "${NETWORK_SETTINGS_FILE}" ${NETWORK_SETTINGS_FILE_PARAMS}
258 # Update DNS configuration.
259 dns_generate_resolvconf
262 function network_settings_print() {
263 settings_print ${NETWORK_SETTINGS_FILE_PARAMS}
266 function firewall_settings_read() {
267 settings_read "${FIREWALL_SETTINGS_FILE}" "${FIREWALL_SETTINGS_PARAMS}"
270 function firewall_settings_write() {
271 settings_write "${FIREWALL_SETTINGS_FILE}" "${FIREWALL_SETTINGS_PARAMS}"
274 function firewall_settings_print() {
275 settings_print "${FIREWALL_SETTINGS_PARAMS}"