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