]> mj.ucw.cz Git - pciids.git/blob - PciIds/Config.pm
Untaint data when sending mail
[pciids.git] / PciIds / Config.pm
1 package PciIds::Config;
2 use strict;
3 use warnings;
4 use PciIds::Startup;
5 use base 'Exporter';
6
7 our @EXPORT = qw(&checkConf &defConf %config &confList);
8
9 our %config;
10
11 sub loadConf() {
12         open CONFIG, $directory."/config" or die "Config file not found. Make sure config is in the directory and the correct path is in Startup.pm\n";
13         foreach( <CONFIG> ) {
14                 next if( /^\s*(|#.*)$/ );
15                 chomp;
16                 my( $name, $val );
17                 die "Invalid syntax on line $_\n" unless( ( $name, $val ) = /^\s*(.*\S)\s*=\s*(.*\S)\s*$/ );
18                 $val =~ s/^"(.*)"$/$1/;
19                 ( $val ) = ( $val =~ /(.*)/ ); #Untaint the value - config is considered part of the program
20                 $config{$name} = $val;
21         }
22         close CONFIG;
23 }
24
25 sub checkConf( $ ) {
26         my( $names ) = @_;
27         foreach( @{$names} ) {
28                 die "Variable not set: $_\n" unless( defined $config{$_} );
29         }
30 }
31
32 sub defConf( $ ) {
33         my( $underlay ) = @_;
34         foreach( keys %{$underlay} ) {
35                 $config{$_} = $underlay->{$_} unless( defined $config{$_} );
36         }
37 }
38
39 sub confList( $ ) {
40         my( $names ) = @_;
41         my( @result );
42         push @result, $config{$_} foreach( @{$names} );
43         return( @result );
44 }
45
46 loadConf();
47
48 return 1;