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