]> git.ipfire.org Git - thirdparty/sarg.git/blobdiff - smartfilter.c
Strengthen the making of the report file name to drastically limit the characters...
[thirdparty/sarg.git] / smartfilter.c
index 8e7306302a63e36579eedfca62dbd8f4367e6dd9..d0d281f611ea61021e4a182d795a2e07bfa01047 100644 (file)
@@ -1,10 +1,11 @@
 /*
- * AUTHOR: Pedro Lineu Orso                         pedro.orso@gmail.com
- *                                                            1998, 2008
  * 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
  */
 
 #include "include/conf.h"
+#include "include/defs.h"
 
 void smartfilter_report(void)
 {
 
    FILE *fp_in = NULL, *fp_ou = NULL, *fp_user = NULL;
-      
+
+   char buf[MAXLEN];
    char url[MAXLEN];
-   char html[MAXLEN];
-   char html2[MAXLEN];
    char csort[255];
    char smart_in[MAXLEN];
    char smart_ou[MAXLEN];
-   char per[MAXLEN];
    char sites[MAXLEN];
    char report[MAXLEN];
-   char period[100];
    char ip[MAXLEN];
    char user[MAXLEN];
    char ouser[MAXLEN];
@@ -49,96 +48,93 @@ void smartfilter_report(void)
    char smartheader[15];
    char ftime[128];
    char smartuser[MAXLEN];
-   char *str;
    int  fuser=0;
    int cstatus;
+   struct getwordstruct gwarea;
+   const struct userinfostruct *uinfo;
 
    ouser[0]='\0';
 
-   sprintf(smartheader,"%s",text[116]);
+   strcpy(smartheader,_("SmartFilter"));
    strup(smartheader);
 
-   sprintf(smart_in,"%s/smartfilter.unsort",dirname);
-   sprintf(sites,"%s/sarg-sites",dirname);
-   sprintf(smart_ou,"%s/smartfilter.log",dirname);
-   sprintf(per,"%s/sarg-period",dirname);
-   sprintf(report,"%s/smartfilter.html",dirname);
+   sprintf(smart_in,"%s/smartfilter.unsort",outdirname);
+   sprintf(sites,"%s/sarg-sites",outdirname);
+   sprintf(smart_ou,"%s/smartfilter.log",outdirname);
+   sprintf(report,"%s/smartfilter.html",outdirname);
 
-   if ((fp_in = fopen(per, "r")) == 0) {
-      fprintf(stderr, "SARG: (smartfilter) %s: %s\n",text[45],per);
-      exit(1);
+   if (snprintf(csort,sizeof(csort),"sort -n -k 1,1 -k 2,2 -k 3,3 -o \"%s\" \"%s\"",smart_ou,smart_in)>=sizeof(csort)) {
+      debuga(_("cannot build the sort command to sort file %s\n"),smart_in);
+      exit(EXIT_FAILURE);
    }
-
-   fgets(period,sizeof(period),fp_in);
-   fclose(fp_in);
-
-   sprintf(csort,"sort -n -k 1,1 -k 2,2 -k 3,3 -o '%s' '%s'",smart_ou,smart_in);
    cstatus=system(csort);
    if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
-      fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
-      fprintf(stderr, "SARG: sort command: %s\n",csort);
-      exit(1);
+      debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
+      debuga(_("sort command: %s\n"),csort);
+      exit(EXIT_FAILURE);
    }
    if((fp_in=fopen(smart_ou,"r"))==NULL) {
-      fprintf(stderr, "SARG: (smartfilter) %s: %s\n",text[8],smart_ou);
-      fprintf(stderr, "SARG: sort command: %s\n",csort);
-      exit(1);
+      debuga(_("(smartfilter) Cannot open log file %s\n"),smart_ou);
+      debuga(_("sort command: %s\n"),csort);
+      exit(EXIT_FAILURE);
    }
    unlink(smart_in);
 
    if((fp_ou=fopen(report,"w"))==NULL) {
-     fprintf(stderr, "SARG: (smartfilter) %s: %s\n",text[8],report);
-     exit(1);
+     debuga(_("(smartfilter) Cannot open log file %s\n"),report);
+     exit(EXIT_FAILURE);
    }
 
    fprintf(fp_ou, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n<html>\n<head>\n  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet);
    fputs("</head>\n",fp_ou);
    if(strlen(FontFace) > 0) fprintf(fp_ou,"<font face=%s>\n",FontFace);
-   fprintf(fp_ou,"<body bgcolor=%s text=%s background='%s'>\n",BgColor,TxColor,BgImage);
-   fputs("<center><table cellpadding=0 cellspacing=0>\n",fp_ou);
-   if(strlen(LogoImage) > 0) fprintf(fp_ou, "<center><table cellpadding=\"0\" cellspacing=\"0\">\n<tr><th class=\"logo\"><img src='%s' border=0 align=absmiddle width=%s height=%s>&nbsp;%s</th></tr>\n<tr><td height=\"5\"></td></tr>\n</table>\n",LogoImage,Width,Height,LogoText);
-
-   fprintf(fp_ou,"<tr><th align=center><b><font color=%s size=+1>%s</font></b></th></tr>\n",TiColor,Title);
-   fprintf(fp_ou,"<tr><td align=center bgcolor=%s><font size=%s>%s: %s</font></td></tr>\n",HeaderBgColor,FontSize,text[89],period);
-   fprintf(fp_ou,"<tr><th bgcolor=%s align=center><font size=%s>%s</font></th></tr>\n",HeaderBgColor,FontSize,text[116]);
-   fputs("</table></center>\n",fp_ou);
-
-   fputs("<center><table cellpadding=0 cellspacing=2>\n",fp_ou);
+   fprintf(fp_ou,"<body bgcolor=\"%s\" text=\"%s\" background=\"%s\">\n",BgColor,TxColor,BgImage);
+   fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ou);
+   write_logo_image(fp_ou);
+
+   fprintf(fp_ou,"<tr><th align=\"center\"><b><font color=\"%s\" size=\"+1\">%s</font></b></th></tr>\n",TiColor,Title);
+   fprintf(fp_ou,"<tr><td align=\"center\" bgcolor=\"%s\"><font size=\"%s\">",HeaderBgColor,FontSize);
+   fprintf(fp_ou,_("Period: %s"),period.html);
+   fputs("</font></td></tr>\n",fp_ou);
+   fprintf(fp_ou,"<tr><th bgcolor=\"%s\" align=\"center\"><font size=\"%s\">%s</font></th></tr>\n",HeaderBgColor,FontSize,_("SmartFilter"));
+   fputs("</table></div>\n",fp_ou);
+
+   fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"2\">\n",fp_ou);
    fputs("<tr><td></td></tr>\n",fp_ou);
    fputs("<tr><td></td></tr>\n",fp_ou);
    fputs("<tr><td></td></tr>\n",fp_ou);
