]> git.ipfire.org Git - thirdparty/sarg.git/blame - readlog.c
Store the elapsed time and the number of bytes into the structure
[thirdparty/sarg.git] / readlog.c
CommitLineData
27d1fa35
FM
1/*
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
3 * 1998, 2012
4 *
5 * SARG donations:
6 * please look at http://sarg.sourceforge.net/donations.php
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
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#include "include/readlog.h"
30
31#define REPORT_EVERY_X_LINES 5000
32#define MAX_OPEN_USER_FILES 10
33
34struct userfilestruct
35{
36 struct userfilestruct *next;
37 struct userinfostruct *user;
38 FILE *file;
39};
40
41numlist weekdays = { { 0, 1, 2, 3, 4, 5, 6 }, 7 };
42numlist hours = { { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, 24 };
43
27d1fa35
FM
44extern char *userfile;
45
46/*!
47Read the log files.
48
49\param Filter The filtering parameters for the file to load.
50
51\retval 1 Records found.
52\retval 0 No record found.
53*/
54int ReadLogFile(struct ReadLogDataStruct *Filter)
55{
56 enum isa_col_id {
57 ISACOL_Ip,
58 ISACOL_UserName,
59 ISACOL_Date,
60 ISACOL_Time,
61 ISACOL_TimeTaken,
62 ISACOL_Bytes,
63 ISACOL_Uri,
64 ISACOL_Status,
65 ISACOL_Last //last entry of the list !
66 };
67 enum InputLogFormat {
68 ILF_Unknown,
69 ILF_Squid,
70 ILF_Common,
71 ILF_Sarg,
72 ILF_Isa,
73 ILF_Last //last entry of the list !
74 };
75
76 enum InputLogFormat ilf;
77 int ilf_count[ILF_Last];
78 longline line;
79 char *linebuf;
80 char *str;
81 char *full_url;
82 char arq_log[255];
83 char fun[MAXLEN];
84 char elap[255];
85 char user[MAX_USER_LEN];
86 char data[255];
6debcf3d 87 char ip[60];
27d1fa35
FM
88 char hora[30];
89 char mes[30];
90 char tbuf2[128];
c5d6ef4b 91 char dia[128]="";
27d1fa35
FM
92 char wuser[MAXLEN];
93 char tmp3[MAXLEN];
94 char sz_Download_Unsort[20000];
95 char start_hour[128];
27d1fa35 96 char authfail_unsort[MAXLEN];
27d1fa35
FM
97 char download_url[MAXLEN];
98 char smartfilter[MAXLEN];
99 const char *arq;
100 const char *url;
101 int iarq;
102 int blen;
103 int OutputNonZero = REPORT_EVERY_X_LINES ;
104 int idata=0;
105 int isa_ncols=0,isa_cols[ISACOL_Last];
106 int x;
107 int hmr;
108 int nopen;
109 int maxopenfiles=MAX_OPEN_USER_FILES;
110 int mindate=0;
111 int maxdate=0;
112 int cstatus;
113 unsigned long int recs1=0UL;
114 unsigned long int recs2=0UL;
115 long int totregsl=0;
116 long int totregsg=0;
117 long int totregsx=0;
27d1fa35
FM
118 long long int iyear, imonth, iday;
119 FILE *fp_in=NULL;
120 FILE *fp_log=NULL;
121 FILE *fp_denied=NULL;
122 FILE *fp_authfail=NULL;
123 FILE *fp_Download_Unsort=NULL;
124 bool from_pipe;
125 bool from_stdin;
126 bool download_flag=false;
127 bool id_is_ip;
128 bool totper=false;
129 struct stat logstat;
130 struct getwordstruct gwarea;
27d1fa35
FM
131 struct tm tt;
132 struct userfilestruct *prev_ufile;
133 struct userinfostruct *uinfo;
134 struct userfilestruct *first_user_file=NULL;
135 struct userfilestruct *ufile;
136 struct userfilestruct *ufile1;
c5d6ef4b 137 struct ReadLogStruct log_entry;
27d1fa35
FM
138 time_t tnum;
139
140 for (ilf=0 ; ilf<ILF_Last ; ilf++) ilf_count[ilf]=0;
141 tmp3[0]='\0';
142 start_hour[0]='\0';
143 first_user_file=NULL;
144
145 snprintf(sz_Download_Unsort,sizeof(sz_Download_Unsort),"%s/download.int_unsort", tmp);
27d1fa35
FM
146 snprintf(authfail_unsort,sizeof(authfail_unsort),"%s/authfail.int_unsort",tmp);
147
148 if(DataFile[0]=='\0') {
149 if((ReportType & REPORT_TYPE_DENIED) != 0) {
60b48ae5 150 fp_denied=denied_open();
27d1fa35
FM
151 }
152
153 if((ReportType & REPORT_TYPE_DENIED) != 0 || (ReportType & REPORT_TYPE_AUTH_FAILURES) != 0) {
154 if((fp_authfail=MY_FOPEN(authfail_unsort,"w"))==NULL) {
155 debuga(_("(log) Cannot open file: %s - %s\n"),authfail_unsort,strerror(errno));
156 exit(EXIT_FAILURE);
157 }
158 }
159 }
160
161 if ((line=longline_create())==NULL) {
162 debuga(_("Not enough memory to read a log file\n"));
163 exit(EXIT_FAILURE);
164 }
165
166 for (iarq=0 ; iarq<NAccessLog ; iarq++) {
167 arq=AccessLog[iarq];
168
169 if(strcmp(arq,"-")==0) {
170 if(debug)
171 debuga(_("Reading access log file: from stdin\n"));
172 fp_in=stdin;
173 from_stdin=true;
174 } else {
175 if (Filter->DateRange[0]!='\0') {
176 if (stat(arq,&logstat)!=0) {
177 debuga(_("Cannot get the modification time of input log file %s (%s). Processing it anyway\n"),arq,strerror(errno));
178 } else {
179 struct tm *logtime=localtime(&logstat.st_mtime);
180 if ((logtime->tm_year+1900)*10000+(logtime->tm_mon+1)*100+logtime->tm_mday<dfrom) {
181 debuga(_("Ignoring old log file %s\n"),arq);
182 continue;
183 }
184 }
185 }
186 fp_in=decomp(arq,&from_pipe);
187 if(fp_in==NULL) {
188 debuga(_("(log) Cannot open log file: %s - %s\n"),arq,strerror(errno));
189 exit(EXIT_FAILURE);
190 }
191 if(debug) debuga(_("Reading access log file: %s\n"),arq);
192 from_stdin=false;
193 }
194
195 ilf=ILF_Unknown;
196 download_flag=false;
197
198 // pre-read the file only if we have to show stats
199 if (ShowReadStatistics && !from_stdin && !from_pipe) {
200 size_t nread,i;
201 bool skipcr=false;
202 char tmp4[MAXLEN];
203
204 recs1=0UL;
205 recs2=0UL;
206
207 while ((nread=fread(tmp4,1,sizeof(tmp4),fp_in))>0) {
208 for (i=0 ; i<nread ; i++)
209 if (skipcr) {
210 if (tmp4[i]!='\n' && tmp4[i]!='\r') {
211 skipcr=false;
212 }
213 } else {
214 if (tmp4[i]=='\n' || tmp4[i]=='\r') {
215 skipcr=true;
216 recs1++;
217 }
218 }
219 }
220 rewind(fp_in);
221 printf(_("SARG: Records in file: %lu, reading: %3.2f%%"),recs1,(float) 0);
222 putchar('\r');
223 fflush( stdout ) ;
224 }
225
226 longline_reset(line);
227
228 while ((linebuf=longline_read(fp_in,line))!=NULL) {
229 blen=strlen(linebuf);
230
231 if (ilf==ILF_Unknown) {
232 if(strncmp(linebuf,"#Software: Mic",14) == 0) {
233 fixendofline(linebuf);
234 if (debug)
235 debuga(_("Log is from Microsoft ISA: %s\n"),linebuf);
236 ilf=ILF_Isa;
237 ilf_count[ilf]++;
238 continue;
239 }
240
241 if(strncmp(linebuf,"*** SARG Log ***",16) == 0) {
242 if (getperiod_fromsarglog(arq,&period)<0) {
243 debuga(_("The name of the file is invalid: %s\n"),arq);
244 exit(EXIT_FAILURE);
245 }
246 ilf=ILF_Sarg;
247 ilf_count[ilf]++;
248 continue;
249 }
250 }
251
252 if(!fp_log && ParsedOutputLog[0] && ilf!=ILF_Sarg) {
253 if(access(ParsedOutputLog,R_OK) != 0) {
254 my_mkdir(ParsedOutputLog);
255 }
256 if (snprintf(arq_log,sizeof(arq_log),"%s/sarg_temp.log",ParsedOutputLog)>=sizeof(arq_log)) {
257 debuga(_("File name too long: %s/sarg_temp.log\n"),ParsedOutputLog);
258 exit(EXIT_FAILURE);
259 }
260 if((fp_log=MY_FOPEN(arq_log,"w"))==NULL) {
261 debuga(_("(log) Cannot open log file: %s - %s\n"),arq_log,strerror(errno));
262 exit(EXIT_FAILURE);
263 }
264 fputs("*** SARG Log ***\n",fp_log);
265 }
266
267 recs2++;
268 if( ShowReadStatistics && !from_stdin && !from_pipe && --OutputNonZero<=0) {
269 double perc = recs2 * 100. / recs1 ;
270 printf(_("SARG: Records in file: %lu, reading: %3.2lf%%"),recs2,perc);
271 putchar('\r');
272 fflush (stdout);
273 OutputNonZero = REPORT_EVERY_X_LINES ;
274 }
275 if(blen < 58) continue;
276 if(strstr(linebuf,"HTTP/0.0") != 0) continue;
277 if(strstr(linebuf,"logfile turned over") != 0) continue;
278 if(linebuf[0] == ' ') continue;
279
280 // exclude_string
281 if(ExcludeString[0] != '\0') {
282 bool exstring=false;
283 getword_start(&gwarea,ExcludeString);
284 while(strchr(gwarea.current,':') != 0) {
285 if (getword_multisep(val1,sizeof(val1),&gwarea,':')<0) {
286 debuga(_("Maybe you have a broken record or garbage in your exclusion string\n"));
287 exit(EXIT_FAILURE);
288 }
289 if((str=(char *) strstr(linebuf,val1)) != (char *) NULL ) {
290 exstring=true;
291 break;
292 }
293 }
294 if(!exstring && (str=(char *) strstr(linebuf,gwarea.current)) != (char *) NULL )
295 exstring=true;
296 if(exstring) continue;
297 }
298
299 totregsl++;
300 if(debugm)
301 printf("BUF=%s\n",linebuf);
302
6debcf3d 303 memset(&log_entry,0,sizeof(log_entry));
27d1fa35
FM
304 if (ilf==ILF_Squid || ilf==ILF_Common || ilf==ILF_Unknown) {
305 getword_start(&gwarea,linebuf);
306 if (getword(data,sizeof(data),&gwarea,' ')<0) {
307 debuga(_("Maybe you have a broken time in your access.log file\n"));
308 exit(EXIT_FAILURE);
309 }
310 if((str=(char *) strchr(data, '.')) != (char *) NULL && (str=(char *) strchr(str+1, '.')) != (char *) NULL ) {
311 strcpy(ip,data);
6debcf3d 312 log_entry.Ip=ip;
27d1fa35
FM
313 if(squid24) {
314 if (getword(user,sizeof(user),&gwarea,' ')<0 || getword_skip(255,&gwarea,' ')<0) {
315 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
316 exit(EXIT_FAILURE);
317 }
318 } else {
319 if (getword_skip(255,&gwarea,' ')<0 || getword(user,sizeof(user),&gwarea,' ')<0) {
320 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
321 exit(EXIT_FAILURE);
322 }
323 }
525f6fa7 324 log_entry.User=user;
27d1fa35
FM
325 if (getword(data,sizeof(data),&gwarea,']')<0 || getword_skip(MAXLEN,&gwarea,'"')<0 ||
326 getword(fun,sizeof(fun),&gwarea,' ')<0) {
327 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
328 exit(EXIT_FAILURE);
329 }
330 if (getword_ptr(linebuf,&full_url,&gwarea,' ')<0) {
331 debuga(_("Maybe you have a broken url in your %s file\n"),arq);
332 exit(EXIT_FAILURE);
333 }
334 if (getword_skip(MAXLEN,&gwarea,' ')<0) {
335 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
336 exit(EXIT_FAILURE);
337 }
338 if (getword(code2,sizeof(code2),&gwarea,' ')<0) {
339 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
340 exit(EXIT_FAILURE);
341 }
2c4bc22b 342 if (getword_atoll(&log_entry.DataSize,&gwarea,' ')<0) {
27d1fa35
FM
343 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
344 exit(EXIT_FAILURE);
345 }
346 if((str=(char *) strchr(gwarea.current, ' ')) != (char *) NULL ) {
347 if (getword(code,sizeof(code),&gwarea,' ')<0) {
348 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
349 exit(EXIT_FAILURE);
350 }
351 } else {
352 if (getword(code,sizeof(code),&gwarea,'\0')<0) {
353 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
354 exit(EXIT_FAILURE);
355 }
356 }
357
358 if ((str = strchr(code, ':')) != NULL)
359 *str = '/';
360
27d1fa35
FM
361 ilf=ILF_Common;
362 ilf_count[ilf]++;
363
364 getword_start(&gwarea,data+1);
365 if (getword_multisep(data,sizeof(data),&gwarea,':')<0){
366 debuga(_("Maybe you have a broken date in your %s file\n"),arq);
367 exit(EXIT_FAILURE);
368 }
369 if (getword_multisep(hora,sizeof(hora),&gwarea,' ')<0){
370 debuga(_("Maybe you have a broken date in your %s file\n"),arq);
371 exit(EXIT_FAILURE);
372 }
373 getword_start(&gwarea,data);
374 if (getword_atoll(&iday,&gwarea,'/')<0){
375 debuga(_("Maybe you have a broken date in your %s file\n"),arq);
376 exit(EXIT_FAILURE);
377 }
378 if (getword(mes,sizeof(mes),&gwarea,'/')<0){
379 debuga(_("Maybe you have a broken date in your %s file\n"),arq);
380 exit(EXIT_FAILURE);
381 }
382 if (getword_atoll(&iyear,&gwarea,'/')<0){
383 debuga(_("Maybe you have a broken date in your %s file\n"),arq);
384 exit(EXIT_FAILURE);
385 }
386
387 imonth=month2num(mes)+1;
388 idata=builddia(iday,imonth,iyear);
389 computedate(iyear,imonth,iday,&tt);
390 if (sscanf(hora,"%d:%d:%d",&tt.tm_hour,&tt.tm_min,&tt.tm_sec)!=3 || tt.tm_hour<0 || tt.tm_hour>=24 ||
391 tt.tm_min<0 || tt.tm_min>=60 || tt.tm_sec<0 || tt.tm_sec>=60) {
392 debuga(_("Invalid time found in %s\n"),arq);
393 exit(EXIT_FAILURE);
394 }
c5d6ef4b 395 log_entry.EntryTime=&tt;
27d1fa35
FM
396 }
397
398 if(ilf==ILF_Unknown || ilf==ILF_Squid) {
399 if (getword(elap,sizeof(elap),&gwarea,' ')<0) {
400 debuga(_("Maybe you have a broken elapsed time in your %s file\n"),arq);
401 exit(EXIT_FAILURE);
402 }
403 while(strcmp(elap,"") == 0 && gwarea.current[0] != '\0')
404 if (getword(elap,sizeof(elap),&gwarea,' ')<0) {
405 debuga(_("Maybe you have a broken elapsed time in your %s file\n"),arq);
406 exit(EXIT_FAILURE);
407 }
408 if(strlen(elap) < 1) continue;
2c4bc22b 409 log_entry.ElapsedTime=atol(elap);
27d1fa35
FM
410 if (getword(ip,sizeof(ip),&gwarea,' ')<0){
411 debuga(_("Maybe you have a broken client IP address in your %s file\n"),arq);
412 exit(EXIT_FAILURE);
413 }
6debcf3d 414 log_entry.Ip=ip;
27d1fa35
FM
415 if (getword(code,sizeof(code),&gwarea,' ')<0){
416 debuga(_("Maybe you have a broken result code in your %s file\n"),arq);
417 exit(EXIT_FAILURE);
418 }
2c4bc22b 419 if (getword_atoll(&log_entry.DataSize,&gwarea,' ')<0){
27d1fa35
FM
420 debuga(_("Maybe you have a broken amount of data in your %s file\n"),arq);
421 exit(EXIT_FAILURE);
422 }
423 if (getword(fun,sizeof(fun),&gwarea,' ')<0){
424 debuga(_("Maybe you have a broken request method in your %s file\n"),arq);
425 exit(EXIT_FAILURE);
426 }
427 if (getword_ptr(linebuf,&full_url,&gwarea,' ')<0){
428 debuga(_("Maybe you have a broken url in your %s file\n"),arq);
429 exit(EXIT_FAILURE);
430 }
431 if (getword(user,sizeof(user),&gwarea,' ')<0){
432 debuga(_("Maybe you have a broken user ID in your %s file\n"),arq);
433 exit(EXIT_FAILURE);
434 }
525f6fa7 435 log_entry.User=user;
27d1fa35
FM
436 ilf=ILF_Squid;
437 ilf_count[ilf]++;
438
439 tnum=atoi(data);
c5d6ef4b
FM
440 log_entry.EntryTime=localtime(&tnum);
441 if (log_entry.EntryTime == NULL) {
27d1fa35
FM
442 debuga(_("Cannot convert the timestamp from the squid log file\n"));
443 exit(EXIT_FAILURE);
444 }
445
c5d6ef4b 446 strftime(tbuf2, sizeof(tbuf2), "%H%M", log_entry.EntryTime);
27d1fa35 447
c5d6ef4b 448 idata=(log_entry.EntryTime->tm_year+1900)*10000+(log_entry.EntryTime->tm_mon+1)*100+log_entry.EntryTime->tm_mday;
27d1fa35
FM
449 }
450 }
451 if (ilf==ILF_Sarg) {
452 getword_start(&gwarea,linebuf);
453 if (getword(data,sizeof(data),&gwarea,'\t')<0){
454 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
455 exit(EXIT_FAILURE);
456 }
457 if (getword(hora,sizeof(hora),&gwarea,'\t')<0) {
458 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
459 exit(EXIT_FAILURE);
460 }
461 if (getword(user,sizeof(user),&gwarea,'\t')<0) {
462 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
463 exit(EXIT_FAILURE);
464 }
525f6fa7 465 log_entry.User=user;
27d1fa35
FM
466 if (getword(ip,sizeof(ip),&gwarea,'\t')<0) {
467 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
468 exit(EXIT_FAILURE);
469 }
6debcf3d 470 log_entry.Ip=ip;
27d1fa35
FM
471 if (getword_ptr(linebuf,&full_url,&gwarea,'\t')<0){
472 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
473 exit(EXIT_FAILURE);
474 }
2c4bc22b 475 if (getword_atoll(&log_entry.DataSize,&gwarea,'\t')<0){
27d1fa35
FM
476 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
477 exit(EXIT_FAILURE);
478 }
479 if (getword(code,sizeof(code),&gwarea,'\t')<0){
480 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
481 exit(EXIT_FAILURE);
482 }
2c4bc22b 483 if (getword_atol(&log_entry.ElapsedTime,&gwarea,'\t')<0){
27d1fa35
FM
484 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
485 exit(EXIT_FAILURE);
486 }
487 if (getword(smartfilter,sizeof(smartfilter),&gwarea,'\0')<0){
488 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
489 exit(EXIT_FAILURE);
490 }
491 getword_start(&gwarea,data);
492 if (getword_atoll(&iday,&gwarea,'/')<0 || iday<1 || iday>31){
493 debuga(_("Maybe you have a broken date in your %s file\n"),arq);
494 exit(EXIT_FAILURE);
495 }
496 if (getword_atoll(&imonth,&gwarea,'/')<0 || imonth<1 || imonth>12){
497 debuga(_("Maybe you have a broken date in your %s file\n"),arq);
498 exit(EXIT_FAILURE);
499 }
500 if (getword_atoll(&iyear,&gwarea,'\0')<0){
501 debuga(_("Maybe you have a broken date in your %s file\n"),arq);
502 exit(EXIT_FAILURE);
503 }
504 idata=builddia(iday,imonth,iyear);
505 computedate(iyear,imonth,iday,&tt);
506 if (sscanf(hora,"%d:%d:%d",&tt.tm_hour,&tt.tm_min,&tt.tm_sec)!=3 || tt.tm_hour<0 || tt.tm_hour>=24 ||
507 tt.tm_min<0 || tt.tm_min>=60 || tt.tm_sec<0 || tt.tm_sec>=60) {
508 debuga(_("Invalid time found in %s\n"),arq);
509 exit(EXIT_FAILURE);
510 }
c5d6ef4b 511 log_entry.EntryTime=&tt;
27d1fa35
FM
512 }
513 if (ilf==ILF_Isa) {
514 if (linebuf[0] == '#') {
515 int ncols,cols[ISACOL_Last];
516
517 fixendofline(linebuf);
518 getword_start(&gwarea,linebuf);
519 // remove the #Fields: column at the beginning of the line
520 if (getword_skip(1000,&gwarea,' ')<0){
521 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
522 exit(EXIT_FAILURE);
523 }
524 for (ncols=0 ; ncols<ISACOL_Last ; ncols++) cols[ncols]=-1;
525 ncols=0;
526 while(gwarea.current[0] != '\0') {
527 if (getword(val1,sizeof(val1),&gwarea,'\t')<0){
528 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
529 exit(EXIT_FAILURE);
530 }
531 if(strcmp(val1,"c-ip") == 0) cols[ISACOL_Ip]=ncols;
532 if(strcmp(val1,"cs-username") == 0) cols[ISACOL_UserName]=ncols;
533 if(strcmp(val1,"date") == 0) cols[ISACOL_Date]=ncols;
534 if(strcmp(val1,"time") == 0) cols[ISACOL_Time]=ncols;
535 if(strcmp(val1,"time-taken") == 0) cols[ISACOL_TimeTaken]=ncols;
536 if(strcmp(val1,"sc-bytes") == 0) cols[ISACOL_Bytes]=ncols;
537 if(strcmp(val1,"cs-uri") == 0) cols[ISACOL_Uri]=ncols;
538 if(strcmp(val1,"sc-status") == 0) cols[ISACOL_Status]=ncols;
539 ncols++;
540 }
541 if (cols[ISACOL_Ip]>=0) {
542 isa_ncols=ncols;
543 for (ncols=0 ; ncols<ISACOL_Last ; ncols++)
544 isa_cols[ncols]=cols[ncols];
545 }
546 continue;
547 }
548 if (!isa_ncols) continue;
549 getword_start(&gwarea,linebuf);
550 for (x=0 ; x<isa_ncols ; x++) {
551 if (getword_ptr(linebuf,&str,&gwarea,'\t')<0) {
552 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
553 exit(EXIT_FAILURE);
554 }
555 if (x==isa_cols[ISACOL_Ip]) {
556 if (strlen(str)>=sizeof(ip)) {
557 debuga(_("Maybe you have a broken IP in your %s file\n"),arq);
558 exit(EXIT_FAILURE);
559 }
560 strcpy(ip,str);
6debcf3d 561 log_entry.Ip=ip;
27d1fa35
FM
562 } else if (x==isa_cols[ISACOL_UserName]) {
563 if (strlen(str)>=sizeof(user)) {
564 debuga(_("Maybe you have a broken user ID in your %s file\n"),arq);
565 exit(EXIT_FAILURE);
566 }
567 strcpy(user,str);
525f6fa7 568 log_entry.User=user;
27d1fa35
FM
569 } else if (x==isa_cols[ISACOL_Date]) {
570 if (strlen(str)>=sizeof(data)) {
571 debuga(_("Maybe you have a broken date in your %s file\n"),arq);
572 exit(EXIT_FAILURE);
573 }
574 strcpy(data,str);
575 } else if (x==isa_cols[ISACOL_Time]) {
576 if (strlen(str)>=sizeof(hora)) {
577 debuga(_("Maybe you have a broken time in your %s file\n"),arq);
578 exit(EXIT_FAILURE);
579 }
580 strcpy(hora,str);
581 } else if (x==isa_cols[ISACOL_TimeTaken]) {
2c4bc22b 582 log_entry.ElapsedTime=atol(str);
27d1fa35 583 } else if (x==isa_cols[ISACOL_Bytes]) {
2c4bc22b 584 log_entry.DataSize=atoll(str);
27d1fa35
FM
585 } else if (x==isa_cols[ISACOL_Uri]) {
586 full_url=str;
587 } else if (x==isa_cols[ISACOL_Status]) {
588 if (strlen(str)>=sizeof(code)) {
589 debuga(_("Maybe you have a broken access code in your %s file\n"),arq);
590 exit(EXIT_FAILURE);
591 }
592 strcpy(code,str);
593 }
594 }
595
596 if(strcmp(code,"401") == 0 || strcmp(code,"403") == 0 || strcmp(code,"407") == 0) {
597 sprintf(val1,"DENIED/%s",code);
598 strcpy(code,val1);
599 }
600 getword_start(&gwarea,data);
601 if (getword_atoll(&iyear,&gwarea,'-')<0){
602 debuga(_("Maybe you have a broken year in your %s file\n"),arq);
603 exit(EXIT_FAILURE);
604 }
605 if (getword_atoll(&imonth,&gwarea,'-')<0){
606 debuga(_("Maybe you have a broken month in your %s file\n"),arq);
607 exit(EXIT_FAILURE);
608 }
609 if (getword_atoll(&iday,&gwarea,'\0')<0){
610 debuga(_("Maybe you have a broken day in your %s file\n"),arq);
611 exit(EXIT_FAILURE);
612 }
613
614 idata=builddia(iday,imonth,iyear);
615 computedate(iyear,imonth,iday,&tt);
616 if (isa_cols[ISACOL_Time]>=0) {
617 if (sscanf(hora,"%d:%d:%d",&tt.tm_hour,&tt.tm_min,&tt.tm_sec)!=3 || tt.tm_hour<0 || tt.tm_hour>=24 ||
618 tt.tm_min<0 || tt.tm_min>=60 || tt.tm_sec<0 || tt.tm_sec>=60) {
619 debuga(_("Invalid time found in %s\n"),arq);
620 exit(EXIT_FAILURE);
621 }
622 }
c5d6ef4b 623 log_entry.EntryTime=&tt;
27d1fa35 624 }
c5d6ef4b 625 if (log_entry.EntryTime==NULL) {
6debcf3d
FM
626 debuga(_("Unknown input log file format: no time\n"));
627 break;
628 }
629 if (log_entry.Ip==NULL) {
630 debuga(_("Unknown input log file format: no IP addresses\n"));
27d1fa35
FM
631 break;
632 }
525f6fa7
FM
633 if (log_entry.User==NULL) {
634 debuga(_("Unknown input log file format: no user\n"));
635 break;
636 }
27d1fa35 637
27d1fa35
FM
638 if(debugm)
639 printf("DATE=%s IDATA=%d DFROM=%d DUNTIL=%d\n",Filter->DateRange,idata,dfrom,duntil);
640
641 if(Filter->DateRange[0] != '\0'){
642 if(idata < dfrom || idata > duntil) continue;
643 }
644
645 // Record only hours usage which is required
c5d6ef4b
FM
646 if( bsearch( &( log_entry.EntryTime->tm_wday ), weekdays.list, weekdays.len, sizeof( int ), compar ) == NULL )
647 continue;
27d1fa35 648
c5d6ef4b
FM
649 if( bsearch( &( log_entry.EntryTime->tm_hour ), hours.list, hours.len, sizeof( int ), compar ) == NULL )
650 continue;
27d1fa35
FM
651
652
525f6fa7
FM
653 if(strlen(log_entry.User) > MAX_USER_LEN) {
654 if (debugm) printf(_("User ID too long: %s\n"),log_entry.User);
27d1fa35
FM
655 totregsx++;
656 continue;
657 }
658
659 // include_users
660 if(IncludeUsers[0] != '\0') {
525f6fa7 661 snprintf(val1,sizeof(val1),":%s:",log_entry.User);
27d1fa35
FM
662 if((str=(char *) strstr(IncludeUsers,val1)) == (char *) NULL )
663 continue;
664 }
665
666 if(vercode(code)) {
667 if (debugm) printf(_("Excluded code: %s\n"),code);
668 totregsx++;
669 continue;
670 }
671
525f6fa7 672 if(testvaliduserchar(log_entry.User))
27d1fa35
FM
673 continue;
674
27d1fa35
FM
675 // replace any tab by a single space
676 for (str=full_url ; *str ; str++)
677 if (*str=='\t') *str=' ';
678 for (str=code ; *str ; str++)
679 if (*str=='\t') *str=' ';
680
681 if(ilf!=ILF_Sarg) {
682 /*
683 The full URL is not saved in sarg log. There is no point in testing the URL to detect
684 a downloaded file.
685 */
686 download_flag=is_download_suffix(full_url);
687 if (download_flag) {
688 safe_strcpy(download_url,full_url,sizeof(download_url));
689 download_count++;
690 }
691 } else
692 download_flag=false;
693
694 url=process_url(full_url,LongUrl);
695 if (!url || url[0] == '\0') continue;
696
697 if(addr[0] != '\0'){
6debcf3d 698 if(strcmp(addr,log_entry.Ip)!=0) continue;
27d1fa35
FM
699 }
700 if(Filter->HostFilter) {
701 if(!vhexclude(url)) {
702 if (debugm) printf(_("Excluded site: %s\n"),url);
703 totregsx++;
704 continue;
705 }
706 }
707
708 if(Filter->StartTime >= 0 && Filter->EndTime >= 0) {
c5d6ef4b 709 hmr=log_entry.EntryTime->tm_hour*100+log_entry.EntryTime->tm_min;
27d1fa35
FM
710 if(hmr < Filter->StartTime || hmr > Filter->EndTime) continue;
711 }
712
713 if(site[0] != '\0'){
714 if(strstr(url,site)==0) continue;
715 }
716
717 if(UserIp) {
525f6fa7 718 log_entry.User=log_entry.Ip;
27d1fa35
FM
719 id_is_ip=true;
720 } else {
721 id_is_ip=false;
525f6fa7 722 if(strcmp(log_entry.User,"-") == 0 || strcmp(log_entry.User," ") == 0 || strcmp(log_entry.User,"") == 0) {
27d1fa35 723 if(RecordsWithoutUser == RECORDWITHOUTUSER_IP) {
525f6fa7 724 log_entry.User=log_entry.Ip;
27d1fa35
FM
725 id_is_ip=true;
726 }
727 if(RecordsWithoutUser == RECORDWITHOUTUSER_IGNORE)
728 continue;
729 if(RecordsWithoutUser == RECORDWITHOUTUSER_EVERYBODY)
525f6fa7 730 log_entry.User="everybody";
27d1fa35 731 } else {
525f6fa7 732 strlow(log_entry.User);
27d1fa35 733 if(NtlmUserFormat == NTLMUSERFORMAT_USER) {
525f6fa7
FM
734 if((str = strchr(log_entry.User,'_')) != 0) {
735 log_entry.User=str+1;
27d1fa35 736 }
525f6fa7
FM
737 if((str = strchr(log_entry.User,'+')) != 0) {
738 log_entry.User=str+1;
27d1fa35
FM
739 }
740 }
741 }
742 }
743
744 if(us[0] != '\0'){
525f6fa7 745 if(strcmp(log_entry.User,us)!=0) continue;
27d1fa35
FM
746 }
747
748 if(Filter->SysUsers) {
525f6fa7 749 snprintf(wuser,sizeof(wuser),":%s:",log_entry.User);
27d1fa35
FM
750 if(strstr(userfile, wuser) == 0)
751 continue;
752 }
753
754 if(Filter->UserFilter) {
525f6fa7
FM
755 if(!vuexclude(log_entry.User)) {
756 if (debugm) printf(_("Excluded user: %s\n"),log_entry.User);
27d1fa35
FM
757 totregsx++;
758 continue;
759 }
760 }
761
525f6fa7
FM
762 if(strcmp(log_entry.User,"-") ==0 || strcmp(log_entry.User," ") ==0 ||
763 strcmp(log_entry.User,"") ==0 || strcmp(log_entry.User,":") ==0)
27d1fa35
FM
764 continue;
765
2c4bc22b 766 if (log_entry.DataSize<0) log_entry.DataSize=0;
27d1fa35 767
2c4bc22b
FM
768 if (log_entry.ElapsedTime<0) log_entry.ElapsedTime=0;
769 if (Filter->max_elapsed>0 && log_entry.ElapsedTime>Filter->max_elapsed) {
770 log_entry.ElapsedTime=0;
27d1fa35
FM
771 }
772
773 if((str=(char *) strstr(linebuf, "[SmartFilter:")) != (char *) NULL ) {
774 fixendofline(str);
775 snprintf(smartfilter,sizeof(smartfilter),"\"%s\"",str+1);
776 } else strcpy(smartfilter,"\"\"");
777
778 nopen=0;
779 prev_ufile=NULL;
525f6fa7 780 for (ufile=first_user_file ; ufile && strcmp(log_entry.User,ufile->user->id)!=0 ; ufile=ufile->next) {
27d1fa35
FM
781 prev_ufile=ufile;
782 if (ufile->file) nopen++;
783 }
784 if (!ufile) {
785 ufile=malloc(sizeof(*ufile));
786 if (!ufile) {
525f6fa7 787 debuga(_("Not enough memory to store the user %s\n"),log_entry.User);
27d1fa35
FM
788 exit(EXIT_FAILURE);
789 }
790 memset(ufile,0,sizeof(*ufile));
791 ufile->next=first_user_file;
792 first_user_file=ufile;
525f6fa7 793 uinfo=userinfo_create(log_entry.User);
27d1fa35
FM
794 ufile->user=uinfo;
795 uinfo->id_is_ip=id_is_ip;
796 } else {
797 if (prev_ufile) {
798 prev_ufile->next=ufile->next;
799 ufile->next=first_user_file;
800 first_user_file=ufile;
801 }
802 }
803 #ifdef ENABLE_DOUBLE_CHECK_DATA
2c4bc22b
FM
804 ufile->user->nbytes+=log_entry.DataSize;
805 ufile->user->elap+=log_entry.ElapsedTime;
27d1fa35
FM
806 #endif
807
808 if (ufile->file==NULL) {
809 if (nopen>=maxopenfiles) {
810 x=0;
811 for (ufile1=first_user_file ; ufile1 ; ufile1=ufile1->next) {
812 if (ufile1->file!=NULL) {
813 if (x>=maxopenfiles) {
814 if (fclose(ufile1->file)==EOF) {
815 debuga(_("Failed to close the log file of user %s - %s\n"),ufile1->user->id,strerror(errno));
816 exit(EXIT_FAILURE);
817 }
818 ufile1->file=NULL;
819 }
820 x++;
821 }
822 }
823 }
824 if (snprintf (tmp3, sizeof(tmp3), "%s/%s.unsort", tmp, ufile->user->filename)>=sizeof(tmp3)) {
825 debuga(_("Temporary user file name too long: %s/%s.unsort\n"), tmp, ufile->user->filename);
826 exit(EXIT_FAILURE);
827 }
828 if ((ufile->file = MY_FOPEN (tmp3, "a")) == NULL) {
829 debuga(_("(log) Cannot open temporary file: %s - %s\n"), tmp3, strerror(errno));
830 exit (1);
831 }
832 }
833
c5d6ef4b
FM
834 strftime(dia, sizeof(dia), "%d/%m/%Y", log_entry.EntryTime);
835 strftime(hora,sizeof(hora),"%H:%M:%S",log_entry.EntryTime);
836
2c4bc22b 837 if (fprintf(ufile->file, "%s\t%s\t%s\t%s\t%"PRIu64"\t%s\t%ld\t%s\n",dia,hora,log_entry.Ip,url,(uint64_t)log_entry.DataSize,code,log_entry.ElapsedTime,smartfilter)<=0) {
525f6fa7 838 debuga(_("Write error in the log file of user %s\n"),log_entry.User);
27d1fa35
FM
839 exit(EXIT_FAILURE);
840 }
841
842 if(fp_log && ilf!=ILF_Sarg)
2c4bc22b 843 fprintf(fp_log, "%s\t%s\t%s\t%s\t%s\t%"PRIu64"\t%s\t%ld\t%s\n",dia,hora,log_entry.User,log_entry.Ip,url,(uint64_t)log_entry.DataSize,code,log_entry.ElapsedTime,smartfilter);
27d1fa35
FM
844
845 totregsg++;
846
847 if(!dataonly && download_flag && strstr(code,"DENIED") == 0) {
848 ndownload = 1;
849
850 if ( ! fp_Download_Unsort ) {
851 if ((fp_Download_Unsort = MY_FOPEN ( sz_Download_Unsort, "a")) == NULL) {
852 debuga(_("(log) Cannot open temporary file: %s - %s\n"),sz_Download_Unsort, strerror(errno));
853 exit (1);
854 }
855 }
525f6fa7 856 fprintf(fp_Download_Unsort,"%s\t%s\t%s\t%s\t%s\n",dia,hora,log_entry.User,log_entry.Ip,download_url);
27d1fa35
FM
857 }
858
60b48ae5 859 if(fp_denied && strstr(code,"DENIED/403") != 0) {
525f6fa7 860 fprintf(fp_denied, "%s\t%s\t%s\t%s\t%s\n",dia,hora,log_entry.User,log_entry.Ip,full_url);
60b48ae5 861 denied_count++;
27d1fa35
FM
862 }
863 if((ReportType & REPORT_TYPE_AUTH_FAILURES) != 0) {
864 if(fp_authfail && (strstr(code,"DENIED/401") != 0 || strstr(code,"DENIED/407") != 0)) {
525f6fa7 865 fprintf(fp_authfail, "%s\t%s\t%s\t%s\t%s\n",dia,hora,log_entry.User,log_entry.Ip,full_url);
27d1fa35
FM
866 authfail_count++;
867 }
868 }
869
870 if (ilf!=ILF_Sarg) {
871 if(!totper || idata<mindate){
872 mindate=idata;
c5d6ef4b 873 memcpy(&period.start,log_entry.EntryTime,sizeof(*log_entry.EntryTime));
27d1fa35
FM
874 strcpy(start_hour,tbuf2);
875 }
876 if (!totper || idata>maxdate) {
877 maxdate=idata;
c5d6ef4b 878 memcpy(&period.end,log_entry.EntryTime,sizeof(*log_entry.EntryTime));
27d1fa35
FM
879 }
880 totper=true;
881 }
882
883 if(debugm){
6debcf3d 884 printf("IP=\t%s\n",log_entry.Ip);
525f6fa7 885 printf("USER=\t%s\n",log_entry.User);
2c4bc22b 886 printf("ELAP=\t%ld\n",log_entry.ElapsedTime);
27d1fa35
FM
887 printf("DATE=\t%s\n",dia);
888 printf("TIME=\t%s\n",hora);
889 printf("FUNC=\t%s\n",fun);
890 printf("URL=\t%s\n",url);
891 printf("CODE=\t%s\n",code);
2c4bc22b 892 printf("LEN=\t%"PRIu64"\n",(uint64_t)log_entry.DataSize);
27d1fa35
FM
893 }
894 }
895
896 if (!from_stdin) {
897 if (from_pipe)
898 pclose(fp_in);
899 else {
900 fclose(fp_in);
901 if( ShowReadStatistics )
902 printf(_("SARG: Records in file: %lu, reading: %3.2f%%\n"),recs1, (float) 100 );
903 }
904 }
905 }
906 longline_destroy(&line);
907
908 if(fp_log != NULL) {
909 char end_hour[128];
910 char val2[40];
911 char val4[255];//val4 must not be bigger than arq_log without fixing the strcpy below
912
913 fclose(fp_log);
914 safe_strcpy(end_hour,tbuf2,sizeof(end_hour));
915 strftime(val2,sizeof(val2),"%d%m%Y",&period.start);
916 strftime(val1,sizeof(val1),"%d%m%Y",&period.end);
917 if (snprintf(val4,sizeof(val4),"%s/sarg-%s_%s-%s_%s.log",ParsedOutputLog,val2,start_hour,val1,end_hour)>=sizeof(val4)) {
918 debuga(_("File name too long: %s/sarg-%s_%s-%s_%s.log\n"),ParsedOutputLog,val2,start_hour,val1,end_hour);
919 exit(EXIT_FAILURE);
920 }
921 if (rename(arq_log,val4)) {
922 debuga(_("failed to rename %s to %s - %s\n"),arq_log,val4,strerror(errno));
923 } else {
924 strcpy(arq_log,val4);
925
926 if(strcmp(ParsedOutputLogCompress,"nocompress") != 0 && ParsedOutputLogCompress[0] != '\0') {
927 /*
928 No double quotes around ParsedOutputLogCompress because it may contain command line options. If double quotes are
929 necessary around the command name, put them in the configuration file.
930 */
931 if (snprintf(val1,sizeof(val1),"%s \"%s\"",ParsedOutputLogCompress,arq_log)>=sizeof(val1)) {
932 debuga(_("Command too long: %s \"%s\"\n"),ParsedOutputLogCompress,arq_log);
933 exit(EXIT_FAILURE);
934 }
935 cstatus=system(val1);
936 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
937 debuga(_("command return status %d\n"),WEXITSTATUS(cstatus));
938 debuga(_("command: %s\n"),val1);
939 exit(EXIT_FAILURE);
940 }
941 }
942 }
943 if(debug)
944 debuga(_("Sarg parsed log saved as %s\n"),arq_log);
945 }
946
947 if (fp_denied) fclose(fp_denied);
948 if (fp_authfail) fclose(fp_authfail);
949 if (fp_Download_Unsort) fclose (fp_Download_Unsort);
950
951 for (ufile=first_user_file ; ufile ; ufile=ufile1) {
952 ufile1=ufile->next;
953 if (ufile->file!=NULL) fclose(ufile->file);
954 free(ufile);
955 }
956
957 if (debug) {
958 int totalcount=0;
959
960 debuga(_(" Records read: %ld, written: %ld, excluded: %ld\n"),totregsl,totregsg,totregsx);
961
962 for (ilf=0 ; ilf<ILF_Last ; ilf++) totalcount+=ilf_count[ilf];
963
964 if(ilf_count[ILF_Common]>0 && ilf_count[ILF_Squid]>0)
965 debuga(_("Log with mixed records format (squid and common log)\n"));
966
967 if(ilf_count[ILF_Common]>0 && ilf_count[ILF_Squid]==0)
968 debuga(_("Common log format\n"));
969
970 if(ilf_count[ILF_Common]==0 && ilf_count[ILF_Squid]>0)
971 debuga(_("Squid log format\n"));
972
973 if(ilf_count[ILF_Sarg]>0)
974 debuga(_("Sarg log format\n"));
975
976 if(totalcount==0 && totregsg)
977 debuga(_("Log with invalid format\n"));
978 }
979
980 if(debugz){
981 debugaz(_("date=%s\n"),dia);
982 debugaz(_("period=%s\n"),period.text);
983 }
984
27d1fa35
FM
985 return((totregsg!=0) ? 1 : 0);
986}