2000-05-13 00:54:27 +00:00
|
|
|
#! /usr/bin/perl -w
|
2000-04-25 19:11:02 +00:00
|
|
|
#
|
2000-05-13 00:54:27 +00:00
|
|
|
# System startup script for tinc
|
2000-06-03 23:32:03 +00:00
|
|
|
# $Id: init.d,v 1.14.2.1 2000/06/03 23:32:03 zarq Exp $
|
2000-05-16 14:34:44 +00:00
|
|
|
#
|
|
|
|
# Based on Lubomir Bulej's Redhat init script.
|
2000-04-25 19:11:02 +00:00
|
|
|
#
|
2000-05-18 23:09:31 +00:00
|
|
|
# Create a file $NETSFILE (/etc/tinc/nets.boot), and put all the names of
|
|
|
|
# the networks in there. These names must be valid directory names under
|
|
|
|
# $TCONF (/etc/tinc). Lines starting with a # will be ignored in this
|
|
|
|
# file.
|
|
|
|
#
|
2000-04-25 19:11:02 +00:00
|
|
|
|
2000-05-13 00:54:27 +00:00
|
|
|
my $DAEMON="/usr/sbin/tincd";
|
|
|
|
my $NAME="tinc";
|
|
|
|
my $DESC="tinc daemons";
|
|
|
|
my $TCONF="/etc/tinc";
|
2000-05-15 09:41:34 +00:00
|
|
|
my $EXTRA="";
|
2000-05-18 23:09:31 +00:00
|
|
|
my $NETSFILE="$TCONF/nets.boot";
|
|
|
|
my @NETS=();
|
2000-05-04 23:26:24 +00:00
|
|
|
|
|
|
|
|
2000-05-15 09:41:34 +00:00
|
|
|
if (! -f $DAEMON) { exit 0; }
|
2000-05-04 23:26:24 +00:00
|
|
|
|
2000-05-18 23:09:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
sub find_nets {
|
2000-05-19 00:15:37 +00:00
|
|
|
if(! open(FH, $NETSFILE)) {
|
|
|
|
warn "Please create $NETSFILE.\n";
|
|
|
|
exit 0;
|
|
|
|
}
|
2000-05-18 23:09:31 +00:00
|
|
|
while (<FH>) {
|
|
|
|
chomp;
|
|
|
|
if( /^[ ]*([^ \#]+)/i ) {
|
|
|
|
push(@NETS, "$1");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if($#NETS == -1) {
|
2000-05-19 00:15:37 +00:00
|
|
|
warn "$NETSFILE doesn't contain any nets.\n";
|
|
|
|
exit 0;
|
2000-05-18 23:09:31 +00:00
|
|
|
}
|
|
|
|
|
2000-05-15 17:15:52 +00:00
|
|
|
}
|
|
|
|
|
2000-05-18 23:09:31 +00:00
|
|
|
|
2000-05-04 23:26:24 +00:00
|
|
|
##############################################################################
|
|
|
|
# vpn_load () Loads VPN configuration
|
|
|
|
#
|
2000-05-13 00:54:27 +00:00
|
|
|
# $_[0] ... VPN to load
|
2000-05-04 23:26:24 +00:00
|
|
|
|
2000-05-13 00:54:27 +00:00
|
|
|
sub vpn_load {
|
|
|
|
my @addr;
|
|
|
|
$CFG="$TCONF/$_[0]/tinc.conf";
|
2000-05-18 23:09:31 +00:00
|
|
|
if(! open($CFG, "< $CFG")) {
|
|
|
|
warn "tinc: $CFG does not exist\n";
|
|
|
|
return 0;
|
|
|
|
}
|
2000-05-04 23:26:24 +00:00
|
|
|
|
|
|
|
# load TINCD config
|
2000-05-13 00:54:27 +00:00
|
|
|
while(<$CFG>) {
|
|
|
|
if( /^[ ]*TapDevice[ =]+([^ \#]+)/i ) {
|
|
|
|
$DEV=$1;
|
|
|
|
chomp($DEV);
|
|
|
|
$DEV =~ s/^.*\/([^\/0-9]+)([0-9]+)$/$1$2/;
|
|
|
|
$NUM = $2;
|
|
|
|
} elsif ( /^[ ]*(MyOwnVPNIP|MyVirtualIP)[ =]+([^ \#]+)/i ) {
|
|
|
|
$VPN=$2;
|
|
|
|
chomp($VPN);
|
2000-05-16 14:34:44 +00:00
|
|
|
} elsif ( /^[ ]*VpnMask[ =]+([^ \#]+)/i ) {
|
|
|
|
$VPNMASK=$1;
|
2000-05-13 00:54:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!defined($DEV)) {
|
2000-05-18 23:09:31 +00:00
|
|
|
warn "tinc: There must be a TapDevice\n";
|
|
|
|
return 0;
|
2000-05-13 00:54:27 +00:00
|
|
|
}
|
|
|
|
if($DEV eq "") {
|
2000-05-18 23:09:31 +00:00
|
|
|
warn "tinc: TapDevice should be of the form /dev/tapN\n";
|
|
|
|
return 0;
|
2000-05-13 00:54:27 +00:00
|
|
|
}
|
|
|
|
if(!defined($VPN)) {
|
2000-05-18 23:09:31 +00:00
|
|
|
warn "tinc: MyVirtualIP required\n";
|
|
|
|
return 0;
|
2000-05-13 00:54:27 +00:00
|
|
|
}
|
|
|
|
if($VPN eq "") {
|
2000-05-18 23:09:31 +00:00
|
|
|
warn "tinc: No argument to MyVirtualIP/MyOwnVPNIP\n";
|
|
|
|
return 0;
|
2000-05-13 00:54:27 +00:00
|
|
|
}
|
2000-05-16 14:34:44 +00:00
|
|
|
if(defined($VPNMASK) && $VPNMASK eq "") {
|
2000-05-18 23:09:31 +00:00
|
|
|
warn "tinc: Invalid argument to VpnMask\n";
|
|
|
|
return 0;
|
2000-05-16 14:34:44 +00:00
|
|
|
}
|
2000-05-21 22:04:56 +00:00
|
|
|
|
2000-05-13 00:54:27 +00:00
|
|
|
$ADR = $VPN;
|
|
|
|
$ADR =~ s/^([^\/]+)\/.*$/$1/;
|
|
|
|
$LEN = $VPN;
|
|
|
|
$LEN =~ s/^.*\/([^\/]+)$/$1/;
|
|
|
|
if($ADR eq "" || $LEN eq "") {
|
2000-05-18 23:09:31 +00:00
|
|
|
warn "tinc: Badly formed MyVirtualIP/MyOwnVPNIP\n";
|
|
|
|
return 0;
|
2000-05-13 00:54:27 +00:00
|
|
|
}
|
|
|
|
@addr = split(/\./, $ADR);
|
|
|
|
|
|
|
|
$ADR = pack('C4', @addr);
|
|
|
|
$MSK = pack('N4', -1 << (32 - $LEN));
|
|
|
|
$BRD = join(".", unpack('C4', $ADR | ~$MSK));
|
|
|
|
$MAC = "fe:fd:" . join(":", map { sprintf "%02x", $_ } unpack('C4', $ADR));
|
2000-05-21 22:27:31 +00:00
|
|
|
|
|
|
|
if(!defined($VPNMASK)) {
|
|
|
|
$VPNMASK = $MSK;
|
2000-06-03 23:32:03 +00:00
|
|
|
$VPNMASK = join(".", unpack('C4', $VPNMASK));
|
2000-05-21 22:27:31 +00:00
|
|
|
}
|
2000-05-13 00:54:27 +00:00
|
|
|
$ADR = join(".", unpack('C4', $ADR));
|
|
|
|
$MSK = join(".", unpack('C4', $MSK));
|
|
|
|
|
|
|
|
1;
|
2000-05-04 23:26:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
##############################################################################
|
|
|
|
# vpn_start () starts specified VPN
|
|
|
|
#
|
2000-05-13 00:54:27 +00:00
|
|
|
# $_[0] ... VPN to start
|
|
|
|
|
|
|
|
sub vpn_start {
|
2000-05-18 23:09:31 +00:00
|
|
|
vpn_load($_[0]) || return 0;
|
2000-05-13 00:54:27 +00:00
|
|
|
|
|
|
|
system("insmod ethertap -s --name=\"ethertap$NUM\" unit=\"$NUM\" >/dev/null");
|
|
|
|
system("ifconfig $DEV hw ether $MAC");
|
2000-06-01 20:21:27 +00:00
|
|
|
system("ifconfig $DEV $ADR netmask $VPNMASK broadcast $BRD mtu 1448 -arp");
|
2000-05-13 00:54:27 +00:00
|
|
|
system("start-stop-daemon --start --quiet --pidfile /var/run/$NAME.$_[0].pid --exec $DAEMON -- -n $_[0] $EXTRA");
|
|
|
|
}
|
2000-05-04 23:26:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##############################################################################
|
|
|
|
# vpn_stop () Stops specified VPN
|
|
|
|
#
|
2000-05-15 17:15:52 +00:00
|
|
|
# $_[0] ... VPN to stop
|
2000-05-04 23:26:24 +00:00
|
|
|
|
2000-05-13 00:54:27 +00:00
|
|
|
sub vpn_stop {
|
|
|
|
vpn_load($_[0]) || return 1;
|
2000-05-04 23:26:24 +00:00
|
|
|
|
2000-05-13 00:54:27 +00:00
|
|
|
system("start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.$_[0].pid --exec $DAEMON -- -n $_[0] $EXTRA -k");
|
2000-05-04 23:26:24 +00:00
|
|
|
|
2000-05-13 00:54:27 +00:00
|
|
|
system("ifconfig $DEV down");
|
|
|
|
system("rmmod ethertap$NUM -s");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(!defined($ARGV[0])) {
|
|
|
|
die "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if($ARGV[0] eq "start") {
|
2000-05-18 23:09:31 +00:00
|
|
|
find_nets;
|
2000-05-13 00:54:27 +00:00
|
|
|
print "Starting $DESC:";
|
2000-05-18 23:09:31 +00:00
|
|
|
foreach $n (@NETS) {
|
2000-05-13 00:54:27 +00:00
|
|
|
print " $n";
|
|
|
|
vpn_start($n);
|
|
|
|
}
|
|
|
|
print ".\n";
|
|
|
|
} elsif ($ARGV[0] eq "stop") {
|
2000-05-18 23:09:31 +00:00
|
|
|
find_nets;
|
2000-05-13 00:54:27 +00:00
|
|
|
print "Stopping $DESC:";
|
2000-05-18 23:09:31 +00:00
|
|
|
foreach $n (@NETS) {
|
2000-05-13 00:54:27 +00:00
|
|
|
print " $n";
|
|
|
|
vpn_stop($n);
|
|
|
|
}
|
|
|
|
print ".\n";
|
|
|
|
} elsif ($ARGV[0] eq "restart" || $ARGV[0] eq "force-reload") {
|
2000-05-18 23:09:31 +00:00
|
|
|
find_nets;
|
2000-05-13 00:54:27 +00:00
|
|
|
print "Stopping $DESC:";
|
2000-05-18 23:09:31 +00:00
|
|
|
foreach $n (@NETS) {
|
2000-05-13 00:54:27 +00:00
|
|
|
print " $n";
|
|
|
|
vpn_stop($n);
|
|
|
|
}
|
|
|
|
print ".\n";
|
|
|
|
print "Starting $DESC:";
|
2000-05-18 23:09:31 +00:00
|
|
|
foreach $n (@NETS) {
|
2000-05-13 00:54:27 +00:00
|
|
|
print " $n";
|
|
|
|
vpn_start($n);
|
|
|
|
}
|
|
|
|
print ".\n";
|
|
|
|
} else {
|
|
|
|
die "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}\n";
|
|
|
|
}
|