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