]> git.ipfire.org Git - thirdparty/sarg.git/blame - datafile.c
Remove string copies and protect against possible buffer overflow
[thirdparty/sarg.git] / datafile.c
CommitLineData
25697a35 1/*
94ff9470 2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
1164c474 3 * 1998, 2010
25697a35
GS
4 *
5 * SARG donations:
6 * please look at http://sarg.sourceforge.net/donations.php
1164c474
FM
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
25697a35
GS
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"
5f3cfd1d 28#include "include/defs.h"
25697a35
GS
29
30void saverecs(char *dirname, char *user, long long int nacc, char *url, long long int nbytes, char *ip, char *hora, char *dia, long long int nelap, long long int incache, long long int oucache);
25697a35
GS
31
32void data_file(char *tmp)
33{
34
35 FILE *fp_in;
36
6e792ade 37 char accdia[11], acchora[9], accuser[MAXLEN], accip[MAXLEN], accurl[MAXLEN];
d6e703cc 38 char oldaccdia[11], oldacchora[9], oldaccip[MAXLEN];
25697a35 39 char dirname[MAXLEN], wdirname[MAXLEN], oldurl[MAXLEN], oldaccuser[MAXLEN];
6e792ade 40 char olduser[MAXLEN], oldmsg[50], acccode[50], oldacccode[50];
5542cb59 41 char ipantes[MAXLEN], nameantes[MAXLEN];
25697a35 42 char accsmart[MAXLEN];
25697a35 43 char crc2[50];
9c7c6346 44 char *str;
2240dcea 45 char tmp3[MAXLEN];
25697a35
GS
46 DIR *dirp;
47 struct dirent *direntp;
5542cb59
FM
48 long long int nbytes=0;
49 long long int nelap=0;
25697a35
GS
50 long long int nacc=0;
51 long long int rtotal=0;
52 long long int incache=0;
53 long long int oucache=0;
6e792ade
FM
54 long long int accbytes;
55 long long int accelap;
c0ec9cc7 56 int dlen;
9c7c6346 57 struct getwordstruct gwarea;
c0ec9cc7 58 const char logext[]=".log";
25697a35
GS
59
60 ipantes[0]='\0';
61 nameantes[0]='\0';
62
63 olduser[0]='\0';
2240dcea 64 strcat(tmp,"/sarg");
25697a35
GS
65
66 dirp = opendir(tmp);
67 while ( (direntp = readdir( dirp )) != NULL ) {
c0ec9cc7
FM
68 dlen=strlen(direntp->d_name)-(sizeof(logext)-1);
69 if (dlen<=0) continue;
70 if(strcmp(direntp->d_name+dlen,logext) != 0)
25697a35 71 continue;
c0ec9cc7 72
9c7c6346
FM
73 if (snprintf(tmp3,sizeof(tmp3),"%s/%s",tmp,direntp->d_name)>=sizeof(tmp3)) {
74 fprintf(stderr,"SARG: (datafile) directory path too long: %s/%s\n",tmp,direntp->d_name);
75 exit(1);
76 }
25697a35 77
936c9905
FM
78 if((fp_in=MY_FOPEN(tmp3,"r"))==NULL){
79 fprintf(stderr, "SARG: (datafile) %s: %s\n",text[45],tmp);
25697a35
GS
80 exit(1);
81 }
25697a35
GS
82
83 ttopen=0;
84 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
9c7c6346
FM
85 getword_start(&gwarea,buf);
86 if (getword(accdia,sizeof(accdia),&gwarea,' ')<0 || getword(acchora,sizeof(acchora),&gwarea,' ')<0 ||
87 getword(accuser,sizeof(accuser),&gwarea,' ')<0 || getword(accip,sizeof(accip),&gwarea,' ')<0 ||
6e792ade
FM
88 getword(accurl,sizeof(accurl),&gwarea,' ')<0 || getword_atoll(&accbytes,&gwarea,' ')<0 ||
89 getword(acccode,sizeof(acccode),&gwarea,' ')<0 || getword_atoll(&accelap,&gwarea,' ')<0 ||
9c7c6346 90 getword_skip(20000,&gwarea,' ')<0 || getword(accsmart,sizeof(accsmart),&gwarea,'"')<0) {
4bcb77cf
FM
91 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp3);
92 exit(1);
93 }
5542cb59 94
246c8489 95 if(Ip2Name) {
25697a35
GS
96 if(strcmp(accip,ipantes) != 0) {
97 strcpy(ipantes,accip);
a1c55d8c 98 ip2name(accip,sizeof(accip));
25697a35 99 strcpy(nameantes,accip);
5542cb59
FM
100 }
101 else strcpy(accip,nameantes);
25697a35 102 }
5542cb59 103
25697a35
GS
104 if(!rtotal){
105 strcpy(oldurl,accurl);
106 strcpy(oldacccode,acccode);
25697a35
GS
107 strcpy(oldaccuser,accuser);
108 strcpy(oldaccip,accip);
109 strcpy(oldaccdia,accdia);
110 strcpy(oldacchora,acchora);
111 rtotal++;
112 }
5542cb59 113
25697a35
GS
114 if(strcmp(oldurl,accurl) != 0 || strcmp(oldaccuser,accuser) != 0){
115 strcpy(oldmsg,"OK");
d6e703cc 116 if(strstr(oldacccode,"DENIED") != 0) strcpy(oldmsg,text[46]);
0a4e18e1 117 gravatmp(oldaccuser,oldurl,nacc,nbytes,oldmsg,nelap,indexonly,incache,oucache);
25697a35 118 strcpy(wdirname,dirname);
14c2eac6 119 saverecs(wdirname,oldaccuser,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache);
25697a35
GS
120 nacc=0;
121 nbytes=0;
122 nelap=0;
123 incache=0;
124 oucache=0;
d6e703cc 125 if(strcmp(oldaccuser,accuser) != 0) ind2=0;
25697a35
GS
126 }
127
128 nacc++;
6e792ade
FM
129 nbytes+=accbytes;
130 nelap+=accelap;
5542cb59 131
9c7c6346
FM
132 strcpy(crc2,acccode);
133 str=strchr(crc2,'/');
134 if (str) *str='\0';
25697a35 135
6e792ade
FM
136 if(strstr(crc2,"MISS") != 0) oucache+=accbytes;
137 else incache+=accbytes;
5542cb59 138
25697a35
GS
139 strcpy(oldurl,accurl);
140 strcpy(oldaccuser,accuser);
141 strcpy(oldacccode,acccode);
25697a35
GS
142 strcpy(oldaccip,accip);
143 strcpy(oldaccdia,accdia);
144 strcpy(oldacchora,acchora);
145 }
5542cb59 146
25697a35 147 fclose(fp_in);
c0ec9cc7 148 }
5542cb59 149
25697a35 150 (void)closedir( dirp );
d2fe0c32
FM
151 if(debug)
152 debuga("Datafile %s successfully",DataFile);
25697a35
GS
153}
154
14c2eac6 155void saverecs(char *dirname, char *user, long long int nacc, char *url, long long int nbytes, char *ip, char *hora, char *dia, long long int nelap, long long int incache, long long int oucache)
25697a35 156{
25697a35
GS
157 FILE *fp_ou;
158 char reg[MAXLEN];
e5b2c6f0 159 char val[20];
25697a35 160
936c9905 161 if((fp_ou=MY_FOPEN(DataFile,"a"))==NULL){
32e71fa4 162 fprintf(stderr, "SARG: (datafile) %s: %s\n",text[45],DataFile);
25697a35
GS
163 exit(1);
164 }
165
085c1e1f 166 if((DataFileFields & DATA_FIELD_USER) != 0) {
25697a35
GS
167 strcpy(reg,user);
168 strncat(reg,DataFileDelimiter,1);
169 }
085c1e1f 170 if((DataFileFields & DATA_FIELD_DATE) != 0) {
25697a35
GS
171 strncat(reg,dia,strlen(dia));
172 strncat(reg,DataFileDelimiter,1);
173 }
085c1e1f 174 if((DataFileFields & DATA_FIELD_TIME) != 0) {
25697a35
GS
175 strncat(reg,hora,strlen(hora));
176 strncat(reg,DataFileDelimiter,1);
177 }
085c1e1f 178 if((DataFileFields & DATA_FIELD_URL) != 0) {
25697a35 179 strcpy(name,url);
d6e703cc 180 if (strcmp(DataFileUrl,"ip") == 0) name2ip(name);
25697a35
GS
181 strncat(reg,name,strlen(name));
182 strncat(reg,DataFileDelimiter,1);
183 }
085c1e1f 184 if((DataFileFields & DATA_FIELD_CONNECT) != 0) {
e5b2c6f0
FM
185 my_lltoa(nacc,val,sizeof(val),0);
186 strcat(reg,val);
25697a35
GS
187 strncat(reg,DataFileDelimiter,1);
188 }
085c1e1f 189 if((DataFileFields & DATA_FIELD_BYTES) != 0) {
e5b2c6f0
FM
190 my_lltoa(nbytes,val,sizeof(val),0);
191 strcat(reg,val);
25697a35
GS
192 strncat(reg,DataFileDelimiter,1);
193 }
085c1e1f 194 if((DataFileFields & DATA_FIELD_IN_CACHE) != 0) {
e5b2c6f0
FM
195 my_lltoa(incache,val,sizeof(val),0);
196 strcat(reg,val);
25697a35
GS
197 strncat(reg,DataFileDelimiter,1);
198 }
085c1e1f 199 if((DataFileFields & DATA_FIELD_OUT_CACHE) != 0) {
e5b2c6f0
FM
200 my_lltoa(oucache,val,sizeof(val),0);
201 strcat(reg,val);
25697a35
GS
202 strncat(reg,DataFileDelimiter,1);
203 }
085c1e1f 204 if((DataFileFields & DATA_FIELD_ELAPSED) != 0) {
e5b2c6f0
FM
205 my_lltoa(nelap,val,sizeof(val),0);
206 strcat(reg,val);
25697a35
GS
207 strncat(reg,DataFileDelimiter,1);
208 }
209
210 reg[strlen(reg)-1]='\n';
211 fputs(reg,fp_ou);
212
213 fclose(fp_ou);
25697a35 214}