3 # System startup script for tinc
4 # $Id: init.d,v 1.7 2000/05/18 23:09:31 zarq Exp $
6 # Based on Lubomir Bulej's Redhat init script.
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
14 my $DAEMON="/usr/sbin/tincd";
16 my $DESC="tinc daemons";
17 my $TCONF="/etc/tinc";
19 my $NETSFILE="$TCONF/nets.boot";
23 if (! -f $DAEMON) { exit 0; }
28 open(FH, $NETSFILE) || die "Please create $NETSFILE.\n";
31 if( /^[ ]*([^ \#]+)/i ) {
36 die "$NETSFILE doesn't contain any nets.\n";
42 ##############################################################################
43 # vpn_load () Loads VPN configuration
45 # $_[0] ... VPN to load
49 $CFG="$TCONF/$_[0]/tinc.conf";
50 if(! open($CFG, "< $CFG")) {
51 warn "tinc: $CFG does not exist\n";
57 if( /^[ ]*TapDevice[ =]+([^ \#]+)/i ) {
60 $DEV =~ s/^.*\/([^\/0-9]+)([0-9]+)$/$1$2/;
62 } elsif ( /^[ ]*(MyOwnVPNIP|MyVirtualIP)[ =]+([^ \#]+)/i ) {
65 } elsif ( /^[ ]*VpnMask[ =]+([^ \#]+)/i ) {
70 warn "tinc: There must be a TapDevice\n";
74 warn "tinc: TapDevice should be of the form /dev/tapN\n";
78 warn "tinc: MyVirtualIP required\n";
82 warn "tinc: No argument to MyVirtualIP/MyOwnVPNIP\n";
85 if(defined($VPNMASK) && $VPNMASK eq "") {
86 warn "tinc: Invalid argument to VpnMask\n";
90 $ADR =~ s/^([^\/]+)\/.*$/$1/;
92 $LEN =~ s/^.*\/([^\/]+)$/$1/;
93 if($ADR eq "" || $LEN eq "") {
94 warn "tinc: Badly formed MyVirtualIP/MyOwnVPNIP\n";
97 @addr = split(/\./, $ADR);
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));
107 # print "$DEV $VPN $NUM $LEN @addr $MAC $MASK $BRD $NET\n";
113 ##############################################################################
114 # vpn_start () starts specified VPN
116 # $_[0] ... VPN to start
119 vpn_load($_[0]) || return 0;
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");
133 ##############################################################################
134 # vpn_stop () Stops specified VPN
136 # $_[0] ... VPN to stop
139 vpn_load($_[0]) || return 1;
141 system("start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.$_[0].pid --exec $DAEMON -- -n $_[0] $EXTRA -k");
143 system("ifconfig $DEV down");
144 system("rmmod ethertap$NUM -s");
148 if(!defined($ARGV[0])) {
149 die "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}\n";
152 if($ARGV[0] eq "start") {
154 print "Starting $DESC:";
160 } elsif ($ARGV[0] eq "stop") {
162 print "Stopping $DESC:";
168 } elsif ($ARGV[0] eq "restart" || $ARGV[0] eq "force-reload") {
170 print "Stopping $DESC:";
176 print "Starting $DESC:";
183 die "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}\n";