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