"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "download.c" between
sarg-2.3.11.tar.gz and sarg-2.4.0.tar.gz

About: SARG ia a Squid Analysis Report Generator.

download.c  (sarg-2.3.11):download.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"
/*! /*!
The buffer to store the list of the suffixes to take into account when generatin g The buffer to store the list of the suffixes to take into account when generatin g
the report of the downloaded files. The suffixes in the list are separated by th e ASCII the report of the downloaded files. The suffixes in the list are separated by th e ASCII
null. null.
*/ */
/*@null@*/static char *DownloadSuffix=NULL; /*@null@*/static char *DownloadSuffix=NULL;
/*! /*!
The index of all the suffixes stored in ::DownloadSuffix. The list is sorted alp habetically. The index of all the suffixes stored in ::DownloadSuffix. The list is sorted alp habetically.
to speed up the search. to speed up the search.
*/ */
/*@null@*/static char **DownloadSuffixIndex=NULL; /*@null@*/static char **DownloadSuffixIndex=NULL;
/*! /*!
The number of suffixes in ::DownloadSuffixIndex. The number of suffixes in ::DownloadSuffixIndex.
*/ */
static int NDownloadSuffix=0; static int NDownloadSuffix=0;
//! Name of the file containing the unsorted downloaded entries.
static char download_unsort[MAXLEN]="";
//! The file handle to write the entries.
static FILE *fp_download=NULL;
//! \c True if at least one downloaded entry exists.
static bool download_exists=false;
/*!
Open a file to store the denied accesses.
\return The file handle or NULL if no file is necessary.
*/
void download_open(void)
{
if ((ReportType & REPORT_TYPE_DOWNLOADS) == 0) {
if (debugz>=LogLevel_Process) debugaz(__FILE__,__LINE__,_("Downlo
ad report not produced as it is not requested\n"));
return;
}
if (Privacy) {
if (debugz>=LogLevel_Process) debugaz(__FILE__,__LINE__,_("Downlo
ad report not produced because privacy option is active\n"));
return;
}
format_path(__FILE__, __LINE__, download_unsort, sizeof(download_unsort),
"%s/download.int_unsort", tmp);
if ((fp_download=MY_FOPEN(download_unsort,"w"))==NULL) {
debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),downl
oad_unsort,strerror(errno));
exit(EXIT_FAILURE);
}
return;
}
/*!
Write one entry in the unsorted downloaded file provided that it is required.
\param log_entry The entry to write into the log file.
\param url The URL of the downloaded file.
*/
void download_write(const struct ReadLogStruct *log_entry,const char *url)
{
char date[80];
if (fp_download && strstr(log_entry->HttpCode,"DENIED") == 0) {
strftime(date,sizeof(date),"%d/%m/%Y\t%H:%M:%S",&log_entry->Entry
Time);
fprintf(fp_download,"%s\t%s\t%s\t%s\n",date,log_entry->User,log_e
ntry->Ip,url);
download_exists=true;
}
}
/*!
Close the file opened by denied_open().
*/
void download_close(void)
{
if (fp_download)
{
if (fclose(fp_download)==EOF) {
debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n")
,download_unsort,strerror(errno));
exit(EXIT_FAILURE);
}
fp_download=NULL;
}
}
/*!
Tell the caller if a download report exists.
\return \c True if the report is available or \c false if no report
was generated.
*/
bool is_download(void)
{
return(download_exists);
}
/*! /*!
Sort the raw log file with the downloaded files. Sort the raw log file with the downloaded files.
\param report_in The name of the file where to store the sorted entries. \param report_in The name of the file where to store the sorted entries.
The file is sorted by columns 3, 1, 2 and 5 that are the columns of the user's I D, the The file is sorted by columns 3, 1, 2 and 5 that are the columns of the user's I D, the
date, the time and the URL. date, the time and the URL.
*/ */
static void download_sort(const char *report_in) static void download_sort(const char *report_in)
{ {
int clen; int clen;
char csort[MAXLEN]; char csort[MAXLEN];
int cstatus; int cstatus;
clen=snprintf(csort,sizeof(csort),"sort -T \"%s\" -t \"\t\" -k 3,3 -k 1,1 clen=snprintf(csort,sizeof(csort),"sort -T \"%s\" -t \"\t\" -k 3,3 -k 1,1
-k 2,2 -k 5,5 -o \"%s\" \"%s/download.int_unsort\"", -k 2,2 -k 5,5 -o \"%s\" \"%s\"",
tmp, report_in, tmp); tmp, report_in, download_unsort);
if (clen>=sizeof(csort)) { if (clen>=sizeof(csort)) {
debuga(_("Command too long: ")); debuga(__FILE__,__LINE__,_("Path too long to sort file \"%s\"\n")
debuga_more("sort -T \"%s\" -t \"\t\" -k 3,3 -k 1,1 -k 2,2 -k 5,5 ,download_unsort);
-o \"%s\" \"%s/download.int_unsort\"",
tmp, report_in, tmp);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
cstatus=system(csort); cstatus=system(csort);
if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)) debuga(__FILE__,__LINE__,_("sort command return status %d\n"),WEX
; ITSTATUS(cstatus));
debuga(_("sort command: %s\n"),csort); debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
exit(EXIT_FAILURE);
}
if (snprintf(csort,sizeof(csort),"%s/download.int_unsort",tmp)>=sizeof(cs
ort)) {
debuga(_("Path too long: "));
debuga_more("%s/download.int_unsort\n",tmp);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (!KeepTempLog && unlink(csort)) { if (!KeepTempLog) {
debuga(_("Cannot delete \"%s\": %s\n"),csort,strerror(errno)); if (unlink(download_unsort)) {
exit(EXIT_FAILURE); debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),
download_unsort,strerror(errno));
exit(EXIT_FAILURE);
}
download_unsort[0]='\0';
} }
} }
/*! /*!
Generate the report of the downloaded files. The list of the suffixes to take in to account Generate the report of the downloaded files. The list of the suffixes to take in to account
is set with set_download_suffix(). is set with set_download_suffix().
*/ */
void download_report(void) void download_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 report_in[MAXLEN]; char report_in[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];
skipping to change at line 116 skipping to change at line 188
int z=0; int z=0;
int count=0; int count=0;
int i; int i;
int day,month,year; int day,month,year;
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;
if (!ndownload) { if (!download_exists) {
if (debugz) debugaz(_("No downloaded files to report\n")); if (!KeepTempLog && download_unsort[0]!='\0' && unlink(download_u
nsort))
debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),
download_unsort,strerror(errno));
download_unsort[0]='\0';
if (debugz>=LogLevel_Process) debugaz(__FILE__,__LINE__,_("No dow
nloaded files to report\n"));
return; return;
} }
if (debugz>=LogLevel_Process)
debuga(__FILE__,__LINE__,_("Creating download report...\n"));
ouser[0]='\0'; ouser[0]='\0';
ouser2[0]='\0'; ouser2[0]='\0';
// sort the raw file // sort the raw file
snprintf(report_in,sizeof(report_in),"%s/download.int_log",tmp); format_path(__FILE__, __LINE__, report_in, sizeof(report_in), "%s/downloa d.int_log", tmp);
download_sort(report_in); download_sort(report_in);
if(access(report_in, R_OK) != 0) {
debugaz(_("Sorted file doesn't exist (to produce the download rep
ort)\n"));
exit(EXIT_FAILURE);
}
// produce the report. // produce the report.
snprintf(report,sizeof(report),"%s/download.html",outdirname); format_path(__FILE__, __LINE__, report, sizeof(report), "%s/download.html ", outdirname);
if((fp_in=MY_FOPEN(report_in,"r"))==NULL) { if ((fp_in=FileObject_Open(report_in))==NULL) {
debugapos("download",_("Cannot open file \"%s\": %s\n"),report_in debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),repor
,strerror(errno)); t_in,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("download",_("Cannot open file \"%s\": %s\n"),report,st debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),repor
rerror(errno)); t,strerror(errno));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Downloa ds"),HTML_JS_NONE); write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Downloa ds"),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",_("Downloads")) ; fprintf(fp_ou,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Downloads")) ;
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"),report_in); debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\" \n"),report_in);
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"),report_in); debuga(__FILE__,__LINE__,_("Invalid record in file \"%s\" \n"),report_in);
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"),report_in); debuga(__FILE__,__LINE__,_("Invalid url in file \"%s\"\n" ),report_in);
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,repo rt_in); debuga(__FILE__,__LINE__,_("Unknown user ID %s in file \" %s\"\n"),user,report_in);
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++; z++;
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(DownloadReportLimit) { if (DownloadReportLimit) {
if(strcmp(ouser2,uinfo->label) == 0) { if (strcmp(ouser2,uinfo->label) == 0) {
count++; count++;
} else { } else {
count=1; count=1;
strcpy(ouser2,uinfo->label); strcpy(ouser2,uinfo->label);
} }
if(count >= DownloadReportLimit) if (count >= DownloadReportLimit)
continue; continue;
} }
for (i=strlen(url)-1 ; i>=0 && (unsigned char)url[i]<' ' ; i--) u rl[i]=0; for (i=strlen(url)-1 ; i>=0 && (unsigned char)url[i]<' ' ; i--) u rl[i]=0;
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,Blo ckIt); fprintf(fp_ou,"<a href=\"%s%s?url=\"",wwwDocumentRoot,Blo ckIt);
output_html_url(fp_ou,url); output_html_url(fp_ou,url);
fprintf(fp_ou,"\"><img src=\"%s/sarg-squidguard-block.png \"></a>&nbsp;",ImageFile); fprintf(fp_ou,"\"><img src=\"%s/sarg-squidguard-block.png \"></a>&nbsp;",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"),report_i
n,FileObject_GetLastCloseError());
exit(EXIT_FAILURE);
}
longline_destroy(&line); longline_destroy(&line);
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(report_in)) { if (!KeepTempLog && unlink(report_in)) {
debuga(_("Cannot delete \"%s\": %s\n"),report_in,strerror(errno)) ; debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),report_i n,strerror(errno));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
return; return;
} }
/*! /*!
Free the memory allocated by set_download_suffix(). Free the memory allocated by set_download_suffix().
*/ */
void free_download(void) void free_download(void)
skipping to change at line 278 skipping to change at line 355
void set_download_suffix(const char *list) void set_download_suffix(const char *list)
{ {
char *str; char *str;
int i, j, k; int i, j, k;
int cmp; int cmp;
free_download(); free_download();
DownloadSuffix=strdup(list); DownloadSuffix=strdup(list);
if (!DownloadSuffix) { if (!DownloadSuffix) {
debuga(_("Download suffix list too long\n")); debuga(__FILE__,__LINE__,_("Download suffix list too long\n"));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
j = 1; j = 1;
for (i=0 ; list[i] ; i++) for (i=0 ; list[i] ; i++)
if (list[i] == ',') j++; if (list[i] == ',') j++;
DownloadSuffixIndex=malloc(j*sizeof(char *)); DownloadSuffixIndex=malloc(j*sizeof(char *));
if (!DownloadSuffixIndex) { if (!DownloadSuffixIndex) {
debuga(_("Too many download suffixes\n")); debuga(__FILE__,__LINE__,_("Too many download suffixes\n"));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
str = DownloadSuffix; str = DownloadSuffix;
for (i=0 ; DownloadSuffix[i] ; i++) { for (i=0 ; DownloadSuffix[i] ; i++) {
if (DownloadSuffix[i] == ',') { if (DownloadSuffix[i] == ',') {
DownloadSuffix[i] = '\0'; DownloadSuffix[i] = '\0';
if (*str) { if (*str) {
cmp = -1; cmp = -1;
for (j=0 ; j<NDownloadSuffix && (cmp=strcasecmp(s tr,DownloadSuffixIndex[j]))>0 ; j++); for (j=0 ; j<NDownloadSuffix && (cmp=strcasecmp(s tr,DownloadSuffixIndex[j]))>0 ; j++);
skipping to change at line 372 skipping to change at line 449
center=(down+up)/2; center=(down+up)/2;
cmp=strcasecmp(suffix,DownloadSuffixIndex[center]); cmp=strcasecmp(suffix,DownloadSuffixIndex[center]);
if (cmp == 0) return(true); if (cmp == 0) return(true);
if (cmp < 0) if (cmp < 0)
up = center-1; up = center-1;
else else
down = center+1; down = center+1;
} }
return(false); return(false);
} }
/*!
Remove any temporary file left by the download module.
*/
void download_cleanup(void)
{
if (fp_download) {
if (fclose(fp_download)==EOF) {
debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n")
,download_unsort,strerror(errno));
exit(EXIT_FAILURE);
}
fp_download=NULL;
}
if (download_unsort[0]) {
if (unlink(download_unsort)==-1)
debuga(__FILE__,__LINE__,_("Failed to delete \"%s\": %s\n
"),download_unsort,strerror(errno));
}
}
 End of changes. 31 change blocks. 
56 lines changed or deleted 143 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)