]> git.ipfire.org Git - people/ms/network.git/blob - functions.config
config: Fix reading in empty values.
[people/ms/network.git] / functions.config
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
22 # Load all global configuration files.
23 function config_read_globals() {
24 network_config_read
25 firewall_config_read
26 }
27
28 function config_read() {
29 local file=${1}
30 assert isset file
31 shift
32
33 local valid_keys=$@
34
35 # Exit if the file cannot be read.
36 [ -r "${file}" ] || return ${EXIT_ERROR}
37
38 local line key val
39 while read -r line; do
40 case "${line}" in
41 *=*)
42 key=$(cli_get_key ${line})
43
44 # If valid keys is set, key must be in the list.
45 if [ -n "${valid_keys}" ]; then
46 if ! listmatch ${key} ${valid_keys}; then
47 log DEBUG "Ignoring configuration setting: ${key}"
48 continue
49 fi
50 fi
51
52 val=$(cli_get_val ${line})
53 val=$(config_strip ${val})
54
55 # Assign variable.
56 printf -v ${key} "%s" "${val}"
57 ;;
58 *)
59 log DEBUG "Invalid line in configuration file: ${line}"
60 ;;
61 esac
62 done < ${file}
63 }
64
65 function config_read_array() {
66 local file=${1}
67 assert isset file
68 shift
69
70 local array=${1}
71 assert isset array
72 shift
73
74 local valid_keys=$@
75
76 # Exit if the file cannot be read.
77 [ -r "${file}" ] || return ${EXIT_ERROR}
78
79 local line key val
80 while read -r line; do
81 case "${line}" in
82 *=*)
83 key=$(cli_get_key ${line})
84
85 # If valid_keys is set, key must be in the list.
86 if [ -n "${valid_keys}" ]; then
87 if ! listmatch ${key} ${valid_keys}; then
88 log DEBUG "Ignoring configuration setting: ${key}"
89 continue
90 fi
91 fi
92
93 val=$(cli_get_val ${line})
94 val=$(config_strip ${val})
95
96 # Assign variable.
97 printf -v "${array}["${key}"]" "%s" "${val}"
98 ;;
99 *)
100 log DEBUG "Invalid line in configuration file: ${line}"
101 ;;
102 esac
103 done < ${file}
104 }
105
106 # Strip leading and trailing "s.
107 function config_strip() {
108 local var=${1}
109
110 if [ "${var:0:1}" = "\"" ]; then
111 var=${var:1}
112 fi
113
114 local last=$(( ${#var} - 1 ))
115 if [ ${last} -ge 0 ] && [ "${var:${last}:1}" = "\"" ]; then
116 var=${var:0:${last}}
117 fi
118
119 print "${var}"
120 }
121
122 function config_write() {
123 local config_file=${1}
124 assert isset config_file
125 shift
126
127 # Check if all values to be written are sane
128 config_check
129
130 log DEBUG "Writing configuration file ${config_file}."
131
132 mkdir -p $(dirname ${config_file}) 2>/dev/null
133 > ${config_file}
134
135 local param
136 for param in $(listsort $@); do
137 echo "${param}=\"${!param}\"" >> ${config_file}
138 done
139 }
140
141 function config_print() {
142 local param
143
144 for param in $(listsort $@); do
145 printf "%-24s = %s\n" "${param}" "${!param}"
146 done
147 }
148
149 function config_check() {
150 # If there is a function defined that is called __check
151 # we call that function
152 [ -n "$(type -t _check)" ] && _check
153 }
154
155 function config_header() {
156 local what=${1}
157 assert isset what
158
159 # Print the header.
160 echo "#"
161 echo "# This is a ${what}."
162 echo "# THIS FILE IS AUTOMATICALLY GENERATED AND"
163 echo "# ANY CUSTOM CHANGES WILL BE OVERWRITTEN!"
164 echo "#"
165 echo "# $(date -u)"
166 echo "#"
167 echo
168 }
169
170 function config_hostname() {
171 local hostname=${1}
172
173 if [ -n "${hostname}" ]; then
174 echo "${hostname}" > ${CONFIG_HOSTNAME}
175 else
176 echo "$(<${CONFIG_HOSTNAME})"
177 fi
178 }
179
180 function config_domainname() {
181 local hostname=$(config_hostname)
182
183 # Strip off the hostname part and just return
184 # the domain part.
185 print "${hostname#*.}"
186 }
187
188 function config_set() {
189 while [ $# -gt 0 ]; do
190 case "${1}" in
191 *=*)
192 local key=$(cli_get_key ${1})
193 local val=$(cli_get_val ${1})
194
195 log INFO "Setting configuration option '${key}=${val}'".
196
197 printf -v ${key} "%s" "${val}"
198 ;;
199 *)
200 warning "Invalid parameter given: ${1}"
201 ;;
202 esac
203 shift
204 done
205 }
206
207 function network_config_read() {
208 local options=${NETWORK_CONFIG_FILE_PARAMS}
209
210 # If the DEBUG variable has already been set,
211 # don't overwrite it.
212 if [ -n "${DEBUG}" ]; then
213 list_remove options DEBUG
214 fi
215
216 config_read ${NETWORK_CONFIG_FILE} ${options}
217 }
218
219 function network_config_write() {
220 config_write ${NETWORK_CONFIG_FILE} ${NETWORK_CONFIG_FILE_PARAMS}
221
222 # Update DNS configuration.
223 dns_generate_resolvconf
224 }
225
226 function network_config_print() {
227 config_print ${NETWORK_CONFIG_FILE_PARAMS}
228 }
229
230 function firewall_config_read() {
231 config_read ${FIREWALL_CONFIG_FILE} ${FIREWALL_CONFIG_PARAMS}
232 }
233
234 function firewall_config_write() {
235 config_write ${FIREWALL_CONFIG_FILE} \
236 ${FIREWALL_CONFIG_PARAMS}
237 }
238
239 function firewall_config_print() {
240 config_print ${FIREWALL_CONFIG_PARAMS}
241 }