]> git.ipfire.org Git - thirdparty/sarg.git/blobdiff - log.c
Add an option to make sarg generates the css template
[thirdparty/sarg.git] / log.c
diff --git a/log.c b/log.c
index 34a7a8e8839477366e13f30524a3589647fde69c..344fc3b1e268c5f6d3a530d07cc3775a4b175ada 100644 (file)
--- a/log.c
+++ b/log.c
 #include "include/conf.h"
 #include "include/defs.h"
 
+#ifdef HAVE_GETOPT_H
+#include <getopt.h>
+#endif
+
 #define REPORT_EVERY_X_LINES 5000
 #define MAX_OPEN_USER_FILES 10
 
@@ -128,6 +132,7 @@ int main(int argc,char *argv[])
    int  iarq=0;
    int isa_ncols=0,isa_cols[ISACOL_Last];
    bool from_stdin;
+   bool from_pipe;
    int blen;
    int maxopenfiles;
    int nopen;
@@ -150,8 +155,20 @@ int main(int argc,char *argv[])
    struct getwordstruct gwarea;
    longline line;
    time_t tnum;
+   struct stat logstat;
    struct userinfostruct *uinfo;
    struct userfilestruct *first_user_file, *ufile, *ufile1, *prev_ufile;
+   static int split=0;
+   static int convert=0;
+   static int output_css=0;
+   int option_index;
+   static struct option long_options[]=
+   {
+      {"convert",no_argument,&convert,1},
+      {"css",no_argument,&output_css,1},
+      {"split",no_argument,&split,1},
+      {0,0,0,0}
+   };
 
 #ifdef HAVE_LOCALE_H
    setlocale(LC_TIME,"");
@@ -187,8 +204,8 @@ int main(int argc,char *argv[])
    UserTabFile[0]='\0';
    BlockIt[0]='\0';
    ExternalCSSFile[0]='\0';
-   SquidGuardLogFormat[0]='\0';
-   SquidGuardLogAlternate[0]='\0';
+   RedirectorLogFormat[0]='\0';
+   NRedirectorLogs=0;
    for (ilf=0 ; ilf<ILF_Last ; ilf++) ilf_count[ilf]=0;
 
    sprintf(ExcludeCodes,"%s/exclude_codes",SYSCONFDIR);
@@ -226,7 +243,7 @@ int main(int argc,char *argv[])
    strcpy(TopsitesSortType,"D");
    LongUrl=0;
    strcpy(FontFace,"Verdana,Tahoma,Arial");
-   strcpy(datetimeby,"elap");
+   datetimeby=DATETIME_BYTE;
    strcpy(CharSet,"ISO-8859-1");
    Privacy=0;
    strcpy(PrivacyString,"***.***.***.***");
@@ -248,7 +265,7 @@ int main(int argc,char *argv[])
    ShowSargLogo=true;
    strcpy(ParsedOutputLog,"no");
    strcpy(ParsedOutputLogCompress,"/bin/gzip -f");
-   strcpy(DisplayedValues,"abbreviation");
+   DisplayedValues=DISPLAY_ABBREV;
    strcpy(HeaderFontSize,"9px");
    strcpy(TitleFontSize,"11px");
    strcpy(AuthUserTemplateFile,"sarg_htaccess");
@@ -263,9 +280,9 @@ int main(int argc,char *argv[])
    NtlmUserFormat=NTLMUSERFORMAT_DOMAINUSER;
    IndexTree=INDEX_TREE_FILE;
    strcpy(RealtimeTypes,"GET,PUT,CONNECT");
-   strcpy(RealtimeUnauthRec,"show");
-   SquidguardIgnoreDate=0;
-   DansguardianIgnoreDate=0;
+   RealtimeUnauthRec=REALTIME_UNAUTH_REC_SHOW;
+   RedirectorIgnoreDate=false;
+   DansguardianIgnoreDate=false;
    DataFileUrl=DATAFILEURL_IP;
    strcpy(MaxElapsed,"28800000");
    BytesInSitesUsersReport=0;
@@ -325,9 +342,6 @@ int main(int argc,char *argv[])
    debugm=0;
    iprel=false;
    userip=false;
-   color1=0;
-   color2=0;
-   color3=0;
    realt=false;
    realtime_refresh=3;
    realtime_access_log_lines=1000;
