]> git.ipfire.org Git - thirdparty/sarg.git/blame - dansguardian_log.c
Fix the creation of the datafile
[thirdparty/sarg.git] / dansguardian_log.c
CommitLineData
25697a35 1/*
94ff9470 2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
1164c474 3 * 1998, 2010
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
GS
31{
32
33 FILE *fp_in = NULL, *fp_ou = NULL, *fp_guard = NULL;
34 char guard_in[MAXLEN];
35 char guard_ou[MAXLEN];
36 char loglocation[MAXLEN] = "/var/log/dansguardian/access.log";
37 char year[10], mon[10], day[10];
38 char hour[15];
39 char user[MAXLEN], code1[255], code2[255];
40 char ip[30];
41 char wdata[127];
e5b2c6f0 42 char *url;
2240dcea 43 char tmp6[MAXLEN];
25697a35 44 int idata=0;
456d78a5 45 int cstatus;
9c7c6346 46 struct getwordstruct gwarea;
25697a35 47
25697a35
GS
48 bzero(day, 3);
49 bzero(mon, 4);
50 bzero(year, 5);
51
52 if(strcmp(df,"e") == 0) {
d6e703cc
FM
53 strncpy(day,period,2);
54 strncpy(mon,period+2,3);
55 strncpy(year,period+5,4);
25697a35
GS
56 conv_month(mon);
57 sprintf(warea,"%s%s%s",year,mon,day);
58 dfrom=atoi(warea);
d6e703cc
FM
59 strncpy(day,period+10,2);
60 strncpy(mon,period+12,3);
61 strncpy(year,period+15,4);
25697a35
GS
62 conv_month(mon);
63 sprintf(warea,"%s%s%s",year,mon,day);
64 duntil=atoi(warea);
65 } else {
d6e703cc
FM
66 strncpy(day,period+7,2);
67 strncpy(mon,period+4,3);
68 strncpy(year,period,4);
25697a35
GS
69 conv_month(mon);
70 sprintf(warea,"%s%s%s",year,mon,day);
71 dfrom=atoi(warea);
d6e703cc
FM
72 strncpy(day,period+17,2);
73 strncpy(mon,period+14,3);
74 strncpy(year,period+10,4);
25697a35
GS
75 conv_month(mon);
76 sprintf(warea,"%s%s%s",year,mon,day);
77 duntil=atoi(warea);
78 }
79
80 sprintf(guard_in,"%s/dansguardian.unsort",tmp);
81 sprintf(guard_ou,"%s/dansguardian.log",tmp);
82
83 if(access(DansGuardianConf, R_OK) != 0) {
d2fe0c32 84 debuga("Cannot open DansGuardian config file: %s",DansGuardianConf);
25697a35
GS
85 exit(1);
86 }
87
88 if((fp_guard=fopen(DansGuardianConf,"r"))==NULL) {
c36c7384 89 fprintf(stderr, "SARG: (dansguardian) %s: %s\n",_("Cannot open log file"),DansGuardianConf);
25697a35
GS
90 exit(1);
91 }
92
936c9905 93 if((fp_ou=MY_FOPEN(guard_in,"a"))==NULL) {
c36c7384 94 fprintf(stderr, "SARG: (dansguardian) %s: %s\n",_("Cannot open log file"),guard_in);
25697a35
GS
95 exit(1);
96 }
97
98 while(fgets(buf,sizeof(buf),fp_guard)!=NULL) {
9c7c6346
FM
99 fixendofline(buf);
100 if(buf[0]=='#')
25697a35
GS
101 continue;
102 if(strstr(buf,"loglocation ") != 0) {
9c7c6346
FM
103 getword_start(&gwarea,buf);
104 if (getword_skip(MAXLEN,&gwarea,'\'')<0 || getword(loglocation,sizeof(loglocation),&gwarea,'\'')<0) {
4bcb77cf
FM
105 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",DansGuardianConf);
106 exit(1);
107 }
25697a35
GS
108 break;
109 }
110 }
111
112 if(debug) {
e5b2c6f0 113 char short_url[1024];
c36c7384 114 getword_start(&gwarea,_("Reading access log file"));
2240dcea 115 if (getword_limit(short_url,sizeof(short_url),&gwarea,' ')<0 || getword_skip(MAXLEN,&gwarea,' ')<0) {
c36c7384 116 printf("SARG: Maybe you have a broken record or garbage in your %s entry.\n",_("Reading access log file"));
4bcb77cf
FM
117 exit(1);
118 }
e5b2c6f0 119 debuga("%s DansGuardian %s: %s",short_url,gwarea.current,loglocation);
25697a35 120 }
936c9905
FM
121
122 if((fp_in=MY_FOPEN(loglocation,"r"))==NULL) {
c36c7384 123 fprintf(stderr, "SARG: (dansguardian) %s: %s\n",_("Cannot open log file"),loglocation);
25697a35
GS
124 exit(1);
125 }
936c9905 126
25697a35
GS
127 while(fgets(buf,sizeof(buf),fp_in) != NULL) {
128 if(strstr(buf," *DENIED* ") == 0)
129 continue;
9c7c6346
FM
130 getword_start(&gwarea,buf);
131 if (getword(year,sizeof(year),&gwarea,'.')<0 || getword(mon,sizeof(mon),&gwarea,'.')<0 ||
132 getword(day,sizeof(day),&gwarea,' ')<0 || getword(hour,sizeof(hour),&gwarea,' ')<0 ||
133 getword(user,sizeof(user),&gwarea,' ')<0 || getword(ip,sizeof(ip),&gwarea,' ')<0 ||
e5b2c6f0
FM
134 getword_skip(MAXLEN,&gwarea,'/')<0 || getword_skip(MAXLEN,&gwarea,'/')<0) {
135 debuga(_("Maybe you have a broken record or garbage in your %s file"),loglocation);
136 exit(1);
137 }
138 if (getword_ptr(buf,&url,&gwarea,' ')<0) {
139 debuga(_("Maybe you have a broken url in your %s file"),loglocation);
140 exit(1);
141 }
142 if (getword_skip(255,&gwarea,' ')<0 ||
9c7c6346 143 getword(code1,sizeof(code1),&gwarea,' ')<0 || getword(code2,sizeof(code2),&gwarea,' ')<0) {
e5b2c6f0 144 debuga(_("Maybe you have a broken record or garbage in your %s file"),loglocation);
4bcb77cf
FM
145 exit(1);
146 }
25697a35
GS
147 sprintf(wdata,"%s%02d%s",year,atoi(mon),day);
148 idata = atoi(wdata);
149
e6414a9d 150 if(DansguardianIgnoreDate) {
491b862f
GS
151 if(idata < dfrom && idata > duntil)
152 continue;
153 }
154
155 if (strcmp(user,"-") == 0) {
156 strcpy(user,ip);
157 bzero(ip, 30);
25697a35 158 }
120d768c 159 fprintf(fp_ou,"%s\t%d\t%s\t%s\t%s\t%s\t%s\n",user,idata,hour,ip,url,code1,code2);
491b862f 160 dansguardian_count++;
25697a35 161 }
491b862f
GS
162
163 if(fp_in) fclose(fp_in);
164 if(fp_guard) fclose(fp_guard);
165 if(fp_ou) fclose(fp_ou);
166
d2fe0c32 167 if(debug)
c36c7384 168 debuga("%s: %s",_("Sorting file"),guard_ou);
491b862f 169
9a2efbd0 170 sprintf(tmp6,"sort -k 1,1 -k 2,2 -k 4,4 \"%s\" -o \"%s\"",guard_in, guard_ou);
456d78a5
FM
171 cstatus=system(tmp6);
172 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
173 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
174 fprintf(stderr, "SARG: sort command: %s\n",tmp6);
175 exit(1);
176 }
25697a35 177 unlink(guard_in);
25697a35 178}