"Fossies" - the Fresh Open Source Software Archive

Member "sarg-2.4.0/dansguardian_log.c" (24 Dec 2019, 6439 Bytes) of package /linux/privat/sarg-2.4.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "dansguardian_log.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.3.11_vs_2.4.0.

    1 /*
    2  * SARG Squid Analysis Report Generator      http://sarg.sourceforge.net
    3  *                                                            1998, 2015
    4  *
    5  * SARG donations:
    6  *      please look at http://sarg.sourceforge.net/donations.php
    7  * Support:
    8  *     http://sourceforge.net/projects/sarg/forums/forum/363374
    9  * ---------------------------------------------------------------------
   10  *
   11  *  This program is free software; you can redistribute it and/or modify
   12  *  it under the terms of the GNU General Public License as published by
   13  *  the Free Software Foundation; either version 2 of the License, or
   14  *  (at your option) any later version.
   15  *
   16  *  This program is distributed in the hope that it will be useful,
   17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   19  *  GNU General Public License for more details.
   20  *
   21  *  You should have received a copy of the GNU General Public License
   22  *  along with this program; if not, write to the Free Software
   23  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
   24  *
   25  */
   26 
   27 #include "include/conf.h"
   28 #include "include/defs.h"
   29 
   30 void dansguardian_log(const struct ReadLogDataStruct *ReadFilter)
   31 {
   32     FILE *fp_in = NULL, *fp_ou = NULL, *fp_guard = NULL;
   33     char buf[MAXLEN];
   34     char guard_in[MAXLEN];
   35     char guard_ou[MAXLEN];
   36     char loglocation[MAXLEN] = "/var/log/dansguardian/access.log";
   37     int year, mon, day;
   38     int hour,min,sec;
   39     char user[MAXLEN], code1[255], code2[255];
   40     char ip[45];
   41     char *url;
   42     char tmp6[MAXLEN];
   43     int  idata=0;
   44     int cstatus;
   45     int dfrom, duntil;
   46     struct getwordstruct gwarea;
   47 
   48     getperiod_torange(&period,&dfrom,&duntil);
   49 
   50     format_path(__FILE__, __LINE__, guard_in, sizeof(guard_in), "%s/dansguardian.int_unsort", tmp);
   51     format_path(__FILE__, __LINE__, guard_ou, sizeof(guard_ou), "%s/dansguardian.int_log", tmp);
   52 
   53     if (access(DansGuardianConf, R_OK) != 0) {
   54         debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),DansGuardianConf,strerror(errno));
   55         exit(EXIT_FAILURE);
   56     }
   57 
   58     if ((fp_guard=fopen(DansGuardianConf,"r"))==NULL) {
   59         debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),DansGuardianConf,strerror(errno));
   60         exit(EXIT_FAILURE);
   61     }
   62 
   63     if ((fp_ou=MY_FOPEN(guard_in,"w"))==NULL) {
   64         debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),guard_in,strerror(errno));
   65         exit(EXIT_FAILURE);
   66     }
   67 
   68     while(fgets(buf,sizeof(buf),fp_guard)!=NULL) {
   69         fixendofline(buf);
   70         if (buf[0]=='#')
   71             continue;
   72         if (strstr(buf,"loglocation ") != 0) {
   73             getword_start(&gwarea,buf);
   74             if (getword_skip(MAXLEN,&gwarea,'\'')<0 || getword(loglocation,sizeof(loglocation),&gwarea,'\'')<0) {
   75                 debuga(__FILE__,__LINE__,_("Invalid record in file \"%s\"\n"),DansGuardianConf);
   76                 exit(EXIT_FAILURE);
   77             }
   78             if (debug) debuga(__FILE__,__LINE__,_("Using the dansguardian log file \"%s\" found in your configuration file \"%s\"\n"),
   79                 loglocation,DansGuardianConf);
   80             break;
   81         }
   82     }
   83     if (fclose(fp_guard)==EOF) {
   84         debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),DansGuardianConf,strerror(errno));
   85         exit(EXIT_FAILURE);
   86     }
   87 
   88     if (debug)
   89         debuga(__FILE__,__LINE__,_("Reading DansGuardian log file \"%s\"\n"),loglocation);
   90 
   91     if ((fp_in=MY_FOPEN(loglocation,"r"))==NULL) {
   92         debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),loglocation,strerror(errno));
   93         exit(EXIT_FAILURE);
   94     }
   95 
   96     while(fgets(buf,sizeof(buf),fp_in) != NULL) {
   97         if (strstr(buf," *DENIED* ") == 0)
   98             continue;
   99         getword_start(&gwarea,buf);
  100         if (getword_atoi(&year,&gwarea,'.')<0 || getword_atoi(&mon,&gwarea,'.')<0 ||
  101             getword_atoi(&day,&gwarea,' ')<0) {
  102             debuga(__FILE__,__LINE__,_("Invalid date in file \"%s\"\n"),loglocation);
  103             exit(EXIT_FAILURE);
  104         }
  105         if (getword_atoi(&hour,&gwarea,':')<0 || getword_atoi(&min,&gwarea,':')<0 || getword_atoi(&sec,&gwarea,' ')<0) {
  106             debuga(__FILE__,__LINE__,_("Invalid time in file \"%s\"\n"),loglocation);
  107             exit(EXIT_FAILURE);
  108         }
  109         if (getword(user,sizeof(user),&gwarea,' ')<0) {
  110             debuga(__FILE__,__LINE__,_("Invalid user in file \"%s\"\n"),loglocation);
  111             exit(EXIT_FAILURE);
  112         }
  113         if (getword(ip,sizeof(ip),&gwarea,' ')<0) {
  114             debuga(__FILE__,__LINE__,_("Invalid IP address in file \"%s\"\n"),loglocation);
  115             exit(EXIT_FAILURE);
  116         }
  117         if (getword_skip(MAXLEN,&gwarea,'/')<0 || getword_skip(MAXLEN,&gwarea,'/')<0) {
  118             debuga(__FILE__,__LINE__,_("Invalid record in file \"%s\"\n"),loglocation);
  119             exit(EXIT_FAILURE);
  120         }
  121         if (getword_ptr(buf,&url,&gwarea,' ')<0) {
  122             debuga(__FILE__,__LINE__,_("Invalid url in file \"%s\"\n"),loglocation);
  123             exit(EXIT_FAILURE);
  124         }
  125         if (getword_skip(255,&gwarea,' ')<0 ||
  126             getword(code1,sizeof(code1),&gwarea,' ')<0 || getword(code2,sizeof(code2),&gwarea,' ')<0) {
  127             debuga(__FILE__,__LINE__,_("Invalid record in file \"%s\"\n"),loglocation);
  128             exit(EXIT_FAILURE);
  129         }
  130         idata = year*10000+mon*100+day;
  131 
  132         if (DansguardianFilterOutDate)
  133         {
  134             if (idata < dfrom || idata > duntil)
  135                 continue;
  136             if (ReadFilter->StartTime>=0 || ReadFilter->EndTime>=0)
  137             {
  138                 int hmr=hour*100+min;
  139                 if (hmr<ReadFilter->StartTime || hmr>=ReadFilter->EndTime)
  140                     continue;
  141             }
  142         }
  143 
  144         if (strcmp(user,"-") == 0) {
  145             strcpy(user,ip);
  146             ip[0]='\0';
  147         }
  148         fprintf(fp_ou,"%s\t%d\t%02d:%02d:%02d\t%s\t%s\t%s\t%s\n",user,idata,hour,min,sec,ip,url,code1,code2);
  149         dansguardian_count++;
  150     }
  151 
  152     if (fclose(fp_in)==EOF) {
  153         debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),loglocation,strerror(errno));
  154         exit(EXIT_FAILURE);
  155     }
  156     if (fclose(fp_ou)==EOF) {
  157         debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),guard_in,strerror(errno));
  158         exit(EXIT_FAILURE);
  159     }
  160 
  161     if (debug)
  162         debuga(__FILE__,__LINE__,_("Sorting file \"%s\"\n"),guard_ou);
  163 
  164     if (snprintf(tmp6, sizeof(tmp6), "sort -t \"\t\" -k 1,1 -k 2,2 -k 4,4 \"%s\" -o \"%s\"", guard_in, guard_ou) >= sizeof(tmp6)) {
  165         debuga(__FILE__,__LINE__,_("Sort command too long when sorting file \"%s\" to \"%s\"\n"), guard_in, guard_ou);
  166         debuga_more("sort -t \"\t\" -k 1,1 -k 2,2 -k 4,4 \"%s\" -o \"%s\"", guard_in, guard_ou);
  167         exit(EXIT_FAILURE);
  168     }
  169     cstatus=system(tmp6);
  170     if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
  171         debuga(__FILE__,__LINE__,_("sort command return status %d\n"),WEXITSTATUS(cstatus));
  172         debuga(__FILE__,__LINE__,_("sort command: %s\n"),tmp6);
  173         exit(EXIT_FAILURE);
  174     }
  175     if (!KeepTempLog && unlink(guard_in)) {
  176         debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),guard_in,strerror(errno));
  177         exit(EXIT_FAILURE);
  178     }
  179 }