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 static FILE *fp_tt
=NULL
;
32 static void maketmp(const char *user
, const char *dirname
, int debug
, int indexonly
);
33 static void maketmp_hour(const char *user
, const char *dirname
, int indexonly
);
34 static void gravatmp_hora(const char *dirname
, const char *user
, const char *data
, const char *hora
, long long int elap
, long long int accbytes
, int indexonly
);
35 static void gravatmpf(const char *oldaccuser
, const char *dirname
, const char *oldurl
, long long int nacc
, long long int nbytes
, const char *oldmsg
, long long int nelap
, int indexonly
, long long int incache
, long long int oucache
);
36 static void gravaporuser(const char *user
, const char *dirname
, const char *url
, const char *ip
, const char *data
, const char *hora
, long long int tam
, long long int elap
, int indexonly
);
37 static void gravager(FILE *fp_gen
, const char *user
, 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
);
38 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
);
47 char accdia
[11], acchora
[9], accuser
[MAXLEN
], accip
[MAXLEN
], *accurl
;
48 char oldaccdia
[11], oldacchora
[9], oldaccip
[MAXLEN
];
49 char wdirname
[MAXLEN
], oldaccuser
[MAXLEN
];
51 char olduser
[MAXLEN
], oldmsg
[50], acccode
[MAXLEN
/2 - 1], oldacccode
[MAXLEN
/2 - 1], user
[MAXLEN
];
52 char ipantes
[MAXLEN
], nameantes
[MAXLEN
];
53 char accsmart
[MAXLEN
];
54 char crc2
[MAXLEN
/2 -1];
55 char siteind
[MAX_TRUNCATED_URL
];
58 char oldaccdiatt
[11],oldacchoratt
[9];
59 long long int nbytes
=0;
60 long long int nelap
=0;
62 long long int rtotal
=0;
63 long long int incache
=0;
64 long long int oucache
=0;
65 long long int accbytes
, accelap
;
68 struct dirent
*direntp
;
69 const char logext
[]=".log";
74 struct getwordstruct gwarea
;
75 struct longlinestruct line
;
81 sprintf(dirname
, "%s%s", outdir
, period
);
82 vrfydir(period
, addr
, site
, us
, email
);
85 debugaz("dirname",dirname
);
88 gperiod(dirname
,period
);
90 if(UserAgentLog
[0] != '\0' && email
[0] == '\0') useragent();
92 sprintf(wdirname
,"%s/sarg-general",dirname
);
93 if((fp_gen
=MY_FOPEN(wdirname
,"a"))==NULL
){
94 fprintf(stderr
, "SARG: (report) %s: %s\n",text
[45],wdirname
);
99 strncat(tmp
,"/sarg",5);
103 while ((direntp
= readdir( dirp
)) != NULL
) {
104 dlen
=strlen(direntp
->d_name
)-(sizeof(logext
)-1);
105 if (dlen
<0) continue;
106 if((strcmp(direntp
->d_name
+dlen
,logext
) != 0) ||
107 (strncmp(direntp
->d_name
,"download.log",12) == 0) ||
108 (strncmp(direntp
->d_name
,"denied.log",10) == 0) ||
109 (strncmp(direntp
->d_name
,"authfail.log.unsort",19) == 0))
111 if (snprintf(tmp3
,sizeof(tmp3
),"%s/%s",tmp
,direntp
->d_name
)>=sizeof(tmp3
)) {
112 fprintf(stderr
, "SARG: (report) directory entry too long: %s/%s\n",tmp
,direntp
->d_name
);
115 if((fp_in
=MY_FOPEN(tmp3
,"r"))==NULL
){
116 fprintf(stderr
, "SARG: (report) %s: %s\n",text
[45],tmp
);
121 if (dlen
>=sizeof(user
)) continue;
122 strncpy(user
,direntp
->d_name
,dlen
);
128 strcpy(wdirname
,dirname
);
129 maketmp(user
,tmp
,debug
,indexonly
);
130 maketmp_hour(user
,tmp
,indexonly
);
134 ip2name(u2
,sizeof(u2
));
135 user_find(name
,sizeof(name
), u2
);
137 if(dotinuser
&& strchr(name
,'_')) {
138 subs(name
,sizeof(name
),"_",".");
144 memset(oldaccdiatt
,0,sizeof(oldaccdiatt
));
145 memset(oldacchoratt
,0,sizeof(oldacchoratt
));
147 if (longline_prepare(&line
)<0) {
148 debuga(_("Not enough memory to read the downloaded files"));
152 while((buf
=longline_read(fp_in
,&line
))!=NULL
) {
153 getword_start(&gwarea
,buf
);
154 if (getword(accdia
,sizeof(accdia
),&gwarea
,'\t')<0 || getword(acchora
,sizeof(acchora
),&gwarea
,'\t')<0 ||
155 getword(accuser
,sizeof(accuser
),&gwarea
,'\t')<0 || getword(accip
,sizeof(accip
),&gwarea
,'\t')<0 ||
156 getword_ptr(buf
,&accurl
,&gwarea
,'\t')<0 || getword_atoll(&accbytes
,&gwarea
,'\t')<0 ||
157 getword(acccode
,sizeof(acccode
),&gwarea
,'\t')<0) {
158 debuga(_("Maybe you have a broken record or garbage in your %s file (%d)"),tmp3
,__LINE__
);
161 if(strncmp(acccode
,"TCP_DENIED/407",14) == 0) continue;
162 if (getword_atoll(&accelap
,&gwarea
,'\t')<0 || getword_skip(20000,&gwarea
,'\t')<0) {
163 printf("SARG: Maybe you have a broken elapsed time in your %s file (%d).\n",tmp3
,__LINE__
);
166 if (getword(accsmart
,sizeof(accsmart
),&gwarea
,'"')<0) {
167 printf("SARG: Maybe you have a broken smart info in your %s file (%d).\n",tmp3
,__LINE__
);
171 if(accsmart
[0] != '\0') {
173 strcpy(wdirname
,dirname
);
174 grava_SmartFilter(wdirname
,accuser
,accip
,accdia
,acchora
,accurl
,accsmart
);
178 if(strcmp(accip
,ipantes
) != 0) {
179 strcpy(ipantes
,accip
);
180 ip2name(accip
,sizeof(accip
));
181 strcpy(nameantes
,accip
);
182 } else strcpy(accip
,nameantes
);
185 strcpy(wdirname
,dirname
);
186 gravatmp_hora(wdirname
,accuser
,accdia
,acchora
,accelap
,accbytes
,indexonly
);
189 strcpy(wdirname
,dirname
);
190 gravaporuser(accuser
,wdirname
,accurl
,accip
,accdia
,acchora
,accbytes
,accelap
,indexonly
);
194 url_len
=strlen(accurl
);
195 if (!oldurl
|| url_len
>=ourl_size
) {
197 oldurl
=realloc(oldurl
,ourl_size
);
199 debuga(_("Not enough memory to store the url"));
203 strcpy(oldurl
,accurl
);
204 strcpy(oldacccode
,acccode
);
205 strcpy(oldaccuser
,accuser
);
206 strcpy(oldaccip
,accip
);
207 strcpy(oldaccdia
,accdia
);
208 strcpy(oldacchora
,acchora
);
212 if(site
[0] != '\0') {
213 if(strcmp(oldaccuser
,accuser
) != 0){
215 if(strstr(oldacccode
,"DENIED") != 0)
216 sprintf(oldmsg
,"%s",text
[46]);
217 gravatmp(oldaccuser
,oldurl
,nacc
,nbytes
,oldmsg
,nelap
,indexonly
,incache
,oucache
);
218 gravager(fp_gen
,oldaccuser
,nacc
,oldurl
,nbytes
,oldaccip
,oldacchora
,oldaccdia
,nelap
,incache
,oucache
);
226 if(strcmp(oldurl
,accurl
) != 0 || strcmp(oldaccuser
,accuser
) != 0){
227 if(strstr(oldacccode
,"DENIED") != 0)
228 strcpy(oldmsg
,text
[46]);
231 gravatmp(oldaccuser
,oldurl
,nacc
,nbytes
,oldmsg
,nelap
,indexonly
,incache
,oucache
);
232 gravager(fp_gen
,oldaccuser
,nacc
,oldurl
,nbytes
,oldaccip
,oldacchora
,oldaccdia
,nelap
,incache
,oucache
);
238 if(strcmp(oldaccuser
,accuser
) != 0)
246 if ((ReportType
& REPORT_TYPE_SITE_USER_TIME_DATE
) != 0 &&
247 (!oldurltt
|| strcmp(oldurltt
,accurl
) || strcmp(oldaccdiatt
,accdia
) || strcmp(oldacchoratt
,acchora
))) {
251 url_to_file(accurl
,siteind
,sizeof(siteind
));
252 snprintf(arqtt
,sizeof(arqtt
),"%s/%s",dirname
,accuser
);
253 if(access(arqtt
, R_OK
) != 0)
255 snprintf(arqtt
,sizeof(arqtt
),"%s/%s/tt%s-%s.html",dirname
,accuser
,accuser
,siteind
);
256 if ((fp_tt
= fopen(arqtt
, "w")) == 0) {
257 fprintf(stderr
, "SARG: (report) %s: %s\n",text
[45],arqtt
);
264 sprintf(httplink,"<font size=%s color=%s><href=http://%s>%s", \
265 FontSize,PrivacyStringColor,PrivacyString,PrivacyString);
267 sprintf(httplink,"<font size=%s><a href=\"http://%s\">%s</a>",FontSize,accurl,accurl);
270 write_html_header(fp_tt
,(IndexTree
== INDEX_TREE_DATE
) ? 4 : 2,_("Site access report"));
271 fprintf(fp_tt
,"<tr><td class=\"header_l\">%s: %s</td></tr>\n",text
[89],period
);
272 fprintf(fp_tt
,"<tr><td class=\"header_l\">%s: %s</td></tr>\n",text
[90],name
);
273 fprintf(fp_tt
,"<tr><td class=\"header_l\">%s: %s, %s</td></tr>\n",text
[104],UserSortField
,UserSortOrder
);
274 fprintf(fp_tt
,"<tr><th class=\"header_c\">%s</th></tr>\n",text
[32]);
275 close_html_header(fp_tt
);
277 fputs("<div class=\"report\"><table cellpadding=\"0\" cellspacing=\"2\">\n",fp_tt
);
278 fputs("<tr><td></td><td></td></tr>",fp_tt
);
280 strncpy(tmp4
,text
[110],4);
281 fprintf(fp_tt
,"<tr><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr>\n",text
[91],tmp4
,text
[110]+5);
284 fputs("<tr><td class=\"data\">",fp_tt
);
285 output_html_string(fp_tt
,accurl
,100);
286 fprintf(fp_tt
,"</td><td class=\"data\">%s</td><td class=\"data\">%s</td></tr>\n",accdia
,acchora
);
288 url_len
=strlen(accurl
);
289 if (!oldurltt
|| url_len
>=ourltt_size
) {
290 ourltt_size
=url_len
+1;
291 oldurltt
=realloc(oldurltt
,ourltt_size
);
293 debuga(_("Not enough memory to store the url"));
297 strcpy(oldurltt
,accurl
);
298 strcpy(oldaccdiatt
,accdia
);
299 strcpy(oldacchoratt
,acchora
);
302 strcpy(crc2
,acccode
);
303 str
=strchr(crc2
,'/');
305 if(strstr(crc2
,"MISS") != 0)
307 else incache
+=accbytes
;
309 if(strcmp(accuser
,oldaccuser
) != 0) {
310 strcpy(wdirname
,dirname
);
311 day_totalize(tmp
,oldaccuser
,indexonly
);
312 strcpy(oldaccuser
,accuser
);
315 strcpy(oldacccode
,acccode
);
316 strcpy(oldaccip
,accip
);
317 url_len
=strlen(accurl
);
318 if (url_len
>=ourl_size
) {
320 oldurl
=realloc(oldurl
,ourl_size
);
322 debuga(_("Not enough memory to store the url"));
326 strcpy(oldurl
,accurl
);
327 strcpy(oldaccdia
,accdia
);
328 strcpy(oldacchora
,acchora
);
331 bzero(user
,sizeof(user
));
334 longline_free(&line
);
335 if (oldurltt
) free(oldurltt
);
340 if(strstr(oldacccode
,"DENIED") != 0)
341 strcpy(oldmsg
,text
[46]);
344 strcpy(wdirname
,dirname
);
345 if(oldaccuser
[0] == '\0')
346 strcpy(oldaccuser
,accuser
);
347 gravatmpf(oldaccuser
,wdirname
,oldurl
,nacc
,nbytes
,oldmsg
,nelap
,indexonly
,incache
,oucache
);
348 strcpy(wdirname
,dirname
);
349 gravager(fp_gen
,oldaccuser
,nacc
,oldurl
,nbytes
,oldaccip
,oldacchora
,oldaccdia
,nelap
,incache
,oucache
);
353 day_totalize(tmp
,oldaccuser
,indexonly
);
357 strcpy(wdirname
,dirname
);
358 totalger(wdirname
, debug
, outdir
);
360 if(email
[0] == '\0') {
361 if((ReportType
& REPORT_TYPE_DOWNLOADS
) != 0) download_report();
363 if(DansGuardianConf
[0] != '\0') {
364 strcpy(wdirname
,dirname
);
368 strcpy(wdirname
,dirname
);
371 strcpy(wdirname
,dirname
);
374 if((ReportType
& REPORT_TYPE_TOPSITES
) != 0) topsites();
376 if((ReportType
& REPORT_TYPE_SITES_USERS
) != 0) siteuser();
379 strcpy(wdirname
,dirname
);
382 if(smartfilter
) smartfilter_report();
384 if(DansGuardianConf
[0] != '\0') dansguardian_report();
388 if((ReportType
& REPORT_TYPE_USERS_SITES
) != 0) htmlrel();
392 if(SuccessfulMsg
) fprintf(stderr
, "SARG: %s %s\n",text
[47],dirname
);
394 strcpy(wdirname
,dirname
);
395 geramail(wdirname
, debug
, outdir
, userip
, email
, TempDir
);
397 if((strcmp(email
,"stdout") != 0) && SuccessfulMsg
)
398 fprintf(stderr
, "SARG: %s %s\n",text
[48],email
);
402 strcpy(wdirname
,dirname
);
403 index_only(wdirname
, debug
);
407 2009-10-13(Frederic) This piece of code is never called so it is commented out for good.
408 if(strlen(email) < 0)
415 static void maketmp(const char *user
, const char *dirname
, int debug
, int indexonly
)
420 char wdirname
[MAXLEN
];
422 if(indexonly
) return;
423 if((ReportType
& REPORT_TYPE_USERS_SITES
) == 0) return;
425 strcpy(wdirname
,tmp
);
426 strcat(wdirname
,"/");
427 strcat(wdirname
,user
);
430 debuga("%s: %s",text
[49],wdirname
);
433 strcat(wdirname
,".utmp");
434 if((fp_ou
=fopen(wdirname
,"w"))==NULL
){
435 fprintf(stderr
, "SARG: (report) %s: %s\n",text
[45],wdirname
);
444 static void maketmp_hour(const char *user
, const char *dirname
, int indexonly
)
449 char wdirname
[MAXLEN
];
451 if(indexonly
) return;
452 if((ReportType
& REPORT_TYPE_USERS_SITES
) == 0) return;
454 strcpy(wdirname
,tmp
);
455 strcat(wdirname
,"/");
456 strcat(wdirname
,user
);
458 strcat(wdirname
,".htmp");
459 if((fp_ou
=fopen(wdirname
,"w"))==NULL
){
460 fprintf(stderr
, "SARG: (report-1) %s: %s - %s\n",text
[45],wdirname
,strerror(errno
));
469 void gravatmp(const char *oldaccuser
, const char *oldurl
, long long int nacc
, long long int nbytes
, const 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((ReportType
& REPORT_TYPE_USERS_SITES
) == 0) return;
478 if (snprintf(wdirname
,sizeof(wdirname
),"%s/%s.utmp",tmp
,oldaccuser
)>=sizeof(wdirname
)) {
479 debuga(_("Path too long %s/%s.utmp"),tmp
,oldaccuser
);
483 if((fp_ou
=MY_FOPEN(wdirname
,"a"))==NULL
){
484 fprintf(stderr
, "SARG: (report) %s: %s\n",text
[45],wdirname
);
488 fprintf(fp_ou
,"%lld\t%lld\t%s\t%s\t%lld\t%lld\t%lld\n",nacc
,nbytes
,oldurl
,oldmsg
,nelap
,incache
,oucache
);
494 fputs("</table>\n</div>\n",fp_tt
);
495 fputs("</body>\n</html>\n",fp_tt
);
505 static void gravatmp_hora(const char *dirname
, const char *user
, const char *data
, const char *hora
, long long int elap
, long long int bytes
, int indexonly
)
509 char wdirname
[MAXLEN
];
511 if(indexonly
|| ((ReportType
& REPORT_TYPE_USERS_SITES
) == 0)) return;
513 if (snprintf(wdirname
,sizeof(wdirname
),"%s/%s.htmp",tmp
,user
)>=sizeof(wdirname
)) {
514 fprintf(stderr
,"SARG: Path too long %s/%s.htmp\n",tmp
,user
);
518 if((fp_ou
=MY_FOPEN(wdirname
,"a"))==NULL
){
519 fprintf(stderr
, "SARG: (report-2) %s: %s - %s\n",text
[45],wdirname
,strerror(errno
));
523 if(strcmp(datetimeby
,"bytes") == 0) fprintf(fp_ou
,"%s\t%s\t%lld\n",data
,hora
,bytes
);
524 else fprintf(fp_ou
,"%s\t%s\t%lld\n",data
,hora
,elap
);
532 static void gravaporuser(const char *user
, const char *dirname
, const char *url
, const char *ip
, const char *data
, const char *hora
, long long int tam
, long long int elap
, int indexonly
)
536 char wdirname
[MAXLEN
];
538 if(indexonly
|| ((ReportType
& REPORT_TYPE_USERS_SITES
) == 0)) return;
540 if (snprintf(wdirname
,sizeof(wdirname
),"%s/%s.ip",tmp
,user
)>=sizeof(wdirname
)) {
541 fprintf(stderr
,"SARG: Path too long %s/%s.ip\n",tmp
,user
);
545 if((fp_ou
=MY_FOPEN(wdirname
,"a"))==NULL
){
546 fprintf(stderr
, "SARG: (report) %s: %s\n",text
[45],wdirname
);
550 fprintf(fp_ou
,"%s\t%s\t%s\t%s\t%lld\t%lld\n",ip
,url
,data
,hora
,tam
,elap
);
559 static void gravatmpf(const char *oldaccuser
, const char *dirname
, const char *oldurl
, long long int nacc
, long long int nbytes
, const char *oldmsg
, long long int nelap
, int indexonly
, long long int incache
, long long int oucache
)
563 char wdirname
[MAXLEN
];
565 if(indexonly
|| ((ReportType
& REPORT_TYPE_USERS_SITES
) == 0)) return;
567 if (snprintf(wdirname
,sizeof(wdirname
),"%s/%s.utmp",tmp
,oldaccuser
)>=sizeof(wdirname
)) {
568 fprintf(stderr
,"SARG: Path too long %s/%s.utmp\n",tmp
,oldaccuser
);
572 if((fp_ou
=MY_FOPEN(wdirname
,"a"))==NULL
){
573 fprintf(stderr
, "SARG: (report) %s: %s\n",text
[45],wdirname
);
577 fprintf(fp_ou
,"%lld\t%lld\t%s\t%s\t%lld\t%lld\t%lld\n",nacc
,nbytes
,oldurl
,oldmsg
,nelap
,incache
,oucache
);
584 fputs("</table>\n",fp_tt
);
585 fputs("</body>\n</html>\n",fp_tt
);
595 static void gravager(FILE *fp_gen
, const char *user
, 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
)
597 fprintf(fp_gen
,"%s\t%lld\t%lld\t%s\t%s\t%s\t%s\t%lld\t%lld\t%lld\n",user
,nacc
,nbytes
,url
,ip
,hora
,dia
,nelap
,incache
,oucache
);
602 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
)
606 char wdirname
[MAXLEN
];
608 sprintf(wdirname
,"%s/smartfilter.unsort",dirname
);
610 if((fp_ou
=MY_FOPEN(wdirname
,"a"))==NULL
){
611 fprintf(stderr
, "SARG: (report) %s: %s\n",text
[45],wdirname
);
615 fprintf(fp_ou
,"%s\t%s\t%s\t%s\t%s\t%s\n",user
,data
,hora
,ip
,url
,smart
);
616 fputs("</body>\n</html>\n",fp_tt
);