]> git.ipfire.org Git - thirdparty/sarg.git/blobdiff - log.c
Fixed regressions in creation and reading of a sarg parsed log (thanks to Joao Alves)
[thirdparty/sarg.git] / log.c
diff --git a/log.c b/log.c
index f45dc1c4071a02355caeb30d37dbe8d1c8c10353..841f3c9fa0c2614f23895158091682b8d31ebf52 100644 (file)
--- a/log.c
+++ b/log.c
@@ -100,6 +100,8 @@ int main(int argc,char *argv[])
    char bufz[MAXLEN];
    char bufy[MAXLEN];
    char tmp2[MAXLEN];
+   char start_hour[128];
+   char end_hour[128];
    enum InputLogFormat ilf;
    int ilf_count[ILF_Last];
    int  ch;
@@ -125,7 +127,7 @@ int main(int argc,char *argv[])
    unsigned long recs1=0UL;
    unsigned long recs2=0UL;
    int OutputNonZero = REPORT_EVERY_X_LINES ;
-   int download_flag;
+   int download_flag=0;
    char download_url[MAXLEN];
    char sz_Last_User[MAXLEN]="";
    struct getwordstruct gwarea;
@@ -200,7 +202,7 @@ int main(int argc,char *argv[])
    ShowSargInfo=1;
    ShowSargLogo=1;
    strcpy(ParsedOutputLog,"no");
-   strcpy(ParsedOutputLogCompress,"/bin/gzip");
+   strcpy(ParsedOutputLogCompress,"/bin/gzip -f");
    strcpy(DisplayedValues,"abbreviation");
    strcpy(HeaderFontSize,"9px");
    strcpy(TitleFontSize,"11px");
@@ -248,6 +250,8 @@ int main(int argc,char *argv[])
    DataFile[0]='\0';
    SquidGuardConf[0]='\0';
    DansGuardianConf[0]='\0';
+   start_hour[0]='\0';
+   end_hour[0]='\0';
 
    denied_count=0;
    download_count=0;
@@ -712,6 +716,7 @@ int main(int argc,char *argv[])
          from_stdin=0;
       }
       ilf=ILF_Unknown;
