From: Frédéric Marchal Date: Sat, 20 Feb 2010 16:15:58 +0000 (+0000) Subject: Use one function to parse the lines from sarg-general instead of multiple codes scatt... X-Git-Tag: v2.3-pre2~81 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2240dcea42dc19e969132876a5cfc12688b7356e;p=thirdparty%2Fsarg.git Use one function to parse the lines from sarg-general instead of multiple codes scattered around the program. Remove a few unused global variables. --- diff --git a/CMakeLists.txt b/CMakeLists.txt index a36227d..1f360f3 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -273,7 +273,6 @@ IF(NOT HAVE_LC_MESSAGES) ENDIF(HAVE_LIBINTL_H) LIST(LENGTH FILES NFILES) IF(NFILES GREATER 0) - MESSAGE("Search LC_MESSAGES in ${FILES}") CHECK_SYMBOL_EXISTS(LC_MESSAGES "${FILES}" HAVE_LC_MESSAGES) ENDIF(NFILES GREATER 0) ENDIF(NOT HAVE_LC_MESSAGES) diff --git a/authfail.c b/authfail.c index 58e7dae..ff82801 100644 --- a/authfail.c +++ b/authfail.c @@ -46,6 +46,8 @@ void authfail_report(void) char data[15]; char hora[15]; char *str; + char tmp4[MAXLEN]; + char csort[MAXLEN]; int z=0; int count=0; int cstatus; diff --git a/dansguardian_log.c b/dansguardian_log.c index bcb708f..cf85e7c 100644 --- a/dansguardian_log.c +++ b/dansguardian_log.c @@ -40,6 +40,7 @@ void dansguardian_log(void) char ip[30]; char wdata[127]; char *url; + char tmp6[MAXLEN]; int idata=0; int cstatus; struct getwordstruct gwarea; @@ -111,7 +112,7 @@ void dansguardian_log(void) if(debug) { char short_url[1024]; getword_start(&gwarea,text[7]); - if (getword_limit(short_url,sizeof(short_url),&gwarea,' ')<0 || getword(href,sizeof(href),&gwarea,' ')<0) { + if (getword_limit(short_url,sizeof(short_url),&gwarea,' ')<0 || getword_skip(MAXLEN,&gwarea,' ')<0) { printf("SARG: Maybe you have a broken record or garbage in your %s entry.\n",text[7]); exit(1); } diff --git a/datafile.c b/datafile.c index e866ee0..8c7ace1 100644 --- a/datafile.c +++ b/datafile.c @@ -42,6 +42,7 @@ void data_file(char *tmp) char accsmart[MAXLEN]; char crc2[50]; char *str; + char tmp3[MAXLEN]; DIR *dirp; struct dirent *direntp; long long int nbytes=0; @@ -60,7 +61,7 @@ void data_file(char *tmp) nameantes[0]='\0'; olduser[0]='\0'; - strncat(tmp,"/sarg",5); + strcat(tmp,"/sarg"); dirp = opendir(tmp); while ( (direntp = readdir( dirp )) != NULL ) { diff --git a/documentation/report.txt b/documentation/report.txt index 0f39583..a80fd5e 100644 --- a/documentation/report.txt +++ b/documentation/report.txt @@ -156,6 +156,22 @@ Append a record to the sarg-general file. +/*! \fn int ger_read(char *buffer,struct generalitemstruct *item,const char *filename) +Parse one line of the file written by gravager(). + +\param buffer The line of text to parse. +\param item The structure to fill with the data parsed from the line. The structure contains +pointers to strings but some of them are limited in length to detect a corruption of the file +and to let the caller use simple string manipulation functions with fixed size buffer. +\param filename The name of the file from which the line is extracted to report it if an +error is found. + +\return Zero on succes. The program exits if an error is found. +*/ + + + + /*! \fn static void grava_SmartFilter(const char *dirname, const char *user, const char *ip, const char *data, const char *hora, const char *url, const char *smart) Append a record into the \c smartfilter.unsort file of the specified directory. diff --git a/email.c b/email.c index 6274479..63c7b8c 100644 --- a/email.c +++ b/email.c @@ -33,20 +33,21 @@ int geramail(const char *dirname, int debug, const char *outdir, int userip, con FILE *fp_in, *fp_top1, *fp_top2, *fp_top3; long long int ttnbytes=0, ttnacc=0, tnacc=0; long long int tnbytes=0, ttnelap=0, tnelap=0; - float perc=0.00; - float perc2=0.00; + long long int nacc, nbytes, elap; + double perc=0.00; + double perc2=0.00; int posicao=0; - char olduser[MAXLEN], csort[MAXLEN], period[MAXLEN], arqper[MAXLEN]; - char wger[MAXLEN], top1[MAXLEN], top2[MAXLEN], top3[MAXLEN], user[MAXLEN], nacc[20], nbytes[20], url[MAXLEN], tusr[MAXLEN]; - char ip[MAXLEN], hora[9], data[15], elap[16], user2[MAXLEN], wperc[8], wperc2[8]; + char olduser[MAX_USER_LEN], csort[MAXLEN], period[MAXLEN], arqper[MAXLEN]; + char wger[MAXLEN], top1[MAXLEN], top2[MAXLEN], top3[MAXLEN], user[MAX_USER_LEN], tusr[MAXLEN]; + char user2[MAX_USER_LEN]; char strip1[MAXLEN], strip2[MAXLEN], strip3[MAXLEN], strip4[MAXLEN], strip5[MAXLEN], strip6[MAXLEN], strip7[MAXLEN]; - char incac[30], oucac[30]; int totuser=0; time_t t; struct tm *local; int cstatus; char warea[MAXLEN]; struct getwordstruct gwarea; + struct generalitemstruct item; strcpy(wger,dirname); strcpy(top1,dirname); @@ -78,52 +79,9 @@ int geramail(const char *dirname, int debug, const char *outdir, int userip, con totuser=0; while(fgets(warea,sizeof(warea),fp_in)) { - getword_start(&gwarea,warea); - if (getword(user,sizeof(user),&gwarea,'\t')<0) { - printf("SARG: Maybe you have a broken user in your %s file.\n",wger); - exit(1); - } - if(strcmp(user,"TOTAL") == 0) { - continue; - } - if (getword(nacc,sizeof(nacc),&gwarea,'\t')<0) { - printf("SARG: Maybe you have a broken number of access in your %s file.\n",wger); - exit(1); - } - if (getword(nbytes,sizeof(nbytes),&gwarea,'\t')<0) { - printf("SARG: Maybe you have a broken number of bytes in your %s file.\n",wger); - exit(1); - } - if (getword(url,sizeof(url),&gwarea,'\t')<0) { - printf("SARG: Maybe you have a broken url in your %s file.\n",wger); - exit(1); - } - if (getword(ip,sizeof(ip),&gwarea,'\t')<0) { - printf("SARG: Maybe you have a broken source IP address in your %s file.\n",wger); - exit(1); - } - if (getword(hora,sizeof(hora),&gwarea,'\t')<0) { - printf("SARG: Maybe you have a broken time in your %s file.\n",wger); - exit(1); - } - if (getword(data,sizeof(data),&gwarea,'\t')<0) { - printf("SARG: Maybe you have a broken date in your %s file.\n",wger); - exit(1); - } - if (getword(elap,sizeof(elap),&gwarea,'\t')<0) { - printf("SARG: Maybe you have a broken elapsed time in your %s file.\n",wger); - exit(1); - } - if (getword(incac,sizeof(incac),&gwarea,'\t')<0) { - printf("SARG: Maybe you have a broken in cache column in your %s file.\n",wger); - exit(1); - } - if (getword(oucac,sizeof(oucac),&gwarea,0)<0) { - printf("SARG: Maybe you have a broken not in cache column in your %s file.\n",wger); - exit(1); - } - - if(strcmp(olduser,user) != 0) + ger_read(warea,&item,wger); + if(item.total) continue; + if(strcmp(olduser,item.user) != 0) { totuser++; @@ -135,19 +93,19 @@ int geramail(const char *dirname, int debug, const char *outdir, int userip, con #else fprintf(fp_top2,"%s\t%lld\t%lld\t%lld\n",olduser,tnbytes,tnacc,tnelap); #endif - ttnbytes=ttnbytes+tnbytes; - ttnacc=ttnacc+tnacc; - ttnelap=ttnelap+tnelap; + ttnbytes+=tnbytes; + ttnacc+=tnacc; + ttnelap+=tnelap; } - strcpy(olduser,user); + strcpy(olduser,item.user); tnbytes=0; tnacc=0; tnelap=0; } - tnbytes=tnbytes+atol(nbytes); - tnacc=tnacc+atol(nacc); - tnelap=tnelap+atol(elap); + tnbytes+=item.nbytes; + tnacc+=item.nacc; + tnelap+=item.nelap; } if (olduser[0] != '\0') { @@ -158,9 +116,9 @@ int geramail(const char *dirname, int debug, const char *outdir, int userip, con #else fprintf(fp_top2,"%s\t%lld\t%lld\t%lld\n",olduser,tnbytes,tnacc,tnelap); #endif - ttnbytes=ttnbytes+tnbytes; - ttnacc=ttnacc+tnacc; - ttnelap=ttnelap+tnelap; + ttnbytes+=tnbytes; + ttnacc+=tnacc; + ttnelap+=tnelap; } fclose(fp_in); @@ -253,15 +211,15 @@ int geramail(const char *dirname, int debug, const char *outdir, int userip, con printf("SARG: Maybe you have a broken user in your %s file.\n",top1); exit(1); } - if (getword(nbytes,sizeof(nbytes),&gwarea,'\t')<0) { + if (getword_atoll(&nbytes,&gwarea,'\t')<0) { printf("SARG: Maybe you have a broken number of bytes in your %s file.\n",top1); exit(1); } - if (getword(nacc,sizeof(nacc),&gwarea,'\t')<0) { + if (getword_atoll(&nacc,&gwarea,'\t')<0) { printf("SARG: Maybe you have a broken number of access in your %s file.\n",top1); exit(1); } - if (getword(elap,sizeof(elap),&gwarea,'\t')<0) { + if (getword_atoll(&elap,&gwarea,'\t')<0) { printf("SARG: Maybe you have a broken elapsed time in your %s file.\n",top1); exit(1); } @@ -271,23 +229,10 @@ int geramail(const char *dirname, int debug, const char *outdir, int userip, con strcpy(user2,user); - tnbytes=atol(nbytes); - - if(tnbytes) { - perc=ttnbytes / 100; - perc=tnbytes / perc; - } else perc = 0; - - if(atol(elap)) { - perc2=ttnelap / 100; - perc2=atol(elap) / perc2; - } else perc2 = 0; + perc=(ttnbytes) ? nbytes * 100. / ttnbytes : 0; + perc2=(ttnelap) ? elap * 100. / ttnelap : 0; posicao++; - tnelap=atol(elap); - - sprintf(wperc,"%3.2f%%",perc); - sprintf(wperc2,"%3.2f%%",perc2); if(strcmp(user,"TOTAL") == 0){ fputs("------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",fp_top3); @@ -300,18 +245,16 @@ int geramail(const char *dirname, int debug, const char *outdir, int userip, con #endif } else { #if defined(__FreeBSD__) - fprintf(fp_top3,"%7d %20s %8s %15s %7s %10s %10qu %7s%%\n",posicao,user2,nacc,fixnum(tnbytes,1),wperc,buildtime(tnelap),tnelap,wperc2); + fprintf(fp_top3,"%7d %20s %8lld %15s %3.2lf%% %10s %10qu %3.2lf%%\n",posicao,user2,nacc,fixnum(nbytes,1),perc,buildtime(elap),elap,perc2); #elif defined(__alpha) || __ALPHA - fprintf(fp_top3,"%7d %20s %8s %15s %7s %10s %10ld %7s%%\n",posicao,user2,nacc,fixnum(tnbytes,1),wperc,buildtime(tnelap),tnelap,wperc2); + fprintf(fp_top3,"%7d %20s %8lld %15s %3.2lf%% %10s %10ld %3.2lf%%\n",posicao,user2,nacc,fixnum(nbytes,1),perc,buildtime(elap),elap,perc2); #else - fprintf(fp_top3,"%7d %20s %8s %15s %7s %10s %10lld %7s\n",posicao,user2,nacc,fixnum(tnbytes,1),wperc,buildtime(tnelap),tnelap,wperc2); + fprintf(fp_top3,"%7d %20s %8lld %15s %3.2lf%% %10s %10lld %3.2lf%%\n",posicao,user2,nacc,fixnum(nbytes,1),perc,buildtime(elap),elap,perc2); #endif } } - if(ttnbytes) { - tnbytes=ttnbytes / totuser; - } else tnbytes=0; + tnbytes=(totuser) ? ttnbytes / totuser : 0; strcpy(strip1,text[96]); strip_latin(strip1); diff --git a/grepday.c b/grepday.c index ee3c26f..c70a790 100644 --- a/grepday.c +++ b/grepday.c @@ -247,6 +247,8 @@ void greport_day(const char *user) char wuser[255]; char csort[255]; char data[20]; + char s[15]; + char tmp5[MAXLEN]; int count=0; int oday=0; int day=0; @@ -257,7 +259,6 @@ void greport_day(const char *user) time_t t; struct tm *local; int cstatus; - char s[15]; struct getwordstruct gwarea; struct getwordstruct gwarea1; diff --git a/html.c b/html.c index b9df41b..82a393b 100644 --- a/html.c +++ b/html.c @@ -50,6 +50,7 @@ void htmlrel(void) char warea[MAXLEN]; char totuser[8]; char ltext110[100]; + char tmp6[MAXLEN]; long long int tnacc=0, ttnacc=0, unacc=0; double perc=0, perc2=0, ouperc=0, inperc=0; char *s; @@ -60,6 +61,7 @@ void htmlrel(void) char siteind[MAX_TRUNCATED_URL]; struct getwordstruct gwarea; struct longlinestruct line,line1; + struct generalitemstruct item; if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return; @@ -95,23 +97,11 @@ void htmlrel(void) totbytes=0; totelap=0; while((buf=longline_read(fp_in,&line))!=NULL) { - if(strncmp(buf,"TOTAL\t",6) == 0) { - getword_start(&gwarea,buf); - if (getword_skip(MAXLEN,&gwarea,'\t')<0 || getword_atoll(<emp,&gwarea,'\t')<0) { - printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arqper); - exit(1); - } - ttnacc+=ltemp; - if (getword_atoll(<emp,&gwarea,'\t')<0) { - printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arqper); - exit(1); - } - totbytes+=ltemp; - if (getword_atoll(<emp,&gwarea,'\t')<0) { - printf("SARG: Maybe you have a broken record or garbage in your %s file (%d).\n",arqper,__LINE__); - exit(1); - } - totelap+=ltemp; + ger_read(buf,&item,arqper); + if(item.total) { + ttnacc+=item.nacc; + totbytes+=item.nbytes; + totelap+=item.nelap; } } fclose(fp_in); diff --git a/include/conf.h b/include/conf.h index 38de51e..623e7e4 100755 --- a/include/conf.h +++ b/include/conf.h @@ -152,6 +152,9 @@ int mkstemps(char *template, int suffixlen); #define MAXLEN 20000 #define MAX_URL_LEN 40000 #define MAX_TRUNCATED_URL 512 +#define MAX_USER_LEN 512 +#define MAX_IP_LEN 64 +#define MAX_DATETIME_LEN 32 #define MAXLOGS 255 #define REPORT_TYPE_USERS_SITES 0x0001UL @@ -207,26 +210,15 @@ int mkstemps(char *template, int suffixlen); char outdir[MAXLEN]; char dirname[MAXLEN]; char buf[MAXLEN]; -char user[MAXLEN]; char period[MAXLEN]; char code[MAXLEN]; char code2[MAXLEN]; char tmp[MAXLEN]; -char tmp3[MAXLEN]; -char tmp4[MAXLEN]; -char tmp5[MAXLEN]; -char tmp6[MAXLEN]; -char tmp7[MAXLEN]; char parse_out[MAXLEN]; char arqtt[MAXLEN]; char html[MAXLEN]; -char datestimes[MAXLEN]; char ConfigFile[MAXLEN]; -char href[MAXLEN]; -char href2[MAXLEN]; -char href3[MAXLEN]; char df[20]; -char day[3], month[4], year[5]; char cdfrom[30]; char cduntil[30]; int LastLog; @@ -234,7 +226,6 @@ int RemoveTempFiles; char ReplaceIndex[256]; unsigned long int Index; int OverwriteReport; -char u2[255]; char RecordsWithoutUser[20]; int UseComma; char MailUtility[PATH_MAX]; @@ -250,7 +241,6 @@ char name[MAXLEN]; int LongUrl; int Ip2Name; char language[255]; -char bufy[MAXLEN]; int NAccessLog; char AccessLog[MAXLOGS][MAXLEN]; int AccessLogFromCmdLine; @@ -287,7 +277,6 @@ int PerUserLimit; int UserIp; char MaxElapsed[255]; char datetimeby[10]; -char csort[255]; char CharSet[255]; char UserInvalidChar[255]; int Graphs; diff --git a/include/defs.h b/include/defs.h index 6a15546..f4c8a7c 100755 --- a/include/defs.h +++ b/include/defs.h @@ -20,6 +20,32 @@ struct longlinestruct size_t end; }; +struct generalitemstruct +{ + //! \c True if the entry is for the total of the file or \c false if it is a single line. + int total; + //! The user to which the entry apply. The length is limited to ::MAX_USER_LEN. + char *user; + //! The number of accesses performed by the user. + long long nacc; + //! The number of bytes transfered. + long long nbytes; + //! The URL accessed by the user. The length is not limited. + char *url; + //! The source IP address of the user. The length is limited to ::MAX_IP_LEN. + char *ip; + //! The time of the access. The length is limited to ::MAX_DATETIME_LEN. + char *time; + //! The date of the access. The length is limited to ::MAX_DATETIME_LEN. + char *date; + //! The number of milliseconds spend processing the request. + long long nelap; + //! The number of bytes fetched from the cache of the proxy (cache hit). + long long incache; + //! The number of bytes fetched from the site (cache miss). + long long oucache; +}; + // auth.c void htaccess(const char *name); @@ -103,6 +129,7 @@ void report_day(const char *user); // report.c void gravatmp(const char *oldaccuser, const char *oldurl, long long int nacc, long long int nbytes, const char *oldmsg, long long int nelap, int indexonly, long long int incache, long long int oucache); void gerarel(void); +int ger_read(char *buffer,struct generalitemstruct *item,const char *filename); // siteuser.c void siteuser(void); diff --git a/log.c b/log.c index 4a750d9..4ca2b7e 100644 --- a/log.c +++ b/log.c @@ -97,12 +97,17 @@ int main(int argc,char *argv[]) char zip[20]; char *str; char tmp2[MAXLEN]; + char tmp3[MAXLEN]; + char tmp4[MAXLEN]; + char tmp5[MAXLEN]; + char tmp6[MAXLEN]; char start_hour[128]; char end_hour[128]; char *linebuf; char hostname[512]; char *url; char *urly; + char user[MAX_USER_LEN]; enum InputLogFormat ilf; int ilf_count[ILF_Last]; int ch; diff --git a/realtime.c b/realtime.c index 867e607..43127b8 100755 --- a/realtime.c +++ b/realtime.c @@ -95,6 +95,7 @@ static int getdata(char *rec, FILE *ftmp) char tbuf[128]; char typ[128]; char warea[MAXLEN]; + char user[MAX_USER_LEN]; char *url; struct getwordstruct gwarea; @@ -129,9 +130,11 @@ static int getdata(char *rec, FILE *ftmp) } if(strncmp(typ,"CONNECT",7) == 0) { if (getword_ptr(rec,&url,&gwarea,' ')<0) { + debuga(_("The URL at column 7 is too long.")); return(-1); } if (getword(user,sizeof(user),&gwarea,' ')<0) { + debuga(_("The user ID at column 8 is too long.")); return(-1); } }else { @@ -179,8 +182,10 @@ static void datashow(const char *tmp) char *buf; char *url; char *ourl=NULL; - char ouser[MAXLEN]=""; + char ouser[MAX_USER_LEN]=""; char typ[128]; + char user[MAX_USER_LEN]; + char u2[MAX_USER_LEN]; int url_len; int ourl_size=0; struct getwordstruct gwarea; @@ -202,19 +207,19 @@ static void datashow(const char *tmp) fixendofline(buf); getword_start(&gwarea,buf); if (getword(dat,sizeof(dat),&gwarea,'\t')<0) { - printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp); + debuga(_("Maybe you have a broken record or garbage in your %s file"),tmp); exit(1); } if (getword(tim,sizeof(tim),&gwarea,'\t')<0) { - printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp); + debuga(_("Maybe you have a broken record or garbage in your %s file"),tmp); exit(1); } if (getword(ip,sizeof(ip),&gwarea,'\t')<0) { - printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp); + debuga(_("Maybe you have a broken record or garbage in your %s file"),tmp); exit(1); } if (getword(user,sizeof(user),&gwarea,'\t')<0) { - printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp); + debuga(_("Maybe you have a broken record or garbage in your %s file"),tmp); exit(1); } if(strlen(dat) < 3 || strlen(user) < 1) continue; diff --git a/report.c b/report.c index 782e821..5730be2 100644 --- a/report.c +++ b/report.c @@ -44,11 +44,11 @@ void gerarel(void) FILE *fp_gen; char *buf; - char accdia[11], acchora[9], accuser[MAXLEN], accip[MAXLEN], *accurl; + char accdia[11], acchora[9], accuser[MAX_USER_LEN], accip[MAXLEN], *accurl; char oldaccdia[11], oldacchora[9], oldaccip[MAXLEN]; - char wdirname[MAXLEN], oldaccuser[MAXLEN]; + char wdirname[MAXLEN], oldaccuser[MAX_USER_LEN]; char *oldurl=NULL; - char olduser[MAXLEN], oldmsg[50], acccode[MAXLEN/2 - 1], oldacccode[MAXLEN/2 - 1], user[MAXLEN]; + char olduser[MAX_USER_LEN], oldmsg[50], acccode[MAXLEN/2 - 1], oldacccode[MAXLEN/2 - 1], user[MAX_USER_LEN]; char ipantes[MAXLEN], nameantes[MAXLEN]; char accsmart[MAXLEN]; char crc2[MAXLEN/2 -1]; @@ -56,6 +56,9 @@ void gerarel(void) char arqtt[256]; char *oldurltt=NULL; char oldaccdiatt[11],oldacchoratt[9]; + char u2[MAX_USER_LEN]; + char tmp3[MAXLEN]; + char tmp4[5]; long long int nbytes=0; long long int nelap=0; long long int nacc=0; @@ -278,8 +281,8 @@ void gerarel(void) fputs("
\n",fp_tt); fputs("",fp_tt); - bzero(tmp4, MAXLEN); strncpy(tmp4,text[110],4); + tmp4[4]='\0'; fprintf(fp_tt,"\n",text[91],tmp4,text[110]+5); } @@ -426,15 +429,12 @@ static void maketmp(const char *user, const char *dirname, int debug, int indexo if(indexonly) return; if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return; - strcpy(wdirname,tmp); - strcat(wdirname,"/"); - strcat(wdirname,user); - - if(debug){ - debuga("%s: %s",text[49],wdirname); + if(debug) debuga("%s: %s/%s",text[49],tmp,user); + if (snprintf(wdirname,sizeof(wdirname),"%s/%s.utmp",tmp,user)>=sizeof(wdirname)) { + debuga(_("Temporary file name too long: %s/%s.utmp"),tmp,user); + exit(1); } - strcat(wdirname,".utmp"); if((fp_ou=fopen(wdirname,"w"))==NULL){ fprintf(stderr, "SARG: (report) %s: %s\n",text[45],wdirname); exit(1); @@ -455,11 +455,11 @@ static void maketmp_hour(const char *user, const char *dirname, int indexonly) if(indexonly) return; if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return; - strcpy(wdirname,tmp); - strcat(wdirname,"/"); - strcat(wdirname,user); + if (snprintf(wdirname,sizeof(wdirname),"%s/%s.htmp",tmp,user)>=sizeof(wdirname)) { + debuga(_("Temporary file name too long: %s/%s.htmp"),tmp,user); + exit(1); + } - strcat(wdirname,".htmp"); if((fp_ou=fopen(wdirname,"w"))==NULL){ fprintf(stderr, "SARG: (report-1) %s: %s - %s\n",text[45],wdirname,strerror(errno)); exit(1); @@ -480,7 +480,7 @@ void gravatmp(const char *oldaccuser, const char *oldurl, long long int nacc, lo if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return; if (snprintf(wdirname,sizeof(wdirname),"%s/%s.utmp",tmp,oldaccuser)>=sizeof(wdirname)) { - debuga(_("Path too long %s/%s.utmp"),tmp,oldaccuser); + debuga(_("Temporary file name too long: %s/%s.utmp"),tmp,oldaccuser); exit(1); } @@ -602,6 +602,229 @@ static void gravager(FILE *fp_gen, const char *user, long long int nacc, const c return; } +int ger_read(char *buffer,struct generalitemstruct *item,const char *filename) +{ + int i; + int sign; + long long int number; + + if (strncmp(buffer,"TOTAL\t",6)==0) { + item->total=1; + buffer+=6; + + sign=+1; + if (*buffer == '-') { + buffer++; + sign=-1; + } else if (*buffer == '+') { + buffer++; + } + number=0LL; + while (isdigit(*buffer)) + number=(number * 10) + (*buffer++)-'0'; + if (*buffer!='\t') { + debuga(_("Invalid total number of accesses in %s"),filename); + exit(1); + } + buffer++; + item->nacc=number*sign; + + sign=+1; + if (*buffer == '-') { + buffer++; + sign=-1; + } else if (*buffer == '+') { + buffer++; + } + number=0LL; + while (isdigit(*buffer)) + number=(number * 10) + (*buffer++)-'0'; + if (*buffer!='\t') { + debuga(_("Invalid total size in %s"),filename); + exit(1); + } + buffer++; + item->nbytes=number*sign; + + sign=+1; + if (*buffer == '-') { + buffer++; + sign=-1; + } else if (*buffer == '+') { + buffer++; + } + number=0LL; + while (isdigit(*buffer)) + number=(number * 10) + (*buffer++)-'0'; + if (*buffer!='\t') { + debuga(_("Invalid total elapsed time in %s"),filename); + exit(1); + } + buffer++; + item->nelap=number*sign; + + sign=+1; + if (*buffer == '-') { + buffer++; + sign=-1; + } else if (*buffer == '+') { + buffer++; + } + number=0LL; + while (isdigit(*buffer)) + number=(number * 10) + (*buffer++)-'0'; + if (*buffer!='\t') { + debuga(_("Invalid total cache hit in %s"),filename); + exit(1); + } + buffer++; + item->incache=number*sign; + + sign=+1; + if (*buffer == '-') { + buffer++; + sign=-1; + } else if (*buffer == '+') { + buffer++; + } + number=0LL; + while (isdigit(*buffer)) + number=(number * 10) + (*buffer++)-'0'; + if (*buffer!='\0') { + debuga(_("Invalid total cache miss in %s"),filename); + exit(1); + } + item->oucache=number*sign; + } else { + item->total=0; + + item->user=buffer; + for (i=0 ; i=' ' ; i++) buffer++; + if (*buffer!='\t') { + debuga(_("User name too long or invalid in %s"),filename); + exit(1); + } + *buffer++='\0'; + + sign=+1; + if (*buffer == '-') { + buffer++; + sign=-1; + } else if (*buffer == '+') { + buffer++; + } + number=0LL; + while (isdigit(*buffer)) + number=(number * 10) + (*buffer++)-'0'; + if (*buffer!='\t') { + debuga(_("Invalid number of accesses in %s"),filename); + exit(1); + } + buffer++; + item->nacc=number*sign; + + sign=+1; + if (*buffer == '-') { + buffer++; + sign=-1; + } else if (*buffer == '+') { + buffer++; + } + number=0LL; + while (isdigit(*buffer)) + number=(number * 10) + (*buffer++)-'0'; + if (*buffer!='\t') { + debuga(_("Invalid number of bytes in %s"),filename); + exit(1); + } + buffer++; + item->nbytes=number*sign; + + item->url=buffer; + while ((unsigned char)*buffer>=' ') buffer++; + if (*buffer!='\t') { + debuga(_("URL too long or invalid in %s"),filename); + exit(1); + } + *buffer++='\0'; + + item->ip=buffer; + for (i=0 ; i=' ' ; i++) buffer++; + if (*buffer!='\t') { + debuga(_("IP address too long or invalid in %s"),filename); + exit(1); + } + *buffer++='\0'; + + item->time=buffer; + for (i=0 ; i=' ' ; i++) buffer++; + if (*buffer!='\t') { + debuga(_("Time too long or invalid in %s"),filename); + exit(1); + } + *buffer++='\0'; + + item->date=buffer; + for (i=0 ; i=' ' ; i++) buffer++; + if (*buffer!='\t') { + debuga(_("Date too long or invalid in %s"),filename); + exit(1); + } + *buffer++='\0'; + + sign=+1; + if (*buffer == '-') { + buffer++; + sign=-1; + } else if (*buffer == '+') { + buffer++; + } + number=0LL; + while (isdigit(*buffer)) + number=(number * 10) + (*buffer++)-'0'; + if (*buffer!='\t') { + debuga(_("Invalid elapsed time in %s"),filename); + exit(1); + } + buffer++; + item->nelap=number*sign; + + sign=+1; + if (*buffer == '-') { + buffer++; + sign=-1; + } else if (*buffer == '+') { + buffer++; + } + number=0LL; + while (isdigit(*buffer)) + number=(number * 10) + (*buffer++)-'0'; + if (*buffer!='\t') { + debuga(_("Invalid cache hit size in %s"),filename); + exit(1); + } + buffer++; + item->incache=number*sign; + + sign=+1; + if (*buffer == '-') { + buffer++; + sign=-1; + } else if (*buffer == '+') { + buffer++; + } + number=0LL; + while (isdigit(*buffer)) + number=(number * 10) + (*buffer++)-'0'; + if (*buffer!='\0') { + debuga(_("Invalid cache miss size in %s"),filename); + exit(1); + } + item->oucache=number*sign; + } + return(0); +} + static void grava_SmartFilter(const char *dirname, const char *user, const char *ip, const char *data, const char *hora, const char *url, const char *smart) { diff --git a/siteuser.c b/siteuser.c index d7ac77a..1959773 100644 --- a/siteuser.c +++ b/siteuser.c @@ -33,8 +33,6 @@ void siteuser(void) FILE *fp_in, *fp_ou; char *buf; - char user[MAXLEN]; - char *url; char wuser[MAXLEN]; char *ourl; char csort[255]; @@ -49,12 +47,10 @@ void siteuser(void) int ourl_size; int url_len; char *users; - long long int nbytes; long long int obytes; - long long int nacc; int cstatus; - struct getwordstruct gwarea; struct longlinestruct line; + struct generalitemstruct item; if(Privacy) return; @@ -108,7 +104,6 @@ void siteuser(void) else fprintf(fp_ou,"\n",text[100],text[91],text[103]); - user[0]='\0'; ourl=NULL; ourl_size=0; obytes=0; @@ -125,53 +120,20 @@ void siteuser(void) } while((buf=longline_read(fp_in,&line))!=NULL) { - getword_start(&gwarea,buf); - if (getword(user,sizeof(user),&gwarea,'\t')<0) { - printf("SARG: Maybe you have an invalid user in the %s file of the siteuser.\n",general2); - exit(1); - } - if(strcmp(user,"TOTAL") == 0) - continue; + ger_read(buf,&item,general2); + if(item.total) continue; if(userip) - fixip(user); + fixip(item.user); - user_find(name, sizeof(name), user); + user_find(name, sizeof(name), item.user); if(dotinuser && strchr(name,'_')) { subs(name,sizeof(name),"_","."); } - /* - In fact, even the first call is unecessary as the resolved user variable is never used. - if(Ip2Name) - ip2name(user,sizeof(user)); - */ - - /* - Is this redundant ip2name a mistake or is it really necessary ? It definitely slow down sarg - if the first ip2name succeed because it will try to resolve a name which is not an IP but if - the first ip2name failed, the second attempt may find the expected address which was resolved - in the mean time by the DNS server. - if(Ip2Name) - ip2name(user,sizeof(user)); - */ - - if (getword_atoll(&nacc,&gwarea,'\t')<0){ - printf("SARG: Maybe you have an invalid number of access in your %s file of the siteuser.\n",general2); - exit(1); - } - if (nacc > 0) nsitesusers = 1; - if (getword_atoll(&nbytes,&gwarea,'\t')<0){ - printf("SARG: Maybe you have an invalid number of bytes in your %s file of the siteuser.\n",general2); - exit(1); - } - if (getword_ptr(buf,&url,&gwarea,'\t')<0) { - debuga(_("Maybe you have an invalid url in your %s file of the siteuser"),general2); - exit(1); - } - + if (item.nacc > 0) nsitesusers = 1; if(!regs) { - url_len=strlen(url); + url_len=strlen(item.url); if (!ourl || url_len>=ourl_size) { ourl_size=url_len+1; ourl=realloc(ourl,ourl_size); @@ -180,13 +142,13 @@ void siteuser(void) exit(1); } } - strcpy(ourl,url); - obytes=nbytes; + strcpy(ourl,item.url); + obytes=item.nbytes; regs++; } sprintf(wuser," %s ",name); - if(strstr(users,wuser) == 0 && strcmp(url,ourl) == 0) { + if(strstr(users,wuser) == 0 && strcmp(item.url,ourl) == 0) { strcat(users,name); strcat(users," "); ucount++; @@ -201,7 +163,7 @@ void siteuser(void) continue; } - if(strcmp(url,ourl) != 0 && nsitesusers) { + if(strcmp(item.url,ourl) != 0 && nsitesusers) { fprintf(fp_ou,"
%s%s%s
%s%s%s
%d",regs); if(BlockIt[0]!='\0') { fprintf(fp_ou,"=ourl_size) { ourl_size=url_len+1; ourl=realloc(ourl,ourl_size); @@ -231,8 +193,8 @@ void siteuser(void) exit(1); } } - strcpy(ourl,url); - obytes=nbytes; + strcpy(ourl,item.url); + obytes=item.nbytes; } } fclose(fp_in); diff --git a/squidguard_log.c b/squidguard_log.c index f084dd1..e3a044d 100644 --- a/squidguard_log.c +++ b/squidguard_log.c @@ -35,11 +35,12 @@ static void read_log(const char *wentp, FILE *fp_ou) { FILE *fp_in = NULL; char leks[5], sep[2], res[MAXLEN]; - char mon[10], hour[15]; + char mon[20], day[3], year[5], hour[15]; char list[MAXLEN]; char wdata[127]; - int idata=0; char url[MAX_URL_LEN]; + char user[MAX_USER_LEN]; + int idata=0; int i; char *str; struct getwordstruct gwarea; @@ -47,7 +48,7 @@ static void read_log(const char *wentp, FILE *fp_ou) if(debug) { getword_start(&gwarea,text[7]); - if (getword(url,sizeof(url),&gwarea,' ')<0 || getword(href,sizeof(href),&gwarea,' ')<0) { + if (getword(url,sizeof(url),&gwarea,' ')<0 || getword_skip(MAXLEN,&gwarea,' ')<0) { printf("SARG: Maybe you have a broken record or garbage in your %s string.\n",text[7]); exit(1); } @@ -98,35 +99,68 @@ static void read_log(const char *wentp, FILE *fp_ou) getword_start(&gwarea1,SquidGuardLogFormat); leks[0]='\0'; if (getword(leks,sizeof(leks),&gwarea1,'#')<0) { - printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wentp); + debuga(_("Maybe you have a broken record or garbage in your %s file."),wentp); exit(1); } while(strcmp(leks,"end") != 0) { if (getword(leks,sizeof(leks),&gwarea1,'#')<0 || getword(sep,sizeof(sep),&gwarea1,'#')<0) { - printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wentp); + debuga(_("Maybe you have a broken record or garbage in your %s file."),wentp); exit(1); } if(strcmp(leks,"end") != 0) { if (getword(res,sizeof(res),&gwarea,sep[0])<0) { - printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wentp); + debuga(_("Maybe you have a broken record or garbage in your %s file."),wentp); exit(1); } - if(strcmp(leks,"year") == 0) + if(strcmp(leks,"year") == 0) { + if (strlen(res)>=sizeof(year)) { + debuga(_("Year string too long in squidGuard log file %s"),wentp); + exit(1); + } strcpy(year,res); - else if(strcmp(leks,"mon") == 0) + } else if(strcmp(leks,"mon") == 0) { + if (strlen(res)>=sizeof(mon)) { + debuga(_("Month string too long in squidGuard log file %s"),wentp); + exit(1); + } strcpy(mon,res); - else if(strcmp(leks,"day") == 0) + } else if(strcmp(leks,"day") == 0) { + if (strlen(res)>=sizeof(day)) { + debuga(_("Day string too long in squidGuard log file %s"),wentp); + exit(1); + } strcpy(day,res); - else if(strcmp(leks,"hour") == 0) + } else if(strcmp(leks,"hour") == 0) { + if (strlen(res)>=sizeof(hour)) { + debuga(_("Hour string too long in squidGuard log file %s"),wentp); + exit(1); + } strcpy(hour,res); - else if(strcmp(leks,"list") == 0) + } else if(strcmp(leks,"list") == 0) { + if (strlen(res)>=sizeof(list)) { + debuga(_("Banning list name too long in squidGuard log file %s"),wentp); + exit(1); + } strcpy(list,res); - else if(strcmp(leks,"ip") == 0) + } else if(strcmp(leks,"ip") == 0) { + if (strlen(res)>=sizeof(ip)) { + debuga(_("IP address too long in squidGuard log file %s"),wentp); + exit(1); + } strcpy(ip,res); - else if(strcmp(leks,"user") == 0) + } else if(strcmp(leks,"user") == 0) { + if (strlen(res)>=sizeof(user)) { + debuga(_("User ID too long in squidGuard log file %s"),wentp); + exit(1); + } strcpy(user,res); - else if(strcmp(leks,"url") == 0) + } else if(strcmp(leks,"url") == 0) { + if (strlen(res)>=sizeof(url)) { + debuga(_("URL too long in squidGuard log file %s"),wentp); + exit(1); + } strcpy(url,res); + } } } } else { @@ -136,7 +170,7 @@ static void read_log(const char *wentp, FILE *fp_ou) getword_skip(MAXLEN,&gwarea,' ')<0 || getword(url,sizeof(url),&gwarea,' ')<0 || getword(ip,sizeof(ip),&gwarea,'/')<0 || getword_skip(MAXLEN,&gwarea,' ')<0 || getword(user,sizeof(user),&gwarea,' ')<0) { - printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wentp); + debuga(_("Maybe you have a broken record or garbage in your %s file."),wentp); exit(1); } /* @@ -186,6 +220,7 @@ void squidguard_log(void) char logdir[MAXLEN]; char year[10], day[10], mon[10]; char user[MAXLEN]; + char tmp6[MAXLEN]; int y; int cstatus; char *str; diff --git a/topsites.c b/topsites.c index 599041f..a1491cb 100644 --- a/topsites.c +++ b/topsites.c @@ -33,13 +33,8 @@ void topsites(void) FILE *fp_in, *fp_ou; char *buf; - char user[MAXLEN]; char *url; char *ourl=NULL; - char ntemp[255]; - char ttnacc[20]; - char ttnbytes[20]; - char ttntime[20]; char csort[255]; char general[MAXLEN]; char general2[MAXLEN]; @@ -48,8 +43,8 @@ void topsites(void) char sites[MAXLEN]; char report[MAXLEN]; char period[100]; - char sortf[10]; - char sortt[10]; + const char *sortf; + const char *sortt; long long int nacc; long long int nbytes; long long int ntime; @@ -63,6 +58,7 @@ void topsites(void) int ourl_size=0; struct getwordstruct gwarea; struct longlinestruct line; + struct generalitemstruct item; if(Privacy) return; @@ -114,57 +110,11 @@ void topsites(void) } while((buf=longline_read(fp_in,&line))!=NULL) { - getword_start(&gwarea,buf); - if (getword(user,sizeof(user),&gwarea,'\t')<0) { - printf("SARG: Maybe you have an invalid user in your %s file of the topsites.\n",general2); - exit(1); - } - if(strcmp(user,"TOTAL") == 0) { - if (getword(ttnacc,sizeof(ttnacc),&gwarea,'\t')<0) { - printf("SARG: Maybe you have an invalid total number of access in your %s file of the topsites.\n",general2); - exit(1); - } - if (getword(ttnbytes,sizeof(ttnbytes),&gwarea,'\t')<0) { - printf("SARG: Maybe you have an invalid total number of bytes in your %s file of the topsites.\n",general2); - exit(1); - } - if (getword(ttntime,sizeof(ttntime),&gwarea,'\t')<0) { - printf("SARG: Maybe you have an invalid total time in your %s file of the topsites.\n",general2); - exit(1); - } - continue; - } - if (getword_atoll(&nacc,&gwarea,'\t')<0) { - printf("SARG: Maybe you have an invalid number of access in your %s file of the topsites.\n",general2); - exit(1); - } - if (getword_atoll(&nbytes,&gwarea,'\t')<0) { - printf("SARG: Maybe you have an invalid number of bytes in your %s file of the topsites.\n",general2); - exit(1); - } - if (getword_ptr(buf,&url,&gwarea,'\t')<0) { - debuga(_("Maybe you have an invalid url in your %s file of the topsites"),general2); - exit(1); - } - if (getword(ntemp,sizeof(ntemp),&gwarea,'\t')<0) { - printf("SARG: Maybe you have a broken record or garbage in column 5 in your %s file of the topsites.\n",general2); - exit(1); - } - if (getword(ntemp,sizeof(ntemp),&gwarea,'\t')<0) { - printf("SARG: Maybe you have a broken record or garbage in column 6 in your %s file of the topsites.\n",general2); - exit(1); - } - if (getword(ntemp,sizeof(ntemp),&gwarea,'\t')<0) { - printf("SARG: Maybe you have a broken record or garbage in column 7 in your %s file of the topsites.\n",general2); - exit(1); - } - if (getword_atoll(&ntime,&gwarea,'\t')<0) { - printf("SARG: Maybe you have a broken record or garbage in column 8 in your %s file of the topsites.\n",general2); - exit(1); - } + ger_read(buf,&item,general2); + if(item.total) continue; if(!regs) { - url_len=strlen(url); + url_len=strlen(item.url); if (!ourl || url_len>=ourl_size) { ourl_size=url_len+1; ourl=realloc(ourl,ourl_size); @@ -173,13 +123,13 @@ void topsites(void) exit(1); } } - strcpy(ourl,url); + strcpy(ourl,item.url); regs++; } - if(strcmp(url,ourl) != 0) { - fprintf(fp_ou,"%015lld\t%015lld\t%015lld\t%s\n",tnacc,tnbytes,tntime,ourl); - url_len=strlen(url); + if(strcmp(item.url,ourl) != 0) { + fprintf(fp_ou,"%lld\t%lld\t%lld\t%s\n",tnacc,tnbytes,tntime,ourl); + url_len=strlen(item.url); if (url_len>=ourl_size) { ourl_size=url_len+1; ourl=realloc(ourl,ourl_size); @@ -188,22 +138,22 @@ void topsites(void) exit(1); } } - strcpy(ourl,url); + strcpy(ourl,item.url); tnacc=0; tnbytes=0; tntime=0; } - tnacc+=nacc; - tnbytes+=nbytes; - tntime+=ntime; + tnacc+=item.nacc; + tnbytes+=item.nbytes; + tntime+=item.nelap; } fclose(fp_in); unlink(general2); longline_free(&line); if (ourl) { - fprintf(fp_ou,"%015lld\t%015lld\t%015lld\t%s\n",tnacc,tnbytes,tntime,ourl); + fprintf(fp_ou,"%lld\t%lld\t%lld\t%s\n",tnacc,tnbytes,tntime,ourl); free(ourl); } @@ -213,15 +163,19 @@ void topsites(void) strlow(TopsitesSortType); if(strcmp(TopsitesSortField,"connect") == 0) - strcpy(sortf,"1,1"); - if(strcmp(TopsitesSortField,"bytes") == 0) - strcpy(sortf,"2,2"); + sortf="-k 1,1 -k 2,2"; + else if(strcmp(TopsitesSortField,"bytes") == 0) + sortf="-k 2,2 -k 1,1"; + else + sortf=""; if(strcmp(TopsitesSortType,"a") == 0) - strcpy(sortt," "); - if(strcmp(TopsitesSortType,"d") == 0) - strcpy(sortt,"-r"); + sortt=""; + else if(strcmp(TopsitesSortType,"d") == 0) + sortt="-r"; + else + sortt=""; - sprintf(csort,"sort %s -k %s -o \"%s\" \"%s\"",sortt,sortf,sites,general3); + sprintf(csort,"sort %s -n %s -o \"%s\" \"%s\"",sortt,sortf,sites,general3); cstatus=system(csort); if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus)); diff --git a/topuser.c b/topuser.c index b23cc79..52c08cc 100644 --- a/topuser.c +++ b/topuser.c @@ -41,10 +41,9 @@ void topuser(void) double perc2=0.00; double inperc=0.00, ouperc=0.00; int posicao=0; - char olduser[MAXLEN], csort[MAXLEN], period[MAXLEN], arqper[MAXLEN]; + char olduser[MAX_USER_LEN], csort[MAXLEN], period[MAXLEN], arqper[MAXLEN]; char wger[MAXLEN], top1[MAXLEN], top2[MAXLEN], top3[MAXLEN]; - char user[MAXLEN], tusr[MAXLEN]; - char ip[MAXLEN], time[30], date[30]; + char user[MAX_USER_LEN], tusr[MAXLEN]; char ipantes[MAXLEN], nameantes[MAXLEN]; char sfield[10]="2,2"; char order[255]="-r"; @@ -60,6 +59,7 @@ void topuser(void) int cstatus; struct getwordstruct gwarea; struct longlinestruct line; + struct generalitemstruct item; ipantes[0]='\0'; nameantes[0]='\0'; @@ -110,51 +110,9 @@ void topuser(void) } while((warea=longline_read(fp_in,&line))!=NULL) { - getword_start(&gwarea,warea); - if (getword(user,sizeof(user),&gwarea,'\t')<0) { - debuga(_("Maybe you have a broken user in your %s file"),wger); - exit(1); - } - if(strcmp(user,"TOTAL") == 0) { - continue; - } - if (getword_atoll(&nacc,&gwarea,'\t')<0) { - debuga(_("Maybe you have a broken number of access in your %s file"),wger); - exit(1); - } - if (getword_atoll(&nbytes,&gwarea,'\t')<0) { - debuga(_("Maybe you have a broken number of bytes in your %s file"),wger); - exit(1); - } - if (getword_ptr(NULL,NULL,&gwarea,'\t')<0) { - debuga(_("Maybe you have a broken url in your %s file"),wger); - exit(1); - } - if (getword(ip,sizeof(ip),&gwarea,'\t')<0) { - debuga(_("Maybe you have a broken user's IP in your %s file"),wger); - exit(1); - } - if (getword(time,sizeof(time),&gwarea,'\t')<0) { - debuga(_("Maybe you have a broken time in your %s file"),wger); - exit(1); - } - if (getword(date,sizeof(date),&gwarea,'\t')<0) { - debuga(_("Maybe you have a broken date in your %s file"),wger); - exit(1); - } - if (getword_atoll(&elap,&gwarea,'\t')<0) { - debuga(_("Maybe you have a broken download duration in your %s file"),wger); - exit(1); - } - if (getword_atoll(&incac,&gwarea,'\t')<0) { - debuga(_("Maybe you have a broken in cache download in your %s file"),wger); - exit(1); - } - if (getword_atoll(&oucac,&gwarea,'\n')<0) { - debuga(_("Maybe you have a broken not in cache download in your %s file"),wger); - exit(1); - } - if(strcmp(olduser,user) != 0) { + ger_read(warea,&item,wger); + if(item.total) continue; + if(strcmp(olduser,item.user) != 0) { totuser++; if (olduser[0] != '\0') { @@ -166,7 +124,7 @@ void topuser(void) ttnincache+=tnincache; ttnoucache+=tnoucache; } - strcpy(olduser,user); + strcpy(olduser,item.user); tnbytes=0; tnacc=0; tnelap=0; @@ -174,11 +132,11 @@ void topuser(void) tnoucache=0; } - tnbytes+=nbytes; - tnacc+=nacc; - tnelap+=elap; - tnincache+=incac; - tnoucache+=oucac; + tnbytes+=item.nbytes; + tnacc+=item.nacc; + tnelap+=item.nelap; + tnincache+=item.incache; + tnoucache+=item.oucache; } fclose(fp_in); longline_free(&line); diff --git a/totger.c b/totger.c index 177fb95..283825c 100644 --- a/totger.c +++ b/totger.c @@ -34,14 +34,10 @@ int totalger(const char *dirname, int debug, const char *outdir) long long int tnbytes=0; long long int telap=0; long long int tincache=0, toucache=0; - long long int nacc, nbytes; - long long int elap; - long long int incac, oucac; - char wger[MAXLEN], user[MAXLEN]; - char ip[MAXLEN], hora[9], data[15]; + char wger[MAXLEN]; char *warea; - struct getwordstruct gwarea; struct longlinestruct line; + struct generalitemstruct item; strcpy(wger,dirname); strcat(wger,"/sarg-general"); @@ -58,67 +54,23 @@ int totalger(const char *dirname, int debug, const char *outdir) while((warea=longline_read(fp_in,&line))!=NULL) { - //printf("%s\n",warea); - getword_start(&gwarea,warea); - if (getword(user,sizeof(user),&gwarea,'\t')<0) { - printf("SARG: Maybe you have a broken user in your %s file.\n",wger); - exit(1); - } - if (getword_atoll(&nacc,&gwarea,'\t')<0) { - printf("SARG: Maybe you have a broken number of access in your %s file.\n",wger); - exit(1); - } - if (getword_atoll(&nbytes,&gwarea,'\t')<0) { - printf("SARG: Maybe you have a broken number of bytes in your %s file.\n",wger); - exit(1); - } - if (getword_ptr(NULL,NULL,&gwarea,'\t')<0) { - debuga(_("Maybe you have a broken url in your %s file"),wger); - exit(1); - } - if (getword(ip,sizeof(ip),&gwarea,'\t')<0) { - printf("SARG: Maybe you have a broken source IP address in your %s file.\n",wger); - exit(1); - } - if (getword(hora,sizeof(hora),&gwarea,'\t')<0) { - printf("SARG: Maybe you have a broken time in your %s file.\n",wger); - exit(1); - } - if (getword(data,sizeof(data),&gwarea,'\t')<0) { - printf("SARG: Maybe you have a broken date in your %s file.\n",wger); - exit(1); - } - if (getword_atoll(&elap,&gwarea,'\t')<0) { - printf("SARG: Maybe you have a broken elapsed time in your %s file.\n",wger); - exit(1); - } - if (getword_atoll(&incac,&gwarea,'\t')<0) { - printf("SARG: Maybe you have a broken in cache column in your %s file.\n",wger); - exit(1); - } - if (getword_atoll(&oucac,&gwarea,'\n')<0) { - printf("SARG: Maybe you have a broken not in cache column in your %s file.\n",wger); - exit(1); - } - tnacc+=nacc; - tnbytes+=nbytes; - telap+=elap; - tincache+=incac; - toucache+=oucac; + ger_read(warea,&item,wger); + tnacc+=item.nacc; + tnbytes+=item.nbytes; + telap+=item.nelap; + tincache+=item.incache; + toucache+=item.oucache; } fclose(fp_in); longline_free(&line); - strcpy(wger,dirname); - strcat(wger,"/sarg-general"); - if((fp_ou=fopen(wger,"a"))==NULL) { - fprintf(stderr, "SARG: (totger) %s: %s\n",text[45],wger); - exit(1); + fprintf(stderr, "SARG: (totger) %s: %s\n",text[45],wger); + exit(1); } - fprintf(fp_ou,"TOTAL\t%015lld\t%015lld\t%015lld\t%015lld\t%015lld\n",tnacc,tnbytes,telap,tincache,toucache); + fprintf(fp_ou,"TOTAL\t%lld\t%lld\t%lld\t%lld\t%lld\n",tnacc,tnbytes,telap,tincache,toucache); fclose(fp_ou); return (0); diff --git a/useragent.c b/useragent.c index 563ea81..8d5212a 100644 --- a/useragent.c +++ b/useragent.c @@ -39,7 +39,9 @@ void useragent(void) char hfile[MAXLEN]; char idate[MAXLEN], fdate[MAXLEN]; char tmp2[MAXLEN]; + char tmp3[MAXLEN]; char day[4],month[5],year[5], wdate[20]; + char csort[MAXLEN]; int agentot=0, agentot2=0, agentdif=0, cont=0, nagent; unsigned long totregsl=0; int cstatus; diff --git a/util.c b/util.c index f20e01f..7e963f3 100644 --- a/util.c +++ b/util.c @@ -774,14 +774,15 @@ void obtuser(const char *dirname, const char *name, char *tuser) void obttotal(const char *dirname, const char *name, char *tbytes, const char *tuser, char *media) { FILE *fp_in; - char buf[MAXLEN]; + char *buf; char wdir[MAXLEN]; - char warea[MAXLEN]; + char user[MAX_USER_LEN]; char sep; long long int med=0; long long int wtuser=0; long long int twork=0; struct getwordstruct gwarea; + struct longlinestruct line; twork=0; tbytes[0]='\0'; @@ -795,7 +796,12 @@ void obttotal(const char *dirname, const char *name, char *tbytes, const char *t } } - while(fgets(buf,sizeof(buf),fp_in)!=NULL) { + if (longline_prepare(&line)<0) { + debuga(_("Not enough memory to read the file %s"),wdir); + exit(1); + } + + while((buf=longline_read(fp_in,&line))!=NULL) { if (strncmp(buf,"TOTAL\t",6) == 0) sep='\t'; //new file else if (strncmp(buf,"TOTAL ",6) == 0) @@ -803,24 +809,25 @@ void obttotal(const char *dirname, const char *name, char *tbytes, const char *t else continue; getword_start(&gwarea,buf); - if (getword(warea,sizeof(warea),&gwarea,sep)<0) { - printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdir); + if (getword(user,sizeof(user),&gwarea,sep)<0) { + debuga(_("Maybe you have a invalid user in your %s file"),wdir); exit(1); } if(strcmp(warea,"TOTAL") != 0) continue; if (getword_skip(MAXLEN,&gwarea,sep)<0) { - printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdir); + debuga(_("Maybe you have a broken record or garbage in your %s file"),wdir); exit(1); } if (getword_atoll(&twork,&gwarea,sep)<0) { - printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdir); + debuga(_("Maybe you have a broken record or garbage in your %s file"),wdir); exit(1); } strcpy(tbytes,fixnum(twork,1)); break; } fclose(fp_in); + longline_free(&line); wtuser=my_atoll(tuser); if(wtuser <= 0) { @@ -1261,7 +1268,7 @@ void removetmp(const char *outdir) exit(1); } while(fgets(buf,sizeof(buf),fp_in)!=NULL) { - if(strncmp(buf,"TOTAL",5) == 0) + if(strncmp(buf,"TOTAL",5) == 0 && (buf[6]=='\t' || buf[6]==' ')) break; } fclose(fp_in);