]>
git.ipfire.org Git - people/stevee/guardian.git/blob - modules/Config.pm
9a8e1fd582e6f7a7ee6fcb70e6f47c5ad383ac50
1 package Guardian
::Config
;
5 use Exporter
qw(import);
7 our @EXPORT_OK = qw(CheckConfig UseConfig);
9 # The default config file which is used, if no one has been specified.
10 my $configfile = "/etc/guardian/guardian.conf";
12 # The maximum amount of chars, which a line in the configfile is allowed to contain.
15 # Hash with default settings. They may be overwritten by settings of the config file.
18 "LogFacility" => "syslog",
20 "BlockTime" => "86400",
24 ## UseConfig configuration function.
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).
34 # If not file has been specified, use the default one.
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);
43 # Validate config settings.
44 my $error = &CheckConfig
(\
%temp);
46 # As long, as no error message is returned, the config is valid.
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);
54 # Return the final configuration hash.
57 # If an error message is returned, exit and print the error message.
59 die "Invalid configuration: $error";
64 ## ReadConfig (configfile) function.
66 ## This function is used to read a given configuration file and store the
67 ## values into a hash which will be returned.
72 # Hash to store the read-in configuration options and values.
75 # Check if the configfile exists and is read-able.
77 die "The given configfile ($file) does not exist, or is not read-able: $!";
80 # Open the config file and read-in all configuration options and values.
81 open(CONF
, "$file") or die "Could not open $file: $!";
83 # Process line by line.
84 while (my $line = <CONF
>) {
86 next if ($line =~ /\#/);
89 next if ($line =~ /^\s*$/);
91 # Remove any newlines.
94 # Remove any spaces at the start and end of the line.
98 # Check line lenght, skip it, if it is longer than, the
100 my $length = length("$line");
101 next if ($length gt $maxlength);
103 # Splitt line into two parts.
104 my ($option, $value) = split (/=/, $line);
106 # Add config option and value to the config hash.
107 $config{$option} = $value;
110 # Close the config file.
113 # Return the configuration hash.
118 ## The CheckConfig function.
120 ## This function is responsible to validate configure options which has
121 ## to be passed as a hash. It will return an error message which provides some
122 ## deeper details, if any problems have been detected.
124 sub CheckConfig
(\
%) {
125 # Dereference the given hash-ref and store
126 # them into a new temporary hash.
127 my %config = %{ $_[0] };
129 # If a BlockTime has been configured, check if the value is a natural number.
130 if (exists($config{BlockTime
})) {
131 # Get the configured value for "BlockTime".
132 my $value = $config{BlockTime
};
134 # Call subroutine for validation.
135 my $error = &check_number
("$value");
137 # If the check fails, immediately return an error message.
139 return "Invalid BlockTime: $error";
143 # If a BlockCount has been configured, check if the value is a natural number.
144 if (exists($config{BlockCount
})) {
145 # Get the configured value for "BlockCount".
146 my $value = $config{BlockCount
};
148 # Call subroutine for validation.
149 my $error = &check_number
("$value");
151 # If the check fails, immediately return an error message.
153 return "Invalid BlockCount: $error";
157 # XXX - add check for validating the configured loglevel.
159 # The config looks good, so return nothing (no error message).
164 ## The check_number subroutine.
166 ## This simple subroutine is used to check if a given string is numeric
167 ## and contains a natural number which has to be greater than zero.
169 sub check_number
($) {
172 # Check if the input is a natural number.
173 unless ($input =~ /^\d+$/) {
174 return "$input is not a natural number";
177 # Check if the number is greater than zero.
178 unless ($input gt "0") {
179 return "$input has to be greater than zero";
182 # Input is okay, return no error message (nothing).