]> git.ipfire.org Git - thirdparty/sarg.git/blobdiff - useragent.c
Merge messages about IP addresses.
[thirdparty/sarg.git] / useragent.c
index c3f6f9aee1480557ee22df82e132ef73bf823f06..9ec1cfc244c089995d1c4f9e7e7f1f0aeb428e58 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * SARG Squid Analysis Report Generator      http://sarg.sourceforge.net
- *                                                            1998, 2011
+ *                                                            1998, 2015
  *
  * SARG donations:
  *      please look at http://sarg.sourceforge.net/donations.php
@@ -58,16 +58,16 @@ void useragent(void)
        ipbefore[0]='\0';
        namebefore[0]='\0';
 
-       sprintf(tmp3,"%s/squagent.unsort",tmp);
-       sprintf(tmp2,"%s/squagent.log",tmp);
+       sprintf(tmp3,"%s/squagent.int_unsort",tmp);
+       sprintf(tmp2,"%s/squagent.int_log",tmp);
 
        if((fp_in=fopen(UserAgentLog,"r"))==NULL) {
-               debuga(_("(useragent) Cannot open file %s\n"),UserAgentLog);
+               debuga(_("Cannot open file \"%s\": %s\n"),UserAgentLog,strerror(errno));
                exit(EXIT_FAILURE);
        }
 
        if((fp_ou=fopen(tmp3,"w"))==NULL) {
-               debuga(_("(useragent) Cannot open file %s\n"),tmp3);
+               debuga(_("Cannot open file \"%s\": %s\n"),tmp3,strerror(errno));
                exit(EXIT_FAILURE);
        }
 
@@ -80,16 +80,16 @@ void useragent(void)
                getword_start(&gwarea,buf);
                if (getword(ip,sizeof(ip),&gwarea,' ')<0 || getword_skip(MAXLEN,&gwarea,'[')<0 ||
                    getword(data,sizeof(data),&gwarea,' ')<0) {
-                       debuga(_("Maybe you have a broken record or garbage in your %s file\n"),UserAgentLog);
+                       debuga(_("Invalid record in file \"%s\"\n"),UserAgentLog);
                        exit(EXIT_FAILURE);
                }
                getword_start(&gwarea1,data);
                if (getword(day,sizeof(day),&gwarea1,'/')<0 || getword(month,sizeof(month),&gwarea1,'/')<0 ||
                    getword(year,sizeof(year),&gwarea1,':')<0) {
-                       debuga(_("Maybe you have a broken date in your %s file\n"),UserAgentLog);
+                       debuga(_("Invalid date in file \"%s\"\n"),UserAgentLog);
                        exit(EXIT_FAILURE);
                }
-               buildymd(day,month,year,wdate);
+               buildymd(day,month,year,wdate,sizeof(wdate));
                ndate=atoi(wdate);
                if (ndate<dfrom) continue;
                if (ndate>duntil) break;
@@ -97,13 +97,13 @@ void useragent(void)
                        strcpy(idate,data);
                strcpy(fdate,data);
                if (getword_skip(MAXLEN,&gwarea,'"')<0 || getword(agent,sizeof(agent),&gwarea,'"')<0) {
-                       debuga(_("Maybe you have a broken useragent entry in your %s file\n"),UserAgentLog);
+                       debuga(_("Invalid useragent in file \"%s\"\n"),UserAgentLog);
                        exit(EXIT_FAILURE);
                }
 
                if(gwarea.current[0]!='\0') {
                        if (getword_skip(MAXLEN,&gwarea,' ')<0 || getword(user,sizeof(user),&gwarea,'\n')<0) {
-                               debuga(_("Maybe you have a broken record or garbage in your %s file\n"),UserAgentLog);
+                               debuga(_("Invalid record in file \"%s\"\n"),UserAgentLog);
                                exit(EXIT_FAILURE);
                        }
                        if(user[0] == '-')
@@ -122,9 +122,12 @@ void useragent(void)
                debuga(_("   Records read: %ld\n"),totregsl);
        }
 
-       fclose(fp_in);
        if (fclose(fp_ou)==EOF) {
-               debuga(_("Failed to close file %s - %s\n"),tmp3,strerror(errno));
+               debuga(_("Write error in \"%s\": %s\n"),tmp3,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+       if (fclose(fp_in)==EOF) {
+               debuga(_("Read error in \"%s\": %s\n"),UserAgentLog,strerror(errno));
                exit(EXIT_FAILURE);
        }
 
@@ -132,7 +135,10 @@ void useragent(void)
                debuga(_("Sorting file: %s\n"),tmp2);
        }
 
-       sprintf(csort,"sort -n -t \"\t\" -k 3,3 -k 2,2 -k 1,1 -o \"%s\" \"%s\"",tmp2,tmp3);
+       if (snprintf(csort,sizeof(csort),"sort -n -t \"\t\" -k 3,3 -k 2,2 -k 1,1 -o \"%s\" \"%s\"",tmp2,tmp3)>=sizeof(csort)) {
+               debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),tmp2,tmp3);
+               exit(EXIT_FAILURE);
+       }
        cstatus=system(csort);
        if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
                debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
