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 //! The global statistics of the whole log read.
31 struct globalstatstruct globstat
;
32 //! \c True to enable the smart filter.
33 bool smartfilter
=false;
35 static FILE *fp_tt
=NULL
;
37 static FILE *maketmp(const char *user
, const char *dirname
, int debug
);
38 static void gravatmp(FILE *fp_ou
, const char *oldurl
, long long int nacc
, long long int nbytes
, const char *oldmsg
, long long int nelap
, long long int incache
, long long int oucache
);
39 static void gravaporuser(const struct userinfostruct
*uinfo
, const char *dirname
, const char *url
, const char *ip
, const char *data
, const char *hora
, long long int tam
, long long int elap
);
40 static void gravager(FILE *fp_gen
,const char *filename
, const struct userinfostruct
*uinfo
, long long int nacc
, const char *url
, long long int nbytes
, const char *ip
, const char *hora
, const char *dia
, long long int nelap
, long long int incache
, long long int oucache
);
41 static void grava_SmartFilter(const char *dirname
, const char *user
, const char *ip
, const char *data
, const char *hora
, const char *url
, const char *smart
);
50 char accdia
[11], acchora
[9], accip
[256], *accurl
;
51 char oldaccdia
[11], oldacchora
[9], oldaccip
[256];
53 char wdirname
[MAXLEN
];
55 char oldmsg
[50], acccode
[MAXLEN
/2 - 1], oldacccode
[MAXLEN
/2 - 1];
56 char ipantes
[256], nameantes
[MAXLEN
];
57 char accsmart
[MAXLEN
];
58 char crc2
[MAXLEN
/2 -1];
59 char siteind
[MAX_TRUNCATED_URL
];
62 char oldaccdiatt
[11],oldacchoratt
[9];
64 char u2
[MAX_USER_LEN
];
65 long long int nbytes
=0;
66 long long int nelap
=0;
68 long long int incache
=0;
69 long long int oucache
=0;
70 long long int accbytes
, accelap
;
73 const char *sort_field
;
74 const char *sort_order
;
81 struct getwordstruct gwarea
;
83 struct userinfostruct
*uinfo
;
88 memset(&globstat
,0,sizeof(globstat
));
90 if (vrfydir(&period
, addr
, site
, us
, email
)<0) {
91 debuga(_("Cannot create the output directory name containing the period as part of the name\n"));
96 debugaz(_("outdirname=%s\n"),outdirname
);
99 if(UserAgentLog
[0] != '\0' && email
[0] == '\0') useragent();
101 snprintf(wdirname
,sizeof(wdirname
),"%s/sarg-general",outdirname
);
102 if((fp_gen
=MY_FOPEN(wdirname
,"w"))==NULL
){
103 debuga(_("(report) Cannot open file %s\n"),wdirname
);
108 sort_labels(&sort_field
,&sort_order
);
110 if (indexonly
|| datetimeby
==0)
113 daystat
=day_prepare();
115 uscan
=userinfo_startscan();
117 debuga(_("Cannot enumerate the user list\n"));
120 while ((uinfo
= userinfo_advancescan(uscan
)) != NULL
) {
121 sort_users_log(tmp
,debug
,uinfo
);
122 if (snprintf(tmp3
,sizeof(tmp3
),"%s/%s.user_log",tmp
,uinfo
->filename
)>=sizeof(tmp3
)) {
123 debuga(_("(report) directory entry too long: %s/%s.user_log\n"),tmp
,uinfo
->filename
);
126 if((fp_in
=MY_FOPEN(tmp3
,"r"))==NULL
){
127 debuga(_("(report) Cannot open file %s\n"),tmp
);
130 user
=uinfo
->filename
;
131 day_newuser(daystat
);
133 strcpy(u2
,uinfo
->id
);
134 if(Ip2Name
&& uinfo
->id_is_ip
) {
135 safe_strcpy(ipantes
,u2
,sizeof(ipantes
));
136 ip2name(u2
,sizeof(u2
));
137 strcpy(nameantes
,u2
);
139 user_find(uinfo
->label
,MAX_USER_LEN
, u2
);
142 fp_tmp
=maketmp(user
,tmp
,debug
);
149 memset(oldaccdiatt
,0,sizeof(oldaccdiatt
));
150 memset(oldacchoratt
,0,sizeof(oldacchoratt
));
151 memset(oldacciptt
,0,sizeof(oldacciptt
));
159 if ((line
=longline_create())==NULL
) {
160 debuga(_("Not enough memory to read the downloaded files\n"));
164 while((buf
=longline_read(fp_in
,line
))!=NULL
) {
165 getword_start(&gwarea
,buf
);
166 if (getword(accdia
,sizeof(accdia
),&gwarea
,'\t')<0 || getword(acchora
,sizeof(acchora
),&gwarea
,'\t')<0 ||
167 getword(accip
,sizeof(accip
),&gwarea
,'\t')<0 ||
168 getword_ptr(buf
,&accurl
,&gwarea
,'\t')<0 || getword_atoll(&accbytes
,&gwarea
,'\t')<0 ||
169 getword(acccode
,sizeof(acccode
),&gwarea
,'\t')<0) {
170 debuga(_("There is a broken record or garbage in file %s\n"),tmp3
);
173 if(strncmp(acccode
,"TCP_DENIED/407",14) == 0) continue;
174 if (getword_atoll(&accelap
,&gwarea
,'\t')<0) {
175 debuga(_("There is a broken elapsed time in file %s\n"),tmp3
);
178 if (getword_skip(20000,&gwarea
,'"')<0 || getword(accsmart
,sizeof(accsmart
),&gwarea
,'"')<0) {
179 debuga(_("There is a broken smart info in file %s\n"),tmp3
);
183 if(accsmart
[0] != '\0') {
185 grava_SmartFilter(tmp
,uinfo
->id
,accip
,accdia
,acchora
,accurl
,accsmart
);
189 if(strcmp(accip
,ipantes
) != 0) {
190 strcpy(ipantes
,accip
);
191 ip2name(accip
,sizeof(accip
));
192 strcpy(nameantes
,accip
);
193 } else safe_strcpy(accip
,nameantes
,sizeof(accip
));
197 day_addpoint(daystat
,accdia
,acchora
,accelap
,accbytes
);
198 if (iprel
) gravaporuser(uinfo
,outdirname
,accurl
,accip
,accdia
,acchora
,accbytes
,accelap
);
202 url_len
=strlen(accurl
);
203 if (!oldurl
|| url_len
>=ourl_size
) {
205 oldurl
=realloc(oldurl
,ourl_size
);
207 debuga(_("Not enough memory to store the url\n"));
211 strcpy(oldurl
,accurl
);
212 strcpy(oldacccode
,acccode
);
213 strcpy(oldaccip
,accip
);
214 strcpy(oldaccdia
,accdia
);
215 strcpy(oldacchora
,acchora
);
218 same_url
=(strcmp(oldurl
,accurl
) == 0);
220 if(site
[0] == '\0') {
222 if(strstr(oldacccode
,"DENIED") != 0)
223 strcpy(oldmsg
,"DENIED");
226 if (fp_tmp
) gravatmp(fp_tmp
,oldurl
,nacc
,nbytes
,oldmsg
,nelap
,incache
,oucache
);
227 gravager(fp_gen
,wdirname
,uinfo
,nacc
,oldurl
,nbytes
,oldaccip
,oldacchora
,oldaccdia
,nelap
,incache
,oucache
);
239 if ((ReportType
& REPORT_TYPE_SITE_USER_TIME_DATE
) != 0 && !indexonly
&&
240 (!oldurltt
|| strcmp(oldurltt
,accurl
) || strcmp(oldaccdiatt
,accdia
) || strcmp(oldacchoratt
,acchora
) ||
241 strcmp(oldacciptt
,accip
))) {
244 snprintf(arqtt
,sizeof(arqtt
),"%s/%s",outdirname
,uinfo
->filename
);
245 if(access(arqtt
, R_OK
) != 0)
247 url_to_file(accurl
,siteind
,sizeof(siteind
));
248 snprintf(arqtt
,sizeof(arqtt
),"%s/%s/tt%s-%s.html",outdirname
,uinfo
->filename
,uinfo
->filename
,siteind
);
249 if ((fp_tt
= fopen(arqtt
, "w")) == 0) {
250 debuga(_("(report) Cannot open file %s\n"),arqtt
);
257 sprintf(httplink,"<font size=%s color=%s><href=http://%s>%s",FontSize,PrivacyStringColor,PrivacyString,PrivacyString);
259 sprintf(httplink,"<font size=%s><a href=\"http://%s\">%s</a>",FontSize,accurl,accurl);
262 write_html_header(fp_tt
,(IndexTree
== INDEX_TREE_DATE
) ? 4 : 2,_("Site access report"),HTML_JS_NONE
);
263 fprintf(fp_tt
,"<tr><td class=\"header_c\">%s: %s</td></tr>\n",_("Period"),period
.html
);
264 fprintf(fp_tt
,"<tr><td class=\"header_c\">%s: %s</td></tr>\n",_("User"),uinfo
->label
);
265 fputs("<tr><td class=\"header_c\">",fp_tt
);
266 fprintf(fp_tt
,_("Sort: %s, %s"),sort_field
,sort_order
);
267 fputs("</td></tr>\n",fp_tt
);
268 fprintf(fp_tt
,"<tr><th class=\"header_c\">%s</th></tr>\n",_("User"));
269 close_html_header(fp_tt
);
271 fputs("<div class=\"report\"><table cellpadding=\"0\" cellspacing=\"2\">\n",fp_tt
);
272 fprintf(fp_tt
,"<tr><th class=\"header_l\">%s</th>",_("ACCESSED SITE"));
273 fprintf(fp_tt
,"<th class=\"header_l\">%s</th>",_("IP"));
274 fprintf(fp_tt
,"<th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr>\n",_("DATE"),_("TIME"));
277 fputs("<tr><td class=\"data\">",fp_tt
);
278 output_html_string(fp_tt
,accurl
,100);
279 fputs("</td>",fp_tt
);
280 fprintf(fp_tt
,"<td class=\"data\">%s</td>",accip
);
281 fprintf(fp_tt
,"<td class=\"data\">%s</td><td class=\"data\">%s</td></tr>\n",accdia
,acchora
);
283 url_len
=strlen(accurl
);
284 if (!oldurltt
|| url_len
>=ourltt_size
) {
285 ourltt_size
=url_len
+1;
286 oldurltt
=realloc(oldurltt
,ourltt_size
);
288 debuga(_("Not enough memory to store the url\n"));
292 strcpy(oldurltt
,accurl
);
293 strcpy(oldaccdiatt
,accdia
);
294 strcpy(oldacchoratt
,acchora
);
295 strcpy(oldacciptt
,accip
);
298 strcpy(crc2
,acccode
);
299 str
=strchr(crc2
,'/');
301 if(strstr(crc2
,"MISS") != 0)
306 strcpy(oldacccode
,acccode
);
307 strcpy(oldaccip
,accip
);
309 url_len
=strlen(accurl
);
310 if (url_len
>=ourl_size
) {
312 oldurl
=realloc(oldurl
,ourl_size
);
314 debuga(_("Not enough memory to store the url\n"));
318 strcpy(oldurl
,accurl
);
320 strcpy(oldaccdia
,accdia
);
321 strcpy(oldacchora
,acchora
);
324 longline_destroy(&line
);
325 if (oldurltt
) free(oldurltt
);
327 if(strstr(oldacccode
,"DENIED") != 0)
328 strcpy(oldmsg
,"DENIED");
331 if (fp_tmp
) gravatmp(fp_tmp
,oldurl
,nacc
,nbytes
,oldmsg
,nelap
,incache
,oucache
);
332 gravager(fp_gen
,wdirname
,uinfo
,nacc
,oldurl
,nbytes
,oldaccip
,oldacchora
,oldaccdia
,nelap
,incache
,oucache
);
337 day_totalize(daystat
,tmp
,uinfo
);
340 if (fclose(fp_tmp
)==EOF
)
341 debuga(_("Failed to close temporary file for user %s - %s\n"),uinfo
->filename
,strerror(errno
));
344 if (!KeepTempLog
&& unlink(tmp3
)) {
345 debuga(_("Cannot delete \"%s\": %s\n"),tmp3
,strerror(errno
));
349 userinfo_stopscan(uscan
);
350 day_cleanup(daystat
);
352 totalger(fp_gen
,wdirname
);
355 if(email
[0] == '\0') {
357 if(DansGuardianConf
[0] != '\0')
360 debugaz(_("Dansguardian report not produced because no dansguardian configuration file was provided\n"));
368 if((ReportType
& REPORT_TYPE_DOWNLOADS
) != 0)
371 debugaz(_("Downloaded files report not requested in report_type\n"));
373 if((ReportType
& REPORT_TYPE_TOPSITES
) != 0)
376 debugaz(_("Top sites report not requested in report_type\n"));
378 if((ReportType
& REPORT_TYPE_SITES_USERS
) != 0)
381 debugaz(_("Sites & users report not requested in report_type\n"));
383 if ((ReportType
& REPORT_TYPE_DENIED
) != 0)
386 debugaz(_("Denied accesses report not requested in report_type\n"));
388 if ((ReportType
& REPORT_TYPE_AUTH_FAILURES
) != 0)
391 debugaz(_("Authentication failures report not requested in report_type\n"));
393 if(smartfilter
) smartfilter_report();
395 if(DansGuardianConf
[0] != '\0')
396 dansguardian_report();
400 if((ReportType
& REPORT_TYPE_USERS_SITES
) != 0)
403 debugaz(_("User's detailed report not requested in report_type\n"));
408 if(SuccessfulMsg
) debuga(_("Successful report generated on %s\n"),outdirname
);
410 geramail(outdirname
, debug
, outdir
, email
, tmp
);
412 if((strcmp(email
,"stdout") != 0) && SuccessfulMsg
)
413 debuga(_("Successful report generated and sent to %s\n"),email
);
416 if(indexonly
) index_only(outdirname
, debug
);
418 removetmp(outdirname
);
422 static FILE *maketmp(const char *user
, const char *dirname
, int debug
)
425 char wdirname
[MAXLEN
];
427 if((ReportType
& REPORT_TYPE_USERS_SITES
) == 0) return(NULL
);
428 if((ReportType
& REPORT_TYPE_TOPUSERS
) == 0) return(NULL
);
430 if(debug
) debuga(_("Making file: %s/%s\n"),tmp
,user
);
431 if (snprintf(wdirname
,sizeof(wdirname
),"%s/%s.utmp",tmp
,user
)>=sizeof(wdirname
)) {
432 debuga(_("Temporary file name too long: %s/%s.utmp\n"),tmp
,user
);
436 if((fp_ou
=fopen(wdirname
,"w"))==NULL
){
437 debuga(_("(report) Cannot open file %s\n"),wdirname
);
445 static void gravatmp(FILE *fp_ou
, const char *oldurl
, long long int nacc
, long long int nbytes
, const char *oldmsg
, long long int nelap
, long long int incache
, long long int oucache
)
448 This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
449 to print a long long int unless it is exactly 64-bits long.
451 fprintf(fp_ou
,"%"PRIu64
"\t%"PRIu64
"\t%s\t%s\t%"PRIu64
"\t%"PRIu64
"\t%"PRIu64
"\n",(uint64_t)nacc
,(uint64_t)nbytes
,oldurl
,oldmsg
,(uint64_t)nelap
,(uint64_t)incache
,(uint64_t)oucache
);
456 fputs("</table>\n</div>\n",fp_tt
);
457 fputs("</body>\n</html>\n",fp_tt
);
465 static void gravaporuser(const struct userinfostruct
*uinfo
, const char *dirname
, const char *url
, const char *ip
, const char *data
, const char *hora
, long long int tam
, long long int elap
)
468 char wdirname
[MAXLEN
];
470 if((ReportType
& REPORT_TYPE_USERS_SITES
) == 0) return;
472 if (snprintf(wdirname
,sizeof(wdirname
),"%s/%s.ip",tmp
,uinfo
->filename
)>=sizeof(wdirname
)) {
473 debuga(_("Path too long %s/%s.ip\n"),tmp
,uinfo
->filename
);
477 if((fp_ou
=MY_FOPEN(wdirname
,"a"))==NULL
){
478 debuga(_("(report) Cannot open file %s\n"),wdirname
);
483 This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
484 to print a long long int unless it is exactly 64-bits long.
486 fprintf(fp_ou
,"%s\t%s\t%s\t%s\t%"PRIu64
"\t%"PRIu64
"\n",ip
,url
,data
,hora
,(uint64_t)tam
,(uint64_t)elap
);
494 static void gravager(FILE *fp_gen
,const char *filename
, const struct userinfostruct
*uinfo
, long long int nacc
, const char *url
, long long int nbytes
, const char *ip
, const char *hora
, const char *dia
, long long int nelap
, long long int incache
, long long int oucache
)
497 This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
498 to print a long long int unless it is exactly 64-bits long.
500 if (fprintf(fp_gen
,"%s\t%"PRIu64
"\t%"PRIu64
"\t%s\t%s\t%s\t%s\t%"PRIu64
"\t%"PRIu64
"\t%"PRIu64
"\n",uinfo
->id
,(uint64_t)nacc
,(uint64_t)nbytes
,url
,ip
,hora
,dia
,(uint64_t)nelap
,(uint64_t)incache
,(uint64_t)oucache
)<0) {
501 debuga(_("Failed to write a line in %s\n"),filename
);
506 globstat
.nbytes
+=nbytes
;
507 globstat
.elap
+=nelap
;
508 globstat
.incache
+=incache
;
509 globstat
.oucache
+=oucache
;
514 Write the total line at the end of the general file.
516 void totalger(FILE *fp_gen
,const char *filename
)
519 This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
520 to print a long long int unless it is exactly 64-bits long.
522 if (fprintf(fp_gen
,"TOTAL\t%"PRIu64
"\t%"PRIu64
"\t%"PRIu64
"\t%"PRIu64
"\t%"PRIu64
"\n",(uint64_t)globstat
.nacc
,(uint64_t)globstat
.nbytes
,(uint64_t)globstat
.elap
,(uint64_t)globstat
.incache
,(uint64_t)globstat
.oucache
)<0) {
523 debuga(_("Failed to write the total line in %s\n"),filename
);
528 int ger_read(char *buffer
,struct generalitemstruct
*item
,const char *filename
)
532 long long int number
;
534 if (strncmp(buffer
,"TOTAL\t",6)==0) {
539 if (*buffer
== '-') {
542 } else if (*buffer
== '+') {
546 while (isdigit(*buffer
))
547 number
=(number
* 10) + (*buffer
++)-'0';
549 debuga(_("Invalid total number of accesses in %s\n"),filename
);
553 item
->nacc
=number
*sign
;
556 if (*buffer
== '-') {
559 } else if (*buffer
== '+') {
563 while (isdigit(*buffer
))
564 number
=(number
* 10) + (*buffer
++)-'0';
566 debuga(_("Invalid total size in %s\n"),filename
);
570 item
->nbytes
=number
*sign
;
573 if (*buffer
== '-') {
576 } else if (*buffer
== '+') {
580 while (isdigit(*buffer
))
581 number
=(number
* 10) + (*buffer
++)-'0';
583 debuga(_("Invalid total elapsed time in %s\n"),filename
);
587 item
->nelap
=number
*sign
;
590 if (*buffer
== '-') {
593 } else if (*buffer
== '+') {
597 while (isdigit(*buffer
))
598 number
=(number
* 10) + (*buffer
++)-'0';
600 debuga(_("Invalid total cache hit in %s\n"),filename
);
604 item
->incache
=number
*sign
;
607 if (*buffer
== '-') {
610 } else if (*buffer
== '+') {
614 while (isdigit(*buffer
))
615 number
=(number
* 10) + (*buffer
++)-'0';
617 debuga(_("Invalid total cache miss in %s\n"),filename
);
620 item
->oucache
=number
*sign
;
625 for (i
=0 ; i
<MAX_USER_LEN
-1 && (unsigned char)*buffer
>=' ' ; i
++) buffer
++;
627 debuga(_("User name too long or invalid in %s\n"),filename
);
633 if (*buffer
== '-') {
636 } else if (*buffer
== '+') {
640 while (isdigit(*buffer
))
641 number
=(number
* 10) + (*buffer
++)-'0';
643 debuga(_("Invalid number of accesses in %s\n"),filename
);
647 item
->nacc
=number
*sign
;
650 if (*buffer
== '-') {
653 } else if (*buffer
== '+') {
657 while (isdigit(*buffer
))
658 number
=(number
* 10) + (*buffer
++)-'0';
660 debuga(_("Invalid number of bytes in %s\n"),filename
);
664 item
->nbytes
=number
*sign
;
667 while ((unsigned char)*buffer
>=' ') buffer
++;
669 debuga(_("URL too long or invalid in %s\n"),filename
);
675 for (i
=0 ; i
<MAX_IP_LEN
-1 && (unsigned char)*buffer
>=' ' ; i
++) buffer
++;
677 debuga(_("IP address too long or invalid in %s\n"),filename
);
683 for (i
=0 ; i
<MAX_DATETIME_LEN
-1 && (unsigned char)*buffer
>=' ' ; i
++) buffer
++;
685 debuga(_("Time too long or invalid in %s\n"),filename
);
691 for (i
=0 ; i
<MAX_DATETIME_LEN
-1 && (unsigned char)*buffer
>=' ' ; i
++) buffer
++;
693 debuga(_("Date too long or invalid in %s\n"),filename
);
699 if (*buffer
== '-') {
702 } else if (*buffer
== '+') {
706 while (isdigit(*buffer
))
707 number
=(number
* 10) + (*buffer
++)-'0';
709 debuga(_("Invalid elapsed time in %s\n"),filename
);
713 item
->nelap
=number
*sign
;
716 if (*buffer
== '-') {
719 } else if (*buffer
== '+') {
723 while (isdigit(*buffer
))
724 number
=(number
* 10) + (*buffer
++)-'0';
726 debuga(_("Invalid cache hit size in %s\n"),filename
);
730 item
->incache
=number
*sign
;
733 if (*buffer
== '-') {
736 } else if (*buffer
== '+') {
740 while (isdigit(*buffer
))
741 number
=(number
* 10) + (*buffer
++)-'0';
743 debuga(_("Invalid cache miss size in %s\n"),filename
);
746 item
->oucache
=number
*sign
;
751 static void grava_SmartFilter(const char *dirname
, const char *user
, const char *ip
, const char *data
, const char *hora
, const char *url
, const char *smart
)
754 char wdirname
[MAXLEN
];
756 if (snprintf(wdirname
,sizeof(wdirname
),"%s/smartfilter.int_unsort",dirname
)>=sizeof(wdirname
)) {
757 debuga(_("File name too long for %s/smartfilter.int_unsort\n"),dirname
);
761 if((fp_ou
=MY_FOPEN(wdirname
,"a"))==NULL
){
762 debuga(_("(report) Cannot open file %s\n"),wdirname
);
766 fprintf(fp_ou
,"%s\t%s\t%s\t%s\t%s\t%s\n",user
,data
,hora
,ip
,url
,smart
);
767 fputs("</body>\n</html>\n",fp_tt
);