]> git.ipfire.org Git - thirdparty/sarg.git/blob - denied.c
Fix indentation of the code
[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 static void show_ignored_denied(FILE *fp_ou,int count)
31 {
32 char ignored[80];
33
34 snprintf(ignored,sizeof(ignored),ngettext("%d more denied access not shown here…","%d more denied accesses 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\"><i>%s</i></td></tr>\n",ignored);
36 }
37
38 void gen_denied_report(void)
39 {
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);
66 if(!denied_count) {
67 unlink(denied_in);
68 return;
69 }
70
71 sprintf(report,"%s/denied.html",outdirname);
72
73 if((fp_in=MY_FOPEN(denied_in,"r"))==NULL) {
74 debuga(_("(denied) Cannot open log file %s\n"),denied_in);
75 exit(EXIT_FAILURE);
76 }
77
78 if((fp_ou=MY_FOPEN(report,"w"))==NULL) {
79 debuga(_("(denied) Cannot open log file %s\n"),report);
80 exit(EXIT_FAILURE);
81 }
82
83 write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Denied"),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",_("Denied"));
88 close_html_header(fp_ou);
89
90 fputs("<div class=\"report\"><table cellpadding=\"0\" 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></tr>\n",_("USERID"),_("IP/NAME"),_("DATE/TIME"),_("ACCESSED SITE"));
92
93 if ((line=longline_create())==NULL) {
94 debuga(_("Not enough memory to read the denied accesses\n"));
95 exit(EXIT_FAILURE);
96 }
97
98 while((buf=longline_read(fp_in,line))!=NULL) {
99 getword_start(&gwarea,buf);
100 if (getword(data,sizeof(data),&gwarea,'\t')<0 || getword(hora,sizeof(hora),&gwarea,'\t')<0 ||
101 getword(user,sizeof(user),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0) {
102 debuga(_("There is a broken record or garbage in file %s\n"),denied_in);
103 exit(EXIT_FAILURE);
104 }
105 if (getword_ptr(buf,&url,&gwarea,'\t')<0) {
106 debuga(_("There is a broken url in file %s\n"),denied_in);
107 exit(EXIT_FAILURE);
108 }
109 if (sscanf(data,"%d/%d/%d",&day,&month,&year)!=3) continue;
110 computedate(year,month,day,&t);
111 strftime(data,sizeof(data),"%x",&t);
112
113 uinfo=userinfo_find_from_id(user);
114 if (!uinfo) {
115 debuga(_("Unknown user ID %s in file %s\n"),user,denied_in);
116 exit(EXIT_FAILURE);
117 }
118
119 new_user=false;
120 if(!z) {
121 strcpy(ouser,user);
122 strcpy(oip,ip);
123 z=true;
124 new_user=true;
125 } else {
126 if(strcmp(ouser,user) != 0) {
127 strcpy(ouser,user);
128 new_user=true;
129 }
130 if(strcmp(oip,ip) != 0) {
131 strcpy(oip,ip);
132 new_user=true;
133 }
134 }
135
136 if(DeniedReportLimit) {
137 if(strcmp(ouser2,uinfo->label) == 0) {
138 count++;
139 } else {
140 if(count>DeniedReportLimit && DeniedReportLimit>0)
141 show_ignored_denied(fp_ou,count-DeniedReportLimit);
142 count=1;
143 strcpy(ouser2,uinfo->label);
144 }
145 if(count > DeniedReportLimit)
146 continue;
147 }
148
149 fputs("<tr>",fp_ou);
150 if (new_user)
151 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);
152 else
153 fputs("<td class=\"data\"></td><td class=\"data\"></td>",fp_ou);
154 fprintf(fp_ou,"<td class=\"data\">%s-%s</td><td class=\"data2\">",data,hora);
155 if(BlockIt[0] != '\0') {
156 fprintf(fp_ou,"<a href=\"%s%s?url=",wwwDocumentRoot,BlockIt);
157 output_html_url(fp_ou,url);
158 fprintf(fp_ou,"\"><img src=\"%s/sarg-squidguard-block.png\"></a>&nbsp;",ImageFile);
159 }
160 fputs("<a href=\"http://",fp_ou);
161 output_html_url(fp_ou,url);
162 fputs("\">http://",fp_ou);
163 output_html_string(fp_ou,url,100);
164 fputs("</a></td></tr>\n",fp_ou);
165 }
166 fclose(fp_in);
167 longline_destroy(&line);
168
169 if(count>DeniedReportLimit && DeniedReportLimit>0)
170 show_ignored_denied(fp_ou,count-DeniedReportLimit);
171
172 fputs("</table></div>\n",fp_ou);
173 if (write_html_trailer(fp_ou)<0)
174 debuga(_("Write error in file %s\n"),report);
175 if (fclose(fp_ou)<0)
176 debuga(_("Failed to close file %s - %s\n"),report,strerror(errno));
177
178 if (unlink(denied_in)==-1)
179 debuga(_("Failed to delete the file \"%s\" after processing it - %s\n"),denied_in,strerror(errno));
180
181 return;
182 }