summaryrefslogtreecommitdiffstats
path: root/perl-install/security/msec.pm
blob: 35d229a4d2082db7312df5763d25fdbeb82cb7d9 (plain)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package security::msec;

use strict;
use vars qw($VERSION);
use MDK::Common::File;
use MDK::Common;

$VERSION = "0.2";



my $check_file = "$::prefix/etc/security/msec/security.conf";



# ***********************************************
#              PRIVATE FUNCTIONS
# ***********************************************

my $num_level;

sub get_default {
    my ($option, $category) = @_;
    my $default_file = "";
    my $default_value = "";
    my $num_level = 0;

    if ($category eq "functions") {
        require security::level;
        $num_level ||= security::level::get();
        $default_file = "$::prefix/usr/share/msec/level.".$num_level;
    }
    elsif ($category eq "checks") { $default_file = "$::prefix/var/lib/msec/security.conf" }

    foreach (cat_($default_file)) {
	   if ($category eq 'functions') {
		  (undef, $default_value) = split / / if /^$option/;
	   } elsif ($category eq 'checks') {
		  (undef, $default_value) = split /=/ if /^$option/;
	   }
    }
    chop $default_value;
    $default_value;
}

sub get_value {
    my ($item, $category) = @_;
    my $value = '';
    my $item_file =
      $category eq 'functions' ? "$::prefix/etc/security/msec/level.local" :
      $category eq 'checks' ? $check_file : '';

    foreach (cat_($item_file)) {
	/^$item/ or next;

	if ($category eq 'functions') {
	    my $i = $_;
	    (undef, $_) = split /\(/;
	    s/[()]//g;
	    $value = $_;
	    $_ = $i;
	} elsif ($category eq 'checks') {
	    (undef, $value) = split(/=/, $_);
	}
	chop $value;
	return $value;
    }
    "default";
}

# ***********************************************
#               SPECIFIC OPTIONS
# ***********************************************


# ***********************************************
#         FUNCTIONS (level.local) RELATED
# ***********************************************

# get_functions() -
#   return a list of functions handled by level.local (see
#   man mseclib for more info).
sub get_functions {
    my (undef, $category) = @_;
    my @functions;

    ## TODO handle 3 last functions here so they can be removed from this list
    my @ignore_list = qw(indirect commit_changes closelog error initlog log set_secure_level
					set_security_conf set_server_level print_changes get_translation create_server_link);

    my %options = (
	    'network' => [qw(accept_bogus_error_responses accept_broadcasted_icmp_echo accept_icmp_echo
					enable_dns_spoofing_protection enable_ip_spoofing_protection
					enable_log_strange_packets enable_promisc_check no_password_aging_for)],
	    'system' =>  [qw(allow_autologin allow_issues allow_reboot allow_remote_root_login
                         allow_root_login allow_user_list allow_x_connections allow_xserver_to_listen
                         authorize_services enable_at_crontab enable_console_log
                         enable_msec_cron enable_pam_wheel_for_su enable_password enable_security_check
                         enable_sulogin password_aging password_history password_length set_root_umask
                         set_shell_history_size set_shell_timeout set_user_umask)]);

    my $file = "$::prefix/usr/share/msec/mseclib.py";
    my $function;

    # read mseclib.py to get each function's name and if it's
    # not in the ignore list, add it to the returned list.
    foreach (cat_($file)) {
        if (/^def/) {
            (undef, $function) = split / /;
            ($function, undef) = split(/\(/, $function);
            if (!member($function, @ignore_list) && member($function, @{$options{$category}})) {
                push(@functions, $function)
            }
        }
    }

    @functions;
}

# get_function_value(function) -
#   return the value of the function passed in argument. If no value is set,
#   return "default".
sub get_function_value {
    shift;
    get_value(@_, 'functions');
}

# get_function_default(function) -
#   return the default value of the function according to the security level
sub get_function_default {
    shift;
    return get_default(@_, "functions");
}

# config_function(function, value) -
#   Apply the configuration to 'prefix'/etc/security/msec/level.local
sub config_function {
    my (undef, $function, $value) = @_;
    my $options_file = "$::prefix/etc/security/msec/level.local";

    substInFile { s/^$function.*\n// } $options_file;
    append_to_file($options_file, "$function ($value)") if $value ne 'default';
}

# ***********************************************
#     PERIODIC CHECKS (security.conf) RELATED
# ***********************************************

# get_default_checks() -
#   return a list of periodic checks handled by security.conf
sub get_default_checks {
    map { if_(/(.*?)=/, $1) } cat_("$::prefix/var/lib/msec/security.conf");
}

# get_check_value(check)
#   return the value of the check passed in argument
sub get_check_value {
    shift;
    get_value(@_, 'checks');
}

# get_check_default(check)
#   Get the default value according to the security level
sub get_check_default {
    my ($check) = @_;
    return get_default($check, 'checks');
}

# config_check(check, value)
#   Apply the configuration to "$::prefix"/etc/security/msec/security.conf
sub config_check {
    my (undef, $check, $value) = @_;
    if ($value eq 'default') {
	   substInFile { s/^$check.*\n// } $check_file;
    } else {
	   setVarsInSh($check_file, { $check => $value });
    }
}

sub new { shift }
1;