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