]> git.ipfire.org Git - thirdparty/sarg.git/blame - smartfilter.c
Rename configure.in as configure.ac
[thirdparty/sarg.git] / smartfilter.c
CommitLineData
25697a35 1/*
94ff9470 2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
110ce984 3 * 1998, 2015
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];
9bd92830
FM
47 char ftime[128];
48 char smartuser[MAXLEN];
9bd92830
FM
49 int cstatus;
50 struct getwordstruct gwarea;
51 const struct userinfostruct *uinfo;
52
53 ouser[0]='\0';
54
1caa1b3d 55 if (snprintf(smart_in,sizeof(smart_in),"%s/smartfilter.int_unsort",tmp)>=sizeof(smart_in)) {
af961877 56 debuga(__FILE__,__LINE__,_("Path too long: "));
041018b6 57 debuga_more("%s/smartfilter.int_unsort\n",tmp);
8d811f76
FM
58 exit(EXIT_FAILURE);
59 }
60 if (snprintf(sites,sizeof(sites),"%s/sarg-sites",outdirname)>=sizeof(sites)) {
af961877 61 debuga(__FILE__,__LINE__,_("Path too long: "));
041018b6 62 debuga_more("%s/sarg-sites\n",outdirname);
8d811f76
FM
63 exit(EXIT_FAILURE);
64 }
1caa1b3d 65 if (snprintf(smart_ou,sizeof(smart_ou),"%s/smartfilter.int_log",tmp)>=sizeof(smart_ou)) {
af961877 66 debuga(__FILE__,__LINE__,_("Path too long: "));
041018b6 67 debuga_more("%s/smartfilter.int_log\n",tmp);
8d811f76
FM
68 exit(EXIT_FAILURE);
69 }
70 if (snprintf(report,sizeof(report),"%s/smartfilter.html",outdirname)>=sizeof(report)) {
af961877 71 debuga(__FILE__,__LINE__,_("Path too long: "));
041018b6 72 debuga_more("%s/smartfilter.html\n",outdirname);
8d811f76
FM
73 exit(EXIT_FAILURE);
74 }
9bd92830 75
eb843369 76 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)) {
af961877 77 debuga(__FILE__,__LINE__,_("Cannot build the sort command to sort file \"%s\"\n"),smart_in);
9bd92830
FM
78 exit(EXIT_FAILURE);
79 }
80 cstatus=system(csort);
81 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
af961877
FM
82 debuga(__FILE__,__LINE__,_("sort command return status %d\n"),WEXITSTATUS(cstatus));
83 debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
9bd92830
FM
84 exit(EXIT_FAILURE);
85 }
86 if((fp_in=fopen(smart_ou,"r"))==NULL) {
af961877
FM
87 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),smart_ou,strerror(errno));
88 debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
9bd92830
FM
89 exit(EXIT_FAILURE);
90 }
11767c6a 91 if (!KeepTempLog && unlink(smart_in)) {
af961877 92 debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),smart_in,strerror(errno));
08f9b029
FM
93 exit(EXIT_FAILURE);
94 }
9bd92830
FM
95
96 if((fp_ou=fopen(report,"w"))==NULL) {
af961877 97 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),report,strerror(errno));
007905af 98 exit(EXIT_FAILURE);
9bd92830
FM
99 }
100
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);
102 fputs("</head>\n",fp_ou);
103 if(strlen(FontFace) > 0) fprintf(fp_ou,"<font face=%s>\n",FontFace);
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);
107
108 fprintf(fp_ou,"<tr><th align=\"center\"><b><font color=\"%s\" size=\"+1\">%s</font></b></th></tr>\n",TiColor,Title);
109 fprintf(fp_ou,"<tr><td align=\"center\" bgcolor=\"%s\"><font size=\"%s\">",HeaderBgColor,FontSize);
110 fprintf(fp_ou,_("Period: %s"),period.html);
111 fputs("</font></td></tr>\n",fp_ou);
112 fprintf(fp_ou,"<tr><th bgcolor=\"%s\" align=\"center\"><font size=\"%s\">%s</font></th></tr>\n",HeaderBgColor,FontSize,_("SmartFilter"));
113 fputs("</table></div>\n",fp_ou);
114
115 fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"2\">\n",fp_ou);
116 fputs("<tr><td></td></tr>\n",fp_ou);
117 fputs("<tr><td></td></tr>\n",fp_ou);
118 fputs("<tr><td></td></tr>\n",fp_ou);
bde5073d 119 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,_("SMARTFILTER"));
9bd92830
FM
120
121 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
122 getword_start(&gwarea,buf);
123 if (getword(user,sizeof(user),&gwarea,'\t')<0 || getword(data,sizeof(data),&gwarea,'\t')<0 ||
007905af
FM
124 getword(hora,sizeof(hora),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0 ||
125 getword(url,sizeof(url),&gwarea,'\t')<0 || getword(smartcat,sizeof(smartcat),&gwarea,'\n')<0) {
af961877 126 debuga(__FILE__,__LINE__,_("Invalid record in file \"%s\"\n"),smart_ou);
9bd92830
FM
127 exit(EXIT_FAILURE);
128 }
129
130 uinfo=userinfo_find_from_id(user);
131 if (!uinfo) {
af961877 132 debuga(__FILE__,__LINE__,_("Unknown user ID %s in file \"%s\"\n"),user,smart_ou);
9bd92830
FM
133 exit(EXIT_FAILURE);
134 }
135 if(strcmp(ouser,user) != 0) {
136 strcpy(ouser,user);
137 sprintf(smartuser,"%s/denied_%s.html",outdirname,uinfo->filename);
507460ae 138 if (fp_user) {
9bd92830
FM
139 fputs("</table>\n",fp_user);
140 if(ShowSargInfo) {
81a022d8 141 zdate(ftime, sizeof(ftime), df);
b3a3c51c
FM
142 fputs("<br><br><div align=\"center\"><font size=\"-2\">",fp_user);
143 fprintf(fp_user,_("Generated by <a href=\"%s\">%s-%s</a> on %s"),URL,PGM,VERSION,ftime);
144 fputs("</font></div>\n",fp_user);
9bd92830
FM
145 }
146 fputs("</body>\n</html>\n",fp_user);
507460ae 147 if (fclose(fp_user)==EOF) {
af961877 148 debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),smartuser,strerror(errno));
507460ae
FM
149 exit(EXIT_FAILURE);
150 }
151 fp_user=NULL;
9bd92830
FM
152 }
153 if ((fp_user = fopen(smartuser, "a")) == 0) {
af961877 154 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),smartuser,strerror(errno));
9bd92830
FM
155 exit(EXIT_FAILURE);
156 }
9bd92830
FM
157
158 fputs("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"",fp_ou);
159 fputs(" \"http://www.w3.org/TR/html4/loose.dtd\">\n",fp_ou);
160 fputs("<html>\n",fp_user);
161 fputs("<head>\n",fp_user);
162 fprintf(fp_user," <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet);
163 fputs("</head>\n",fp_user);
164
165 if(FontFace[0] != 0) {
166 /*
167 Before merging the sprintf and the fputs, the code looked like this:
168 sprintf(html2,"<font face=%s>\n",FontFace);
169 fputs(url,fp_user);
170 The two lines don't use the same buffer so the string formated by sprintf is not the string
171 written to fp_user. I (fmarchal) assumed it was a typo and replaced it by a fprintf but
172 that font tag is not valid outside of the body. So, the generated html was likely
173 containing garbage not rendered by the browser.
174 */
175 fprintf(fp_user,"<font face=%s>\n",FontFace);
176 }
177 fprintf(fp_user,"<body bgcolor=\"%s\" text=\"%s\" background=\"%s\">\n",BgColor,TxColor,BgImage);
178 fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_user);
179 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);
180 fprintf(fp_user,"<tr><th align=\"center\"><b><font color=\"%s\" size=\"+1\">%s</font></b></th></tr>\n",TiColor,Title);
181 fputs("<tr><td align=center bgcolor=\"%s\"><font size=\"%s\">",fp_user);
182 fprintf(fp_user,_("Period: %s"),period.html);
183 fputs("</font></td></tr>\n",fp_user);
184 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);
185 fputs("</table></div>\n",fp_user);
186 fputs("<div align=\"center\"><table cellpadding=0 cellspacing=2>\n",fp_user);
187 fputs("<tr><td></td></tr>\n",fp_user);
188 fputs("<tr><td></td></tr>\n",fp_user);
189 fputs("<tr><td></td></tr>\n",fp_user);
bde5073d 190 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,_("SMARTFILTER"));
9bd92830
FM
191 }
192 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);
193
194 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);
195 }
196
197 fputs("</table>\n",fp_ou);
198
199 if(ShowSargInfo) {
81a022d8 200 zdate(ftime, sizeof(ftime), df);
05a933cc
FM
201 fputs("<br><br><div align=\"center\"><font size=\"-2\">",fp_ou);
202 fprintf(fp_ou,_("Generated by <a href=\"%s\">%s-%s</a> on %s"),URL,PGM,VERSION,ftime);
203 fputs("</font></div>\n",fp_ou);
9bd92830
FM
204 }
205
206 fputs("</body>\n</html>\n",fp_user);
207
507460ae 208 if (fclose(fp_ou)==EOF) {
af961877 209 debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),report,strerror(errno));
507460ae
FM
210 exit(EXIT_FAILURE);
211 }
9bd92830
FM
212 if(fp_user) {
213 fputs("</table>\n",fp_user);
214 if(ShowSargInfo) {
81a022d8 215 zdate(ftime, sizeof(ftime), df);
05a933cc
FM
216 fputs("<br><br><div align=\"center\"><font size=\"-2\">",fp_user);
217 fprintf(fp_user,_("Generated by <a href=\"%s\">%s-%s</a> on %s"),URL,PGM,VERSION,ftime);
218 fputs("</font></div>\n",fp_user);
9bd92830
FM
219 }
220 fputs("</body>\n</html>\n",fp_user);
507460ae 221 if (fclose(fp_user)==EOF) {
af961877 222 debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),smartuser,strerror(errno));
507460ae
FM
223 exit(EXIT_FAILURE);
224 }
9bd92830
FM
225 }
226
feae2658 227 if (!KeepTempLog && unlink(smart_ou)) {
af961877 228 debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),smart_ou,strerror(errno));
feae2658
FM
229 exit(EXIT_FAILURE);
230 }
231
9bd92830 232 return;
25697a35 233}