]> git.ipfire.org Git - thirdparty/sarg.git/blame - denied.c
Report any error while reading the day summary file
[thirdparty/sarg.git] / denied.c
CommitLineData
25697a35 1/*
94ff9470 2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
fbd133bb 3 * 1998, 2011
25697a35
GS
4 *
5 * SARG donations:
6 * please look at http://sarg.sourceforge.net/donations.php
1164c474
FM
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
25697a35
GS
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"
5f3cfd1d 28#include "include/defs.h"
25697a35 29
f209e4f0
FM
30static void show_ignored_denied(FILE *fp_ou,int count)
31{
9bd92830 32 char ignored[80];
f209e4f0 33
9bd92830 34 snprintf(ignored,sizeof(ignored),ngettext("%d more denied access not shown here…","%d more denied accesses not shown here…",count),count);
7ae50eee 35 fprintf(fp_ou,"<tr><td class=\"data\"></td><td class=\"data\"></td><td class=\"data\"></td><td class=\"data2 more\">%s</td></tr>\n",ignored);
f209e4f0
FM
36}
37
32e71fa4 38void gen_denied_report(void)
25697a35 39{
9bd92830
FM
40 FILE *fp_in = NULL, *fp_ou = NULL;
41
42 char *buf;
43 char *url;
44 char denied_in[MAXLEN];
45 char report[MAXLEN];
46 char ip[MAXLEN];
47 char oip[MAXLEN];
48 char user[MAXLEN];
49 char ouser[MAXLEN];
50 char ouser2[MAXLEN];
51 char data[15];
52 char hora[15];
53 bool z=false;
54 int count=0;
55 int day,month,year;
56 bool new_user;
57 struct getwordstruct gwarea;
58 longline line;
59 struct userinfostruct *uinfo;
60 struct tm t;
61
62 ouser[0]='\0';
63 ouser2[0]='\0';
64
65 sprintf(denied_in,"%s/denied.log",tmp);
08f9b029 66 if (!denied_count) {
9bd92830 67 unlink(denied_in);
9f93fec3 68 if (debugz) debugaz(_("Denied report not produced because it is empty\n"));
9bd92830
FM
69 return;
70 }
71
72 sprintf(report,"%s/denied.html",outdirname);
73
74 if((fp_in=MY_FOPEN(denied_in,"r"))==NULL) {
007905af
FM
75 debuga(_("(denied) Cannot open log file %s\n"),denied_in);
76 exit(EXIT_FAILURE);
9bd92830
FM
77 }
78
79 if((fp_ou=MY_FOPEN(report,"w"))==NULL) {
007905af
FM
80 debuga(_("(denied) Cannot open log file %s\n"),report);
81 exit(EXIT_FAILURE);
9bd92830
FM
82 }
83
84 write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Denied"),HTML_JS_NONE);
85 fputs("<tr><td class=\"header_c\">",fp_ou);
86 fprintf(fp_ou,_("Period: %s"),period.html);
87 fputs("</td></tr>\n",fp_ou);
88 fprintf(fp_ou,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Denied"));
89 close_html_header(fp_ou);
90
91 fputs("<div class=\"report\"><table cellpadding=\"0\" cellspacing=\"2\">\n",fp_ou);
92 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></tr>\n",_("USERID"),_("IP/NAME"),_("DATE/TIME"),_("ACCESSED SITE"));
93
94 if ((line=longline_create())==NULL) {
95 debuga(_("Not enough memory to read the denied accesses\n"));
96 exit(EXIT_FAILURE);
97 }
98
99 while((buf=longline_read(fp_in,line))!=NULL) {
100 getword_start(&gwarea,buf);
101 if (getword(data,sizeof(data),&gwarea,'\t')<0 || getword(hora,sizeof(hora),&gwarea,'\t')<0 ||
007905af 102 getword(user,sizeof(user),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0) {
9bd92830
FM
103 debuga(_("There is a broken record or garbage in file %s\n"),denied_in);
104 exit(EXIT_FAILURE);
105 }
106 if (getword_ptr(buf,&url,&gwarea,'\t')<0) {
107 debuga(_("There is a broken url in file %s\n"),denied_in);
108 exit(EXIT_FAILURE);
109 }
110 if (sscanf(data,"%d/%d/%d",&day,&month,&year)!=3) continue;
111 computedate(year,month,day,&t);
112 strftime(data,sizeof(data),"%x",&t);
113
114 uinfo=userinfo_find_from_id(user);
115 if (!uinfo) {
116 debuga(_("Unknown user ID %s in file %s\n"),user,denied_in);
117 exit(EXIT_FAILURE);
118 }
119
120 new_user=false;
121 if(!z) {
122 strcpy(ouser,user);
123 strcpy(oip,ip);
124 z=true;
125 new_user=true;
126 } else {
127 if(strcmp(ouser,user) != 0) {
128 strcpy(ouser,user);
129 new_user=true;
130 }
131 if(strcmp(oip,ip) != 0) {
132 strcpy(oip,ip);
133 new_user=true;
134 }
135 }
136
137 if(DeniedReportLimit) {
138 if(strcmp(ouser2,uinfo->label) == 0) {
139 count++;
140 } else {
141 if(count>DeniedReportLimit && DeniedReportLimit>0)
142 show_ignored_denied(fp_ou,count-DeniedReportLimit);
143 count=1;
144 strcpy(ouser2,uinfo->label);
145 }
146 if(count > DeniedReportLimit)
147 continue;
148 }
149
150 fputs("<tr>",fp_ou);
151 if (new_user)
152 fprintf(fp_ou,"<td class=\"data\"><a href=\"%s/%s.html\">%s</a></td><td class=\"data\">%s</td>",uinfo->filename,uinfo->filename,uinfo->label,ip);
153 else
154 fputs("<td class=\"data\"></td><td class=\"data\"></td>",fp_ou);
155 fprintf(fp_ou,"<td class=\"data\">%s-%s</td><td class=\"data2\">",data,hora);
67a93701 156 if(BlockIt[0] != '\0' && url[0]!=ALIAS_PREFIX) {
9bd92830
FM
157 fprintf(fp_ou,"<a href=\"%s%s?url=",wwwDocumentRoot,BlockIt);
158 output_html_url(fp_ou,url);
159 fprintf(fp_ou,"\"><img src=\"%s/sarg-squidguard-block.png\"></a>&nbsp;",ImageFile);
160 }
58c40ade 161 output_html_link(fp_ou,"",url,100); //the URL contains the scheme
67a93701 162 fputs("</td></tr>\n",fp_ou);
9bd92830
FM
163 }
164 fclose(fp_in);
165 longline_destroy(&line);
166
167 if(count>DeniedReportLimit && DeniedReportLimit>0)
168 show_ignored_denied(fp_ou,count-DeniedReportLimit);
169
170 fputs("</table></div>\n",fp_ou);
171 if (write_html_trailer(fp_ou)<0)
172 debuga(_("Write error in file %s\n"),report);
173 if (fclose(fp_ou)<0)
174 debuga(_("Failed to close file %s - %s\n"),report,strerror(errno));
175
176 if (unlink(denied_in)==-1)
177 debuga(_("Failed to delete the file \"%s\" after processing it - %s\n"),denied_in,strerror(errno));
178
179 return;
25697a35 180}