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