]>
Commit | Line | Data |
---|---|---|
25697a35 | 1 | /* |
94ff9470 | 2 | * SARG Squid Analysis Report Generator http://sarg.sourceforge.net |
110ce984 | 3 | * 1998, 2015 |
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" |
8e53b2e7 | 29 | #include "include/readlog.h" |
25697a35 | 30 | |
60b48ae5 FM |
31 | //! Name of the file containing the unsorted denied entries. |
32 | static char denied_unsort[MAXLEN]=""; | |
8e53b2e7 FM |
33 | //! The file handle to write the entries. |
34 | static FILE *fp_denied=NULL; | |
35 | //! \c True if at least one denied entry exists. | |
36 | static bool denied_exists=false; | |
60b48ae5 FM |
37 | |
38 | /*! | |
39 | Open a file to store the denied accesses. | |
40 | ||
41 | \return The file handle or NULL if no file is necessary. | |
42 | */ | |
8e53b2e7 | 43 | void denied_open(void) |
60b48ae5 | 44 | { |
8e53b2e7 | 45 | if ((ReportType & REPORT_TYPE_DENIED) == 0) { |
af961877 | 46 | if (debugz>=LogLevel_Process) debugaz(__FILE__,__LINE__,_("Denied report not produced as it is not requested\n")); |
8e53b2e7 FM |
47 | return; |
48 | } | |
49 | if (Privacy) { | |
af961877 | 50 | if (debugz>=LogLevel_Process) debugaz(__FILE__,__LINE__,_("Denied report not produced because privacy option is active\n")); |
8e53b2e7 FM |
51 | return; |
52 | } | |
bd43d81f | 53 | |
60b48ae5 FM |
54 | snprintf(denied_unsort,sizeof(denied_unsort),"%s/denied.int_unsort",tmp); |
55 | if ((fp_denied=MY_FOPEN(denied_unsort,"w"))==NULL) { | |
af961877 | 56 | debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),denied_unsort,strerror(errno)); |
60b48ae5 FM |
57 | exit(EXIT_FAILURE); |
58 | } | |
8e53b2e7 FM |
59 | return; |
60 | } | |
61 | ||
62 | /*! | |
63 | Write one entry in the unsorted denied file provided that it is required. | |
64 | ||
65 | \param log_entry The entry to write into the log file. | |
66 | */ | |
67 | void denied_write(const struct ReadLogStruct *log_entry) | |
68 | { | |
69 | char date[80]; | |
bd43d81f | 70 | |
8e53b2e7 | 71 | if (fp_denied && strstr(log_entry->HttpCode,"DENIED/403") != 0) { |
cb53374b | 72 | strftime(date,sizeof(date),"%d/%m/%Y\t%H:%M:%S",&log_entry->EntryTime); |
8e53b2e7 FM |
73 | fprintf(fp_denied, "%s\t%s\t%s\t%s\n",date,log_entry->User,log_entry->Ip,log_entry->Url); |
74 | denied_exists=true; | |
75 | } | |
76 | } | |
77 | ||
78 | /*! | |
79 | Close the file opened by denied_open(). | |
80 | */ | |
81 | void denied_close(void) | |
82 | { | |
507460ae FM |
83 | if (fp_denied) { |
84 | if (fclose(fp_denied)==EOF) { | |
af961877 | 85 | debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),denied_unsort,strerror(errno)); |
8e53b2e7 FM |
86 | exit(EXIT_FAILURE); |
87 | } | |
6befdb6f | 88 | fp_denied=NULL; |
8e53b2e7 FM |
89 | } |
90 | } | |
91 | ||
92 | /*! | |
93 | Tell the caller if a denied report exists. | |
94 | ||
95 | \return \c True if the report is available or \c false if no report | |
96 | was generated. | |
97 | */ | |
98 | bool is_denied(void) | |
99 | { | |
100 | return(denied_exists); | |
60b48ae5 FM |
101 | } |
102 | ||
f209e4f0 FM |
103 | static void show_ignored_denied(FILE *fp_ou,int count) |
104 | { | |
9bd92830 | 105 | char ignored[80]; |
f209e4f0 | 106 | |
9bd92830 | 107 | snprintf(ignored,sizeof(ignored),ngettext("%d more denied access not shown here…","%d more denied accesses not shown here…",count),count); |
7ae50eee | 108 | 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 |
109 | } |
110 | ||
60b48ae5 FM |
111 | /*! |
112 | Generate a report containing the denied accesses. | |
113 | */ | |
32e71fa4 | 114 | void gen_denied_report(void) |
25697a35 | 115 | { |
9bd92830 FM |
116 | FILE *fp_in = NULL, *fp_ou = NULL; |
117 | ||
118 | char *buf; | |
119 | char *url; | |
6befdb6f | 120 | char denied_sort[MAXLEN]; |
9bd92830 FM |
121 | char report[MAXLEN]; |
122 | char ip[MAXLEN]; | |
123 | char oip[MAXLEN]; | |
124 | char user[MAXLEN]; | |
6befdb6f FM |
125 | char ouser[MAXLEN]=""; |
126 | char ouser2[MAXLEN]=""; | |
9bd92830 FM |
127 | char data[15]; |
128 | char hora[15]; | |
60b48ae5 | 129 | char csort[4098]; |
6befdb6f | 130 | bool z=false; |
9bd92830 FM |
131 | int count=0; |
132 | int day,month,year; | |
60b48ae5 | 133 | int cstatus; |
9bd92830 FM |
134 | bool new_user; |
135 | struct getwordstruct gwarea; | |
136 | longline line; | |
137 | struct userinfostruct *uinfo; | |
138 | struct tm t; | |
139 | ||
8e53b2e7 | 140 | if (!denied_exists) { |
ab7f8e50 | 141 | if (!KeepTempLog && denied_unsort[0]!='\0' && unlink(denied_unsort)) |
af961877 | 142 | debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),denied_unsort,strerror(errno)); |
8e53b2e7 | 143 | denied_unsort[0]='\0'; |
af961877 | 144 | if (debugz>=LogLevel_Process) debugaz(__FILE__,__LINE__,_("Denied report not produced because it is empty\n")); |
9bd92830 FM |
145 | return; |
146 | } | |
b7413c4c | 147 | if (debugz>=LogLevel_Process) |
af961877 | 148 | debuga(__FILE__,__LINE__,_("Creating denied accesses report...\n")); |
9bd92830 | 149 | |
6befdb6f | 150 | if (snprintf(denied_sort,sizeof(denied_sort),"%s/denied.int_log",tmp)>=sizeof(denied_sort)) { |
af961877 | 151 | debuga(__FILE__,__LINE__,_("Temporary directory path too long to sort the denied accesses\n")); |
6befdb6f FM |
152 | exit(EXIT_FAILURE); |
153 | } | |
60b48ae5 | 154 | 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)) { |
af961877 | 155 | debuga(__FILE__,__LINE__,_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),denied_unsort,denied_sort); |
60b48ae5 FM |
156 | exit(EXIT_FAILURE); |
157 | } | |
158 | cstatus=system(csort); | |
159 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
af961877 FM |
160 | debuga(__FILE__,__LINE__,_("sort command return status %d\n"),WEXITSTATUS(cstatus)); |
161 | debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort); | |
60b48ae5 FM |
162 | exit(EXIT_FAILURE); |
163 | } | |
164 | if (unlink(denied_unsort)) { | |
af961877 | 165 | debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),denied_unsort,strerror(errno)); |
60b48ae5 FM |
166 | exit(EXIT_FAILURE); |
167 | } | |
168 | denied_unsort[0]='\0'; | |
169 | ||
9bd92830 FM |
170 | sprintf(report,"%s/denied.html",outdirname); |
171 | ||
60b48ae5 | 172 | if((fp_in=MY_FOPEN(denied_sort,"r"))==NULL) { |
af961877 | 173 | debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),denied_sort,strerror(errno)); |
007905af | 174 | exit(EXIT_FAILURE); |
9bd92830 FM |
175 | } |
176 | ||
177 | if((fp_ou=MY_FOPEN(report,"w"))==NULL) { | |
af961877 | 178 | debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),report,strerror(errno)); |
007905af | 179 | exit(EXIT_FAILURE); |
9bd92830 FM |
180 | } |
181 | ||
182 | write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Denied"),HTML_JS_NONE); | |
183 | fputs("<tr><td class=\"header_c\">",fp_ou); | |
184 | fprintf(fp_ou,_("Period: %s"),period.html); | |
185 | fputs("</td></tr>\n",fp_ou); | |
186 | fprintf(fp_ou,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Denied")); | |
187 | close_html_header(fp_ou); | |
188 | ||
189 | fputs("<div class=\"report\"><table cellpadding=\"0\" cellspacing=\"2\">\n",fp_ou); | |
190 | 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")); | |
191 | ||
192 | if ((line=longline_create())==NULL) { | |
af961877 | 193 | debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\"\n"),denied_sort); |
9bd92830 FM |
194 | exit(EXIT_FAILURE); |
195 | } | |
196 | ||
197 | while((buf=longline_read(fp_in,line))!=NULL) { | |
198 | getword_start(&gwarea,buf); | |
199 | if (getword(data,sizeof(data),&gwarea,'\t')<0 || getword(hora,sizeof(hora),&gwarea,'\t')<0 || | |
007905af | 200 | getword(user,sizeof(user),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0) { |
af961877 | 201 | debuga(__FILE__,__LINE__,_("Invalid record in file \"%s\"\n"),denied_sort); |
9bd92830 FM |
202 | exit(EXIT_FAILURE); |
203 | } | |
204 | if (getword_ptr(buf,&url,&gwarea,'\t')<0) { | |
af961877 | 205 | debuga(__FILE__,__LINE__,_("Invalid url in file \"%s\"\n"),denied_sort); |
9bd92830 FM |
206 | exit(EXIT_FAILURE); |
207 | } | |
208 | if (sscanf(data,"%d/%d/%d",&day,&month,&year)!=3) continue; | |
209 | computedate(year,month,day,&t); | |
210 | strftime(data,sizeof(data),"%x",&t); | |
211 | ||
212 | uinfo=userinfo_find_from_id(user); | |
213 | if (!uinfo) { | |
af961877 | 214 | debuga(__FILE__,__LINE__,_("Unknown user ID %s in file \"%s\"\n"),user,denied_sort); |
9bd92830 FM |
215 | exit(EXIT_FAILURE); |
216 | } | |
217 | ||
218 | new_user=false; | |
219 | if(!z) { | |
220 | strcpy(ouser,user); | |
221 | strcpy(oip,ip); | |
222 | z=true; | |
223 | new_user=true; | |
224 | } else { | |
225 | if(strcmp(ouser,user) != 0) { | |
226 | strcpy(ouser,user); | |
227 | new_user=true; | |
228 | } | |
229 | if(strcmp(oip,ip) != 0) { | |
230 | strcpy(oip,ip); | |
231 | new_user=true; | |
232 | } | |
233 | } | |
234 | ||
235 | if(DeniedReportLimit) { | |
236 | if(strcmp(ouser2,uinfo->label) == 0) { | |
237 | count++; | |
238 | } else { | |
239 | if(count>DeniedReportLimit && DeniedReportLimit>0) | |
240 | show_ignored_denied(fp_ou,count-DeniedReportLimit); | |
241 | count=1; | |
242 | strcpy(ouser2,uinfo->label); | |
243 | } | |
244 | if(count > DeniedReportLimit) | |
245 | continue; | |
246 | } | |
247 | ||
248 | fputs("<tr>",fp_ou); | |
dbf8818d FM |
249 | if (new_user) { |
250 | if (uinfo->topuser) | |
251 | 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); | |
252 | else | |
253 | fprintf(fp_ou,"<td class=\"data\">%s</td><td class=\"data\">%s</td>",uinfo->label,ip); | |
254 | } else | |
9bd92830 FM |
255 | fputs("<td class=\"data\"></td><td class=\"data\"></td>",fp_ou); |
256 | fprintf(fp_ou,"<td class=\"data\">%s-%s</td><td class=\"data2\">",data,hora); | |
67a93701 | 257 | if(BlockIt[0] != '\0' && url[0]!=ALIAS_PREFIX) { |
9bd92830 FM |
258 | fprintf(fp_ou,"<a href=\"%s%s?url=",wwwDocumentRoot,BlockIt); |
259 | output_html_url(fp_ou,url); | |
260 | fprintf(fp_ou,"\"><img src=\"%s/sarg-squidguard-block.png\"></a> ",ImageFile); | |
261 | } | |
6fa33a32 | 262 | output_html_link(fp_ou,url,100); |
67a93701 | 263 | fputs("</td></tr>\n",fp_ou); |
9bd92830 | 264 | } |
204781f4 | 265 | if (fclose(fp_in)==EOF) { |
af961877 | 266 | debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),denied_sort,strerror(errno)); |
204781f4 FM |
267 | exit(EXIT_FAILURE); |
268 | } | |
9bd92830 FM |
269 | longline_destroy(&line); |
270 | ||
271 | if(count>DeniedReportLimit && DeniedReportLimit>0) | |
272 | show_ignored_denied(fp_ou,count-DeniedReportLimit); | |
273 | ||
274 | fputs("</table></div>\n",fp_ou); | |
342bd723 | 275 | write_html_trailer(fp_ou); |
507460ae | 276 | if (fclose(fp_ou)==EOF) { |
af961877 | 277 | debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),report,strerror(errno)); |
507460ae FM |
278 | exit(EXIT_FAILURE); |
279 | } | |
9bd92830 | 280 | |
b378aaf1 | 281 | if (!KeepTempLog && unlink(denied_sort)==-1) |
af961877 | 282 | debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),denied_sort,strerror(errno)); |
9bd92830 FM |
283 | |
284 | return; | |
25697a35 | 285 | } |
60b48ae5 FM |
286 | |
287 | /*! | |
288 | Remove any temporary file left by the denied module. | |
289 | */ | |
290 | void denied_cleanup(void) | |
291 | { | |
507460ae FM |
292 | if (fp_denied){ |
293 | if (fclose(fp_denied)==EOF) { | |
af961877 | 294 | debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),denied_unsort,strerror(errno)); |
507460ae FM |
295 | exit(EXIT_FAILURE); |
296 | } | |
6befdb6f | 297 | fp_denied=NULL; |
60b48ae5 | 298 | } |
d89ead3c | 299 | if (!KeepTempLog && denied_unsort[0]) { |
8e53b2e7 | 300 | if (unlink(denied_unsort)==-1) |
af961877 | 301 | debuga(__FILE__,__LINE__,_("Failed to delete \"%s\": %s\n"),denied_unsort,strerror(errno)); |
60b48ae5 FM |
302 | } |
303 | } |