]> git.ipfire.org Git - thirdparty/sarg.git/blame - report.c
Rename configure.in as configure.ac
[thirdparty/sarg.git] / report.c
CommitLineData
25697a35 1/*
94ff9470 2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
110ce984 3 * 1998, 2015
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"
28#include "include/defs.h"
29
9dc20988
FM
30//! The global statistics of the whole log read.
31struct globalstatstruct globstat;
27d1fa35
FM
32//! \c True to enable the smart filter.
33bool smartfilter=false;
9dc20988 34
507460ae 35//! The file to store the HTML page where the time of access is reported for one site and one user.
e5b2c6f0 36static FILE *fp_tt=NULL;
507460ae
FM
37//! The name of the file containing the access time of the site/user.
38static char arqtt[4096]="";
e5b2c6f0 39
d8e04f05 40static FILE *maketmp(const char *user, const char *dirname, int debug);
d8e04f05 41static void gravatmp(FILE *fp_ou, const char *oldurl, long long int nacc, long long int nbytes, const char *oldmsg, long long int nelap, long long int incache, long long int oucache);
f72b484a 42static void closett(void);
6588b137 43static void gravaporuser(const struct userinfostruct *uinfo, const char *dirname, const char *url, const char *ip, const char *data, const char *hora, long long int tam, long long int elap);
9dc20988 44static void gravager(FILE *fp_gen,const char *filename, const struct userinfostruct *uinfo, long long int nacc, const char *url, long long int nbytes, const char *ip, const char *hora, const char *dia, long long int nelap, long long int incache, long long int oucache);
5160b452 45static void grava_SmartFilter(const char *dirname, const char *user, const char *ip, const char *data, const char *hora, const char *url, const char *smart);
5f3cfd1d 46
32e71fa4 47void gerarel(void)
25697a35 48{
9bd92830
FM
49 FILE *fp_in;
50 FILE *fp_gen;
d8e04f05 51 FILE *fp_tmp=NULL;
9bd92830
FM
52
53 char *buf;
4d2a6d53
FM
54 char accdia[11], acchora[9], accip[256], *accurl;
55 char oldaccdia[11], oldacchora[9], oldaccip[256];
56 char oldacciptt[256];
9bd92830
FM
57 char wdirname[MAXLEN];
58 char *oldurl=NULL;
93551487 59 char oldmsg[50], acccode[MAXLEN/2 - 1], oldacccode[MAXLEN/2 - 1];
4d2a6d53 60 char ipantes[256], nameantes[MAXLEN];
9bd92830
FM
61 char accsmart[MAXLEN];
62 char crc2[MAXLEN/2 -1];
63 char siteind[MAX_TRUNCATED_URL];
9bd92830
FM
64 char *oldurltt=NULL;
65 char oldaccdiatt[11],oldacchoratt[9];
66 char tmp3[MAXLEN];
9bd92830 67 char u2[MAX_USER_LEN];
b6b6cb8c 68 char userlabel[MAX_USER_LEN];
9bd92830
FM
69 long long int nbytes=0;
70 long long int nelap=0;
71 long long int nacc=0;
9bd92830
FM
72 long long int incache=0;
73 long long int oucache=0;
74 long long int accbytes, accelap;
75 char *str;
93551487 76 userscan uscan;
9bd92830
FM
77 const char *sort_field;
78 const char *sort_order;
93551487 79 const char *user;
9bd92830
FM
80 int url_len;
81 int ourl_size=0;
82 int ourltt_size=0;
83 int same_url;
d8e04f05 84 bool new_user;
9bd92830
FM
85 struct getwordstruct gwarea;
86 longline line;
d8e04f05 87 struct userinfostruct *uinfo;
026ddd8b 88 DayObject daystat;
9bd92830
FM
89
90 ipantes[0]='\0';
27d1fa35 91 smartfilter=false;
9dc20988 92 memset(&globstat,0,sizeof(globstat));
9bd92830
FM
93
94 if (vrfydir(&period, addr, site, us, email)<0) {
af961877 95 debuga(__FILE__,__LINE__,_("Cannot create the output directory name containing the period as part of the name\n"));
9bd92830
FM
96 exit(EXIT_FAILURE);
97 }
98
cb59dc47 99 if(debugz>=LogLevel_Process){
af961877 100 debugaz(__FILE__,__LINE__,_("outdirname=%s\n"),outdirname);
9bd92830
FM
101 }
102
103 if(UserAgentLog[0] != '\0' && email[0] == '\0') useragent();
104
105 snprintf(wdirname,sizeof(wdirname),"%s/sarg-general",outdirname);
106 if((fp_gen=MY_FOPEN(wdirname,"w"))==NULL){
af961877 107 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),wdirname,strerror(errno));
9bd92830
FM
108 exit(EXIT_FAILURE);
109 }
110
9bd92830 111 fp_tt=NULL;
fefdc019 112 sort_labels(&sort_field,&sort_order);
9bd92830 113
026ddd8b
FM
114 if (indexonly || datetimeby==0)
115 daystat=NULL;
116 else
117 daystat=day_prepare();
bd43d81f 118
93551487
FM
119 uscan=userinfo_startscan();
120 if (uscan == NULL) {
af961877 121 debuga(__FILE__,__LINE__,_("Cannot enumerate the user list\n"));
9bd92830
FM
122 exit(EXIT_FAILURE);
123 }
93551487 124 while ((uinfo = userinfo_advancescan(uscan)) != NULL ) {
461b479d 125 sort_users_log(tmp,debug,uinfo);
170a77ea 126 if (snprintf(tmp3,sizeof(tmp3),"%s/%s.user_log",tmp,uinfo->filename)>=sizeof(tmp3)) {
af961877 127 debuga(__FILE__,__LINE__,_("Path too long: "));
96dadc9f 128 debuga_more("%s/%s.user_log\n",tmp,uinfo->filename);
9bd92830
FM
129 exit(EXIT_FAILURE);
130 }
131 if((fp_in=MY_FOPEN(tmp3,"r"))==NULL){
af961877 132 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),tmp3,strerror(errno));
9bd92830
FM
133 exit(EXIT_FAILURE);
134 }
93551487 135 user=uinfo->filename;
026ddd8b 136 day_newuser(daystat);
9bd92830 137
9bd92830
FM
138 strcpy(u2,uinfo->id);
139 if(Ip2Name && uinfo->id_is_ip) {
4d2a6d53 140 safe_strcpy(ipantes,u2,sizeof(ipantes));
9bd92830
FM
141 ip2name(u2,sizeof(u2));
142 strcpy(nameantes,u2);
143 }
b6b6cb8c
FM
144 user_find(userlabel,MAX_USER_LEN, u2);
145 userinfo_label(uinfo,userlabel);
9bd92830 146
6588b137 147 if (!indexonly) {
d8e04f05 148 fp_tmp=maketmp(user,tmp,debug);
6588b137 149 }
9bd92830
FM
150
151 ttopen=0;
d8e04f05 152 oldurl=NULL;
9bd92830
FM
153 oldurltt=NULL;
154 ourltt_size=0;
155 memset(oldaccdiatt,0,sizeof(oldaccdiatt));
156 memset(oldacchoratt,0,sizeof(oldacchoratt));
4d2a6d53 157 memset(oldacciptt,0,sizeof(oldacciptt));
d8e04f05
FM
158 new_user=true;
159 nacc=0;
160 nbytes=0;
161 nelap=0;
162 incache=0;
163 oucache=0;
9bd92830
FM
164
165 if ((line=longline_create())==NULL) {
af961877 166 debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\"\n"),tmp3);
9bd92830
FM
167 exit(EXIT_FAILURE);
168 }
169
170 while((buf=longline_read(fp_in,line))!=NULL) {
171 getword_start(&gwarea,buf);
172 if (getword(accdia,sizeof(accdia),&gwarea,'\t')<0 || getword(acchora,sizeof(acchora),&gwarea,'\t')<0 ||
007905af
FM
173 getword(accip,sizeof(accip),&gwarea,'\t')<0 ||
174 getword_ptr(buf,&accurl,&gwarea,'\t')<0 || getword_atoll(&accbytes,&gwarea,'\t')<0 ||
175 getword(acccode,sizeof(acccode),&gwarea,'\t')<0) {
af961877 176 debuga(__FILE__,__LINE__,_("Invalid record in file \"%s\"\n"),tmp3);
9bd92830
FM
177 exit(EXIT_FAILURE);
178 }
179 if(strncmp(acccode,"TCP_DENIED/407",14) == 0) continue;
180 if (getword_atoll(&accelap,&gwarea,'\t')<0) {
af961877 181 debuga(__FILE__,__LINE__,_("Invalid elapsed time in file \"%s\"\n"),tmp3);
9bd92830
FM
182 exit(EXIT_FAILURE);
183 }
184 if (getword_skip(20000,&gwarea,'"')<0 || getword(accsmart,sizeof(accsmart),&gwarea,'"')<0) {
af961877 185 debuga(__FILE__,__LINE__,_("Invalid smart info in file \"%s\"\n"),tmp3);
9bd92830
FM
186 exit(EXIT_FAILURE);
187 }
188
189 if(accsmart[0] != '\0') {
27d1fa35 190 smartfilter=true;
1caa1b3d 191 grava_SmartFilter(tmp,uinfo->id,accip,accdia,acchora,accurl,accsmart);
9bd92830
FM
192 }
193
194 if(Ip2Name) {
195 if(strcmp(accip,ipantes) != 0) {
196 strcpy(ipantes,accip);
197 ip2name(accip,sizeof(accip));
198 strcpy(nameantes,accip);
4d2a6d53 199 } else safe_strcpy(accip,nameantes,sizeof(accip));
9bd92830
FM
200 }
201
6588b137 202 if (!indexonly) {
026ddd8b 203 day_addpoint(daystat,accdia,acchora,accelap,accbytes);
d8e04f05 204 if (iprel) gravaporuser(uinfo,outdirname,accurl,accip,accdia,acchora,accbytes,accelap);
9bd92830
FM
205 }
206
d8e04f05 207 if(new_user){
9bd92830
FM
208 url_len=strlen(accurl);
209 if (!oldurl || url_len>=ourl_size) {
210 ourl_size=url_len+1;
211 oldurl=realloc(oldurl,ourl_size);
212 if (!oldurl) {
af961877 213 debuga(__FILE__,__LINE__,_("Not enough memory to store the url\n"));
9bd92830
FM
214 exit(EXIT_FAILURE);
215 }
216 }
217 strcpy(oldurl,accurl);
218 strcpy(oldacccode,acccode);
9bd92830
FM
219 strcpy(oldaccip,accip);
220 strcpy(oldaccdia,accdia);
221 strcpy(oldacchora,acchora);
d8e04f05 222 new_user=false;
9bd92830
FM
223 }
224 same_url=(strcmp(oldurl,accurl) == 0);
225
d8e04f05
FM
226 if(site[0] == '\0') {
227 if(!same_url){
9bd92830
FM
228 if(strstr(oldacccode,"DENIED") != 0)
229 strcpy(oldmsg,"DENIED");
230 else
231 strcpy(oldmsg,"OK");
d8e04f05
FM
232 if (fp_tmp) gravatmp(fp_tmp,oldurl,nacc,nbytes,oldmsg,nelap,incache,oucache);
233 gravager(fp_gen,wdirname,uinfo,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache);
9bd92830
FM
234 nacc=0;
235 nbytes=0;
236 nelap=0;
237 incache=0;
238 oucache=0;
239 }
240 }
241 nacc++;
242 nbytes+=accbytes;
243 nelap+=accelap;
244
1219fb1a 245 if ((ReportType & REPORT_TYPE_SITE_USER_TIME_DATE) != 0 && !indexonly &&
4d2a6d53
FM
246 (!oldurltt || strcmp(oldurltt,accurl) || strcmp(oldaccdiatt,accdia) || strcmp(oldacchoratt,acchora) ||
247 strcmp(oldacciptt,accip))) {
9bd92830
FM
248
249 if(!ttopen) {
250 snprintf(arqtt,sizeof(arqtt),"%s/%s",outdirname,uinfo->filename);
251 if(access(arqtt, R_OK) != 0)
252 my_mkdir(arqtt);
f72b484a 253 snprintf(arqtt,sizeof(arqtt),"%s/%s/tt.html",outdirname,uinfo->filename);
9bd92830 254 if ((fp_tt = fopen(arqtt, "w")) == 0) {
af961877 255 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),arqtt,strerror(errno));
9bd92830
FM
256 exit(EXIT_FAILURE);
257 }
258 ttopen=1;
259
260 /*
261 if(Privacy)
007905af 262 sprintf(httplink,"<font size=%s color=%s><href=http://%s>%s",FontSize,PrivacyStringColor,PrivacyString,PrivacyString);
9bd92830
FM
263 else
264 sprintf(httplink,"<font size=%s><a href=\"http://%s\">%s</a>",FontSize,accurl,accurl);
265 */
266
267 write_html_header(fp_tt,(IndexTree == INDEX_TREE_DATE) ? 4 : 2,_("Site access report"),HTML_JS_NONE);
268 fprintf(fp_tt,"<tr><td class=\"header_c\">%s:&nbsp;%s</td></tr>\n",_("Period"),period.html);
269 fprintf(fp_tt,"<tr><td class=\"header_c\">%s:&nbsp;%s</td></tr>\n",_("User"),uinfo->label);
270 fputs("<tr><td class=\"header_c\">",fp_tt);
271 fprintf(fp_tt,_("Sort:&nbsp;%s, %s"),sort_field,sort_order);
272 fputs("</td></tr>\n",fp_tt);
273 fprintf(fp_tt,"<tr><th class=\"header_c\">%s</th></tr>\n",_("User"));
274 close_html_header(fp_tt);
275
276 fputs("<div class=\"report\"><table cellpadding=\"0\" cellspacing=\"2\">\n",fp_tt);
f72b484a
FM
277 }
278 if (!oldurltt || strcmp(oldurltt,accurl)) {
279 const char *url=accurl;
280 if (*url==ALIAS_PREFIX) url++;
281 url_to_anchor(accurl,siteind,sizeof(siteind));
282 fprintf(fp_tt,"<tr class=\"tt\"><td colspan=\"3\"><a name=\"%s\">",siteind);
283 fprintf(fp_tt,"<b>%s</b>",_("Accessed site: "));
284 output_html_string(fp_tt,url,100);
285 fputs("</a></td></tr>\n",fp_tt);
286 fprintf(fp_tt,"<tr><th class=\"header_l\">%s</th>",_("IP"));
eb7ab620 287 fprintf(fp_tt,"<th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr>\n",_("DATE"),pgettext("wall clock","TIME"));
9bd92830
FM
288 }
289
f72b484a 290 fprintf(fp_tt,"<tr><td class=\"data2\">%s</td>",accip);
4d2a6d53 291 fprintf(fp_tt,"<td class=\"data\">%s</td><td class=\"data\">%s</td></tr>\n",accdia,acchora);
9bd92830
FM
292
293 url_len=strlen(accurl);
294 if (!oldurltt || url_len>=ourltt_size) {
295 ourltt_size=url_len+1;
296 oldurltt=realloc(oldurltt,ourltt_size);
297 if (!oldurltt) {
af961877 298 debuga(__FILE__,__LINE__,_("Not enough memory to store the url\n"));
9bd92830
FM
299 exit(EXIT_FAILURE);
300 }
301 }
302 strcpy(oldurltt,accurl);
303 strcpy(oldaccdiatt,accdia);
304 strcpy(oldacchoratt,acchora);
4d2a6d53 305 strcpy(oldacciptt,accip);
9bd92830
FM
306 }
307
308 strcpy(crc2,acccode);
309 str=strchr(crc2,'/');
310 if (str) *str='\0';
311 if(strstr(crc2,"MISS") != 0)
312 oucache+=accbytes;
d8e04f05
FM
313 else
314 incache+=accbytes;
9bd92830 315
9bd92830
FM
316 strcpy(oldacccode,acccode);
317 strcpy(oldaccip,accip);
318 if (!same_url) {
319 url_len=strlen(accurl);
320 if (url_len>=ourl_size) {
321 ourl_size=url_len+1;
322 oldurl=realloc(oldurl,ourl_size);
323 if (!oldurl) {
af961877 324 debuga(__FILE__,__LINE__,_("Not enough memory to store the url\n"));
9bd92830
FM
325 exit(EXIT_FAILURE);
326 }
327 }
328 strcpy(oldurl,accurl);
329 }
330 strcpy(oldaccdia,accdia);
331 strcpy(oldacchora,acchora);
9bd92830 332 }
204781f4 333 if (fclose(fp_in)==EOF) {
af961877 334 debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),tmp3,strerror(errno));
204781f4
FM
335 exit(EXIT_FAILURE);
336 }
9bd92830
FM
337 longline_destroy(&line);
338 if (oldurltt) free(oldurltt);
d8e04f05
FM
339 if (oldurl) {
340 if(strstr(oldacccode,"DENIED") != 0)
341 strcpy(oldmsg,"DENIED");
342 else
343 strcpy(oldmsg,"OK");
344 if (fp_tmp) gravatmp(fp_tmp,oldurl,nacc,nbytes,oldmsg,nelap,incache,oucache);
f72b484a 345 closett();
d8e04f05
FM
346 gravager(fp_gen,wdirname,uinfo,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache);
347 free(oldurl);
348 oldurl=NULL;
349 }
d8e04f05 350 if(!new_user) {
026ddd8b 351 day_totalize(daystat,tmp,uinfo);
d8e04f05
FM
352 }
353 if (fp_tmp) {
507460ae 354 if (fclose(fp_tmp)==EOF) {
af961877 355 debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),uinfo->filename,strerror(errno));
507460ae
FM
356 exit(EXIT_FAILURE);
357 }
d8e04f05
FM
358 fp_tmp=NULL;
359 }
11767c6a 360 if (!KeepTempLog && unlink(tmp3)) {
af961877 361 debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),tmp3,strerror(errno));
08f9b029
FM
362 exit(EXIT_FAILURE);
363 }
9bd92830 364 }
93551487 365 userinfo_stopscan(uscan);
026ddd8b 366 day_cleanup(daystat);
9bd92830 367
9dc20988 368 totalger(fp_gen,wdirname);
507460ae 369 if (fclose(fp_gen)==EOF) {
af961877 370 debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),wdirname,strerror(errno));
507460ae
FM
371 exit(EXIT_FAILURE);
372 }
9bd92830 373
9bd92830 374 if(email[0] == '\0') {
edc276df 375 if (!indexonly) {
9f93fec3 376 if(DansGuardianConf[0] != '\0')
edc276df 377 dansguardian_log();
cb59dc47 378 else if (debugz>=LogLevel_Process)
af961877 379 debugaz(__FILE__,__LINE__,_("Dansguardian report not produced because no dansguardian configuration file was provided\n"));
9bd92830 380
330b1c52 381 redirector_log();
9a759ea8 382 }
9bd92830 383
9a759ea8
FM
384 topuser();
385
386 if (!indexonly) {
5138c1b9
FM
387 if((ReportType & REPORT_TYPE_DOWNLOADS) != 0)
388 download_report();
cb59dc47 389 else if (debugz>=LogLevel_Process)
af961877 390 debugaz(__FILE__,__LINE__,_("Downloaded files report not requested in report_type\n"));
5138c1b9 391
9f93fec3
FM
392 if((ReportType & REPORT_TYPE_TOPSITES) != 0)
393 topsites();
cb59dc47 394 else if (debugz>=LogLevel_Process)
af961877 395 debugaz(__FILE__,__LINE__,_("Top sites report not requested in report_type\n"));
9f93fec3
FM
396
397 if((ReportType & REPORT_TYPE_SITES_USERS) != 0)
398 siteuser();
cb59dc47 399 else if (debugz>=LogLevel_Process)
af961877 400 debugaz(__FILE__,__LINE__,_("Sites & users report not requested in report_type\n"));
bd43d81f 401
1bd73f70
FM
402 if ((ReportType & REPORT_TYPE_DENIED) != 0)
403 gen_denied_report();
cb59dc47 404 else if (debugz>=LogLevel_Process)
af961877 405 debugaz(__FILE__,__LINE__,_("Denied accesses report not requested in report_type\n"));
9bd92830 406
1bd73f70
FM
407 if ((ReportType & REPORT_TYPE_AUTH_FAILURES) != 0)
408 authfail_report();
cb59dc47 409 else if (debugz>=LogLevel_Process)
af961877 410 debugaz(__FILE__,__LINE__,_("Authentication failures report not requested in report_type\n"));
9bd92830 411
edc276df 412 if(smartfilter) smartfilter_report();
9bd92830 413
9f93fec3
FM
414 if(DansGuardianConf[0] != '\0')
415 dansguardian_report();
9bd92830 416
330b1c52 417 redirector_report();
9bd92830 418
9f93fec3
FM
419 if((ReportType & REPORT_TYPE_USERS_SITES) != 0)
420 htmlrel();
cb59dc47 421 else if (debugz>=LogLevel_Process)
af961877 422 debugaz(__FILE__,__LINE__,_("User's detailed report not requested in report_type\n"));
edc276df 423 }
9bd92830
FM
424
425 make_index();
426
af961877 427 if(SuccessfulMsg) debuga(__FILE__,__LINE__,_("Successful report generated on %s\n"),outdirname);
9bd92830
FM
428 } else {
429 geramail(outdirname, debug, outdir, email, tmp);
430
431 if((strcmp(email,"stdout") != 0) && SuccessfulMsg)
af961877 432 debuga(__FILE__,__LINE__,_("Successful report generated and sent to %s\n"),email);
9bd92830
FM
433 }
434
6588b137 435 if(indexonly) index_only(outdirname, debug);
9bd92830
FM
436
437 removetmp(outdirname);
438 return;
25697a35
GS
439}
440
d8e04f05 441static FILE *maketmp(const char *user, const char *dirname, int debug)
25697a35 442{
9bd92830
FM
443 FILE *fp_ou;
444 char wdirname[MAXLEN];
25697a35 445
d8e04f05
FM
446 if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return(NULL);
447 if((ReportType & REPORT_TYPE_TOPUSERS) == 0) return(NULL);
25697a35 448
af961877 449 if(debug) debuga(__FILE__,__LINE__,_("Making file %s/%s\n"),tmp,user);
9bd92830 450 if (snprintf(wdirname,sizeof(wdirname),"%s/%s.utmp",tmp,user)>=sizeof(wdirname)) {
af961877 451 debuga(__FILE__,__LINE__,_("Path too long: "));
041018b6 452 debuga_more("%s/%s.utmp\n",tmp,user);
9bd92830
FM
453 exit(EXIT_FAILURE);
454 }
25697a35 455
9bd92830 456 if((fp_ou=fopen(wdirname,"w"))==NULL){
af961877 457 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),wdirname,strerror(errno));
9bd92830
FM
458 exit(EXIT_FAILURE);
459 }
25697a35 460
d8e04f05 461 return(fp_ou);
25697a35
GS
462}
463
464
d8e04f05 465static void gravatmp(FILE *fp_ou, const char *oldurl, long long int nacc, long long int nbytes, const char *oldmsg, long long int nelap, long long int incache, long long int oucache)
25697a35 466{
9bd92830
FM
467 /*
468 This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
469 to print a long long int unless it is exactly 64-bits long.
470 */
471 fprintf(fp_ou,"%"PRIu64"\t%"PRIu64"\t%s\t%s\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\n",(uint64_t)nacc,(uint64_t)nbytes,oldurl,oldmsg,(uint64_t)nelap,(uint64_t)incache,(uint64_t)oucache);
472
f72b484a
FM
473 return;
474}
475
476static void closett(void)
477{
9bd92830
FM
478 ttopen=0;
479
480 if(fp_tt) {
481 fputs("</table>\n</div>\n",fp_tt);
482 fputs("</body>\n</html>\n",fp_tt);
507460ae 483 if (fclose(fp_tt)==EOF) {
af961877 484 debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),arqtt,strerror(errno));
507460ae
FM
485 exit(EXIT_FAILURE);
486 }
9bd92830
FM
487 fp_tt=NULL;
488 }
25697a35
GS
489}
490
6588b137 491static void gravaporuser(const struct userinfostruct *uinfo, const char *dirname, const char *url, const char *ip, const char *data, const char *hora, long long int tam, long long int elap)
25697a35 492{
9bd92830
FM
493 FILE *fp_ou;
494 char wdirname[MAXLEN];
25697a35 495
6588b137 496 if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return;
25697a35 497
9bd92830 498 if (snprintf(wdirname,sizeof(wdirname),"%s/%s.ip",tmp,uinfo->filename)>=sizeof(wdirname)) {
af961877 499 debuga(__FILE__,__LINE__,_("Path too long: "));
041018b6 500 debuga_more("%s/%s.ip\n",tmp,uinfo->filename);
9bd92830
FM
501 exit(EXIT_FAILURE);
502 }
25697a35 503
9bd92830 504 if((fp_ou=MY_FOPEN(wdirname,"a"))==NULL){
af961877 505 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),wdirname,strerror(errno));
9bd92830
FM
506 exit(EXIT_FAILURE);
507 }
25697a35 508
9bd92830
FM
509 /*
510 This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
511 to print a long long int unless it is exactly 64-bits long.
512 */
513 fprintf(fp_ou,"%s\t%s\t%s\t%s\t%"PRIu64"\t%"PRIu64"\n",ip,url,data,hora,(uint64_t)tam,(uint64_t)elap);
25697a35 514
507460ae 515 if (fclose(fp_ou)==EOF) {
af961877 516 debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),wdirname,strerror(errno));
507460ae
FM
517 exit(EXIT_FAILURE);
518 }
4157aa09 519
9bd92830 520 return;
25697a35
GS
521}
522
523
9dc20988 524static void gravager(FILE *fp_gen,const char *filename, const struct userinfostruct *uinfo, long long int nacc, const char *url, long long int nbytes, const char *ip, const char *hora, const char *dia, long long int nelap, long long int incache, long long int oucache)
25697a35 525{
9bd92830
FM
526 /*
527 This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
528 to print a long long int unless it is exactly 64-bits long.
529 */
9dc20988 530 if (fprintf(fp_gen,"%s\t%"PRIu64"\t%"PRIu64"\t%s\t%s\t%s\t%s\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\n",uinfo->id,(uint64_t)nacc,(uint64_t)nbytes,url,ip,hora,dia,(uint64_t)nelap,(uint64_t)incache,(uint64_t)oucache)<0) {
af961877 531 debuga(__FILE__,__LINE__,_("Write error in file \"%s\": %s\n"),filename,strerror(errno));
9dc20988
FM
532 exit(EXIT_FAILURE);
533 }
534
535 globstat.nacc+=nacc;
536 globstat.nbytes+=nbytes;
537 globstat.elap+=nelap;
538 globstat.incache+=incache;
539 globstat.oucache+=oucache;
9bd92830 540 return;
25697a35
GS
541}
542
9dc20988
FM
543/*!
544Write the total line at the end of the general file.
545*/
546void totalger(FILE *fp_gen,const char *filename)
547{
548 /*
549 This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
550 to print a long long int unless it is exactly 64-bits long.
551 */
552 if (fprintf(fp_gen,"TOTAL\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\n",(uint64_t)globstat.nacc,(uint64_t)globstat.nbytes,(uint64_t)globstat.elap,(uint64_t)globstat.incache,(uint64_t)globstat.oucache)<0) {
af961877 553 debuga(__FILE__,__LINE__,_("Failed to write the total line in %s\n"),filename);
9dc20988
FM
554 exit(EXIT_FAILURE);
555 }
556}
557
2240dcea
FM
558int ger_read(char *buffer,struct generalitemstruct *item,const char *filename)
559{
9bd92830
FM
560 int i;
561 int sign;
562 long long int number;
563
564 if (strncmp(buffer,"TOTAL\t",6)==0) {
565 item->total=1;
566 buffer+=6;
567
568 sign=+1;
569 if (*buffer == '-') {
570 buffer++;
571 sign=-1;
572 } else if (*buffer == '+') {
573 buffer++;
574 }
575 number=0LL;
576 while (isdigit(*buffer))
577 number=(number * 10) + (*buffer++)-'0';
578 if (*buffer!='\t') {
af961877 579 debuga(__FILE__,__LINE__,_("Invalid total number of accesses in file \"%s\"\n"),filename);
9bd92830
FM
580 exit(EXIT_FAILURE);
581 }
582 buffer++;
583 item->nacc=number*sign;
584
585 sign=+1;
586 if (*buffer == '-') {
587 buffer++;
588 sign=-1;
589 } else if (*buffer == '+') {
590 buffer++;
591 }
592 number=0LL;
593 while (isdigit(*buffer))
594 number=(number * 10) + (*buffer++)-'0';
595 if (*buffer!='\t') {
af961877 596 debuga(__FILE__,__LINE__,_("Invalid total size in file \"%s\"\n"),filename);
9bd92830
FM
597 exit(EXIT_FAILURE);
598 }
599 buffer++;
600 item->nbytes=number*sign;
601
602 sign=+1;
603 if (*buffer == '-') {
604 buffer++;
605 sign=-1;
606 } else if (*buffer == '+') {
607 buffer++;
608 }
609 number=0LL;
610 while (isdigit(*buffer))
611 number=(number * 10) + (*buffer++)-'0';
612 if (*buffer!='\t') {
af961877 613 debuga(__FILE__,__LINE__,_("Invalid total elapsed time in file \"%s\"\n"),filename);
9bd92830
FM
614 exit(EXIT_FAILURE);
615 }
616 buffer++;
617 item->nelap=number*sign;
618
619 sign=+1;
620 if (*buffer == '-') {
621 buffer++;
622 sign=-1;
623 } else if (*buffer == '+') {
624 buffer++;
625 }
626 number=0LL;
627 while (isdigit(*buffer))
628 number=(number * 10) + (*buffer++)-'0';
629 if (*buffer!='\t') {
af961877 630 debuga(__FILE__,__LINE__,_("Invalid total cache hit in file \"%s\"\n"),filename);
9bd92830
FM
631 exit(EXIT_FAILURE);
632 }
633 buffer++;
634 item->incache=number*sign;
635
636 sign=+1;
637 if (*buffer == '-') {
638 buffer++;
639 sign=-1;
640 } else if (*buffer == '+') {
641 buffer++;
642 }
643 number=0LL;
644 while (isdigit(*buffer))
645 number=(number * 10) + (*buffer++)-'0';
646 if (*buffer!='\0') {
af961877 647 debuga(__FILE__,__LINE__,_("Invalid total cache miss in file \"%s\"\n"),filename);
9bd92830
FM
648 exit(EXIT_FAILURE);
649 }
650 item->oucache=number*sign;
651 } else {
652 item->total=0;
653
654 item->user=buffer;
655 for (i=0 ; i<MAX_USER_LEN-1 && (unsigned char)*buffer>=' ' ; i++) buffer++;
656 if (*buffer!='\t') {
af961877 657 debuga(__FILE__,__LINE__,_("User name too long or invalid in file \"%s\"\n"),filename);
9bd92830
FM
658 exit(EXIT_FAILURE);
659 }
660 *buffer++='\0';
661
662 sign=+1;
663 if (*buffer == '-') {
664 buffer++;
665 sign=-1;
666 } else if (*buffer == '+') {
667 buffer++;
668 }
669 number=0LL;
670 while (isdigit(*buffer))
671 number=(number * 10) + (*buffer++)-'0';
672 if (*buffer!='\t') {
af961877 673 debuga(__FILE__,__LINE__,_("Invalid number of accesses in file \"%s\"\n"),filename);
9bd92830
FM
674 exit(EXIT_FAILURE);
675 }
676 buffer++;
677 item->nacc=number*sign;
678
679 sign=+1;
680 if (*buffer == '-') {
681 buffer++;
682 sign=-1;
683 } else if (*buffer == '+') {
684 buffer++;
685 }
686 number=0LL;
687 while (isdigit(*buffer))
688 number=(number * 10) + (*buffer++)-'0';
689 if (*buffer!='\t') {
af961877 690 debuga(__FILE__,__LINE__,_("Invalid number of bytes in file \"%s\"\n"),filename);
9bd92830
FM
691 exit(EXIT_FAILURE);
692 }
693 buffer++;
694 item->nbytes=number*sign;
695
696 item->url=buffer;
697 while ((unsigned char)*buffer>=' ') buffer++;
698 if (*buffer!='\t') {
af961877 699 debuga(__FILE__,__LINE__,_("URL too long or invalid in file \"%s\"\n"),filename);
9bd92830
FM
700 exit(EXIT_FAILURE);
701 }
702 *buffer++='\0';
703
704 item->ip=buffer;
705 for (i=0 ; i<MAX_IP_LEN-1 && (unsigned char)*buffer>=' ' ; i++) buffer++;
706 if (*buffer!='\t') {
af961877 707 debuga(__FILE__,__LINE__,_("IP address too long or invalid in file \"%s\"\n"),filename);
9bd92830
FM
708 exit(EXIT_FAILURE);
709 }
710 *buffer++='\0';
711
712 item->time=buffer;
713 for (i=0 ; i<MAX_DATETIME_LEN-1 && (unsigned char)*buffer>=' ' ; i++) buffer++;
714 if (*buffer!='\t') {
af961877 715 debuga(__FILE__,__LINE__,_("Time too long or invalid in file \"%s\"\n"),filename);
9bd92830
FM
716 exit(EXIT_FAILURE);
717 }
718 *buffer++='\0';
719
720 item->date=buffer;
721 for (i=0 ; i<MAX_DATETIME_LEN-1 && (unsigned char)*buffer>=' ' ; i++) buffer++;
722 if (*buffer!='\t') {
af961877 723 debuga(__FILE__,__LINE__,_("Date too long or invalid in file \"%s\"\n"),filename);
9bd92830
FM
724 exit(EXIT_FAILURE);
725 }
726 *buffer++='\0';
727
728 sign=+1;
729 if (*buffer == '-') {
730 buffer++;
731 sign=-1;
732 } else if (*buffer == '+') {
733 buffer++;
734 }
735 number=0LL;
736 while (isdigit(*buffer))
737 number=(number * 10) + (*buffer++)-'0';
738 if (*buffer!='\t') {
af961877 739 debuga(__FILE__,__LINE__,_("Invalid elapsed time in file \"%s\"\n"),filename);
9bd92830
FM
740 exit(EXIT_FAILURE);
741 }
742 buffer++;
743 item->nelap=number*sign;
744
745 sign=+1;
746 if (*buffer == '-') {
747 buffer++;
748 sign=-1;
749 } else if (*buffer == '+') {
750 buffer++;
751 }
752 number=0LL;
753 while (isdigit(*buffer))
754 number=(number * 10) + (*buffer++)-'0';
755 if (*buffer!='\t') {
af961877 756 debuga(__FILE__,__LINE__,_("Invalid cache hit size in file \"%s\"\n"),filename);
9bd92830
FM
757 exit(EXIT_FAILURE);
758 }
759 buffer++;
760 item->incache=number*sign;
761
762 sign=+1;
763 if (*buffer == '-') {
764 buffer++;
765 sign=-1;
766 } else if (*buffer == '+') {
767 buffer++;
768 }
769 number=0LL;
770 while (isdigit(*buffer))
771 number=(number * 10) + (*buffer++)-'0';
772 if (*buffer!='\0') {
af961877 773 debuga(__FILE__,__LINE__,_("Invalid cache miss size in file \"%s\"\n"),filename);
9bd92830
FM
774 exit(EXIT_FAILURE);
775 }
776 item->oucache=number*sign;
777 }
778 return(0);
2240dcea
FM
779}
780
5160b452 781static void grava_SmartFilter(const char *dirname, const char *user, const char *ip, const char *data, const char *hora, const char *url, const char *smart)
25697a35 782{
9bd92830
FM
783 FILE *fp_ou;
784 char wdirname[MAXLEN];
25697a35 785
1caa1b3d 786 if (snprintf(wdirname,sizeof(wdirname),"%s/smartfilter.int_unsort",dirname)>=sizeof(wdirname)) {
af961877 787 debuga(__FILE__,__LINE__,_("Path too long: "));
041018b6 788 debuga_more("%s/smartfilter.int_unsort\n",dirname);
1caa1b3d
FM
789 exit(EXIT_FAILURE);
790 }
25697a35 791
9bd92830 792 if((fp_ou=MY_FOPEN(wdirname,"a"))==NULL){
af961877 793 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),wdirname,strerror(errno));
9bd92830
FM
794 exit(EXIT_FAILURE);
795 }
25697a35 796
9bd92830
FM
797 fprintf(fp_ou,"%s\t%s\t%s\t%s\t%s\t%s\n",user,data,hora,ip,url,smart);
798 fputs("</body>\n</html>\n",fp_tt);
25697a35 799
507460ae 800 if (fclose(fp_ou)==EOF) {
af961877 801 debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),wdirname,strerror(errno));
507460ae
FM
802 exit(EXIT_FAILURE);
803 }
25697a35 804
9bd92830 805 return;
25697a35 806}