3 # System startup script for tinc
4 # $Id: init.d,v 1.9 2000/05/19 01:17:32 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 if(! open(FH, $NETSFILE)) {
29 warn "Please create $NETSFILE.\n";
34 if( /^[ ]*([^ \#]+)/i ) {
39 warn "$NETSFILE doesn't contain any nets.\n";
46 ##############################################################################
47 # vpn_load () Loads VPN configuration
49 # $_[0] ... VPN to load
53 $CFG="$TCONF/$_[0]/tinc.conf";
54 if(! open($CFG, "< $CFG")) {
55 warn "tinc: $CFG does not exist\n";
61 if( /^[ ]*TapDevice[ =]+([^ \#]+)/i ) {
64 $DEV =~ s/^.*\/([^\/0-9]+)([0-9]+)$/$1$2/;
66 } elsif ( /^[ ]*(MyOwnVPNIP|MyVirtualIP)[ =]+([^ \#]+)/i ) {
69 } elsif ( /^[ ]*VpnMask[ =]+([^ \#]+)/i ) {
74 warn "tinc: There must be a TapDevice\n";
78 warn "tinc: TapDevice should be of the form /dev/tapN\n";
82 warn "tinc: MyVirtualIP required\n";
86 warn "tinc: No argument to MyVirtualIP/MyOwnVPNIP\n";
89 if(defined($VPNMASK) && $VPNMASK eq "") {
90 warn "tinc: Invalid argument to VpnMask\n";
94 $ADR =~ s/^([^\/]+)\/.*$/$1/;
96 $LEN =~ s/^.*\/([^\/]+)$/$1/;
97 if($ADR eq "" || $LEN eq "") {
98 warn "tinc: Badly formed MyVirtualIP/MyOwnVPNIP\n";
101 @addr = split(/\./, $ADR);
103 $ADR = pack('C4', @addr);
104 $MSK = pack('N4', -1 << (32 - $LEN));
105 $BRD = join(".", unpack('C4', $ADR | ~$MSK));
106 # $NET = join(".", unpack('C4', $ADR & $MSK));
107 $MAC = "fe:fd:" . join(":", map { sprintf "%02x", $_ } unpack('C4', $ADR));
108 $VPNMASK = pack('C4', split(/\./, $VPNMASK));
109 $VPNNET = join(".", unpack('C4', $ADR & $VPNMASK));
110 $VPNMASK = join(".", unpack('C4', $VPNMASK));
111 $ADR = join(".", unpack('C4', $ADR));
112 $MSK = join(".", unpack('C4', $MSK));
114 # print "$DEV $VPN $NUM $LEN @addr $MAC $MASK $BRD $NET\n";
120 ##############################################################################
121 # vpn_start () starts specified VPN
123 # $_[0] ... VPN to start
126 vpn_load($_[0]) || return 0;
128 system("insmod ethertap -s --name=\"ethertap$NUM\" unit=\"$NUM\" >/dev/null");
129 system("ifconfig $DEV hw ether $MAC");
130 system("ifconfig $DEV $ADR netmask $MSK broadcast $BRD -arp");
131 system("start-stop-daemon --start --quiet --pidfile /var/run/$NAME.$_[0].pid --exec $DAEMON -- -n $_[0] $EXTRA");
132 if(defined($VPNMASK)) {
133 system("route add -net $VPNNET netmask $VPNMASK dev $DEV");
140 ##############################################################################
141 # vpn_stop () Stops specified VPN
143 # $_[0] ... VPN to stop
146 vpn_load($_[0]) || return 1;
148 system("start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.$_[0].pid --exec $DAEMON -- -n $_[0] $EXTRA -k");
150 system("ifconfig $DEV down");
151 system("rmmod ethertap$NUM -s");
155 if(!defined($ARGV[0])) {
156 die "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}\n";
159 if($ARGV[0] eq "start") {
161 print "Starting $DESC:";
167 } elsif ($ARGV[0] eq "stop") {
169 print "Stopping $DESC:";
175 } elsif ($ARGV[0] eq "restart" || $ARGV[0] eq "force-reload") {
177 print "Stopping $DESC:";
183 print "Starting $DESC:";
190 die "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}\n";