]> git.ipfire.org Git - thirdparty/sarg.git/blobdiff - denied.c
Indent the configure script for more readability.
[thirdparty/sarg.git] / denied.c
index 14e897d5d6a935ef0e0acf609b99149535a9c168..88cd37f1ac447341bf6c84aef108846aca180a32 100644 (file)
--- a/denied.c
+++ b/denied.c
@@ -1,6 +1,6 @@
 /*
  * SARG Squid Analysis Report Generator      http://sarg.sourceforge.net
- *                                                            1998, 2012
+ *                                                            1998, 2015
  *
  * SARG donations:
  *      please look at http://sarg.sourceforge.net/donations.php
@@ -30,8 +30,6 @@
 
 //! Name of the file containing the unsorted denied entries.
 static char denied_unsort[MAXLEN]="";
-//! Name of the file containing the sorted denied entries.
-static char denied_sort[MAXLEN]="";
 //! The file handle to write the entries.
 static FILE *fp_denied=NULL;
 //! \c True if at least one denied entry exists.
@@ -45,17 +43,17 @@ Open a file to store the denied accesses.
 void denied_open(void)
 {
        if ((ReportType & REPORT_TYPE_DENIED) == 0) {
-               if (debugz) debugaz(_("Denied report not produced as it is not requested\n"));
+               if (debugz>=LogLevel_Process) debugaz(__FILE__,__LINE__,_("Denied report not produced as it is not requested\n"));
                return;
        }
        if (Privacy) {
-               if (debugz) debugaz(_("Denied report not produced because privacy option is active\n"));
+               if (debugz>=LogLevel_Process) debugaz(__FILE__,__LINE__,_("Denied report not produced because privacy option is active\n"));
                return;
        }
-       
+
        snprintf(denied_unsort,sizeof(denied_unsort),"%s/denied.int_unsort",tmp);
        if ((fp_denied=MY_FOPEN(denied_unsort,"w"))==NULL) {
-               debuga(_("(log) Cannot open file: %s - %s\n"),denied_unsort,strerror(errno));
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),denied_unsort,strerror(errno));
                exit(EXIT_FAILURE);
        }
        return;
@@ -69,9 +67,9 @@ Write one entry in the unsorted denied file provided that it is required.
 void denied_write(const struct ReadLogStruct *log_entry)
 {
        char date[80];
-       
+
        if (fp_denied && strstr(log_entry->HttpCode,"DENIED/403") != 0) {
-               strftime(date,sizeof(date),"%d/%m/%Y\t%H:%M:%S",log_entry->EntryTime);
+               strftime(date,sizeof(date),"%d/%m/%Y\t%H:%M:%S",&log_entry->EntryTime);
                fprintf(fp_denied, "%s\t%s\t%s\t%s\n",date,log_entry->User,log_entry->Ip,log_entry->Url);
                denied_exists=true;
        }
@@ -82,13 +80,12 @@ Close the file opened by denied_open().
 */
 void denied_close(void)
 {
-       if (fp_denied)
-       {
-               if (fclose(fp_denied)==EOF)
-               {
-                       debuga(_("Write error in %s: %s\n"),denied_unsort,strerror(errno));
+       if (fp_denied) {
+               if (fclose(fp_denied)==EOF) {
+                       debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),denied_unsort,strerror(errno));
                        exit(EXIT_FAILURE);
                }
+               fp_denied=NULL;
        }
 }
 
@@ -116,20 +113,22 @@ Generate a report containing the denied accesses.
 */
 void gen_denied_report(void)
 {
-       FILE *fp_in = NULL, *fp_ou = NULL;
+       FileObject *fp_in = NULL;
+       FILE *fp_ou = NULL;
 
        char *buf;
        char *url;
+       char denied_sort[MAXLEN];
        char report[MAXLEN];
        char ip[MAXLEN];
        char oip[MAXLEN];
        char user[MAXLEN];
-       char ouser[MAXLEN];
-       char ouser2[MAXLEN];
+       char ouser[MAXLEN]="";
+       char ouser2[MAXLEN]="";
        char data[15];
        char hora[15];
        char csort[4098];
-       bool  z=false;
+       bool z=false;
        int  count=0;
        int day,month,year;
        int cstatus;
@@ -139,45 +138,45 @@ void gen_denied_report(void)
        struct userinfostruct *uinfo;
        struct tm t;
 
-       ouser[0]='\0';
-       ouser2[0]='\0';
-
-       sprintf(denied_sort,"%s/denied.int_log",tmp);
        if (!denied_exists) {
-               if (unlink(denied_sort)==-1)
-               {
-                       debuga(_("Failed to delete %s: %s\n"),denied_sort,strerror(errno));
-               }
+               if (!KeepTempLog && denied_unsort[0]!='\0' && unlink(denied_unsort))
+                       debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),denied_unsort,strerror(errno));
                denied_unsort[0]='\0';
-               if (debugz) debugaz(_("Denied report not produced because it is empty\n"));
+               if (debugz>=LogLevel_Process) debugaz(__FILE__,__LINE__,_("Denied report not produced because it is empty\n"));
                return;
        }
+       if (debugz>=LogLevel_Process)
+               debuga(__FILE__,__LINE__,_("Creating denied accesses report...\n"));
 
+       if (snprintf(denied_sort,sizeof(denied_sort),"%s/denied.int_log",tmp)>=sizeof(denied_sort)) {
+               debuga(__FILE__,__LINE__,_("Temporary directory path too long to sort the denied accesses\n"));
+               exit(EXIT_FAILURE);
+       }
        if (snprintf(csort,sizeof(csort),"sort -T \"%s\" -t \"\t\" -k 3,3 -k 5,5 -o \"%s\" \"%s\"",tmp,denied_sort,denied_unsort)>=sizeof(csort)) {
-               debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),denied_unsort,denied_sort);
+               debuga(__FILE__,__LINE__,_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),denied_unsort,denied_sort);
                exit(EXIT_FAILURE);
        }
        cstatus=system(csort);
        if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
