File: config_vars.pl

package info (click to toggle)
fwknop 1.9.12-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,696 kB
  • ctags: 604
  • sloc: perl: 14,617; ansic: 1,258; sh: 462; makefile: 88
file content (109 lines) | stat: -rwxr-xr-x 3,275 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/perl -w
#
############################################################################
#
# File: config_vars.pl
#
# Purpose: To provide basic usage validation for cipherdyne.org project
#          variables.
#
############################################################################
#
# $Id: config_vars.pl 571 2006-10-16 02:41:41Z mbr $
#

use strict;

my $config_file = 'config_vars.conf';

open C, "< $config_file" or die $!;
my @lines = <C>;
close C;

my %config = ();
for my $line (@lines) {
    next unless $line;
    next if $line =~ /^\s*#/;
    if ($line =~ /^\s*(\S+)\s+(\S+)/) {
        $config{$1}{$2} = '';
    }
}

PROG: for my $prog (keys %config) {
    unless (-e $prog) {
        print "[-] program: $prog does not exist in current directory.\n";
        next PROG;
    }
    open F, "< $prog" or die "[*] Could not open $prog: $!";
    my @prog_lines = <F>;
    close F;
    my %config_vars = ();
    my %used_vars   = ();
    CONF: for my $config (keys %{$config{$prog}}) {
        unless (-e $config) {
            print "[-] config: $config for program: $prog does not exist.\n";
            next CONF;
        }
        open F, "< $config" or die "[*] Could not open $config: $!";
        my @config_lines = <F>;
        close F;
        for my $line (@config_lines) {
            next unless $line;
            next unless $line =~ /\S/;
            next if $line =~ /^\s*#/;
            if ($line =~ /^\s*(\S+)\s/) {
                $config_vars{$1} = '';
            }
        }
    }
    my $line_num = 1;
    ### see if the program is using an undefined configuration
    ### variable
    for my $line (@prog_lines) {
        if ($prog =~ /\.c/) {  ### C code file
            if ($line =~ m|\"(\w+)\s+\"|) {
                my $var = $1;
                unless (defined $config_vars{$var}) {
                    print "[-] Config var: $var (line $line_num) ",
                        "is not defined in config files for program: $prog\n";
                }
                $used_vars{$var} = '';
            }
        } else {
            my $var1 = '';
            my $var2 = '';
            if ($line =~ m|\$config\{\'(\S+)\'\}|) {
                $var1 = $1;
            }
            if ($line =~ m|\$cmds\{\'(\S+)\'\}|) {
                $var2 = "$1Cmd";
            }
            if ($var1) {
                unless (defined $config_vars{$var1}) {
                    print "[-] Config var: $var1 (line $line_num) ",
                        "is not defined in config files for program: $prog\n";
                }
                $used_vars{$var1} = '';
            }
            if ($var2) {
                unless (defined $config_vars{$var2}) {
                    print "[-] Config var: $var2 (line $line_num) ",
                        "is not defined in config files for program: $prog\n";
                }
                $used_vars{$var2} = '';
            }
        }
        $line_num++;
    }

    ### see if the config files define a configuration variable
    ### that is not used by the program
    for my $var (sort keys %config_vars) {
        unless (defined $used_vars{$var}) {
            print "[-] $var is defined in config files, ",
                "but not used in $prog\n";
        }
    }
}

exit 0;