2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2012 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 config_read
() {
29 # Exit if the file cannot be read.
30 [ -r "${file}" ] ||
return ${EXIT_ERROR}
33 while read -r line
; do
36 key
=$
(cli_get_key
${line})
38 # If valid keys is set, key must be in the list.
39 if [ -n "${valid_keys}" ]; then
40 if ! listmatch
${key} ${valid_keys}; then
41 log DEBUG
"Ignoring configuration setting: ${key}"
46 val
=$
(cli_get_val
${line})
47 val
=$
(config_strip
${val})
50 printf -v ${key} "%s" "${val}"
53 log DEBUG
"Invalid line in configuration file: ${line}"
59 function config_read_array
() {
70 # Exit if the file cannot be read.
71 [ -r "${file}" ] ||
return ${EXIT_ERROR}
74 while read -r line
; do
77 key
=$
(cli_get_key
${line})
79 # If valid_keys is set, key must be in the list.
80 if [ -n "${valid_keys}" ]; then
81 if ! listmatch
${key} ${valid_keys}; then
82 log DEBUG
"Ignoring configuration setting: ${key}"
87 val
=$
(cli_get_val
${line})
88 val
=$
(config_strip
${val})
91 printf -v "${array}["${key}"]" "%s" "${val}"
94 log DEBUG
"Invalid line in configuration file: ${line}"
100 # Strip leading and trailing "s.
101 function config_strip
() {
104 # Do nothing for strings that contain spaces.
105 if contains_spaces
${var}; then
113 function config_write
() {
114 local config_file
=${1}
115 assert isset config_file
118 # Check if all values to be written are sane
119 if ! config_check
; then
120 log CRITICAL
"Configuration check failed. No config has been written."
124 log DEBUG
"Writing configuration file ${config_file}."
126 mkdir
-p $
(dirname ${config_file}) 2>/dev
/null
130 for param
in $
(listsort $@
); do
131 echo "${param}=\"${!param}\"" >> ${config_file}
135 function config_print() {
138 for param in $(listsort $@); do
139 printf "%-32s = %s
\n" "${param}" "${!param}"
143 function config_check() {
144 # If there is a function defined that is called __check
145 # we call that function
146 [ -n "$
(type -t _check
)" ] && _check || return $?
149 function config_header() {
155 echo "# This is a ${what}."
156 echo "# THIS FILE IS AUTOMATICALLY GENERATED AND"
157 echo "# ANY CUSTOM CHANGES WILL BE OVERWRITTEN!"
164 function config_hostname
() {
167 if [ -n "${hostname}" ]; then
168 echo "${hostname}" > ${CONFIG_HOSTNAME}
170 echo "$(<${CONFIG_HOSTNAME})"
174 function config_domainname
() {
175 local hostname
=$
(config_hostname
)
177 # Strip off the hostname part and just return
179 print
"${hostname#*.}"
182 function config_set
() {
183 while [ $# -gt 0 ]; do
186 local key
=$
(cli_get_key
${1})
187 local val
=$
(cli_get_val
${1})
189 log INFO
"Setting configuration option '${key}=${val}'".
191 printf -v ${key} "%s" "${val}"
194 warning
"Invalid parameter given: ${1}"
201 function network_config_read
() {
202 local options
=${NETWORK_CONFIG_FILE_PARAMS}
204 # If the DEBUG variable has already been set,
205 # don't overwrite it.
206 if [ -n "${DEBUG}" ]; then
207 list_remove options DEBUG
210 config_read
${NETWORK_CONFIG_FILE} ${options}
213 function network_config_write
() {
214 config_write
${NETWORK_CONFIG_FILE} ${NETWORK_CONFIG_FILE_PARAMS}
216 # Update DNS configuration.
217 dns_generate_resolvconf
220 function network_config_print
() {
221 config_print
${NETWORK_CONFIG_FILE_PARAMS}
224 function firewall_config_read
() {
225 config_read
"${FIREWALL_CONFIG_FILE}" "${FIREWALL_CONFIG_PARAMS}"
228 function firewall_config_write
() {
229 config_write
"${FIREWALL_CONFIG_FILE}" "${FIREWALL_CONFIG_PARAMS}"
232 function firewall_config_print
() {
233 config_print
"${FIREWALL_CONFIG_PARAMS}"