gateway: formatting
[olsrd.git] / openwrt / olsrd-stable-git / files / olsrd.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2008  Alina Friedrichsen <x-alina@gmx.net>
3 # Special thanks to bittorf wireless ))
4 START=65
5
6 OLSRD_OLSRD_SCHEMA='ignore:internal config_file:internal DebugLevel=0 AllowNoInt=yes'
7 OLSRD_IPCCONNECT_SCHEMA='ignore:internal Host:list Net:list2'
8 OLSRD_LOADPLUGIN_SCHEMA='ignore:internal library:internal Host4:list Net4:list2 Host:list Net:list2 Host6:list Net6:list2 Ping:list redistribute:list NonOlsrIf:list name:list lat lon latlon_infile HNA:list2 hosts:list2'
9 OLSRD_INTERFACE_SCHEMA='ignore:internal interface:internal AutoDetectChanges:bool'
10 OLSRD_INTERFACE_DEFAULTS_SCHEMA='AutoDetectChanges:bool'
11
12 T='     '
13 N='
14 '
15
16 validate_varname() {
17         local varname="$1"
18         [ -z "$varname" -o "$varname" != "${varname%%[!A-Za-z0-9_]*}" ] && return 1
19         return 0
20 }
21
22 validate_ifname() {
23         local ifname="$1"
24         [ -z "$ifname" -o "$ifname" != "${ifname%%[!A-Za-z0-9.:_-]*}" ] && return 1
25         return 0
26 }
27
28 validate_olsrd_option() {
29         local str="$1"
30         [ -z "$str" -o "$str" != "${str%%[!     0-9A-Za-z./|:_-]*}" ] && return 1
31         return 0
32 }
33
34 get_ifname() {
35         IFNAME=
36         local interface="$1"
37         validate_varname "$interface" || return 1
38         local ifname
39
40         config_get ifname "$interface" ifname
41         validate_ifname "$ifname" || return 1
42         IFNAME="$ifname"
43
44         return 0
45 }
46
47 system_config() {
48         local cfg="$1"
49         local cfgt
50         local hostname
51         local latlon
52
53         config_get cfgt "$cfg" TYPE
54
55         if [ "$cfgt" = "system" ]; then
56                 config_get hostname "$cfg" hostname
57                 hostname="${hostname:-OpenWrt}"
58                 SYSTEM_HOSTNAME="$hostname"
59         fi
60
61         if [ -z "$SYSTEM_LAT" -o -z "$SYSTEM_LON" ]; then
62                 config_get latlon "$cfg" latlon
63                 IFS=" ${T}${N},"
64                 set -- $latlon
65                 unset IFS
66                 SYSTEM_LAT="$1"
67                 SYSTEM_LON="$2"
68         fi
69
70         if [ -z "$SYSTEM_LAT" -o -z "$SYSTEM_LON" ]; then
71                 config_get latlon "$cfg" latitude
72                 SYSTEM_LAT="$latlon"
73                 config_get latlon "$cfg" longitude
74                 SYSTEM_LON="$latlon"
75         fi
76 }
77
78 olsrd_find_config_file() {
79         local cfg="$1"
80         validate_varname "$cfg" || return 0
81
82         config_get_bool ignore "$cfg" ignore 0
83         [ "$ignore" -ne 0 ] && return 0
84         config_get OLSRD_CONFIG_FILE "$cfg" config_file
85
86         return 0
87 }
88
89 warning_invalid_value() {
90         local package="$1"
91         validate_varname "$package" || package=
92         local config="$2"
93         validate_varname "$config" || config=
94         local option="$3"
95         validate_varname "$option" || option=
96
97         echo -n "Warning: Invalid value" 1>&2
98
99         if [ -n "$package" -a -n "$config" ]; then
100                 echo -n " in option '$package.$config${option:+.}$option'" 1>&2
101         fi
102
103         echo ", skipped" 1>&2
104
105         return 0
106 }
107
108 olsrd_write_option() {
109         local param="$1"
110         local cfg="$2"
111         validate_varname "$cfg" || return 1
112         local option="$3"
113         validate_varname "$option" || return 1
114         local value="$4"
115         local option_type="$5"
116
117         if [ "$option_type" = bool ]; then
118                 case "$value" in
119                         1|on|true|enabled|yes) value=yes;;
120                         0|off|false|disabled|no) value=no;;
121                         *) warning_invalid_value olsrd "$cfg" "$option"; return 1;;
122                 esac
123         fi
124
125         if ! validate_olsrd_option "$value"; then
126                 warning_invalid_value olsrd "$cfg" "$option"
127                 return 1
128         fi
129
130         if [ "$value" != "${value%%[G-Zg-z_-]*}" ]; then
131                 if [ "$option" != "Ip6AddrType" -a "$option" != "LinkQualityMult" -a "$value" != "yes" -a "$value" != "no" ]; then
132                         value="\"$value\""
133                 fi
134         fi
135
136         echo -n "${N}$param$option $value"
137
138         return 0
139 }
140
141 olsrd_write_plparam() {
142         local param="$1"
143         local cfg="$2"
144         validate_varname "$cfg" || return 1
145         local option="$3"
146         validate_varname "$option" || return 1
147         local value="$4"
148         local option_type="$5"
149         local _option
150
151         if [ "$option_type" = bool ]; then
152                 case "$value" in
153                         1|on|true|enabled|yes) value=yes;;
154                         0|off|false|disabled|no) value=no;;
155                         *) warning_invalid_value olsrd "$cfg" "$option"; return 1;;
156                 esac
157         fi
158
159         if ! validate_olsrd_option "$value"; then
160                 warning_invalid_value olsrd "$cfg" "$option"
161                 return 1
162         fi
163
164         IFS='-_'
165         set -- $option
166         option="$*"
167         unset IFS
168         _option="$option"
169         if [ "$option" = 'hosts' ]; then
170                 set -- $value
171                 option="$1"
172                 shift
173                 value="$*"
174         fi
175         if [ "$option" = 'NonOlsrIf' ]; then
176                 if validate_varname "$value"; then
177                         if get_ifname "$value"; then
178                                 ifname="$IFNAME"
179                                 echo "Info: mdns Interface '$value' ifname '$ifname' found" 1>&2
180                         else
181                                 echo "Warning: mdns Interface '$value' not found, skipped" 1>&2
182                         fi
183                 else
184                         warning_invalid_value olsrd "$cfg" "NonOlsrIf"
185                 fi
186                 [ -z "$ifname" ] || value=$ifname
187         fi
188
189         echo -n "${N}${param}PlParam \"$option\" \"$value\""
190
191         return 0
192 }
193
194 config_update_schema() {
195         unset IFS
196         local schema_varname="$1"
197         validate_varname "$schema_varname" || return 1
198         local command="$2"
199         validate_varname "$command" || return 1
200         local option="$3"
201         validate_varname "$option" || return 1
202         local value="$4"
203         local schema
204         local cur_option
205
206         case "$varname" in
207                 *_LENGTH) return 0;;
208                 *_ITEM*) return 0;;
209         esac
210
211         eval "export -n -- \"schema=\${$schema_varname}\""
212
213         for cur_option in $schema; do
214                 [ "${cur_option%%[:=]*}" = "$option" ] && return 0
215         done
216
217         if [ "$command" = list ]; then
218                 set -- $value
219                 if [ "$#" -ge "3" ]; then
220                         schema_entry="$option:list3"
221                 elif [ "$#" -ge "2" ]; then
222                         schema_entry="$option:list2"
223                 else
224                         schema_entry="$option:list"
225                 fi
226         else
227                 schema_entry="$option"
228         fi
229
230         append "$schema_varname" "$schema_entry"
231
232         return 0
233 }
234
235 config_write_options() {
236         unset IFS
237         local schema="$1"
238         local cfg="$2"
239         validate_varname "$cfg" || return 1
240         local write_func="$3"
241         [ -z "$write_func" ] && output_func=echo
242         local write_param="$4"
243         local schema_entry
244         local option
245         local option_length
246         local option_type
247         local default
248         local value
249         local list_size
250         local list_item
251         local list_value
252         local i
253         local position
254
255         for schema_entry in $schema; do
256                 default="${schema_entry#*[=]}"
257                 [ "$default" = "$schema_entry" ] && default=
258                 option="${schema_entry%%[=]*}"
259                 IFS=':'
260                 set -- $option
261                 unset IFS
262                 option="$1"
263                 option_type="$2"
264                 validate_varname "$option" || continue
265                 [ -z "$option_type" ] || validate_varname "$option_type" || continue
266                 [ "$option_type" = internal ] && continue
267                 config_get value "$cfg" "$option"
268
269                 if [ -z "$value" ]; then
270                         IFS='+'
271                         set -- $default
272                         unset IFS
273                         value=$*
274                 elif [ "$value" = '-' -a -n "$default" ]; then
275                         continue
276                 fi
277
278                 [ -z "$value" ] && continue
279
280                 case "$option_type" in
281                         list) list_size=1;;
282                         list2) list_size=2;;
283                         list3) list_size=3;;
284                         *) list_size=0;;
285                 esac
286
287                 if [ "$list_size" -gt 0 ]; then
288                         config_get option_length "$cfg" "${option}_LENGTH"
289                         if [ -n "$option_length" ]; then
290                                 i=1
291                                 while [ "$i" -le "$option_length" ]; do
292                                         config_get list_value "$cfg" "${option}_ITEM$i"
293                                         "$write_func" "$write_param" "$cfg" "$option" "$list_value" "$option_type" || break
294                                         i=$((i + 1))
295                                 done
296                         else
297                                 list_value=
298                                 i=0
299                                 for list_item in $value; do
300                                         append "list_value" "$list_item"
301                                         i=$((i + 1))
302                                         position=$((i % list_size))
303                                         if [ "$position" -eq 0 ]; then
304                                                 "$write_func" "$write_param" "$cfg" "$option" "$list_value" "$option_type" || break
305                                                 list_value=
306                                         fi
307                                 done
308                                 [ "$position" -ne 0 ] && "$write_func" "$write_param" "$cfg" "$option" "$list_value" "$option_type"
309                         fi
310                 else
311                         "$write_func" "$write_param" "$cfg" "$option" "$value" "$option_type"
312                 fi
313         done
314
315         return 0
316 }
317
318 olsrd_write_olsrd() {
319         local cfg="$1"
320         validate_varname "$cfg" || return 0
321         local ignore
322         local ipversion
323
324         config_get_bool ignore "$cfg" ignore 0
325         [ "$ignore" -ne 0 ] && return 0
326
327         [ "$OLSRD_COUNT" -gt 0 ] && return 0
328
329         config_get ipversion "$cfg" IpVersion
330         if [ "$ipversion" = "6and4" ]; then
331                 OLSRD_IPVERSION_6AND4=1
332                 config_set "$cfg" IpVersion '6'
333         fi
334
335         config_write_options "$OLSRD_OLSRD_SCHEMA" "$cfg" olsrd_write_option
336         echo
337         OLSRD_COUNT=$((OLSRD_COUNT + 1))
338
339         return 0
340 }
341
342 olsrd_write_ipcconnect() {
343         local cfg="$1"
344         validate_varname "$cfg" || return 0
345         local ignore
346
347         config_get_bool ignore "$cfg" ignore 0
348         [ "$ignore" -ne 0 ] && return 0
349
350         [ "$IPCCONNECT_COUNT" -gt 0 ] && return 0
351
352         echo -n "${N}IpcConnect${N}{"
353         config_write_options "$OLSRD_IPCCONNECT_SCHEMA" "$cfg" olsrd_write_option "${T}"
354         echo "${N}}"
355         IPCCONNECT_COUNT=$((IPCCONNECT_COUNT + 1))
356
357         return 0
358 }
359
360 olsrd_write_hna4() {
361         local cfg="$1"
362         validate_varname "$cfg" || return 0
363         local ignore
364
365         config_get_bool ignore "$cfg" ignore 0
366         [ "$ignore" -ne 0 ] && return 0
367
368         config_get netaddr "$cfg" netaddr
369         if ! validate_olsrd_option "$netaddr"; then
370                 warning_invalid_value olsrd "$cfg" "netaddr"
371                 return 0
372         fi
373
374         config_get netmask "$cfg" netmask
375         if ! validate_olsrd_option "$netmask"; then
376                 warning_invalid_value olsrd "$cfg" "netmask"
377                 return 0
378         fi
379
380         [ "$HNA4_COUNT" -le 0 ] && echo -n "${N}Hna4${N}{"
381         echo -n "${N}${T}${T}$netaddr $netmask"
382         HNA4_COUNT=$((HNA4_COUNT + 1))
383
384         return 0
385 }
386
387 olsrd_write_hna6() {
388         local cfg="$1"
389         validate_varname "$cfg" || return 0
390         local ignore
391
392         config_get_bool ignore "$cfg" ignore 0
393         [ "$ignore" -ne 0 ] && return 0
394
395         config_get netaddr "$cfg" netaddr
396         if ! validate_olsrd_option "$netaddr"; then
397                 warning_invalid_value olsrd "$cfg" "netaddr"
398                 return 0
399         fi
400
401         config_get prefix "$cfg" prefix
402         if ! validate_olsrd_option "$prefix"; then
403                 warning_invalid_value olsrd "$cfg" "prefix"
404                 return 0
405         fi
406
407         [ "$HNA6_COUNT" -le 0 ] && echo -n "${N}Hna6${N}{"
408         echo -n "${N}${T}${T}$netaddr $prefix"
409         HNA6_COUNT=$((HNA6_COUNT + 1))
410
411         return 0
412 }
413
414 olsrd_write_loadplugin() {
415         local cfg="$1"
416         validate_varname "$cfg" || return 0
417         local ignore
418         local name
419         local suffix
420         local lat
421         local lon
422         local latlon_infile
423
424         config_get_bool ignore "$cfg" ignore 0
425         [ "$ignore" -ne 0 ] && return 0
426
427         config_get library "$cfg" library
428         if ! validate_olsrd_option "$library"; then
429                 warning_invalid_value olsrd "$cfg" "library"
430                 return 0
431         fi
432         if ! [ -x "/lib/$library" -o -x "/usr/lib/$library" -o -x "/usr/local/lib/$library" ]; then
433                 echo "Warning: Plugin library '$library' not found, skipped" 1>&2
434                 return 0
435         fi
436
437         case "$library" in
438                 olsrd_nameservice.*)
439                         config_get name "$cfg" name
440                         [ -z "$name" ] && config_set "$cfg" name $SYSTEM_HOSTNAME
441
442                         config_get suffix "$cfg" suffix
443                         [ -z "$suffix" ] && config_set "$cfg" suffix '.olsr'
444
445                         config_get lat "$cfg" lat
446                         config_get lon "$cfg" lon
447                         config_get latlon_infile "$cfg" latlon_infile
448                         if [ \( -z "$lat" -o -z "$lat" \) -a -z "$latlon_infile" ]; then
449                                 if [ -f '/var/run/latlon.txt' ]; then
450                                         config_set "$cfg" lat ''
451                                         config_set "$cfg" lon ''
452                                         config_set "$cfg" latlon_infile '/var/run/latlon.txt'
453                                 else
454                                         config_set "$cfg" lat "$SYSTEM_LAT"
455                                         config_set "$cfg" lon "$SYSTEM_LON"
456                                 fi
457                         fi
458
459                         config_get latlon_file "$cfg" latlon_file
460                         [ -z "$latlon_file" ] && config_set "$cfg" latlon_file '/var/run/latlon.js'
461                 ;;
462         esac
463
464         echo -n "${N}LoadPlugin \"$library\"${N}{"
465         config_write_options "$OLSRD_LOADPLUGIN_SCHEMA" "$cfg" olsrd_write_plparam "${T}"
466         echo "${N}}"
467
468         return 0
469 }
470
471 olsrd_write_interface() {
472         local cfg="$1"
473         validate_varname "$cfg" || return 0
474         local ignore
475         local interfaces
476         local interface
477         local ifnames
478
479         config_get_bool ignore "$cfg" ignore 0
480         [ "$ignore" -ne 0 ] && return 0
481
482         ifnames=
483         config_get interfaces "$cfg" interface
484         for interface in $interfaces; do
485                 if validate_varname "$interface"; then
486                         if get_ifname "$interface"; then
487                                 ifnames="$ifnames \"$IFNAME\""
488                         else
489                                 echo "Warning: Interface '$interface' not found, skipped" 1>&2
490                         fi
491                 else
492                         warning_invalid_value olsrd "$cfg" "interface"
493                 fi
494         done
495
496         [ -z "$ifnames" ] && return 0
497
498         echo -n "${N}Interface$ifnames${N}{"
499         config_write_options "$OLSRD_INTERFACE_SCHEMA" "$cfg" olsrd_write_option "${T}"
500         echo "${N}}"
501         INTERFACES_COUNT=$((INTERFACES_COUNT + 1))
502
503         return 0
504 }
505
506 olsrd_write_interface_defaults() {
507         local cfg="$1"
508         validate_varname "$cfg" || return 0
509
510         echo -n "${N}InterfaceDefaults$ifnames${N}{"
511         config_write_options "$OLSRD_INTERFACE_DEFAULTS_SCHEMA" "$cfg" olsrd_write_option "${T}"
512         echo "${N}}"
513
514         return 1
515 }
516
517 olsrd_update_schema() {
518         local command="$1"
519         validate_varname "$command" || return 0
520         local varname="$2"
521         validate_varname "$varname" || return 0
522         local value="$3"
523         local cfg="$CONFIG_SECTION"
524         local cfgt
525         local cur_varname
526
527         config_get cfgt "$cfg" TYPE
528         case "$cfgt" in
529                 olsrd) config_update_schema OLSRD_OLSRD_SCHEMA "$command" "$varname" "$value";;
530                 IpcConnect) config_update_schema OLSRD_IPCCONNECT_SCHEMA "$command" "$varname" "$value";;
531                 LoadPlugin) config_update_schema OLSRD_LOADPLUGIN_SCHEMA "$command" "$varname" "$value";;
532                 Interface) config_update_schema OLSRD_INTERFACE_SCHEMA "$command" "$varname" "$value";;
533                 InterfaceDefaults) config_update_schema OLSRD_INTERFACE_DEFAULTS_SCHEMA "$command" "$varname" "$value";;
534         esac
535
536         return 0
537 }
538
539 olsrd_write_config() {
540         OLSRD_IPVERSION_6AND4=0
541         OLSRD_COUNT=0
542         config_foreach olsrd_write_olsrd olsrd
543         IPCCONNECT_COUNT=0
544         config_foreach olsrd_write_ipcconnect IpcConnect
545         HNA4_COUNT=0
546         config_foreach olsrd_write_hna4 Hna4
547         [ "$HNA4_COUNT" -gt 0 ] && echo "${N}}"
548         HNA6_COUNT=0
549         config_foreach olsrd_write_hna6 Hna6
550         [ "$HNA6_COUNT" -gt 0 ] && echo "${N}}"
551         config_foreach olsrd_write_loadplugin LoadPlugin
552         INTERFACES_COUNT=0
553         config_foreach olsrd_write_interface_defaults InterfaceDefaults
554         config_foreach olsrd_write_interface Interface
555         echo
556
557         return 0
558 }
559
560 start() {
561         SYSTEM_HOSTNAME=
562         SYSTEM_LAT=
563         SYSTEM_LON=
564         config_load system
565         config_foreach system_config system
566
567         option_cb() {
568                 olsrd_update_schema "option" "$@"
569         }
570
571         list_cb() {
572                 olsrd_update_schema "list" "$@"
573         }
574
575         include /lib/network
576         scan_interfaces
577         config_load olsrd
578         reset_cb
579
580         OLSRD_CONFIG_FILE=
581         config_foreach olsrd_find_config_file olsrd
582
583         if [ -z "$OLSRD_CONFIG_FILE" ]; then
584                 mkdir -p -- /var/etc/
585                 olsrd_write_config > /var/etc/olsrd.conf
586                 if [ "$INTERFACES_COUNT" -gt 0 -a "$OLSRD_COUNT" -gt 0 ]; then
587                         OLSRD_CONFIG_FILE=/var/etc/olsrd.conf
588                 fi
589         fi
590
591         [ -z "$OLSRD_CONFIG_FILE" ] && return 1
592
593         local bindv6only='0'
594         if [ "$OLSRD_IPVERSION_6AND4" -ne 0 ]; then
595                 bindv6only="$(sysctl -n net.ipv6.bindv6only)"
596                 sysctl -w net.ipv6.bindv6only=1
597                 sed -e 's/^\t\t[0-9.]*[ ][0-9.]*$//' < "$OLSRD_CONFIG_FILE" > /var/etc/olsrd.conf.ipv6
598                 sed -i '/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/d' /var/etc/olsrd.conf.ipv6
599                 olsrd -f /var/etc/olsrd.conf.ipv6 -nofork < /dev/null > /dev/null &
600
601                 sed -e 's/^IpVersion[ ][ ]*6$/IpVersion 4/' -e 's/^\t\t[A-Fa-f0-9.:]*[:][A-Fa-f0-9.:]*[ ][0-9]*$//' < "$OLSRD_CONFIG_FILE" > /var/etc/olsrd.conf.ipv4
602                 olsrd -f /var/etc/olsrd.conf.ipv4 -nofork < /dev/null > /dev/null &
603                 sleep 3
604                 sysctl -w net.ipv6.bindv6only="$bindv6only"
605         else
606                 olsrd -f "$OLSRD_CONFIG_FILE" -nofork < /dev/null > /dev/null &
607         fi
608 }
609
610 stop() {
611         killall olsrd
612 }