]> git.ipfire.org Git - people/arne_f/network.git/blob - functions.util
a953a27e0a472f398394f91e4c43e5bb616e070d
[people/arne_f/network.git] / functions.util
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
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 # Print a pretty error message
23 function error() {
24 echo -e " ${COLOUR_ERROR}ERROR${COLOUR_NORMAL} : $@" >&2
25 }
26
27 function error_log() {
28 error "$@"
29 log ERROR "$@"
30 }
31
32 # Print a pretty warn message
33 function warning() {
34 echo -e " ${COLOUR_WARN}WARNING${COLOUR_NORMAL}: $@" >&2
35 }
36
37 function warning_log() {
38 warning "$@"
39 log WARNING "$@"
40 }
41
42 function listsort() {
43 local i
44 for i in $@; do
45 echo "${i}"
46 done | sort | tr "\n" " "
47 }
48
49 function config_read() {
50 local config_file=${1}
51
52 if [ -e "${config_file}" ]; then
53 . ${config_file}
54 config_check
55 fi
56 }
57
58 function config_write() {
59 local config_file=${1}
60 shift
61
62 # Check if all values to be written are sane
63 config_check
64
65 log DEBUG "Writing configuration file ${config_file}."
66
67 > ${config_file}
68
69 local param
70 for param in $(listsort $@); do
71 echo "${param}=\"${!param}\"" >> ${config_file}
72 done
73 }
74
75 function config_print() {
76 local param
77
78 for param in $(listsort $@); do
79 printf "%-16s = %s\n" "${param}" "${!param}"
80 done
81 }
82
83 function config_check() {
84 # If there is a function defined that is called __check
85 # we call that function
86 [ -n "$(type -t _check)" ] && _check
87 }
88
89 function network_config_set() {
90 while [ $# -gt 0 ]; do
91 case "${1}" in
92 *=*)
93 log INFO "Setting configuration option '${1}'".
94 eval ${1}
95 ;;
96 *)
97 warning "Invalid parameter given: ${1}"
98 ;;
99 esac
100 shift
101 done
102
103 # Write configuration to disk
104 network_config_write
105 }
106
107 function network_config_read() {
108 config_read ${CONFIG_FILE}
109 }
110
111 function network_config_write() {
112 config_write ${CONFIG_FILE} ${CONFIG_FILE_PARAMS}
113 }
114
115 function network_config_print() {
116 config_print ${CONFIG_FILE_PARAMS}
117 }
118
119 # Speedup function to avoid a call of the basename binary
120 function basename() {
121 echo "${1##*/}"
122 }
123
124 function enabled() {
125 local param=${1}
126
127 [ "${!param}" = "yes" ] || [ "${!param}" = "on" ] || [ "${!param}" = "1" ]
128 }
129
130 function mac_generate() {
131 local mac=()
132 for i in $(seq 0 5); do
133 mac[i]="$(uuid)"
134 mac[i]="0x${mac[i]:0:2}"
135 done
136
137 # Remove multicast bit
138 # and set address is software assigned
139 # XXX must doublecheck if this works
140 mac[0]=$((mac[0] & 0xfe))
141 mac[0]=$((mac[0] | 0x02))
142
143 local output
144 for i in ${mac[*]}; do
145 if [ -n "${output}" ]; then
146 output="${output}:"
147 fi
148
149 output="${output}$(printf "%02x" ${i})"
150 done
151
152 # Check if output is valid
153 assert mac_is_valid ${output}
154
155 echo ${output}
156 }
157
158 function mac_format() {
159 local mac=${1}
160
161 local output
162
163 if [ "${#mac}" = "12" ]; then
164 # Add colons (:) to mac address
165 output=${mac:0:2}
166 local i
167 for i in 2 4 6 8 10; do
168 output="${output}:${mac:${i}:2}"
169 done
170 fi
171
172 assert mac_is_valid ${output}
173
174 echo "${output}"
175 }
176
177 function mac_is_valid() {
178 local mac=${1}
179
180 [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]]
181 }
182
183 function uuid() {
184 echo $(</proc/sys/kernel/random/uuid)
185 }
186
187 function isset() {
188 local var=${1}
189
190 [ -n "${!var}" ]
191 }
192
193 function isoneof() {
194 local var=${!1}
195 shift
196
197 for i in $@; do
198 [ "${var}" = "${i}" ] && return ${EXIT_OK}
199 done
200
201 return ${EXIT_ERROR}
202 }
203
204 function isbool() {
205 local var=${1}
206
207 isoneof ${var} 0 1 no yes on off
208 }
209
210 function isinteger() {
211 local var=${!1}
212
213 [[ ${var} =~ ^[0-9]+$ ]]
214 }
215
216 function ismac() {
217 local mac=${!1}
218
219 mac_is_valid ${mac}
220 }
221
222 function assert() {
223 local assertion="$@"
224
225 if ! ${assertion}; then
226 error_log "Assertion '${assertion}' failed."
227 exit ${EXIT_ERROR}
228 fi
229
230 return ${EXIT_OK}
231 }
232
233 function uppercase() {
234 local input
235 read input
236 echo "${input^^}"
237 }