#!/usr/bin/perl # Drakwizard # Copyright (C) 2002 Arnaud Desmons # Copyright (C) 2003 Florent Villard # # 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. package MDK::Wizard::Squid; use strict; use common; use services; 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 => 'Squid wizard', var => { wiz_squid_level => '', wiz_squid_mem => '', wiz_squid_port => '', wiz_squid_mynetw => '', wiz_squid_defdir => '/var/spool/squid', wiz_squid_cachepeer => '', wiz_squid_peerport => '', wiz_squid_menupeer => '', wiz_squid_disk => '' }, needed_rpm => [ 'squid' ], defaultimage => "$ENV{__WIZ_HOME__}proxy_wizard/images/proxy.png" }; $o->{pages} = { welcome => { 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 => 'port' }, 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}{wiz_squid_port} ||= 3128; }, post => \&testport, data => [ { label => N('Proxy port:'), val => \$o->{var}{wiz_squid_port} }, ], 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' }, error_low => { name => N('Warning.') . "\n\n" . N('You must choose a port greater than 1024 and lower than 65535') . "\n\n" . 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 "192.168.1.0/255.255.255.0" or a text format like ".domain.net"'), 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 "192.168.1.0/255.255.255.0" or a text format like ".domain.net"') }, ], 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 "No upper level proxy" 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' }, valcachepeer => { name => N('Cache hierarchy') . "\n\n" . N('Enter the qualified hostname (like "cache.domain.net") 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('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} }, { 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') . N('The wizard successfully configured your proxy server.'), end => 1, next => 0 }, }; sub new { my ($class, $conf) = @_; bless { o => $o, }, $class; } sub network_mask { 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 ufs /var/spool/squid 100 16 256.*|cache_dir ufs $o->{var}{wiz_squid_defdir} $o->{var}{wiz_squid_disk} 16 256|; s|^\s*\#?\s*acl\s*mynetwork\s*src.*|acl mynetwork src $o->{var}{wiz_squid_mynetw}|; s|^\s*\#?\s*(cache_mem\s*)\d+(\s*MB.*)|$1$o->{var}{wiz_squid_mem} MB|; s|^\s*\#?\s*(http_port\s*)\d+(.*)|$1$o->{var}{wiz_squid_port}|; } $file; 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;