]>
Commit | Line | Data |
---|---|---|
e9df08ad MT |
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 | ||
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=$(settings_strip ${val}) | |
48 | ||
49 | # Assign variable. | |
50 | printf -v ${key} "%s" "${val}" | |
51 | ;; | |
52 | *) | |
53 | log DEBUG "Invalid line in configuration file: ${line}" | |
54 | ;; | |
55 | esac | |
56 | done < ${file} | |
57 | } | |
58 | ||
59 | function settings_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=$(settings_strip ${val}) | |
89 | ||
90 | # Assign variable. | |
91 | printf -v "${array}["${key}"]" "%s" "${val}" | |
92 | ;; | |
93 | *) | |
94 | log DEBUG "Invalid line in configuration file: ${line}" | |
95 | ;; | |
96 | esac | |
97 | done < ${file} | |
98 | } | |
99 | ||
100 | # Strip leading and trailing "s. | |
101 | function settings_strip() { | |
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 | |
109 | ||
110 | unquote "${var}" | |
111 | } | |
112 | ||
113 | function settings_write() { | |
114 | local settings_file=${1} | |
115 | assert isset settings_file | |
116 | shift | |
117 | ||
118 | # Check if all values to be written are sane | |
119 | if ! settings_check; then | |
120 | log CRITICAL "Configuration check failed. No settings have been written." | |
121 | return ${EXIT_ERROR} | |
122 | fi | |
123 | ||
124 | log DEBUG "Writing settings file ${settings_file}." | |
125 | ||
126 | mkdir -p $(dirname ${settings_file}) 2>/dev/null | |
127 | > ${settings_file} | |
128 | ||
129 | local param | |
130 | for param in $(listsort $@); do | |
131 | echo "${param}=\"${!param}\"" >> ${settings_file} | |
132 | done | |
133 | } | |
134 | ||
135 | function settings_remove() { | |
136 | local settings_file="${1}" | |
137 | ||
138 | local abspath="$(readlink -e "${settings_file}")" | |
139 | if [ "${settings_file}" != "${abspath}" ]; then | |
140 | log ERROR "Can only handle absolute paths" | |
141 | return ${EXIT_ERROR} | |
142 | fi | |
143 | ||
144 | rm -f "${settings_file}" | |
145 | } | |
146 | ||
147 | function settings_print() { | |
148 | local param | |
149 | ||
150 | for param in $(listsort $@); do | |
151 | printf "%-32s = %s\n" "${param}" "${!param}" | |
152 | done | |
153 | } | |
154 | ||
155 | function settings_check() { | |
156 | # If there is a function defined that is called __check | |
157 | # we call that function | |
ea699552 MT |
158 | if [ -n "$(type -t hook_check)" ]; then |
159 | hook_check || return $? | |
e9df08ad MT |
160 | fi |
161 | ||
162 | return ${EXIT_OK} | |
163 | } | |
164 | ||
165 | function settings_set() { | |
166 | while [ $# -gt 0 ]; do | |
167 | case "${1}" in | |
168 | *=*) | |
169 | local key=$(cli_get_key ${1}) | |
170 | local val=$(cli_get_val ${1}) | |
171 | ||
172 | log INFO "Setting configuration option '${key}=${val}'". | |
173 | ||
174 | printf -v ${key} "%s" "${val}" | |
175 | ;; | |
176 | *) | |
177 | warning "Invalid parameter given: ${1}" | |
178 | ;; | |
179 | esac | |
180 | shift | |
181 | done | |
182 | } | |
183 | ||
184 | function network_settings_read() { | |
185 | local options="${NETWORK_SETTINGS_FILE_PARAMS}" | |
186 | ||
187 | # If the DEBUG variable has already been set, | |
188 | # don't overwrite it. | |
189 | if [ -n "${DEBUG}" ]; then | |
190 | list_remove options DEBUG | |
191 | fi | |
192 | ||
193 | settings_read "${NETWORK_SETTINGS_FILE}" ${options} | |
194 | } | |
195 | ||
196 | function network_settings_write() { | |
197 | settings_write "${NETWORK_SETTINGS_FILE}" ${NETWORK_SETTINGS_FILE_PARAMS} | |
198 | ||
199 | # Update DNS configuration. | |
200 | dns_generate_resolvconf | |
201 | } | |
202 | ||
203 | function network_settings_print() { | |
204 | settings_print ${NETWORK_SETTINGS_FILE_PARAMS} | |
205 | } | |
206 | ||
207 | function firewall_settings_read() { | |
208 | settings_read "${FIREWALL_SETTINGS_FILE}" "${FIREWALL_SETTINGS_PARAMS}" | |
209 | } | |
210 | ||
211 | function firewall_settings_write() { | |
212 | settings_write "${FIREWALL_SETTINGS_FILE}" "${FIREWALL_SETTINGS_PARAMS}" | |
213 | } | |
214 | ||
215 | function firewall_settings_print() { | |
216 | settings_print "${FIREWALL_SETTINGS_PARAMS}" | |
217 | } |