]>
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"
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
]= "" ;
36 Open a file to store the denied accesses.
38 \return The file handle or NULL if no file is necessary.
40 FILE * denied_open ( void )
44 if (( ReportType
& REPORT_TYPE_DENIED
) == 0 ) return ( NULL
);
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
));
54 static void show_ignored_denied ( FILE * fp_ou
, int count
)
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
);
63 Generate a report containing the denied accesses.
65 void gen_denied_report ( void )
67 FILE * fp_in
= NULL
, * fp_ou
= NULL
;
85 struct getwordstruct gwarea
;
87 struct userinfostruct
* uinfo
;
93 sprintf ( denied_sort
, "%s/denied.int_log" , tmp
);
96 if ( debugz
) debugaz ( _ ( "Denied report not produced because it is empty \n " ));
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
);
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
);
110 if ( unlink ( denied_unsort
)) {
111 debuga ( _ ( "Cannot delete %s - %s \n " ), denied_unsort
, strerror ( errno
));
114 denied_unsort
[ 0 ]= '\0' ;
116 sprintf ( report
, "%s/denied.html" , outdirname
);
118 if (( fp_in
= MY_FOPEN ( denied_sort
, "r" ))== NULL
) {
119 debuga ( _ ( "(denied) Cannot open log file %s \n " ), denied_sort
);
123 if (( fp_ou
= MY_FOPEN ( report
, "w" ))== NULL
) {
124 debuga ( _ ( "(denied) Cannot open log file %s \n " ), report
);
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
);
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" ));
138 if (( line
= longline_create ())== NULL
) {
139 debuga ( _ ( "Not enough memory to read the denied accesses \n " ));
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
);
150 if ( getword_ptr ( buf
,& url
,& gwarea
, ' \t ' )< 0 ) {
151 debuga ( _ ( "There is a broken url in file %s \n " ), denied_sort
);
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
);
158 uinfo
= userinfo_find_from_id ( user
);
160 debuga ( _ ( "Unknown user ID %s in file %s \n " ), user
, denied_sort
);
171 if ( strcmp ( ouser
, user
) != 0 ) {
175 if ( strcmp ( oip
, ip
) != 0 ) {
181 if ( DeniedReportLimit
) {
182 if ( strcmp ( ouser2
, uinfo
-> label
) == 0 ) {
185 if ( count
> DeniedReportLimit
&& DeniedReportLimit
> 0 )
186 show_ignored_denied ( fp_ou
, count
- DeniedReportLimit
);
188 strcpy ( ouser2
, uinfo
-> label
);
190 if ( count
> DeniedReportLimit
)
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
);
199 fprintf ( fp_ou
, "<td class= \" data \" >%s</td><td class= \" data \" >%s</td>" , uinfo
-> label
, ip
);
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> " , ImageFile
);
208 output_html_link ( fp_ou
, url
, 100 );
209 fputs ( "</td></tr> \n " , fp_ou
);
212 longline_destroy (& line
);
214 if ( count
> DeniedReportLimit
&& DeniedReportLimit
> 0 )
215 show_ignored_denied ( fp_ou
, count
- DeniedReportLimit
);
217 fputs ( "</table></div> \n " , fp_ou
);
218 if ( write_html_trailer ( fp_ou
)< 0 )
219 debuga ( _ ( "Write error in file %s \n " ), report
);
221 debuga ( _ ( "Failed to close file %s - %s \n " ), report
, strerror ( errno
));
223 if ( unlink ( denied_sort
)==- 1 )
224 debuga ( _ ( "Failed to delete the file \" %s \" after processing it - %s \n " ), denied_sort
, strerror ( errno
));
231 Remove any temporary file left by the denied module.
233 void denied_cleanup ( void )
238 if ( denied_unsort
[ 0 ]) {
239 unlink ( denied_unsort
);