]>
Commit | Line | Data |
---|---|---|
25697a35 | 1 | /* |
c37945ed | 2 | * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com |
e3af0ae9 | 3 | * 1998, 2009 |
94ff9470 | 4 | * SARG Squid Analysis Report Generator http://sarg.sourceforge.net |
25697a35 GS |
5 | * |
6 | * SARG donations: | |
7 | * please look at http://sarg.sourceforge.net/donations.php | |
8 | * --------------------------------------------------------------------- | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation; either version 2 of the License, or | |
13 | * (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | * GNU General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with this program; if not, write to the Free Software | |
22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. | |
23 | * | |
24 | */ | |
25 | ||
26 | #include "include/conf.h" | |
5f3cfd1d | 27 | #include "include/defs.h" |
25697a35 | 28 | |
32e71fa4 | 29 | void smartfilter_report(void) |
25697a35 GS |
30 | { |
31 | ||
32 | FILE *fp_in = NULL, *fp_ou = NULL, *fp_user = NULL; | |
120d768c | 33 | |
25697a35 | 34 | char url[MAXLEN]; |
25697a35 GS |
35 | char csort[255]; |
36 | char smart_in[MAXLEN]; | |
37 | char smart_ou[MAXLEN]; | |
38 | char per[MAXLEN]; | |
39 | char sites[MAXLEN]; | |
40 | char report[MAXLEN]; | |
d6e703cc | 41 | char period[100]; |
25697a35 GS |
42 | char ip[MAXLEN]; |
43 | char user[MAXLEN]; | |
44 | char ouser[MAXLEN]; | |
45 | char data[15]; | |
46 | char hora[15]; | |
47 | char smartcat[256]; | |
48 | char smartheader[15]; | |
49 | char ftime[128]; | |
50 | char smartuser[MAXLEN]; | |
51 | char *str; | |
52 | int fuser=0; | |
456d78a5 | 53 | int cstatus; |
25697a35 GS |
54 | |
55 | ouser[0]='\0'; | |
56 | ||
57 | sprintf(smartheader,"%s",text[116]); | |
58 | strup(smartheader); | |
59 | ||
60 | sprintf(smart_in,"%s/smartfilter.unsort",dirname); | |
d6e703cc | 61 | sprintf(sites,"%s/sarg-sites",dirname); |
25697a35 | 62 | sprintf(smart_ou,"%s/smartfilter.log",dirname); |
d6e703cc | 63 | sprintf(per,"%s/sarg-period",dirname); |
25697a35 GS |
64 | sprintf(report,"%s/smartfilter.html",dirname); |
65 | ||
66 | if ((fp_in = fopen(per, "r")) == 0) { | |
67 | fprintf(stderr, "SARG: (smartfilter) %s: %s\n",text[45],per); | |
68 | exit(1); | |
69 | } | |
70 | ||
05b90947 FM |
71 | if (!fgets(period,sizeof(period),fp_in)) { |
72 | fprintf(stderr,"SARG: (smartfilter) read error in %s\n",per); | |
73 | exit(1); | |
74 | } | |
25697a35 GS |
75 | fclose(fp_in); |
76 | ||
05b90947 FM |
77 | 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)) { |
78 | fprintf(stderr,"SARG: cannot sort file %s\n",smart_in); | |
79 | exit(1); | |
80 | } | |
456d78a5 FM |
81 | cstatus=system(csort); |
82 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
83 | fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus)); | |
84 | fprintf(stderr, "SARG: sort command: %s\n",csort); | |
85 | exit(1); | |
86 | } | |
25697a35 | 87 | if((fp_in=fopen(smart_ou,"r"))==NULL) { |
456d78a5 FM |
88 | fprintf(stderr, "SARG: (smartfilter) %s: %s\n",text[8],smart_ou); |
89 | fprintf(stderr, "SARG: sort command: %s\n",csort); | |
90 | exit(1); | |
25697a35 | 91 | } |
456d78a5 | 92 | unlink(smart_in); |
25697a35 GS |
93 | |
94 | if((fp_ou=fopen(report,"w"))==NULL) { | |
95 | fprintf(stderr, "SARG: (smartfilter) %s: %s\n",text[8],report); | |
96 | exit(1); | |
97 | } | |
98 | ||
d6e703cc | 99 | 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 | 100 | fputs("</head>\n",fp_ou); |
d6e703cc | 101 | if(strlen(FontFace) > 0) fprintf(fp_ou,"<font face=%s>\n",FontFace); |
dfb337be FM |
102 | fprintf(fp_ou,"<body bgcolor=\"%s\" text=\"%s\" background=\"%s\">\n",BgColor,TxColor,BgImage); |
103 | fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ou); | |
104 | write_logo_image(fp_ou); | |
25697a35 | 105 | |
dfb337be FM |
106 | fprintf(fp_ou,"<tr><th align=\"center\"><b><font color=\"%s\" size=\"+1\">%s</font></b></th></tr>\n",TiColor,Title); |
107 | fprintf(fp_ou,"<tr><td align=\"center\" bgcolor=\"%s\"><font size=\"%s\">%s: %s</font></td></tr>\n",HeaderBgColor,FontSize,text[89],period); | |
108 | fprintf(fp_ou,"<tr><th bgcolor=\"%s\" align=\"center\"><font size=\"%s\">%s</font></th></tr>\n",HeaderBgColor,FontSize,text[116]); | |
109 | fputs("</table></div>\n",fp_ou); | |
25697a35 | 110 | |
dfb337be | 111 | fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"2\">\n",fp_ou); |
25697a35 GS |
112 | fputs("<tr><td></td></tr>\n",fp_ou); |
113 | fputs("<tr><td></td></tr>\n",fp_ou); | |
114 | fputs("<tr><td></td></tr>\n",fp_ou); | |
dfb337be | 115 | 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,text[98],HeaderBgColor,FontSize,text[111],HeaderBgColor,FontSize,text[110],HeaderBgColor,FontSize,text[91],HeaderBgColor,FontSize,smartheader); |
25697a35 GS |
116 | |
117 | while(fgets(buf,sizeof(buf),fp_in)!=NULL) { | |
120d768c FM |
118 | if (getword(user,sizeof(user),buf,'\t')<0 || getword(data,sizeof(data),buf,'\t')<0 || |
119 | getword(hora,sizeof(hora),buf,'\t')<0 || getword(ip,sizeof(ip),buf,'\t')<0 || | |
120 | getword(url,sizeof(url),buf,'\t')<0 || getword(smartcat,sizeof(smartcat),buf,'\n')<0) { | |
4bcb77cf FM |
121 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",smart_ou); |
122 | exit(1); | |
123 | } | |
25697a35 GS |
124 | |
125 | if((str=(char *) strstr(user, "_")) != (char *) NULL ) { | |
126 | if((str=(char *) strstr(str+1, "_")) != (char *) NULL ) | |
127 | fixip(user); | |
128 | } | |
129 | ||
130 | if(strcmp(ouser,user) != 0) { | |
131 | strcpy(ouser,user); | |
132 | sprintf(smartuser,"%s/denied_%s.html",dirname,user); | |
133 | if(fuser) { | |
134 | fuser=0; | |
135 | fputs("</table>\n",fp_user); | |
136 | if(strcmp(ShowSargInfo,"yes") == 0) { | |
120d768c | 137 | zdate(ftime, sizeof(ftime), DateFormat); |
dfb337be | 138 | fprintf(fp_user,"<br><br><div align=\"center\"><font size=\"-2\">%s <a href=\"%s\">%s-%s</a> %s %s</font></div>\n",text[108],URL,PGM,VERSION,text[109],ftime); |
354c1a68 | 139 | } |
25697a35 GS |
140 | fputs("</body>\n</html>\n",fp_user); |
141 | fclose(fp_user); | |
142 | } | |
143 | if ((fp_user = fopen(smartuser, "a")) == 0) { | |
144 | fprintf(stderr, "SARG: (smartfilter) %s: %s\n",text[45],smartuser); | |
145 | exit(1); | |
146 | } | |
147 | fuser=1; | |
148 | ||
149 | fputs("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"",fp_ou); | |
150 | fputs(" \"http://www.w3.org/TR/html4/loose.dtd\">\n",fp_ou); | |
151 | fputs("<html>\n",fp_user); | |
152 | fputs("<head>\n",fp_user); | |
354c1a68 | 153 | fprintf(fp_user," <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet); |
25697a35 GS |
154 | fputs("</head>\n",fp_user); |
155 | ||
120d768c | 156 | if(FontFace[0] != 0) { |
354c1a68 FM |
157 | /* |
158 | Before merging the sprintf and the fputs, the code looked like this: | |
25697a35 GS |
159 | sprintf(html2,"<font face=%s>\n",FontFace); |
160 | fputs(url,fp_user); | |
354c1a68 FM |
161 | The two lines don't use the same buffer so the string formated by sprintf is not the string |
162 | written to fp_user. I (fmarchal) assumed it was a typo and replaced it by a fprintf but | |
163 | that font tag is not valid outside of the body. So, the generated html was likely | |
164 | containing garbage not rendered by the browser. | |
165 | */ | |
166 | fprintf(fp_user,"<font face=%s>\n",FontFace); | |
25697a35 | 167 | } |
dfb337be FM |
168 | fprintf(fp_user,"<body bgcolor=\"%s\" text=\"%s\" background=\"%s\">\n",BgColor,TxColor,BgImage); |
169 | fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_user); | |
170 | 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); | |
171 | fprintf(fp_user,"<tr><th align=\"center\"><b><font color=\"%s\" size=\"+1\">%s</font></b></th></tr>\n",TiColor,Title); | |
d6e703cc FM |
172 | fprintf(fp_user,"<tr><td align=center bgcolor=%s><font size=%s>%s: %s</font></td></tr>\n",HeaderBgColor,FontSize,text[89],period); |
173 | fprintf(fp_user,"<tr><td align=center bgcolor=%s><font size=%s>%s:</font><font size=%s> %s</font></td></tr>\n",HeaderBgColor,FontSize,text[90],FontSize,user); | |
dfb337be FM |
174 | fputs("</table></div>\n",fp_user); |
175 | fputs("<div align=\"center\"><table cellpadding=0 cellspacing=2>\n",fp_user); | |
25697a35 GS |
176 | fputs("<tr><td></td></tr>\n",fp_user); |
177 | fputs("<tr><td></td></tr>\n",fp_user); | |
178 | fputs("<tr><td></td></tr>\n",fp_user); | |
354c1a68 | 179 | 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,text[98],HeaderBgColor,FontSize,text[111],HeaderBgColor,FontSize,text[110],HeaderBgColor,FontSize,text[91],HeaderBgColor,FontSize,smartheader); |
25697a35 | 180 | } |
354c1a68 | 181 | 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,user,TxBgColor,FontSize,ip,TxBgColor,FontSize,data,hora,TxBgColor,FontSize,url,TxBgColor,FontSize,smartcat); |
25697a35 | 182 | |
354c1a68 | 183 | 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,user,TxBgColor,FontSize,ip,TxBgColor,FontSize,data,hora,TxBgColor,FontSize,url,TxBgColor,FontSize,smartcat); |
25697a35 GS |
184 | } |
185 | ||
186 | fputs("</table>\n",fp_ou); | |
187 | ||
188 | if(strcmp(ShowSargInfo,"yes") == 0) { | |
120d768c | 189 | zdate(ftime, sizeof(ftime), DateFormat); |
dfb337be | 190 | fprintf(fp_ou,"<br><br><div align=\"center\"><font size=\"-2\">%s <a href=\"%s\">%s-%s</a> %s %s</font></div>\n",text[108],URL,PGM,VERSION,text[109],ftime); |
25697a35 GS |
191 | } |
192 | ||
193 | fputs("</body>\n</html>\n",fp_user); | |
194 | ||
195 | fclose(fp_ou); | |
196 | if(fp_user) { | |
197 | fputs("</table>\n",fp_user); | |
198 | if(strcmp(ShowSargInfo,"yes") == 0) { | |
120d768c | 199 | zdate(ftime, sizeof(ftime), DateFormat); |
dfb337be | 200 | fprintf(fp_user,"<br><br><div align=\"center\"><font size=\"-2\">%s <a href=\"%s\">%s-%s</a> %s %s</font></div>\n",text[108],URL,PGM,VERSION,text[109],ftime); |
25697a35 GS |
201 | } |
202 | fputs("</body>\n</html>\n",fp_user); | |
203 | fclose(fp_user); | |
204 | } | |
205 | ||
206 | return; | |
207 | } |