]> git.ipfire.org Git - thirdparty/sarg.git/blame - log.c
Rename configure.in as configure.ac
[thirdparty/sarg.git] / log.c
CommitLineData
25697a35 1/*
94ff9470 2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
110ce984 3 * 1998, 2015
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"
27d1fa35 29#include "include/readlog.h"
6068ae56 30#include "include/filelist.h"
25697a35 31
9b179eb0
FM
32#ifdef HAVE_GETOPT_H
33#include <getopt.h>
34#endif
35
27d1fa35
FM
36//! The log file filtering.
37struct ReadLogDataStruct ReadFilter;
f2ec8c75 38
27d1fa35
FM
39//! The list of the system users.
40/*@null@*/char *userfile=NULL;
25697a35 41
6068ae56
FM
42//! List of the input log files to process.
43FileListObject AccessLog=NULL;
44
45
728f6a04
FM
46//! Selected locale set through the environment variable.
47char *CurrentLocale=NULL;
48
936c9905 49static void getusers(const char *pwdfile, int debug);
25697a35 50
25f0793f 51int main(int argc,char *argv[])
25697a35 52{
9bd92830
FM
53 extern int optind;
54 extern int optopt;
55 extern char *optarg;
56
9bd92830
FM
57 char hm_str[15];
58 char uagent[MAXLEN];
59 char hexclude[MAXLEN];
2c7e8c23 60 char splitprefix[MAXLEN];
9bd92830 61 int ch;
9bd92830 62 int errflg=0;
9bd92830 63 bool dns=false;
9bd92830 64 int iarq=0;
9bd92830 65 int lastlog=-1;
27d1fa35 66 int LogStatus;
9bd92830
FM
67 bool realt;
68 bool userip;
0459449f
FM
69 time_t start_time;
70 time_t end_time;
71 time_t read_start_time;
72 time_t read_end_time;
73 time_t process_start_time;
74 time_t process_end_time;
75 double read_elapsed;
76 double process_elapsed;
6068ae56 77 FileListIterator FIter;
9bd92830
FM
78 static int split=0;
79 static int convert=0;
80 static int output_css=0;
0459449f 81 static int show_statis=0;
9bd92830
FM
82 int option_index;
83 static struct option long_options[]=
84 {
85 {"convert",no_argument,&convert,1},
86 {"css",no_argument,&output_css,1},
c6551b37 87 {"help",no_argument,NULL,'h'},
9bd92830
FM
88 {"lastlog",required_argument,NULL,2},
89 {"keeplogs",no_argument,NULL,3},
90 {"split",no_argument,&split,1},
2c7e8c23 91 {"splitprefix",required_argument,NULL,'P'},
0459449f 92 {"statistics",no_argument,&show_statis,1},
4f22e788 93 {"version",no_argument,NULL,'V'},
9bd92830
FM
94 {0,0,0,0}
95 };
25697a35 96
0459449f
FM
97 start_time=time(NULL);
98
abfc169e 99#ifdef HAVE_LOCALE_H
9bd92830 100 setlocale(LC_TIME,"");
abfc169e
FM
101#endif
102
cec3eed3 103#if defined(ENABLE_NLS) && defined(HAVE_LOCALE_H)
728f6a04
FM
104 CurrentLocale=setlocale (LC_ALL, "");
105 if (!CurrentLocale) {
9bd92830
FM
106 fprintf(stderr,"SARG: Cannot set the locale LC_ALL to the environment variable\n");
107 exit(EXIT_FAILURE);
108 }
109 if (!bindtextdomain (PACKAGE_NAME, LOCALEDIR)) {
110 fprintf(stderr,"SARG: Cannot bind to text domain %s in directory %s (%s)\n",PACKAGE_NAME,LOCALEDIR,strerror(errno));
111 exit(EXIT_FAILURE);
112 }
113 if (!textdomain (PACKAGE_NAME)) {
114 fprintf(stderr,"SARG: Cannot set gettext domain for %s PACKAGE_NAME (%s)\n",PACKAGE_NAME,strerror(errno));
115 exit(EXIT_FAILURE);
116 }
abfc169e
FM
117#endif //ENABLE_NLS
118
9bd92830
FM
119 BgImage[0]='\0';
120 LogoImage[0]='\0';
121 LogoText[0]='\0';
122 PasswdFile[0]='\0';
123 OutputEmail[0]='\0';
124 UserAgentLog[0]='\0';
125 ExcludeHosts[0]='\0';
126 ExcludeUsers[0]='\0';
127 ConfigFile[0]='\0';
128 code[0]='\0';
129 LastLog=0;
130 ReportType=0UL;
131 UserTabFile[0]='\0';
132 BlockIt[0]='\0';
133 ExternalCSSFile[0]='\0';
134 RedirectorLogFormat[0]='\0';
135 NRedirectorLogs=0;
9bd92830 136
8d811f76 137 snprintf(ExcludeCodes,sizeof(ExcludeCodes),"%s/exclude_codes",SYSCONFDIR);
9bd92830
FM
138 strcpy(GraphDaysBytesBarColor,"orange");
139 strcpy(BgColor,"#ffffff");
140 strcpy(TxColor,"#000000");
141 strcpy(TxBgColor,"lavender");
142 strcpy(TiColor,"darkblue");
143 strcpy(Width,"80");
144 strcpy(Height,"45");
145 strcpy(LogoTextColor,"#000000");
146 strcpy(HeaderColor,"darkblue");
147 strcpy(HeaderBgColor,"#dddddd");
148 strcpy(LogoTextColor,"#006699");
149 strcpy(FontSize,"9px");
150 strcpy(TempDir,"/tmp");
151 strcpy(OutputDir,"/var/www/html/squid-reports");
829a53c2 152 AnonymousOutputFiles=false;
9bd92830 153 Ip2Name=false;
81a022d8 154 DateFormat='u';
9bd92830
FM
155 OverwriteReport=false;
156 RemoveTempFiles=true;
56b97279 157 strcpy(ReplaceIndex,INDEX_HTML_FILE);
9bd92830
FM
158 Index=INDEX_YES;
159 RecordsWithoutUser=RECORDWITHOUTUSER_IP;
160 UseComma=0;
161 strcpy(MailUtility,"mailx");
162 TopSitesNum=100;
163 TopUsersNum=0;
164 UserIp=0;
165 TopuserSort=TOPUSER_SORT_BYTES | TOPUSER_SORT_REVERSE;
166 UserSort=USER_SORT_BYTES | USER_SORT_REVERSE;
167 TopsitesSort=TOPSITE_SORT_CONNECT | TOPSITE_SORT_REVERSE;
168 LongUrl=0;
169 strcpy(FontFace,"Verdana,Tahoma,Arial");
170 datetimeby=DATETIME_BYTE;
171 strcpy(CharSet,"ISO-8859-1");
172 Privacy=0;
173 strcpy(PrivacyString,"***.***.***.***");
174 strcpy(PrivacyStringColor,"blue");
175 SuccessfulMsg=true;
176 TopUserFields=TOPUSERFIELDS_NUM | TOPUSERFIELDS_DATE_TIME | TOPUSERFIELDS_USERID | TOPUSERFIELDS_CONNECT |
007905af
FM
177 TOPUSERFIELDS_BYTES | TOPUSERFIELDS_SETYB | TOPUSERFIELDS_IN_CACHE_OUT |
178 TOPUSERFIELDS_USED_TIME | TOPUSERFIELDS_MILISEC | TOPUSERFIELDS_PTIME |
179 TOPUSERFIELDS_TOTAL | TOPUSERFIELDS_AVERAGE;
9bd92830 180 UserReportFields=USERREPORTFIELDS_CONNECT | USERREPORTFIELDS_BYTES | USERREPORTFIELDS_SETYB |
007905af
FM
181 USERREPORTFIELDS_IN_CACHE_OUT | USERREPORTFIELDS_USED_TIME | USERREPORTFIELDS_MILISEC |
182 USERREPORTFIELDS_PTIME | USERREPORTFIELDS_TOTAL | USERREPORTFIELDS_AVERAGE;
9bd92830
FM
183 strcpy(DataFileDelimiter,";");
184 DataFileFields=DATA_FIELD_USER | DATA_FIELD_DATE | DATA_FIELD_TIME | DATA_FIELD_URL | DATA_FIELD_CONNECT |
007905af 185 DATA_FIELD_BYTES | DATA_FIELD_IN_CACHE | DATA_FIELD_OUT_CACHE | DATA_FIELD_ELAPSED;
9bd92830 186 ShowReadStatistics=true;
2f4787e6 187 ShowReadPercent=false;
9bd92830
FM
188 strcpy(IndexSortOrder,"D");
189 ShowSargInfo=true;
190 ShowSargLogo=true;
8949e757 191 ParsedOutputLog[0]='\0';
9bd92830
FM
192 strcpy(ParsedOutputLogCompress,"/bin/gzip -f");
193 DisplayedValues=DISPLAY_ABBREV;
194 strcpy(HeaderFontSize,"9px");
195 strcpy(TitleFontSize,"11px");
196 strcpy(AuthUserTemplateFile,"sarg_htaccess");
197 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");
198 Graphs=true;
3becf85c 199#if defined(FONTDIR)
9bd92830 200 strcpy(GraphFont,FONTDIR"/DejaVuSans.ttf");
3becf85c 201#else
9bd92830 202 GraphFont[0]='\0';
3becf85c 203#endif
9bd92830
FM
204 strcpy(Ulimit,"20000");
205 NtlmUserFormat=NTLMUSERFORMAT_DOMAINUSER;
206 IndexTree=INDEX_TREE_FILE;
9aaa3361 207 IndexFields=INDEXFIELDS_DIRSIZE;
193b9516 208 strcpy(RealtimeTypes,"GET,PUT,CONNECT,POST");
9bd92830
FM
209 RealtimeUnauthRec=REALTIME_UNAUTH_REC_SHOW;
210 RedirectorFilterOutDate=true;
211 DansguardianFilterOutDate=true;
212 DataFileUrl=DATAFILEURL_IP;
213 strcpy(MaxElapsed,"28800000");
214 BytesInSitesUsersReport=0;
215 UserAuthentication=0;
216 strcpy(LDAPHost,"127.0.0.1");
217 LDAPPort=389;
218 LDAPProtocolVersion=3;
219 LDAPBindDN[0]='\0';
220 LDAPBindPW[0]='\0';
221 LDAPBaseSearch[0]='\0';
222 strcpy(LDAPFilterSearch, "(uid=%s)");
223 strcpy(LDAPTargetAttr, "cn");
fd75cd90 224 LDAPNativeCharset[0]='\0';
9bd92830
FM
225 SortTableJs[0]='\0';
226
9bd92830 227 tmp[0]='\0';
9bd92830 228 us[0]='\0';
27d1fa35 229 ReadFilter.DateRange[0]='\0';
81a022d8 230 df='\0';
9bd92830
FM
231 uagent[0]='\0';
232 hexclude[0]='\0';
233 addr[0]='\0';
27d1fa35
FM
234 ReadFilter.StartTime=-1;
235 ReadFilter.EndTime=-1;
9bd92830
FM
236 site[0]='\0';
237 outdir[0]='\0';
2c7e8c23 238 splitprefix[0]='\0';
9bd92830 239 email[0]='\0';
9bd92830
FM
240 UserInvalidChar[0]='\0';
241 DataFile[0]='\0';
242 SquidGuardConf[0]='\0';
243 DansGuardianConf[0]='\0';
9bd92830 244 hm_str[0]='\0';
22715352 245 HostAliasFile[0]='\0';
c4f0ea8f 246 UserAliasFile[0]='\0';
9bd92830 247
9bd92830 248 dansguardian_count=0;
330b1c52 249 redirector_count=0;
9bd92830
FM
250 useragent_count=0;
251 DeniedReportLimit=10;
252 AuthfailReportLimit=10;
253 DansGuardianReportLimit=10;
254 SquidGuardReportLimit=10;
255 DownloadReportLimit=50;
256 UserReportLimit=0;
257 debug=0;
258 debugz=0;
9bd92830
FM
259 iprel=false;
260 userip=false;
261 realt=false;
262 realtime_refresh=3;
263 realtime_access_log_lines=1000;
264 cost=0.01;
265 nocost=50000000;
9bd92830
FM
266 squid24=false;
267 dfrom=0;
268 duntil=0;
11767c6a 269 KeepTempLog=false;
4d62cb0a
FM
270 NumLogSuccessiveErrors=3;
271 NumLogTotalErrors=50;
0459449f
FM
272 lines_read=0UL;
273 records_kept=0UL;
274 nusers=0UL;
9bd92830 275
9c1a94f6
FM
276 memset(IncludeUsers,0,sizeof(IncludeUsers));
277 memset(ExcludeString,0,sizeof(ExcludeString));
9bd92830
FM
278 memset(&period,0,sizeof(period));
279
9bd92830
FM
280 AccessLogFromCmdLine=0;
281 RedirectorLogFromCmdLine=0;
282
283 strcpy(Title,_("Squid User Access Report"));
284
4246ae8d 285 while((ch = getopt_long(argc, argv, "a:b:c:d:e:f:g:hikl:L:no:P:prs:t:u:Vw:xyz",long_options,&option_index)) != -1){
9bd92830
FM
286 switch(ch)
287 {
288 case 0:
289 break;
290 case 2:
291 lastlog=atoi(optarg);
292 break;
293 case 3:
294 lastlog=0;
295 break;
296 case 'a':
a87d4d11 297 safe_strcpy(addr,optarg,sizeof(addr));
9bd92830 298 break;
d6f08599 299 case 'b': //unused option
a87d4d11 300 safe_strcpy(uagent,optarg,sizeof(uagent));
9bd92830
FM
301 break;
302 case 'c':
a87d4d11 303 safe_strcpy(hexclude,optarg,sizeof(hexclude));
9bd92830
FM
304 break;
305 case 'd':
27d1fa35 306 safe_strcpy(ReadFilter.DateRange,optarg,sizeof(ReadFilter.DateRange));
60ec7f09 307 date_from(ReadFilter.DateRange,sizeof(ReadFilter.DateRange), &dfrom, &duntil);
9bd92830
FM
308 break;
309 case 'e':
a87d4d11 310 safe_strcpy(email,optarg,sizeof(email));
9bd92830
FM
311 break;
312 case 'f':
a87d4d11 313 safe_strcpy(ConfigFile,optarg,sizeof(ConfigFile));
9bd92830
FM
314 break;
315 case 'g':
81a022d8 316 df=*optarg;
9bd92830
FM
317 break;
318 case 'h':
319 usage(argv[0]);
320 exit(EXIT_SUCCESS);
321 case 'i':
322 iprel=true;
323 break;
11767c6a
FM
324 case 'k':
325 KeepTempLog=true;
326 break;
9bd92830 327 case 'l':
6068ae56
FM
328 if (!AccessLog)
329 AccessLog=FileList_Create();
330 if (!FileList_AddFile(AccessLog,optarg)) {
af961877 331 debuga(__FILE__,__LINE__,_("Not enough memory to store the input log file names\n"));
9bd92830
FM
332 exit(EXIT_FAILURE);
333 }
9bd92830
FM
334 AccessLogFromCmdLine++;
335 break;
336 case 'L':
337 if (NRedirectorLogs>MAX_REDIRECTOR_LOGS) {
af961877 338 debuga(__FILE__,__LINE__,_("Too many redirector logs passed on command line with option -L.\n"));
9bd92830
FM
339 exit(EXIT_FAILURE);
340 }
341 if (strlen(optarg)>=MAX_REDIRECTOR_FILELEN) {
af961877 342 debuga(__FILE__,__LINE__,_("Redirector log file name too long passed on command line with opton -L: %s\n"),optarg);
9bd92830
FM
343 exit(EXIT_FAILURE);
344 }
345 strcpy(RedirectorLogs[NRedirectorLogs],optarg);
346 NRedirectorLogs++;
347 RedirectorLogFromCmdLine++;
348 break;
9bd92830
FM
349 case 'n':
350 dns=true;
351 break;
352 case 'o':
a87d4d11 353 safe_strcpy(outdir,optarg,sizeof(outdir));
9bd92830
FM
354 break;
355 case 'p':
356 userip=true;
357 break;
2c7e8c23 358 case 'P':
a87d4d11 359 safe_strcpy(splitprefix,optarg,sizeof(splitprefix));
2c7e8c23 360 break;
9bd92830
FM
361 case 'r':
362 realt=true;
363 break;
364 case 's':
a87d4d11 365 safe_strcpy(site,optarg,sizeof(site));
9bd92830
FM
366 break;
367 case 't':
368 {
369 int h1,m1,h2,m2;
370
371 if(strstr(optarg,"-") == 0) {
372 if(sscanf(optarg,"%d:%d",&h1,&m1)!=2) {
af961877 373 debuga(__FILE__,__LINE__,_("Time period passed on the command line with option -t must be HH:MM\n"));
9bd92830
FM
374 exit(EXIT_FAILURE);
375 }
27d1fa35
FM
376 ReadFilter.StartTime=h1*100+m1;
377 ReadFilter.EndTime=ReadFilter.StartTime;
9bd92830
FM
378 snprintf(hm_str,sizeof(hm_str),"%02d:%02d",h1,m1);
379 } else {
380 if(sscanf(optarg,"%d:%d-%d:%d",&h1,&m1,&h2,&m2)!=4) {
af961877 381 debuga(__FILE__,__LINE__,_("Time range passed on the command line with option -t must be HH:MM-HH:MM\n"));
9bd92830
FM
382 exit(EXIT_FAILURE);
383 }
27d1fa35
FM
384 ReadFilter.StartTime=h1*100+m1;
385 ReadFilter.EndTime=h2*100+m2;
9bd92830
FM
386 snprintf(hm_str,sizeof(hm_str),"%02d:%02d-%02d:%02d",h1,m1,h2,m2);
387 }
388 break;
389 }
390 case 'u':
a87d4d11 391 safe_strcpy(us,optarg,sizeof(us));
9bd92830 392 break;
4f22e788 393 case 'V':
9bd92830
FM
394 version();
395 break;
396 case 'w':
a87d4d11 397 safe_strcpy(tmp,optarg,sizeof(tmp));
9bd92830
FM
398 break;
399 case 'x':
400 debug++;
401 break;
d6f08599 402 case 'y': //unused option
9bd92830
FM
403 langcode++;
404 break;
405 case 'z':
406 debugz++;
407 break;
007905af 408 case ':':
af961877 409 debuga(__FILE__,__LINE__,_("Option -%c requires an argument\n"),optopt);
007905af 410 exit(EXIT_FAILURE);
9bd92830
FM
411 case '?':
412 usage(argv[0]);
413 exit(EXIT_FAILURE);
414 default:
415 abort();
416 }
417 }
418
419 if (errflg>0) {
420 usage(argv[0]);
421 exit(2);
422 }
423
6068ae56
FM
424 if(output_css) {
425 css_content(stdout);
426 exit(EXIT_SUCCESS);
427 }
428
9bd92830 429 if (optind<argc) {
6068ae56
FM
430 if (!AccessLog)
431 AccessLog=FileList_Create();
9bd92830 432 for (iarq=optind ; iarq<argc ; iarq++) {
6068ae56 433 if (!FileList_AddFile(AccessLog,argv[iarq])) {
af961877 434 debuga(__FILE__,__LINE__,_("Not enough memory to store the input log file names\n"));
9bd92830
FM
435 exit(EXIT_FAILURE);
436 }
9bd92830
FM
437 AccessLogFromCmdLine++;
438 }
439 }
440
af961877 441 if(debug) debuga(__FILE__,__LINE__,_("Init\n"));
9bd92830
FM
442
443 if(ConfigFile[0] == '\0') snprintf(ConfigFile,sizeof(ConfigFile),"%s/sarg.conf",SYSCONFDIR);
444 if(access(ConfigFile, R_OK) != 0) {
af961877 445 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),ConfigFile,strerror(errno));
9bd92830
FM
446 exit(EXIT_FAILURE);
447 }
448
449 if(access(ConfigFile, R_OK) == 0)
450 getconf();
451
452 if(userip) UserIp=true;
453
51b166d4 454 if(dns) ip2name_forcedns();
9bd92830
FM
455
456 if (lastlog>=0) LastLog=lastlog;
457
2c7e8c23
FM
458 if(outdir[0] == '\0') strcpy(outdir,OutputDir);
459 if(outdir[0] != '\0') strcat(outdir,"/");
460
9bd92830
FM
461 if(IndexTree == INDEX_TREE_FILE)
462 strcpy(ImageFile,"../images");
463 else
464 strcpy(ImageFile,"../../../images");
465
11284535 466 dataonly=(DataFile[0] != '\0');
9bd92830 467
81a022d8
FM
468 if (df=='\0') df=DateFormat;
469 if (df=='\0') df='u';
470 if (df=='w')
3e80818c
FM
471 IndexTree=INDEX_TREE_FILE;
472
6068ae56
FM
473 if(AccessLog==NULL) {
474 AccessLog=FileList_Create();
475 if (!FileList_AddFile(AccessLog,"/var/log/squid/access.log")) {
af961877 476 debuga(__FILE__,__LINE__,_("Not enough memory to store the input log file names\n"));
6068ae56
FM
477 exit(EXIT_FAILURE);
478 }
9bd92830
FM
479 }
480
8b4e9578
FM
481 if(realt) {
482 realtime();
483 exit(EXIT_SUCCESS);
484 }
9bd92830 485 if(split) {
6068ae56
FM
486 const char *file;
487
488 FIter=FileListIter_Open(AccessLog);
489 while ((file=FileListIter_Next(FIter))!=NULL)
490 splitlog(file, df, dfrom, duntil, convert, splitprefix);
491 FileListIter_Close(FIter);
9bd92830
FM
492 exit(EXIT_SUCCESS);
493 }
494 if(convert) {
6068ae56
FM
495 const char *file;
496
497 FIter=FileListIter_Open(AccessLog);
498 while ((file=FileListIter_Next(FIter))!=NULL)
499 convlog(file, df, dfrom, duntil);
500 FileListIter_Close(FIter);
9bd92830
FM
501 exit(EXIT_SUCCESS);
502 }
503
504 load_excludecodes(ExcludeCodes);
505
506 if(access(PasswdFile, R_OK) == 0) {
507 getusers(PasswdFile,debug);
27d1fa35
FM
508 ReadFilter.SysUsers=true;
509 } else {
510 ReadFilter.SysUsers=false;
9bd92830
FM
511 }
512
513 if(hexclude[0] == '\0')
514 strcpy(hexclude,ExcludeHosts);
515 if(hexclude[0] != '\0') {
516 gethexclude(hexclude,debug);
27d1fa35
FM
517 ReadFilter.HostFilter=true;
518 } else {
519 ReadFilter.HostFilter=false;
9bd92830
FM
520 }
521
522 if(ReportType == 0) {
523 ReportType=REPORT_TYPE_TOPUSERS | REPORT_TYPE_TOPSITES | REPORT_TYPE_USERS_SITES |
007905af
FM
524 REPORT_TYPE_SITES_USERS | REPORT_TYPE_DATE_TIME | REPORT_TYPE_DENIED |
525 REPORT_TYPE_AUTH_FAILURES | REPORT_TYPE_SITE_USER_TIME_DATE | REPORT_TYPE_DOWNLOADS;
9bd92830
FM
526 }
527
528 if(access(ExcludeUsers, R_OK) == 0) {
529 getuexclude(ExcludeUsers,debug);
27d1fa35
FM
530 ReadFilter.UserFilter=true;
531 } else {
532 ReadFilter.UserFilter=false;
9bd92830 533 }
22715352
FM
534 if (HostAliasFile[0] != '\0')
535 read_hostalias(HostAliasFile);
c4f0ea8f
FM
536 if (UserAliasFile[0] != '\0')
537 read_useralias(UserAliasFile);
9bd92830 538
6588b137 539 indexonly=false;
27d1fa35 540 if(ReadFilter.UserFilter) {
9bd92830 541 if(is_indexonly())
6588b137 542 indexonly=true;
9bd92830 543 }
6588b137
FM
544 if(strcmp(ExcludeUsers,"indexonly") == 0) indexonly=true;
545 if(Index == INDEX_ONLY) indexonly=true;
9bd92830 546
27d1fa35
FM
547 if(MaxElapsed[0] != '\0')
548 ReadFilter.max_elapsed=atol(MaxElapsed);
549 else
550 ReadFilter.max_elapsed=0;
9bd92830 551
4b64c316 552 if(uagent[0] != '\0') strcpy(UserAgentLog,uagent);
9bd92830
FM
553
554 if(tmp[0] == '\0') strcpy(tmp,TempDir);
555 else strcpy(TempDir,tmp);
556 /*
557 For historical reasons, the temporary directory is the subdirectory "sarg" of the path
558 provided by the user.
559 */
560 strcat(tmp,"/sarg");
561
562 if (tmp[0]!='\0' && strncmp(outdir,tmp,strlen(tmp))==0) {
af961877 563 debuga(__FILE__,__LINE__,_("The output directory \"%s\" must be outside of the temporary directory \"%s\"\n"),outdir,tmp);
9bd92830
FM
564 exit(EXIT_FAILURE);
565 }
566
9bd92830
FM
567 if(email[0] == '\0' && OutputEmail[0] != '\0') strcpy(email,OutputEmail);
568
569 if(email[0] != '\0') {
570 my_mkdir(tmp);
571 strcpy(outdir,tmp);
572 strcat(outdir,"/");
573 }
574
575 if(access(tmp, R_OK) == 0) {
af961877 576 if (debug) debuga(__FILE__,__LINE__,_("Deleting temporary directory \"%s\"\n"),tmp);
170a77ea 577 emptytmpdir(tmp);
9bd92830
FM
578 }
579 my_mkdir(tmp);
9bd92830
FM
580
581 if(debug) {
6068ae56
FM
582 const char *file;
583
af961877
FM
584 debuga(__FILE__,__LINE__,_("Parameters:\n"));
585 debuga(__FILE__,__LINE__,_(" Hostname or IP address (-a) = %s\n"),addr);
586 debuga(__FILE__,__LINE__,_(" Useragent log (-b) = %s\n"),uagent);
587 debuga(__FILE__,__LINE__,_(" Exclude file (-c) = %s\n"),hexclude);
588 debuga(__FILE__,__LINE__,_(" Date from-until (-d) = %s\n"),ReadFilter.DateRange);
589 debuga(__FILE__,__LINE__,_(" Email address to send reports (-e) = %s\n"),email);
590 debuga(__FILE__,__LINE__,_(" Config file (-f) = %s\n"),ConfigFile);
81a022d8 591 if (df=='e')
af961877 592 debuga(__FILE__,__LINE__,_(" Date format (-g) = Europe (dd/mm/yyyy)\n"));
81a022d8 593 else if (df=='u')
af961877 594 debuga(__FILE__,__LINE__,_(" Date format (-g) = USA (mm/dd/yyyy)\n"));
81a022d8 595 else if (df=='w')
af961877
FM
596 debuga(__FILE__,__LINE__,_(" Date format (-g) = Sites & Users (yyyy/ww)\n"));
597 debuga(__FILE__,__LINE__,_(" IP report (-i) = %s\n"),(iprel) ? _("Yes") : _("No"));
598 debuga(__FILE__,__LINE__,_(" Keep temporary files (-k) = %s\n"),(KeepTempLog) ? _("Yes") : _("No"));
6068ae56
FM
599 FIter=FileListIter_Open(AccessLog);
600 while ((file=FileListIter_NextWithMask(FIter))!=NULL)
af961877 601 debuga(__FILE__,__LINE__,_(" Input log (-l) = %s\n"),file);
6068ae56 602 FileListIter_Close(FIter);
9bd92830 603 for (iarq=0 ; iarq<NRedirectorLogs ; iarq++)
af961877
FM
604 debuga(__FILE__,__LINE__,_(" Redirector log (-L) = %s\n"),RedirectorLogs[iarq]);
605 debuga(__FILE__,__LINE__,_(" Resolve IP Address (-n) = %s\n"),(Ip2Name) ? _("Yes") : _("No"));
606 debuga(__FILE__,__LINE__,_(" Output dir (-o) = %s\n"),outdir);
607 debuga(__FILE__,__LINE__,_("Use Ip Address instead of userid (-p) = %s\n"),(UserIp) ? _("Yes") : _("No"));
608 debuga(__FILE__,__LINE__,_(" Accessed site (-s) = %s\n"),site);
609 debuga(__FILE__,__LINE__,_(" Time (-t) = %s\n"),hm_str);
610 debuga(__FILE__,__LINE__,_(" User (-u) = %s\n"),us);
611 debuga(__FILE__,__LINE__,_(" Temporary dir (-w) = %s\n"),tmp);
612 debuga(__FILE__,__LINE__,_(" Debug messages (-x) = %s\n"),(debug) ? _("Yes") : _("No"));
613 debuga(__FILE__,__LINE__,_(" Process messages (-z) = %d\n"),debugz);
614 debuga(__FILE__,__LINE__,_(" Previous reports to keep (--lastlog) = %d\n"),LastLog);
615 debuga(__FILE__,__LINE__,"\n");
9bd92830
FM
616 }
617
9bd92830 618 if(debug)
af961877 619 debuga(__FILE__,__LINE__,_("sarg version: %s\n"),VERSION);
25697a35 620
04a01ed3 621#ifdef ENABLE_DOUBLE_CHECK_DATA
af961877 622 debuga(__FILE__,__LINE__,_("Sarg compiled to report warnings if the output is inconsistent\n"));
04a01ed3
FM
623#endif
624
e112fa1f 625#ifdef HAVE_RLIM_T
9bd92830
FM
626 if (Ulimit[0] != '\0') {
627 struct rlimit rl;
628 long l1, l2;
629 int rc=0;
243a9fab 630
25697a35 631#if defined(RLIMIT_NOFILE)
9bd92830 632 getrlimit (RLIMIT_NOFILE, &rl);
ed1ac52f 633#elif defined(RLIMIT_OFILE)
9bd92830 634 getrlimit (RLIMIT_OFILE, &rl);
243a9fab
FM
635#else
636#warning "No rlimit resource for the number of open files"
25697a35 637#endif
9bd92830
FM
638 l1 = rl.rlim_cur;
639 l2 = rl.rlim_max;
d6e703cc 640
9bd92830
FM
641 rl.rlim_cur = atol(Ulimit);
642 rl.rlim_max = atol(Ulimit);
243a9fab 643#if defined(RLIMIT_NOFILE)
9bd92830 644 rc=setrlimit (RLIMIT_NOFILE, &rl);
243a9fab 645#elif defined(RLIMIT_OFILE)
9bd92830 646 rc=setrlimit (RLIMIT_OFILE, &rl);
243a9fab
FM
647#else
648#warning "No rlimit resource for the number of open files"
649#endif
9bd92830 650 if(rc == -1) {
96dadc9f 651 debuga(_("setrlimit error: %s\n"),strerror(errno));
9bd92830 652 }
25697a35 653
9bd92830 654 if(debug)
c4633554 655 debuga("Maximum file descriptor: cur=%ld max=%ld, changed to cur="RLIM_STRING" max="RLIM_STRING"\n",l1,l2,rl.rlim_cur,rl.rlim_max);
9bd92830 656 }
e112fa1f 657#endif
25697a35 658
9bd92830
FM
659 init_usertab(UserTabFile);
660
0459449f 661 read_start_time=time(NULL);
27d1fa35 662 LogStatus=ReadLogFile(&ReadFilter);
0459449f
FM
663 read_end_time=time(NULL);
664 read_elapsed=(double)read_end_time-(double)read_start_time;
9bd92830 665
6068ae56 666 FileList_Destroy(&AccessLog);
9bd92830
FM
667 free_download();
668 free_excludecodes();
669 free_exclude();
670
c4a702b4
FM
671 if (debug) {
672 char date0[30], date1[30];
673 struct tm Start,End;
674
675 GetLogPeriod(&Start,&End);
676 strftime(date0,sizeof(date0),"%x",&Start);
677 strftime(date1,sizeof(date1),"%x",&End);
678 // TRANSLATORS: The %s are the start and end dates in locale format.
af961877 679 debuga(__FILE__,__LINE__,_("Period covered by log files: %s-%s\n"),date0,date1);
c4a702b4
FM
680 }
681
27d1fa35 682 if (!LogStatus){
af961877
FM
683 debuga(__FILE__,__LINE__,_("No records found\n"));
684 debuga(__FILE__,__LINE__,_("End\n"));
9bd92830
FM
685 userinfo_free();
686 if(userfile) free(userfile);
687 close_usertab();
688 exit(EXIT_SUCCESS);
689 }
690
6a943fc1 691 if (debug) {
9bd92830
FM
692 char date0[30], date1[30];
693
6a943fc1
FM
694 strftime(date0,sizeof(date0),"%x",&period.start);
695 strftime(date1,sizeof(date1),"%x",&period.end);
696 // TRANSLATORS: The %s are the start and end dates in locale format.
af961877 697 debuga(__FILE__,__LINE__,_("Period extracted from log files: %s-%s\n"),date0,date1);
5cbbe9a4
FM
698 }
699 if (ReadFilter.DateRange[0] != '\0') {
9bd92830
FM
700 getperiod_fromrange(&period,dfrom,duntil);
701 }
702 if (getperiod_buildtext(&period)<0) {
af961877 703 debuga(__FILE__,__LINE__,_("Failed to build the string representation of the date range\n"));
9bd92830
FM
704 exit(EXIT_FAILURE);
705 }
706
0459449f 707 process_start_time=time(NULL);
9bd92830
FM
708 if(DataFile[0] != '\0')
709 data_file(tmp);
710 else
711 gerarel();
0459449f
FM
712 process_end_time=time(NULL);
713 process_elapsed=(double)process_end_time-(double)process_start_time;
9bd92830 714
60b48ae5 715 denied_cleanup();
16b013cc 716 authfail_cleanup();
11284535 717 download_cleanup();
9bd92830 718
11767c6a 719 if(!KeepTempLog && strcmp(tmp,"/tmp") != 0) {
9bd92830
FM
720 unlinkdir(tmp,0);
721 }
722
0326d73b 723 ip2name_cleanup();
0a6722a1 724 free_hostalias();
c4f0ea8f 725 free_useralias();
9bd92830
FM
726 userinfo_free();
727 if(userfile)
728 free(userfile);
729 close_usertab();
730
0459449f
FM
731 end_time=time(NULL);
732
733 if (show_statis) {
734 double elapsed=(double)end_time-(double)start_time;
af961877 735 debuga(__FILE__,__LINE__,_("Total execution time: %.0lf seconds\n"),elapsed);
0459449f 736 if (read_elapsed>0.) {
af961877 737 debuga(__FILE__,__LINE__,_("Lines read: %lu lines in %.0lf seconds (%.0lf lines/s)\n"),lines_read,read_elapsed,(double)lines_read/read_elapsed);
0459449f
FM
738 }
739 if (process_elapsed>0.) {
af961877
FM
740 debuga(__FILE__,__LINE__,_("Processed records: %lu records in %.0lf seconds (%.0lf records/s)\n"),records_kept,process_elapsed,(double)records_kept/process_elapsed);
741 debuga(__FILE__,__LINE__,_("Users: %lu users in %.0lf seconds (%.0lf users/s)\n"),nusers,process_elapsed,(double)nusers/process_elapsed);
0459449f
FM
742 }
743 }
744
9bd92830 745 if(debug)
af961877 746 debuga(__FILE__,__LINE__,_("End\n"));
9bd92830
FM
747
748 exit(EXIT_SUCCESS);
25697a35
GS
749}
750
751
936c9905 752static void getusers(const char *pwdfile, int debug)
25697a35 753{
9bd92830
FM
754 FILE *fp_usr;
755 char buf[255];
756 char *str;
757 long int nreg=0;
758
759 if(debug)
af961877 760 debuga(__FILE__,__LINE__,_("Loading password file \"%s\"\n"),pwdfile);
9bd92830
FM
761
762 if ((fp_usr = fopen(pwdfile, "r")) == NULL) {
af961877 763 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),pwdfile,strerror(errno));
9bd92830
FM
764 exit(EXIT_FAILURE);
765 }
766
767 if (fseek(fp_usr, 0, SEEK_END)==-1) {
af961877 768 debuga(__FILE__,__LINE__,_("Failed to move till the end of file \"%s\": %s\n"),pwdfile,strerror(errno));
9bd92830
FM
769 exit(EXIT_FAILURE);
770 }
771 nreg = ftell(fp_usr);
772 if (nreg<0) {
af961877 773 debuga(__FILE__,__LINE__,_("Cannot get the size of file \"%s\"\n"),pwdfile);
9bd92830
FM
774 exit(EXIT_FAILURE);
775 }
776 nreg = nreg+5000;
777 if (fseek(fp_usr, 0, SEEK_SET)==-1) {
af961877 778 debuga(__FILE__,__LINE__,_("Failed to rewind file \"%s\": %s\n"),pwdfile,strerror(errno));
9bd92830
FM
779 exit(EXIT_FAILURE);
780 }
781
782 if((userfile=(char *) malloc(nreg))==NULL){
af961877 783 debuga(__FILE__,__LINE__,_("malloc error (%ld bytes required)\n"),nreg);
9bd92830
FM
784 exit(EXIT_FAILURE);
785 }
786
9c1a94f6 787 memset(userfile,0,nreg);
9bd92830
FM
788 strcpy(userfile,":");
789
790 while(fgets(buf,sizeof(buf),fp_usr)!=NULL) {
791 str=strchr(buf,':');
792 if (!str) {
af961877 793 debuga(__FILE__,__LINE__,_("Invalid user in file \"%s\"\n"),pwdfile);
9bd92830
FM
794 exit(EXIT_FAILURE);
795 }
796 str[1]='\0';
797 strcat(userfile,buf);
798 }
799
204781f4 800 if (fclose(fp_usr)==EOF) {
af961877 801 debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),pwdfile,strerror(errno));
204781f4
FM
802 exit(EXIT_FAILURE);
803 }
9bd92830
FM
804
805 return;
25697a35 806}