X-Git-Url: http://git.ipfire.org/?a=blobdiff_plain;f=dansguardian_log.c;h=1f678bbdc4dadfcfbef335db1f9b298f6d2d97ca;hb=0c752ade6feb4ca27fae4a61a76e8442ce7a9a18;hp=d999a5c4da8a9977d269f9a00c861549a442d0aa;hpb=94ff9470def23029000c8dc1e0aa0eba7c88a919;p=thirdparty%2Fsarg.git diff --git a/dansguardian_log.c b/dansguardian_log.c index d999a5c..1f678bb 100644 --- a/dansguardian_log.c +++ b/dansguardian_log.c @@ -1,10 +1,11 @@ /* - * AUTHOR: Pedro Lineu Orso orso@penguintech.com.br - * 1998, 2005 * 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 @@ -24,132 +25,146 @@ */ #include "include/conf.h" +#include "include/defs.h" -int dansguardian_log() +void dansguardian_log(void) { + 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; - 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 x, y; - - if(strlen(DansGuardianConf) < 1) - return; - - bzero(day, 3); - bzero(mon, 4); - bzero(year, 5); - - if(strcmp(df,"e") == 0) { - strncpy(day,periodo,2); - strncpy(mon,periodo+2,3); - strncpy(year,periodo+5,4); - conv_month(mon); - sprintf(warea,"%s%s%s",year,mon,day); - dfrom=atoi(warea); - strncpy(day,periodo+10,2); - strncpy(mon,periodo+12,3); - strncpy(year,periodo+15,4); - conv_month(mon); - sprintf(warea,"%s%s%s",year,mon,day); - duntil=atoi(warea); - } else { - strncpy(day,periodo+7,2); - strncpy(mon,periodo+4,3); - strncpy(year,periodo,4); - conv_month(mon); - sprintf(warea,"%s%s%s",year,mon,day); - dfrom=atoi(warea); - strncpy(day,periodo+17,2); - strncpy(mon,periodo+14,3); - strncpy(year,periodo+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((fp_ou=fopen(guard_in,"a"))==NULL) { - 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) { - getword(loglocation,buf,'\''); - getword(loglocation,buf,'\''); - break; - } - } - - if(debug) { - strcpy(buf,text[7]); - getword(urly,buf,' '); - getword(href,buf,' '); - sprintf(msg,"%s DansGuardian %s: %s",urly,buf,loglocation); - debuga(msg); - } - - if((fp_in=fopen(loglocation,"r"))==NULL) { - 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; - getword(year,buf,'.'); - getword(mon,buf,'.'); - getword(day,buf,' '); - getword(hour,buf,' '); - getword(user,buf,' '); - getword(ip,buf,' '); - getword(url,buf,'/'); - getword(url,buf,'/'); - getword(url,buf,' '); - getword(code1,buf,' '); - getword(code1,buf,' '); - getword(code2,buf,' '); - sprintf(wdata,"%s%02d%s",year,atoi(mon),day); - idata = atoi(wdata); - - if(idata >= dfrom && idata <= duntil) { - if(strcmp(user,"-") == 0) - strcpy(user,ip); - sprintf(tmp6,"%s %d %s %s %s %s %s\n",user,idata,hour,ip,url,code1,code2); - fputs(tmp6, fp_ou); - dansguardian_count++; - } - } - fclose(fp_in); - fclose(fp_guard); - fclose(fp_ou); - sprintf(tmp6,"sort -k 1,1 -k 2,2 -k 4,4 '%s' -o '%s'",guard_in, guard_ou); - system(tmp6); - unlink(guard_in); - return; + 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); + } }