+      download_flag=0;
       // pre-Read the file only if I have to show stats
       if(ShowReadStatistics && !from_stdin) {
          rewind(fp_in);
@@ -738,9 +743,9 @@ int main(int argc,char *argv[])
 
             if(strncmp(bufz,"*** SARG Log ***",16) == 0) {
                getword_start(&gwarea,arqtt);
-               if (getword(val2,sizeof(val2),&gwarea,'-')<0 || getword(val2,sizeof(val2),&gwarea,'_')<0 ||
-                   getword(val3,sizeof(val3),&gwarea,'-')<0 || getword(val3,sizeof(val3),&gwarea,'_')<0) {
-                  printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
+               if (getword_skip(2000,&gwarea,'-')<0 || getword(val2,sizeof(val2),&gwarea,'_')<0 ||
+                   getword_skip(10,&gwarea,'-')<0 || getword(val3,sizeof(val3),&gwarea,'_')<0) {
+                  printf("SARG: The name of the file is invalid: %s\n",arq);
                   exit(1);
                }
                sprintf(period,"%s-%s",val2,val3);
@@ -750,10 +755,9 @@ int main(int argc,char *argv[])
             }
          }
 
-         if(strcmp(ParsedOutputLog, "no") != 0 && ilf!=ILF_Sarg) {
+         if(!fp_log && strcmp(ParsedOutputLog, "no") != 0 && ilf!=ILF_Sarg) {
             if(access(ParsedOutputLog,R_OK) != 0) {
-               sprintf(csort,"%s",ParsedOutputLog);
-               my_mkdir(csort);
+               my_mkdir(ParsedOutputLog);
             }
             sprintf(arq_log,"%s/sarg_temp.log",ParsedOutputLog);
             if((fp_log=MY_FOPEN(arq_log,"w"))==NULL) {
@@ -912,39 +916,39 @@ int main(int argc,char *argv[])
          }
          if (ilf==ILF_Sarg) {
             getword_start(&gwarea,bufz);
-            if (getword(data,sizeof(data),&gwarea,' ')<0){
+            if (getword(data,sizeof(data),&gwarea,'\t')<0){
                printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
                exit(1);
             }
-            if (getword(hora,sizeof(hora),&gwarea,' ')<0) {
+            if (getword(hora,sizeof(hora),&gwarea,'\t')<0) {
                printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
                exit(1);
             }
-            if (getword(user,sizeof(user),&gwarea,' ')<0) {
+            if (getword(user,sizeof(user),&gwarea,'\t')<0) {
                printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
                exit(1);
             }
-            if (getword(ip,sizeof(ip),&gwarea,' ')<0) {
+            if (getword(ip,sizeof(ip),&gwarea,'\t')<0) {
                printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
                exit(1);
             }
-            if (getword(url,sizeof(url),&gwarea,' ')<0){
+            if (getword(url,sizeof(url),&gwarea,'\t')<0){
                printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
                exit(1);
             }
-            if (getword(tam,sizeof(tam),&gwarea,' ')<0){
+            if (getword(tam,sizeof(tam),&gwarea,'\t')<0){
                printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
                exit(1);
             }
-            if (getword(code,sizeof(code),&gwarea,' ')<0){
+            if (getword(code,sizeof(code),&gwarea,'\t')<0){
                printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
                exit(1);
             }
-            if (getword(elap,sizeof(elap),&gwarea,' ')<0){
+            if (getword(elap,sizeof(elap),&gwarea,'\t')<0){
                printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
                exit(1);
             }
-            if (getword(smartfilter,sizeof(smartfilter),&gwarea,' ')<0){
+            if (getword(smartfilter,sizeof(smartfilter),&gwarea,'\0')<0){
                printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
                exit(1);
             }
@@ -1132,10 +1136,16 @@ int main(int argc,char *argv[])
          if(strstr(ReportType,"denied") != 0)
             strcpy(urly,url);
 
-         download_flag=is_download_suffix(url);
-         if (download_flag) {
-            strcpy(download_url,url);
-            download_count++;
+         if(ilf!=ILF_Sarg) {
+            /*
+            The full URL is not saved in sarg log. There is no point in testing the URL to detect
+            a downloaded file.
+            */
+            download_flag=is_download_suffix(url);
+            if (download_flag) {
+               strcpy(download_url,url);
+               download_count++;
+            }
          }
 
          // remove any protocol:// at the beginning of the URL
@@ -1192,15 +1202,15 @@ int main(int argc,char *argv[])
                exit(1);
             }
             getword_start(&gwarea,data);
-            if (getword_multisep(dia,sizeof(dia),&gwarea,'/')<0){
+            if (getword(dia,sizeof(dia),&gwarea,'/')<0){
                printf("SARG: Maybe you have a broken date in your %s file.\n",arq);
                exit(1);
             }
-            if (getword_multisep(mes,sizeof(mes),&gwarea,'/')<0){
+            if (getword(mes,sizeof(mes),&gwarea,'/')<0){
                printf("SARG: Maybe you have a broken date in your %s file.\n",arq);
                exit(1);
             }
-            if (getword_multisep(ano,sizeof(ano),&gwarea,'/')<0){
+            if (getword(ano,sizeof(ano),&gwarea,'/')<0){
                printf("SARG: Maybe you have a broken date in your %s file.\n",arq);
                exit(1);
             }
@@ -1213,18 +1223,31 @@ int main(int argc,char *argv[])
             idata=atoi(wdata);
          } else if (ilf==ILF_Sarg) {
             getword_start(&gwarea,data);
-            if (getword_multisep(mes,sizeof(mes),&gwarea,'/')<0){
-               printf("SARG: Maybe you have a broken date in your %s file.\n",arq);
-               exit(1);
-            }
-            if (getword_multisep(dia,sizeof(dia),&gwarea,'/')<0){
-               printf("SARG: Maybe you have a broken date in your %s file.\n",arq);
-               exit(1);
+            if(strcmp(df,"u") == 0) {
+               if (getword(mes,sizeof(mes),&gwarea,'/')<0){
+                  printf("SARG: Maybe you have a broken date in your %s file.\n",arq);
+                  exit(1);
+               }
+               if (getword(dia,sizeof(dia),&gwarea,'/')<0){
+                  printf("SARG: Maybe you have a broken date in your %s file.\n",arq);
+                  exit(1);
+               }
+            } else {
+               if (getword(dia,sizeof(dia),&gwarea,'/')<0){
+                  printf("SARG: Maybe you have a broken date in your %s file.\n",arq);
+                  exit(1);
+               }
+               if (getword(mes,sizeof(mes),&gwarea,'/')<0){
+                  printf("SARG: Maybe you have a broken date in your %s file.\n",arq);
+                  exit(1);
+               }
             }
-            if (getword_multisep(ano,sizeof(ano),&gwarea,0)<0){
+            if (getword(ano,sizeof(ano),&gwarea,0)<0){
                printf("SARG: Maybe you have a broken date in your %s file.\n",arq);
                exit(1);
             }
+            snprintf(wdata,9,"%s%s%s",ano,mes,dia);
+            idata=atoi(wdata);
          }
 
          if(debugm)
@@ -1351,8 +1374,7 @@ int main(int argc,char *argv[])
                }
                fputs (bufz, fp_Write_User);
 
-               if(strcmp(ParsedOutputLog, "no") != 0 && ilf!=ILF_Sarg)
-                  fputs(bufz,fp_log);
+               if(fp_log && ilf!=ILF_Sarg) fputs(bufz,fp_log);
 
                totregsg++;
 
@@ -1384,7 +1406,7 @@ int main(int argc,char *argv[])
                   totper++;
                   mindate=idata;
                   sprintf(period,"%s-",tbuf);
-                  sprintf(per_hour,"%s-",tbuf2);
+                  strcpy(start_hour,tbuf2);
                   if(date[0] != '\0')
                      fixper(tbuf, period, cduntil);
                   if(debugz){
@@ -1477,7 +1499,6 @@ int main(int argc,char *argv[])
 
    if(date[0] == '\0' && ilf_count[ILF_Sarg]==0) {
       strcat(period,tbuf);
-      strcat(per_hour,tbuf2);
    }
 
    if(debugz){
@@ -1497,34 +1518,36 @@ int main(int argc,char *argv[])
 
    if(fp_log != NULL) {
       fclose(fp_log);
+      strcpy(end_hour,tbuf2);
       getword_start(&gwarea,period);
-      if (getword_multisep(val2,sizeof(val2),&gwarea,'-')<0){
+      if (getword(val2,sizeof(val2),&gwarea,'-')<0){
          printf("SARG: Maybe you have a broken date range definition.\n");
          exit(1);
       }
-      if (getword_multisep(val1,sizeof(val1),&gwarea,'\0')<0){
+      if (getword(val1,sizeof(val1),&gwarea,'\0')<0){
          printf("SARG: Maybe you have a broken date range definition.\n");
          exit(1);
       }
-      getword_start(&gwarea,per_hour);
-      if (getword_multisep(val3,sizeof(val3),&gwarea,'-')<0){
-         printf("SARG: Maybe you have a broken date range definition.\n");
-         exit(1);
-      }
-      sprintf(val4,"%s/sarg-%s_%s-%s_%s.log",ParsedOutputLog,val2,val3,val1,gwarea.current);
-      rename(arq_log,val4);
-      strcpy(arq_log,val4);
-
-      if(strcmp(ParsedOutputLogCompress,"nocompress") != 0) {
-         sprintf(val1,"\"%s\" \"%s\"",ParsedOutputLogCompress,arq_log);
-         cstatus=system(val1);
-         if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
-            fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus));
-            fprintf(stderr, "SARG: command: %s\n",val1);
-            exit(1);
+      sprintf(val4,"%s/sarg-%s_%s-%s_%s.log",ParsedOutputLog,val2,start_hour,val1,end_hour);
+      if (rename(arq_log,val4)) {
+         fprintf(stderr,"SARG: failed to rename %s to %s - %s\n",arq_log,val4,strerror(errno));
+      } else {
+         strcpy(arq_log,val4);
+
+         if(strcmp(ParsedOutputLogCompress,"nocompress") != 0 && ParsedOutputLogCompress[0] != '\0') {
+            /*
+            No double quotes around ParsedOutputLogCompress because it may contain command line options. If double quotes are
+            necessary around the command name, put them in the configuration file.
+            */
+            sprintf(val1,"%s \"%s\"",ParsedOutputLogCompress,arq_log);
+            cstatus=system(val1);
+            if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
+               fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus));
+               fprintf(stderr, "SARG: command: %s\n",val1);
+               exit(1);
+            }
          }
       }
-
       if(debug)
          debuga("%s %s",text[123],arq_log);
    }