]> git.ipfire.org Git - thirdparty/sarg.git/commitdiff
Accept long URLs up to 40000 bytes
authorFrédéric Marchal <fmarchal@users.sourceforge.net>
Tue, 9 Feb 2010 18:20:01 +0000 (18:20 +0000)
committerFrédéric Marchal <fmarchal@users.sourceforge.net>
Tue, 9 Feb 2010 18:20:01 +0000 (18:20 +0000)
19 files changed:
authfail.c
btree_cache.c
dansguardian_log.c
denied.c
documentation/util.txt
download.c
html.c
include/conf.h
include/defs.h
log.c
realtime.c
report.c
siteuser.c
squidguard_log.c
topsites.c
topuser.c
totger.c
usertab.c
util.c

index 3f3543f2037c29224c432e9ded02bb18218cb759..856723fe5c3b3d1898224469b4fce0ecf3b92a52 100644 (file)
@@ -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("<tr><td></td></tr>\n",fp_ou);
    fprintf(fp_ou,"<tr><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr>\n",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,"<tr><td class=\"data2\">%s</td><td class=\"data2\">%s</td><td class=\"data2\">%s-%s</td><td class=\"data2\">%s<a href=\"%s\">%s</a></td></th>\n",name,ip,data,hora,BlockImage,url,url);
+      fprintf(fp_ou,"<tr><td class=\"data2\">%s</td><td class=\"data2\">%s</td><td class=\"data2\">%s-%s</td><td class=\"data2\">",name,ip,data,hora);
+      if(BlockIt[0]!='\0') {
+         fprintf(fp_ou,"<a href=\"%s%s?url=",wwwDocumentRoot,BlockIt);
+         output_html_url(fp_ou,url);
+         fputs("\"><img src=\"../images/sarg-squidguard-block.png\"></a>&nbsp;",fp_ou);
+      }
+      fputs("<a href=\"",fp_ou);
+      output_html_url(fp_ou,url);
+      fputs("\">",fp_ou);
+      output_html_string(fp_ou,url,100);
+      fputs("</a></td></th>\n",fp_ou);
    }
    fclose(fp_in);
+   longline_free(&line);
 
    fputs("</table></div>\n",fp_ou);
    write_html_trailer(fp_ou);
index 719c3810141e1fa4c73faaef67c33e9afa7b0ddc..03cfd41fd4ba0d400618913a2f8dff89c64b39c4 100644 (file)
@@ -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
index 0595aa0e28c62c6ee370e76e5c838e2198b1a678..c16e9559e2590f60af280acec4eff839ee3b74b9 100644 (file)
@@ -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) {
index 4918acb800bb7c69bca47d485c9e5b52bb64c263..9315b86fc743e789dca468c6a3cb598034f50b32 100644 (file)
--- 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("<tr><td></td></tr>\n",fp_ou);
    fprintf(fp_ou,"<tr><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr>\n",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,"<a href=\"%s%s?url=%s\"><img src=\"%s/sarg-squidguard-block.png\"></a>&nbsp;",wwwDocumentRoot,BlockIt,url,ImageFile);
-      else BlockImage[0]='\0';
-
-      fprintf(fp_ou,"<tr><td class=\"data\">%s</td><td class=\"data\">%s</td><td class=\"data\">%s-%s</td><td class=\"data2\">%s<a href=\"http://%s\">%s</a></td></tr>\n",name,ip,data,hora,BlockImage,url,url);
+      fprintf(fp_ou,"<tr><td class=\"data\">%s</td><td class=\"data\">%s</td><td class=\"data\">%s-%s</td><td class=\"data2\">",name,ip,data,hora);
+      if(BlockIt[0] != '\0') {
+         fprintf(fp_ou,"<a href=\"%s%s?url=",wwwDocumentRoot,BlockIt);
+         output_html_url(fp_ou,url);
+         fprintf(fp_ou,"\"><img src=\"%s/sarg-squidguard-block.png\"></a>&nbsp;",ImageFile);
+      }
+      fputs("<a href=\"http://",fp_ou);
+      output_html_url(fp_ou,url);
+      fputs("\">",fp_ou);
+      output_html_string(fp_ou,url,100);
+      fputs("</a></td></tr>\n",fp_ou);
    }
    fclose(fp_in);
+   longline_free(&line);
 
    fputs("</table></div>\n",fp_ou);
    write_html_trailer(fp_ou);
