/*
* SARG Squid Analysis Report Generator http://sarg.sourceforge.net
- * 1998, 2010
+ * 1998, 2015
*
* SARG donations:
* please look at http://sarg.sourceforge.net/donations.php
#include "include/conf.h"
#include "include/defs.h"
-int geramail(const char *dirname, int debug, const char *outdir, int userip, const char *email, const char *TempDir)
-{
-
- FILE *fp_in, *fp_top1, *fp_top2, *fp_top3;
- long long int ttnbytes=0, ttnacc=0, tnacc=0;
- long long int tnbytes=0, ttnelap=0, tnelap=0;
- long long int nacc, nbytes, elap;
- double perc=0.00;
- double perc2=0.00;
- int posicao=0;
- char olduser[MAX_USER_LEN], csort[MAXLEN], period[MAXLEN], arqper[MAXLEN];
- char wger[MAXLEN], top1[MAXLEN], top2[MAXLEN], top3[MAXLEN], user[MAX_USER_LEN], tusr[MAXLEN];
- char strip1[MAXLEN], strip2[MAXLEN], strip3[MAXLEN], strip4[MAXLEN], strip5[MAXLEN], strip6[MAXLEN], strip7[MAXLEN];
- int totuser=0;
- time_t t;
- struct tm *local;
- int cstatus;
- char warea[MAXLEN];
- struct getwordstruct gwarea;
- struct generalitemstruct item;
-
- strcpy(wger,dirname);
- strcpy(top1,dirname);
- strcpy(top2,dirname);
- strcpy(top3,dirname);
- strcpy(tusr,dirname);
- strcat(wger,"/sarg-general");
- strcat(top1,"/top");
- strcat(top2,"/top.tmp");
- strcat(top3,"/report");
- strcat(tusr,"/sarg-users");
-
- if((fp_in=fopen(wger,"r"))==NULL) {
- fprintf(stderr, "SARG: (email) %s: %s\n",_("Cannot open file"),wger);
- exit(1);
- }
-
- if((fp_top1=fopen(top1,"w"))==NULL) {
- fprintf(stderr, "SARG: (email) %s: %s\n",_("Cannot open file"),top1);
- exit(1);
- }
-
- if((fp_top2=fopen(top2,"w"))==NULL) {
- fprintf(stderr, "SARG: (email) %s: %s\n",_("Cannot open file"),top2);
- exit(1);
- }
-
- olduser[0]='\0';
- totuser=0;
-
- while(fgets(warea,sizeof(warea),fp_in)) {
- ger_read(warea,&item,wger);
- if(item.total) continue;
- if(strcmp(olduser,item.user) != 0)
- {
- totuser++;
-
- if (olduser[0] != '\0') {
-#if defined(__FreeBSD__)
- fprintf(fp_top2,"%s\t%qu\t%qu\t%qu\n",olduser,tnbytes,tnacc,tnelap);
-#elif defined(__alpha) || __ALPHA
- fprintf(fp_top2,"%s\t%ld\t%ld\t%ld\n",olduser,tnbytes,tnacc,tnelap);
-#else
- fprintf(fp_top2,"%s\t%lld\t%lld\t%lld\n",olduser,tnbytes,tnacc,tnelap);
-#endif
- ttnbytes+=tnbytes;
- ttnacc+=tnacc;
- ttnelap+=tnelap;
- }
- strcpy(olduser,item.user);
- tnbytes=0;
- tnacc=0;
- tnelap=0;
- }
-
- tnbytes+=item.nbytes;
- tnacc+=item.nacc;
- tnelap+=item.nelap;
- }
-
- if (olduser[0] != '\0') {
-#if defined(__FreeBSD__)
- fprintf(fp_top2,"%s\t%qu\t%qu\t%qu\n",olduser,tnbytes,tnacc,tnelap);
-#elif defined(__alpha) || __ALPHA
- fprintf(fp_top2,"%s\t%ld\t%ld\t%ld\n",olduser,tnbytes,tnacc,tnelap);
-#else
- fprintf(fp_top2,"%s\t%lld\t%lld\t%lld\n",olduser,tnbytes,tnacc,tnelap);
+#ifdef ENABLE_DOUBLE_CHECK_DATA
+extern struct globalstatstruct globstat;
#endif
- ttnbytes+=tnbytes;
- ttnacc+=tnacc;
- ttnelap+=tnelap;
- }
-
- fclose(fp_in);
- fclose(fp_top2);
-
- sprintf(csort,"sort -n -T \"%s\" -r -k 2,2 -o \"%s\" \"%s\"", TempDir, top1, top2);
- cstatus=system(csort);
- if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
- fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
- fprintf(stderr, "SARG: sort command: %s\n",csort);
- exit(1);
- }
-
- unlink(top2);
-
- if((fp_top1=fopen(top1,"a"))==NULL) {
- fprintf(stderr, "SARG: (email) %s: %s\n",_("Cannot open file"),top1);
- exit(1);
- }
-#if defined(__FreeBSD__)
- fprintf(fp_top1,"TOTAL\t%qu\t%qu\t%qu\n",ttnbytes,ttnacc,ttnelap);
-#elif defined(__alpha) || __ALPHA
- fprintf(fp_top1,"TOTAL\t%ld\t%ld\t%ld\n",ttnbytes,ttnacc,ttnelap);
-#else
- fprintf(fp_top1,"TOTAL\t%lld\t%lld\t%lld\n",ttnbytes,ttnacc,ttnelap);
-#endif
- fclose(fp_top1);
-
- /*
- * Obtem o period
- */
-
- strcpy(arqper,dirname);
- strcat(arqper,"/sarg-period");
-
- if ((fp_in = fopen(arqper, "r")) == 0){
- fprintf(stderr, "SARG: (email) %s: %s\n",_("Cannot open file"),arqper);
- exit(1);
- }
-
- if (!fgets(period,sizeof(period),fp_in)) {
- fprintf(stderr,"SARG: (email) read error in %s\n",arqper);
- exit(1);
- }
- fclose(fp_in);
-
- if((fp_top1=fopen(top1,"r"))==NULL) {
- fprintf(stderr, "SARG: (email) %s: %s\n",_("Cannot open file"),top1);
- exit(1);
- }
-
- if((fp_top3=fopen(top3,"w"))==NULL) {
- fprintf(stderr, "SARG: (email) %s: %s\n",_("Cannot open file"),top3);
- exit(1);
- }
-
- strcpy(strip1,_("Squid User Access Report"));
- strip_latin(strip1);
- fprintf(fp_top3,"%s\n",strip1);
-
- strcpy(strip1,_("Decreasing Access (bytes)"));
- strip_latin(strip1);
- fprintf(fp_top3,"%s\n",strip1);
- strcpy(strip1,_("Period"));
- strip_latin(strip1);
- fprintf(fp_top3,"%s %s\n\n",strip1,period);
+//! Name of the file containing the e-mail to send.
+static char EmailFileName[MAXLEN]="";
- strcpy(strip1,_("NUM"));
- strip_latin(strip1);
- strcpy(strip2,_("USERID"));
- strip_latin(strip2);
- strcpy(strip3,_("CONNECT"));
- strip_latin(strip3);
- strcpy(strip4,_("BYTES"));
- strip_latin(strip4);
- strcpy(strip5,_("ELAPSED TIME"));
- strip_latin(strip5);
- strcpy(strip6,_("MILISEC"));
- strip_latin(strip6);
- strcpy(strip7,_("TIME"));
- strip_latin(strip7);
-
- fprintf(fp_top3,"%-7s %-20s %-8s %-15s %%%-6s %-10s %-10s %%%-7s\n------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",strip1,strip2,strip3,strip4,strip4,strip5,strip6,strip7);
-
- while(fgets(warea,sizeof(warea),fp_top1))
- {
- getword_start(&gwarea,warea);
- if (getword(user,sizeof(user),&gwarea,'\t')<0) {
- debuga(_("There may be a broken user in file %s"),top1);
- exit(1);
- }
- if (getword_atoll(&nbytes,&gwarea,'\t')<0) {
- debuga(_("There may be a broken number of bytes in file %s"),top1);
- exit(1);
- }
- if (getword_atoll(&nacc,&gwarea,'\t')<0) {
- debuga(_("There is a broken number of access in file %s"),top1);
- exit(1);
- }
- if (getword_atoll(&elap,&gwarea,'\t')<0) {
- debuga(_("There is a broken elapsed time in file %s"),top1);
- exit(1);
- }
-
- perc=(ttnbytes) ? nbytes * 100. / ttnbytes : 0;
- perc2=(ttnelap) ? elap * 100. / ttnelap : 0;
-
- posicao++;
-
- if(strcmp(user,"TOTAL") == 0){
- fputs("------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",fp_top3);
-#if defined(__FreeBSD__)
- fprintf(fp_top3,"%-7s %20s %8qu %15s %8s %9s %10qu\n",_("TOTAL")," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap);
-#elif defined(__alpha) || __ALPHA
- fprintf(fp_top3,"%-7s %20s %8ld %15s %8s %9s %10ld\n",_("TOTAL")," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap);
-#else
- fprintf(fp_top3,"%-7s %20s %8lld %15s %8s %9s %10lld\n",_("TOTAL")," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap);
-#endif
- } else {
-#if defined(__FreeBSD__)
- fprintf(fp_top3,"%7d %20s %8lld %15s %3.2lf%% %10s %10qu %3.2lf%%\n",posicao,user,nacc,fixnum(nbytes,1),perc,buildtime(elap),elap,perc2);
-#elif defined(__alpha) || __ALPHA
- fprintf(fp_top3,"%7d %20s %8lld %15s %3.2lf%% %10s %10ld %3.2lf%%\n",posicao,user,nacc,fixnum(nbytes,1),perc,buildtime(elap),elap,perc2);
-#else
- fprintf(fp_top3,"%7d %20s %8lld %15s %3.2lf%% %10s %10lld %3.2lf%%\n",posicao,user,nacc,fixnum(nbytes,1),perc,buildtime(elap),elap,perc2);
-#endif
- }
- }
-
- tnbytes=(totuser) ? ttnbytes / totuser : 0;
-
- strcpy(strip1,_("AVERAGE"));
- strip_latin(strip1);
-#if defined(__FreeBSD__)
- fprintf(fp_top3,"%-7s %20s %8qu %15s %8s %9s %10qu\n",strip1," ",ttnacc/totuser,fixnum(tnbytes,1)," ",buildtime(ttnelap/totuser),ttnelap/totuser);
-#elif defined(__alpha) || __ALPHA
- fprintf(fp_top3,"%-7s %20s %8ld %15s %8s %9s %10ld\n",strip1," ",ttnacc/totuser,fixnum(tnbytes,1)," ",buildtime(ttnelap/totuser),ttnelap/totuser);
-#else
- fprintf(fp_top3,"%-7s %20s %8lld %15s %8s %9s %10lld\n",strip1," ",ttnacc/totuser,fixnum(tnbytes,1)," ",buildtime(ttnelap/totuser),ttnelap/totuser);
-#endif
-
- fclose(fp_top1);
- unlink(top1);
-
- t = time(NULL);
- local = localtime(&t);
- fprintf(fp_top3, "\n%s\n", asctime(local));
-
- fclose(fp_top3);
-
- if(strcmp(email,"stdout") == 0) {
- if((fp_top3=fopen(top3,"r"))==NULL) {
- fprintf(stderr, "SARG: (email) %s: %s\n",_("Cannot open file"),top3);
- exit(1);
- }
-
- while(fgets(buf,sizeof(buf),fp_top3)!=NULL)
- fputs(buf,stdout);
- } else {
- sprintf(buf,"\"%s\" -s \"SARG %s, %s\" \"%s\" <\"%s\"",MailUtility,_("Report"),asctime(local),email,top3);
- cstatus=system(buf);
- if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
- fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus));
- fprintf(stderr, "SARG: command: %s\n",buf);
- exit(1);
- }
- }
-
- if (snprintf(warea,sizeof(warea),"%s/sarg",TempDir)>=sizeof(warea)) {
- fprintf(stderr,"SARG: Temporary directory name too long: %s\n",warea);
- exit(1);
- }
- unlinkdir(warea,0);
+/*!
+ * Generate a file name to write the e-mail and open the file.
+ *
+ * \param Module The module for which the e-mail is generated.
+ *
+ * \return The file to which the e-mail can be written.
+ */
+FILE *Email_OutputFile(const char *Module)
+{
+ FILE *fp;
+
+ if(strcmp(email,"stdout") == 0) {
+ EmailFileName[0]='\0';
+ return(stdout);
+ }
+
+ snprintf(EmailFileName,sizeof(EmailFileName),"%s/%s.int_unsort",tmp,Module);
+ if ((fp=fopen(EmailFileName,"w"))==NULL) {
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),EmailFileName,strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ return(fp);
+}
- return (0);
+/*!
+ * Send the e-mail.
+ *
+ * \param fp The file opened by Email_OutputFile().
+ */
+void Email_Send(FILE *fp,const char *Subject)
+{
+ char warea[MAXLEN];
+ int cstatus;
+
+ if (fp==stdout) return;//to stdout
+
+ if (fclose(fp)==EOF) {
+ debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),EmailFileName,strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+
+ snprintf(warea,sizeof(warea),"%s -s \"%s\" \"%s\" <\"%s\"",MailUtility,Subject,email,EmailFileName);
+ if (debug)
+ debuga(__FILE__,__LINE__,_("Sending mail with command: %s\n"),warea);
+ cstatus=system(warea);
+ if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
+ debuga(__FILE__,__LINE__,_("command return status %d\n"),WEXITSTATUS(cstatus));
+ debuga(__FILE__,__LINE__,_("command: %s\n"),warea);
+ exit(EXIT_FAILURE);
+ }
+ if (!KeepTempLog && unlink(EmailFileName)) {
+ debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),EmailFileName,strerror(errno));
+ exit(EXIT_FAILURE);
+ }
}