sarg  2.4.0
About: SARG ia a Squid Analysis Report Generator.
  Fossies Dox: sarg-2.4.0.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

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