]>
git.ipfire.org Git - thirdparty/sarg.git/blob - squidguard_log.c
2 * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
4 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
7 * please look at http://sarg.sourceforge.net/donations.php
8 * ---------------------------------------------------------------------
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
26 #include "include/conf.h"
27 #include "include/defs.h"
29 static char **files_done
= NULL
;
30 static int nfiles_done
= 0;
33 static void read_log(const char *wentp
, FILE *fp_ou
)
37 char leks
[5], sep
[2], res
[MAXLEN
];
38 char mon
[10], hour
[15];
46 if (getword(urly
,sizeof(urly
),buf
,' ')<0 || getword(href
,sizeof(href
),buf
,' ')<0) {
47 printf("SARG: Maybe you have a broken record or garbage in your %s string.\n",text
[7]);
50 sprintf(msg
,"%s squidGuard %s: %s",urly
,buf
,wentp
);
54 /* With squidGuard, you can log groups in only one log file.
55 We must parse each log files only one time. Example :
57 domainlist porn/domains
62 domainlist aggressive/domains
63 urllist aggressive/urls
67 domainlist audio-video/domains
68 urllist audio-video/urls
72 for (i
=0; i
<nfiles_done
; i
++)
73 if (!strcmp(wentp
, files_done
[i
])) return;
76 files_done
= realloc(files_done
, nfiles_done
*sizeof(char *));
78 perror("parse squidGuard - realloc");
81 files_done
[nfiles_done
-1] = strdup(wentp
);
82 if (!files_done
[nfiles_done
-1]) {
83 perror("parse squidGuard - strdup");
87 if ((fp_in
=fopen(wentp
,"r"))==NULL
) {
88 fprintf(stderr
, "SARG: (squidguard) %s: %s\n",text
[8],wentp
);
92 while (fgets(buf
,sizeof(buf
),fp_in
) != NULL
) {
93 if(strlen(SquidGuardLogFormat
) > 0) {
94 strcpy(bufbsf
,SquidGuardLogFormat
);
96 if (getword(leks
,sizeof(leks
),bufbsf
,'#')<0) {
97 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wentp
);
100 while(strcmp(leks
,"end") != 0) {
101 if (getword(leks
,sizeof(leks
),bufbsf
,'#')<0 || getword(sep
,sizeof(sep
),bufbsf
,'#')<0) {
102 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wentp
);
105 if(strcmp(leks
,"end") != 0) {
106 if (getword(res
,sizeof(res
),buf
,sep
[0])<0) {
107 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wentp
);
110 if(strcmp(leks
,"year") == 0)
112 else if(strcmp(leks
,"year") == 0)
114 else if(strcmp(leks
,"mon") == 0)
116 else if(strcmp(leks
,"day") == 0)
118 else if(strcmp(leks
,"hour") == 0)
120 else if(strcmp(leks
,"list") == 0)
122 else if(strcmp(leks
,"ip") == 0)
124 else if(strcmp(leks
,"user") == 0)
126 else if(strcmp(leks
,"url") == 0)
131 if (getword(year
,sizeof(year
),buf
,'-')<0 || getword(mon
,sizeof(mon
),buf
,'-')<0 ||
132 getword(day
,sizeof(day
),buf
,' ')<0 || getword(hour
,sizeof(hour
),buf
,' ')<0 ||
133 getword(list
,sizeof(list
),buf
,'/')<0 || getword(list
,sizeof(list
),buf
,'/')<0 ||
134 getword(tmp5
,sizeof(tmp5
),buf
,'/')<0 || getword(tmp5
,sizeof(tmp5
),buf
,'/')<0 ||
135 getword(url
,sizeof(url
),buf
,'/')<0 || getword(tmp5
,sizeof(tmp5
),buf
,' ')<0 ||
136 getword(ip
,sizeof(ip
),buf
,'/')<0 || getword(user
,sizeof(user
),buf
,' ')<0 ||
137 getword(user
,sizeof(user
),buf
,' ')<0) {
138 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wentp
);
143 sprintf(warea
,"%s%s%s",year
,mon
,day
);
144 sprintf(wdata
,"%s%s%s",year
,mon
,day
);
147 if(strcmp(SquidguardIgnoreDate
,"on") == 0) {
148 if(idata
< dfrom
|| idata
> duntil
)
152 if (strcmp(user
,"-") == 0) {
156 fprintf(fp_ou
,"%s %s%s%s %s %s %s %s\n",user
,year
,mon
,day
,hour
,ip
,url
,list
);
159 if (fp_in
) fclose(fp_in
);
164 void squidguard_log(void)
166 FILE *fp_ou
= NULL
, *fp_guard
= NULL
;
167 char guard_in
[MAXLEN
];
168 char guard_ou
[MAXLEN
];
170 char year
[10], day
[10], mon
[10];
179 if(strlen(SquidGuardConf
) < 1 && strlen(SquidGuardLogAlternate
) < 1)
182 if (strlen(SquidGuardLogAlternate
) > 0)
183 SquidGuardConf
[0]='\0';
185 sprintf(guard_in
,"%s/squidguard.unsort",tmp
);
186 sprintf(guard_ou
,"%s/squidguard.log",tmp
);
187 if((fp_ou
=fopen(guard_in
,"a"))==NULL
) {
188 fprintf(stderr
, "SARG: (squidguard) %s: %s\n",text
[8],guard_in
);
196 if(strcmp(SquidguardIgnoreDate
,"on") == 0) {
197 if(strcmp(df
,"e") == 0) {
198 strncpy(day
,period
,2);
199 strncpy(mon
,period
+2,3);
200 strncpy(year
,period
+5,4);
202 sprintf(warea
,"%s%s%s",year
,mon
,day
);
204 strncpy(day
,period
+10,2);
205 strncpy(mon
,period
+12,3);
206 strncpy(year
,period
+15,4);
208 sprintf(warea
,"%s%s%s",year
,mon
,day
);
211 strncpy(day
,period
+7,2);
212 strncpy(mon
,period
+4,3);
213 strncpy(year
,period
,4);
215 sprintf(warea
,"%s%s%s",year
,mon
,day
);
217 strncpy(day
,period
+17,2);
218 strncpy(mon
,period
+14,3);
219 strncpy(year
,period
+10,4);
221 sprintf(warea
,"%s%s%s",year
,mon
,day
);
226 if(strlen(SquidGuardConf
) > 0) {
227 if(access(SquidGuardConf
, R_OK
) != 0) {
228 sprintf(msg
,"Cannot open squidGuard config file: %s",SquidGuardConf
);
233 if((fp_guard
=fopen(SquidGuardConf
,"r"))==NULL
) {
234 fprintf(stderr
, "SARG: (squidguard) %s: %s\n",text
[8],SquidGuardConf
);
239 while(fgets(buf
,sizeof(buf
),fp_guard
)!=NULL
) {
240 if(strstr(buf
,"\n") != 0)
241 buf
[strlen(buf
)-1]='\0';
242 if((str
=get_param_value("logdir",buf
))!=NULL
) {
243 if (getword(logdir
,sizeof(logdir
),str
,' ')<0) {
244 printf("SARG: Maybe you have an invalid logdir line in your %s file.\n",SquidGuardConf
);
247 } else if((str
=get_param_value("log",buf
))!=NULL
) {
248 if((str2
=get_param_value("anonymous",str
))!=NULL
)
252 If logdir is defined, we prepend it to the log file name, otherwise, we assume
253 the log directive provides an absolute file name to the log file. Therefore,
254 we don't need to add an additionnal / at the beginning of the log file name.
256 y
=(logdir
[0]) ? sprintf(wentp
,"%s/",logdir
) : 0;
258 Spaces are allowed in the name of the log file. The file name ends at the first #
259 because it is assumed it is an end of line comment. Any space before the # is then
260 removed. Any control character (i.e. a character with a code lower than 32) ends
261 the file name. That includes the terminating zero.
263 while((unsigned char)*str
>=' ' && *str
!='#' && y
<sizeof(wentp
)-1)
267 while(*str
==' ' && y
>0) {
273 read_log(wentp
,fp_ou
);
277 sprintf(wentp
,"%s",SquidGuardLogAlternate
);
278 read_log(wentp
,fp_ou
);
281 if (fp_guard
) fclose(fp_guard
);
282 if (fp_ou
) fclose(fp_ou
);
285 sprintf(msg
,"%s: %s",text
[54],guard_ou
);
289 sprintf(tmp6
,"sort -k 1,1 -k 2,2 -k 4,4 '%s' -o '%s'",guard_in
, guard_ou
);
290 cstatus
=system(tmp6
);
291 if (!WIFEXITED(cstatus
) || WEXITSTATUS(cstatus
)) {
292 fprintf(stderr
, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus
));
293 fprintf(stderr
, "SARG: sort command: %s\n",tmp6
);