From: Frédéric Marchal Date: Tue, 9 Feb 2010 18:20:01 +0000 (+0000) Subject: Accept long URLs up to 40000 bytes X-Git-Tag: v2.3-pre2~96 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ac422f9bbd4b9d4b9e1c0fbde323176765cc93ec;p=thirdparty%2Fsarg.git Accept long URLs up to 40000 bytes --- diff --git a/authfail.c b/authfail.c index 3f3543f..856723f 100644 --- a/authfail.c +++ b/authfail.c @@ -32,7 +32,8 @@ void authfail_report(void) FILE *fp_in = NULL, *fp_ou = NULL; - char url[MAXLEN]; + char *buf; + char url[MAX_URL_LEN]; char authfail_in[MAXLEN]; char per[MAXLEN]; char report[MAXLEN]; @@ -49,6 +50,7 @@ void authfail_report(void) int count=0; int cstatus; struct getwordstruct gwarea; + struct longlinestruct line; if(DataFile[0] != '\0') return; @@ -105,7 +107,12 @@ void authfail_report(void) fputs("\n",fp_ou); fprintf(fp_ou,"%s%s%s%s\n",text[98],text[111],text[110],text[91]); - while(fgets(buf,sizeof(buf),fp_in)!=NULL) { + if (longline_prepare(&line)<0) { + debuga(_("Not enough memory to read file %s"),authfail_in); + exit(1); + } + + while((buf=longline_read(fp_in,&line))!=NULL) { getword_start(&gwarea,buf); if (getword(data,sizeof(data),&gwarea,'\t')<0 || getword(hora,sizeof(hora),&gwarea,'\t')<0 || getword(user,sizeof(user),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0 || @@ -154,9 +161,20 @@ void authfail_report(void) continue; } - fprintf(fp_ou,"%s%s%s-%s%s%s\n",name,ip,data,hora,BlockImage,url,url); + fprintf(fp_ou,"%s%s%s-%s",name,ip,data,hora); + if(BlockIt[0]!='\0') { + fprintf(fp_ou," ",fp_ou); + } + fputs("",fp_ou); + output_html_string(fp_ou,url,100); + fputs("\n",fp_ou); } fclose(fp_in); + longline_free(&line); fputs("\n",fp_ou); write_html_trailer(fp_ou); diff --git a/btree_cache.c b/btree_cache.c index 719c381..03cfd41 100644 --- a/btree_cache.c +++ b/btree_cache.c @@ -1,10 +1,11 @@ /* - * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com - * 1998, 2009 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net + * 1998, 2010 * * SARG donations: * please look at http://sarg.sourceforge.net/donations.php + * Support: + * http://sourceforge.net/projects/sarg/forums/forum/363374 * --------------------------------------------------------------------- * * This program is free software; you can redistribute it and/or modify diff --git a/dansguardian_log.c b/dansguardian_log.c index 0595aa0..c16e955 100644 --- a/dansguardian_log.c +++ b/dansguardian_log.c @@ -39,6 +39,7 @@ void dansguardian_log(void) char user[MAXLEN], code1[255], code2[255]; char ip[30]; char wdata[127]; + char url[MAX_URL_LEN]; int idata=0; int cstatus; struct getwordstruct gwarea; @@ -109,11 +110,11 @@ void dansguardian_log(void) if(debug) { getword_start(&gwarea,text[7]); - if (getword(urly,sizeof(urly),&gwarea,' ')<0 || getword(href,sizeof(href),&gwarea,' ')<0) { + if (getword(url,sizeof(url),&gwarea,' ')<0 || getword(href,sizeof(href),&gwarea,' ')<0) { printf("SARG: Maybe you have a broken record or garbage in your %s entry.\n",text[7]); exit(1); } - debuga("%s DansGuardian %s: %s",urly,gwarea.current,loglocation); + debuga("%s DansGuardian %s: %s",url,gwarea.current,loglocation); } if((fp_in=MY_FOPEN(loglocation,"r"))==NULL) { diff --git a/denied.c b/denied.c index 4918acb..9315b86 100644 --- a/denied.c +++ b/denied.c @@ -32,7 +32,8 @@ void gen_denied_report(void) FILE *fp_in = NULL, *fp_ou = NULL; - char url[MAXLEN]; + char *buf; + char url[MAX_URL_LEN]; char denied_in[MAXLEN]; char per[MAXLEN]; char report[MAXLEN]; @@ -48,6 +49,7 @@ void gen_denied_report(void) int z=0; int count=0; struct getwordstruct gwarea; + struct longlinestruct line; ouser[0]='\0'; ouser2[0]='\0'; @@ -91,7 +93,12 @@ void gen_denied_report(void) fputs("\n",fp_ou); fprintf(fp_ou,"%s%s%s%s\n",text[98],text[111],text[110],text[91]); - while(fgets(buf,sizeof(buf),fp_in)!=NULL) { + if (longline_prepare(&line)<0) { + debuga(_("Not enough memory to read the denied accesses")); + exit(1); + } + + while((buf=longline_read(fp_in,&line))!=NULL) { getword_start(&gwarea,buf); if (getword(data,sizeof(data),&gwarea,'\t')<0 || getword(hora,sizeof(hora),&gwarea,'\t')<0 || getword(user,sizeof(user),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0 || @@ -140,13 +147,20 @@ void gen_denied_report(void) continue; } - if(BlockIt[0] != '\0') - sprintf(BlockImage," ",wwwDocumentRoot,BlockIt,url,ImageFile); - else BlockImage[0]='\0'; - - fprintf(fp_ou,"%s%s%s-%s%s%s\n",name,ip,data,hora,BlockImage,url,url); + fprintf(fp_ou,"%s%s%s-%s",name,ip,data,hora); + if(BlockIt[0] != '\0') { + fprintf(fp_ou," ",ImageFile); + } + fputs("",fp_ou); + output_html_string(fp_ou,url,100); + fputs("\n",fp_ou); } fclose(fp_in); + longline_free(&line); fputs("\n",fp_ou); write_html_trailer(fp_ou); diff --git a/documentation/util.txt b/documentation/util.txt index 36e8d26..d63bad0 100644 --- a/documentation/util.txt +++ b/documentation/util.txt @@ -6,6 +6,13 @@ +/*! \def INITIAL_LINE_BUFFER_SIZE +The initial buffer size to allocate to read a long line from a text file. +*/ + + + + /*! \var static char mtab1[12][4]; The list of the months. */ @@ -798,11 +805,27 @@ is written in a centered table. -/*! \fn void output_html_string(FILE *fp_ou,const char *str) + +/*! \fn void output_html_string(FILE *fp_ou,const char *str,int maxlen) Write a string in a file and replace the problematic ASCII characters by their equivalent HTML entities. \param fp_ou The handle of the output file. \param str The string to output. +\param maxlen The maximum number of bytes to write from the string. Set to zero to have no limit. + +If the string is longer than the requested length, only the requested number of bytes are output and +the string is truncated and ended by …. +*/ + + + + + +/*! \fn void output_html_url(FILE *fp_ou,const char *url) +Write an URL into the file and replace any & by &. + +\param fp_ou The handle of the output file. +\param url The URL to output. */ @@ -816,3 +839,53 @@ Delete a directory and its content. \param contentonly \c True to delete only the content of the directory and leave the directory itself in place. If set to \c zero, the directory is removed too. */ + + + + + +/*! \fn int longline_prepare(struct longlinestruct *line) +Prepare the buffer to read long text lines from a file. + +The memory allocated by this function must be freed with a call to +longline_free(). + +\param line The buffer to initialize. + +\retval 0 No error. +\retval -1 Not enough memory. +*/ + + + + + +/*! \fn char *longline_read(FILE *fp_in,struct longlinestruct *line) +Read one long line of text from a file. If the buffer is too short, it is +expended until the line can fit in it. + +The function always read as many bytes as can fit in the buffer and split the lines +to return one line at a time to the caller. The returned lines are always terminated +by a null ASCII character. The CR or LF are removed. + +Any empty line is skipped. + +\param fp_in The file to read. +\param line The buffer initialized by longline_preapre(). + +\return A pointer to the beginning of the string in the buffer or NULL if it is +the last string read. + +\note If not enough memory is available to read the line, the program is terminated +with an error message. +*/ + + + + + +/*! \fn void longline_free(struct longlinestruct *line) +Free the memory allocated by longline_prepare(). + +\param line The buffer to free. +*/ diff --git a/download.c b/download.c index 5711f3f..1018ded 100644 --- a/download.c +++ b/download.c @@ -36,7 +36,8 @@ void download_report(void) FILE *fp_in = NULL, *fp_ou = NULL; - char url[MAXLEN]; + char *buf; + char url[MAX_URL_LEN]; char report_in[MAXLEN]; char wdirname[MAXLEN]; char report[MAXLEN]; @@ -53,6 +54,7 @@ void download_report(void) int count=0; int i; struct getwordstruct gwarea; + struct longlinestruct line; ouser[0]='\0'; ouser2[0]='\0'; @@ -96,7 +98,12 @@ void download_report(void) fputs("\n",fp_ou); fprintf(fp_ou,"%s%s%s%s\n",text[98],text[111],text[110],text[91]); - while(fgets(buf,sizeof(buf),fp_in)!=NULL) { + if (longline_prepare(&line)<0) { + debuga(_("Not enough memory to read the downloaded files")); + exit(1); + } + + while((buf=longline_read(fp_in,&line))!=NULL) { getword_start(&gwarea,buf); if (getword(data,sizeof(data),&gwarea,'\t')<0 || getword(hora,sizeof(hora),&gwarea,'\t')<0 || getword(user,sizeof(user),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0 || @@ -148,13 +155,19 @@ void download_report(void) for (i=strlen(url)-1 ; i>=0 && (unsigned char)url[i]<' ' ; i--) url[i]=0; fprintf(fp_ou,"%s%s%s-%s",name,ip,data,hora); - if(BlockIt[0]!='\0') - fprintf(fp_ou," ",wwwDocumentRoot,BlockIt,url,ImageFile); - fprintf(fp_ou,"",url); - output_html_string(fp_ou,url); + if(BlockIt[0]!='\0') { + fprintf(fp_ou," ",ImageFile); + } + fputs("",fp_ou); + output_html_string(fp_ou,url,100); fputs("\n",fp_ou); } fclose(fp_in); + longline_free(&line); fputs("\n",fp_ou); write_html_trailer(fp_ou); diff --git a/html.c b/html.c index a7c61cc..53d498f 100644 --- a/html.c +++ b/html.c @@ -39,8 +39,9 @@ void htmlrel(void) long long int ltemp; long long int ntotuser; long long int userbytes, userelap; + char *linebuf; char arqin[MAXLEN], arqou[MAXLEN], arqper[MAXLEN], arqip[MAXLEN]; - char url[MAXLEN], tmsg[50], csort[MAXLEN]; + char url[MAX_URL_LEN], tmsg[50], csort[MAXLEN]; char period[MAXLEN], usuario[MAXLEN], wusuario[MAXLEN], u2[MAXLEN], duser[MAXLEN]; char userurl[1024], userhora[9], userdia[9]; char user_ip[MAXLEN], olduserip[MAXLEN], tmp2[MAXLEN], tmp3[MAXLEN]; @@ -48,6 +49,7 @@ void htmlrel(void) char *str; char warea[MAXLEN]; char totuser[8]; + char ltext110[100]; long long int tnacc=0, ttnacc=0, unacc=0; double perc=0, perc2=0, ouperc=0, inperc=0; char *s; @@ -55,9 +57,10 @@ void htmlrel(void) int cstatus; const char txtext[]=".txt"; int dlen; - char urly[MAXLEN]; - char siteind[MAXLEN]; + char urly[MAX_URL_LEN]; + char siteind[MAX_URL_LEN]; struct getwordstruct gwarea; + struct longlinestruct line; if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return; @@ -193,13 +196,18 @@ void htmlrel(void) exit(1); } + if (longline_prepare(&line)<0) { + debuga(_("Not enough memory to read file %s"),arqin); + exit(1); + } + tnacc=0; tnbytes=0; tnelap=0; tnincache=0; tnoucache=0; - while(fgets(buf,sizeof(buf),fp_in)!=NULL) { - getword_start(&gwarea,buf); + while((linebuf=longline_read(fp_in,&line))!=NULL) { + getword_start(&gwarea,linebuf); if (getword_atoll(<emp,&gwarea,'\t')<0) { printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arqin); exit(1); @@ -210,8 +218,15 @@ void htmlrel(void) exit(1); } tnbytes+=ltemp; - if (getword_skip(MAXLEN,&gwarea,'\t')<0 || getword_skip(MAXLEN,&gwarea,'\t')<0 || - getword_atoll(<emp,&gwarea,'\t')<0) { + if (getword_skip(MAX_URL_LEN,&gwarea,'\t')<0) { + printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arqin); + exit(1); + } + if (getword_skip(MAXLEN,&gwarea,'\t')<0) { + printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arqin); + exit(1); + } + if (getword_atoll(<emp,&gwarea,'\t')<0) { printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arqin); exit(1); } @@ -277,8 +292,8 @@ void htmlrel(void) debuga("%s: %s",text[61],usuario); } - while(fgets(buf,sizeof(buf),fp_in)!=NULL) { - getword_start(&gwarea,buf); + while((linebuf=longline_read(fp_in,&line))!=NULL) { + getword_start(&gwarea,linebuf); if (getword_atoll(&twork,&gwarea,'\t')<0) { printf("SARG: Maybe you have a broken number of access in your %s file.\n",arqin); exit(1); @@ -326,15 +341,16 @@ void htmlrel(void) str=strchr(url,'/'); if (str){ *str='\0'; - strcat(url,"..."); + strcat(url,"…"); strcat(url,module); } } if((ReportType & REPORT_TYPE_SITE_USER_TIME_DATE) != 0) { - strcpy(ltext110,text[110]); - for(s=ltext110; *s; ++s) - *s=tolower(*s); + s=text[110]; + for(z1=0 ; z10) { + for (i=0 ; i0 && bufz[blen-1]!='\r' && bufz[blen-1]!='\n' && !feof(fp_in)) { - fprintf(stderr,"SARG: line too long (more than %d bytes) in %s\n",sizeof(bufz)-1,arq); - exit(1); - } + line.start=0; + line.end=0; + line.length=0; + + while ((linebuf=longline_read(fp_in,&line))!=NULL) { + blen=strlen(linebuf); if (ilf==ILF_Unknown) { - if(strncmp(bufz,"#Software: Mic",14) == 0) { - fixendofline(bufz); + if(strncmp(linebuf,"#Software: Mic",14) == 0) { + fixendofline(linebuf); if (debug) - debuga("%s: %s",text[143],bufz); + debuga("%s: %s",text[143],linebuf); ilf=ILF_Isa; ilf_count[ilf]++; continue; } - if(strncmp(bufz,"*** SARG Log ***",16) == 0) { + if(strncmp(linebuf,"*** SARG Log ***",16) == 0) { getword_start(&gwarea,arqtt); if (getword_skip(2000,&gwarea,'-')<0 || getword(val2,sizeof(val2),&gwarea,'_')<0 || getword_skip(10,&gwarea,'-')<0 || getword(val3,sizeof(val3),&gwarea,'_')<0) { @@ -798,12 +819,12 @@ int main(int argc,char *argv[]) OutputNonZero = REPORT_EVERY_X_LINES ; } if(blen < 58) continue; - if(strstr(bufz,"HTTP/0.0") != 0) continue; - if(strstr(bufz,"logfile turned over") != 0) continue; - if(bufz[0] == ' ') continue; + if(strstr(linebuf,"HTTP/0.0") != 0) continue; + if(strstr(linebuf,"logfile turned over") != 0) continue; + if(linebuf[0] == ' ') continue; // Record only hours usage which is required - tt = (time_t) strtoul( bufz, NULL, 10 ); + tt = (time_t) strtoul( linebuf, NULL, 10 ); t = localtime( &tt ); if( bsearch( &( t -> tm_wday ), weekdays.list, weekdays.len, @@ -823,23 +844,23 @@ int main(int argc,char *argv[]) printf("SARG: Maybe you have a broken record or garbage in your exclusion string.\n"); exit(1); } - if((str=(char *) strstr(bufz,val1)) != (char *) NULL ) + if((str=(char *) strstr(linebuf,val1)) != (char *) NULL ) exstring++; } - if((str=(char *) strstr(bufz,gwarea.current)) != (char *) NULL ) + if((str=(char *) strstr(linebuf,gwarea.current)) != (char *) NULL ) exstring++; } if(exstring) continue; - if ((str = strchr(bufz, '\n')) != NULL) + if ((str = strchr(linebuf, '\n')) != NULL) *str = '\0'; /* strip \n */ totregsl++; if(debugm) - printf("BUF=%s\n",bufz); + printf("BUF=%s\n",linebuf); if (ilf==ILF_Squid || ilf==ILF_Common || ilf==ILF_Unknown) { - getword_start(&gwarea,bufz); + getword_start(&gwarea,linebuf); if (getword(data,sizeof(data),&gwarea,' ')<0) { printf("SARG: Maybe you have a broken time in your access.log file.\n"); exit(1); @@ -860,8 +881,15 @@ int main(int argc,char *argv[]) } } if (getword(data,sizeof(data),&gwarea,']')<0 || getword_skip(MAXLEN,&gwarea,'"')<0 || - getword(fun,sizeof(fun),&gwarea,' ')<0 || getword(url,sizeof(url),&gwarea,' ')<0 || - getword_skip(MAXLEN,&gwarea,' ')<0 || getword(code2,sizeof(code2),&gwarea,' ')<0 || + getword(fun,sizeof(fun),&gwarea,' ')<0) { + printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq); + exit(1); + } + if (getword(url,sizeof(url),&gwarea,' ')<0) { + printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq); + exit(1); + } + if (getword_skip(MAXLEN,&gwarea,' ')<0 || getword(code2,sizeof(code2),&gwarea,' ')<0 || getword(tam,sizeof(tam),&gwarea,' ')<0) { printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq); exit(1); @@ -920,10 +948,6 @@ int main(int argc,char *argv[]) printf("SARG: Maybe you have a broken URI in your %s file.\n",arq); exit(1); } -// while (strstr(bufz,"%20") != 0) { -// getword(warea,bufz,' '); -// strcat(url,warea); -// } if (getword(user,sizeof(user),&gwarea,' ')<0){ printf("SARG: Maybe you have a broken user ID in your %s file.\n",arq); exit(1); @@ -933,7 +957,7 @@ int main(int argc,char *argv[]) } } if (ilf==ILF_Sarg) { - getword_start(&gwarea,bufz); + getword_start(&gwarea,linebuf); if (getword(data,sizeof(data),&gwarea,'\t')<0){ printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq); exit(1); @@ -972,11 +996,11 @@ int main(int argc,char *argv[]) } } if (ilf==ILF_Isa) { - if (bufz[0] == '#') { + if (linebuf[0] == '#') { int ncols,cols[ISACOL_Last]; - fixendofline(bufz); - getword_start(&gwarea,bufz); + fixendofline(linebuf); + getword_start(&gwarea,linebuf); // remove the #Fields: column at the beginning of the line if (getword_skip(1000,&gwarea,' ')<0){ printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq); @@ -1007,7 +1031,7 @@ int main(int argc,char *argv[]) continue; } if (!isa_ncols) continue; - getword_start(&gwarea,bufz); + getword_start(&gwarea,linebuf); for (x=0 ; x 255) { - arqtt[255]='\0'; - } + snprintf(arqtt,sizeof(arqtt),"%s/%s/tt%s-%s.html",dirname,accuser,accuser,siteind); if ((fp_tt = fopen(arqtt, "w")) == 0) { fprintf(stderr, "SARG: (report) %s: %s\n",text[45],arqtt); exit(1); @@ -243,10 +254,6 @@ void gerarel(void) sprintf(httplink,"%s",FontSize,accurl,accurl); */ - sprintf(ltext110,"%s",text[110]); - for(str=ltext110; *str; ++str) - *str=tolower(*str); - write_html_header(fp_tt,(IndexTree == INDEX_TREE_DATE) ? 4 : 2,_("Site access report")); fprintf(fp_tt,"%s: %s\n",text[89],period); fprintf(fp_tt,"%s: %s\n",text[90],name); @@ -261,46 +268,46 @@ void gerarel(void) fprintf(fp_tt,"%s%s%s\n",text[91],tmp4,text[110]+5); } - sprintf(html,"%s%s%s\n",accurl,accdia,acchora); + fprintf(fp_tt,"%s%s%s\n",accurl,accdia,acchora); - if(strcmp(html,html_old) != 0) - fputs(html,fp_tt); - strcpy(html_old, html); - } else bzero(ltext110, 50); + strcpy(oldurltt,accurl); + strcpy(oldaccdiatt,accdia); + strcpy(oldacchoratt,acchora); + } strcpy(crc2,acccode); str=strchr(crc2,'/'); if (str) *str='\0'; - if(strstr(crc2,"MISS") != 0) oucache+=accbytes; else incache+=accbytes; - strcpy(oldurl,accurl); - if(strcmp(accuser,oldaccuser) != 0) { strcpy(wdirname,dirname); day_totalize(tmp,oldaccuser,indexonly); + strcpy(oldaccuser,accuser); } - strcpy(oldaccuser,accuser); strcpy(oldacccode,acccode); strcpy(oldaccip,accip); + strcpy(oldurl,accurl); strcpy(oldaccdia,accdia); strcpy(oldacchora,acchora); } - bzero(user,MAXLEN); + bzero(user,sizeof(user)); fclose(fp_in); unlink(tmp3); + longline_free(&line); } closedir(dirp); - strcpy(oldmsg,"OK"); if(strstr(oldacccode,"DENIED") != 0) - sprintf(oldmsg,"%s",text[46]); + strcpy(oldmsg,text[46]); + else + strcpy(oldmsg,"OK"); strcpy(wdirname,dirname); - if(strlen(oldaccuser) == 0) + if(oldaccuser[0] == '\0') strcpy(oldaccuser,accuser); gravatmpf(oldaccuser,wdirname,oldurl,nacc,nbytes,oldmsg,nelap,indexonly,incache,oucache); strcpy(wdirname,dirname); diff --git a/siteuser.c b/siteuser.c index 4f29e77..4de0827 100644 --- a/siteuser.c +++ b/siteuser.c @@ -32,10 +32,11 @@ void siteuser(void) FILE *fp_in, *fp_ou; + char *buf; char user[MAXLEN]; - char url[MAXLEN]; + char url[MAX_URL_LEN]; char wuser[MAXLEN]; - char ourl[MAXLEN]; + char ourl[MAX_URL_LEN]; char csort[255]; char general[MAXLEN]; char general2[MAXLEN]; @@ -51,6 +52,7 @@ void siteuser(void) long long int nacc; int cstatus; struct getwordstruct gwarea; + struct longlinestruct line; if(Privacy) return; @@ -114,7 +116,12 @@ void siteuser(void) } strcpy(users," "); - while(fgets(buf,sizeof(buf),fp_in)!=NULL) { + if (longline_prepare(&line)<0) { + debuga(_("Not enough memory to read file %s"),general2); + exit(1); + } + + 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); @@ -182,16 +189,23 @@ void siteuser(void) continue; } - if(BlockIt[0]!='\0') - sprintf(BlockImage," ",wwwDocumentRoot,BlockIt,ourl); - else BlockImage[0]='\0'; - if(strcmp(url,ourl) != 0 && nsitesusers) { - if(BytesInSitesUsersReport) { - sprintf(wwork2,"%s",fixnum(obytes,1)); - fprintf(fp_ou,"%d%s%s%s%s\n",regs,BlockImage,ourl,ourl,wwork2,users); - } else - fprintf(fp_ou,"%d%s%s%s\n",regs,BlockImage,ourl,ourl,users); + fprintf(fp_ou,"%d",regs); + if(BlockIt[0]!='\0') { + fprintf(fp_ou," ",fp_ou); + } + fputs("",fp_ou); + output_html_string(fp_ou,ourl,100); + fputs("",fp_ou); + + if(BytesInSitesUsersReport) + fprintf(fp_ou,"%s",fixnum(obytes,1)); + fprintf(fp_ou,"%s\n",users); + regs++; ucount=0; strcpy(users,name); @@ -201,9 +215,14 @@ void siteuser(void) } } fclose(fp_in); + longline_free(&line); if(nsitesusers) { - fprintf(fp_ou,"%d%s%s\n",regs,ourl,ourl,users); + fprintf(fp_ou,"%d",fp_ou); + output_html_string(fp_ou,ourl,100); + fprintf(fp_ou,"%s\n",users); } unlink(general2); @@ -216,5 +235,4 @@ void siteuser(void) free(users); return; - } diff --git a/squidguard_log.c b/squidguard_log.c index 1e6b3ba..fe80098 100644 --- a/squidguard_log.c +++ b/squidguard_log.c @@ -39,6 +39,7 @@ static void read_log(const char *wentp, FILE *fp_ou) char list[MAXLEN]; char wdata[127]; int idata=0; + char url[MAX_URL_LEN]; int i; char *str; struct getwordstruct gwarea; @@ -46,11 +47,11 @@ static void read_log(const char *wentp, FILE *fp_ou) if(debug) { getword_start(&gwarea,text[7]); - if (getword(urly,sizeof(urly),&gwarea,' ')<0 || getword(href,sizeof(href),&gwarea,' ')<0) { + if (getword(url,sizeof(url),&gwarea,' ')<0 || getword(href,sizeof(href),&gwarea,' ')<0) { printf("SARG: Maybe you have a broken record or garbage in your %s string.\n",text[7]); exit(1); } - debuga("%s squidGuard %s: %s",urly,gwarea.current,wentp); + debuga("%s squidGuard %s: %s",url,gwarea.current,wentp); } /* With squidGuard, you can log groups in only one log file. diff --git a/topsites.c b/topsites.c index e4d4d81..93a7ae1 100644 --- a/topsites.c +++ b/topsites.c @@ -32,8 +32,9 @@ void topsites(void) FILE *fp_in, *fp_ou; - char url[MAXLEN]; - char ourl[MAXLEN]; + char *buf; + char url[MAX_URL_LEN]; + char ourl[MAX_URL_LEN]; char ntemp[255]; char ttnacc[20]; char ttnbytes[20]; @@ -58,6 +59,7 @@ void topsites(void) int regs=0; int cstatus; struct getwordstruct gwarea; + struct longlinestruct line; if(Privacy) return; @@ -103,7 +105,12 @@ void topsites(void) exit(1); } - while(fgets(buf,sizeof(buf),fp_in)!=NULL) { + if (longline_prepare(&line)<0) { + debuga(_("Not enough memory to read file %s"),general2); + exit(1); + } + + while((buf=longline_read(fp_in,&line))!=NULL) { getword_start(&gwarea,buf); if (getword(url,sizeof(url),&gwarea,'\t')<0) { printf("SARG: Maybe you have an invalid user in your %s file of the topsites.\n",general2); @@ -173,15 +180,16 @@ void topsites(void) tnbytes+=nbytes; tntime+=ntime; } + fclose(fp_in); + unlink(general2); + longline_free(&line); my_lltoa(tnacc,val1,15); my_lltoa(tnbytes,val2,15); my_lltoa(tntime,val3,15); fprintf(fp_ou,"%s\t%s\t%s\t%s\n",val1,val2,val3,ourl); - fclose(fp_in); fclose(fp_ou); - unlink(general2); strlow(TopsitesSortField); strlow(TopsitesSortType); @@ -231,7 +239,12 @@ void topsites(void) regs=0; ntopsites = 0; - while(regs ",wwwDocumentRoot,BlockIt,url); - else BlockImage[0]='\0'; + fprintf(fp_ou,"%d",regs); + if(BlockIt[0] != '\0') { + fprintf(fp_ou," ",fp_ou); + } - fprintf(fp_ou,"%d%s%s%s%s%s\n",regs,BlockImage,url,url,wwork1,wwork2,wwork3); + fputs("",fp_ou); + output_html_string(fp_ou,url,100); + fprintf(fp_ou,"%s%s%s\n",wwork1,wwork2,wwork3); regs++; } fclose(fp_in); + longline_free(&line); fputs("\n",fp_ou); write_html_trailer(fp_ou); diff --git a/topuser.c b/topuser.c index 55058eb..b49ff2d 100644 --- a/topuser.c +++ b/topuser.c @@ -49,14 +49,17 @@ void topuser(void) char sfield[10]="2,2"; char order[255]="-r"; char title[80]; + char *warea; + char user2[MAXLEN]; + char name[MAXLEN]; + char ltext110[100]; int totuser=0; int topcount=0; + int i; char *s; int cstatus; - char warea[MAXLEN]; - char user2[MAXLEN]; - char name[MAXLEN]; struct getwordstruct gwarea; + struct longlinestruct line; ipantes[0]='\0'; nameantes[0]='\0'; @@ -101,7 +104,12 @@ void topuser(void) olduser[0]='\0'; totuser=0; - while(fgets(warea,sizeof(warea),fp_in)) { + if (longline_prepare(&line)<0) { + debuga(_("Not enough memory to read the file %s"),wger); + exit(1); + } + + while((warea=longline_read(fp_in,&line))!=NULL) { 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); @@ -118,7 +126,7 @@ void topuser(void) 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) { + if (getword_skip(MAX_URL_LEN,&gwarea,'\t')<0) { printf("SARG: Maybe you have a broken url in your %s file.\n",wger); exit(1); } @@ -178,6 +186,7 @@ void topuser(void) tnoucache+=oucac; } fclose(fp_in); + longline_free(&line); if (olduser[0] != '\0') { my_lltoa(tnbytes,val1,15); @@ -284,7 +293,12 @@ void topuser(void) ntopuser = 0; - while(fgets(warea,sizeof(warea),fp_top1)) { + if (longline_prepare(&line)<0) { + debuga(_("Not enough memory to read the downloaded files")); + exit(1); + } + + while((warea=longline_read(fp_top1,&line))!=NULL) { 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",top1); @@ -345,9 +359,10 @@ void topuser(void) } if((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (ReportType & REPORT_TYPE_DATE_TIME) != 0) { - sprintf(ltext110,"%s",text[110]); - for(s=ltext110; *s; ++s) - *s=tolower(*s); + s=text[110]; + for(i=0 ; i",fp_top3); #ifdef HAVE_GD if(Graphs) { @@ -397,6 +412,7 @@ void topuser(void) } fclose(fp_top1); unlink(top1); + longline_free(&line); if((TopUserFields & TOPUSERFIELDS_TOTAL) != 0) { fputs("",fp_top3); diff --git a/totger.c b/totger.c index cc98a97..469168b 100644 --- a/totger.c +++ b/totger.c @@ -37,10 +37,11 @@ int totalger(const char *dirname, int debug, const char *outdir) long long int nacc, nbytes; long long int elap; long long int incac, oucac; - char wger[MAXLEN], user[MAXLEN], url[MAXLEN]; + char wger[MAXLEN], user[MAXLEN], url[MAX_URL_LEN]; char ip[MAXLEN], hora[9], data[15]; - char warea[MAXLEN]; + char *warea; struct getwordstruct gwarea; + struct longlinestruct line; strcpy(wger,dirname); strcat(wger,"/sarg-general"); @@ -50,7 +51,12 @@ int totalger(const char *dirname, int debug, const char *outdir) exit(1); } - while(fgets(warea,sizeof(warea),fp_in)) + if (longline_prepare(&line)<0) { + debuga(_("Not enough memory to read the temporary file %s"),wger); + exit(1); + } + + while((warea=longline_read(fp_in,&line))!=NULL) { //printf("%s\n",warea); getword_start(&gwarea,warea); @@ -102,6 +108,7 @@ int totalger(const char *dirname, int debug, const char *outdir) } fclose(fp_in); + longline_free(&line); strcpy(wger,dirname); strcat(wger,"/sarg-general"); diff --git a/usertab.c b/usertab.c index df4d006..3981718 100644 --- a/usertab.c +++ b/usertab.c @@ -1,10 +1,11 @@ /* - * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com - * 1998, 2010 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net + * 1998, 2010 * * SARG donations: * please look at http://sarg.sourceforge.net/donations.php + * Support: + * http://sourceforge.net/projects/sarg/forums/forum/363374 * --------------------------------------------------------------------- * * This program is free software; you can redistribute it and/or modify diff --git a/util.c b/util.c index 3a1e231..86975b5 100644 --- a/util.c +++ b/util.c @@ -31,11 +31,13 @@ #include "include/defs.h" #if defined(HAVE_BACKTRACE) -#define USE_GETWORD_BACKTRACE 0 +#define USE_GETWORD_BACKTRACE 1 #else #define USE_GETWORD_BACKTRACE 0 #endif +#define INITIAL_LINE_BUFFER_SIZE 32768 + static char mtab1[12][4]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}; //! The list of the HTTP codes to exclude from the report. @@ -1466,10 +1468,11 @@ void write_html_trailer(FILE *fp_ou) fputs("\n\n",fp_ou); } -void output_html_string(FILE *fp_ou,const char *str) +void output_html_string(FILE *fp_ou,const char *str,int maxlen) { + int i=0; - while (*str) { + while (*str && (maxlen<=0 || i0 && i>=maxlen) + fputs("…",fp_ou); +} + +void output_html_url(FILE *fp_ou,const char *url) +{ + while (*url) { + if (*url=='&') + fputs("&",fp_ou); + else + fputc(*url,fp_ou); + url++; } } @@ -1604,3 +1621,80 @@ void unlinkdir(const char *dir,int contentonly) } } } + +int longline_prepare(struct longlinestruct *line) +{ + line->size=INITIAL_LINE_BUFFER_SIZE; + line->buffer=malloc(line->size); + if (!line->buffer) + return(-1); + line->start=0; + line->end=0; + line->length=0; + return(0); +} + +char *longline_read(FILE *fp_in,struct longlinestruct *line) +{ + int i; + int skipcr; + char *newbuf; + size_t nread; + + if (!line->buffer) return(NULL); + if (feof(fp_in)) return(NULL); + + line->start=line->end; + skipcr=1; + while (skipcr || (line->buffer[line->end]!='\n' && line->buffer[line->end]!='\r')) { + if (line->end>=line->length) { + if (line->start>0) { + for (i=line->start ; iend ; i++) + line->buffer[i-line->start]=line->buffer[i]; + line->length-=line->start; + line->end-=line->start; + line->start=0; + } + if (line->length>=line->size) { + line->size+=8192; + newbuf=realloc(line->buffer,line->size); + if (!newbuf) { + debuga(_("Not enough memory to read one more line from the input log file")); + exit(1); + } + line->buffer=newbuf; + } + nread=fread(line->buffer+line->end,1,line->size-line->end,fp_in); + if (nread==0) { + if (line->end<=line->start) return(NULL); + if (line->end>=line->size) { + line->size++; + newbuf=realloc(line->buffer,line->size); + if (!newbuf) { + debuga(_("Not enough memory to read one more line from the input log file")); + exit(1); + } + line->buffer=newbuf; + } + line->buffer[line->end]='\0'; + return(line->buffer+line->start); + } + line->length+=nread; + } + if (skipcr && line->buffer[line->end]!='\n' && line->buffer[line->end]!='\r') { + skipcr=0; + line->start=line->end; + } + line->end++; + } + line->buffer[line->end++]='\0'; + return(line->buffer+line->start); +} + +void longline_free(struct longlinestruct *line) +{ + if (line->buffer) { + free(line->buffer); + line->buffer=NULL; + } +}