-   fprintf(fp_ou,"<tr><th bgcolor=%s><font size=%s>%s</font></th><th bgcolor=%s><font size=%s>%s</font></th><th bgcolor=%s><font size=%s>%s</font></th><th bgcolor=%s><font size=%s>%s</font></th><th bgcolor=%s><font size=%s>%s</font></th></tr>\n",HeaderBgColor,FontSize,text[98],HeaderBgColor,FontSize,text[111],HeaderBgColor,FontSize,text[110],HeaderBgColor,FontSize,text[91],HeaderBgColor,FontSize,smartheader);
+   fprintf(fp_ou,"<tr><th bgcolor=%s><font size=\"%s\">%s</font></th><th bgcolor=\"%s\"><font size=\"%s\">%s</font></th><th bgcolor=\"%s\"><font size=\"%s\">%s</font></th><th bgcolor=\"%s\"><font size=\"%s\">%s</font></th><th bgcolor=\"%s\"><font size=\"%s\">%s</font></th></tr>\n",HeaderBgColor,FontSize,_("USERID"),HeaderBgColor,FontSize,_("IP/NAME"),HeaderBgColor,FontSize,_("DATE/TIME"),HeaderBgColor,FontSize,_("ACCESSED SITE"),HeaderBgColor,FontSize,smartheader);
 
    while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
