From: Frédéric Marchal Date: Thu, 5 Jul 2012 13:45:06 +0000 (+0200) Subject: Improvements in the denied report code X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6befdb6f05320721a133f54631dcd58cc37bd5cb;p=thirdparty%2Fsarg.git Improvements in the denied report code The sorted file name doesn't have to be global. We make sure the file handle is closed even if the caller doesn't close it by calling denied_close(). --- diff --git a/denied.c b/denied.c index 14e897d..d1e612a 100644 --- a/denied.c +++ b/denied.c @@ -30,8 +30,6 @@ //! 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. @@ -89,6 +87,7 @@ void denied_close(void) debuga(_("Write error in %s: %s\n"),denied_unsort,strerror(errno)); exit(EXIT_FAILURE); } + fp_denied=NULL; } } @@ -120,16 +119,17 @@ void gen_denied_report(void) char *buf; char *url; + char denied_sort[MAXLEN]; char report[MAXLEN]; char ip[MAXLEN]; char oip[MAXLEN]; char user[MAXLEN]; - char ouser[MAXLEN]; - char ouser2[MAXLEN]; + char ouser[MAXLEN]=""; + char ouser2[MAXLEN]=""; char data[15]; char hora[15]; char csort[4098]; - bool z=false; + bool z=false; int count=0; int day,month,year; int cstatus; @@ -139,10 +139,6 @@ void gen_denied_report(void) struct userinfostruct *uinfo; struct tm t; - ouser[0]='\0'; - ouser2[0]='\0'; - - sprintf(denied_sort,"%s/denied.int_log",tmp); if (!denied_exists) { if (unlink(denied_sort)==-1) { @@ -153,6 +149,10 @@ void gen_denied_report(void) return; } + if (snprintf(denied_sort,sizeof(denied_sort),"%s/denied.int_log",tmp)>=sizeof(denied_sort)) { + debuga(_("Temporary directory path too long to sort the denied accesses\n")); + exit(EXIT_FAILURE); + } 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); @@ -278,7 +278,6 @@ void gen_denied_report(void) 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; } @@ -288,9 +287,10 @@ Remove any temporary file left by the denied module. */ void denied_cleanup(void) { - if(denied_sort[0]) { - if (unlink(denied_sort)==-1) - debuga(_("Failed to delete %s: %s\n"),denied_sort,strerror(errno)); + if (fp_denied) + { + fclose(fp_denied); + fp_denied=NULL; } if(denied_unsort[0]) { if (unlink(denied_unsort)==-1)