]> git.ipfire.org Git - thirdparty/sarg.git/commitdiff
Improvements in the denied report code
authorFrédéric Marchal <fmarchal@users.sourceforge.net>
Thu, 5 Jul 2012 13:45:06 +0000 (15:45 +0200)
committerFrédéric Marchal <fmarchal@users.sourceforge.net>
Thu, 5 Jul 2012 13:45:06 +0000 (15:45 +0200)
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().

denied.c

index 14e897d5d6a935ef0e0acf609b99149535a9c168..d1e612a502a9e5f28ca5989cd62ee7ae9d39f0e5 100644 (file)
--- 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)