#!/usr/bin/perl # Drakwizard # Copyright (C) 2002,2003 Mandrakesoft # # Authors: Arnaud Desmons # 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; use MDK::Wizard::Wizcommon; my $wiz = new MDK::Wizard::Wizcommon; my $in = interactive->vnew; my $HOSTNAME = $wiz->{net}->network_get("HOSTNAME"); my $DOMAINNAME = chomp_(`dnsdomainname`); my $SHORTHOSTNAME = chomp_(`hostname -s`); if (!$HOSTNAME) { $HOSTNAME = chomp_(`hostname`) } my $df = common::to_utf8(scalar(-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 => N("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 => '' }, init => sub { my ($err, $msg) = test_host_domain($SHORTHOSTNAME, $DOMAINNAME); if (!$err) { $in->ask_warn(N("Error"), $msg); die 'wizcancel'; } ($err, $msg); }, needed_rpm => [ 'squid' ], defaultimage => "/usr/share/mcc/themes/default/drakproxy-mdk.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", 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:', val_ref => \$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 => N("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 => N("Upper level proxy hostname:"), val => \$o->{var}{wiz_squid_cachepeer} }, { label => N("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_ref => \$o->{var}{wiz_squid_port} }, { label => N("Memory cache (MB):"), type => 'field', val_ref => \$o->{var}{wiz_squid_mem} }, { label => N("Disk space (MB):"), type => 'field', val_ref => \$o->{var}{wiz_squid_disk} }, { label => N("Access Control:"), type => 'field', val_ref => \$o->{var}{wiz_squid_mynetw} }, { label => N("Upper level proxy hostname:"), type => 'field', val_ref => \$o->{var}{wiz_squid_cachepeer} }, { label => N("Upper level proxy port:"), type => 'field', val_ref => \$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_ref => \$o->{var}{wiz_squid_port} }, { label => N("Memory cache (MB):"), type => 'field', val_ref => \$o->{var}{wiz_squid_mem} }, { label => N("Disk space (MB):"), type => 'field', val_ref => \$o->{var}{wiz_squid_disk} }, { label => N("Access Control:"), type => 'field', val_ref => \$o->{var}{wiz_squid_mynetw} }, ], post => \&do_it, next => 'end' }, end => { name => N("Congratulations") . N("The wizard has successfully configured your proxy server."), end => 1, }, error_end => { name => N("Failed"), data => [ { label => N("Relaunch drakwizard, and try to change some parameters.") } ], no_back => 1, end => 1, }, }; sub new { my ($class) = @_; bless $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`; return 'warning_port' if $o->{var}{used_by}; } sub port { my $ret; my @lines = grep { m!$o->{var}{wiz_squid_port}/tcp! } cat_("/etc/services"); foreach (@lines) { s/\t/ /g; $ret .= "\n$_"; } $ret; } sub do_it { $::testing and return; my $in = 'interactive'->vnew('su', 'Squid Config'); check_starts_on_boot($in, 'squid'); my $w = $in->wait_message(N("Squid proxy"), N("Configuring your system as a Proxy server...")); 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 = find { /^\s*cache_peer/ } cat_($file); 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 (length($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"); } substInFile { s|^visible_hostname.*|visible_hostname $HOSTNAME|; } $file; system("/sbin/chkconfig --level 345 squid on"); services::restart_or_start('squid'); undef $w; check_started('squid'); } 1;