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