]> git.ipfire.org Git - people/ms/network.git/blob - functions.config
DNS: Add options to configure local DNS servers.
[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 config_file=${1}
30 assert isset config_file
31
32 if [ -e "${config_file}" ]; then
33 . ${config_file}
34 config_check
35 fi
36 }
37
38 function config_write() {
39 local config_file=${1}
40 assert isset config_file
41 shift
42
43 # Check if all values to be written are sane
44 config_check
45
46 log DEBUG "Writing configuration file ${config_file}."
47
48 mkdir -p $(dirname ${config_file}) 2>/dev/null
49 > ${config_file}
50
51 local param
52 for param in $(listsort $@); do
53 echo "${param}=\"${!param}\"" >> ${config_file}
54 done
55 }
56
57 function config_print() {
58 local param
59
60 for param in $(listsort $@); do
61 printf "%-24s = %s\n" "${param}" "${!param}"
62 done
63 }
64
65 function config_check() {
66 # If there is a function defined that is called __check
67 # we call that function
68 [ -n "$(type -t _check)" ] && _check
69 }
70
71 function config_header() {
72 local what=${1}
73 assert isset what
74
75 # Print the header.
76 echo "#"
77 echo "# This is a ${what}."
78 echo "# THIS FILE IS AUTOMATICALLY GENERATED AND WILL OVERWRITE"
79 echo "# ANY CUSTOM CHANGES!"
80 echo "#"
81 echo "# $(date -u)"
82 echo "#"
83 echo
84 }
85
86 function config_hostname() {
87 local hostname=${1}
88
89 if [ -n "${hostname}" ]; then
90 echo "${hostname}" > ${CONFIG_HOSTNAME}
91 else
92 echo "$(<${CONFIG_HOSTNAME})"
93 fi
94 }
95
96 function config_set() {
97 while [ $# -gt 0 ]; do
98 case "${1}" in
99 *=*)
100 log INFO "Setting configuration option '${1}'".
101 eval ${1}
102 ;;
103 *)
104 warning "Invalid parameter given: ${1}"
105 ;;
106 esac
107 shift
108 done
109 }
110
111 function network_config_read() {
112 # Save state of DEBUG and restore it later.
113 local debug=${DEBUG}
114
115 config_read ${NETWORK_CONFIG_FILE}
116
117 if [ -n "${debug}" ]; then
118 DEBUG=${debug}
119 fi
120 }
121
122 function network_config_write() {
123 config_write ${NETWORK_CONFIG_FILE} ${NETWORK_CONFIG_FILE_PARAMS}
124
125 # Update DNS configuration.
126 dns_generate_resolvconf
127 }
128
129 function network_config_print() {
130 config_print ${NETWORK_CONFIG_FILE_PARAMS}
131 }
132
133 function firewall_config_read() {
134 config_read ${FIREWALL_CONFIG_FILE}
135 }
136
137 function firewall_config_write() {
138 config_write ${FIREWALL_CONFIG_FILE} \
139 ${FIREWALL_CONFIG_PARAMS}
140 }
141
142 function firewall_config_print() {
143 config_print ${FIREWALL_CONFIG_PARAMS}
144 }