]> git.ipfire.org Git - people/stevee/network.git/blob - src/functions/functions.settings
Fix hook settings writing and checking
[people/stevee/network.git] / src / functions / functions.settings
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 function 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 if ! listmatch ${key} ${valid_keys}; then
59 if ! enabled ignore_superfluous_settings; then
60 log DEBUG "Ignoring configuration setting: ${key}"
61 fi
62
63 continue
64 fi
65 fi
66
67 val=$(cli_get_val ${line})
68 val=$(settings_strip ${val})
69
70 # Assign variable.
71 printf -v ${key} "%s" "${val}"
72 ;;
73 *)
74 log DEBUG "Invalid line in configuration file: ${line}"
75 ;;
76 esac
77 done < ${file}
78 }
79
80 function settings_read_array() {
81 local file=${1}
82 assert isset file
83 shift
84
85 local array=${1}
86 assert isset array
87 shift
88
89 local valid_keys=$@
90
91 # Exit if the file cannot be read.
92 [ -r "${file}" ] || return ${EXIT_ERROR}
93
94 local line key val
95 while read -r line; do
96 case "${line}" in
97 *=*)
98 key=$(cli_get_key ${line})
99
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}"
104 continue
105 fi
106 fi
107
108 val=$(cli_get_val ${line})
109 val=$(settings_strip ${val})
110
111 # Assign variable.
112 printf -v "${array}["${key}"]" "%s" "${val}"
113 ;;
114 *)
115 log DEBUG "Invalid line in configuration file: ${line}"
116 ;;
117 esac
118 done < ${file}
119 }
120
121 # Strip leading and trailing "s.
122 function settings_strip() {
123 local var="$@"
124
125 # Do nothing for strings that contain spaces.
126 #if contains_spaces ${var}; then
127 # print "${var}"
128 # return ${EXIT_OK}
129 #fi
130
131 unquote "${var}"
132 }
133
134 function settings_write() {
135 local settings_file="${1}"
136 assert isset settings_file
137 shift
138
139 local check_func
140
141 local arg
142 while read arg; do
143 case "${arg}" in
144 --check=*)
145 check_func="$(cli_get_val "${arg}")"
146 ;;
147
148 # Stop argument processing when reaching the first
149 # configuration parameter
150 *)
151 break
152 ;;
153 esac
154 shift
155 done <<< "$(args $@)"
156
157 # Check if all values to be written are sane
158 if isset check_func && ! settings_check "${check_func}"; then
159 return ${EXIT_ERROR}
160 fi
161
162 log DEBUG "Writing settings file ${settings_file}."
163
164 mkdir -p $(dirname ${settings_file}) 2>/dev/null
165 > ${settings_file}
166
167 local param
168 for param in $(listsort $@); do
169 echo "${param}=\"${!param}\"" >> ${settings_file}
170 done
171 }
172
173 function settings_remove() {
174 local settings_file="${1}"
175
176 local abspath="$(readlink -e "${settings_file}")"
177 if [ "${settings_file}" != "${abspath}" ]; then
178 log ERROR "Can only handle absolute paths"
179 return ${EXIT_ERROR}
180 fi
181
182 rm -f "${settings_file}"
183 }
184
185 function settings_print() {
186 local param
187
188 for param in $(listsort $@); do
189 printf "%-32s = %s\n" "${param}" "${!param}"
190 done
191 }
192
193 function settings_check() {
194 local check_func="${1}"
195
196 # Execute the check function
197 "${check_func}"
198 local ret="${?}"
199
200 case "${ret}" in
201 # OK
202 ${EXIT_OK}|${EXIT_TRUE})
203 log DEBUG "Configuration check succeeded."
204 return ${EXIT_TRUE}
205 ;;
206
207 # Error
208 ${EXIT_ERROR}|${EXIT_FALSE})
209 log CRITICAL "Configuration check failed. No settings have been written."
210 return ${EXIT_FALSE}
211 ;;
212
213 # Command not found
214 ${EXIT_COMMAND_NOT_FOUND})
215 log CRITICAL "Configuration check function '${check_func}' was not found."
216 return ${EXIT_FALSE}
217 ;;
218 esac
219
220 log CRITICAL "Unhandled exit code for '${check_func}': ${ret}"
221 return ${EXIT_ERROR}
222 }
223
224 function settings_set() {
225 while [ $# -gt 0 ]; do
226 case "${1}" in
227 *=*)
228 local key=$(cli_get_key ${1})
229 local val=$(cli_get_val ${1})
230
231 log INFO "Setting configuration option '${key}=${val}'".
232
233 printf -v ${key} "%s" "${val}"
234 ;;
235 *)
236 warning "Invalid parameter given: ${1}"
237 ;;
238 esac
239 shift
240 done
241 }
242
243 function network_settings_read() {
244 local options="${NETWORK_SETTINGS_FILE_PARAMS}"
245
246 # If the DEBUG variable has already been set,
247 # don't overwrite it.
248 if [ -n "${DEBUG}" ]; then
249 list_remove options DEBUG
250 fi
251
252 settings_read "${NETWORK_SETTINGS_FILE}" ${options}
253 }
254
255 function network_settings_write() {
256 settings_write "${NETWORK_SETTINGS_FILE}" ${NETWORK_SETTINGS_FILE_PARAMS}
257
258 # Update DNS configuration.
259 dns_generate_resolvconf
260 }
261
262 function network_settings_print() {
263 settings_print ${NETWORK_SETTINGS_FILE_PARAMS}
264 }
265
266 function firewall_settings_read() {
267 settings_read "${FIREWALL_SETTINGS_FILE}" "${FIREWALL_SETTINGS_PARAMS}"
268 }
269
270 function firewall_settings_write() {
271 settings_write "${FIREWALL_SETTINGS_FILE}" "${FIREWALL_SETTINGS_PARAMS}"
272 }
273
274 function firewall_settings_print() {
275 settings_print "${FIREWALL_SETTINGS_PARAMS}"
276 }