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