]> git.ipfire.org Git - thirdparty/sarg.git/commitdiff
Download report encapsulated in a module
authorFrédéric Marchal <fmarchal@users.sourceforge.net>
Fri, 31 Aug 2012 17:09:19 +0000 (19:09 +0200)
committerFrédéric Marchal <fmarchal@users.sourceforge.net>
Fri, 31 Aug 2012 17:09:19 +0000 (19:09 +0200)
Module build upon the model of the denied and authfail reports.

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

index 0da4e5cf60f695e6390b6112248253f463acfae0..37b4065a4378f85d948a566b8e233fbe83a0a632 100644 (file)
@@ -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));
+       }
+}
index c66ecfa4c9959070e86b79603a76cba54f92de38..8556c64bbf79022e1bf27d8ac8de45da925546be 100755 (executable)
@@ -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.
index 03170ca141346628bc5d6795c68e76ec836fbd8d..be362d1c481b11891edf6cc6247cf2cdc2c644c4 100755 (executable)
@@ -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 9c63c7b735875f91f9be233456a3b1464eb60f51..4e6053e480692e2d1c3e99a0cb8056f9fd4c2591 100644 (file)
--- 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);
index a9f636ce37f339aeb35cbb172529179f807983a8..5d5b040468f3d6e65f4530883e56e5fc5d878c24 100644 (file)
--- 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 || idata<mindate){
@@ -646,7 +630,7 @@ int ReadLogFile(struct ReadLogDataStruct *Filter)
 
        denied_close();
        authfail_close();
-       if (fp_Download_Unsort) fclose (fp_Download_Unsort);
+       download_close();
 
        for (ufile=first_user_file ; ufile ; ufile=ufile1) {
                ufile1=ufile->next;
index 2423a1f0b2d85c18ba3db77263e23640cef5853f..48b68b1ddb91a0500bfe96e4d21de427c5c01b63 100644 (file)
--- a/topuser.c
+++ b/topuser.c
@@ -237,7 +237,7 @@ void topuser(void)
                if((ReportType & REPORT_TYPE_SITES_USERS) != 0 && !Privacy) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"siteuser.html\">%s</a></td></tr>\n",_("Sites & Users"));
                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 (is_download()) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"download.html\">%s</a></td></tr>\n",_("Downloads"));
                if (is_denied()) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"denied.html\">%s</a></td></tr>\n",_("Denied accesses"));
                if (is_authfail()) 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"));