Initial revision
[l2tpns.git] / conform.cfg
1 #!/usr/bin/perl -w
2 # vim:ft=perl
3
4 die "l2tpns requires RedHat 7.3 or above" if i_isa("SOE_linux_rh6");
5
6 my $restart = 0;
7
8 my %conf = ();
9 for my $c (i_isa_fetchall('L2tpns_config')) {
10         foreach my $opt (keys %$c) {
11                 if (ref $conf{$opt} and ref $conf{$opt} eq 'ARRAY') {
12                         $c->{$opt} = [ $c->{$opt} ] unless ref $c->{$opt};
13                         push @{$conf{$opt}}, @{$c->{$opt}};
14                 } elsif (ref $c->{$opt} and ref $c->{$opt} eq 'ARRAY') {
15                         # Make sure to copy to avoid changing /etc/machine
16                         $conf{$opt} = [ $conf{$opt} ] if $conf{$opt};
17                         $conf{$opt} ||= [];
18                         push @{$conf{$opt}}, @{$c->{$opt}};
19                 } else {
20                         $conf{$opt} = $c->{$opt};
21                 }
22         }
23 }
24
25 $conf{Address_pool} ||= i_isa("Address_pool"); # backwards compat
26
27 unless (i_isa("No_throttle")) {
28         chomp(my $kernel = `uname -r`);
29         print "WARN: l2tpns requires kernel 2.4.18-187OIE1. This is included in $_path/rpm/kernel-2.4.18-187OIE1.i386.rpm\n"
30                 unless ($kernel eq '2.4.18-187OIE1' || $kernel =~ /^2\.4\.2\d/);
31 }
32
33 # Recompile the server if needed
34 if ((stat("src/l2tpns.c"))[9] > (stat("src/l2tpns"))[9]) {
35         chdir("src");
36         command("make clean");
37         command("make");
38         chdir("..");
39         $restart++;
40 }
41
42 command("mkdir /dev/net") and ++$restart unless -d "/dev/net";
43 command("mknod /dev/net/tun c 10 200") and ++$restart unless -c "/dev/net/tun";
44
45 my $cluster = i_isa('Gateway_cluster');
46 my $cluster_name = $cluster->{master} || $cluster->{slave} || die 'Not a master or a slave' if $cluster;
47 my $master = $cluster && $cluster->{master};
48 my $command = $master ? "cluster_master" : "l2tpns";
49 push @{$m{$_class}->{Monitor}->{process}->{tests}}, $command;
50
51
52 if ($cluster) {
53         $conf{'save state'} ||= 'no';
54         if (!$master && !$cluster->{bind_address}) {
55                 die 'No bind address for cluster slave';
56         }
57         $conf{'bind address'} ||= $cluster->{bind_address} unless $master;
58         my $cluster_master;
59         my @cluster_slaves = ();
60         my @cluster_slave_addresses = ();
61         foreach my $host (type_list('Gateway_cluster')) {
62                 my $host_conf = OIE::Conform::i_isa(\%m, $host, 'Gateway_cluster');
63                 if ($host_conf->{master} eq $cluster_name) {
64                         $cluster_master = $host;
65                 } elsif ($host_conf->{slave} eq $cluster_name) {
66                         push @cluster_slaves, $host;
67                         push @{$conf{Address_pool}}, map { "$host_conf->{bind_address}:$_" } @{$m{$host}->{L2tpns_config}->{Address_pool}} if $master;
68                         push @cluster_slave_addresses, $m{$host}->{int_eth0}->{ip};
69                 }
70         }
71
72         if ($master) {
73                 push @{$m{$_class}->{inittab_include}},
74                         "$_path/src/cluster_master $m{$iam}->{int_eth0}->{ip}";
75                 push @{$m{$_class}->{inittab_disable}},
76                         "$_path/src/l2tpns";
77                 $m{$_class}->{Firewall}->{$_} = '32792:udp'
78                         foreach @cluster_slave_addresses;
79         }
80         $conf{'cluster master'} ||= $m{$cluster_master}->{int_eth0}->{ip};
81 }
82
83 # Build up address pool
84 my $pool = $conf{Address_pool};
85 if ($pool) {
86         my $address_pool = "";
87
88         foreach (@$pool) {
89                 $address_pool .= "$_\n";
90         }
91
92         text_install("$_path/etc/ip_pool.txt", $address_pool) and $restart++;
93 } else {
94         print "WARN: No Address_pool defined in machines.\n";
95 }
96 delete $conf{"Address_pool"}; # Don't add it to the conf file
97
98 my $servicenet = $conf{"servicenet"};
99 if ($servicenet) {
100         $conf{'servicenet'} = 'yes';
101         push @{$conf{plugin}}, 'servicenet' unless grep /^servicenet$/, @{$conf{plugin}};
102         file_install("/etc/rc.d/rc.firewall.INPUT.servicenet", "$_path/etc/rc.firewall.INPUT.servicenet", undef, undef,
103                 "s/#SERVICENET#/$servicenet/g")
104                 and queue_command("/etc/rc.d/rc.firewall");
105 } else {
106         $conf{'servicenet'} = 'no';
107         # Uninstall
108         if (-f "/etc/rc.d/rc.firewall.INPUT.servicenet") {
109                 unlink "/etc/rc.d/rc.firewall.INPUT.servicenet";
110                 command("iptables -F snet");
111         }
112 }
113
114 # Note that we don't file_install the config file, but instead modify it
115 # in place
116
117 my $config = slurp_file("$_path/etc/l2tpns.cfg");
118
119 # plugins need to go first, else they won't pick up params
120 foreach my $p (@{$conf{plugin}}) {
121         $config =~ s/^#?\s*plugin\s+=\s+\Q$p\E$/plugin = $p/mg or
122                 $config = "plugin = $p\n\n$config";
123 }
124 delete $conf{plugin};
125
126 foreach my $c (keys %conf) {
127         $config =~ s/^#?\s*\Q$c\E\s+=\s+.*$/$c = $conf{$c}/mg or
128                 $config .= "$c = $conf{$c}\n\n";
129 }
130
131 file_install("/etc/rc.d/rc.firewall.INPUT.l2tpns", "$_path/etc/rc.firewall.INPUT.l2tpns")
132         and queue_command("/etc/rc.d/rc.firewall");
133
134 text_install("$_path/etc/l2tpns.cfg", $config) and $restart++;
135
136 queue_command("killall $command") if $restart;
137