@@ -140,19 +146,19 @@ void useragent(void)
                exit(EXIT_FAILURE);
        }
        if((fp_in=fopen(tmp2,"r"))==NULL) {
-               debuga(_("(useragent) Cannot open file %s\n"),tmp2);
+               debuga(_("Cannot open file \"%s\": %s\n"),tmp2,strerror(errno));
                debuga(_("sort command: %s\n"),csort);
                exit(EXIT_FAILURE);
        }
 
-       if (unlink(tmp3)) {
-               debuga(_("Cannot delete %s - %s\n"),tmp3,strerror(errno));
+       if (!KeepTempLog && unlink(tmp3)) {
+               debuga(_("Cannot delete \"%s\": %s\n"),tmp3,strerror(errno));
                exit(EXIT_FAILURE);
        }
 
        snprintf(hfile,sizeof(hfile),"%s/useragent.html", outdirname);
        if((fp_ht=fopen(hfile,"w"))==NULL) {
-               debuga(_("(useragent) Cannot open file %s\n"),hfile);
+               debuga(_("Cannot open file \"%s\": %s\n"),hfile,strerror(errno));
                exit(EXIT_FAILURE);
        }
 
@@ -174,7 +180,7 @@ void useragent(void)
        while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
                getword_start(&gwarea,buf);
                if (getword(ip,sizeof(ip),&gwarea,'\t')<0) {
-                       debuga(_("There is an invalid IP address in file %s\n"),tmp2);
+                       debuga(_("Invalid IP address in file \"%s\"\n"),tmp2);
                        exit(EXIT_FAILURE);
                }
 
@@ -187,11 +193,11 @@ void useragent(void)
                }
 
                if (getword(agent,sizeof(agent),&gwarea,'\t')<0) {
-                       debuga(_("There is an invalid useragent in file %s\n"),tmp2);
+                       debuga(_("Invalid useragent in file \"%s\"\n"),tmp2);
                        exit(EXIT_FAILURE);
                }
                if (getword(user,sizeof(user),&gwarea,'\t')<0) {
-                       debuga(_("There is an invalid user ID in file %s\n"),tmp2);
+                       debuga(_("Invalid user ID in file \"%s\"\n"),tmp2);
                        exit(EXIT_FAILURE);
                }
 
@@ -210,9 +216,15 @@ void useragent(void)
        }
 
        fputs("</table>\n",fp_ht);
-       fclose(fp_in);
+       if (fclose(fp_in)==EOF) {
+               debuga(_("Read error in \"%s\": %s\n"),tmp2,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
 
-       sprintf(csort,"sort -t \"\t\" -k 2,2 -o \"%s\" \"%s\"",tmp3,tmp2);
+       if (snprintf(csort,sizeof(csort),"sort -t \"\t\" -k 2,2 -o \"%s\" \"%s\"",tmp3,tmp2)>=sizeof(csort)) {
+               debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),tmp2,tmp3);
+               exit(EXIT_FAILURE);
+       }
        cstatus=system(csort);
        if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
                debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
@@ -220,18 +232,18 @@ void useragent(void)
                exit(EXIT_FAILURE);
        }
        if((fp_in=fopen(tmp3,"r"))==NULL) {
-               debuga(_("(useragent) Cannot open file %s\n"),tmp3);
+               debuga(_("Cannot open file \"%s\": %s\n"),tmp3,strerror(errno));
                debuga(_("sort command: %s\n"),csort);
                exit(EXIT_FAILURE);
        }
 
