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