/*
* SARG Squid Analysis Report Generator http://sarg.sourceforge.net
- * 1998, 2011
+ * 1998, 2015
*
* SARG donations:
* please look at http://sarg.sourceforge.net/donations.php
//! The number of invalid lines found in the redirector report.
static int RedirectorErrors=0;
+//! The file containing the sorted entries.
+static char redirector_sorted[MAXLEN]="";
static void parse_log(FILE *fp_ou,char *buf)
{
const char *url;
char user[MAX_USER_LEN];
char ip[45];
+ char userlabel[MAX_USER_LEN];
long long int lmon, lday, lyear;
int mon, day, year;
int idata=0;
getword_start(&gwarea1,RedirectorLogFormat);
leks[0]='\0';
if (getword(leks,sizeof(leks),&gwarea1,'#')<0) {
- debuga(_("Invalid \"redirector_log_format\" option in your sarg.conf (too many characters before first tag)\n"));
+ debuga(__FILE__,__LINE__,_("Invalid \"redirector_log_format\" option in your sarg.conf (too many characters before first tag)\n"));
exit(EXIT_FAILURE);
}
year=0;
full_url[0]='\0';
while(strcmp(leks,"end") != 0) {
if (getword(leks,sizeof(leks),&gwarea1,'#')<0) {
- debuga(_("Invalid \"redirector_log_format\" option in your sarg.conf (missing # at end of tag)\n"));
+ debuga(__FILE__,__LINE__,_("Invalid \"redirector_log_format\" option in your sarg.conf (missing # at end of tag)\n"));
exit(EXIT_FAILURE);
}
if (getword(sep,sizeof(sep),&gwarea1,'#')<0) {
- debuga(_("Invalid \"redirector_log_format\" option in your sarg.conf (too many characters in column separator)\n"));
+ debuga(__FILE__,__LINE__,_("Invalid \"redirector_log_format\" option in your sarg.conf (too many characters in column separator)\n"));
exit(EXIT_FAILURE);
}
if(strcmp(leks,"end") != 0) {
if (getword_limit(res,sizeof(res),&gwarea,sep[0])<0) {
- debuga(_("Parsing of tag \"%s\" in redirector log %s returned no result\n"),leks,wentp);
+ debuga(__FILE__,__LINE__,_("Parsing of tag \"%s\" in redirector log \"%s\" returned no result\n"),leks,wentp);
RedirectorErrors++;
return;
}
day=atoi(res);
} else if(strcmp(leks,"hour") == 0) {
if (strlen(res)>=sizeof(hour)) {
- debuga(_("Hour string too long in redirector log file %s\n"),wentp);
+ debuga(__FILE__,__LINE__,_("Hour string too long in redirector log file \"%s\"\n"),wentp);
RedirectorErrors++;
return;
}
strcpy(hour,res);
} else if(strcmp(leks,"source") == 0) {
if (strlen(res)>=sizeof(source)) {
- debuga(_("Banning source name too long in redirector log file %s\n"),wentp);
+ debuga(__FILE__,__LINE__,_("Banning source name too long in redirector log file \"%s\"\n"),wentp);
RedirectorErrors++;
return;
}
strcpy(source,res);
} else if(strcmp(leks,"list") == 0) {
if (strlen(res)>=sizeof(list)) {
- debuga(_("Banning list name too long in redirector log file %s\n"),wentp);
+ debuga(__FILE__,__LINE__,_("Banning list name too long in redirector log file \"%s\"\n"),wentp);
RedirectorErrors++;
return;
}
strcpy(list,res);
} else if(strcmp(leks,"ip") == 0) {
if (strlen(res)>=sizeof(ip)) {
- debuga(_("IP address too long in redirector log file %s\n"),wentp);
+ debuga(__FILE__,__LINE__,_("IP address too long in redirector log file \"%s\"\n"),wentp);
RedirectorErrors++;
return;
}
strcpy(ip,res);
} else if(strcmp(leks,"user") == 0) {
if (strlen(res)>=sizeof(user)) {
- debuga(_("User ID too long in redirector log file %s\n"),wentp);
+ debuga(__FILE__,__LINE__,_("User ID too long in redirector log file \"%s\"\n"),wentp);
RedirectorErrors++;
return;
}
strcpy(user,res);
} else if(strcmp(leks,"url") == 0) {
- if (strlen(res)>=sizeof(full_url)) {
- debuga(_("URL too long in redirector log file %s\n"),wentp);
- RedirectorErrors++;
- return;
- }
- strcpy(full_url,res);
+ /*
+ * Don't worry about the url being truncated as we only keep the host name
+ * any way...
+ */
+ safe_strcpy(full_url,res,sizeof(full_url));
}
}
}
} else {
if (getword_atoll(&lyear,&gwarea,'-')<0 || getword_atoll(&lmon,&gwarea,'-')<0 ||
getword_atoll(&lday,&gwarea,' ')<0) {
- debuga(_("Invalid date found in file %s\n"),wentp);
+ debuga(__FILE__,__LINE__,_("Invalid date in file \"%s\"\n"),wentp);
RedirectorErrors++;
return;
}
mon=(int)lmon;
day=(int)lday;
if (getword(hour,sizeof(hour),&gwarea,' ')<0) {
- debuga(_("Invalid time found in file %s\n"),wentp);
+ debuga(__FILE__,__LINE__,_("Invalid time in file \"%s\"\n"),wentp);
RedirectorErrors++;
return;
}
if (getword_skip(MAXLEN,&gwarea,'(')<0 || getword(source,sizeof(source),&gwarea,'/')<0) {
- debuga(_("Invalid redirected source in file %s\n"),wentp);
+ debuga(__FILE__,__LINE__,_("Invalid redirected source in file \"%s\"\n"),wentp);
RedirectorErrors++;
return;
}
if (getword(list,sizeof(list),&gwarea,'/')<0) {
- debuga(_("Invalid redirected list in file %s\n"),wentp);
+ debuga(__FILE__,__LINE__,_("Invalid redirected list in file \"%s\"\n"),wentp);
RedirectorErrors++;
return;
}
if (getword_skip(MAXLEN,&gwarea,' ')<0 || getword_limit(full_url,sizeof(full_url),&gwarea,' ')<0) {
- debuga(_("Invalid URL in file %s\n"),wentp);
+ debuga(__FILE__,__LINE__,_("Invalid url in file \"%s\"\n"),wentp);
RedirectorErrors++;
return;
}
if (getword(ip,sizeof(ip),&gwarea,'/')<0) {
- debuga(_("Invalid source IP in file %s\n"),wentp);
+ debuga(__FILE__,__LINE__,_("Invalid source IP in file \"%s\"\n"),wentp);
RedirectorErrors++;
return;
}
if (getword_skip(MAXLEN,&gwarea,' ')<0 || getword(user,sizeof(user),&gwarea,' ')<0) {
- debuga(_("Invalid user in file %s\n"),wentp);
+ debuga(__FILE__,__LINE__,_("Invalid user in file \"%s\"\n"),wentp);
RedirectorErrors++;
return;
}
id_is_ip=true;
} else {
id_is_ip=false;
- if(strcmp(user,"-") == 0 || strcmp(user," ") == 0 || strcmp(user,"") == 0) {
+ if (user[0]=='\0' || (user[1]=='\0' && (user[0]=='-' || user[0]==' '))) {
if(RecordsWithoutUser == RECORDWITHOUTUSER_IP) {
strcpy(user,ip);
id_is_ip=true;
}
uinfo=userinfo_find_from_id(user);
if (!uinfo) {
- uinfo=userinfo_create(user);
- uinfo->id_is_ip=id_is_ip;
+ uinfo=userinfo_create(user,(id_is_ip) ? NULL : ip);
uinfo->no_report=true;
if(Ip2Name && id_is_ip) ip2name(user,sizeof(user));
- user_find(uinfo->label,MAX_USER_LEN, user);
+ user_find(userlabel,MAX_USER_LEN, user);
+ userinfo_label(uinfo,userlabel);
}
fprintf(fp_ou,"%s\t%04d%02d%02d\t%s\t%s\t%s\t",uinfo->id,year,mon,day,hour,ip,url);
if (source[0] && list[0])
static void read_log(const char *wentp, FILE *fp_ou,int dfrom,int duntil)
{
- FILE *fp_in = NULL;
+ FileObject *fp_in = NULL;
char *buf;
int i;
longline line;
if(debug) {
- debuga(_("Reading redirector log file %s\n"),wentp);
+ debuga(__FILE__,__LINE__,_("Reading redirector log file \"%s\"\n"),wentp);
}
/* With squidGuard, you can log groups in only one log file.
nfiles_done++;
files_done = realloc(files_done, nfiles_done*sizeof(char *));
if (!files_done) {
- debuga(_("Not enough memory to store the name of the new redirector log to be read - %s\n"),strerror(errno));
+ debuga(__FILE__,__LINE__,_("Not enough memory to store the name of the new redirector log to be read - %s\n"),strerror(errno));
exit(EXIT_FAILURE);
}
files_done[nfiles_done-1] = strdup(wentp);
if (!files_done[nfiles_done-1]) {
- debuga(_("Not enough memory to store the name of the new redirector log to be read - %s\n"),strerror(errno));
+ debuga(__FILE__,__LINE__,_("Not enough memory to store the name of the new redirector log to be read - %s\n"),strerror(errno));
exit(EXIT_FAILURE);
}
- if ((fp_in=fopen(wentp,"r"))==NULL) {
- debuga(_("(squidguard) Cannot open log file %s\n"),wentp);
+ if ((fp_in=FileObject_Open(wentp))==NULL) {
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),wentp,FileObject_GetLastOpenError());
exit(EXIT_FAILURE);
}
if ((line=longline_create())==NULL) {
- debuga(_("Not enough memory to read the redirector log\n"));
+ debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\"\n"),wentp);
exit(EXIT_FAILURE);
}
while ((buf=longline_read(fp_in,line)) != NULL) {
parse_log(fp_ou,buf);
}
- fclose(fp_in);
+ if (FileObject_Close(fp_in)) {
+ debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),wentp,FileObject_GetLastCloseError());
+ exit(EXIT_FAILURE);
+ }
longline_destroy(&line);
return;
}
FILE *fp_ou = NULL, *fp_guard = NULL;
char buf[MAXLEN];
char guard_in[MAXLEN];
- char guard_ou[MAXLEN];
char logdir[MAXLEN];
char user[MAXLEN];
char tmp6[MAXLEN];
str2 = user;
if(SquidGuardConf[0] == '\0' && NRedirectorLogs == 0) {
- if (debugz) debugaz(_("No redirector logs provided to produce that kind of report\n"));
+ if (debugz>=LogLevel_Process) debugaz(__FILE__,__LINE__,_("No redirector logs provided to produce that kind of report\n"));
return;
}
- sprintf(guard_in,"%s/redirector.unsort",tmp);
- sprintf(guard_ou,"%s/redirector.log",tmp);
- if((fp_ou=fopen(guard_in,"a"))==NULL) {
- debuga(_("(squidguard) Cannot open log file %s\n"),guard_in);
+ snprintf(guard_in,sizeof(guard_in),"%s/redirector.int_unsort",tmp);
+ if((fp_ou=fopen(guard_in,"w"))==NULL) {
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),guard_in,strerror(errno));
exit(EXIT_FAILURE);
}
read_log(RedirectorLogs[i],fp_ou,dfrom,duntil);
} else {
if(access(SquidGuardConf, R_OK) != 0) {
- debuga(_("Cannot open squidGuard config file: %s\n"),SquidGuardConf);
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),SquidGuardConf,strerror(errno));
exit(EXIT_FAILURE);
}
if((fp_guard=fopen(SquidGuardConf,"r"))==NULL) {
- debuga(_("(squidguard) Cannot open log file %s\n"),SquidGuardConf);
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),SquidGuardConf,strerror(errno));
exit(EXIT_FAILURE);
}
read_log(wentp,fp_ou,dfrom,duntil);
}
}
+ if (fclose(fp_guard)==EOF) {
+ debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),SquidGuardConf,strerror(errno));
+ exit(EXIT_FAILURE);
+ }
}
- if (fp_guard) fclose(fp_guard);
- if (fp_ou) fclose(fp_ou);
+ if (fp_ou && fclose(fp_ou)==EOF) {
+ debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),guard_in,strerror(errno));
+ exit(EXIT_FAILURE);
+ }
if (files_done) {
for (y=0; y<nfiles_done; y++)
free(files_done);
}
- if(debug) {
- debuga(_("Sorting file: %s\n"),guard_ou);
- }
+ if (redirector_count) {
+ snprintf(redirector_sorted,sizeof(redirector_sorted),"%s/redirector.int_log",tmp);
+ if(debug) {
+ debuga(__FILE__,__LINE__,_("Sorting file \"%s\"\n"),redirector_sorted);
+ }
- if (snprintf(tmp6,sizeof(tmp6),"sort -t \"\t\" -k 1,1 -k 2,2 -k 4,4 \"%s\" -o \"%s\"",guard_in, guard_ou)>=sizeof(tmp6)) {
- debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),guard_in,guard_ou);
- exit(EXIT_FAILURE);
- }
- cstatus=system(tmp6);
- if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
- debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
- debuga(_("sort command: %s\n"),tmp6);
- exit(EXIT_FAILURE);
+ if (snprintf(tmp6,sizeof(tmp6),"sort -t \"\t\" -k 1,1 -k 2,2 -k 4,4 \"%s\" -o \"%s\"",guard_in, redirector_sorted)>=sizeof(tmp6)) {
+ debuga(__FILE__,__LINE__,_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),guard_in,redirector_sorted);
+ exit(EXIT_FAILURE);
+ }
+ cstatus=system(tmp6);
+ if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
+ debuga(__FILE__,__LINE__,_("sort command return status %d\n"),WEXITSTATUS(cstatus));
+ debuga(__FILE__,__LINE__,_("sort command: %s\n"),tmp6);
+ exit(EXIT_FAILURE);
+ }
}
- if (unlink(guard_in)) {
- debuga(_("Cannot delete %s - %s\n"),guard_in,strerror(errno));
+ if (!KeepTempLog && unlink(guard_in)) {
+ debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),guard_in,strerror(errno));
exit(EXIT_FAILURE);
}
return;
void redirector_report(void)
{
- FILE *fp_in = NULL, *fp_ou = NULL;
+ FileObject *fp_in = NULL;
+ FILE *fp_ou = NULL;
char *buf;
char *url;
- char squidguard_in[MAXLEN];
char report[MAXLEN];
char ip[45];
char rule[255];
ouser[0]='\0';
ouser2[0]='\0';
- sprintf(squidguard_in,"%s/redirector.log",tmp);
if(!redirector_count) {
- unlink(squidguard_in);
- if (debugz) debugaz(_("Redirector report not generated because it is empty\n"));
+ if (debugz>=LogLevel_Process) {
+ if (redirector_sorted[0])
+ debugaz(__FILE__,__LINE__,_("Redirector report not generated because it is empty\n"));
+ }
return;
}
snprintf(report,sizeof(report),"%s/redirector.html",outdirname);
- if((fp_in=fopen(squidguard_in,"r"))==NULL) {
- debuga(_("(squidguard) Cannot open log file %s\n"),squidguard_in);
+ if((fp_in=FileObject_Open(redirector_sorted))==NULL) {
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),redirector_sorted,FileObject_GetLastOpenError());
exit(EXIT_FAILURE);
}
if((fp_ou=fopen(report,"w"))==NULL) {
- debuga(_("(squidguard) Cannot open log file %s\n"),report);
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),report,strerror(errno));
exit(EXIT_FAILURE);
}
if ((line=longline_create())==NULL) {
- debuga(_("Not enough memory to read the processed redirector log\n"));
+ debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\"\n"),redirector_sorted);
exit(EXIT_FAILURE);
}
while((buf=longline_read(fp_in,line))!=NULL) {
getword_start(&gwarea,buf);
if (getword(user,sizeof(user),&gwarea,'\t')<0) {
- debuga(_("Invalid user in file %s\n"),squidguard_in);
+ debuga(__FILE__,__LINE__,_("Invalid user in file \"%s\"\n"),redirector_sorted);
exit(EXIT_FAILURE);
}
if (getword_atoll(&data2,&gwarea,'\t')<0) {
- debuga(_("Invalid date in file %s\n"),squidguard_in);
+ debuga(__FILE__,__LINE__,_("Invalid date in file \"%s\"\n"),redirector_sorted);
exit(EXIT_FAILURE);
}
if (getword(hora,sizeof(hora),&gwarea,'\t')<0) {
- debuga(_("Invalid time in file %s\n"),squidguard_in);
+ debuga(__FILE__,__LINE__,_("Invalid time in file \"%s\"\n"),redirector_sorted);
exit(EXIT_FAILURE);
}
if (getword(ip,sizeof(ip),&gwarea,'\t')<0) {
- debuga(_("Invalid IP address in file %s\n"),squidguard_in);
+ debuga(__FILE__,__LINE__,_("Invalid IP address in file \"%s\"\n"),redirector_sorted);
exit(EXIT_FAILURE);
}
if (getword_ptr(buf,&url,&gwarea,'\t')<0) {
- debuga(_("Invalid URL in file %s\n"),squidguard_in);
+ debuga(__FILE__,__LINE__,_("Invalid url in file \"%s\"\n"),redirector_sorted);
exit(EXIT_FAILURE);
}
if (getword(rule,sizeof(rule),&gwarea,'\n')<0) {
- debuga(_("Invalid rule in file %s\n"),squidguard_in);
+ debuga(__FILE__,__LINE__,_("Invalid rule in file \"%s\"\n"),redirector_sorted);
exit(EXIT_FAILURE);
}
uinfo=userinfo_find_from_id(user);
if (!uinfo) {
- debuga(_("Unknown user ID %s in file %s\n"),user,squidguard_in);
+ debuga(__FILE__,__LINE__,_("Unknown user ID %s in file \"%s\"\n"),user,redirector_sorted);
exit(EXIT_FAILURE);
}
output_html_link(fp_ou,url,100);
fprintf(fp_ou,"</td><td class=\"data2\">%s</td></tr>\n",rule);
}
- fclose(fp_in);
+ if (FileObject_Close(fp_in)) {
+ debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),redirector_sorted,FileObject_GetLastCloseError());
+ exit(EXIT_FAILURE);
+ }
longline_destroy(&line);
if(count>SquidGuardReportLimit && SquidGuardReportLimit>0)
show_ignored_redirector(fp_ou,count-SquidGuardReportLimit);
fputs("</table>\n",fp_ou);
-
+
if (RedirectorErrors>0)
{
fputs("<div class=\"warn\"><span>",fp_ou);
fprintf(fp_ou,ngettext("%d error found in the log file. Some entries may be missing.","%d errors found in the log file. Some entries may be missing.",RedirectorErrors),RedirectorErrors);
fputs("</span></div>\n",fp_ou);
}
-
+
fputs("</div>\n",fp_ou);
- if (write_html_trailer(fp_ou)<0)
- debuga(_("Write error in file %s\n"),report);
- if (fclose(fp_ou)==EOF)
- debuga(_("Failed to close file %s - %s\n"),report,strerror(errno));
+ write_html_trailer(fp_ou);
+ if (fclose(fp_ou)==EOF) {
+ debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),report,strerror(errno));
+ exit(EXIT_FAILURE);
+ }
- if (unlink(squidguard_in)) {
- debuga(_("Cannot delete %s - %s\n"),squidguard_in,strerror(errno));
+ if (!KeepTempLog && unlink(redirector_sorted)) {
+ debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),redirector_sorted,strerror(errno));
exit(EXIT_FAILURE);
}