Fix bashism/non-POSIX usage of $UID.
[grml-network.git] / sbin / grml-router
1 #!/bin/sh
2 # Filename:      grml-router
3 # Purpose:       set up your box as NAT-router
4 # Authors:       grml-team (grml.org), Ulrich Dangel <schula@grml.org>, Michael Prokop <mika@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2.
7 ################################################################################
8
9 CONFIG_FILE=/etc/grml/routersetup
10 . /etc/grml/lsb-functions
11 . /etc/grml/net-functions
12 . /etc/grml/script-functions
13
14 check4root || exit 1
15
16 if ! [ -r $CONFIG_FILE ] ; then
17   eerror "$CONFIG_FILE could not be read."
18   exit 1
19 fi
20
21 . $CONFIG_FILE
22
23 if [ -z "$OUTDEV" ] ; then
24     eewarn "Outgoing device is not set in $CONFIG_FILE"
25     eewarn "Setting Outgoing device to auto"
26     OUTDEV=auto
27 fi
28
29 case "$OUTDEV" in
30     auto|default)
31     OUTDEV=$(defaultGWDev)
32     if [ -z "$OUTDEV" ] ; then
33             eerror "The outgoing device could not be determined."
34             eerror "Please adjust OUTDEV in $CONFIG_FILE"
35             exit 1
36     fi
37     ;;
38 esac
39
40 [ -n "$IPTABLES" ] || IPTABLES=/sbin/iptables
41
42 check4progs $IPTABLES || exit 1
43
44 case "$1" in
45     start)
46         einfo "Adjusting kernel variables (net.ipv4.*)"
47           sysctl -w net.ipv4.conf.all.rp_filter=1    1>/dev/null && \
48           sysctl -w net.ipv4.conf.all.log_martians=1 1>/dev/null && \
49           sysctl -w net.ipv4.ip_forward=1            1>/dev/null
50         eend $?
51         einfo "Setting up iptables rule"
52           eindent
53
54             einfo "Adding masquarade rule"
55             $IPTABLES -t nat -A POSTROUTING -o $OUTDEV -j MASQUERADE
56             eend $?
57           eoutdent
58    ;;
59
60    stop)
61        einfo "Resetting kernel variables"
62          sysctl -w net.ipv4.ip_forward=0            1>/dev/null && \
63          sysctl -w net.ipv4.conf.all.log_martians=0 1>/dev/null
64        eend $?
65
66        einfo "Removing iptables rule"
67          $IPTABLES -t nat -D POSTROUTING -o $OUTDEV -j MASQUERADE
68        eend $?
69    ;;
70
71    restart)
72         $0 stop
73         sleep 1
74         $0 start
75    ;;
76
77    info)
78     einfo "$0 - script which turns on router capabilities (NAT)"
79     einfo "Configure it via $CONFIG_FILE" ; eend 0
80    ;;
81
82    *)
83     echo "Usage: $0 {start|stop|restart|info}"
84     exit 1
85    ;;
86 esac
87
88 ## END OF FILE #################################################################
89 # vim: ft=sh expandtab ai