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