]>
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 | ||
27 | # Print a pretty warn message | |
28 | function warning() { | |
29 | echo -e " ${WARN}WARNING${NORMAL}: $@" >&2 | |
30 | } | |
31 | ||
32 | function listsort() { | |
33 | local i | |
34 | for i in $@; do | |
35 | echo "${i}" | |
36 | done | sort | tr "\n" " " | |
37 | } | |
38 | ||
39 | function config_read() { | |
40 | local config_file=${1} | |
41 | ||
42 | if [ -e "${config_file}" ]; then | |
43 | . ${config_file} | |
44 | config_check | |
45 | fi | |
46 | } | |
47 | ||
48 | function config_write() { | |
49 | local config_file=${1} | |
50 | shift | |
51 | ||
52 | # Check if all values to be written are sane | |
53 | config_check | |
54 | ||
55 | > ${config_file} | |
56 | ||
57 | local param | |
58 | for param in $(listsort $@); do | |
59 | echo "${param}=\"${!param}\"" >> ${config_file} | |
60 | done | |
61 | } | |
62 | ||
63 | function config_print() { | |
64 | local param | |
65 | ||
66 | for param in $(listsort $@); do | |
67 | printf "%-16s = %s\n" "${param}" "${!param}" | |
68 | done | |
69 | } | |
70 | ||
71 | function config_check() { | |
72 | # If there is a function defined that is called __check | |
73 | # we call that function | |
74 | [ -n "$(type -t _check)" ] && _check | |
75 | } | |
76 | ||
77 | function network_config_set() { | |
78 | while [ $# -gt 0 ]; do | |
79 | case "${1}" in | |
80 | *=*) | |
81 | eval ${1} | |
82 | ;; | |
83 | *) | |
84 | warning "Invalid parameter given: ${1}" | |
85 | ;; | |
86 | esac | |
87 | shift | |
88 | done | |
89 | ||
90 | # Write configuration to disk | |
91 | network_config_write | |
92 | } | |
93 | ||
94 | function network_config_read() { | |
95 | config_read ${CONFIG_FILE} | |
96 | } | |
97 | ||
98 | function network_config_write() { | |
99 | config_write ${CONFIG_FILE} ${CONFIG_FILE_PARAMS} | |
100 | } | |
101 | ||
102 | function network_config_print() { | |
103 | config_print ${CONFIG_FILE_PARAMS} | |
104 | } | |
105 | ||
106 | # Speedup function to avoid a call of the basename binary | |
107 | function basename() { | |
108 | echo "${1##*/}" | |
109 | } | |
110 | ||
111 | function enabled() { | |
112 | local param=${1} | |
113 | ||
114 | [ "${!param}" = "yes" ] || [ "${!param}" = "on" ] || [ "${!param}" = "1" ] | |
115 | } | |
116 | ||
117 | function mac_generate() { | |
118 | local mac=() | |
119 | for i in $(seq 0 5); do | |
120 | mac[i]="0x$(uuid | cut -c 1-2)" | |
121 | done | |
122 | ||
123 | # Remove multicast bit | |
124 | # and set address is software assigned | |
125 | # XXX must doublecheck if this works | |
126 | mac[0]=$((mac[0] & 0xfe)) | |
127 | mac[0]=$((mac[0] | 0x02)) | |
128 | ||
129 | local output | |
130 | for i in ${mac[*]}; do | |
131 | if [ -n "${output}" ]; then | |
132 | output="${output}:" | |
133 | fi | |
134 | ||
135 | output="${output}$(printf "%02x" ${i})" | |
136 | done | |
137 | ||
138 | # Check if output is valid | |
139 | assert mac_is_valid ${output} | |
140 | ||
141 | echo ${output} | |
142 | } | |
143 | ||
144 | function mac_is_valid() { | |
145 | local mac=${1} | |
146 | ||
147 | [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]] | |
148 | } | |
149 | ||
150 | function uuid() { | |
151 | cat /proc/sys/kernel/random/uuid | |
152 | } | |
153 | ||
154 | function isset() { | |
155 | local var=${1} | |
156 | ||
157 | [ -n "${!var}" ] | |
158 | } | |
159 | ||
160 | function isoneof() { | |
161 | local var=${!1} | |
162 | shift | |
163 | ||
164 | for i in $@; do | |
165 | [ "${var}" = "${i}" ] && return ${EXIT_OK} | |
166 | done | |
167 | ||
168 | return ${EXIT_ERROR} | |
169 | } | |
170 | ||
171 | function isbool() { | |
172 | local var=${1} | |
173 | ||
174 | isoneof ${var} 0 1 no yes on off | |
175 | } | |
176 | ||
177 | function isinteger() { | |
178 | local var=${!1} | |
179 | ||
180 | [[ ${var} =~ ^[0-9]+$ ]] | |
181 | } | |
182 | ||
183 | function ismac() { | |
184 | local mac=${!1} | |
185 | ||
186 | mac_is_valid ${mac} | |
187 | } | |
188 | ||
189 | function assert() { | |
190 | local assertion="$@" | |
191 | ||
192 | if ! ${assertion}; then | |
193 | error "Assertion '${assertion}' failed." | |
194 | exit ${EXIT_ERROR} | |
195 | fi | |
196 | ||
197 | return ${EXIT_OK} | |
198 | } |