2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
6 * please look at http://sarg.sourceforge.net/donations.php
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
9 * ---------------------------------------------------------------------
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.
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.
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.
27 #include "include/conf.h"
28 #include "include/defs.h"
30 void dansguardian_log(void)
32 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";
40 char user
[MAXLEN
], code1
[255], code2
[255];
47 struct getwordstruct gwarea
;
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
;
52 snprintf(guard_in
,sizeof(guard_in
),"%s/dansguardian.int_unsort",tmp
);
53 snprintf(guard_ou
,sizeof(guard_ou
),"%s/dansguardian.int_log",tmp
);
55 if((fp_guard
=fopen(DansGuardianConf
,"r"))==NULL
) {
56 debugapos("dansguardian",_("Cannot open file \"%s\": %s\n"),DansGuardianConf
,strerror(errno
));
60 if((fp_ou
=MY_FOPEN(guard_in
,"a"))==NULL
) {
61 debugapos("dansguardian",_("Cannot open file \"%s\": %s\n"),guard_in
,strerror(errno
));
65 while(fgets(buf
,sizeof(buf
),fp_guard
)!=NULL
) {
69 if(strstr(buf
,"loglocation ") != 0) {
70 getword_start(&gwarea
,buf
);
71 if (getword_skip(MAXLEN
,&gwarea
,'\'')<0 || getword(loglocation
,sizeof(loglocation
),&gwarea
,'\'')<0) {
72 debuga(_("Invalid record in file \"%s\"\n"),DansGuardianConf
);
75 if (debug
) debuga(_("Using the dansguardian log file \"%s\" found in your configuration file \"%s\"\n"),
76 loglocation
,DansGuardianConf
);
82 debuga(_("Reading DansGuardian log file \"%s\"\n"),loglocation
);
84 if((fp_in
=MY_FOPEN(loglocation
,"r"))==NULL
) {
85 debugapos("dansguardian",_("Cannot open file \"%s\": %s\n"),loglocation
,strerror(errno
));
89 while(fgets(buf
,sizeof(buf
),fp_in
) != NULL
) {
90 if(strstr(buf
," *DENIED* ") == 0)
92 getword_start(&gwarea
,buf
);
93 if (getword_atoi(&year
,&gwarea
,'.')<0 || getword_atoi(&mon
,&gwarea
,'.')<0 ||
94 getword_atoi(&day
,&gwarea
,' ')<0) {
95 debuga(_("Invalid date in file \"%s\"\n"),loglocation
);
98 if (getword_atoi(&hour
,&gwarea
,':')<0 || getword(minsec
,sizeof(minsec
),&gwarea
,' ')<0) {
99 debuga(_("Invalid time in file \"%s\"\n"),loglocation
);
102 if (getword(user
,sizeof(user
),&gwarea
,' ')<0) {
103 debuga(_("Invalid user in file \"%s\"\n"),loglocation
);
106 if (getword(ip
,sizeof(ip
),&gwarea
,' ')<0) {
107 debuga(_("Invalid IP address in file \"%s\"\n"),loglocation
);
110 if (getword_skip(MAXLEN
,&gwarea
,'/')<0 || getword_skip(MAXLEN
,&gwarea
,'/')<0) {
111 debuga(_("Invalid record in file \"%s\"\n"),loglocation
);
114 if (getword_ptr(buf
,&url
,&gwarea
,' ')<0) {
115 debuga(_("Invalid url in file \"%s\"\n"),loglocation
);
118 if (getword_skip(255,&gwarea
,' ')<0 ||
119 getword(code1
,sizeof(code1
),&gwarea
,' ')<0 || getword(code2
,sizeof(code2
),&gwarea
,' ')<0) {
120 debuga(_("Invalid record in file \"%s\"\n"),loglocation
);
123 idata
= year
*10000+mon
*100+day
;
125 if(DansguardianFilterOutDate
) {
126 if(idata
< dfrom
|| idata
> duntil
)
130 if (strcmp(user
,"-") == 0) {
134 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
);
135 dansguardian_count
++;
138 if(fp_in
) fclose(fp_in
);
139 if(fp_guard
) fclose(fp_guard
);
140 if(fp_ou
) fclose(fp_ou
);
143 debuga(_("Sorting file \"%s\"\n"),guard_ou
);
145 snprintf(tmp6
,sizeof(tmp6
),"sort -t \"\t\" -k 1,1 -k 2,2 -k 4,4 \"%s\" -o \"%s\"",guard_in
, guard_ou
);
146 cstatus
=system(tmp6
);
147 if (!WIFEXITED(cstatus
) || WEXITSTATUS(cstatus
)) {
148 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus
));
149 debuga(_("sort command: %s\n"),tmp6
);
152 if (!KeepTempLog
&& unlink(guard_in
)) {
153 debuga(_("Cannot delete \"%s\": %s\n"),guard_in
,strerror(errno
));