]> git.ipfire.org Git - thirdparty/sarg.git/blobdiff - redirector.c
Optimize away a useless strcpy.
[thirdparty/sarg.git] / redirector.c
index a0678ee2912bc5b1488f7111edfb358afe84477f..10a047e40d86ec9f6bae1de20e56f55523fb55ef 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * SARG Squid Analysis Report Generator      http://sarg.sourceforge.net
- *                                                            1998, 2013
+ *                                                            1998, 2015
  *
  * SARG donations:
  *      please look at http://sarg.sourceforge.net/donations.php
@@ -35,16 +35,21 @@ static int RedirectorErrors=0;
 //! The file containing the sorted entries.
 static char redirector_sorted[MAXLEN]="";
 
-static void parse_log(FILE *fp_ou,char *buf)
+extern char StripUserSuffix[MAX_USER_LEN];
+extern int StripSuffixLen;
+
+static void parse_log(FILE *fp_ou,char *buf,int dfrom,int duntil)
 {
        char leks[5], sep[2], res[MAXLEN];
        char hour[15];
        char source[128], list[128];
        char full_url[MAX_URL_LEN];
        const char *url;
-       char user[MAX_USER_LEN];
+       char UserBuf[MAX_USER_LEN];
+       const char *user;
        char ip[45];
        char userlabel[MAX_USER_LEN];
+       char IpBuf[MAX_USER_LEN];
        long long int lmon, lday, lyear;
        int mon, day, year;
        int  idata=0;
@@ -52,13 +57,14 @@ static void parse_log(FILE *fp_ou,char *buf)
        struct getwordstruct gwarea;
        struct getwordstruct gwarea1;
        struct userinfostruct *uinfo;
+       enum UserProcessError PUser;
 
        getword_start(&gwarea,buf);
        if(RedirectorLogFormat[0] != '\0') {
                getword_start(&gwarea1,RedirectorLogFormat);
                leks[0]='\0';
                if (getword(leks,sizeof(leks),&gwarea1,'#')<0) {
-                       debuga(_("Invalid \"redirector_log_format\" option in your sarg.conf (too many characters before first tag)\n"));
+                       debuga(__FILE__,__LINE__,_("Invalid \"redirector_log_format\" option in your sarg.conf (too many characters before first tag)\n"));
                        exit(EXIT_FAILURE);
                }
                year=0;
@@ -68,20 +74,20 @@ static void parse_log(FILE *fp_ou,char *buf)
                source[0]='\0';
                list[0]='\0';
                ip[0]='\0';
-               user[0]='\0';
+               UserBuf[0]='\0';
                full_url[0]='\0';
                while(strcmp(leks,"end") != 0) {
                        if (getword(leks,sizeof(leks),&gwarea1,'#')<0) {
-                               debuga(_("Invalid \"redirector_log_format\" option in your sarg.conf (missing # at end of tag)\n"));
+                               debuga(__FILE__,__LINE__,_("Invalid \"redirector_log_format\" option in your sarg.conf (missing # at end of tag)\n"));
                                exit(EXIT_FAILURE);
                        }
                        if (getword(sep,sizeof(sep),&gwarea1,'#')<0) {
-                               debuga(_("Invalid \"redirector_log_format\" option in your sarg.conf (too many characters in column separator)\n"));
+                               debuga(__FILE__,__LINE__,_("Invalid \"redirector_log_format\" option in your sarg.conf (too many characters in column separator)\n"));
                                exit(EXIT_FAILURE);
                        }
                        if(strcmp(leks,"end") != 0) {
                                if (getword_limit(res,sizeof(res),&gwarea,sep[0])<0) {
-                                       debuga(_("Parsing of tag \"%s\" in redirector log %s returned no result\n"),leks,wentp);
+                                       debuga(__FILE__,__LINE__,_("Parsing of tag \"%s\" in redirector log \"%s\" returned no result\n"),leks,wentp);
                                        RedirectorErrors++;
                                        return;
                                }
@@ -93,39 +99,39 @@ static void parse_log(FILE *fp_ou,char *buf)
                                        day=atoi(res);
                                } else if(strcmp(leks,"hour") == 0) {
                                        if (strlen(res)>=sizeof(hour)) {
-                                               debuga(_("Hour string too long in redirector log file %s\n"),wentp);
+                                               debuga(__FILE__,__LINE__,_("Hour string too long in redirector log file \"%s\"\n"),wentp);
                                                RedirectorErrors++;
                                                return;
                                        }
                                        strcpy(hour,res);
                                } else if(strcmp(leks,"source") == 0) {
                                        if (strlen(res)>=sizeof(source)) {
-                                               debuga(_("Banning source name too long in redirector log file %s\n"),wentp);
+                                               debuga(__FILE__,__LINE__,_("Banning source name too long in redirector log file \"%s\"\n"),wentp);
                                                RedirectorErrors++;
                                                return;
                                        }
                                        strcpy(source,res);
                                } else if(strcmp(leks,"list") == 0) {
                                        if (strlen(res)>=sizeof(list)) {
-                                               debuga(_("Banning list name too long in redirector log file %s\n"),wentp);
+                                               debuga(__FILE__,__LINE__,_("Banning list name too long in redirector log file \"%s\"\n"),wentp);
                                                RedirectorErrors++;
                                                return;
                                        }
                                        strcpy(list,res);
                                } else if(strcmp(leks,"ip") == 0) {
                                        if (strlen(res)>=sizeof(ip)) {
-                                               debuga(_("IP address too long in redirector log file %s\n"),wentp);
+                                               debuga(__FILE__,__LINE__,_("IP address too long in redirector log file \"%s\"\n"),wentp);
                                                RedirectorErrors++;
                                                return;
                                        }
                                        strcpy(ip,res);
                                } else if(strcmp(leks,"user") == 0) {
-                                       if (strlen(res)>=sizeof(user)) {
-                                               debuga(_("User ID too long in redirector log file %s\n"),wentp);
+                                       if (strlen(res)>=sizeof(UserBuf)) {
+                                               debuga(__FILE__,__LINE__,_("User ID too long in redirector log file \"%s\"\n"),wentp);
                                                RedirectorErrors++;
                                                return;
                                        }
-                                       strcpy(user,res);
+                                       strcpy(UserBuf,res);
                                } else if(strcmp(leks,"url") == 0) {
                                        /*
                                         * Don't worry about the url being truncated as we only keep the host name
@@ -138,7 +144,7 @@ static void parse_log(FILE *fp_ou,char *buf)
        } else {
                if (getword_atoll(&lyear,&gwarea,'-')<0 || getword_atoll(&lmon,&gwarea,'-')<0 ||
                                getword_atoll(&lday,&gwarea,' ')<0) {
-                       debuga(_("Invalid date found in file %s\n"),wentp);
+                       debuga(__FILE__,__LINE__,_("Invalid date in file \"%s\"\n"),wentp);
                        RedirectorErrors++;
                        return;
                }
@@ -146,32 +152,32 @@ static void parse_log(FILE *fp_ou,char *buf)
                mon=(int)lmon;
                day=(int)lday;
                if (getword(hour,sizeof(hour),&gwarea,' ')<0) {
-                       debuga(_("Invalid time found in file %s\n"),wentp);
+                       debuga(__FILE__,__LINE__,_("Invalid time in file \"%s\"\n"),wentp);
                        RedirectorErrors++;
                        return;
                }
                if (getword_skip(MAXLEN,&gwarea,'(')<0 || getword(source,sizeof(source),&gwarea,'/')<0) {
-                       debuga(_("Invalid redirected source in file %s\n"),wentp);
+                       debuga(__FILE__,__LINE__,_("Invalid redirected source in file \"%s\"\n"),wentp);
                        RedirectorErrors++;
                        return;
                }
                if (getword(list,sizeof(list),&gwarea,'/')<0) {
-                       debuga(_("Invalid redirected list in file %s\n"),wentp);
+                       debuga(__FILE__,__LINE__,_("Invalid redirected list in file \"%s\"\n"),wentp);
                        RedirectorErrors++;
                        return;
                }
                if (getword_skip(MAXLEN,&gwarea,' ')<0 || getword_limit(full_url,sizeof(full_url),&gwarea,' ')<0) {
-                       debuga(_("Invalid URL in file %s\n"),wentp);
+                       debuga(__FILE__,__LINE__,_("Invalid url in file \"%s\"\n"),wentp);
                        RedirectorErrors++;
                        return;
                }
                if (getword(ip,sizeof(ip),&gwarea,'/')<0) {
-                       debuga(_("Invalid source IP in file %s\n"),wentp);
+                       debuga(__FILE__,__LINE__,_("Invalid source IP in file \"%s\"\n"),wentp);
                        RedirectorErrors++;
                        return;
                }
-               if (getword_skip(MAXLEN,&gwarea,' ')<0 || getword(user,sizeof(user),&gwarea,' ')<0) {
-                       debuga(_("Invalid user in file %s\n"),wentp);
+               if (getword_skip(MAXLEN,&gwarea,' ')<0 || getword(UserBuf,sizeof(UserBuf),&gwarea,' ')<0) {
+                       debuga(__FILE__,__LINE__,_("Invalid user in file \"%s\"\n"),wentp);
                        RedirectorErrors++;
                        return;
                }
@@ -180,33 +186,25 @@ static void parse_log(FILE *fp_ou,char *buf)
 
        //sprintf(warea,"%04d%02d%02d",year,mon,day);
 
-       if(RedirectorFilterOutDate) {
+       if (RedirectorFilterOutDate) {
                idata = year*10000+mon*100+day;
                if(idata < dfrom || idata > duntil)
                        return;
        }
 
-       if(UserIp) {
-               strcpy(user,ip);
-               id_is_ip=true;
-       } else {
-               id_is_ip=false;
-               if (user[0]=='\0' || (user[1]=='\0' && (user[0]=='-' || user[0]==' '))) {
-                       if(RecordsWithoutUser == RECORDWITHOUTUSER_IP) {
-                               strcpy(user,ip);
-                               id_is_ip=true;
-                       }
-                       if(RecordsWithoutUser == RECORDWITHOUTUSER_IGNORE)
-                               return;
-                       if(RecordsWithoutUser == RECORDWITHOUTUSER_EVERYBODY)
-                               strcpy(user,"everybody");
-               }
-       }
+       user=UserBuf;
+       PUser=process_user(&user,ip,&id_is_ip);
+       if (PUser!=USERERR_NoError) return;
+
        uinfo=userinfo_find_from_id(user);
        if (!uinfo) {
                uinfo=userinfo_create(user,(id_is_ip) ? NULL : ip);
                uinfo->no_report=true;
-               if(Ip2Name && id_is_ip) ip2name(user,sizeof(user));
+               if (Ip2Name && id_is_ip) {
+                       strcpy(IpBuf,user);
+                       ip2name(IpBuf,sizeof(IpBuf));
+                       user=IpBuf;
+               }
                user_find(userlabel,MAX_USER_LEN, user);
                userinfo_label(uinfo,userlabel);
        }
@@ -222,13 +220,13 @@ static void parse_log(FILE *fp_ou,char *buf)
 
 static void read_log(const char *wentp, FILE *fp_ou,int dfrom,int duntil)
 {
-       FILE *fp_in = NULL;
+       FileObject *fp_in = NULL;
        char *buf;
        int  i;
        longline line;
 
        if(debug) {
-               debuga(_("Reading redirector log file %s\n"),wentp);
+               debuga(__FILE__,__LINE__,_("Reading redirector log file \"%s\"\n"),wentp);
        }
 
        /* With squidGuard, you can log groups in only one log file.
@@ -255,29 +253,32 @@ static void read_log(const char *wentp, FILE *fp_ou,int dfrom,int duntil)
        nfiles_done++;
        files_done = realloc(files_done, nfiles_done*sizeof(char *));
        if (!files_done) {
-               debuga(_("Not enough memory to store the name of the new redirector log to be read - %s\n"),strerror(errno));
+               debuga(__FILE__,__LINE__,_("Not enough memory to store the name of the new redirector log to be read - %s\n"),strerror(errno));
                exit(EXIT_FAILURE);
        }
        files_done[nfiles_done-1] = strdup(wentp);
        if (!files_done[nfiles_done-1]) {
-               debuga(_("Not enough memory to store the name of the new redirector log to be read - %s\n"),strerror(errno));
+               debuga(__FILE__,__LINE__,_("Not enough memory to store the name of the new redirector log to be read - %s\n"),strerror(errno));
                exit(EXIT_FAILURE);
        }
 
-       if ((fp_in=fopen(wentp,"r"))==NULL) {
-               debuga(_("(squidguard) Cannot open log file %s: %s\n"),wentp,strerror(errno));
+       if ((fp_in=FileObject_Open(wentp))==NULL) {
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),wentp,FileObject_GetLastOpenError());
                exit(EXIT_FAILURE);
        }
 
        if ((line=longline_create())==NULL) {
-               debuga(_("Not enough memory to read the redirector log\n"));
+               debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\"\n"),wentp);
                exit(EXIT_FAILURE);
        }
 
        while ((buf=longline_read(fp_in,line)) != NULL) {
-               parse_log(fp_ou,buf);
+               parse_log(fp_ou,buf,dfrom,duntil);
+       }
+       if (FileObject_Close(fp_in)) {
+               debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),wentp,FileObject_GetLastCloseError());
+               exit(EXIT_FAILURE);
        }
-       fclose(fp_in);
        longline_destroy(&line);
        return;
 }
@@ -301,30 +302,29 @@ void redirector_log(void)
        str2 = user;
 
        if(SquidGuardConf[0] == '\0' && NRedirectorLogs == 0) {
-               if (debugz) debugaz(_("No redirector logs provided to produce that kind of report\n"));
+               if (debugz>=LogLevel_Process) debugaz(__FILE__,__LINE__,_("No redirector logs provided to produce that kind of report\n"));
                return;
        }
 
        snprintf(guard_in,sizeof(guard_in),"%s/redirector.int_unsort",tmp);
        if((fp_ou=fopen(guard_in,"w"))==NULL) {
-               debuga(_("(squidguard) Cannot open log file %s: %s\n"),guard_in,strerror(errno));
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),guard_in,strerror(errno));
                exit(EXIT_FAILURE);
        }
 
-       dfrom=(period.start.tm_year+1900)*10000+(period.start.tm_mon+1)*100+period.start.tm_mday;
-       duntil=(period.end.tm_year+1900)*10000+(period.end.tm_mon+1)*100+period.end.tm_mday;
+       getperiod_torange(&period,&dfrom,&duntil);
 
        if (NRedirectorLogs>0) {
                for (i=0 ; i<NRedirectorLogs ; i++)
                        read_log(RedirectorLogs[i],fp_ou,dfrom,duntil);
        } else {
                if(access(SquidGuardConf, R_OK) != 0) {
-                       debuga(_("Cannot open squidGuard config file: %s\n"),SquidGuardConf);
+                       debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),SquidGuardConf,strerror(errno));
                        exit(EXIT_FAILURE);
                }
 
                if((fp_guard=fopen(SquidGuardConf,"r"))==NULL) {
-                       debuga(_("(squidguard) Cannot open log file %s: %s\n"),SquidGuardConf,strerror(errno));
+                       debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),SquidGuardConf,strerror(errno));
                        exit(EXIT_FAILURE);
                }
 
@@ -373,11 +373,14 @@ void redirector_log(void)
                                read_log(wentp,fp_ou,dfrom,duntil);
                        }
                }
+               if (fclose(fp_guard)==EOF) {
+                       debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),SquidGuardConf,strerror(errno));
+                       exit(EXIT_FAILURE);
+               }
        }
 
-       if (fp_guard) fclose(fp_guard);
        if (fp_ou && fclose(fp_ou)==EOF) {
-               debuga(_("Write error in %s: %s\n"),guard_in,strerror(errno));
+               debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),guard_in,strerror(errno));
                exit(EXIT_FAILURE);
        }
 
@@ -390,23 +393,23 @@ void redirector_log(void)
        if (redirector_count) {
                snprintf(redirector_sorted,sizeof(redirector_sorted),"%s/redirector.int_log",tmp);
                if(debug) {
-                       debuga(_("Sorting file: %s\n"),redirector_sorted);
+                       debuga(__FILE__,__LINE__,_("Sorting file \"%s\"\n"),redirector_sorted);
                }
 
                if (snprintf(tmp6,sizeof(tmp6),"sort -t \"\t\" -k 1,1 -k 2,2 -k 4,4 \"%s\" -o \"%s\"",guard_in, redirector_sorted)>=sizeof(tmp6)) {
-                       debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),guard_in,redirector_sorted);
+                       debuga(__FILE__,__LINE__,_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),guard_in,redirector_sorted);
                        exit(EXIT_FAILURE);
                }
                cstatus=system(tmp6);
                if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
-                       debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
-                       debuga(_("sort command: %s\n"),tmp6);
+                       debuga(__FILE__,__LINE__,_("sort command return status %d\n"),WEXITSTATUS(cstatus));
+                       debuga(__FILE__,__LINE__,_("sort command: %s\n"),tmp6);
                        exit(EXIT_FAILURE);
                }
        }
 
        if (!KeepTempLog && unlink(guard_in)) {
-               debuga(_("Cannot delete \"%s\": %s\n"),guard_in,strerror(errno));
+               debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),guard_in,strerror(errno));
                exit(EXIT_FAILURE);
        }
        return;
@@ -422,7 +425,8 @@ static void show_ignored_redirector(FILE *fp_ou,int count)
 
 void redirector_report(void)
 {
-       FILE *fp_in = NULL, *fp_ou = NULL;
+       FileObject *fp_in = NULL;
+       FILE *fp_ou = NULL;
 
        char *buf;
        char *url;
@@ -449,27 +453,27 @@ void redirector_report(void)
        ouser2[0]='\0';
 
        if(!redirector_count) {
-               if (debugz) {
+               if (debugz>=LogLevel_Process) {
                        if (redirector_sorted[0])
-                               debugaz(_("Redirector report not generated because it is empty\n"));
+                               debugaz(__FILE__,__LINE__,_("Redirector report not generated because it is empty\n"));
                }
                return;
        }
 
        snprintf(report,sizeof(report),"%s/redirector.html",outdirname);
 
-       if((fp_in=fopen(redirector_sorted,"r"))==NULL) {
-               debuga(_("(squidguard) Cannot open log file %s: %s\n"),redirector_sorted,strerror(errno));
+       if((fp_in=FileObject_Open(redirector_sorted))==NULL) {
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),redirector_sorted,FileObject_GetLastOpenError());
                exit(EXIT_FAILURE);
        }
 
        if((fp_ou=fopen(report,"w"))==NULL) {
-               debuga(_("(squidguard) Cannot open log file %s: %s\n"),report,strerror(errno));
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),report,strerror(errno));
                exit(EXIT_FAILURE);
        }
 
        if ((line=longline_create())==NULL) {
-               debuga(_("Not enough memory to read the processed redirector log\n"));
+               debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\"\n"),redirector_sorted);
                exit(EXIT_FAILURE);
        }
 
@@ -486,37 +490,37 @@ void redirector_report(void)
        while((buf=longline_read(fp_in,line))!=NULL) {
                getword_start(&gwarea,buf);
                if (getword(user,sizeof(user),&gwarea,'\t')<0) {
-                       debuga(_("Invalid user in file %s\n"),redirector_sorted);
+                       debuga(__FILE__,__LINE__,_("Invalid user in file \"%s\"\n"),redirector_sorted);
                        exit(EXIT_FAILURE);
                }
                if (getword_atoll(&data2,&gwarea,'\t')<0) {
-                       debuga(_("Invalid date in file %s\n"),redirector_sorted);
+                       debuga(__FILE__,__LINE__,_("Invalid date in file \"%s\"\n"),redirector_sorted);
                        exit(EXIT_FAILURE);
                }
                if (getword(hora,sizeof(hora),&gwarea,'\t')<0) {
-                       debuga(_("Invalid time in file %s\n"),redirector_sorted);
+                       debuga(__FILE__,__LINE__,_("Invalid time in file \"%s\"\n"),redirector_sorted);
                        exit(EXIT_FAILURE);
                }
                if (getword(ip,sizeof(ip),&gwarea,'\t')<0) {
-                       debuga(_("Invalid IP address in file %s\n"),redirector_sorted);
+                       debuga(__FILE__,__LINE__,_("Invalid IP address in file \"%s\"\n"),redirector_sorted);
                        exit(EXIT_FAILURE);
                }
                if (getword_ptr(buf,&url,&gwarea,'\t')<0) {
-                       debuga(_("Invalid URL in file %s\n"),redirector_sorted);
+                       debuga(__FILE__,__LINE__,_("Invalid url in file \"%s\"\n"),redirector_sorted);
                        exit(EXIT_FAILURE);
                }
                if (getword(rule,sizeof(rule),&gwarea,'\n')<0) {
-                       debuga(_("Invalid rule in file %s\n"),redirector_sorted);
+                       debuga(__FILE__,__LINE__,_("Invalid rule in file \"%s\"\n"),redirector_sorted);
                        exit(EXIT_FAILURE);
                }
 
                uinfo=userinfo_find_from_id(user);
                if (!uinfo) {
-                       debuga(_("Unknown user ID %s in file %s\n"),user,redirector_sorted);
+                       debuga(__FILE__,__LINE__,_("Unknown user ID %s in file \"%s\"\n"),user,redirector_sorted);
                        exit(EXIT_FAILURE);
                }
 
-               computedate(data2/10000,(data2/100)%10,data2%100,&t);
+               computedate(data2/10000,(data2/100)%100,data2%100,&t);
                strftime(data,sizeof(data),"%x",&t);
 
                new_user=false;
@@ -561,7 +565,10 @@ void redirector_report(void)
                output_html_link(fp_ou,url,100);
                fprintf(fp_ou,"</td><td class=\"data2\">%s</td></tr>\n",rule);
        }
-       fclose(fp_in);
+       if (FileObject_Close(fp_in)) {
+               debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),redirector_sorted,FileObject_GetLastCloseError());
+               exit(EXIT_FAILURE);
+       }
        longline_destroy(&line);
 
        if(count>SquidGuardReportLimit && SquidGuardReportLimit>0)
@@ -577,15 +584,14 @@ void redirector_report(void)
        }
 
        fputs("</div>\n",fp_ou);
-       if (write_html_trailer(fp_ou)<0)
-               debuga(_("Write error in file %s\n"),report);
+       write_html_trailer(fp_ou);
        if (fclose(fp_ou)==EOF) {
-               debuga(_("Write error in %s: %s\n"),report,strerror(errno));
+               debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),report,strerror(errno));
                exit(EXIT_FAILURE);
        }
 
        if (!KeepTempLog && unlink(redirector_sorted)) {
-               debuga(_("Cannot delete \"%s\": %s\n"),redirector_sorted,strerror(errno));
+               debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),redirector_sorted,strerror(errno));
                exit(EXIT_FAILURE);
        }