]> git.ipfire.org Git - thirdparty/sarg.git/blame - readlog.c
Update the Russian translation
[thirdparty/sarg.git] / readlog.c
CommitLineData
27d1fa35
FM
1/*
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
110ce984 3 * 1998, 2015
27d1fa35
FM
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"
6068ae56 30#include "include/filelist.h"
27d1fa35
FM
31
32#define REPORT_EVERY_X_LINES 5000
33#define MAX_OPEN_USER_FILES 10
34
35struct userfilestruct
36{
37 struct userfilestruct *next;
38 struct userinfostruct *user;
39 FILE *file;
40};
41
7c8c06c5
FM
42enum ExcludeReasonEnum
43{
44 //! User name too long.
45 ER_UserNameTooLong,
46 //! Squid logged an incomplete query received from the client.
47 ER_IncompleteQuery,
48 //! Log file turned over.
49 ER_LogfileTurnedOver,
7c8c06c5
FM
50 //! Excluded by exclude_string from sarg.conf.
51 ER_ExcludeString,
52 //! Unknown input log file format.
53 ER_UnknownFormat,
54 //! Line to be ignored from the input log file.
55 ER_FormatData,
56 //! Entry not withing the requested date range.
57 ER_OutOfDateRange,
58 //! Ignored week day.
59 ER_OutOfWDayRange,
60 //! Ignored hour.
61 ER_OutOfHourRange,
62 //! User is not in the include_users list.
63 ER_User,
64 //! HTTP code excluded by exclude_code file.
65 ER_HttpCode,
66 //! Invalid character found in user name.
67 ER_InvalidUserChar,
68 //! No URL in entry.
69 ER_NoUrl,
70 //! Not the IP address requested with -a.
71 ER_UntrackedIpAddr,
72 //! URL excluded by -c or exclude_hosts.
73 ER_Url,
74 //! Entry time outside of requested hour range.
75 ER_OutOfTimeRange,
76 //! Not the URL requested by -s.
77 ER_UntrackedUrl,
78 //! No user in entry.
79 ER_NoUser,
80 //! Not the user requested by -u.
81 ER_UntrackedUser,
82 //! System user.
83 ER_SysUser,
84 //! User ignored by exclude_users
85 ER_IgnoredUser,
86
87 ER_Last //!< last entry of the list
88};
89
27d1fa35
FM
90numlist weekdays = { { 0, 1, 2, 3, 4, 5, 6 }, 7 };
91numlist 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 };
36a0b94c
FM
92//! Domain suffix to strip from the user name.
93char StripUserSuffix[MAX_USER_LEN]="";
2c058416
FM
94//! Length of the suffix to strip from the user name.
95int StripSuffixLen=0;
27d1fa35 96
6068ae56 97extern FileListObject AccessLog;
27d1fa35 98
1c91da07
FM
99extern const struct ReadLogProcessStruct ReadSquidLog;
100extern const struct ReadLogProcessStruct ReadCommonLog;
101extern const struct ReadLogProcessStruct ReadSargLog;
102extern const struct ReadLogProcessStruct ReadExtLog;
103
104//! The list of the supported log formats.
105static const struct ReadLogProcessStruct const *LogFormats[]=
106{
107 &ReadSquidLog,
108 &ReadCommonLog,
109 &ReadSargLog,
110 &ReadExtLog
111};
112
944cf283
FM
113//! The path to the sarg log file.
114static char SargLogFile[4096]="";
115//! Handle to the sarg log file. NULL if not created.
116static FILE *fp_log=NULL;
117//! The number of records read from the input logs.
118static long int totregsl=0;
119//! The number of records kept.
120static long int totregsg=0;
121//! The number of records excluded.
122static long int totregsx=0;
123//! The beginning of a linked list of user's file.
124static struct userfilestruct *first_user_file=NULL;
125//! Count the number of occurence of each input log format.
8e501bd6 126static unsigned long int format_count[sizeof(LogFormats)/sizeof(*LogFormats)];
944cf283
FM
127//! The minimum date found in the input logs.
128static int mindate=0;
129static int maxdate=0;
7c8c06c5
FM
130//! Count the number of excluded records.
131static unsigned long int excluded_count[ER_Last];
6a943fc1
FM
132//! Earliest date found in the log.
133static int EarliestDate=-1;
134//! The earliest date in time format.
135static struct tm EarliestDateTime;
136//! Latest date found in the log.
137static int LatestDate=-1;
138//! The latest date in time format.
139static struct tm LatestDateTime;
27d1fa35 140
800eafb8
FM
141/*!
142 * Read from standard input.
143 *
144 * \param Data The file object.
145 * \param Buffer The boffer to store the data read.
146 * \param Size How many bytes to read.
147 *
148 * \return The number of bytes read.
149 */
150static int Stdin_Read(void *Data,void *Buffer,int Size)
151{
152 return(fread(Buffer,1,Size,(FILE *)Data));
153}
154
155/*!
156 * Check if end of file is reached.
157 *
158 * \param Data The file object.
159 *
160 * \return \c True if end of file is reached.
161 */
162static int Stdin_Eof(void *Data)
163{
164 return(feof((FILE *)Data));
165}
166
167/*!
168 * Mimic a close of standard input but do nothing
169 *
170 * \param Data File to close.
171 *
172 * \return EOF on error.
173 */
174static int Stdin_Close(void *Data)
175{
176 return(0);
177}
178
179/*!
180 * Open a file object to read from standard input.
181 *
182 * \return The object to pass to other function in this module.
183 */
184static FileObject *Stdin_Open(void)
185{
186 FileObject *File;
187
188 FileObject_SetLastOpenError(NULL);
189 File=calloc(1,sizeof(*File));
190 if (!File)
191 {
192 FileObject_SetLastOpenError(_("Not enough memory"));
193 return(NULL);
194 }
195 File->Data=stdin;
196 File->Read=Stdin_Read;
197 File->Eof=Stdin_Eof;
198 File->Rewind=NULL;
199 File->Close=Stdin_Close;
200 return(File);
201}
202
8b4e9578
FM
203/*!
204 * Initialize the memory structure needed by LogLine_Parse() to parse
205 * a log line.
206 *
207 * \param log_line The structure to initialize.
208 */
209void LogLine_Init(struct LogLineStruct *log_line)
210{
211 log_line->current_format=NULL;
212 log_line->current_format_idx=-1;
213 log_line->file_name="";
214 log_line->successive_errors=0;
215 log_line->total_errors=0;
216}
217
218/*!
219 * Set the name of the log file being parsed.
220 *
221 * \param log_line Data structure to parse the log line.
222 * \param file_name The name of the log file being read.
223 */
224void LogLine_File(struct LogLineStruct *log_line,const char *file_name)
225{
226 log_line->file_name=file_name;
227}
228
229/*!
230 * Parse the next line from a log file.
231 *
232 * \param log_line A buffer to store the data about the current parsing.
233 * \param log_entry The variable to store the parsed data.
234 * \param linebuf The text line read from the log file.
235 *
236 * \return
237 */
238enum ReadLogReturnCodeEnum LogLine_Parse(struct LogLineStruct *log_line,struct ReadLogStruct *log_entry,char *linebuf)
239{
240 enum ReadLogReturnCodeEnum log_entry_status=RLRC_Unknown;
241 int x;
242
243 if (log_line->current_format)
244 {
245 memset(log_entry,0,sizeof(*log_entry));
246 log_entry_status=log_line->current_format->ReadEntry(linebuf,log_entry);
247 }
248
249 // find out what line format to use
250 if (log_entry_status==RLRC_Unknown)
251 {
252 for (x=0 ; x<(int)(sizeof(LogFormats)/sizeof(*LogFormats)) ; x++)
253 {
254 if (LogFormats[x]==log_line->current_format) continue;
255 memset(log_entry,0,sizeof(*log_entry));
256 log_entry_status=LogFormats[x]->ReadEntry(linebuf,log_entry);
257 if (log_entry_status!=RLRC_Unknown)
258 {
259 log_line->current_format=LogFormats[x];
260 log_line->current_format_idx=x;
261 if (debugz>=LogLevel_Process)
262 {
263 /* TRANSLATORS: The argument is the log format name as translated by you. */
af961877 264 debuga(__FILE__,__LINE__,_("Log format identified as \"%s\" for %s\n"),_(log_line->current_format->Name),log_line->file_name);
8b4e9578
FM
265 }
266 break;
267 }
268 }
269 if (x>=(int)(sizeof(LogFormats)/sizeof(*LogFormats)))
270 {
271 if (++log_line->successive_errors>NumLogSuccessiveErrors) {
af961877 272 debuga(__FILE__,__LINE__,ngettext("%d consecutive error found in the input log file %s\n",
8b4e9578
FM
273 "%d consecutive errors found in the input log file %s\n",log_line->successive_errors),log_line->successive_errors,log_line->file_name);
274 exit(EXIT_FAILURE);
275 }
276 if (NumLogTotalErrors>=0 && ++log_line->total_errors>NumLogTotalErrors) {
af961877 277 debuga(__FILE__,__LINE__,ngettext("%d error found in the input log file (last in %s)\n",
8b4e9578
FM
278 "%d errors found in the input log file (last in %s)\n",log_line->total_errors),log_line->total_errors,log_line->file_name);
279 exit(EXIT_FAILURE);
280 }
af961877 281 debuga(__FILE__,__LINE__,_("The following line read from %s could not be parsed and is ignored\n%s\n"),log_line->file_name,linebuf);
8b4e9578
FM
282 }
283 else
284 log_line->successive_errors=0;
285 }
286
287 if (log_line->current_format_idx<0 || log_line->current_format==NULL) {
af961877 288 debuga(__FILE__,__LINE__,_("Sarg failed to determine the format of the input log file %s\n"),log_line->file_name);
8b4e9578
FM
289 exit(EXIT_FAILURE);
290 }
291 if (log_entry_status==RLRC_InternalError) {
af961877 292 debuga(__FILE__,__LINE__,_("Internal error encountered while processing %s\nSee previous message to know the reason for that error.\n"),log_line->file_name);
8b4e9578
FM
293 exit(EXIT_FAILURE);
294 }
295 return(log_entry_status);
296}
297
944cf283
FM
298/*!
299Read a single log file.
27d1fa35 300
944cf283 301\param arq The log file name to read.
27d1fa35 302*/
944cf283 303static void ReadOneLogFile(struct ReadLogDataStruct *Filter,const char *arq)
27d1fa35 304{
27d1fa35
FM
305 longline line;
306 char *linebuf;
307 char *str;
27d1fa35 308 char hora[30];
c5d6ef4b 309 char dia[128]="";
944cf283 310 char tmp3[MAXLEN]="";
27d1fa35
FM
311 char download_url[MAXLEN];
312 char smartfilter[MAXLEN];
27d1fa35 313 const char *url;
27d1fa35
FM
314 int OutputNonZero = REPORT_EVERY_X_LINES ;
315 int idata=0;
27d1fa35
FM
316 int x;
317 int hmr;
318 int nopen;
319 int maxopenfiles=MAX_OPEN_USER_FILES;
27d1fa35
FM
320 unsigned long int recs1=0UL;
321 unsigned long int recs2=0UL;
800eafb8 322 FileObject *fp_in=NULL;
27d1fa35
FM
323 bool download_flag=false;
324 bool id_is_ip;
1c91da07 325 enum ReadLogReturnCodeEnum log_entry_status;
074a6d8f 326 enum UserProcessError PUser;
27d1fa35
FM
327 struct stat logstat;
328 struct getwordstruct gwarea;
27d1fa35
FM
329 struct userfilestruct *prev_ufile;
330 struct userinfostruct *uinfo;
27d1fa35
FM
331 struct userfilestruct *ufile;
332 struct userfilestruct *ufile1;
c5d6ef4b 333 struct ReadLogStruct log_entry;
8b4e9578 334 struct LogLineStruct log_line;
27d1fa35 335
8b4e9578
FM
336 LogLine_Init(&log_line);
337 LogLine_File(&log_line,arq);
944cf283
FM
338 for (x=0 ; x<sizeof(LogFormats)/sizeof(*LogFormats) ; x++)
339 if (LogFormats[x]->NewFile)
340 LogFormats[x]->NewFile(arq);
27d1fa35 341
944cf283 342 if (arq[0]=='-' && arq[1]=='\0') {
800eafb8 343 fp_in=Stdin_Open();
944cf283 344 if(debug)
af961877 345 debuga(__FILE__,__LINE__,_("Reading access log file: from stdin\n"));
944cf283
FM
346 } else {
347 if (Filter->DateRange[0]!='\0') {
348 if (stat(arq,&logstat)!=0) {
af961877 349 debuga(__FILE__,__LINE__,_("Cannot get the modification time of input log file %s (%s). Processing it anyway\n"),arq,strerror(errno));
944cf283
FM
350 } else {
351 struct tm *logtime=localtime(&logstat.st_mtime);
352 if ((logtime->tm_year+1900)*10000+(logtime->tm_mon+1)*100+logtime->tm_mday<dfrom) {
af961877 353 debuga(__FILE__,__LINE__,_("Ignoring old log file %s\n"),arq);
944cf283 354 return;
27d1fa35
FM
355 }
356 }
27d1fa35 357 }
800eafb8
FM
358 fp_in=decomp(arq);
359 if (fp_in==NULL) {
360 debuga(__FILE__,__LINE__,_("Cannot open input log file \"%s\": %s\n"),arq,FileObject_GetLastOpenError());
944cf283
FM
361 exit(EXIT_FAILURE);
362 }
800eafb8 363 if (debug) debuga(__FILE__,__LINE__,_("Reading access log file: %s\n"),arq);
944cf283 364 }
27d1fa35 365
944cf283 366 download_flag=false;
27d1fa35 367
944cf283
FM
368 recs1=0UL;
369 recs2=0UL;
2f4787e6 370
944cf283 371 // pre-read the file only if we have to show stats
800eafb8 372 if (ShowReadStatistics && ShowReadPercent && fp_in->Rewind) {
a1e4e370 373 int nread,i;
944cf283
FM
374 bool skipcr=false;
375 char tmp4[MAXLEN];
27d1fa35 376
800eafb8 377 while ((nread=FileObject_Read(fp_in,tmp4,sizeof(tmp4)))>0) {
944cf283
FM
378 for (i=0 ; i<nread ; i++)
379 if (skipcr) {
380 if (tmp4[i]!='\n' && tmp4[i]!='\r') {
381 skipcr=false;
27d1fa35 382 }
944cf283
FM
383 } else {
384 if (tmp4[i]=='\n' || tmp4[i]=='\r') {
385 skipcr=true;
386 recs1++;
387 }
388 }
27d1fa35 389 }
800eafb8 390 FileObject_Rewind(fp_in);
944cf283
FM
391 printf(_("SARG: Records in file: %lu, reading: %3.2f%%"),recs1,(float) 0);
392 putchar('\r');
393 fflush( stdout ) ;
394 }
27d1fa35 395
944cf283 396 if ((line=longline_create())==NULL) {
af961877 397 debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\"\n"),arq);
944cf283
FM
398 exit(EXIT_FAILURE);
399 }
27d1fa35 400
944cf283 401 while ((linebuf=longline_read(fp_in,line))!=NULL) {
944cf283 402 lines_read++;
27d1fa35 403
944cf283
FM
404 recs2++;
405 if (ShowReadStatistics && --OutputNonZero<=0) {
406 if (recs1>0) {
407 double perc = recs2 * 100. / recs1 ;
408 printf(_("SARG: Records in file: %lu, reading: %3.2lf%%"),recs2,perc);
409 } else {
410 printf(_("SARG: Records in file: %lu"),recs2);
27d1fa35 411 }
944cf283
FM
412 putchar('\r');
413 fflush (stdout);
414 OutputNonZero = REPORT_EVERY_X_LINES ;
415 }
0c87646f 416
944cf283
FM
417 /*
418 The following checks are retained here as I don't know to
419 what format they apply. They date back to pre 2.4 versions.
420 */
421 //if(blen < 58) continue; //this test conflict with the reading of the sarg log header line
7c8c06c5
FM
422 if(strstr(linebuf,"HTTP/0.0") != 0) {//recorded by squid when encountering an incomplete query
423 excluded_count[ER_IncompleteQuery]++;
424 continue;
425 }
426 if(strstr(linebuf,"logfile turned over") != 0) {//reported by newsyslog
427 excluded_count[ER_LogfileTurnedOver]++;
428 continue;
429 }
944cf283
FM
430
431 // exclude_string
432 if(ExcludeString[0] != '\0') {
433 bool exstring=false;
434 getword_start(&gwarea,ExcludeString);
435 while(strchr(gwarea.current,':') != 0) {
436 if (getword_multisep(val1,sizeof(val1),&gwarea,':')<0) {
af961877 437 debuga(__FILE__,__LINE__,_("Invalid record in exclusion string\n"));
944cf283 438 exit(EXIT_FAILURE);
27d1fa35 439 }
944cf283 440 if((str=(char *) strstr(linebuf,val1)) != (char *) NULL ) {
27d1fa35 441 exstring=true;
944cf283
FM
442 break;
443 }
27d1fa35 444 }
944cf283
FM
445 if(!exstring && (str=(char *) strstr(linebuf,gwarea.current)) != (char *) NULL )
446 exstring=true;
7c8c06c5
FM
447 if(exstring) {
448 excluded_count[ER_ExcludeString]++;
449 continue;
450 }
944cf283 451 }
27d1fa35 452
944cf283 453 totregsl++;
4246ae8d 454 if (debugz>=LogLevel_Data)
944cf283 455 printf("BUF=%s\n",linebuf);
27d1fa35 456
944cf283 457 // process the line
8b4e9578
FM
458 log_entry_status=LogLine_Parse(&log_line,&log_entry,linebuf);
459 if (log_entry_status==RLRC_Unknown)
460 {
461 excluded_count[ER_UnknownFormat]++;
462 continue;
944cf283
FM
463 }
464 if (log_entry_status==RLRC_Ignore) {
7c8c06c5 465 excluded_count[ER_FormatData]++;
944cf283
FM
466 continue;
467 }
8b4e9578 468 format_count[log_line.current_format_idx]++;
944cf283 469
8b4e9578 470 if (!fp_log && ParsedOutputLog[0] && log_line.current_format!=&ReadSargLog) {
944cf283
FM
471 if(access(ParsedOutputLog,R_OK) != 0) {
472 my_mkdir(ParsedOutputLog);
473 }
474 if (snprintf(SargLogFile,sizeof(SargLogFile),"%s/sarg_temp.log",ParsedOutputLog)>=sizeof(SargLogFile)) {
af961877 475 debuga(__FILE__,__LINE__,_("Path too long: "));
041018b6 476 debuga_more("%s/sarg_temp.log\n",ParsedOutputLog);
1c91da07
FM
477 exit(EXIT_FAILURE);
478 }
944cf283 479 if((fp_log=MY_FOPEN(SargLogFile,"w"))==NULL) {
af961877 480 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),SargLogFile,strerror(errno));
1c91da07
FM
481 exit(EXIT_FAILURE);
482 }
944cf283
FM
483 fputs("*** SARG Log ***\n",fp_log);
484 }
1c91da07 485
944cf283 486 if (log_entry.Ip==NULL) {
af961877 487 debuga(__FILE__,__LINE__,_("Unknown input log file format: no IP addresses\n"));
944cf283
FM
488 break;
489 }
490 if (log_entry.User==NULL) {
af961877 491 debuga(__FILE__,__LINE__,_("Unknown input log file format: no user\n"));
944cf283
FM
492 break;
493 }
494 if (log_entry.Url==NULL) {
af961877 495 debuga(__FILE__,__LINE__,_("Unknown input log file format: no URL\n"));
944cf283
FM
496 break;
497 }
1c91da07 498
944cf283 499 idata=builddia(log_entry.EntryTime.tm_mday,log_entry.EntryTime.tm_mon+1,log_entry.EntryTime.tm_year+1900);
4246ae8d 500 if (debugz>=LogLevel_Data)
944cf283 501 printf("DATE=%s IDATA=%d DFROM=%d DUNTIL=%d\n",Filter->DateRange,idata,dfrom,duntil);
27d1fa35 502
6a943fc1
FM
503 if (EarliestDate<0 || idata<EarliestDate) {
504 EarliestDate=idata;
505 memcpy(&EarliestDateTime,&log_entry.EntryTime,sizeof(struct tm));
506 }
507 if (LatestDate<0 || idata>LatestDate) {
508 LatestDate=idata;
509 memcpy(&LatestDateTime,&log_entry.EntryTime,sizeof(struct tm));
510 }
944cf283 511 if(Filter->DateRange[0] != '\0'){
7c8c06c5
FM
512 if(idata < dfrom || idata > duntil) {
513 excluded_count[ER_OutOfDateRange]++;
514 continue;
515 }
944cf283 516 }
27d1fa35 517
944cf283 518 // Record only hours usage which is required
7c8c06c5
FM
519 if( bsearch( &( log_entry.EntryTime.tm_wday ), weekdays.list, weekdays.len, sizeof( int ), compar ) == NULL ) {
520 excluded_count[ER_OutOfWDayRange]++;
944cf283 521 continue;
7c8c06c5 522 }
27d1fa35 523
7c8c06c5
FM
524 if( bsearch( &( log_entry.EntryTime.tm_hour ), hours.list, hours.len, sizeof( int ), compar ) == NULL ) {
525 excluded_count[ER_OutOfHourRange]++;
944cf283 526 continue;
7c8c06c5 527 }
27d1fa35 528
074a6d8f
FM
529 PUser=process_user(&log_entry.User,log_entry.Ip,&id_is_ip);
530 switch (PUser)
36a0b94c 531 {
074a6d8f
FM
532 case USERERR_NoError:
533 break;
534 case USERERR_NameTooLong:
535 if (debugz>=LogLevel_Process) debuga(__FILE__,__LINE__,_("User ID too long: %s\n"),log_entry.User);
536 excluded_count[ER_UserNameTooLong]++;
537 totregsx++;
538 continue;
539 case USERERR_Excluded:
7c8c06c5 540 excluded_count[ER_User]++;
27d1fa35 541 continue;
074a6d8f
FM
542 case USERERR_InvalidChar:
543 excluded_count[ER_InvalidUserChar]++;
544 continue;
545 case USERERR_EmptyUser:
546 excluded_count[ER_NoUser]++;
547 continue;
548 case USERERR_SysUser:
549 excluded_count[ER_SysUser]++;
550 continue;
551 case USERERR_Ignored:
552 excluded_count[ER_IgnoredUser]++;
553 totregsx++;
554 continue;
555 case USERERR_Untracked:
556 excluded_count[ER_UntrackedUser]++;
557 continue;
944cf283 558 }
27d1fa35 559
944cf283 560 if(vercode(log_entry.HttpCode)) {
af961877 561 if (debugz>=LogLevel_Process) debuga(__FILE__,__LINE__,_("Excluded code: %s\n"),log_entry.HttpCode);
7c8c06c5 562 excluded_count[ER_HttpCode]++;
944cf283
FM
563 totregsx++;
564 continue;
565 }
566
944cf283
FM
567 // replace any tab by a single space
568 for (str=log_entry.Url ; *str ; str++)
569 if (*str=='\t') *str=' ';
570 for (str=log_entry.HttpCode ; *str ; str++)
571 if (*str=='\t') *str=' ';
572
8b4e9578 573 if (log_line.current_format!=&ReadSargLog) {
944cf283
FM
574 /*
575 The full URL is not saved in sarg log. There is no point in testing the URL to detect
576 a downloaded file.
577 */
578 download_flag=is_download_suffix(log_entry.Url);
579 if (download_flag) {
580 safe_strcpy(download_url,log_entry.Url,sizeof(download_url));
27d1fa35 581 }
944cf283
FM
582 } else
583 download_flag=false;
27d1fa35 584
944cf283 585 url=process_url(log_entry.Url,LongUrl);
7c8c06c5
FM
586 if (!url || url[0] == '\0') {
587 excluded_count[ER_NoUrl]++;
588 continue;
589 }
944cf283
FM
590
591 if(addr[0] != '\0'){
7c8c06c5
FM
592 if(strcmp(addr,log_entry.Ip)!=0) {
593 excluded_count[ER_UntrackedIpAddr]++;
594 continue;
595 }
944cf283
FM
596 }
597 if(Filter->HostFilter) {
598 if(!vhexclude(url)) {
af961877 599 if (debugz>=LogLevel_Data) debuga(__FILE__,__LINE__,_("Excluded site: %s\n"),url);
7c8c06c5 600 excluded_count[ER_Url]++;
27d1fa35
FM
601 totregsx++;
602 continue;
603 }
944cf283 604 }
27d1fa35 605
944cf283
FM
606 if(Filter->StartTime >= 0 && Filter->EndTime >= 0) {
607 hmr=log_entry.EntryTime.tm_hour*100+log_entry.EntryTime.tm_min;
7c8c06c5
FM
608 if(hmr < Filter->StartTime || hmr > Filter->EndTime) {
609 excluded_count[ER_OutOfTimeRange]++;
610 continue;
611 }
944cf283 612 }
27d1fa35 613
944cf283 614 if(site[0] != '\0'){
7c8c06c5
FM
615 if(strstr(url,site)==0) {
616 excluded_count[ER_UntrackedUrl]++;
617 continue;
618 }
944cf283 619 }
27d1fa35 620
944cf283 621 if (log_entry.DataSize<0) log_entry.DataSize=0;
0c87646f 622
944cf283
FM
623 if (log_entry.ElapsedTime<0) log_entry.ElapsedTime=0;
624 if (Filter->max_elapsed>0 && log_entry.ElapsedTime>Filter->max_elapsed) {
625 log_entry.ElapsedTime=0;
626 }
27d1fa35 627
944cf283
FM
628 if((str=(char *) strstr(linebuf, "[SmartFilter:")) != (char *) NULL ) {
629 fixendofline(str);
630 snprintf(smartfilter,sizeof(smartfilter),"\"%s\"",str+1);
631 } else strcpy(smartfilter,"\"\"");
27d1fa35 632
944cf283
FM
633 nopen=0;
634 prev_ufile=NULL;
7799ec8d 635 for (ufile=first_user_file ; ufile && strcmp(log_entry.User,ufile->user->id)!=0 ; ufile=ufile->next) {
944cf283
FM
636 prev_ufile=ufile;
637 if (ufile->file) nopen++;
638 }
639 if (!ufile) {
640 ufile=malloc(sizeof(*ufile));
27d1fa35 641 if (!ufile) {
7799ec8d 642 debuga(__FILE__,__LINE__,_("Not enough memory to store the user %s\n"),log_entry.User);
944cf283
FM
643 exit(EXIT_FAILURE);
644 }
645 memset(ufile,0,sizeof(*ufile));
646 ufile->next=first_user_file;
647 first_user_file=ufile;
8e134f1a
FM
648 /*
649 * This id_is_ip stuff is just to store the string only once if the user is
650 * identified by its IP address instead of a distinct ID and IP address.
651 */
7799ec8d 652 uinfo=userinfo_create(log_entry.User,(id_is_ip) ? NULL : log_entry.Ip);
944cf283 653 ufile->user=uinfo;
944cf283
FM
654 nusers++;
655 } else {
656 if (prev_ufile) {
657 prev_ufile->next=ufile->next;
27d1fa35
FM
658 ufile->next=first_user_file;
659 first_user_file=ufile;
27d1fa35 660 }
944cf283 661 }
1c91da07 662#ifdef ENABLE_DOUBLE_CHECK_DATA
d6e3e724
FM
663 if (strcmp(log_entry.HttpCode,"TCP_DENIED/407")!=0) {
664 ufile->user->nbytes+=log_entry.DataSize;
665 ufile->user->elap+=log_entry.ElapsedTime;
666 }
1c91da07 667#endif
27d1fa35 668
944cf283
FM
669 if (ufile->file==NULL) {
670 if (nopen>=maxopenfiles) {
671 x=0;
672 for (ufile1=first_user_file ; ufile1 ; ufile1=ufile1->next) {
673 if (ufile1->file!=NULL) {
674 if (x>=maxopenfiles) {
675 if (fclose(ufile1->file)==EOF) {
af961877 676 debuga(__FILE__,__LINE__,_("Write error in log file of user %s: %s\n"),ufile1->user->id,strerror(errno));
944cf283 677 exit(EXIT_FAILURE);
27d1fa35 678 }
944cf283 679 ufile1->file=NULL;
27d1fa35 680 }
944cf283 681 x++;
27d1fa35
FM
682 }
683 }
27d1fa35 684 }
944cf283 685 if (snprintf (tmp3, sizeof(tmp3), "%s/%s.user_unsort", tmp, ufile->user->filename)>=sizeof(tmp3)) {
af961877 686 debuga(__FILE__,__LINE__,_("Temporary user file name too long: %s/%s.user_unsort\n"), tmp, ufile->user->filename);
27d1fa35
FM
687 exit(EXIT_FAILURE);
688 }
944cf283 689 if ((ufile->file = MY_FOPEN (tmp3, "a")) == NULL) {
af961877 690 debuga(__FILE__,__LINE__,_("(log) Cannot open temporary file %s: %s\n"), tmp3, strerror(errno));
944cf283 691 exit (1);
1c91da07 692 }
944cf283 693 }
27d1fa35 694
944cf283
FM
695 strftime(dia, sizeof(dia), "%d/%m/%Y",&log_entry.EntryTime);
696 strftime(hora,sizeof(hora),"%H:%M:%S",&log_entry.EntryTime);
27d1fa35 697
944cf283
FM
698 if (fprintf(ufile->file, "%s\t%s\t%s\t%s\t%"PRIu64"\t%s\t%ld\t%s\n",dia,hora,
699 log_entry.Ip,url,(uint64_t)log_entry.DataSize,
700 log_entry.HttpCode,log_entry.ElapsedTime,smartfilter)<=0) {
7799ec8d 701 debuga(__FILE__,__LINE__,_("Write error in the log file of user %s\n"),log_entry.User);
944cf283
FM
702 exit(EXIT_FAILURE);
703 }
704 records_kept++;
27d1fa35 705
8b4e9578 706 if (fp_log && log_line.current_format!=&ReadSargLog) {
944cf283 707 fprintf(fp_log, "%s\t%s\t%s\t%s\t%s\t%"PRIu64"\t%s\t%ld\t%s\n",dia,hora,
7799ec8d 708 log_entry.User,log_entry.Ip,url,(uint64_t)log_entry.DataSize,
944cf283
FM
709 log_entry.HttpCode,log_entry.ElapsedTime,smartfilter);
710 }
711
712 totregsg++;
713
714 denied_write(&log_entry);
715 authfail_write(&log_entry);
88776d28 716 if (download_flag) download_write(&log_entry,download_url);
27d1fa35 717
8b4e9578 718 if (log_line.current_format!=&ReadSargLog) {
944cf283
FM
719 if (period.start.tm_year==0 || idata<mindate || compare_date(&period.start,&log_entry.EntryTime)>0){
720 mindate=idata;
721 memcpy(&period.start,&log_entry.EntryTime,sizeof(log_entry.EntryTime));
722 }
723 if (period.end.tm_year==0 || idata>maxdate || compare_date(&period.end,&log_entry.EntryTime)<0) {
724 maxdate=idata;
725 memcpy(&period.end,&log_entry.EntryTime,sizeof(log_entry.EntryTime));
27d1fa35
FM
726 }
727 }
728
4246ae8d 729 if (debugz>=LogLevel_Data){
944cf283 730 printf("IP=\t%s\n",log_entry.Ip);
7799ec8d 731 printf("USER=\t%s\n",log_entry.User);
944cf283
FM
732 printf("ELAP=\t%ld\n",log_entry.ElapsedTime);
733 printf("DATE=\t%s\n",dia);
734 printf("TIME=\t%s\n",hora);
735 //printf("FUNC=\t%s\n",fun);
736 printf("URL=\t%s\n",url);
737 printf("CODE=\t%s\n",log_entry.HttpCode);
738 printf("LEN=\t%"PRIu64"\n",(uint64_t)log_entry.DataSize);
27d1fa35
FM
739 }
740 }
741 longline_destroy(&line);
742
800eafb8
FM
743 if (FileObject_Close(fp_in)) {
744 debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),arq,FileObject_GetLastCloseError());
745 exit(EXIT_FAILURE);
746 }
747 if (ShowReadStatistics) {
748 if (ShowReadPercent)
749 printf(_("SARG: Records in file: %lu, reading: %3.2f%%\n"),recs2, (float) 100 );
750 else
751 printf(_("SARG: Records in file: %lu\n"),recs2);
944cf283
FM
752 }
753}
754
7c8c06c5
FM
755/*!
756 * Display a line with the excluded entries count.
757 *
758 * \param Explain A translated string explaining the exluded count.
759 * \param Reason The reason number.
760 */
761static void DisplayExcludeCount(const char *Explain,enum ExcludeReasonEnum Reason)
762{
763 if (excluded_count[Reason]>0) {
af961877 764 debuga(__FILE__,__LINE__," %s: %lu\n",Explain,excluded_count[Reason]);
7c8c06c5
FM
765 }
766}
767
944cf283
FM
768/*!
769Read the log files.
770
771\param Filter The filtering parameters for the file to load.
772
773\retval 1 Records found.
774\retval 0 No record found.
775*/
776int ReadLogFile(struct ReadLogDataStruct *Filter)
777{
944cf283
FM
778 int x;
779 int cstatus;
780 struct userfilestruct *ufile;
781 struct userfilestruct *ufile1;
6068ae56
FM
782 FileListIterator FIter;
783 const char *file;
944cf283
FM
784
785 for (x=0 ; x<sizeof(format_count)/sizeof(*format_count) ; x++) format_count[x]=0;
7c8c06c5 786 for (x=0 ; x<sizeof(excluded_count)/sizeof(*excluded_count) ; x++) excluded_count[x]=0;
944cf283
FM
787 first_user_file=NULL;
788
789 if (!dataonly) {
790 denied_open();
791 authfail_open();
792 download_open();
793 }
794
6068ae56
FM
795 FIter=FileListIter_Open(AccessLog);
796 while ((file=FileListIter_Next(FIter))!=NULL)
797 ReadOneLogFile(Filter,file);
798 FileListIter_Close(FIter);
944cf283 799
27d1fa35 800 if(fp_log != NULL) {
27d1fa35 801 char val2[40];
944cf283 802 char val4[4096];//val4 must not be bigger than SargLogFile without fixing the strcpy below
0c87646f 803
507460ae 804 if (fclose(fp_log)==EOF) {
af961877 805 debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),SargLogFile,strerror(errno));
507460ae
FM
806 exit(EXIT_FAILURE);
807 }
944cf283
FM
808 strftime(val2,sizeof(val2),"%d%m%Y_%H%M",&period.start);
809 strftime(val1,sizeof(val1),"%d%m%Y_%H%M",&period.end);
810 if (snprintf(val4,sizeof(val4),"%s/sarg-%s-%s.log",ParsedOutputLog,val2,val1)>=sizeof(val4)) {
af961877 811 debuga(__FILE__,__LINE__,_("Path too long: "));
041018b6 812 debuga_more("%s/sarg-%s-%s.log\n",ParsedOutputLog,val2,val1);
27d1fa35
FM
813 exit(EXIT_FAILURE);
814 }
944cf283 815 if (rename(SargLogFile,val4)) {
af961877 816 debuga(__FILE__,__LINE__,_("failed to rename %s to %s - %s\n"),SargLogFile,val4,strerror(errno));
27d1fa35 817 } else {
944cf283 818 strcpy(SargLogFile,val4);
27d1fa35
FM
819
820 if(strcmp(ParsedOutputLogCompress,"nocompress") != 0 && ParsedOutputLogCompress[0] != '\0') {
821 /*
822 No double quotes around ParsedOutputLogCompress because it may contain command line options. If double quotes are
823 necessary around the command name, put them in the configuration file.
824 */
944cf283 825 if (snprintf(val1,sizeof(val1),"%s \"%s\"",ParsedOutputLogCompress,SargLogFile)>=sizeof(val1)) {
af961877 826 debuga(__FILE__,__LINE__,_("Command too long: %s \"%s\"\n"),ParsedOutputLogCompress,SargLogFile);
27d1fa35
FM
827 exit(EXIT_FAILURE);
828 }
829 cstatus=system(val1);
830 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
af961877
FM
831 debuga(__FILE__,__LINE__,_("command return status %d\n"),WEXITSTATUS(cstatus));
832 debuga(__FILE__,__LINE__,_("command: %s\n"),val1);
27d1fa35
FM
833 exit(EXIT_FAILURE);
834 }
835 }
836 }
837 if(debug)
af961877 838 debuga(__FILE__,__LINE__,_("Sarg parsed log saved as %s\n"),SargLogFile);
0c87646f 839 }
27d1fa35 840
8e53b2e7 841 denied_close();
16b013cc 842 authfail_close();
11284535 843 download_close();
27d1fa35
FM
844
845 for (ufile=first_user_file ; ufile ; ufile=ufile1) {
846 ufile1=ufile->next;
507460ae 847 if (ufile->file!=NULL && fclose(ufile->file)==EOF) {
af961877 848 debuga(__FILE__,__LINE__,_("Write error in log file of user %s: %s\n"),ufile->user->id,strerror(errno));
507460ae
FM
849 exit(EXIT_FAILURE);
850 }
27d1fa35
FM
851 free(ufile);
852 }
853
854 if (debug) {
8e501bd6 855 unsigned long int totalcount=0;
27d1fa35 856
af961877 857 debuga(__FILE__,__LINE__,_(" Records read: %ld, written: %ld, excluded: %ld\n"),totregsl,totregsg,totregsx);
27d1fa35 858
7c8c06c5
FM
859 for (x=sizeof(excluded_count)/sizeof(*excluded_count)-1 ; x>=0 && excluded_count[x]>0 ; x--);
860 if (x>=0) {
af961877 861 debuga(__FILE__,__LINE__,_("Reasons for excluded entries:\n"));
7c8c06c5
FM
862 DisplayExcludeCount(_("User name too long"),ER_UserNameTooLong);
863 DisplayExcludeCount(_("Squid logged an incomplete query received from the client"),ER_IncompleteQuery);
864 DisplayExcludeCount(_("Log file turned over"),ER_LogfileTurnedOver);
7c8c06c5
FM
865 DisplayExcludeCount(_("Excluded by \"exclude_string\" in sarg.conf"),ER_ExcludeString);
866 DisplayExcludeCount(_("Unknown input log file format"),ER_UnknownFormat);
867 DisplayExcludeCount(_("Line ignored by the input log format"),ER_FormatData);
868 DisplayExcludeCount(_("Time outside the requested date range (-d)"),ER_OutOfDateRange);
869 DisplayExcludeCount(_("Ignored week day (\"weekdays\" parameter in sarg.conf)"),ER_OutOfWDayRange);
870 DisplayExcludeCount(_("Ignored hour (\"hours\" parameter in sarg.conf)"),ER_OutOfHourRange);
871 DisplayExcludeCount(_("User is not in the \"include_users\" list"),ER_User);
872 DisplayExcludeCount(_("HTTP code excluded by \"exclude_code\" file"),ER_HttpCode);
873 DisplayExcludeCount(_("Invalid character found in user name"),ER_InvalidUserChar);
874 DisplayExcludeCount(_("No URL in entry"),ER_NoUrl);
875 DisplayExcludeCount(_("Not the IP address requested with -a"),ER_UntrackedIpAddr);
876 DisplayExcludeCount(_("URL excluded by -c or \"exclude_hosts\""),ER_Url);
877 DisplayExcludeCount(_("Entry time outside of requested hour range (-t)"),ER_OutOfTimeRange);
878 DisplayExcludeCount(_("Not the URL requested by -s"),ER_UntrackedUrl);
879 DisplayExcludeCount(_("No user in entry"),ER_NoUser);
880 DisplayExcludeCount(_("Not the user requested by -u"),ER_UntrackedUser);
881 DisplayExcludeCount(_("System user as defined by \"password\" in sarg.conf"),ER_SysUser);
882 DisplayExcludeCount(_("User ignored by \"exclude_users\""),ER_IgnoredUser);
883 }
884
1c91da07
FM
885 for (x=0 ; x<sizeof(LogFormats)/sizeof(*LogFormats) ; x++) {
886 if (format_count[x]>0) {
887 /* TRANSLATORS: It displays the number of lines found in the input log files
888 * for each supported log format. The log format name is the %s and is a string
889 * you translate somewhere else. */
af961877 890 debuga(__FILE__,__LINE__,_("%s: %lu entries\n"),_(LogFormats[x]->Name),format_count[x]);
1c91da07
FM
891 totalcount+=format_count[x];
892 }
893 }
27d1fa35 894
1c91da07 895 if (totalcount==0 && totregsg)
af961877 896 debuga(__FILE__,__LINE__,_("Log with invalid format\n"));
27d1fa35
FM
897 }
898
27d1fa35
FM
899 return((totregsg!=0) ? 1 : 0);
900}
6a943fc1
FM
901
902/*!
903 * Get the start and end date of the period covered by the log files.
904 */
b6fb8c79 905bool GetLogPeriod(struct tm *Start,struct tm *End)
6a943fc1 906{
b6fb8c79
FM
907 bool Valid=false;
908
6a943fc1
FM
909 if (EarliestDate>=0) {
910 memcpy(Start,&EarliestDateTime,sizeof(struct tm));
b6fb8c79 911 Valid=true;
6a943fc1
FM
912 } else {
913 memset(Start,0,sizeof(struct tm));
914 }
915 if (LatestDate>=0) {
916 memcpy(End,&LatestDateTime,sizeof(struct tm));
b6fb8c79 917 Valid=true;
6a943fc1
FM
918 } else {
919 memset(End,0,sizeof(struct tm));
920 }
b6fb8c79 921 return(Valid);
6a943fc1 922}