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