-      if (getword(user,sizeof(user),buf,' ')<0 || getword(data,sizeof(data),buf,' ')<0 ||
-          getword(hora,sizeof(hora),buf,' ')<0 || getword(ip,sizeof(ip),buf,' ')<0 ||
-          getword(url,sizeof(url),buf,' ')<0 || getword(smartcat,sizeof(smartcat),buf,'\n')<0) {
-         printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",smart_ou);
-         exit(1);
+      getword_start(&gwarea,buf);
+      if (getword(user,sizeof(user),&gwarea,'\t')<0 || getword(data,sizeof(data),&gwarea,'\t')<0 ||
+          getword(hora,sizeof(hora),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0 ||
+          getword(url,sizeof(url),&gwarea,'\t')<0 || getword(smartcat,sizeof(smartcat),&gwarea,'\n')<0) {
+         debuga(_("There is a broken record or garbage in file %s\n"),smart_ou);
+         exit(EXIT_FAILURE);
       }
 
-      if((str=(char *) strstr(user, "_")) != (char *) NULL ) {
-         if((str=(char *) strstr(str+1, "_")) != (char *) NULL )
-            fixip(user);
+      uinfo=userinfo_find_from_id(user);
+      if (!uinfo) {
+         debuga(_("Unknown user ID %s in file %s\n"),user,smart_ou);
+         exit(EXIT_FAILURE);
       }
-
       if(strcmp(ouser,user) != 0) {
          strcpy(ouser,user);
-         sprintf(smartuser,"%s/denied_%s.html",dirname,user);
+         sprintf(smartuser,"%s/denied_%s.html",outdirname,uinfo->filename);
          if(fuser) {
             fuser=0;
             fputs("</table>\n",fp_user);
-            if(strcmp(ShowSargInfo,"yes") == 0) {
-               zdate(ftime, DateFormat);
-               sprintf(html2,"<br><br><center><font size=-2>%s <a href='%s'>%s-%s</a> %s %s</font></center>\n",text[108],URL,PGM,VERSION,text[109],ftime);
-               fputs(html2,fp_user);
-           }
+            if(ShowSargInfo) {
+               zdate(ftime, sizeof(ftime), DateFormat);
+               fprintf(fp_user,"<br><br><div align=\"center\"><font size=\"-2\">%s <a href=\"%s\">%s-%s</a> %s %s</font></div>\n",_("Generated by"),URL,PGM,VERSION,_("on"),ftime);
+            }
             fputs("</body>\n</html>\n",fp_user);
             fclose(fp_user);
          }
          if ((fp_user = fopen(smartuser, "a")) == 0) {
-            fprintf(stderr, "SARG: (smartfilter) %s: %s\n",text[45],smartuser);
-            exit(1);
+            debuga(_("(smartfilter) Cannot open file %s\n"),smartuser);
+            exit(EXIT_FAILURE);
          }
          fuser=1;
 
@@ -146,42 +142,46 @@ void smartfilter_report(void)
          fputs(" \"http://www.w3.org/TR/html4/loose.dtd\">\n",fp_ou);
          fputs("<html>\n",fp_user);
          fputs("<head>\n",fp_user);
-         sprintf(html,"  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet);
-         fputs(html,fp_user);
+         fprintf(fp_user,"  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet);
          fputs("</head>\n",fp_user);
 
-         if(strlen(FontFace) > 0) {
+         if(FontFace[0] != 0) {
+            /*
+            Before merging the sprintf and the fputs, the code looked like this:
             sprintf(html2,"<font face=%s>\n",FontFace);
             fputs(url,fp_user);
+            The two lines don't use the same buffer so the string formated by sprintf is not the string
+            written to fp_user. I (fmarchal) assumed it was a typo and replaced it by a fprintf but
+            that font tag is not valid outside of the body. So, the generated html was likely
+            containing garbage not rendered by the browser.
+            */
+            fprintf(fp_user,"<font face=%s>\n",FontFace);
          }
-         sprintf(html2,"<body bgcolor=%s text=%s background='%s'>\n",BgColor,TxColor,BgImage);
-         fputs(html2,fp_user);
-         fputs("<center><table cellpadding=0 cellspacing=0>\n",fp_user);
-         if(strlen(LogoImage) > 0) fprintf(fp_user,"<tr><th align=left><img src='%s' border=0 align=absmiddle width=%s height=%s><font color=%s>%s</font>\n",LogoImage,Width,Height,LogoTextColor,LogoText);
-         fprintf(fp_user,"<tr><th align=center><b><font color=%s size=+1>%s</font></b></th></tr>\n",TiColor,Title);
-         fprintf(fp_user,"<tr><td align=center bgcolor=%s><font size=%s>%s: %s</font></td></tr>\n",HeaderBgColor,FontSize,text[89],period);
-         fprintf(fp_user,"<tr><td align=center bgcolor=%s><font size=%s>%s:</font><font size=%s> %s</font></td></tr>\n",HeaderBgColor,FontSize,text[90],FontSize,user);
-         fputs("</table></center>\n",fp_user);
-         fputs("<center><table cellpadding=0 cellspacing=2>\n",fp_user);
+         fprintf(fp_user,"<body bgcolor=\"%s\" text=\"%s\" background=\"%s\">\n",BgColor,TxColor,BgImage);
+         fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_user);
+         if(LogoImage[0]!='\0') fprintf(fp_user,"<tr><th align=left><img src=\"%s\" border=\"0\" align=\"absmiddle\" width=\"%s\" height=\"%s\"><font color=\"%s\">%s</font>\n",LogoImage,Width,Height,LogoTextColor,LogoText);
+         fprintf(fp_user,"<tr><th align=\"center\"><b><font color=\"%s\" size=\"+1\">%s</font></b></th></tr>\n",TiColor,Title);
+         fputs("<tr><td align=center bgcolor=\"%s\"><font size=\"%s\">",fp_user);
+         fprintf(fp_user,_("Period: %s"),period.html);
+         fputs("</font></td></tr>\n",fp_user);
+         fprintf(fp_user,"<tr><td align=\"center\" bgcolor=\"%s\"><font size=\"%s\">%s:</font><font size=\"%s\"> %s</font></td></tr>\n",HeaderBgColor,FontSize,_("User"),FontSize,uinfo->label);
+         fputs("</table></div>\n",fp_user);
+         fputs("<div align=\"center\"><table cellpadding=0 cellspacing=2>\n",fp_user);
          fputs("<tr><td></td></tr>\n",fp_user);
          fputs("<tr><td></td></tr>\n",fp_user);
          fputs("<tr><td></td></tr>\n",fp_user);
-         sprintf(html2,"<tr><th bgcolor=%s><font size=%s>%s</font></th><th bgcolor=%s><font size=%s>%s</font></th><th bgcolor=%s><font size=%s>%s</font></th><th bgcolor=%s><font size=%s>%s</font></th><th bgcolor=%s><font size=%s>%s</font></th></tr>\n",HeaderBgColor,FontSize,text[98],HeaderBgColor,FontSize,text[111],HeaderBgColor,FontSize,text[110],HeaderBgColor,FontSize,text[91],HeaderBgColor,FontSize,smartheader);
-         fputs(html2,fp_user);
+         fprintf(fp_user,"<tr><th bgcolor=%s><font size=%s>%s</font></th><th bgcolor=%s><font size=%s>%s</font></th><th bgcolor=%s><font size=%s>%s</font></th><th bgcolor=%s><font size=%s>%s</font></th><th bgcolor=%s><font size=%s>%s</font></th></tr>\n",HeaderBgColor,FontSize,_("USERID"),HeaderBgColor,FontSize,_("IP/NAME"),HeaderBgColor,FontSize,_("DATE/TIME"),HeaderBgColor,FontSize,_("ACCESSED SITE"),HeaderBgColor,FontSize,smartheader);
       }
-      sprintf(html2,"<tr><td bgcolor=%s align=center><font size=%s>%s</font></td><td bgcolor=%s align=center><font size=%s>%s</font></td><td bgcolor=%s align=center><font size=%s>%s-%s</font></td><td bgcolor=%s><font size=%s>%s</font></td><td bgcolor=%s><font size=%s>%s</font></td></th>\n",TxBgColor,FontSize,user,TxBgColor,FontSize,ip,TxBgColor,FontSize,data,hora,TxBgColor,FontSize,url,TxBgColor,FontSize,smartcat);
-      fputs(html2,fp_user);
+      fprintf(fp_user,"<tr><td bgcolor=%s align=center><font size=%s>%s</font></td><td bgcolor=%s align=center><font size=%s>%s</font></td><td bgcolor=%s align=center><font size=%s>%s-%s</font></td><td bgcolor=%s><font size=%s>%s</font></td><td bgcolor=%s><font size=%s>%s</font></td></th>\n",TxBgColor,FontSize,uinfo->label,TxBgColor,FontSize,ip,TxBgColor,FontSize,data,hora,TxBgColor,FontSize,url,TxBgColor,FontSize,smartcat);
 
-      sprintf(html,"<tr><td bgcolor=%s align=center><font size=%s>%s</font></td><td bgcolor=%s align=center><font size=%s>%s</font></td><td bgcolor=%s align=center><font size=%s>%s-%s</font></td><td bgcolor=%s><font size=%s>%s</font></td><td bgcolor=%s><font size=%s>%s</font></td></th>\n",TxBgColor,FontSize,user,TxBgColor,FontSize,ip,TxBgColor,FontSize,data,hora,TxBgColor,FontSize,url,TxBgColor,FontSize,smartcat);
-      fputs(html,fp_ou);
+      fprintf(fp_ou,"<tr><td bgcolor=%s align=center><font size=%s>%s</font></td><td bgcolor=%s align=center><font size=%s>%s</font></td><td bgcolor=%s align=center><font size=%s>%s-%s</font></td><td bgcolor=%s><font size=%s>%s</font></td><td bgcolor=%s><font size=%s>%s</font></td></th>\n",TxBgColor,FontSize,uinfo->label,TxBgColor,FontSize,ip,TxBgColor,FontSize,data,hora,TxBgColor,FontSize,url,TxBgColor,FontSize,smartcat);
    }
 
    fputs("</table>\n",fp_ou);
 
-   if(strcmp(ShowSargInfo,"yes") == 0) {
-      zdate(ftime, DateFormat);
-      sprintf(html,"<br><br><center><font size=-2>%s <a href='%s'>%s-%s</a> %s %s</font></center>\n",text[108],URL,PGM,VERSION,text[109],ftime);
-      fputs(html,fp_ou);
+   if(ShowSargInfo) {
+      zdate(ftime, sizeof(ftime), DateFormat);
+      fprintf(fp_ou,"<br><br><div align=\"center\"><font size=\"-2\">%s <a href=\"%s\">%s-%s</a> %s %s</font></div>\n",_("Generated by"),URL,PGM,VERSION,_("on"),ftime);
    }
 
    fputs("</body>\n</html>\n",fp_user);
@@ -189,10 +189,9 @@ void smartfilter_report(void)
    fclose(fp_ou);
    if(fp_user) {
       fputs("</table>\n",fp_user);
-      if(strcmp(ShowSargInfo,"yes") == 0) {
-         zdate(ftime, DateFormat);
-         sprintf(html2,"<br><br><center><font size=-2>%s <a href='%s'>%s-%s</a> %s %s</font></center>\n",text[108],URL,PGM,VERSION,text[109],ftime);
-         fputs(html2,fp_user);
+      if(ShowSargInfo) {
+         zdate(ftime, sizeof(ftime), DateFormat);
+         fprintf(fp_user,"<br><br><div align=\"center\"><font size=\"-2\">%s <a href=\"%s\">%s-%s</a> %s %s</font></div>\n",_("Generated by"),URL,PGM,VERSION,_("on"),ftime);
       }
       fputs("</body>\n</html>\n",fp_user);
       fclose(fp_user);