-               debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
-               debuga(_("sort command: %s\n"),csort);
+               debuga(__FILE__,__LINE__,_("sort command return status %d\n"),WEXITSTATUS(cstatus));
+               debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
                exit(EXIT_FAILURE);
        }
        if (unlink(denied_unsort)) {
-               debuga(_("Cannot delete %s - %s\n"),denied_unsort,strerror(errno));
+               debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),denied_unsort,strerror(errno));
                exit(EXIT_FAILURE);
        }
        denied_unsort[0]='\0';
 
        sprintf(report,"%s/denied.html",outdirname);
 
-       if((fp_in=MY_FOPEN(denied_sort,"r"))==NULL) {
-               debuga(_("(denied) Cannot open log file %s\n"),denied_sort);
+       if((fp_in=FileObject_Open(denied_sort))==NULL) {
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),denied_sort,FileObject_GetLastOpenError());
                exit(EXIT_FAILURE);
        }
 
        if((fp_ou=MY_FOPEN(report,"w"))==NULL) {
-               debuga(_("(denied) Cannot open log file %s\n"),report);
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),report,strerror(errno));
                exit(EXIT_FAILURE);
        }
 
@@ -192,7 +191,7 @@ void gen_denied_report(void)
        fprintf(fp_ou,"<tr><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr>\n",_("USERID"),_("IP/NAME"),_("DATE/TIME"),_("ACCESSED SITE"));
 
        if ((line=longline_create())==NULL) {
-               debuga(_("Not enough memory to read the denied accesses\n"));
+               debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\"\n"),denied_sort);
                exit(EXIT_FAILURE);
        }
 
@@ -200,11 +199,11 @@ void gen_denied_report(void)
                getword_start(&gwarea,buf);
                if (getword(data,sizeof(data),&gwarea,'\t')<0 || getword(hora,sizeof(hora),&gwarea,'\t')<0 ||
                    getword(user,sizeof(user),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0) {
-                       debuga(_("There is a broken record or garbage in file %s\n"),denied_sort);
+                       debuga(__FILE__,__LINE__,_("Invalid record in file \"%s\"\n"),denied_sort);
                        exit(EXIT_FAILURE);
                }
                if (getword_ptr(buf,&url,&gwarea,'\t')<0) {
-                       debuga(_("There is a broken url in file %s\n"),denied_sort);
+                       debuga(__FILE__,__LINE__,_("Invalid url in file \"%s\"\n"),denied_sort);
                        exit(EXIT_FAILURE);
                }
                if (sscanf(data,"%d/%d/%d",&day,&month,&year)!=3) continue;
@@ -213,7 +212,7 @@ void gen_denied_report(void)
 
                uinfo=userinfo_find_from_id(user);
                if (!uinfo) {
-                       debuga(_("Unknown user ID %s in file %s\n"),user,denied_sort);
+                       debuga(__FILE__,__LINE__,_("Unknown user ID %s in file \"%s\"\n"),user,denied_sort);
                        exit(EXIT_FAILURE);
                }
 
@@ -264,21 +263,24 @@ void gen_denied_report(void)
                output_html_link(fp_ou,url,100);
                fputs("</td></tr>\n",fp_ou);
        }
-       fclose(fp_in);
+       if (FileObject_Close(fp_in)) {
+               debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),denied_sort,FileObject_GetLastCloseError());
+               exit(EXIT_FAILURE);
+       }
        longline_destroy(&line);
 
        if(count>DeniedReportLimit && DeniedReportLimit>0)
                show_ignored_denied(fp_ou,count-DeniedReportLimit);
 
        fputs("</table></div>\n",fp_ou);
-       if (write_html_trailer(fp_ou)<0)
-               debuga(_("Write error in file %s\n"),report);
-       if (fclose(fp_ou)<0)
-               debuga(_("Failed to close file %s - %s\n"),report,strerror(errno));
+       write_html_trailer(fp_ou);
+       if (fclose(fp_ou)==EOF) {
+               debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),report,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
 
-       if (unlink(denied_sort)==-1)
-               debuga(_("Failed to delete the file \"%s\" after processing it - %s\n"),denied_sort,strerror(errno));
-       denied_sort[0]='\0';
+       if (!KeepTempLog && unlink(denied_sort)==-1)
+               debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),denied_sort,strerror(errno));
 
        return;
 }
@@ -288,12 +290,15 @@ Remove any temporary file left by the denied module.
 */
 void denied_cleanup(void)
 {
-       if(denied_sort[0]) {
-               if (unlink(denied_sort)==-1)
-                       debuga(_("Failed to delete %s: %s\n"),denied_sort,strerror(errno));
+       if (fp_denied){
+               if (fclose(fp_denied)==EOF) {
+                       debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),denied_unsort,strerror(errno));
+                       exit(EXIT_FAILURE);
+               }
+               fp_denied=NULL;
        }
-       if(denied_unsort[0]) {
+       if (!KeepTempLog && denied_unsort[0]) {
                if (unlink(denied_unsort)==-1)
-                       debuga(_("Failed to delete %s: %s\n"),denied_unsort,strerror(errno));
+                       debuga(__FILE__,__LINE__,_("Failed to delete \"%s\": %s\n"),denied_unsort,strerror(errno));
        }
 }