More strict conversion

This commit is contained in:
Jamie Cameron
2014-01-11 18:07:37 -08:00
parent 763ad98b09
commit 65376caaaf
7 changed files with 104 additions and 128 deletions

View File

@@ -2,6 +2,9 @@
# calamaris.cgi
# Run calamaris on the squid logfile(s)
use strict;
use warnings;
our (%text, %in, %access, $squid_version, %config, %gconfig, $module_name);
require './squid-lib.pl';
$access{'calamaris'} || &error($text{'calamaris_ecannot'});
&ui_print_header(undef, $text{'calamaris_title'}, "");
@@ -15,10 +18,12 @@ if (!&has_command($config{'calamaris'})) {
}
# Work out Calamaris version and args
if (`$config{'calamaris'} -V 2>&1` =~ /(revision:|Calamaris)\s+(\d\S+)/i) {
my $ver;
if (&backquote_command("$config{'calamaris'} -V 2>&1") =~
/(revision:|Calamaris)\s+(\d\S+)/i) {
$ver = $2;
}
$args = $config{'cal_all'} ? " -a" : "";
my $args = $config{'cal_all'} ? " -a" : "";
if ($ver >= 2.5) {
if ($config{'cal_fmt'} eq 'w') {
$args .= " -F html";
@@ -33,10 +38,12 @@ else {
$args .= " $config{'cal_extra'}";
# are there any logfiles to analyse?
$ld = $config{'log_dir'};
opendir(DIR, $ld);
while($f = readdir(DIR)) {
local @st = stat("$ld/$f");
my $ld = $config{'log_dir'};
my $fh;
my @files;
opendir($fh, $ld);
while(my $f = readdir($fh)) {
my @st = stat("$ld/$f");
if ($f =~ /^access.log.*gz$/) {
push(@files, [ "gunzip -c $ld/$f |", $st[9] ]);
}
@@ -47,7 +54,7 @@ while($f = readdir(DIR)) {
push(@files, [ "$ld/$f", $st[9] ]);
}
}
closedir(DIR);
closedir($fh);
if (!@files) {
print &text('calamaris_elogs', "<tt>$ld</tt>",
"$gconfig{'webprefix'}/config.cgi?$module_name"),"<p>\n";
@@ -56,62 +63,69 @@ if (!@files) {
}
# run calamaris, parsing newest files and records first
$temp = &transname();
open(CAL, "| $config{'calamaris'} $args >$temp 2>/dev/null");
my $temp = &transname();
my $fh2;
open($fh2, "| $config{'calamaris'} $args >$temp 2>/dev/null");
if ($config{'cal_max'}) {
# read only the last N lines
print &text('calamaris_last', $config{'cal_max'}),"<p>\n";
@files = sort { $b->[1] <=> $a->[1] } @files;
$lnum = 0;
foreach $f (@files) {
$left = $config{'cal_max'} - $lnum;
my $lnum = 0;
foreach my $f (@files) {
my $left = $config{'cal_max'} - $lnum;
last if ($left <= 0);
my $fh3;
if ($f->[0] =~ /\|$/) {
open(LOG, "$f->[0] tail -$left |");
open($fh3, "$f->[0] tail -$left |");
}
else {
open(LOG, "tail -$left $f->[0] |");
open($fh3, "tail -$left $f->[0] |");
}
while(<LOG>) {
while(<$fh3>) {
print CAL $_;
$lnum++;
}
close(LOG);
close($fh3);
}
}
else {
# read all the log files
foreach $f (@files) {
open(LOG, $f->[0]);
while(read(LOG, $buf, 1024) > 0) {
print CAL $buf;
my $fh3;
foreach my $f (@files) {
open($fh3, $f->[0]);
my $buf;
while(read($fh3, $buf, 1024) > 0) {
print $fh2 $buf;
}
close(LOG);
close($fh3);
}
}
close(CAL);
# Put the calamaris output into a nice webmin like table.
$date = &make_date(time());
my $date = &make_date(time());
print &ui_table_start(&text('calamaris_gen', $date), undef, 2);
# Get the output
open(OUT, $temp);
my $fh4;
open($fh4, $temp);
my $html;
if ($config{'cal_fmt'} eq 'm') {
$html = "<pre>";
while(<OUT>) {
while(<$fh4>) {
$html .= &html_escape($_);
}
$html = "</pre>";
}
else {
while(<OUT>) {
my $inbody = 0;
while(<$fh4>) {
if (/<\s*\/head/i || /<\s*body/i) { $inbody = 1; }
elsif (/<\s*\/body/i) { $inbody = 0; }
elsif ($inbody) { $html .= $_; }
}
}
close(OUT);
close($fh4);
unlink($temp);
# Show it

View File

@@ -1,21 +1,24 @@
#!/usr/local/bin/perl
# Delete multiple proxy restrictions
use strict;
use warnings;
our (%text, %in, %access, $squid_version, %config);
require './squid-lib.pl';
&error_setup($text{'dhttp_err'});
$access{'actrl'} || &error($text{'eacl_ecannot'});
&ReadParse();
@d = split(/\0/, $in{'d'});
my @d = split(/\0/, $in{'d'});
@d || &error($text{'dhttp_enone'});
# Get the existing restrictions
&lock_file($config{'squid_conf'});
$conf = &get_config();
@https = &find_config("http_access", $conf);
my $conf = &get_config();
my @https = &find_config("http_access", $conf);
# Delete them
foreach $d (sort { $b <=> $c } @d) {
$http = $conf->[$d];
foreach my $d (sort { $b <=> $a } @d) {
my $http = $conf->[$d];
splice(@https, &indexof($http, @https), 1);
}

View File

@@ -1,21 +1,24 @@
#!/usr/local/bin/perl
# Delete multiple proxy REPLY restrictions
use strict;
use warnings;
our (%text, %in, %access, $squid_version, %config);
require './squid-lib.pl';
&error_setup($text{'dhttp_err'});
$access{'actrl'} || &error($text{'eacl_ecannot'});
&ReadParse();
@d = split(/\0/, $in{'d'});
my @d = split(/\0/, $in{'d'});
@d || &error($text{'dhttp_enone'});
# Get the existing restrictions
&lock_file($config{'squid_conf'});
$conf = &get_config();
@http_replies = &find_config("http_reply_access", $conf);
my $conf = &get_config();
my @http_replies = &find_config("http_reply_access", $conf);
# Delete them
foreach $d (sort { $b <=> $c } @d) {
$http_reply = $conf->[$d];
foreach my $d (sort { $b <=> $a } @d) {
my $http_reply = $conf->[$d];
splice(@http_replies, &indexof($http_reply, @http_replies), 1);
}

View File

@@ -1,25 +1,28 @@
#!/usr/local/bin/perl
# Delete a bunch of delay pools
use strict;
use warnings;
our (%text, %in, %access, $squid_version, %config);
require './squid-lib.pl';
&error_setup($text{'dpool_err'});
$access{'delay'} || &error($text{'delay_ecannot'});
&ReadParse();
@d = split(/\0/, $in{'d'});
my @d = split(/\0/, $in{'d'});
@d || &error($text{'dpool_enone'});
# Get the current settings
&lock_file($config{'squid_conf'});
$conf = &get_config();
@pools = &find_config("delay_class", $conf);
@params = &find_config("delay_parameters", $conf);
@access = &find_config("delay_access", $conf);
$pools = &find_config("delay_pools", $conf);
my $conf = &get_config();
my @pools = &find_config("delay_class", $conf);
my @params = &find_config("delay_parameters", $conf);
my @access = &find_config("delay_access", $conf);
my $pools = &find_config("delay_pools", $conf);
# Do the deletion, highest first
foreach $d (sort { $b <=> $a } @d) {
($pool) = grep { $_->{'values'}->[0] == $d } @pools;
($param) = grep { $_->{'values'}->[0] == $d } @params;
foreach my $d (sort { $b <=> $a } @d) {
my ($pool) = grep { $_->{'values'}->[0] == $d } @pools;
my ($param) = grep { $_->{'values'}->[0] == $d } @params;
@access = grep { $_->{'values'}->[0] != $d } @access;
@pools = grep { $_ ne $pool } @pools;
@params = grep { $_ ne $param } @params;

View File

@@ -2,27 +2,30 @@
# init_cache.cgi
# Initialize the cache by running squid with the -z option
use strict;
use warnings;
our (%text, %in, %access, $squid_version, %config);
require './squid-lib.pl';
$access{'rebuild'} || &error($text{'icache_ecannot'});
&ReadParse();
$whatfailed = $text{'icache_ftic'};
&error_setup($text{'icache_ftic'});
# set user to run squid as..
&lock_file($config{'squid_conf'});
$conf = &get_config();
my $conf = &get_config();
if (!$in{'nouser'}) {
$in{'user'} || &error($text{'icache_ymcautrsa'});
@uinfo = getpwnam($in{'user'});
my @uinfo = getpwnam($in{'user'});
scalar(@uinfo) || &error($text{'icache_euser'});
@ginfo = getgrgid($uinfo[3]);
my @ginfo = getgrgid($uinfo[3]);
if ($squid_version < 2) {
$dir = { 'name' => 'cache_effective_user',
'values' => [ $in{'user'}, $ginfo[0] ] };
my $dir = { 'name' => 'cache_effective_user',
'values' => [ $in{'user'}, $ginfo[0] ] };
&save_directive($conf, "cache_effective_user", [ $dir ]);
}
else {
$dir = { 'name' => 'cache_effective_user',
'values' => [ $in{'user'} ] };
my $dir = { 'name' => 'cache_effective_user',
'values' => [ $in{'user'} ] };
&save_directive($conf, "cache_effective_user", [ $dir ]);
$dir = { 'name' => 'cache_effective_group',
'values' => [ $ginfo[0] ] };
@@ -34,19 +37,13 @@ if (!$in{'nouser'}) {
# Stop squid (if running)
&ui_print_unbuffered_header(undef, $text{'icache_title'}, "");
if ($pidstruct = &find_config("pid_filename", $conf)) {
$pidfile = $pidstruct->{'values'}->[0];
}
else { $pidfile = $config{'pid_file'}; }
if (open(PID, $pidfile)) {
<PID> =~ /(\d+)/; $pid = $1;
close(PID);
}
my $pid = &is_squid_running();
my $stopped = 0;
if ($pid && kill(0, $pid)) {
print "<p>$text{'clear_stop'}<br>\n";
&system_logged("$config{'squid_path'} -f $config{'squid_conf'} ".
"-k shutdown >/dev/null 2>&1");
for($i=0; $i<40; $i++) {
for(my $i=0; $i<40; $i++) {
if (!kill(0, $pid)) { last; }
sleep(1);
}
@@ -55,35 +52,36 @@ if ($pid && kill(0, $pid)) {
}
# Initialize the cache
($user, $group) = &get_squid_user($conf);
my ($user, $group) = &get_squid_user($conf);
if ($user) {
foreach $c (split(/\s+/, $in{'caches'})) {
mkdir($c, 0755);
foreach my $c (split(/\s+/, $in{'caches'})) {
&make_dir($c, 0755);
}
}
&chown_files($user, $group, $conf);
$cmd = "$config{'squid_path'} -f $config{'squid_conf'} -z";
my $cmd = "$config{'squid_path'} -f $config{'squid_conf'} -z";
print "<p>", &text('icache_itscwtc',$cmd), "<br>\n";
print "<pre>\n";
&additional_log('exec', undef, $cmd);
open(INIT, "$cmd 2>&1 |");
while(<INIT>) {
print;
my $fh;
open($fh, "$cmd 2>&1 |");
while(<$fh>) {
print &html_escape($_);
}
close(INIT);
close($fh);
print "</pre>\n";
# Try to re-start squid (if it was running before)
if ($stopped) {
$temp = &transname();
my $temp = &transname();
&system_logged("$config{'squid_path'} -sY -f $config{'squid_conf'} >$temp 2>&1 </dev/null &");
sleep(3);
$errs = `cat $temp`;
my $errs = &read_file_contents($temp);
unlink($temp);
if ($errs) {
&system_logged("$config{'squid_path'} -k shutdown -f $config{'squid_conf'} >/dev/null 2>&1");
print "$text{'clear_failrestart'}<br>\n";
print "<pre>$errs</pre>\n";
print "<pre>".&html_escape($errs)."</pre>\n";
}
}

View File

@@ -1,6 +1,9 @@
#!/usr/local/bin/perl
# Call squidclient to remove just one URL
use strict;
use warnings;
our (%text, %in, %access, $squid_version, %config);
require './squid-lib.pl';
&error_setup($text{'purge_err'});
$access{'rebuild'} || &error($text{'clear_ecannot'});
@@ -10,10 +13,11 @@ $in{'url'} || &error($text{'purge_eurl'});
&ui_print_header(undef, $text{'purge_title'}, "");
# Get the port number
$conf = &get_config();
my $conf = &get_config();
my $port;
if ($squid_version >= 2.3) {
@ports = &find_config("http_port", $conf);
foreach $p (@ports) {
my @ports = &find_config("http_port", $conf);
foreach my $p (@ports) {
if ($p->{'values'}->[0] =~ /(\d+)$/) {
$port = $1;
last;
@@ -27,9 +31,9 @@ else {
# Run it
print &text('purge_doing', "<tt>$in{'url'}</tt>"),"\n";
$cmd = "$config{'squidclient'} -p $port -m PURGE ".quotemeta($in{'url'});
$out = &backquote_logged("$cmd 2>&1");
print "<pre>$out</pre>\n";
my $cmd = "$config{'squidclient'} -p $port -m PURGE ".quotemeta($in{'url'});
my $out = &backquote_logged("$cmd 2>&1");
print "<pre>".&html_escape($out)."</pre>\n";
if ($?) {
print $text{'purge_failed'},"<br>\n";
}

View File

@@ -1,49 +0,0 @@
#!/usr/local/bin/perl
# save_auth.cgi
# Save authentication options
require './squid-lib.pl';
$access{'proxyauth'} || &error($text{'eauth_ecannot'});
&ReadParse();
&lock_file($config{'squid_conf'});
$conf = &get_config();
$whatfailed = $text{'sauth_ftsao'};
if ($in{'authfile_def'}) {
&save_directive($conf, "proxy_auth", [ ]);
}
else {
$in{'authfile'} =~ /^\// || &error($text{'sauth_iomuf'});
if (!-r $in{'authfile'}) {
&open_tempfile(AUTH, ">$in{'authfile'}");
&close_tempfile(AUTH);
($user, $group) = &get_squid_user($conf);
if ($user) {
@uinfo = getpwnam($user);
@ginfo = getgrnam($group);
chown($uinfo[2], $ginfo[2], $in{'authfile'});
chmod(0644, $in{'authfile'});
}
}
push(@vals, $in{'authfile'});
if (!$in{'authdom_def'}) {
$in{'authdom'}=~/^\S+$/ || &error($text{'sauth_iomd'});
push(@vals, $in{'authdom'});
}
&save_directive($conf, "proxy_auth",
[ { 'name' => 'proxy_auth',
'values' => \@vals } ]);
}
&flush_file_lines();
# check if the proxy_auth directive is supported
$out = `$config{'squid_path'} -f $config{'squid_conf'} -k check 2>&1`;
if ($out =~ /proxy_auth/) {
# it isn't .. roll back
&save_directive($conf, "proxy_auth", [ ]);
&flush_file_lines();
&error($text{'sauth_msg1'});
}
&unlock_file($config{'squid_conf'});
&redirect("");