denied.c (sarg-2.3.11) | : | denied.c (sarg-2.4.0) | ||
---|---|---|---|---|
/* | /* | |||
* SARG Squid Analysis Report Generator http://sarg.sourceforge.net | * SARG Squid Analysis Report Generator http://sarg.sourceforge.net | |||
* 1998, 2013 | * 1998, 2015 | |||
* | * | |||
* SARG donations: | * SARG donations: | |||
* please look at http://sarg.sourceforge.net/donations.php | * please look at http://sarg.sourceforge.net/donations.php | |||
* Support: | * Support: | |||
* http://sourceforge.net/projects/sarg/forums/forum/363374 | * http://sourceforge.net/projects/sarg/forums/forum/363374 | |||
* --------------------------------------------------------------------- | * --------------------------------------------------------------------- | |||
* | * | |||
* This program is free software; you can redistribute it and/or modify | * This program is free software; you can redistribute it and/or modify | |||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | |||
* the Free Software Foundation; either version 2 of the License, or | * the Free Software Foundation; either version 2 of the License, or | |||
skipping to change at line 29 | skipping to change at line 29 | |||
* GNU General Public License for more details. | * GNU General Public License for more details. | |||
* | * | |||
* You should have received a copy of the GNU General Public License | * You should have received a copy of the GNU General Public License | |||
* along with this program; if not, write to the Free Software | * along with this program; if not, write to the Free Software | |||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. | |||
* | * | |||
*/ | */ | |||
#include "include/conf.h" | #include "include/conf.h" | |||
#include "include/defs.h" | #include "include/defs.h" | |||
#include "include/readlog.h" | ||||
//! Name of the file containing the unsorted denied entries. | ||||
static char denied_unsort[MAXLEN]=""; | ||||
//! The file handle to write the entries. | ||||
static FILE *fp_denied=NULL; | ||||
//! \c True if at least one denied entry exists. | ||||
static bool denied_exists=false; | ||||
/*! | ||||
Open a file to store the denied accesses. | ||||
\return The file handle or NULL if no file is necessary. | ||||
*/ | ||||
void denied_open(void) | ||||
{ | ||||
if ((ReportType & REPORT_TYPE_DENIED) == 0) { | ||||
if (debugz>=LogLevel_Process) debugaz(__FILE__,__LINE__,_("Denied | ||||
report not produced as it is not requested\n")); | ||||
return; | ||||
} | ||||
if (Privacy) { | ||||
if (debugz>=LogLevel_Process) debugaz(__FILE__,__LINE__,_("Denied | ||||
report not produced because privacy option is active\n")); | ||||
return; | ||||
} | ||||
format_path(__FILE__, __LINE__, denied_unsort, sizeof(denied_unsort), "%s | ||||
/denied.int_unsort", tmp); | ||||
if ((fp_denied=MY_FOPEN(denied_unsort,"w"))==NULL) { | ||||
debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),denie | ||||
d_unsort,strerror(errno)); | ||||
exit(EXIT_FAILURE); | ||||
} | ||||
return; | ||||
} | ||||
/*! | ||||
Write one entry in the unsorted denied file provided that it is required. | ||||
\param log_entry The entry to write into the log file. | ||||
*/ | ||||
void denied_write(const struct ReadLogStruct *log_entry) | ||||
{ | ||||
char date[80]; | ||||
if (fp_denied && strstr(log_entry->HttpCode,"DENIED/403") != 0) { | ||||
strftime(date,sizeof(date),"%d/%m/%Y\t%H:%M:%S",&log_entry->Entry | ||||
Time); | ||||
fprintf(fp_denied, "%s\t%s\t%s\t%s\n",date,log_entry->User,log_en | ||||
try->Ip,log_entry->Url); | ||||
denied_exists=true; | ||||
} | ||||
} | ||||
/*! | ||||
Close the file opened by denied_open(). | ||||
*/ | ||||
void denied_close(void) | ||||
{ | ||||
if (fp_denied) { | ||||
if (fclose(fp_denied)==EOF) { | ||||
debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n") | ||||
,denied_unsort,strerror(errno)); | ||||
exit(EXIT_FAILURE); | ||||
} | ||||
fp_denied=NULL; | ||||
} | ||||
} | ||||
/*! | ||||
Tell the caller if a denied report exists. | ||||
\return \c True if the report is available or \c false if no report | ||||
was generated. | ||||
*/ | ||||
bool is_denied(void) | ||||
{ | ||||
return(denied_exists); | ||||
} | ||||
static void show_ignored_denied(FILE *fp_ou,int count) | static void show_ignored_denied(FILE *fp_ou,int count) | |||
{ | { | |||
char ignored[80]; | char ignored[80]; | |||
snprintf(ignored,sizeof(ignored),ngettext("%d more denied access not show n here…","%d more denied accesses not shown here…",count),count); | snprintf(ignored,sizeof(ignored),ngettext("%d more denied access not show n here…","%d more denied accesses not shown here…",count),count); | |||
fprintf(fp_ou,"<tr><td class=\"data\"></td><td class=\"data\"></td><td cl ass=\"data\"></td><td class=\"data2 more\">%s</td></tr>\n",ignored); | fprintf(fp_ou,"<tr><td class=\"data\"></td><td class=\"data\"></td><td cl ass=\"data\"></td><td class=\"data2 more\">%s</td></tr>\n",ignored); | |||
} | } | |||
/*! | ||||
Generate a report containing the denied accesses. | ||||
*/ | ||||
void gen_denied_report(void) | void gen_denied_report(void) | |||
{ | { | |||
FILE *fp_in = NULL, *fp_ou = NULL; | FileObject *fp_in = NULL; | |||
FILE *fp_ou = NULL; | ||||
char *buf; | char *buf; | |||
char *url; | char *url; | |||
char denied_in[MAXLEN]; | char denied_sort[MAXLEN]; | |||
char report[MAXLEN]; | char report[MAXLEN]; | |||
char ip[MAXLEN]; | char ip[MAXLEN]; | |||
char oip[MAXLEN]; | char oip[MAXLEN]; | |||
char user[MAXLEN]; | char user[MAXLEN]; | |||
char ouser[MAXLEN]; | char ouser[MAXLEN]=""; | |||
char ouser2[MAXLEN]; | char ouser2[MAXLEN]=""; | |||
char data[15]; | char data[15]; | |||
char hora[15]; | char hora[15]; | |||
bool z=false; | char csort[4098]; | |||
bool z=false; | ||||
int count=0; | int count=0; | |||
int day,month,year; | int day,month,year; | |||
int cstatus; | ||||
bool new_user; | bool new_user; | |||
struct getwordstruct gwarea; | struct getwordstruct gwarea; | |||
longline line; | longline line; | |||
struct userinfostruct *uinfo; | struct userinfostruct *uinfo; | |||
struct tm t; | struct tm t; | |||
ouser[0]='\0'; | if (!denied_exists) { | |||
ouser2[0]='\0'; | if (!KeepTempLog && denied_unsort[0]!='\0' && unlink(denied_unsor | |||
t)) | ||||
sprintf(denied_in,"%s/denied.int_log",tmp); | debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"), | |||
if (!denied_count) { | denied_unsort,strerror(errno)); | |||
if (!KeepTempLog && unlink(denied_in)) | denied_unsort[0]='\0'; | |||
debuga(_("Cannot delete \"%s\": %s\n"),denied_in,strerror | if (debugz>=LogLevel_Process) debugaz(__FILE__,__LINE__,_("Denied | |||
(errno)); | report not produced because it is empty\n")); | |||
if (debugz) debugaz(_("Denied report not produced because it is e | ||||
mpty\n")); | ||||
return; | return; | |||
} | } | |||
if (debugz>=LogLevel_Process) | ||||
debuga(__FILE__,__LINE__,_("Creating denied accesses report...\n" | ||||
)); | ||||
if (snprintf(denied_sort,sizeof(denied_sort),"%s/denied.int_log",tmp)>=si | ||||
zeof(denied_sort)) { | ||||
debuga(__FILE__,__LINE__,_("Temporary directory path too long to | ||||
sort the denied accesses\n")); | ||||
exit(EXIT_FAILURE); | ||||
} | ||||
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)) { | ||||
debuga(__FILE__,__LINE__,_("Sort command too long when sorting fi | ||||
le \"%s\" to \"%s\"\n"),denied_unsort,denied_sort); | ||||
exit(EXIT_FAILURE); | ||||
} | ||||
cstatus=system(csort); | ||||
if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | ||||
debuga(__FILE__,__LINE__,_("sort command return status %d\n"),WEX | ||||
ITSTATUS(cstatus)); | ||||
debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort); | ||||
exit(EXIT_FAILURE); | ||||
} | ||||
if (unlink(denied_unsort)) { | ||||
debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),denied_u | ||||
nsort,strerror(errno)); | ||||
exit(EXIT_FAILURE); | ||||
} | ||||
denied_unsort[0]='\0'; | ||||
sprintf(report,"%s/denied.html",outdirname); | format_path(__FILE__, __LINE__, report, sizeof(report), "%s/denied.html", outdirname); | |||
if((fp_in=MY_FOPEN(denied_in,"r"))==NULL) { | if ((fp_in=FileObject_Open(denied_sort))==NULL) { | |||
debugapos("denied",_("Cannot open file \"%s\": %s\n"),denied_in,s | debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),denie | |||
trerror(errno)); | d_sort,FileObject_GetLastOpenError()); | |||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | |||
} | } | |||
if((fp_ou=MY_FOPEN(report,"w"))==NULL) { | if ((fp_ou=MY_FOPEN(report,"w"))==NULL) { | |||
debugapos("denied",_("Cannot open file \"%s\": %s\n"),report,stre | debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),repor | |||
rror(errno)); | t,strerror(errno)); | |||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | |||
} | } | |||
write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Denied" ),HTML_JS_NONE); | write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Denied" ),HTML_JS_NONE); | |||
fputs("<tr><td class=\"header_c\">",fp_ou); | fputs("<tr><td class=\"header_c\">",fp_ou); | |||
fprintf(fp_ou,_("Period: %s"),period.html); | fprintf(fp_ou,_("Period: %s"),period.html); | |||
fputs("</td></tr>\n",fp_ou); | fputs("</td></tr>\n",fp_ou); | |||
fprintf(fp_ou,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Denied")); | fprintf(fp_ou,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Denied")); | |||
close_html_header(fp_ou); | close_html_header(fp_ou); | |||
fputs("<div class=\"report\"><table cellpadding=\"0\" cellspacing=\"2\">\ n",fp_ou); | fputs("<div class=\"report\"><table cellpadding=\"0\" cellspacing=\"2\">\ n",fp_ou); | |||
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",_("US ERID"),_("IP/NAME"),_("DATE/TIME"),_("ACCESSED SITE")); | 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",_("US ERID"),_("IP/NAME"),_("DATE/TIME"),_("ACCESSED SITE")); | |||
if ((line=longline_create())==NULL) { | if ((line=longline_create())==NULL) { | |||
debuga(_("Not enough memory to read file \"%s\"\n"),denied_in); | debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\" \n"),denied_sort); | |||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | |||
} | } | |||
while((buf=longline_read(fp_in,line))!=NULL) { | while((buf=longline_read(fp_in,line))!=NULL) { | |||
getword_start(&gwarea,buf); | getword_start(&gwarea,buf); | |||
if (getword(data,sizeof(data),&gwarea,'\t')<0 || getword(hora,siz eof(hora),&gwarea,'\t')<0 || | if (getword(data,sizeof(data),&gwarea,'\t')<0 || getword(hora,siz eof(hora),&gwarea,'\t')<0 || | |||
getword(user,sizeof(user),&gwarea,'\t')<0 || getword(ip,sizeo f(ip),&gwarea,'\t')<0) { | getword(user,sizeof(user),&gwarea,'\t')<0 || getword(ip,sizeo f(ip),&gwarea,'\t')<0) { | |||
debuga(_("Invalid record in file \"%s\"\n"),denied_in); | debuga(__FILE__,__LINE__,_("Invalid record in file \"%s\" \n"),denied_sort); | |||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | |||
} | } | |||
if (getword_ptr(buf,&url,&gwarea,'\t')<0) { | if (getword_ptr(buf,&url,&gwarea,'\t')<0) { | |||
debuga(_("Invalid url in file \"%s\"\n"),denied_in); | debuga(__FILE__,__LINE__,_("Invalid url in file \"%s\"\n" ),denied_sort); | |||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | |||
} | } | |||
if (sscanf(data,"%d/%d/%d",&day,&month,&year)!=3) continue; | if (sscanf(data,"%d/%d/%d",&day,&month,&year)!=3) continue; | |||
computedate(year,month,day,&t); | computedate(year,month,day,&t); | |||
strftime(data,sizeof(data),"%x",&t); | strftime(data,sizeof(data),"%x",&t); | |||
uinfo=userinfo_find_from_id(user); | uinfo=userinfo_find_from_id(user); | |||
if (!uinfo) { | if (!uinfo) { | |||
debuga(_("Unknown user ID %s in file \"%s\"\n"),user,deni ed_in); | debuga(__FILE__,__LINE__,_("Unknown user ID %s in file \" %s\"\n"),user,denied_sort); | |||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | |||
} | } | |||
new_user=false; | new_user=false; | |||
if(!z) { | if (!z) { | |||
strcpy(ouser,user); | strcpy(ouser,user); | |||
strcpy(oip,ip); | strcpy(oip,ip); | |||
z=true; | z=true; | |||
new_user=true; | new_user=true; | |||
} else { | } else { | |||
if(strcmp(ouser,user) != 0) { | if (strcmp(ouser,user) != 0) { | |||
strcpy(ouser,user); | strcpy(ouser,user); | |||
new_user=true; | new_user=true; | |||
} | } | |||
if(strcmp(oip,ip) != 0) { | if (strcmp(oip,ip) != 0) { | |||
strcpy(oip,ip); | strcpy(oip,ip); | |||
new_user=true; | new_user=true; | |||
} | } | |||
} | } | |||
if(DeniedReportLimit) { | if (DeniedReportLimit) { | |||
if(strcmp(ouser2,uinfo->label) == 0) { | if (strcmp(ouser2,uinfo->label) == 0) { | |||
count++; | count++; | |||
} else { | } else { | |||
if(count>DeniedReportLimit && DeniedReportLimit>0 ) | if (count>DeniedReportLimit && DeniedReportLimit> 0) | |||
show_ignored_denied(fp_ou,count-DeniedRep ortLimit); | show_ignored_denied(fp_ou,count-DeniedRep ortLimit); | |||
count=1; | count=1; | |||
strcpy(ouser2,uinfo->label); | strcpy(ouser2,uinfo->label); | |||
} | } | |||
if(count > DeniedReportLimit) | if (count > DeniedReportLimit) | |||
continue; | continue; | |||
} | } | |||
fputs("<tr>",fp_ou); | fputs("<tr>",fp_ou); | |||
if (new_user) { | if (new_user) { | |||
if (uinfo->topuser) | if (uinfo->topuser) | |||
fprintf(fp_ou,"<td class=\"data\"><a href=\"%s/%s .html\">%s</a></td><td class=\"data\">%s</td>",uinfo->filename,uinfo->filename,u info->label,ip); | fprintf(fp_ou,"<td class=\"data\"><a href=\"%s/%s .html\">%s</a></td><td class=\"data\">%s</td>",uinfo->filename,uinfo->filename,u info->label,ip); | |||
else | else | |||
fprintf(fp_ou,"<td class=\"data\">%s</td><td clas s=\"data\">%s</td>",uinfo->label,ip); | fprintf(fp_ou,"<td class=\"data\">%s</td><td clas s=\"data\">%s</td>",uinfo->label,ip); | |||
} else | } else | |||
fputs("<td class=\"data\"></td><td class=\"data\"></td>", fp_ou); | fputs("<td class=\"data\"></td><td class=\"data\"></td>", fp_ou); | |||
fprintf(fp_ou,"<td class=\"data\">%s-%s</td><td class=\"data2\">" ,data,hora); | fprintf(fp_ou,"<td class=\"data\">%s-%s</td><td class=\"data2\">" ,data,hora); | |||
if(BlockIt[0] != '\0' && url[0]!=ALIAS_PREFIX) { | if (BlockIt[0] != '\0' && url[0]!=ALIAS_PREFIX) { | |||
fprintf(fp_ou,"<a href=\"%s%s?url=",wwwDocumentRoot,Block It); | fprintf(fp_ou,"<a href=\"%s%s?url=",wwwDocumentRoot,Block It); | |||
output_html_url(fp_ou,url); | output_html_url(fp_ou,url); | |||
fprintf(fp_ou,"\"><img src=\"%s/sarg-squidguard-block.png \"></a> ",ImageFile); | fprintf(fp_ou,"\"><img src=\"%s/sarg-squidguard-block.png \"></a> ",ImageFile); | |||
} | } | |||
output_html_link(fp_ou,url,100); | output_html_link(fp_ou,url,100); | |||
fputs("</td></tr>\n",fp_ou); | fputs("</td></tr>\n",fp_ou); | |||
} | } | |||
fclose(fp_in); | if (FileObject_Close(fp_in)) { | |||
debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),denied_s | ||||
ort,FileObject_GetLastCloseError()); | ||||
exit(EXIT_FAILURE); | ||||
} | ||||
longline_destroy(&line); | longline_destroy(&line); | |||
if(count>DeniedReportLimit && DeniedReportLimit>0) | if (count>DeniedReportLimit && DeniedReportLimit>0) | |||
show_ignored_denied(fp_ou,count-DeniedReportLimit); | show_ignored_denied(fp_ou,count-DeniedReportLimit); | |||
fputs("</table></div>\n",fp_ou); | fputs("</table></div>\n",fp_ou); | |||
if (write_html_trailer(fp_ou)<0) | write_html_trailer(fp_ou); | |||
debuga(_("Write error in file \"%s\"\n"),report); | if (fclose(fp_ou)==EOF) { | |||
if (fclose(fp_ou)==EOF) | debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),report, | |||
debuga(_("Failed to close file \"%s\": %s\n"),report,strerror(err | strerror(errno)); | |||
no)); | exit(EXIT_FAILURE); | |||
} | ||||
if (!KeepTempLog && unlink(denied_in)==-1) | if (!KeepTempLog && unlink(denied_sort)==-1) | |||
debuga(_("Cannot delete \"%s\": %s\n"),denied_in,strerror(errno)) | debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),denied_s | |||
; | ort,strerror(errno)); | |||
return; | return; | |||
} | } | |||
/*! | ||||
Remove any temporary file left by the denied module. | ||||
*/ | ||||
void denied_cleanup(void) | ||||
{ | ||||
if (fp_denied){ | ||||
if (fclose(fp_denied)==EOF) { | ||||
debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n") | ||||
,denied_unsort,strerror(errno)); | ||||
exit(EXIT_FAILURE); | ||||
} | ||||
fp_denied=NULL; | ||||
} | ||||
if (!KeepTempLog && denied_unsort[0]) { | ||||
if (unlink(denied_unsort)==-1) | ||||
debuga(__FILE__,__LINE__,_("Failed to delete \"%s\": %s\n | ||||
"),denied_unsort,strerror(errno)); | ||||
} | ||||
} | ||||
End of changes. 29 change blocks. | ||||
45 lines changed or deleted | 163 lines changed or added |