]> git.ipfire.org Git - thirdparty/sarg.git/blob - denied.c
Remove old commented out code
[thirdparty/sarg.git] / denied.c
1 /*
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
3 * 1998, 2012
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 //! Name of the file containing the unsorted denied entries.
31 static char denied_unsort[MAXLEN]="";
32 //! Name of the file containing the sorted denied entries.
33 static char denied_sort[MAXLEN]="";
34
35 /*!
36 Open a file to store the denied accesses.
37
38 \return The file handle or NULL if no file is necessary.
39 */
40 FILE *denied_open(void)
41 {
42 FILE *fp_denied;
43
44 if((ReportType & REPORT_TYPE_DENIED) == 0) return(NULL);
45
46 snprintf(denied_unsort,sizeof(denied_unsort),"%s/denied.int_unsort",tmp);
47 if ((fp_denied=MY_FOPEN(denied_unsort,"w"))==NULL) {
48 debuga(_("(log) Cannot open file: %s - %s\n"),denied_unsort,strerror(errno));
49 exit(EXIT_FAILURE);
50 }
51 return(fp_denied);
52 }
53
54 static void show_ignored_denied(FILE *fp_ou,int count)
55 {
56 char ignored[80];
57
58 snprintf(ignored,sizeof(ignored),ngettext("%d more denied access not shown here…","%d more denied accesses not shown here…",count),count);
59 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);
60 }
61
62 /*!
63 Generate a report containing the denied accesses.
64 */
65 void gen_denied_report(void)
66 {
67 FILE *fp_in = NULL, *fp_ou = NULL;
68
69 char *buf;
70 char *url;
71 char report[MAXLEN];
72 char ip[MAXLEN];
73 char oip[MAXLEN];
74 char user[MAXLEN];
75 char ouser[MAXLEN];
76 char ouser2[MAXLEN];
77 char data[15];
78 char hora[15];
79 char csort[4098];
80 bool z=false;
81 int count=0;
82 int day,month,year;
83 int cstatus;
84 bool new_user;
85 struct getwordstruct gwarea;
86 longline line;
87 struct userinfostruct *uinfo;
88 struct tm t;
89
90 ouser[0]='\0';
91 ouser2[0]='\0';
92
93 sprintf(denied_sort,"%s/denied.int_log",tmp);
94 if (!denied_count) {
95 unlink(denied_sort);
96 if (debugz) debugaz(_("Denied report not produced because it is empty\n"));
97 return;
98 }
99
100 if (snprintf(csort,sizeof(csort),"sort -T \"%s\" -t \"\t\" -k 3,3 -k 5,5 -o \"%s\" \"%s\"",tmp,denied_sort,denied_unsort)>=sizeof(csort)) {
101 debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),denied_unsort,denied_sort);
102 exit(EXIT_FAILURE);
103 }
104 cstatus=system(csort);
105 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
106 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
107 debuga(_("sort command: %s\n"),csort);
108 exit(EXIT_FAILURE);
109 }
110 if (unlink(denied_unsort)) {
111 debuga(_("Cannot delete %s - %s\n"),denied_unsort,strerror(errno));
112 exit(EXIT_FAILURE);
113 }
114 denied_unsort[0]='\0';
115
116 sprintf(report,"%s/denied.html",outdirname);
117
118 if((fp_in=MY_FOPEN(denied_sort,"r"))==NULL) {
119 debuga(_("(denied) Cannot open log file %s\n"),denied_sort);
120 exit(EXIT_FAILURE);
121 }
122
123 if((fp_ou=MY_FOPEN(report,"w"))==NULL) {
124 debuga(_("(denied) Cannot open log file %s\n"),report);
125 exit(EXIT_FAILURE);
126 }
127
128 write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Denied"),HTML_JS_NONE);
129 fputs("<tr><td class=\"header_c\">",fp_ou);
130 fprintf(fp_ou,_("Period: %s"),period.html);
131 fputs("</td></tr>\n",fp_ou);
132 fprintf(fp_ou,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Denied"));
133 close_html_header(fp_ou);
134
135 fputs("<div class=\"report\"><table cellpadding=\"0\" cellspacing=\"2\">\n",fp_ou);
136 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"));
137
138 if ((line=longline_create())==NULL) {
139 debuga(_("Not enough memory to read the denied accesses\n"));
140 exit(EXIT_FAILURE);
141 }
142
143 while((buf=longline_read(fp_in,line))!=NULL) {
144 getword_start(&gwarea,buf);
145 if (getword(data,sizeof(data),&gwarea,'\t')<0 || getword(hora,sizeof(hora),&gwarea,'\t')<0 ||
146 getword(user,sizeof(user),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0) {
147 debuga(_("There is a broken record or garbage in file %s\n"),denied_sort);
148 exit(EXIT_FAILURE);
149 }
150 if (getword_ptr(buf,&url,&gwarea,'\t')<0) {
151 debuga(_("There is a broken url in file %s\n"),denied_sort);
152 exit(EXIT_FAILURE);
153 }
154 if (sscanf(data,"%d/%d/%d",&day,&month,&year)!=3) continue;
155 computedate(year,month,day,&t);
156 strftime(data,sizeof(data),"%x",&t);
157
158 uinfo=userinfo_find_from_id(user);
159 if (!uinfo) {
160 debuga(_("Unknown user ID %s in file %s\n"),user,denied_sort);
161 exit(EXIT_FAILURE);
162 }
163
164 new_user=false;
165 if(!z) {
166 strcpy(ouser,user);
167 strcpy(oip,ip);
168 z=true;
169 new_user=true;
170 } else {
171 if(strcmp(ouser,user) != 0) {
172 strcpy(ouser,user);
173 new_user=true;
174 }
175 if(strcmp(oip,ip) != 0) {
176 strcpy(oip,ip);
177 new_user=true;
178 }
179 }
180
181 if(DeniedReportLimit) {
182 if(strcmp(ouser2,uinfo->label) == 0) {
183 count++;
184 } else {
185 if(count>DeniedReportLimit && DeniedReportLimit>0)
186 show_ignored_denied(fp_ou,count-DeniedReportLimit);
187 count=1;
188 strcpy(ouser2,uinfo->label);
189 }
190 if(count > DeniedReportLimit)
191 continue;
192 }
193
194 fputs("<tr>",fp_ou);
195 if (new_user) {
196 if (uinfo->topuser)
197 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);
198 else
199 fprintf(fp_ou,"<td class=\"data\">%s</td><td class=\"data\">%s</td>",uinfo->label,ip);
200 } else
201 fputs("<td class=\"data\"></td><td class=\"data\"></td>",fp_ou);
202 fprintf(fp_ou,"<td class=\"data\">%s-%s</td><td class=\"data2\">",data,hora);
203 if(BlockIt[0] != '\0' && url[0]!=ALIAS_PREFIX) {
204 fprintf(fp_ou,"<a href=\"%s%s?url=",wwwDocumentRoot,BlockIt);
205 output_html_url(fp_ou,url);
206 fprintf(fp_ou,"\"><img src=\"%s/sarg-squidguard-block.png\"></a>&nbsp;",ImageFile);
207 }
208 output_html_link(fp_ou,url,100);
209 fputs("</td></tr>\n",fp_ou);
210 }
211 fclose(fp_in);
212 longline_destroy(&line);
213
214 if(count>DeniedReportLimit && DeniedReportLimit>0)
215 show_ignored_denied(fp_ou,count-DeniedReportLimit);
216
217 fputs("</table></div>\n",fp_ou);
218 if (write_html_trailer(fp_ou)<0)
219 debuga(_("Write error in file %s\n"),report);
220 if (fclose(fp_ou)<0)
221 debuga(_("Failed to close file %s - %s\n"),report,strerror(errno));
222
223 if (unlink(denied_sort)==-1)
224 debuga(_("Failed to delete the file \"%s\" after processing it - %s\n"),denied_sort,strerror(errno));
225 denied_sort[0]='\0';
226
227 return;
228 }
229
230 /*!
231 Remove any temporary file left by the denied module.
232 */
233 void denied_cleanup(void)
234 {
235 if(denied_sort[0]) {
236 unlink(denied_sort);
237 }
238 if(denied_unsort[0]) {
239 unlink(denied_unsort);
240 }
241 }