]>
Commit | Line | Data |
---|---|---|
3647b19f MT |
1 | #!/bin/bash |
2 | ############################################################################### | |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
5 | # Copyright (C) 2012 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 | ||
3647b19f | 22 | function config_read() { |
144a8f96 MT |
23 | local file=${1} |
24 | assert isset file | |
25 | shift | |
26 | ||
27 | local valid_keys=$@ | |
28 | ||
29 | # Exit if the file cannot be read. | |
30 | [ -r "${file}" ] || return ${EXIT_ERROR} | |
31 | ||
32 | local line key val | |
33 | while read -r line; do | |
34 | case "${line}" in | |
35 | *=*) | |
36 | key=$(cli_get_key ${line}) | |
37 | ||
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}" | |
42 | continue | |
43 | fi | |
44 | fi | |
45 | ||
46 | val=$(cli_get_val ${line}) | |
47 | val=$(config_strip ${val}) | |
48 | ||
49 | # Assign variable. | |
50 | printf -v ${key} "%s" "${val}" | |
51 | ;; | |
52 | *) | |
53 | log DEBUG "Invalid line in configuration file: ${line}" | |
7bd91daa MT |
54 | ;; |
55 | esac | |
56 | done < ${file} | |
57 | } | |
58 | ||
59 | function config_read_array() { | |
60 | local file=${1} | |
61 | assert isset file | |
62 | shift | |
63 | ||
64 | local array=${1} | |
65 | assert isset array | |
66 | shift | |
67 | ||
68 | local valid_keys=$@ | |
69 | ||
70 | # Exit if the file cannot be read. | |
71 | [ -r "${file}" ] || return ${EXIT_ERROR} | |
72 | ||
73 | local line key val | |
74 | while read -r line; do | |
75 | case "${line}" in | |
76 | *=*) | |
77 | key=$(cli_get_key ${line}) | |
78 | ||
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}" | |
83 | continue | |
84 | fi | |
85 | fi | |
86 | ||
87 | val=$(cli_get_val ${line}) | |
88 | val=$(config_strip ${val}) | |
89 | ||
90 | # Assign variable. | |
91 | printf -v "${array}["${key}"]" "%s" "${val}" | |
92 | ;; | |
93 | *) | |
94 | log DEBUG "Invalid line in configuration file: ${line}" | |
144a8f96 MT |
95 | ;; |
96 | esac | |
97 | done < ${file} | |
98 | } | |
99 | ||
100 | # Strip leading and trailing "s. | |
101 | function config_strip() { | |
f80ce052 MT |
102 | local var="$@" |
103 | ||
104 | # Do nothing for strings that contain spaces. | |
105 | if contains_spaces ${var}; then | |
106 | print "${var}" | |
107 | return ${EXIT_OK} | |
108 | fi | |
144a8f96 | 109 | |
04854c77 | 110 | unquote "${var}" |
3647b19f MT |
111 | } |
112 | ||
113 | function config_write() { | |
114 | local config_file=${1} | |
d2a21d01 | 115 | assert isset config_file |
3647b19f MT |
116 | shift |
117 | ||
118 | # Check if all values to be written are sane | |
119 | config_check | |
120 | ||
121 | log DEBUG "Writing configuration file ${config_file}." | |
122 | ||
123 | mkdir -p $(dirname ${config_file}) 2>/dev/null | |
124 | > ${config_file} | |
125 | ||
126 | local param | |
127 | for param in $(listsort $@); do | |
128 | echo "${param}=\"${!param}\"" >> ${config_file} | |
129 | done | |
130 | } | |
131 | ||
132 | function config_print() { | |
133 | local param | |
134 | ||
135 | for param in $(listsort $@); do | |
acc9efd5 | 136 | printf "%-24s = %s\n" "${param}" "${!param}" |
3647b19f MT |
137 | done |
138 | } | |
139 | ||
140 | function config_check() { | |
141 | # If there is a function defined that is called __check | |
142 | # we call that function | |
143 | [ -n "$(type -t _check)" ] && _check | |
144 | } | |
145 | ||
97cb552e MT |
146 | function config_header() { |
147 | local what=${1} | |
148 | assert isset what | |
149 | ||
150 | # Print the header. | |
151 | echo "#" | |
152 | echo "# This is a ${what}." | |
cd464143 MT |
153 | echo "# THIS FILE IS AUTOMATICALLY GENERATED AND" |
154 | echo "# ANY CUSTOM CHANGES WILL BE OVERWRITTEN!" | |
97cb552e MT |
155 | echo "#" |
156 | echo "# $(date -u)" | |
157 | echo "#" | |
158 | echo | |
159 | } | |
160 | ||
3647b19f MT |
161 | function config_hostname() { |
162 | local hostname=${1} | |
163 | ||
164 | if [ -n "${hostname}" ]; then | |
165 | echo "${hostname}" > ${CONFIG_HOSTNAME} | |
166 | else | |
167 | echo "$(<${CONFIG_HOSTNAME})" | |
168 | fi | |
169 | } | |
170 | ||
144a8f96 MT |
171 | function config_domainname() { |
172 | local hostname=$(config_hostname) | |
173 | ||
174 | # Strip off the hostname part and just return | |
175 | # the domain part. | |
176 | print "${hostname#*.}" | |
177 | } | |
178 | ||
3647b19f MT |
179 | function config_set() { |
180 | while [ $# -gt 0 ]; do | |
181 | case "${1}" in | |
182 | *=*) | |
6c8635c9 MT |
183 | local key=$(cli_get_key ${1}) |
184 | local val=$(cli_get_val ${1}) | |
185 | ||
186 | log INFO "Setting configuration option '${key}=${val}'". | |
187 | ||
144a8f96 | 188 | printf -v ${key} "%s" "${val}" |
3647b19f MT |
189 | ;; |
190 | *) | |
191 | warning "Invalid parameter given: ${1}" | |
192 | ;; | |
193 | esac | |
194 | shift | |
195 | done | |
196 | } | |
197 | ||
198 | function network_config_read() { | |
144a8f96 | 199 | local options=${NETWORK_CONFIG_FILE_PARAMS} |
3647b19f | 200 | |
144a8f96 MT |
201 | # If the DEBUG variable has already been set, |
202 | # don't overwrite it. | |
203 | if [ -n "${DEBUG}" ]; then | |
204 | list_remove options DEBUG | |
3647b19f | 205 | fi |
144a8f96 MT |
206 | |
207 | config_read ${NETWORK_CONFIG_FILE} ${options} | |
3647b19f MT |
208 | } |
209 | ||
210 | function network_config_write() { | |
519d9b82 | 211 | config_write ${NETWORK_CONFIG_FILE} ${NETWORK_CONFIG_FILE_PARAMS} |
acc9efd5 MT |
212 | |
213 | # Update DNS configuration. | |
214 | dns_generate_resolvconf | |
3647b19f MT |
215 | } |
216 | ||
217 | function network_config_print() { | |
519d9b82 | 218 | config_print ${NETWORK_CONFIG_FILE_PARAMS} |
3647b19f MT |
219 | } |
220 | ||
fe52c5e0 MT |
221 | function firewall_config_file() { |
222 | local protocol="${1}" | |
223 | assert isset protocol | |
224 | ||
225 | local file | |
226 | case "${protocol}" in | |
227 | ipv6) | |
228 | file="${FIREWALL6_CONFIG_FILE}" | |
229 | ;; | |
230 | ipv4) | |
231 | file="${FIREWALL4_CONFIG_FILE}" | |
232 | ;; | |
233 | esac | |
234 | assert isset file | |
235 | ||
236 | print "${file}" | |
237 | return ${EXIT_OK} | |
238 | } | |
239 | ||
240 | function firewall_config_env() { | |
241 | local protocol="${1}" | |
242 | assert isset protocol | |
243 | ||
244 | case "${protocol}" in | |
245 | ipv6) | |
246 | file="${FIREWALL6_CONFIG_FILE}" | |
247 | params="${FIREWALL6_CONFIG_PARAMS}" | |
248 | ;; | |
249 | ipv4) | |
250 | file="${FIREWALL4_CONFIG_FILE}" | |
251 | params="${FIREWALL4_CONFIG_PARAMS}" | |
252 | ;; | |
253 | esac | |
254 | assert isset file | |
255 | assert isset params | |
256 | } | |
257 | ||
3647b19f | 258 | function firewall_config_read() { |
fe52c5e0 MT |
259 | local file params |
260 | firewall_config_env "$@" | |
261 | ||
262 | config_read "${file}" "${params}" | |
3647b19f MT |
263 | } |
264 | ||
265 | function firewall_config_write() { | |
fe52c5e0 MT |
266 | local file params |
267 | firewall_config_env "$@" | |
268 | ||
269 | config_write "${file}" "${params}" | |
3647b19f MT |
270 | } |
271 | ||
272 | function firewall_config_print() { | |
fe52c5e0 MT |
273 | local file params |
274 | firewall_config_env "$@" | |
275 | ||
276 | config_print "${params}" | |
3647b19f | 277 | } |