From 0f14446a89c090cd985ee544b19fdfa0afe04dad Mon Sep 17 00:00:00 2001 From: Alexander Marx Date: Thu, 27 Aug 2015 14:20:30 +0200 Subject: [PATCH] Squid-accounting: New Version using dma mailservice Signed-off-by: Michael Tremer --- lfs/squid-accounting | 12 ++- src/squid-accounting/accounting.cgi | 109 ++++++++++------------------ src/squid-accounting/acct.de.pl | 2 +- src/squid-accounting/acct.en.pl | 2 +- src/squid-accounting/acct.pl | 88 +++++++++++++++------- 5 files changed, 106 insertions(+), 107 deletions(-) diff --git a/lfs/squid-accounting b/lfs/squid-accounting index a2ffd90445..aa2ca509e4 100644 --- a/lfs/squid-accounting +++ b/lfs/squid-accounting @@ -9,15 +9,15 @@ include Config -VER = 1.0.3 +VER = 1.0.4 THISAPP = squid-accounting-$(VER) DIR_APP = $(DIR_SRC)/$(THISAPP) TARGET = $(DIR_INFO)/$(THISAPP) PROG = squid-accounting -PAK_VER = 6 +PAK_VER = 7 -DEPS = "perl-DBI perl-DBD-SQLite perl-File-ReadBackwards perl-PDF-API2 sendEmail" +DEPS = "perl-DBI perl-DBD-SQLite perl-File-ReadBackwards perl-PDF-API2" ############################################################################### # Top-level Rules @@ -50,8 +50,7 @@ $(TARGET) : $(patsubst %,$(DIR_DL)/%,$(objects)) chmod 777 /var/log/accounting.log #Set permissions for logo and graphs chmod -R 777 /srv/web/ipfire/html/accounting - - + install -v -m 755 $(DIR_APP)/squid-accounting/accounting.cgi /srv/web/ipfire/cgi-bin/ install -v -m 755 $(DIR_APP)/squid-accounting/acct.pl /usr/local/bin/ install -v -m 644 $(DIR_APP)/squid-accounting/acct-lib.pl /var/ipfire/accounting/ @@ -61,10 +60,9 @@ $(TARGET) : $(patsubst %,$(DIR_DL)/%,$(objects)) install -v -m 755 $(DIR_APP)/squid-accounting/dbinstall.pl /var/ipfire/accounting/ install -v -m 644 $(DIR_APP)/squid-accounting/config/backup/includes/squid-accounting \ /var/ipfire/backup/addons/includes/squid-accounting - + #activate hourly logging of proxy logfile ln -sf /usr/local/bin/acct.pl /etc/fcron.hourly/squid-accounting - chown -R nobody.nobody /var/ipfire/accounting @rm -rf $(DIR_APP) @$(POSTBUILD) diff --git a/src/squid-accounting/accounting.cgi b/src/squid-accounting/accounting.cgi index 1ec9849eb7..1064763c28 100755 --- a/src/squid-accounting/accounting.cgi +++ b/src/squid-accounting/accounting.cgi @@ -56,12 +56,17 @@ my $count=0; my $col; my $proxlog=$Lang::tr{'stopped'}; my $proxsrv=$Lang::tr{'stopped'}; +my $mailfile="${General::swroot}/dma/mail.conf"; &Header::getcgihash(\%cgiparams); &General::readhash("${General::swroot}/main/settings", \%mainsettings); &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color); &General::readhash("$settingsfile", \%settings) if(-f $settingsfile); +if ( -f $mailfile){ + &General::readhash($mailfile, \%mail); +} + #Find out which lang is set (used later to set decimal separator correctly) my $uplang=uc($mainsettings{'LANGUAGE'}); setlocale LC_NUMERIC,"$mainsettings{'LANGUAGE'}_$uplang"; @@ -136,11 +141,6 @@ if ($cgiparams{'ACTION'} eq "$Lang::tr{'save'}"){ #SaveButton on configsite if ($cgiparams{'USEMAIL'} eq 'on'){ $errormessage=&checkmailsettings; }elsif($cgiparams{'USEMAIL'} ne 'on'){ - $cgiparams{'txt_mailserver'}=''; - $cgiparams{'txt_mailport'}=''; - $cgiparams{'txt_mailuser'}=''; - $cgiparams{'txt_mailpass'}=''; - $cgiparams{'mail_tls'}=''; $cgiparams{'txt_mailsender'}=''; $cgiparams{'txt_mailsubject'}=''; $mailtxt=''; @@ -155,11 +155,6 @@ if ($cgiparams{'ACTION'} eq "$Lang::tr{'save'}"){ #SaveButton on configsite $settings{'CURRENCY'} = $cgiparams{'txt_currency'}; $settings{'SKIPURLS'} = $skipurls; $settings{'USEMAIL'} = $cgiparams{'USEMAIL'}; - $settings{'MAILSRV'} = $cgiparams{'txt_mailserver'}; - $settings{'MAILPORT'} = $cgiparams{'txt_mailport'}; - $settings{'MAILUSER'} = $cgiparams{'txt_mailuser'}; - $settings{'MAILPASS'} = $cgiparams{'txt_mailpass'}; - $settings{'TLS'} = $cgiparams{'mail_tls'}; $settings{'MAILSENDER'} = $cgiparams{'txt_mailsender'}; $settings{'MAILSUB'} = $cgiparams{'txt_mailsubject'}; $settings{'MAILTXT'} = $mailtxt; @@ -568,10 +563,6 @@ sub configsite{ #If update set fieldvalues new if($cgiparams{'update'} eq 'on'){ $settings{'USEMAIL'} = 'on'; - $settings{'MAILSRV'} = $cgiparams{'txt_mailserver'}; - $settings{'MAILPORT'} = $cgiparams{'txt_mailport'}; - $settings{'MAILUSER'} = $cgiparams{'txt_mailuser'}; - $settings{'MAILPASS'} = $cgiparams{'txt_mailpass'}; $settings{'MAILSUB'} = $cgiparams{'txt_mailsubject'}; $settings{'MAILTXT'} = $cgiparams{'txt_mailtxt'}; } @@ -580,14 +571,13 @@ sub configsite{ $checked{'logging'}{$settings{'LOG'}} = 'CHECKED'; $checked{'multiuser'}{$settings{'MULTIUSER'}} = 'CHECKED'; $checked{'usemail'}{$settings{'USEMAIL'}} = 'CHECKED'; - $checked{'mail_tls'}{$settings{'TLS'}} = 'CHECKED'; - + #Open site &Header::openpage($Lang::tr{'acct settings'}, 1, ''); &Header::openbigbox('100%', 'center'); &error; &Header::openbox('100%', 'left', $Lang::tr{'acct config'}); - + #### JAVA SCRIPT #### print< @@ -631,57 +621,45 @@ END $Lang::tr{'acct multiuser'} - + $Lang::tr{'acct mwst'} + $Lang::tr{'acct currency'} - + $Lang::tr{'acct skipurl'} +END + +if ($mail{'USEMAIL'} eq 'on'){ + if (!$settings{'MAILSENDER'}){ + $settings{'MAILSENDER'} = $mail{'SENDER'}; + } +print < $Lang::tr{'acct usemail'} -
+END +} +print <
- - - - - - - - - - - - - - - - - - - - - + - - - @@ -693,7 +671,6 @@ END
$Lang::tr{'acct mailaddr'}
$Lang::tr{'acct mailport'}
$Lang::tr{'acct mailuser'}*
$Lang::tr{'acct mailpass'}*
$Lang::tr{'acct tls'}
$Lang::tr{'acct mailsender'}$Lang::tr{'acct mailsender'}
 
