]> git.ipfire.org Git - thirdparty/sarg.git/blame - datafile.c
Protect ip2name against buffer overflows and replace characters that are valid in...
[thirdparty/sarg.git] / datafile.c
CommitLineData
25697a35 1/*
c37945ed
FM
2 * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
3 * 1998, 2008
94ff9470 4 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
25697a35
GS
5 *
6 * SARG donations:
7 * please look at http://sarg.sourceforge.net/donations.php
8 * ---------------------------------------------------------------------
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
23 *
24 */
25
26#include "include/conf.h"
27
28void 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);
29void grava_SmartFilter(char *dirname, char *user, char *ip, char *data, char *hora, char *url, char *smart);
30
31void data_file(char *tmp)
32{
33
34 FILE *fp_in;
35
36 char accdia[11], acchora[9], accuser[MAXLEN], accip[MAXLEN], accurl[MAXLEN], accbytes[12], accelap[10];
d6e703cc 37 char oldaccdia[11], oldacchora[9], oldaccip[MAXLEN];
25697a35
GS
38 char dirname[MAXLEN], wdirname[MAXLEN], oldurl[MAXLEN], oldaccuser[MAXLEN];
39 char olduser[MAXLEN], oldmsg[50], acccode[50], oldaccelap[10], oldacccode[50];
40 char ipantes[MAXLEN], nameantes[MAXLEN];
41 char accsmart[MAXLEN];
42 char Msg[MAXLEN];
25697a35
GS
43 char wcrc[50];
44 char crc2[50];
45 char wdname[MAXLEN];
46 char wname2[MAXLEN];
47 DIR *dirp;
48 struct dirent *direntp;
49 long long int nbytes=0;
50 long long int nelap=0;
51 long long int nacc=0;
52 long long int rtotal=0;
53 long long int incache=0;
54 long long int oucache=0;
25697a35
GS
55
56 ipantes[0]='\0';
57 nameantes[0]='\0';
58
59 olduser[0]='\0';
60 strncat(tmp,"/sarg",5);
61
62 dirp = opendir(tmp);
63 while ( (direntp = readdir( dirp )) != NULL ) {
64 if(strstr(direntp->d_name,".log") == 0)
65 continue;
66 sprintf(tmp3,"%s/%s",tmp,direntp->d_name);
67
0dda06bf
FM
68#if defined(HAVE_FOPEN64)
69 if((fp_in=(long)fopen64(tmp3,"r"))==NULL){
70#else
25697a35 71 if((fp_in=fopen(tmp3,"r"))==NULL){
0dda06bf
FM
72#endif
73 fprintf(stderr, "SARG: (datafile) %s: %s\n",text[45],tmp);
25697a35
GS
74 exit(1);
75 }
76 strcpy(wdname,direntp->d_name);
77 strip_prefix:
4bcb77cf
FM
78 if (getword(wname2,sizeof(wname2),wdname,'.')<0) {
79 printf("SARG: Maybe you have a broken record or garbage in your %s directory.\n",tmp);
80 exit(1);
81 }
25697a35
GS
82 strcat(user,wname2);
83
84 ttopen=0;
85 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
4bcb77cf
FM
86 if (getword(accdia,sizeof(accdia),buf,' ')<0 || getword(acchora,sizeof(acchora),buf,' ')<0 ||
87 getword(accuser,sizeof(accuser),buf,' ')<0 || getword(accip,sizeof(accip),buf,' ')<0 ||
88 getword(accurl,sizeof(accurl),buf,' ')<0 || getword(accbytes,sizeof(accbytes),buf,' ')<0 ||
89 getword(acccode,sizeof(acccode),buf,' ')<0 || getword(accelap,sizeof(accelap),buf,' ')<0 ||
90 getword(accsmart,sizeof(accsmart),buf,' ')<0 || getword(accsmart,sizeof(accsmart),buf,'"')<0) {
91 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp3);
92 exit(1);
93 }
25697a35
GS
94
95 if(strcmp(Ip2Name,"yes") == 0) {
96 if(strcmp(accip,ipantes) != 0) {
97 strcpy(ipantes,accip);
a1c55d8c 98 ip2name(accip,sizeof(accip));
25697a35 99 strcpy(nameantes,accip);
d6e703cc
FM
100 }
101 else strcpy(accip,nameantes);
25697a35
GS
102 }
103
104 if(!rtotal){
105 strcpy(oldurl,accurl);
106 strcpy(oldacccode,acccode);
107 strcpy(oldaccelap,accelap);
108 strcpy(oldaccuser,accuser);
109 strcpy(oldaccip,accip);
110 strcpy(oldaccdia,accdia);
111 strcpy(oldacchora,acchora);
112 rtotal++;
113 }
114
115 if(strcmp(oldurl,accurl) != 0 || strcmp(oldaccuser,accuser) != 0){
116 strcpy(oldmsg,"OK");
d6e703cc 117 if(strstr(oldacccode,"DENIED") != 0) strcpy(oldmsg,text[46]);
25697a35
GS
118 strcpy(wdirname,dirname);
119 gravatmp(oldaccuser,wdirname,oldurl,nacc,nbytes,oldmsg,nelap,indexonly,incache,oucache);
120 strcpy(wdirname,dirname);
14c2eac6 121 saverecs(wdirname,oldaccuser,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache);
25697a35
GS
122 nacc=0;
123 nbytes=0;
124 nelap=0;
125 incache=0;
126 oucache=0;
d6e703cc 127 if(strcmp(oldaccuser,accuser) != 0) ind2=0;
25697a35
GS
128 }
129
130 nacc++;
131 nbytes+=my_atoll(accbytes);
132 nelap+=my_atoll(accelap);
133
134 strcpy(wcrc,acccode);
4bcb77cf
FM
135 if (getword(crc2,sizeof(crc2),wcrc,'/')<0) {
136 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp3);
137 exit(1);
138 }
25697a35 139
d6e703cc
FM
140 if(strstr(crc2,"MISS") != 0) oucache+=my_atoll(accbytes);
141 else incache+=my_atoll(accbytes);
25697a35
GS
142
143 strcpy(oldurl,accurl);
144 strcpy(oldaccuser,accuser);
145 strcpy(oldacccode,acccode);
146 strcpy(oldaccelap,accelap);
147 strcpy(oldaccip,accip);
148 strcpy(oldaccdia,accdia);
149 strcpy(oldacchora,acchora);
150 }
151
152 fclose(fp_in);
153}
154
155 (void)closedir( dirp );
156 if(debug) {
157 sprintf(Msg,"Datafile %s successfully",DataFile);
158 debuga(Msg);
159 }
25697a35
GS
160}
161
14c2eac6 162void 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
163{
164
165 FILE *fp_ou;
166 char reg[MAXLEN];
167
0dda06bf
FM
168#if defined(HAVE_FOPEN64)
169 if((fp_ou=(long)fopen64(DataFile,"a"))==NULL){
170#else
25697a35 171 if((fp_ou=fopen(DataFile,"a"))==NULL){
0dda06bf
FM
172#endif
173 fprintf(stderr, "SARG: (datafile) %s: %s\n",text[45],DataFile);
25697a35
GS
174 exit(1);
175 }
176
177 my_lltoa(nacc,val1,0);
178 my_lltoa(nbytes,val2,0);
179 my_lltoa(nelap,val3,0);
180 my_lltoa(incache,val4,0);
181 my_lltoa(oucache,val5,0);
182
183 if(strstr(DataFileFields,"user") != 0) {
184 strcpy(reg,user);
185 strncat(reg,DataFileDelimiter,1);
186 }
187 if(strstr(DataFileFields,"date") != 0) {
188 strncat(reg,dia,strlen(dia));
189 strncat(reg,DataFileDelimiter,1);
190 }
191 if(strstr(DataFileFields,"time") != 0) {
192 strncat(reg,hora,strlen(hora));
193 strncat(reg,DataFileDelimiter,1);
194 }
195 if(strstr(DataFileFields,"url") != 0) {
196 strcpy(name,url);
d6e703cc 197 if (strcmp(DataFileUrl,"ip") == 0) name2ip(name);
25697a35
GS
198 strncat(reg,name,strlen(name));
199 strncat(reg,DataFileDelimiter,1);
200 }
201 if(strstr(DataFileFields,"connect") != 0) {
202 strncat(reg,val1,strlen(val1));
203 strncat(reg,DataFileDelimiter,1);
204 }
205 if(strstr(DataFileFields,"bytes") != 0) {
206 strncat(reg,val2,strlen(val2));
207 strncat(reg,DataFileDelimiter,1);
208 }
209 if(strstr(DataFileFields,"in_cache") != 0) {
210 strncat(reg,val4,strlen(val4));
211 strncat(reg,DataFileDelimiter,1);
212 }
213 if(strstr(DataFileFields,"out_cache") != 0) {
214 strncat(reg,val5,strlen(val5));
215 strncat(reg,DataFileDelimiter,1);
216 }
217 if(strstr(DataFileFields,"elapsed") != 0) {
218 strncat(reg,val3,strlen(val3));
219 strncat(reg,DataFileDelimiter,1);
220 }
221
222 reg[strlen(reg)-1]='\n';
223 fputs(reg,fp_ou);
224
225 fclose(fp_ou);
25697a35 226}