]> git.ipfire.org Git - thirdparty/sarg.git/blame - log.c
Remove trailing spaces in sarg.conf
[thirdparty/sarg.git] / log.c
CommitLineData
25697a35 1/*
94ff9470 2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
ad500baa 3 * 1998, 2010
94ff9470 4 *
25697a35 5 * SARG donations:
94ff9470 6 * please look at http://sarg.sourceforge.net/donations.php
ad500baa
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"
5f3cfd1d 28#include "include/defs.h"
25697a35 29
9b179eb0
FM
30#ifdef HAVE_GETOPT_H
31#include <getopt.h>
32#endif
33
25697a35 34#define REPORT_EVERY_X_LINES 5000
f2ec8c75
FM
35#define MAX_OPEN_USER_FILES 10
36
37struct userfilestruct
38{
39 struct userfilestruct *next;
40 struct userinfostruct *user;
41 FILE *file;
42};
25697a35 43
2824ec9b 44/*@null@*/static char *userfile=NULL;
25697a35
GS
45
46numlist weekdays = { { 0, 1, 2, 3, 4, 5, 6 }, 7 };
47numlist hours = { { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
48 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, 24 };
49
936c9905 50static void getusers(const char *pwdfile, int debug);
25697a35 51
25f0793f 52int main(int argc,char *argv[])
25697a35 53{
076cbab8
FM
54 enum isa_col_id {
55 ISACOL_Ip,
56 ISACOL_UserName,
57 ISACOL_Date,
58 ISACOL_Time,
59 ISACOL_TimeTaken,
60 ISACOL_Bytes,
61 ISACOL_Uri,
62 ISACOL_Status,
63 ISACOL_Last //last entry of the list !
64 };
58d7d8fe
FM
65 enum InputLogFormat {
66 ILF_Unknown,
67 ILF_Squid,
68 ILF_Common,
69 ILF_Sarg,
70 ILF_Isa,
71 ILF_Last //last entry of the list !
72 };
25697a35 73
120d768c 74 FILE *fp_in = NULL, *fp_denied=NULL, *fp_authfail=NULL, *fp_log=NULL;
25697a35
GS
75
76 char sz_Download_Unsort[ 20000 ] ;
77 FILE * fp_Download_Unsort = NULL ;
25697a35
GS
78
79 extern int optind;
80 extern int optopt;
81 extern char *optarg;
82
25f0793f 83 char data[255];
25697a35 84 char elap[255];
25697a35 85 char ip[MAXLEN];
25697a35 86 char tam[255];
25f0793f
FM
87 char fun[MAXLEN];
88 char wuser[MAXLEN];
25697a35 89 char smartfilter[MAXLEN];
2357ef77 90 char dia[128];
25697a35
GS
91 char mes[30];
92 char ano[30];
93 char hora[30];
94 char wtemp[MAXLEN];
25697a35
GS
95 char date[255];
96 char arq[255];
97 char arq_log[255];
58eec3c0
FM
98 int hm, hmf, hmr;
99 char hm_str[15];
25697a35
GS
100 char uagent[MAXLEN];
101 char hexclude[MAXLEN];
456d78a5
FM
102 char csort[MAXLEN];
103 int cstatus;
25697a35
GS
104 char tbuf2[128];
105 char zip[20];
106 char *str;
2240dcea 107 char tmp3[MAXLEN];
f84a35a3
FM
108 char denied_unsort[MAXLEN];
109 char denied_sort[MAXLEN];
110 char authfail_unsort[MAXLEN];
ff8d5836
FM
111 char start_hour[128];
112 char end_hour[128];
ac422f9b 113 char *linebuf;
e5b2c6f0
FM
114 char hostname[512];
115 char *url;
116 char *urly;
2240dcea 117 char user[MAX_USER_LEN];
58d7d8fe
FM
118 enum InputLogFormat ilf;
119 int ilf_count[ILF_Last];
25697a35 120 int ch;
f2ec8c75 121 int x;
25697a35
GS
122 int errflg=0;
123 int puser=0;
2824ec9b
FM
124 bool fhost=false;
125 bool dns=false;
126 bool fuser=false;
25697a35 127 int idata=0;
ed1ac52f 128 int mindate=0;
fa6552b0 129 int maxdate=0;
25697a35 130 int iarq=0;
076cbab8 131 int isa_ncols=0,isa_cols[ISACOL_Last];
2824ec9b 132 bool from_stdin;
d2855b39 133 bool from_pipe;
ab950637 134 int blen;
f2ec8c75
FM
135 int maxopenfiles;
136 int nopen;
8bb4048f 137 bool id_is_ip;
2357ef77 138 long totregsl=0;
25697a35
GS
139 long totregsg=0;
140 long totregsx=0;
fa6552b0 141 bool totper=false;
58d7d8fe 142 long int max_elapsed=0;
fa6552b0 143 long long int iyear, imonth, iday;
2824ec9b 144 bool realt;
8bb4048f 145 bool userip;
fa6552b0 146 struct tm tt;
25697a35 147 struct tm *t;
7c0b0078
FM
148 unsigned long recs1=0UL;
149 unsigned long recs2=0UL;
25697a35 150 int OutputNonZero = REPORT_EVERY_X_LINES ;
2824ec9b 151 bool download_flag=false;
c36c7384 152 char *download_url=NULL;
9c7c6346 153 struct getwordstruct gwarea;
afaa3b67 154 longline line;
fa6552b0 155 time_t tnum;
c6a3ce24 156 struct stat logstat;
f2ec8c75
FM
157 struct userinfostruct *uinfo;
158 struct userfilestruct *first_user_file, *ufile, *ufile1, *prev_ufile;
9b179eb0
FM
159 static int split=0;
160 static int convert=0;
d183fb7f 161 static int output_css=0;
9b179eb0
FM
162 int option_index;
163 static struct option long_options[]=
164 {
165 {"convert",no_argument,&convert,1},
d183fb7f 166 {"css",no_argument,&output_css,1},
9b179eb0
FM
167 {"split",no_argument,&split,1},
168 {0,0,0,0}
169 };
25697a35 170
abfc169e
FM
171#ifdef HAVE_LOCALE_H
172 setlocale(LC_TIME,"");
173#endif
174
cec3eed3 175#if defined(ENABLE_NLS) && defined(HAVE_LOCALE_H)
fd46f082
FM
176 if (!setlocale (LC_ALL, "")) {
177 fprintf(stderr,"SARG: Cannot set the locale LC_ALL to the environment variable\n");
178 exit(EXIT_FAILURE);
179 }
180 if (!bindtextdomain (PACKAGE_NAME, LOCALEDIR)) {
181 fprintf(stderr,"SARG: Cannot bind to text domain %s in directory %s (%s)\n",PACKAGE_NAME,LOCALEDIR,strerror(errno));
182 exit(EXIT_FAILURE);
183 }
184 if (!textdomain (PACKAGE_NAME)) {
185 fprintf(stderr,"SARG: Cannot set gettext domain for %s PACKAGE_NAME (%s)\n",PACKAGE_NAME,strerror(errno));
186 exit(EXIT_FAILURE);
187 }
abfc169e
FM
188#endif //ENABLE_NLS
189
25697a35
GS
190 BgImage[0]='\0';
191 LogoImage[0]='\0';
192 LogoText[0]='\0';
193 PasswdFile[0]='\0';
194 OutputEmail[0]='\0';
25697a35
GS
195 UserAgentLog[0]='\0';
196 ExcludeHosts[0]='\0';
197 ExcludeUsers[0]='\0';
198 ConfigFile[0]='\0';
199 code[0]='\0';
0511cf2d 200 LastLog=0;
085c1e1f 201 ReportType=0UL;
25697a35
GS
202 UserTabFile[0]='\0';
203 BlockIt[0]='\0';
204 ExternalCSSFile[0]='\0';
1f482a8d
FM
205 RedirectorLogFormat[0]='\0';
206 NRedirectorLogs=0;
58d7d8fe 207 for (ilf=0 ; ilf<ILF_Last ; ilf++) ilf_count[ilf]=0;
25697a35 208
25697a35
GS
209 sprintf(ExcludeCodes,"%s/exclude_codes",SYSCONFDIR);
210 strcpy(GraphDaysBytesBarColor,"orange");
491b862f 211 strcpy(BgColor,"#ffffff");
25697a35
GS
212 strcpy(TxColor,"#000000");
213 strcpy(TxBgColor,"lavender");
214 strcpy(TiColor,"darkblue");
215 strcpy(Width,"80");
216 strcpy(Height,"45");
217 strcpy(LogoTextColor,"#000000");
218 strcpy(HeaderColor,"darkblue");
491b862f 219 strcpy(HeaderBgColor,"#dddddd");
94ff9470 220 strcpy(LogoTextColor,"#006699");
25697a35
GS
221 strcpy(FontSize,"9px");
222 strcpy(TempDir,"/tmp");
d6e703cc 223 strcpy(OutputDir,"/var/www/html/squid-reports");
8bb4048f 224 Ip2Name=false;
25697a35 225 strcpy(DateFormat,"u");
2824ec9b
FM
226 OverwriteReport=false;
227 RemoveTempFiles=true;
25697a35 228 strcpy(ReplaceIndex,"index.html");
0349fa24 229 Index=INDEX_YES;
f2ec8c75 230 RecordsWithoutUser=RECORDWITHOUTUSER_IP;
e6414a9d 231 UseComma=0;
25697a35 232 strcpy(MailUtility,"mailx");
c0ec9cc7 233 TopSitesNum=100;
3becf85c 234 TopUsersNum=0;
246c8489 235 UserIp=0;
15d5372b
FM
236 TopuserSort=TOPUSER_SORT_BYTES | TOPUSER_SORT_REVERSE;
237 UserSort=USER_SORT_BYTES | USER_SORT_REVERSE;
238 TopsitesSort=TOPSITE_SORT_CONNECT | TOPSITE_SORT_REVERSE;
246c8489 239 LongUrl=0;
25697a35 240 strcpy(FontFace,"Verdana,Tahoma,Arial");
ba638499 241 datetimeby=DATETIME_BYTE;
25697a35 242 strcpy(CharSet,"ISO-8859-1");
e6414a9d 243 Privacy=0;
25697a35
GS
244 strcpy(PrivacyString,"***.***.***.***");
245 strcpy(PrivacyStringColor,"blue");
2824ec9b 246 SuccessfulMsg=true;
085c1e1f
FM
247 TopUserFields=TOPUSERFIELDS_NUM | TOPUSERFIELDS_DATE_TIME | TOPUSERFIELDS_USERID | TOPUSERFIELDS_CONNECT |
248 TOPUSERFIELDS_BYTES | TOPUSERFIELDS_SETYB | TOPUSERFIELDS_IN_CACHE_OUT |
249 TOPUSERFIELDS_USED_TIME | TOPUSERFIELDS_MILISEC | TOPUSERFIELDS_PTIME |
250 TOPUSERFIELDS_TOTAL | TOPUSERFIELDS_AVERAGE;
251 UserReportFields=USERREPORTFIELDS_CONNECT | USERREPORTFIELDS_BYTES | USERREPORTFIELDS_SETYB |
252 USERREPORTFIELDS_IN_CACHE_OUT | USERREPORTFIELDS_USED_TIME | USERREPORTFIELDS_MILISEC |
253 USERREPORTFIELDS_PTIME | USERREPORTFIELDS_TOTAL | USERREPORTFIELDS_AVERAGE;
25697a35 254 strcpy(DataFileDelimiter,";");
085c1e1f
FM
255 DataFileFields=DATA_FIELD_USER | DATA_FIELD_DATE | DATA_FIELD_TIME | DATA_FIELD_URL | DATA_FIELD_CONNECT |
256 DATA_FIELD_BYTES | DATA_FIELD_IN_CACHE | DATA_FIELD_OUT_CACHE | DATA_FIELD_ELAPSED;
2824ec9b 257 ShowReadStatistics=true;
25697a35 258 strcpy(IndexSortOrder,"D");
2824ec9b
FM
259 ShowSargInfo=true;
260 ShowSargLogo=true;
25697a35 261 strcpy(ParsedOutputLog,"no");
ff8d5836 262 strcpy(ParsedOutputLogCompress,"/bin/gzip -f");
e43854ef 263 DisplayedValues=DISPLAY_ABBREV;
25697a35
GS
264 strcpy(HeaderFontSize,"9px");
265 strcpy(TitleFontSize,"11px");
d5d021c5 266 strcpy(AuthUserTemplateFile,"sarg_htaccess");
6e792ade 267 set_download_suffix("7z,ace,arj,avi,bat,bin,bz2,bzip,cab,com,cpio,dll,doc,dot,exe,gz,iso,lha,lzh,mdb,mov,mp3,mpeg,mpg,mso,nrg,ogg,ppt,rar,rtf,shs,src,sys,tar,tgz,vcd,vob,wma,wmv,zip");
2824ec9b 268 Graphs=true;
3becf85c
FM
269#if defined(FONTDIR)
270 strcpy(GraphFont,FONTDIR"/DejaVuSans.ttf");
271#else
272 GraphFont[0]='\0';
273#endif
25697a35 274 strcpy(Ulimit,"20000");
f2ec8c75 275 NtlmUserFormat=NTLMUSERFORMAT_DOMAINUSER;
0349fa24 276 IndexTree=INDEX_TREE_FILE;
491b862f 277 strcpy(RealtimeTypes,"GET,PUT,CONNECT");
71d78774 278 RealtimeUnauthRec=REALTIME_UNAUTH_REC_SHOW;
64dfb824
FM
279 RedirectorFilterOutDate=true;
280 DansguardianFilterOutDate=true;
f84a35a3 281 DataFileUrl=DATAFILEURL_IP;
d6e703cc 282 strcpy(MaxElapsed,"28800000");
e6414a9d
FM
283 BytesInSitesUsersReport=0;
284 UserAuthentication=0;
e3af0ae9 285 strcpy(LDAPHost,"127.0.0.1");
965c4a6f
FM
286 LDAPPort=389;
287 LDAPProtocolVersion=3;
e3af0ae9
PO
288 LDAPBindDN[0]='\0';
289 LDAPBindPW[0]='\0';
290 LDAPBaseSearch[0]='\0';
1b048c43 291 strcpy(LDAPFilterSearch, "(uid=%s)");
e3af0ae9 292 strcpy(LDAPTargetAttr, "cn");
25697a35
GS
293
294 dia[0]='\0';
295 mes[0]='\0';
296 ano[0]='\0';
297 hora[0]='\0';
298 tmp[0]='\0';
25697a35
GS
299 tmp3[0]='\0';
300 wtemp[0]='\0';
25697a35
GS
301 us[0]='\0';
302 date[0]='\0';
303 df[0]='\0';
304 uagent[0]='\0';
305 hexclude[0]='\0';
306 addr[0]='\0';
58eec3c0
FM
307 hm=-1;
308 hmf=-1;
25697a35
GS
309 site[0]='\0';
310 outdir[0]='\0';
311 elap[0]='\0';
312 email[0]='\0';
313 zip[0]='\0';
314 UserInvalidChar[0]='\0';
315 DataFile[0]='\0';
316 SquidGuardConf[0]='\0';
317 DansGuardianConf[0]='\0';
ff8d5836
FM
318 start_hour[0]='\0';
319 end_hour[0]='\0';
25697a35 320
25697a35
GS
321 denied_count=0;
322 download_count=0;
323 authfail_count=0;
324 dansguardian_count=0;
325 squidguard_count=0;
c0ec9cc7 326 useragent_count=0;
25697a35
GS
327 DeniedReportLimit=10;
328 AuthfailReportLimit=10;
329 DansGuardianReportLimit=10;
330 SquidGuardReportLimit=10;
94ff9470 331 DownloadReportLimit=50;
25697a35
GS
332 UserReportLimit=0;
333 debug=0;
334 debugz=0;
335 debugm=0;
2824ec9b
FM
336 iprel=false;
337 userip=false;
2824ec9b 338 realt=false;
491b862f
GS
339 realtime_refresh=3;
340 realtime_access_log_lines=1000;
341 cost=0.01;
342 nocost=50000000;
d6e703cc 343 ndownload=0;
2824ec9b 344 squid24=false;
42b117e3
FM
345 dfrom=0;
346 duntil=0;
25697a35 347
f2ec8c75
FM
348 bzero(IncludeUsers, sizeof(IncludeUsers));
349 bzero(ExcludeString, sizeof(ExcludeString));
350 first_user_file=NULL;
fa6552b0 351 memset(&period,0,sizeof(period));
25697a35 352
0a4e18e1 353 NAccessLog=0;
3becf85c 354 for(x=0; x<MAXLOGS; x++)
0a4e18e1
FM
355 AccessLog[x][0]='\0';
356 AccessLogFromCmdLine=0;
1f482a8d 357 RedirectorLogFromCmdLine=0;
25697a35 358
c36c7384 359 strcpy(Title,_("Squid User Access Report"));
25697a35 360
9b179eb0 361 while((ch = getopt_long_only(argc, argv, "a:b:c:d:e:f:g:u:l:L:o:s:t:w:hijmnprvxyz",long_options,&option_index)) != -1){
25697a35
GS
362 switch(ch)
363 {
9b179eb0
FM
364 case 0:
365 break;
25697a35
GS
366 case 'a':
367 strcpy(addr,optarg);
368 break;
369 case 'b':
370 strcpy(uagent,optarg);
371 break;
372 case 'c':
373 strcpy(hexclude,optarg);
374 break;
375 case 'd':
9c7c6346
FM
376 strncpy(date,optarg,sizeof(date)-1);
377 date[sizeof(date)-1]='\0';
42b117e3 378 date_from(date, &dfrom, &duntil);
25697a35
GS
379 break;
380 case 'e':
381 strcpy(email,optarg);
382 break;
383 case 'f':
384 strcpy(ConfigFile,optarg);
385 break;
386 case 'g':
387 strcpy(df,optarg);
388 break;
936c9905
FM
389 case 'h':
390 usage(argv[0]);
2824ec9b 391 exit(EXIT_SUCCESS);
25697a35 392 case 'i':
2824ec9b 393 iprel=true;
25697a35
GS
394 break;
395 case 'l':
0a4e18e1 396 if (NAccessLog>=MAXLOGS) {
1f482a8d
FM
397 debuga(_("Too many log files passed on command line with option -l.\n"));
398 exit(EXIT_FAILURE);
399 }
400 if (strlen(optarg)>=MAX_LOG_FILELEN) {
401 debuga(_("Log file name too long passed on command line with option -l: %s\n"),optarg);
06b39c87 402 exit(EXIT_FAILURE);
4af2f8ff 403 }
0a4e18e1
FM
404 strcpy(AccessLog[NAccessLog],optarg);
405 NAccessLog++;
406 AccessLogFromCmdLine++;
25697a35 407 break;
491b862f 408 case 'L':
1f482a8d
FM
409 if (NRedirectorLogs>MAX_REDIRECTOR_LOGS) {
410 debuga(_("Too many redirector logs passed on command line with option -L.\n"));
411 exit(EXIT_FAILURE);
412 }
413 if (strlen(optarg)>=MAX_REDIRECTOR_FILELEN) {
414 debuga(_("Redirector log file name too long passed on command line with opton -L: %s\n"),optarg);
415 exit(EXIT_FAILURE);
416 }
417 strcpy(RedirectorLogs[NRedirectorLogs],optarg);
418 NRedirectorLogs++;
419 RedirectorLogFromCmdLine++;
491b862f 420 break;
25697a35
GS
421 case 'm':
422 debugm++;
423 break;
424 case 'n':
2824ec9b 425 dns=true;
25697a35
GS
426 break;
427 case 'o':
428 strcpy(outdir,optarg);
429 break;
430 case 'p':
2824ec9b 431 userip=true;
25697a35 432 break;
491b862f 433 case 'r':
2824ec9b 434 realt=true;
491b862f 435 break;
25697a35
GS
436 case 's':
437 strcpy(site,optarg);
438 break;
439 case 't':
9c7c6346 440 {
58eec3c0 441 int h1,m1,h2,m2;
9c7c6346 442
25697a35 443 if(strstr(optarg,"-") == 0) {
58eec3c0
FM
444 if(sscanf(optarg,"%d:%d",&h1,&m1)!=2) {
445 debuga(_("Time period passed on the command line with option -t must be MM:SS\n"));
446 exit(EXIT_FAILURE);
447 }
448 hm=h1*100+m1;
449 hmf=hm;
450 snprintf(hm_str,sizeof(hm_str),"%02d:%02d",h1,m1);
25697a35 451 } else {
58eec3c0
FM
452 if(sscanf(optarg,"%d:%d-%d:%d",&h1,&m1,&h2,&m2)!=4) {
453 debuga(_("Time range passed on the command line with option -t must be MM:SS-MM:SS\n"));
06b39c87 454 exit(EXIT_FAILURE);
4bcb77cf 455 }
58eec3c0
FM
456 hm=h1*100+m1;
457 hmf=h2*100+m2;
458 snprintf(hm_str,sizeof(hm_str),"%02d:%02d-%02d:%02d",h1,m1,h2,m2);
25697a35 459 }
25697a35 460 break;
9c7c6346 461 }
25697a35
GS
462 case 'u':
463 strcpy(us,optarg);
464 break;
465 case 'v':
466 version();
467 break;
468 case 'w':
469 strcpy(tmp,optarg);
470 break;
471 case 'x':
472 debug++;
473 break;
474 case 'y':
475 langcode++;
476 break;
477 case 'z':
478 debugz++;
479 break;
9b179eb0 480 /*case ':':
10210234 481 debuga(_("Option -%c require an argument\n"),optopt);
936c9905 482 errflg++;
9b179eb0 483 break;*/
25697a35
GS
484 case '?':
485 usage(argv[0]);
06b39c87 486 exit(EXIT_FAILURE);
9b179eb0
FM
487 default:
488 abort();
25697a35 489 }
25697a35
GS
490 }
491
2824ec9b 492 if (errflg>0) {
25697a35
GS
493 usage(argv[0]);
494 exit(2);
495 }
496
ed7127e3
FM
497 if (optind<argc) {
498 for (iarq=optind ; iarq<argc ; iarq++) {
499 if (NAccessLog>=MAXLOGS) {
500 debuga(_("Too many log files passed on command line.\n"));
501 exit(EXIT_FAILURE);
502 }
503 if (strlen(argv[iarq])>=MAX_LOG_FILELEN) {
504 debuga(_("Log file name too long passed on command line: %s\n"),argv[iarq]);
505 exit(EXIT_FAILURE);
506 }
507 strcpy(AccessLog[NAccessLog],argv[iarq]);
508 NAccessLog++;
509 AccessLogFromCmdLine++;
510 }
511 }
512
10210234 513 if(debug) debuga(_("Init\n"));
25697a35 514
2824ec9b 515 if(ConfigFile[0] == '\0') snprintf(ConfigFile,sizeof(ConfigFile),"%s/sarg.conf",SYSCONFDIR);
936c9905 516 if(access(ConfigFile, R_OK) != 0) {
10210234 517 debuga(_("Cannot open config file: %s - %s\n"),ConfigFile,strerror(errno));
06b39c87 518 exit(EXIT_FAILURE);
936c9905
FM
519 }
520
25697a35 521 if(access(ConfigFile, R_OK) == 0)
d6e703cc 522 getconf();
936c9905 523
8bb4048f 524 if(userip) UserIp=true;
be081e85 525
2824ec9b 526 if(dns) Ip2Name=true;
be081e85 527
491b862f
GS
528 if(realt) {
529 realtime();
2824ec9b 530 exit(EXIT_SUCCESS);
491b862f 531 }
25697a35 532
0349fa24 533 if(IndexTree == INDEX_TREE_FILE)
491b862f
GS
534 strcpy(ImageFile,"../images");
535 else
536 strcpy(ImageFile,"../../../images");
537
25697a35 538 dataonly=0;
c274f011 539 if(DataFile[0] != '\0')
25697a35 540 dataonly++;
936c9905 541
2824ec9b 542 if(NAccessLog == 0) {
0a4e18e1
FM
543 strcpy(AccessLog[0],"/var/log/squid/access.log");
544 NAccessLog++;
25697a35
GS
545 }
546
d183fb7f
FM
547 if(output_css) {
548 css_content(stdout);
549 exit(EXIT_SUCCESS);
550 }
9b179eb0 551 if(split) {
b24165e3
FM
552 for (iarq=0 ; iarq<NAccessLog ; iarq++)
553 splitlog(AccessLog[iarq], df, dfrom, duntil, convert);
2824ec9b 554 exit(EXIT_SUCCESS);
25697a35 555 }
9b179eb0 556 if(convert) {
b24165e3
FM
557 for (iarq=0 ; iarq<NAccessLog ; iarq++)
558 convlog(AccessLog[iarq], df, dfrom, duntil);
2824ec9b 559 exit(EXIT_SUCCESS);
25697a35
GS
560 }
561
48864d28 562 load_excludecodes(ExcludeCodes);
25697a35
GS
563
564 if(access(PasswdFile, R_OK) == 0) {
565 getusers(PasswdFile,debug);
566 puser++;
567 }
568
569 if(hexclude[0] == '\0')
570 strcpy(hexclude,ExcludeHosts);
43f18f45 571 if(hexclude[0] != '\0') {
936c9905 572 gethexclude(hexclude,debug);
2824ec9b 573 fhost=true;
25697a35
GS
574 }
575
085c1e1f
FM
576 if(ReportType == 0) {
577 ReportType=REPORT_TYPE_TOPUSERS | REPORT_TYPE_TOPSITES | REPORT_TYPE_USERS_SITES |
578 REPORT_TYPE_SITES_USERS | REPORT_TYPE_DATE_TIME | REPORT_TYPE_DENIED |
579 REPORT_TYPE_AUTH_FAILURES | REPORT_TYPE_SITE_USER_TIME_DATE | REPORT_TYPE_DOWNLOADS;
580 }
25697a35
GS
581
582 if(access(ExcludeUsers, R_OK) == 0) {
583 getuexclude(ExcludeUsers,debug);
2824ec9b 584 fuser=true;
25697a35
GS
585 }
586
587 indexonly=0;
588 if(fuser) {
43f18f45 589 if(is_indexonly())
25697a35
GS
590 indexonly++;
591 }
d6e703cc 592 if(strcmp(ExcludeUsers,"indexonly") == 0) indexonly++;
0349fa24 593 if(Index == INDEX_ONLY) indexonly++;
25697a35 594
db5151ed 595 if(MaxElapsed[0] != '\0') max_elapsed=atol(MaxElapsed);
25697a35 596
db5151ed 597 if(outdir[0] == '\0') strcpy(outdir,OutputDir);
25697a35
GS
598 strcat(outdir,"/");
599
d6e703cc 600 if(uagent[0] == '\0') strcpy(uagent,UserAgentLog);
25697a35 601
d6e703cc
FM
602 if(tmp[0] == '\0') strcpy(tmp,TempDir);
603 else strcpy(TempDir,tmp);
4e302ff5
FM
604 /*
605 For historical reasons, the temporary directory is the subdirectory "sarg" of the path
606 provided by the user.
607 */
608 strcat(tmp,"/sarg");
25697a35 609
1c74d05a
FM
610 if (tmp[0]!='\0' && strncmp(outdir,tmp,strlen(tmp))==0) {
611 debuga(_("The output directory \"%s\" must be outside of the temporary directory \"%s\"\n"),outdir,tmp);
612 exit(EXIT_FAILURE);
613 }
614
d6e703cc
FM
615 if(df[0] == '\0') strcpy(df,DateFormat);
616 else strcpy(DateFormat,df);
25697a35
GS
617
618 if(df[0] == '\0') {
619 strcpy(df,"u");
620 strcpy(DateFormat,"u");
621 }
fa6552b0
FM
622 if (df[0]=='w')
623 IndexTree=INDEX_TREE_FILE;
25697a35 624
4af2f8ff 625 if(email[0] == '\0' && OutputEmail[0] != '\0') strcpy(email,OutputEmail);
25697a35 626
e6414a9d 627 if(email[0] != '\0') {
4e302ff5
FM
628 my_mkdir(tmp);
629 strcpy(outdir,tmp);
25697a35 630 strcat(outdir,"/");
d6e703cc 631 }
25697a35 632
4e302ff5
FM
633 if(access(tmp, R_OK) == 0) {
634 unlinkdir(tmp,1);
51465d08 635 }
4e302ff5
FM
636 my_mkdir(tmp);
637 snprintf(denied_unsort,sizeof(denied_unsort),"%s/denied.log.unsort",tmp);
638 snprintf(denied_sort,sizeof(denied_sort),"%s/denied.log",tmp);
639 snprintf(authfail_unsort,sizeof(authfail_unsort),"%s/authfail.log.unsort",tmp);
25697a35
GS
640
641 if(debug) {
10210234
FM
642 debuga(_("Parameters:\n"));
643 debuga(_(" Hostname or IP address (-a) = %s\n"),addr);
644 debuga(_(" Useragent log (-b) = %s\n"),uagent);
645 debuga(_(" Exclude file (-c) = %s\n"),hexclude);
646 debuga(_(" Date from-until (-d) = %s\n"),date);
647 debuga(_(" Email address to send reports (-e) = %s\n"),email);
648 debuga(_(" Config file (-f) = %s\n"),ConfigFile);
25697a35 649 if(strcmp(df,"e") == 0)
10210234 650 debuga(_(" Date format (-g) = Europe (dd/mm/yyyy)\n"));
25697a35 651 if(strcmp(df,"u") == 0)
10210234 652 debuga(_(" Date format (-g) = USA (mm/dd/yyyy)\n"));
25697a35 653 if(strcmp(df,"w") == 0)
10210234
FM
654 debuga(_(" Date format (-g) = Sites & Users (yyyy/ww)\n"));
655 debuga(_(" IP report (-i) = %s\n"),(iprel) ? _("Yes") : _("No"));
0a4e18e1 656 for (iarq=0 ; iarq<NAccessLog ; iarq++)
10210234 657 debuga(_(" Input log (-l) = %s\n"),AccessLog[iarq]);
1f482a8d
FM
658 for (iarq=0 ; iarq<NRedirectorLogs ; iarq++)
659 debuga(_(" Redirector log (-L) = %s\n"),RedirectorLogs[iarq]);
10210234
FM
660 debuga(_(" Resolve IP Address (-n) = %s\n"),(Ip2Name) ? _("Yes") : _("No"));
661 debuga(_(" Output dir (-o) = %s\n"),outdir);
662 debuga(_("Use Ip Address instead of userid (-p) = %s\n"),(UserIp) ? _("Yes") : _("No"));
663 debuga(_(" Accessed site (-s) = %s\n"),site);
58eec3c0 664 debuga(_(" Time (-t) = %s\n"),hm_str);
10210234
FM
665 debuga(_(" User (-u) = %s\n"),us);
666 debuga(_(" Temporary dir (-w) = %s\n"),tmp);
667 debuga(_(" Debug messages (-x) = %s\n"),(debug) ? _("Yes") : _("No"));
668 debuga(_(" Process messages (-z) = %s\n"),(debugz) ? _("Yes") : _("No"));
669 debuga("\n");
25697a35
GS
670 }
671
672 if(debugm) {
3c567423
FM
673 printf(_("Parameters:\n"));
674 printf(_(" Hostname or IP address (-a) = %s\n"),addr);
675 printf(_(" Useragent log (-b) = %s\n"),uagent);
676 printf(_(" Exclude file (-c) = %s\n"),hexclude);
677 printf(_(" Date from-until (-d) = %s\n"),date);
678 printf(_(" Email address to send reports (-e) = %s\n"),email);
679 printf(_(" Config file (-f) = %s\n"),ConfigFile);
25697a35 680 if(strcmp(df,"e") == 0)
3c567423 681 printf(_(" Date format (-g) = Europe (dd/mm/yyyy)\n"));
25697a35 682 if(strcmp(df,"u") == 0)
3c567423 683 printf(_(" Date format (-g) = USA (mm/dd/yyyy)\n"));
25697a35 684 if(strcmp(df,"w") == 0)
3c567423
FM
685 printf(_(" Date format (-g) = Sites & Users (yyyy/ww)\n"));
686 printf(_(" IP report (-i) = %s\n"),(iprel) ? _("Yes") : _("No"));
0a4e18e1 687 for (iarq=0 ; iarq<NAccessLog ; iarq++)
3c567423 688 printf(_(" Input log (-l) = %s\n"),AccessLog[iarq]);
1f482a8d
FM
689 for (iarq=0 ; iarq<NRedirectorLogs ; iarq++)
690 printf(_(" Redirector log (-L) = %s\n"),RedirectorLogs[iarq]);
3c567423
FM
691 printf(_(" Resolve IP Address (-n) = %s\n"),(Ip2Name) ? _("Yes") : _("No"));
692 printf(_(" Output dir (-o) = %s\n"),outdir);
693 printf(_("Use Ip Address instead of userid (-p) = %s\n"),(UserIp) ? _("Yes") : _("No"));
694 printf(_(" Accessed site (-s) = %s\n"),site);
58eec3c0 695 printf(_(" Time (-t) = %s\n"),hm_str);
3c567423
FM
696 printf(_(" User (-u) = %s\n"),us);
697 printf(_(" Temporary dir (-w) = %s\n"),tmp);
698 printf(_(" Debug messages (-x) = %s\n"),(debug) ? _("Yes") : _("No"));
699 printf(_(" Process messages (-z) = %s\n"),(debugz) ? _("Yes") : _("No"));
700 printf(_("sarg version: %s\n"),VERSION);
25697a35
GS
701 }
702
d2fe0c32 703 if(debug)
10210234 704 debuga(_("sarg version: %s\n"),VERSION);
25697a35 705
04a01ed3
FM
706#ifdef ENABLE_DOUBLE_CHECK_DATA
707 debuga(_("Sarg compiled to report warnings if the output is inconsistent\n"));
708#endif
709
f2ec8c75 710 maxopenfiles=MAX_OPEN_USER_FILES;
e112fa1f 711#ifdef HAVE_RLIM_T
eee60323 712 if (Ulimit[0] != '\0') {
e112fa1f 713 struct rlimit rl;
243a9fab
FM
714 long l1, l2;
715 int rc=0;
716
25697a35 717#if defined(RLIMIT_NOFILE)
d6e703cc 718 getrlimit (RLIMIT_NOFILE, &rl);
ed1ac52f 719#elif defined(RLIMIT_OFILE)
d6e703cc 720 getrlimit (RLIMIT_OFILE, &rl);
243a9fab
FM
721#else
722#warning "No rlimit resource for the number of open files"
25697a35 723#endif
d6e703cc
FM
724 l1 = rl.rlim_cur;
725 l2 = rl.rlim_max;
726
eee60323
FM
727 rl.rlim_cur = atol(Ulimit);
728 rl.rlim_max = atol(Ulimit);
243a9fab 729#if defined(RLIMIT_NOFILE)
eee60323 730 rc=setrlimit (RLIMIT_NOFILE, &rl);
243a9fab 731#elif defined(RLIMIT_OFILE)
eee60323 732 rc=setrlimit (RLIMIT_OFILE, &rl);
243a9fab
FM
733#else
734#warning "No rlimit resource for the number of open files"
735#endif
eee60323 736 if(rc == -1) {
f2ec8c75 737 debuga(_("setrlimit error - %s\n"),strerror(errno));
d6e703cc 738 }
25697a35 739
d2fe0c32
FM
740 if(debug)
741 debuga("Maximum file descriptor: cur=%ld max=%ld, changed to cur="RLIM_STRING" max="RLIM_STRING,l1,l2,rl.rlim_cur,rl.rlim_max);
25697a35 742 }
e112fa1f 743#endif
25697a35 744
965c4a6f 745 init_usertab(UserTabFile);
25697a35 746
afaa3b67 747 if ((line=longline_create())==NULL) {
10210234 748 debuga(_("Not enough memory to read a log file\n"));
06b39c87 749 exit(EXIT_FAILURE);
ac422f9b
FM
750 }
751
4e302ff5 752 snprintf(sz_Download_Unsort,sizeof(sz_Download_Unsort),"%s/download.unsort", tmp);
25697a35 753
f84a35a3
FM
754 if(DataFile[0]=='\0') {
755 if((ReportType & REPORT_TYPE_DENIED) != 0) {
756 if((fp_denied=MY_FOPEN(denied_unsort,"w"))==NULL) {
d574e592 757 debuga(_("(log) Cannot open file: %s - %s\n"),denied_unsort,strerror(errno));
06b39c87 758 exit(EXIT_FAILURE);
f84a35a3 759 }
294cb3d7 760 }
294cb3d7 761
085c1e1f 762 if((ReportType & REPORT_TYPE_DENIED) != 0 || (ReportType & REPORT_TYPE_AUTH_FAILURES) != 0) {
f84a35a3 763 if((fp_authfail=MY_FOPEN(authfail_unsort,"w"))==NULL) {
d574e592 764 debuga(_("(log) Cannot open file: %s - %s\n"),authfail_unsort,strerror(errno));
06b39c87 765 exit(EXIT_FAILURE);
294cb3d7
FM
766 }
767 }
768 }
769
0a4e18e1
FM
770 for (iarq=0 ; iarq<NAccessLog ; iarq++) {
771 strcpy(arq,AccessLog[iarq]);
25697a35 772
936c9905 773 strcpy(arqtt,arq);
d6e703cc 774
58d7d8fe
FM
775 if(strcmp(arq,"-")==0) {
776 if(debug)
10210234 777 debuga(_("Reading access log file: from stdin\n"));
58d7d8fe 778 fp_in=stdin;
2824ec9b 779 from_stdin=true;
58d7d8fe 780 } else {
c6a3ce24
FM
781 if (date[0]!='\0') {
782 if (stat(arq,&logstat)!=0) {
783 debuga(_("Cannot get the modification time of input log file %s (%s). Processing it anyway\n"),arq,strerror(errno));
784 } else {
785 struct tm *logtime=localtime(&logstat.st_mtime);
786 if ((logtime->tm_year+1900)*10000+(logtime->tm_mon+1)*100+logtime->tm_mday<dfrom) {
787 debuga(_("Ignoring old log file %s\n"),arq);
788 continue;
789 }
790 }
791 }
d2855b39
FM
792 fp_in=decomp(arq,&from_pipe);
793 if(fp_in==NULL) {
d574e592 794 debuga(_("(log) Cannot open log file: %s - %s\n"),arq,strerror(errno));
06b39c87 795 exit(EXIT_FAILURE);
936c9905 796 }
d2855b39 797 if(debug) debuga(_("Reading access log file: %s\n"),arq);
2824ec9b 798 from_stdin=false;
25697a35 799 }
58d7d8fe 800 ilf=ILF_Unknown;
2824ec9b 801 download_flag=false;
ac422f9b 802 // pre-read the file only if we have to show stats
d2855b39 803 if(ShowReadStatistics && !from_stdin && !from_pipe) {
ac422f9b 804 size_t nread,i;
2824ec9b 805 bool skipcr=false;
afaa3b67 806 char tmp4[MAXLEN];
ac422f9b 807
7c0b0078
FM
808 recs1=0UL;
809 recs2=0UL;
25697a35 810
afaa3b67 811 while ((nread=fread(tmp4,1,sizeof(tmp4),fp_in))>0) {
ac422f9b
FM
812 for (i=0 ; i<nread ; i++)
813 if (skipcr) {
afaa3b67 814 if (tmp4[i]!='\n' && tmp4[i]!='\r') {
2824ec9b 815 skipcr=false;
ac422f9b
FM
816 }
817 } else {
afaa3b67 818 if (tmp4[i]=='\n' || tmp4[i]=='\r') {
2824ec9b 819 skipcr=true;
ac422f9b
FM
820 recs1++;
821 }
822 }
823 }
936c9905 824 rewind(fp_in);
fcdc0918
FM
825 printf(_("SARG: Records in file: %lu, reading: %3.2f%%"),recs1,(float) 0);
826 putchar('\r');
936c9905
FM
827 fflush( stdout ) ;
828 }
829
afaa3b67 830 longline_reset(line);
ac422f9b 831
afaa3b67 832 while ((linebuf=longline_read(fp_in,line))!=NULL) {
ac422f9b 833 blen=strlen(linebuf);
58d7d8fe
FM
834
835 if (ilf==ILF_Unknown) {
ac422f9b
FM
836 if(strncmp(linebuf,"#Software: Mic",14) == 0) {
837 fixendofline(linebuf);
f8ed23e3 838 if (debug)
10210234 839 debuga(_("Log is from Microsoft ISA: %s\n"),linebuf);
58d7d8fe
FM
840 ilf=ILF_Isa;
841 ilf_count[ilf]++;
842 continue;
843 }
844
ac422f9b 845 if(strncmp(linebuf,"*** SARG Log ***",16) == 0) {
fa6552b0 846 if (getperiod_fromsarglog(arqtt,&period)<0) {
10210234 847 debuga(_("The name of the file is invalid: %s\n"),arq);
06b39c87 848 exit(EXIT_FAILURE);
58d7d8fe 849 }
58d7d8fe
FM
850 ilf=ILF_Sarg;
851 ilf_count[ilf]++;
852 continue;
853 }
854 }
855
ff8d5836 856 if(!fp_log && strcmp(ParsedOutputLog, "no") != 0 && ilf!=ILF_Sarg) {
58d7d8fe 857 if(access(ParsedOutputLog,R_OK) != 0) {
ff8d5836 858 my_mkdir(ParsedOutputLog);
58d7d8fe
FM
859 }
860 sprintf(arq_log,"%s/sarg_temp.log",ParsedOutputLog);
861 if((fp_log=MY_FOPEN(arq_log,"w"))==NULL) {
d574e592 862 debuga(_("(log) Cannot open log file: %s - %s\n"),arq_log,strerror(errno));
06b39c87 863 exit(EXIT_FAILURE);
58d7d8fe
FM
864 }
865 fputs("*** SARG Log ***\n",fp_log);
866 }
867
936c9905 868 recs2++;
43273c35 869 if( ShowReadStatistics && !from_stdin && !from_pipe && --OutputNonZero<=0) {
2824ec9b 870 double perc = recs2 * 100. / recs1 ;
43273c35 871 printf(_("SARG: Records in file: %lu, reading: %3.2lf%%"),recs2,perc);
fcdc0918 872 putchar('\r');
25697a35
GS
873 fflush (stdout);
874 OutputNonZero = REPORT_EVERY_X_LINES ;
936c9905 875 }
ab950637 876 if(blen < 58) continue;
ac422f9b
FM
877 if(strstr(linebuf,"HTTP/0.0") != 0) continue;
878 if(strstr(linebuf,"logfile turned over") != 0) continue;
879 if(linebuf[0] == ' ') continue;
936c9905 880
936c9905 881 // exclude_string
9c7c6346 882 if(ExcludeString[0] != '\0') {
2824ec9b 883 bool exstring=false;
9c7c6346
FM
884 getword_start(&gwarea,ExcludeString);
885 while(strchr(gwarea.current,':') != 0) {
886 if (getword_multisep(val1,sizeof(val1),&gwarea,':')<0) {
10210234 887 debuga(_("Maybe you have a broken record or garbage in your exclusion string\n"));
06b39c87 888 exit(EXIT_FAILURE);
936c9905 889 }
2824ec9b
FM
890 if((str=(char *) strstr(linebuf,val1)) != (char *) NULL ) {
891 exstring=true;
892 break;
893 }
936c9905 894 }
2824ec9b
FM
895 if(!exstring && (str=(char *) strstr(linebuf,gwarea.current)) != (char *) NULL )
896 exstring=true;
3376a277 897 if(exstring) continue;
936c9905 898 }
936c9905 899
936c9905 900 totregsl++;
936c9905 901 if(debugm)
ac422f9b 902 printf("BUF=%s\n",linebuf);
936c9905 903
fa6552b0 904 t=NULL;
58d7d8fe 905 if (ilf==ILF_Squid || ilf==ILF_Common || ilf==ILF_Unknown) {
ac422f9b 906 getword_start(&gwarea,linebuf);
9c7c6346 907 if (getword(data,sizeof(data),&gwarea,' ')<0) {
d574e592 908 debuga(_("Maybe you have a broken time in your access.log file\n"));
06b39c87 909 exit(EXIT_FAILURE);
936c9905 910 }
fa6552b0
FM
911 if((str=(char *) strchr(data, '.')) != (char *) NULL && (str=(char *) strchr(str+1, '.')) != (char *) NULL ) {
912 strcpy(ip,data);
913 strcpy(elap,"0");
914 if(squid24) {
915 if (getword(user,sizeof(user),&gwarea,' ')<0 || getword_skip(255,&gwarea,' ')<0) {
9f70c14e 916 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
06b39c87 917 exit(EXIT_FAILURE);
ac422f9b 918 }
fa6552b0
FM
919 } else {
920 if (getword_skip(255,&gwarea,' ')<0 || getword(user,sizeof(user),&gwarea,' ')<0) {
2824ec9b
FM
921 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
922 exit(EXIT_FAILURE);
923 }
fa6552b0
FM
924 }
925 if (getword(data,sizeof(data),&gwarea,']')<0 || getword_skip(MAXLEN,&gwarea,'"')<0 ||
926 getword(fun,sizeof(fun),&gwarea,' ')<0) {
927 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
928 exit(EXIT_FAILURE);
929 }
930 if (getword_ptr(linebuf,&url,&gwarea,' ')<0) {
931 debuga(_("Maybe you have a broken url in your %s file\n"),arq);
932 exit(EXIT_FAILURE);
933 }
934 if (getword_skip(MAXLEN,&gwarea,' ')<0) {
935 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
936 exit(EXIT_FAILURE);
937 }
938 if (getword(code2,sizeof(code2),&gwarea,' ')<0) {
939 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
940 exit(EXIT_FAILURE);
941 }
942 if (getword(tam,sizeof(tam),&gwarea,' ')<0) {
943 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
944 exit(EXIT_FAILURE);
945 }
946 if((str=(char *) strchr(gwarea.current, ' ')) != (char *) NULL ) {
947 if (getword(code,sizeof(code),&gwarea,' ')<0) {
2824ec9b
FM
948 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
949 exit(EXIT_FAILURE);
950 }
fa6552b0
FM
951 } else {
952 if (getword(code,sizeof(code),&gwarea,'\0')<0) {
9f70c14e 953 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
06b39c87 954 exit(EXIT_FAILURE);
936c9905 955 }
fa6552b0
FM
956 }
957
958 if ((str = strchr(code, ':')) != NULL)
959 *str = '/';
936c9905 960
fa6552b0
FM
961 if(strcmp(tam,"\0") == 0)
962 strcpy(tam,"0");
936c9905 963
fa6552b0
FM
964 ilf=ILF_Common;
965 ilf_count[ilf]++;
936c9905 966
fa6552b0
FM
967 getword_start(&gwarea,data+1);
968 if (getword_multisep(data,sizeof(data),&gwarea,':')<0){
969 debuga(_("Maybe you have a broken date in your %s file\n"),arq);
970 exit(EXIT_FAILURE);
936c9905 971 }
fa6552b0
FM
972 if (getword_multisep(hora,sizeof(hora),&gwarea,' ')<0){
973 debuga(_("Maybe you have a broken date in your %s file\n"),arq);
974 exit(EXIT_FAILURE);
975 }
976 getword_start(&gwarea,data);
977 if (getword_atoll(&iday,&gwarea,'/')<0){
978 debuga(_("Maybe you have a broken date in your %s file\n"),arq);
979 exit(EXIT_FAILURE);
980 }
981 if (getword(mes,sizeof(mes),&gwarea,'/')<0){
982 debuga(_("Maybe you have a broken date in your %s file\n"),arq);
983 exit(EXIT_FAILURE);
984 }
985 if (getword_atoll(&iyear,&gwarea,'/')<0){
986 debuga(_("Maybe you have a broken date in your %s file\n"),arq);
987 exit(EXIT_FAILURE);
988 }
989
990 imonth=month2num(mes)+1;
991 idata=builddia(iday,imonth,iyear);
992 computedate(iyear,imonth,iday,&tt);
993 t=&tt;
936c9905
FM
994 }
995
58d7d8fe 996 if(ilf==ILF_Unknown || ilf==ILF_Squid) {
9c7c6346 997 if (getword(elap,sizeof(elap),&gwarea,' ')<0) {
d574e592 998 debuga(_("Maybe you have a broken elapsed time in your %s file\n"),arq);
06b39c87 999 exit(EXIT_FAILURE);
936c9905 1000 }
9c7c6346
FM
1001 while(strcmp(elap,"") == 0 && gwarea.current[0] != '\0')
1002 if (getword(elap,sizeof(elap),&gwarea,' ')<0) {
d574e592 1003 debuga(_("Maybe you have a broken elapsed time in your %s file\n"),arq);
06b39c87 1004 exit(EXIT_FAILURE);
936c9905
FM
1005 }
1006 if(strlen(elap) < 1) continue;
9c7c6346 1007 if (getword(ip,sizeof(ip),&gwarea,' ')<0){
d574e592 1008 debuga(_("Maybe you have a broken client IP address in your %s file\n"),arq);
06b39c87 1009 exit(EXIT_FAILURE);
936c9905 1010 }
9c7c6346 1011 if (getword(code,sizeof(code),&gwarea,' ')<0){
d574e592 1012 debuga(_("Maybe you have a broken result code in your %s file\n"),arq);
06b39c87 1013 exit(EXIT_FAILURE);
936c9905 1014 }
9c7c6346 1015 if (getword(tam,sizeof(tam),&gwarea,' ')<0){
d574e592 1016 debuga(_("Maybe you have a broken amount of data in your %s file\n"),arq);
06b39c87 1017 exit(EXIT_FAILURE);
936c9905 1018 }
9c7c6346 1019 if (getword(fun,sizeof(fun),&gwarea,' ')<0){
d574e592 1020 debuga(_("Maybe you have a broken request method in your %s file\n"),arq);
06b39c87 1021 exit(EXIT_FAILURE);
936c9905 1022 }
e5b2c6f0 1023 if (getword_ptr(linebuf,&url,&gwarea,' ')<0){
10210234 1024 debuga(_("Maybe you have a broken url in your %s file\n"),arq);
06b39c87 1025 exit(EXIT_FAILURE);
936c9905 1026 }
9c7c6346 1027 if (getword(user,sizeof(user),&gwarea,' ')<0){
d574e592 1028 debuga(_("Maybe you have a broken user ID in your %s file\n"),arq);
06b39c87 1029 exit(EXIT_FAILURE);
936c9905 1030 }
58d7d8fe
FM
1031 ilf=ILF_Squid;
1032 ilf_count[ilf]++;
fa6552b0
FM
1033
1034 tnum=atoi(data);
1035 t=localtime(&tnum);
1036 if (t == NULL) {
1037 debuga(_("Cannot convert the timestamp from the squid log file\n"));
1038 exit(EXIT_FAILURE);
1039 }
1040
1041 strftime(tbuf2, sizeof(tbuf2), "%H%M", t);
1042
1043 idata=(t->tm_year+1900)*10000+(t->tm_mon+1)*100+t->tm_mday;
936c9905 1044 }
58d7d8fe
FM
1045 }
1046 if (ilf==ILF_Sarg) {
ac422f9b 1047 getword_start(&gwarea,linebuf);
ff8d5836 1048 if (getword(data,sizeof(data),&gwarea,'\t')<0){
9f70c14e 1049 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
06b39c87 1050 exit(EXIT_FAILURE);
936c9905 1051 }
ff8d5836 1052 if (getword(hora,sizeof(hora),&gwarea,'\t')<0) {
9f70c14e 1053 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
06b39c87 1054 exit(EXIT_FAILURE);
936c9905 1055 }
ff8d5836 1056 if (getword(user,sizeof(user),&gwarea,'\t')<0) {
9f70c14e 1057 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
06b39c87 1058 exit(EXIT_FAILURE);
936c9905 1059 }
ff8d5836 1060 if (getword(ip,sizeof(ip),&gwarea,'\t')<0) {
9f70c14e 1061 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
06b39c87 1062 exit(EXIT_FAILURE);
936c9905 1063 }
e5b2c6f0 1064 if (getword_ptr(linebuf,&url,&gwarea,'\t')<0){
10210234 1065 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
06b39c87 1066 exit(EXIT_FAILURE);
936c9905 1067 }
ff8d5836 1068 if (getword(tam,sizeof(tam),&gwarea,'\t')<0){
9f70c14e 1069 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
06b39c87 1070 exit(EXIT_FAILURE);
936c9905 1071 }
ff8d5836 1072 if (getword(code,sizeof(code),&gwarea,'\t')<0){
9f70c14e 1073 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
06b39c87 1074 exit(EXIT_FAILURE);
936c9905 1075 }
ff8d5836 1076 if (getword(elap,sizeof(elap),&gwarea,'\t')<0){
9f70c14e 1077 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
06b39c87 1078 exit(EXIT_FAILURE);
936c9905 1079 }
ff8d5836 1080 if (getword(smartfilter,sizeof(smartfilter),&gwarea,'\0')<0){
9f70c14e 1081 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
06b39c87 1082 exit(EXIT_FAILURE);
936c9905 1083 }
fa6552b0 1084 getword_start(&gwarea,data);
324ba7f3
FM
1085 if (getword_atoll(&iday,&gwarea,'/')<0 || iday<1 || iday>31){
1086 debuga(_("Maybe you have a broken date in your %s file\n"),arq);
1087 exit(EXIT_FAILURE);
1088 }
1089 if (getword_atoll(&imonth,&gwarea,'/')<0 || imonth<1 || imonth>12){
1090 debuga(_("Maybe you have a broken date in your %s file\n"),arq);
1091 exit(EXIT_FAILURE);
fa6552b0
FM
1092 }
1093 if (getword_atoll(&iyear,&gwarea,'\0')<0){
1094 debuga(_("Maybe you have a broken date in your %s file\n"),arq);
1095 exit(EXIT_FAILURE);
1096 }
1097 idata=builddia(iday,imonth,iyear);
1098 computedate(iyear,imonth,iday,&tt);
1099 t=&tt;
58d7d8fe
FM
1100 }
1101 if (ilf==ILF_Isa) {
ac422f9b 1102 if (linebuf[0] == '#') {
076cbab8
FM
1103 int ncols,cols[ISACOL_Last];
1104
ac422f9b
FM
1105 fixendofline(linebuf);
1106 getword_start(&gwarea,linebuf);
076cbab8 1107 // remove the #Fields: column at the beginning of the line
9c7c6346 1108 if (getword_skip(1000,&gwarea,' ')<0){
10210234 1109 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
06b39c87 1110 exit(EXIT_FAILURE);
936c9905 1111 }
076cbab8
FM
1112 for (ncols=0 ; ncols<ISACOL_Last ; ncols++) cols[ncols]=-1;
1113 ncols=0;
9c7c6346
FM
1114 while(gwarea.current[0] != '\0') {
1115 if (getword(val1,sizeof(val1),&gwarea,'\t')<0){
9f70c14e 1116 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
06b39c87 1117 exit(EXIT_FAILURE);
936c9905 1118 }
076cbab8
FM
1119 if(strcmp(val1,"c-ip") == 0) cols[ISACOL_Ip]=ncols;
1120 if(strcmp(val1,"cs-username") == 0) cols[ISACOL_UserName]=ncols;
1121 if(strcmp(val1,"date") == 0) cols[ISACOL_Date]=ncols;
1122 if(strcmp(val1,"time") == 0) cols[ISACOL_Time]=ncols;
1123 if(strcmp(val1,"time-taken") == 0) cols[ISACOL_TimeTaken]=ncols;
1124 if(strcmp(val1,"sc-bytes") == 0) cols[ISACOL_Bytes]=ncols;
1125 if(strcmp(val1,"cs-uri") == 0) cols[ISACOL_Uri]=ncols;
1126 if(strcmp(val1,"sc-status") == 0) cols[ISACOL_Status]=ncols;
1127 ncols++;
936c9905 1128 }
076cbab8
FM
1129 if (cols[ISACOL_Ip]>=0) {
1130 isa_ncols=ncols;
3376a277
FM
1131 for (ncols=0 ; ncols<ISACOL_Last ; ncols++)
1132 isa_cols[ncols]=cols[ncols];
936c9905 1133 }
076cbab8 1134 continue;
936c9905 1135 }
076cbab8 1136 if (!isa_ncols) continue;
ac422f9b 1137 getword_start(&gwarea,linebuf);
076cbab8 1138 for (x=0 ; x<isa_ncols ; x++) {
e5b2c6f0 1139 if (getword_ptr(linebuf,&str,&gwarea,'\t')<0) {
10210234 1140 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
06b39c87 1141 exit(EXIT_FAILURE);
936c9905 1142 }
076cbab8 1143 if (x==isa_cols[ISACOL_Ip]) {
e5b2c6f0 1144 if (strlen(str)>=sizeof(ip)) {
10210234 1145 debuga(_("Maybe you have a broken IP in your %s file\n"),arq);
06b39c87 1146 exit(EXIT_FAILURE);
5160b452 1147 }
e5b2c6f0 1148 strcpy(ip,str);
076cbab8 1149 } else if (x==isa_cols[ISACOL_UserName]) {
e5b2c6f0 1150 if (strlen(str)>=sizeof(user)) {
d574e592 1151 debuga(_("Maybe you have a broken user ID in your %s file\n"),arq);
06b39c87 1152 exit(EXIT_FAILURE);
5160b452 1153 }
e5b2c6f0 1154 strcpy(user,str);
076cbab8 1155 } else if (x==isa_cols[ISACOL_Date]) {
e5b2c6f0 1156 if (strlen(str)>=sizeof(data)) {
10210234 1157 debuga(_("Maybe you have a broken date in your %s file\n"),arq);
06b39c87 1158 exit(EXIT_FAILURE);
5160b452 1159 }
e5b2c6f0 1160 strcpy(data,str);
076cbab8 1161 } else if (x==isa_cols[ISACOL_Time]) {
e5b2c6f0 1162 if (strlen(str)>=sizeof(hora)) {
10210234 1163 debuga(_("Maybe you have a broken time in your %s file\n"),arq);
06b39c87 1164 exit(EXIT_FAILURE);
5160b452 1165 }
e5b2c6f0 1166 strcpy(hora,str);
076cbab8 1167 } else if (x==isa_cols[ISACOL_TimeTaken]) {
e5b2c6f0 1168 if (strlen(str)>=sizeof(elap)) {
10210234 1169 debuga(_("Maybe you have a broken download duration in your %s file\n"),arq);
06b39c87 1170 exit(EXIT_FAILURE);
5160b452 1171 }
e5b2c6f0 1172 strcpy(elap,str);
076cbab8 1173 } else if (x==isa_cols[ISACOL_Bytes]) {
e5b2c6f0 1174 if (strlen(str)>=sizeof(tam)) {
10210234 1175 debuga(_("Maybe you have a broken download size in your %s file\n"),arq);
06b39c87 1176 exit(EXIT_FAILURE);
5160b452 1177 }
e5b2c6f0 1178 strcpy(tam,str);
076cbab8 1179 } else if (x==isa_cols[ISACOL_Uri]) {
e5b2c6f0 1180 url=str;
076cbab8 1181 } else if (x==isa_cols[ISACOL_Status]) {
e5b2c6f0 1182 if (strlen(str)>=sizeof(code)) {
10210234 1183 debuga(_("Maybe you have a broken access code in your %s file\n"),arq);
06b39c87 1184 exit(EXIT_FAILURE);
5160b452 1185 }
e5b2c6f0 1186 strcpy(code,str);
936c9905
FM
1187 }
1188 }
1189
1190 if(strcmp(code,"401") == 0 || strcmp(code,"403") == 0 || strcmp(code,"407") == 0) {
1191 sprintf(val1,"DENIED/%s",code);
1192 strcpy(code,val1);
1193 }
9c7c6346 1194 getword_start(&gwarea,data);
fa6552b0
FM
1195 if (getword_atoll(&iyear,&gwarea,'-')<0){
1196 debuga(_("Maybe you have a broken year in your %s file\n"),arq);
06b39c87 1197 exit(EXIT_FAILURE);
936c9905 1198 }
fa6552b0
FM
1199 if (getword_atoll(&imonth,&gwarea,'-')<0){
1200 debuga(_("Maybe you have a broken month in your %s file\n"),arq);
06b39c87 1201 exit(EXIT_FAILURE);
936c9905 1202 }
fa6552b0
FM
1203 if (getword_atoll(&iday,&gwarea,'\0')<0){
1204 debuga(_("Maybe you have a broken day in your %s file\n"),arq);
06b39c87 1205 exit(EXIT_FAILURE);
936c9905 1206 }
936c9905 1207
fa6552b0
FM
1208 idata=builddia(iday,imonth,iyear);
1209 computedate(iyear,imonth,iday,&tt);
1210 t=&tt;
1211 }
1212 if (t==NULL) {
1213 debuga(_("Unknown input log file format\n"));
1214 break;
936c9905
FM
1215 }
1216
324ba7f3 1217 strftime(dia, sizeof(dia), "%d/%m/%Y", t);
43cc1649
FM
1218 snprintf(hora,sizeof(hora),"%02d:%02d:%02d",t->tm_hour,t->tm_min,t->tm_sec);
1219
936c9905
FM
1220 if(debugm)
1221 printf("DATE=%s IDATA=%d DFROM=%d DUNTIL=%d\n",date,idata,dfrom,duntil);
1222
0445cbf9
FM
1223 if(date[0] != '\0'){
1224 if(idata < dfrom || idata > duntil) continue;
1225 }
809a7534 1226
9426efec 1227 // Record only hours usage which is required
9f70c14e
FM
1228 if (t) {
1229 if( bsearch( &( t -> tm_wday ), weekdays.list, weekdays.len,
1230 sizeof( int ), compar ) == NULL )
1231 continue;
9426efec 1232
9f70c14e
FM
1233 if( bsearch( &( t -> tm_hour ), hours.list, hours.len,
1234 sizeof( int ), compar ) == NULL )
1235 continue;
1236 }
9426efec
FM
1237
1238
809a7534
FM
1239 if(strlen(user) > MAX_USER_LEN) {
1240 if (debugm) printf(_("User ID too long: %s\n"),user);
1241 totregsx++;
1242 continue;
1243 }
1244
1245 // include_users
1246 if(IncludeUsers[0] != '\0') {
1247 sprintf(val1,":%s:",user);
1248 if((str=(char *) strstr(IncludeUsers,val1)) == (char *) NULL )
1249 continue;
1250 }
1251
1252 if(vercode(code)) {
1253 if (debugm) printf(_("Excluded code: %s\n"),code);
1254 totregsx++;
1255 continue;
1256 }
1257
1258 if(testvaliduserchar(user))
1259 continue;
1260
1261#if 0
1262 if((str = strstr(user,"%20")) != NULL) {
1263 /*
1264 This is a patch introduced to solve bug #1624251 reported at sourceforge but
1265 the side effect is to truncate the name at the first space and merge the reports
1266 of people whose name is identical up to the first space.
1267
1268 The old code used to truncate the user name at the first % if a %20 was
1269 found anywhere in the string. That means the string could be truncated
1270 at the wrong place if another % occured before the %20. This new code should
1271 avoid that problem and only truncate at the space. There is no bug
1272 report indicating that anybody noticed this.
1273 */
1274 *str='\0';
1275 }
1276
1277 /*
1278 Code prior to 2.2.7 used to replace any %xx by a dot as long as a %5c was
1279 found in the user name.
1280 */
1281 while((str = strstr(user,"%5c")) != NULL) {
1282 *str='.';
1283 for (x=3 ; str[x] ; x++) str[x-2]=str[x];
1284 }
1285#endif
1286
1287 urly=url;
1288
1289 if(ilf!=ILF_Sarg) {
1290 /*
1291 The full URL is not saved in sarg log. There is no point in testing the URL to detect
1292 a downloaded file.
1293 */
1294 download_flag=is_download_suffix(url);
1295 if (download_flag) {
1296 download_url=url;
1297 download_count++;
1298 }
c36c7384 1299 } else
2824ec9b 1300 download_flag=false;
809a7534
FM
1301
1302 // remove any protocol:// at the beginning of the URL
1303 if ((str = strchr(url,'/')) != NULL && str[1] == '/') {
1304 int i;
1305
1306 str+=2;
1307 for (i=0 ; str[i] ; i++)
1308 url[i]=str[i];
1309 url[i]='\0';
1310 }
1311
1312 if(!LongUrl) {
f84a35a3 1313 url_hostname(url,hostname,sizeof(hostname));
809a7534
FM
1314 url=hostname;
1315 }
1316
0445cbf9
FM
1317 if(url[0] == '\0') continue;
1318
f2ec8c75
FM
1319 if(addr[0] != '\0'){
1320 if(strcmp(addr,ip)!=0) continue;
1321 }
1322 if(fhost) {
1323 if(!vhexclude(url)) {
1324 if (debugm) printf(_("Excluded site: %s\n"),url);
1325 totregsx++;
1326 continue;
936c9905
FM
1327 }
1328 }
25697a35 1329
58eec3c0
FM
1330 if(hm >= 0 && hmf >= 0) {
1331 hmr=t->tm_hour*100+t->tm_min;
1332 if(hmr < hm || hmr > hmf) continue;
936c9905 1333 }
f2ec8c75
FM
1334
1335 if(site[0] != '\0'){
1336 if(strstr(url,site)==0) continue;
936c9905
FM
1337 }
1338
8bb4048f 1339 if(UserIp) {
936c9905 1340 strcpy(user,ip);
8bb4048f 1341 id_is_ip=true;
f2ec8c75 1342 } else {
8bb4048f 1343 id_is_ip=false;
f2ec8c75
FM
1344 if(strcmp(user,"-") == 0 || strcmp(user," ") == 0 || strcmp(user,"") == 0) {
1345 if(RecordsWithoutUser == RECORDWITHOUTUSER_IP) {
1346 strcpy(user,ip);
8bb4048f 1347 id_is_ip=true;
f2ec8c75
FM
1348 }
1349 if(RecordsWithoutUser == RECORDWITHOUTUSER_IGNORE)
1350 continue;
1351 if(RecordsWithoutUser == RECORDWITHOUTUSER_EVERYBODY)
1352 strcpy(user,"everybody");
1353 } else {
1354 strlow(user);
1355 if(NtlmUserFormat == NTLMUSERFORMAT_USER) {
1356 if((str = strchr(user,'_')) != 0) {
1357 strcpy(warea,str+1);
1358 strcpy(user,warea);
1359 }
1360 if((str = strchr(user,'+')) != 0) {
1361 strcpy(warea,str+1);
1362 strcpy(user,warea);
1363 }
1364 }
1365 }
120d768c 1366 }
936c9905 1367
f2ec8c75
FM
1368 if(us[0] != '\0'){
1369 if(strcmp(user,us)!=0) continue;
936c9905
FM
1370 }
1371
1372 if(puser) {
1373 sprintf(wuser,":%s:",user);
1374 if(strstr(userfile, wuser) == 0)
1375 continue;
1376 }
1377
f2ec8c75
FM
1378 if(fuser) {
1379 if(!vuexclude(user)) {
1380 if (debugm) printf(_("Excluded user: %s\n"),user);
1381 totregsx++;
1382 continue;
936c9905
FM
1383 }
1384 }
1385
f2ec8c75
FM
1386 if(strcmp(user,"-") ==0 || strcmp(user," ") ==0 || strcmp(user,"") ==0 || strcmp(user,":") ==0)
1387 continue;
936c9905 1388
f2ec8c75 1389 if(max_elapsed) {
936c9905
FM
1390 if(atol(elap)>max_elapsed) {
1391 elap[0]='0';
1392 elap[1]='\0';
1393 }
1394 }
1395
f2ec8c75
FM
1396 if((str=(char *) strstr(linebuf, "[SmartFilter:")) != (char *) NULL ) {
1397 fixendofline(str);
1398 sprintf(smartfilter,"\"%s\"",str+1);
1399 } else sprintf(smartfilter,"\"\"");
1400
1401 nopen=0;
1402 prev_ufile=NULL;
1403 for (ufile=first_user_file ; ufile && strcmp(user,ufile->user->id)!=0 ; ufile=ufile->next) {
1404 prev_ufile=ufile;
1405 if (ufile->file) nopen++;
1406 }
1407 if (!ufile) {
1408 ufile=malloc(sizeof(*ufile));
1409 if (!ufile) {
10210234 1410 debuga(_("Not enough memory to store the user %s\n"),user);
06b39c87 1411 exit(EXIT_FAILURE);
f2ec8c75
FM
1412 }
1413 memset(ufile,0,sizeof(*ufile));
1414 ufile->next=first_user_file;
1415 first_user_file=ufile;
1416 uinfo=userinfo_create(user);
1417 ufile->user=uinfo;
1418 uinfo->id_is_ip=id_is_ip;
1419 } else {
1420 if (prev_ufile) {
1421 prev_ufile->next=ufile->next;
1422 ufile->next=first_user_file;
1423 first_user_file=ufile;
1424 }
1425 }
1426
1427 if (ufile->file==NULL) {
1428 if (nopen>=maxopenfiles) {
1429 x=0;
1430 for (ufile1=first_user_file ; ufile1 ; ufile1=ufile1->next) {
1431 if (ufile1->file!=NULL) {
1432 if (x>=maxopenfiles) {
43cc1649
FM
1433 if (fclose(ufile1->file)==EOF) {
1434 debuga(_("Failed to close the log file of user %s - %s\n"),ufile1->user->id,strerror(errno));
1435 exit(EXIT_FAILURE);
1436 }
f2ec8c75
FM
1437 ufile1->file=NULL;
1438 }
1439 x++;
25697a35 1440 }
936c9905 1441 }
f2ec8c75 1442 }
4e302ff5
FM
1443 if (snprintf (tmp3, sizeof(tmp3), "%s/%s.unsort", tmp, ufile->user->filename)>=sizeof(tmp3)) {
1444 debuga(_("Temporary user file name too long: %s/%s.unsort\n"), tmp, ufile->user->filename);
06b39c87 1445 exit(EXIT_FAILURE);
0445cbf9 1446 }
f2ec8c75 1447 if ((ufile->file = MY_FOPEN (tmp3, "a")) == NULL) {
9426efec 1448 debuga(_("(log) Cannot open temporary file: %s - %s\n"), tmp3, strerror(errno));
f2ec8c75
FM
1449 exit (1);
1450 }
1451 }
ac422f9b 1452
f2ec8c75
FM
1453 /*if ( strcmp ( user , sz_Last_User ) != 0 ) {
1454 if ( fp_Write_User )
1455 fclose( fp_Write_User ) ;
4e302ff5 1456 sprintf (tmp3, "%s/%s.unsort", tmp, user);
25697a35 1457
f2ec8c75 1458 if ((fp_Write_User = MY_FOPEN (tmp3, "a")) == NULL) {
c36c7384 1459 fprintf (stderr, "%s: (log) %s: %s - %s\n", argv[0], _("Cannot open temporary file"), tmp3, strerror(errno));
f2ec8c75
FM
1460 exit (1);
1461 }
1462 strcpy( sz_Last_User , user ) ;
1463 }*/
43cc1649
FM
1464 if (fprintf(ufile->file, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",dia,hora,ip,url,tam,code,elap,smartfilter)<=0) {
1465 debuga(_("Write error in the log file of user %s\n"),user);
1466 exit(EXIT_FAILURE);
1467 }
25697a35 1468
f2ec8c75
FM
1469 if(fp_log && ilf!=ILF_Sarg)
1470 fprintf(fp_log, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",dia,hora,user,ip,url,tam,code,elap,smartfilter);
25697a35 1471
f2ec8c75 1472 totregsg++;
936c9905 1473
f84a35a3 1474 if(!dataonly && download_flag && download_url && strstr(code,"DENIED") == 0) {
f2ec8c75 1475 ndownload = 1;
936c9905 1476
f2ec8c75
FM
1477 if ( ! fp_Download_Unsort ) {
1478 if ((fp_Download_Unsort = MY_FOPEN ( sz_Download_Unsort, "a")) == NULL) {
f2e7644d 1479 debuga(_("(log) Cannot open temporary file: %s - %s\n"),sz_Download_Unsort, strerror(errno));
f2ec8c75 1480 exit (1);
936c9905
FM
1481 }
1482 }
f2ec8c75
FM
1483 fprintf(fp_Download_Unsort,"%s\t%s\t%s\t%s\t%s\n",dia,hora,user,ip,download_url);
1484 }
1485
1486 if((ReportType & REPORT_TYPE_DENIED) != 0 || (ReportType & REPORT_TYPE_AUTH_FAILURES) != 0) {
f84a35a3 1487 if(fp_denied && strstr(code,"DENIED/403") != 0) {
f2ec8c75
FM
1488 fprintf(fp_denied, "%s\t%s\t%s\t%s\t%s\n",dia,hora,user,ip,urly);
1489 denied_count++;
1490 }
f84a35a3
FM
1491 if(fp_authfail && (strstr(code,"DENIED/401") != 0 || strstr(code,"DENIED/407") != 0)) {
1492 fprintf(fp_authfail, "%s\t%s\t%s\t%s\t%s\n",dia,hora,user,ip,urly);
f2ec8c75
FM
1493 authfail_count++;
1494 }
1495 }
936c9905 1496
fa6552b0
FM
1497 if (ilf!=ILF_Sarg) {
1498 if(!totper || idata<mindate){
1499 mindate=idata;
1500 memcpy(&period.start,t,sizeof(*t));
1501 strcpy(start_hour,tbuf2);
1502 }
1503 if (!totper || idata>maxdate) {
1504 maxdate=idata;
1505 memcpy(&period.end,t,sizeof(*t));
936c9905 1506 }
fa6552b0 1507 totper=true;
936c9905 1508 }
f2ec8c75
FM
1509
1510 if(debugm){
1511 printf("IP=\t%s\n",ip);
1512 printf("USER=\t%s\n",user);
1513 printf("ELAP=\t%s\n",elap);
1514 printf("DATE=\t%s\n",dia);
1515 printf("TIME=\t%s\n",hora);
1516 printf("FUNC=\t%s\n",fun);
1517 printf("URL=\t%s\n",url);
1518 printf("CODE=\t%s\n",code);
1519 printf("LEN=\t%s\n",tam);
1520 }
25697a35 1521 }
58d7d8fe 1522 if (!from_stdin) {
d2855b39
FM
1523 if (from_pipe)
1524 pclose(fp_in);
43273c35 1525 else {
d2855b39 1526 fclose(fp_in);
43273c35
FM
1527 if( ShowReadStatistics )
1528 printf(_("SARG: Records in file: %lu, reading: %3.2f%%\n"),recs1, (float) 100 );
1529 }
58d7d8fe 1530 }
25697a35
GS
1531 }
1532
42b117e3
FM
1533 if (debug)
1534 debuga(_(" Records read: %ld, written: %ld, excluded: %ld\n"),totregsl,totregsg,totregsx);
1535
afaa3b67 1536 longline_destroy(&line);
2357ef77 1537 if ( fp_Download_Unsort )
25697a35
GS
1538 fclose (fp_Download_Unsort);
1539
f2ec8c75
FM
1540 for (ufile=first_user_file ; ufile ; ufile=ufile1) {
1541 ufile1=ufile->next;
1542 if (ufile->file!=NULL) fclose(ufile->file);
1543 free(ufile);
1544 }
25697a35 1545
43f18f45
FM
1546 free_download();
1547 free_excludecodes();
1548 free_exclude();
1549
25697a35 1550 if(debug) {
58d7d8fe
FM
1551 int totalcount=0;
1552
1553 for (ilf=0 ; ilf<ILF_Last ; ilf++) totalcount+=ilf_count[ilf];
1554
58d7d8fe 1555 if(ilf_count[ILF_Common]>0 && ilf_count[ILF_Squid]>0)
10210234 1556 debuga(_("Log with mixed records format (squid and common log)\n"));
936c9905 1557
58d7d8fe 1558 if(ilf_count[ILF_Common]>0 && ilf_count[ILF_Squid]==0)
10210234 1559 debuga(_("Common log format\n"));
936c9905 1560
58d7d8fe 1561 if(ilf_count[ILF_Common]==0 && ilf_count[ILF_Squid]>0)
10210234 1562 debuga(_("Squid log format\n"));
25697a35 1563
58d7d8fe 1564 if(ilf_count[ILF_Sarg]>0)
10210234 1565 debuga(_("Sarg log format\n"));
936c9905 1566
42b117e3
FM
1567 if(totalcount==0 && totregsg)
1568 debuga(_("Log with invalid format\n"));
25697a35 1569 }
936c9905 1570
25697a35 1571 if(!totregsg){
9426efec
FM
1572 debuga(_("No records found\n"));
1573 debuga(_("End\n"));
42b117e3
FM
1574 if(fp_denied) fclose(fp_denied);
1575 if(fp_authfail) fclose(fp_authfail);
f2ec8c75 1576 userinfo_free();
42b117e3 1577 if(userfile) free(userfile);
cd025686 1578 close_usertab();
2824ec9b 1579 exit(EXIT_SUCCESS);
25697a35
GS
1580 }
1581
42b117e3
FM
1582 if (date[0]!='\0') {
1583 char date0[30], date1[30];
1584
1585 strftime(date0,sizeof(date0),"%d/%m/%Y",&period.start);
1586 strftime(date1,sizeof(date1),"%d/%m/%Y",&period.end);
1587 debuga(_("Period covered by log files: %s-%s\n"),date0,date1);
1588 getperiod_fromrange(&period,dfrom,duntil);
1589 }
1590 if (getperiod_buildtext(&period)<0) {
1591 debuga(_("Failed to build the string representation of the date range\n"));
1592 exit(EXIT_FAILURE);
1593 }
1594
25697a35
GS
1595 if(debugz){
1596 debugaz("data",dia);
fa6552b0 1597 debugaz("period",period.text);
25697a35
GS
1598 }
1599
d2fe0c32 1600 if(debug)
fa6552b0 1601 debuga(_("Period: %s\n"),period.text);
25697a35 1602
25697a35
GS
1603// fclose(fp_ou);
1604 if(fp_denied)
1605 fclose(fp_denied);
1606 if(fp_authfail)
1607 fclose(fp_authfail);
1608
58d7d8fe 1609 if(fp_log != NULL) {
2357ef77 1610 fclose(fp_log);
ff8d5836 1611 strcpy(end_hour,tbuf2);
fa6552b0
FM
1612 strftime(val2,sizeof(val2),"%d%m%Y",&period.start);
1613 strftime(val1,sizeof(val1),"%d%m%Y",&period.end);
ff8d5836
FM
1614 sprintf(val4,"%s/sarg-%s_%s-%s_%s.log",ParsedOutputLog,val2,start_hour,val1,end_hour);
1615 if (rename(arq_log,val4)) {
9426efec 1616 debuga(_("failed to rename %s to %s - %s\n"),arq_log,val4,strerror(errno));
ff8d5836
FM
1617 } else {
1618 strcpy(arq_log,val4);
1619
1620 if(strcmp(ParsedOutputLogCompress,"nocompress") != 0 && ParsedOutputLogCompress[0] != '\0') {
1621 /*
1622 No double quotes around ParsedOutputLogCompress because it may contain command line options. If double quotes are
1623 necessary around the command name, put them in the configuration file.
1624 */
1625 sprintf(val1,"%s \"%s\"",ParsedOutputLogCompress,arq_log);
1626 cstatus=system(val1);
1627 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
9426efec
FM
1628 debuga(_("command return status %d\n"),WEXITSTATUS(cstatus));
1629 debuga(_("command: %s\n"),val1);
06b39c87 1630 exit(EXIT_FAILURE);
ff8d5836 1631 }
456d78a5 1632 }
25697a35 1633 }
d2fe0c32 1634 if(debug)
10210234 1635 debuga(_("Sarg parsed log saved as %s\n"),arq_log);
25697a35 1636 }
25697a35 1637
f84a35a3
FM
1638 if(DataFile[0] == '\0' && (ReportType & REPORT_TYPE_DENIED) != 0) {
1639 sprintf(csort,"sort -T \"%s\" -k 3,3 -k 5,5 -o \"%s\" \"%s\"",tmp,denied_sort,denied_unsort);
456d78a5
FM
1640 cstatus=system(csort);
1641 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
9426efec
FM
1642 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
1643 debuga(_("sort command: %s\n"),csort);
06b39c87 1644 exit(EXIT_FAILURE);
456d78a5 1645 }
f84a35a3 1646 unlink(denied_unsort);
25697a35 1647 }
936c9905 1648
491b862f 1649 sort_users_log(tmp, debug);
25697a35 1650
4157aa09 1651 if(DataFile[0] != '\0')
25697a35
GS
1652 data_file(tmp);
1653 else
1654 gerarel();
1655
085c1e1f 1656 if((ReportType & REPORT_TYPE_DENIED) != 0)
f84a35a3 1657 unlink(denied_sort);
936c9905 1658
d6e703cc 1659 if(strcmp(tmp,"/tmp") != 0) {
51465d08 1660 unlinkdir(tmp,0);
d6e703cc 1661 }
25697a35 1662
f2ec8c75 1663 userinfo_free();
491b862f
GS
1664 if(userfile)
1665 free(userfile);
cd025686 1666 close_usertab();
491b862f 1667
d6e703cc 1668 if(debug)
10210234 1669 debuga(_("End\n"));
d6e703cc 1670
2824ec9b 1671 exit(EXIT_SUCCESS);
25697a35
GS
1672
1673}
1674
1675
936c9905 1676static void getusers(const char *pwdfile, int debug)
25697a35
GS
1677{
1678
1679 FILE *fp_usr;
1680 char buf[255];
936c9905 1681 char *str;
39186a2f 1682 long int nreg=0;
25697a35 1683
d2fe0c32 1684 if(debug)
d574e592 1685 debuga(_("Loading password file from %s\n"),pwdfile);
25697a35
GS
1686
1687 if ((fp_usr = fopen(pwdfile, "r")) == NULL) {
d574e592 1688 debuga(_("(getusers) Cannot open file %s - %s\n"),pwdfile,strerror(errno));
06b39c87 1689 exit(EXIT_FAILURE);
25697a35
GS
1690 }
1691
6443bf6d
FM
1692 if (fseek(fp_usr, 0, SEEK_END)==-1) {
1693 debuga(_("Failed to move till the end of the users file %s: %s\n"),pwdfile,strerror(errno));
1694 exit(EXIT_FAILURE);
1695 }
d6dfc139 1696 nreg = ftell(fp_usr);
5355d243 1697 if (nreg<0) {
9426efec 1698 debuga(_("Cannot get the size of file %s\n"),pwdfile);
06b39c87 1699 exit(EXIT_FAILURE);
5355d243 1700 }
936c9905 1701 nreg = nreg+5000;
6443bf6d
FM
1702 if (fseek(fp_usr, 0, SEEK_SET)==-1) {
1703 debuga(_("Failed to rewind the users file %s: %s\n"),pwdfile,strerror(errno));
1704 exit(EXIT_FAILURE);
1705 }
25697a35
GS
1706
1707 if((userfile=(char *) malloc(nreg))==NULL){
d574e592 1708 debuga(_("malloc error (%ld)\n"),nreg);
06b39c87 1709 exit(EXIT_FAILURE);
25697a35
GS
1710 }
1711
1712 bzero(userfile,nreg);
936c9905
FM
1713 strcpy(userfile,":");
1714
1715 while(fgets(buf,sizeof(buf),fp_usr)!=NULL) {
1716 str=strchr(buf,':');
1717 if (!str) {
d574e592 1718 debuga(_("You have an invalid user in your %s file\n"),pwdfile);
06b39c87 1719 exit(EXIT_FAILURE);
936c9905 1720 }
2824ec9b 1721 str[1]='\0';
936c9905 1722 strcat(userfile,buf);
25697a35
GS
1723 }
1724
1725 fclose(fp_usr);
1726
1727 return;
1728}