]>
git.ipfire.org Git - thirdparty/sarg.git/blob - denied.c
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
6 * please look at http://sarg.sourceforge.net/donations.php
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
9 * ---------------------------------------------------------------------
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.
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.
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.
27 #include "include/conf.h"
28 #include "include/defs.h"
29 #include "include/readlog.h"
31 //! Name of the file containing the unsorted denied entries.
32 static char denied_unsort
[ MAXLEN
]= "" ;
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 ;
39 Open a file to store the denied accesses.
41 \return The file handle or NULL if no file is necessary.
43 void denied_open ( void )
45 if (( ReportType
& REPORT_TYPE_DENIED
) == 0 ) {
46 if ( debugz
) debugaz ( _ ( "Denied report not produced as it is not requested \n " ));
50 if ( debugz
) debugaz ( _ ( "Denied report not produced because privacy option is active \n " ));
54 snprintf ( denied_unsort
, sizeof ( denied_unsort
), "%s/denied.int_unsort" , tmp
);
55 if (( fp_denied
= MY_FOPEN ( denied_unsort
, "w" ))== NULL
) {
56 debuga ( _ ( "(log) Cannot open file %s: %s \n " ), denied_unsort
, strerror ( errno
));
63 Write one entry in the unsorted denied file provided that it is required.
65 \param log_entry The entry to write into the log file.
67 void denied_write ( const struct ReadLogStruct
* log_entry
)
71 if ( fp_denied
&& strstr ( log_entry
-> HttpCode
, "DENIED/403" ) != 0 ) {
72 strftime ( date
, sizeof ( date
), "%d/%m/%Y \t %H:%M:%S" ,& log_entry
-> EntryTime
);
73 fprintf ( fp_denied
, "%s \t %s \t %s \t %s \n " , date
, log_entry
-> User
, log_entry
-> Ip
, log_entry
-> Url
);
79 Close the file opened by denied_open().
81 void denied_close ( void )
84 if ( fclose ( fp_denied
)== EOF
) {
85 debuga ( _ ( "Write error in %s: %s \n " ), denied_unsort
, strerror ( errno
));
93 Tell the caller if a denied report exists.
95 \return \c True if the report is available or \c false if no report
100 return ( denied_exists
);
103 static void show_ignored_denied ( FILE * fp_ou
, int count
)
107 snprintf ( ignored
, sizeof ( ignored
), ngettext ( "%d more denied access not shown here…" , "%d more denied accesses not shown here…" , count
), count
);
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
);
112 Generate a report containing the denied accesses.
114 void gen_denied_report ( void )
116 FILE * fp_in
= NULL
, * fp_ou
= NULL
;
120 char denied_sort
[ MAXLEN
];
125 char ouser
[ MAXLEN
]= "" ;
126 char ouser2
[ MAXLEN
]= "" ;
135 struct getwordstruct gwarea
;
137 struct userinfostruct
* uinfo
;
140 if (! denied_exists
) {
141 if (! KeepTempLog
&& denied_unsort
[ 0 ]!= '\0' && unlink ( denied_unsort
))
142 debuga ( _ ( "Cannot delete \" %s \" : %s \n " ), denied_unsort
, strerror ( errno
));
143 denied_unsort
[ 0 ]= '\0' ;
144 if ( debugz
) debugaz ( _ ( "Denied report not produced because it is empty \n " ));
148 if ( snprintf ( denied_sort
, sizeof ( denied_sort
), "%s/denied.int_log" , tmp
)>= sizeof ( denied_sort
)) {
149 debuga ( _ ( "Temporary directory path too long to sort the denied accesses \n " ));
152 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
)) {
153 debuga ( _ ( "Sort command too long when sorting file \" %s \" to \" %s \"\n " ), denied_unsort
, denied_sort
);
156 cstatus
= system ( csort
);
157 if (! WIFEXITED ( cstatus
) || WEXITSTATUS ( cstatus
)) {
158 debuga ( _ ( "sort command return status %d \n " ), WEXITSTATUS ( cstatus
));
159 debuga ( _ ( "sort command: %s \n " ), csort
);
162 if ( unlink ( denied_unsort
)) {
163 debuga ( _ ( "Cannot delete %s - %s \n " ), denied_unsort
, strerror ( errno
));
166 denied_unsort
[ 0 ]= '\0' ;
168 sprintf ( report
, "%s/denied.html" , outdirname
);
170 if (( fp_in
= MY_FOPEN ( denied_sort
, "r" ))== NULL
) {
171 debuga ( _ ( "(denied) Cannot open log file %s: %s \n " ), denied_sort
, strerror ( errno
));
175 if (( fp_ou
= MY_FOPEN ( report
, "w" ))== NULL
) {
176 debuga ( _ ( "(denied) Cannot open log file %s: %s \n " ), report
, strerror ( errno
));
180 write_html_header ( fp_ou
,( IndexTree
== INDEX_TREE_DATE
) ? 3 : 1 , _ ( "Denied" ), HTML_JS_NONE
);
181 fputs ( "<tr><td class= \" header_c \" >" , fp_ou
);
182 fprintf ( fp_ou
, _ ( "Period: %s" ), period
. html
);
183 fputs ( "</td></tr> \n " , fp_ou
);
184 fprintf ( fp_ou
, "<tr><th class= \" header_c \" >%s</th></tr> \n " , _ ( "Denied" ));
185 close_html_header ( fp_ou
);
187 fputs ( "<div class= \" report \" ><table cellpadding= \" 0 \" cellspacing= \" 2 \" > \n " , fp_ou
);
188 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" ));
190 if (( line
= longline_create ())== NULL
) {
191 debuga ( _ ( "Not enough memory to read the denied accesses \n " ));
195 while (( buf
= longline_read ( fp_in
, line
))!= NULL
) {
196 getword_start (& gwarea
, buf
);
197 if ( getword ( data
, sizeof ( data
),& gwarea
, ' \t ' )< 0 || getword ( hora
, sizeof ( hora
),& gwarea
, ' \t ' )< 0 ||
198 getword ( user
, sizeof ( user
),& gwarea
, ' \t ' )< 0 || getword ( ip
, sizeof ( ip
),& gwarea
, ' \t ' )< 0 ) {
199 debuga ( _ ( "There is a broken record or garbage in file %s \n " ), denied_sort
);
202 if ( getword_ptr ( buf
,& url
,& gwarea
, ' \t ' )< 0 ) {
203 debuga ( _ ( "There is a broken url in file %s \n " ), denied_sort
);
206 if ( sscanf ( data
, "%d/%d/%d" ,& day
,& month
,& year
)!= 3 ) continue ;
207 computedate ( year
, month
, day
,& t
);
208 strftime ( data
, sizeof ( data
), "%x" ,& t
);
210 uinfo
= userinfo_find_from_id ( user
);
212 debuga ( _ ( "Unknown user ID %s in file %s \n " ), user
, denied_sort
);
223 if ( strcmp ( ouser
, user
) != 0 ) {
227 if ( strcmp ( oip
, ip
) != 0 ) {
233 if ( DeniedReportLimit
) {
234 if ( strcmp ( ouser2
, uinfo
-> label
) == 0 ) {
237 if ( count
> DeniedReportLimit
&& DeniedReportLimit
> 0 )
238 show_ignored_denied ( fp_ou
, count
- DeniedReportLimit
);
240 strcpy ( ouser2
, uinfo
-> label
);
242 if ( count
> DeniedReportLimit
)
249 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
);
251 fprintf ( fp_ou
, "<td class= \" data \" >%s</td><td class= \" data \" >%s</td>" , uinfo
-> label
, ip
);
253 fputs ( "<td class= \" data \" ></td><td class= \" data \" ></td>" , fp_ou
);
254 fprintf ( fp_ou
, "<td class= \" data \" >%s-%s</td><td class= \" data2 \" >" , data
, hora
);
255 if ( BlockIt
[ 0 ] != '\0' && url
[ 0 ]!= ALIAS_PREFIX
) {
256 fprintf ( fp_ou
, "<a href= \" %s%s?url=" , wwwDocumentRoot
, BlockIt
);
257 output_html_url ( fp_ou
, url
);
258 fprintf ( fp_ou
, " \" ><img src= \" %s/sarg-squidguard-block.png \" ></a> " , ImageFile
);
260 output_html_link ( fp_ou
, url
, 100 );
261 fputs ( "</td></tr> \n " , fp_ou
);
264 longline_destroy (& line
);
266 if ( count
> DeniedReportLimit
&& DeniedReportLimit
> 0 )
267 show_ignored_denied ( fp_ou
, count
- DeniedReportLimit
);
269 fputs ( "</table></div> \n " , fp_ou
);
270 if ( write_html_trailer ( fp_ou
)< 0 )
271 debuga ( _ ( "Write error in file %s \n " ), report
);
272 if ( fclose ( fp_ou
)== EOF
) {
273 debuga ( _ ( "Write error in %s: %s \n " ), report
, strerror ( errno
));
277 if (! KeepTempLog
&& unlink ( denied_sort
)==- 1 )
278 debuga ( _ ( "Cannot delete \" %s \" : %s \n " ), denied_sort
, strerror ( errno
));
284 Remove any temporary file left by the denied module.
286 void denied_cleanup ( void )
289 if ( fclose ( fp_denied
)== EOF
) {
290 debuga ( _ ( "Write error in %s: %s \n " ), denied_unsort
, strerror ( errno
));
295 if (! KeepTempLog
&& denied_unsort
[ 0 ]) {
296 if ( unlink ( denied_unsort
)==- 1 )
297 debuga ( _ ( "Failed to delete %s: %s \n " ), denied_unsort
, strerror ( errno
));