$Lang::tr{'acct subject'}
- @@ -1119,28 +1096,28 @@ print< $Lang::tr{'acct customer'}   $Lang::tr{'acct hoster'} - + - + - + - + - + - + @@ -1148,7 +1125,7 @@ print< - + + - + - + - + @@ -1180,25 +1157,25 @@ print< - + - + - + - + END @@ -2175,18 +2152,6 @@ END } sub checkmailsettings{ - #Check if mailserver is an ip address or a domain - if ($cgiparams{'txt_mailserver'} =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/){ - if (! &General::validip($cgiparams{'txt_mailserver'})){ - $errormessage.="$Lang::tr{'acct invalid mailip'} $cgiparams{'txt_mailserver'}
"; - } - }elsif(! &General::validfqdn($cgiparams{'txt_mailserver'})){ - $errormessage.="$Lang::tr{'acct invalid mailfqdn'} $cgiparams{'txt_mailserver'}
"; - } - #Check valid mailserverport - if($cgiparams{'txt_mailport'} < 1 || $cgiparams{'txt_mailport'} > 65535){ - $errormessage.="$Lang::tr{'acct invalid mailport'} $cgiparams{'txt_mailport'}
"; - } #Check valid sender if(! $cgiparams{'txt_mailsender'}){ $errormessage.="$Lang::tr{'acct empty field'} $Lang::tr{'acct mailsender'}
"; diff --git a/src/squid-accounting/acct.de.pl b/src/squid-accounting/acct.de.pl index 2110cf7014..0291a9d250 100644 --- a/src/squid-accounting/acct.de.pl +++ b/src/squid-accounting/acct.de.pl @@ -88,9 +88,9 @@ 'acct mwst' => 'Mehrwertsteuer in %', 'acct name1' => 'Name 1', 'acct name2' => 'Name 2', +'acct not optional' => 'Diese Felder sind Pflichtfelder', 'acct nr' => 'RG-Nr.', 'acct oldestdb' => 'Erster DB-Eintrag', -'acct optional' => 'Diese Felder sind optional', 'acct parameter' => 'Parameter', 'acct path' => 'Pfad', 'acct pdf billtxt' => 'Rechnung', diff --git a/src/squid-accounting/acct.en.pl b/src/squid-accounting/acct.en.pl index 3e3ed19017..913f2a79fa 100644 --- a/src/squid-accounting/acct.en.pl +++ b/src/squid-accounting/acct.en.pl @@ -90,7 +90,7 @@ 'acct name2' => 'Name 2', 'acct nr' => 'RG-No.', 'acct oldestdb' => 'Fist DB Entry', -'acct optional' => 'These fields are optional', +'acct not optional' => 'These fields are required', 'acct parameter' => 'Parameter', 'acct path' => 'Path', 'acct pdf billtxt' => 'Bill', diff --git a/src/squid-accounting/acct.pl b/src/squid-accounting/acct.pl index 7222689458..68eb63c75e 100755 --- a/src/squid-accounting/acct.pl +++ b/src/squid-accounting/acct.pl @@ -27,6 +27,8 @@ use Time::Local; use File::ReadBackwards; use strict; +use MIME::Lite; + #use warnings; require '/var/ipfire/general-functions.pl'; @@ -62,6 +64,11 @@ my ($mini,$max)=&ACCT::getminmax; my $now = localtime; my $proxylog; my $proxysrv; +my $dmafile="${General::swroot}/dma/dma.conf"; +my $authfile="${General::swroot}/dma/auth.conf"; +my $mailfile="${General::swroot}/dma/mail.conf"; +my %mail=(); +my %dma=(); ######## # Main # @@ -69,6 +76,7 @@ my $proxysrv; &checkproxy; + #If we have a disabled file and the proxy is off, we don't need to check anything, exit! if((! -f $proxyenabled || $proxylog eq $Lang::tr{'stopped'}) && -f "${General::swroot}/accounting/disabled"){ &ACCT::logger($settings{'LOG'}," Proxy or proxylogging disabled - exiting with no data collection\n"); @@ -92,7 +100,7 @@ if (-f $proxyenabled && $proxylog eq $Lang::tr{'running'}){ $dbh=&ACCT::connectdb; my $m=sprintf("%d",(localtime((time-3600)))[4]+1); &ACCT::logger($settings{'LOG'},"month before one hour $m, now is ".($mon+1)."\n"); - if ($m < ($mon+1) || $m == '12' && ($mon+1) == '1'){ + if ($m = ($mon+1) || $m == '12' && ($mon+1) == '1'){ #Logrotate my $year1=$year+1900; system ("tar", "cfz", "/var/log/accounting-$m-$year1.tar.gz", "/var/log/accounting.log"); @@ -103,11 +111,22 @@ if (-f $proxyenabled && $proxylog eq $Lang::tr{'running'}){ #move all db entries older than this month to second table and cumulate them daily &ACCT::movedbdata; &ACCT::logger($settings{'LOG'},"New Month. Old trafficvalues moved to ACCT_HIST Table\n"); - if ($settings{'USEMAIL'} eq 'on'){ + #check if mail is enabled + if ( -f $mailfile){ + &General::readhash($mailfile, \%mail); + } + if ($mail{'USEMAIL'} eq 'on'){ &ACCT::logger($settings{'LOG'},"Mailserver is activated - Now sending bills via mail...\n"); my $res=&ACCT::getbillgroups; foreach my $line (@$res){ my ($grp) = @$line; + open (FILE, "<", $dmafile) or die $!; + foreach my $line () { + $line =~ m/^([A-Z]+)\s+?(.*)?$/; + my $key = $1; + my $val = $2; + $dma{$key}=$val; + } &sendbill($grp,$settings{'MWST'},$settings{'CURRENCY'}); } }else{ @@ -214,7 +233,7 @@ sub sendbill { $month = '0'.$actmonth if $actmonth < 10; $month = '12' if $actmonth == 0; my $actyear = $now[5]; - my ($from,$till)=&ACCT::getmonth($actmonth,$actyear); #FIXME month and year as variables! + my ($from,$till)=&ACCT::getmonth($actmonth,$actyear); my @billar = &ACCT::GetTaValues($from,$till,$rggrp); my $address_cust = &ACCT::getTaAddress($rggrp,'CUST'); my $address_host = &ACCT::getTaAddress($rggrp,'HOST'); @@ -229,23 +248,34 @@ sub sendbill { if ($back eq '0'){ &ACCT::logger($settings{'LOG'},"Bill for $company_cust successfully created.\n"); - my $file="'/var/ipfire/accounting/bill/$rggrp/$month-$actyear-$no.pdf'"; + my $file="/var/ipfire/accounting/bill/$rggrp/$month-$actyear-$no.pdf"; $settings{'MAILTXT'} =~ tr/\|/\r\n/ ; - my $cmd = "/usr/local/bin/sendEmail "; - $cmd .= " -f $settings{'MAILSENDER'}"; #Sender - $cmd .= " -t $email"; #Recipient - if ($ccmail){ - $cmd .= " -cc $ccmail"; - } - #Send Mail via TLS? - if ($settings{'TLS'} eq 'on'){ - $cmd .= " -o tls=yes"; #TLS - } - $cmd .= " -u '$settings{'MAILSUB'}'"; #Subject - $cmd .= " -m '$settings{'MAILTXT'}'"; #Mailtext - $cmd .= " -s $settings{'MAILSRV'}:$settings{'MAILPORT'}"; #Mailserver:port - $cmd .= " -a $file"; - my $res=system ($cmd); + + #extract filename from path + my ($filename) = $file =~ m{([^/]+)$}; + + my $msg = MIME::Lite->new( + From => $mail{'SENDER'}, + To => $email, + Cc => $ccmail, + Subject => $settings{'MAILSUB'}, + Type => 'multipart/mixed' + ); + + $msg->attach( + Type => 'TEXT', + Data => $settings{'MAILTXT'} + ); + + $msg->attach( + Type => 'application/pdf', + Path => $file, + Filename => $filename, + Disposition => 'attachment' + ); + + my $res=$msg->send_by_sendmail; + if ($res == 0){ &ACCT::logger($settings{'LOG'},"Bill for $company_cust successfully sent.\n"); }elsif ($res > 0){ @@ -255,13 +285,19 @@ sub sendbill { }else{ &ACCT::logger($settings{'LOG'},"ERROR Bill for $company_cust could not be created.\n"); - my $cmd = "/usr/local/bin/sendEmail "; - $cmd .= " -f $settings{'MAILSENDER'}"; - $cmd .= " -t $settings{'MAILSENDER'}"; - $cmd .= " -u Fehler Squid Accounting"; - $cmd .= " -m 'Die Rechnung konnte nicht erzeugt und per Mail versendet werden' $company_cust"; - $cmd .= " -s $settings{'MAILSRV'}:$settings{'MAILPORT'}"; - my $res=system ($cmd); + my $msg = MIME::Lite->new( + From => $mail{'SENDER'}, + To => $mail{'RECIPIENT'}, + Subject => "ERROR Squid Accounting", + Type => 'multipart/mixed' + ); + + $msg->attach( + Type => 'TEXT', + Data => "The bill could not be created for customer $company_cust" + ); + + $msg->send_by_sendmail; return 0; } } -- 2.39.2
$Lang::tr{'acct bank'}$Lang::tr{'acct bank'}*
$Lang::tr{'acct company'}$Lang::tr{'acct company'}* $Lang::tr{'acct iban'}*$Lang::tr{'acct iban'}
$Lang::tr{'acct name1'}*$Lang::tr{'acct name1'} $Lang::tr{'acct bic'}*$Lang::tr{'acct bic'}
$Lang::tr{'acct str'}$Lang::tr{'acct str'}* $Lang::tr{'acct blz'}
$Lang::tr{'acct str_nr'}$Lang::tr{'acct str_nr'}* $Lang::tr{'acct kto'} @@ -1157,16 +1134,16 @@ print< - $Lang::tr{'acct plz'}$Lang::tr{'acct plz'}* $Lang::tr{'acct email'}$Lang::tr{'acct email'}*
$Lang::tr{'acct city'}$Lang::tr{'acct city'}* $Lang::tr{'acct inet'}*$Lang::tr{'acct inet'}
$Lang::tr{'acct ustid'}$Lang::tr{'acct ustid'}*
$Lang::tr{'acct tel'}*$Lang::tr{'acct tel'}
$Lang::tr{'acct fax'}*$Lang::tr{'acct fax'}
*$Lang::tr{'acct optional'}*$Lang::tr{'acct not optional'}