]> git.ipfire.org Git - people/stevee/guardian.git/blob - modules/Config.pm
Drop PID file when exiting guardian.
[people/stevee/guardian.git] / modules / Config.pm
1 package Guardian::Config;
2 use strict;
3 use warnings;
4
5 use Exporter qw(import);
6
7 our @EXPORT_OK = qw(CheckConfig UseConfig);
8
9 # The default config file which is used, if no one has been specified.
10 my $configfile = "/etc/guardian/guardian.conf";
11
12 # The maximum amount of chars, which a line in the configfile is allowed to contain.
13 my $maxlength = "64";
14
15 # Hash with default settings. They may be overwritten by settings of the config file.
16 my %defaults = (
17 "LogLevel" => "info",
18 "LogFacility" => "syslog",
19 "BlockCount" => "3",
20 "BlockTime" => "86400",
21 );
22
23 #
24 ## UseConfig configuration function.
25 #
26 ## This function does the main work. It is responsible for calling the subfunction
27 ## to read the given config file (or use the default one if none has been specified),
28 ## and push the returned object to the validate subfunction. Finally the validated
29 ## settings will be merged with the default ones (existing defaults will be overwritten).
30 #
31 sub UseConfig ($) {
32 my $file = $_[0];
33
34 # If not file has been specified, use the default one.
35 unless ($file) {
36 $file = $configfile;
37 }
38
39 # Call subfunction to get the settings from config file.
40 # Store the options and values in a temporary hash.
41 my %temp = &ReadConfig($file);
42
43 # Validate config settings.
44 my $error = &CheckConfig(\%temp);
45
46 # As long, as no error message is returned, the config is valid.
47 unless ($error) {
48 # Merge hash with contains the default
49 # and temporary config hash. If both hashes contains
50 # the same keys, the keys+values of the first one (%defaults)
51 # will be overwritten.
52 my %config = (%defaults, %temp);
53
54 # Return the final configuration hash.
55 return %config;
56
57 # If an error message is returned, exit and print the error message.
58 } else {
59 die "Invalid configuration: $error";
60 }
61 }
62
63 #
64 ## ReadConfig (configfile) function.
65 #
66 ## This function is used to read a given configuration file and store the
67 ## values into a hash which will be returned.
68 #
69 sub ReadConfig ($) {
70 my $file = $_[0];
71
72 # Hash to store the read-in configuration options and values.
73 my %config = ();
74
75 # Check if the configfile exists and is read-able.
76 unless (-r "$file") {
77 die "The given configfile ($file) does not exist, or is not read-able: $!";
78 }
79
80 # Open the config file and read-in all configuration options and values.
81 open(CONF, "$file") or die "Could not open $file: $!";
82
83 # Process line by line.
84 while (my $line = <CONF>) {
85 # Skip comments.
86 next if ($line =~ /\#/);
87
88 # Skip blank lines.
89 next if ($line =~ /^\s*$/);
90
91 # Remove any newlines.
92 chomp($line);
93
94 # Check line lenght, skip it, if it is longer than, the
95 # allowed maximum.
96 my $length = length("$line");
97 next if ($length gt $maxlength);
98
99 # Remove any whitespaces.
100 $line=~ s/ //g;
101
102 # Splitt line into two parts.
103 my ($option, $value) = split (/=/, $line);
104
105 # Add config option and value to the config hash.
106 $config{$option} = $value;
107 }
108
109 # Close the config file.
110 close(CONF);
111
112 # Return the configuration hash.
113 return %config;
114 }
115
116 #
117 ## The CheckConfig function.
118 #
119 ## This function is responsible to validate configure options which has
120 ## to be passed as a hash. It will return an error message which provides some
121 ## deeper details, if any problems have been detected.
122 #
123 sub CheckConfig (\%) {
124 # Dereference the given hash-ref and store
125 # them into a new temporary hash.
126 my %config = %{ $_[0] };
127
128 # If a BlockTime has been configured, check if the value is a natural number.
129 if (exists($config{BlockTime})) {
130 # Get the configured value for "BlockTime".
131 my $value = $config{BlockTime};
132
133 # Call subroutine for validation.
134 my $error = &check_number("$value");
135
136 # If the check fails, immediately return an error message.
137 if ($error) {
138 return "Invalid BlockTime: $error";
139 }
140 }
141
142 # If a BlockCount has been configured, check if the value is a natural number.
143 if (exists($config{BlockCount})) {
144 # Get the configured value for "BlockCount".
145 my $value = $config{BlockCount};
146
147 # Call subroutine for validation.
148 my $error = &check_number("$value");
149
150 # If the check fails, immediately return an error message.
151 if ($error) {
152 return "Invalid BlockCount: $error";
153 }
154 }
155
156 # XXX - add check for validating the configured loglevel.
157
158 # The config looks good, so return nothing (no error message).
159 return undef
160 }
161
162 #
163 ## The check_number subroutine.
164 #
165 ## This simple subroutine is used to check if a given string is numeric
166 ## and contains a natural number which has to be greater than zero.
167 #
168 sub check_number ($) {
169 my $input = $_[0];
170
171 # Check if the input is a natural number.
172 unless ($input =~ /^\d+$/) {
173 return "$input is not a natural number";
174 }
175
176 # Check if the number is greater than zero.
177 unless ($input gt "0") {
178 return "$input has to be greater than zero";
179 }
180
181 # Input is okay, return no error message (nothing).
182 return undef;
183 }
184
185 1;