]> git.ipfire.org Git - thirdparty/sarg.git/blame - report.c
Add a double check on the data at the user's level
[thirdparty/sarg.git] / report.c
CommitLineData
25697a35 1/*
94ff9470 2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
fbd133bb 3 * 1998, 2011
25697a35
GS
4 *
5 * SARG donations:
6 * please look at http://sarg.sourceforge.net/donations.php
1164c474
FM
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
25697a35
GS
9 * ---------------------------------------------------------------------
10 *
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.
15 *
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.
20 *
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.
24 *
25 */
26
27#include "include/conf.h"
28#include "include/defs.h"
29
9dc20988
FM
30//! The global statistics of the whole log read.
31struct globalstatstruct globstat;
32
e5b2c6f0
FM
33static FILE *fp_tt=NULL;
34
6588b137
FM
35static void maketmp(const char *user, const char *dirname, int debug);
36static void maketmp_hour(const char *user, const char *dirname);
37static void gravatmp_hora(const char *dirname, const struct userinfostruct *uinfo, const char *data, const char *hora, long long int elap, long long int accbytes);
38static void gravatmp(const struct userinfostruct *uinfo, 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);
39static 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);
9dc20988 40static 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);
5160b452 41static void grava_SmartFilter(const char *dirname, const char *user, const char *ip, const char *data, const char *hora, const char *url, const char *smart);
5f3cfd1d 42
32e71fa4 43void gerarel(void)
25697a35 44{
9bd92830
FM
45 FILE *fp_in;
46 FILE *fp_gen;
47
48 char *buf;
49 char accdia[11], acchora[9], accip[MAXLEN], *accurl;
50 char oldaccdia[11], oldacchora[9], oldaccip[MAXLEN];
51 char wdirname[MAXLEN];
52 char *oldurl=NULL;
93551487 53 char oldmsg[50], acccode[MAXLEN/2 - 1], oldacccode[MAXLEN/2 - 1];
9bd92830
FM
54 char ipantes[MAXLEN], nameantes[MAXLEN];
55 char accsmart[MAXLEN];
56 char crc2[MAXLEN/2 -1];
57 char siteind[MAX_TRUNCATED_URL];
58 char arqtt[256];
59 char *oldurltt=NULL;
60 char oldaccdiatt[11],oldacchoratt[9];
61 char tmp3[MAXLEN];
9bd92830
FM
62 char u2[MAX_USER_LEN];
63 long long int nbytes=0;
64 long long int nelap=0;
65 long long int nacc=0;
66 long long int rtotal=0;
67 long long int incache=0;
68 long long int oucache=0;
69 long long int accbytes, accelap;
70 char *str;
93551487 71 userscan uscan;
9bd92830
FM
72 const char *sort_field;
73 const char *sort_order;
93551487 74 const char *user;
9bd92830
FM
75 int url_len;
76 int ourl_size=0;
77 int ourltt_size=0;
78 int same_url;
79 int new_user;
80 struct getwordstruct gwarea;
81 longline line;
82 struct userinfostruct *uinfo,*puinfo;
83
84 ipantes[0]='\0';
85 smartfilter=0;
9dc20988 86 memset(&globstat,0,sizeof(globstat));
9bd92830
FM
87
88 if (vrfydir(&period, addr, site, us, email)<0) {
89 debuga(_("Cannot create the output directory name containing the period as part of the name\n"));
90 exit(EXIT_FAILURE);
91 }
92
93 if(debugz){
9f93fec3 94 debugaz(_("outdirname=%s\n"),outdirname);
9bd92830
FM
95 }
96
97 if(UserAgentLog[0] != '\0' && email[0] == '\0') useragent();
98
99 snprintf(wdirname,sizeof(wdirname),"%s/sarg-general",outdirname);
100 if((fp_gen=MY_FOPEN(wdirname,"w"))==NULL){
101 debuga(_("(report) Cannot open file %s\n"),wdirname);
102 exit(EXIT_FAILURE);
103 }
104
105 puinfo=NULL;
106 fp_tt=NULL;
fefdc019 107 sort_labels(&sort_field,&sort_order);
9bd92830 108
93551487
FM
109 uscan=userinfo_startscan();
110 if (uscan == NULL) {
111 debuga(_("Cannot enumerate the user list\n"));
9bd92830
FM
112 exit(EXIT_FAILURE);
113 }
93551487
FM
114 while ((uinfo = userinfo_advancescan(uscan)) != NULL ) {
115 if (snprintf(tmp3,sizeof(tmp3),"%s/%s.log",tmp,uinfo->filename)>=sizeof(tmp3)) {
116 debuga(_("(report) directory entry too long: %s/%s.log\n"),tmp,uinfo->filename);
9bd92830
FM
117 exit(EXIT_FAILURE);
118 }
119 if((fp_in=MY_FOPEN(tmp3,"r"))==NULL){
120 debuga(_("(report) Cannot open file %s\n"),tmp);
121 exit(EXIT_FAILURE);
122 }
93551487 123 user=uinfo->filename;
9bd92830 124
9bd92830
FM
125 strcpy(u2,uinfo->id);
126 if(Ip2Name && uinfo->id_is_ip) {
127 strcpy(ipantes,u2);
128 ip2name(u2,sizeof(u2));
129 strcpy(nameantes,u2);
130 }
131 user_find(uinfo->label,MAX_USER_LEN, u2);
132
6588b137
FM
133 if (!indexonly) {
134 maketmp(user,tmp,debug);
135 maketmp_hour(user,tmp);
136 }
9bd92830
FM
137
138 ttopen=0;
139 oldurltt=NULL;
140 ourltt_size=0;
141 memset(oldaccdiatt,0,sizeof(oldaccdiatt));
142 memset(oldacchoratt,0,sizeof(oldacchoratt));
143 new_user=1;
144
145 if ((line=longline_create())==NULL) {
146 debuga(_("Not enough memory to read the downloaded files\n"));
147 exit(EXIT_FAILURE);
148 }
149
150 while((buf=longline_read(fp_in,line))!=NULL) {
151 getword_start(&gwarea,buf);
152 if (getword(accdia,sizeof(accdia),&gwarea,'\t')<0 || getword(acchora,sizeof(acchora),&gwarea,'\t')<0 ||
007905af
FM
153 getword(accip,sizeof(accip),&gwarea,'\t')<0 ||
154 getword_ptr(buf,&accurl,&gwarea,'\t')<0 || getword_atoll(&accbytes,&gwarea,'\t')<0 ||
155 getword(acccode,sizeof(acccode),&gwarea,'\t')<0) {
9bd92830
FM
156 debuga(_("There is a broken record or garbage in file %s\n"),tmp3);
157 exit(EXIT_FAILURE);
158 }
159 if(strncmp(acccode,"TCP_DENIED/407",14) == 0) continue;
160 if (getword_atoll(&accelap,&gwarea,'\t')<0) {
161 debuga(_("There is a broken elapsed time in file %s\n"),tmp3);
162 exit(EXIT_FAILURE);
163 }
164 if (getword_skip(20000,&gwarea,'"')<0 || getword(accsmart,sizeof(accsmart),&gwarea,'"')<0) {
165 debuga(_("There is a broken smart info in file %s\n"),tmp3);
166 exit(EXIT_FAILURE);
167 }
168
169 if(accsmart[0] != '\0') {
170 smartfilter++;
171 grava_SmartFilter(outdirname,uinfo->id,accip,accdia,acchora,accurl,accsmart);
172 }
173
174 if(Ip2Name) {
175 if(strcmp(accip,ipantes) != 0) {
176 strcpy(ipantes,accip);
177 ip2name(accip,sizeof(accip));
178 strcpy(nameantes,accip);
179 } else strcpy(accip,nameantes);
180 }
181
6588b137
FM
182 if (!indexonly) {
183 gravatmp_hora(outdirname,uinfo,accdia,acchora,accelap,accbytes);
9bd92830 184
6588b137 185 if(iprel) gravaporuser(uinfo,outdirname,accurl,accip,accdia,acchora,accbytes,accelap);
9bd92830
FM
186 }
187
188 if(!rtotal){
189 url_len=strlen(accurl);
190 if (!oldurl || url_len>=ourl_size) {
191 ourl_size=url_len+1;
192 oldurl=realloc(oldurl,ourl_size);
193 if (!oldurl) {
194 debuga(_("Not enough memory to store the url\n"));
195 exit(EXIT_FAILURE);
196 }
197 }
198 strcpy(oldurl,accurl);
199 strcpy(oldacccode,acccode);
200 puinfo=uinfo;
201 strcpy(oldaccip,accip);
202 strcpy(oldaccdia,accdia);
203 strcpy(oldacchora,acchora);
204 new_user=0;
205 rtotal++;
206 }
207 same_url=(strcmp(oldurl,accurl) == 0);
208
209 if(site[0] != '\0') {
210 if(new_user){
211 if(strstr(oldacccode,"DENIED") != 0)
212 strcpy(oldmsg,"DENIED");
213 else
214 strcpy(oldmsg,"OK");
6588b137 215 if (!indexonly) gravatmp(puinfo,oldurl,nacc,nbytes,oldmsg,nelap,incache,oucache);
9dc20988 216 gravager(fp_gen,wdirname,puinfo,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache);
9bd92830
FM
217 nacc=0;
218 nbytes=0;
219 nelap=0;
220 incache=0;
221 oucache=0;
222 }
223 } else {
224 if(!same_url || new_user){
225 if(strstr(oldacccode,"DENIED") != 0)
226 strcpy(oldmsg,"DENIED");
227 else
228 strcpy(oldmsg,"OK");
6588b137 229 if (!indexonly) gravatmp(puinfo,oldurl,nacc,nbytes,oldmsg,nelap,incache,oucache);
9dc20988 230 gravager(fp_gen,wdirname,puinfo,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache);
9bd92830
FM
231 nacc=0;
232 nbytes=0;
233 nelap=0;
234 incache=0;
235 oucache=0;
236 }
237 }
238 nacc++;
239 nbytes+=accbytes;
240 nelap+=accelap;
241
1219fb1a 242 if ((ReportType & REPORT_TYPE_SITE_USER_TIME_DATE) != 0 && !indexonly &&
007905af 243 (!oldurltt || strcmp(oldurltt,accurl) || strcmp(oldaccdiatt,accdia) || strcmp(oldacchoratt,acchora))) {
9bd92830
FM
244
245 if(!ttopen) {
246 snprintf(arqtt,sizeof(arqtt),"%s/%s",outdirname,uinfo->filename);
247 if(access(arqtt, R_OK) != 0)
248 my_mkdir(arqtt);
249 url_to_file(accurl,siteind,sizeof(siteind));
250 snprintf(arqtt,sizeof(arqtt),"%s/%s/tt%s-%s.html",outdirname,uinfo->filename,uinfo->filename,siteind);
251 if ((fp_tt = fopen(arqtt, "w")) == 0) {
252 debuga(_("(report) Cannot open file %s\n"),arqtt);
253 exit(EXIT_FAILURE);
254 }
255 ttopen=1;
256
257 /*
258 if(Privacy)
007905af 259 sprintf(httplink,"<font size=%s color=%s><href=http://%s>%s",FontSize,PrivacyStringColor,PrivacyString,PrivacyString);
9bd92830
FM
260 else
261 sprintf(httplink,"<font size=%s><a href=\"http://%s\">%s</a>",FontSize,accurl,accurl);
262 */
263
264 write_html_header(fp_tt,(IndexTree == INDEX_TREE_DATE) ? 4 : 2,_("Site access report"),HTML_JS_NONE);
265 fprintf(fp_tt,"<tr><td class=\"header_c\">%s:&nbsp;%s</td></tr>\n",_("Period"),period.html);
266 fprintf(fp_tt,"<tr><td class=\"header_c\">%s:&nbsp;%s</td></tr>\n",_("User"),uinfo->label);
267 fputs("<tr><td class=\"header_c\">",fp_tt);
268 fprintf(fp_tt,_("Sort:&nbsp;%s, %s"),sort_field,sort_order);
269 fputs("</td></tr>\n",fp_tt);
270 fprintf(fp_tt,"<tr><th class=\"header_c\">%s</th></tr>\n",_("User"));
271 close_html_header(fp_tt);
272
273 fputs("<div class=\"report\"><table cellpadding=\"0\" cellspacing=\"2\">\n",fp_tt);
bde5073d 274 fprintf(fp_tt,"<tr><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr>\n",_("ACCESSED SITE"),_("DATE"),_("TIME"));
9bd92830
FM
275 }
276
277 fputs("<tr><td class=\"data\">",fp_tt);
278 output_html_string(fp_tt,accurl,100);
279 fprintf(fp_tt,"</td><td class=\"data\">%s</td><td class=\"data\">%s</td></tr>\n",accdia,acchora);
280
281 url_len=strlen(accurl);
282 if (!oldurltt || url_len>=ourltt_size) {
283 ourltt_size=url_len+1;
284 oldurltt=realloc(oldurltt,ourltt_size);
285 if (!oldurltt) {
286 debuga(_("Not enough memory to store the url\n"));
287 exit(EXIT_FAILURE);
288 }
289 }
290 strcpy(oldurltt,accurl);
291 strcpy(oldaccdiatt,accdia);
292 strcpy(oldacchoratt,acchora);
293 }
294
295 strcpy(crc2,acccode);
296 str=strchr(crc2,'/');
297 if (str) *str='\0';
298 if(strstr(crc2,"MISS") != 0)
299 oucache+=accbytes;
300 else incache+=accbytes;
301
302 if(new_user) {
303 new_user=0;
6588b137 304 if (!indexonly) day_totalize(tmp,puinfo);
9bd92830
FM
305 }
306
307 puinfo=uinfo;
308 strcpy(oldacccode,acccode);
309 strcpy(oldaccip,accip);
310 if (!same_url) {
311 url_len=strlen(accurl);
312 if (url_len>=ourl_size) {
313 ourl_size=url_len+1;
314 oldurl=realloc(oldurl,ourl_size);
315 if (!oldurl) {
316 debuga(_("Not enough memory to store the url\n"));
317 exit(EXIT_FAILURE);
318 }
319 }
320 strcpy(oldurl,accurl);
321 }
322 strcpy(oldaccdia,accdia);
323 strcpy(oldacchora,acchora);
324
325 }
326 fclose(fp_in);
327 longline_destroy(&line);
328 if (oldurltt) free(oldurltt);
08f9b029
FM
329 if (unlink(tmp3)) {
330 debuga(_("Cannot delete %s - %s\n"),tmp3,strerror(errno));
331 exit(EXIT_FAILURE);
332 }
9bd92830 333 }
93551487 334 userinfo_stopscan(uscan);
9bd92830
FM
335
336 if (oldurl) {
337 if(strstr(oldacccode,"DENIED") != 0)
338 strcpy(oldmsg,"DENIED");
339 else
340 strcpy(oldmsg,"OK");
6588b137 341 if (!indexonly) gravatmp(puinfo,oldurl,nacc,nbytes,oldmsg,nelap,incache,oucache);
9dc20988 342 gravager(fp_gen,wdirname,puinfo,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache);
9bd92830
FM
343 free(oldurl);
344 }
9dc20988 345 totalger(fp_gen,wdirname);
9bd92830 346 fclose(fp_gen);
6588b137 347 if (puinfo && !indexonly) day_totalize(tmp,puinfo);
9bd92830 348
9bd92830 349 if(email[0] == '\0') {
edc276df 350 if (!indexonly) {
9f93fec3 351 if(DansGuardianConf[0] != '\0')
edc276df 352 dansguardian_log();
9f93fec3
FM
353 else if (debugz)
354 debugaz(_("Dansguardian report not produced because no dansguardian configuration file was provided\n"));
9bd92830 355
330b1c52 356 redirector_log();
9a759ea8 357 }
9bd92830 358
9a759ea8
FM
359 topuser();
360
361 if (!indexonly) {
5138c1b9
FM
362 if((ReportType & REPORT_TYPE_DOWNLOADS) != 0)
363 download_report();
364 else if (debugz)
365 debugaz(_("Downloaded files report not requested in report_type\n"));
366
9f93fec3
FM
367 if((ReportType & REPORT_TYPE_TOPSITES) != 0)
368 topsites();
369 else if (debugz)
370 debugaz(_("Top sites report not requested in report_type\n"));
371
372 if((ReportType & REPORT_TYPE_SITES_USERS) != 0)
373 siteuser();
374 else if (debugz)
375 debugaz(_("Sites & users report not requested in report_type\n"));
1bd73f70
FM
376
377 if ((ReportType & REPORT_TYPE_DENIED) != 0)
378 gen_denied_report();
9f93fec3
FM
379 else if (debugz)
380 debugaz(_("Denied accesses report not requested in report_type\n"));
9bd92830 381
1bd73f70
FM
382 if ((ReportType & REPORT_TYPE_AUTH_FAILURES) != 0)
383 authfail_report();
9f93fec3
FM
384 else if (debugz)
385 debugaz(_("Authentication failures report not requested in report_type\n"));
9bd92830 386
edc276df 387 if(smartfilter) smartfilter_report();
9bd92830 388
9f93fec3
FM
389 if(DansGuardianConf[0] != '\0')
390 dansguardian_report();
9bd92830 391
330b1c52 392 redirector_report();
9bd92830 393
9f93fec3
FM
394 if((ReportType & REPORT_TYPE_USERS_SITES) != 0)
395 htmlrel();
396 else if (debugz)
397 debugaz(_("User's detailed report not requested in report_type\n"));
edc276df 398 }
9bd92830
FM
399
400 make_index();
401
402 if(SuccessfulMsg) debuga(_("Successful report generated on %s\n"),outdirname);
403 } else {
404 geramail(outdirname, debug, outdir, email, tmp);
405
406 if((strcmp(email,"stdout") != 0) && SuccessfulMsg)
407 debuga(_("Successful report generated and sent to %s\n"),email);
408 }
409
6588b137 410 if(indexonly) index_only(outdirname, debug);
9bd92830
FM
411
412 removetmp(outdirname);
413 return;
25697a35
GS
414}
415
6588b137 416static void maketmp(const char *user, const char *dirname, int debug)
25697a35 417{
9bd92830
FM
418 FILE *fp_ou;
419 char wdirname[MAXLEN];
25697a35 420
9bd92830 421 if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return;
25697a35 422
9bd92830
FM
423 if(debug) debuga(_("Making file: %s/%s\n"),tmp,user);
424 if (snprintf(wdirname,sizeof(wdirname),"%s/%s.utmp",tmp,user)>=sizeof(wdirname)) {
425 debuga(_("Temporary file name too long: %s/%s.utmp\n"),tmp,user);
426 exit(EXIT_FAILURE);
427 }
25697a35 428
9bd92830
FM
429 if((fp_ou=fopen(wdirname,"w"))==NULL){
430 debuga(_("(report) Cannot open file %s\n"),wdirname);
431 exit(EXIT_FAILURE);
432 }
25697a35 433
9bd92830
FM
434 fclose(fp_ou);
435 return;
25697a35
GS
436}
437
438
6588b137 439static void maketmp_hour(const char *user, const char *dirname)
25697a35 440{
9bd92830
FM
441 FILE *fp_ou;
442 char wdirname[MAXLEN];
25697a35 443
9bd92830
FM
444 if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return;
445 if(datetimeby==0) return;
25697a35 446
9bd92830
FM
447 if (snprintf(wdirname,sizeof(wdirname),"%s/%s.htmp",tmp,user)>=sizeof(wdirname)) {
448 debuga(_("Temporary file name too long: %s/%s.htmp\n"),tmp,user);
449 exit(EXIT_FAILURE);
450 }
25697a35 451
9bd92830
FM
452 if((fp_ou=fopen(wdirname,"w"))==NULL){
453 debuga(_("(report-1) Cannot open file %s - %s\n"),wdirname,strerror(errno));
454 exit(EXIT_FAILURE);
455 }
25697a35 456
9bd92830
FM
457 fclose(fp_ou);
458 return;
25697a35
GS
459}
460
461
6588b137 462static void gravatmp(const struct userinfostruct *uinfo, 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)
25697a35 463{
9bd92830
FM
464 FILE *fp_ou;
465 char wdirname[MAXLEN];
466
9bd92830
FM
467 if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return;
468
469 if (snprintf(wdirname,sizeof(wdirname),"%s/%s.utmp",tmp,uinfo->filename)>=sizeof(wdirname)) {
470 debuga(_("Temporary file name too long: %s/%s.utmp\n"),tmp,uinfo->filename);
471 exit(EXIT_FAILURE);
472 }
473
474 if((fp_ou=MY_FOPEN(wdirname,"a"))==NULL){
475 debuga(_("(report) Cannot open file %s\n"),wdirname);
476 exit(EXIT_FAILURE);
477 }
478
479 /*
480 This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
481 to print a long long int unless it is exactly 64-bits long.
482 */
483 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);
484
485 if (fclose(fp_ou)==EOF) {
486 debuga(_("Failed to close file %s - %s\n"),wdirname,strerror(errno));
487 exit(EXIT_FAILURE);
488 }
489 ttopen=0;
490
491 if(fp_tt) {
492 fputs("</table>\n</div>\n",fp_tt);
493 fputs("</body>\n</html>\n",fp_tt);
494 fclose(fp_tt);
495 fp_tt=NULL;
496 }
497
498 return;
25697a35
GS
499}
500
6588b137 501static void gravatmp_hora(const char *dirname, const struct userinfostruct *uinfo, const char *data, const char *hora, long long int elap, long long int bytes)
25697a35 502{
9bd92830
FM
503 FILE *fp_ou;
504 char wdirname[MAXLEN];
505 int hour;
506
6588b137 507 if(((ReportType & REPORT_TYPE_USERS_SITES) == 0) || datetimeby==0) return;
9bd92830
FM
508
509 if (snprintf(wdirname,sizeof(wdirname),"%s/%s.htmp",tmp,uinfo->filename)>=sizeof(wdirname)) {
510 debuga(_("Path too long %s/%s.htmp\n"),tmp,uinfo->filename);
511 exit(EXIT_FAILURE);
512 }
513
514 if((fp_ou=MY_FOPEN(wdirname,"a"))==NULL){
515 debuga(_("(report-2) Cannot open file %s - %s\n"),wdirname,strerror(errno));
516 exit(EXIT_FAILURE);
517 }
518
519 hour=atoi(hora);
520 fprintf(fp_ou,"%s\t%d",data,hour);
521 if((datetimeby & DATETIME_BYTE)!=0) fprintf(fp_ou,"\t%"PRIu64,(uint64_t)bytes);
522 if((datetimeby & DATETIME_ELAP)!=0) fprintf(fp_ou,"\t%"PRIu64,(uint64_t)elap);
523 fputs("\n",fp_ou);
524
525 if (fclose(fp_ou)==EOF) {
526 debuga(_("Failed to close file %s - %s\n"),wdirname,strerror(errno));
527 exit(EXIT_FAILURE);
528 }
529
530 return;
25697a35
GS
531}
532
533
6588b137 534static 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)
25697a35 535{
9bd92830
FM
536 FILE *fp_ou;
537 char wdirname[MAXLEN];
25697a35 538
6588b137 539 if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return;
25697a35 540
9bd92830
FM
541 if (snprintf(wdirname,sizeof(wdirname),"%s/%s.ip",tmp,uinfo->filename)>=sizeof(wdirname)) {
542 debuga(_("Path too long %s/%s.ip\n"),tmp,uinfo->filename);
543 exit(EXIT_FAILURE);
544 }
25697a35 545
9bd92830
FM
546 if((fp_ou=MY_FOPEN(wdirname,"a"))==NULL){
547 debuga(_("(report) Cannot open file %s\n"),wdirname);
548 exit(EXIT_FAILURE);
549 }
25697a35 550
9bd92830
FM
551 /*
552 This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
553 to print a long long int unless it is exactly 64-bits long.
554 */
555 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);
25697a35 556
9bd92830 557 fclose(fp_ou);
4157aa09 558
9bd92830 559 return;
25697a35
GS
560}
561
562
9dc20988 563static 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)
25697a35 564{
9bd92830
FM
565 /*
566 This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
567 to print a long long int unless it is exactly 64-bits long.
568 */
9dc20988
FM
569 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) {
570 debuga(_("Failed to write a line in %s\n"),filename);
571 exit(EXIT_FAILURE);
572 }
573
574 globstat.nacc+=nacc;
575 globstat.nbytes+=nbytes;
576 globstat.elap+=nelap;
577 globstat.incache+=incache;
578 globstat.oucache+=oucache;
9bd92830 579 return;
25697a35
GS
580}
581
9dc20988
FM
582/*!
583Write the total line at the end of the general file.
584*/
585void totalger(FILE *fp_gen,const char *filename)
586{
587 /*
588 This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
589 to print a long long int unless it is exactly 64-bits long.
590 */
591 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) {
592 debuga(_("Failed to write the total line in %s\n"),filename);
593 exit(EXIT_FAILURE);
594 }
595}
596
2240dcea
FM
597int ger_read(char *buffer,struct generalitemstruct *item,const char *filename)
598{
9bd92830
FM
599 int i;
600 int sign;
601 long long int number;
602
603 if (strncmp(buffer,"TOTAL\t",6)==0) {
604 item->total=1;
605 buffer+=6;
606
607 sign=+1;
608 if (*buffer == '-') {
609 buffer++;
610 sign=-1;
611 } else if (*buffer == '+') {
612 buffer++;
613 }
614 number=0LL;
615 while (isdigit(*buffer))
616 number=(number * 10) + (*buffer++)-'0';
617 if (*buffer!='\t') {
618 debuga(_("Invalid total number of accesses in %s\n"),filename);
619 exit(EXIT_FAILURE);
620 }
621 buffer++;
622 item->nacc=number*sign;
623
624 sign=+1;
625 if (*buffer == '-') {
626 buffer++;
627 sign=-1;
628 } else if (*buffer == '+') {
629 buffer++;
630 }
631 number=0LL;
632 while (isdigit(*buffer))
633 number=(number * 10) + (*buffer++)-'0';
634 if (*buffer!='\t') {
635 debuga(_("Invalid total size in %s\n"),filename);
636 exit(EXIT_FAILURE);
637 }
638 buffer++;
639 item->nbytes=number*sign;
640
641 sign=+1;
642 if (*buffer == '-') {
643 buffer++;
644 sign=-1;
645 } else if (*buffer == '+') {
646 buffer++;
647 }
648 number=0LL;
649 while (isdigit(*buffer))
650 number=(number * 10) + (*buffer++)-'0';
651 if (*buffer!='\t') {
652 debuga(_("Invalid total elapsed time in %s\n"),filename);
653 exit(EXIT_FAILURE);
654 }
655 buffer++;
656 item->nelap=number*sign;
657
658 sign=+1;
659 if (*buffer == '-') {
660 buffer++;
661 sign=-1;
662 } else if (*buffer == '+') {
663 buffer++;
664 }
665 number=0LL;
666 while (isdigit(*buffer))
667 number=(number * 10) + (*buffer++)-'0';
668 if (*buffer!='\t') {
669 debuga(_("Invalid total cache hit in %s\n"),filename);
670 exit(EXIT_FAILURE);
671 }
672 buffer++;
673 item->incache=number*sign;
674
675 sign=+1;
676 if (*buffer == '-') {
677 buffer++;
678 sign=-1;
679 } else if (*buffer == '+') {
680 buffer++;
681 }
682 number=0LL;
683 while (isdigit(*buffer))
684 number=(number * 10) + (*buffer++)-'0';
685 if (*buffer!='\0') {
686 debuga(_("Invalid total cache miss in %s\n"),filename);
687 exit(EXIT_FAILURE);
688 }
689 item->oucache=number*sign;
690 } else {
691 item->total=0;
692
693 item->user=buffer;
694 for (i=0 ; i<MAX_USER_LEN-1 && (unsigned char)*buffer>=' ' ; i++) buffer++;
695 if (*buffer!='\t') {
696 debuga(_("User name too long or invalid in %s\n"),filename);
697 exit(EXIT_FAILURE);
698 }
699 *buffer++='\0';
700
701 sign=+1;
702 if (*buffer == '-') {
703 buffer++;
704 sign=-1;
705 } else if (*buffer == '+') {
706 buffer++;
707 }
708 number=0LL;
709 while (isdigit(*buffer))
710 number=(number * 10) + (*buffer++)-'0';
711 if (*buffer!='\t') {
712 debuga(_("Invalid number of accesses in %s\n"),filename);
713 exit(EXIT_FAILURE);
714 }
715 buffer++;
716 item->nacc=number*sign;
717
718 sign=+1;
719 if (*buffer == '-') {
720 buffer++;
721 sign=-1;
722 } else if (*buffer == '+') {
723 buffer++;
724 }
725 number=0LL;
726 while (isdigit(*buffer))
727 number=(number * 10) + (*buffer++)-'0';
728 if (*buffer!='\t') {
729 debuga(_("Invalid number of bytes in %s\n"),filename);
730 exit(EXIT_FAILURE);
731 }
732 buffer++;
733 item->nbytes=number*sign;
734
735 item->url=buffer;
736 while ((unsigned char)*buffer>=' ') buffer++;
737 if (*buffer!='\t') {
738 debuga(_("URL too long or invalid in %s\n"),filename);
739 exit(EXIT_FAILURE);
740 }
741 *buffer++='\0';
742
743 item->ip=buffer;
744 for (i=0 ; i<MAX_IP_LEN-1 && (unsigned char)*buffer>=' ' ; i++) buffer++;
745 if (*buffer!='\t') {
746 debuga(_("IP address too long or invalid in %s\n"),filename);
747 exit(EXIT_FAILURE);
748 }
749 *buffer++='\0';
750
751 item->time=buffer;
752 for (i=0 ; i<MAX_DATETIME_LEN-1 && (unsigned char)*buffer>=' ' ; i++) buffer++;
753 if (*buffer!='\t') {
754 debuga(_("Time too long or invalid in %s\n"),filename);
755 exit(EXIT_FAILURE);
756 }
757 *buffer++='\0';
758
759 item->date=buffer;
760 for (i=0 ; i<MAX_DATETIME_LEN-1 && (unsigned char)*buffer>=' ' ; i++) buffer++;
761 if (*buffer!='\t') {
762 debuga(_("Date too long or invalid in %s\n"),filename);
763 exit(EXIT_FAILURE);
764 }
765 *buffer++='\0';
766
767 sign=+1;
768 if (*buffer == '-') {
769 buffer++;
770 sign=-1;
771 } else if (*buffer == '+') {
772 buffer++;
773 }
774 number=0LL;
775 while (isdigit(*buffer))
776 number=(number * 10) + (*buffer++)-'0';
777 if (*buffer!='\t') {
778 debuga(_("Invalid elapsed time in %s\n"),filename);
779 exit(EXIT_FAILURE);
780 }
781 buffer++;
782 item->nelap=number*sign;
783
784 sign=+1;
785 if (*buffer == '-') {
786 buffer++;
787 sign=-1;
788 } else if (*buffer == '+') {
789 buffer++;
790 }
791 number=0LL;
792 while (isdigit(*buffer))
793 number=(number * 10) + (*buffer++)-'0';
794 if (*buffer!='\t') {
795 debuga(_("Invalid cache hit size in %s\n"),filename);
796 exit(EXIT_FAILURE);
797 }
798 buffer++;
799 item->incache=number*sign;
800
801 sign=+1;
802 if (*buffer == '-') {
803 buffer++;
804 sign=-1;
805 } else if (*buffer == '+') {
806 buffer++;
807 }
808 number=0LL;
809 while (isdigit(*buffer))
810 number=(number * 10) + (*buffer++)-'0';
811 if (*buffer!='\0') {
812 debuga(_("Invalid cache miss size in %s\n"),filename);
813 exit(EXIT_FAILURE);
814 }
815 item->oucache=number*sign;
816 }
817 return(0);
2240dcea
FM
818}
819
5160b452 820static void grava_SmartFilter(const char *dirname, const char *user, const char *ip, const char *data, const char *hora, const char *url, const char *smart)
25697a35 821{
9bd92830
FM
822 FILE *fp_ou;
823 char wdirname[MAXLEN];
25697a35 824
9bd92830 825 sprintf(wdirname,"%s/smartfilter.unsort",dirname);
25697a35 826
9bd92830
FM
827 if((fp_ou=MY_FOPEN(wdirname,"a"))==NULL){
828 debuga(_("(report) Cannot open file %s\n"),wdirname);
829 exit(EXIT_FAILURE);
830 }
25697a35 831
9bd92830
FM
832 fprintf(fp_ou,"%s\t%s\t%s\t%s\t%s\t%s\n",user,data,hora,ip,url,smart);
833 fputs("</body>\n</html>\n",fp_tt);
25697a35 834
9bd92830 835 fclose(fp_ou);
25697a35 836
9bd92830 837 return;
25697a35 838}