]> git.ipfire.org Git - thirdparty/sarg.git/blobdiff - dansguardian_log.c
Update the Russian translation
[thirdparty/sarg.git] / dansguardian_log.c
index 9f5ba8c1e2416ef87b897862e468a8119cce0dc4..1f678bbdc4dadfcfbef335db1f9b298f6d2d97ca 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * SARG Squid Analysis Report Generator      http://sarg.sourceforge.net
- *                                                            1998, 2010
+ *                                                            1998, 2015
  *
  * SARG donations:
  *      please look at http://sarg.sourceforge.net/donations.php
@@ -34,11 +34,11 @@ void dansguardian_log(void)
        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];
+       int year, mon, day;
+       int hour;
+       char minsec[15];
        char user[MAXLEN], code1[255], code2[255];
-       char ip[30];
-       char wdata[127];
+       char ip[45];
        char *url;
        char tmp6[MAXLEN];
        int  idata=0;
@@ -49,21 +49,21 @@ void dansguardian_log(void)
        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.unsort",tmp);
-       snprintf(guard_ou,sizeof(guard_ou),"%s/dansguardian.log",tmp);
+       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(_("Cannot open DansGuardian config file: %s\n"),DansGuardianConf);
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),DansGuardianConf,strerror(errno));
                exit(EXIT_FAILURE);
        }
 
        if((fp_guard=fopen(DansGuardianConf,"r"))==NULL) {
-               debuga(_("(dansguardian) Cannot open log file: %s\n"),DansGuardianConf);
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),DansGuardianConf,strerror(errno));
                exit(EXIT_FAILURE);
        }
 
-       if((fp_ou=MY_FOPEN(guard_in,"a"))==NULL) {
-               debuga(_("(dansguardian) Cannot open log file: %s\n"),guard_in);
+       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);
        }
 
@@ -74,18 +74,24 @@ void dansguardian_log(void)
                if(strstr(buf,"loglocation ") != 0) {
                        getword_start(&gwarea,buf);
                        if (getword_skip(MAXLEN,&gwarea,'\'')<0 || getword(loglocation,sizeof(loglocation),&gwarea,'\'')<0) {
-                               debuga(_("Maybe you have a broken record or garbage in your %s file\n"),DansGuardianConf);
+                               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(_("Reading DansGuardian log file: %s\n"),loglocation);
+               debuga(__FILE__,__LINE__,_("Reading DansGuardian log file \"%s\"\n"),loglocation);
 
        if((fp_in=MY_FOPEN(loglocation,"r"))==NULL) {
-               debuga(_("(dansguardian) Cannot open log file: %s\n"),loglocation);
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),loglocation,strerror(errno));
                exit(EXIT_FAILURE);
        }
 
@@ -93,54 +99,72 @@ void dansguardian_log(void)
                if(strstr(buf," *DENIED* ") == 0)
                        continue;
                getword_start(&gwarea,buf);
-               if (getword(year,sizeof(year),&gwarea,'.')<0 || getword(mon,sizeof(mon),&gwarea,'.')<0 ||
-                   getword(day,sizeof(day),&gwarea,' ')<0 || getword(hour,sizeof(hour),&gwarea,' ')<0 ||
-                   getword(user,sizeof(user),&gwarea,' ')<0 || getword(ip,sizeof(ip),&gwarea,' ')<0 ||
-                   getword_skip(MAXLEN,&gwarea,'/')<0 || getword_skip(MAXLEN,&gwarea,'/')<0) {
-                       debuga(_("Maybe you have a broken record or garbage in your %s file\n"),loglocation);
+               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(_("Maybe you have a broken url in your %s file\n"),loglocation);
+                       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(_("Maybe you have a broken record or garbage in your %s file\n"),loglocation);
+                       debuga(__FILE__,__LINE__,_("Invalid record in file \"%s\"\n"),loglocation);
                        exit(EXIT_FAILURE);
                }
-               sprintf(wdata,"%s%02d%s",year,atoi(mon),day);
-               idata = atoi(wdata);
+               idata = year*10000+mon*100+day;
 
                if(DansguardianFilterOutDate) {
-                       if(idata < dfrom && idata > duntil)
+                       if(idata < dfrom || idata > duntil)
                                continue;
                }
 
                if (strcmp(user,"-") == 0) {
                        strcpy(user,ip);
-                       bzero(ip, 30);
+                       ip[0]='\0';
                }
-               fprintf(fp_ou,"%s\t%d\t%s\t%s\t%s\t%s\t%s\n",user,idata,hour,ip,url,code1,code2);
+               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(fp_in) fclose(fp_in);
-       if(fp_guard) fclose(fp_guard);
-       if(fp_ou) fclose(fp_ou);
+       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(_("Sorting file: %s\n"),guard_ou);
+               debuga(__FILE__,__LINE__,_("Sorting file \"%s\"\n"),guard_ou);
 
-       snprintf(tmp6,sizeof(tmp6),"sort -k 1,1 -k 2,2 -k 4,4 \"%s\" -o \"%s\"",guard_in, 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(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
-               debuga(_("sort command: %s\n"),tmp6);
+               debuga(__FILE__,__LINE__,_("sort command return status %d\n"),WEXITSTATUS(cstatus));
+               debuga(__FILE__,__LINE__,_("sort command: %s\n"),tmp6);
                exit(EXIT_FAILURE);
        }
-       if (unlink(guard_in)) {
-               debuga(_("Cannot delete %s - %s\n"),guard_in,strerror(errno));
+       if (!KeepTempLog && unlink(guard_in)) {
+               debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),guard_in,strerror(errno));
                exit(EXIT_FAILURE);
        }
 }