Test for existence of configured tinc networks. This will also make
[tinc] / debian / init.d
1 #! /usr/bin/perl -w
2 #
3 # System startup script for tinc
4 # $Id: init.d,v 1.4 2000/05/15 09:41:34 guus Exp $
5 #
6
7 my $DAEMON="/usr/sbin/tincd";
8 my $NAME="tinc";
9 my $DESC="tinc daemons";
10 my $TCONF="/etc/tinc";
11 my $EXTRA="";
12
13 # $NETS is a space seperated list of all tinc networks.
14 my $NETS="";
15
16 if ("$NETS" eq "") { print "No tinc networks configured."; exit 0; }
17
18 if (! -f $DAEMON) { exit 0; }
19
20 ##############################################################################
21 # vpn_load ()           Loads VPN configuration
22
23 # $_[0] ... VPN to load
24
25
26 sub vpn_load {
27     my @addr;
28     $CFG="$TCONF/$_[0]/tinc.conf";
29     open($CFG, "< $CFG") || die "tinc: $CFG does not exist";
30
31     # load TINCD config
32     while(<$CFG>) {
33         if( /^[ ]*TapDevice[ =]+([^ \#]+)/i ) {
34             $DEV=$1;
35             chomp($DEV);
36             $DEV =~ s/^.*\/([^\/0-9]+)([0-9]+)$/$1$2/;
37             $NUM = $2;
38         } elsif ( /^[ ]*(MyOwnVPNIP|MyVirtualIP)[ =]+([^ \#]+)/i ) {
39             $VPN=$2;
40             chomp($VPN);
41         }
42     }
43     if(!defined($DEV)) {
44         die "tinc: There must be a TapDevice";
45     }
46     if($DEV eq "") {
47         die "tinc: TapDevice should be of the form /dev/tapN";
48     }
49     if(!defined($VPN)) {
50         die "tinc: MyVirtualIP required";
51     }
52     if($VPN eq "") {
53         die "tinc: No argument to MyVirtualIP/MyOwnVPNIP";
54     }
55     $ADR = $VPN;
56     $ADR =~ s/^([^\/]+)\/.*$/$1/;
57     $LEN = $VPN;
58     $LEN =~ s/^.*\/([^\/]+)$/$1/;
59     if($ADR eq "" || $LEN eq "") {
60         die "tinc: Badly formed MyVirtualIP/MyOwnVPNIP";
61     }
62     @addr = split(/\./, $ADR);
63
64     $ADR = pack('C4', @addr);
65     $MSK = pack('N4', -1 << (32 - $LEN));
66     $BRD = join(".", unpack('C4', $ADR | ~$MSK));
67 #    $NET = join(".", unpack('C4', $ADR & $MSK));
68     $MAC = "fe:fd:" . join(":", map { sprintf "%02x", $_ } unpack('C4', $ADR));
69     $ADR = join(".", unpack('C4', $ADR));
70     $MSK = join(".", unpack('C4', $MSK));
71     
72 #    print "$DEV $VPN $NUM $LEN @addr $MAC $MASK $BRD $NET\n";
73
74     1;
75 }
76
77
78 ##############################################################################
79 # vpn_start ()          starts specified VPN
80
81 # $_[0] ... VPN to start
82
83 sub vpn_start {
84     vpn_load($_[0]) || die "tinc: could not vpn_load $_[0]";
85
86     if (! -c "/dev/$DEV") {
87         if (-e "/dev/$DEV") {
88             unlink("/dev/$DEV");
89         }
90         $num = $NUM + 16;
91         system("echo mknod --mode=0600 /dev/$DEV c 36 $num");
92     }
93     system("insmod ethertap -s --name=\"ethertap$NUM\" unit=\"$NUM\" >/dev/null");
94     system("ifconfig $DEV hw ether $MAC");
95     system("ifconfig $DEV $ADR netmask $MSK broadcast $BRD");
96     system("start-stop-daemon --start --quiet --pidfile /var/run/$NAME.$_[0].pid --exec $DAEMON -- -n $_[0] $EXTRA");
97 }
98
99
100
101
102 ##############################################################################
103 # vpn_stop ()           Stops specified VPN
104 #
105 # $1 ... VPN to stop
106
107 sub vpn_stop {
108     vpn_load($_[0]) || return 1;
109
110     system("start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.$_[0].pid --exec $DAEMON -- -n $_[0] $EXTRA -k");
111     
112     system("ifconfig $DEV down");
113     system("rmmod ethertap$NUM -s");
114 }
115
116
117 if(!defined($ARGV[0])) {
118     die "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}\n";
119 }
120
121 if($ARGV[0] eq "start") {
122     print "Starting $DESC:";
123     foreach $n (split(" ", $NETS)) {
124         print " $n";
125         vpn_start($n);
126     }
127     print ".\n";
128 } elsif ($ARGV[0] eq "stop") {
129     print "Stopping $DESC:";
130     foreach $n (split(" ", $NETS)) {
131         print " $n";
132         vpn_stop($n);
133     }
134     print ".\n";
135 } elsif ($ARGV[0] eq "restart" || $ARGV[0] eq "force-reload") {
136     print "Stopping $DESC:";
137     foreach $n (split(" ", $NETS)) {
138         print " $n";
139         vpn_stop($n);
140     }
141     print ".\n";
142     print "Starting $DESC:";
143     foreach $n (split(" ", $NETS)) {
144         print " $n";
145         vpn_start($n);
146     }
147     print ".\n";
148 } else {
149     die "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}\n";
150 }