]> git.ipfire.org Git - thirdparty/sarg.git/blame - log.c
Detect external commands failures and print the exact command that produced the error...
[thirdparty/sarg.git] / log.c
CommitLineData
25697a35 1/*
c37945ed
FM
2 * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
3 * 1998, 2008
94ff9470
GS
4 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
5 *
25697a35 6 * SARG donations:
94ff9470 7 * please look at http://sarg.sourceforge.net/donations.php
25697a35
GS
8 * ---------------------------------------------------------------------
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
23 *
24 */
25
26#include "include/conf.h"
27
28#define LEGACY_WRITE_USER
29#define LEGACY_WRITE_DOWNLOAD
30#define REPORT_EVERY_X_LINES 5000
31
32
33char *userfile;
34char *excludefile;
35char *excludeuser;
36
37char sz_Last_User[ MAXLEN ] = { 0 } ;
38int bool_ShowReadStatistics ;
39
40numlist weekdays = { { 0, 1, 2, 3, 4, 5, 6 }, 7 };
41numlist hours = { { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
42 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, 24 };
43
44void getusers(char *pwdfile, int debug);
45void gethexclude(char *hexfile, int debug);
46void getuexclude(char *uexfile, int debug);
47void ttx(char *user);
48int compar( const void *, const void * );
49
50#define _FILE_OFFSET_BITS 64
51
52int main(argc,argv)
53 int argc;
54 char *argv[];
55{
56
57 FILE *fp_in = NULL, *fp_ou = NULL, *fp_denied, *fp_usr, *fp_authfail, *fp_log;
58
59 char sz_Download_Unsort[ 20000 ] ;
60 FILE * fp_Download_Unsort = NULL ;
61 FILE * fp_Write_User = NULL ;
62
63 extern int optind;
64 extern int optopt;
65 extern char *optarg;
66
67 char w[MAXLEN];
68 char data[255];
69 char elap[255];
70 char none[255];
71 char ip[MAXLEN];
72 char msg[MAXLEN];
73 char tam[255];
74 char fun[255];
75 char wuser[MAXLEN];
25697a35
GS
76 char smartfilter[MAXLEN];
77 char dia[128];
78 char wdata[128];
79 char mes[30];
80 char ano[30];
81 char hora[30];
82 char wtemp[MAXLEN];
83 char wtemp2[255];
84 char date[255];
85 char arq[255];
86 char arq_log[255];
87 char warq[255][255];
88 char hm[15], hmf[15], hmr[15], whm[15];
89 int chm=0;
90 char uagent[MAXLEN];
91 char hexclude[MAXLEN];
456d78a5
FM
92 char csort[MAXLEN];
93 int cstatus;
25697a35
GS
94 char tbuf[128];
95 char tbuf2[128];
96 char zip[20];
97 char *str;
98 char bufz[MAXLEN];
99 char bufy[MAXLEN];
100 int common;
101 int common_log=0;
102 int squid_log=0;
103 int ch;
d6e703cc 104 int x, l;
25697a35
GS
105 int errflg=0;
106 int puser=0;
107 int fhost=0;
108 int dns=0;
109 int fuser=0;
110 int idata=0;
111 int narq=0;
112 int iarq=0;
113 int exstring=0;
114 int i0=0,i1=0,i2=0,i3=0,i4=0,i5=0,i6=0,i7=0,i8=0;
115 long totregsl=0;
116 long totregsg=0;
117 long totregsx=0;
118 long totper=0;
119 long int max_elapsed=0;
120 time_t tt;
121 struct tm *t;
122 unsigned long nreg=0;
123 off_t recs1=0;
124 unsigned long recs2=0;
125 struct rlimit rl;
126 int OutputNonZero = REPORT_EVERY_X_LINES ;
127
128 BgImage[0]='\0';
129 LogoImage[0]='\0';
130 LogoText[0]='\0';
131 PasswdFile[0]='\0';
132 OutputEmail[0]='\0';
133 Ip2Name[0]='\0';
134 UserAgentLog[0]='\0';
135 ExcludeHosts[0]='\0';
136 ExcludeUsers[0]='\0';
137 ConfigFile[0]='\0';
138 code[0]='\0';
139 LastLog[0]='\0';
140 UserIp[0]='\0';
25697a35
GS
141 ReportType[0]='\0';
142 UserTabFile[0]='\0';
143 BlockIt[0]='\0';
144 ExternalCSSFile[0]='\0';
145 SquidGuardLogFormat[0]='\0';
491b862f 146 SquidGuardLogAlternate[0]='\0';
d6e703cc 147 arq[0]='\0';
25697a35
GS
148
149 strcpy(AccessLog,"/usr/local/squid/var/logs/access.log");
150 sprintf(ExcludeCodes,"%s/exclude_codes",SYSCONFDIR);
151 strcpy(GraphDaysBytesBarColor,"orange");
491b862f 152 strcpy(BgColor,"#ffffff");
25697a35
GS
153 strcpy(TxColor,"#000000");
154 strcpy(TxBgColor,"lavender");
155 strcpy(TiColor,"darkblue");
156 strcpy(Width,"80");
157 strcpy(Height,"45");
158 strcpy(LogoTextColor,"#000000");
159 strcpy(HeaderColor,"darkblue");
491b862f 160 strcpy(HeaderBgColor,"#dddddd");
94ff9470 161 strcpy(LogoTextColor,"#006699");
25697a35
GS
162 strcpy(FontSize,"9px");
163 strcpy(TempDir,"/tmp");
d6e703cc 164 strcpy(OutputDir,"/var/www/html/squid-reports");
25697a35
GS
165 strcpy(Ip2Name,"no");
166 strcpy(DateFormat,"u");
167 strcpy(OverwriteReport,"no");
168 strcpy(RemoveTempFiles,"yes");
169 strcpy(ReplaceIndex,"index.html");
170 strcpy(Index,"yes");
171 strcpy(RecordsWithoutUser,"ip");
172 strcpy(UseComma,"no");
173 strcpy(MailUtility,"mailx");
174 strcpy(TopSitesNum,"100");
175 strcpy(UserIp,"no");
176 strcpy(TopuserSortField,"BYTES");
177 strcpy(UserSortField,"BYTES");
178 strcpy(TopuserSortOrder,"reverse");
179 strcpy(UserSortOrder,"reverse");
180 strcpy(TopsitesSortField,"CONNECT");
181 strcpy(TopsitesSortType,"D");
182 strcpy(LongUrl,"no");
183 strcpy(language,"English");
184 strcpy(FontFace,"Verdana,Tahoma,Arial");
d6e703cc 185 strcpy(datetimeby,"elap");
25697a35
GS
186 strcpy(CharSet,"ISO-8859-1");
187 strcpy(Privacy,"no");
188 strcpy(PrivacyString,"***.***.***.***");
189 strcpy(PrivacyStringColor,"blue");
190 strcpy(SuccessfulMsg,"yes");
191 strcpy(TopUserFields,"NUM DATE_TIME USERID CONNECT BYTES %BYTES IN-CACHE-OUT USED_TIME MILISEC %TIME TOTAL AVERAGE");
192 strcpy(UserReportFields,"CONNECT BYTES %BYTES IN-CACHE-OUT USED_TIME MILISEC %TIME TOTAL AVERAGE");
193 strcpy(DataFileDelimiter,";");
194 strcpy(DataFileFields,"user;date;time;url;connect;bytes;in_cache;out_cache;elapsed");
195 strcpy(SiteUserTimeDateType,"table");
196 strcpy(ShowReadStatistics,"yes");
197 strcpy(IndexSortOrder,"D");
198 strcpy(ShowSargInfo,"yes");
199 strcpy(ShowSargLogo,"yes");
200 strcpy(ParsedOutputLog,"no");
201 strcpy(ParsedOutputLogCompress,"/bin/gzip");
202 strcpy(DisplayedValues,"abbreviation");
203 strcpy(HeaderFontSize,"9px");
204 strcpy(TitleFontSize,"11px");
205 strcpy(AuthUserFile,"/usr/local/sarg/passwd");
206 strcpy(AuthName,"SARG, Restricted Access");
207 strcpy(AuthType,"basic");
208 strcpy(Require,"require user admin %u");
209 strcpy(DownloadSuffix,"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");
210 strcpy(Graphs,"yes");
211 strcpy(Ulimit,"20000");
212 strcpy(NtlmUserFormat,"domainname+username");
491b862f
GS
213 strcpy(IndexTree,"file");
214 strcpy(RealtimeTypes,"GET,PUT,CONNECT");
d6e703cc 215 strcpy(RealtimeUnauthRec,"show");
491b862f
GS
216 strcpy(SquidguardIgnoreDate,"off");
217 strcpy(DansguardianIgnoreDate,"off");
218 strcpy(DataFileUrl,"ip");
d6e703cc
FM
219 strcpy(MaxElapsed,"28800000");
220 strcpy(BytesInSitesUsersReport,"no");
25697a35
GS
221
222 dia[0]='\0';
223 mes[0]='\0';
224 ano[0]='\0';
225 hora[0]='\0';
226 tmp[0]='\0';
227 tmp2[0]='\0';
228 tmp3[0]='\0';
229 wtemp[0]='\0';
230 wtemp2[0]='\0';
231 us[0]='\0';
232 date[0]='\0';
233 df[0]='\0';
234 uagent[0]='\0';
235 hexclude[0]='\0';
236 addr[0]='\0';
237 hm[0]='\0';
238 hmf[0]='\0';
239 site[0]='\0';
240 outdir[0]='\0';
241 elap[0]='\0';
242 email[0]='\0';
243 zip[0]='\0';
244 UserInvalidChar[0]='\0';
245 DataFile[0]='\0';
246 SquidGuardConf[0]='\0';
247 DansGuardianConf[0]='\0';
248
249 excode=0;
250 denied_count=0;
251 download_count=0;
252 authfail_count=0;
253 dansguardian_count=0;
254 squidguard_count=0;
255 DeniedReportLimit=10;
256 AuthfailReportLimit=10;
257 DansGuardianReportLimit=10;
258 SquidGuardReportLimit=10;
94ff9470 259 DownloadReportLimit=50;
25697a35
GS
260 UserReportLimit=0;
261 debug=0;
262 debugz=0;
263 debugm=0;
264 iprel=0;
265 userip=0;
266 color1=0;
267 color2=0;
268 color3=0;
269 sarglog=0;
270 isalog=0;
491b862f
GS
271 dotinuser=0;
272 realt=0;
273 realtime_refresh=3;
274 realtime_access_log_lines=1000;
275 cost=0.01;
276 nocost=50000000;
d6e703cc
FM
277 ndownload=0;
278 squid24=0;
25697a35
GS
279
280 bzero(IncludeUsers, MAXLEN);
281 bzero(ExcludeString, MAXLEN);
282
283 for(x=0; x<=254; x++)
284 warq[x][0]='\0';
285
286 language_load(language);
287 strcpy(Title,text[88]);
288
491b862f 289 while((ch = getopt(argc, argv, "a:b:c:d:e:f:g:u:l:L:o:s:t:w:hijmnprvxyz")) != -1){
25697a35
GS
290 switch(ch)
291 {
292 case 'a':
293 strcpy(addr,optarg);
294 break;
295 case 'b':
296 strcpy(uagent,optarg);
297 break;
298 case 'c':
299 strcpy(hexclude,optarg);
300 break;
301 case 'd':
302 strcpy(date,optarg);
303 strcpy(cduntil,optarg);
4bcb77cf
FM
304 if (getword(cdfrom,sizeof(cdfrom),cduntil,'-')<0) {
305 printf("SARG: Maybe you have a broken record or garbage in your date range.\n");
306 exit(1);
307 }
25697a35
GS
308 date_from(date, cdfrom, cduntil);
309 dfrom=atoi(cdfrom);
310 duntil=atoi(cduntil);
311 break;
312 case 'e':
313 strcpy(email,optarg);
314 break;
315 case 'f':
316 strcpy(ConfigFile,optarg);
317 break;
318 case 'g':
319 strcpy(df,optarg);
320 break;
321 case 'h':
322 usage(argv[0]);
323 exit(0);
324 break;
325 case 'i':
326 iprel++;
327 break;
328 case 'l':
329 strcpy(warq[narq],optarg);
330 narq++;
331 break;
491b862f
GS
332 case 'L':
333 strcpy(SquidGuardLogAlternate,optarg);
334 break;
25697a35
GS
335 case 'm':
336 debugm++;
337 break;
338 case 'n':
339 dns++;
340 break;
341 case 'o':
342 strcpy(outdir,optarg);
343 break;
344 case 'p':
345 userip++;
346 break;
491b862f
GS
347 case 'r':
348 realt++;
349 break;
25697a35
GS
350 case 's':
351 strcpy(site,optarg);
352 break;
353 case 't':
354 if(strstr(optarg,"-") == 0) {
355 strcpy(hm,optarg);
356 strcpy(hmf,optarg);
357 } else {
4bcb77cf
FM
358 if (getword(hm,sizeof(hm),optarg,'-')<0) {
359 printf("SARG: Maybe you have a broken record or garbage in your time range.\n");
360 exit(1);
361 }
25697a35
GS
362 strcpy(hmf,optarg);
363 }
364 if(strlen(hm) > 5) {
365 printf("SARG: time period must be MM or MM:SS. Exit.\n");
366 exit(1);
367 }
368 bzero(whm,15);
369 if(strstr(hm,":") != 0) {
4bcb77cf
FM
370 if (getword(warea,sizeof(warea),hm,':')<0) {
371 printf("SARG: Maybe you have a broken record or garbage in your time range.\n");
372 exit(1);
373 }
25697a35
GS
374 sprintf(whm,"%s%s",warea,hm);
375 strcpy(hm,whm);
376 }
377 bzero(whm,15);
378 if(strstr(hmf,":") != 0) {
4bcb77cf
FM
379 if (getword(warea,sizeof(warea),hmf,':')<0) {
380 printf("SARG: Maybe you have a broken record or garbage in your time range.\n");
381 exit(1);
382 }
25697a35
GS
383 sprintf(whm,"%s%s",warea,hmf);
384 strcpy(hmf,whm);
385 }
386 break;
387 case 'u':
388 strcpy(us,optarg);
389 break;
390 case 'v':
391 version();
392 break;
393 case 'w':
394 strcpy(tmp,optarg);
395 break;
396 case 'x':
397 debug++;
398 break;
399 case 'y':
400 langcode++;
401 break;
402 case 'z':
403 debugz++;
404 break;
405 case ':':
406 fprintf(stderr, "Option -%c require an argument\n",optopt);
407 errflg++;
408 break;
409 case '?':
410 usage(argv[0]);
411 exit(1);
412 break;
413 }
414
415 }
416
417 if (errflg) {
418 usage(argv[0]);
419 exit(2);
420 }
421
d6e703cc 422 if(debug) debuga("Init");
25697a35 423
d6e703cc
FM
424 if(ConfigFile[0] == '\0') sprintf(ConfigFile,"%s/sarg.conf",SYSCONFDIR);
425 else if(access(ConfigFile, R_OK) != 0) {
426 sprintf(msg,"Cannot open config file: %s - %s",ConfigFile,strerror(errno));
427 debuga(msg);
428 exit(1);
429 }
25697a35
GS
430
431 if(access(ConfigFile, R_OK) == 0)
d6e703cc 432 getconf();
491b862f
GS
433
434 if(realt) {
435 realtime();
436 exit(0);
437 }
25697a35 438
491b862f
GS
439 if(strcmp(IndexTree,"file") == 0)
440 strcpy(ImageFile,"../images");
441 else
442 strcpy(ImageFile,"../../../images");
443
25697a35
GS
444 dataonly=0;
445 if(DataFile[0] != '\0');
446 dataonly++;
447
448 str2=(char *)subs(TopUserFields,"%BYTES","SETYB");
449 strcpy(TopUserFields,str2);
450
451 str2=(char *)subs(UserReportFields,"%BYTES","SETYB");
452 strcpy(UserReportFields,str2);
453
454 if(!narq) {
455 strcpy(warq[0],AccessLog);
456 narq++;
457 }
458
459 if(strcmp(hexclude,"onvert") == 0 && strcmp(site,"plit") != 0) {
460 convlog(warq[0], df, dfrom, duntil);
461 exit(0);
462 }
463
464 if(strcmp(site,"plit") == 0) {
465 splitlog(warq[0], df, dfrom, duntil, hexclude);
466 exit(0);
467 }
468
469 if(ExcludeCodes[0] != '\0') {
470 if((excludecode=(char *) malloc(1024))==NULL) {
471 fprintf(stderr, "SARG: %s (1024):\n",text[59]);
472 exit(1);
473 }
474 bzero(excludecode,1024);
475 load_excludecodes();
476 }
477
478 if(access(PasswdFile, R_OK) == 0) {
479 getusers(PasswdFile,debug);
480 puser++;
481 }
482
483 if(hexclude[0] == '\0')
484 strcpy(hexclude,ExcludeHosts);
485 if(strlen(hexclude) > 0) {
486 if(access(hexclude, R_OK) != 0) {
487 sprintf(msg,"Cannot open exclude_hosts file: %s - %s",hexclude,strerror(errno));
488 debuga(msg);
489 exit(1);
490 } else {
491 gethexclude(hexclude,debug);
492 fhost++;
493 }
494 }
495
496 if(ReportType[0] == '\0')
491b862f 497 strcpy(ReportType,"topusers topsites users_sites sites_users date_time denied auth_failures site_user_time_date downloads");
25697a35
GS
498
499 if(access(ExcludeUsers, R_OK) == 0) {
500 getuexclude(ExcludeUsers,debug);
501 fuser++;
502 }
503
504 indexonly=0;
505 if(fuser) {
506 if(strstr(excludeuser,"indexonly") != 0)
507 indexonly++;
508 }
d6e703cc
FM
509 if(strcmp(ExcludeUsers,"indexonly") == 0) indexonly++;
510 if(strcmp(Index,"only") == 0) indexonly++;
25697a35 511
d6e703cc 512 if(dns) strcpy(Ip2Name,"yes");
25697a35 513
d6e703cc 514 if(strcmp(UserIp,"yes") == 0) userip++;
25697a35 515
d6e703cc 516 if(strlen(MaxElapsed)>1) max_elapsed=atol(MaxElapsed);
25697a35 517
d6e703cc 518 if(strlen(outdir)<1) strcpy(outdir,OutputDir);
25697a35
GS
519 strcat(outdir,"/");
520
521
d6e703cc 522 if(arq[0] == '\0') strcpy(arq,AccessLog);
25697a35 523
d6e703cc 524 if(uagent[0] == '\0') strcpy(uagent,UserAgentLog);
25697a35 525
d6e703cc
FM
526 if(tmp[0] == '\0') strcpy(tmp,TempDir);
527 else strcpy(TempDir,tmp);
25697a35 528
d6e703cc
FM
529 if(df[0] == '\0') strcpy(df,DateFormat);
530 else strcpy(DateFormat,df);
25697a35
GS
531
532 if(df[0] == '\0') {
533 strcpy(df,"u");
534 strcpy(DateFormat,"u");
535 }
536
d6e703cc 537 if(strlen(email)<1 && strlen(OutputEmail)>0) strcpy(email,OutputEmail);
25697a35
GS
538
539 strcpy(tmp2,tmp);
25697a35
GS
540
541 if(strlen(email) > 0) {
d6e703cc 542 sprintf(wtemp2,"%s/sarg",tmp2);
25697a35 543 my_mkdir(wtemp2);
d6e703cc 544 strcat(tmp2,"/sarg");
25697a35
GS
545 strcpy(outdir,tmp2);
546 strcat(outdir,"/");
d6e703cc 547 }
25697a35
GS
548
549 strcat(tmp2,"/sarg.log");
550
551 sprintf(warea,"%s/sarg",tmp);
552 if(access(warea, R_OK) == 0) {
553 sprintf(tmp3,"rm -rf %s",warea);
554 system(tmp3);
555 }
556
557 sprintf(tmp3,"%s/sarg",tmp);
558 my_mkdir(tmp3);
491b862f
GS
559 strcpy(tmp4,tmp3);
560 strcpy(tmp5,tmp3);
561 strcpy(tmp6,tmp3);
25697a35
GS
562 strcat(tmp4,"/denied.log.unsort");
563 strcat(tmp5,"/denied.log");
564 strcat(tmp6,"/authfail.log.unsort");
565
566 if(debug) {
567 fprintf(stderr, "SARG: %s:\nSARG:\n",text[22]);
568 fprintf(stderr, "SARG: %35s (-a) = %s\n",text[23],addr);
569 fprintf(stderr, "SARG: %35s (-b) = %s\n",text[71],uagent);
570 fprintf(stderr, "SARG: %35s (-c) = %s\n",text[69],hexclude);
571 fprintf(stderr, "SARG: %35s (-d) = %s\n",text[24],date);
572 fprintf(stderr, "SARG: %35s (-e) = %s\n",text[41],email);
573 fprintf(stderr, "SARG: %35s (-f) = %s\n",text[70],ConfigFile);
574 if(strcmp(df,"e") == 0)
575 fprintf(stderr, "SARG: %35s (-g) = %s (dd/mm/yyyy)\n",text[25],text[26]);
576 if(strcmp(df,"u") == 0)
577 fprintf(stderr, "SARG: %35s (-g) = %s (mm/dd/yyyy)\n",text[25],text[27]);
578 if(strcmp(df,"w") == 0)
579 fprintf(stderr, "SARG: %35s (-g) = %s (yyyy/ww)\n",text[25],text[85]);
580 if(iprel)
581 fprintf(stderr, "SARG: %35s (-i) = %s\n",text[28],text[1]);
582 else
583 fprintf(stderr, "SARG: %35s (-i) = %s\n",text[28],text[2]);
584 fprintf(stderr, "SARG: %35s (-l) = %s\n",text[37],arq);
585 if(strcmp(Ip2Name,"yes") == 0)
586 fprintf(stderr, "SARG: %35s (-n) = %s\n",text[65],text[1]);
587 else
588 fprintf(stderr, "SARG: %35s (-n) = %s\n",text[65],text[2]);
589 fprintf(stderr, "SARG: %35s (-o) = %s\n",text[38],outdir);
590 if(strcmp(UserIp,"yes") == 0)
591 fprintf(stderr, "SARG: %35s (-p) = %s\n",text[29],text[1]);
592 else
593 fprintf(stderr, "SARG: %35s (-p) = %s\n",text[29],text[2]);
594 fprintf(stderr, "SARG: %35s (-s) = %s\n",text[30],site);
595 fprintf(stderr, "SARG: %35s (-t) = %s\n",text[31],hm);
596 fprintf(stderr, "SARG: %35s (-u) = %s\n",text[32],us);
597 fprintf(stderr, "SARG: %35s (-w) = %s\n",text[34],tmp);
598 if(debug)
599 fprintf(stderr, "SARG: %35s (-x) = %s\n",text[35],text[1]);
600 else
601 fprintf(stderr, "SARG: %35s (-x) = %s\n",text[35],text[2]);
602 if(debugz)
603 fprintf(stderr, "SARG: %35s (-z) = %s\n",text[36],text[1]);
604 else
605 fprintf(stderr, "SARG: %35s (-z) = %s\n",text[36],text[2]);
606 fprintf(stderr, "SARG:\n");
607 }
608
609 if(debugm) {
610 printf("%s:\nSARG:\n",text[22]);
611 printf("%35s (-a) = %s\n",text[23],addr);
612 printf("%35s (-b) = %s\n",text[71],uagent);
613 printf("%35s (-c) = %s\n",text[69],hexclude);
614 printf("%35s (-d) = %s\n",text[24],date);
615 printf("%35s (-e) = %s\n",text[41],email);
616 printf("%35s (-f) = %s\n",text[70],ConfigFile);
617 if(strcmp(df,"e") == 0)
618 printf("%35s (-g) = %s (dd/mm/yyyy)\n",text[25],text[26]);
619 if(strcmp(df,"u") == 0)
620 printf("%35s (-g) = %s (mm/dd/yyyy)\n",text[25],text[27]);
621 if(strcmp(df,"w") == 0)
622 printf("%35s (-g) = %s (yyyy/ww)\n",text[25],text[85]);
623 if(iprel)
624 printf("%35s (-i) = %s\n",text[28],text[1]);
625 else
626 printf("%35s (-i) = %s\n",text[28],text[2]);
627 printf("%35s (-l) = %s\n",text[37],arq);
628 if(strcmp(Ip2Name,"yes") == 0)
629 printf("%35s (-n) = %s\n",text[65],text[1]);
630 else
631 printf("%35s (-n) = %s\n",text[65],text[2]);
632 printf("%35s (-o) = %s\n",text[38],outdir);
633 if(strcmp(UserIp,"yes") == 0)
634 printf("%35s (-p) = %s\n",text[29],text[1]);
635 else
636 printf("%35s (-p) = %s\n",text[29],text[2]);
637 printf("%35s (-s) = %s\n",text[30],site);
638 printf("%35s (-t) = %s\n",text[31],hm);
639 printf("%35s (-u) = %s\n",text[32],us);
640 printf("%35s (-w) = %s\n",text[34],tmp);
641 if(debug)
642 printf("%35s (-x) = %s\n",text[35],text[1]);
643 else
644 printf("%35s (-x) = %s\n",text[35],text[2]);
645 if(debugz)
646 printf("%35s (-z) = %s\n",text[36],text[1]);
647 else
648 printf("%35s (-z) = %s\n",text[36],text[2]);
649 printf("sarg %s: %s\n",text[73],VERSION);
650 printf("Language=%s\n\n",text[3]);
651 }
652
653 if(debug){
654 sprintf(msg,"sarg %s: %s",text[73],VERSION);
655 debuga(msg);
656 }
25697a35 657
d6e703cc 658 if (strlen(Ulimit) > 0) {
25697a35 659#if defined(RLIMIT_NOFILE)
d6e703cc 660 getrlimit (RLIMIT_NOFILE, &rl);
25697a35 661#else #if defined(RLIMIT_OFILE)
d6e703cc 662 getrlimit (RLIMIT_OFILE, &rl);
25697a35 663#endif
d6e703cc
FM
664 l1 = rl.rlim_cur;
665 l2 = rl.rlim_max;
666
667 if(Ulimit[0] != '\0') {
668 rl.rlim_cur = atol(Ulimit);
669 rl.rlim_max = atol(Ulimit);
670 #if defined(RLIMIT_NOFILE)
671 if(setrlimit (RLIMIT_NOFILE, &rl) == -1) {
672 #else #if defined(RLIMIT_OFILE)
673 if(setrlimit (RLIMIT_OFILE, &rl) == -1) {
674 #endif
675 sprintf(msg,"setrlimit error - %s\n",strerror(errno));
676 debuga(msg);
677 }
678 }
25697a35 679
d6e703cc
FM
680 if(debug) {
681 sprintf(msg,"Maximum file descriptor: cur=%ld max=%ld, changed to cur=%ld max=%ld",l1,l2,rl.rlim_cur,rl.rlim_max);
682 debuga(msg);
683 }
25697a35
GS
684 }
685
d6e703cc 686 if (UserTabFile[0] != '\0') {
25697a35
GS
687 if(debug) {
688 sprintf(msg,"%s: %s",text[86],UserTabFile);
689 debuga(msg);
690 }
691 if((fp_usr=fopen(UserTabFile,"r"))==NULL) {
692 fprintf(stderr, "%s: (log) %s: %s - %s\n",argv[0],text[45],UserTabFile,strerror(errno));
693 exit(1);
694 }
695 nreg = lseek(fileno(fp_usr), 0, SEEK_END);
696 lseek(fileno(fp_usr), 0, 0);
697 if((userfile=(char *) malloc(nreg+100))==NULL){
698 fprintf(stderr, "%s ERROR: %s",argv[0],text[87]);
699 exit(1);
700 }
701 bzero(userfile,nreg+100);
702 strncat(userfile,":",1);
703 z1=0;
704 z2=1;
d6e703cc
FM
705 while(fgets(buf,MAXLEN,fp_usr)!=NULL) {
706 buf[strlen(buf)-1]='\0';
707 if(strstr(buf,"\r") != 0) buf[strlen(buf)-1]='\0';
4bcb77cf
FM
708 if (getword_multisep(bufy,sizeof(bufy),buf,' ')<0) {
709 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",UserTabFile);
710 exit(1);
711 }
d6e703cc
FM
712 for(z1=0; z1<=strlen(bufy); z1++) {
713 userfile[z2]=bufy[z1];
714 z2++;
715 }
716 strncat(userfile,":",1);
717 for(z1=0; z1<=strlen(buf); z1++) {
718 userfile[z2]=buf[z1];
719 z2++;
720 }
721 strncat(userfile,":",1);
25697a35
GS
722 }
723 fclose(fp_usr);
724 }
725
726 sprintf ( sz_Download_Unsort , "%s/sarg/download.unsort", tmp);
727 bool_ShowReadStatistics = ( strcmp(ShowReadStatistics,"yes") == 0 ) ;
728
729 while(narq--) {
730 strcpy(arq,warq[iarq]);
731 iarq++;
732
733 strcpy(arqtt,arq);
734 decomp(arq,zip,tmp);
735 if(debug) {
736 sprintf(msg, "%s: %s",text[7],arq);
737 debuga(msg);
738 }
739
740#if defined(HAVE_FOPEN64)
741 if((fp_in=(long)fopen64(arq,"r"))==NULL) {
742#else
743 if((fp_in=fopen(arq,"r"))==NULL) {
744#endif
745 fprintf(stderr, "%s: (log) %s: %s - %s\n",argv[0],text[8],arq,strerror(errno));
746 exit(1);
747 }
748 fgets(bufz,sizeof(bufz),fp_in);
d6e703cc
FM
749 if(!isalog && strncmp(bufz,"#Software: Mic",14) == 0) isalog++;
750
25697a35 751 if(strncmp(bufz,"*** SARG Log ***",16) == 0) {
4bcb77cf
FM
752 if (getword(val2,sizeof(val2),arqtt,'-')<0 || getword(val2,sizeof(val2),arqtt,'_')<0 ||
753 getword(val3,sizeof(val3),arqtt,'-')<0 || getword(val3,sizeof(val3),arqtt,'_')<0) {
754 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
755 exit(1);
756 }
d6e703cc 757 sprintf(period,"%s-%s",val2,val3);
25697a35
GS
758 sarglog=1;
759 } else lseek(fileno(fp_in), 0, 0);
760
761 if(strcmp(ParsedOutputLog, "no") != 0 && !sarglog) {
762 if(access(ParsedOutputLog,R_OK) != 0) {
763 sprintf(csort,"%s",ParsedOutputLog);
764 my_mkdir(csort);
765 }
766 sprintf(arq_log,"%s/sarg_temp.log",ParsedOutputLog);
0dda06bf
FM
767#if defined(HAVE_FOPEN64)
768 if((fp_log=(long)fopen64(arq_log,"w"))==NULL) {
769#else
25697a35 770 if((fp_log=fopen(arq_log,"w"))==NULL) {
0dda06bf 771#endif
25697a35
GS
772 fprintf(stderr, "%s: (log) %s: %s - %s\n",argv[0],text[8],arq_log,strerror(errno));
773 exit(1);
774 }
775 fputs("*** SARG Log ***\n",fp_log);
776 }
777
778 if(strstr(ReportType,"denied") != 0) {
0dda06bf
FM
779#if defined(HAVE_FOPEN64)
780 if((fp_denied=(long)fopen64(tmp4,"w"))==NULL) {
781#else
25697a35 782 if((fp_denied=fopen(tmp4,"w"))==NULL) {
0dda06bf 783#endif
25697a35
GS
784 fprintf(stderr, "%s: (log) %s: %s - %s\n",argv[0],text[45],tmp4,strerror(errno));
785 exit(1);
786 }
787 }
788
789 if(DataFile[0]=='\0') {
790 if(strstr(ReportType,"denied") != 0 || strstr(ReportType,"auth_failures") != 0) {
0dda06bf
FM
791#if defined(HAVE_FOPEN64)
792 if((fp_authfail=(long)fopen64(tmp6,"w"))==NULL) {
793#else
25697a35 794 if((fp_authfail=fopen(tmp6,"w"))==NULL) {
0dda06bf 795#endif
25697a35
GS
796 fprintf(stderr, "%s: (log) %s: %s - %s\n",argv[0],text[45],tmp6,strerror(errno));
797 exit(1);
798 }
799 }
800 }
801
802 // pre-Read the file only if I have to show stats
803 if(bool_ShowReadStatistics) {
804 rewind(fp_in);
805 recs1=0;
806 recs2=0;
25697a35 807
d6e703cc
FM
808 while( fgets(bufz,sizeof(bufz),fp_in) != NULL ) recs1++;
809 rewind(fp_in);
810 printf("SARG: Records in file: " OFFSET_STRING ", reading: %3.2f%%\r",recs1,(float) 0);
25697a35
GS
811 fflush( stdout ) ;
812 }
813
25697a35
GS
814 while(fgets(bufz,sizeof(bufz),fp_in)!=NULL) {
815 recs2++;
816 if( bool_ShowReadStatistics && ! --OutputNonZero) {
817 perc = recs2 * 100 ;
818 perc = perc / recs1 ;
d6e703cc 819 printf("SARG: Records in file: " OFFSET_STRING ", reading: %3.2f%%\r",recs1,perc);
25697a35
GS
820 fflush (stdout);
821 OutputNonZero = REPORT_EVERY_X_LINES ;
822 }
823 if(strlen(bufz) > MAXLEN-1) continue;
824 if(!bufz[0]) continue;
825 if(strstr(bufz,"HTTP/0.0") != 0) continue;
826 if(strstr(bufz,"logfile turned over") != 0) continue;
827 if(bufz[0] == ' ') continue;
828 if(strlen(bufz) < 58) continue;
829
830 // Record only hours usage which is required
831 tt = (time_t) strtoul( bufz, NULL, 10 );
832 t = localtime( &tt );
833
834 if( bsearch( &( t -> tm_wday ), weekdays.list, weekdays.len,
835 sizeof( int ), compar ) == NULL )
836 continue;
837
838 if( bsearch( &( t -> tm_hour ), hours.list, hours.len,
839 sizeof( int ), compar ) == NULL )
840 continue;
841
842 // exclude_string
843 exstring=0;
844 if(strlen(ExcludeString) > 0) {
845 strcpy(warea,bufz);
846 strcpy(html,ExcludeString);
847 while(strstr(html,":") != 0) {
4bcb77cf
FM
848 if (getword_multisep(val1,sizeof(val1),html,':')<0) {
849 printf("SARG: Maybe you have a broken record or garbage in your exclusion string.\n");
850 exit(1);
851 }
25697a35
GS
852 if((str=(char *) strstr(warea,val1)) != (char *) NULL )
853 exstring++;
854 }
855 if((str=(char *) strstr(warea,html)) != (char *) NULL )
856 exstring++;
857 }
858 if(exstring) continue;
859
860 strcpy(bufy,bufz);
861 if ((str = strchr(bufz, '\n')) != NULL)
862 *str = '\0'; /* strip \n */
863
864 totregsl++;
865 common=0;
866 if(debugm)
867 printf("BUF=%s\n",bufz);
868
869 if(!sarglog && !isalog) {
4bcb77cf
FM
870 if (getword(data,sizeof(data),bufz,' ')<0) {
871 printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n");
872 exit(1);
873 }
25697a35
GS
874 if((str=(char *) strstr(data, ".")) != (char *) NULL ) {
875 if((str=(char *) strstr(str+1, ".")) != (char *) NULL ) {
876 strcpy(ip,data);
877 strcpy(elap,"0");
4bcb77cf
FM
878 if(squid24) {
879 if (getword(user,sizeof(user),bufz,' ')<0 || getword(none,sizeof(none),bufz,' ')<0) {
880 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
881 exit(1);
882 }
883 } else {
884 if (getword(none,sizeof(none),bufz,' ')<0 || getword(user,sizeof(user),bufz,' ')<0) {
885 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
886 exit(1);
887 }
888 }
889 if (getword(data,sizeof(data),bufz,']')<0 || getword(fun,sizeof(fun),bufz,'"')<0 ||
890 getword(fun,sizeof(fun),bufz,' ')<0 || getword(url,sizeof(url),bufz,' ')<0 ||
891 getword(code2,sizeof(code2),bufz,' ')<0 || getword(code2,sizeof(code2),bufz,' ')<0 ||
892 getword(tam,sizeof(tam),bufz,' ')<0) {
893 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
894 exit(1);
895 }
896 if((str=(char *) strstr(bufz, " ")) != (char *) NULL ) {
897 if (getword(code,sizeof(code),bufz,' ')<0) {
898 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
899 exit(1);
900 }
901 }
25697a35
GS
902 else strcpy(code,bufz);
903
904 if ((str = strchr(code, ':')) != NULL)
905 *str = '/';
906
907 if(strcmp(tam,"\0") == 0)
908 strcpy(tam,"0");
909
910 common++;
911 common_log=1;
912 }
913 }
914
915 if(!common) {
4bcb77cf
FM
916 if (getword(elap,sizeof(elap),bufz,' ')<0) {
917 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
918 exit(1);
919 }
94ff9470 920 while(strcmp(elap,"") == 0 && strlen(bufz) > 0)
4bcb77cf
FM
921 if (getword(elap,sizeof(elap),bufz,' ')<0) {
922 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
923 exit(1);
924 }
94ff9470 925 if(strlen(elap) < 1) continue;
4bcb77cf
FM
926 if (getword(ip,sizeof(ip),bufz,' ')<0){
927 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
928 exit(1);
929 }
930 if (getword(code,sizeof(code),bufz,' ')<0){
931 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
932 exit(1);
933 }
934 if (getword(tam,sizeof(tam),bufz,' ')<0){
935 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
936 exit(1);
937 }
938 if (getword(fun,sizeof(fun),bufz,' ')<0){
939 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
940 exit(1);
941 }
942 if (getword(url,sizeof(url),bufz,' ')<0){
943 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
944 exit(1);
945 }
d6e703cc
FM
946// while (strstr(bufz,"%20") != 0) {
947// getword(warea,bufz,' ');
948// strcat(url,warea);
949// }
4bcb77cf
FM
950 if (getword(user,sizeof(user),bufz,' ')<0){
951 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
952 exit(1);
953 }
954 squid_log=1;
955 }
25697a35 956 } else if(!isalog) {
4bcb77cf
FM
957 if (getword(data,sizeof(data),bufz,' ')<0){
958 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
959 exit(1);
960 }
961 if (getword(hora,sizeof(hora),bufz,' ')<0) {
962 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
963 exit(1);
964 }
965 if (getword(user,sizeof(user),bufz,' ')<0) {
966 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
967 exit(1);
968 }
969 if (getword(ip,sizeof(ip),bufz,' ')<0) {
970 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
971 exit(1);
972 }
973 if (getword(url,sizeof(url),bufz,' ')<0){
974 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
975 exit(1);
976 }
977 if (getword(tam,sizeof(tam),bufz,' ')<0){
978 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
979 exit(1);
980 }
981 if (getword(code,sizeof(code),bufz,' ')<0){
982 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
983 exit(1);
984 }
985 if (getword(elap,sizeof(elap),bufz,' ')<0){
986 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
987 exit(1);
988 }
989 if (getword(smartfilter,sizeof(smartfilter),bufz,' ')<0){
990 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
991 exit(1);
992 }
25697a35
GS
993 } else if(isalog) {
994 if(!i0) {
4bcb77cf
FM
995 if (getword(val1,sizeof(val1),bufz,' ')<0){
996 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
997 exit(1);
998 }
25697a35 999 while(strstr(bufz,"\t") != 0) {
4bcb77cf
FM
1000 if (getword(val1,sizeof(val1),bufz,'\t')<0){
1001 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
1002 exit(1);
1003 }
25697a35
GS
1004 i0++;
1005 if(strcmp(val1,"c-ip") == 0) i1=i0;
1006 if(strcmp(val1,"cs-username") == 0) i2=i0;
1007 if(strcmp(val1,"date") == 0) i3=i0;
1008 if(strcmp(val1,"time") == 0) i4=i0;
1009 if(strcmp(val1,"time-taken") == 0) i5=i0;
1010 if(strcmp(val1,"sc-bytes") == 0) i6=i0;
1011 if(strcmp(val1,"cs-uri") == 0) i7=i0;
1012 if(strcmp(val1,"sc-status") == 0) i8=i0;
1013 }
1014 }
1015 fgets(bufz,sizeof(bufz),fp_in);
1016 strcpy(val1,bufz);
4bcb77cf
FM
1017 for(x=0; x<=i1-1; x++) {
1018 if (getword3(ip,sizeof(ip),val1,'\t')<0) {
1019 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
1020 exit(1);
1021 }
1022 }
25697a35 1023 strcpy(val1,bufz);
4bcb77cf
FM
1024 for(x=0; x<=i2-1; x++) {
1025 if (getword3(user,sizeof(user),val1,'\t')<0) {
1026 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
1027 exit(1);
1028 }
1029 }
25697a35 1030 strcpy(val1,bufz);
4bcb77cf
FM
1031 for(x=0; x<=i3-1; x++) {
1032 if (getword3(data,sizeof(data),val1,'\t')<0) {
1033 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
1034 exit(1);
1035 }
1036 }
25697a35 1037 strcpy(val1,bufz);
4bcb77cf
FM
1038 for(x=0; x<=i4-1; x++) {
1039 if (getword3(hora,sizeof(hora),val1,'\t')<0) {
1040 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
1041 exit(1);
1042 }
1043 }
25697a35 1044 strcpy(val1,bufz);
4bcb77cf
FM
1045 for(x=0; x<=i5-1; x++) {
1046 if (getword3(elap,sizeof(elap),val1,'\t')<0) {
1047 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
1048 exit(1);
1049 }
1050 }
25697a35 1051 strcpy(val1,bufz);
4bcb77cf
FM
1052 for(x=0; x<=i6-1; x++) {
1053 if (getword3(tam,sizeof(tam),val1,'\t')<0) {
1054 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
1055 exit(1);
1056 }
1057 }
25697a35 1058 strcpy(val1,bufz);
4bcb77cf
FM
1059 for(x=0; x<=i7-1; x++) {
1060 if (getword3(url,sizeof(url),val1,'\t')<0) {
1061 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
1062 exit(1);
1063 }
1064 }
25697a35 1065 strcpy(val1,bufz);
4bcb77cf
FM
1066 for(x=0; x<=i8-1; x++) {
1067 if (getword3(code,sizeof(code),val1,'\t')<0) {
1068 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
1069 exit(1);
1070 }
1071 }
25697a35
GS
1072
1073 if(strcmp(code,"401") == 0 || strcmp(code,"403") == 0 || strcmp(code,"407") == 0) {
1074 sprintf(val1,"DENIED/%s",code);
1075 strcpy(code,val1);
1076 }
4bcb77cf
FM
1077 if (getword(ano,sizeof(ano),data,'-')<0){
1078 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
1079 exit(1);
1080 }
1081 if (getword(mes,sizeof(mes),data,'-')<0){
1082 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq);
1083 exit(1);
1084 }
25697a35
GS
1085 strcpy(dia,data);
1086 conv_month_name(mes);
1087 sprintf(data," %s/%s/%s:%s",dia,mes,ano,hora);
1088 }
1089
d6e703cc
FM
1090 if(strlen(user) > 150) {
1091 totregsx++;
1092 continue;
1093 }
1094
25697a35
GS
1095 // include_users
1096 if(strlen(IncludeUsers) > 0) {
1097 sprintf(val1,":%s:",user);
1098 if((str=(char *) strstr(IncludeUsers,val1)) == (char *) NULL )
1099 continue;
1100 }
1101
1102 if(excode) {
1103 if(vercode(code)) {
1104 totregsx++;
1105 continue;
1106 }
1107 }
1108
1109 if(testvaliduserchar(user))
1110 continue;
1111
d6e703cc 1112 if(strstr(user,"%20") != 0) {
4bcb77cf
FM
1113 if (getword(w,sizeof(w),user,'%')<0) {
1114 printf("SARG: Maybe you have a broken user in your %s file.\n",arq);
1115 exit(1);
1116 }
d6e703cc
FM
1117 strcpy(user,w);
1118 }
1119
1120 while(strstr(user,"%5c") != 0) {
4bcb77cf
FM
1121 if (getword_multisep(w,sizeof(w),user,'%')<0){
1122 printf("SARG: Maybe you have a broken user in your %s file.\n",arq);
1123 exit(1);
1124 }
25697a35
GS
1125 strcpy(wuser,user+2);
1126 sprintf(user,"%s.%s",w,wuser);
1127 }
1128
1129 str=user;
1130 for(z1=0; z1<strlen(str); z1++) {
1131 if(isalnum(str[z1]) || ispunct(str[z1])) {
94ff9470 1132 if(str[z1]=='.') dotinuser++;
25697a35
GS
1133 if(str[z1]=='?' || str[z1]=='.' || str[z1]==':' || str[z1]=='/' || str[z1]=='\\')
1134 str[z1]='_';
1135 }
1136 }
1137
1138 strlow(user);
1139 if(strncmp(NtlmUserFormat,"user",4) == 0) {
1140 if(strstr(user,"_") != 0)
4bcb77cf
FM
1141 if (getword_multisep(warea,sizeof(warea),user,'_')<0){
1142 printf("SARG: Maybe you have a broken user in your %s file.\n",arq);
1143 exit(1);
1144 }
94ff9470 1145 if(strstr(user,"+") != 0)
4bcb77cf
FM
1146 if (getword_multisep(warea,sizeof(warea),user,'+')<0){
1147 printf("SARG: Maybe you have a broken user in your %s file.\n",arq);
1148 exit(1);
1149 }
25697a35
GS
1150 }
1151
1152 if(strstr(ReportType,"denied") != 0)
1153 strcpy(urly,url);
1154
1155 if(strlen(DownloadSuffix)) {
1156 suffix[0]='\0';
1157 download_flag=0;
1158 if(strncmp(url+strlen(url)-4,".",1) == 0)
1159 strcpy(suffix,url+strlen(url)-3);
1160 else strcpy(suffix,url+strlen(url)-4);
1161 if(strstr(DownloadSuffix,suffix) != 0) {
1162 strcpy(download_url,url);
1163 download_flag=1;
1164 download_count++;
1165 }
1166 }
1167
1168 if (strchr(url,'/')) {
4bcb77cf
FM
1169 if (getword(w,sizeof(w),url,'/')<0){
1170 printf("SARG: Maybe you have a broken url in your %s file.\n",arq);
1171 exit(1);
1172 }
1173 if (getword(w,sizeof(w),url,'/')<0){
1174 printf("SARG: Maybe you have a broken url in your %s file.\n",arq);
1175 exit(1);
1176 }
1177 if (!strchr(url,'/')) {
491b862f
GS
1178 totregsx++;
1179 continue;
1180 }
25697a35
GS
1181 }
1182
1183 if(strcmp(LongUrl,"no") == 0) {
4bcb77cf
FM
1184 if (getword_multisep(w,sizeof(w),url,'/')<0){
1185 printf("SARG: Maybe you have a broken url in your %s file.\n",arq);
1186 exit(1);
1187 }
25697a35 1188 strcpy(url,w);
491b862f 1189 if(strlen(url) > 512 && strstr(url,"%") != 0) {
4bcb77cf
FM
1190 if (getword_multisep(w,sizeof(w),url,'%')<0){
1191 printf("SARG: Maybe you have a broken url in your %s file.\n",arq);
1192 exit(1);
1193 }
491b862f
GS
1194 strcpy(url,w);
1195 }
25697a35
GS
1196 }
1197
1198 if(!sarglog) {
1199 if(!common && !isalog) {
1200 tt=atoi(data);
1201 t=localtime(&tt);
1202
1203 strftime(tbuf2, 127, "%H%M", t);
1204 if(strncmp(df,"u",1) == 0)
1205 strftime(tbuf, 127, "%Y%b%d", t);
1206 if(strncmp(df,"e",1) == 0)
1207 strftime(tbuf, 127, "%d%b%Y", t);
491b862f
GS
1208 if(strncmp(df,"w",1) == 0) {
1209 strcpy(IndexTree,"file");
25697a35 1210 strftime(tbuf, 127, "%Y.%U", t);
491b862f 1211 }
25697a35
GS
1212
1213 strftime(dia, 127, "%d/%m/%Y", t);
1214 strftime(wdata, 127, "%Y%m%d", t);
1215
1216 idata=atoi(wdata);
1217
1218 if(strncmp(df,"u",1)==0)
1219 strftime(dia, 127, "%m/%d/%Y", t);
1220 sprintf(hora,"%02d:%02d:%02d",t->tm_hour,t->tm_min,t->tm_sec);
4bcb77cf
FM
1221 } else {
1222 strcpy(wtemp,data+1);
1223 if (getword_multisep(data,sizeof(data),wtemp,':')<0){
1224 printf("SARG: Maybe you have a broken date in your %s file.\n",arq);
1225 exit(1);
1226 }
1227 if (getword_multisep(hora,sizeof(hora),wtemp,' ')<0){
1228 printf("SARG: Maybe you have a broken date in your %s file.\n",arq);
1229 exit(1);
1230 }
1231 if (getword_multisep(dia,sizeof(dia),data,'/')<0){
1232 printf("SARG: Maybe you have a broken date in your %s file.\n",arq);
1233 exit(1);
1234 }
1235 if (getword_multisep(mes,sizeof(mes),data,'/')<0){
1236 printf("SARG: Maybe you have a broken date in your %s file.\n",arq);
1237 exit(1);
1238 }
1239 if (getword_multisep(ano,sizeof(ano),data,'/')<0){
1240 printf("SARG: Maybe you have a broken date in your %s file.\n",arq);
1241 exit(1);
1242 }
25697a35
GS
1243
1244 if(strcmp(df,"u") == 0)
1245 sprintf(tbuf,"%s%s%s",ano,mes,dia);
1246 if(strcmp(df,"e") == 0)
1247 sprintf(tbuf,"%s%s%s",dia,mes,ano);
1248 builddia(dia,mes,ano,df,wdata);
1249 idata=atoi(wdata);
1250 }
1251 } else {
4bcb77cf
FM
1252 if (getword_multisep(mes,sizeof(mes),data,'/')<0){
1253 printf("SARG: Maybe you have a broken date in your %s file.\n",arq);
1254 exit(1);
1255 }
1256 if (getword_multisep(dia,sizeof(dia),data,'/')<0){
1257 printf("SARG: Maybe you have a broken date in your %s file.\n",arq);
1258 exit(1);
1259 }
1260 if (getword_multisep(ano,sizeof(ano),data,0)<0){
1261 printf("SARG: Maybe you have a broken date in your %s file.\n",arq);
1262 exit(1);
1263 }
25697a35
GS
1264 }
1265
1266 if(debugm)
1267 printf("DATE=%s IDATA=%d DFROM=%d DUNTIL=%d\n",date,idata,dfrom,duntil);
1268
1269 l=1;
1270 if(strlen(us)>0){
1271 if(strcmp(user,us)==0)
1272 l=1;else l=0;
1273 }
1274
1275 if(l){
1276 if(strlen(addr)>0){
1277 if(strcmp(addr,ip)==0)
1278 l=1;else l=0;
1279 }
1280 if(fhost) {
94ff9470
GS
1281// l=vhexclude(excludefile,ip);
1282 l=vhexclude(excludefile,url);
25697a35
GS
1283 if(!l)
1284 totregsx++;
1285 }
1286 }
1287
1288 if(l){
1289 if(strlen(date) > 0){
1290 if(idata >= dfrom && idata <= duntil)
1291 l=1;else l=0;
1292 }
1293 }
1294 if(l){
1295 if(strlen(hm)>0) {
1296 strcpy(whm,hora);
1297 bzero(hmr,15);
1298 chm++;
1299 while(chm) {
4bcb77cf
FM
1300 if (getword_multisep(warea,sizeof(warea),whm,':')<0){
1301 printf("SARG: Maybe you have a broken time in your %s file.\n",arq);
1302 exit(1);
1303 }
25697a35
GS
1304 strncat(hmr,warea,2);
1305 chm--;
1306 }
1307 strncat(hmr,whm,2);
1308
1309 if(atoi(hmr) >= atoi(hm) && atoi(hmr) <= atoi(hmf))
1310 l=1;else l=0;
1311 }
1312 }
1313 if(l){
1314 if(strlen(site)>0){
1315 if(strstr(url,site)!=0)
1316 l=1;else l=0;
1317 }
1318 }
1319
1320 if(userip)
1321 strcpy(user,ip);
1322
1323 if(strcmp(user,"-") == 0 || strcmp(user," ") == 0 || strcmp(user,"") == 0) {
1324 if(strcmp(RecordsWithoutUser,"ip") == 0)
1325 strcpy(user,ip);
1326 if(strcmp(RecordsWithoutUser,"ignore") == 0)
1327 continue;
1328 if(strcmp(RecordsWithoutUser,"everybody") == 0)
1329 strcpy(user,"everybody");
1330 }
1331
d6e703cc
FM
1332 if(dotinuser) {
1333 str2=(char *)subs(user,"_",".");
1334 strcpy(user,str2);
1335 dotinuser=0;
1336 }
1337
25697a35
GS
1338 if(puser) {
1339 sprintf(wuser,":%s:",user);
1340 if(strstr(userfile, wuser) == 0)
1341 continue;
1342 }
1343
1344 if(l) {
1345 if(fuser) {
1346 l=vuexclude(excludeuser,user);
1347 if(!l)
1348 totregsx++;
1349 }
1350 }
1351
1352 if(l) {
1353 if(userip)
1354 fixip(user);
1355 }
1356
1357 if(l&&max_elapsed) {
1358 if(atol(elap)>max_elapsed) {
1359 elap[0]='0';
1360 elap[1]='\0';
1361 }
1362 }
1363
1364 if(l) {
1365 if(strcmp(user,"-") !=0 && strlen(url) > 0 && strcmp(user," ") !=0 && strcmp(user,"") !=0 && strcmp(user,":") !=0){
1366 if((str=(char *) strstr(bufz, "[SmartFilter:")) != (char *) NULL ) {
1367 str[strlen(str)-1]='\0';
1368 sprintf(smartfilter,"\"%s\"",str+1);
1369 } else sprintf(smartfilter,"\"\"");
1370
1371 sprintf(bufz, "%s %s %s %s %s %s %s %s %s\n",dia,hora,user,ip,url,tam,code,elap,smartfilter);
1372
1373#ifdef LEGACY_WRITE_USER
1374 sprintf(tmp3,"%s/sarg/%s.unsort",tmp,user);
1375#if defined(HAVE_FOPEN64)
1376 if((fp_ou=fopen64(tmp3,"a"))==NULL) {
1377#else
1378 if((fp_ou=fopen(tmp3,"a"))==NULL) {
1379#endif
d6e703cc 1380 fprintf(stderr, "%s: (zzzlog) %s: %s - %s\n",argv[0],text[9],tmp3,strerror(errno));
25697a35
GS
1381 exit(1);
1382 }
1383 fputs(bufz,fp_ou);
1384
1385#else
1386 if ( strcmp ( user , sz_Last_User ) != 0 ) {
1387 if ( fp_Write_User )
1388 fclose( fp_Write_User ) ;
1389 sprintf (tmp3, "%s/sarg/%s.unsort", tmp, user);
1390
1391#if defined(HAVE_FOPEN64)
1392 if ((fp_Write_User = fopen64 (tmp3, "a")) == NULL) {
1393#else
1394 if ((fp_Write_User = fopen (tmp3, "a")) == NULL) {
1395#endif
1396 fprintf (stderr, "%s: (log) %s: %s - %s\n", argv[0], text[9], tmp3, strerror(errno));
1397 exit (1);
1398 }
1399 strcpy( sz_Last_User , user ) ;
1400 }
1401 fputs (bufz, fp_Write_User);
1402#endif
1403
1404 if(strcmp(ParsedOutputLog, "no") != 0 && !sarglog)
1405 fputs(bufz,fp_log);
1406
1407#ifdef LEGACY_WRITE_USER
1408 fclose(fp_ou);
1409#endif
1410 totregsg++;
1411
d6e703cc
FM
1412 if(download_flag && strstr(code,"DENIED") == 0) {
1413 ndownload = 1;
1414 sprintf(bufz, "%s %s %s %s %s %s\n",dia,hora,user,ip,download_url);
25697a35
GS
1415
1416#ifdef LEGACY_WRITE_DOWNLOAD
1417 sprintf(tmp3,"%s/sarg/download.unsort",tmp);
1418#if defined(HAVE_FOPEN64)
1419 if((fp_ou=fopen64(tmp3,"a"))==NULL) {
1420#else
1421 if((fp_ou=fopen(tmp3,"a"))==NULL) {
1422#endif
1423 fprintf(stderr, "%s: (log) %s: %s - %s\n",argv[0],text[9],tmp3,strerror(errno));
1424 exit(1);
1425 }
1426 fputs(bufz,fp_ou);
1427 fclose(fp_ou);
1428#else
1429 if ( ! fp_Download_Unsort ) {
1430#if defined(HAVE_FOPEN64)
1431 if ((fp_Download_Unsort = fopen64 ( sz_Download_Unsort, "a")) == NULL) {
1432#else
1433 if ((fp_Download_Unsort = fopen (sz_Download_Unsort, "a")) == NULL) {
1434#endif
1435 fprintf (stderr, "%s: (log) %s: %s - %s\n", argv[0], text[9], tmp3, strerror(errno));
1436 exit (1);
1437 }
1438 }
1439 fputs (bufz, fp_Download_Unsort);
1440#endif
1441 }
1442
1443 if(strstr(ReportType,"denied") != 0 || strstr(ReportType,"auth_failures") != 0) {
1444 if(strstr(code,"DENIED/403") != 0) {
1445 sprintf(bufz, "%s %s %s %s %s\n",dia,hora,user,ip,urly);
1446 fputs(bufz,fp_denied);
1447 denied_count++;
1448 }
1449 if(strstr(code,"DENIED/401") != 0 || strstr(code,"DENIED/407") != 0) {
1450 sprintf(bufz, "%s %s %s %s %s\n",dia,hora,user,ip,urly);
1451 if(fp_authfail)
1452 fputs(bufz,fp_authfail);
1453 authfail_count++;
1454 }
1455 }
1456
1457 if(!totper && !sarglog){
1458 totper++;
d6e703cc 1459 sprintf(period,"%s-",tbuf);
25697a35
GS
1460 sprintf(per_hour,"%s-",tbuf2);
1461 if(strlen(date)>0)
d6e703cc 1462 fixper(tbuf, period, cduntil);
25697a35
GS
1463 if(debugz){
1464 debugaz("tbuf",tbuf);
d6e703cc 1465 debugaz("period",period);
25697a35
GS
1466 }
1467 }
1468 }
1469
1470 if(debugm){
1471 printf("IP=\t%s\n",ip);
1472 printf("USER=\t%s\n",user);
1473 printf("ELAP=\t%s\n",elap);
1474 printf("DATE=\t%s\n",dia);
1475 printf("TIME=\t%s\n",hora);
1476 printf("FUNC=\t%s\n",fun);
1477 printf("URL=\t%s\n",url);
1478 printf("CODE=\t%s\n",code);
1479 printf("LEN=\t%s\n",tam);
1480 }
1481 }
1482 }
1483 if( bool_ShowReadStatistics )
d6e703cc 1484 printf("SARG: Records in file: " OFFSET_STRING ", reading: %3.2f%%\n",recs1, (float) 100 );
25697a35
GS
1485 }
1486
1487 if ( fp_Download_Unsort )
1488 fclose (fp_Download_Unsort);
1489
1490 if (fp_Write_User)
1491 fclose (fp_Write_User);
1492
1493 if(debug) {
1494 sprintf(msg, " %s: %ld, %s: %ld, %s: %ld",text[10],totregsl,text[11],totregsg,text[68],totregsx);
1495 debuga(msg);
1496
1497 if((common_log) && (squid_log))
1498 debuga(text[12]);
1499
1500 if((common_log) && (!squid_log))
1501 debuga(text[13]);
1502
1503 if((!common_log) && (squid_log))
1504 debuga(text[14]);
1505
1506 if(sarglog)
1507 debuga(text[124]);
1508
1509 if((!common_log) && (!squid_log) && (!sarglog) && (!isalog)) {
1510 if(!totregsg) {
1511 fprintf(stderr, "SARG: %s\n",text[16]);
1512 fprintf(stderr, "SARG: %s\n",text[21]);
1513 } else fprintf(stderr, "SARG: %s\n",text[15]);
1514 bzero(msg,sizeof(msg));
1515 fclose(fp_in);
1516// fclose(fp_ou);
1517 if(fp_denied)
1518 fclose(fp_denied);
1519 if(fp_authfail)
1520 fclose(fp_authfail);
1521 if(tmp4)
1522 unlink(tmp4);
1523 if(tmp6)
1524 unlink(tmp6);
1525 unlink(tmp3);
1526 exit(0);
1527 }
1528 }
1529
1530 if(!totregsg){
1531 fprintf(stderr, "SARG: %s\n",text[16]);
1532 fprintf(stderr, "SARG: %s\n",text[21]);
1533 fclose(fp_in);
1534// fclose(fp_ou);
1535 if(fp_denied)
1536 fclose(fp_denied);
1537 if(fp_authfail)
1538 fclose(fp_authfail);
1539 exit(0);
1540 }
1541
1542 if(date[0] == '\0' && !sarglog) {
d6e703cc 1543 strcat(period,tbuf);
25697a35
GS
1544 strcat(per_hour,tbuf2);
1545 }
1546
1547 if(debugz){
1548 debugaz("data",dia);
1549 debugaz("tbuf",tbuf);
d6e703cc 1550 debugaz("period",period);
25697a35
GS
1551 }
1552
1553 if(debug){
d6e703cc 1554 sprintf(msg, "%s: %s",text[17],period);
25697a35
GS
1555 debuga(msg);
1556 }
1557
1558 fclose(fp_in);
1559// fclose(fp_ou);
1560 if(fp_denied)
1561 fclose(fp_denied);
1562 if(fp_authfail)
1563 fclose(fp_authfail);
1564
1565 if(strcmp(ParsedOutputLog, "no") != 0 && !sarglog) {
1566 fclose(fp_log);
d6e703cc 1567 strcpy(val1,period);
4bcb77cf
FM
1568 if (getword_multisep(val2,sizeof(val2),val1,'-')<0){
1569 printf("SARG: Maybe you have a broken date range definition.\n");
1570 exit(1);
1571 }
1572 if (getword_multisep(val3,sizeof(val3),per_hour,'-')<0){
1573 printf("SARG: Maybe you have a broken date range definition.\n");
1574 exit(1);
1575 }
25697a35
GS
1576 sprintf(val4,"%s/sarg-%s_%s-%s_%s.log",ParsedOutputLog,val2,val3,val1,per_hour);
1577 rename(arq_log,val4);
1578 strcpy(arq_log,val4);
1579
1580 if(strcmp(ParsedOutputLogCompress,"nocompress") != 0) {
1581 sprintf(val1,"%s %s",ParsedOutputLogCompress,arq_log);
456d78a5
FM
1582 cstatus=system(val1);
1583 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
1584 fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus));
1585 fprintf(stderr, "SARG: command: %s\n",val1);
1586 exit(1);
1587 }
25697a35
GS
1588 }
1589
1590 if(debug) {
1591 sprintf(msg,"%s %s",text[123],arq_log);
1592 debuga(msg);
1593 }
1594 }
25697a35
GS
1595
1596 if(strstr(ReportType,"denied") != 0) {
94ff9470 1597 sprintf(csort,"sort -T %s -k 3,3 -k 5,5 -o '%s' '%s'",tmp,tmp5,tmp4);
456d78a5
FM
1598 cstatus=system(csort);
1599 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
1600 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
1601 fprintf(stderr, "SARG: sort command: %s\n",csort);
1602 exit(1);
1603 }
25697a35
GS
1604 unlink(tmp4);
1605 }
491b862f
GS
1606
1607 sort_users_log(tmp, debug);
25697a35
GS
1608
1609 report_gen:
1610 if(strlen(DataFile) > 0)
1611 data_file(tmp);
1612 else
1613 gerarel();
1614
1615 unlink(tmp2);
1616 if(strstr(ReportType,"denied") != 0)
1617 unlink(tmp5);
1618
1619 if((strlen(zip) > 0 && strcmp(zip,"zcat") !=0)) {
1620 recomp(arq, zip); }
1621// else unlink(arq);
1622
d6e703cc
FM
1623 if(strcmp(tmp,"/tmp") != 0) {
1624 sprintf(tmp4,"rm -rf %s",tmp);
1625 system(tmp4);
1626 }
25697a35 1627
491b862f
GS
1628 if(excludecode)
1629 free(excludecode);
1630 if(userfile)
1631 free(userfile);
1632 if(excludefile)
1633 free(excludefile);
1634 if(excludeuser)
1635 free(excludeuser);
1636
d6e703cc
FM
1637 if(debug)
1638 debuga(text[21]);
1639
25697a35
GS
1640 exit(0);
1641
1642}
1643
1644
1645void getusers(char *pwdfile, int debug)
1646{
1647
1648 FILE *fp_usr;
1649 char buf[255];
1650 char Msg[255];
1651 char user[255];
1652 unsigned long int nreg=0;
1653
1654 if(debug) {
1655 sprintf(Msg,"%s: %s",text[60],pwdfile);
1656 debuga(Msg);
1657 }
1658
1659 if ((fp_usr = fopen(pwdfile, "r")) == NULL) {
1660 fprintf(stderr, "SARG: (getusers) %s: %s - %s\n",text[45],pwdfile,strerror(errno));
1661 exit(1);
1662 }
1663
1664 nreg = lseek(fileno(fp_usr), (off_t)0, SEEK_END);
1665 nreg = nreg+5000;
1666 lseek(fileno(fp_usr), (off_t)0, 0);
1667
1668 if((userfile=(char *) malloc(nreg))==NULL){
1669 fprintf(stderr, "SARG: %s (%ld):\n",text[59],nreg);
1670 exit(1);
1671 }
1672
1673 bzero(userfile,nreg);
1674 sprintf(userfile,":");
1675
d6e703cc 1676 while(fgets(buf,255,fp_usr)!=NULL) {
4bcb77cf
FM
1677 if (getword_multisep(user,sizeof(user),buf,':')<0){
1678 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",pwdfile);
1679 exit(1);
1680 }
25697a35
GS
1681 strncat(userfile,user,strlen(user));
1682 strncat(userfile,":",1);
1683 }
1684
1685 fclose(fp_usr);
1686
1687 return;
1688}
1689
1690
1691void gethexclude(char *hexfile, int debug)
1692{
1693
1694 FILE *fp_ex;
1695 char buf[255];
1696 char Msg[255];
1697 unsigned long int nreg=0;
1698
1699 if(debug) {
1700 sprintf(Msg,"%s: %s",text[67],hexfile);
1701 debuga(Msg);
1702 }
1703
1704 if ((fp_ex = fopen(hexfile, "r")) == NULL) {
1705 fprintf(stderr, "SARG: (gethexclude) %s: %s - %s\n",text[45],hexfile,strerror(errno));
1706 exit(1);
1707 }
1708
1709 nreg = lseek(fileno(fp_ex), (off_t)0, SEEK_END);
1710 lseek(fileno(fp_ex), (off_t)0, 0);
1711
1712 if((excludefile=(char *) malloc(nreg+11))==NULL){
1713 fprintf(stderr, "SARG: %s (%ld):\n",text[59],nreg);
1714 exit(1);
1715 }
1716
1717 bzero(excludefile,nreg+11);
1718
1719 while(fgets(buf,255,fp_ex)!=NULL){
1720 if(strstr(buf,"#") != 0)
1721 continue;
1722 buf[strlen(buf)-1]='\0';
1723 strcat(excludefile,buf);
1724 strcat(excludefile," ");
1725 }
1726
d6e703cc 1727 strcat(excludefile,"*END* ");
25697a35
GS
1728
1729 fclose(fp_ex);
1730
1731 return;
1732}
1733
1734
1735void getuexclude(char *uexfile, int debug)
1736{
1737
1738 FILE *fp_ex;
1739 char buf[255];
1740 char Msg[255];
1741 unsigned long int nreg=0;
1742
1743 if(debug) {
1744 sprintf(Msg,"%s: %s",text[67],uexfile);
1745 debuga(Msg);
1746 }
1747
1748 if ((fp_ex = fopen(uexfile, "r")) == NULL) {
1749 fprintf(stderr, "SARG: (gethexclude) %s: %s - %s\n",text[45],uexfile,strerror(errno));
1750 exit(1);
1751 }
1752
1753 nreg = lseek(fileno(fp_ex), (off_t)0, SEEK_END);
1754 lseek(fileno(fp_ex), (off_t)0, 0);
1755
1756 if((excludeuser=(char *) malloc(nreg+11))==NULL){
1757 fprintf(stderr, "SARG: %s (%ld):\n",text[59],nreg);
1758 exit(1);
1759 }
1760
1761 bzero(excludeuser,nreg+11);
1762
1763 while(fgets(buf,255,fp_ex)!=NULL){
1764 if(strstr(buf,"#") != 0)
1765 continue;
1766 buf[strlen(buf)-1]='\0';
1767 strcat(excludeuser,buf);
1768 strcat(excludeuser," ");
1769 }
1770
d6e703cc 1771 strcat(excludeuser,"*END* ");
25697a35
GS
1772
1773 fclose(fp_ex);
1774
1775 return;
1776}