]> git.ipfire.org Git - thirdparty/sarg.git/blob - smartfilter.c
Use boolean to enable all the options instead of string compares.
[thirdparty/sarg.git] / smartfilter.c
1 /*
2 * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
3 * 1998, 2008
4 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
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 #include "include/defs.h"
28
29 void smartfilter_report(void)
30 {
31
32 FILE *fp_in = NULL, *fp_ou = NULL, *fp_user = NULL;
33
34 char url[MAXLEN];
35 char csort[255];
36 char smart_in[MAXLEN];
37 char smart_ou[MAXLEN];
38 char per[MAXLEN];
39 char sites[MAXLEN];
40 char report[MAXLEN];
41 char period[100];
42 char ip[MAXLEN];
43 char user[MAXLEN];
44 char ouser[MAXLEN];
45 char data[15];
46 char hora[15];
47 char smartcat[256];
48 char smartheader[15];
49 char ftime[128];
50 char smartuser[MAXLEN];
51 char *str;
52 int fuser=0;
53 int cstatus;
54 struct getwordstruct gwarea;
55
56 ouser[0]='\0';
57
58 sprintf(smartheader,"%s",text[116]);
59 strup(smartheader);
60
61 sprintf(smart_in,"%s/smartfilter.unsort",dirname);
62 sprintf(sites,"%s/sarg-sites",dirname);
63 sprintf(smart_ou,"%s/smartfilter.log",dirname);
64 sprintf(per,"%s/sarg-period",dirname);
65 sprintf(report,"%s/smartfilter.html",dirname);
66
67 if ((fp_in = fopen(per, "r")) == 0) {
68 fprintf(stderr, "SARG: (smartfilter) %s: %s\n",text[45],per);
69 exit(1);
70 }
71
72 if (!fgets(period,sizeof(period),fp_in)) {
73 fprintf(stderr,"SARG: (smartfilter) read error in %s\n",per);
74 exit(1);
75 }
76 fclose(fp_in);
77
78 if (snprintf(csort,sizeof(csort),"sort -n -k 1,1 -k 2,2 -k 3,3 -o \"%s\" \"%s\"",smart_ou,smart_in)>=sizeof(csort)) {
79 fprintf(stderr,"SARG: cannot sort file %s\n",smart_in);
80 exit(1);
81 }
82 cstatus=system(csort);
83 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
84 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
85 fprintf(stderr, "SARG: sort command: %s\n",csort);
86 exit(1);
87 }
88 if((fp_in=fopen(smart_ou,"r"))==NULL) {
89 fprintf(stderr, "SARG: (smartfilter) %s: %s\n",text[8],smart_ou);
90 fprintf(stderr, "SARG: sort command: %s\n",csort);
91 exit(1);
92 }
93 unlink(smart_in);
94
95 if((fp_ou=fopen(report,"w"))==NULL) {
96 fprintf(stderr, "SARG: (smartfilter) %s: %s\n",text[8],report);
97 exit(1);
98 }
99
100 fprintf(fp_ou, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n<html>\n<head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet);
101 fputs("</head>\n",fp_ou);
102 if(strlen(FontFace) > 0) fprintf(fp_ou,"<font face=%s>\n",FontFace);
103 fprintf(fp_ou,"<body bgcolor=\"%s\" text=\"%s\" background=\"%s\">\n",BgColor,TxColor,BgImage);
104 fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ou);
105 write_logo_image(fp_ou);
106
107 fprintf(fp_ou,"<tr><th align=\"center\"><b><font color=\"%s\" size=\"+1\">%s</font></b></th></tr>\n",TiColor,Title);
108 fprintf(fp_ou,"<tr><td align=\"center\" bgcolor=\"%s\"><font size=\"%s\">%s: %s</font></td></tr>\n",HeaderBgColor,FontSize,text[89],period);
109 fprintf(fp_ou,"<tr><th bgcolor=\"%s\" align=\"center\"><font size=\"%s\">%s</font></th></tr>\n",HeaderBgColor,FontSize,text[116]);
110 fputs("</table></div>\n",fp_ou);
111
112 fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"2\">\n",fp_ou);
113 fputs("<tr><td></td></tr>\n",fp_ou);
114 fputs("<tr><td></td></tr>\n",fp_ou);
115 fputs("<tr><td></td></tr>\n",fp_ou);
116 fprintf(fp_ou,"<tr><th bgcolor=%s><font size=\"%s\">%s</font></th><th bgcolor=\"%s\"><font size=\"%s\">%s</font></th><th bgcolor=\"%s\"><font size=\"%s\">%s</font></th><th bgcolor=\"%s\"><font size=\"%s\">%s</font></th><th bgcolor=\"%s\"><font size=\"%s\">%s</font></th></tr>\n",HeaderBgColor,FontSize,text[98],HeaderBgColor,FontSize,text[111],HeaderBgColor,FontSize,text[110],HeaderBgColor,FontSize,text[91],HeaderBgColor,FontSize,smartheader);
117
118 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
119 getword_start(&gwarea,buf);
120 if (getword(user,sizeof(user),&gwarea,'\t')<0 || getword(data,sizeof(data),&gwarea,'\t')<0 ||
121 getword(hora,sizeof(hora),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0 ||
122 getword(url,sizeof(url),&gwarea,'\t')<0 || getword(smartcat,sizeof(smartcat),&gwarea,'\n')<0) {
123 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",smart_ou);
124 exit(1);
125 }
126
127 if((str=(char *) strstr(user, "_")) != (char *) NULL ) {
128 if((str=(char *) strstr(str+1, "_")) != (char *) NULL )
129 fixip(user);
130 }
131
132 if(strcmp(ouser,user) != 0) {
133 strcpy(ouser,user);
134 sprintf(smartuser,"%s/denied_%s.html",dirname,user);
135 if(fuser) {
136 fuser=0;
137 fputs("</table>\n",fp_user);
138 if(ShowSargInfo) {
139 zdate(ftime, sizeof(ftime), DateFormat);
140 fprintf(fp_user,"<br><br><div align=\"center\"><font size=\"-2\">%s <a href=\"%s\">%s-%s</a> %s %s</font></div>\n",text[108],URL,PGM,VERSION,text[109],ftime);
141 }
142 fputs("</body>\n</html>\n",fp_user);
143 fclose(fp_user);
144 }
145 if ((fp_user = fopen(smartuser, "a")) == 0) {
146 fprintf(stderr, "SARG: (smartfilter) %s: %s\n",text[45],smartuser);
147 exit(1);
148 }
149 fuser=1;
150
151 fputs("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"",fp_ou);
152 fputs(" \"http://www.w3.org/TR/html4/loose.dtd\">\n",fp_ou);
153 fputs("<html>\n",fp_user);
154 fputs("<head>\n",fp_user);
155 fprintf(fp_user," <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet);
156 fputs("</head>\n",fp_user);
157
158 if(FontFace[0] != 0) {
159 /*
160 Before merging the sprintf and the fputs, the code looked like this:
161 sprintf(html2,"<font face=%s>\n",FontFace);
162 fputs(url,fp_user);
163 The two lines don't use the same buffer so the string formated by sprintf is not the string
164 written to fp_user. I (fmarchal) assumed it was a typo and replaced it by a fprintf but
165 that font tag is not valid outside of the body. So, the generated html was likely
166 containing garbage not rendered by the browser.
167 */
168 fprintf(fp_user,"<font face=%s>\n",FontFace);
169 }
170 fprintf(fp_user,"<body bgcolor=\"%s\" text=\"%s\" background=\"%s\">\n",BgColor,TxColor,BgImage);
171 fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_user);
172 if(LogoImage[0]!='\0') fprintf(fp_user,"<tr><th align=left><img src=\"%s\" border=\"0\" align=\"absmiddle\" width=\"%s\" height=\"%s\"><font color=\"%s\">%s</font>\n",LogoImage,Width,Height,LogoTextColor,LogoText);
173 fprintf(fp_user,"<tr><th align=\"center\"><b><font color=\"%s\" size=\"+1\">%s</font></b></th></tr>\n",TiColor,Title);
174 fprintf(fp_user,"<tr><td align=center bgcolor=%s><font size=%s>%s: %s</font></td></tr>\n",HeaderBgColor,FontSize,text[89],period);
175 fprintf(fp_user,"<tr><td align=center bgcolor=%s><font size=%s>%s:</font><font size=%s> %s</font></td></tr>\n",HeaderBgColor,FontSize,text[90],FontSize,user);
176 fputs("</table></div>\n",fp_user);
177 fputs("<div align=\"center\"><table cellpadding=0 cellspacing=2>\n",fp_user);
178 fputs("<tr><td></td></tr>\n",fp_user);
179 fputs("<tr><td></td></tr>\n",fp_user);
180 fputs("<tr><td></td></tr>\n",fp_user);
181 fprintf(fp_user,"<tr><th bgcolor=%s><font size=%s>%s</font></th><th bgcolor=%s><font size=%s>%s</font></th><th bgcolor=%s><font size=%s>%s</font></th><th bgcolor=%s><font size=%s>%s</font></th><th bgcolor=%s><font size=%s>%s</font></th></tr>\n",HeaderBgColor,FontSize,text[98],HeaderBgColor,FontSize,text[111],HeaderBgColor,FontSize,text[110],HeaderBgColor,FontSize,text[91],HeaderBgColor,FontSize,smartheader);
182 }
183 fprintf(fp_user,"<tr><td bgcolor=%s align=center><font size=%s>%s</font></td><td bgcolor=%s align=center><font size=%s>%s</font></td><td bgcolor=%s align=center><font size=%s>%s-%s</font></td><td bgcolor=%s><font size=%s>%s</font></td><td bgcolor=%s><font size=%s>%s</font></td></th>\n",TxBgColor,FontSize,user,TxBgColor,FontSize,ip,TxBgColor,FontSize,data,hora,TxBgColor,FontSize,url,TxBgColor,FontSize,smartcat);
184
185 fprintf(fp_ou,"<tr><td bgcolor=%s align=center><font size=%s>%s</font></td><td bgcolor=%s align=center><font size=%s>%s</font></td><td bgcolor=%s align=center><font size=%s>%s-%s</font></td><td bgcolor=%s><font size=%s>%s</font></td><td bgcolor=%s><font size=%s>%s</font></td></th>\n",TxBgColor,FontSize,user,TxBgColor,FontSize,ip,TxBgColor,FontSize,data,hora,TxBgColor,FontSize,url,TxBgColor,FontSize,smartcat);
186 }
187
188 fputs("</table>\n",fp_ou);
189
190 if(ShowSargInfo) {
191 zdate(ftime, sizeof(ftime), DateFormat);
192 fprintf(fp_ou,"<br><br><div align=\"center\"><font size=\"-2\">%s <a href=\"%s\">%s-%s</a> %s %s</font></div>\n",text[108],URL,PGM,VERSION,text[109],ftime);
193 }
194
195 fputs("</body>\n</html>\n",fp_user);
196
197 fclose(fp_ou);
198 if(fp_user) {
199 fputs("</table>\n",fp_user);
200 if(ShowSargInfo) {
201 zdate(ftime, sizeof(ftime), DateFormat);
202 fprintf(fp_user,"<br><br><div align=\"center\"><font size=\"-2\">%s <a href=\"%s\">%s-%s</a> %s %s</font></div>\n",text[108],URL,PGM,VERSION,text[109],ftime);
203 }
204 fputs("</body>\n</html>\n",fp_user);
205 fclose(fp_user);
206 }
207
208 return;
209 }