]> git.ipfire.org Git - thirdparty/sarg.git/blame - dansguardian_log.c
Patch #2657320 applied.
[thirdparty/sarg.git] / dansguardian_log.c
CommitLineData
25697a35 1/*
c37945ed
FM
2 * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
3 * 1998, 2008
94ff9470 4 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
25697a35
GS
5 *
6 * SARG donations:
7 * please look at http://sarg.sourceforge.net/donations.php
8 * ---------------------------------------------------------------------
9 *
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.
14 *
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.
19 *
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.
23 *
24 */
25
26#include "include/conf.h"
27
28int dansguardian_log()
29{
30
31 FILE *fp_in = NULL, *fp_ou = NULL, *fp_guard = NULL;
32 char guard_in[MAXLEN];
33 char guard_ou[MAXLEN];
34 char loglocation[MAXLEN] = "/var/log/dansguardian/access.log";
35 char year[10], mon[10], day[10];
36 char hour[15];
37 char user[MAXLEN], code1[255], code2[255];
38 char ip[30];
39 char wdata[127];
40 int idata=0;
41 int x, y;
456d78a5 42 int cstatus;
25697a35 43
25697a35
GS
44 bzero(day, 3);
45 bzero(mon, 4);
46 bzero(year, 5);
47
48 if(strcmp(df,"e") == 0) {
d6e703cc
FM
49 strncpy(day,period,2);
50 strncpy(mon,period+2,3);
51 strncpy(year,period+5,4);
25697a35
GS
52 conv_month(mon);
53 sprintf(warea,"%s%s%s",year,mon,day);
54 dfrom=atoi(warea);
d6e703cc
FM
55 strncpy(day,period+10,2);
56 strncpy(mon,period+12,3);
57 strncpy(year,period+15,4);
25697a35
GS
58 conv_month(mon);
59 sprintf(warea,"%s%s%s",year,mon,day);
60 duntil=atoi(warea);
61 } else {
d6e703cc
FM
62 strncpy(day,period+7,2);
63 strncpy(mon,period+4,3);
64 strncpy(year,period,4);
25697a35
GS
65 conv_month(mon);
66 sprintf(warea,"%s%s%s",year,mon,day);
67 dfrom=atoi(warea);
d6e703cc
FM
68 strncpy(day,period+17,2);
69 strncpy(mon,period+14,3);
70 strncpy(year,period+10,4);
25697a35
GS
71 conv_month(mon);
72 sprintf(warea,"%s%s%s",year,mon,day);
73 duntil=atoi(warea);
74 }
75
76 sprintf(guard_in,"%s/dansguardian.unsort",tmp);
77 sprintf(guard_ou,"%s/dansguardian.log",tmp);
78
79 if(access(DansGuardianConf, R_OK) != 0) {
80 sprintf(msg,"Cannot open DansGuardian config file: %s",DansGuardianConf);
81 debuga(msg);
82 exit(1);
83 }
84
85 if((fp_guard=fopen(DansGuardianConf,"r"))==NULL) {
86 fprintf(stderr, "SARG: (dansguardian) %s: %s\n",text[8],DansGuardianConf);
87 exit(1);
88 }
89
0dda06bf
FM
90#if defined(HAVE_FOPEN64)
91 if((fp_ou=(long)fopen64(guard_in,"a"))==NULL) {
92#else
25697a35 93 if((fp_ou=fopen(guard_in,"a"))==NULL) {
0dda06bf 94#endif
25697a35
GS
95 fprintf(stderr, "SARG: (dansguardian) %s: %s\n",text[8],guard_in);
96 exit(1);
97 }
98
99 while(fgets(buf,sizeof(buf),fp_guard)!=NULL) {
100 if(strstr(buf,"\n") != 0)
101 buf[strlen(buf)-1]='\0';
102 if(strncmp(buf,"#",1) == 0)
103 continue;
104 if(strstr(buf,"loglocation ") != 0) {
4bcb77cf
FM
105 if (getword(loglocation,sizeof(loglocation),buf,'\'')<0 || getword(loglocation,sizeof(loglocation),buf,'\'')<0) {
106 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",DansGuardianConf);
107 exit(1);
108 }
25697a35
GS
109 break;
110 }
111 }
112
113 if(debug) {
114 strcpy(buf,text[7]);
4bcb77cf
FM
115 if (getword(urly,sizeof(urly),buf,' ')<0 || getword(href,sizeof(href),buf,' ')<0) {
116 printf("SARG: Maybe you have a broken record or garbage in your %s entry.\n",text[7]);
117 exit(1);
118 }
25697a35
GS
119 sprintf(msg,"%s DansGuardian %s: %s",urly,buf,loglocation);
120 debuga(msg);
121 }
122
0dda06bf
FM
123#if defined(HAVE_FOPEN64)
124 if((fp_in=(long)fopen64(loglocation,"r"))==NULL) {
125#else
25697a35 126 if((fp_in=fopen(loglocation,"r"))==NULL) {
0dda06bf 127#endif
25697a35
GS
128 fprintf(stderr, "SARG: (dansguardian) %s: %s\n",text[8],loglocation);
129 exit(1);
130 }
131
132 while(fgets(buf,sizeof(buf),fp_in) != NULL) {
133 if(strstr(buf," *DENIED* ") == 0)
134 continue;
4bcb77cf
FM
135 if (getword(year,sizeof(year),buf,'.')<0 || getword(mon,sizeof(mon),buf,'.')<0 ||
136 getword(day,sizeof(day),buf,' ')<0 || getword(hour,sizeof(hour),buf,' ')<0 ||
137 getword(user,sizeof(user),buf,' ')<0 || getword(ip,sizeof(ip),buf,' ')<0 ||
138 getword(url,sizeof(url),buf,'/')<0 || getword(url,sizeof(url),buf,'/')<0 ||
139 getword(url,sizeof(url),buf,' ')<0 || getword(code1,sizeof(code1),buf,' ')<0 ||
140 getword(code1,sizeof(code1),buf,' ')<0 || getword(code2,sizeof(code2),buf,' ')<0) {
141 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",loglocation);
142 exit(1);
143 }
25697a35
GS
144 sprintf(wdata,"%s%02d%s",year,atoi(mon),day);
145 idata = atoi(wdata);
146
491b862f
GS
147 if(strcmp(DansguardianIgnoreDate,"on") == 0) {
148 if(idata < dfrom && idata > duntil)
149 continue;
150 }
151
152 if (strcmp(user,"-") == 0) {
153 strcpy(user,ip);
154 bzero(ip, 30);
25697a35 155 }
491b862f
GS
156 sprintf(tmp6,"%s %d %s %s %s %s %s\n",user,idata,hour,ip,url,code1,code2);
157 fputs(tmp6, fp_ou);
158 dansguardian_count++;
25697a35 159 }
491b862f
GS
160
161 if(fp_in) fclose(fp_in);
162 if(fp_guard) fclose(fp_guard);
163 if(fp_ou) fclose(fp_ou);
164
165 if(debug) {
166 sprintf(msg,"%s: %s",text[54],guard_ou);
167 debuga(msg);
168 }
169
94ff9470 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
GS
177 unlink(guard_in);
178 return;
179}