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