-       if (unlink(tmp2)) {
-               debuga(_("Cannot delete %s - %s\n"),tmp2,strerror(errno));
+       if (!KeepTempLog && unlink(tmp2)) {
+               debuga(_("Cannot delete \"%s\": %s\n"),tmp2,strerror(errno));
                exit(EXIT_FAILURE);
        }
 
        if((fp_ou=fopen(tmp2,"w"))==NULL) {
-               debuga(_("(useragent) Cannot open file %s\n"),tmp2);
+               debuga(_("Cannot open file \"%s\": %s\n"),tmp2,strerror(errno));
                exit(EXIT_FAILURE);
        }
 
@@ -241,11 +253,11 @@ void useragent(void)
        while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
                getword_start(&gwarea,buf);
                if (getword(ip,sizeof(ip),&gwarea,'\t')<0) {
-                       debuga(_("There is an invalid IP address in file %s\n"),tmp3);
+                       debuga(_("Invalid IP address in file \"%s\"\n"),tmp3);
                        exit(EXIT_FAILURE);
                }
                if (getword(agent,sizeof(agent),&gwarea,'\t')<0) {
-                       debuga(_("There is an invalid useragent in file %s\n"),tmp3);
+                       debuga(_("Invalid useragent in file \"%s\"\n"),tmp3);
                        exit(EXIT_FAILURE);
                }
 
@@ -267,18 +279,24 @@ void useragent(void)
        fprintf(fp_ou,"%06d %s\n",agentot,agent);
        agentot2+=agentot;
 
-       fclose(fp_in);
        if (fclose(fp_ou)==EOF) {
-               debuga(_("Failed to close file %s - %s\n"),tmp3,strerror(errno));
+               debuga(_("Write error in \"%s\": %s\n"),tmp2,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+       if (fclose(fp_in)==EOF) {
+               debuga(_("Read error in \"%s\": %s\n"),tmp3,strerror(errno));
                exit(EXIT_FAILURE);
        }
 
-       if (unlink(tmp3)) {
-               debuga(_("Cannot delete %s - %s\n"),tmp3,strerror(errno));
+       if (!KeepTempLog && unlink(tmp3)) {
+               debuga(_("Cannot delete \"%s\": %s\n"),tmp3,strerror(errno));
                exit(EXIT_FAILURE);
        }
 
-       sprintf(csort,"sort -n -r -k 1,1 -o \"%s\" \"%s\"",tmp3,tmp2);
+       if (snprintf(csort,sizeof(csort),"sort -n -r -k 1,1 -o \"%s\" \"%s\"",tmp3,tmp2)>=sizeof(csort)) {
+               debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),tmp2,tmp3);
+               exit(EXIT_FAILURE);
+       }
        cstatus=system(csort);
        if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
                debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
@@ -286,13 +304,13 @@ void useragent(void)
                exit(EXIT_FAILURE);
        }
        if((fp_in=fopen(tmp3,"r"))==NULL) {
-               debuga(_("(useragent) Cannot open file %s\n"),tmp3);
+               debuga(_("Cannot open file \"%s\": %s\n"),tmp3,strerror(errno));
                debuga(_("sort command: %s\n"),csort);
                exit(EXIT_FAILURE);
        }
 
-       if (unlink(tmp2)) {
-               debuga(_("Cannot delete %s - %s\n"),tmp2,strerror(errno));
+       if (!KeepTempLog && unlink(tmp2)) {
+               debuga(_("Cannot delete \"%s\": %s\n"),tmp2,strerror(errno));
                exit(EXIT_FAILURE);
        }
 
@@ -306,7 +324,7 @@ void useragent(void)
                fixendofline(buf);
                getword_start(&gwarea,buf);
                if (getword(tagent,sizeof(tagent),&gwarea,' ')<0) {
-                       debuga(_("There is an invalid useragent in file %s\n"),tmp3);
+                       debuga(_("Invalid useragent in file \"%s\"\n"),tmp3);
                        exit(EXIT_FAILURE);
                }
                nagent=atoi(tagent);
@@ -316,16 +334,20 @@ void useragent(void)
                output_html_string(fp_ht,gwarea.current,250);
                fprintf(fp_ht,"</td><td class=\"data\">%d</td><td class=\"data\">%3.2lf</td></tr>\n",nagent,perc);
        }
-       fclose(fp_in);
+       if (fclose(fp_in)==EOF) {
+               debuga(_("Read error in \"%s\": %s\n"),tmp3,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
 
        fputs("</table></div>\n",fp_ht);
-       if (write_html_trailer(fp_ht)<0)
-               debuga(_("Write error in file %s\n"),hfile);
-       if (fclose(fp_ht)==EOF)
-               debuga(_("Failed to close file %s - %s\n"),hfile,strerror(errno));
+       write_html_trailer(fp_ht);
+       if (fclose(fp_ht)==EOF) {
+               debuga(_("Write error in \"%s\": %s\n"),hfile,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
 
-       if (unlink(tmp3)) {
-               debuga(_("Cannot delete %s - %s\n"),tmp3,strerror(errno));
+       if (!KeepTempLog && unlink(tmp3)) {
+               debuga(_("Cannot delete \"%s\": %s\n"),tmp3,strerror(errno));
                exit(EXIT_FAILURE);
        }