]>
git.ipfire.org Git - thirdparty/sarg.git/blob - smartfilter.c
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
6 * please look at http://sarg.sourceforge.net/donations.php
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
9 * ---------------------------------------------------------------------
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.
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.
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.
27 #include "include/conf.h"
28 #include "include/defs.h"
30 void smartfilter_report ( void )
32 FILE * fp_in
= NULL
, * fp_ou
= NULL
, * fp_user
= NULL
;
37 char smart_in
[ MAXLEN
];
38 char smart_ou
[ MAXLEN
];
48 char smartuser
[ MAXLEN
];
50 struct getwordstruct gwarea
;
51 const struct userinfostruct
* uinfo
;
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
);
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
);
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
);
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
);
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
);
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
);
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
);
91 if (! KeepTempLog
&& unlink ( smart_in
)) {
92 debuga ( __FILE__
, __LINE__
, _ ( "Cannot delete \" %s \" : %s \n " ), smart_in
, strerror ( errno
));
96 if (( fp_ou
= fopen ( report
, "w" ))== NULL
) {
97 debuga ( __FILE__
, __LINE__
, _ ( "Cannot open file \" %s \" : %s \n " ), report
, strerror ( errno
));
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
);
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
);
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" ));
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
);
130 uinfo
= userinfo_find_from_id ( user
);
132 debuga ( __FILE__
, __LINE__
, _ ( "Unknown user ID %s in file \" %s \"\n " ), user
, smart_ou
);
135 if ( strcmp ( ouser
, user
) != 0 ) {
137 sprintf ( smartuser
, "%s/denied_%s.html" , outdirname
, uinfo
-> filename
);
139 fputs ( "</table> \n " , fp_user
);
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
);
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
));
153 if (( fp_user
= fopen ( smartuser
, "a" )) == 0 ) {
154 debuga ( __FILE__
, __LINE__
, _ ( "Cannot open file \" %s \" : %s \n " ), smartuser
, strerror ( errno
));
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
);
165 if ( FontFace
[ 0 ] != 0 ) {
167 Before merging the sprintf and the fputs, the code looked like this:
168 sprintf(html2,"<font face=%s>\n",FontFace);
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.
175 fprintf ( fp_user
, "<font face=%s> \n " , FontFace
);
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" ));
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
);
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
);
197 fputs ( "</table> \n " , fp_ou
);
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
);
206 fputs ( "</body> \n </html> \n " , fp_user
);
208 if ( fclose ( fp_ou
)== EOF
) {
209 debuga ( __FILE__
, __LINE__
, _ ( "Write error in \" %s \" : %s \n " ), report
, strerror ( errno
));
213 fputs ( "</table> \n " , fp_user
);
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
);
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
));
227 if (! KeepTempLog
&& unlink ( smart_ou
)) {
228 debuga ( __FILE__
, __LINE__
, _ ( "Cannot delete \" %s \" : %s \n " ), smart_ou
, strerror ( errno
));