]>
Commit | Line | Data |
---|---|---|
1848564d MT |
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 " ${FAIL}ERROR${NORMAL} : $@" >&2 | |
25 | } | |
26 | ||
1b7a1578 MT |
27 | function error_log() { |
28 | error "$@" | |
29 | log ERROR "$@" | |
30 | } | |
31 | ||
1848564d MT |
32 | # Print a pretty warn message |
33 | function warning() { | |
34 | echo -e " ${WARN}WARNING${NORMAL}: $@" >&2 | |
35 | } | |
36 | ||
1b7a1578 MT |
37 | function warning_log() { |
38 | warning "$@" | |
39 | log WARNING "$@" | |
40 | } | |
41 | ||
1848564d MT |
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 | ||
1b7a1578 MT |
65 | log DEBUG "Writing configuration file ${config_file}." |
66 | ||
1848564d MT |
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 | *=*) | |
1b7a1578 MT |
93 | log INFO "Setting configuration option '${1}'". |
94 | eval ${1} | |
1848564d MT |
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]="0x$(uuid | cut -c 1-2)" | |
134 | done | |
135 | ||
136 | # Remove multicast bit | |
137 | # and set address is software assigned | |
138 | # XXX must doublecheck if this works | |
139 | mac[0]=$((mac[0] & 0xfe)) | |
140 | mac[0]=$((mac[0] | 0x02)) | |
141 | ||
142 | local output | |
143 | for i in ${mac[*]}; do | |
144 | if [ -n "${output}" ]; then | |
145 | output="${output}:" | |
146 | fi | |
147 | ||
148 | output="${output}$(printf "%02x" ${i})" | |
149 | done | |
150 | ||
151 | # Check if output is valid | |
152 | assert mac_is_valid ${output} | |
153 | ||
154 | echo ${output} | |
155 | } | |
156 | ||
157 | function mac_is_valid() { | |
158 | local mac=${1} | |
159 | ||
160 | [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]] | |
161 | } | |
162 | ||
163 | function uuid() { | |
164 | cat /proc/sys/kernel/random/uuid | |
165 | } | |
166 | ||
167 | function isset() { | |
168 | local var=${1} | |
169 | ||
170 | [ -n "${!var}" ] | |
171 | } | |
172 | ||
173 | function isoneof() { | |
174 | local var=${!1} | |
175 | shift | |
176 | ||
177 | for i in $@; do | |
178 | [ "${var}" = "${i}" ] && return ${EXIT_OK} | |
179 | done | |
180 | ||
181 | return ${EXIT_ERROR} | |
182 | } | |
183 | ||
184 | function isbool() { | |
185 | local var=${1} | |
186 | ||
187 | isoneof ${var} 0 1 no yes on off | |
188 | } | |
189 | ||
190 | function isinteger() { | |
191 | local var=${!1} | |
192 | ||
193 | [[ ${var} =~ ^[0-9]+$ ]] | |
194 | } | |
195 | ||
196 | function ismac() { | |
197 | local mac=${!1} | |
198 | ||
199 | mac_is_valid ${mac} | |
200 | } | |
201 | ||
202 | function assert() { | |
203 | local assertion="$@" | |
204 | ||
205 | if ! ${assertion}; then | |
4c670d7c | 206 | error_log "Assertion '${assertion}' failed." |
1848564d MT |
207 | exit ${EXIT_ERROR} |
208 | fi | |
209 | ||
210 | return ${EXIT_OK} | |
211 | } |