]> git.ipfire.org Git - network.git/blame - functions.config
wpa_supplicant: Fix typo.
[network.git] / functions.config
CommitLineData
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
22# Load all global configuration files.
23function config_read_globals() {
24 network_config_read
25 firewall_config_read
26}
27
28function config_read() {
144a8f96
MT
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}"
7bd91daa
MT
60 ;;
61 esac
62 done < ${file}
63}
64
65function 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}"
144a8f96
MT
101 ;;
102 esac
103 done < ${file}
104}
105
106# Strip leading and trailing "s.
107function config_strip() {
f80ce052
MT
108 local var="$@"
109
110 # Do nothing for strings that contain spaces.
111 if contains_spaces ${var}; then
112 print "${var}"
113 return ${EXIT_OK}
114 fi
144a8f96
MT
115
116 if [ "${var:0:1}" = "\"" ]; then
117 var=${var:1}
118 fi
3647b19f 119
144a8f96 120 local last=$(( ${#var} - 1 ))
780b5867 121 if [ ${last} -ge 0 ] && [ "${var:${last}:1}" = "\"" ]; then
144a8f96 122 var=${var:0:${last}}
3647b19f 123 fi
144a8f96
MT
124
125 print "${var}"
3647b19f
MT
126}
127
128function config_write() {
129 local config_file=${1}
d2a21d01 130 assert isset config_file
3647b19f
MT
131 shift
132
133 # Check if all values to be written are sane
134 config_check
135
136 log DEBUG "Writing configuration file ${config_file}."
137
138 mkdir -p $(dirname ${config_file}) 2>/dev/null
139 > ${config_file}
140
141 local param
142 for param in $(listsort $@); do
143 echo "${param}=\"${!param}\"" >> ${config_file}
144 done
145}
146
147function config_print() {
148 local param
149
150 for param in $(listsort $@); do
acc9efd5 151 printf "%-24s = %s\n" "${param}" "${!param}"
3647b19f
MT
152 done
153}
154
155function config_check() {
156 # If there is a function defined that is called __check
157 # we call that function
158 [ -n "$(type -t _check)" ] && _check
159}
160
97cb552e
MT
161function config_header() {
162 local what=${1}
163 assert isset what
164
165 # Print the header.
166 echo "#"
167 echo "# This is a ${what}."
cd464143
MT
168 echo "# THIS FILE IS AUTOMATICALLY GENERATED AND"
169 echo "# ANY CUSTOM CHANGES WILL BE OVERWRITTEN!"
97cb552e
MT
170 echo "#"
171 echo "# $(date -u)"
172 echo "#"
173 echo
174}
175
3647b19f
MT
176function config_hostname() {
177 local hostname=${1}
178
179 if [ -n "${hostname}" ]; then
180 echo "${hostname}" > ${CONFIG_HOSTNAME}
181 else
182 echo "$(<${CONFIG_HOSTNAME})"
183 fi
184}
185
144a8f96
MT
186function config_domainname() {
187 local hostname=$(config_hostname)
188
189 # Strip off the hostname part and just return
190 # the domain part.
191 print "${hostname#*.}"
192}
193
3647b19f
MT
194function config_set() {
195 while [ $# -gt 0 ]; do
196 case "${1}" in
197 *=*)
6c8635c9
MT
198 local key=$(cli_get_key ${1})
199 local val=$(cli_get_val ${1})
200
201 log INFO "Setting configuration option '${key}=${val}'".
202
144a8f96 203 printf -v ${key} "%s" "${val}"
3647b19f
MT
204 ;;
205 *)
206 warning "Invalid parameter given: ${1}"
207 ;;
208 esac
209 shift
210 done
211}
212
213function network_config_read() {
144a8f96 214 local options=${NETWORK_CONFIG_FILE_PARAMS}
3647b19f 215
144a8f96
MT
216 # If the DEBUG variable has already been set,
217 # don't overwrite it.
218 if [ -n "${DEBUG}" ]; then
219 list_remove options DEBUG
3647b19f 220 fi
144a8f96
MT
221
222 config_read ${NETWORK_CONFIG_FILE} ${options}
3647b19f
MT
223}
224
225function network_config_write() {
519d9b82 226 config_write ${NETWORK_CONFIG_FILE} ${NETWORK_CONFIG_FILE_PARAMS}
acc9efd5
MT
227
228 # Update DNS configuration.
229 dns_generate_resolvconf
3647b19f
MT
230}
231
232function network_config_print() {
519d9b82 233 config_print ${NETWORK_CONFIG_FILE_PARAMS}
3647b19f
MT
234}
235
236function firewall_config_read() {
144a8f96 237 config_read ${FIREWALL_CONFIG_FILE} ${FIREWALL_CONFIG_PARAMS}
3647b19f
MT
238}
239
240function firewall_config_write() {
241 config_write ${FIREWALL_CONFIG_FILE} \
242 ${FIREWALL_CONFIG_PARAMS}
243}
244
245function firewall_config_print() {
246 config_print ${FIREWALL_CONFIG_PARAMS}
247}