]> git.ipfire.org Git - people/stevee/network.git/blob - src/functions/functions.settings
Split port hooks in to (create|remove|up|down) actions
[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 # Check if all values to be written are sane
140 if ! settings_check; then
141 log CRITICAL "Configuration check failed. No settings have been written."
142 return ${EXIT_ERROR}
143 fi
144
145 log DEBUG "Writing settings file ${settings_file}."
146
147 mkdir -p $(dirname ${settings_file}) 2>/dev/null
148 > ${settings_file}
149
150 local param
151 for param in $(listsort $@); do
152 echo "${param}=\"${!param}\"" >> ${settings_file}
153 done
154 }
155
156 function settings_remove() {
157 local settings_file="${1}"
158
159 local abspath="$(readlink -e "${settings_file}")"
160 if [ "${settings_file}" != "${abspath}" ]; then
161 log ERROR "Can only handle absolute paths"
162 return ${EXIT_ERROR}
163 fi
164
165 rm -f "${settings_file}"
166 }
167
168 function settings_print() {
169 local param
170
171 for param in $(listsort $@); do
172 printf "%-32s = %s\n" "${param}" "${!param}"
173 done
174 }
175
176 function settings_check() {
177 # If there is a function defined that is called __check
178 # we call that function
179 if [ -n "$(type -t hook_check)" ]; then
180 hook_check || return $?
181 fi
182
183 return ${EXIT_OK}
184 }
185
186 function settings_set() {
187 while [ $# -gt 0 ]; do
188 case "${1}" in
189 *=*)
190 local key=$(cli_get_key ${1})
191 local val=$(cli_get_val ${1})
192
193 log INFO "Setting configuration option '${key}=${val}'".
194
195 printf -v ${key} "%s" "${val}"
196 ;;
197 *)
198 warning "Invalid parameter given: ${1}"
199 ;;
200 esac
201 shift
202 done
203 }
204
205 function network_settings_read() {
206 local options="${NETWORK_SETTINGS_FILE_PARAMS}"
207
208 # If the DEBUG variable has already been set,
209 # don't overwrite it.
210 if [ -n "${DEBUG}" ]; then
211 list_remove options DEBUG
212 fi
213
214 settings_read "${NETWORK_SETTINGS_FILE}" ${options}
215 }
216
217 function network_settings_write() {
218 settings_write "${NETWORK_SETTINGS_FILE}" ${NETWORK_SETTINGS_FILE_PARAMS}
219
220 # Update DNS configuration.
221 dns_generate_resolvconf
222 }
223
224 function network_settings_print() {
225 settings_print ${NETWORK_SETTINGS_FILE_PARAMS}
226 }
227
228 function firewall_settings_read() {
229 settings_read "${FIREWALL_SETTINGS_FILE}" "${FIREWALL_SETTINGS_PARAMS}"
230 }
231
232 function firewall_settings_write() {
233 settings_write "${FIREWALL_SETTINGS_FILE}" "${FIREWALL_SETTINGS_PARAMS}"
234 }
235
236 function firewall_settings_print() {
237 settings_print "${FIREWALL_SETTINGS_PARAMS}"
238 }