[go: up one dir, main page]

Menu

[r600]: / tags / v2_2_7 / squidguard_log.c  Maximize  Restore  History

Download this file

331 lines (301 with data), 11.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
* SARG Squid Analysis Report Generator http://sarg.sourceforge.net
* 1998, 2010
*
* 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
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
*
*/
#include "include/conf.h"
#include "include/defs.h"
static char **files_done = NULL;
static int nfiles_done = 0;
static void read_log(const char *wentp, FILE *fp_ou)
{
FILE *fp_in = NULL;
char leks[5], sep[2], res[MAXLEN];
char mon[10], hour[15];
char list[MAXLEN];
char wdata[127];
char url[MAXLEN];
char urly[MAXLEN];
int idata=0;
int i;
char *str;
struct getwordstruct gwarea;
struct getwordstruct gwarea1;
if(debug) {
getword_start(&gwarea,text[7]);
if (getword(urly,sizeof(urly),&gwarea,' ')<0 || getword(href,sizeof(href),&gwarea,' ')<0) {
printf("SARG: Maybe you have a broken record or garbage in your %s string.\n",text[7]);
exit(1);
}
debuga("%s squidGuard %s: %s",urly,gwarea.current,wentp);
}
/* With squidGuard, you can log groups in only one log file.
We must parse each log files only one time. Example :
dest porn {
domainlist porn/domains
urllist porn/urls
log file1.log
}
dest aggressive {
domainlist aggressive/domains
urllist aggressive/urls
log file2.log
}
dest audio-video {
domainlist audio-video/domains
urllist audio-video/urls
log file1.log
}
*/
for (i=0; i<nfiles_done; i++)
if (!strcmp(wentp, files_done[i])) return;
nfiles_done++;
files_done = realloc(files_done, nfiles_done*sizeof(char *));
if (!files_done) {
perror("parse squidGuard - realloc");
exit(EXIT_FAILURE);
}
files_done[nfiles_done-1] = strdup(wentp);
if (!files_done[nfiles_done-1]) {
perror("parse squidGuard - strdup");
exit(EXIT_FAILURE);
}
if ((fp_in=fopen(wentp,"r"))==NULL) {
fprintf(stderr, "SARG: (squidguard) %s: %s\n",text[8],wentp);
exit(1);
}
while (fgets(buf,sizeof(buf),fp_in) != NULL) {
getword_start(&gwarea,buf);
if(SquidGuardLogFormat[0] != '\0') {
getword_start(&gwarea1,SquidGuardLogFormat);
leks[0]='\0';
if (getword(leks,sizeof(leks),&gwarea1,'#')<0) {
printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wentp);
exit(1);
}
while(strcmp(leks,"end") != 0) {
if (getword(leks,sizeof(leks),&gwarea1,'#')<0 || getword(sep,sizeof(sep),&gwarea1,'#')<0) {
printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wentp);
exit(1);
}
if(strcmp(leks,"end") != 0) {
if (getword(res,sizeof(res),&gwarea,sep[0])<0) {
printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wentp);
exit(1);
}
if(strcmp(leks,"year") == 0)
strcpy(year,res);
else if(strcmp(leks,"year") == 0)
strcpy(year,res);
else if(strcmp(leks,"mon") == 0)
strcpy(mon,res);
else if(strcmp(leks,"day") == 0)
strcpy(day,res);
else if(strcmp(leks,"hour") == 0)
strcpy(hour,res);
else if(strcmp(leks,"list") == 0)
strcpy(list,res);
else if(strcmp(leks,"ip") == 0)
strcpy(ip,res);
else if(strcmp(leks,"user") == 0)
strcpy(user,res);
else if(strcmp(leks,"url") == 0)
strcpy(url,res);
}
}
} else {
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_skip(MAXLEN,&gwarea,'/')<0 || getword(list,sizeof(list),&gwarea,'/')<0 ||
getword_skip(MAXLEN,&gwarea,' ')<0 || getword(url,sizeof(url),&gwarea,' ')<0 ||
getword(ip,sizeof(ip),&gwarea,'/')<0 || getword_skip(MAXLEN,&gwarea,' ')<0 ||
getword(user,sizeof(user),&gwarea,' ')<0) {
printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wentp);
exit(1);
}
/*
The URL may be "http://url:port/data" if the method is GET or simply "url:port/" if the method is CONNECT.
The following code removes the protocol:// if it is detected and always truncates the URL after the domain name.
It will fail if the URL doesn't start with the protocol and contains two consecutive / in the path (i.e.
the URL is not normalized).
*/
str=strchr(url,'/');
if (str) {
if (str[1]=='/') {
str+=2;
for (i=0 ; *str && *str!='/' ; i++) url[i]=*str++;
url[i]='\0';
} else {
*str='\0';
}
}
}
sprintf(warea,"%s%s%s",year,mon,day);
sprintf(wdata,"%s%s%s",year,mon,day);
idata = atoi(wdata);
if(SquidguardIgnoreDate) {
if(idata < dfrom || idata > duntil)
continue;
}
if (strcmp(user,"-") == 0) {
strcpy(user,ip);
ip[0]='\0';
}
fprintf(fp_ou,"%s\t%s%s%s\t%s\t%s\t%s\t%s\n",user,year,mon,day,hour,ip,url,list);
squidguard_count++;
}
if (fp_in) fclose(fp_in);
return;
}
void squidguard_log(void)
{
FILE *fp_ou = NULL, *fp_guard = NULL;
char guard_in[MAXLEN];
char guard_ou[MAXLEN];
char logdir[MAXLEN];
char year[10], day[10], mon[10];
char user[MAXLEN];
int y;
int cstatus;
char *str;
char *str2;
str2 = user;
if(strlen(SquidGuardConf) < 1 && strlen(SquidGuardLogAlternate) < 1)
return;
if (SquidGuardLogAlternate[0] != '\0')
SquidGuardConf[0]='\0';
sprintf(guard_in,"%s/squidguard.unsort",tmp);
sprintf(guard_ou,"%s/squidguard.log",tmp);
if((fp_ou=fopen(guard_in,"a"))==NULL) {
fprintf(stderr, "SARG: (squidguard) %s: %s\n",text[8],guard_in);
exit(1);
}
bzero(day, 3);
bzero(mon, 4);
bzero(year, 5);
if(SquidguardIgnoreDate) {
if(strcmp(df,"e") == 0) {
strncpy(day,period,2);
strncpy(mon,period+2,3);
strncpy(year,period+5,4);
conv_month(mon);
sprintf(warea,"%s%s%s",year,mon,day);
dfrom=atoi(warea);
strncpy(day,period+10,2);
strncpy(mon,period+12,3);
strncpy(year,period+15,4);
conv_month(mon);
sprintf(warea,"%s%s%s",year,mon,day);
duntil=atoi(warea);
} else {
strncpy(day,period+7,2);
strncpy(mon,period+4,3);
strncpy(year,period,4);
conv_month(mon);
sprintf(warea,"%s%s%s",year,mon,day);
dfrom=atoi(warea);
strncpy(day,period+17,2);
strncpy(mon,period+14,3);
strncpy(year,period+10,4);
conv_month(mon);
sprintf(warea,"%s%s%s",year,mon,day);
duntil=atoi(warea);
}
}
if(SquidGuardConf[0] != 0) {
if(access(SquidGuardConf, R_OK) != 0) {
debuga("Cannot open squidGuard config file: %s",SquidGuardConf);
exit(1);
}
if((fp_guard=fopen(SquidGuardConf,"r"))==NULL) {
fprintf(stderr, "SARG: (squidguard) %s: %s\n",text[8],SquidGuardConf);
exit(1);
}
logdir[0]=0;
while(fgets(buf,sizeof(buf),fp_guard)!=NULL) {
fixendofline(buf);
if((str=get_param_value("logdir",buf))!=NULL) {
/*
We want to tolerate spaces inside the directory name but we must also
remove the trailing spaces left by the editor after the directory name.
This should not be a problem as nobody use a file name with trailing spaces.
*/
for (y=strlen(str)-1 ; y>=0 && (unsigned char)str[y]<=' ' ; y--);
if (y>=sizeof(logdir)-1) y=sizeof(logdir)-2;
logdir[y+1] = '\0';
while (y>=0) {
logdir[y] = str[y];
y--;
}
} else if((str=get_param_value("log",buf))!=NULL) {
if((str2=get_param_value("anonymous",str))!=NULL)
str=str2;
/*
If logdir is defined, we prepend it to the log file name, otherwise, we assume
the log directive provides an absolute file name to the log file. Therefore,
we don't need to add an additionnal / at the beginning of the log file name.
*/
y=(logdir[0]) ? sprintf(wentp,"%s/",logdir) : 0;
/*
Spaces are allowed in the name of the log file. The file name ends at the first #
because it is assumed it is an end of line comment. Any space before the # is then
removed. Any control character (i.e. a character with a code lower than 32) ends
the file name. That includes the terminating zero.
*/
while((unsigned char)*str>=' ' && *str!='#' && y<sizeof(wentp)-1)
wentp[y++]=*str++;
if(*str=='#') {
str--;
while(*str==' ' && y>0) {
str--;
y--;
}
}
wentp[y]=0;
read_log(wentp,fp_ou);
}
}
} else {
sprintf(wentp,"%s",SquidGuardLogAlternate);
read_log(wentp,fp_ou);
}
if (fp_guard) fclose(fp_guard);
if (fp_ou) fclose(fp_ou);
if (files_done) {
for (y=0; y<nfiles_done; y++)
if (files_done[y]) free(files_done[y]);
free(files_done);
}
if(debug) {
debuga("%s: %s",text[54],guard_ou);
}
sprintf(tmp6,"sort -k 1,1 -k 2,2 -k 4,4 \"%s\" -o \"%s\"",guard_in, guard_ou);
cstatus=system(tmp6);
if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
fprintf(stderr, "SARG: sort command: %s\n",tmp6);
exit(1);
}
unlink(guard_in);
return;
}