]> git.ipfire.org Git - thirdparty/sarg.git/blob - smartfilter.c
Merge commit 'd5db6e96506c29e73b39513a'
[thirdparty/sarg.git] / smartfilter.c
1 /*
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
3 * 1998, 2011
4 *
5 * SARG donations:
6 * please look at http://sarg.sourceforge.net/donations.php
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
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"
28 #include "include/defs.h"
29
30 void smartfilter_report(void)
31 {
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 ftime[128];
48 char smartuser[MAXLEN];
49 int fuser=0;
50 int cstatus;
51 struct getwordstruct gwarea;
52 const struct userinfostruct *uinfo;
53
54 ouser[0]='\0';
55
56 if (snprintf(smart_in,sizeof(smart_in),"%s/smartfilter.unsort",outdirname)>=sizeof(smart_in)) {
57 debuga(_("File name too long: %s/smartfilter.unsort\n"),outdirname);
58 exit(EXIT_FAILURE);
59 }
60 if (snprintf(sites,sizeof(sites),"%s/sarg-sites",outdirname)>=sizeof(sites)) {
61 debuga(_("File name too long: %s/sarg-sites\n"),outdirname);
62 exit(EXIT_FAILURE);
63 }
64 if (snprintf(smart_ou,sizeof(smart_ou),"%s/smartfilter.log",outdirname)>=sizeof(smart_ou)) {
65 debuga(_("File name too long: %s/smartfilter.log\n"),outdirname);
66 exit(EXIT_FAILURE);
67 }
68 if (snprintf(report,sizeof(report),"%s/smartfilter.html",outdirname)>=sizeof(report)) {
69 debuga(_("File name too long: %s/smartfilter.html\n"),outdirname);
70 exit(EXIT_FAILURE);
71 }
72
73 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)) {
74 debuga(_("cannot build the sort command to sort file %s\n"),smart_in);
75 exit(EXIT_FAILURE);
76 }
77 cstatus=system(csort);
78 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
79 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
80 debuga(_("sort command: %s\n"),csort);
81 exit(EXIT_FAILURE);
82 }
83 if((fp_in=fopen(smart_ou,"r"))==NULL) {
84 debuga(_("(smartfilter) Cannot open log file %s\n"),smart_ou);
85 debuga(_("sort command: %s\n"),csort);
86 exit(EXIT_FAILURE);
87 }
88 if (unlink(smart_in)) {
89 debuga(_("Cannot delete %s - %s\n"),smart_in,strerror(errno));
90 exit(EXIT_FAILURE);
91 }
92
93 if((fp_ou=fopen(report,"w"))==NULL) {
94 debuga(_("(smartfilter) Cannot open log file %s\n"),report);
95 exit(EXIT_FAILURE);
96 }
97
98 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);
99 fputs("</head>\n",fp_ou);
100 if(strlen(FontFace) > 0) fprintf(fp_ou,"<font face=%s>\n",FontFace);
101 fprintf(fp_ou,"<body bgcolor=\"%s\" text=\"%s\" background=\"%s\">\n",BgColor,TxColor,BgImage);
102 fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ou);
103 write_logo_image(fp_ou);
104
105 fprintf(fp_ou,"<tr><th align=\"center\"><b><font color=\"%s\" size=\"+1\">%s</font></b></th></tr>\n",TiColor,Title);
106 fprintf(fp_ou,"<tr><td align=\"center\" bgcolor=\"%s\"><font size=\"%s\">",HeaderBgColor,FontSize);
107 fprintf(fp_ou,_("Period: %s"),period.html);
108 fputs("</font></td></tr>\n",fp_ou);
109 fprintf(fp_ou,"<tr><th bgcolor=\"%s\" align=\"center\"><font size=\"%s\">%s</font></th></tr>\n",HeaderBgColor,FontSize,_("SmartFilter"));
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,_("USERID"),HeaderBgColor,FontSize,_("IP/NAME"),HeaderBgColor,FontSize,_("DATE/TIME"),HeaderBgColor,FontSize,_("ACCESSED SITE"),HeaderBgColor,FontSize,_("SMARTFILTER"));
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 debuga(_("There is a broken record or garbage in file %s\n"),smart_ou);
124 exit(EXIT_FAILURE);
125 }
126
127 uinfo=userinfo_find_from_id(user);
128 if (!uinfo) {
129 debuga(_("Unknown user ID %s in file %s\n"),user,smart_ou);
130 exit(EXIT_FAILURE);
131 }
132 if(strcmp(ouser,user) != 0) {
133 strcpy(ouser,user);
134 sprintf(smartuser,"%s/denied_%s.html",outdirname,uinfo->filename);
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",_("Generated by"),URL,PGM,VERSION,_("on"),ftime);
141 }
142 fputs("</body>\n</html>\n",fp_user);
143 fclose(fp_user);
144 }
145 if ((fp_user = fopen(smartuser, "a")) == 0) {
146 debuga(_("(smartfilter) Cannot open file %s\n"),smartuser);
147 exit(EXIT_FAILURE);
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 fputs("<tr><td align=center bgcolor=\"%s\"><font size=\"%s\">",fp_user);
175 fprintf(fp_user,_("Period: %s"),period.html);
176 fputs("</font></td></tr>\n",fp_user);
177 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);
178 fputs("</table></div>\n",fp_user);
179 fputs("<div align=\"center\"><table cellpadding=0 cellspacing=2>\n",fp_user);
180 fputs("<tr><td></td></tr>\n",fp_user);
181 fputs("<tr><td></td></tr>\n",fp_user);
182 fputs("<tr><td></td></tr>\n",fp_user);
183 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"));
184 }
185 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);
186
187 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);
188 }
189
190 fputs("</table>\n",fp_ou);
191
192 if(ShowSargInfo) {
193 zdate(ftime, sizeof(ftime), DateFormat);
194 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);
195 }
196
197 fputs("</body>\n</html>\n",fp_user);
198
199 fclose(fp_ou);
200 if(fp_user) {
201 fputs("</table>\n",fp_user);
202 if(ShowSargInfo) {
203 zdate(ftime, sizeof(ftime), DateFormat);
204 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);
205 }
206 fputs("</body>\n</html>\n",fp_user);
207 fclose(fp_user);
208 }
209
210 return;
211 }