From: Frédéric Marchal Date: Tue, 3 Jul 2012 19:42:06 +0000 (+0200) Subject: Denied report structured as a module X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=8e53b2e7f648d61a3e083a16c32c4212ccb5bf98;p=thirdparty%2Fsarg.git Denied report structured as a module 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. --- diff --git a/denied.c b/denied.c index 2af2361..14e897d 100644 --- a/denied.c +++ b/denied.c @@ -26,29 +26,81 @@ #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)); } } diff --git a/include/conf.h b/include/conf.h index 4d8598e..7da17d9 100755 --- a/include/conf.h +++ b/include/conf.h @@ -439,7 +439,6 @@ char SortTableJs[256]; char HostAliasFile[512]; int idate; -int denied_count; int download_count; int authfail_count; int dansguardian_count; diff --git a/include/defs.h b/include/defs.h index f8c1e73..8b8b154 100755 --- a/include/defs.h +++ b/include/defs.h @@ -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 aec6d89..491b34f 100644 --- 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; diff --git a/readlog.c b/readlog.c index ec24349..09ab3f7 100644 --- 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); diff --git a/topuser.c b/topuser.c index 67c3df1..e85c0a3 100644 --- a/topuser.c +++ b/topuser.c @@ -238,7 +238,7 @@ void topuser(void) if(dansguardian_count) fprintf(fp_top3,"%s\n",_("DansGuardian")); if(redirector_count) fprintf(fp_top3,"%s\n",_("Redirector")); if ((ReportType & REPORT_TYPE_DOWNLOADS) != 0 && download_count && !Privacy && ndownload) fprintf(fp_top3,"%s\n",_("Downloads")); - if ((ReportType & REPORT_TYPE_DENIED) != 0 && denied_count && !Privacy) fprintf(fp_top3,"%s\n",_("Denied accesses")); + if (is_denied()) fprintf(fp_top3,"%s\n",_("Denied accesses")); if ((ReportType & REPORT_TYPE_AUTH_FAILURES) != 0 && authfail_count && !Privacy) fprintf(fp_top3,"%s\n",_("Authentication Failures")); if(smartfilter) fprintf(fp_top3,"%s\n",_("SmartFilter")); if(UserAgentLog[0] != '\0' && useragent_count) fprintf(fp_top3,"%s\n",_("Useragent"));