Project:
IPFire
Code Location:
git://git.ipfire.org/network.gitmaster
/
functions.config
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/bin/bash ############################################################################### # # # IPFire.org - A linux based firewall # # Copyright (C) 2012 IPFire Network Development Team # # # # This program is free software: you can redistribute it and/or modify # # it under the terms of the GNU General Public License as published by # # the Free Software Foundation, either version 3 of the License, or # # (at your option) any later version. # # # # This program is distributed in the hope that it will be useful, # # but WITHOUT ANY WARRANTY; without even the implied warranty of # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # # GNU General Public License for more details. # # # # You should have received a copy of the GNU General Public License # # along with this program. If not, see <http://www.gnu.org/licenses/>. # # # ############################################################################### function config_read() { local file=${1} assert isset file shift local valid_keys=$@ # Exit if the file cannot be read. [ -r "${file}" ] || return ${EXIT_ERROR} local line key val while read -r line; do case "${line}" in *=*) key=$(cli_get_key ${line}) # If valid keys is set, key must be in the list. if [ -n "${valid_keys}" ]; then if ! listmatch ${key} ${valid_keys}; then log DEBUG "Ignoring configuration setting: ${key}" continue fi fi val=$(cli_get_val ${line}) val=$(config_strip ${val}) # Assign variable. printf -v ${key} "%s" "${val}" ;; *) log DEBUG "Invalid line in configuration file: ${line}" ;; esac done < ${file} } function config_read_array() { local file=${1} assert isset file shift local array=${1} assert isset array shift local valid_keys=$@ # Exit if the file cannot be read. [ -r "${file}" ] || return ${EXIT_ERROR} local line key val while read -r line; do case "${line}" in *=*) key=$(cli_get_key ${line}) # If valid_keys is set, key must be in the list. if [ -n "${valid_keys}" ]; then if ! listmatch ${key} ${valid_keys}; then log DEBUG "Ignoring configuration setting: ${key}" continue fi fi val=$(cli_get_val ${line}) val=$(config_strip ${val}) # Assign variable. printf -v "${array}["${key}"]" "%s" "${val}" ;; *) log DEBUG "Invalid line in configuration file: ${line}" ;; esac done < ${file} } # Strip leading and trailing "s. function config_strip() { local var="$@" # Do nothing for strings that contain spaces. if contains_spaces ${var}; then print "${var}" return ${EXIT_OK} fi unquote "${var}" } function config_write() { local config_file=${1} assert isset config_file shift # Check if all values to be written are sane if ! config_check; then log CRITICAL "Configuration check failed. No config has been written." return ${EXIT_ERROR} fi log DEBUG "Writing configuration file ${config_file}." mkdir -p $(dirname ${config_file}) 2>/dev/null > ${config_file} local param for param in $(listsort $@); do echo "${param}=\"${!param}\"" >> ${config_file} done } function config_print() { local param for param in $(listsort $@); do printf "%-32s = %s\n" "${param}" "${!param}" done } function config_check() { # If there is a function defined that is called __check # we call that function if [ -n "$(type -t _check)" ]; then _check || return $? fi return ${EXIT_OK} } function config_header() { local what=${1} assert isset what # Print the header. echo "#" echo "# This is a ${what}." echo "# THIS FILE IS AUTOMATICALLY GENERATED AND" echo "# ANY CUSTOM CHANGES WILL BE OVERWRITTEN!" echo "#" echo "# $(date -u)" echo "#" echo } function config_hostname() { local hostname=${1} if [ -n "${hostname}" ]; then echo "${hostname}" > ${CONFIG_HOSTNAME} else echo "$(<${CONFIG_HOSTNAME})" fi } function config_domainname() { local hostname=$(config_hostname) # Strip off the hostname part and just return # the domain part. print "${hostname#*.}" } function config_set() { while [ $# -gt 0 ]; do case "${1}" in *=*) local key=$(cli_get_key ${1}) local val=$(cli_get_val ${1}) log INFO "Setting configuration option '${key}=${val}'". printf -v ${key} "%s" "${val}" ;; *) warning "Invalid parameter given: ${1}" ;; esac shift done } function network_config_read() { local options=${NETWORK_CONFIG_FILE_PARAMS} # If the DEBUG variable has already been set, # don't overwrite it. if [ -n "${DEBUG}" ]; then list_remove options DEBUG fi config_read ${NETWORK_CONFIG_FILE} ${options} } function network_config_write() { config_write ${NETWORK_CONFIG_FILE} ${NETWORK_CONFIG_FILE_PARAMS} # Update DNS configuration. dns_generate_resolvconf } function network_config_print() { config_print ${NETWORK_CONFIG_FILE_PARAMS} } function firewall_config_read() { config_read "${FIREWALL_CONFIG_FILE}" "${FIREWALL_CONFIG_PARAMS}" } function firewall_config_write() { config_write "${FIREWALL_CONFIG_FILE}" "${FIREWALL_CONFIG_PARAMS}" } function firewall_config_print() { config_print "${FIREWALL_CONFIG_PARAMS}" }
