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