X-Git-Url: http://git.ipfire.org/?a=blobdiff_plain;f=download.c;h=f826e7014bbb851eb398fd019299ea66bad56e51;hb=57b01ce6fa1ea0622d91fb1ebcabcb2ccb648ba6;hp=0da4e5cf60f695e6390b6112248253f463acfae0;hpb=bd43d81faa4dbf3000558431dc94b1d69dc6e636;p=thirdparty%2Fsarg.git diff --git a/download.c b/download.c index 0da4e5c..f826e70 100644 --- a/download.c +++ b/download.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 @@ -26,6 +26,7 @@ #include "include/conf.h" #include "include/defs.h" +#include "include/readlog.h" /*! The buffer to store the list of the suffixes to take into account when generating @@ -45,6 +46,80 @@ 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>=LogLevel_Process) debugaz(__FILE__,__LINE__,_("Download report not produced as it is not requested\n")); + return; + } + if (Privacy) { + if (debugz>=LogLevel_Process) debugaz(__FILE__,__LINE__,_("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(__FILE__,__LINE__,_("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(__FILE__,__LINE__,_("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,25 +134,24 @@ 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(__FILE__,__LINE__,_("Path too long to sort file \"%s\"\n"),download_unsort); 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); - 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); + debuga(__FILE__,__LINE__,_("sort command return status %d\n"),WEXITSTATUS(cstatus)); + debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort); 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(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),download_unsort,strerror(errno)); + exit(EXIT_FAILURE); + } + download_unsort[0]='\0'; } } @@ -87,7 +161,8 @@ is set with set_download_suffix(). */ void download_report(void) { - FILE *fp_in = NULL, *fp_ou = NULL; + FileObject *fp_in = NULL; + FILE *fp_ou = NULL; char *buf; char *url; @@ -110,32 +185,33 @@ void download_report(void) struct userinfostruct *uinfo; struct tm t; - if (!ndownload) { - if (debugz) debugaz(_("No downloaded files to report\n")); + if (!download_exists) { + if (!KeepTempLog && download_unsort[0]!='\0' && unlink(download_unsort)) + debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),download_unsort,strerror(errno)); + download_unsort[0]='\0'; + if (debugz>=LogLevel_Process) debugaz(__FILE__,__LINE__,_("No downloaded files to report\n")); return; } + if (debugz>=LogLevel_Process) + debuga(__FILE__,__LINE__,_("Creating download report...\n")); ouser[0]='\0'; ouser2[0]='\0'; // 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); - if((fp_in=MY_FOPEN(report_in,"r"))==NULL) { - debuga(_("(download) Cannot open log file %s\n"),report_in); + if((fp_in=FileObject_Open(report_in))==NULL) { + debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),report_in,FileObject_GetLastOpenError()); exit(EXIT_FAILURE); } if((fp_ou=MY_FOPEN(report,"w"))==NULL) { - debuga(_("(download) Cannot open log file %s\n"),report); + debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),report,strerror(errno)); exit(EXIT_FAILURE); } @@ -150,7 +226,7 @@ void download_report(void) fprintf(fp_ou,"%s%s%s%s\n",_("USERID"),_("IP/NAME"),_("DATE/TIME"),_("ACCESSED SITE")); if ((line=longline_create())==NULL) { - debuga(_("Not enough memory to read the downloaded files\n")); + debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\"\n"),report_in); exit(EXIT_FAILURE); } @@ -158,11 +234,11 @@ void download_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"),report_in); + debuga(__FILE__,__LINE__,_("Invalid record in file \"%s\"\n"),report_in); exit(EXIT_FAILURE); } if (getword_ptr(buf,&url,&gwarea,'\t')<0) { - debuga(_("There is a broken url in file %s\n"),report_in); + debuga(__FILE__,__LINE__,_("Invalid url in file \"%s\"\n"),report_in); exit(EXIT_FAILURE); } if (sscanf(data,"%d/%d/%d",&day,&month,&year)!=3) continue; @@ -171,7 +247,7 @@ void download_report(void) uinfo=userinfo_find_from_id(user); if (!uinfo) { - debuga(_("Unknown user ID %s in file %s\n"),user,report_in); + debuga(__FILE__,__LINE__,_("Unknown user ID %s in file \"%s\"\n"),user,report_in); exit(EXIT_FAILURE); } new_user=false; @@ -221,17 +297,21 @@ void download_report(void) output_html_link(fp_ou,url,100); fputs("\n",fp_ou); } - fclose(fp_in); + if (FileObject_Close(fp_in)) { + debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),report_in,FileObject_GetLastCloseError()); + exit(EXIT_FAILURE); + } longline_destroy(&line); fputs("\n",fp_ou); - if (write_html_trailer(fp_ou)<0) - debuga(_("Write error in file %s\n"),report); - if (fclose(fp_ou)==EOF) - 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 (!KeepTempLog && unlink(report_in)) { - debuga(_("Cannot delete \"%s\": %s\n"),report_in,strerror(errno)); + debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),report_in,strerror(errno)); exit(EXIT_FAILURE); } @@ -272,7 +352,7 @@ void set_download_suffix(const char *list) DownloadSuffix=strdup(list); if (!DownloadSuffix) { - debuga(_("Download suffix list too long\n")); + debuga(__FILE__,__LINE__,_("Download suffix list too long\n")); exit(EXIT_FAILURE); } j = 1; @@ -280,7 +360,7 @@ void set_download_suffix(const char *list) if (list[i] == ',') j++; DownloadSuffixIndex=malloc(j*sizeof(char *)); if (!DownloadSuffixIndex) { - debuga(_("Too many download suffixes\n")); + debuga(__FILE__,__LINE__,_("Too many download suffixes\n")); exit(EXIT_FAILURE); } @@ -367,3 +447,20 @@ 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) { + if (fclose(fp_download)==EOF) { + debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),download_unsort,strerror(errno)); + exit(EXIT_FAILURE); + } + fp_download=NULL; + } + if (download_unsort[0]) { + if (unlink(download_unsort)==-1) + debuga(__FILE__,__LINE__,_("Failed to delete \"%s\": %s\n"),download_unsort,strerror(errno)); + } +}