]> git.ipfire.org Git - thirdparty/sarg.git/blobdiff - squidguard_log.c
Give a try to splint and apply a few of the recommandations
[thirdparty/sarg.git] / squidguard_log.c
index 116a4c81e7d8c34feb1bb1f5ee34a5487a7c6da4..0123a8944363a0c58e22d6565f5548dde5645718 100644 (file)
@@ -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
@@ -33,10 +34,14 @@ static int nfiles_done = 0;
 static void read_log(const char *wentp, FILE *fp_ou)
 {
    FILE *fp_in = NULL;
+   char buf[MAXLEN];
    char leks[5], sep[2], res[MAXLEN];
-   char mon[10], hour[15];
+   char mon[20], day[3], year[5], hour[15];
    char list[MAXLEN];
    char wdata[127];
+   char url[MAX_URL_LEN];
+   char user[MAX_USER_LEN];
+   char ip[25];
    int  idata=0;
    int  i;
    char *str;
@@ -44,12 +49,7 @@ static void read_log(const char *wentp, FILE *fp_ou)
    struct getwordstruct gwarea1;
 
    if(debug) {
-      getword_start(&gwarea,text[7]);
-      if (getword(urly,sizeof(urly),&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(_("Reading squidGuard log file %s\n"),wentp);
    }
 
    /* With squidGuard, you can log groups in only one log file.
@@ -76,18 +76,18 @@ static void read_log(const char *wentp, FILE *fp_ou)
    nfiles_done++;
    files_done = realloc(files_done, nfiles_done*sizeof(char *));
    if (!files_done) {
-       perror("parse squidGuard - realloc");
-       exit(EXIT_FAILURE);
+      debuga(_("Not enough memory to store the name of the new squidGuard log to be read - %s\n"),strerror(errno));
+      exit(EXIT_FAILURE);
    }
    files_done[nfiles_done-1] = strdup(wentp);
    if (!files_done[nfiles_done-1]) {
-       perror("parse squidGuard - strdup");
-       exit(EXIT_FAILURE);
+      debuga(_("Not enough memory to store the name of the new squidGuard log to be read - %s\n"),strerror(errno));
+      exit(EXIT_FAILURE);
    }
 
    if ((fp_in=fopen(wentp,"r"))==NULL) {
-      fprintf(stderr, "SARG: (squidguard) %s: %s\n",text[8],wentp);
-      exit(1);
+      debuga(_("(squidguard) Cannot open log file %s\n"),wentp);
+      exit(EXIT_FAILURE);
    }
 
    while (fgets(buf,sizeof(buf),fp_in) != NULL) {
@@ -96,37 +96,68 @@ static void read_log(const char *wentp, FILE *fp_ou)
          getword_start(&gwarea1,SquidGuardLogFormat);
          leks[0]='\0';
          if (getword(leks,sizeof(leks),&gwarea1,'#')<0) {
-            printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wentp);
-            exit(1);
+            debuga(_("There is a broken record or garbage in your %s file\n"),wentp);
+            exit(EXIT_FAILURE);
          }
          while(strcmp(leks,"end") != 0) {
             if (getword(leks,sizeof(leks),&gwarea1,'#')<0 || getword(sep,sizeof(sep),&gwarea1,'#')<0) {
-               printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wentp);
-               exit(1);
+               debuga(_("Maybe you have a broken record or garbage in your %s file\n"),wentp);
+               exit(EXIT_FAILURE);
             }
             if(strcmp(leks,"end") != 0) {
                if (getword(res,sizeof(res),&gwarea,sep[0])<0) {
-                  printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wentp);
-                  exit(1);
+                  debuga(_("Maybe you have a broken record or garbage in your %s file\n"),wentp);
+                  exit(EXIT_FAILURE);
                }
-               if(strcmp(leks,"year") == 0)
+               if(strcmp(leks,"year") == 0) {
+                  if (strlen(res)>=sizeof(year)) {
+                     debuga(_("Year string too long in squidGuard log file %s\n"),wentp);
+                     exit(EXIT_FAILURE);
+                  }
                   strcpy(year,res);
-               else if(strcmp(leks,"year") == 0)
-                  strcpy(year,res);
-               else if(strcmp(leks,"mon") == 0)
+               } else if(strcmp(leks,"mon") == 0) {
+                  if (strlen(res)>=sizeof(mon)) {
+                     debuga(_("Month string too long in squidGuard log file %s\n"),wentp);
+                     exit(EXIT_FAILURE);
+                  }
                   strcpy(mon,res);
-               else if(strcmp(leks,"day") == 0)
+               } else if(strcmp(leks,"day") == 0) {
+                  if (strlen(res)>=sizeof(day)) {
+                     debuga(_("Day string too long in squidGuard log file %s\n"),wentp);
+                     exit(EXIT_FAILURE);
+                  }
                   strcpy(day,res);
-               else if(strcmp(leks,"hour") == 0)
+               } else if(strcmp(leks,"hour") == 0) {
+                  if (strlen(res)>=sizeof(hour)) {
+                     debuga(_("Hour string too long in squidGuard log file %s\n"),wentp);
+                     exit(EXIT_FAILURE);
+                  }
                   strcpy(hour,res);
-               else if(strcmp(leks,"list") == 0)
+               } else if(strcmp(leks,"list") == 0) {
+                  if (strlen(res)>=sizeof(list)) {
+                     debuga(_("Banning list name too long in squidGuard log file %s\n"),wentp);
+                     exit(EXIT_FAILURE);
+                  }
                   strcpy(list,res);
-               else if(strcmp(leks,"ip") == 0)
+               } else if(strcmp(leks,"ip") == 0) {
+                  if (strlen(res)>=sizeof(ip)) {
+                     debuga(_("IP address too long in squidGuard log file %s\n"),wentp);
+                     exit(EXIT_FAILURE);
+                  }
                   strcpy(ip,res);
-               else if(strcmp(leks,"user") == 0)
+               } else if(strcmp(leks,"user") == 0) {
+                  if (strlen(res)>=sizeof(user)) {
+                     debuga(_("User ID too long in squidGuard log file %s\n"),wentp);
+                     exit(EXIT_FAILURE);
+                  }
                   strcpy(user,res);
-               else if(strcmp(leks,"url") == 0)
+               } else if(strcmp(leks,"url") == 0) {
+                  if (strlen(res)>=sizeof(url)) {
+                     debuga(_("URL too long in squidGuard log file %s\n"),wentp);
+                     exit(EXIT_FAILURE);
+                  }
                   strcpy(url,res);
+               }
             }
          }
       } else {
@@ -136,8 +167,8 @@ static void read_log(const char *wentp, FILE *fp_ou)
              getword_skip(MAXLEN,&gwarea,' ')<0 || getword(url,sizeof(url),&gwarea,' ')<0 ||
              getword(ip,sizeof(ip),&gwarea,'/')<0 || getword_skip(MAXLEN,&gwarea,' ')<0 ||
              getword(user,sizeof(user),&gwarea,' ')<0) {
-            printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wentp);
-            exit(1);
+            debuga(_("There is a broken record or garbage in your %s file\n"),wentp);
+            exit(EXIT_FAILURE);
           }
           /*
           The URL may be "http://url:port/data" if the method is GET or simply "url:port/" if the method is CONNECT.
@@ -181,11 +212,13 @@ static void read_log(const char *wentp, FILE *fp_ou)
 void squidguard_log(void)
 {
    FILE *fp_ou = NULL, *fp_guard = NULL;
+   char buf[MAXLEN];
    char guard_in[MAXLEN];
    char guard_ou[MAXLEN];
    char logdir[MAXLEN];
    char year[10], day[10], mon[10];
    char user[MAXLEN];
+   char tmp6[MAXLEN];
    int  y;
    int cstatus;
    char *str;
@@ -202,8 +235,8 @@ void squidguard_log(void)
    sprintf(guard_in,"%s/squidguard.unsort",tmp);
    sprintf(guard_ou,"%s/squidguard.log",tmp);
    if((fp_ou=fopen(guard_in,"a"))==NULL) {
-      fprintf(stderr, "SARG: (squidguard) %s: %s\n",text[8],guard_in);
-      exit(1);
+      debuga(_("(squidguard) Cannot open log file %s\n"),guard_in);
+      exit(EXIT_FAILURE);
    }
 
    bzero(day, 3);
@@ -242,13 +275,13 @@ void squidguard_log(void)
 
    if(SquidGuardConf[0] != 0) {
       if(access(SquidGuardConf, R_OK) != 0) {
-         debuga("Cannot open squidGuard config file: %s",SquidGuardConf);
-         exit(1);
+         debuga(_("Cannot open squidGuard config file: %s\n"),SquidGuardConf);
+         exit(EXIT_FAILURE);
       }
 
       if((fp_guard=fopen(SquidGuardConf,"r"))==NULL) {
-         fprintf(stderr, "SARG: (squidguard) %s: %s\n",text[8],SquidGuardConf);
-         exit(1);
+         debuga(_("(squidguard) Cannot open log file %s\n"),SquidGuardConf);
+         exit(EXIT_FAILURE);
       }
 
       logdir[0]=0;
@@ -297,7 +330,7 @@ void squidguard_log(void)
          }
       }
    } else {
-      sprintf(wentp,"%s",SquidGuardLogAlternate);
+      strcpy(wentp,SquidGuardLogAlternate);
       read_log(wentp,fp_ou);
    }
 
@@ -311,15 +344,15 @@ void squidguard_log(void)
    }
 
    if(debug) {
-      debuga("%s: %s",text[54],guard_ou);
+      debuga(_("Sorting file: %s\n"),guard_ou);
    }
 
    sprintf(tmp6,"sort -k 1,1 -k 2,2 -k 4,4 \"%s\" -o \"%s\"",guard_in, guard_ou);
    cstatus=system(tmp6);
    if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
-      fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
-      fprintf(stderr, "SARG: sort command: %s\n",tmp6);
-      exit(1);
+      debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
+      debuga(_("sort command: %s\n"),tmp6);
+      exit(EXIT_FAILURE);
    }
 
    unlink(guard_in);