index 36e8d261ad5fb8d9e6133c3a40a468230898213d..d63bad00c98b9d4758598c94587ded01b41eae25 100644 (file)
@@ -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 &hellip;.
+*/
+
+
+
+
+
+/*! \fn void output_html_url(FILE *fp_ou,const char *url)
+Write an URL into the file and replace any & by &amp;.
+
+\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.
+*/
index 5711f3f188c65f2e7b49304ea513b6319b4a734e..1018deded480b0f80162fd7a3bf4835cae63d93b 100644 (file)
@@ -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("<tr><td></td></tr>\n",fp_ou);
    fprintf(fp_ou,"<tr><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr>\n",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,"<tr><td class=\"data\">%s</td><td class=\"data\">%s</td><td class=\"data\">%s-%s</td><td class=\"data2\">",name,ip,data,hora);
-      if(BlockIt[0]!='\0')
-         fprintf(fp_ou,"<a href=\"%s%s?url=%s\"><img src=\"%s/sarg-squidguard-block.png\"></a>&nbsp;",wwwDocumentRoot,BlockIt,url,ImageFile);
-      fprintf(fp_ou,"<a href=\"%s\">",url);
-      output_html_string(fp_ou,url);
+      if(BlockIt[0]!='\0') {
+         fprintf(fp_ou,"<a href=\"%s%s?url=\"",wwwDocumentRoot,BlockIt);
+         output_html_url(fp_ou,url);
+         fprintf(fp_ou,"\"><img src=\"%s/sarg-squidguard-block.png\"></a>&nbsp;",ImageFile);
+      }
+      fputs("<a href=\"",fp_ou);
+      output_html_url(fp_ou,url);
+      fputs("\">",fp_ou);
+      output_html_string(fp_ou,url,100);
       fputs("</a></td></tr>\n",fp_ou);
    }
    fclose(fp_in);
+   longline_free(&line);
 
    fputs("</table></div>\n",fp_ou);
    write_html_trailer(fp_ou);
diff --git a/html.c b/html.c
index a7c61cc8925d01454a7eb1e8a814c92187c03767..53d498f43bde8dd05eb8d68ffdaf500458cf3899 100644 (file)
--- 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(&ltemp,&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(&ltemp,&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(&ltemp,&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,"&hellip;");
                   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 ; z1<sizeof(ltext110)-1 && s[z1] ; z1++)
+                  ltext110[z1]=tolower(s[z1]);
+               ltext110[z1]='\0';
                for(z1=0; urly[z1]; z1++) {
                   if(urly[z1]=='?' || urly[z1]=='-' || urly[z1]=='.' || urly[z1]==':' || urly[z1]=='/' || urly[z1]=='\\' ||
                      urly[z1]=='*' || urly[z1]=='\'' || urly[z1]=='\"' || urly[z1]=='$')
@@ -482,6 +498,7 @@ void htmlrel(void)
       }
 
       fclose(fp_in);
+      longline_free(&line);
 
       if(iprel)
          unlink(arqip);
index 76f3ae47c8ac0dca9023943165d7dc48644a69e9..3ceeb850fbf875b26023b0219426436180ecb8f1 100755 (executable)
@@ -150,6 +150,7 @@ int mkstemps(char *template, int suffixlen);
 #endif /*__MINGW32__*/
 
 #define MAXLEN 20000
+#define MAX_URL_LEN 40000
 #define MAXLOGS 255
 
 #define REPORT_TYPE_USERS_SITES         0x0001UL
@@ -207,8 +208,6 @@ FILE *fp_tt;
 char outdir[MAXLEN];
 char dirname[MAXLEN];
 char buf[MAXLEN];
-char url[MAXLEN];
-char urly[MAXLEN];
 char user[MAXLEN];
 char period[MAXLEN];
 char code[MAXLEN];
@@ -229,7 +228,6 @@ char href2[MAXLEN];
 char href3[MAXLEN];
 char df[20];
 char day[3], month[4], year[5];
-char ltext110[50];
 char cdfrom[30];
 char cduntil[30];
 int LastLog;
@@ -328,7 +326,6 @@ char TitleFontSize[5];
 char wwwDocumentRoot[MAXLEN];
 char ExternalCSSFile[MAXLEN];
 char BlockIt[255];
-char BlockImage[512];
 char NtlmUserFormat[30];
 unsigned long int IndexTree;
 int UserAuthentication;
@@ -354,7 +351,6 @@ char wwork1[MAXLEN];
 char wwork2[MAXLEN];
 char wwork3[MAXLEN];
 char mask[MAXLEN];
-char html_old[MAXLEN];
 char site[MAXLEN];
 char us[50];
 char email[MAXLEN];
index 954a4d543cbbd0dc4cfbb8ad4eb724e659b6553b..427c9f1c5a9f8e534318c62bf651168626b6b594 100755 (executable)
@@ -5,6 +5,20 @@ struct getwordstruct
    const char *beginning;
 };
 
