]> git.ipfire.org Git - thirdparty/sarg.git/blobdiff - dansguardian_log.c
Add support to decompress xz files
[thirdparty/sarg.git] / dansguardian_log.c
index e8876df96a4f2a1b4fd5c7b6056c3fc6f6d4fbf1..1f678bbdc4dadfcfbef335db1f9b298f6d2d97ca 100644 (file)
@@ -1,10 +1,11 @@
 /*
- * AUTHOR: Pedro Lineu Orso                         pedro.orso@gmail.com
- *                                                            1998, 2008
  * SARG Squid Analysis Report Generator      http://sarg.sourceforge.net
+ *                                                            1998, 2015
  *
  * SARG donations:
  *      please look at http://sarg.sourceforge.net/donations.php
+ * Support:
+ *     http://sourceforge.net/projects/sarg/forums/forum/363374
  * ---------------------------------------------------------------------
  *
  *  This program is free software; you can redistribute it and/or modify
  */
 
 #include "include/conf.h"
+#include "include/defs.h"
 
 void dansguardian_log(void)
 {
-
-   FILE *fp_in = NULL, *fp_ou = NULL, *fp_guard = NULL;
-   char guard_in[MAXLEN];
-   char guard_ou[MAXLEN];
-   char loglocation[MAXLEN] = "/var/log/dansguardian/access.log";
-   char year[10], mon[10], day[10];
-   char hour[15];
-   char user[MAXLEN], code1[255], code2[255];
-   char ip[30];
-   char wdata[127];
-   int  idata=0;
-   int cstatus;
-
-   bzero(day, 3);
-   bzero(mon, 4);
-   bzero(year, 5);
-
-   if(strcmp(df,"e") == 0) {
-      strncpy(day,period,2);
-      strncpy(mon,period+2,3);
-      strncpy(year,period+5,4);
-      conv_month(mon);
-      sprintf(warea,"%s%s%s",year,mon,day);
-      dfrom=atoi(warea);
-      strncpy(day,period+10,2);
-      strncpy(mon,period+12,3);
-      strncpy(year,period+15,4);
-      conv_month(mon);
-      sprintf(warea,"%s%s%s",year,mon,day);
-      duntil=atoi(warea);
-   } else {
-      strncpy(day,period+7,2);
-      strncpy(mon,period+4,3);
-      strncpy(year,period,4);
-      conv_month(mon);
-      sprintf(warea,"%s%s%s",year,mon,day);
-      dfrom=atoi(warea);
-      strncpy(day,period+17,2);
-      strncpy(mon,period+14,3);
-      strncpy(year,period+10,4);
-      conv_month(mon);
-      sprintf(warea,"%s%s%s",year,mon,day);
-      duntil=atoi(warea);
-   }
-
-   sprintf(guard_in,"%s/dansguardian.unsort",tmp);
-   sprintf(guard_ou,"%s/dansguardian.log",tmp);
-
-   if(access(DansGuardianConf, R_OK) != 0) {
-      sprintf(msg,"Cannot open DansGuardian config file: %s",DansGuardianConf);
-      debuga(msg);
-      exit(1);
-   }
-
-   if((fp_guard=fopen(DansGuardianConf,"r"))==NULL) {
-      fprintf(stderr, "SARG: (dansguardian) %s: %s\n",text[8],DansGuardianConf);
-      exit(1);
-   }
-
-#if defined(HAVE_FOPEN64)
-   if((fp_ou=fopen64(guard_in,"a"))==NULL) {
-#else
-   if((fp_ou=fopen(guard_in,"a"))==NULL) {
-#endif
-      fprintf(stderr, "SARG: (dansguardian) %s: %s\n",text[8],guard_in);
-      exit(1);
-   }
-
-   while(fgets(buf,sizeof(buf),fp_guard)!=NULL) {
-      if(strstr(buf,"\n") != 0)
-         buf[strlen(buf)-1]='\0';
-      if(strncmp(buf,"#",1) == 0)
-         continue;
-      if(strstr(buf,"loglocation ") != 0) {
-         if (getword(loglocation,sizeof(loglocation),buf,'\'')<0 || getword(loglocation,sizeof(loglocation),buf,'\'')<0) {
-            printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",DansGuardianConf);
-            exit(1);
-         }
-         break;
-      }
-   }
-
-   if(debug) {
-      strcpy(buf,text[7]);
-      if (getword(urly,sizeof(urly),buf,' ')<0 || getword(href,sizeof(href),buf,' ')<0) {
-         printf("SARG: Maybe you have a broken record or garbage in your %s entry.\n",text[7]);
-         exit(1);
-      }
-      sprintf(msg,"%s DansGuardian %s: %s",urly,buf,loglocation);
-      debuga(msg);
-   }
-   
-#if defined(HAVE_FOPEN64)
-   if((fp_in=fopen64(loglocation,"r"))==NULL) {
-#else
-   if((fp_in=fopen(loglocation,"r"))==NULL) {
-#endif
-      fprintf(stderr, "SARG: (dansguardian) %s: %s\n",text[8],loglocation);
-      exit(1);
-   }
-   while(fgets(buf,sizeof(buf),fp_in) != NULL) {
-      if(strstr(buf," *DENIED* ") == 0)
-         continue;
-      if (getword(year,sizeof(year),buf,'.')<0 || getword(mon,sizeof(mon),buf,'.')<0 ||
-          getword(day,sizeof(day),buf,' ')<0 || getword(hour,sizeof(hour),buf,' ')<0 ||
-          getword(user,sizeof(user),buf,' ')<0 || getword(ip,sizeof(ip),buf,' ')<0 ||
-          getword(url,sizeof(url),buf,'/')<0 || getword(url,sizeof(url),buf,'/')<0 ||
-          getword(url,sizeof(url),buf,' ')<0 || getword(code1,sizeof(code1),buf,' ')<0 ||
-          getword(code1,sizeof(code1),buf,' ')<0 || getword(code2,sizeof(code2),buf,' ')<0) {
-         printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",loglocation);
-         exit(1);
-      }
-      sprintf(wdata,"%s%02d%s",year,atoi(mon),day);
-      idata = atoi(wdata);
-
-      if(strcmp(DansguardianIgnoreDate,"on") == 0) {
-         if(idata < dfrom && idata > duntil)
-            continue;
-      }
-
-      if (strcmp(user,"-") == 0) {
-         strcpy(user,ip);
-         bzero(ip, 30);
-      }
-      fprintf(fp_ou,"%s %d %s %s %s %s %s\n",user,idata,hour,ip,url,code1,code2);
-      dansguardian_count++;
-   }
-
-   if(fp_in) fclose(fp_in);
-   if(fp_guard) fclose(fp_guard);
-   if(fp_ou) fclose(fp_ou);
-
-   if(debug) {
-      sprintf(msg,"%s: %s",text[54],guard_ou);
-      debuga(msg);
-   }
-
-   sprintf(tmp6,"sort -k 1,1 -k 2,2 -k 4,4 '%s' -o '%s'",guard_in, guard_ou);
-   cstatus=system(tmp6);
-   if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
-      fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
-      fprintf(stderr, "SARG: sort command: %s\n",tmp6);
-      exit(1);
-   }
-   unlink(guard_in);
+       FILE *fp_in = NULL, *fp_ou = NULL, *fp_guard = NULL;
+       char buf[MAXLEN];
+       char guard_in[MAXLEN];
+       char guard_ou[MAXLEN];
+       char loglocation[MAXLEN] = "/var/log/dansguardian/access.log";
+       int year, mon, day;
+       int hour;
+       char minsec[15];
+       char user[MAXLEN], code1[255], code2[255];
+       char ip[45];
+       char *url;
+       char tmp6[MAXLEN];
+       int  idata=0;
+       int cstatus;
+       int dfrom, duntil;
+       struct getwordstruct gwarea;
+
+       dfrom=(period.start.tm_year+1900)*10000+(period.start.tm_mon+1)*100+period.start.tm_mday;
+       duntil=(period.end.tm_year+1900)*10000+(period.end.tm_mon+1)*100+period.end.tm_mday;
+
+       snprintf(guard_in,sizeof(guard_in),"%s/dansguardian.int_unsort",tmp);
+       snprintf(guard_ou,sizeof(guard_ou),"%s/dansguardian.int_log",tmp);
+
+       if(access(DansGuardianConf, R_OK) != 0) {
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),DansGuardianConf,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       if((fp_guard=fopen(DansGuardianConf,"r"))==NULL) {
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),DansGuardianConf,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       if((fp_ou=MY_FOPEN(guard_in,"w"))==NULL) {
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),guard_in,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       while(fgets(buf,sizeof(buf),fp_guard)!=NULL) {
+               fixendofline(buf);
+               if(buf[0]=='#')
+                       continue;
+               if(strstr(buf,"loglocation ") != 0) {
+                       getword_start(&gwarea,buf);
+                       if (getword_skip(MAXLEN,&gwarea,'\'')<0 || getword(loglocation,sizeof(loglocation),&gwarea,'\'')<0) {
+                               debuga(__FILE__,__LINE__,_("Invalid record in file \"%s\"\n"),DansGuardianConf);
+                               exit(EXIT_FAILURE);
+                       }
+                       if (debug) debuga(__FILE__,__LINE__,_("Using the dansguardian log file \"%s\" found in your configuration file \"%s\"\n"),
+                               loglocation,DansGuardianConf);
+                       break;
+               }
+       }
+       if (fclose(fp_guard)==EOF) {
+               debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),DansGuardianConf,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       if(debug)
+               debuga(__FILE__,__LINE__,_("Reading DansGuardian log file \"%s\"\n"),loglocation);
+
+       if((fp_in=MY_FOPEN(loglocation,"r"))==NULL) {
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),loglocation,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       while(fgets(buf,sizeof(buf),fp_in) != NULL) {
+               if(strstr(buf," *DENIED* ") == 0)
+                       continue;
+               getword_start(&gwarea,buf);
+               if (getword_atoi(&year,&gwarea,'.')<0 || getword_atoi(&mon,&gwarea,'.')<0 ||
+                   getword_atoi(&day,&gwarea,' ')<0) {
+                       debuga(__FILE__,__LINE__,_("Invalid date in file \"%s\"\n"),loglocation);
+                       exit(EXIT_FAILURE);
+               }
+               if (getword_atoi(&hour,&gwarea,':')<0 || getword(minsec,sizeof(minsec),&gwarea,' ')<0) {
+                       debuga(__FILE__,__LINE__,_("Invalid time in file \"%s\"\n"),loglocation);
+                       exit(EXIT_FAILURE);
+               }
+               if (getword(user,sizeof(user),&gwarea,' ')<0) {
+                       debuga(__FILE__,__LINE__,_("Invalid user in file \"%s\"\n"),loglocation);
+                       exit(EXIT_FAILURE);
+               }
+               if (getword(ip,sizeof(ip),&gwarea,' ')<0) {
+                       debuga(__FILE__,__LINE__,_("Invalid IP address in file \"%s\"\n"),loglocation);
+                       exit(EXIT_FAILURE);
+               }
+               if (getword_skip(MAXLEN,&gwarea,'/')<0 || getword_skip(MAXLEN,&gwarea,'/')<0) {
+                       debuga(__FILE__,__LINE__,_("Invalid record in file \"%s\"\n"),loglocation);
+                       exit(EXIT_FAILURE);
+               }
+               if (getword_ptr(buf,&url,&gwarea,' ')<0) {
+                       debuga(__FILE__,__LINE__,_("Invalid url in file \"%s\"\n"),loglocation);
+                       exit(EXIT_FAILURE);
+               }
+               if (getword_skip(255,&gwarea,' ')<0 ||
+                   getword(code1,sizeof(code1),&gwarea,' ')<0 || getword(code2,sizeof(code2),&gwarea,' ')<0) {
+                       debuga(__FILE__,__LINE__,_("Invalid record in file \"%s\"\n"),loglocation);
+                       exit(EXIT_FAILURE);
+               }
+               idata = year*10000+mon*100+day;
+
+               if(DansguardianFilterOutDate) {
+                       if(idata < dfrom || idata > duntil)
+                               continue;
+               }
+
+               if (strcmp(user,"-") == 0) {
+                       strcpy(user,ip);
+                       ip[0]='\0';
+               }
+               fprintf(fp_ou,"%s\t%d\t%02d:%s\t%s\t%s\t%s\t%s\n",user,idata,hour,minsec,ip,url,code1,code2);
+               dansguardian_count++;
+       }
+
+       if (fclose(fp_in)==EOF) {
+               debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),loglocation,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+       if (fclose(fp_ou)==EOF) {
+               debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),guard_in,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       if(debug)
+               debuga(__FILE__,__LINE__,_("Sorting file \"%s\"\n"),guard_ou);
+
+       snprintf(tmp6,sizeof(tmp6),"sort -t \"\t\" -k 1,1 -k 2,2 -k 4,4 \"%s\" -o \"%s\"",guard_in, guard_ou);
+       cstatus=system(tmp6);
+       if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
+               debuga(__FILE__,__LINE__,_("sort command return status %d\n"),WEXITSTATUS(cstatus));
+               debuga(__FILE__,__LINE__,_("sort command: %s\n"),tmp6);
+               exit(EXIT_FAILURE);
+       }
+       if (!KeepTempLog && unlink(guard_in)) {
+               debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),guard_in,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
 }