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