]> git.ipfire.org Git - thirdparty/sarg.git/blame - dansguardian_log.c
Add support to decompress xz files
[thirdparty/sarg.git] / dansguardian_log.c
CommitLineData
25697a35 1/*
94ff9470 2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
110ce984 3 * 1998, 2015
25697a35
GS
4 *
5 * SARG donations:
6 * please look at http://sarg.sourceforge.net/donations.php
1164c474
FM
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
25697a35
GS
9 * ---------------------------------------------------------------------
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
24 *
25 */
26
27#include "include/conf.h"
5f3cfd1d 28#include "include/defs.h"
25697a35 29
32e71fa4 30void dansguardian_log(void)
25697a35 31{
9bd92830
FM
32 FILE *fp_in = NULL, *fp_ou = NULL, *fp_guard = NULL;
33 char buf[MAXLEN];
34 char guard_in[MAXLEN];
35 char guard_ou[MAXLEN];
36 char loglocation[MAXLEN] = "/var/log/dansguardian/access.log";
bd8b7715 37 int year, mon, day;
2f1416e8
FM
38 int hour;
39 char minsec[15];
9bd92830 40 char user[MAXLEN], code1[255], code2[255];
473d6bd8 41 char ip[45];
9bd92830
FM
42 char *url;
43 char tmp6[MAXLEN];
44 int idata=0;
45 int cstatus;
46 int dfrom, duntil;
47 struct getwordstruct gwarea;
48
49 dfrom=(period.start.tm_year+1900)*10000+(period.start.tm_mon+1)*100+period.start.tm_mday;
50 duntil=(period.end.tm_year+1900)*10000+(period.end.tm_mon+1)*100+period.end.tm_mday;
51
c98d6a0f
FM
52 snprintf(guard_in,sizeof(guard_in),"%s/dansguardian.int_unsort",tmp);
53 snprintf(guard_ou,sizeof(guard_ou),"%s/dansguardian.int_log",tmp);
9bd92830
FM
54
55 if(access(DansGuardianConf, R_OK) != 0) {
af961877 56 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),DansGuardianConf,strerror(errno));
9bd92830
FM
57 exit(EXIT_FAILURE);
58 }
59
60 if((fp_guard=fopen(DansGuardianConf,"r"))==NULL) {
af961877 61 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),DansGuardianConf,strerror(errno));
9bd92830
FM
62 exit(EXIT_FAILURE);
63 }
64
c99c403c 65 if((fp_ou=MY_FOPEN(guard_in,"w"))==NULL) {
af961877 66 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),guard_in,strerror(errno));
9bd92830
FM
67 exit(EXIT_FAILURE);
68 }
69
70 while(fgets(buf,sizeof(buf),fp_guard)!=NULL) {
71 fixendofline(buf);
72 if(buf[0]=='#')
73 continue;
74 if(strstr(buf,"loglocation ") != 0) {
75 getword_start(&gwarea,buf);
76 if (getword_skip(MAXLEN,&gwarea,'\'')<0 || getword(loglocation,sizeof(loglocation),&gwarea,'\'')<0) {
af961877 77 debuga(__FILE__,__LINE__,_("Invalid record in file \"%s\"\n"),DansGuardianConf);
9bd92830
FM
78 exit(EXIT_FAILURE);
79 }
af961877 80 if (debug) debuga(__FILE__,__LINE__,_("Using the dansguardian log file \"%s\" found in your configuration file \"%s\"\n"),
9f93fec3 81 loglocation,DansGuardianConf);
9bd92830
FM
82 break;
83 }
84 }
204781f4 85 if (fclose(fp_guard)==EOF) {
af961877 86 debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),DansGuardianConf,strerror(errno));
204781f4
FM
87 exit(EXIT_FAILURE);
88 }
9bd92830
FM
89
90 if(debug)
af961877 91 debuga(__FILE__,__LINE__,_("Reading DansGuardian log file \"%s\"\n"),loglocation);
9bd92830
FM
92
93 if((fp_in=MY_FOPEN(loglocation,"r"))==NULL) {
af961877 94 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),loglocation,strerror(errno));
9bd92830
FM
95 exit(EXIT_FAILURE);
96 }
97
98 while(fgets(buf,sizeof(buf),fp_in) != NULL) {
99 if(strstr(buf," *DENIED* ") == 0)
100 continue;
101 getword_start(&gwarea,buf);
bd8b7715 102 if (getword_atoi(&year,&gwarea,'.')<0 || getword_atoi(&mon,&gwarea,'.')<0 ||
2f1416e8 103 getword_atoi(&day,&gwarea,' ')<0) {
af961877 104 debuga(__FILE__,__LINE__,_("Invalid date in file \"%s\"\n"),loglocation);
2f1416e8
FM
105 exit(EXIT_FAILURE);
106 }
107 if (getword_atoi(&hour,&gwarea,':')<0 || getword(minsec,sizeof(minsec),&gwarea,' ')<0) {
af961877 108 debuga(__FILE__,__LINE__,_("Invalid time in file \"%s\"\n"),loglocation);
2f1416e8
FM
109 exit(EXIT_FAILURE);
110 }
111 if (getword(user,sizeof(user),&gwarea,' ')<0) {
af961877 112 debuga(__FILE__,__LINE__,_("Invalid user in file \"%s\"\n"),loglocation);
2f1416e8
FM
113 exit(EXIT_FAILURE);
114 }
115 if (getword(ip,sizeof(ip),&gwarea,' ')<0) {
af961877 116 debuga(__FILE__,__LINE__,_("Invalid IP address in file \"%s\"\n"),loglocation);
2f1416e8
FM
117 exit(EXIT_FAILURE);
118 }
119 if (getword_skip(MAXLEN,&gwarea,'/')<0 || getword_skip(MAXLEN,&gwarea,'/')<0) {
af961877 120 debuga(__FILE__,__LINE__,_("Invalid record in file \"%s\"\n"),loglocation);
9bd92830
FM
121 exit(EXIT_FAILURE);
122 }
123 if (getword_ptr(buf,&url,&gwarea,' ')<0) {
af961877 124 debuga(__FILE__,__LINE__,_("Invalid url in file \"%s\"\n"),loglocation);
9bd92830
FM
125 exit(EXIT_FAILURE);
126 }
127 if (getword_skip(255,&gwarea,' ')<0 ||
007905af 128 getword(code1,sizeof(code1),&gwarea,' ')<0 || getword(code2,sizeof(code2),&gwarea,' ')<0) {
af961877 129 debuga(__FILE__,__LINE__,_("Invalid record in file \"%s\"\n"),loglocation);
9bd92830
FM
130 exit(EXIT_FAILURE);
131 }
bd8b7715 132 idata = year*10000+mon*100+day;
9bd92830
FM
133
134 if(DansguardianFilterOutDate) {
c1cb6a29 135 if(idata < dfrom || idata > duntil)
9bd92830
FM
136 continue;
137 }
138
139 if (strcmp(user,"-") == 0) {
140 strcpy(user,ip);
473d6bd8 141 ip[0]='\0';
9bd92830 142 }
2f1416e8 143 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);
9bd92830
FM
144 dansguardian_count++;
145 }
146
204781f4 147 if (fclose(fp_in)==EOF) {
af961877 148 debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),loglocation,strerror(errno));
204781f4
FM
149 exit(EXIT_FAILURE);
150 }
507460ae 151 if (fclose(fp_ou)==EOF) {
af961877 152 debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),guard_in,strerror(errno));
507460ae
FM
153 exit(EXIT_FAILURE);
154 }
9bd92830
FM
155
156 if(debug)
af961877 157 debuga(__FILE__,__LINE__,_("Sorting file \"%s\"\n"),guard_ou);
9bd92830 158
eb843369 159 snprintf(tmp6,sizeof(tmp6),"sort -t \"\t\" -k 1,1 -k 2,2 -k 4,4 \"%s\" -o \"%s\"",guard_in, guard_ou);
9bd92830
FM
160 cstatus=system(tmp6);
161 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
af961877
FM
162 debuga(__FILE__,__LINE__,_("sort command return status %d\n"),WEXITSTATUS(cstatus));
163 debuga(__FILE__,__LINE__,_("sort command: %s\n"),tmp6);
9bd92830
FM
164 exit(EXIT_FAILURE);
165 }
11767c6a 166 if (!KeepTempLog && unlink(guard_in)) {
af961877 167 debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),guard_in,strerror(errno));
08f9b029
FM
168 exit(EXIT_FAILURE);
169 }
25697a35 170}