Add default tinc-up and tinc-down scripts for a Debian system. These
[oweals/tinc.git] / debian / tinc-up
1 #! /usr/bin/perl -w
2 #
3 # Device configuration script for tinc
4 # $Id: tinc-up,v 1.1.2.1 2000/11/24 16:52:57 zarq Exp $
5 #
6 # Based on Lubomir Bulej's Redhat init script.
7 #
8 # This file is called after the tap device is opened by tinc.  The
9 # environment variable IFNAME contains the name of the device; NETNAME
10 # contains the name of the network that was started.
11
12 my $IFNAME=$ENV{"IFNAME"};
13 my $NETNAME=$ENV{"NETNAME"};
14
15
16 ##############################################################################
17 # vpn_load ()           Loads VPN configuration
18
19 # $_[0] ... VPN to load
20
21 sub vpn_load {
22     my @addr;
23     $CFG="$TCONF/$_[0]/tinc.conf";
24     if(! open($CFG, "< $CFG")) {
25         warn "tinc: $CFG does not exist\n";
26         return 0;
27     }
28
29     # load TINCD config
30     while(<$CFG>) {
31         if( /^[ ]*TapDevice[ =]+([^ \#]+)/i ) {
32             $DEV=$1;
33             chomp($DEV);
34             $DEV =~ s/^.*\/([^\/0-9]+)([0-9]+)$/$1$2/;
35             $NUM = $2;
36         } elsif ( /^[ ]*(MyOwnVPNIP|MyVirtualIP)[ =]+([^ \#]+)/i ) {
37             $VPN=$2;
38             chomp($VPN);
39         } elsif ( /^[ ]*VpnMask[ =]+([^ \#]+)/i ) {
40             $VPNMASK=$1;
41             chomp($VPNMASK);
42         }
43     }
44     if(!defined($DEV)) {
45         warn "tinc: There must be a TapDevice\n";
46         return 0;
47     }
48     if($DEV eq "") {
49         warn "tinc: TapDevice should be of the form /dev/tapN\n";
50         return 0;
51     }
52     if(!defined($VPN)) {
53         warn "tinc: MyVirtualIP required\n";
54         return 0;
55     }
56     if($VPN eq "") {
57         warn "tinc: No argument to MyVirtualIP/MyOwnVPNIP\n";
58         return 0;
59     }
60     if(defined($VPNMASK) && $VPNMASK eq "") {
61         warn "tinc: Invalid argument to VpnMask\n";
62         return 0;
63     }
64
65     $ADR = $VPN;
66     $ADR =~ s/^([^\/]+)\/.*$/$1/;
67     $LEN = $VPN;
68     $LEN =~ s/^.*\/([^\/]+)$/$1/;
69     if($ADR eq "" || $LEN eq "") {
70         warn "tinc: Badly formed MyVirtualIP/MyOwnVPNIP\n";
71         return 0;
72     }
73     @addr = split(/\./, $ADR);
74
75     $ADR = pack('C4', @addr);
76     $MSK = pack('N4', -1 << (32 - $LEN));
77     $BRD = join(".", unpack('C4', $ADR | ~$MSK));
78     $MAC = "fe:fd:" . join(":", map { sprintf "%02x", $_ } unpack('C4', $ADR));
79
80     if(!defined($VPNMASK)) {
81         $VPNMASK = $MSK;
82         $VPNMASK = join(".", unpack('C4', $VPNMASK));
83     }
84     $ADR = join(".", unpack('C4', $ADR));
85     $MSK = join(".", unpack('C4', $MSK));
86     
87     1;
88 }
89
90
91 ##############################################################################
92 # vpn_start ()          starts specified VPN
93
94 # $_[0] ... VPN to start
95
96 sub vpn_start {
97     vpn_load($_[0]) || return 0;
98
99     system("insmod ethertap -s --name=\"ethertap$NUM\" unit=\"$NUM\" >/dev/null");
100     system("ifconfig $DEV hw ether $MAC");
101     system("ifconfig $DEV $ADR netmask $VPNMASK broadcast $BRD mtu 1448 -arp");
102     system("start-stop-daemon --start --quiet --pidfile /var/run/$NAME.$_[0].pid --exec $DAEMON -- -n $_[0] $EXTRA");
103 }
104
105
106
107
108 ##############################################################################
109 # vpn_stop ()           Stops specified VPN
110 #
111 # $_[0] ... VPN to stop
112
113 sub vpn_stop {
114     vpn_load($_[0]) || return 1;
115
116     system("start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.$_[0].pid --exec $DAEMON -- -n $_[0] $EXTRA -k");
117     
118     system("ifconfig $DEV down");
119     system("rmmod ethertap$NUM -s");
120 }
121
122
123 if(!defined($ARGV[0])) {
124     die "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}\n";
125 }
126
127 if($ARGV[0] eq "start") {
128     find_nets;
129     print "Starting $DESC:";
130     foreach $n (@NETS) {
131         print " $n";
132         vpn_start($n);
133     }
134     print ".\n";
135 } elsif ($ARGV[0] eq "stop") {
136     find_nets;
137     print "Stopping $DESC:";
138     foreach $n (@NETS) {
139         print " $n";
140         vpn_stop($n);
141     }
142     print ".\n";
143 } elsif ($ARGV[0] eq "restart" || $ARGV[0] eq "force-reload") {
144     find_nets;
145     print "Stopping $DESC:";
146     foreach $n (@NETS) {
147         print " $n";
148         vpn_stop($n);
149     }
150     print ".\n";
151     print "Starting $DESC:";
152     foreach $n (@NETS) {
153         print " $n";
154         vpn_start($n);
155     }
156     print ".\n";
157 } else {
158     die "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}\n";
159 }