Various small changes.
[tinc] / debian / tinc-up
1 #! /usr/bin/perl -w
2 #
3 # Device configuration script for tinc
4 # $Id: tinc-up,v 1.1.2.2 2000/12/22 16:54:56 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         $DEV = "/dev/tap0";
46     }
47     if($DEV eq "") {
48         warn "tinc: TapDevice should be of the form /dev/tapN\n";
49         return 0;
50     }
51     if(!defined($VPN)) {
52         warn "tinc: MyVirtualIP required\n";
53         return 0;
54     }
55     if($VPN eq "") {
56         warn "tinc: No argument to MyVirtualIP/MyOwnVPNIP\n";
57         return 0;
58     }
59     if(defined($VPNMASK) && $VPNMASK eq "") {
60         warn "tinc: Invalid argument to VpnMask\n";
61         return 0;
62     }
63
64     $ADR = $VPN;
65     $ADR =~ s/^([^\/]+)\/.*$/$1/;
66     $LEN = $VPN;
67     $LEN =~ s/^.*\/([^\/]+)$/$1/;
68     if($ADR eq "" || $LEN eq "") {
69         warn "tinc: Badly formed MyVirtualIP/MyOwnVPNIP\n";
70         return 0;
71     }
72     @addr = split(/\./, $ADR);
73
74     $ADR = pack('C4', @addr);
75     $MSK = pack('N4', -1 << (32 - $LEN));
76     $BRD = join(".", unpack('C4', $ADR | ~$MSK));
77     $MAC = "fe:fd:00:00:00:00";
78
79     if(!defined($VPNMASK)) {
80         $VPNMASK = $MSK;
81         $VPNMASK = join(".", unpack('C4', $VPNMASK));
82     }
83     $ADR = join(".", unpack('C4', $ADR));
84     $MSK = join(".", unpack('C4', $MSK));
85
86     1;
87 }
88
89
90 ##############################################################################
91 # vpn_start ()          starts specified VPN
92
93 # $_[0] ... VPN to start
94
95 sub vpn_start {
96     vpn_load($_[0]) || return 0;
97
98     system("insmod ethertap -s --name=\"ethertap$NUM\" unit=\"$NUM\" >/dev/null");
99     system("ifconfig $DEV hw ether $MAC");
100     system("ifconfig $DEV $ADR netmask $VPNMASK broadcast $BRD mtu 1448 -arp");
101     system("start-stop-daemon --start --quiet --pidfile /var/run/$NAME.$_[0].pid --exec $DAEMON -- -n $_[0] $EXTRA");
102 }
103
104
105
106
107 ##############################################################################
108 # vpn_stop ()           Stops specified VPN
109 #
110 # $_[0] ... VPN to stop
111
112 sub vpn_stop {
113     vpn_load($_[0]) || return 1;
114
115     system("start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.$_[0].pid --exec $DAEMON -- -n $_[0] $EXTRA -k");
116     
117     system("ifconfig $DEV down");
118     system("rmmod ethertap$NUM -s");
119 }
120
121
122 if(!defined($ARGV[0])) {
123     die "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}\n";
124 }
125
126 if($ARGV[0] eq "start") {
127     find_nets;
128     print "Starting $DESC:";
129     foreach $n (@NETS) {
130         print " $n";
131         vpn_start($n);
132     }
133     print ".\n";
134 } elsif ($ARGV[0] eq "stop") {
135     find_nets;
136     print "Stopping $DESC:";
137     foreach $n (@NETS) {
138         print " $n";
139         vpn_stop($n);
140     }
141     print ".\n";
142 } elsif ($ARGV[0] eq "restart" || $ARGV[0] eq "force-reload") {
143     find_nets;
144     print "Stopping $DESC:";
145     foreach $n (@NETS) {
146         print " $n";
147         vpn_stop($n);
148     }
149     print ".\n";
150     print "Starting $DESC:";
151     foreach $n (@NETS) {
152         print " $n";
153         vpn_start($n);
154     }
155     print ".\n";
156 } else {
157     die "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}\n";
158 }