]> git.ipfire.org Git - thirdparty/sarg.git/blob - smartfilter.c
Indent the configure script for more readability.
[thirdparty/sarg.git] / smartfilter.c
1 /*
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
3 * 1998, 2015
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 cstatus;
50 struct getwordstruct gwarea;
51 const struct userinfostruct *uinfo;
52
53 ouser[0]='\0';
54
55 if (snprintf(smart_in,sizeof(smart_in),"%s/smartfilter.int_unsort",tmp)>=sizeof(smart_in)) {
56 debuga(__FILE__,__LINE__,_("Path too long: "));
57 debuga_more("%s/smartfilter.int_unsort\n",tmp);
58 exit(EXIT_FAILURE);
59 }
60 if (snprintf(sites,sizeof(sites),"%s/sarg-sites",outdirname)>=sizeof(sites)) {
61 debuga(__FILE__,__LINE__,_("Path too long: "));
62 debuga_more("%s/sarg-sites\n",outdirname);
63 exit(EXIT_FAILURE);
64 }
65 if (snprintf(smart_ou,sizeof(smart_ou),"%s/smartfilter.int_log",tmp)>=sizeof(smart_ou)) {
66 debuga(__FILE__,__LINE__,_("Path too long: "));
67 debuga_more("%s/smartfilter.int_log\n",tmp);
68 exit(EXIT_FAILURE);
69 }
70 if (snprintf(report,sizeof(report),"%s/smartfilter.html",outdirname)>=sizeof(report)) {
71 debuga(__FILE__,__LINE__,_("Path too long: "));
72 debuga_more("%s/smartfilter.html\n",outdirname);
73 exit(EXIT_FAILURE);
74 }
75
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)) {
77 debuga(__FILE__,__LINE__,_("Cannot build the sort command to sort file \"%s\"\n"),smart_in);
78 exit(EXIT_FAILURE);
79 }
80 cstatus=system(csort);
81 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
82 debuga(__FILE__,__LINE__,_("sort command return status %d\n"),WEXITSTATUS(cstatus));
83 debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
84 exit(EXIT_FAILURE);
85 }
86 if((fp_in=fopen(smart_ou,"r"))==NULL) {
87 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),smart_ou,strerror(errno));
88 debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
89 exit(EXIT_FAILURE);
90 }
91 if (!KeepTempLog && unlink(smart_in)) {
92 debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),smart_in,strerror(errno));
93 exit(EXIT_FAILURE);
94 }
95
96 if((fp_ou=fopen(report,"w"))==NULL) {
97 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),report,strerror(errno));
98 exit(EXIT_FAILURE);
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);
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"));
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 ||
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) {
126 debuga(__FILE__,__LINE__,_("Invalid record in file \"%s\"\n"),smart_ou);
127 exit(EXIT_FAILURE);
128 }
129
130 uinfo=userinfo_find_from_id(user);
131 if (!uinfo) {
132 debuga(__FILE__,__LINE__,_("Unknown user ID %s in file \"%s\"\n"),user,smart_ou);
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);
138 if (fp_user) {
139 fputs("</table>\n",fp_user);
140 if(ShowSargInfo) {
141 zdate(ftime, sizeof(ftime), df);
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);
145 }
146 fputs("</body>\n</html>\n",fp_user);
147 if (fclose(fp_user)==EOF) {
148 debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),smartuser,strerror(errno));
149 exit(EXIT_FAILURE);
150 }
151 fp_user=NULL;
152 }
153 if ((fp_user = fopen(smartuser, "a")) == 0) {
154 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),smartuser,strerror(errno));
155 exit(EXIT_FAILURE);
156 }
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);
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"));
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) {
200 zdate(ftime, sizeof(ftime), df);
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);
204 }
205
206 fputs("</body>\n</html>\n",fp_user);
207
208 if (fclose(fp_ou)==EOF) {
209 debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),report,strerror(errno));
210 exit(EXIT_FAILURE);
211 }
212 if(fp_user) {
213 fputs("</table>\n",fp_user);
214 if(ShowSargInfo) {
215 zdate(ftime, sizeof(ftime), df);
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);
219 }
220 fputs("</body>\n</html>\n",fp_user);
221 if (fclose(fp_user)==EOF) {
222 debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),smartuser,strerror(errno));
223 exit(EXIT_FAILURE);
224 }
225 }
226
227 if (!KeepTempLog && unlink(smart_ou)) {
228 debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),smart_ou,strerror(errno));
229 exit(EXIT_FAILURE);
230 }
231
232 return;
233 }