]> git.ipfire.org Git - thirdparty/sarg.git/blame - datafile.c
Apply a few more recommandation from splint
[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 29
f84a35a3 30void saverecs(FILE *fp_ou, const struct userinfostruct *uinfo, 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
f84a35a3 35 FILE *fp_in, *fp_ou=NULL;
25697a35 36
f84a35a3
FM
37 char *buf;
38 char accdia[11], acchora[9], accip[MAXLEN], *accurl;
d6e703cc 39 char oldaccdia[11], oldacchora[9], oldaccip[MAXLEN];
f84a35a3 40 char *oldurl;
6e792ade 41 char olduser[MAXLEN], oldmsg[50], acccode[50], oldacccode[50];
5542cb59 42 char ipantes[MAXLEN], nameantes[MAXLEN];
25697a35 43 char accsmart[MAXLEN];
25697a35 44 char crc2[50];
9c7c6346 45 char *str;
2240dcea 46 char tmp3[MAXLEN];
f2ec8c75
FM
47 char user[MAX_USER_LEN];
48 char u2[MAX_USER_LEN];
25697a35
GS
49 DIR *dirp;
50 struct dirent *direntp;
5542cb59
FM
51 long long int nbytes=0;
52 long long int nelap=0;
25697a35 53 long long int nacc=0;
f84a35a3 54 int rtotal=0;
25697a35
GS
55 long long int incache=0;
56 long long int oucache=0;
6e792ade
FM
57 long long int accbytes;
58 long long int accelap;
c0ec9cc7 59 int dlen;
f2ec8c75 60 int new_user;
f84a35a3
FM
61 int same_url;
62 int url_len;
63 int ourl_size;
9c7c6346 64 struct getwordstruct gwarea;
f2ec8c75 65 struct userinfostruct *uinfo;
afaa3b67 66 longline line;
c0ec9cc7 67 const char logext[]=".log";
25697a35
GS
68
69 ipantes[0]='\0';
70 nameantes[0]='\0';
71
72 olduser[0]='\0';
2240dcea 73 strcat(tmp,"/sarg");
f84a35a3
FM
74 oldurl=NULL;
75 ourl_size=0;
25697a35
GS
76
77 dirp = opendir(tmp);
78 while ( (direntp = readdir( dirp )) != NULL ) {
c0ec9cc7
FM
79 dlen=strlen(direntp->d_name)-(sizeof(logext)-1);
80 if (dlen<=0) continue;
81 if(strcmp(direntp->d_name+dlen,logext) != 0)
25697a35 82 continue;
c0ec9cc7 83
f2ec8c75
FM
84 if (dlen>0) {
85 if (dlen>=sizeof(user)) continue;
86 strncpy(user,direntp->d_name,dlen);
87 user[dlen]=0;
88 } else {
89 user[0]='\0';
90 }
91
92 uinfo=userinfo_find_from_file(user);
93 if (!uinfo) {
f84a35a3 94 debuga(_("Ignoring unknown user file %s\n"),user);
f2ec8c75
FM
95 continue;
96 }
97 strcpy(u2,uinfo->id);
f84a35a3
FM
98 if(Ip2Name && uinfo->id_is_ip) {
99 strcpy(ipantes,u2);
100 ip2name(u2,sizeof(u2));
101 strcpy(nameantes,u2);
102 }
f2ec8c75
FM
103 user_find(uinfo->label,MAX_USER_LEN, u2);
104
9c7c6346 105 if (snprintf(tmp3,sizeof(tmp3),"%s/%s",tmp,direntp->d_name)>=sizeof(tmp3)) {
f84a35a3 106 debuga(_("(datafile) directory path too long: %s/%s\n"),tmp,direntp->d_name);
06b39c87 107 exit(EXIT_FAILURE);
9c7c6346 108 }
25697a35 109
936c9905 110 if((fp_in=MY_FOPEN(tmp3,"r"))==NULL){
9f70c14e 111 debuga(_("(datafile) Cannot open file %s\n"),tmp3);
06b39c87 112 exit(EXIT_FAILURE);
f84a35a3
FM
113 }
114
afaa3b67 115 if ((line=longline_create())==NULL) {
10210234 116 debuga(_("Not enough memory to read the downloaded files.\n"));
06b39c87 117 exit(EXIT_FAILURE);
25697a35 118 }
25697a35
GS
119
120 ttopen=0;
f2ec8c75 121 new_user=1;
afaa3b67 122 while((buf=longline_read(fp_in,line))!=NULL) {
9c7c6346 123 getword_start(&gwarea,buf);
f84a35a3
FM
124 if (getword(accdia,sizeof(accdia),&gwarea,'\t')<0 || getword(acchora,sizeof(acchora),&gwarea,'\t')<0 ||
125 getword(accip,sizeof(accip),&gwarea,'\t')<0 ||
126 getword_ptr(buf,&accurl,&gwarea,'\t')<0 || getword_atoll(&accbytes,&gwarea,'\t')<0 ||
127 getword(acccode,sizeof(acccode),&gwarea,'\t')<0 || getword_atoll(&accelap,&gwarea,'\t')<0) {
128 debuga(_("There is a broken record or garbage in file %s\n"),tmp3);
06b39c87 129 exit(EXIT_FAILURE);
f84a35a3
FM
130 }
131 if (getword_skip(20000,&gwarea,'"')<0 || getword(accsmart,sizeof(accsmart),&gwarea,'"')<0) {
132 debuga(_("There is an invalid smart info in file %s\n"),tmp3);
06b39c87 133 exit(EXIT_FAILURE);
4bcb77cf 134 }
5542cb59 135
246c8489 136 if(Ip2Name) {
25697a35
GS
137 if(strcmp(accip,ipantes) != 0) {
138 strcpy(ipantes,accip);
a1c55d8c 139 ip2name(accip,sizeof(accip));
25697a35 140 strcpy(nameantes,accip);
5542cb59
FM
141 }
142 else strcpy(accip,nameantes);
25697a35 143 }
5542cb59 144
25697a35 145 if(!rtotal){
f84a35a3
FM
146 url_len=strlen(accurl);
147 if (!oldurl || url_len>=ourl_size) {
148 ourl_size=url_len+1;
149 oldurl=realloc(oldurl,ourl_size);
150 if (!oldurl) {
151 debuga(_("Not enough memory to store the url\n"));
06b39c87 152 exit(EXIT_FAILURE);
f84a35a3
FM
153 }
154 }
25697a35
GS
155 strcpy(oldurl,accurl);
156 strcpy(oldacccode,acccode);
25697a35
GS
157 strcpy(oldaccip,accip);
158 strcpy(oldaccdia,accdia);
159 strcpy(oldacchora,acchora);
f84a35a3 160 new_user=0;
25697a35
GS
161 rtotal++;
162 }
f84a35a3 163 same_url=(strcmp(oldurl,accurl) == 0);
5542cb59 164
f84a35a3
FM
165 if(!same_url || new_user){
166 if(strstr(oldacccode,"DENIED") != 0)
167 strcpy(oldmsg,_("DENIED"));
168 else
169 strcpy(oldmsg,"OK");
170 if(!fp_ou && (fp_ou=MY_FOPEN(DataFile,"w"))==NULL){
9f70c14e 171 debuga(_("(datafile) Cannot open file %s\n"),DataFile);
06b39c87 172 exit(EXIT_FAILURE);
f84a35a3
FM
173 }
174 saverecs(fp_ou,uinfo,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache);
25697a35
GS
175 nacc=0;
176 nbytes=0;
177 nelap=0;
178 incache=0;
179 oucache=0;
f84a35a3 180 new_user=0;
25697a35
GS
181 }
182
183 nacc++;
6e792ade
FM
184 nbytes+=accbytes;
185 nelap+=accelap;
5542cb59 186
9c7c6346
FM
187 strcpy(crc2,acccode);
188 str=strchr(crc2,'/');
189 if (str) *str='\0';
25697a35 190
6e792ade
FM
191 if(strstr(crc2,"MISS") != 0) oucache+=accbytes;
192 else incache+=accbytes;
5542cb59 193
f84a35a3
FM
194 if (!same_url) {
195 url_len=strlen(accurl);
196 if (url_len>=ourl_size) {
197 ourl_size=url_len+1;
198 oldurl=realloc(oldurl,ourl_size);
199 if (!oldurl) {
200 debuga(_("Not enough memory to store the url\n"));
06b39c87 201 exit(EXIT_FAILURE);
f84a35a3
FM
202 }
203 }
204 strcpy(oldurl,accurl);
205 }
f2ec8c75 206 new_user=0;
25697a35 207 strcpy(oldacccode,acccode);
25697a35
GS
208 strcpy(oldaccip,accip);
209 strcpy(oldaccdia,accdia);
210 strcpy(oldacchora,acchora);
211 }
5542cb59 212
25697a35 213 fclose(fp_in);
afaa3b67 214 longline_destroy(&line);
c0ec9cc7 215 }
25697a35 216 (void)closedir( dirp );
f84a35a3
FM
217 if (oldurl) free(oldurl);
218 if (fp_ou) fclose(fp_ou);
219
d2fe0c32 220 if(debug)
fcdc0918 221 debuga(_("Datafile %s written successfully\n"),DataFile);
25697a35
GS
222}
223
f84a35a3 224void saverecs(FILE *fp_ou, const struct userinfostruct *uinfo, 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 225{
e5b2c6f0 226 char val[20];
f84a35a3 227 char name[512];
25697a35 228
085c1e1f 229 if((DataFileFields & DATA_FIELD_USER) != 0) {
f2ec8c75
FM
230 fputs(uinfo->label,fp_ou);
231 fputc(DataFileDelimiter[0],fp_ou);
25697a35 232 }
085c1e1f 233 if((DataFileFields & DATA_FIELD_DATE) != 0) {
f2ec8c75
FM
234 fputs(dia,fp_ou);
235 fputc(DataFileDelimiter[0],fp_ou);
25697a35 236 }
085c1e1f 237 if((DataFileFields & DATA_FIELD_TIME) != 0) {
f2ec8c75
FM
238 fputs(hora,fp_ou);
239 fputc(DataFileDelimiter[0],fp_ou);
25697a35 240 }
085c1e1f 241 if((DataFileFields & DATA_FIELD_URL) != 0) {
f84a35a3
FM
242 url_hostname(url,name,sizeof(name));
243 if (DataFileUrl == DATAFILEURL_IP) name2ip(name);
f2ec8c75
FM
244 fputs(name,fp_ou);
245 fputc(DataFileDelimiter[0],fp_ou);
25697a35 246 }
085c1e1f 247 if((DataFileFields & DATA_FIELD_CONNECT) != 0) {
e5b2c6f0 248 my_lltoa(nacc,val,sizeof(val),0);
f2ec8c75
FM
249 fputs(val,fp_ou);
250 fputc(DataFileDelimiter[0],fp_ou);
25697a35 251 }
085c1e1f 252 if((DataFileFields & DATA_FIELD_BYTES) != 0) {
e5b2c6f0 253 my_lltoa(nbytes,val,sizeof(val),0);
f2ec8c75
FM
254 fputs(val,fp_ou);
255 fputc(DataFileDelimiter[0],fp_ou);
25697a35 256 }
085c1e1f 257 if((DataFileFields & DATA_FIELD_IN_CACHE) != 0) {
e5b2c6f0 258 my_lltoa(incache,val,sizeof(val),0);
f2ec8c75
FM
259 fputs(val,fp_ou);
260 fputc(DataFileDelimiter[0],fp_ou);
25697a35 261 }
085c1e1f 262 if((DataFileFields & DATA_FIELD_OUT_CACHE) != 0) {
e5b2c6f0 263 my_lltoa(oucache,val,sizeof(val),0);
f2ec8c75
FM
264 fputs(val,fp_ou);
265 fputc(DataFileDelimiter[0],fp_ou);
25697a35 266 }
085c1e1f 267 if((DataFileFields & DATA_FIELD_ELAPSED) != 0) {
e5b2c6f0 268 my_lltoa(nelap,val,sizeof(val),0);
f2ec8c75
FM
269 fputs(val,fp_ou);
270 fputc(DataFileDelimiter[0],fp_ou);
25697a35
GS
271 }
272
f2ec8c75 273 fputc('\n',fp_ou);
25697a35 274}