summaryrefslogtreecommitdiffstats
path: root/proxy_wizard/Squid.pm
blob: ebcd2dfcf19d05cf8aa1bef02230347fc2af453f (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
#!/usr/bin/perl

# Drakwizard

# Copyright (C) 2002,2003 Mandrakesoft
#
# Authors: Arnaud Desmons <adesmons@mandrakesoft.com> 
#          Florent Villard <warly@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.

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 (member($o->{var}{wiz_squid_port}, qw(3128 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;