]> git.ipfire.org Git - thirdparty/sarg.git/commitdiff
Denied report structured as a module
authorFrédéric Marchal <fmarchal@users.sourceforge.net>
Tue, 3 Jul 2012 19:42:06 +0000 (21:42 +0200)
committerFrédéric Marchal <fmarchal@users.sourceforge.net>
Tue, 3 Jul 2012 19:42:06 +0000 (21:42 +0200)
The denied report is produced by a module taking care of (almost) every
aspect of the report creation by itself.

The only point still taken into account outside of the module is the
DataFile variable that disable the denied report.

denied.c
include/conf.h
include/defs.h
log.c
readlog.c
topuser.c

index 2af236164aae8913bf8a74313b8b7377218a2622..14e897d5d6a935ef0e0acf609b99149535a9c168 100644 (file)
--- a/denied.c
+++ b/denied.c
 
 #include "include/conf.h"
 #include "include/defs.h"
+#include "include/readlog.h"
 
 //! 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.
+static bool denied_exists=false;
 
 /*!
 Open a file to store the denied accesses.
 
 \return The file handle or NULL if no file is necessary.
 */
-FILE *denied_open(void)
+void denied_open(void)
 {
-       FILE *fp_denied;
-       
-       if((ReportType & REPORT_TYPE_DENIED) == 0) return(NULL);
+       if ((ReportType & REPORT_TYPE_DENIED) == 0) {
+               if (debugz) debugaz(_("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"));
+               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));
                exit(EXIT_FAILURE);
        }
-       return(fp_denied);
+       return;
+}
+
+/*!
+Write one entry in the unsorted denied file provided that it is required.
+
+\param log_entry The entry to write into the log file.
+*/
+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);
+               fprintf(fp_denied, "%s\t%s\t%s\t%s\n",date,log_entry->User,log_entry->Ip,log_entry->Url);
+               denied_exists=true;
+       }
+}
+
+/*!
+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));
+                       exit(EXIT_FAILURE);
+               }
+       }
+}
+
+/*!
+Tell the caller if a denied report exists.
+
+\return \c True if the report is available or \c false if no report
+was generated.
+*/
+bool is_denied(void)
+{
+       return(denied_exists);
 }
 
 static void show_ignored_denied(FILE *fp_ou,int count)
@@ -91,8 +143,12 @@ void gen_denied_report(void)
        ouser2[0]='\0';
 
        sprintf(denied_sort,"%s/denied.int_log",tmp);
