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