d762adcd4714fea5180be566ff2237698b536d7b
[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 # Latest change: Sam Okt 07 23:18:38 CEST 2006 [mika]
8 ################################################################################
9
10 # exit on any error
11 set -e
12
13 CONFIG_FILE=/etc/grml/routersetup
14 . /etc/grml/lsb-functions
15 . /etc/grml/net-functions
16 . /etc/grml/script-functions
17
18 if [ $UID != 0 ]; then
19    eerror "Error: become root before starting $0"
20    exit 100
21 fi
22
23 if ! [ -r $CONFIG_FILE ] ; then
24   eerror "$CONFIG_FILE could not be read."
25   exit 1
26 fi
27
28 . $CONFIG_FILE
29
30 if [ -z "$OUTDEV" ] ; then
31     eewarn "Outgoing device is not set in $CONFIG_FILE"
32     eewarn "Setting Outgoing device to auto"
33     OUTDEV=auto
34 fi
35
36 case "$OUTDEV" in
37     auto|default)
38     OUTDEV=$(defaultGWDev)
39     if [ -z "$OUTDEV" ] ; then
40             eerror "The outgoing device could not be determined."
41             eerror "Please adjust OUTDEV in $CONFIG_FILE"
42             exit 1
43     fi
44     ;;
45 esac
46
47 [ -n "$IPTABLES" ] || IPTABLES=/sbin/iptables
48
49 check4progs $IPTABLES || exit 1
50
51 case "$1" in
52     start)
53         einfo "Adjusting kernel variables (net.ipv4.*)"
54           sysctl -w net.ipv4.conf.all.rp_filter=1    1>/dev/null && \
55           sysctl -w net.ipv4.conf.all.log_martians=1 1>/dev/null && \
56           sysctl -w net.ipv4.ip_forward=1            1>/dev/null
57         eend $?
58         einfo "Setting up iptables rule"
59           eindent
60
61             einfo "Adding masquarade rule"
62             $IPTABLES -t nat -A POSTROUTING -o $OUTDEV -j MASQUERADE
63             eend $?
64           eoutdent
65    ;;
66
67    stop)
68        einfo "Resetting kernel variables"
69          sysctl -w net.ipv4.ip_forward=0            1>/dev/null && \
70          sysctl -w net.ipv4.conf.all.log_martians=0 1>/dev/null
71        eend $?
72
73        einfo "Removing iptables rule"
74          $IPTABLES -t nat -D POSTROUTING -o $OUTDEV -j MASQUERADE
75        eend $?
76    ;;
77
78    restart)
79         $0 stop
80         sleep 1
81         $0 start
82    ;;
83
84    info)
85     einfo "$0 - script which turns on router capabilities (NAT)"
86     einfo "Configure it via $CONFIG_FILE" ; eend 0
87    ;;
88
89    *)
90     echo "Usage: $0 {start|stop|restart|info}"
91     exit 1
92    ;;
93 esac
94
95 ## END OF FILE #################################################################
96 # vim: ft=sh expandtab ai