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