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