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