From: Frédéric Marchal Date: Fri, 31 Aug 2012 17:09:19 +0000 (+0200) Subject: Download report encapsulated in a module X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=112845356f18848881d3c4738af59e686881b69f;p=thirdparty%2Fsarg.git Download report encapsulated in a module Module build upon the model of the denied and authfail reports. --- diff --git a/download.c b/download.c index 0da4e5c..37b4065 100644 --- a/download.c +++ b/download.c @@ -45,6 +45,81 @@ The number of suffixes in ::DownloadSuffixIndex. */ static int NDownloadSuffix=0; +//! Name of the file containing the unsorted downloaded entries. +static char download_unsort[MAXLEN]=""; +//! The file handle to write the entries. +static FILE *fp_download=NULL; +//! \c True if at least one downloaded entry exists. +static bool download_exists=false; + +/*! +Open a file to store the denied accesses. + +\return The file handle or NULL if no file is necessary. +*/ +void download_open(void) +{ + if ((ReportType & REPORT_TYPE_DOWNLOADS) == 0) { + if (debugz) debugaz(_("Download report not produced as it is not requested\n")); + return; + } + if (Privacy) { + if (debugz) debugaz(_("Download report not produced because privacy option is active\n")); + return; + } + + snprintf(download_unsort,sizeof(download_unsort),"%s/download.int_unsort",tmp); + if ((fp_download=MY_FOPEN(download_unsort,"w"))==NULL) { + debuga(_("(log) Cannot open file: %s - %s\n"),download_unsort,strerror(errno)); + exit(EXIT_FAILURE); + } + return; +} + +/*! +Write one entry in the unsorted downloaded file provided that it is required. + +\param log_entry The entry to write into the log file. +\param url The URL of the downloaded file. +*/ +void download_write(const struct ReadLogStruct *log_entry,const char *url) +{ + char date[80]; + + if (fp_download && strstr(log_entry->HttpCode,"DENIED") != 0) { + strftime(date,sizeof(date),"%d/%m/%Y\t%H:%M:%S",&log_entry->EntryTime); + fprintf(fp_download,"%s\t%s\t%s\t%s\n",date,log_entry->User,log_entry->Ip,url); + download_exists=true; + } +} + +/*! +Close the file opened by denied_open(). +*/ +void download_close(void) +{ + if (fp_download) + { + if (fclose(fp_download)==EOF) + { + debuga(_("Write error in %s: %s\n"),download_unsort,strerror(errno)); + exit(EXIT_FAILURE); + } + fp_download=NULL; + } +} + +/*! +Tell the caller if a download report exists. + +\return \c True if the report is available or \c false if no report +was generated. +*/ +bool is_download(void) +{ + return(download_exists); +} + /*! Sort the raw log file with the downloaded files. @@ -59,10 +134,10 @@ static void download_sort(const char *report_in) char csort[MAXLEN]; int cstatus; - clen=snprintf(csort,sizeof(csort),"sort -T \"%s\" -t \"\t\" -k 3,3 -k 1,1 -k 2,2 -k 5,5 -o \"%s\" \"%s/download.int_unsort\"", - tmp, report_in, tmp); + clen=snprintf(csort,sizeof(csort),"sort -T \"%s\" -t \"\t\" -k 3,3 -k 1,1 -k 2,2 -k 5,5 -o \"%s\" \"%s\"", + tmp, report_in, download_unsort); if (clen>=sizeof(csort)) { - debuga(_("Path too long to sort the file: %s/download.int_unsort\n"),tmp); + debuga(_("Path too long to sort the file: %s\n"),download_unsort); exit(EXIT_FAILURE); } cstatus=system(csort); @@ -71,13 +146,12 @@ static void download_sort(const char *report_in) debuga(_("sort command: %s\n"),csort); exit(EXIT_FAILURE); } - if (snprintf(csort,sizeof(csort),"%s/download.int_unsort",tmp)>=sizeof(csort)) { - debuga(_("Path too long for %s/download.int_unsort\n"),tmp); - exit(EXIT_FAILURE); - } - if (!KeepTempLog && unlink(csort)) { - debuga(_("Cannot delete \"%s\": %s\n"),csort,strerror(errno)); - exit(EXIT_FAILURE); + if (!KeepTempLog) { + if (unlink(download_unsort)) { + debuga(_("Cannot delete \"%s\": %s\n"),download_unsort,strerror(errno)); + exit(EXIT_FAILURE); + } + download_unsort[0]='\0'; } } @@ -110,7 +184,10 @@ void download_report(void) struct userinfostruct *uinfo; struct tm t; - if (!ndownload) { + if (!download_exists) { + if (!KeepTempLog && download_unsort[0]!='\0' && unlink(download_unsort)) + debuga(_("Cannot delete \"%s\": %s\n"),download_unsort,strerror(errno)); + download_unsort[0]='\0'; if (debugz) debugaz(_("No downloaded files to report\n")); return; } @@ -121,10 +198,6 @@ void download_report(void) // sort the raw file snprintf(report_in,sizeof(report_in),"%s/download.int_log",tmp); download_sort(report_in); - if(access(report_in, R_OK) != 0) { - debugaz(_("Sorted file doesn't exist (to produce the download report)\n")); - exit(EXIT_FAILURE); - } // produce the report. snprintf(report,sizeof(report),"%s/download.html",outdirname); @@ -367,3 +440,18 @@ bool is_download_suffix(const char *url) return(false); } +/*! +Remove any temporary file left by the download module. +*/ +void download_cleanup(void) +{ + if (fp_download) + { + fclose(fp_download); + fp_download=NULL; + } + if (download_unsort[0]) { + if (unlink(download_unsort)==-1) + debuga(_("Failed to delete %s: %s\n"),download_unsort,strerror(errno)); + } +} diff --git a/include/conf.h b/include/conf.h index c66ecfa..8556c64 100755 --- a/include/conf.h +++ b/include/conf.h @@ -462,7 +462,6 @@ unsigned long int records_kept; unsigned long int nusers; int idate; -int download_count; int dansguardian_count; int redirector_count; int useragent_count; @@ -473,9 +472,9 @@ int sarglog; int isalog; int dfrom; int duntil; -int dataonly; -bool indexonly; -bool iprel; +bool dataonly; +bool indexonly; +bool iprel; int langcode; int debug; int debugz; @@ -492,7 +491,6 @@ int realtime_access_log_lines; int rc; int ntopsites; int nrepday; -int ndownload; int ntopuser; bool squid24; //! \c True to keep the temporary files for inspection. diff --git a/include/defs.h b/include/defs.h index 03170ca..be362d1 100755 --- a/include/defs.h +++ b/include/defs.h @@ -152,10 +152,15 @@ void gen_denied_report(void); void denied_cleanup(void); // download.c +void download_open(void); +void download_write(const struct ReadLogStruct *log_entry,const char *url); +void download_close(void); +bool is_download(void); void download_report(void); void free_download(void); void set_download_suffix(const char *list); bool is_download_suffix(const char *url); +void download_cleanup(void); // email.c int geramail(const char *dirname, int debug, const char *outdir, const char *email, const char *TempDir); diff --git a/log.c b/log.c index 9c63c7b..4e6053e 100644 --- a/log.c +++ b/log.c @@ -233,7 +233,6 @@ int main(int argc,char *argv[]) hm_str[0]='\0'; HostAliasFile[0]='\0'; - download_count=0; dansguardian_count=0; redirector_count=0; useragent_count=0; @@ -253,7 +252,6 @@ int main(int argc,char *argv[]) realtime_access_log_lines=1000; cost=0.01; nocost=50000000; - ndownload=0; squid24=false; dfrom=0; duntil=0; @@ -468,9 +466,7 @@ int main(int argc,char *argv[]) else strcpy(ImageFile,"../../../images"); - dataonly=0; - if(DataFile[0] != '\0') - dataonly++; + dataonly=(DataFile[0] != '\0'); if (df=='\0') df=DateFormat; if (df=='\0') df='u'; @@ -725,6 +721,7 @@ int main(int argc,char *argv[]) denied_cleanup(); authfail_cleanup(); + download_cleanup(); if(!KeepTempLog && strcmp(tmp,"/tmp") != 0) { unlinkdir(tmp,0); diff --git a/readlog.c b/readlog.c index a9f636c..5d5b040 100644 --- a/readlog.c +++ b/readlog.c @@ -77,7 +77,6 @@ int ReadLogFile(struct ReadLogDataStruct *Filter) char dia[128]=""; char wuser[MAXLEN]; char tmp3[MAXLEN]; - char sz_Download_Unsort[20000]; char start_hour[128]; char download_url[MAXLEN]; char smartfilter[MAXLEN]; @@ -105,7 +104,6 @@ int ReadLogFile(struct ReadLogDataStruct *Filter) long int totregsx=0; FILE *fp_in=NULL; FILE *fp_log=NULL; - FILE *fp_Download_Unsort=NULL; bool from_pipe; bool from_stdin; bool download_flag=false; @@ -127,11 +125,10 @@ int ReadLogFile(struct ReadLogDataStruct *Filter) start_hour[0]='\0'; first_user_file=NULL; - snprintf(sz_Download_Unsort,sizeof(sz_Download_Unsort),"%s/download.int_unsort", tmp); - - if(DataFile[0]=='\0') { + if (!dataonly) { denied_open(); authfail_open(); + download_open(); } if ((line=longline_create())==NULL) { @@ -385,7 +382,6 @@ int ReadLogFile(struct ReadLogDataStruct *Filter) download_flag=is_download_suffix(log_entry.Url); if (download_flag) { safe_strcpy(download_url,log_entry.Url,sizeof(download_url)); - download_count++; } } else download_flag=false; @@ -547,21 +543,9 @@ int ReadLogFile(struct ReadLogDataStruct *Filter) totregsg++; - if(!dataonly && download_flag && strstr(log_entry.HttpCode,"DENIED") == 0) { - ndownload = 1; - - if ( ! fp_Download_Unsort ) { - if ((fp_Download_Unsort = MY_FOPEN ( sz_Download_Unsort, "a")) == NULL) { - debuga(_("(log) Cannot open temporary file: %s - %s\n"),sz_Download_Unsort, strerror(errno)); - exit (1); - } - } - fprintf(fp_Download_Unsort,"%s\t%s\t%s\t%s\t%s\n",dia,hora, - log_entry.User,log_entry.Ip,download_url); - } - denied_write(&log_entry); authfail_write(&log_entry); + download_write(&log_entry,download_url); if (current_format!=&ReadSargLog) { if(!totper || idatanext; diff --git a/topuser.c b/topuser.c index 2423a1f..48b68b1 100644 --- a/topuser.c +++ b/topuser.c @@ -237,7 +237,7 @@ void topuser(void) if((ReportType & REPORT_TYPE_SITES_USERS) != 0 && !Privacy) fprintf(fp_top3,"%s\n",_("Sites & Users")); 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 (is_download()) fprintf(fp_top3,"%s\n",_("Downloads")); if (is_denied()) fprintf(fp_top3,"%s\n",_("Denied accesses")); if (is_authfail()) fprintf(fp_top3,"%s\n",_("Authentication Failures")); if(smartfilter) fprintf(fp_top3,"%s\n",_("SmartFilter"));