1 | 2 | simandl | #!/bin/bash |
2 | | | # author : Petr Simandl www.simandl.cz |
3 | 4 | simandl | # release date : 07/09/2004 |
4 | 2 | simandl | # name : sedlo |
5 | | | # description : dynamic side routing tables tool |
6 | | | # license : GPL |
7 | | | |
8 | 5 | simandl | sl_version="0.0.3pre4" |
9 | 2 | simandl | |
10 | 4 | simandl | PATH=$PATH:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin |
11 | 2 | simandl | |
12 | | | sl_nmcnf="sedlo.conf" |
13 | | | sl_sedlocnf="/etc/$sl_nmcnf" |
14 | | | sl_sedlocache="/var/cache/sedlo" |
15 | | | |
16 | | | sl_rttab="/etc/iproute2/rt_tables" |
17 | | | sl_rtnmin=110 |
18 | 3 | simandl | sl_rtnmax=200 |
19 | | | sl_ipnodef="10.0.0.0/8" |
20 | 2 | simandl | |
21 | 3 | simandl | slm_unknown="Nezname parametry : " |
22 | | | |
23 | | | sl_ipcmd=`which ip` |
24 | | | sl_trcmd=`which tr` |
25 | | | sl_wgetcmd=`which wget` |
26 | 2 | simandl | sl_hnmcmd=`which hostname` |
27 | | | sl_awkcmd=`which awk` |
28 | 3 | simandl | sl_catcmd=`which cat` |
29 | | | sl_grepcmd=`which grep` |
30 | 2 | simandl | |
31 | 5 | simandl | if [ -e $sl_sedlocnf ] |
32 | 2 | simandl | then |
33 | | | sl_nop=1 |
34 | | | else |
35 | 5 | simandl | echo "$sl_sedlocnf not found" |
36 | 2 | simandl | exit 1 |
37 | | | fi |
38 | | | |
39 | 5 | simandl | if [ -e $sl_rttab ] |
40 | | | then |
41 | 2 | simandl | sl_nop=1 |
42 | | | else |
43 | 5 | simandl | echo "$sl_rttab not found" |
44 | 2 | simandl | exit 1 |
45 | | | fi |
46 | | | |
47 | 5 | simandl | sl_murlcfg=`cat $sl_sedlocnf | grep "^mcnf" | uniq | awk '{print $2}'` |
48 | 2 | simandl | |
49 | | | ###################################################################### |
50 | 5 | simandl | s_flqq() |
51 | | | { |
52 | | | if [ $scm_info -gt 0 ]; then echo "Managing rules" ; fi |
53 | | | |
54 | | | |
55 | | | sl_rules=`($sl_ipcmd ru ls | $sl_grepcmd -v "from all lookup" | $sl_awkcmd '{print $3"*ru"}' ; \ |
56 | | | $sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd "^ip" | $sl_awkcmd '{print $2"*ip"}') | sort | uniq` |
57 | | | |
58 | | | sl_merged=`echo $sl_rules $sl_ips | sort | uniq` |
59 | | | |
60 | | | echo $sl_merged |
61 | | | |
62 | | | } # s_flru |
63 | | | ###################################################################### |
64 | 4 | simandl | s_flru() |
65 | | | { |
66 | 5 | simandl | if [ $scm_info -gt 0 ]; then echo "Flushing all rules" ; fi |
67 | 4 | simandl | |
68 | 5 | simandl | sl_rules=`$sl_ipcmd ru ls | $sl_grepcmd -v "from all lookup" | $sl_grepcmd " 10." | $sl_trcmd '[:blank:]' '*'` |
69 | 4 | simandl | |
70 | 5 | simandl | for sl_rule in $sl_rules |
71 | | | do |
72 | | | # sl_ipn=`echo $sl_rule | $sl_awkcmd -F '*' '{print $2}'` |
73 | | | sl_ipgws=`echo $sl_rule | $sl_awkcmd -F '*' '{print $2,$3,$4,$5,$6,$7}'` |
74 | | | $sl_ipcmd ru del $sl_ipgws |
75 | | | done |
76 | | | |
77 | 4 | simandl | } # s_flru |
78 | | | ###################################################################### |
79 | 3 | simandl | s_fillrules() |
80 | 2 | simandl | { |
81 | 5 | simandl | s_flru |
82 | 4 | simandl | |
83 | 3 | simandl | if [ $scm_info -gt 0 ]; then echo "Creating rules" ; fi |
84 | 2 | simandl | |
85 | 5 | simandl | sl_ips=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd "^ip" | $sl_awkcmd '{print $2"*"$4"*"$5"*"$6}'` |
86 | 2 | simandl | |
87 | 5 | simandl | for sl_ip in $sl_ips |
88 | 3 | simandl | do |
89 | 5 | simandl | sl_ipn=`echo $sl_ip | $sl_awkcmd -F '*' '{print $1}'` |
90 | | | sl_ipgws=`echo $sl_ip | $sl_awkcmd -F '*' '{print $2,$3,$4}'` |
91 | | | sl_ok="no" |
92 | 3 | simandl | for sl_ipgw in $sl_ipgws |
93 | 2 | simandl | do |
94 | 5 | simandl | sl_tbl=`$sl_ipcmd ro ls ta $sl_ipgw` |
95 | | | if [ "$sl_tbl x" != " x" ] && [ "$sl_ok" = "no" ] |
96 | 2 | simandl | then |
97 | 5 | simandl | if [ $scm_info -gt 1 ]; then echo "Creating new rules to send $sl_ipn to table $sl_ipgw" ; fi |
98 | | | $sl_ipcmd ru add from $sl_ipn lookup $sl_ipgw |
99 | | | sl_ok="yes" |
100 | 3 | simandl | else |
101 | 5 | simandl | if [ "$sl_ok" = "no" ] |
102 | | | then |
103 | | | if [ $scm_info -gt 1 ]; then echo "For $sl_ipn table $sl_ipgw not used because it is empty" ; fi |
104 | | | else |
105 | | | if [ $scm_info -gt 1 ]; then echo "For $sl_ipn table $sl_ipgw not used because it has lower priority" ; fi |
106 | | | fi |
107 | 3 | simandl | fi |
108 | | | done |
109 | | | done |
110 | 2 | simandl | |
111 | 5 | simandl | $sl_ipcmd ru add from $sl_ipnodef to $sl_ipnodef lookup main |
112 | 4 | simandl | |
113 | 3 | simandl | } # s_fillrules |
114 | | | ###################################################################### |
115 | | | s_filltables() |
116 | | | { |
117 | | | if [ $scm_info -gt 0 ]; then echo "Filling tables" ; fi |
118 | | | |
119 | 5 | simandl | sl_igws=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd "^igw" | $sl_awkcmd '{print $3"*"$2}'` |
120 | 3 | simandl | |
121 | | | for sl_igw in $sl_igws |
122 | | | do |
123 | 5 | simandl | sl_igwn=`echo $sl_igw | $sl_awkcmd -F '*' '{print $1}'` |
124 | | | sl_igwip=`echo $sl_igw | $sl_awkcmd -F '*' '{print $2}'` |
125 | | | sl_igwgt=`$sl_ipcmd ro ls | $sl_grepcmd "^$sl_igwip " | $sl_awkcmd '{print $3}'` |
126 | | | if [ "$sl_igwgt x" = " x" ] |
127 | | | then |
128 | | | if [ $scm_info -gt 1 ]; then echo "Route not found for igw $sl_igwn - leaving table as is" ; fi |
129 | | | else |
130 | | | sl_tbl=`$sl_ipcmd ro ls ta $sl_igwn` |
131 | | | if [ "$sl_tbl x" != " x" ] |
132 | | | then |
133 | | | $sl_ipcmd ro fl ta $sl_igwn |
134 | | | fi |
135 | | | $sl_ipcmd ro add 0.0.0.0/1 via $sl_igwgt ta $sl_igwn |
136 | | | $sl_ipcmd ro add 128.0.0.0/1 via $sl_igwgt ta $sl_igwn |
137 | | | if [ $scm_info -gt 1 ]; then echo "Table filled for igw $sl_igwn" ; fi |
138 | 2 | simandl | fi |
139 | 3 | simandl | done |
140 | 2 | simandl | |
141 | 3 | simandl | } # s_filltables |
142 | | | ###################################################################### |
143 | | | s_mktables() |
144 | | | { |
145 | | | if [ $scm_info -gt 0 ]; then echo "Creating tables " ; fi |
146 | 5 | simandl | sl_igws=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd "^igw" | $sl_awkcmd '{print $3}'` |
147 | 3 | simandl | for sl_igw in $sl_igws |
148 | | | do |
149 | 5 | simandl | sl_igwrttb=`$sl_catcmd $sl_rttab | $sl_awkcmd '{print $2}' | $sl_grepcmd $sl_igw ` |
150 | | | if [ "$sl_igwrttb x" = " x" ] |
151 | 3 | simandl | then |
152 | 5 | simandl | if [ $scm_info -gt 1 ]; then echo "Creating table for $sl_igw" ; fi |
153 | | | sl_cnt="$sl_rtnmax" |
154 | 3 | simandl | sl_ok="no" |
155 | | | until [ "$sl_cnt" -eq "$sl_rtnmin" ] || [ "$sl_ok" = "yes" ] |
156 | | | do |
157 | 5 | simandl | #space is used to recognized two and three digit numbers |
158 | | | sl_igwrttb=`cat $sl_rttab | awk '{print $1" "}' | grep "$sl_cnt " ` |
159 | | | if [ "$sl_igwrttb x" = " x" ] |
160 | 3 | simandl | then |
161 | | | sl_ok="yes" |
162 | | | echo "$sl_cnt $sl_igw" >> $sl_rttab |
163 | | | fi |
164 | | | sl_cnt=$(($sl_cnt - 1 )) |
165 | | | done |
166 | | | else |
167 | 5 | simandl | if [ $scm_info -gt 1 ]; then echo "Table found for $sl_igw no action taken" ; fi |
168 | 3 | simandl | fi |
169 | | | |
170 | 2 | simandl | done |
171 | 3 | simandl | } # s_mktables |
172 | 2 | simandl | ###################################################################### |
173 | | | s_getcfg() |
174 | | | { |
175 | 3 | simandl | if [ $scm_info -gt 0 ]; then echo "Getting config" ; fi |
176 | 5 | simandl | if [ $scm_info -gt 1 ]; then echo "Using main config $sl_murlcfg" ; fi |
177 | 3 | simandl | if [ $scm_info -gt 1 ]; then echo "Using local config $sl_sedlocnf" ; fi |
178 | 4 | simandl | |
179 | 5 | simandl | wget $sl_murlcfg -O "$sl_sedlocache/$sl_nmcnf.main.tmp" -q |
180 | 4 | simandl | |
181 | | | if [ -s $sl_sedlocache/$sl_nmcnf.main.tmp ] |
182 | 3 | simandl | then |
183 | 4 | simandl | date > $sl_sedlocache/last_getcnf.txt |
184 | | | cp $sl_sedlocache/$sl_nmcnf.main.tmp $sl_sedlocache/$sl_nmcnf.main |
185 | | | if [ $scm_info -gt 1 ]; then echo "Main config accepted" ; fi |
186 | 5 | simandl | else |
187 | 4 | simandl | if [ $scm_info -gt 1 ]; then echo "Main config not accepted - using cached config" ; fi |
188 | 5 | simandl | echo -n "Main config not found " > $sl_sedlocache/last_getcnf.txt |
189 | 4 | simandl | date >> $sl_sedlocache/last_getcnf.txt |
190 | 3 | simandl | fi |
191 | 2 | simandl | |
192 | 5 | simandl | # preparing cached config from local and main |
193 | | | # the local config should be processed as the second to have |
194 | | | # higher priority for rules from local config |
195 | 4 | simandl | |
196 | 5 | simandl | echo "# generated file" > $sl_sedlocache/$sl_nmcnf |
197 | | | for sl_file in `ls $sl_sedlocache/$sl_nmcnf.main ; ls $sl_sedlocnf` |
198 | | | do |
199 | | | cat $sl_file | grep "^mcnf" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2}' >> $sl_sedlocache/$sl_nmcnf |
200 | | | cat $sl_file | grep "^igw" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2"\t"$3}' >> $sl_sedlocache/$sl_nmcnf |
201 | | | cat $sl_file | grep "^ip" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6}' >> $sl_sedlocache/$sl_nmcnf |
202 | | | done |
203 | 4 | simandl | #cat $sl_sedlocache/$sl_nmcnf | sort | uniq > $sl_sedlocache/$sl_nmcnf.uniq |
204 | | | #mv $sl_sedlocache/$sl_nmcnf.uniq $sl_sedlocache/$sl_nmcnf |
205 | 5 | simandl | |
206 | 2 | simandl | } |
207 | | | ###################################################################### |
208 | | | s_version() |
209 | | | { |
210 | | | echo sedlo $sl_version |
211 | 3 | simandl | } # s_version |
212 | 2 | simandl | ###################################################################### |
213 | 3 | simandl | s_report() |
214 | | | { |
215 | | | echo "##### SEDLO #####" |
216 | 4 | simandl | echo "date : `date`" |
217 | 3 | simandl | echo "version : $sl_version" |
218 | | | echo "local_config : $sl_sedlocnf" |
219 | 5 | simandl | echo "main_config : $sl_murlcfg" |
220 | 4 | simandl | echo "last update : `cat $sl_sedlocache/last_getcnf.txt`" |
221 | 3 | simandl | echo "##### TABLES #####" |
222 | 5 | simandl | cat $sl_rttab |
223 | 3 | simandl | echo "##### RULES #####" |
224 | 5 | simandl | $sl_ipcmd ru ls |
225 | 3 | simandl | } # s_report |
226 | | | ###################################################################### |
227 | 2 | simandl | s_help() |
228 | | | { |
229 | 5 | simandl | echo Pouziti: sedlo [param] |
230 | | | echo param: |
231 | | | echo -V vypise verzi |
232 | | | echo -help vypise napovedu |
233 | | | echo -v malo upovidany |
234 | | | echo -vv hodne upovidany |
235 | | | echo -nogetcfg zajisti ze se nedude znovu nacitat konfigurace a pouzije se predchozi z cache |
236 | | | echo -report vypise prehled pravidel a tabulek |
237 | | | echo -flru odstrani vsechny pravidla |
238 | 3 | simandl | } # s_help |
239 | 2 | simandl | ###################################################################### |
240 | | | ###################################################################### |
241 | | | |
242 | 3 | simandl | sl_unknown="" |
243 | | | scm_nogetcfg=0 |
244 | 4 | simandl | scm_flru=0 |
245 | 3 | simandl | scm_info=0 |
246 | 2 | simandl | |
247 | 3 | simandl | # parsing input parameters |
248 | | | while [ "a$1" != "a" ] |
249 | | | do |
250 | | | case $1 in |
251 | 4 | simandl | -V) |
252 | | | s_version |
253 | | | exit 0 |
254 | 3 | simandl | ;; |
255 | | | -h) |
256 | 4 | simandl | s_help |
257 | | | exit 0 |
258 | 3 | simandl | ;; |
259 | | | -report) |
260 | 4 | simandl | s_report |
261 | | | exit 0 |
262 | 3 | simandl | ;; |
263 | 4 | simandl | -flru) |
264 | | | scm_flru=1 |
265 | | | shift |
266 | | | ;; |
267 | 3 | simandl | -help) |
268 | 4 | simandl | s_help |
269 | | | exit 0 |
270 | 3 | simandl | ;; |
271 | | | -nogetcfg) |
272 | | | scm_nogetcfg=1 |
273 | | | shift |
274 | | | ;; |
275 | 4 | simandl | -v) |
276 | 3 | simandl | scm_info=1 |
277 | | | shift |
278 | | | ;; |
279 | 4 | simandl | -vv) |
280 | 3 | simandl | scm_info=2 |
281 | | | shift |
282 | | | ;; |
283 | | | *) |
284 | | | sl_unknown="$sl_unknown$1 " |
285 | | | shift |
286 | | | esac |
287 | | | done |
288 | | | |
289 | | | # printing the list of bad parameters (if there are some) |
290 | | | if [ "a$sl_unknown" != "a" ] |
291 | | | then |
292 | | | echo "$slm_unknown $sl_unknown" |
293 | | | s_help |
294 | 4 | simandl | exit 0 |
295 | 3 | simandl | fi |
296 | | | |
297 | 4 | simandl | if [ $scm_flru -eq 1 ] |
298 | | | then |
299 | 5 | simandl | s_flqq |
300 | | | exit 0 |
301 | 4 | simandl | fi |
302 | | | |
303 | 3 | simandl | if [ $scm_nogetcfg -eq 0 ] |
304 | | | then |
305 | | | s_getcfg |
306 | | | fi |
307 | | | |
308 | 2 | simandl | s_mktables |
309 | 3 | simandl | s_filltables |
310 | | | s_fillrules |
311 | | | |
312 | 2 | simandl | exit 0 |
313 | 5 | simandl | |