dansguardian_log.c (sarg-2.3.11) | : | dansguardian_log.c (sarg-2.4.0) | ||
---|---|---|---|---|
/* | /* | |||
* SARG Squid Analysis Report Generator http://sarg.sourceforge.net | * SARG Squid Analysis Report Generator http://sarg.sourceforge.net | |||
* 1998, 2013 | * 1998, 2015 | |||
* | * | |||
* SARG donations: | * SARG donations: | |||
* please look at http://sarg.sourceforge.net/donations.php | * please look at http://sarg.sourceforge.net/donations.php | |||
* Support: | * Support: | |||
* http://sourceforge.net/projects/sarg/forums/forum/363374 | * http://sourceforge.net/projects/sarg/forums/forum/363374 | |||
* --------------------------------------------------------------------- | * --------------------------------------------------------------------- | |||
* | * | |||
* This program is free software; you can redistribute it and/or modify | * This program is free software; you can redistribute it and/or modify | |||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | |||
* the Free Software Foundation; either version 2 of the License, or | * the Free Software Foundation; either version 2 of the License, or | |||
skipping to change at line 30 | skipping to change at line 30 | |||
* | * | |||
* You should have received a copy of the GNU General Public License | * You should have received a copy of the GNU General Public License | |||
* along with this program; if not, write to the Free Software | * along with this program; if not, write to the Free Software | |||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. | |||
* | * | |||
*/ | */ | |||
#include "include/conf.h" | #include "include/conf.h" | |||
#include "include/defs.h" | #include "include/defs.h" | |||
void dansguardian_log(void) | void dansguardian_log(const struct ReadLogDataStruct *ReadFilter) | |||
{ | { | |||
FILE *fp_in = NULL, *fp_ou = NULL, *fp_guard = NULL; | FILE *fp_in = NULL, *fp_ou = NULL, *fp_guard = NULL; | |||
char buf[MAXLEN]; | char buf[MAXLEN]; | |||
char guard_in[MAXLEN]; | char guard_in[MAXLEN]; | |||
char guard_ou[MAXLEN]; | char guard_ou[MAXLEN]; | |||
char loglocation[MAXLEN] = "/var/log/dansguardian/access.log"; | char loglocation[MAXLEN] = "/var/log/dansguardian/access.log"; | |||
int year, mon, day; | int year, mon, day; | |||
int hour; | int hour,min,sec; | |||
char minsec[15]; | ||||
char user[MAXLEN], code1[255], code2[255]; | char user[MAXLEN], code1[255], code2[255]; | |||
char ip[45]; | char ip[45]; | |||
char *url; | char *url; | |||
char tmp6[MAXLEN]; | char tmp6[MAXLEN]; | |||
int idata=0; | int idata=0; | |||
int cstatus; | int cstatus; | |||
int dfrom, duntil; | int dfrom, duntil; | |||
struct getwordstruct gwarea; | struct getwordstruct gwarea; | |||
dfrom=(period.start.tm_year+1900)*10000+(period.start.tm_mon+1)*100+perio | getperiod_torange(&period,&dfrom,&duntil); | |||
d.start.tm_mday; | ||||
duntil=(period.end.tm_year+1900)*10000+(period.end.tm_mon+1)*100+period.e | ||||
nd.tm_mday; | ||||
snprintf(guard_in,sizeof(guard_in),"%s/dansguardian.int_unsort",tmp); | format_path(__FILE__, __LINE__, guard_in, sizeof(guard_in), "%s/dansguard | |||
snprintf(guard_ou,sizeof(guard_ou),"%s/dansguardian.int_log",tmp); | ian.int_unsort", tmp); | |||
format_path(__FILE__, __LINE__, guard_ou, sizeof(guard_ou), "%s/dansguard | ||||
ian.int_log", tmp); | ||||
if((fp_guard=fopen(DansGuardianConf,"r"))==NULL) { | if (access(DansGuardianConf, R_OK) != 0) { | |||
debugapos("dansguardian",_("Cannot open file \"%s\": %s\n"),DansG | debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),DansG | |||
uardianConf,strerror(errno)); | uardianConf,strerror(errno)); | |||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | |||
} | } | |||
if((fp_ou=MY_FOPEN(guard_in,"a"))==NULL) { | if ((fp_guard=fopen(DansGuardianConf,"r"))==NULL) { | |||
debugapos("dansguardian",_("Cannot open file \"%s\": %s\n"),guard | debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),DansG | |||
_in,strerror(errno)); | uardianConf,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); | exit(EXIT_FAILURE); | |||
} | } | |||
while(fgets(buf,sizeof(buf),fp_guard)!=NULL) { | while(fgets(buf,sizeof(buf),fp_guard)!=NULL) { | |||
fixendofline(buf); | fixendofline(buf); | |||
if(buf[0]=='#') | if (buf[0]=='#') | |||
continue; | continue; | |||
if(strstr(buf,"loglocation ") != 0) { | if (strstr(buf,"loglocation ") != 0) { | |||
getword_start(&gwarea,buf); | getword_start(&gwarea,buf); | |||
if (getword_skip(MAXLEN,&gwarea,'\'')<0 || getword(logloc ation,sizeof(loglocation),&gwarea,'\'')<0) { | if (getword_skip(MAXLEN,&gwarea,'\'')<0 || getword(logloc ation,sizeof(loglocation),&gwarea,'\'')<0) { | |||
debuga(_("Invalid record in file \"%s\"\n"),DansG uardianConf); | debuga(__FILE__,__LINE__,_("Invalid record in fil e \"%s\"\n"),DansGuardianConf); | |||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | |||
} | } | |||
if (debug) debuga(_("Using the dansguardian log file \"%s \" found in your configuration file \"%s\"\n"), | if (debug) debuga(__FILE__,__LINE__,_("Using the dansguar dian log file \"%s\" found in your configuration file \"%s\"\n"), | |||
loglocation,DansGuardianConf); | loglocation,DansGuardianConf); | |||
break; | break; | |||
} | } | |||
} | } | |||
if (fclose(fp_guard)==EOF) { | ||||
debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),DansGuar | ||||
dianConf,strerror(errno)); | ||||
exit(EXIT_FAILURE); | ||||
} | ||||
if(debug) | 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) { | if ((fp_in=MY_FOPEN(loglocation,"r"))==NULL) { | |||
debugapos("dansguardian",_("Cannot open file \"%s\": %s\n"),loglo | debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),loglo | |||
cation,strerror(errno)); | cation,strerror(errno)); | |||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | |||
} | } | |||
while(fgets(buf,sizeof(buf),fp_in) != NULL) { | while(fgets(buf,sizeof(buf),fp_in) != NULL) { | |||
if(strstr(buf," *DENIED* ") == 0) | if (strstr(buf," *DENIED* ") == 0) | |||
continue; | continue; | |||
getword_start(&gwarea,buf); | getword_start(&gwarea,buf); | |||
if (getword_atoi(&year,&gwarea,'.')<0 || getword_atoi(&mon,&gware a,'.')<0 || | if (getword_atoi(&year,&gwarea,'.')<0 || getword_atoi(&mon,&gware a,'.')<0 || | |||
getword_atoi(&day,&gwarea,' ')<0) { | getword_atoi(&day,&gwarea,' ')<0) { | |||
debuga(_("Invalid date in file \"%s\"\n"),loglocation); | debuga(__FILE__,__LINE__,_("Invalid date in file \"%s\"\n "),loglocation); | |||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | |||
} | } | |||
if (getword_atoi(&hour,&gwarea,':')<0 || getword(minsec,sizeof(mi | if (getword_atoi(&hour,&gwarea,':')<0 || getword_atoi(&min,&gware | |||
nsec),&gwarea,' ')<0) { | a,':')<0 || getword_atoi(&sec,&gwarea,' ')<0) { | |||
debuga(_("Invalid time in file \"%s\"\n"),loglocation); | debuga(__FILE__,__LINE__,_("Invalid time in file \"%s\"\n | |||
"),loglocation); | ||||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | |||
} | } | |||
if (getword(user,sizeof(user),&gwarea,' ')<0) { | if (getword(user,sizeof(user),&gwarea,' ')<0) { | |||
debuga(_("Invalid user in file \"%s\"\n"),loglocation); | debuga(__FILE__,__LINE__,_("Invalid user in file \"%s\"\n "),loglocation); | |||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | |||
} | } | |||
if (getword(ip,sizeof(ip),&gwarea,' ')<0) { | if (getword(ip,sizeof(ip),&gwarea,' ')<0) { | |||
debuga(_("Invalid IP address in file \"%s\"\n"),loglocati on); | debuga(__FILE__,__LINE__,_("Invalid IP address in file \" %s\"\n"),loglocation); | |||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | |||
} | } | |||
if (getword_skip(MAXLEN,&gwarea,'/')<0 || getword_skip(MAXLEN,&gw area,'/')<0) { | if (getword_skip(MAXLEN,&gwarea,'/')<0 || getword_skip(MAXLEN,&gw area,'/')<0) { | |||
debuga(_("Invalid record in file \"%s\"\n"),loglocation); | debuga(__FILE__,__LINE__,_("Invalid record in file \"%s\" \n"),loglocation); | |||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | |||
} | } | |||
if (getword_ptr(buf,&url,&gwarea,' ')<0) { | if (getword_ptr(buf,&url,&gwarea,' ')<0) { | |||
debuga(_("Invalid url in file \"%s\"\n"),loglocation); | debuga(__FILE__,__LINE__,_("Invalid url in file \"%s\"\n" ),loglocation); | |||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | |||
} | } | |||
if (getword_skip(255,&gwarea,' ')<0 || | if (getword_skip(255,&gwarea,' ')<0 || | |||
getword(code1,sizeof(code1),&gwarea,' ')<0 || getword(code2,s izeof(code2),&gwarea,' ')<0) { | getword(code1,sizeof(code1),&gwarea,' ')<0 || getword(code2,s izeof(code2),&gwarea,' ')<0) { | |||
debuga(_("Invalid record in file \"%s\"\n"),loglocation); | debuga(__FILE__,__LINE__,_("Invalid record in file \"%s\" \n"),loglocation); | |||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | |||
} | } | |||
idata = year*10000+mon*100+day; | idata = year*10000+mon*100+day; | |||
if(DansguardianFilterOutDate) { | if (DansguardianFilterOutDate) | |||
if(idata < dfrom || idata > duntil) | { | |||
if (idata < dfrom || idata > duntil) | ||||
continue; | continue; | |||
if (ReadFilter->StartTime>=0 || ReadFilter->EndTime>=0) | ||||
{ | ||||
int hmr=hour*100+min; | ||||
if (hmr<ReadFilter->StartTime || hmr>=ReadFilter- | ||||
>EndTime) | ||||
continue; | ||||
} | ||||
} | } | |||
if (strcmp(user,"-") == 0) { | if (strcmp(user,"-") == 0) { | |||
strcpy(user,ip); | strcpy(user,ip); | |||
ip[0]='\0'; | 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); | fprintf(fp_ou,"%s\t%d\t%02d:%02d:%02d\t%s\t%s\t%s\t%s\n",user,ida ta,hour,min,sec,ip,url,code1,code2); | |||
dansguardian_count++; | dansguardian_count++; | |||
} | } | |||
if(fp_in) fclose(fp_in); | if (fclose(fp_in)==EOF) { | |||
if(fp_guard) fclose(fp_guard); | debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),loglocat | |||
if(fp_ou) fclose(fp_ou); | ion,strerror(errno)); | |||
exit(EXIT_FAILURE); | ||||
} | ||||
if (fclose(fp_ou)==EOF) { | ||||
debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),guard_i | ||||
n,strerror(errno)); | ||||
exit(EXIT_FAILURE); | ||||
} | ||||
if(debug) | if (debug) | |||
debuga(_("Sorting file \"%s\"\n"),guard_ou); | 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 | if (snprintf(tmp6, sizeof(tmp6), "sort -t \"\t\" -k 1,1 -k 2,2 -k 4,4 \"% | |||
\"%s\"",guard_in, guard_ou); | s\" -o \"%s\"", guard_in, guard_ou) >= sizeof(tmp6)) { | |||
debuga(__FILE__,__LINE__,_("Sort command too long when sorting fi | ||||
le \"%s\" to \"%s\"\n"), guard_in, guard_ou); | ||||
debuga_more("sort -t \"\t\" -k 1,1 -k 2,2 -k 4,4 \"%s\" -o \"%s\" | ||||
", guard_in, guard_ou); | ||||
exit(EXIT_FAILURE); | ||||
} | ||||
cstatus=system(tmp6); | cstatus=system(tmp6); | |||
if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |||
debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)) | debuga(__FILE__,__LINE__,_("sort command return status %d\n"),WEX | |||
; | ITSTATUS(cstatus)); | |||
debuga(_("sort command: %s\n"),tmp6); | debuga(__FILE__,__LINE__,_("sort command: %s\n"),tmp6); | |||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | |||
} | } | |||
if (!KeepTempLog && unlink(guard_in)) { | if (!KeepTempLog && unlink(guard_in)) { | |||
debuga(_("Cannot delete \"%s\": %s\n"),guard_in,strerror(errno)); | debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),guard_in ,strerror(errno)); | |||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | |||
} | } | |||
} | } | |||
End of changes. 30 change blocks. | ||||
49 lines changed or deleted | 81 lines changed or added |