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