@@ -347,12 +361,15 @@ int main(int argc,char *argv[])
    for(x=0; x<MAXLOGS; x++)
       AccessLog[x][0]='\0';
    AccessLogFromCmdLine=0;
+   RedirectorLogFromCmdLine=0;
 
    strcpy(Title,_("Squid User Access Report"));
 
-   while((ch = getopt(argc, argv, "a:b:c:d:e:f:g:u:l:L:o:s:t:w:hijmnprvxyz")) != -1){
+   while((ch = getopt_long_only(argc, argv, "a:b:c:d:e:f:g:u:l:L:o:s:t:w:hijmnprvxyz",long_options,&option_index)) != -1){
       switch(ch)
       {
+         case 0:
+            break;
          case 'a':
             strcpy(addr,optarg);
             break;
@@ -384,7 +401,11 @@ int main(int argc,char *argv[])
             break;
          case 'l':
             if (NAccessLog>=MAXLOGS) {
-               printf(_("SARG: Too many log files passed on command line with option -l.\n"));
+               debuga(_("Too many log files passed on command line with option -l.\n"));
+               exit(EXIT_FAILURE);
+            }
+            if (strlen(optarg)>=MAX_LOG_FILELEN) {
+               debuga(_("Log file name too long passed on command line with option -l: %s\n"),optarg);
                exit(EXIT_FAILURE);
             }
             strcpy(AccessLog[NAccessLog],optarg);
@@ -392,7 +413,17 @@ int main(int argc,char *argv[])
             AccessLogFromCmdLine++;
             break;
          case 'L':
-            strcpy(SquidGuardLogAlternate,optarg);
+            if (NRedirectorLogs>MAX_REDIRECTOR_LOGS) {
+               debuga(_("Too many redirector logs passed on command line with option -L.\n"));
+               exit(EXIT_FAILURE);
+            }
+            if (strlen(optarg)>=MAX_REDIRECTOR_FILELEN) {
+               debuga(_("Redirector log file name too long passed on command line with opton -L: %s\n"),optarg);
+               exit(EXIT_FAILURE);
+            }
+            strcpy(RedirectorLogs[NRedirectorLogs],optarg);
+            NRedirectorLogs++;
+            RedirectorLogFromCmdLine++;
             break;
          case 'm':
             debugm++;
@@ -456,15 +487,16 @@ int main(int argc,char *argv[])
          case 'z':
             debugz++;
             break;
-         case ':':
+         /*case ':':
             debuga(_("Option -%c require an argument\n"),optopt);
             errflg++;
-            break;
+            break;*/
          case '?':
             usage(argv[0]);
             exit(EXIT_FAILURE);
+         default:
+            abort();
       }
-
    }
 
    if (errflg>0) {
@@ -506,13 +538,16 @@ int main(int argc,char *argv[])
       NAccessLog++;
    }
 
-   if(strcmp(hexclude,"onvert") == 0 && strcmp(site,"plit") != 0) {
-      convlog(AccessLog[0], df, dfrom, duntil);
+   if(output_css) {
+      css_content(stdout);
       exit(EXIT_SUCCESS);
    }
-
-   if(strcmp(site,"plit") == 0) {
-      splitlog(AccessLog[0], df, dfrom, duntil, hexclude);
+   if(split) {
+      splitlog(AccessLog[0], df, dfrom, duntil, convert);
+      exit(EXIT_SUCCESS);
+   }
+   if(convert) {
+      convlog(AccessLog[0], df, dfrom, duntil);
       exit(EXIT_SUCCESS);
    }
 
@@ -612,6 +647,8 @@ int main(int argc,char *argv[])
       debuga(_("                       IP report (-i) = %s\n"),(iprel) ? _("Yes") : _("No"));
       for (iarq=0 ; iarq<NAccessLog ; iarq++)
          debuga(_("                       Input log (-l) = %s\n"),AccessLog[iarq]);
+      for (iarq=0 ; iarq<NRedirectorLogs ; iarq++)
+         debuga(_("                  Redirector log (-L) = %s\n"),RedirectorLogs[iarq]);
       debuga(_("              Resolve IP Address (-n) = %s\n"),(Ip2Name) ? _("Yes") : _("No"));
       debuga(_("                      Output dir (-o) = %s\n"),outdir);
       debuga(_("Use Ip Address instead of userid (-p) = %s\n"),(UserIp) ? _("Yes") : _("No"));
@@ -641,6 +678,8 @@ int main(int argc,char *argv[])
       printf(_("                       IP report (-i) = %s\n"),(iprel) ? _("Yes") : _("No"));
       for (iarq=0 ; iarq<NAccessLog ; iarq++)
          printf(_("                       Input log (-l) = %s\n"),AccessLog[iarq]);
+      for (iarq=0 ; iarq<NRedirectorLogs ; iarq++)
+         printf(_("                  Redirector log (-L) = %s\n"),RedirectorLogs[iarq]);
       printf(_("              Resolve IP Address (-n) = %s\n"),(Ip2Name) ? _("Yes") : _("No"));
       printf(_("                      Output dir (-o) = %s\n"),outdir);
       printf(_("Use Ip Address instead of userid (-p) = %s\n"),(UserIp) ? _("Yes") : _("No"));
@@ -727,19 +766,29 @@ int main(int argc,char *argv[])
          fp_in=stdin;
          from_stdin=true;
       } else {
-         decomp(arq,zip,tmp);
-         if(debug)
-            debuga(_("Reading access log file: %s\n"),arq);
-         if((fp_in=MY_FOPEN(arq,"r"))==NULL) {
+         if (date[0]!='\0') {
+            if (stat(arq,&logstat)!=0) {
+               debuga(_("Cannot get the modification time of input log file %s (%s). Processing it anyway\n"),arq,strerror(errno));
+            } else {
+               struct tm *logtime=localtime(&logstat.st_mtime);
+               if ((logtime->tm_year+1900)*10000+(logtime->tm_mon+1)*100+logtime->tm_mday<dfrom) {
+                  debuga(_("Ignoring old log file %s\n"),arq);
+                  continue;
+               }
+            }
+         }
+         fp_in=decomp(arq,&from_pipe);
+         if(fp_in==NULL) {
             debuga(_("(log) Cannot open log file: %s - %s\n"),arq,strerror(errno));
             exit(EXIT_FAILURE);
          }
+         if(debug) debuga(_("Reading access log file: %s\n"),arq);
          from_stdin=false;
       }
       ilf=ILF_Unknown;
       download_flag=false;
       // pre-read the file only if we have to show stats
-      if(ShowReadStatistics && !from_stdin) {
+      if(ShowReadStatistics && !from_stdin && !from_pipe) {
          size_t nread,i;
          bool skipcr=false;
          char tmp4[MAXLEN];
@@ -980,12 +1029,6 @@ int main(int argc,char *argv[])
                strftime(tbuf2, sizeof(tbuf2), "%H%M", t);
 
                idata=(t->tm_year+1900)*10000+(t->tm_mon+1)*100+t->tm_mday;
-
-               if(strncmp(df,"u",1)==0)
-                  strftime(dia, sizeof(dia), "%m/%d/%Y", t);
-               else
-                  strftime(dia, sizeof(dia), "%d/%m/%Y", t);
-               sprintf(hora,"%02d:%02d:%02d",t->tm_hour,t->tm_min,t->tm_sec);
             }
          }
          if (ilf==ILF_Sarg) {
@@ -1027,24 +1070,13 @@ int main(int argc,char *argv[])
                exit(EXIT_FAILURE);
             }
             getword_start(&gwarea,data);
-            if(strcmp(df,"u") == 0) {
-               if (getword_atoll(&imonth,&gwarea,'/')<0){
-                  debuga(_("Maybe you have a broken date in your %s file\n"),arq);
-                  exit(EXIT_FAILURE);
-               }
-               if (getword_atoll(&iday,&gwarea,'/')<0){
-                  debuga(_("Maybe you have a broken date in your %s file\n"),arq);
-                  exit(EXIT_FAILURE);
-               }
-            } else {
-               if (getword_atoll(&iday,&gwarea,'/')<0){
-                  debuga(_("Maybe you have a broken date in your %s file\n"),arq);
-                  exit(EXIT_FAILURE);
-               }
-               if (getword_atoll(&imonth,&gwarea,'/')<0){
-                  debuga(_("Maybe you have a broken date in your %s file\n"),arq);
-                  exit(EXIT_FAILURE);
-               }
+            if (getword_atoll(&iday,&gwarea,'/')<0 || iday<1 || iday>31){
+               debuga(_("Maybe you have a broken date in your %s file\n"),arq);
+               exit(EXIT_FAILURE);
+            }
+            if (getword_atoll(&imonth,&gwarea,'/')<0 || imonth<1 || imonth>12){
+               debuga(_("Maybe you have a broken date in your %s file\n"),arq);
+               exit(EXIT_FAILURE);
             }
             if (getword_atoll(&iyear,&gwarea,'\0')<0){
                debuga(_("Maybe you have a broken date in your %s file\n"),arq);
@@ -1170,6 +1202,9 @@ int main(int argc,char *argv[])
             break;
          }
 
+         strftime(dia, sizeof(dia), "%d/%m/%Y", t);
+         snprintf(hora,sizeof(hora),"%02d:%02d:%02d",t->tm_hour,t->tm_min,t->tm_sec);
+
          if(debugm)
             printf("DATE=%s IDATA=%d DFROM=%d DUNTIL=%d\n",date,idata,dfrom,duntil);
 
@@ -1395,7 +1430,10 @@ int main(int argc,char *argv[])
                for (ufile1=first_user_file ; ufile1 ; ufile1=ufile1->next) {
                   if (ufile1->file!=NULL) {
                      if (x>=maxopenfiles) {
-                        fclose(ufile1->file);
+                        if (fclose(ufile1->file)==EOF) {
+                           debuga(_("Failed to close the log file of user %s - %s\n"),ufile1->user->id,strerror(errno));
+                           exit(EXIT_FAILURE);
+                        }
                         ufile1->file=NULL;
                      }
                      x++;
@@ -1423,7 +1461,10 @@ int main(int argc,char *argv[])
             }
             strcpy( sz_Last_User , user ) ;
          }*/
-         fprintf(ufile->file, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",dia,hora,ip,url,tam,code,elap,smartfilter);
+         if (fprintf(ufile->file, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",dia,hora,ip,url,tam,code,elap,smartfilter)<=0) {
+            debuga(_("Write error in the log file of user %s\n"),user);
+            exit(EXIT_FAILURE);
+         }
 
          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);
@@ -1479,7 +1520,10 @@ int main(int argc,char *argv[])
          }
       }
       if (!from_stdin) {
-         fclose(fp_in);
+         if (from_pipe)
+            pclose(fp_in);
+         else
+            fclose(fp_in);
          if( ShowReadStatistics )
             printf(_("SARG: Records in file: %lu, reading: %3.2f%%\n"),recs1, (float) 100 );
       }
@@ -1612,11 +1656,6 @@ int main(int argc,char *argv[])
    if((ReportType & REPORT_TYPE_DENIED) != 0)
       unlink(denied_sort);
 
-   if(zip[0] != '\0' && strcmp(zip,"zcat") !=0) {
-      recomp(arq, zip);
-   }
-//   else  unlink(arq);
-
    if(strcmp(tmp,"/tmp") != 0) {
       unlinkdir(tmp,0);
    }
@@ -1650,14 +1689,20 @@ static void getusers(const char *pwdfile, int debug)
       exit(EXIT_FAILURE);
    }
 
-   fseek(fp_usr, 0, SEEK_END);
+   if (fseek(fp_usr, 0, SEEK_END)==-1) {
+      debuga(_("Failed to move till the end of the users file %s: %s\n"),pwdfile,strerror(errno));
+      exit(EXIT_FAILURE);
+   }
    nreg = ftell(fp_usr);
    if (nreg<0) {
       debuga(_("Cannot get the size of file %s\n"),pwdfile);
       exit(EXIT_FAILURE);
    }
    nreg = nreg+5000;
-   fseek(fp_usr, 0, SEEK_SET);
+   if (fseek(fp_usr, 0, SEEK_SET)==-1) {
+      debuga(_("Failed to rewind the users file %s: %s\n"),pwdfile,strerror(errno));
+      exit(EXIT_FAILURE);
+   }
 
    if((userfile=(char *) malloc(nreg))==NULL){
       debuga(_("malloc error (%ld)\n"),nreg);