From: Frédéric Marchal Date: Tue, 3 Jul 2012 09:52:16 +0000 (+0200) Subject: Reduce the dependency of the main code to the denied report X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=60b48ae5ae36366f0c94fb20f0fafd0761e39d68;p=thirdparty%2Fsarg.git Reduce the dependency of the main code to the denied report Part of the code producing the denied report is changed to become a module and centralize every aspect of the denied report generation. --- diff --git a/denied.c b/denied.c index 8b76517..2af2361 100644 --- a/denied.c +++ b/denied.c @@ -27,6 +27,30 @@ #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]; @@ -35,13 +59,15 @@ static void show_ignored_denied(FILE *fp_ou,int count) fprintf(fp_ou,"%s\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]; @@ -50,9 +76,11 @@ void gen_denied_report(void) 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; @@ -62,17 +90,33 @@ void gen_denied_report(void) 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); } @@ -100,11 +144,11 @@ void gen_denied_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"),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; @@ -113,7 +157,7 @@ void gen_denied_report(void) 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); } @@ -176,8 +220,22 @@ void gen_denied_report(void) 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); + } +} diff --git a/include/defs.h b/include/defs.h index 8025388..25838e5 100755 --- a/include/defs.h +++ b/include/defs.h @@ -137,7 +137,9 @@ void data_file(char *tmp); FILE *decomp(const char *arq, bool *pipe); // denied.c +FILE *denied_open(void); void gen_denied_report(void); +void denied_cleanup(void); // download.c void download_report(void); diff --git a/log.c b/log.c index 6d0a9c0..aec6d89 100644 --- a/log.c +++ b/log.c @@ -34,7 +34,6 @@ //! The log file filtering. struct ReadLogDataStruct ReadFilter; -char denied_sort[MAXLEN]; //! The list of the system users. /*@null@*/char *userfile=NULL; @@ -557,7 +556,6 @@ int main(int argc,char *argv[]) unlinkdir(tmp,1); } my_mkdir(tmp); - snprintf(denied_sort,sizeof(denied_sort),"%s/denied.int_log",tmp); if(debug) { debuga(_("Parameters:\n")); @@ -702,9 +700,7 @@ int main(int argc,char *argv[]) else gerarel(); - if((ReportType & REPORT_TYPE_DENIED) != 0) { - unlink(denied_sort); - } + denied_cleanup(); if(strcmp(tmp,"/tmp") != 0) { unlinkdir(tmp,0); diff --git a/readlog.c b/readlog.c index 0253f98..8b7fb96 100644 --- a/readlog.c +++ b/readlog.c @@ -41,7 +41,6 @@ struct userfilestruct 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; /*! @@ -95,9 +94,7 @@ int ReadLogFile(struct ReadLogDataStruct *Filter) 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; @@ -149,15 +146,11 @@ int ReadLogFile(struct ReadLogDataStruct *Filter) 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) { @@ -902,11 +895,9 @@ int ReadLogFile(struct ReadLogDataStruct *Filter) 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)) { @@ -1030,22 +1021,5 @@ int ReadLogFile(struct ReadLogDataStruct *Filter) 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); }