*/
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.
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);
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';
}
}
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;
}
// 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);
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));
+ }
+}
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];
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;
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) {
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;
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){
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;
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"));