2 # Functions for configuring squid.conf
4 BEGIN { push(@INC, ".."); };
8 %access = &get_module_acl();
9 $auth_program = "$module_config_directory/squid-auth.pl";
10 $auth_database = "$module_config_directory/users";
11 @caseless_acl_types = ( "url_regex", "urlpath_regex", "proxy_auth_regex",
12 "srcdom_regex", "dstdom_regex", "ident_regex" );
14 # Get the squid version
15 if (open(VERSION, "$module_config_directory/version")) {
16 chop($squid_version = <VERSION>);
20 # choice_input(text, name, &config, default, [display, option]+)
21 # Display a number of radio buttons for selecting some option
24 local($v, $vv, $rv, $i);
25 $v = &find_config($_[1], $_[2]);
26 $vv = $v ? $v->{'value'} : $_[3];
27 $rv = "<td><b>$_[0]</b></td> <td valign=top>";
28 for($i=4; $i<@_; $i+=2) {
29 $rv .= "<input type=radio name=$_[1] value=\"".$_[$i+1]."\" ".
30 ($vv eq $_[$i+1] ? "checked" : "")."> $_[$i]\n";
35 # select_input(text, name, &config, default, [display, option]+)
36 # Like choice_input, but uses a drop-down select field
39 local($v, $vv, $rv, $i);
40 $v = &find_config($_[1], $_[2]);
41 $vv = $v ? $v->{'value'} : $_[3];
42 $rv = "<td><b>$_[0]</b></td> <td valign=top><select name=$_[1]>";
43 for($i=4; $i<@_; $i+=2) {
44 $rv .= "<option value=\"".$_[$i+1]."\" ".
45 ($vv eq $_[$i+1] ? "selected" : "")."> $_[$i]\n";
47 return $rv."</select></td>\n";
50 # save_choice(name, default, &config)
51 # Save a selection from choice_input()
54 if ($in{$_[0]} eq $_[1]) { &save_directive($_[2], $_[0], [ ]); }
55 else { &save_directive($_[2], $_[0], [{ 'name' => $_[0],
56 'values' => [ $in{$_[0]} ] }]); }
59 # list_input(text, name, &config, type, [default])
60 # Display a list of values
64 foreach $v (&find_config($_[1], $_[2])) {
65 push(@av, @{$v->{'values'}});
68 $opt = sprintf "<input type=radio name=$_[1]_def value=1 %s> $_[4]\n",
70 $opt .= sprintf "<input type=radio name=$_[1]_def value=0 %s>\n",
75 $rv = "<td valign=top><b>$_[0]</b></td> <td valign=top>";
76 if ($opt) { $rv .= "$opt Listed..<br>\n"; }
77 $rv .= "<textarea name=$_[1] rows=3 cols=15>".
78 join("\n", @av)."</textarea></td>\n";
82 $rv = "<td valign=top><b>$_[0]</b></td> <td colspan=3 valign=top>$opt";
83 $rv .= "<input name=$_[1] size=50 value=\"".join(' ',@av)."\"></td>\n";
88 # save_list(name, &checkfunc, &config)
91 local($v, @vals, $err);
92 if (!$in{"$_[0]_def"}) {
93 @vals = split(/\s+/, $in{$_[0]});
96 &check_error($_[1], $v);
100 if (@vals) { &save_directive($_[2], $_[0],
101 [{ 'name' => $_[0], values => \@vals }]); }
102 else { &save_directive($_[2], $_[0], [ ]); }
105 # check_error(&function, value)
109 local $err = &{$_[0]}($_[1]);
110 if ($err) { &error($err); }
113 # address_input(text, name, &config, type)
114 # Display a text area for entering 0 or more addresses
118 foreach $v (&find_config($_[1], $_[2])) {
119 push(@av, @{$v->{'values'}});
123 $rv = "<td valign=top><b>$_[0]</b></td> <td valign=top>";
124 $rv .= "<textarea name=$_[1] rows=3 cols=15>".
125 join("\n", @av)."</textarea></td>\n";
128 # one long text field
129 $rv = "<td valign=top><b>$_[0]</b></td> <td colspan=3 valign=top>";
130 $rv .= "<input name=$_[1] size=50 value=\"".join(' ',@av)."\"></td>\n";
135 # save_address(name, config)
139 foreach $addr (split(/\s+/, $in{$_[0]})) {
140 &check_ipaddress($addr) || &error(&text('lib_emsg1',$addr));
143 if (@vals) { &save_directive($_[1], $_[0],
144 [{ 'name' => $_[0], values => \@vals }]); }
145 else { &save_directive($_[1], $_[0], [ ]); }
148 # opt_input(text, name, &config, default, size, units)
149 # Display an optional field for entering something
153 $v = &find_config($_[1], $_[2]);
154 $rv = "<td valign=top><b>$_[0]</b></td> <td valign=top nowrap";
155 $rv .= $_[4] > 30 ? " colspan=3>\n" : ">\n";
156 $rv .= sprintf "<input type=radio name=$_[1]_def value=1 %s> $_[3]\n",
158 $rv .= sprintf "<input type=radio name=$_[1]_def value=0 %s> ",
160 $rv .= sprintf "<input name=$_[1] size=$_[4] value=\"%s\"> %s</td>\n",
161 $v ? $v->{'value'} : "", $_[5];
165 # save_opt(name, &function, &config)
166 # Save an input from opt_input()
169 if ($in{"$_[0]_def"}) { &save_directive($_[2], $_[0], [ ]); }
171 &check_error($_[1], $in{$_[0]});
172 local $dir = { 'name' => $_[0], 'values' => [ $in{$_[0]} ] };
173 &save_directive($_[2], $_[0], [ $dir ]);
177 # opt_time_input(text, name, &config, default, size)
180 local($v, $rv, $u, %ts );
181 $v = &find_config($_[1], $_[2]);
182 $rv = "<td valign=top><b>$_[0]</b></td> <td valign=top nowrap>\n";
183 $rv .= sprintf "<input type=radio name=$_[1]_def value=1 %s> $_[3]\n",
185 $rv .= sprintf "<input type=radio name=$_[1]_def value=0 %s> ",
187 $rv .= &time_fields($_[1], $_[4], $v ? @{$v->{'values'}} : ( ));
192 # time_field(name, size, time, units)
196 %ts = ( "second"=> $text{"lib_seconds"},
197 "minute"=> $text{"lib_minutes"},
198 "hour"=> $text{"lib_hours"},
199 "day"=> $text{"lib_days"},
200 "week"=> $text{"lib_weeks"},
201 "fortnight"=> $text{"lib_fortnights"},
202 "month"=> $text{"lib_months"},
203 "year"=> $text{"lib_years"},
204 "decade"=> $text{"lib_decades"} );
205 $rv .= sprintf "<input name=$_[0] size=$_[1] value=\"%s\">\n", $_[2];
206 $rv .= "<select name=$_[0]_u>\n";
207 foreach $u (keys %ts) {
208 $rv .= sprintf "<option value=$u %s>$ts{$u}\n",
209 $_[3] =~ /^$u/ ? "selected" : "";
211 $rv .= "</select>\n";
215 # save_opt_time(name, &config)
218 local %ts = ( "second"=> $text{"lib_seconds"},
219 "minute"=> $text{"lib_minutes"},
220 "hour"=> $text{"lib_hours"},
221 "day"=> $text{"lib_days"},
222 "week"=> $text{"lib_weeks"},
223 "fortnight"=> $text{"lib_fortnights"},
224 "month"=> $text{"lib_months"},
225 "year"=> $text{"lib_years"},
226 "decade"=> $text{"lib_decades"} );
228 if ($in{"$_[0]_def"}) { &save_directive($_[1], $_[0], [ ]); }
229 elsif ($in{$_[0]} !~ /^[0-9\.]+$/) {
230 &error(&text('lib_emsg2', $in{$_[0]}, $ts{$in{"$_[0]_u"}}) );
233 local $dir = { 'name' => $_[0],
234 'values' => [ $in{$_[0]}, $in{"$_[0]_u"} ] };
235 &save_directive($_[1], $_[0], [ $dir ]);
239 # opt_bytes_input(text, name, &config, default, size)
242 local($v, $rv, $u, %ss);
243 @ss = ( [ "KB", $text{'lib_kb'} ],
244 [ "MB", $text{'lib_mb'} ],
245 [ "GB", $text{'lib_gb'} ] );
246 $v = &find_config($_[1], $_[2]);
247 $rv = "<td valign=top><b>$_[0]</b></td> <td valign=top nowrap>\n";
248 $rv .= sprintf "<input type=radio name=$_[1]_def value=1 %s> $_[3]\n",
250 $rv .= sprintf "<input type=radio name=$_[1]_def value=0 %s> ",
252 $rv .= sprintf "<input name=$_[1] size=$_[4] value=\"%s\">\n",
253 $v ? $v->{'values'}->[0] : "";
254 $rv .= "<select name=$_[1]_u>\n";
256 $rv .= sprintf "<option value=$u->[0] %s>$u->[1]\n",
257 $v && $v->{'values'}->[1] eq $u->[0] ? "selected" : "";
259 $rv .= sprintf "<option value='' %s>bytes\n",
260 $v && $v->{'values'}->[1] eq "" ? "selected" : "";
261 $rv .= "</select></td>\n";
265 # save_opt_bytes(name, &config)
268 local %ss = ( "KB"=> $text{'lib_kb'},
269 "MB"=> $text{'lib_mb'},
270 "GB"=> $text{'lib_gb'} );
272 if ($in{"$_[0]_def"}) { &save_directive($_[1], $_[0], [ ]); }
273 elsif ($in{$_[0]} !~ /^[0-9\.]+$/) {
274 &error(&text('lib_emsg3', $in{$_[0]}, $ss{$in{"$_[0]_u"}}) );
277 local $dir = { 'name' => $_[0],
278 'values' => [ $in{$_[0]}, $in{"$_[0]_u"} ] };
279 &save_directive($_[1], $_[0], [ $dir ]);
283 %acl_types = ("src", $text{'lib_aclca'},
284 "dst", $text{'lib_aclwsa'},
285 "srcdomain", $text{'lib_aclch'},
286 "dstdomain", $text{'lib_aclwsh'},
287 "time", $text{'lib_acldat'},
288 "url_regex", $text{'lib_aclur'},
289 "urlpath_regex", $text{'lib_aclupr'},
290 "port", $text{'lib_aclup'},
291 "proto", $text{'lib_aclup1'},
292 "method", $text{'lib_aclrm'},
293 "browser", $text{'lib_aclbr'},
294 "user", $text{'lib_aclpl'},
295 "arp", $text{'lib_aclarp'} );
296 if ($squid_version >= 2.0) {
297 $acl_types{'src_as'} = $text{'lib_aclsan'};
298 $acl_types{'dst_as'} = $text{'lib_acldan'};
299 $acl_types{'proxy_auth'} = $text{'lib_aclea'};
300 $acl_types{'srcdom_regex'} = $text{'lib_aclcr'};
301 $acl_types{'dstdom_regex'} = $text{'lib_aclwsr'};
303 if ($squid_version >= 2.2) {
304 $acl_types{'ident'} = $text{'lib_aclru'};
305 $acl_types{'myip'} = $text{'lib_aclpia'};
306 delete($acl_types{'user'});
308 if ($squid_version >= 2.3) {
309 $acl_types{'maxconn'} = $text{'lib_aclmc'};
310 $acl_types{'myport'} = $text{'lib_aclpp'};
311 $acl_types{'snmp_community'} = $text{'lib_aclsc'};
313 if ($squid_version >= 2.4) {
314 $acl_types{'req_mime_type'} = $text{'lib_aclrmt'};
315 $acl_types{'proxy_auth_regex'} = $text{'lib_aclear'};
317 if ($squid_version >= 2.5) {
318 $acl_types{'rep_mime_type'} = $text{'lib_aclrpmt'};
319 $acl_types{'ident_regex'} = $text{'lib_aclrur'};
320 $acl_types{'external'} = $text{'lib_aclext'};
321 $acl_types{'max_user_ip'} = $text{'lib_aclmuip'};
325 # Returns HTML for a link to put in the top-right corner of every page
328 return undef if ($config{'restart_pos'} == 2);
329 local $pid = &is_squid_running();
330 local $args = "redir=".&urlize(&this_url())."&pid=$pid";
332 return ($access{'restart'} ? "<a href=\"restart.cgi?$args\">$text{'lib_buttac'}</a><br>\n" : "").
333 ($access{'start'} ? "<a href=\"stop.cgi?$args\">$text{'lib_buttss'}</a>\n" : "");
336 return $access{'start'} ? "<a href=\"start.cgi?$args\">$text{'lib_buttss1'}</a>\n" : "";
341 # Returns the process ID if squid is running
344 local $conf = &get_config();
345 local ($pidstruct, $pidfile);
346 $pidstruct = &find_config("pid_filename", $conf);
348 $pidstruct = &find_config("pid_filename", $conf, 2);
350 $pidfile = $pidstruct ? $pidstruct->{'values'}->[0] : $config{'pid_file'};
351 if ($pidfile eq "none" || !$pidfile) {
352 local ($pid) = &find_byname("squid");
356 return &check_pid_file($pidfile);
361 # Returns the URL in the apache directory of the current script
365 $url = $ENV{'SCRIPT_NAME'};
366 if (defined($ENV{'QUERY_STRING'})) { $url .= "?$ENV{'QUERY_STRING'}"; }
370 # list_auth_users(file)
373 local(@rv, $lnum); $lnum = 0;
376 if (/^(#*)([^:]+):(\S+)/) {
377 push(@rv, { 'user' => $2, 'pass' => $3,
378 'enabled' => !$1, 'line' => $lnum });
383 if ($config{'sort_conf'}) {
384 return sort { $a->{'user'} cmp $b->{'user'} } @rv;
391 # get_squid_user(&config)
392 # Returns the effective user and group (if any)
395 if ($squid_version < 2) {
396 local $ceu = &find_config("cache_effective_user", $_[0]);
397 if ($ceu) { return ($ceu->{'values'}->[0], $ceu->{'values'}->[1]); }
398 return (undef, undef);
401 local $ceu = &find_config("cache_effective_user", $_[0]);
402 local $ceg = &find_config("cache_effective_group", $_[0]);
403 return ($ceu->{'values'}->[0], $ceg ? $ceg->{'values'}->[0]
404 : $ceu->{'values'}->[1]);
408 # chown_files(user, group, config)
409 # Change ownership of all squid log and cache directories
412 local(@list, $pidstruct, $pidfile);
413 @list = ( $config{'log_dir'} );
416 if ($str = &find_config("pid_filename", $_[2])) {
417 $pidfile = $str->{'values'}->[0];
419 else { $pidfile = $config{'pid_file'}; }
420 push(@list, $pidfile);
422 # add other log directories
423 foreach $d ("cache_access_log", "access_log", "cache_log",
424 "cache_store_log", "cache_swap_log") {
425 if (($str = &find_config($d, $_[2])) &&
426 $str->{'values'}->[0] =~ /^(\S+)\/[^\/]+$/) {
431 # add cache directories
432 if (@str = &find_config("cache_dir", $_[2])) {
433 foreach $str (@str) {
434 push(@list, $str->{'values'}->[0]);
437 else { push(@list, $config{'cache_dir'}); }
438 system("chown -Rf $_[0]:$_[1] ".join(" ",@list)." >/dev/null 2>&1");
444 local @f = grep { $_ ne '' } split(/\//, $_[0]);
445 return 1 if ($access{'root'} eq '/');
446 local @a = grep { $_ ne '' } split(/\//, $access{'root'});
448 for($i=0; $i<@a; $i++) {
449 return 0 if ($a[$i] ne $f[$i]);
454 # get_auth_file(&config)
457 if ($squid_version >= 2.5) {
458 local @auth = &find_config("auth_param", $_[0]);
459 local ($program) = grep { $_->{'values'}->[0] eq 'basic' &&
460 $_->{'values'}->[1] eq 'program' } @auth;
461 return $program ? $program->{'values'}->[3] : undef;
464 local $authprog = &find_value("authenticate_program", $_[0]);
465 return $authprog =~ /(\S+)\s+(\/\S+)$/ ? $2 : undef;
469 # parse_external(&external_acl_type)
472 local @v = @{$_[0]->{'values'}};
473 local $rv = { 'name' => $v[0] };
474 for($i=1; $v[$i] =~ /^(\S+)=(\S+)$/; $i++) {
475 $rv->{'opts'}->{$1} = $2;
477 if ($v[$i] =~ /^\"(.*)\"$/) {
478 $rv->{'format'} = $1;
481 $rv->{'format'} = $v[$i];
484 $rv->{'program'} = $v[$i++];
485 $rv->{'args'} = [ @v[$i .. $#v] ];
489 # check_cache(&config, &caches)
490 # Returns 1 if the cache directory looks OK, 0 if not. Also fills in the
494 local (@cachestruct, @caches, $c, $coss);
495 if (@cachestruct = &find_config("cache_dir", $_[0])) {
496 if ($squid_version >= 2.3) {
497 @caches = map { $_->{'values'}->[1] } @cachestruct;
500 @caches = map { $_->{'values'}->[0] } @cachestruct;
502 ($coss) = grep { $_->{'values'}->[0] eq "coss" } @cachestruct;
505 @caches = ( $config{'cache_dir'} );
509 # Allow COSS files too
510 foreach $c (@caches) {
511 return 0 if (!-f $c && (!-d $c || !-d "$c/00"));
515 # Check for dirs only
516 foreach $c (@caches) {
517 return 0 if (!-d $c || !-d "$c/00");
524 # Returns the port Squid is listening on
527 local $conf = &get_config();
529 if ($squid_version >= 2.3) {
531 LOOP: foreach $p (&find_config("http_port", $conf)) {
532 foreach $v (@{$p->{'values'}}) {
533 if ($v =~ /^(\d+)$/) {
536 elsif ($v =~ /^(\S+):(\d+)$/) {
539 last LOOP if ($port);
544 $port = &find_value("http_port", $conf);
546 return defined($port) ? $port : 3128;
549 # apply_configuration()
550 # Activate the current Squid configuration
551 sub apply_configuration
553 if ($config{'squid_restart'}) {
554 local $out = &backquote_logged("$config{'squid_restart'} 2>&1");
555 return "<pre>$out</pre>" if ($?);
558 $out = &backquote_logged("$config{'squid_path'} -f $config{'squid_conf'} -k reconfigure 2>&1");
559 return "<pre>$out</pre>" if ($? && $out !~ /warning/i);
564 # list_cachemgr_actions()
565 # Returns a list of actions for use in the cachemgr_passwd directive
566 sub list_cachemgr_actions
568 return ("5min" ,"60min" ,"asndb" ,"authenticator" ,"cbdata" ,"client_list" ,"comm_incoming" ,"config" ,"counters" ,"delay" ,"digest_stats" ,"dns" ,"events" ,"filedescriptors" ,"fqdncache" ,"histograms" ,"http_headers" ,"info" ,"io" ,"ipcache" ,"mem" ,"menu" ,"netdb" ,"non_peers" ,"objects" ,"offline_toggle" ,"pconn" ,"peer_select" ,"redirector" ,"refresh" ,"server_list" ,"shutdown" ,"store_digest" ,"storedir" ,"utilization" ,"via_headers" ,"vm_objects");