-       if (!denied_count) {
-               unlink(denied_sort);
+       if (!denied_exists) {
+               if (unlink(denied_sort)==-1)
+               {
+                       debuga(_("Failed to delete %s: %s\n"),denied_sort,strerror(errno));
+               }
+               denied_unsort[0]='\0';
                if (debugz) debugaz(_("Denied report not produced because it is empty\n"));
                return;
        }
@@ -233,9 +289,11 @@ Remove any temporary file left by the denied module.
 void denied_cleanup(void)
 {
        if(denied_sort[0]) {
-               unlink(denied_sort);
+               if (unlink(denied_sort)==-1)
+                       debuga(_("Failed to delete %s: %s\n"),denied_sort,strerror(errno));
        }
        if(denied_unsort[0]) {
-               unlink(denied_unsort);
+               if (unlink(denied_unsort)==-1)
+                       debuga(_("Failed to delete %s: %s\n"),denied_unsort,strerror(errno));
        }
 }
index 4d8598e7eccefea69e2ea7f74eca6cdbb669e291..7da17d9a6d30b1bc88dbdfedec6e1b167a7d2f35 100755 (executable)
@@ -439,7 +439,6 @@ char SortTableJs[256];
 char HostAliasFile[512];
 
 int  idate;
-int  denied_count;
 int  download_count;
 int  authfail_count;
 int  dansguardian_count;
index f8c1e7315e57cb2914999bdd246c238e36a04c07..8b8b1541297645587f4646f6370e42929fd12c00 100755 (executable)
@@ -2,6 +2,8 @@
 \brief Declaration of the structures and functions.
 */
 
+#include "readlog.h"
+
 struct getwordstruct
 {
    const char *current;
@@ -137,7 +139,10 @@ void data_file(char *tmp);
 FILE *decomp(const char *arq, bool *pipe);
 
 // denied.c
-FILE *denied_open(void);
+void denied_open(void);
+void denied_write(const struct ReadLogStruct *log_entry);
+void denied_close(void);
+bool is_denied(void);
 void gen_denied_report(void);
 void denied_cleanup(void);
 
diff --git a/log.c b/log.c
index aec6d893f9e6430ddd25caa73eb123c76d8aec91..491b34f924f679b119626e266d486c38af73ecb2 100644 (file)
--- a/log.c
+++ b/log.c
@@ -220,7 +220,6 @@ int main(int argc,char *argv[])
        hm_str[0]='\0';
        HostAliasFile[0]='\0';
 
-       denied_count=0;
        download_count=0;
        authfail_count=0;
        dansguardian_count=0;
index ec243495a2d97d4ae4e99315b578d325caa2f09b..09ab3f771db9dfc6e43af80bf1bacb328182d408 100644 (file)
--- a/readlog.c
+++ b/readlog.c
@@ -117,7 +117,6 @@ int ReadLogFile(struct ReadLogDataStruct *Filter)
        long long int iyear, imonth, iday;
        FILE *fp_in=NULL;
        FILE *fp_log=NULL;
-       FILE *fp_denied=NULL;
        FILE *fp_authfail=NULL;
        FILE *fp_Download_Unsort=NULL;
        bool from_pipe;
@@ -145,9 +144,7 @@ int ReadLogFile(struct ReadLogDataStruct *Filter)
        snprintf(authfail_unsort,sizeof(authfail_unsort),"%s/authfail.int_unsort",tmp);
 
        if(DataFile[0]=='\0') {
-               if((ReportType & REPORT_TYPE_DENIED) != 0) {
-                       fp_denied=denied_open();
-               }
+               denied_open();
 
                if((ReportType & REPORT_TYPE_DENIED) != 0 || (ReportType & REPORT_TYPE_AUTH_FAILURES) != 0) {
                        if((fp_authfail=MY_FOPEN(authfail_unsort,"w"))==NULL) {
@@ -865,10 +862,7 @@ int ReadLogFile(struct ReadLogDataStruct *Filter)
                                fprintf(fp_Download_Unsort,"%s\t%s\t%s\t%s\t%s\n",dia,hora,log_entry.User,log_entry.Ip,download_url);
                        }
 
-                       if(fp_denied && strstr(log_entry.HttpCode,"DENIED/403") != 0) {
-                               fprintf(fp_denied, "%s\t%s\t%s\t%s\t%s\n",dia,hora,log_entry.User,log_entry.Ip,log_entry.Url);
-                               denied_count++;
-                       }
+                       denied_write(&log_entry);
                        if((ReportType & REPORT_TYPE_AUTH_FAILURES) != 0) {
                                if(fp_authfail && (strstr(log_entry.HttpCode,"DENIED/401") != 0 || strstr(log_entry.HttpCode,"DENIED/407") != 0)) {
                                        fprintf(fp_authfail, "%s\t%s\t%s\t%s\t%s\n",dia,hora,log_entry.User,log_entry.Ip,log_entry.Url);
@@ -953,7 +947,7 @@ int ReadLogFile(struct ReadLogDataStruct *Filter)
                        debuga(_("Sarg parsed log saved as %s\n"),arq_log);
        }       
 
-       if (fp_denied) fclose(fp_denied);
+       denied_close();
        if (fp_authfail) fclose(fp_authfail);
        if (fp_Download_Unsort) fclose (fp_Download_Unsort);
 
index 67c3df1c02c95861f6b32e463a53c42870ea138a..e85c0a38271a4eacbcb1eafaed986789ff063149 100644 (file)
--- a/topuser.c
+++ b/topuser.c
@@ -238,7 +238,7 @@ void topuser(void)
                if(dansguardian_count) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"dansguardian.html\">%s</a></td></tr>\n",_("DansGuardian"));
                if(redirector_count) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"redirector.html\">%s</a></td></tr>\n",_("Redirector"));
                if ((ReportType & REPORT_TYPE_DOWNLOADS) != 0 && download_count && !Privacy && ndownload) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"download.html\">%s</a></td></tr>\n",_("Downloads"));
-               if ((ReportType & REPORT_TYPE_DENIED) != 0 && denied_count && !Privacy) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"denied.html\">%s</a></td></tr>\n",_("Denied accesses"));
+               if (is_denied()) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"denied.html\">%s</a></td></tr>\n",_("Denied accesses"));
                if ((ReportType & REPORT_TYPE_AUTH_FAILURES) != 0 && authfail_count && !Privacy) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"authfail.html\">%s</a></td></tr>\n",_("Authentication Failures"));
                if(smartfilter) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"smartfilter.html\">%s</a></td></tr>\n",_("SmartFilter"));
                if(UserAgentLog[0] != '\0' && useragent_count) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"useragent.html\">%s</a></td></tr>\n",_("Useragent"));