+struct longlinestruct
+{
+   //! The buffer to store the data read from the log file.
+   char *buffer;
+   //! The size of the buffer.
+   size_t size;
+   //! The number of bytes stored in the buffer.
+   size_t length;
+   //! The position of the beginning of the current string.
+   size_t start;
+   //! The position of the end of the current string.
+   size_t end;
+};
+
 // auth.c
 void htaccess(const char *name);
 
@@ -158,7 +172,8 @@ void write_logo_image(FILE *fp_ou);
 void write_html_header(FILE *fp_ou, int depth, const char *title);
 void close_html_header(FILE *fp_ou);
 void write_html_trailer(FILE *fp_ou);
-void output_html_string(FILE *fp_ou,const char *str);
+void output_html_string(FILE *fp_ou,const char *str,int maxlen);
+void output_html_url(FILE *fp_ou,const char *url);
 void subs(char *str, int size, char *from, char *to);
 void conv_month(char *month);
 void debuga(const char *msg,...);
@@ -189,3 +204,6 @@ void baddata(void);
 char *get_param_value(const char *param,char *line);
 int compar( const void *, const void * );
 void unlinkdir(const char *dir,int contentonly);
+int longline_prepare(struct longlinestruct *line);
+char *longline_read(FILE *fp_in,struct longlinestruct *line);
+void longline_free(struct longlinestruct *line);
diff --git a/log.c b/log.c
index efa3d90fa2f2c42c23797b7ca27e8f07c7b318ca..ae0ec600e3e56fef3ee0ca91a3e9d7ec54ccbd49 100644 (file)
--- a/log.c
+++ b/log.c
@@ -29,7 +29,6 @@
 
 #define REPORT_EVERY_X_LINES 5000
 
-
 char *userfile;
 
 numlist weekdays = { { 0, 1, 2, 3, 4, 5, 6 }, 7 };
@@ -97,10 +96,12 @@ int main(int argc,char *argv[])
    char tbuf2[128];
    char zip[20];
    char *str;
-   char bufz[2*MAXLEN];
    char tmp2[MAXLEN];
    char start_hour[128];
    char end_hour[128];
+   char *linebuf;
+   char url[MAX_URL_LEN];
+   char urly[MAX_URL_LEN];
    enum InputLogFormat ilf;
    int ilf_count[ILF_Last];
    int  ch;
@@ -128,9 +129,10 @@ int main(int argc,char *argv[])
    unsigned long recs2=0UL;
    int OutputNonZero = REPORT_EVERY_X_LINES ;
    int download_flag=0;
-   char download_url[MAXLEN];
+   char download_url[MAX_URL_LEN];
    char sz_Last_User[MAXLEN]="";
    struct getwordstruct gwarea;
+   struct longlinestruct line;
 
    BgImage[0]='\0';
    LogoImage[0]='\0';
@@ -694,6 +696,11 @@ int main(int argc,char *argv[])
 
    init_usertab(UserTabFile);
 
+   if (longline_prepare(&line)<0) {
+      debuga(_("Not enough memory to read a log file"));
+      exit(1);
+   }
+
    sprintf ( sz_Download_Unsort , "%s/sarg/download.unsort", tmp);
 
    if((ReportType & REPORT_TYPE_DENIED) != 0) {
@@ -734,36 +741,50 @@ int main(int argc,char *argv[])
       }
       ilf=ILF_Unknown;
       download_flag=0;
