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