summaryrefslogtreecommitdiffstats
path: root/perl-install/security/msec.pm
blob: 9057050030fad337c368318c9f46e9171afc108b (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package security::msec;

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

$VERSION = "0.2";

=head1 NAME

msec - Perl functions to handle msec configuration files

=head1 SYNOPSYS

    require security::msec;

    my $msec = new msec;

    $secure_level = get_secure_level;

    @functions = $msec->get_functions;
    foreach @functions { %options{$_} = $msec->get_function_value($_) }
    foreach @functions { %defaults{$_} = $msec->get_function_default($_) }
    foreach @functions { $msec->config_function($_, %options{$_}) }

    @checks = $msec->get_checks;
    foreach @checks { %options{$_} = $msec->get_check_value($_) }
    foreach @checks { %defaults{$_} = $msec->get_check_default($_) }
    foreach @checks { $msec->config_check($_, %options{$_}) }

=head1 DESCRIPTION

C<msec> is a perl module used by draksec to customize the different options
that can be set in msec's configuration files.

=head1 COPYRIGHT

Copyright (C) 2000,2001,2002 MandrakeSoft <cbelisle@mandrakesoft.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

=cut

use MDK::Common;


my $check_file = "$::prefix/etc/security/msec/security.conf";
# ***********************************************
#              PRIVATE FUNCTIONS
# ***********************************************

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

    if ($category eq "functions") {
        my $word_level = get_secure_level();
        if ($word_level eq "Dangerous") { $num_level = 0 }
        elsif ($word_level eq "Poor") { $num_level = 1 }
        elsif ($word_level eq "Standard") { $num_level = 2 }
        elsif ($word_level eq "High") { $num_level = 3 }
        elsif ($word_level eq "Higher") { $num_level = 4 }
        elsif ($word_level eq "Paranoid") { $num_level = 5 }
        $default_file = "$::prefix/usr/share/msec/level.".$num_level;
    }
    elsif ($category eq "checks") { $default_file = "$::prefix/var/lib/msec/security.conf"; }

    open F, $default_file;
    while(<F>) {
	   if ($_ =~ /^$option/) { (undef, $default_value) = split(/$category eq "functions" ? ' ' : '=' /o, $_); }
    }
    close F;
    chop $default_value;

    $default_value;
}


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

# get_secure_level() - Get the secure level

# duplicated with some drakx code

sub get_secure_level {
    shift;
    my $num_level = 2;

    $num_level = cat_("$::prefix/etc/profile")           =~ /export SECURE_LEVEL=(\d+)/ && $1 ||
                cat_("$::prefix/etc/profile.d/msec.sh") =~ /export SECURE_LEVEL=(\d+)/ && $1 ||
                ${{ getVarsFromSh("$::prefix/etc/sysconfig/msec") }}{SECURE_LEVEL};
		# || $ENV{SECURE_LEVEL};

    my @sec_levels = ("Dangerous",  "Poor", "Standard", "High",  "Higher", "Paranoid");
    return $sec_levels[$num_level];}

sub get_seclevel_list {
    qw(Standard High Higher Paranoid);
}

sub set_secure_level {
    my $word_level = $_[1];

    my %sec_levels = ("Dangerous" => 0,  "Poor" => 1, "Standard" => 2, "High" => 3,  "Higher" => 4, "Paranoid" => 5);
    my $run_level = $sec_levels{$word_level};
    system "/usr/sbin/msec", $run_level ? $run_level : 3;
}

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

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

    ## 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.
    open F, $file;
    while (<F>) {
        if ($_ =~ /^def/) {
            (undef, $function) = split(/ /, $_);
            ($function, undef) = split(/\(/, $function);
            if (!(member($function, @ignore_list))) {
                push(@functions, $function) if (member($function, @{$options{$category}}));
            }
        }
    }
    close F;

    @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 {
    my ($function) = @_;
    my $value = '';
    my $msec_options = "$::prefix/etc/security/msec/level.local";
    my $found = 0;

    if (-e $msec_options) {
        open F, $msec_options;
        while(<F>) {
            if($_ =~ /^$function/) {
                (undef, $value) = split(/\(/, $_);
                chop $value; chop $value;
                $found = 1;
            }
        }
        close F;
	if ($found == 0) { $value = "default" }
    }
    else { $value = "default" }

    $value;
}

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

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

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

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

# get_checks() -
#   return a list of periodic checks handled by security.conf
sub get_checks {
    my $check;
    my @checks = ();

    my $check_file = "$::prefix/var/lib/msec/security.conf";
    my @ignore_list = qw(MAIL_USER);

    if (-e $check_file) {
        open F, $check_file;
        while (<F>) {
            ($check, undef) = split(/=/, $_);
            if(!(member($check, @ignore_list))) { push(@checks, $check) }
        }
        close F;
    }

    @checks;
}

# get_check_value(check)
#   return the value of the check passed in argument
sub get_check_value {
    shift;
    my ($check) = @_;
    my $check_file = $check_file;
    my $value = '';
    my $found = 0;

    if (-e $check_file) {
        open F, $check_file;
	   while(<F>) {
            if($_ =~ /^$check/) {
                (undef, $value) = split(/=/, $_);
			 chop $value;
			 $found = 1;
			 close F;
            }
        }
	   close F;
	   $value = "default" if ($found == 0);
    }
    else { $value = "default" }

    $value;
}

# 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 {
    shift;
    my ($check, $value) = @_;
    if ($value eq 'default') {
	   substInFile { s/^$check.*// } $check_file;
    } else {
	   setVarsInSh($check_file, { $check => $value });
    }
}

sub new { shift }
1;