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