From e4371f18753c98687f71de82810acc79428c51f9 Mon Sep 17 00:00:00 2001 From: Florent Villard Date: Fri, 5 Sep 2003 11:42:20 +0000 Subject: *** empty log message *** --- proxy_wizard/Squid.pm | 240 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 213 insertions(+), 27 deletions(-) (limited to 'proxy_wizard') diff --git a/proxy_wizard/Squid.pm b/proxy_wizard/Squid.pm index 3c1d87d9..001cabf5 100755 --- a/proxy_wizard/Squid.pm +++ b/proxy_wizard/Squid.pm @@ -26,58 +26,152 @@ use common; require MDK::Wizard::Wizcommon; my $wiz = new MDK::Wizard::Wizcommon; +my $df = -d '/var/spool/squid' ? `/bin/df -m /var/spool/squid` : `/bin/df -m /var/spool`; +my ($free_disk) = $df =~ m/\d+\s+\d+\s+\d+\s+(\d+)/; +my $mem = `/usr/bin/free -m`; +($mem) = $mem =~ /Mem:\s+(\d+)/; +my %level = ( + 1 => N('All - No access restriction'), + 2 => N('Local Network - access for local network (recommended)'), + 3 => N('Localhost - access restricted to this server only') +); +my %upper = ( + 1 => N('No upper level proxy (recommended)'), + 2 => N('Define an upper level proxy') +); my $o = { - name => 'configuration wizard', + name => 'Squid wizard', var => { - ip1 => '', - ip2 => '' - } + wiz_squid_level => '', + wiz_squid_mem => '', + wiz_squid_port => '', + wiz_squid_mynetw => '', + wiz_squid_defdir => '', + wiz_squid_cachepeer => '', + wiz_squid_peerport => '', + wiz_squid_menupeer => '', + wiz_squid_disk => '' + }, + needed_rpm => [ 'squid' ], + defaultimage => "$ENV{__WIZARD_HOME__}proxy_wizard/images/proxy.png" }; $o->{pages} = { welcome => { - name => N('') . "\n\n" . N('') . "\n\n" . N(''), + name => N('Proxy Configuration Wizard') . "\n\n" . N('Squid is a web caching proxy server, it allows faster web access for your local network.') . "\n\n" . N('This wizard will help you in configuring your proxy server.'), no_back => 1, - next => 'ip_range' + next => 'port' }, - confige => { - name => N('') . "\n\n" . N('') . "\n\n" . N(''), + port => { + name => N('Proxy Port') . "\n\n" . N('Proxy port value sets what port the proxy server will listen on for http requests. Default is 3128, other common value can be 8080, the port value needs to be greater than 1024.') . "\n\n" . N(''), pre => sub { - $o->{var}{ip1} ||= f1(); - $o->{var}{ip2} ||= f2(); + $o->{var}{wiz_squid_port} ||= 3128; }, - post => \&check, + post => \&testport, data => [ - { label => '' }, - { label => N(''), val => \$o->{var}{ip1} }, - { label => N(''), val => \$o->{var}{ip2} }, + { label => N('Proxy port:'), val => \$o->{var}{wiz_squid_port} }, ], - next => 'summary' + next => 'memory' + }, + warning_port => { + name => N('Warning.') . "\n\n" . N('You have entered a port that may be useful for this service:') . "\n\n" . N('Press Next if you want to keep this value, or Back to correct your choice.'), + data => [ + { label => '/etc/services:', fixed_val => \$o->{var}{used_by} }, + ], + next => 'memory' }, - warning => { + error_low => { name => N('Warning.'), - data => [ { label => N('') } ], - next => 'summary' + data => [ + { label => N('You must choose a port greater than 1024 and lower than 65535') }, + { label => N('Press back to change the value.') } + ], + next => 'memory' + }, + memory => { + name => N('Proxy Cache Size') . "\n\n" . N('Memory Cache is the amount of RAM dedicated to cache memory operations (note that actual memory usage of the whole squid process is bigger).') . "\n\n" . N('Disk Cache is the amount of disk space that can be used for caching on disk.') . "\n\n" . N('For your information, here is /var/spool/squid space on disk:') . "\n\n" . $df, + pre => sub { + $o->{var}{wiz_squid_mem} ||= int ($mem / 4); + $o->{var}{wiz_squid_disk} ||= int ($free_disk/2); + }, + data => [ + { label => N('Memory cache (MB):'), val => \$o->{var}{wiz_squid_mem} }, + { label => N('Disk space (MB):'), val => \$o->{var}{wiz_squid_disk} }, + ], + next => 'level' + }, + level => { + name => N('Access Control') . "\n\n" . N('The proxy can be configured to use different access control levels') . "\n\n" . N('Choose the level that suits your needs. If you don\'t know, the Local Network level is usually the most appropriate. Beware that the All level may be not secure.'), + pre => sub { + $o->{var}{wiz_squid_level} ||= 2; + }, + data => [ + { label => '', val => \$o->{var}{wiz_squid_level}, list => [ keys %level ], format => sub { $level{$_[0]} } }, + ], + post => sub { + return 'shownet' if $o->{var}{wiz_squid_level} == 2 + }, + next => 'cachepeer' + }, + shownet => { + name => N('Grant access on local network') . "\n\n" . N('Access will be allowed for hosts on the network. Here is the information found about your current local network, you can modify it if needed.') . "\n\n" . N('You can use either a numeric format like \q192.168.1.0/255.255.255.0\q or a text format like \q.domain.net\q'), + pre => sub { + $o->{var}{wiz_squid_mynetw} ||= network_mask() + }, + data => [ + { label => 'Authorized network:', val => \$o->{var}{wiz_squid_mynetw}, help => N('Use numeric format like \q192.168.1.0/255.255.255.0\q or a text format like \q.domain.net\q') }, + ], + next => 'cachepeer' + }, + cachepeer => { + name => N('Cache hierarchy') . "\n\n" . N('As an option, Squid can be configured in proxy cascading. You can add a new upper level proxy by specifying its hostname and port.') . "\n\n" . N('You can safely select \qNo upper level proxy\q if you don\'t need this feature.'), + pre => sub { + $o->{var}{wiz_squid_menupeer} ||= 1 + }, + data => [ + { label => '', val => \$o->{var}{wiz_squid_menupeer}, list => [ keys %upper ], format => sub { $upper{$_[0]} } }, + ], + post => sub { + return 'valcachepeer' if $o->{var}{wiz_squid_menupeer} == 2 + }, + next => 'summary_2' }, - error => { - name => N('Error.'), - data => [ { label => N('') } ], - next => 'config' + valcachepeer => { + name => N('Cache hierarchy') . "\n\n" . N('Enter the qualified hostname (like \qcache.domain.net\q) and the port of the proxy to use.'), + data => [ + { label => 'Upper level proxy hostname:', val => \$o->{var}{wiz_squid_cachepeer} }, + { label => 'Upper level proxy port:', val => \$o->{var}{wiz_squid_peerport} }, + ], + next => 'summary' }, summary => { - name => N('') . "\n\n" . N('') . "\n\n" . N(''), + name => N('Configuring the Proxy') . "\n\n" . N('The wizard collected the following parameters needed to configure your proxy:') . "\n\n" . N('To accept these values, and configure your server, click the Next button or use the Back button to correct them.'), data => [ - { label => N(''), type => 'field', val => \$o->{var}{ip1} }, - { label => '' }, - { label => N(''), type => 'field', val => \$o->{var}{ip2} }, + { label => N('Port:'), type => 'field', val => \$o->{var}{wiz_squid_port} }, + { label => N('Memory cache (MB):'), type => 'field', val => \$o->{var}{wiz_squid_mem} }, + { label => N('Disk space (MB):'), type => 'field', val => \$o->{var}{wiz_squid_disk} }, + { label => N('Access Control:'), type => 'field', val => \$o->{var}{wiz_squid_mynetw} }, + { label => N('Upper level proxy hostname:'), type => 'field', val => \$o->{var}{wiz_squid_cachepeer} }, + { label => N('Upper level proxy port:'), type => 'field', val => \$o->{var}{wiz_squid_peerport} }, + ], + post => \&do_it, + next => 'end' + }, + summary_2 => { + name => N('Configuring the Proxy') . "\n\n" . N('The wizard collected the following parameters needed to configure your proxy:') . "\n\n" . N('To accept these values, and configure your server, click the Next button or use the Back button to correct them.'), + data => [ + { label => N('Port:'), type => 'field', val => \$o->{var}{wiz_squid_port} }, + { label => N('Memory cache (MB):'), type => 'field', val => \$o->{var}{wiz_squid_mem} }, + { label => N('Disk space (MB):'), type => 'field', val => \$o->{var}{wiz_squid_disk} }, + { label => N('Access Control:'), type => 'field', val => \$o->{var}{wiz_squid_mynetw} }, ], post => \&do_it, next => 'end' }, end => { name => N('Congratulation'), - data => [ { label => N('') } ], + data => [ { label => N('The wizard successfully configured your proxy server.') } ], end => 1, next => 0 }, @@ -90,4 +184,96 @@ sub new { }, $class; } +sub network_mask { + my $o = IFCFG->new(); + my $wiz_ip_server = $wiz->{net}->itf_get("IPADDR"); + my $mask = $wiz->{net}->itf_get("NETMASK"); + "$1.$2.$3.0/$mask" if $wiz_ip_server =~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/; +} + +sub testport { + if ($o->{var}{wiz_squid_port} == 3128 || $o->{var}{wiz_squid_port} == 8080) { + return '' + } + elsif ($o->{var}{wiz_squid_port} <= 1024 || $o->{var}{wiz_squid_port} >= 65536) { + return 'error_low'; + } + $o->{var}{used_by} = `/bin/grep $o->{var}{wiz_squid_port} /etc/services`; + if ($o->{var}{used_by}) { + return 'warning_port' + } +} + +sub port { + my $ret; + my @lines = grep(/$o->{var}{wiz_squid_port}\/tcp/, cat_("/etc/services")); + foreach (@lines) { + s/\t/ /g; + $ret .= "\n$_"; + } + $ret; +} + +sub do_it { + $::testing and return; + my $file="/etc/squid/squid.conf"; + -f $file and MDK::Common::cp_af($file, $file.".orig"); + MDK::Common::cp_af("__WIZ_HOME__/proxy_wizard/scripts/squid.conf.default", $file); + substInFile { + s|^\s*\#?\s*(cache_dir.*$o->{var}{wiz_squid_defdir}\s*)\d*(.*)|$1$o->{var}{wiz_squid_disk}$2|; + s|^\s*\#?\s*(acl\s*mynetwork\s*src\s*).*$|$1$o->{var}{wiz_squid_mynetw}\n|; + s|^\s*\#?\s*(cache_mem\s*)\d*(\s*MB.*)|$1$o->{var}{wiz_squid_mem}$2|; + s|^\s*\#?\s*(http_port\s*)\d*(.*)|$1$o->{var}{wiz_squid_port}$2|; + } $file; + print "LEVEL $o->{var}{'wiz_squid_level'}\n\n"; + exit; + standalone::explanations("$file: cache_dir = $o->{var}{wiz_squid_defdir} $o->{var}{wiz_squid_disk} +mynetw = $o->{var}{wiz_squid_mynetw} cache_mem = $o->{var}{wiz_squid_mem} http_port = $o->{var}{wiz_squid_port} +level = $o->{var}{wiz_squid_level}"); + if ($o->{var}{wiz_squid_level} == 1) { + substInFile { + s|^\s*\#?\s*(http_access\s*)deny(\s*all.*)|\#$&\n$1allow$2|; + } $file; + } + elsif ($o->{var}{wiz_squid_level} == 2) { + substInFile { + s|^\s*\#?\s*(http_access\s*)allow(\s*all.*)|\#$&\n$1deny$2|; + s|^\s*\#?\s*(http_access\s*allow\s*)localhost|\#$&\n$1mynetwork|; + } $file; + } + elsif ($o->{var}{wiz_squid_level} == 3) { + substInFile { + s|^\s*\#?\s*(http_access\s*)allow(\s*all.*)|\#$&\n$1deny$2|; + s|^\s*\#?\s*(http_access\s*allow\s*)mynetwork|\#$&\n$1localhost|; + } $file; + } else { # should not happen + die "wiz_squid_level error"; + } + my $t = 0; + foreach (cat_($file)) { + if (/^\s*cache_peer.*/) { + $t = $_;; + last; + } + } + if ($o->{var}{wiz_squid_menupeer} == 1 && length $t) { + substInFile { + s|^\s*!\#\s*(cache_peer.*)|\#$&|; + } $file; + } + elsif ($o->{var}{wiz_squid_menupeer} == 2 && length $o->{var}{wiz_squid_cachepeer}) { + if (lenght $t) { + substInFile { + s|^\s*\#?\s*(cache_peer.*)|\#$&|; + } $file; + } + append_to_file($file, "cache_peer $o->{var}{wiz_squid_cachepeer} parent $o->{var}{wiz_squid_peerport} 3130"); + } + system("/sbin/chkconfig --level 345 squid on"); + if (services::is_service_running('squid')) { + services::restart('squid') + } else { + services::start('squid') + } +} 1; -- cgit v1.2.1