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