Read /etc/tinc/nets.boot to find the networks that have to be started.
[tinc] / debian / init.d
1 #! /usr/bin/perl -w
2 #
3 # System startup script for tinc
4 # $Id: init.d,v 1.7 2000/05/18 23:09:31 zarq Exp $
5 #
6 # Based on Lubomir Bulej's Redhat init script.
7 #
8 # Create a file $NETSFILE (/etc/tinc/nets.boot), and put all the names of
9 # the networks in there.  These names must be valid directory names under
10 # $TCONF (/etc/tinc).  Lines starting with a # will be ignored in this
11 # file.
12 #
13
14 my $DAEMON="/usr/sbin/tincd";
15 my $NAME="tinc";
16 my $DESC="tinc daemons";
17 my $TCONF="/etc/tinc";
18 my $EXTRA="";
19 my $NETSFILE="$TCONF/nets.boot";
20 my @NETS=();
21
22
23 if (! -f $DAEMON) { exit 0; }
24
25
26
27 sub find_nets {
28     open(FH, $NETSFILE) || die "Please create $NETSFILE.\n";
29     while (<FH>) {
30         chomp;
31         if( /^[ ]*([^ \#]+)/i ) {
32             push(@NETS, "$1");
33         }
34     }
35     if($#NETS == -1) {
36         die "$NETSFILE doesn't contain any nets.\n";
37     }
38     
39 }
40
41
42 ##############################################################################
43 # vpn_load ()           Loads VPN configuration
44
45 # $_[0] ... VPN to load
46
47 sub vpn_load {
48     my @addr;
49     $CFG="$TCONF/$_[0]/tinc.conf";
50     if(! open($CFG, "< $CFG")) {
51         warn "tinc: $CFG does not exist\n";
52         return 0;
53     }
54
55     # load TINCD config
56     while(<$CFG>) {
57         if( /^[ ]*TapDevice[ =]+([^ \#]+)/i ) {
58             $DEV=$1;
59             chomp($DEV);
60             $DEV =~ s/^.*\/([^\/0-9]+)([0-9]+)$/$1$2/;
61             $NUM = $2;
62         } elsif ( /^[ ]*(MyOwnVPNIP|MyVirtualIP)[ =]+([^ \#]+)/i ) {
63             $VPN=$2;
64             chomp($VPN);
65         } elsif ( /^[ ]*VpnMask[ =]+([^ \#]+)/i ) {
66             $VPNMASK=$1;
67         }
68     }
69     if(!defined($DEV)) {
70         warn "tinc: There must be a TapDevice\n";
71         return 0;
72     }
73     if($DEV eq "") {
74         warn "tinc: TapDevice should be of the form /dev/tapN\n";
75         return 0;
76     }
77     if(!defined($VPN)) {
78         warn "tinc: MyVirtualIP required\n";
79         return 0;
80     }
81     if($VPN eq "") {
82         warn "tinc: No argument to MyVirtualIP/MyOwnVPNIP\n";
83         return 0;
84     }
85     if(defined($VPNMASK) && $VPNMASK eq "") {
86         warn "tinc: Invalid argument to VpnMask\n";
87         return 0;
88     }
89     $ADR = $VPN;
90     $ADR =~ s/^([^\/]+)\/.*$/$1/;
91     $LEN = $VPN;
92     $LEN =~ s/^.*\/([^\/]+)$/$1/;
93     if($ADR eq "" || $LEN eq "") {
94         warn "tinc: Badly formed MyVirtualIP/MyOwnVPNIP\n";
95         return 0;
96     }
97     @addr = split(/\./, $ADR);
98
99     $ADR = pack('C4', @addr);
100     $MSK = pack('N4', -1 << (32 - $LEN));
101     $BRD = join(".", unpack('C4', $ADR | ~$MSK));
102 #    $NET = join(".", unpack('C4', $ADR & $MSK));
103     $MAC = "fe:fd:" . join(":", map { sprintf "%02x", $_ } unpack('C4', $ADR));
104     $ADR = join(".", unpack('C4', $ADR));
105     $MSK = join(".", unpack('C4', $MSK));
106     
107 #    print "$DEV $VPN $NUM $LEN @addr $MAC $MASK $BRD $NET\n";
108
109     1;
110 }
111
112
113 ##############################################################################
114 # vpn_start ()          starts specified VPN
115
116 # $_[0] ... VPN to start
117
118 sub vpn_start {
119     vpn_load($_[0]) || return 0;
120
121     system("insmod ethertap -s --name=\"ethertap$NUM\" unit=\"$NUM\" >/dev/null");
122     system("ifconfig $DEV hw ether $MAC");
123     system("ifconfig $DEV $ADR netmask $MSK broadcast $BRD -arp");
124     system("start-stop-daemon --start --quiet --pidfile /var/run/$NAME.$_[0].pid --exec $DAEMON -- -n $_[0] $EXTRA");
125     if(defined($VPNMASK)) {
126         system("route add -net $ADR netmask $VPNMASK dev $DEV");
127     }
128 }
129
130
131
132
133 ##############################################################################
134 # vpn_stop ()           Stops specified VPN
135 #
136 # $_[0] ... VPN to stop
137
138 sub vpn_stop {
139     vpn_load($_[0]) || return 1;
140
141     system("start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.$_[0].pid --exec $DAEMON -- -n $_[0] $EXTRA -k");
142     
143     system("ifconfig $DEV down");
144     system("rmmod ethertap$NUM -s");
145 }
146
147
148 if(!defined($ARGV[0])) {
149     die "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}\n";
150 }
151
152 if($ARGV[0] eq "start") {
153     find_nets;
154     print "Starting $DESC:";
155     foreach $n (@NETS) {
156         print " $n";
157         vpn_start($n);
158     }
159     print ".\n";
160 } elsif ($ARGV[0] eq "stop") {
161     find_nets;
162     print "Stopping $DESC:";
163     foreach $n (@NETS) {
164         print " $n";
165         vpn_stop($n);
166     }
167     print ".\n";
168 } elsif ($ARGV[0] eq "restart" || $ARGV[0] eq "force-reload") {
169     find_nets;
170     print "Stopping $DESC:";
171     foreach $n (@NETS) {
172         print " $n";
173         vpn_stop($n);
174     }
175     print ".\n";
176     print "Starting $DESC:";
177     foreach $n (@NETS) {
178         print " $n";
179         vpn_start($n);
180     }
181     print ".\n";
182 } else {
183     die "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}\n";
184 }