"Fossies" - the Fresh Open Source Software Archive  

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

About: SARG ia a Squid Analysis Report Generator.

log.c  (sarg-2.3.11):log.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"
#include "include/filelist.h"
#ifdef HAVE_GETOPT_H #ifdef HAVE_GETOPT_H
#include <getopt.h> #include <getopt.h>
#endif #endif
#define REPORT_EVERY_X_LINES 5000 //! The log file filtering.
#define MAX_OPEN_USER_FILES 10 struct ReadLogDataStruct ReadFilter;
struct userfilestruct //! The list of the system users.
{ /*@null@*/char *userfile=NULL;
struct userfilestruct *next;
struct userinfostruct *user;
FILE *file;
};
/*@null@*/static char *userfile=NULL;
numlist weekdays = { { 0, 1, 2, 3, 4, 5, 6 }, 7 };
numlist hours = { { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17
, 18, 19, 20, 21, 22, 23 }, 24 };
//! List of the input log files to process.
FileListObject AccessLog=NULL;
//! Selected locale set through the environment variable. //! Selected locale set through the environment variable.
char *CurrentLocale=NULL; char *CurrentLocale=NULL;
//! Set to \c true if a useragent log is provided on the command line.
bool UserAgentFromCmdLine=false;
extern FileListObject UserAgentLog;
static void getusers(const char *pwdfile, int debug); static void getusers(const char *pwdfile, int debug);
static void CleanTemporaryDir();
int main(int argc,char *argv[]) int main(int argc,char *argv[])
{ {
enum isa_col_id {
ISACOL_Ip,
ISACOL_UserName,
ISACOL_Date,
ISACOL_Time,
ISACOL_TimeTaken,
ISACOL_Bytes,
ISACOL_Uri,
ISACOL_Status,
ISACOL_Last //last entry of the list !
};
enum InputLogFormat {
ILF_Unknown,
ILF_Squid,
ILF_Common,
ILF_Sarg,
ILF_Isa,
ILF_Last //last entry of the list !
};
FILE *fp_in = NULL, *fp_denied=NULL, *fp_authfail=NULL, *fp_log=NULL;
char sz_Download_Unsort[ 20000 ] ;
FILE * fp_Download_Unsort = NULL ;
extern int optind; extern int optind;
extern int optopt; extern int optopt;
extern char *optarg; extern char *optarg;
char data[255];
char elap[255];
char ip[MAXLEN];
char tam[255];
char fun[MAXLEN];
char wuser[MAXLEN];
char smartfilter[MAXLEN];
char dia[128];
char mes[30];
char hora[30];
char date[255];
char arq[255];
char arq_log[255];
int hm, hmf, hmr;
char hm_str[15]; char hm_str[15];
char uagent[MAXLEN];
char hexclude[MAXLEN]; char hexclude[MAXLEN];
char csort[MAXLEN];
int cstatus;
char tbuf2[128];
char *str;
char tmp3[MAXLEN];
char denied_unsort[MAXLEN];
char denied_sort[MAXLEN];
char authfail_unsort[MAXLEN];
char start_hour[128];
char *linebuf;
const char *url;
char *full_url;
char user[MAX_USER_LEN];
char splitprefix[MAXLEN]; char splitprefix[MAXLEN];
enum InputLogFormat ilf;
int ilf_count[ILF_Last];
int ch; int ch;
int x;
int errflg=0; int errflg=0;
int puser=0;
bool fhost=false;
bool dns=false; bool dns=false;
bool fuser=false;
int idata=0;
int mindate=0;
int maxdate=0;
int iarq=0; int iarq=0;
int isa_ncols=0,isa_cols[ISACOL_Last];
int lastlog=-1; int lastlog=-1;
long long int nbytes; int LogStatus;
long int elap_time;
bool from_stdin;
bool from_pipe;
int blen;
int maxopenfiles;
int nopen;
bool id_is_ip;
long totregsl=0;
long totregsg=0;
long totregsx=0;
bool totper=false;
long int max_elapsed=0;
long long int iyear, imonth, iday;
bool realt; bool realt;
bool userip; bool userip;
struct tm tt;
struct tm *t;
unsigned long recs1=0UL;
unsigned long recs2=0UL;
unsigned long int lines_read=0UL;
unsigned long int records_kept=0UL;
unsigned long int nusers=0UL;
int OutputNonZero = REPORT_EVERY_X_LINES ;
bool download_flag=false;
char download_url[MAXLEN];
struct getwordstruct gwarea;
longline line;
time_t tnum;
time_t start_time; time_t start_time;
time_t end_time; time_t end_time;
time_t read_start_time; time_t read_start_time;
time_t read_end_time; time_t read_end_time;
time_t process_start_time; time_t process_start_time;
time_t process_end_time; time_t process_end_time;
double read_elapsed; double read_elapsed;
double process_elapsed; double process_elapsed;
struct stat logstat; FileListIterator FIter;
struct userinfostruct *uinfo;
struct userfilestruct *first_user_file, *ufile, *ufile1, *prev_ufile;
static int split=0; static int split=0;
static int convert=0; static int convert=0;
static int output_css=0; static int output_css=0;
static int show_statis=0; static int show_statis=0;
static int show_version=0;
int option_index; int option_index;
static struct option long_options[]= static struct option long_options[]=
{ {
{"convert",no_argument,&convert,1}, {"convert",no_argument,&convert,1},
{"css",no_argument,&output_css,1}, {"css",no_argument,&output_css,1},
{"help",no_argument,NULL,'h'}, {"help",no_argument,NULL,'h'},
{"lastlog",required_argument,NULL,2}, {"lastlog",required_argument,NULL,2},
{"keeplogs",no_argument,NULL,3}, {"keeplogs",no_argument,NULL,3},
{"split",no_argument,&split,1}, {"split",no_argument,&split,1},
{"splitprefix",required_argument,NULL,'P'}, {"splitprefix",required_argument,NULL,'P'},
{"statistics",no_argument,&show_statis,1}, {"statistics",no_argument,&show_statis,1},
{"version",no_argument,&show_version,'V'},
{0,0,0,0} {0,0,0,0}
}; };
start_time=time(NULL); start_time=time(NULL);
#ifdef HAVE_LOCALE_H #ifdef HAVE_LOCALE_H
setlocale(LC_TIME,""); setlocale(LC_TIME,"");
#endif #endif
#if defined(ENABLE_NLS) && defined(HAVE_LOCALE_H) #if defined(ENABLE_NLS) && defined(HAVE_LOCALE_H)
skipping to change at line 216 skipping to change at line 127
fprintf(stderr,"SARG: Cannot set gettext domain for %s PACKAGE_NA ME (%s)\n",PACKAGE_NAME,strerror(errno)); fprintf(stderr,"SARG: Cannot set gettext domain for %s PACKAGE_NA ME (%s)\n",PACKAGE_NAME,strerror(errno));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
#endif //ENABLE_NLS #endif //ENABLE_NLS
BgImage[0]='\0'; BgImage[0]='\0';
LogoImage[0]='\0'; LogoImage[0]='\0';
LogoText[0]='\0'; LogoText[0]='\0';
PasswdFile[0]='\0'; PasswdFile[0]='\0';
OutputEmail[0]='\0'; OutputEmail[0]='\0';
UserAgentLog[0]='\0';
ExcludeHosts[0]='\0'; ExcludeHosts[0]='\0';
ExcludeUsers[0]='\0'; ExcludeUsers[0]='\0';
ConfigFile[0]='\0'; ConfigFile[0]='\0';
code[0]='\0'; code[0]='\0';
LastLog=0; LastLog=0;
ReportType=0UL; ReportType=0UL;
UserTabFile[0]='\0'; UserTabFile[0]='\0';
BlockIt[0]='\0'; BlockIt[0]='\0';
ExternalCSSFile[0]='\0'; ExternalCSSFile[0]='\0';
RedirectorLogFormat[0]='\0'; RedirectorLogFormat[0]='\0';
NRedirectorLogs=0; NRedirectorLogs=0;
for (ilf=0 ; ilf<ILF_Last ; ilf++) ilf_count[ilf]=0;
snprintf(ExcludeCodes,sizeof(ExcludeCodes),"%s/exclude_codes",SYSCONFDIR) ; snprintf(ExcludeCodes,sizeof(ExcludeCodes),"%s/exclude_codes",SYSCONFDIR) ;
strcpy(GraphDaysBytesBarColor,"orange"); strcpy(GraphDaysBytesBarColor,"orange");
strcpy(BgColor,"#ffffff"); strcpy(BgColor,"#ffffff");
strcpy(TxColor,"#000000"); strcpy(TxColor,"#000000");
strcpy(TxBgColor,"lavender"); strcpy(TxBgColor,"lavender");
strcpy(TiColor,"darkblue"); strcpy(TiColor,"darkblue");
strcpy(Width,"80"); strcpy(Width,"80");
strcpy(Height,"45"); strcpy(Height,"45");
strcpy(LogoTextColor,"#000000"); strcpy(LogoTextColor,"#000000");
strcpy(HeaderColor,"darkblue"); strcpy(HeaderColor,"darkblue");
strcpy(HeaderBgColor,"#dddddd"); strcpy(HeaderBgColor,"#dddddd");
strcpy(LogoTextColor,"#006699"); strcpy(LogoTextColor,"#006699");
strcpy(FontSize,"9px"); strcpy(FontSize,"9px");
strcpy(TempDir,"/tmp"); strcpy(TempDir,"/tmp");
TempDirPath[0] = '\0';
strcpy(OutputDir,"/var/www/html/squid-reports"); strcpy(OutputDir,"/var/www/html/squid-reports");
AnonymousOutputFiles=false; AnonymousOutputFiles=false;
Ip2Name=false; Ip2Name=false;
strcpy(DateFormat,"u"); DateFormat='u';
OverwriteReport=false; OverwriteReport=false;
RemoveTempFiles=true; RemoveTempFiles=true;
strcpy(ReplaceIndex,"index.html"); strcpy(ReplaceIndex,INDEX_HTML_FILE);
Index=INDEX_YES; Index=INDEX_YES;
RecordsWithoutUser=RECORDWITHOUTUSER_IP; RecordsWithoutUser=RECORDWITHOUTUSER_IP;
UseComma=0; UseComma=0;
strcpy(MailUtility,"mailx"); strcpy(MailUtility,"mailx");
TopSitesNum=100; TopSitesNum=100;
TopUsersNum=0; TopUsersNum=0;
UserIp=0; UserIp=0;
TopuserSort=TOPUSER_SORT_BYTES | TOPUSER_SORT_REVERSE; TopuserSort=TOPUSER_SORT_BYTES | TOPUSER_SORT_REVERSE;
UserSort=USER_SORT_BYTES | USER_SORT_REVERSE; UserSort=USER_SORT_BYTES | USER_SORT_REVERSE;
TopsitesSort=TOPSITE_SORT_CONNECT | TOPSITE_SORT_REVERSE; TopsitesSort=TOPSITE_SORT_CONNECT | TOPSITE_SORT_REVERSE;
skipping to change at line 280 skipping to change at line 190
TOPUSERFIELDS_BYTES | TOPUSERFIELDS_SETYB | TOPUSERFIELDS_IN_CACHE_ OUT | TOPUSERFIELDS_BYTES | TOPUSERFIELDS_SETYB | TOPUSERFIELDS_IN_CACHE_ OUT |
TOPUSERFIELDS_USED_TIME | TOPUSERFIELDS_MILISEC | TOPUSERFIELDS_PTI ME | TOPUSERFIELDS_USED_TIME | TOPUSERFIELDS_MILISEC | TOPUSERFIELDS_PTI ME |
TOPUSERFIELDS_TOTAL | TOPUSERFIELDS_AVERAGE; TOPUSERFIELDS_TOTAL | TOPUSERFIELDS_AVERAGE;
UserReportFields=USERREPORTFIELDS_CONNECT | USERREPORTFIELDS_BYTES | USER REPORTFIELDS_SETYB | UserReportFields=USERREPORTFIELDS_CONNECT | USERREPORTFIELDS_BYTES | USER REPORTFIELDS_SETYB |
USERREPORTFIELDS_IN_CACHE_OUT | USERREPORTFIELDS_USED_TIME | USERRE PORTFIELDS_MILISEC | USERREPORTFIELDS_IN_CACHE_OUT | USERREPORTFIELDS_USED_TIME | USERRE PORTFIELDS_MILISEC |
USERREPORTFIELDS_PTIME | USERREPORTFIELDS_TOTAL | USERREPORTFIELDS_ AVERAGE; USERREPORTFIELDS_PTIME | USERREPORTFIELDS_TOTAL | USERREPORTFIELDS_ AVERAGE;
strcpy(DataFileDelimiter,";"); strcpy(DataFileDelimiter,";");
DataFileFields=DATA_FIELD_USER | DATA_FIELD_DATE | DATA_FIELD_TIME | DATA _FIELD_URL | DATA_FIELD_CONNECT | DataFileFields=DATA_FIELD_USER | DATA_FIELD_DATE | DATA_FIELD_TIME | DATA _FIELD_URL | DATA_FIELD_CONNECT |
DATA_FIELD_BYTES | DATA_FIELD_IN_CACHE | DATA_FIELD_OUT_CACHE | DAT A_FIELD_ELAPSED; DATA_FIELD_BYTES | DATA_FIELD_IN_CACHE | DATA_FIELD_OUT_CACHE | DAT A_FIELD_ELAPSED;
ShowReadStatistics=true; ShowReadStatistics=true;
ShowReadPercent=false;
strcpy(IndexSortOrder,"D"); strcpy(IndexSortOrder,"D");
ShowSargInfo=true; ShowSargInfo=true;
ShowSargLogo=true; ShowSargLogo=true;
ParsedOutputLog[0]='\0'; ParsedOutputLog[0]='\0';
strcpy(ParsedOutputLogCompress,"/bin/gzip -f"); strcpy(ParsedOutputLogCompress,"/bin/gzip -f");
DisplayedValues=DISPLAY_ABBREV; DisplayedValues=DISPLAY_ABBREV;
strcpy(HeaderFontSize,"9px"); strcpy(HeaderFontSize,"9px");
strcpy(TitleFontSize,"11px"); strcpy(TitleFontSize,"11px");
strcpy(AuthUserTemplateFile,"sarg_htaccess"); strcpy(AuthUserTemplateFile,"sarg_htaccess");
set_download_suffix("7z,ace,arj,avi,bat,bin,bz2,bzip,cab,com,cpio,dll,doc ,dot,exe,gz,iso,lha,lzh,mdb,mov,mp3,mpeg,mpg,mso,nrg,ogg,ppt,rar,rtf,shs,src,sys ,tar,tgz,vcd,vob,wma,wmv,zip"); set_download_suffix("7z,ace,arj,avi,bat,bin,bz2,bzip,cab,com,cpio,dll,doc ,dot,exe,gz,iso,lha,lzh,mdb,mov,mp3,mpeg,mpg,mso,nrg,ogg,ppt,rar,rtf,shs,src,sys ,tar,tgz,vcd,vob,wma,wmv,zip");
Graphs=true; Graphs=true;
#if defined(FONTDIR) #if defined(FONTDIR)
strcpy(GraphFont,FONTDIR"/DejaVuSans.ttf"); strcpy(GraphFont,FONTDIR"/DejaVuSans.ttf");
#else #else
GraphFont[0]='\0'; GraphFont[0]='\0';
#endif #endif
strcpy(Ulimit,"20000"); strcpy(Ulimit,"20000");
NtlmUserFormat=NTLMUSERFORMAT_DOMAINUSER; NtlmUserFormat=NTLMUSERFORMAT_DOMAINUSER;
IndexTree=INDEX_TREE_FILE; IndexTree=INDEX_TREE_FILE;
IndexFields=INDEXFIELDS_DIRSIZE; IndexFields=INDEXFIELDS_DIRSIZE;
strcpy(RealtimeTypes,"GET,PUT,CONNECT"); strcpy(RealtimeTypes,"GET,PUT,CONNECT,POST");
RealtimeUnauthRec=REALTIME_UNAUTH_REC_SHOW; RealtimeUnauthRec=REALTIME_UNAUTH_REC_SHOW;
RedirectorFilterOutDate=true; RedirectorFilterOutDate=true;
DansguardianFilterOutDate=true; DansguardianFilterOutDate=true;
DataFileUrl=DATAFILEURL_IP; DataFileUrl=DATAFILEURL_IP;
strcpy(MaxElapsed,"28800000"); strcpy(MaxElapsed,"28800000");
BytesInSitesUsersReport=0; BytesInSitesUsersReport=0;
UserAuthentication=0; UserAuthentication=0;
strcpy(LDAPHost,"127.0.0.1"); strcpy(LDAPHost,"127.0.0.1");
LDAPPort=389; LDAPPort=389;
LDAPProtocolVersion=3; LDAPProtocolVersion=3;
LDAPBindDN[0]='\0'; LDAPBindDN[0]='\0';
LDAPBindPW[0]='\0'; LDAPBindPW[0]='\0';
LDAPBaseSearch[0]='\0'; LDAPBaseSearch[0]='\0';
strcpy(LDAPFilterSearch, "(uid=%s)"); strcpy(LDAPFilterSearch, "(uid=%s)");
strcpy(LDAPTargetAttr, "cn"); strcpy(LDAPTargetAttr, "cn");
LDAPNativeCharset[0]='\0';
SortTableJs[0]='\0'; SortTableJs[0]='\0';
dia[0]='\0';
mes[0]='\0';
hora[0]='\0';
tmp[0]='\0'; tmp[0]='\0';
tmp3[0]='\0';
us[0]='\0'; us[0]='\0';
date[0]='\0'; ReadFilter.DateRange[0]='\0';
df[0]='\0'; df='\0';
uagent[0]='\0';
hexclude[0]='\0'; hexclude[0]='\0';
addr[0]='\0'; addr[0]='\0';
hm=-1; ReadFilter.StartTime=-1;
hmf=-1; ReadFilter.EndTime=-1;
site[0]='\0'; site[0]='\0';
outdir[0]='\0'; outdir[0]='\0';
splitprefix[0]='\0'; splitprefix[0]='\0';
elap[0]='\0';
email[0]='\0'; email[0]='\0';
UserInvalidChar[0]='\0'; UserInvalidChar[0]='\0';
DataFile[0]='\0'; DataFile[0]='\0';
SquidGuardConf[0]='\0'; SquidGuardConf[0]='\0';
DansGuardianConf[0]='\0'; DansGuardianConf[0]='\0';
start_hour[0]='\0';
hm_str[0]='\0'; hm_str[0]='\0';
HostAliasFile[0]='\0'; HostAliasFile[0]='\0';
UserAliasFile[0]='\0';
denied_count=0;
download_count=0;
authfail_count=0;
dansguardian_count=0; dansguardian_count=0;
redirector_count=0; redirector_count=0;
useragent_count=0; useragent_count=0;
DeniedReportLimit=10; DeniedReportLimit=10;
SiteUsersReportLimit=0;
AuthfailReportLimit=10; AuthfailReportLimit=10;
DansGuardianReportLimit=10; DansGuardianReportLimit=10;
SquidGuardReportLimit=10; SquidGuardReportLimit=10;
DownloadReportLimit=50; DownloadReportLimit=50;
UserReportLimit=0; UserReportLimit=0;
debug=0; debug=0;
debugz=0; debugz=0;
debugm=0;
iprel=false; iprel=false;
userip=false; userip=false;
realt=false; realt=false;
realtime_refresh=3; realtime_refresh=3;
realtime_access_log_lines=1000; realtime_access_log_lines=1000;
cost=0.01; cost=0.01;
nocost=50000000; nocost=50000000;
ndownload=0;
squid24=false; squid24=false;
dfrom=0; ReadFilter.StartDate=0;
duntil=0; ReadFilter.EndDate=0;
KeepTempLog=false; KeepTempLog=false;
NumLogSuccessiveErrors=3;
NumLogTotalErrors=50;
lines_read=0UL;
records_kept=0UL;
nusers=0UL;
bzero(IncludeUsers, sizeof(IncludeUsers)); memset(IncludeUsers,0,sizeof(IncludeUsers));
bzero(ExcludeString, sizeof(ExcludeString)); memset(ExcludeString,0,sizeof(ExcludeString));
first_user_file=NULL;
memset(&period,0,sizeof(period)); memset(&period,0,sizeof(period));
NAccessLog=0;
for(x=0; x<MAXLOGS; x++)
AccessLog[x][0]='\0';
AccessLogFromCmdLine=0; AccessLogFromCmdLine=0;
RedirectorLogFromCmdLine=0; RedirectorLogFromCmdLine=0;
strcpy(Title,_("Squid User Access Report")); strcpy(Title,_("Squid User Access Report"));
while((ch = getopt_long_only(argc, argv, "a:b:c:d:e:f:g:hikl:L:mno:P:prs: t:u:vw:xyz",long_options,&option_index)) != -1){ while((ch = getopt_long(argc, argv, "a:b:c:d:e:f:g:hikl:L:no:P:prs:t:u:Vw :xyz",long_options,&option_index)) != -1){
switch(ch) switch(ch)
{ {
case 0: case 0:
break; break;
case 2: case 2:
lastlog=atoi(optarg); lastlog=atoi(optarg);
break; break;
case 3: case 3:
lastlog=0; lastlog=0;
break; break;
case 'a': case 'a':
safe_strcpy(addr,optarg,sizeof(addr)); safe_strcpy(addr,optarg,sizeof(addr));
break; break;
case 'b': //unused option case 'b': //unused option
safe_strcpy(uagent,optarg,sizeof(uagent)); UserAgentFromCmdLine=true;
if (!UserAgentLog)
UserAgentLog=FileList_Create();
if (!FileList_AddFile(UserAgentLog,optarg)) {
debuga(__FILE__,__LINE__,_("Not enough me
mory to store a user agent file name\n"));
exit(EXIT_FAILURE);
}
break; break;
case 'c': case 'c':
safe_strcpy(hexclude,optarg,sizeof(hexclude)); safe_strcpy(hexclude,optarg,sizeof(hexclude));
break; break;
case 'd': case 'd':
safe_strcpy(date,optarg,sizeof(date)); safe_strcpy(ReadFilter.DateRange,optarg,sizeof(Re
date_from(date, sizeof(date), &dfrom, &duntil); adFilter.DateRange));
date_from(&ReadFilter);
break; break;
case 'e': case 'e':
safe_strcpy(email,optarg,sizeof(email)); safe_strcpy(email,optarg,sizeof(email));
break; break;
case 'f': case 'f':
safe_strcpy(ConfigFile,optarg,sizeof(ConfigFile)) ; safe_strcpy(ConfigFile,optarg,sizeof(ConfigFile)) ;
break; break;
case 'g': case 'g':
safe_strcpy(df,optarg,sizeof(df)); df=*optarg;
break; break;
case 'h': case 'h':
usage(argv[0]); usage(argv[0]);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
case 'i': case 'i':
iprel=true; iprel=true;
break; break;
case 'k': case 'k':
KeepTempLog=true; KeepTempLog=true;
break; break;
case 'l': case 'l':
if (NAccessLog>=MAXLOGS) { if (!AccessLog)
debuga(_("Too many log files passed on co AccessLog=FileList_Create();
mmand line with option %s.\n"),"-l"); if (!FileList_AddFile(AccessLog,optarg)) {
exit(EXIT_FAILURE); debuga(__FILE__,__LINE__,_("Not enough me
} mory to store the input log file names\n"));
if (strlen(optarg)>=MAX_LOG_FILELEN) {
debuga(_("Log file name too long passed o
n command line with option %s: %s\n"),"-l",optarg);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
strcpy(AccessLog[NAccessLog],optarg);
NAccessLog++;
AccessLogFromCmdLine++; AccessLogFromCmdLine++;
break; break;
case 'L': case 'L':
if (NRedirectorLogs>MAX_REDIRECTOR_LOGS) { if (NRedirectorLogs>MAX_REDIRECTOR_LOGS) {
debuga(_("Too many log files passed on co mmand line with option %s.\n"),"-L"); debuga(__FILE__,__LINE__,_("Too many redi rector logs passed on command line with option -L.\n"));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (strlen(optarg)>=MAX_REDIRECTOR_FILELEN) { if (strlen(optarg)>=MAX_REDIRECTOR_FILELEN) {
debuga(_("Log file name too long passed o n command line with option %s: %s\n"),"-L",optarg); debuga(__FILE__,__LINE__,_("Redirector lo g file name too long passed on command line with opton -L: %s\n"),optarg);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
strcpy(RedirectorLogs[NRedirectorLogs],optarg); strcpy(RedirectorLogs[NRedirectorLogs],optarg);
NRedirectorLogs++; NRedirectorLogs++;
RedirectorLogFromCmdLine++; RedirectorLogFromCmdLine++;
break; break;
case 'm':
debugm++;
break;
case 'n': case 'n':
dns=true; dns=true;
break; break;
case 'o': case 'o':
safe_strcpy(outdir,optarg,sizeof(outdir)); safe_strcpy(outdir,optarg,sizeof(outdir));
break; break;
case 'p': case 'p':
userip=true; userip=true;
break; break;
case 'P': case 'P':
skipping to change at line 478 skipping to change at line 380
case 'r': case 'r':
realt=true; realt=true;
break; break;
case 's': case 's':
safe_strcpy(site,optarg,sizeof(site)); safe_strcpy(site,optarg,sizeof(site));
break; break;
case 't': case 't':
{ {
int h1,m1,h2,m2; int h1,m1,h2,m2;
if(strstr(optarg,"-") == 0) { if (strstr(optarg,"-") == 0) {
if(sscanf(optarg,"%d:%d",&h1,&m1)!=2) { if (sscanf(optarg,"%d:%d",&h1,&m1)!=2) {
debuga(_("Time period passed on t debuga(__FILE__,__LINE__,_("Time
he command line with option -t must be HH:MM\n")); period passed on the command line with option -t must be HH:MM\n"));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
hm=h1*100+m1; ReadFilter.StartTime=h1*100+m1;
hmf=hm; ReadFilter.EndTime=ReadFilter.StartTime+1
;
snprintf(hm_str,sizeof(hm_str),"%02d:%02d ",h1,m1); snprintf(hm_str,sizeof(hm_str),"%02d:%02d ",h1,m1);
} else { } else {
if(sscanf(optarg,"%d:%d-%d:%d",&h1,&m1,&h if (sscanf(optarg,"%d:%d-%d:%d",&h1,&m1,&
2,&m2)!=4) { h2,&m2)!=4) {
debuga(_("Time range passed on th debuga(__FILE__,__LINE__,_("Time
e command line with option -t must be HH:MM-HH:MM\n")); range passed on the command line with option -t must be HH:MM-HH:MM\n"));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
hm=h1*100+m1; ReadFilter.StartTime=h1*100+m1;
hmf=h2*100+m2; ReadFilter.EndTime=h2*100+m2;
snprintf(hm_str,sizeof(hm_str),"%02d:%02d -%02d:%02d",h1,m1,h2,m2); snprintf(hm_str,sizeof(hm_str),"%02d:%02d -%02d:%02d",h1,m1,h2,m2);
} }
break; break;
} }
case 'u': case 'u':
safe_strcpy(us,optarg,sizeof(us)); safe_strcpy(us,optarg,sizeof(us));
break; break;
case 'v': case 'V':
version(); show_version=1;
break; break;
case 'w': case 'w':
safe_strcpy(tmp,optarg,sizeof(tmp)); safe_strcpy(tmp,optarg,sizeof(tmp));
break; break;
case 'x': case 'x':
debug++; debug++;
break; break;
case 'y': //unused option case 'y': //unused option
langcode++; langcode++;
break; break;
case 'z': case 'z':
debugz++; debugz++;
break; break;
case ':': case ':':
debuga(_("Option -%c requires an argument\n"),opt opt); debuga(__FILE__,__LINE__,_("Option -%c requires a n argument\n"),optopt);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
case '?': case '?':
usage(argv[0]); usage(argv[0]);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
default: default:
abort(); abort();
} }
} }
if (errflg>0) { if (errflg>0) {
usage(argv[0]); usage(argv[0]);
exit(2); exit(2);
} }
if (show_version) {
version();
}
if (output_css) {
css_content(stdout);
exit(EXIT_SUCCESS);
}
if (optind<argc) { if (optind<argc) {
if (!AccessLog)
AccessLog=FileList_Create();
for (iarq=optind ; iarq<argc ; iarq++) { for (iarq=optind ; iarq<argc ; iarq++) {
if (NAccessLog>=MAXLOGS) { if (!FileList_AddFile(AccessLog,argv[iarq])) {
debuga(_("Too many log files passed on command li debuga(__FILE__,__LINE__,_("Not enough memory to
ne.\n")); store the input log file names\n"));
exit(EXIT_FAILURE);
}
if (strlen(argv[iarq])>=MAX_LOG_FILELEN) {
debuga(_("Log file name too long passed on comman
d line: %s\n"),argv[iarq]);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
strcpy(AccessLog[NAccessLog],argv[iarq]);
NAccessLog++;
AccessLogFromCmdLine++; AccessLogFromCmdLine++;
} }
} }
if(debug) debuga(_("Init\n")); if (debug) debuga(__FILE__,__LINE__,_("Init\n"));
if(ConfigFile[0] == '\0') snprintf(ConfigFile,sizeof(ConfigFile),"%s/sarg if (ConfigFile[0] == '\0') snprintf(ConfigFile,sizeof(ConfigFile),"%s/sar
.conf",SYSCONFDIR); g.conf",SYSCONFDIR);
if(access(ConfigFile, R_OK) != 0) { if (access(ConfigFile, R_OK) != 0) {
debuga(_("Cannot open file \"%s\": %s\n"),ConfigFile,strerror(err debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),Confi
no)); gFile,strerror(errno));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if(access(ConfigFile, R_OK) == 0) if (access(ConfigFile, R_OK) == 0)
getconf(); getconf(ConfigFile);
if(userip) UserIp=true; if (userip) UserIp=true;
if(dns) ip2name_forcedns(); if (dns) ip2name_forcedns();
if (lastlog>=0) LastLog=lastlog; if (lastlog>=0) LastLog=lastlog;
if(outdir[0] == '\0') strcpy(outdir,OutputDir); if (outdir[0] == '\0') strcpy(outdir,OutputDir);
if(outdir[0] != '\0') strcat(outdir,"/"); if (outdir[0] != '\0') strcat(outdir,"/");
if(realt) {
realtime();
exit(EXIT_SUCCESS);
}
if(IndexTree == INDEX_TREE_FILE) if (IndexTree == INDEX_TREE_FILE)
strcpy(ImageFile,"../images"); strcpy(ImageFile,"../images");
else else
strcpy(ImageFile,"../../../images"); strcpy(ImageFile,"../../../images");
dataonly=0; dataonly=(DataFile[0] != '\0');
if(DataFile[0] != '\0')
dataonly++; if (df=='\0') df=DateFormat;
if (df=='\0') df='u';
if(df[0] == '\0') strcpy(df,DateFormat); if (df=='w')
else strcpy(DateFormat,df);
if(df[0] == '\0') {
strcpy(df,"u");
strcpy(DateFormat,"u");
}
if (df[0]=='w')
IndexTree=INDEX_TREE_FILE; IndexTree=INDEX_TREE_FILE;
if(NAccessLog == 0) { if (AccessLog==NULL) {
strcpy(AccessLog[0],"/var/log/squid/access.log"); AccessLog=FileList_Create();
NAccessLog++; if (!FileList_AddFile(AccessLog,"/var/log/squid/access.log")) {
debuga(__FILE__,__LINE__,_("Not enough memory to store th
e input log file names\n"));
exit(EXIT_FAILURE);
}
} }
if(output_css) { if (realt) {
css_content(stdout); realtime();
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
if(split) { if (split) {
for (iarq=0 ; iarq<NAccessLog ; iarq++) const char *file;
splitlog(AccessLog[iarq], df, dfrom, duntil, convert, spl
itprefix); FIter=FileListIter_Open(AccessLog);
while ((file=FileListIter_Next(FIter))!=NULL)
splitlog(file, df, &ReadFilter, convert, splitprefix);
FileListIter_Close(FIter);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
if(convert) { if (convert) {
for (iarq=0 ; iarq<NAccessLog ; iarq++) const char *file;
convlog(AccessLog[iarq], df, dfrom, duntil);
FIter=FileListIter_Open(AccessLog);
while ((file=FileListIter_Next(FIter))!=NULL)
convlog(file, df, &ReadFilter);
FileListIter_Close(FIter);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
load_excludecodes(ExcludeCodes); load_excludecodes(ExcludeCodes);
if(access(PasswdFile, R_OK) == 0) { if (access(PasswdFile, R_OK) == 0) {
getusers(PasswdFile,debug); getusers(PasswdFile,debug);
puser++; ReadFilter.SysUsers=true;
} else {
ReadFilter.SysUsers=false;
} }
if(hexclude[0] == '\0') if (hexclude[0] == '\0')
strcpy(hexclude,ExcludeHosts); strcpy(hexclude,ExcludeHosts);
if(hexclude[0] != '\0') { if (hexclude[0] != '\0') {
gethexclude(hexclude,debug); gethexclude(hexclude,debug);
fhost=true; ReadFilter.HostFilter=true;
} else {
ReadFilter.HostFilter=false;
} }
if(ReportType == 0) { if (ReportType == 0) {
ReportType=REPORT_TYPE_TOPUSERS | REPORT_TYPE_TOPSITES | REPORT_T YPE_USERS_SITES | ReportType=REPORT_TYPE_TOPUSERS | REPORT_TYPE_TOPSITES | REPORT_T YPE_USERS_SITES |
REPORT_TYPE_SITES_USERS | REPORT_TYPE_DATE_TIME | REPO RT_TYPE_DENIED | REPORT_TYPE_SITES_USERS | REPORT_TYPE_DATE_TIME | REPO RT_TYPE_DENIED |
REPORT_TYPE_AUTH_FAILURES | REPORT_TYPE_SITE_USER_TIME REPORT_TYPE_AUTH_FAILURES | REPORT_TYPE_SITE_U
_DATE | REPORT_TYPE_DOWNLOADS; SER_TIME_DATE |
REPORT_TYPE_DOWNLOADS | REPORT_TYPE_USERAGENT;
} }
if (!FileList_IsEmpty(UserAgentLog))
ReportType|=REPORT_TYPE_USERAGENT;
if(access(ExcludeUsers, R_OK) == 0) { if (access(ExcludeUsers, R_OK) == 0) {
getuexclude(ExcludeUsers,debug); getuexclude(ExcludeUsers,debug);
fuser=true; ReadFilter.UserFilter=true;
} else {
ReadFilter.UserFilter=false;
} }
if (HostAliasFile[0] != '\0') if (HostAliasFile[0] != '\0')
read_hostalias(HostAliasFile); read_hostalias(HostAliasFile);
if (UserAliasFile[0] != '\0')
read_useralias(UserAliasFile);
indexonly=false; indexonly=false;
if(fuser) { if (ReadFilter.UserFilter) {
if(is_indexonly()) if (is_indexonly())
indexonly=true; indexonly=true;
} }
if(strcmp(ExcludeUsers,"indexonly") == 0) indexonly=true; if (strcmp(ExcludeUsers,"indexonly") == 0) indexonly=true;
if(Index == INDEX_ONLY) indexonly=true; if (Index == INDEX_ONLY) indexonly=true;
if(MaxElapsed[0] != '\0') max_elapsed=atol(MaxElapsed);
if(uagent[0] == '\0') strcpy(uagent,UserAgentLog); if (MaxElapsed[0] != '\0')
ReadFilter.max_elapsed=atol(MaxElapsed);
else
ReadFilter.max_elapsed=0;
if(tmp[0] == '\0') strcpy(tmp,TempDir); if (tmp[0] == '\0') strcpy(tmp,TempDir);
else strcpy(TempDir,tmp); else strcpy(TempDir,tmp);
/* /*
For historical reasons, the temporary directory is the subdirectory "sarg For historical reasons, the temporary directory used to be subdirectory "
" of the path sarg" of the path
provided by the user. provided by the user. The full temporary directory was the predictable na
me /tmp/sarg. It is unsafe
to use a predictable name in the world writable /tmp as malicious users m
ight use that knowledge
to lure sarg into some kind of nasty activity it was not designed for.
The default is now to use a random name safely created by the system but
it is still possible to
use a known fixed path set with a parameter in sarg.conf.
*/ */
strcat(tmp,"/sarg"); if (TempDirPath[0]) {
append_to_path(tmp, sizeof(tmp), TempDirPath);
} else {
append_to_path(tmp, sizeof(tmp), "sargXXXXXX");
if (mkdtemp(tmp) == NULL) {
debuga(__FILE__,__LINE__,_("Failed to get a unique tempor
ary directory name based on template \"%s\": %s\n"), tmp, strerror(errno));
exit(EXIT_FAILURE);
}
}
if (tmp[0]!='\0' && strncmp(outdir,tmp,strlen(tmp))==0) { if (tmp[0]!='\0' && strncmp(outdir,tmp,strlen(tmp))==0) {
debuga(_("The output directory \"%s\" must be outside of the temp orary directory \"%s\"\n"),outdir,tmp); debuga(__FILE__,__LINE__,_("The output directory \"%s\" must be o utside of the temporary directory \"%s\"\n"),outdir,tmp);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
atexit(CleanTemporaryDir);
if(email[0] == '\0' && OutputEmail[0] != '\0') strcpy(email,OutputEmail); if (email[0] == '\0' && OutputEmail[0] != '\0') strcpy(email,OutputEmail) ;
if(email[0] != '\0') { if (email[0] != '\0') {
my_mkdir(tmp); my_mkdir(tmp);
strcpy(outdir,tmp); strcpy(outdir,tmp);
strcat(outdir,"/"); strcat(outdir,"/");
} }
if(access(tmp, R_OK) == 0) { makeTmpDir(tmp);
if (debug) debuga(_("Deleting temporary directory \"%s\"\n"),tmp)
; if (debug) {
emptytmpdir(tmp); const char *file;
}
my_mkdir(tmp); debuga(__FILE__,__LINE__,_("Parameters:\n"));
snprintf(denied_unsort,sizeof(denied_unsort),"%s/denied.int_unsort",tmp); debuga(__FILE__,__LINE__,_(" Hostname or IP address (-a)
snprintf(denied_sort,sizeof(denied_sort),"%s/denied.int_log",tmp); = %s\n"),addr);
snprintf(authfail_unsort,sizeof(authfail_unsort),"%s/authfail.int_unsort" FIter=FileListIter_Open(UserAgentLog);
,tmp); while ((file=FileListIter_NextWithMask(FIter))!=NULL)
debuga(__FILE__,__LINE__,_(" Useragent
if(debug) { log (-b) = %s\n"),file);
debuga(_("Parameters:\n")); FileListIter_Close(FIter);
debuga(_(" Hostname or IP address (-a) = %s\n"),addr); debuga(__FILE__,__LINE__,_(" Exclude file (-c)
debuga(_(" Useragent log (-b) = %s\n"),uagent); = %s\n"),hexclude);
debuga(_(" Exclude file (-c) = %s\n"),hexclude debuga(__FILE__,__LINE__,_(" Date from-until (-d)
); = %s\n"),ReadFilter.DateRange);
debuga(_(" Date from-until (-d) = %s\n"),date); debuga(__FILE__,__LINE__,_(" Email address to send reports (-e)
debuga(_(" Email address to send reports (-e) = %s\n"),email); = %s\n"),email);
debuga(_(" Config file (-f) = %s\n"),ConfigFi debuga(__FILE__,__LINE__,_(" Config file (-f)
le); = %s\n"),ConfigFile);
if(strcmp(df,"e") == 0) if (df=='e')
debuga(_(" Date format (-g) = Europe debuga(__FILE__,__LINE__,_(" Date for
(dd/mm/yyyy)\n")); mat (-g) = Europe (dd/mm/yyyy)\n"));
if(strcmp(df,"u") == 0) else if (df=='u')
debuga(_(" Date format (-g) = USA (mm debuga(__FILE__,__LINE__,_(" Date for
/dd/yyyy)\n")); mat (-g) = USA (mm/dd/yyyy)\n"));
if(strcmp(df,"w") == 0) else if (df=='w')
debuga(_(" Date format (-g) = Sites & debuga(__FILE__,__LINE__,_(" Date for
Users (yyyy/ww)\n")); mat (-g) = Sites & Users (yyyy/ww)\n"));
debuga(_(" IP report (-i) = %s\n"),(iprel) debuga(__FILE__,__LINE__,_(" IP report (-i)
? _("Yes") : _("No")); = %s\n"),(iprel) ? _("Yes") : _("No"));
debuga(_(" Keep temporary files (-k) = %s\n"),(KeepTem debuga(__FILE__,__LINE__,_(" Keep temporary files (-k)
pLog) ? _("Yes") : _("No")); = %s\n"),(KeepTempLog) ? _("Yes") : _("No"));
for (iarq=0 ; iarq<NAccessLog ; iarq++) FIter=FileListIter_Open(AccessLog);
debuga(_(" Input log (-l) = %s\n"), while ((file=FileListIter_NextWithMask(FIter))!=NULL)
AccessLog[iarq]); debuga(__FILE__,__LINE__,_(" Input
for (iarq=0 ; iarq<NRedirectorLogs ; iarq++) log (-l) = %s\n"),file);
debuga(_(" Redirector log (-L) = %s\n"), FileListIter_Close(FIter);
RedirectorLogs[iarq]);
debuga(_(" Resolve IP Address (-n) = %s\n"),(Ip2Name
) ? _("Yes") : _("No"));
debuga(_(" Output dir (-o) = %s\n"),outdir);
debuga(_("Use Ip Address instead of userid (-p) = %s\n"),(UserIp)
? _("Yes") : _("No"));
debuga(_(" Accessed site (-s) = %s\n"),site);
debuga(_(" Time (-t) = %s\n"),hm_str);
debuga(_(" User (-u) = %s\n"),us);
debuga(_(" Temporary dir (-w) = %s\n"),tmp);
debuga(_(" Debug messages (-x) = %s\n"),(debug)
? _("Yes") : _("No"));
debuga(_(" Process messages (-z) = %s\n"),(debugz)
? _("Yes") : _("No"));
debuga(_(" Previous reports to keep (--lastlog) = %d\n"),LastLog)
;
debuga("\n");
}
if(debugm) {
printf(_("Parameters:\n"));
printf(_(" Hostname or IP address (-a) = %s\n"),addr);
printf(_(" Useragent log (-b) = %s\n"),uagent);
printf(_(" Exclude file (-c) = %s\n"),hexclude
);
printf(_(" Date from-until (-d) = %s\n"),date);
printf(_(" Email address to send reports (-e) = %s\n"),email);
printf(_(" Config file (-f) = %s\n"),ConfigFi
le);
if(strcmp(df,"e") == 0)
printf(_(" Date format (-g) = Europe
(dd/mm/yyyy)\n"));
if(strcmp(df,"u") == 0)
printf(_(" Date format (-g) = USA (mm
/dd/yyyy)\n"));
if(strcmp(df,"w") == 0)
printf(_(" Date format (-g) = Sites &
Users (yyyy/ww)\n"));
printf(_(" IP report (-i) = %s\n"),(iprel)
? _("Yes") : _("No"));
printf(_(" Keep temporary files (-k) = %s\n"),(KeepTem
pLog) ? _("Yes") : _("No"));
for (iarq=0 ; iarq<NAccessLog ; iarq++)
printf(_(" Input log (-l) = %s\n"),
AccessLog[iarq]);
for (iarq=0 ; iarq<NRedirectorLogs ; iarq++) for (iarq=0 ; iarq<NRedirectorLogs ; iarq++)
printf(_(" Redirector log (-L) = %s\n"), debuga(__FILE__,__LINE__,_(" Redirector
RedirectorLogs[iarq]); log (-L) = %s\n"),RedirectorLogs[iarq]);
printf(_(" Resolve IP Address (-n) = %s\n"),(Ip2Name debuga(__FILE__,__LINE__,_(" Resolve IP Address (-n)
) ? _("Yes") : _("No")); = %s\n"),(Ip2Name) ? _("Yes") : _("No"));
printf(_(" Output dir (-o) = %s\n"),outdir); debuga(__FILE__,__LINE__,_(" Output dir (-o)
printf(_("Use Ip Address instead of userid (-p) = %s\n"),(UserIp) = %s\n"),outdir);
? _("Yes") : _("No")); debuga(__FILE__,__LINE__,_("Use Ip Address instead of userid (-p)
printf(_(" Accessed site (-s) = %s\n"),site); = %s\n"),(UserIp) ? _("Yes") : _("No"));
printf(_(" Time (-t) = %s\n"),hm_str); debuga(__FILE__,__LINE__,_(" Accessed site (-s)
printf(_(" User (-u) = %s\n"),us); = %s\n"),site);
printf(_(" Temporary dir (-w) = %s\n"),tmp); debuga(__FILE__,__LINE__,_(" Time (-t)
printf(_(" Debug messages (-x) = %s\n"),(debug) = %s\n"),hm_str);
? _("Yes") : _("No")); debuga(__FILE__,__LINE__,_(" User (-u)
printf(_(" Process messages (-z) = %s\n"),(debugz) = %s\n"),us);
? _("Yes") : _("No")); debuga(__FILE__,__LINE__,_(" Temporary dir (-w)
printf(_(" Previous reports to keep (--lastlog) = %d\n"),LastLog) = %s\n"),tmp);
; debuga(__FILE__,__LINE__,_(" Debug messages (-x)
printf(_("SARG version: %s\n"),VERSION); = %s\n"),(debug) ? _("Yes") : _("No"));
debuga(__FILE__,__LINE__,_(" Process messages (-z)
= %d\n"),debugz);
debuga(__FILE__,__LINE__,_(" Previous reports to keep (--lastlog)
= %d\n"),LastLog);
debuga(__FILE__,__LINE__,"\n");
} }
if(debug) if (debug)
debuga(_("SARG version: %s\n"),VERSION); debuga(__FILE__,__LINE__,_("sarg version: %s\n"),VERSION);
#ifdef ENABLE_DOUBLE_CHECK_DATA #ifdef ENABLE_DOUBLE_CHECK_DATA
debuga(_("Sarg compiled to report warnings if the output is inconsistent\ n")); debuga(__FILE__,__LINE__,_("Sarg compiled to report warnings if the outpu t is inconsistent\n"));
#endif #endif
maxopenfiles=MAX_OPEN_USER_FILES;
#ifdef HAVE_RLIM_T #ifdef HAVE_RLIM_T
if (Ulimit[0] != '\0') { if (Ulimit[0] != '\0') {
struct rlimit rl; struct rlimit rl;
long l1, l2; long l1, l2;
int rc=0; int rc=0;
#if defined(RLIMIT_NOFILE) #if defined(RLIMIT_NOFILE)
getrlimit (RLIMIT_NOFILE, &rl); getrlimit (RLIMIT_NOFILE, &rl);
#elif defined(RLIMIT_OFILE) #elif defined(RLIMIT_OFILE)
getrlimit (RLIMIT_OFILE, &rl); getrlimit (RLIMIT_OFILE, &rl);
skipping to change at line 779 skipping to change at line 675
rl.rlim_cur = atol(Ulimit); rl.rlim_cur = atol(Ulimit);
rl.rlim_max = atol(Ulimit); rl.rlim_max = atol(Ulimit);
#if defined(RLIMIT_NOFILE) #if defined(RLIMIT_NOFILE)
rc=setrlimit (RLIMIT_NOFILE, &rl); rc=setrlimit (RLIMIT_NOFILE, &rl);
#elif defined(RLIMIT_OFILE) #elif defined(RLIMIT_OFILE)
rc=setrlimit (RLIMIT_OFILE, &rl); rc=setrlimit (RLIMIT_OFILE, &rl);
#else #else
#warning "No rlimit resource for the number of open files" #warning "No rlimit resource for the number of open files"
#endif #endif
if(rc == -1) { if (rc == -1) {
debuga(_("setrlimit error: %s\n"),strerror(errno)); debuga(_("setrlimit error: %s\n"),strerror(errno));
} }
if(debug) if (debug)
debuga("Maximum file descriptor: cur=%ld max=%ld, changed to cur="RLIM_STRING" max="RLIM_STRING"\n",l1,l2,rl.rlim_cur,rl.rlim_max); debuga("Maximum file descriptor: cur=%ld max=%ld, changed to cur="RLIM_STRING" max="RLIM_STRING"\n",l1,l2,rl.rlim_cur,rl.rlim_max);
} }
#endif #endif
init_usertab(UserTabFile);
if ((line=longline_create())==NULL) {
debuga(_("Not enough memory to read a log file\n"));
exit(EXIT_FAILURE);
}
snprintf(sz_Download_Unsort,sizeof(sz_Download_Unsort),"%s/download.int_u
nsort", tmp);
if(DataFile[0]=='\0') {
if((ReportType & REPORT_TYPE_DENIED) != 0) {
if((fp_denied=MY_FOPEN(denied_unsort,"w"))==NULL) {
debugapos("log",_("Cannot open file \"%s\": %s\n"
),denied_unsort,strerror(errno));
exit(EXIT_FAILURE);
}
}
if((ReportType & REPORT_TYPE_DENIED) != 0 || (ReportType & REPORT
_TYPE_AUTH_FAILURES) != 0) {
if((fp_authfail=MY_FOPEN(authfail_unsort,"w"))==NULL) {
debugapos("log",_("Cannot open file \"%s\": %s\n"
),authfail_unsort,strerror(errno));
exit(EXIT_FAILURE);
}
}
}
read_start_time=time(NULL); read_start_time=time(NULL);
for (iarq=0 ; iarq<NAccessLog ; iarq++) { LogStatus=ReadLogFile(&ReadFilter);
strcpy(arq,AccessLog[iarq]);
strcpy(arqtt,arq);
if(strcmp(arq,"-")==0) {
if(debug)
debuga(_("Reading access log file: from stdin\n")
);
fp_in=stdin;
from_stdin=true;
} else {
if (date[0]!='\0') {
if (stat(arq,&logstat)!=0) {
debuga(_("Cannot get the modification tim
e of input log file \"%s\": %s\nProcessing it anyway\n"),arq,strerror(errno));
} else {
struct tm *logtime=localtime(&logstat.st_
mtime);
if ((logtime->tm_year+1900)*10000+(logtim
e->tm_mon+1)*100+logtime->tm_mday<dfrom) {
debuga(_("Ignoring old log file %
s\n"),arq);
continue;
}
}
}
fp_in=decomp(arq,&from_pipe);
if(fp_in==NULL) {
debugapos("log",_("Cannot open file \"%s\": %s\n"
),arq,strerror(errno));
exit(EXIT_FAILURE);
}
if(debug) debuga(_("Reading access log file: %s\n"),arq);
from_stdin=false;
}
ilf=ILF_Unknown;
download_flag=false;
// pre-read the file only if we have to show stats
if(ShowReadStatistics && !from_stdin && !from_pipe) {
size_t nread,i;
bool skipcr=false;
char tmp4[MAXLEN];
recs1=0UL;
recs2=0UL;
while ((nread=fread(tmp4,1,sizeof(tmp4),fp_in))>0) {
for (i=0 ; i<nread ; i++)
if (skipcr) {
if (tmp4[i]!='\n' && tmp4[i]!='\r
') {
skipcr=false;
}
} else {
if (tmp4[i]=='\n' || tmp4[i]=='\r
') {
skipcr=true;
recs1++;
}
}
}
rewind(fp_in);
printf(_("SARG: Records in file: %lu, reading: %3.2lf%%")
,recs1,0.);
putchar('\r');
fflush( stdout ) ;
}
longline_reset(line);
while ((linebuf=longline_read(fp_in,line))!=NULL) {
blen=strlen(linebuf);
lines_read++;
if (ilf==ILF_Unknown) {
if(strncmp(linebuf,"#Software: Mic",14) == 0) {
fixendofline(linebuf);
if (debug)
debuga(_("Log is from Microsoft I
SA: %s\n"),linebuf);
ilf=ILF_Isa;
ilf_count[ilf]++;
continue;
}
if(strncmp(linebuf,"*** SARG Log ***",16) == 0) {
if (getperiod_fromsarglog(arqtt,&period)<
0) {
debuga(_("The name of the file is
invalid: %s\n"),arq);
exit(EXIT_FAILURE);
}
ilf=ILF_Sarg;
ilf_count[ilf]++;
continue;
}
}
if(!fp_log && ParsedOutputLog[0] && ilf!=ILF_Sarg) {
if(access(ParsedOutputLog,R_OK) != 0) {
my_mkdir(ParsedOutputLog);
}
if (snprintf(arq_log,sizeof(arq_log),"%s/sarg_tem
p.log",ParsedOutputLog)>=sizeof(arq_log)) {
debuga(_("Path too long: "));
debuga_more("%s/sarg_temp.log\n",ParsedOu
tputLog);
exit(EXIT_FAILURE);
}
if((fp_log=MY_FOPEN(arq_log,"w"))==NULL) {
debugapos("log",_("Cannot open file \"%s\
": %s\n"),arq_log,strerror(errno));
exit(EXIT_FAILURE);
}
fputs("*** SARG Log ***\n",fp_log);
}
recs2++;
if( ShowReadStatistics && !from_stdin && !from_pipe && --
OutputNonZero<=0) {
double perc = recs2 * 100. / recs1 ;
printf(_("SARG: Records in file: %lu, reading: %3
.2lf%%"),recs2,perc);
putchar('\r');
fflush (stdout);
OutputNonZero = REPORT_EVERY_X_LINES ;
}
if(blen < 58) continue;
if(strstr(linebuf,"HTTP/0.0") != 0) continue;
if(strstr(linebuf,"logfile turned over") != 0) continue;
if(linebuf[0] == ' ') continue;
// exclude_string
if(ExcludeString[0] != '\0') {
bool exstring=false;
getword_start(&gwarea,ExcludeString);
while(strchr(gwarea.current,':') != 0) {
if (getword_multisep(val1,sizeof(val1),&g
warea,':')<0) {
debuga(_("Invalid record in exclu
sion string\n"));
exit(EXIT_FAILURE);
}
if((str=(char *) strstr(linebuf,val1)) !=
(char *) NULL ) {
exstring=true;
break;
}
}
if(!exstring && (str=(char *) strstr(linebuf,gwar
ea.current)) != (char *) NULL )
exstring=true;
if(exstring) continue;
}
totregsl++;
if(debugm)
printf("BUF=%s\n",linebuf);
t=NULL;
if (ilf==ILF_Squid || ilf==ILF_Common || ilf==ILF_Unknown
) {
getword_start(&gwarea,linebuf);
if (getword(data,sizeof(data),&gwarea,' ')<0) {
debuga(_("Invalid time in file \"%s\"\n")
,arq);
exit(EXIT_FAILURE);
}
if((str=(char *) strchr(data, '.')) != (char *) N
ULL && (str=(char *) strchr(str+1, '.')) != (char *) NULL ) {
strcpy(ip,data);
strcpy(elap,"0");
if(squid24) {
if (getword(user,sizeof(user),&gw
area,' ')<0 || getword_skip(255,&gwarea,' ')<0) {
debuga(_("Invalid record
in file \"%s\"\n"),arq);
exit(EXIT_FAILURE);
}
} else {
if (getword_skip(255,&gwarea,' ')
<0 || getword(user,sizeof(user),&gwarea,' ')<0) {
debuga(_("Invalid record
in file \"%s\"\n"),arq);
exit(EXIT_FAILURE);
}
}
if (getword(data,sizeof(data),&gwarea,']'
)<0 || getword_skip(MAXLEN,&gwarea,'"')<0 ||
getword(fun,sizeof(fun),&gwarea,' ')<
0) {
debuga(_("Invalid record in file
\"%s\"\n"),arq);
exit(EXIT_FAILURE);
}
if (getword_ptr(linebuf,&full_url,&gwarea
,' ')<0) {
debuga(_("Invalid url in file \"%
s\"\n"),arq);
exit(EXIT_FAILURE);
}
if (getword_skip(MAXLEN,&gwarea,' ')<0) {
debuga(_("Invalid record in file
\"%s\"\n"),arq);
exit(EXIT_FAILURE);
}
if (getword(code2,sizeof(code2),&gwarea,'
')<0) {
debuga(_("Invalid record in file
\"%s\"\n"),arq);
exit(EXIT_FAILURE);
}
if (getword(tam,sizeof(tam),&gwarea,' ')<
0) {
debuga(_("Invalid record in file
\"%s\"\n"),arq);
exit(EXIT_FAILURE);
}
if((str=(char *) strchr(gwarea.current, '
')) != (char *) NULL ) {
if (getword(code,sizeof(code),&gw
area,' ')<0) {
debuga(_("Invalid record
in file \"%s\"\n"),arq);
exit(EXIT_FAILURE);
}
} else {
if (getword(code,sizeof(code),&gw
area,'\0')<0) {
debuga(_("Invalid record
in file \"%s\"\n"),arq);
exit(EXIT_FAILURE);
}
}
if ((str = strchr(code, ':')) != NULL)
*str = '/';
if(strcmp(tam,"\0") == 0)
strcpy(tam,"0");
ilf=ILF_Common;
ilf_count[ilf]++;
getword_start(&gwarea,data+1);
if (getword_multisep(data,sizeof(data),&g
warea,':')<0){
debuga(_("Invalid date in file \"
%s\"\n"),arq);
exit(EXIT_FAILURE);
}
if (getword_multisep(hora,sizeof(hora),&g
warea,' ')<0){
debuga(_("Invalid date in file \"
%s\"\n"),arq);
exit(EXIT_FAILURE);
}
getword_start(&gwarea,data);
if (getword_atoll(&iday,&gwarea,'/')<0){
debuga(_("Invalid date in file \"
%s\"\n"),arq);
exit(EXIT_FAILURE);
}
if (getword(mes,sizeof(mes),&gwarea,'/')<
0){
debuga(_("Invalid date in file \"
%s\"\n"),arq);
exit(EXIT_FAILURE);
}
if (getword_atoll(&iyear,&gwarea,'/')<0){
debuga(_("Invalid date in file \"
%s\"\n"),arq);
exit(EXIT_FAILURE);
}
imonth=month2num(mes)+1;
idata=builddia(iday,imonth,iyear);
computedate(iyear,imonth,iday,&tt);
if (sscanf(hora,"%d:%d:%d",&tt.tm_hour,&t
t.tm_min,&tt.tm_sec)!=3 || tt.tm_hour<0 || tt.tm_hour>=24 ||
tt.tm_min<0 || tt.tm_min>
=60 || tt.tm_sec<0 || tt.tm_sec>=60) {
debuga(_("Invalid time in file \"
%s\"\n"),arq);
exit(EXIT_FAILURE);
}
t=&tt;
}
if(ilf==ILF_Unknown || ilf==ILF_Squid) {
if (getword(elap,sizeof(elap),&gwarea,' '
)<0) {
debuga(_("Invalid elapsed time in
file \"%s\"\n"),arq);
exit(EXIT_FAILURE);
}
while(strcmp(elap,"") == 0 && gwarea.curr
ent[0] != '\0')
if (getword(elap,sizeof(elap),&gw
area,' ')<0) {
debuga(_("Invalid elapsed
time in file \"%s\"\n"),arq);
exit(EXIT_FAILURE);
}
if(strlen(elap) < 1) continue;
if (getword(ip,sizeof(ip),&gwarea,' ')<0)
{
debuga(_("Invalid client IP addre
ss in file \"%s\"\n"),arq);
exit(EXIT_FAILURE);
}
if (getword(code,sizeof(code),&gwarea,' '
)<0){
debuga(_("Invalid result code in
file \"%s\"\n"),arq);
exit(EXIT_FAILURE);
}
if (getword(tam,sizeof(tam),&gwarea,' ')<
0){
debuga(_("Invalid amount of data
in file \"%s\"\n"),arq);
exit(EXIT_FAILURE);
}
if (getword(fun,sizeof(fun),&gwarea,' ')<
0){
debuga(_("Invalid request method
in file \"%s\"\n"),arq);
exit(EXIT_FAILURE);
}
if (getword_ptr(linebuf,&full_url,&gwarea
,' ')<0){
debuga(_("Invalid url in file \"%
s\"\n"),arq);
exit(EXIT_FAILURE);
}
if (getword(user,sizeof(user),&gwarea,' '
)<0){
debuga(_("Invalid user ID in file
\"%s\"\n"),arq);
exit(EXIT_FAILURE);
}
ilf=ILF_Squid;
ilf_count[ilf]++;
tnum=atoi(data);
t=localtime(&tnum);
if (t == NULL) {
debuga(_("Cannot convert the time
stamp from the squid log file\n"));
exit(EXIT_FAILURE);
}
strftime(tbuf2, sizeof(tbuf2), "%H%M", t)
;
idata=(t->tm_year+1900)*10000+(t->tm_mon+
1)*100+t->tm_mday;
}
}
if (ilf==ILF_Sarg) {
getword_start(&gwarea,linebuf);
if (getword(data,sizeof(data),&gwarea,'\t')<0){
debuga(_("Invalid record in file \"%s\"\n
"),arq);
exit(EXIT_FAILURE);
}
if (getword(hora,sizeof(hora),&gwarea,'\t')<0) {
debuga(_("Invalid record in file \"%s\"\n
"),arq);
exit(EXIT_FAILURE);
}
if (getword(user,sizeof(user),&gwarea,'\t')<0) {
debuga(_("Invalid record in file \"%s\"\n
"),arq);
exit(EXIT_FAILURE);
}
if (getword(ip,sizeof(ip),&gwarea,'\t')<0) {
debuga(_("Invalid record in file \"%s\"\n
"),arq);
exit(EXIT_FAILURE);
}
if (getword_ptr(linebuf,&full_url,&gwarea,'\t')<0
){
debuga(_("Invalid record in file \"%s\"\n
"),arq);
exit(EXIT_FAILURE);
}
if (getword(tam,sizeof(tam),&gwarea,'\t')<0){
debuga(_("Invalid record in file \"%s\"\n
"),arq);
exit(EXIT_FAILURE);
}
if (getword(code,sizeof(code),&gwarea,'\t')<0){
debuga(_("Invalid record in file \"%s\"\n
"),arq);
exit(EXIT_FAILURE);
}
if (getword(elap,sizeof(elap),&gwarea,'\t')<0){
debuga(_("Invalid record in file \"%s\"\n
"),arq);
exit(EXIT_FAILURE);
}
if (getword(smartfilter,sizeof(smartfilter),&gwar
ea,'\0')<0){
debuga(_("Invalid record in file \"%s\"\n
"),arq);
exit(EXIT_FAILURE);
}
getword_start(&gwarea,data);
if (getword_atoll(&iday,&gwarea,'/')<0 || iday<1
|| iday>31){
debuga(_("Invalid record in file \"%s\"\n
"),arq);
exit(EXIT_FAILURE);
}
if (getword_atoll(&imonth,&gwarea,'/')<0 || imont
h<1 || imonth>12){
debuga(_("Invalid record in file \"%s\"\n
"),arq);
exit(EXIT_FAILURE);
}
if (getword_atoll(&iyear,&gwarea,'\0')<0){
debuga(_("Invalid record in file \"%s\"\n
"),arq);
exit(EXIT_FAILURE);
}
idata=builddia(iday,imonth,iyear);
computedate(iyear,imonth,iday,&tt);
if (sscanf(hora,"%d:%d:%d",&tt.tm_hour,&tt.tm_min
,&tt.tm_sec)!=3 || tt.tm_hour<0 || tt.tm_hour>=24 ||
tt.tm_min<0 || tt.tm_min>=60 || t
t.tm_sec<0 || tt.tm_sec>=60) {
debuga(_("Invalid time in file \"%s\"\n")
,arq);
exit(EXIT_FAILURE);
}
t=&tt;
}
if (ilf==ILF_Isa) {
if (linebuf[0] == '#') {
int ncols,cols[ISACOL_Last];
fixendofline(linebuf);
getword_start(&gwarea,linebuf);
// remove the #Fields: column at the begi
nning of the line
if (getword_skip(1000,&gwarea,' ')<0){
debuga(_("Invalid record in file
\"%s\"\n"),arq);
exit(EXIT_FAILURE);
}
for (ncols=0 ; ncols<ISACOL_Last ; ncols+
+) cols[ncols]=-1;
ncols=0;
while(gwarea.current[0] != '\0') {
if (getword(val1,sizeof(val1),&gw
area,'\t')<0){
debuga(_("Invalid record
in file \"%s\"\n"),arq);
exit(EXIT_FAILURE);
}
if(strcmp(val1,"c-ip") == 0) cols
[ISACOL_Ip]=ncols;
if(strcmp(val1,"cs-username") ==
0) cols[ISACOL_UserName]=ncols;
if(strcmp(val1,"date") == 0) cols
[ISACOL_Date]=ncols;
if(strcmp(val1,"time") == 0) cols
[ISACOL_Time]=ncols;
if(strcmp(val1,"time-taken") == 0
) cols[ISACOL_TimeTaken]=ncols;
if(strcmp(val1,"sc-bytes") == 0)
cols[ISACOL_Bytes]=ncols;
if(strcmp(val1,"cs-uri") == 0) co
ls[ISACOL_Uri]=ncols;
if(strcmp(val1,"sc-status") == 0)
cols[ISACOL_Status]=ncols;
ncols++;
}
if (cols[ISACOL_Ip]>=0) {
isa_ncols=ncols;
for (ncols=0 ; ncols<ISACOL_Last
; ncols++)
isa_cols[ncols]=cols[ncol
s];
}
continue;
}
if (!isa_ncols) continue;
getword_start(&gwarea,linebuf);
for (x=0 ; x<isa_ncols ; x++) {
if (getword_ptr(linebuf,&str,&gwarea,'\t'
)<0) {
debuga(_("Invalid record in file
\"%s\"\n"),arq);
exit(EXIT_FAILURE);
}
if (x==isa_cols[ISACOL_Ip]) {
if (strlen(str)>=sizeof(ip)) {
debuga(_("Invalid IP addr
ess in file \"%s\"\n"),arq);
exit(EXIT_FAILURE);
}
strcpy(ip,str);
} else if (x==isa_cols[ISACOL_UserName])
{
if (strlen(str)>=sizeof(user)) {
debuga(_("Invalid user ID
in file \"%s\"\n"),arq);
exit(EXIT_FAILURE);
}
strcpy(user,str);
} else if (x==isa_cols[ISACOL_Date]) {
if (strlen(str)>=sizeof(data)) {
debuga(_("Invalid record
in file \"%s\"\n"),arq);
exit(EXIT_FAILURE);
}
strcpy(data,str);
} else if (x==isa_cols[ISACOL_Time]) {
if (strlen(str)>=sizeof(hora)) {
debuga(_("Invalid time in
file \"%s\"\n"),arq);
exit(EXIT_FAILURE);
}
strcpy(hora,str);
} else if (x==isa_cols[ISACOL_TimeTaken])
{
if (strlen(str)>=sizeof(elap)) {
debuga(_("Invalid downloa
d duration in file \"%s\"\n"),arq);
exit(EXIT_FAILURE);
}
strcpy(elap,str);
} else if (x==isa_cols[ISACOL_Bytes]) {
if (strlen(str)>=sizeof(tam)) {
debuga(_("Invalid downloa
d size in file \"%s\"\n"),arq);
exit(EXIT_FAILURE);
}
strcpy(tam,str);
} else if (x==isa_cols[ISACOL_Uri]) {
full_url=str;
} else if (x==isa_cols[ISACOL_Status]) {
if (strlen(str)>=sizeof(code)) {
debuga(_("Invalid access
code in file \"%s\"\n"),arq);
exit(EXIT_FAILURE);
}
strcpy(code,str);
}
}
if(strcmp(code,"401") == 0 || strcmp(code,"403")
== 0 || strcmp(code,"407") == 0) {
sprintf(val1,"DENIED/%s",code);
strcpy(code,val1);
}
getword_start(&gwarea,data);
if (getword_atoll(&iyear,&gwarea,'-')<0){
debuga(_("Invalid year in file \"%s\"\n")
,arq);
exit(EXIT_FAILURE);
}
if (getword_atoll(&imonth,&gwarea,'-')<0){
debuga(_("Invalid month in file \"%s\"\n"
),arq);
exit(EXIT_FAILURE);
}
if (getword_atoll(&iday,&gwarea,'\0')<0){
debuga(_("Invalid day in file \"%s\"\n"),
arq);
exit(EXIT_FAILURE);
}
idata=builddia(iday,imonth,iyear);
computedate(iyear,imonth,iday,&tt);
if (isa_cols[ISACOL_Time]>=0) {
if (sscanf(hora,"%d:%d:%d",&tt.tm_hour,&t
t.tm_min,&tt.tm_sec)!=3 || tt.tm_hour<0 || tt.tm_hour>=24 ||
tt.tm_min<0 || tt.tm_min>
=60 || tt.tm_sec<0 || tt.tm_sec>=60) {
debuga(_("Invalid time in file \"
%s\"\n"),arq);
exit(EXIT_FAILURE);
}
}
t=&tt;
}
if (t==NULL) {
debuga(_("Unknown input log file format\n"));
break;
}
strftime(dia, sizeof(dia), "%d/%m/%Y", t);
snprintf(hora,sizeof(hora),"%02d:%02d:%02d",t->tm_hour,t-
>tm_min,t->tm_sec);
if(debugm)
printf("DATE=%s IDATA=%d DFROM=%d DUNTIL=%d\n",da
te,idata,dfrom,duntil);
if(date[0] != '\0'){
if(idata < dfrom || idata > duntil) continue;
}
// Record only hours usage which is required
if (t) {
if( bsearch( &( t -> tm_wday ), weekdays.list, we
ekdays.len, sizeof( int ), compar ) == NULL )
continue;
if( bsearch( &( t -> tm_hour ), hours.list, hours
.len, sizeof( int ), compar ) == NULL )
continue;
}
if(strlen(user) > MAX_USER_LEN) {
if (debugm) printf(_("User ID too long: %s\n"),us
er);
totregsx++;
continue;
}
// include_users
if(IncludeUsers[0] != '\0') {
snprintf(val1,sizeof(val1),":%s:",user);
if((str=(char *) strstr(IncludeUsers,val1)) == (c
har *) NULL )
continue;
}
if(vercode(code)) {
if (debugm) printf(_("Excluded code: %s\n"),code)
;
totregsx++;
continue;
}
if(testvaliduserchar(user))
continue;
#if 0
if((str = strstr(user,"%20")) != NULL) {
/*
This is a patch introduced to solve bug #1624251
reported at sourceforge but
the side effect is to truncate the name at the fi
rst space and merge the reports
of people whose name is identical up to the first
space.
The old code used to truncate the user name at th
e first % if a %20 was
found anywhere in the string. That means the stri
ng could be truncated
at the wrong place if another % occured before th
e %20. This new code should
avoid that problem and only truncate at the space
. There is no bug
report indicating that anybody noticed this.
*/
*str='\0';
}
/*
Code prior to 2.2.7 used to replace any %xx by a dot as l
ong as a %5c was
found in the user name.
*/
while((str = strstr(user,"%5c")) != NULL) {
*str='.';
for (x=3 ; str[x] ; x++) str[x-2]=str[x];
}
#endif
// replace any tab by a single space
for (str=full_url ; *str ; str++)
if (*str=='\t') *str=' ';
for (str=code ; *str ; str++)
if (*str=='\t') *str=' ';
if(ilf!=ILF_Sarg) {
/*
The full URL is not saved in sarg log. There is n
o point in testing the URL to detect
a downloaded file.
*/
download_flag=is_download_suffix(full_url);
if (download_flag) {
safe_strcpy(download_url,full_url,sizeof(
download_url));
download_count++;
}
} else
download_flag=false;
url=process_url(full_url,LongUrl);
if (!url || url[0] == '\0') continue;
if(addr[0] != '\0'){
if(strcmp(addr,ip)!=0) continue;
}
if(fhost) {
if(!vhexclude(url)) {
if (debugm) printf(_("Excluded site: %s\n
"),url);
totregsx++;
continue;
}
}
if(hm >= 0 && hmf >= 0) {
hmr=t->tm_hour*100+t->tm_min;
if(hmr < hm || hmr > hmf) continue;
}
if(site[0] != '\0'){
if(strstr(url,site)==0) continue;
}
if(UserIp) {
strcpy(user,ip);
id_is_ip=true;
} else {
id_is_ip=false;
if(strcmp(user,"-") == 0 || strcmp(user," ") == 0
|| strcmp(user,"") == 0) {
if(RecordsWithoutUser == RECORDWITHOUTUSE
R_IP) {
strcpy(user,ip);
id_is_ip=true;
}
if(RecordsWithoutUser == RECORDWITHOUTUSE
R_IGNORE)
continue;
if(RecordsWithoutUser == RECORDWITHOUTUSE
R_EVERYBODY)
strcpy(user,"everybody");
} else {
strlow(user);
if(NtlmUserFormat == NTLMUSERFORMAT_USER)
{
if ((str=strchr(user,'+'))!=NULL
|| (str=strchr(user,'\\'))!=NULL || (str=strchr(user,'_'))!=NULL) {
strcpy(warea,str+1);
strcpy(user,warea);
}
}
}
}
if(us[0] != '\0'){
if(strcmp(user,us)!=0) continue;
}
if(puser) {
snprintf(wuser,sizeof(wuser),":%s:",user);
if(strstr(userfile, wuser) == 0)
continue;
}
if(fuser) {
if(!vuexclude(user)) {
if (debugm) printf(_("Excluded user: %s\n
"),user);
totregsx++;
continue;
}
}
if(strcmp(user,"-") ==0 || strcmp(user," ") ==0 || strcmp
(user,"") ==0 || strcmp(user,":") ==0)
continue;
nbytes=atoll(tam);
if (nbytes<0) nbytes=0;
elap_time=atol(elap);
if (elap_time<0) elap_time=0;
if(max_elapsed) {
if(elap_time>max_elapsed) {
elap_time=0;
}
}
if((str=(char *) strstr(linebuf, "[SmartFilter:")) != (ch
ar *) NULL ) {
fixendofline(str);
snprintf(smartfilter,sizeof(smartfilter),"\"%s\""
,str+1);
} else strcpy(smartfilter,"\"\"");
nopen=0;
prev_ufile=NULL;
for (ufile=first_user_file ; ufile && strcmp(user,ufile->
user->id)!=0 ; ufile=ufile->next) {
prev_ufile=ufile;
if (ufile->file) nopen++;
}
if (!ufile) {
ufile=malloc(sizeof(*ufile));
if (!ufile) {
debuga(_("Not enough memory to store user
%s\n"),user);
exit(EXIT_FAILURE);
}
memset(ufile,0,sizeof(*ufile));
ufile->next=first_user_file;
first_user_file=ufile;
uinfo=userinfo_create(user,ip);
ufile->user=uinfo;
uinfo->id_is_ip=id_is_ip;
nusers++;
} else {
if (prev_ufile) {
prev_ufile->next=ufile->next;
ufile->next=first_user_file;
first_user_file=ufile;
}
}
#ifdef ENABLE_DOUBLE_CHECK_DATA
if (strcmp(code,"TCP_DENIED/407")!=0) {
ufile->user->nbytes+=nbytes;
ufile->user->elap+=elap_time;
}
#endif
if (ufile->file==NULL) {
if (nopen>=maxopenfiles) {
x=0;
for (ufile1=first_user_file ; ufile1 ; uf
ile1=ufile1->next) {
if (ufile1->file!=NULL) {
if (x>=maxopenfiles) {
if (fclose(ufile1
->file)==EOF) {
debuga(_(
"Failed to close file \"%s\": %s\n"),ufile1->user->id,strerror(errno));
exit(EXIT
_FAILURE);
}
ufile1->file=NULL
;
}
x++;
}
}
}
if (snprintf (tmp3, sizeof(tmp3), "%s/%s.user_uns
ort", tmp, ufile->user->filename)>=sizeof(tmp3)) {
debuga(_("Path too long: "));
debuga_more("%s/%s.user_unsort\n", tmp, u
file->user->filename);
exit(EXIT_FAILURE);
}
if ((ufile->file = MY_FOPEN (tmp3, "a")) == NULL)
{
debugapos("log",_("Cannot open file \"%s\
": %s\n"), tmp3, strerror(errno));
exit (1);
}
}
if (fprintf(ufile->file, "%s\t%s\t%s\t%s\t%"PRIi64"\t%s\t
%ld\t%s\n",dia,hora,ip,url,(int64_t)nbytes,code,elap_time,smartfilter)<=0) {
debuga(_("Write error in the log file of user %s\
n"),user);
exit(EXIT_FAILURE);
}
records_kept++;
if(fp_log && ilf!=ILF_Sarg)
fprintf(fp_log, "%s\t%s\t%s\t%s\t%s\t%"PRIi64"\t%
s\t%ld\t%s\n",dia,hora,user,ip,url,(int64_t)nbytes,code,elap_time,smartfilter);
totregsg++;
if(!dataonly && download_flag && strstr(code,"DENIED") ==
0) {
ndownload = 1;
if ( ! fp_Download_Unsort ) {
if ((fp_Download_Unsort = MY_FOPEN ( sz_D
ownload_Unsort, "a")) == NULL) {
debugapos("log",_("Cannot open fi
le \"%s\": %s\n"),sz_Download_Unsort, strerror(errno));
exit (1);
}
}
fprintf(fp_Download_Unsort,"%s\t%s\t%s\t%s\t%s\n"
,dia,hora,user,ip,download_url);
}
if((ReportType & REPORT_TYPE_DENIED) != 0) {
if(fp_denied && strstr(code,"DENIED/403") != 0) {
fprintf(fp_denied, "%s\t%s\t%s\t%s\t%s\n"
,dia,hora,user,ip,full_url);
denied_count++;
}
}
if((ReportType & REPORT_TYPE_AUTH_FAILURES) != 0) {
if(fp_authfail && (strstr(code,"DENIED/401") != 0
|| strstr(code,"DENIED/407") != 0)) {
fprintf(fp_authfail, "%s\t%s\t%s\t%s\t%s\
n",dia,hora,user,ip,full_url);
authfail_count++;
}
}
if (ilf!=ILF_Sarg) {
if(!totper || idata<mindate){
mindate=idata;
memcpy(&period.start,t,sizeof(*t));
strcpy(start_hour,tbuf2);
}
if (!totper || idata>maxdate) {
maxdate=idata;
memcpy(&period.end,t,sizeof(*t));
}
totper=true;
}
if(debugm){
printf("IP=\t%s\n",ip);
printf("USER=\t%s\n",user);
printf("ELAP=\t%ld\n",elap_time);
printf("DATE=\t%s\n",dia);
printf("TIME=\t%s\n",hora);
printf("FUNC=\t%s\n",fun);
printf("URL=\t%s\n",url);
printf("CODE=\t%s\n",code);
printf("LEN=\t%"PRIi64"\n",(int64_t)nbytes);
}
}
if (!from_stdin) {
if (from_pipe)
pclose(fp_in);
else {
fclose(fp_in);
if( ShowReadStatistics ) {
printf(_("SARG: Records in file: %lu, rea
ding: %3.2lf%%"),recs1,100.);
putchar('\n');
}
}
}
}
read_end_time=time(NULL); read_end_time=time(NULL);
read_elapsed=(double)read_end_time-(double)read_start_time; read_elapsed=(double)read_end_time-(double)read_start_time;
if (debug) FileList_Destroy(&AccessLog);
debuga(_(" Records read: %ld, written: %ld, excluded: %ld\n"),t
otregsl,totregsg,totregsx);
longline_destroy(&line);
if ( fp_Download_Unsort )
fclose (fp_Download_Unsort);
for (ufile=first_user_file ; ufile ; ufile=ufile1) {
ufile1=ufile->next;
if (ufile->file!=NULL) fclose(ufile->file);
free(ufile);
}
free_download(); free_download();
free_excludecodes(); free_excludecodes();
free_exclude(); free_exclude();
if(debug) { if (debug) {
int totalcount=0; char date0[30], date1[30];
struct tm Start,End;
for (ilf=0 ; ilf<ILF_Last ; ilf++) totalcount+=ilf_count[ilf];
if(ilf_count[ILF_Common]>0 && ilf_count[ILF_Squid]>0)
debuga(_("Log with mixed records format (squid and common
log)\n"));
if(ilf_count[ILF_Common]>0 && ilf_count[ILF_Squid]==0)
debuga(_("Common log format\n"));
if(ilf_count[ILF_Common]==0 && ilf_count[ILF_Squid]>0)
debuga(_("Squid log format\n"));
if(ilf_count[ILF_Sarg]>0)
debuga(_("Sarg log format\n"));
if(totalcount==0 && totregsg) if (GetLogPeriod(&Start,&End)) {
debuga(_("Log with invalid format\n")); strftime(date0,sizeof(date0),"%x",&Start);
strftime(date1,sizeof(date1),"%x",&End);
// TRANSLATORS: The %s are the start and end dates in loc
ale format.
debuga(__FILE__,__LINE__,_("Period covered by log files:
%s-%s\n"),date0,date1);
}
} }
if(!totregsg){ if (!LogStatus){
debuga(_("No records found\n")); debuga(__FILE__,__LINE__,_("No records found\n"));
debuga(_("End\n")); debuga(__FILE__,__LINE__,_("End\n"));
if(fp_denied) fclose(fp_denied);
if(fp_authfail) fclose(fp_authfail);
userinfo_free(); userinfo_free();
if(userfile) free(userfile); if (userfile) free(userfile);
close_usertab(); close_usertab();
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
if (date[0]!='\0') { if (debug) {
char date0[30], date1[30]; char date0[30], date1[30];
strftime(date0,sizeof(date0),"%d/%m/%Y",&period.start); strftime(date0,sizeof(date0),"%x",&period.start);
strftime(date1,sizeof(date1),"%d/%m/%Y",&period.end); strftime(date1,sizeof(date1),"%x",&period.end);
debuga(_("Period covered by log files: %s-%s\n"),date0,date1); // TRANSLATORS: The %s are the start and end dates in locale form
getperiod_fromrange(&period,dfrom,duntil); at.
debuga(__FILE__,__LINE__,_("Period extracted from log files: %s-%
s\n"),date0,date1);
}
if (ReadFilter.DateRange[0] != '\0') {
getperiod_fromrange(&period,&ReadFilter);
} }
if (getperiod_buildtext(&period)<0) { if (getperiod_buildtext(&period)<0) {
debuga(_("Failed to build the string representation of the date r ange\n")); debuga(__FILE__,__LINE__,_("Failed to build the string representa tion of the date range\n"));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if(debugz){
debugaz(_("date=%s\n"),dia);
debugaz(_("period=%s\n"),period.text);
}
if(debug) {
debuga(_("Period: %s"),period.text);
debuga_more("\n");
}
if(fp_denied)
fclose(fp_denied);
if(fp_authfail)
fclose(fp_authfail);
if(fp_log != NULL) {
char end_hour[128];
char val2[40];
char val4[255];//val4 must not be bigger than arq_log without fix
ing the strcpy below
fclose(fp_log);
safe_strcpy(end_hour,tbuf2,sizeof(end_hour));
strftime(val2,sizeof(val2),"%d%m%Y",&period.start);
strftime(val1,sizeof(val1),"%d%m%Y",&period.end);
if (snprintf(val4,sizeof(val4),"%s/sarg-%s_%s-%s_%s.log",ParsedOu
tputLog,val2,start_hour,val1,end_hour)>=sizeof(val4)) {
debuga(_("Path too long: "));
debuga_more("%s/sarg-%s_%s-%s_%s.log\n",ParsedOutputLog,v
al2,start_hour,val1,end_hour);
exit(EXIT_FAILURE);
}
if (rename(arq_log,val4)) {
debuga(_("Failed to rename \"%s\" into \"%s\": %s\n"),arq
_log,val4,strerror(errno));
} else {
strcpy(arq_log,val4);
if(strcmp(ParsedOutputLogCompress,"nocompress") != 0 && P
arsedOutputLogCompress[0] != '\0') {
/*
No double quotes around ParsedOutputLogCompress b
ecause it may contain command line options. If double quotes are
necessary around the command name, put them in th
e configuration file.
*/
if (snprintf(val1,sizeof(val1),"%s \"%s\"",Parsed
OutputLogCompress,arq_log)>=sizeof(val1)) {
/* TRANSLATORS: The message is followed b
y the command that's too long. */
debuga(_("Command too long: "));
debuga_more("%s \"%s\"\n",ParsedOutputLog
Compress,arq_log);
exit(EXIT_FAILURE);
}
cstatus=system(val1);
if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus))
{
debuga(_("command return status %d\n"),WE
XITSTATUS(cstatus));
debuga(_("command: %s\n"),val1);
exit(EXIT_FAILURE);
}
}
}
if(debug)
debuga(_("Sarg parsed log saved as %s\n"),arq_log);
}
if(DataFile[0] == '\0' && (ReportType & REPORT_TYPE_DENIED) != 0) {
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(_("Command too long: "));
debuga_more("sort -T \"%s\" -t \"\t\" -k 3,3 -k 5,5 -o \"
%s\" \"%s\"",tmp,denied_sort,denied_unsort);
exit(EXIT_FAILURE);
}
cstatus=system(csort);
if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
debuga(_("sort command return status %d\n"),WEXITSTATUS(c
status));
debuga(_("sort command: %s\n"),csort);
exit(EXIT_FAILURE);
}
if (!KeepTempLog && unlink(denied_unsort)) {
debuga(_("Cannot delete \"%s\": %s\n"),denied_unsort,stre
rror(errno));
exit(EXIT_FAILURE);
}
}
process_start_time=time(NULL); process_start_time=time(NULL);
if(DataFile[0] != '\0') if (DataFile[0] != '\0')
data_file(tmp); data_file(tmp);
else else
gerarel(); gerarel(&ReadFilter);
process_end_time=time(NULL); process_end_time=time(NULL);
process_elapsed=(double)process_end_time-(double)process_start_time; process_elapsed=(double)process_end_time-(double)process_start_time;
if((ReportType & REPORT_TYPE_DENIED) != 0) { denied_cleanup();
if (!KeepTempLog && unlink(denied_sort) && errno!=ENOENT) authfail_cleanup();
debuga(_("Cannot delete \"%s\": %s\n"),denied_sort,strerr download_cleanup();
or(errno)); CleanTemporaryDir();
}
if(!KeepTempLog && strcmp(tmp,"/tmp") != 0) {
unlinkdir(tmp,0);
}
ip2name_cleanup(); ip2name_cleanup();
free_hostalias(); free_hostalias();
free_useralias();
userinfo_free(); userinfo_free();
if(userfile) if (userfile)
free(userfile); free(userfile);
close_usertab(); close_usertab();
FileList_Destroy(&UserAgentLog);
end_time=time(NULL); end_time=time(NULL);
if (show_statis) { if (show_statis) {
double elapsed=(double)end_time-(double)start_time; double elapsed=(double)end_time-(double)start_time;
debuga(_("Total execution time: %.0lf seconds\n"),elapsed); debuga(__FILE__,__LINE__,_("Total execution time: %.0lf seconds\n "),elapsed);
if (read_elapsed>0.) { if (read_elapsed>0.) {
debuga(_("Lines read: %lu lines in %.0lf seconds (%.0lf l ines/s)\n"),lines_read,read_elapsed,(double)lines_read/read_elapsed); debuga(__FILE__,__LINE__,_("Lines read: %lu lines in %.0l f seconds (%.0lf lines/s)\n"),lines_read,read_elapsed,(double)lines_read/read_el apsed);
} }
if (process_elapsed>0.) { if (process_elapsed>0.) {
debuga(_("Processed records: %lu records in %.0lf seconds debuga(__FILE__,__LINE__,_("Processed records: %lu record
(%.0lf records/s)\n"),records_kept,process_elapsed,(double)records_kept/process s in %.0lf seconds (%.0lf records/s)\n"),records_kept,process_elapsed,(double)re
_elapsed); cords_kept/process_elapsed);
debuga(_("Users: %lu users in %.0lf seconds (%.0lf users/ debuga(__FILE__,__LINE__,_("Users: %lu users in %.0lf sec
s)\n"),nusers,process_elapsed,(double)nusers/process_elapsed); onds (%.0lf users/s)\n"),nusers,process_elapsed,(double)nusers/process_elapsed);
} }
} }
if(debug) if (debug)
debuga(_("End\n")); debuga(__FILE__,__LINE__,_("End\n"));
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
static void getusers(const char *pwdfile, int debug) static void getusers(const char *pwdfile, int debug)
{ {
FILE *fp_usr; FILE *fp_usr;
char buf[255]; char buf[255];
char *str; char *str;
long int nreg=0; long int nreg=0;
if(debug) if (debug)
debuga(_("Loading password file from %s\n"),pwdfile); debuga(__FILE__,__LINE__,_("Loading password file \"%s\"\n"),pwdf
ile);
if ((fp_usr = fopen(pwdfile, "r")) == NULL) { if ((fp_usr = fopen(pwdfile, "r")) == NULL) {
debugapos("getusers",_("Cannot open file \"%s\": %s\n"),pwdfile,s trerror(errno)); debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),pwdfi le,strerror(errno));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (fseek(fp_usr, 0, SEEK_END)==-1) { if (fseek(fp_usr, 0, SEEK_END)==-1) {
debuga(_("Failed to move till the end of file \"%s\": %s\n"),pwdf ile,strerror(errno)); debuga(__FILE__,__LINE__,_("Failed to move till the end of file \ "%s\": %s\n"),pwdfile,strerror(errno));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
nreg = ftell(fp_usr); nreg = ftell(fp_usr);
if (nreg<0) { if (nreg<0) {
debuga(_("Cannot get the size of file \"%s\": %s\n"),pwdfile,stre rror(errno)); debuga(__FILE__,__LINE__,_("Cannot get the size of file \"%s\"\n" ),pwdfile);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
nreg = nreg+5000; nreg = nreg+5000;
if (fseek(fp_usr, 0, SEEK_SET)==-1) { if (fseek(fp_usr, 0, SEEK_SET)==-1) {
debuga(_("Failed to rewind file \"%s\": %s\n"),pwdfile,strerror(e rrno)); debuga(__FILE__,__LINE__,_("Failed to rewind file \"%s\": %s\n"), pwdfile,strerror(errno));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if((userfile=(char *) malloc(nreg))==NULL){ if ((userfile=(char *) malloc(nreg))==NULL){
debuga(_("malloc failed to allocate %ld bytes\n"),nreg); debuga(__FILE__,__LINE__,_("malloc error (%ld bytes required)\n")
,nreg);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
bzero(userfile,nreg); memset(userfile,0,nreg);
strcpy(userfile,":"); strcpy(userfile,":");
while(fgets(buf,sizeof(buf),fp_usr)!=NULL) { while(fgets(buf,sizeof(buf),fp_usr)!=NULL) {
str=strchr(buf,':'); str=strchr(buf,':');
if (!str) { if (!str) {
debuga(_("Invalid user in file \"%s\"\n"),pwdfile); debuga(__FILE__,__LINE__,_("Invalid user in file \"%s\"\n "),pwdfile);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
str[1]='\0'; str[1]='\0';
strcat(userfile,buf); strcat(userfile,buf);
} }
fclose(fp_usr); if (fclose(fp_usr)==EOF) {
debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),pwdfile,
strerror(errno));
exit(EXIT_FAILURE);
}
return; return;
} }
static void CleanTemporaryDir()
{
if (!KeepTempLog && strcmp(tmp,"/tmp") != 0) {
unlinkdir(tmp,0);
}
}
 End of changes. 134 change blocks. 
1502 lines changed or deleted 313 lines changed or added

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