3 # System startup script for tinc
4 # $Id: init.d,v 1.3 2000/05/13 00:54:27 zarq Exp $
7 my $DAEMON="/usr/sbin/tincd";
9 my $DESC="tinc daemons";
11 my $TCONF="/etc/tinc";
14 if (! -f $DAEMON) { exit 0; }
18 print "**tinc: daemon $DAEMON does not exist or is not executable!\n";
22 # Check the configuration directory
24 print "**tinc: configuration directory ($TCONF) not found!\n";
29 ##############################################################################
30 # vpn_load () Loads VPN configuration
32 # $_[0] ... VPN to load
37 $CFG="$TCONF/$_[0]/tinc.conf";
38 open($CFG, "< $CFG") || die "tinc: $CFG does not exist";
42 if( /^[ ]*TapDevice[ =]+([^ \#]+)/i ) {
45 $DEV =~ s/^.*\/([^\/0-9]+)([0-9]+)$/$1$2/;
47 } elsif ( /^[ ]*(MyOwnVPNIP|MyVirtualIP)[ =]+([^ \#]+)/i ) {
53 die "tinc: There must be a TapDevice";
56 die "tinc: TapDevice should be of the form /dev/tapN";
59 die "tinc: MyVirtualIP required";
62 die "tinc: No argument to MyVirtualIP/MyOwnVPNIP";
65 $ADR =~ s/^([^\/]+)\/.*$/$1/;
67 $LEN =~ s/^.*\/([^\/]+)$/$1/;
68 if($ADR eq "" || $LEN eq "") {
69 die "tinc: Badly formed MyVirtualIP/MyOwnVPNIP";
71 @addr = split(/\./, $ADR);
73 $ADR = pack('C4', @addr);
74 $MSK = pack('N4', -1 << (32 - $LEN));
75 $BRD = join(".", unpack('C4', $ADR | ~$MSK));
76 # $NET = join(".", unpack('C4', $ADR & $MSK));
77 $MAC = "fe:fd:" . join(":", map { sprintf "%02x", $_ } unpack('C4', $ADR));
78 $ADR = join(".", unpack('C4', $ADR));
79 $MSK = join(".", unpack('C4', $MSK));
81 # print "$DEV $VPN $NUM $LEN @addr $MAC $MASK $BRD $NET\n";
87 ##############################################################################
88 # vpn_start () starts specified VPN
90 # $_[0] ... VPN to start
93 vpn_load($_[0]) || die "tinc: could not vpn_load $_[0]";
95 if (! -c "/dev/$DEV") {
100 system("echo mknod --mode=0600 /dev/$DEV c 36 $num");
102 system("insmod ethertap -s --name=\"ethertap$NUM\" unit=\"$NUM\" >/dev/null");
103 system("ifconfig $DEV hw ether $MAC");
104 system("ifconfig $DEV $ADR netmask $MSK broadcast $BRD");
105 system("start-stop-daemon --start --quiet --pidfile /var/run/$NAME.$_[0].pid --exec $DAEMON -- -n $_[0] $EXTRA");
111 ##############################################################################
112 # vpn_stop () Stops specified VPN
117 vpn_load($_[0]) || return 1;
119 system("start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.$_[0].pid --exec $DAEMON -- -n $_[0] $EXTRA -k");
121 system("ifconfig $DEV down");
122 system("rmmod ethertap$NUM -s");
126 if(!defined($ARGV[0])) {
127 die "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}\n";
130 if($ARGV[0] eq "start") {
131 print "Starting $DESC:";
132 foreach $n (split(" ", $NETS)) {
137 } elsif ($ARGV[0] eq "stop") {
138 print "Stopping $DESC:";
139 foreach $n (split(" ", $NETS)) {
144 } elsif ($ARGV[0] eq "restart" || $ARGV[0] eq "force-reload") {
145 print "Stopping $DESC:";
146 foreach $n (split(" ", $NETS)) {
151 print "Starting $DESC:";
152 foreach $n (split(" ", $NETS)) {
158 die "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}\n";