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)  

dansguardian_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 
30 static void show_ignored_dansguardian(FILE *fp_ou,int count)
31 {
32  char ignored[80];
33 
34  snprintf(ignored,sizeof(ignored),ngettext("%d more dansguardian entry not shown here…","%d more dansguardian entries not shown here…",count),count);
35  fprintf(fp_ou,"<tr><td class=\"data\"></td><td class=\"data\"></td><td class=\"data\"></td><td class=\"data2 more\">%s</td><td class=\"data\"></td></tr>\n",ignored);
36 }
37 
39 {
40  FILE *fp_in = NULL, *fp_ou = NULL;
41 
42  char buf[MAXLEN];
43  char *url;
44  char dansguardian_in[MAXLEN];
45  char report[MAXLEN];
46  char ip[MAXLEN];
47  char rule[255];
48  char oip[MAXLEN];
49  char user[MAXLEN];
50  char ouser[MAXLEN];
51  char date[15];
52  char date2[15];
53  char hour[15];
54  char ouser2[255];
55  int z=0;
56  int count=0;
57  struct getwordstruct gwarea;
58 
59  ouser[0]='\0';
60 
61  format_path(__FILE__, __LINE__, dansguardian_in, sizeof(dansguardian_in), "%s/dansguardian.int_log", tmp);
62  if (!dansguardian_count) {
63  if (!KeepTempLog && unlink(dansguardian_in))
64  debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),dansguardian_in,strerror(errno));
65  if (debugz>=LogLevel_Process) debugaz(__FILE__,__LINE__,_("Dansguardian report not generated because it is empty\n"));
66  return;
67  }
68 
69  format_path(__FILE__, __LINE__, report, sizeof(report), "%s/dansguardian.html", outdirname);
70 
71  if ((fp_in=MY_FOPEN(dansguardian_in,"r"))==NULL) {
72  debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),dansguardian_in,strerror(errno));
73  exit(EXIT_FAILURE);
74  }
75 
76  if ((fp_ou=MY_FOPEN(report,"w"))==NULL) {
77  debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),report,strerror(errno));
78  exit(EXIT_FAILURE);
79  }
80 
81  write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("DansGuardian"),HTML_JS_NONE);
82  fputs("<tr><td class=\"header_c\">",fp_ou);
83  fprintf(fp_ou,_("Period: %s"),period.html);
84  fputs("</td></tr>\n",fp_ou);
85  fprintf(fp_ou,"<tr><th class=\"header_c\">%s</th></tr>\n",_("DansGuardian"));
86  close_html_header(fp_ou);
87 
88  fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing=\"2\">\n",fp_ou);
89  fprintf(fp_ou,"<tr><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr>\n",_("USERID"),_("IP/NAME"),_("DATE/TIME"),_("ACCESSED SITE"),_("CAUSE"));
90 
91  while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
92  getword_start(&gwarea,buf);
93  if (getword(user,sizeof(user),&gwarea,'\t')<0 || getword(date2,sizeof(date2),&gwarea,'\t')<0 ||
94  getword(hour,sizeof(hour),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0) {
95  debuga(__FILE__,__LINE__,_("Invalid record in file \"%s\"\n"),dansguardian_in);
96  exit(EXIT_FAILURE);
97  }
98  if (getword_ptr(buf,&url,&gwarea,'\t')<0) {
99  debuga(__FILE__,__LINE__,_("Invalid url in file \"%s\"\n"),dansguardian_in);
100  exit(EXIT_FAILURE);
101  }
102  if (getword(rule,sizeof(rule),&gwarea,'\n')<0) {
103  debuga(__FILE__,__LINE__,_("Invalid rule in file \"%s\"\n"),dansguardian_in);
104  exit(EXIT_FAILURE);
105  }
106 
107  if (UserIp)
108  strcpy(user,ip);
109 
110  memset(date,0,sizeof(date));
111  if (df!='u') {
112  strncpy(date,date2+6,2);
113  strcat(date,"/");
114  strncat(date,date2+4,2);
115  strcat(date,"/");
116  strncat(date,date2,4);
117  } else {
118  strncpy(date,date2+4,2);
119  strcat(date,"/");
120  strncat(date,date2+6,2);
121  strcat(date,"/");
122  strncat(date,date2,4);
123  }
124 
125  if (Ip2Name)
126  ip2name(ip,sizeof(ip));
127 
128  if (!z) {
129  strcpy(ouser,user);
130  strcpy(oip,ip);
131  z++;
132  } else {
133  if (strcmp(ouser,user) == 0)
134  user[0]='\0';
135  if (user[0] != '\0')
136  strcpy(ouser,user);
137  if (strcmp(oip,ip) == 0)
138  ip[0]='\0';
139  if (ip[0] != '\0')
140  strcpy(oip,ip);
141  }
142 
143  user_find(name, sizeof(name), user);
144 
146  if (strcmp(ouser2,name) == 0) {
147  count++;
148  } else {
151  count=1;
152  strcpy(ouser2,name);
153  }
154  if (count > DansGuardianReportLimit)
155  continue;
156  }
157 
158  fprintf(fp_ou,"<tr><td class=\"data2\">%s</td><td class=\"data2\">%s</td><td class=\"data2\">%s-%s</td><td class=\"data2\">",name,ip,date,hour);
159  output_html_link(fp_ou,url,100);
160  fprintf(fp_ou,"</td><td class=\"data2\">%s</td></tr>\n",rule);
161  }
162  if (fclose(fp_in)==EOF) {
163  debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),dansguardian_in,strerror(errno));
164  exit(EXIT_FAILURE);
165  }
166 
169 
170  fputs("</table></div>\n",fp_ou);
171  write_html_trailer(fp_ou);
172  if (fclose(fp_ou)==EOF) {
173  debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),report,strerror(errno));
174  exit(EXIT_FAILURE);
175  }
176 
177  if (!KeepTempLog && unlink(dansguardian_in)) {
178  debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),dansguardian_in,strerror(errno));
179  exit(EXIT_FAILURE);
180  }
181 
182  return;
183 }
getword_start
void getword_start(struct getwordstruct *gwarea, const char *line)
Definition: util.c:74
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
dansguardian_report
void dansguardian_report(void)
Definition: dansguardian_report.c:38
format_path
int format_path(const char *file, int line, char *output_buffer, int buffer_size, const char *format,...)
Definition: util.c:2665
KeepTempLog
bool KeepTempLog
True to keep the temporary files for inspection.
Definition: conf.h:505
MY_FOPEN
#define MY_FOPEN
Definition: conf.h:129
_
#define _(String)
Definition: conf.h:155
ip2name
void ip2name(char *ip, int ip_len)
Definition: ip2name.c:216
MAXLEN
#define MAXLEN
Definition: conf.h:176
INDEX_TREE_DATE
#define INDEX_TREE_DATE
Definition: conf.h:240
name
char name[20000]
Definition: conf.h:335
HTML_JS_NONE
#define HTML_JS_NONE
Value to exclude all the javascripts from the html page.
Definition: conf.h:285
show_ignored_dansguardian
static void show_ignored_dansguardian(FILE *fp_ou, int count)
Definition: dansguardian_report.c:30
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
IndexTree
unsigned long int IndexTree
How to display the index of the reports.
Definition: conf.h:416
periodstruct::html
char html[90]
The HTML representation of the date.
Definition: conf.h:307
getword
int getword(char *word, int limit, struct getwordstruct *gwarea, char stop)
Definition: util.c:90
conf.h
Include headers and define global variables. */.
df
char df
Definition: conf.h:319
getwordstruct
Definition: defs.h:26
dansguardian_count
int dansguardian_count
Definition: conf.h:478
debugaz
void debugaz(const char *File, int Line, const char *msg,...)
Definition: util.c:646
write_html_trailer
void write_html_trailer(FILE *fp_ou)
Definition: util.c:2157
tmp
char tmp[20000]
Definition: conf.h:315
defs.h
Declaration of the structures and functions.
ngettext
#define ngettext(Msgid1, Msgid2, N)
Definition: gettext.h:75
outdirname
char outdirname[20000]
Definition: conf.h:311
user_find
void user_find(char *mappedname, int namelen, const char *userlogin)
Definition: usertab.c:460
DansGuardianReportLimit
int DansGuardianReportLimit
Definition: conf.h:495
debugz
int debugz
Definition: conf.h:490
period
struct periodstruct period
Definition: conf.h:312
UserIp
bool UserIp
Definition: conf.h:365
Ip2Name
bool Ip2Name
Definition: conf.h:337
output_html_link
void output_html_link(FILE *fp_ou, const char *url, int maxlen)
Definition: util.c:2214
LogLevel_Process
@ LogLevel_Process
Process informational messages.
Definition: defs.h:15