#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)
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;
}
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));
}
}
char HostAliasFile[512];
int idate;
-int denied_count;
int download_count;
int authfail_count;
int dansguardian_count;
\brief Declaration of the structures and functions.
*/
+#include "readlog.h"
+
struct getwordstruct
{
const char *current;
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);
hm_str[0]='\0';
HostAliasFile[0]='\0';
- denied_count=0;
download_count=0;
authfail_count=0;
dansguardian_count=0;
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;
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) {
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);
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);
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 ((ReportType & REPORT_TYPE_DENIED) != 0 && denied_count && !Privacy) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"denied.html\">%s</a></td></tr>\n",_("Denied accesses"));
+ if (is_denied()) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"denied.html\">%s</a></td></tr>\n",_("Denied accesses"));
if ((ReportType & REPORT_TYPE_AUTH_FAILURES) != 0 && authfail_count && !Privacy) 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"));
if(UserAgentLog[0] != '\0' && useragent_count) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"useragent.html\">%s</a></td></tr>\n",_("Useragent"));