]> git.ipfire.org Git - thirdparty/sarg.git/blob - denied.c
Plot the bytes graphs with the same scale as the text report
[thirdparty/sarg.git] / denied.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 gen_denied_report(void)
31 {
32
33 FILE *fp_in = NULL, *fp_ou = NULL;
34
35 char *buf;
36 char *url;
37 char denied_in[MAXLEN];
38 char report[MAXLEN];
39 char ip[MAXLEN];
40 char oip[MAXLEN];
41 char user[MAXLEN];
42 char ouser[MAXLEN];
43 char ouser2[MAXLEN];
44 char data[15];
45 char hora[15];
46 bool z=false;
47 int count=0;
48 int day,month,year;
49 bool new_user;
50 struct getwordstruct gwarea;
51 longline line;
52 struct userinfostruct *uinfo;
53 struct tm t;
54
55 ouser[0]='\0';
56 ouser2[0]='\0';
57
58 sprintf(denied_in,"%s/sarg/denied.log",TempDir);
59 if(!denied_count) {
60 unlink(denied_in);
61 return;
62 }
63
64 sprintf(report,"%s/denied.html",outdirname);
65
66 if((fp_in=MY_FOPEN(denied_in,"r"))==NULL) {
67 debuga(_("(denied) Cannot open log file %s\n"),denied_in);
68 exit(EXIT_FAILURE);
69 }
70
71 if((fp_ou=MY_FOPEN(report,"w"))==NULL) {
72 debuga(_("(denied) Cannot open log file %s\n"),report);
73 exit(EXIT_FAILURE);
74 }
75
76 write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Denied"));
77 fputs("<tr><td class=\"header_c\">",fp_ou);
78 fprintf(fp_ou,_("Period: %s"),period.html);
79 fputs("</td></tr>\n",fp_ou);
80 fprintf(fp_ou,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Denied"));
81 close_html_header(fp_ou);
82
83 fputs("<div class=\"report\"><table cellpadding=\"0\" cellspacing=\"2\">\n",fp_ou);
84 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"));
85
86 if ((line=longline_create())==NULL) {
87 debuga(_("Not enough memory to read the denied accesses\n"));
88 exit(EXIT_FAILURE);
89 }
90
91 while((buf=longline_read(fp_in,line))!=NULL) {
92 getword_start(&gwarea,buf);
93 if (getword(data,sizeof(data),&gwarea,'\t')<0 || getword(hora,sizeof(hora),&gwarea,'\t')<0 ||
94 getword(user,sizeof(user),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0) {
95 debuga(_("There is a broken record or garbage in file %s\n"),denied_in);
96 exit(EXIT_FAILURE);
97 }
98 if (getword_ptr(buf,&url,&gwarea,'\t')<0) {
99 debuga(_("There is a broken url in file %s\n"),denied_in);
100 exit(EXIT_FAILURE);
101 }
102 if (sscanf(data,"%d/%d/%d",&day,&month,&year)!=3) continue;
103 computedate(year,month,day,&t);
104 strftime(data,sizeof(data),"%x",&t);
105
106 uinfo=userinfo_find_from_id(user);
107 if (!uinfo) {
108 debuga(_("Unknown user ID %s in file %s\n"),user,denied_in);
109 exit(EXIT_FAILURE);
110 }
111
112 new_user=false;
113 if(!z) {
114 strcpy(ouser,user);
115 strcpy(oip,ip);
116 z=true;
117 new_user=true;
118 } else {
119 if(strcmp(ouser,user) != 0) {
120 strcpy(ouser,user);
121 new_user=true;
122 }
123 if(strcmp(oip,ip) != 0) {
124 strcpy(oip,ip);
125 new_user=true;
126 }
127 }
128
129 if(DeniedReportLimit) {
130 if(strcmp(ouser2,uinfo->label) == 0) {
131 count++;
132 } else {
133 count=1;
134 strcpy(ouser2,uinfo->label);
135 }
136 if(count >= DeniedReportLimit)
137 continue;
138 }
139
140 fputs("<tr>",fp_ou);
141 if (new_user)
142 fprintf(fp_ou,"<td class=\"data\">%s</td><td class=\"data\">%s</td>",uinfo->label,ip);
143 else
144 fputs("<td class=\"data\"></td><td class=\"data\"></td>",fp_ou);
145 fprintf(fp_ou,"<td class=\"data\">%s-%s</td><td class=\"data2\">",data,hora);
146 if(BlockIt[0] != '\0') {
147 fprintf(fp_ou,"<a href=\"%s%s?url=",wwwDocumentRoot,BlockIt);
148 output_html_url(fp_ou,url);
149 fprintf(fp_ou,"\"><img src=\"%s/sarg-squidguard-block.png\"></a>&nbsp;",ImageFile);
150 }
151 fputs("<a href=\"http://",fp_ou);
152 output_html_url(fp_ou,url);
153 fputs("\">http://",fp_ou);
154 output_html_string(fp_ou,url,100);
155 fputs("</a></td></tr>\n",fp_ou);
156 }
157 fclose(fp_in);
158 longline_destroy(&line);
159
160 fputs("</table></div>\n",fp_ou);
161 if (write_html_trailer(fp_ou)<0)
162 debuga(_("Write error in file %s\n"),report);
163 if (fclose(fp_ou)<0)
164 debuga(_("Failed to close file %s - %s\n"),report,strerror(errno));
165
166 unlink(denied_in);
167
168 return;
169 }