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