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