]> git.ipfire.org Git - thirdparty/sarg.git/blob - squidguard_report.c
31aadb17f2e0f371a49d2b9bb937a0701a0b962f
[thirdparty/sarg.git] / squidguard_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 void squidguard_report(void)
31 {
32
33 FILE *fp_in = NULL, *fp_ou = NULL;
34
35 char *buf;
36 char *url;
37 char squidguard_in[MAXLEN];
38 char report[MAXLEN];
39 char ip[MAXLEN];
40 char rule[255];
41 char oip[MAXLEN];
42 char user[MAXLEN];
43 char ouser[MAXLEN];
44 char data[15];
45 char hora[15];
46 char ouser2[255];
47 char oname[MAXLEN];
48 bool z=false;
49 int count=0;
50 long long int data2;
51 bool new_user;
52 struct getwordstruct gwarea;
53 const struct userinfostruct *uinfo;
54 struct tm t;
55 longline line;
56
57 ouser[0]='\0';
58 ouser2[0]='\0';
59
60 sprintf(squidguard_in,"%s/redirector.log",tmp);
61 if(!squidguard_count) {
62 unlink(squidguard_in);
63 return;
64 }
65
66 snprintf(report,sizeof(report),"%s/redirector.html",outdirname);
67
68 if((fp_in=fopen(squidguard_in,"r"))==NULL) {
69 debuga(_("(squidguard) Cannot open log file %s\n"),squidguard_in);
70 exit(EXIT_FAILURE);
71 }
72
73 if((fp_ou=fopen(report,"w"))==NULL) {
74 debuga(_("(squidguard) Cannot open log file %s\n"),report);
75 exit(EXIT_FAILURE);
76 }
77
78 if ((line=longline_create())==NULL) {
79 debuga(_("Not enough memory to read the processed redirector log\n"));
80 exit(EXIT_FAILURE);
81 }
82
83 write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Redirector report"),HTML_JS_NONE);
84 fputs("<tr><td class=\"header_c\">",fp_ou);
85 fprintf(fp_ou,_("Period: %s"),period.html);
86 fputs("</td></tr>\n",fp_ou);
87 fprintf(fp_ou,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Redirector report"));
88 close_html_header(fp_ou);
89
90 fputs("<div class=\"report\"><table cellpadding=1 cellspacing=2>\n",fp_ou);
91 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"),_("RULE"));
92
93 while((buf=longline_read(fp_in,line))!=NULL) {
94 getword_start(&gwarea,buf);
95 if (getword(user,sizeof(user),&gwarea,'\t')<0) {
96 debuga(_("Invalid user in file %s\n"),squidguard_in);
97 exit(EXIT_FAILURE);
98 }
99 if (getword_atoll(&data2,&gwarea,'\t')<0) {
100 debuga(_("Invalid date in file %s\n"),squidguard_in);
101 exit(EXIT_FAILURE);
102 }
103 if (getword(hora,sizeof(hora),&gwarea,'\t')<0) {
104 debuga(_("Invalid time in file %s\n"),squidguard_in);
105 exit(EXIT_FAILURE);
106 }
107 if (getword(ip,sizeof(ip),&gwarea,'\t')<0) {
108 debuga(_("Invalid IP address in file %s\n"),squidguard_in);
109 exit(EXIT_FAILURE);
110 }
111 if (getword_ptr(buf,&url,&gwarea,'\t')<0) {
112 debuga(_("Invalid URL in file %s\n"),squidguard_in);
113 exit(EXIT_FAILURE);
114 }
115 if (getword(rule,sizeof(rule),&gwarea,'\n')<0) {
116 debuga(_("Invalid rule in file %s\n"),squidguard_in);
117 exit(EXIT_FAILURE);
118 }
119
120 uinfo=userinfo_find_from_id(user);
121 if (!uinfo) {
122 debuga(_("Unknown user ID %s in file %s\n"),user,squidguard_in);
123 exit(EXIT_FAILURE);
124 }
125
126 computedate(data2/10000,(data2/100)%10,data2%100,&t);
127 strftime(data,sizeof(data),"%x",&t);
128
129 new_user=false;
130 if(!z) {
131 strcpy(ouser,user);
132 strcpy(oip,ip);
133 strcpy(oname,ip);
134 if (Ip2Name && !uinfo->id_is_ip) ip2name(oname,sizeof(oname));
135 z=true;
136 new_user=true;
137 } else {
138 if(strcmp(ouser,user) != 0) {
139 strcpy(ouser,user);
140 new_user=true;
141 }
142 if(strcmp(oip,ip) != 0) {
143 strcpy(oip,ip);
144 strcpy(oname,ip);
145 if (Ip2Name && !uinfo->id_is_ip) ip2name(oname,sizeof(oname));
146 new_user=true;
147 }
148 }
149
150 if(SquidGuardReportLimit) {
151 if(strcmp(ouser2,uinfo->label) == 0) {
152 count++;
153 } else {
154 count=1;
155 strcpy(ouser2,uinfo->label);
156 }
157 if(count >= SquidGuardReportLimit)
158 continue;
159 }
160
161 if (new_user)
162 fprintf(fp_ou,"<tr><td class=\"data2\">%s</td><td class=\"data2\">%s</td>",uinfo->label,ip);
163 else
164 fputs("<tr><td class=\"data2\"></td><td class=\"data2\"></td>",fp_ou);
165 fprintf(fp_ou,"<td class=\"data2\">%s-%s</td><td class=\"data2\"><a href=\"http://",data,hora);
166 output_html_url(fp_ou,url);
167 fputs("\">",fp_ou);
168 output_html_string(fp_ou,url,100);
169 fprintf(fp_ou,"</a></td><td class=\"data2\">%s</td></tr>\n",rule);
170 }
171 fclose(fp_in);
172 longline_destroy(&line);
173
174 fputs("</table></div>\n",fp_ou);
175 if (write_html_trailer(fp_ou)<0)
176 debuga(_("Write error in file %s\n"),report);
177 if (fclose(fp_ou)==EOF)
178 debuga(_("Failed to close file %s - %s\n"),report,strerror(errno));
179
180 unlink(squidguard_in);
181
182 return;
183 }