Handle hostnames with upper-case letters
[webmin.git] / bandwidth / setup.cgi
1 #!/usr/local/bin/perl
2 # Add needed firewall rules and syslog entry, and apply configurations
3
4 require './bandwidth-lib.pl';
5 &ReadParse();
6 $access{'setup'} || &error($text{'setup_ecannot'});
7
8 # Work out interface
9 $iface = $in{'iface'} || $in{'other'};
10 $iface =~ /^\S+$/ || &error($text{'setup_eiface'});
11
12 # Add missing firewall rules
13 $err = &setup_rules($iface);
14 &error($err) if ($err);
15
16 if ($syslog_module eq "syslog") {
17         # Add syslog entry
18         $conf = &syslog::get_config();
19         $sysconf = &find_sysconf($conf);
20         if (!$sysconf) {
21                 &lock_file($syslog::config{'syslog_conf'});
22                 if ($syslog::config{'tags'}) {
23                         local $conf = &syslog::get_config();
24                         ($tag) = grep { $_->{'tag'} eq '*' } @$conf;
25                         }
26                 &syslog::create_log({ 'file' => $bandwidth_log,
27                                       'active' => 1,
28                                       'section' => $tag,
29                                       'sel' => [ &get_loglevel() ] });
30                 &unlock_file($syslog::config{'syslog_conf'});
31                 $err = &syslog::restart_syslog();
32                 &error($err) if ($err);
33                 }
34         }
35 else {
36         # Add syslog-ng entry
37         $conf = &syslog_ng::get_config();
38         ($dest, $filter, $log) = &find_sysconf_ng($conf);
39         &lock_file($syslog_ng::config{'syslogng_conf'});
40         if (!$dest) {
41                 # Create destination file entry
42                 $dest = { 'name' => 'destination',
43                           'type' => 1,
44                           'values' => [ 'd_bandwidth' ],
45                           'members' => [
46                                 { 'name' => 'file',
47                                   'values' => [ $bandwidth_log ] }
48                                         ]
49                         };
50                 &syslog_ng::save_directive($conf, undef, undef, $dest, 0);
51                 }
52         if (!$filter) {
53                 # Create filter for facility and level
54                 local @ll = &get_loglevel();
55                 ($fac, $lvl) = split(/\./, $ll[0]);
56                 $lvl =~ s/^=//;
57                 $filter = { 'name' => 'filter',
58                             'type' => 1,
59                             'values' => [ 'f_bandwidth' ],
60                             'members' => [ ]
61                           };
62                 if ($fac ne "*") {
63                         push(@{$filter->{'members'}},
64                              { 'name' => 'facility',
65                                'values' => [ $fac ] });
66                         }
67                 if ($fac ne "*" && $lvl ne "*") {
68                         push(@{$filter->{'members'}}, "and");
69                         }
70                 if ($lvl ne "*") {
71                         push(@{$filter->{'members'}},
72                              { 'name' => 'priority',
73                                'values' => [ $lvl ] });
74                         }
75                 &syslog_ng::save_directive($conf, undef, undef, $filter, 0);
76                 }
77         if (!$log) {
78                 # Create log for the default source, destination and filter
79                 @sources = &syslog_ng::find("source", $conf);
80                 $log = { 'name' => 'log',
81                          'type' => 1,
82                          'values' => [ ],
83                          'members' => [
84                                 { 'name' => 'source',
85                                   'values' => [ $sources[0]->{'value'} ], },
86                                 { 'name' => 'filter',
87                                   'values' => [ "f_bandwidth" ], },
88                                 { 'name' => 'destination',
89                                   'values' => [ "d_bandwidth" ], },
90                                         ]
91                         };
92                 &syslog_ng::save_directive($conf, undef, undef, $log, 0);
93                 }
94         &unlock_file($syslog_ng::config{'syslogng_conf'});
95         }
96
97 # Save the interface
98 &lock_file($module_config_file);
99 $config{'iface'} = $iface;
100 &save_module_config();
101 &unlock_file($module_config_file);
102
103 # Setup the rotation cron job
104 $job = &find_cron_job();
105 if (!$job) {
106         &cron::create_wrapper($cron_cmd, $module_name, "rotate.pl");
107         $job = { 'user' => 'root',
108                  'active' => 1,
109                  'command' => $cron_cmd,
110                  'mins' => '0',
111                  'hours' => '*',
112                  'days' => '*',
113                  'months' => '*',
114                  'weekdays' => '*',
115                 };
116         &lock_file(&cron::cron_file($job));
117         &cron::create_cron_job($job);
118         &unlock_file(&cron::cron_file($job));
119         }
120
121 &webmin_log("setup", undef, $iface);
122 &redirect("");
123