#include "include/conf.h"
#include "include/defs.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]="";
+
+/*!
+Open a file to store the denied accesses.
+
+\return The file handle or NULL if no file is necessary.
+*/
+FILE *denied_open(void)
+{
+ FILE *fp_denied;
+
+ if((ReportType & REPORT_TYPE_DENIED) == 0) return(NULL);
+
+ 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);
+}
+
static void show_ignored_denied(FILE *fp_ou,int count)
{
char ignored[80];
fprintf(fp_ou,"<tr><td class=\"data\"></td><td class=\"data\"></td><td class=\"data\"></td><td class=\"data2 more\">%s</td></tr>\n",ignored);
}
+/*!
+Generate a report containing the denied accesses.
+*/
void gen_denied_report(void)
{
FILE *fp_in = NULL, *fp_ou = NULL;
char *buf;
char *url;
- char denied_in[MAXLEN];
char report[MAXLEN];
char ip[MAXLEN];
char oip[MAXLEN];
char ouser2[MAXLEN];
char data[15];
char hora[15];
+ char csort[4098];
bool z=false;
int count=0;
int day,month,year;
+ int cstatus;
bool new_user;
struct getwordstruct gwarea;
longline line;
ouser[0]='\0';
ouser2[0]='\0';
- sprintf(denied_in,"%s/denied.int_log",tmp);
+ sprintf(denied_sort,"%s/denied.int_log",tmp);
if (!denied_count) {
- unlink(denied_in);
+ unlink(denied_sort);
if (debugz) debugaz(_("Denied report not produced because it is empty\n"));
return;
}
+ if (snprintf(csort,sizeof(csort),"sort -T \"%s\" -t \"\t\" -k 3,3 -k 5,5 -o \"%s\" \"%s\"",tmp,denied_sort,denied_unsort)>=sizeof(csort)) {
+ debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),denied_unsort,denied_sort);
+ 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 (unlink(denied_unsort)) {
+ debuga(_("Cannot delete %s - %s\n"),denied_unsort,strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ denied_unsort[0]='\0';
+
sprintf(report,"%s/denied.html",outdirname);
- if((fp_in=MY_FOPEN(denied_in,"r"))==NULL) {
- debuga(_("(denied) Cannot open log file %s\n"),denied_in);
+ if((fp_in=MY_FOPEN(denied_sort,"r"))==NULL) {
+ debuga(_("(denied) Cannot open log file %s\n"),denied_sort);
exit(EXIT_FAILURE);
}
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"),denied_in);
+ debuga(_("There is a broken record or garbage in file %s\n"),denied_sort);
exit(EXIT_FAILURE);
}
if (getword_ptr(buf,&url,&gwarea,'\t')<0) {
- debuga(_("There is a broken url in file %s\n"),denied_in);
+ debuga(_("There is a broken url in file %s\n"),denied_sort);
exit(EXIT_FAILURE);
}
if (sscanf(data,"%d/%d/%d",&day,&month,&year)!=3) continue;
uinfo=userinfo_find_from_id(user);
if (!uinfo) {
- debuga(_("Unknown user ID %s in file %s\n"),user,denied_in);
+ debuga(_("Unknown user ID %s in file %s\n"),user,denied_sort);
exit(EXIT_FAILURE);
}
if (fclose(fp_ou)<0)
debuga(_("Failed to close file %s - %s\n"),report,strerror(errno));
- if (unlink(denied_in)==-1)
- debuga(_("Failed to delete the file \"%s\" after processing it - %s\n"),denied_in,strerror(errno));
+ if (unlink(denied_sort)==-1)
+ debuga(_("Failed to delete the file \"%s\" after processing it - %s\n"),denied_sort,strerror(errno));
+ denied_sort[0]='\0';
return;
}
+
+/*!
+Remove any temporary file left by the denied module.
+*/
+void denied_cleanup(void)
+{
+ if(denied_sort[0]) {
+ unlink(denied_sort);
+ }
+ if(denied_unsort[0]) {
+ unlink(denied_unsort);
+ }
+}
numlist weekdays = { { 0, 1, 2, 3, 4, 5, 6 }, 7 };
numlist hours = { { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, 24 };
-extern char denied_sort[MAXLEN];
extern char *userfile;
/*!
char tmp3[MAXLEN];
char sz_Download_Unsort[20000];
char start_hour[128];
- char denied_unsort[MAXLEN];
char authfail_unsort[MAXLEN];
- char csort[MAXLEN];
char download_url[MAXLEN];
char smartfilter[MAXLEN];
const char *arq;
first_user_file=NULL;
snprintf(sz_Download_Unsort,sizeof(sz_Download_Unsort),"%s/download.int_unsort", tmp);
- snprintf(denied_unsort,sizeof(denied_unsort),"%s/denied.int_unsort",tmp);
snprintf(authfail_unsort,sizeof(authfail_unsort),"%s/authfail.int_unsort",tmp);
if(DataFile[0]=='\0') {
if((ReportType & REPORT_TYPE_DENIED) != 0) {
- if((fp_denied=MY_FOPEN(denied_unsort,"w"))==NULL) {
- debuga(_("(log) Cannot open file: %s - %s\n"),denied_unsort,strerror(errno));
- exit(EXIT_FAILURE);
- }
+ fp_denied=denied_open();
}
if((ReportType & REPORT_TYPE_DENIED) != 0 || (ReportType & REPORT_TYPE_AUTH_FAILURES) != 0) {
fprintf(fp_Download_Unsort,"%s\t%s\t%s\t%s\t%s\n",dia,hora,user,ip,download_url);
}
- if((ReportType & REPORT_TYPE_DENIED) != 0) {
- if(fp_denied && strstr(code,"DENIED/403") != 0) {
- fprintf(fp_denied, "%s\t%s\t%s\t%s\t%s\n",dia,hora,user,ip,full_url);
- denied_count++;
- }
+ if(fp_denied && strstr(code,"DENIED/403") != 0) {
+ fprintf(fp_denied, "%s\t%s\t%s\t%s\t%s\n",dia,hora,user,ip,full_url);
+ denied_count++;
}
if((ReportType & REPORT_TYPE_AUTH_FAILURES) != 0) {
if(fp_authfail && (strstr(code,"DENIED/401") != 0 || strstr(code,"DENIED/407") != 0)) {
debugaz(_("period=%s\n"),period.text);
}
- if(DataFile[0] == '\0' && (ReportType & REPORT_TYPE_DENIED) != 0) {
- if (snprintf(csort,sizeof(csort),"sort -T \"%s\" -t \"\t\" -k 3,3 -k 5,5 -o \"%s\" \"%s\"",tmp,denied_sort,denied_unsort)>=sizeof(csort)) {
- debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),denied_unsort,denied_sort);
- 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 (unlink(denied_unsort)) {
- debuga(_("Cannot delete %s - %s\n"),denied_unsort,strerror(errno));
- exit(EXIT_FAILURE);
- }
- }
-
return((totregsg!=0) ? 1 : 0);
}