-      // pre-Read the file only if I have to show stats
+      // pre-read the file only if we have to show stats
       if(ShowReadStatistics && !from_stdin) {
-         rewind(fp_in);
+         size_t nread,i;
+         int skipcr=0;
+
          recs1=0UL;
          recs2=0UL;
 
-         while( fgets(bufz,sizeof(bufz),fp_in) != NULL ) recs1++;
+         while ((nread=fread(line.buffer,1,line.size,fp_in))>0) {
+            for (i=0 ; i<nread ; i++)
+               if (skipcr) {
+                  if (line.buffer[i]!='\n' && line.buffer[i]!='\r') {
+                     skipcr=0;
+                  }
+               } else {
+                  if (line.buffer[i]=='\n' || line.buffer[i]=='\r') {
+                     skipcr=1;
+                     recs1++;
+                  }
+               }
+         }
          rewind(fp_in);
          printf("SARG: Records in file: %lu, reading: %3.2f%%\r",recs1,(float) 0);
          fflush( stdout ) ;
       }
 
-      while(fgets(bufz,sizeof(bufz),fp_in)!=NULL) {
-         blen=strlen(bufz);
-         if (blen>0 && 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<isa_ncols ; x++) {
                if (getword(val1,sizeof(val1),&gwarea,'\t')<0) {
                   printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
@@ -1373,13 +1397,11 @@ int main(int argc,char *argv[])
 
          if(l) {
             if(strcmp(user,"-") !=0 && url[0] != '\0' && strcmp(user," ") !=0 && strcmp(user,"") !=0 && strcmp(user,":") !=0){
-               if((str=(char *) strstr(bufz, "[SmartFilter:")) != (char *) NULL ) {
+               if((str=(char *) strstr(linebuf, "[SmartFilter:")) != (char *) NULL ) {
                   fixendofline(str);
                   sprintf(smartfilter,"\"%s\"",str+1);
                } else sprintf(smartfilter,"\"\"");
 
-               sprintf(bufz, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",dia,hora,user,ip,url,tam,code,elap,smartfilter);
-
                if ( strcmp ( user , sz_Last_User ) != 0 ) {
                   if ( fp_Write_User )
                      fclose( fp_Write_User ) ;
@@ -1391,9 +1413,11 @@ int main(int argc,char *argv[])
                   }
                   strcpy( sz_Last_User , user ) ;
                }
-               fputs (bufz, fp_Write_User);
+               fprintf(fp_Write_User, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",dia,hora,user,ip,url,tam,code,elap,smartfilter);
+
+               if(fp_log && ilf!=ILF_Sarg)
+                  fprintf(fp_log, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",dia,hora,user,ip,url,tam,code,elap,smartfilter);
 
-               if(fp_log && ilf!=ILF_Sarg) fputs(bufz,fp_log);
 
                totregsg++;
 
@@ -1455,6 +1479,7 @@ int main(int argc,char *argv[])
       }
    }
 
+   longline_free(&line);
    if ( fp_Download_Unsort )
      fclose (fp_Download_Unsort);
 
index 016771259e1b71727a3e69c4d92d8a0bbee3c8c4..78dc1bcf0eafca8e818854ea07e32e9ddf9382a8 100755 (executable)
@@ -32,8 +32,6 @@ static void datashow(const char *);
 static void getlog(void);
 static void header(void);
 
-char dat[128];
-char tim[128];
 char typ[128];
 char ouser[MAXLEN]="";
 char ourl[MAXLEN]="";
@@ -51,9 +49,10 @@ static void getlog(void)
    char template1[255]="/var/tmp/sargtpl1.XXXXXX";
    char template2[255]="/var/tmp/sargtpl2.XXXXXX";
    char cmd[512];
-   char buf[MAXLEN];
+   char *buf;
    int  fd1,fd2;
    int cstatus;
+   struct longlinestruct line;
 
    init_usertab(UserTabFile);
 
@@ -65,15 +64,21 @@ static void getlog(void)
        exit(1);
    }
 
+   if (longline_prepare(&line)<0) {
+      debuga(_("Not enough memory to read the log file"));
+      exit(1);
+   }
+
    sprintf(cmd,"tail -%d %s",realtime_access_log_lines,AccessLog[0]);
    fp = popen(cmd, "r");
-   while(fgets(buf,sizeof(buf),fp) != NULL )
+   while((buf=longline_read(fp,&line)) != NULL )
       if (getdata(buf,tmp)<0) {
          fprintf(stderr,"SARG: Maybe a broken record or garbage was returned by %s.\n",cmd);
          exit(1);
       }
    pclose(fp);
    fclose(tmp);
+   longline_free(&line);
 
    sprintf(cmd,"sort -r -k 1,1 -k 2,2 -o \"%s\" \"%s\"",template2,template1);
    cstatus=system(cmd);
@@ -90,8 +95,10 @@ static int getdata(char *rec, FILE *ftmp)
 {
    time_t tt;
    struct tm *t;
+   char dat[128];
    char tbuf[128];
    char warea[MAXLEN];
+   char url[MAX_URL_LEN];
    struct getwordstruct gwarea;
 
    getword_start(&gwarea,rec);
@@ -170,7 +177,10 @@ static int getdata(char *rec, FILE *ftmp)
 static void datashow(const char *tmp)
 {
    FILE *fin;
+   char dat[128];
+   char tim[128];
    char buf[MAXLEN];
+   char url[MAX_URL_LEN];
    struct getwordstruct gwarea;
 
    if((fin=fopen(tmp,"r"))==NULL) {
index fb482597be9328ec163fa61907a67fd04eb8e40e..3f7f9c532b06992652a0a6de33627813a9f88c28 100644 (file)
--- a/report.c
+++ b/report.c
@@ -41,13 +41,17 @@ void gerarel(void)
    FILE *fp_in;
    FILE *fp_gen;
 
-   char accdia[11], acchora[9], accuser[MAXLEN], accip[MAXLEN], accurl[MAXLEN];
+   char *buf;
+   char accdia[11], acchora[9], accuser[MAXLEN], accip[MAXLEN], accurl[MAX_URL_LEN];
    char oldaccdia[11], oldacchora[9], oldaccip[MAXLEN];
-   char wdirname[MAXLEN], oldurl[MAXLEN], oldaccuser[MAXLEN];
+   char wdirname[MAXLEN], oldurl[MAX_URL_LEN], oldaccuser[MAXLEN];
    char olduser[MAXLEN], oldmsg[50], acccode[MAXLEN/2 - 1], oldacccode[MAXLEN/2 - 1], user[MAXLEN];
    char ipantes[MAXLEN], nameantes[MAXLEN];
    char accsmart[MAXLEN];
    char crc2[MAXLEN/2 -1];
+   char siteind[MAX_URL_LEN];
+   char arqtt[256];
+   char oldurltt[MAX_URL_LEN],oldaccdiatt[11],oldacchoratt[9];
    long long int nbytes=0;
    long long int nelap=0;
    long long int nacc=0;
@@ -60,8 +64,8 @@ void gerarel(void)
    struct dirent *direntp;
    const char logext[]=".log";
    int dlen;
-   char siteind[MAXLEN];
    struct getwordstruct gwarea;
+   struct longlinestruct line;
 
    ipantes[0]='\0';
    nameantes[0]='\0';
@@ -127,9 +131,16 @@ void gerarel(void)
       }
 
       ttopen=0;
-      bzero(html_old, MAXLEN);
+      memset(oldurltt,0,sizeof(oldurltt));
+      memset(oldaccdiatt,0,sizeof(oldaccdiatt));
+      memset(oldacchoratt,0,sizeof(oldacchoratt));
 
-      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(accdia,sizeof(accdia),&gwarea,'\t')<0 || getword(acchora,sizeof(acchora),&gwarea,'\t')<0 ||
              getword(accuser,sizeof(accuser),&gwarea,'\t')<0 || getword(accip,sizeof(accip),&gwarea,'\t')<0 ||
@@ -195,9 +206,10 @@ void gerarel(void)
             }
          } else {
             if(strcmp(oldurl,accurl) != 0 || strcmp(oldaccuser,accuser) != 0){
-               strcpy(oldmsg,"OK");
                if(strstr(oldacccode,"DENIED") != 0)
-                  sprintf(oldmsg,"%s",text[46]);
+                  strcpy(oldmsg,text[46]);
+               else
+                  strcpy(oldmsg,"OK");
                gravatmp(oldaccuser,oldurl,nacc,nbytes,oldmsg,nelap,indexonly,incache,oucache);
                gravager(fp_gen,oldaccuser,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache);
                nacc=0;
@@ -213,7 +225,9 @@ void gerarel(void)
          nbytes+=accbytes;
          nelap+=accelap;
 
-         if((ReportType & REPORT_TYPE_SITE_USER_TIME_DATE) != 0) {
+         if ((ReportType & REPORT_TYPE_SITE_USER_TIME_DATE) != 0 &&
+             (strcmp(oldurltt,accurl) || strcmp(oldaccdiatt,accdia) || strcmp(oldacchoratt,acchora))) {
+
             if(!ttopen) {
                ind2++;
                strcpy(siteind,accurl);
@@ -222,13 +236,10 @@ void gerarel(void)
                      *str=='\'' || *str=='\"' || *str=='$' || *str=='@')
                      *str='_';
                }
-               sprintf(arqtt,"%s/%s",dirname,accuser);
+               snprintf(arqtt,sizeof(arqtt),"%s/%s",dirname,accuser);
                if(access(arqtt, R_OK) != 0)
                   my_mkdir(arqtt);
-               sprintf(arqtt,"%s/%s/tt%s-%s.html",dirname,accuser,accuser,siteind);
-               if(strlen(arqtt) > 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,"<font size=%s><a href=\"http://%s\">%s</a>",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,"<tr><td class=\"header_l\">%s:&nbsp;%s</td></tr>\n",text[89],period);
                fprintf(fp_tt,"<tr><td class=\"header_l\">%s:&nbsp;%s</td></tr>\n",text[90],name);
@@ -261,46 +268,46 @@ void gerarel(void)
                fprintf(fp_tt,"<tr><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr>\n",text[91],tmp4,text[110]+5);
             }
 
-            sprintf(html,"<tr><td class=\"data\">%s</td><td class=\"data\">%s</td><td class=\"data\">%s</td></tr>\n",accurl,accdia,acchora);
+            fprintf(fp_tt,"<tr><td class=\"data\">%s</td><td class=\"data\">%s</td><td class=\"data\">%s</td></tr>\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);
index 4f29e77e29e2da6c625c42a0c3d807cb10878368..4de082714b8e24ed63374a0ef79eaa53514bf8d3 100644 (file)
@@ -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,"<a href=\"%s%s?url=%s\"><img src=\"../images/sarg-squidguard-block.png\"></a>&nbsp;",wwwDocumentRoot,BlockIt,ourl);
-      else BlockImage[0]='\0';
-
       if(strcmp(url,ourl) != 0 && nsitesusers) {
-         if(BytesInSitesUsersReport) {
-            sprintf(wwork2,"%s",fixnum(obytes,1));
-            fprintf(fp_ou,"<tr><td class=\"data\">%d</td><td class=\"data2\">%s<a href=\"http://%s\">%s</a></td><td class=\"data\">%s</td><td class=\"data2\">%s</td></tr>\n",regs,BlockImage,ourl,ourl,wwork2,users);
-         } else
-            fprintf(fp_ou,"<tr><td class=\"data\">%d</td><td class=\"data2\">%s<a href=\"http://%s\">%s</a></td><td class=\"data2\">%s</td></tr>\n",regs,BlockImage,ourl,ourl,users);
+         fprintf(fp_ou,"<tr><td class=\"data\">%d</td><td class=\"data2\">",regs);
+         if(BlockIt[0]!='\0') {
+            fprintf(fp_ou,"<a href=\"%s%s?url=",wwwDocumentRoot,BlockIt);
+            output_html_url(fp_ou,ourl);
+            fputs("\"><img src=\"../images/sarg-squidguard-block.png\"></a>&nbsp;",fp_ou);
+         }
+         fputs("<a href=\"http://",fp_ou);
+         output_html_url(fp_ou,ourl);
+         fputs("\">",fp_ou);
+         output_html_string(fp_ou,ourl,100);
+         fputs("</a></td>",fp_ou);
+
+         if(BytesInSitesUsersReport)
+            fprintf(fp_ou,"<td class=\"data\">%s</td>",fixnum(obytes,1));
+         fprintf(fp_ou,"<td class=\"data2\">%s</td></tr>\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,"<tr><td class=\"data\">%d</td><td class=\"data2\"><a href=\"http://%s\">%s</a></td><td class=\"data2\">%s</td></tr>\n",regs,ourl,ourl,users);
+      fprintf(fp_ou,"<tr><td class=\"data\">%d</td><td class=\"data2\"><a href=\"http://",regs);
+      output_html_url(fp_ou,ourl);
+      fputs("\">",fp_ou);
+      output_html_string(fp_ou,ourl,100);
+      fprintf(fp_ou,"</a></td><td class=\"data2\">%s</td></tr>\n",users);
    }
 
    unlink(general2);
@@ -216,5 +235,4 @@ void siteuser(void)
       free(users);
 
    return;
-
 }
index 1e6b3ba24970f4f071bbac3cc45976c192724624..fe8009803feb88637f7871328aadaa9bb6b684e7 100644 (file)
@@ -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.
index e4d4d81d9a1071977306ffa21cd536f4784c5e91..93a7ae19501e1f290270a0762bdf53eaad61554c 100644 (file)
@@ -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<TopSitesNum && fgets(buf,sizeof(buf),fp_in)!=NULL) {
+   if (longline_prepare(&line)<0) {
+      debuga(_("Not enough memory to read file %s"),sites);
+      exit(1);
+   }
+
+   while(regs<TopSitesNum && (buf=longline_read(fp_in,&line))!=NULL) {
       getword_start(&gwarea,buf);
       if (getword_atoll(&nacc,&gwarea,'\t')<0) {
          printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",sites);
@@ -252,15 +265,23 @@ void topsites(void)
       strcpy(wwork2,fixnum(twork2,1));
       strcpy(wwork3,fixtime(twork3));
 
-      if(BlockIt[0] != '\0')
-         sprintf(BlockImage,"<a href=\"%s%s?url=%s\"><img src=\"../images/sarg-squidguard-block.png\"></a>&nbsp;",wwwDocumentRoot,BlockIt,url);
-      else BlockImage[0]='\0';
+      fprintf(fp_ou,"<tr><td class=\"data\">%d</td><td class=\"data2 link\">",regs);
 
+      if(BlockIt[0] != '\0') {
+         fprintf(fp_ou,"<a href=\"%s%s?url=\"",wwwDocumentRoot,BlockIt);
+         output_html_url(fp_ou,url);
+         fputs("\"><img src=\"../images/sarg-squidguard-block.png\"></a>&nbsp;",fp_ou);
+      }
 
-      fprintf(fp_ou,"<tr><td class=\"data\">%d</td><td class=\"data2 link\">%s<a href=\"http://%s\">%s</a></td><td class=\"data\">%s</td><td class=\"data\">%s</td><td class=\"data\">%s</td></tr>\n",regs,BlockImage,url,url,wwork1,wwork2,wwork3);
+      fputs("<a href=\"http://",fp_ou);
+      output_html_url(fp_ou,url);
+      fputs("\">",fp_ou);
+      output_html_string(fp_ou,url,100);
+      fprintf(fp_ou,"</a></td><td class=\"data\">%s</td><td class=\"data\">%s</td><td class=\"data\">%s</td></tr>\n",wwork1,wwork2,wwork3);
       regs++;
    }
    fclose(fp_in);
+   longline_free(&line);
 
    fputs("</table></div>\n",fp_ou);
    write_html_trailer(fp_ou);
index 55058ebc200ef9c6752c45e9302ab942a99dd820..b49ff2d8fe765bbecccb732d9d2d28c8e23ca5d2 100644 (file)
--- 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<sizeof(ltext110)-1 && s[i] ; i++)
+            ltext110[i]=tolower(s[i]);
+         ltext110[i]='\0';
          fputs("<td class=\"data2\">",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("<tr>",fp_top3);
index cc98a973aad33a4c411d314d782d6f3fda2cd331..469168bbb8470ebf3605341a44e8c6b885cd55ff 100644 (file)
--- 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");
index df4d0063f639e96991dbff1a78b3ee2a217f4c20..3981718fbf4807406530af78c19a97bf2fcd88bd 100644 (file)
--- 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 3a1e231ba7573073778cd49e2633b140baaabd7d..86975b5698a797c009aa82ecc3f9a6e1d5bdc313 100644 (file)
--- a/util.c
+++ b/util.c
 #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("</body>\n</html>\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 || i<maxlen)) {
       switch (*str) {
          case '&':
             fputs("&amp;",fp_ou);
@@ -1490,6 +1493,20 @@ void output_html_string(FILE *fp_ou,const char *str)
             fputc(*str,fp_ou);
       }
       str++;
+      i++;
+   }
+   if (maxlen>0 && i>=maxlen)
+      fputs("&hellip;",fp_ou);
+}
+
+void output_html_url(FILE *fp_ou,const char *url)
+{
+   while (*url) {
+      if (*url=='&')
+         fputs("&amp;",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 ; i<line->end ; 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;
+   }
+}