diff options
Diffstat (limited to 'perl-install/security/msec.pm')
| -rw-r--r-- | perl-install/security/msec.pm | 400 |
1 files changed, 152 insertions, 248 deletions
diff --git a/perl-install/security/msec.pm b/perl-install/security/msec.pm index 1c7295a7f..4258653ef 100644 --- a/perl-install/security/msec.pm +++ b/perl-install/security/msec.pm @@ -1,283 +1,187 @@ package security::msec; -use diagnostics; use strict; +use MDK::Common; -use common; -use log; -sub get_secure_level { - my ($prefix) = @_; +#------------------------------------------------------------- +# msec options managment methods - cat_("$prefix/etc/profile") =~ /export SECURE_LEVEL=(\d+)/ && $1 || #- 8.0 msec - cat_("$prefix/etc/profile.d/msec.sh") =~ /export SECURE_LEVEL=(\d+)/ && $1 || #- 8.1 msec - ${{ getVarsFromSh("$prefix/etc/sysconfig/msec") }}{SECURE_LEVEL} || #- 8.2 msec - $ENV{SECURE_LEVEL}; + +#------------------------------------------------------------- +# option defaults + +sub load_defaults { + my ($msec, $category) = @_; + my $separator = $msec->{$category}{def_separator}; + map { + my ($opt, $val) = split(/$separator/, $_, 2); + chop $val; + if_($opt ne 'set_security_conf', $opt => $val); + } cat_($msec->{$category}{defaults_file}), if_($category eq "checks", 'MAIL_USER'); } -sub config_security_user { - my ($prefix, $sec_user) = @_; - my %t = getVarsFromSh("$prefix/etc/security/msec/security.conf"); - if (@_ > 1) { - $t{MAIL_USER} = $sec_user; - setVarsInSh("$prefix/etc/security/msec/security.conf", \%t); - } - $t{MAIL_USER}; + +# get_XXX_default(function) - +# return the default of the function|check passed in argument. + +sub get_check_default { + my ($msec, $check) = @_; + $msec->{checks}{default}{$check}; +} + +sub get_function_default { + my ($msec, $function) = @_; + $msec->{functions}{default}{$function}; } -sub get_functions { - my $prefix = $_; - 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 - enable_libsafe password_aging password_length create_server_link); - my $file = "$prefix/usr/share/msec/mseclib.py"; - my $function = ''; - - open F, $file; - while (<F>) { - if ($_ =~ /^def/) { - (undef, $function) = split(/ /, $_); - ($function, undef) = split(/\(/, $function); - if (!(member($function, @ignore_list))) { push(@functions, $function); } - } - } - close F; - @functions; + +#------------------------------------------------------------- +# option values + +sub load_values { + my ($msec, $category) = @_; + my $separator = $msec->{$category}{val_separator}; + map { + my ($opt, $val) = split /$separator/; + chop $val; + $val =~ s/[()]//g; + chop $opt if $separator eq '\('; # $opt =~ s/ //g if $separator eq '\('; + if_(defined($val), $opt => $val); + } cat_($msec->{$category}{values_file}); } -sub get_value { - my ($prefix, $function) = @_; - my $value = ''; - my $msec_options = "$prefix/etc/security/msec/level.local"; - my $msec_defaults = "$prefix/etc/security/msec/msec.defaults"; - my $found = 0; - - if (-e $msec_options) { - open F, $msec_options; - while(<F>) { - if($_ =~ /^$function/) { - (undef, $value) = split(/\(/, $_); - chop $value; chop $value; - $found = 1; - } - if ($found == 0) { $value = "default"; } - } - close F; - } - else { $value = "default"; } - $value; + +# get_XXX_value(check|function) - +# return the value of the function|check passed in argument. +# If no value is set, return "default". + +sub get_function_value { + my ($msec, $function) = @_; + exists $msec->{functions}{value}{$function} ? $msec->{functions}{value}{$function} : "default"; } -sub get_checks { - my ($prefix) = $_; - my %checks = (); - my $check_file = "$prefix/etc/security/msec/security.conf"; - my $def_check = "$prefix/var/lib/msec/security.conf"; - my @ignore_list = qw(MAIL_USER); - my $key = ''; - - open F, $def_check; - while (<F>) { - ($key, undef) = split(/=/, $_); - if(!(member($key, @ignore_list))) { $checks{$key} = "default"; } - } - close F; - - if (-e $check_file) { - open F, $check_file; - while (<F>) { - ($key, undef) = split(/=/, $_); - if(!(member($key, @ignore_list))) { - (undef, $checks{$key}) = split(/=/, $_); - chop $checks{$key}; - } - } - close F; - } +sub get_check_value { + my ($msec, $check) = @_; + $msec->{checks}{value}{$check} || "default"; +} + + - %checks; +#------------------------------------------------------------- +# get list of check|functions + +# list_(functions|checks) - +# return a list of functions|checks handled by level.local|security.conf + +sub raw_checks_list { + my ($msec) = @_; + keys %{$msec->{checks}{default}}; } -sub config_option { - my ($prefix, $option, $value, $category) =@_; - my %options_hash = ( ); - my $key = ""; - my $options_file = ""; - - if($category eq "functions") { $options_file = "$prefix/etc/security/msec/level.local"; } - elsif($category eq "checks") { $options_file ="$prefix/etc/security/msec/security.conf"; } - - if(-e $options_file) { - open F, $options_file; - if($category eq "functions") { - while(<F>) { - if (!($_ =~ /^from mseclib/) && $_ ne "\n") { - my ($name, $value_set) = split (/\(/, $_); - chop $value_set; chop $value_set; - $options_hash{$name} = $value_set; - } - } - } - elsif($category eq "checks") { - %options_hash = getVarsFromSh($options_file); - } - close F; - } - - $options_hash{$option} = $value; - - open F, '>'.$options_file; - if ($category eq "functions") { print F "from mseclib import *\n\n"; } - foreach $key (keys %options_hash) { - if ($options_hash{$key} ne "default") { - if($category eq "functions") { print F "$key"."($options_hash{$key})\n"; } - elsif($category eq "checks") { print F "$key=$options_hash{$key}\n"; } - } - } - close F; +sub list_checks { + my ($msec) = @_; + difference2([ $msec->raw_checks_list ], [ qw(MAIL_WARN MAIL_USER) ]); } -sub get_options { - my ($prefix, $category) = @_; - my %options = (); - my @functions = (); - my @checks = (); - my $key = ""; - - if ($category eq "functions") { - @functions = get_functions($prefix); - foreach $key (@functions) { - $options{$key} = get_value($prefix, $key); - } - } - elsif ($category eq "checks") { - %options = get_checks($prefix); - } +sub list_functions { + my ($msec, $category) = @_; - %options; + ## 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_xauth_from_root 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)]); + + # get all function names; filter out those which are in the ignore + # list, return what lefts. + grep { !member($_, @ignore_list) && member($_, @{$options{$category}}) } keys %{$msec->{functions}{default}}; } -sub get_default { - my ($prefix, $function, $security) = @_; - my $default_file = "$prefix/usr/share/msec/level.".$security; - my $default_value = ""; - - open F, $default_file; - while(<F>) { - if ($_ =~ /^$function/) { (undef, $default_value) = split(/ /, $_); } - } - close F; - chop $default_value; - $default_value; + +#------------------------------------------------------------- +# set back checks|functions values + +sub set_function { + my ($msec, $function, $value) = @_; + $msec->{functions}{value}{$function} = $value; } -sub choose_security_level { - my ($in, $security, $libsafe, $email, $signal) = @_; - - my %l = ( - 0 => _("Welcome To Crackers"), - 1 => _("Poor"), - 2 => _("Standard"), - 3 => _("High"), - 4 => _("Higher"), - 5 => _("Paranoid"), - ); - - my %help = ( - 0 => _("This level is to be used with care. It makes your system more easy to use, - but very sensitive: it must not be used for a machine connected to others - or to the Internet. There is no password access."), - 1 => _("Password are now enabled, but use as a networked computer is still not recommended."), - 2 => _("This is the standard security recommended for a computer that will be used to connect to the Internet as a client."), - 3 => _("There are already some restrictions, and more automatic checks are run every night."), - 4 => _("With this security level, the use of this system as a server becomes possible. - The security is now high enough to use the system as a server which can accept - connections from many clients. Note: if your machine is only a client on the Internet, you should choose a lower level."), - 5 => _("This is similar to the previous level, but the system is entirely closed and security features are at their maximum."), - ); - - delete @l{0,1}; - delete $l{5} if !$::expert; - - $in->ask_from( - ("DrakSec Basic Options"), - ("Please choose the desired security level") . "\n\n" . - join('', map { "$l{$_}: " . formatAlaTeX($help{$_}) . "\n\n" } keys %l), - [ - { label => _("Security level"), val => $security, list => [ sort keys %l ], format => sub { $l{$_} } }, - if_($in->do_pkgs->is_installed('libsafe') && arch() =~ /^i.86/, - { label => _("Use libsafe for servers"), val => $libsafe, type => 'bool', text => - _("A library which defends against buffer overflow and format string attacks.") } ), - { label => _("Security Administrator (login or email)"), val => $email }, - { val => _("Advanced Options"), type => 'button', clicked_may_quit => sub { $$signal = 1; } } - ], - ); +sub set_check { + my ($msec, $check, $value) = @_; + $msec->{checks}{value}{$check} = $value; } -sub choose_functions { - my ($in, $rfunctions, $signal, $security) = @_; - my $i = 0; - my @display = (); - my $key = ""; - my $default = ""; - - foreach $key (keys %$rfunctions) { - $default = get_default('', $key, $security); - if ($default eq "yes" || $default eq "no") { - $display[$i] = { label => $key." (default=$default)", val =>\$$rfunctions{$key}, list => ["yes", "no", "default"] }; - } - elsif ($default eq "ALL" || $default eq "NONE" || $default eq "LOCAL") { - $display[$i] = { label => $key." (default=$default)", val =>\$$rfunctions{$key}, list => ["ALL", "NONE", "LOCAL", "default"] }; - } - else { - $display[$i] = { label => $key." (default=$default)", val => \$$rfunctions{$key} }; + +#------------------------------------------------------------- +# apply configuration + +# config_(check|function)(check|function, value) - +# Apply the configuration to 'prefix'/etc/security/msec/security.conf||/etc/security/msec/level.local + +sub apply_functions { + my ($msec) = @_; + my @list = sort($msec->list_functions('system'), $msec->list_functions('network')); + touch($msec->{functions}{values_file}) if !-e $msec->{functions}{values_file}; + substInFile { + foreach my $function (@list) { s/^$function.*\n// } + if (eof) { + $_ .= join("\n", if_(!$_, ''), (map { + my $value = $msec->get_function_value($_); + if_($value ne 'default', "$_ ($value)"); + } @list), ""); } - $i++; - } - - # TODO find a way to add the button outside of the scrolling zone - $in->ask_from( - ("DrakSec - Advanced Options"), - ("You can customize the following options. For more information, see the mseclib manual page."), - [ - { val => _("Basic Options"), type => 'button', clicked_may_quit => sub { $$signal = 0; print ""; } }, - { val => _("Security Checks"), type => 'button', clicked_may_quit => sub {$$signal = 2; print ""; } }, - @display - ], - ); + } $msec->{functions}{values_file}; } -sub choose_checks { - my ($in, $roptions, $signal, $security) = @_; - my $i = 0; - my @display = (); - my $key = ''; - my $def_checks = "/var/lib/msec/security.conf"; +sub apply_checks { + my ($msec) = @_; + my @list = sort $msec->raw_checks_list; + setVarsInSh($msec->{checks}{values_file}, + { + map { + my $value = $msec->get_check_value($_); + if_($value ne 'default', $_ => $value); + } @list + } + ); +} - my %defaults = getVarsFromSh($def_checks); +sub reload { + my ($msec) = @_; + require security::level; + my $num_level = security::level::get(); + $msec->{functions}{defaults_file} = "$::prefix/usr/share/msec/level.$num_level"; + $msec->{functions}{default} = { $msec->load_defaults('functions') }; +} - foreach $key (keys %$roptions) { - if ($defaults{$key} eq "yes" || $defaults{$key} eq "no") { - $display[$i] = { label => $key." (default=$defaults{$key})", val => \$$roptions{$key}, list => ["yes", "no", "default"] }; - } - else { $display[$i] = { label => $key." (default=$defaults{$key})", val => \$$roptions{$key} }; - } - $i++; - } - - $in->ask_from( - ("DrakSec - Security Checks"), - ("You can customize the following security checks. For more information, see the mseclib manual page."), - [ - { val => _("Basic Options"), type => 'button', clicked_may_quit => sub { $$signal = 0; print ""; } }, - { val => _("Advanced Options"), type => 'button', clicked_may_quit => sub { $$signal = 1; print ""; } }, -# { val => _("Firewall Configuration"), type => 'button', clicked_may_quit => sub { $$signal = 3; print ""; } }, - @display - ], - ); +sub new { + my ($type) = @_; + my $msec = bless {}, $type; + + $msec->{functions}{values_file} = "$::prefix/etc/security/msec/level.local"; + $msec->{checks}{values_file} = "$::prefix/etc/security/msec/security.conf"; + $msec->{checks}{defaults_file} = "$::prefix/var/lib/msec/security.conf"; + $msec->{checks}{val_separator} = '='; + $msec->{functions}{val_separator} = '\('; + $msec->{checks}{def_separator} = '='; + $msec->{functions}{def_separator} = ' '; + $msec->reload; + + $msec->{checks}{default} = { $msec->load_defaults('checks') }; + $msec->{functions}{value} = { $msec->load_values('functions') }; + $msec->{checks}{value} = { $msec->load_values('checks') }; + $msec; } 1; |
