]> git.ipfire.org Git - thirdparty/sarg.git/blame - smartfilter.c
Decouple the denied and authentication failure reports
[thirdparty/sarg.git] / smartfilter.c
CommitLineData
25697a35 1/*
94ff9470 2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
fbd133bb 3 * 1998, 2011
25697a35
GS
4 *
5 * SARG donations:
6 * please look at http://sarg.sourceforge.net/donations.php
1164c474
FM
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
25697a35
GS
9 * ---------------------------------------------------------------------
10 *
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.
15 *
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.
20 *
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.
24 *
25 */
26
27#include "include/conf.h"
5f3cfd1d 28#include "include/defs.h"
25697a35 29
32e71fa4 30void smartfilter_report(void)
25697a35 31{
9bd92830
FM
32 FILE *fp_in = NULL, *fp_ou = NULL, *fp_user = NULL;
33
34 char buf[MAXLEN];
35 char url[MAXLEN];
36 char csort[255];
37 char smart_in[MAXLEN];
38 char smart_ou[MAXLEN];
39 char sites[MAXLEN];
40 char report[MAXLEN];
41 char ip[MAXLEN];
42 char user[MAXLEN];
43 char ouser[MAXLEN];
44 char data[15];
45 char hora[15];
46 char smartcat[256];
47 char smartheader[15];
48 char ftime[128];
49 char smartuser[MAXLEN];
50 int fuser=0;
51 int cstatus;
52 struct getwordstruct gwarea;
53 const struct userinfostruct *uinfo;
54
55 ouser[0]='\0';
56
57 strcpy(smartheader,_("SmartFilter"));
58 strup(smartheader);
59
60 sprintf(smart_in,"%s/smartfilter.unsort",outdirname);
61 sprintf(sites,"%s/sarg-sites",outdirname);
62 sprintf(smart_ou,"%s/smartfilter.log",outdirname);
63 sprintf(report,"%s/smartfilter.html",outdirname);
64
eb843369 65 if (snprintf(csort,sizeof(csort),"sort -n -t \"\t\" -k 1,1 -k 2,2 -k 3,3 -o \"%s\" \"%s\"",smart_ou,smart_in)>=sizeof(csort)) {
9bd92830
FM
66 debuga(_("cannot build the sort command to sort file %s\n"),smart_in);
67 exit(EXIT_FAILURE);
68 }
69 cstatus=system(csort);
70 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
71 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
72 debuga(_("sort command: %s\n"),csort);
73 exit(EXIT_FAILURE);
74 }
75 if((fp_in=fopen(smart_ou,"r"))==NULL) {
76 debuga(_("(smartfilter) Cannot open log file %s\n"),smart_ou);
77 debuga(_("sort command: %s\n"),csort);
78 exit(EXIT_FAILURE);
79 }
08f9b029
FM
80 if (unlink(smart_in)) {
81 debuga(_("Cannot delete %s - %s\n"),smart_in,strerror(errno));
82 exit(EXIT_FAILURE);
83 }
9bd92830
FM
84
85 if((fp_ou=fopen(report,"w"))==NULL) {
007905af
FM
86 debuga(_("(smartfilter) Cannot open log file %s\n"),report);
87 exit(EXIT_FAILURE);
9bd92830
FM
88 }
89
90 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);
91 fputs("</head>\n",fp_ou);
92 if(strlen(FontFace) > 0) fprintf(fp_ou,"<font face=%s>\n",FontFace);
93 fprintf(fp_ou,"<body bgcolor=\"%s\" text=\"%s\" background=\"%s\">\n",BgColor,TxColor,BgImage);
94 fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ou);
95 write_logo_image(fp_ou);
96
97 fprintf(fp_ou,"<tr><th align=\"center\"><b><font color=\"%s\" size=\"+1\">%s</font></b></th></tr>\n",TiColor,Title);
98 fprintf(fp_ou,"<tr><td align=\"center\" bgcolor=\"%s\"><font size=\"%s\">",HeaderBgColor,FontSize);
99 fprintf(fp_ou,_("Period: %s"),period.html);
100 fputs("</font></td></tr>\n",fp_ou);
101 fprintf(fp_ou,"<tr><th bgcolor=\"%s\" align=\"center\"><font size=\"%s\">%s</font></th></tr>\n",HeaderBgColor,FontSize,_("SmartFilter"));
102 fputs("</table></div>\n",fp_ou);
103
104 fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"2\">\n",fp_ou);
105 fputs("<tr><td></td></tr>\n",fp_ou);
106 fputs("<tr><td></td></tr>\n",fp_ou);
107 fputs("<tr><td></td></tr>\n",fp_ou);
108 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,_("USERID"),HeaderBgColor,FontSize,_("IP/NAME"),HeaderBgColor,FontSize,_("DATE/TIME"),HeaderBgColor,FontSize,_("ACCESSED SITE"),HeaderBgColor,FontSize,smartheader);
109
110 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
111 getword_start(&gwarea,buf);
112 if (getword(user,sizeof(user),&gwarea,'\t')<0 || getword(data,sizeof(data),&gwarea,'\t')<0 ||
007905af
FM
113 getword(hora,sizeof(hora),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0 ||
114 getword(url,sizeof(url),&gwarea,'\t')<0 || getword(smartcat,sizeof(smartcat),&gwarea,'\n')<0) {
9bd92830
FM
115 debuga(_("There is a broken record or garbage in file %s\n"),smart_ou);
116 exit(EXIT_FAILURE);
117 }
118
119 uinfo=userinfo_find_from_id(user);
120 if (!uinfo) {
121 debuga(_("Unknown user ID %s in file %s\n"),user,smart_ou);
122 exit(EXIT_FAILURE);
123 }
124 if(strcmp(ouser,user) != 0) {
125 strcpy(ouser,user);
126 sprintf(smartuser,"%s/denied_%s.html",outdirname,uinfo->filename);
127 if(fuser) {
128 fuser=0;
129 fputs("</table>\n",fp_user);
130 if(ShowSargInfo) {
131 zdate(ftime, sizeof(ftime), DateFormat);
132 fprintf(fp_user,"<br><br><div align=\"center\"><font size=\"-2\">%s <a href=\"%s\">%s-%s</a> %s %s</font></div>\n",_("Generated by"),URL,PGM,VERSION,_("on"),ftime);
133 }
134 fputs("</body>\n</html>\n",fp_user);
135 fclose(fp_user);
136 }
137 if ((fp_user = fopen(smartuser, "a")) == 0) {
138 debuga(_("(smartfilter) Cannot open file %s\n"),smartuser);
139 exit(EXIT_FAILURE);
140 }
141 fuser=1;
142
143 fputs("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"",fp_ou);
144 fputs(" \"http://www.w3.org/TR/html4/loose.dtd\">\n",fp_ou);
145 fputs("<html>\n",fp_user);
146 fputs("<head>\n",fp_user);
147 fprintf(fp_user," <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet);
148 fputs("</head>\n",fp_user);
149
150 if(FontFace[0] != 0) {
151 /*
152 Before merging the sprintf and the fputs, the code looked like this:
153 sprintf(html2,"<font face=%s>\n",FontFace);
154 fputs(url,fp_user);
155 The two lines don't use the same buffer so the string formated by sprintf is not the string
156 written to fp_user. I (fmarchal) assumed it was a typo and replaced it by a fprintf but
157 that font tag is not valid outside of the body. So, the generated html was likely
158 containing garbage not rendered by the browser.
159 */
160 fprintf(fp_user,"<font face=%s>\n",FontFace);
161 }
162 fprintf(fp_user,"<body bgcolor=\"%s\" text=\"%s\" background=\"%s\">\n",BgColor,TxColor,BgImage);
163 fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_user);
164 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);
165 fprintf(fp_user,"<tr><th align=\"center\"><b><font color=\"%s\" size=\"+1\">%s</font></b></th></tr>\n",TiColor,Title);
166 fputs("<tr><td align=center bgcolor=\"%s\"><font size=\"%s\">",fp_user);
167 fprintf(fp_user,_("Period: %s"),period.html);
168 fputs("</font></td></tr>\n",fp_user);
169 fprintf(fp_user,"<tr><td align=\"center\" bgcolor=\"%s\"><font size=\"%s\">%s:</font><font size=\"%s\"> %s</font></td></tr>\n",HeaderBgColor,FontSize,_("User"),FontSize,uinfo->label);
170 fputs("</table></div>\n",fp_user);
171 fputs("<div align=\"center\"><table cellpadding=0 cellspacing=2>\n",fp_user);
172 fputs("<tr><td></td></tr>\n",fp_user);
173 fputs("<tr><td></td></tr>\n",fp_user);
174 fputs("<tr><td></td></tr>\n",fp_user);
175 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,_("USERID"),HeaderBgColor,FontSize,_("IP/NAME"),HeaderBgColor,FontSize,_("DATE/TIME"),HeaderBgColor,FontSize,_("ACCESSED SITE"),HeaderBgColor,FontSize,smartheader);
176 }
177 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,uinfo->label,TxBgColor,FontSize,ip,TxBgColor,FontSize,data,hora,TxBgColor,FontSize,url,TxBgColor,FontSize,smartcat);
178
179 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,uinfo->label,TxBgColor,FontSize,ip,TxBgColor,FontSize,data,hora,TxBgColor,FontSize,url,TxBgColor,FontSize,smartcat);
180 }
181
182 fputs("</table>\n",fp_ou);
183
184 if(ShowSargInfo) {
185 zdate(ftime, sizeof(ftime), DateFormat);
186 fprintf(fp_ou,"<br><br><div align=\"center\"><font size=\"-2\">%s <a href=\"%s\">%s-%s</a> %s %s</font></div>\n",_("Generated by"),URL,PGM,VERSION,_("on"),ftime);
187 }
188
189 fputs("</body>\n</html>\n",fp_user);
190
191 fclose(fp_ou);
192 if(fp_user) {
193 fputs("</table>\n",fp_user);
194 if(ShowSargInfo) {
195 zdate(ftime, sizeof(ftime), DateFormat);
196 fprintf(fp_user,"<br><br><div align=\"center\"><font size=\"-2\">%s <a href=\"%s\">%s-%s</a> %s %s</font></div>\n",_("Generated by"),URL,PGM,VERSION,_("on"),ftime);
197 }
198 fputs("</body>\n</html>\n",fp_user);
199 fclose(fp_user);
200 }
201
202 return;
25697a35 203}