]> git.ipfire.org Git - thirdparty/sarg.git/blame - datafile.c
Overwrite any existing dansguardian temporary file
[thirdparty/sarg.git] / datafile.c
CommitLineData
25697a35 1/*
94ff9470 2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
61d965f3 3 * 1998, 2012
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{
9bd92830 34 FILE *fp_in, *fp_ou=NULL;
25697a35 35
9bd92830
FM
36 char *buf;
37 char accdia[11], acchora[9], accip[MAXLEN], *accurl;
38 char oldaccdia[11], oldacchora[9], oldaccip[MAXLEN];
39 char *oldurl;
5025f299 40 char acccode[50], oldacccode[50];
9bd92830
FM
41 char ipantes[MAXLEN], nameantes[MAXLEN];
42 char accsmart[MAXLEN];
43 char crc2[50];
44 char *str;
45 char tmp3[MAXLEN];
9bd92830 46 char u2[MAX_USER_LEN];
93551487 47 userscan uscan;
9bd92830
FM
48 long long int nbytes=0;
49 long long int nelap=0;
50 long long int nacc=0;
51 int rtotal=0;
52 long long int incache=0;
53 long long int oucache=0;
54 long long int accbytes;
55 long long int accelap;
9bd92830
FM
56 int new_user;
57 int same_url;
58 int url_len;
59 int ourl_size;
60 struct getwordstruct gwarea;
61 struct userinfostruct *uinfo;
62 longline line;
25697a35 63
9bd92830
FM
64 ipantes[0]='\0';
65 nameantes[0]='\0';
25697a35 66
9bd92830
FM
67 oldurl=NULL;
68 ourl_size=0;
25697a35 69
93551487
FM
70 uscan=userinfo_startscan();
71 if (uscan == NULL) {
72 debuga(_("Cannot enumerate the user list\n"));
9bd92830
FM
73 exit(EXIT_FAILURE);
74 }
93551487 75 while ( (uinfo = userinfo_advancescan(uscan)) != NULL ) {
9bd92830
FM
76 strcpy(u2,uinfo->id);
77 if(Ip2Name && uinfo->id_is_ip) {
78 strcpy(ipantes,u2);
79 ip2name(u2,sizeof(u2));
80 strcpy(nameantes,u2);
81 }
82 user_find(uinfo->label,MAX_USER_LEN, u2);
93551487 83 if(debug) debuga(_("Reading user file: %s/%s\n"),tmp,uinfo->filename);
f2ec8c75 84
461b479d 85 sort_users_log(tmp,debug,uinfo);
170a77ea
FM
86 if (snprintf(tmp3,sizeof(tmp3),"%s/%s.user_log",tmp,uinfo->filename)>=sizeof(tmp3)) {
87 debuga(_("(datafile) directory path too long: %s/%s.user_log\n"),tmp,uinfo->filename);
9bd92830
FM
88 exit(EXIT_FAILURE);
89 }
f2ec8c75 90
9bd92830 91 if((fp_in=MY_FOPEN(tmp3,"r"))==NULL){
d6f0349d 92 debuga(_("(datafile) Cannot open file %s: %s\n"),tmp3,strerror(errno));
9bd92830
FM
93 exit(EXIT_FAILURE);
94 }
25697a35 95
9bd92830
FM
96 if ((line=longline_create())==NULL) {
97 debuga(_("Not enough memory to read the downloaded files.\n"));
98 exit(EXIT_FAILURE);
99 }
f84a35a3 100
9bd92830
FM
101 ttopen=0;
102 new_user=1;
103 while((buf=longline_read(fp_in,line))!=NULL) {
104 getword_start(&gwarea,buf);
105 if (getword(accdia,sizeof(accdia),&gwarea,'\t')<0 || getword(acchora,sizeof(acchora),&gwarea,'\t')<0 ||
007905af
FM
106 getword(accip,sizeof(accip),&gwarea,'\t')<0 ||
107 getword_ptr(buf,&accurl,&gwarea,'\t')<0 || getword_atoll(&accbytes,&gwarea,'\t')<0 ||
108 getword(acccode,sizeof(acccode),&gwarea,'\t')<0 || getword_atoll(&accelap,&gwarea,'\t')<0) {
9bd92830
FM
109 debuga(_("There is a broken record or garbage in file %s\n"),tmp3);
110 exit(EXIT_FAILURE);
111 }
112 if (getword_skip(20000,&gwarea,'"')<0 || getword(accsmart,sizeof(accsmart),&gwarea,'"')<0) {
113 debuga(_("There is an invalid smart info in file %s\n"),tmp3);
114 exit(EXIT_FAILURE);
115 }
25697a35 116
9bd92830
FM
117 if(Ip2Name) {
118 if(strcmp(accip,ipantes) != 0) {
119 strcpy(ipantes,accip);
120 ip2name(accip,sizeof(accip));
121 strcpy(nameantes,accip);
122 }
123 else strcpy(accip,nameantes);
124 }
5542cb59 125
9bd92830
FM
126 if(!rtotal){
127 url_len=strlen(accurl);
128 if (!oldurl || url_len>=ourl_size) {
129 ourl_size=url_len+1;
130 oldurl=realloc(oldurl,ourl_size);
131 if (!oldurl) {
132 debuga(_("Not enough memory to store the url\n"));
133 exit(EXIT_FAILURE);
134 }
135 }
136 strcpy(oldurl,accurl);
137 strcpy(oldacccode,acccode);
138 strcpy(oldaccip,accip);
139 strcpy(oldaccdia,accdia);
140 strcpy(oldacchora,acchora);
141 new_user=0;
142 rtotal++;
143 }
144 same_url=(strcmp(oldurl,accurl) == 0);
5542cb59 145
9bd92830
FM
146 if(!same_url || new_user){
147 if(!fp_ou && (fp_ou=MY_FOPEN(DataFile,"w"))==NULL){
d6f0349d 148 debuga(_("(datafile) Cannot open file %s: %s\n"),DataFile,strerror(errno));
9bd92830
FM
149 exit(EXIT_FAILURE);
150 }
151 saverecs(fp_ou,uinfo,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache);
152 nacc=0;
153 nbytes=0;
154 nelap=0;
155 incache=0;
156 oucache=0;
157 new_user=0;
158 }
5542cb59 159
9bd92830
FM
160 nacc++;
161 nbytes+=accbytes;
162 nelap+=accelap;
25697a35 163
9bd92830
FM
164 strcpy(crc2,acccode);
165 str=strchr(crc2,'/');
166 if (str) *str='\0';
5542cb59 167
9bd92830
FM
168 if(strstr(crc2,"MISS") != 0) oucache+=accbytes;
169 else incache+=accbytes;
25697a35 170
9bd92830
FM
171 if (!same_url) {
172 url_len=strlen(accurl);
173 if (url_len>=ourl_size) {
174 ourl_size=url_len+1;
175 oldurl=realloc(oldurl,ourl_size);
176 if (!oldurl) {
177 debuga(_("Not enough memory to store the url\n"));
178 exit(EXIT_FAILURE);
179 }
180 }
181 strcpy(oldurl,accurl);
182 }
183 new_user=0;
184 strcpy(oldacccode,acccode);
185 strcpy(oldaccip,accip);
186 strcpy(oldaccdia,accdia);
187 strcpy(oldacchora,acchora);
188 }
5542cb59 189
9bd92830
FM
190 fclose(fp_in);
191 longline_destroy(&line);
192 }
93551487 193 userinfo_stopscan(uscan);
9bd92830
FM
194 if (oldurl) free(oldurl);
195 if (fp_ou) fclose(fp_ou);
5542cb59 196
9bd92830
FM
197 if(debug)
198 debuga(_("Datafile %s written successfully\n"),DataFile);
25697a35
GS
199}
200
f84a35a3 201void 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 202{
9bd92830
FM
203 char val[20];
204 char name[512];
25697a35 205
9bd92830
FM
206 if((DataFileFields & DATA_FIELD_USER) != 0) {
207 fputs(uinfo->label,fp_ou);
208 fputc(DataFileDelimiter[0],fp_ou);
209 }
210 if((DataFileFields & DATA_FIELD_DATE) != 0) {
211 fputs(dia,fp_ou);
212 fputc(DataFileDelimiter[0],fp_ou);
213 }
214 if((DataFileFields & DATA_FIELD_TIME) != 0) {
215 fputs(hora,fp_ou);
216 fputc(DataFileDelimiter[0],fp_ou);
217 }
218 if((DataFileFields & DATA_FIELD_URL) != 0) {
219 url_hostname(url,name,sizeof(name));
4afbb7a5 220 if (DataFileUrl == DATAFILEURL_IP) name2ip(name,sizeof(name));
9bd92830
FM
221 fputs(name,fp_ou);
222 fputc(DataFileDelimiter[0],fp_ou);
223 }
224 if((DataFileFields & DATA_FIELD_CONNECT) != 0) {
225 my_lltoa(nacc,val,sizeof(val),0);
226 fputs(val,fp_ou);
227 fputc(DataFileDelimiter[0],fp_ou);
228 }
229 if((DataFileFields & DATA_FIELD_BYTES) != 0) {
230 my_lltoa(nbytes,val,sizeof(val),0);
231 fputs(val,fp_ou);
232 fputc(DataFileDelimiter[0],fp_ou);
233 }
234 if((DataFileFields & DATA_FIELD_IN_CACHE) != 0) {
235 my_lltoa(incache,val,sizeof(val),0);
236 fputs(val,fp_ou);
237 fputc(DataFileDelimiter[0],fp_ou);
238 }
239 if((DataFileFields & DATA_FIELD_OUT_CACHE) != 0) {
240 my_lltoa(oucache,val,sizeof(val),0);
241 fputs(val,fp_ou);
242 fputc(DataFileDelimiter[0],fp_ou);
243 }
244 if((DataFileFields & DATA_FIELD_ELAPSED) != 0) {
245 my_lltoa(nelap,val,sizeof(val),0);
246 fputs(val,fp_ou);
247 fputc(DataFileDelimiter[0],fp_ou);
248 }
25697a35 249
9bd92830 250 fputc('\n',fp_ou);
25697a35 251}