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