initial checkin
[grml-scripts.git] / usr_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     eerror "Outgouing device is not set in $CONFIG_FILE"
32     exit 1
33 fi
34
35 case "$OUTDEV" in
36     auto|default)
37     OUTDEV=$(defaultGWDev)
38     ;;
39 esac
40
41 [ -n "$IPTABLES" ] || IPTABLES=/sbin/iptables
42
43 check4progs $IPTABLES || exit 1
44
45 case "$1" in
46     start)
47         einfo "Adjusting kernel variables (net.ipv4.*)"
48           sysctl -w net.ipv4.conf.all.rp_filter=1    1>/dev/null && \
49           sysctl -w net.ipv4.conf.all.log_martians=1 1>/dev/null && \
50           sysctl -w net.ipv4.ip_forward=1            1>/dev/null
51         eend $?
52         einfo "Setting up iptables rule"
53           eindent
54
55             einfo "Adding masquarade rule"
56             $IPTABLES -t nat -A POSTROUTING -o $OUTDEV -j MASQUERADE
57             eend $?
58           eoutdent
59    ;;
60
61    stop)
62        einfo "Resetting kernel variables"
63          sysctl -w net.ipv4.ip_forward=0            1>/dev/null && \
64          sysctl -w net.ipv4.conf.all.log_martians=0 1>/dev/null
65        eend $?
66
67        einfo "Removing iptables rule"
68          $IPTABLES -t nat -D POSTROUTING -o $OUTDEV -j MASQUERADE
69        eend $?
70    ;;
71
72    restart)
73         $0 stop
74         sleep 1
75         $0 start
76    ;;
77
78    info)
79     einfo "$0 - script which turns on router capabilities (NAT)"
80     einfo "Configure it via $CONFIG_FILE" ; eend 0
81    ;;
82
83    *)
84     echo "Usage: $0 {start|stop|restart|info}"
85     exit 1
86    ;;
87 esac
88
89 ## END OF FILE #################################################################
90 # vim: ft=sh expandtab ai