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