]>
git.ipfire.org Git - thirdparty/sarg.git/blob - report.c
2 * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
4 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
7 * please look at http://sarg.sourceforge.net/donations.php
8 * ---------------------------------------------------------------------
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.
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.
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.
26 #include "include/conf.h"
27 #include "include/defs.h"
34 char accdia
[ 11 ], acchora
[ 9 ], accuser
[ MAXLEN
], accip
[ MAXLEN
], accurl
[ MAXLEN
], accbytes
[ 12 ], accelap
[ 10 ];
35 char oldaccdia
[ 11 ], oldacchora
[ 9 ], oldaccip
[ MAXLEN
], wdir
[ MAXLEN
], per1
[ MAXLEN
];
36 char wdirname
[ MAXLEN
], oldurl
[ MAXLEN
], oldaccuser
[ MAXLEN
];
37 char olduser
[ MAXLEN
], oldmsg
[ 50 ], acccode
[ 50 ], oldaccelap
[ 10 ], oldacccode
[ 50 ], user
[ MAXLEN
];
38 char ipantes
[ MAXLEN
], nameantes
[ MAXLEN
], wdname
[ MAXLEN
], wname2
[ MAXLEN
];
39 char accsmart
[ MAXLEN
];
43 long long int nbytes
= 0 ;
44 long long int nelap
= 0 ;
46 long long int rtotal
= 0 ;
47 long long int incache
= 0 ;
48 long long int oucache
= 0 ;
51 struct dirent
* direntp
;
57 sprintf ( dirname
, "%s%s" , outdir
, periodo
);
58 sprintf ( wdir
, "%s%s" , outdir
, periodo
);
60 vrfydir ( wdir
, per1
, addr
, site
, us
, email
);
63 debugaz ( "dirname" , dirname
);
67 strcpy ( wdirname
, dirname
);
70 if ( strlen ( UserAgentLog
) > 0 && email
[ 0 ] == '\0' )
74 strncat ( tmp
, "/sarg" , 5 );
77 while (( direntp
= readdir ( dirp
)) != NULL
) {
78 if ( strstr ( direntp
-> d_name
, ".log" ) == 0 )
80 if ( strncmp ( direntp
-> d_name
, "download.log" , 12 ) == 0 )
82 if ( strncmp ( direntp
-> d_name
, "denied.log" , 10 ) == 0 )
84 if ( strncmp ( direntp
-> d_name
, "authfail.log.unsort" , 19 ) == 0 )
86 sprintf ( tmp3
, "%s/%s" , tmp
, direntp
-> d_name
);
87 if (( fp_in
= fopen ( tmp3
, "r" ))== NULL
){
88 fprintf ( stderr
, "SARG: (report) %s: %s \n " , text
[ 45 ], tmp
);
92 strcpy ( wdname
, direntp
-> d_name
);
94 getword ( wname2
, wdname
, '.' );
97 if ( strcmp ( wdname
, "log" ) != 0 ) {
102 strcpy ( wdirname
, dirname
);
103 maketmp ( user
, tmp
, debug
, indexonly
);
104 maketmp_hour ( user
, tmp
, indexonly
);
107 bzero ( html_old
, MAXLEN
);
109 while ( fgets ( buf
, sizeof ( buf
), fp_in
)!= NULL
) {
110 getword ( accdia
, buf
, ' ' );
111 getword ( acchora
, buf
, ' ' );
112 getword ( accuser
, buf
, ' ' );
113 getword ( accip
, buf
, ' ' );
114 getword ( accurl
, buf
, ' ' );
115 getword ( accbytes
, buf
, ' ' );
116 getword ( acccode
, buf
, ' ' );
117 if ( strncmp ( acccode
, "TCP_DENIED/407" , 14 ) == 0 )
119 getword ( accelap
, buf
, ' ' );
120 getword ( accsmart
, buf
, ' ' );
121 getword ( accsmart
, buf
, '"' );
123 if ( strlen ( accsmart
) > 0 ) {
125 strcpy ( wdirname
, dirname
);
126 grava_SmartFilter ( wdirname
, accuser
, accip
, accdia
, acchora
, accurl
, accsmart
);
129 if ( strcmp ( Ip2Name
, "yes" ) == 0 ) {
130 if ( strcmp ( accip
, ipantes
) != 0 ) {
131 strcpy ( ipantes
, accip
);
133 strcpy ( nameantes
, accip
);
134 } else strcpy ( accip
, nameantes
);
137 strcpy ( wdirname
, dirname
);
138 gravatmp_hora ( wdirname
, accuser
, accdia
, acchora
, accelap
, accbytes
, indexonly
);
141 strcpy ( wdirname
, dirname
);
142 gravaporuser ( accuser
, wdirname
, accurl
, accip
, accdia
, acchora
, accbytes
, accelap
, indexonly
);
146 strcpy ( oldurl
, accurl
);
147 strcpy ( oldacccode
, acccode
);
148 strcpy ( oldaccelap
, accelap
);
149 strcpy ( oldaccuser
, accuser
);
150 strcpy ( oldaccip
, accip
);
151 strcpy ( oldaccdia
, accdia
);
152 strcpy ( oldacchora
, acchora
);
156 if ( site
[ 0 ] != '\0' ) {
157 if ( strcmp ( oldaccuser
, accuser
) != 0 ){
159 if ( strstr ( oldacccode
, "DENIED" ) != 0 )
160 sprintf ( oldmsg
, "%s" , text
[ 46 ]);
161 gravatmp ( oldaccuser
, wdirname
, oldurl
, nacc
, nbytes
, oldmsg
, nelap
, indexonly
, incache
, oucache
);
162 gravager ( wdirname
, oldaccuser
, nacc
, oldurl
, nbytes
, oldaccip
, oldacchora
, oldaccdia
, nelap
,
171 if ( strcmp ( oldurl
, accurl
) != 0 || strcmp ( oldaccuser
, accuser
) != 0 ){
173 if ( strstr ( oldacccode
, "DENIED" ) != 0 )
174 sprintf ( oldmsg
, "%s" , text
[ 46 ]);
175 strcpy ( wdirname
, dirname
);
176 gravatmp ( oldaccuser
, wdirname
, oldurl
, nacc
, nbytes
, oldmsg
, nelap
, indexonly
, incache
, oucache
);
177 strcpy ( wdirname
, dirname
);
178 gravager ( wdirname
, oldaccuser
, nacc
, oldurl
, nbytes
, oldaccip
, oldacchora
, oldaccdia
, nelap
, incache
, oucache
);
184 if ( strcmp ( oldaccuser
, accuser
) != 0 )
189 nbytes
+= my_atoll ( accbytes
);
190 nelap
+= my_atoll ( accelap
);
192 if ( strstr ( ReportType
, "site_user_time_date" ) != 0 ) {
195 strcpy ( siteind
, accurl
);
197 for ( z1
= 0 ; z1
< strlen ( str
); z1
++) {
198 if ( str
[ z1
]== '?' || str
[ z1
]== '-' || str
[ z1
]== '.' || str
[ z1
]== ':' || str
[ z1
]== '/' || str
[ z1
]== ' \\ ' )
201 sprintf ( arqtt
, "%s/%s" , dirname
, accuser
);
202 if ( access ( arqtt
, R_OK
) != 0 )
204 sprintf ( arqtt
, "%s/%s/tt%s-%s.html" , dirname
, accuser
, accuser
, siteind
);
205 if ( strlen ( arqtt
) > 255 ) {
206 strncpy ( val7
, arqtt
, 255 );
210 if (( fp_tt
= fopen ( arqtt
, "w" )) == 0 ) {
211 fprintf ( stderr
, "SARG: (report) %s: %s \n " , text
[ 45 ], arqtt
);
216 if ( strcmp ( Privacy
, "yes" ) == 0 )
217 sprintf ( httplink
, "<font size=%s color=%s><href=http://%s>%s" , \
218 FontSize
, PrivacyStringColor
, PrivacyString
, PrivacyString
);
220 sprintf ( httplink
, "<font size=%s><a href=http://%s>%s</A>" , FontSize
, accurl
, accurl
);
222 sprintf ( ltext110
, "%s" , text
[ 110 ]);
224 for ( s
= ltext110
; * s
; ++ s
)
228 fputs ( "<!DOCTYPE HTML PUBLIC \" -//W3C//DTD HTML 4.01 Transitional//EN \" " , fp_tt
);
229 fputs ( " \" http://www.w3.org/TR/html4/loose.dtd \" > \n " , fp_tt
);
230 fputs ( "<html> \n " , fp_tt
);
231 fputs ( "<head> \n " , fp_tt
);
232 sprintf ( html
, " <meta http-equiv= \" Content-Type \" content= \" text/html; charset=%s \" > \n " , CharSet
);
235 fputs ( "</head> \n " , fp_tt
);
237 sprintf ( html
, "<body bgcolor=%s text=%s background='%s'> \n " , BgColor
, TxColor
, BgImage
);
240 fputs ( "<center><table cellpadding=0 cellspacing=0> \n " , fp_tt
);
242 if ( strlen ( LogoImage
) > 0 ) {
243 fputs ( "<center><table cellpadding= \" 0 \" cellspacing= \" 0 \" > \n " , fp_tt
);
244 sprintf ( html
, "<tr><th class= \" logo \" ><img src='%s' border=0 align=absmiddle width=%s height=%s> %s</th></tr> \n " , LogoImage
, Width
, Height
, LogoText
);
246 fputs ( "<tr><td height= \" 5 \" ></td></tr> \n " , fp_tt
);
247 fputs ( "</table> \n " , fp_tt
);
250 if ( strcmp ( IndexTree
, "date" ) == 0 )
251 show_sarg ( fp_tt
, "../../../.." );
253 show_sarg ( fp_tt
, "../.." );
255 sprintf ( html
, "<tr><th class= \" title \" colspan= \" 2 \" >%s</th></tr> \n " , Title
);
259 if ( strcmp ( Ip2Name
, "yes" ) == 0 )
261 if ( UserTabFile
[ 0 ] != '\0' ) {
262 sprintf ( warea
, ":%s:" , u2
);
263 if (( str
=( char *) strstr ( userfile
, warea
)) != ( char *) NULL
) {
265 str2
=( char *) strstr ( str
+ 1 , ":" );
268 while ( str2
[ z1
] != ':' ) {
272 } else strcpy ( name
, u2
);
273 } else strcpy ( name
, u2
);
275 if ( dotinuser
&& strstr ( name
, "_" )) {
276 str2
=( char *) subs ( name
, "_" , "." );
280 sprintf ( html
, "<tr><td class= \" header \" colspan= \" 2 \" >%s: %s</td></tr> \n " , text
[ 89 ], periodo
);
282 sprintf ( html
, "<tr><td class= \" header \" colspan= \" 2 \" >%s: %s</td></tr> \n " , text
[ 90 ], name
);
284 sprintf ( html
, "<tr><td class= \" header \" colspan= \" 2 \" >%s: %s, %s</td></tr> \n " , text
[ 104 ], UserSortField
, UserSortOrder
);
286 sprintf ( html
, "<tr><td class= \" header3 \" colspan=2>%s %s</td></tr> \n " , text
[ 32 ], text
[ 55 ]);
289 fputs ( "</table></center> \n " , fp_tt
);
290 fputs ( "<center><table cellpadding= \" 0 \" cellspacing= \" 2 \" > \n " , fp_tt
);
291 fputs ( "<tr><td></td><td></td></tr>" , fp_tt
);
294 strncpy ( tmp4
, text
[ 110 ], 4 );
295 strncpy ( tmp5
, text
[ 110 ]+ 5 , 4 );
296 sprintf ( html
, "<tr><th class= \" data \" >%s</th><th class= \" data \" >%s</th><th class= \" data \" >%s</th></tr> \n " , text
[ 91 ], tmp4
, tmp5
);
300 sprintf ( html
, "<tr><td class= \" data \" >%s</td><td class= \" data \" >%s</td><td class= \" data \" >%s</td></tr> \n " , accurl
, accdia
, acchora
);
302 if ( strcmp ( html
, html_old
) != 0 )
304 strcpy ( html_old
, html
);
309 strcpy ( wcrc
, acccode
);
310 getword ( crc2
, wcrc
, '/' );
312 if ( strstr ( crc2
, "MISS" ) != 0 )
313 oucache
+= my_atoll ( accbytes
);
314 else incache
+= my_atoll ( accbytes
);
316 strcpy ( oldurl
, accurl
);
318 if ( strcmp ( accuser
, oldaccuser
) != 0 ) {
319 strcpy ( wdirname
, dirname
);
320 totaliza_day ( tmp
, oldaccuser
, indexonly
);
323 strcpy ( oldaccuser
, accuser
);
324 strcpy ( oldacccode
, acccode
);
325 strcpy ( oldaccelap
, accelap
);
326 strcpy ( oldaccip
, accip
);
327 strcpy ( oldaccdia
, accdia
);
328 strcpy ( oldacchora
, acchora
);
337 if ( strstr ( oldacccode
, "DENIED" ) != 0 )
338 sprintf ( oldmsg
, "%s" , text
[ 46 ]);
339 strcpy ( wdirname
, dirname
);
340 gravatmpf ( oldaccuser
, wdirname
, oldurl
, nacc
, nbytes
, oldmsg
, nelap
, indexonly
, incache
, oucache
);
341 strcpy ( wdirname
, dirname
);
342 gravager ( wdirname
, oldaccuser
, nacc
, oldurl
, nbytes
, oldaccip
, oldacchora
, oldaccdia
, nelap
, incache
, oucache
);
343 strcpy ( wdirname
, dirname
);
344 totaliza_day ( tmp
, oldaccuser
, indexonly
);
348 strcpy ( wdirname
, dirname
);
349 totalger ( wdirname
, debug
, outdir
);
351 if ( strlen ( email
) == 0 ) {
352 if ( strstr ( ReportType
, "downloads" ) != 0 )
355 if ( strlen ( DansGuardianConf
) > 0 ) {
356 strcpy ( wdirname
, dirname
);
360 strcpy ( wdirname
, dirname
);
363 strcpy ( wdirname
, dirname
);
366 if ( strstr ( ReportType
, "topsites" ) != 0 )
369 if ( strstr ( ReportType
, "sites_users" ) != 0 )
373 strcpy ( wdirname
, dirname
);
377 smartfilter_report ();
379 if ( strlen ( DansGuardianConf
) > 0 )
380 dansguardian_report ();
384 if ( strstr ( ReportType
, "users_sites" ) != 0 )
389 if ( strncmp ( SuccessfulMsg
, "yes" , 3 ) == 0 )
390 fprintf ( stderr
, "SARG: %s %s \n " , text
[ 47 ], dirname
);
392 strcpy ( wdirname
, dirname
);
393 geramail ( wdirname
, debug
, outdir
, userip
, email
, TempDir
);
395 if ( strcmp ( email
, "stdout" ) != 0 ) {
396 if ( strncmp ( SuccessfulMsg
, "yes" , 3 ) == 0 )
397 fprintf ( stderr
, "SARG: %s %s \n " , text
[ 48 ], email
);
402 strcpy ( wdirname
, dirname
);
403 index_only ( wdirname
, debug
);
412 void maketmp ( char * user
, char * dirname
, int debug
, int indexonly
)
417 char wdirname
[ MAXLEN
];
419 if ( indexonly
) return ;
420 if ( strstr ( ReportType
, "users_sites" ) == 0 ) return ;
422 strcpy ( wdirname
, tmp
);
423 strcat ( wdirname
, "/" );
424 strcat ( wdirname
, user
);
427 sprintf ( msg
, "%s: %s" , text
[ 49 ], wdirname
);
431 strcat ( wdirname
, ".utmp" );
432 if (( fp_ou
= fopen ( wdirname
, "w" ))== NULL
){
433 fprintf ( stderr
, "SARG: (report) %s: %s \n " , text
[ 45 ], wdirname
);
442 void maketmp_hour ( char * user
, char * dirname
, int indexonly
)
447 char wdirname
[ MAXLEN
];
449 if ( indexonly
) return ;
450 if ( strstr ( ReportType
, "users_sites" ) == 0 ) return ;
452 strcpy ( wdirname
, tmp
);
453 strcat ( wdirname
, "/" );
454 strcat ( wdirname
, user
);
456 strcat ( wdirname
, ".htmp" );
457 if (( fp_ou
= fopen ( wdirname
, "w" ))== NULL
){
458 fprintf ( stderr
, "SARG: (report-1) %s: %s - %s \n " , text
[ 45 ], wdirname
, strerror ( errno
));
467 void gravatmp ( char * oldaccuser
, char * dirname
, char * oldurl
, long long int nacc
, long long int nbytes
, char * oldmsg
, long long int nelap
, int indexonly
, long long int incache
, long long int oucache
)
473 char wdirname
[ MAXLEN
];
475 if ( indexonly
) return ;
476 if ( strstr ( ReportType
, "users_sites" ) == 0 ) return ;
478 strcpy ( wdirname
, tmp
);
479 strcat ( wdirname
, "/" );
480 strcat ( wdirname
, oldaccuser
);
481 strcat ( wdirname
, ".utmp" );
483 if (( fp_ou
= fopen ( wdirname
, "a" ))== NULL
){
484 fprintf ( stderr
, "SARG: (report) %s: %s \n " , text
[ 45 ], wdirname
);
488 my_lltoa ( nacc
, val1
, 15 );
489 my_lltoa ( nbytes
, val2
, 15 );
490 my_lltoa ( nelap
, val3
, 15 );
491 my_lltoa ( incache
, val4
, 15 );
492 my_lltoa ( oucache
, val5
, 15 );
493 sprintf ( reg
, "%s %s %s %s %s %s %s \n " , val1
, val2
, oldurl
, oldmsg
, val3
, val4
, val5
);
500 fputs ( "</table> \n " , fp_tt
);
501 fputs ( "</body> \n </html> \n " , fp_tt
);
510 void gravatmp_hora ( char * dirname
, char * user
, char * data
, char * hora
, char * elap
, char * bytes
, int indexonly
)
516 char wdirname
[ MAXLEN
];
518 if ( indexonly
) return ;
519 if ( strstr ( ReportType
, "users_sites" ) == 0 ) return ;
521 strcpy ( wdirname
, tmp
);
522 strcat ( wdirname
, "/" );
523 strcat ( wdirname
, user
);
524 strcat ( wdirname
, ".htmp" );
526 if (( fp_ou
= fopen ( wdirname
, "a" ))== NULL
){
527 fprintf ( stderr
, "SARG: (report-2) %s: %s - %s \n " , text
[ 45 ], wdirname
, strerror ( errno
));
531 if ( strcmp ( datetimeby
, "bytes" ) == 0 )
532 sprintf ( reg
, "%s %s %s \n " , data
, hora
, bytes
);
533 else sprintf ( reg
, "%s %s %s \n " , data
, hora
, elap
);
542 void gravaporuser ( char * user
, char * dirname
, char * url
, char * ip
, char * data
, char * hora
, char * tam
, char * elap
, int indexonly
)
548 char wdirname
[ MAXLEN
];
550 if ( indexonly
) return ;
551 if ( strstr ( ReportType
, "users_sites" ) == 0 ) return ;
553 strcpy ( wdirname
, tmp
);
554 strcat ( wdirname
, "/" );
555 strcat ( wdirname
, user
);
556 strcat ( wdirname
, ".ip" );
558 if (( fp_ou
= fopen ( wdirname
, "a" ))== NULL
){
559 fprintf ( stderr
, "SARG: (report) %s: %s \n " , text
[ 45 ], wdirname
);
563 sprintf ( reg
, "%s %s %s %s %s %s \n " , ip
, url
, data
, hora
, tam
, elap
);
573 void gravatmpf ( char * oldaccuser
, char * dirname
, char * oldurl
, long long int nacc
, long long int nbytes
, char * oldmsg
, long long int nelap
, int indexonly
, long long int incache
, long long int oucache
)
579 char wdirname
[ MAXLEN
];
581 if ( indexonly
) return ;
582 if ( strstr ( ReportType
, "users_sites" ) == 0 ) return ;
584 strcpy ( wdirname
, tmp
);
585 strcat ( wdirname
, "/" );
586 strcat ( wdirname
, oldaccuser
);
587 strcat ( wdirname
, ".utmp" );
589 if (( fp_ou
= fopen ( wdirname
, "a" ))== NULL
){
590 fprintf ( stderr
, "SARG: (report) %s: %s \n " , text
[ 45 ], wdirname
);
594 my_lltoa ( nacc
, val1
, 15 );
595 my_lltoa ( nbytes
, val2
, 15 );
596 my_lltoa ( nelap
, val3
, 15 );
597 my_lltoa ( incache
, val4
, 15 );
598 my_lltoa ( oucache
, val5
, 15 );
599 sprintf ( reg
, "%s %s %s %s %s %s %s \n " , val1
, val2
, oldurl
, oldmsg
, val3
, val4
, val5
);
607 fputs ( "</table> \n " , fp_tt
);
608 fputs ( "</html> \n " , fp_tt
);
617 void gravager ( char * dirname
, char * user
, long long int nacc
, char * url
, long long int nbytes
, char * ip
, char * hora
, char * dia
, long long int nelap
, long long int incache
, long long int oucache
)
624 strcat ( dirname
, "geral" );
626 if (( fp_ou
= fopen ( dirname
, "a" ))== NULL
){
627 fprintf ( stderr
, "SARG: (report) %s: %s \n " , text
[ 45 ], dirname
);
631 my_lltoa ( nacc
, val1
, 15 );
632 my_lltoa ( nbytes
, val2
, 15 );
633 my_lltoa ( nelap
, val3
, 15 );
634 my_lltoa ( incache
, val4
, 15 );
635 my_lltoa ( oucache
, val5
, 15 );
636 sprintf ( reg
, "%s %s %s %s %s %s %s %s %s %s \n " , user
, val1
, val2
, url
, ip
, hora
, dia
, val3
, val4
, val5
);
644 void grava_SmartFilter ( char * dirname
, char * user
, char * ip
, char * data
, char * hora
, char * url
, char * smart
)
650 char wdirname
[ MAXLEN
];
652 sprintf ( wdirname
, "%s/smartfilter.unsort" , dirname
);
654 if (( fp_ou
= fopen ( wdirname
, "a" ))== NULL
){
655 fprintf ( stderr
, "SARG: (report) %s: %s \n " , text
[ 45 ], wdirname
);
659 sprintf ( reg
, "%s %s %s %s %s %s \n " , user
, data
, hora
, ip
, url
, smart
);
661 fputs ( "</body> \n </html> \n " , fp_tt
);